blob: 18878424c7a8eb24f6ebc5c94ca0939fe594cd98 [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 Delaunay9e0a13b2021-09-02 11:56:17 +0200122 if (!serial && tee_find_device(NULL, NULL, NULL, NULL))
Patrick Delaunay7511d142020-03-18 09:22:47 +0100123 tee = true;
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200124#endif
Patrick Delaunay7511d142020-03-18 09:22:47 +0100125
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100126 memset(parts, 0, sizeof(parts));
127 memset(ids, 0, sizeof(ids));
128
129 /* probe all MTD devices */
130 for (uclass_first_device(UCLASS_MTD, &dev);
131 dev;
132 uclass_next_device(&dev)) {
Patrick Delaunay9742bee2020-11-06 19:02:00 +0100133 log_debug("mtd device = %s\n", dev->name);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100134 }
135
Patrick Delaunayb7120412021-02-25 11:49:28 +0100136 if (nand) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100137 mtd = get_mtd_device_nm("nand0");
138 if (!IS_ERR_OR_NULL(mtd)) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100139 board_set_mtdparts("nand0", ids, parts,
Patrick Delaunayb7120412021-02-25 11:49:28 +0100140 CONFIG_MTDPARTS_NAND0_BOOT,
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200141#ifdef CONFIG_STM32MP15x_STM32IMAGE
142 !nor && tee ? CONFIG_MTDPARTS_NAND0_TEE : NULL,
143#endif
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100144 "-(UBI)");
145 put_mtd_device(mtd);
146 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100147 }
148
Patrick Delaunayb7120412021-02-25 11:49:28 +0100149 if (spinand) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100150 mtd = get_mtd_device_nm("spi-nand0");
151 if (!IS_ERR_OR_NULL(mtd)) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100152 board_set_mtdparts("spi-nand0", ids, parts,
Patrick Delaunayb7120412021-02-25 11:49:28 +0100153 CONFIG_MTDPARTS_SPINAND0_BOOT,
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200154#ifdef CONFIG_STM32MP15x_STM32IMAGE
155 !nor && tee ? CONFIG_MTDPARTS_SPINAND0_TEE : NULL,
156#endif
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100157 "-(UBI)");
158 put_mtd_device(mtd);
159 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100160 }
161
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100162 if (nor) {
163 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100164 board_set_mtdparts("nor0", ids, parts,
Patrick Delaunayb7120412021-02-25 11:49:28 +0100165 CONFIG_MTDPARTS_NOR0_BOOT,
Patrick Delaunay9c88dbf2021-07-26 11:21:36 +0200166#ifdef CONFIG_STM32MP15x_STM32IMAGE
167 tee ? CONFIG_MTDPARTS_NOR0_TEE : NULL,
168#endif
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100169 "-(nor_user)");
170 }
171 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100172
173 mtd_initialized = true;
174 *mtdids = ids;
175 *mtdparts = parts;
Patrick Delaunay9742bee2020-11-06 19:02:00 +0100176 log_debug("mtdids=%s & mtdparts=%s\n", ids, parts);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100177}