stm32mp1: dynamically detect op-tee presence
[oweals/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 #include <tee.h>
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 /**
23  * update the variables "mtdids" and "mtdparts" with boot, tee and user strings
24  */
25 static void board_get_mtdparts(const char *dev,
26                                char *mtdids,
27                                char *mtdparts,
28                                const char *boot,
29                                const char *tee,
30                                const char *user)
31 {
32         /* mtdids: "<dev>=<dev>, ...." */
33         if (mtdids[0] != '\0')
34                 strcat(mtdids, ",");
35         strcat(mtdids, dev);
36         strcat(mtdids, "=");
37         strcat(mtdids, dev);
38
39         /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
40         if (mtdparts[0] != '\0')
41                 strncat(mtdparts, ";", MTDPARTS_LEN);
42         else
43                 strcat(mtdparts, "mtdparts=");
44
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);
51         }
52
53         if (tee) {
54                 strncat(mtdparts, tee, MTDPARTS_LEN);
55                 strncat(mtdparts, ",", MTDPARTS_LEN);
56         }
57
58         strncat(mtdparts, user, MTDPARTS_LEN);
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;
68         bool tee = false;
69
70         if (mtd_initialized) {
71                 *mtdids = ids;
72                 *mtdparts = parts;
73                 return;
74         }
75
76         if (CONFIG_IS_ENABLED(OPTEE) &&
77             tee_find_device(NULL, NULL, NULL, NULL))
78                 tee = true;
79
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)) {
92                 board_get_mtdparts("nand0", ids, parts,
93                                    CONFIG_MTDPARTS_NAND0_BOOT,
94                                    tee ? CONFIG_MTDPARTS_NAND0_TEE : NULL,
95                                    "-(UBI)");
96                 put_mtd_device(mtd);
97         }
98
99         mtd = get_mtd_device_nm("spi-nand0");
100         if (!IS_ERR_OR_NULL(mtd)) {
101                 board_get_mtdparts("spi-nand0", ids, parts,
102                                    CONFIG_MTDPARTS_SPINAND0_BOOT,
103                                    tee ? CONFIG_MTDPARTS_SPINAND0_TEE : NULL,
104                                    "-(UBI)");
105                 put_mtd_device(mtd);
106         }
107
108         if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
109                 board_get_mtdparts("nor0", ids, parts,
110                                    CONFIG_MTDPARTS_NOR0_BOOT,
111                                    tee ? CONFIG_MTDPARTS_NOR0_TEE : NULL,
112                                    "-(nor_user)");
113
114         mtd_initialized = true;
115         *mtdids = ids;
116         *mtdparts = parts;
117         debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
118 }