blob: 8b636d62faf9f40d2fbb0cd116ab605e484dff3a [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 Delaunay9c88dbf2021-07-26 11:21:36 +020014#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunay472407a2020-03-18 09:22:49 +010015#include <tee.h>
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +020016#endif
Patrick Delaunayc5112242020-03-18 09:24:55 +010017#include <asm/arch/stm32prog.h>
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010018#include <asm/arch/sys_proto.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010020
21#define MTDPARTS_LEN 256
22#define MTDIDS_LEN 128
23
24/*
25 * Get a global data pointer
26 */
27DECLARE_GLOBAL_DATA_PTR;
28
29/**
Patrick Delaunay7511d142020-03-18 09:22:47 +010030 * update the variables "mtdids" and "mtdparts" with boot, tee and user strings
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010031 */
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010032static void board_set_mtdparts(const char *dev,
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010033 char *mtdids,
Patrick Delaunay7511d142020-03-18 09:22:47 +010034 char *mtdparts,
35 const char *boot,
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +020036#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunay7511d142020-03-18 09:22:47 +010037 const char *tee,
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +020038#endif
Patrick Delaunay7511d142020-03-18 09:22:47 +010039 const char *user)
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010040{
Patrick Delaunay7511d142020-03-18 09:22:47 +010041 /* mtdids: "<dev>=<dev>, ...." */
42 if (mtdids[0] != '\0')
43 strcat(mtdids, ",");
44 strcat(mtdids, dev);
45 strcat(mtdids, "=");
46 strcat(mtdids, dev);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010047
Patrick Delaunay7511d142020-03-18 09:22:47 +010048 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
49 if (mtdparts[0] != '\0')
50 strncat(mtdparts, ";", MTDPARTS_LEN);
51 else
52 strcat(mtdparts, "mtdparts=");
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010053
Patrick Delaunay7511d142020-03-18 09:22:47 +010054 strncat(mtdparts, dev, MTDPARTS_LEN);
55 strncat(mtdparts, ":", MTDPARTS_LEN);
56
57 if (boot) {
58 strncat(mtdparts, boot, MTDPARTS_LEN);
59 strncat(mtdparts, ",", MTDPARTS_LEN);
60 }
61
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +020062#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunay472407a2020-03-18 09:22:49 +010063 if (tee) {
Patrick Delaunay7511d142020-03-18 09:22:47 +010064 strncat(mtdparts, tee, MTDPARTS_LEN);
65 strncat(mtdparts, ",", MTDPARTS_LEN);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010066 }
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +020067#endif
Patrick Delaunay7511d142020-03-18 09:22:47 +010068
69 strncat(mtdparts, user, MTDPARTS_LEN);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010070}
71
72void board_mtdparts_default(const char **mtdids, const char **mtdparts)
73{
74 struct mtd_info *mtd;
75 struct udevice *dev;
76 static char parts[3 * MTDPARTS_LEN + 1];
77 static char ids[MTDIDS_LEN + 1];
78 static bool mtd_initialized;
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +020079 bool nor, nand, spinand, serial;
80#ifdef CONFIG_STM32MP15x_STM32IMAGE
81 bool tee = false;
82#endif
Patrick Delaunaydaa5f012020-03-18 09:22:44 +010083
84 if (mtd_initialized) {
85 *mtdids = ids;
86 *mtdparts = parts;
87 return;
88 }
89
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010090 nor = false;
91 nand = false;
92 spinand = false;
Patrick Delaunayc5112242020-03-18 09:24:55 +010093 serial = false;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +010094
95 switch (get_bootmode() & TAMP_BOOT_DEVICE_MASK) {
96 case BOOT_SERIAL_UART:
97 case BOOT_SERIAL_USB:
Patrick Delaunayc5112242020-03-18 09:24:55 +010098 serial = true;
99 if (CONFIG_IS_ENABLED(CMD_STM32PROG)) {
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200100#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunayc5112242020-03-18 09:24:55 +0100101 tee = stm32prog_get_tee_partitions();
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200102#endif
Patrick Delaunayc5112242020-03-18 09:24:55 +0100103 nor = stm32prog_get_fsbl_nor();
104 }
105 nand = true;
106 spinand = true;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100107 break;
108 case BOOT_FLASH_NAND:
109 nand = true;
110 break;
111 case BOOT_FLASH_SPINAND:
112 spinand = true;
113 break;
114 case BOOT_FLASH_NOR:
115 nor = true;
116 break;
117 default:
118 break;
119 }
120
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200121#ifdef CONFIG_STM32MP15x_STM32IMAGE
Patrick Delaunayc5112242020-03-18 09:24:55 +0100122 if (!serial && CONFIG_IS_ENABLED(OPTEE) &&
Patrick Delaunay472407a2020-03-18 09:22:49 +0100123 tee_find_device(NULL, NULL, NULL, NULL))
Patrick Delaunay7511d142020-03-18 09:22:47 +0100124 tee = true;
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200125#endif
Patrick Delaunay7511d142020-03-18 09:22:47 +0100126
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100127 memset(parts, 0, sizeof(parts));
128 memset(ids, 0, sizeof(ids));
129
130 /* probe all MTD devices */
131 for (uclass_first_device(UCLASS_MTD, &dev);
132 dev;
133 uclass_next_device(&dev)) {
Patrick Delaunay9742bee2020-11-06 19:02:00 +0100134 log_debug("mtd device = %s\n", dev->name);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100135 }
136
Patrick Delaunayb7120412021-02-25 11:49:28 +0100137 if (nand) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100138 mtd = get_mtd_device_nm("nand0");
139 if (!IS_ERR_OR_NULL(mtd)) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100140 board_set_mtdparts("nand0", ids, parts,
Patrick Delaunayb7120412021-02-25 11:49:28 +0100141 CONFIG_MTDPARTS_NAND0_BOOT,
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200142#ifdef CONFIG_STM32MP15x_STM32IMAGE
143 !nor && tee ? CONFIG_MTDPARTS_NAND0_TEE : NULL,
144#endif
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100145 "-(UBI)");
146 put_mtd_device(mtd);
147 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100148 }
149
Patrick Delaunayb7120412021-02-25 11:49:28 +0100150 if (spinand) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100151 mtd = get_mtd_device_nm("spi-nand0");
152 if (!IS_ERR_OR_NULL(mtd)) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100153 board_set_mtdparts("spi-nand0", ids, parts,
Patrick Delaunayb7120412021-02-25 11:49:28 +0100154 CONFIG_MTDPARTS_SPINAND0_BOOT,
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200155#ifdef CONFIG_STM32MP15x_STM32IMAGE
156 !nor && tee ? CONFIG_MTDPARTS_SPINAND0_TEE : NULL,
157#endif
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100158 "-(UBI)");
159 put_mtd_device(mtd);
160 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100161 }
162
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100163 if (nor) {
164 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100165 board_set_mtdparts("nor0", ids, parts,
Patrick Delaunayb7120412021-02-25 11:49:28 +0100166 CONFIG_MTDPARTS_NOR0_BOOT,
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200167#ifdef CONFIG_STM32MP15x_STM32IMAGE
168 tee ? CONFIG_MTDPARTS_NOR0_TEE : NULL,
169#endif
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100170 "-(nor_user)");
171 }
172 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100173
174 mtd_initialized = true;
175 *mtdids = ids;
176 *mtdparts = parts;
Patrick Delaunay9742bee2020-11-06 19:02:00 +0100177 log_debug("mtdids=%s & mtdparts=%s\n", ids, parts);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100178}