blob: 69eb10844d4ef711e93d0bddd0609dcd73ce0598 [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>
Patrick Delaunay9742bee2020-11-06 19:02:00 +010011#include <log.h>
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010012#include <mtd.h>
13#include <mtd_node.h>
Patrick Delaunay472407a2020-03-18 09:22:49 +010014#include <tee.h>
Patrick Delaunayc5112242020-03-18 09:24:55 +010015#include <asm/arch/stm32prog.h>
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010016#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010018
19#define MTDPARTS_LEN 256
20#define MTDIDS_LEN 128
21
22/*
23 * Get a global data pointer
24 */
25DECLARE_GLOBAL_DATA_PTR;
26
27/**
Patrick Delaunay7511d142020-03-18 09:22:47 +010028 * update the variables "mtdids" and "mtdparts" with boot, tee and user strings
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010029 */
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010030static void board_set_mtdparts(const char *dev,
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010031 char *mtdids,
Patrick Delaunay7511d142020-03-18 09:22:47 +010032 char *mtdparts,
33 const char *boot,
34 const char *tee,
35 const char *user)
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010036{
Patrick Delaunay7511d142020-03-18 09:22:47 +010037 /* mtdids: "<dev>=<dev>, ...." */
38 if (mtdids[0] != '\0')
39 strcat(mtdids, ",");
40 strcat(mtdids, dev);
41 strcat(mtdids, "=");
42 strcat(mtdids, dev);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010043
Patrick Delaunay7511d142020-03-18 09:22:47 +010044 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
45 if (mtdparts[0] != '\0')
46 strncat(mtdparts, ";", MTDPARTS_LEN);
47 else
48 strcat(mtdparts, "mtdparts=");
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010049
Patrick Delaunay7511d142020-03-18 09:22:47 +010050 strncat(mtdparts, dev, MTDPARTS_LEN);
51 strncat(mtdparts, ":", MTDPARTS_LEN);
52
53 if (boot) {
54 strncat(mtdparts, boot, MTDPARTS_LEN);
55 strncat(mtdparts, ",", MTDPARTS_LEN);
56 }
57
Patrick Delaunay472407a2020-03-18 09:22:49 +010058 if (tee) {
Patrick Delaunay7511d142020-03-18 09:22:47 +010059 strncat(mtdparts, tee, MTDPARTS_LEN);
60 strncat(mtdparts, ",", MTDPARTS_LEN);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010061 }
Patrick Delaunay7511d142020-03-18 09:22:47 +010062
63 strncat(mtdparts, user, MTDPARTS_LEN);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010064}
65
66void board_mtdparts_default(const char **mtdids, const char **mtdparts)
67{
68 struct mtd_info *mtd;
69 struct udevice *dev;
70 static char parts[3 * MTDPARTS_LEN + 1];
71 static char ids[MTDIDS_LEN + 1];
72 static bool mtd_initialized;
Patrick Delaunayc5112242020-03-18 09:24:55 +010073 bool tee, nor, nand, spinand, serial;
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010074
75 if (mtd_initialized) {
76 *mtdids = ids;
77 *mtdparts = parts;
78 return;
79 }
80
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010081 tee = false;
82 nor = false;
83 nand = false;
84 spinand = false;
Patrick Delaunayc5112242020-03-18 09:24:55 +010085 serial = false;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010086
87 switch (get_bootmode() & TAMP_BOOT_DEVICE_MASK) {
88 case BOOT_SERIAL_UART:
89 case BOOT_SERIAL_USB:
Patrick Delaunayc5112242020-03-18 09:24:55 +010090 serial = true;
91 if (CONFIG_IS_ENABLED(CMD_STM32PROG)) {
92 tee = stm32prog_get_tee_partitions();
93 nor = stm32prog_get_fsbl_nor();
94 }
95 nand = true;
96 spinand = true;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010097 break;
98 case BOOT_FLASH_NAND:
99 nand = true;
100 break;
101 case BOOT_FLASH_SPINAND:
102 spinand = true;
103 break;
104 case BOOT_FLASH_NOR:
105 nor = true;
106 break;
107 default:
108 break;
109 }
110
Patrick Delaunayc5112242020-03-18 09:24:55 +0100111 if (!serial && CONFIG_IS_ENABLED(OPTEE) &&
Patrick Delaunay472407a2020-03-18 09:22:49 +0100112 tee_find_device(NULL, NULL, NULL, NULL))
Patrick Delaunay7511d142020-03-18 09:22:47 +0100113 tee = true;
114
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100115 memset(parts, 0, sizeof(parts));
116 memset(ids, 0, sizeof(ids));
117
118 /* probe all MTD devices */
119 for (uclass_first_device(UCLASS_MTD, &dev);
120 dev;
121 uclass_next_device(&dev)) {
Patrick Delaunay9742bee2020-11-06 19:02:00 +0100122 log_debug("mtd device = %s\n", dev->name);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100123 }
124
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100125 if (nor || nand) {
126 mtd = get_mtd_device_nm("nand0");
127 if (!IS_ERR_OR_NULL(mtd)) {
128 const char *mtd_boot = CONFIG_MTDPARTS_NAND0_BOOT;
129 const char *mtd_tee = CONFIG_MTDPARTS_NAND0_TEE;
130
131 board_set_mtdparts("nand0", ids, parts,
132 !nor ? mtd_boot : NULL,
133 !nor && tee ? mtd_tee : NULL,
134 "-(UBI)");
135 put_mtd_device(mtd);
136 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100137 }
138
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100139 if (nor || spinand) {
140 mtd = get_mtd_device_nm("spi-nand0");
141 if (!IS_ERR_OR_NULL(mtd)) {
142 const char *mtd_boot = CONFIG_MTDPARTS_SPINAND0_BOOT;
143 const char *mtd_tee = CONFIG_MTDPARTS_SPINAND0_TEE;
144
145 board_set_mtdparts("spi-nand0", ids, parts,
146 !nor ? mtd_boot : NULL,
147 !nor && tee ? mtd_tee : NULL,
148 "-(UBI)");
149 put_mtd_device(mtd);
150 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100151 }
152
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100153 if (nor) {
154 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) {
155 const char *mtd_boot = CONFIG_MTDPARTS_NOR0_BOOT;
156 const char *mtd_tee = CONFIG_MTDPARTS_NOR0_TEE;
157
158 board_set_mtdparts("nor0", ids, parts,
159 mtd_boot,
160 tee ? mtd_tee : NULL,
161 "-(nor_user)");
162 }
163 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100164
165 mtd_initialized = true;
166 *mtdids = ids;
167 *mtdparts = parts;
Patrick Delaunay9742bee2020-11-06 19:02:00 +0100168 log_debug("mtdids=%s & mtdparts=%s\n", ids, parts);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100169}