Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
| 2 | /* |
| 3 | * Copyright (C) 2020, STMicroelectronics - All Rights Reserved |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <env.h> |
| 9 | #include <env_internal.h> |
| 10 | #include <mtd.h> |
| 11 | #include <mtd_node.h> |
Patrick Delaunay | 43df0a1 | 2020-03-18 09:22:49 +0100 | [diff] [blame^] | 12 | #include <tee.h> |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 13 | |
| 14 | #define MTDPARTS_LEN 256 |
| 15 | #define MTDIDS_LEN 128 |
| 16 | |
| 17 | /* |
| 18 | * Get a global data pointer |
| 19 | */ |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | /** |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 23 | * update the variables "mtdids" and "mtdparts" with boot, tee and user strings |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 24 | */ |
| 25 | static void board_get_mtdparts(const char *dev, |
| 26 | char *mtdids, |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 27 | char *mtdparts, |
| 28 | const char *boot, |
| 29 | const char *tee, |
| 30 | const char *user) |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 31 | { |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 32 | /* mtdids: "<dev>=<dev>, ...." */ |
| 33 | if (mtdids[0] != '\0') |
| 34 | strcat(mtdids, ","); |
| 35 | strcat(mtdids, dev); |
| 36 | strcat(mtdids, "="); |
| 37 | strcat(mtdids, dev); |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 38 | |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 39 | /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */ |
| 40 | if (mtdparts[0] != '\0') |
| 41 | strncat(mtdparts, ";", MTDPARTS_LEN); |
| 42 | else |
| 43 | strcat(mtdparts, "mtdparts="); |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 44 | |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 45 | strncat(mtdparts, dev, MTDPARTS_LEN); |
| 46 | strncat(mtdparts, ":", MTDPARTS_LEN); |
| 47 | |
| 48 | if (boot) { |
| 49 | strncat(mtdparts, boot, MTDPARTS_LEN); |
| 50 | strncat(mtdparts, ",", MTDPARTS_LEN); |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 51 | } |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 52 | |
Patrick Delaunay | 43df0a1 | 2020-03-18 09:22:49 +0100 | [diff] [blame^] | 53 | if (tee) { |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 54 | strncat(mtdparts, tee, MTDPARTS_LEN); |
| 55 | strncat(mtdparts, ",", MTDPARTS_LEN); |
| 56 | } |
| 57 | |
| 58 | strncat(mtdparts, user, MTDPARTS_LEN); |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void board_mtdparts_default(const char **mtdids, const char **mtdparts) |
| 62 | { |
| 63 | struct mtd_info *mtd; |
| 64 | struct udevice *dev; |
| 65 | static char parts[3 * MTDPARTS_LEN + 1]; |
| 66 | static char ids[MTDIDS_LEN + 1]; |
| 67 | static bool mtd_initialized; |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 68 | bool tee = false; |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 69 | |
| 70 | if (mtd_initialized) { |
| 71 | *mtdids = ids; |
| 72 | *mtdparts = parts; |
| 73 | return; |
| 74 | } |
| 75 | |
Patrick Delaunay | 43df0a1 | 2020-03-18 09:22:49 +0100 | [diff] [blame^] | 76 | if (CONFIG_IS_ENABLED(OPTEE) && |
| 77 | tee_find_device(NULL, NULL, NULL, NULL)) |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 78 | tee = true; |
| 79 | |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 80 | memset(parts, 0, sizeof(parts)); |
| 81 | memset(ids, 0, sizeof(ids)); |
| 82 | |
| 83 | /* probe all MTD devices */ |
| 84 | for (uclass_first_device(UCLASS_MTD, &dev); |
| 85 | dev; |
| 86 | uclass_next_device(&dev)) { |
| 87 | pr_debug("mtd device = %s\n", dev->name); |
| 88 | } |
| 89 | |
| 90 | mtd = get_mtd_device_nm("nand0"); |
| 91 | if (!IS_ERR_OR_NULL(mtd)) { |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 92 | board_get_mtdparts("nand0", ids, parts, |
| 93 | CONFIG_MTDPARTS_NAND0_BOOT, |
| 94 | tee ? CONFIG_MTDPARTS_NAND0_TEE : NULL, |
| 95 | "-(UBI)"); |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 96 | put_mtd_device(mtd); |
| 97 | } |
| 98 | |
| 99 | mtd = get_mtd_device_nm("spi-nand0"); |
| 100 | if (!IS_ERR_OR_NULL(mtd)) { |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 101 | board_get_mtdparts("spi-nand0", ids, parts, |
| 102 | CONFIG_MTDPARTS_SPINAND0_BOOT, |
| 103 | tee ? CONFIG_MTDPARTS_SPINAND0_TEE : NULL, |
| 104 | "-(UBI)"); |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 105 | put_mtd_device(mtd); |
| 106 | } |
| 107 | |
| 108 | if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) |
Patrick Delaunay | 28a28ba | 2020-03-18 09:22:47 +0100 | [diff] [blame] | 109 | board_get_mtdparts("nor0", ids, parts, |
| 110 | CONFIG_MTDPARTS_NOR0_BOOT, |
| 111 | tee ? CONFIG_MTDPARTS_NOR0_TEE : NULL, |
| 112 | "-(nor_user)"); |
Patrick Delaunay | 939ba16 | 2020-03-18 09:22:44 +0100 | [diff] [blame] | 113 | |
| 114 | mtd_initialized = true; |
| 115 | *mtdids = ids; |
| 116 | *mtdparts = parts; |
| 117 | debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts); |
| 118 | } |