blob: 71a0b449adfe1f4204a5345afc1fe09dc8794b42 [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>
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010017
18#define MTDPARTS_LEN 256
19#define MTDIDS_LEN 128
20
21/*
22 * Get a global data pointer
23 */
24DECLARE_GLOBAL_DATA_PTR;
25
26/**
Patrick Delaunay7511d142020-03-18 09:22:47 +010027 * update the variables "mtdids" and "mtdparts" with boot, tee and user strings
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010028 */
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010029static void board_set_mtdparts(const char *dev,
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010030 char *mtdids,
Patrick Delaunay7511d142020-03-18 09:22:47 +010031 char *mtdparts,
32 const char *boot,
33 const char *tee,
34 const char *user)
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010035{
Patrick Delaunay7511d142020-03-18 09:22:47 +010036 /* mtdids: "<dev>=<dev>, ...." */
37 if (mtdids[0] != '\0')
38 strcat(mtdids, ",");
39 strcat(mtdids, dev);
40 strcat(mtdids, "=");
41 strcat(mtdids, dev);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010042
Patrick Delaunay7511d142020-03-18 09:22:47 +010043 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
44 if (mtdparts[0] != '\0')
45 strncat(mtdparts, ";", MTDPARTS_LEN);
46 else
47 strcat(mtdparts, "mtdparts=");
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010048
Patrick Delaunay7511d142020-03-18 09:22:47 +010049 strncat(mtdparts, dev, MTDPARTS_LEN);
50 strncat(mtdparts, ":", MTDPARTS_LEN);
51
52 if (boot) {
53 strncat(mtdparts, boot, MTDPARTS_LEN);
54 strncat(mtdparts, ",", MTDPARTS_LEN);
55 }
56
Patrick Delaunay472407a2020-03-18 09:22:49 +010057 if (tee) {
Patrick Delaunay7511d142020-03-18 09:22:47 +010058 strncat(mtdparts, tee, MTDPARTS_LEN);
59 strncat(mtdparts, ",", MTDPARTS_LEN);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010060 }
Patrick Delaunay7511d142020-03-18 09:22:47 +010061
62 strncat(mtdparts, user, MTDPARTS_LEN);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010063}
64
65void board_mtdparts_default(const char **mtdids, const char **mtdparts)
66{
67 struct mtd_info *mtd;
68 struct udevice *dev;
69 static char parts[3 * MTDPARTS_LEN + 1];
70 static char ids[MTDIDS_LEN + 1];
71 static bool mtd_initialized;
Patrick Delaunayc5112242020-03-18 09:24:55 +010072 bool tee, nor, nand, spinand, serial;
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010073
74 if (mtd_initialized) {
75 *mtdids = ids;
76 *mtdparts = parts;
77 return;
78 }
79
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010080 tee = false;
81 nor = false;
82 nand = false;
83 spinand = false;
Patrick Delaunayc5112242020-03-18 09:24:55 +010084 serial = false;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010085
86 switch (get_bootmode() & TAMP_BOOT_DEVICE_MASK) {
87 case BOOT_SERIAL_UART:
88 case BOOT_SERIAL_USB:
Patrick Delaunayc5112242020-03-18 09:24:55 +010089 serial = true;
90 if (CONFIG_IS_ENABLED(CMD_STM32PROG)) {
91 tee = stm32prog_get_tee_partitions();
92 nor = stm32prog_get_fsbl_nor();
93 }
94 nand = true;
95 spinand = true;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010096 break;
97 case BOOT_FLASH_NAND:
98 nand = true;
99 break;
100 case BOOT_FLASH_SPINAND:
101 spinand = true;
102 break;
103 case BOOT_FLASH_NOR:
104 nor = true;
105 break;
106 default:
107 break;
108 }
109
Patrick Delaunayc5112242020-03-18 09:24:55 +0100110 if (!serial && CONFIG_IS_ENABLED(OPTEE) &&
Patrick Delaunay472407a2020-03-18 09:22:49 +0100111 tee_find_device(NULL, NULL, NULL, NULL))
Patrick Delaunay7511d142020-03-18 09:22:47 +0100112 tee = true;
113
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100114 memset(parts, 0, sizeof(parts));
115 memset(ids, 0, sizeof(ids));
116
117 /* probe all MTD devices */
118 for (uclass_first_device(UCLASS_MTD, &dev);
119 dev;
120 uclass_next_device(&dev)) {
Patrick Delaunay9742bee2020-11-06 19:02:00 +0100121 log_debug("mtd device = %s\n", dev->name);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100122 }
123
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100124 if (nor || nand) {
125 mtd = get_mtd_device_nm("nand0");
126 if (!IS_ERR_OR_NULL(mtd)) {
127 const char *mtd_boot = CONFIG_MTDPARTS_NAND0_BOOT;
128 const char *mtd_tee = CONFIG_MTDPARTS_NAND0_TEE;
129
130 board_set_mtdparts("nand0", ids, parts,
131 !nor ? mtd_boot : NULL,
132 !nor && tee ? mtd_tee : NULL,
133 "-(UBI)");
134 put_mtd_device(mtd);
135 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100136 }
137
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100138 if (nor || spinand) {
139 mtd = get_mtd_device_nm("spi-nand0");
140 if (!IS_ERR_OR_NULL(mtd)) {
141 const char *mtd_boot = CONFIG_MTDPARTS_SPINAND0_BOOT;
142 const char *mtd_tee = CONFIG_MTDPARTS_SPINAND0_TEE;
143
144 board_set_mtdparts("spi-nand0", ids, parts,
145 !nor ? mtd_boot : NULL,
146 !nor && tee ? mtd_tee : NULL,
147 "-(UBI)");
148 put_mtd_device(mtd);
149 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100150 }
151
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100152 if (nor) {
153 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) {
154 const char *mtd_boot = CONFIG_MTDPARTS_NOR0_BOOT;
155 const char *mtd_tee = CONFIG_MTDPARTS_NOR0_TEE;
156
157 board_set_mtdparts("nor0", ids, parts,
158 mtd_boot,
159 tee ? mtd_tee : NULL,
160 "-(nor_user)");
161 }
162 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100163
164 mtd_initialized = true;
165 *mtdids = ids;
166 *mtdparts = parts;
Patrick Delaunay9742bee2020-11-06 19:02:00 +0100167 log_debug("mtdids=%s & mtdparts=%s\n", ids, parts);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100168}