blob: 9f5897f8c860640e72b24a9a28ddaa849e6e9522 [file] [log] [blame]
Patrick Delaunaydaa5f012020-03-18 09:22:44 +01001// 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>
Patrick Delaunayc5112242020-03-18 09:24:55 +01007#include <dfu.h>
Patrick Delaunaydaa5f012020-03-18 09:22:44 +01008#include <dm.h>
9#include <env.h>
10#include <env_internal.h>
11#include <mtd.h>
12#include <mtd_node.h>
Patrick Delaunay472407a2020-03-18 09:22:49 +010013#include <tee.h>
Patrick Delaunayc5112242020-03-18 09:24:55 +010014#include <asm/arch/stm32prog.h>
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010015#include <asm/arch/sys_proto.h>
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010016
17#define MTDPARTS_LEN 256
18#define MTDIDS_LEN 128
19
20/*
21 * Get a global data pointer
22 */
23DECLARE_GLOBAL_DATA_PTR;
24
25/**
Patrick Delaunay7511d142020-03-18 09:22:47 +010026 * update the variables "mtdids" and "mtdparts" with boot, tee and user strings
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010027 */
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010028static void board_set_mtdparts(const char *dev,
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010029 char *mtdids,
Patrick Delaunay7511d142020-03-18 09:22:47 +010030 char *mtdparts,
31 const char *boot,
32 const char *tee,
33 const char *user)
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010034{
Patrick Delaunay7511d142020-03-18 09:22:47 +010035 /* mtdids: "<dev>=<dev>, ...." */
36 if (mtdids[0] != '\0')
37 strcat(mtdids, ",");
38 strcat(mtdids, dev);
39 strcat(mtdids, "=");
40 strcat(mtdids, dev);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010041
Patrick Delaunay7511d142020-03-18 09:22:47 +010042 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
43 if (mtdparts[0] != '\0')
44 strncat(mtdparts, ";", MTDPARTS_LEN);
45 else
46 strcat(mtdparts, "mtdparts=");
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010047
Patrick Delaunay7511d142020-03-18 09:22:47 +010048 strncat(mtdparts, dev, MTDPARTS_LEN);
49 strncat(mtdparts, ":", MTDPARTS_LEN);
50
51 if (boot) {
52 strncat(mtdparts, boot, MTDPARTS_LEN);
53 strncat(mtdparts, ",", MTDPARTS_LEN);
54 }
55
Patrick Delaunay472407a2020-03-18 09:22:49 +010056 if (tee) {
Patrick Delaunay7511d142020-03-18 09:22:47 +010057 strncat(mtdparts, tee, MTDPARTS_LEN);
58 strncat(mtdparts, ",", MTDPARTS_LEN);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010059 }
Patrick Delaunay7511d142020-03-18 09:22:47 +010060
61 strncat(mtdparts, user, MTDPARTS_LEN);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010062}
63
64void board_mtdparts_default(const char **mtdids, const char **mtdparts)
65{
66 struct mtd_info *mtd;
67 struct udevice *dev;
68 static char parts[3 * MTDPARTS_LEN + 1];
69 static char ids[MTDIDS_LEN + 1];
70 static bool mtd_initialized;
Patrick Delaunayc5112242020-03-18 09:24:55 +010071 bool tee, nor, nand, spinand, serial;
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010072
73 if (mtd_initialized) {
74 *mtdids = ids;
75 *mtdparts = parts;
76 return;
77 }
78
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010079 tee = false;
80 nor = false;
81 nand = false;
82 spinand = false;
Patrick Delaunayc5112242020-03-18 09:24:55 +010083 serial = false;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010084
85 switch (get_bootmode() & TAMP_BOOT_DEVICE_MASK) {
86 case BOOT_SERIAL_UART:
87 case BOOT_SERIAL_USB:
Patrick Delaunayc5112242020-03-18 09:24:55 +010088 serial = true;
89 if (CONFIG_IS_ENABLED(CMD_STM32PROG)) {
90 tee = stm32prog_get_tee_partitions();
91 nor = stm32prog_get_fsbl_nor();
92 }
93 nand = true;
94 spinand = true;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010095 break;
96 case BOOT_FLASH_NAND:
97 nand = true;
98 break;
99 case BOOT_FLASH_SPINAND:
100 spinand = true;
101 break;
102 case BOOT_FLASH_NOR:
103 nor = true;
104 break;
105 default:
106 break;
107 }
108
Patrick Delaunayc5112242020-03-18 09:24:55 +0100109 if (!serial && CONFIG_IS_ENABLED(OPTEE) &&
Patrick Delaunay472407a2020-03-18 09:22:49 +0100110 tee_find_device(NULL, NULL, NULL, NULL))
Patrick Delaunay7511d142020-03-18 09:22:47 +0100111 tee = true;
112
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100113 memset(parts, 0, sizeof(parts));
114 memset(ids, 0, sizeof(ids));
115
116 /* probe all MTD devices */
117 for (uclass_first_device(UCLASS_MTD, &dev);
118 dev;
119 uclass_next_device(&dev)) {
120 pr_debug("mtd device = %s\n", dev->name);
121 }
122
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100123 if (nor || nand) {
124 mtd = get_mtd_device_nm("nand0");
125 if (!IS_ERR_OR_NULL(mtd)) {
126 const char *mtd_boot = CONFIG_MTDPARTS_NAND0_BOOT;
127 const char *mtd_tee = CONFIG_MTDPARTS_NAND0_TEE;
128
129 board_set_mtdparts("nand0", ids, parts,
130 !nor ? mtd_boot : NULL,
131 !nor && tee ? mtd_tee : NULL,
132 "-(UBI)");
133 put_mtd_device(mtd);
134 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100135 }
136
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100137 if (nor || spinand) {
138 mtd = get_mtd_device_nm("spi-nand0");
139 if (!IS_ERR_OR_NULL(mtd)) {
140 const char *mtd_boot = CONFIG_MTDPARTS_SPINAND0_BOOT;
141 const char *mtd_tee = CONFIG_MTDPARTS_SPINAND0_TEE;
142
143 board_set_mtdparts("spi-nand0", ids, parts,
144 !nor ? mtd_boot : NULL,
145 !nor && tee ? mtd_tee : NULL,
146 "-(UBI)");
147 put_mtd_device(mtd);
148 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100149 }
150
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100151 if (nor) {
152 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) {
153 const char *mtd_boot = CONFIG_MTDPARTS_NOR0_BOOT;
154 const char *mtd_tee = CONFIG_MTDPARTS_NOR0_TEE;
155
156 board_set_mtdparts("nor0", ids, parts,
157 mtd_boot,
158 tee ? mtd_tee : NULL,
159 "-(nor_user)");
160 }
161 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100162
163 mtd_initialized = true;
164 *mtdids = ids;
165 *mtdparts = parts;
166 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
167}