stm32mp1: move MTDPART configuration in Kconfig
[platform/kernel/u-boot.git] / board / st / common / stm32mp_mtdparts.c
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>
12
13 #define MTDPARTS_LEN            256
14 #define MTDIDS_LEN              128
15
16 /*
17  * Get a global data pointer
18  */
19 DECLARE_GLOBAL_DATA_PTR;
20
21 /**
22  * update the variables "mtdids" and "mtdparts" with boot, tee and user strings
23  */
24 static void board_get_mtdparts(const char *dev,
25                                char *mtdids,
26                                char *mtdparts,
27                                const char *boot,
28                                const char *tee,
29                                const char *user)
30 {
31         /* mtdids: "<dev>=<dev>, ...." */
32         if (mtdids[0] != '\0')
33                 strcat(mtdids, ",");
34         strcat(mtdids, dev);
35         strcat(mtdids, "=");
36         strcat(mtdids, dev);
37
38         /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
39         if (mtdparts[0] != '\0')
40                 strncat(mtdparts, ";", MTDPARTS_LEN);
41         else
42                 strcat(mtdparts, "mtdparts=");
43
44         strncat(mtdparts, dev, MTDPARTS_LEN);
45         strncat(mtdparts, ":", MTDPARTS_LEN);
46
47         if (boot) {
48                 strncat(mtdparts, boot, MTDPARTS_LEN);
49                 strncat(mtdparts, ",", MTDPARTS_LEN);
50         }
51
52         if (CONFIG_IS_ENABLED(STM32MP1_OPTEE) && tee) {
53                 strncat(mtdparts, tee, MTDPARTS_LEN);
54                 strncat(mtdparts, ",", MTDPARTS_LEN);
55         }
56
57         strncat(mtdparts, user, MTDPARTS_LEN);
58 }
59
60 void board_mtdparts_default(const char **mtdids, const char **mtdparts)
61 {
62         struct mtd_info *mtd;
63         struct udevice *dev;
64         static char parts[3 * MTDPARTS_LEN + 1];
65         static char ids[MTDIDS_LEN + 1];
66         static bool mtd_initialized;
67         bool tee = false;
68
69         if (mtd_initialized) {
70                 *mtdids = ids;
71                 *mtdparts = parts;
72                 return;
73         }
74
75         if (CONFIG_IS_ENABLED(STM32MP1_OPTEE))
76                 tee = true;
77
78         memset(parts, 0, sizeof(parts));
79         memset(ids, 0, sizeof(ids));
80
81         /* probe all MTD devices */
82         for (uclass_first_device(UCLASS_MTD, &dev);
83              dev;
84              uclass_next_device(&dev)) {
85                 pr_debug("mtd device = %s\n", dev->name);
86         }
87
88         mtd = get_mtd_device_nm("nand0");
89         if (!IS_ERR_OR_NULL(mtd)) {
90                 board_get_mtdparts("nand0", ids, parts,
91                                    CONFIG_MTDPARTS_NAND0_BOOT,
92                                    tee ? CONFIG_MTDPARTS_NAND0_TEE : NULL,
93                                    "-(UBI)");
94                 put_mtd_device(mtd);
95         }
96
97         mtd = get_mtd_device_nm("spi-nand0");
98         if (!IS_ERR_OR_NULL(mtd)) {
99                 board_get_mtdparts("spi-nand0", ids, parts,
100                                    CONFIG_MTDPARTS_SPINAND0_BOOT,
101                                    tee ? CONFIG_MTDPARTS_SPINAND0_TEE : NULL,
102                                    "-(UBI)");
103                 put_mtd_device(mtd);
104         }
105
106         if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
107                 board_get_mtdparts("nor0", ids, parts,
108                                    CONFIG_MTDPARTS_NOR0_BOOT,
109                                    tee ? CONFIG_MTDPARTS_NOR0_TEE : NULL,
110                                    "-(nor_user)");
111
112         mtd_initialized = true;
113         *mtdids = ids;
114         *mtdparts = parts;
115         debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
116 }