blob: 2b6413be16697bbd49d8fb841e3254993828c94b [file] [log] [blame]
Patrick Delaunay939ba162020-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>
7#include <dm.h>
8#include <env.h>
9#include <env_internal.h>
10#include <mtd.h>
11#include <mtd_node.h>
Patrick Delaunay43df0a12020-03-18 09:22:49 +010012#include <tee.h>
Patrick Delaunay939ba162020-03-18 09:22:44 +010013
14#define MTDPARTS_LEN 256
15#define MTDIDS_LEN 128
16
17/*
18 * Get a global data pointer
19 */
20DECLARE_GLOBAL_DATA_PTR;
21
22/**
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010023 * update the variables "mtdids" and "mtdparts" with boot, tee and user strings
Patrick Delaunay939ba162020-03-18 09:22:44 +010024 */
25static void board_get_mtdparts(const char *dev,
26 char *mtdids,
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010027 char *mtdparts,
28 const char *boot,
29 const char *tee,
30 const char *user)
Patrick Delaunay939ba162020-03-18 09:22:44 +010031{
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010032 /* mtdids: "<dev>=<dev>, ...." */
33 if (mtdids[0] != '\0')
34 strcat(mtdids, ",");
35 strcat(mtdids, dev);
36 strcat(mtdids, "=");
37 strcat(mtdids, dev);
Patrick Delaunay939ba162020-03-18 09:22:44 +010038
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010039 /* mtdparts: "mtdparts=<dev>:<mtdparts_<dev>>;..." */
40 if (mtdparts[0] != '\0')
41 strncat(mtdparts, ";", MTDPARTS_LEN);
42 else
43 strcat(mtdparts, "mtdparts=");
Patrick Delaunay939ba162020-03-18 09:22:44 +010044
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010045 strncat(mtdparts, dev, MTDPARTS_LEN);
46 strncat(mtdparts, ":", MTDPARTS_LEN);
47
48 if (boot) {
49 strncat(mtdparts, boot, MTDPARTS_LEN);
50 strncat(mtdparts, ",", MTDPARTS_LEN);
Patrick Delaunay939ba162020-03-18 09:22:44 +010051 }
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010052
Patrick Delaunay43df0a12020-03-18 09:22:49 +010053 if (tee) {
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010054 strncat(mtdparts, tee, MTDPARTS_LEN);
55 strncat(mtdparts, ",", MTDPARTS_LEN);
56 }
57
58 strncat(mtdparts, user, MTDPARTS_LEN);
Patrick Delaunay939ba162020-03-18 09:22:44 +010059}
60
61void board_mtdparts_default(const char **mtdids, const char **mtdparts)
62{
63 struct mtd_info *mtd;
64 struct udevice *dev;
65 static char parts[3 * MTDPARTS_LEN + 1];
66 static char ids[MTDIDS_LEN + 1];
67 static bool mtd_initialized;
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010068 bool tee = false;
Patrick Delaunay939ba162020-03-18 09:22:44 +010069
70 if (mtd_initialized) {
71 *mtdids = ids;
72 *mtdparts = parts;
73 return;
74 }
75
Patrick Delaunay43df0a12020-03-18 09:22:49 +010076 if (CONFIG_IS_ENABLED(OPTEE) &&
77 tee_find_device(NULL, NULL, NULL, NULL))
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010078 tee = true;
79
Patrick Delaunay939ba162020-03-18 09:22:44 +010080 memset(parts, 0, sizeof(parts));
81 memset(ids, 0, sizeof(ids));
82
83 /* probe all MTD devices */
84 for (uclass_first_device(UCLASS_MTD, &dev);
85 dev;
86 uclass_next_device(&dev)) {
87 pr_debug("mtd device = %s\n", dev->name);
88 }
89
90 mtd = get_mtd_device_nm("nand0");
91 if (!IS_ERR_OR_NULL(mtd)) {
Patrick Delaunay28a28ba2020-03-18 09:22:47 +010092 board_get_mtdparts("nand0", ids, parts,
93 CONFIG_MTDPARTS_NAND0_BOOT,
94 tee ? CONFIG_MTDPARTS_NAND0_TEE : NULL,
95 "-(UBI)");
Patrick Delaunay939ba162020-03-18 09:22:44 +010096 put_mtd_device(mtd);
97 }
98
99 mtd = get_mtd_device_nm("spi-nand0");
100 if (!IS_ERR_OR_NULL(mtd)) {
Patrick Delaunay28a28ba2020-03-18 09:22:47 +0100101 board_get_mtdparts("spi-nand0", ids, parts,
102 CONFIG_MTDPARTS_SPINAND0_BOOT,
103 tee ? CONFIG_MTDPARTS_SPINAND0_TEE : NULL,
104 "-(UBI)");
Patrick Delaunay939ba162020-03-18 09:22:44 +0100105 put_mtd_device(mtd);
106 }
107
108 if (!uclass_get_device(UCLASS_SPI_FLASH, 0, &dev))
Patrick Delaunay28a28ba2020-03-18 09:22:47 +0100109 board_get_mtdparts("nor0", ids, parts,
110 CONFIG_MTDPARTS_NOR0_BOOT,
111 tee ? CONFIG_MTDPARTS_NOR0_TEE : NULL,
112 "-(nor_user)");
Patrick Delaunay939ba162020-03-18 09:22:44 +0100113
114 mtd_initialized = true;
115 *mtdids = ids;
116 *mtdparts = parts;
117 debug("%s:mtdids=%s & mtdparts=%s\n", __func__, ids, parts);
118}