blob: f074fc189d4c7d952d0cc9a8f6ca0a19296bc15f [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 Delaunayb7120412021-02-25 11:49:28 +0100125 if (nand) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100126 mtd = get_mtd_device_nm("nand0");
127 if (!IS_ERR_OR_NULL(mtd)) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100128 const char *mtd_tee = CONFIG_MTDPARTS_NAND0_TEE;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100129 board_set_mtdparts("nand0", ids, parts,
Patrick Delaunayb7120412021-02-25 11:49:28 +0100130 CONFIG_MTDPARTS_NAND0_BOOT,
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100131 !nor && tee ? mtd_tee : NULL,
132 "-(UBI)");
133 put_mtd_device(mtd);
134 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100135 }
136
Patrick Delaunayb7120412021-02-25 11:49:28 +0100137 if (spinand) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100138 mtd = get_mtd_device_nm("spi-nand0");
139 if (!IS_ERR_OR_NULL(mtd)) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100140 const char *mtd_tee = CONFIG_MTDPARTS_SPINAND0_TEE;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100141 board_set_mtdparts("spi-nand0", ids, parts,
Patrick Delaunayb7120412021-02-25 11:49:28 +0100142 CONFIG_MTDPARTS_SPINAND0_BOOT,
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100143 !nor && tee ? mtd_tee : NULL,
144 "-(UBI)");
145 put_mtd_device(mtd);
146 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100147 }
148
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100149 if (nor) {
150 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev)) {
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100151 const char *mtd_tee = CONFIG_MTDPARTS_NOR0_TEE;
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100152 board_set_mtdparts("nor0", ids, parts,
Patrick Delaunayb7120412021-02-25 11:49:28 +0100153 CONFIG_MTDPARTS_NOR0_BOOT,
Patrick Delaunaycbbb53a2020-03-18 09:22:53 +0100154 tee ? mtd_tee : NULL,
155 "-(nor_user)");
156 }
157 }
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100158
159 mtd_initialized = true;
160 *mtdids = ids;
161 *mtdparts = parts;
Patrick Delaunay9742bee2020-11-06 19:02:00 +0100162 log_debug("mtdids=%s & mtdparts=%s\n", ids, parts);
Patrick Delaunaydaa5f012020-03-18 09:22:44 +0100163}