tdx-cfg-block.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2016-2020 Toradex
  4. */
  5. #include <common.h>
  6. #include <asm/global_data.h>
  7. #include "tdx-cfg-block.h"
  8. #include "tdx-eeprom.h"
  9. #include <command.h>
  10. #include <asm/cache.h>
  11. #if defined(CONFIG_TARGET_APALIS_IMX6) || \
  12. defined(CONFIG_TARGET_APALIS_IMX8) || \
  13. defined(CONFIG_TARGET_APALIS_IMX8X) || \
  14. defined(CONFIG_TARGET_COLIBRI_IMX6) || \
  15. defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
  16. defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
  17. defined(CONFIG_TARGET_VERDIN_IMX8MN) || \
  18. defined(CONFIG_TARGET_VERDIN_IMX8MP)
  19. #include <asm/arch/sys_proto.h>
  20. #else
  21. #define is_cpu_type(cpu) (0)
  22. #endif
  23. #if defined(CONFIG_CPU_PXA27X)
  24. #include <asm/arch-pxa/pxa.h>
  25. #else
  26. #define cpu_is_pxa27x(cpu) (0)
  27. #endif
  28. #include <cli.h>
  29. #include <console.h>
  30. #include <env.h>
  31. #include <flash.h>
  32. #include <malloc.h>
  33. #include <mmc.h>
  34. #include <nand.h>
  35. #include <asm/mach-types.h>
  36. DECLARE_GLOBAL_DATA_PTR;
  37. #define TAG_VALID 0xcf01
  38. #define TAG_MAC 0x0000
  39. #define TAG_CAR_SERIAL 0x0021
  40. #define TAG_HW 0x0008
  41. #define TAG_INVALID 0xffff
  42. #define TAG_FLAG_VALID 0x1
  43. #define TDX_EEPROM_ID_MODULE 0
  44. #define TDX_EEPROM_ID_CARRIER 1
  45. #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
  46. #define TDX_CFG_BLOCK_MAX_SIZE 512
  47. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
  48. #define TDX_CFG_BLOCK_MAX_SIZE 64
  49. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
  50. #define TDX_CFG_BLOCK_MAX_SIZE 64
  51. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
  52. #define TDX_CFG_BLOCK_MAX_SIZE 64
  53. #else
  54. #error Toradex config block location not set
  55. #endif
  56. #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
  57. #define TDX_CFG_BLOCK_EXTRA_MAX_SIZE 64
  58. #endif
  59. struct toradex_tag {
  60. u32 len:14;
  61. u32 flags:2;
  62. u32 id:16;
  63. };
  64. bool valid_cfgblock;
  65. struct toradex_hw tdx_hw_tag;
  66. struct toradex_eth_addr tdx_eth_addr;
  67. u32 tdx_serial;
  68. #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
  69. u32 tdx_car_serial;
  70. bool valid_cfgblock_carrier;
  71. struct toradex_hw tdx_car_hw_tag;
  72. #endif
  73. const char * const toradex_modules[] = {
  74. [0] = "UNKNOWN MODULE",
  75. [1] = "Colibri PXA270 312MHz",
  76. [2] = "Colibri PXA270 520MHz",
  77. [3] = "Colibri PXA320 806MHz",
  78. [4] = "Colibri PXA300 208MHz",
  79. [5] = "Colibri PXA310 624MHz",
  80. [6] = "Colibri PXA320 806MHz IT",
  81. [7] = "Colibri PXA300 208MHz XT",
  82. [8] = "Colibri PXA270 312MHz",
  83. [9] = "Colibri PXA270 520MHz",
  84. [10] = "Colibri VF50 128MB", /* not currently on sale */
  85. [11] = "Colibri VF61 256MB",
  86. [12] = "Colibri VF61 256MB IT",
  87. [13] = "Colibri VF50 128MB IT",
  88. [14] = "Colibri iMX6 Solo 256MB",
  89. [15] = "Colibri iMX6 DualLite 512MB",
  90. [16] = "Colibri iMX6 Solo 256MB IT",
  91. [17] = "Colibri iMX6 DualLite 512MB IT",
  92. [18] = "UNKNOWN MODULE",
  93. [19] = "UNKNOWN MODULE",
  94. [20] = "Colibri T20 256MB",
  95. [21] = "Colibri T20 512MB",
  96. [22] = "Colibri T20 512MB IT",
  97. [23] = "Colibri T30 1GB",
  98. [24] = "Colibri T20 256MB IT",
  99. [25] = "Apalis T30 2GB",
  100. [26] = "Apalis T30 1GB",
  101. [27] = "Apalis iMX6 Quad 1GB",
  102. [28] = "Apalis iMX6 Quad 2GB IT",
  103. [29] = "Apalis iMX6 Dual 512MB",
  104. [30] = "Colibri T30 1GB IT",
  105. [31] = "Apalis T30 1GB IT",
  106. [32] = "Colibri iMX7 Solo 256MB",
  107. [33] = "Colibri iMX7 Dual 512MB",
  108. [34] = "Apalis TK1 2GB",
  109. [35] = "Apalis iMX6 Dual 1GB IT",
  110. [36] = "Colibri iMX6ULL 256MB",
  111. [37] = "Apalis iMX8 QuadMax 4GB Wi-Fi / BT IT",
  112. [38] = "Colibri iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
  113. [39] = "Colibri iMX7 Dual 1GB (eMMC)",
  114. [40] = "Colibri iMX6ULL 512MB Wi-Fi / BT IT",
  115. [41] = "Colibri iMX7 Dual 512MB EPDC",
  116. [42] = "Apalis TK1 4GB",
  117. [43] = "Colibri T20 512MB IT SETEK",
  118. [44] = "Colibri iMX6ULL 512MB IT",
  119. [45] = "Colibri iMX6ULL 512MB Wi-Fi / Bluetooth",
  120. [46] = "Apalis iMX8 QuadXPlus 2GB Wi-Fi / BT IT",
  121. [47] = "Apalis iMX8 QuadMax 4GB IT",
  122. [48] = "Apalis iMX8 QuadPlus 2GB Wi-Fi / BT",
  123. [49] = "Apalis iMX8 QuadPlus 2GB",
  124. [50] = "Colibri iMX8 QuadXPlus 2GB IT",
  125. [51] = "Colibri iMX8 DualX 1GB Wi-Fi / Bluetooth",
  126. [52] = "Colibri iMX8 DualX 1GB",
  127. [53] = "Apalis iMX8 QuadXPlus 2GB ECC IT",
  128. [54] = "Apalis iMX8 DualXPlus 1GB",
  129. [55] = "Verdin iMX8M Mini Quad 2GB Wi-Fi / BT IT",
  130. [56] = "Verdin iMX8M Nano Quad 1GB Wi-Fi / BT", /* not currently on sale */
  131. [57] = "Verdin iMX8M Mini DualLite 1GB",
  132. [58] = "Verdin iMX8M Plus Quad 4GB Wi-Fi / BT IT",
  133. [59] = "Verdin iMX8M Mini Quad 2GB IT",
  134. [60] = "Verdin iMX8M Mini DualLite 1GB WB IT",
  135. [61] = "Verdin iMX8M Plus Quad 2GB",
  136. };
  137. const char * const toradex_carrier_boards[] = {
  138. [0] = "UNKNOWN CARRIER BOARD",
  139. [155] = "Dahlia",
  140. [156] = "Verdin Development Board",
  141. };
  142. const char * const toradex_display_adapters[] = {
  143. [0] = "UNKNOWN DISPLAY ADAPTER",
  144. [157] = "Verdin DSI to HDMI Adapter",
  145. [159] = "Verdin DSI to LVDS Adapter",
  146. };
  147. #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_MMC
  148. static int tdx_cfg_block_mmc_storage(u8 *config_block, int write)
  149. {
  150. struct mmc *mmc;
  151. int dev = CONFIG_TDX_CFG_BLOCK_DEV;
  152. int offset = CONFIG_TDX_CFG_BLOCK_OFFSET;
  153. uint part = CONFIG_TDX_CFG_BLOCK_PART;
  154. uint blk_start;
  155. int ret = 0;
  156. /* Read production parameter config block from eMMC */
  157. mmc = find_mmc_device(dev);
  158. if (!mmc) {
  159. puts("No MMC card found\n");
  160. ret = -ENODEV;
  161. goto out;
  162. }
  163. if (mmc_init(mmc)) {
  164. puts("MMC init failed\n");
  165. return -EINVAL;
  166. }
  167. if (part != mmc_get_blk_desc(mmc)->hwpart) {
  168. if (blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part)) {
  169. puts("MMC partition switch failed\n");
  170. ret = -ENODEV;
  171. goto out;
  172. }
  173. }
  174. if (offset < 0)
  175. offset += mmc->capacity;
  176. blk_start = ALIGN(offset, mmc->write_bl_len) / mmc->write_bl_len;
  177. if (!write) {
  178. /* Careful reads a whole block of 512 bytes into config_block */
  179. if (blk_dread(mmc_get_blk_desc(mmc), blk_start, 1,
  180. (unsigned char *)config_block) != 1) {
  181. ret = -EIO;
  182. goto out;
  183. }
  184. } else {
  185. /* Just writing one 512 byte block */
  186. if (blk_dwrite(mmc_get_blk_desc(mmc), blk_start, 1,
  187. (unsigned char *)config_block) != 1) {
  188. ret = -EIO;
  189. goto out;
  190. }
  191. }
  192. out:
  193. /* Switch back to regular eMMC user partition */
  194. blk_select_hwpart_devnum(IF_TYPE_MMC, 0, 0);
  195. return ret;
  196. }
  197. #endif
  198. #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NAND
  199. static int read_tdx_cfg_block_from_nand(unsigned char *config_block)
  200. {
  201. size_t size = TDX_CFG_BLOCK_MAX_SIZE;
  202. struct mtd_info *mtd = get_nand_dev_by_index(0);
  203. if (!mtd)
  204. return -ENODEV;
  205. /* Read production parameter config block from NAND page */
  206. return nand_read_skip_bad(mtd, CONFIG_TDX_CFG_BLOCK_OFFSET,
  207. &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
  208. config_block);
  209. }
  210. static int write_tdx_cfg_block_to_nand(unsigned char *config_block)
  211. {
  212. size_t size = TDX_CFG_BLOCK_MAX_SIZE;
  213. /* Write production parameter config block to NAND page */
  214. return nand_write_skip_bad(get_nand_dev_by_index(0),
  215. CONFIG_TDX_CFG_BLOCK_OFFSET,
  216. &size, NULL, TDX_CFG_BLOCK_MAX_SIZE,
  217. config_block, WITH_WR_VERIFY);
  218. }
  219. #endif
  220. #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_NOR
  221. static int read_tdx_cfg_block_from_nor(unsigned char *config_block)
  222. {
  223. /* Read production parameter config block from NOR flash */
  224. memcpy(config_block, (void *)CONFIG_TDX_CFG_BLOCK_OFFSET,
  225. TDX_CFG_BLOCK_MAX_SIZE);
  226. return 0;
  227. }
  228. static int write_tdx_cfg_block_to_nor(unsigned char *config_block)
  229. {
  230. /* Write production parameter config block to NOR flash */
  231. return flash_write((void *)config_block, CONFIG_TDX_CFG_BLOCK_OFFSET,
  232. TDX_CFG_BLOCK_MAX_SIZE);
  233. }
  234. #endif
  235. #ifdef CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM
  236. static int read_tdx_cfg_block_from_eeprom(unsigned char *config_block)
  237. {
  238. return read_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
  239. TDX_CFG_BLOCK_MAX_SIZE);
  240. }
  241. static int write_tdx_cfg_block_to_eeprom(unsigned char *config_block)
  242. {
  243. return write_tdx_eeprom_data(TDX_EEPROM_ID_MODULE, 0x0, config_block,
  244. TDX_CFG_BLOCK_MAX_SIZE);
  245. }
  246. #endif
  247. int read_tdx_cfg_block(void)
  248. {
  249. int ret = 0;
  250. u8 *config_block = NULL;
  251. struct toradex_tag *tag;
  252. size_t size = TDX_CFG_BLOCK_MAX_SIZE;
  253. int offset;
  254. /* Allocate RAM area for config block */
  255. config_block = memalign(ARCH_DMA_MINALIGN, size);
  256. if (!config_block) {
  257. printf("Not enough malloc space available!\n");
  258. return -ENOMEM;
  259. }
  260. memset(config_block, 0, size);
  261. #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
  262. ret = tdx_cfg_block_mmc_storage(config_block, 0);
  263. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
  264. ret = read_tdx_cfg_block_from_nand(config_block);
  265. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
  266. ret = read_tdx_cfg_block_from_nor(config_block);
  267. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
  268. ret = read_tdx_cfg_block_from_eeprom(config_block);
  269. #else
  270. ret = -EINVAL;
  271. #endif
  272. if (ret)
  273. goto out;
  274. /* Expect a valid tag first */
  275. tag = (struct toradex_tag *)config_block;
  276. if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
  277. valid_cfgblock = false;
  278. ret = -EINVAL;
  279. goto out;
  280. }
  281. valid_cfgblock = true;
  282. offset = 4;
  283. /*
  284. * check if there is enough space for storing tag and value of the
  285. * biggest element
  286. */
  287. while (offset + sizeof(struct toradex_tag) +
  288. sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
  289. tag = (struct toradex_tag *)(config_block + offset);
  290. offset += 4;
  291. if (tag->id == TAG_INVALID)
  292. break;
  293. if (tag->flags == TAG_FLAG_VALID) {
  294. switch (tag->id) {
  295. case TAG_MAC:
  296. memcpy(&tdx_eth_addr, config_block + offset,
  297. 6);
  298. /* NIC part of MAC address is serial number */
  299. tdx_serial = ntohl(tdx_eth_addr.nic) >> 8;
  300. break;
  301. case TAG_HW:
  302. memcpy(&tdx_hw_tag, config_block + offset, 8);
  303. break;
  304. }
  305. }
  306. /* Get to next tag according to current tags length */
  307. offset += tag->len * 4;
  308. }
  309. /* Cap product id to avoid issues with a yet unknown one */
  310. if (tdx_hw_tag.prodid >= (sizeof(toradex_modules) /
  311. sizeof(toradex_modules[0])))
  312. tdx_hw_tag.prodid = 0;
  313. out:
  314. free(config_block);
  315. return ret;
  316. }
  317. static int get_cfgblock_interactive(void)
  318. {
  319. char message[CONFIG_SYS_CBSIZE];
  320. char *soc;
  321. char it = 'n';
  322. char wb = 'n';
  323. int len = 0;
  324. /* Unknown module by default */
  325. tdx_hw_tag.prodid = 0;
  326. if (cpu_is_pxa27x())
  327. sprintf(message, "Is the module the 312 MHz version? [y/N] ");
  328. else
  329. it = 'y';
  330. #if defined(CONFIG_TARGET_APALIS_IMX8) || \
  331. defined(CONFIG_TARGET_APALIS_IMX8X) || \
  332. defined(CONFIG_TARGET_COLIBRI_IMX6ULL) || \
  333. defined(CONFIG_TARGET_COLIBRI_IMX8X) || \
  334. defined(CONFIG_TARGET_VERDIN_IMX8MM) || \
  335. defined(CONFIG_TARGET_VERDIN_IMX8MP)
  336. sprintf(message, "Does the module have Wi-Fi / Bluetooth? [y/N] ");
  337. len = cli_readline(message);
  338. wb = console_buffer[0];
  339. #endif
  340. soc = env_get("soc");
  341. if (!strcmp("mx6", soc)) {
  342. #ifdef CONFIG_TARGET_APALIS_IMX6
  343. if (it == 'y' || it == 'Y') {
  344. if (is_cpu_type(MXC_CPU_MX6Q))
  345. tdx_hw_tag.prodid = APALIS_IMX6Q_IT;
  346. else
  347. tdx_hw_tag.prodid = APALIS_IMX6D_IT;
  348. } else {
  349. if (is_cpu_type(MXC_CPU_MX6Q))
  350. tdx_hw_tag.prodid = APALIS_IMX6Q;
  351. else
  352. tdx_hw_tag.prodid = APALIS_IMX6D;
  353. }
  354. #elif CONFIG_TARGET_COLIBRI_IMX6
  355. if (it == 'y' || it == 'Y') {
  356. if (is_cpu_type(MXC_CPU_MX6DL))
  357. tdx_hw_tag.prodid = COLIBRI_IMX6DL_IT;
  358. else if (is_cpu_type(MXC_CPU_MX6SOLO))
  359. tdx_hw_tag.prodid = COLIBRI_IMX6S_IT;
  360. } else {
  361. if (is_cpu_type(MXC_CPU_MX6DL))
  362. tdx_hw_tag.prodid = COLIBRI_IMX6DL;
  363. else if (is_cpu_type(MXC_CPU_MX6SOLO))
  364. tdx_hw_tag.prodid = COLIBRI_IMX6S;
  365. }
  366. #elif CONFIG_TARGET_COLIBRI_IMX6ULL
  367. if (it == 'y' || it == 'Y') {
  368. if (wb == 'y' || wb == 'Y')
  369. tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT_IT;
  370. else
  371. tdx_hw_tag.prodid = COLIBRI_IMX6ULL_IT;
  372. } else {
  373. if (wb == 'y' || wb == 'Y')
  374. tdx_hw_tag.prodid = COLIBRI_IMX6ULL_WIFI_BT;
  375. else
  376. tdx_hw_tag.prodid = COLIBRI_IMX6ULL;
  377. }
  378. #endif
  379. } else if (!strcmp("imx7d", soc))
  380. tdx_hw_tag.prodid = COLIBRI_IMX7D;
  381. else if (!strcmp("imx7s", soc))
  382. tdx_hw_tag.prodid = COLIBRI_IMX7S;
  383. else if (is_cpu_type(MXC_CPU_IMX8QM)) {
  384. if (it == 'y' || it == 'Y') {
  385. if (wb == 'y' || wb == 'Y')
  386. tdx_hw_tag.prodid = APALIS_IMX8QM_WIFI_BT_IT;
  387. else
  388. tdx_hw_tag.prodid = APALIS_IMX8QM_IT;
  389. } else {
  390. if (wb == 'y' || wb == 'Y')
  391. tdx_hw_tag.prodid = APALIS_IMX8QP_WIFI_BT;
  392. else
  393. tdx_hw_tag.prodid = APALIS_IMX8QP;
  394. }
  395. } else if (is_cpu_type(MXC_CPU_IMX8QXP)) {
  396. #ifdef CONFIG_TARGET_APALIS_IMX8X
  397. if (it == 'y' || it == 'Y' || wb == 'y' || wb == 'Y') {
  398. tdx_hw_tag.prodid = APALIS_IMX8QXP_WIFI_BT_IT;
  399. } else {
  400. if (gd->ram_size == 0x40000000)
  401. tdx_hw_tag.prodid = APALIS_IMX8DXP;
  402. else
  403. tdx_hw_tag.prodid = APALIS_IMX8QXP;
  404. }
  405. #elif CONFIG_TARGET_COLIBRI_IMX8X
  406. if (it == 'y' || it == 'Y') {
  407. if (wb == 'y' || wb == 'Y')
  408. tdx_hw_tag.prodid = COLIBRI_IMX8QXP_WIFI_BT_IT;
  409. else
  410. tdx_hw_tag.prodid = COLIBRI_IMX8QXP_IT;
  411. } else {
  412. if (wb == 'y' || wb == 'Y')
  413. tdx_hw_tag.prodid = COLIBRI_IMX8DX_WIFI_BT;
  414. else
  415. tdx_hw_tag.prodid = COLIBRI_IMX8DX;
  416. }
  417. #endif
  418. } else if (is_cpu_type(MXC_CPU_IMX8MMDL)) {
  419. if (wb == 'y' || wb == 'Y')
  420. tdx_hw_tag.prodid = VERDIN_IMX8MMDL_WIFI_BT_IT;
  421. else
  422. tdx_hw_tag.prodid = VERDIN_IMX8MMDL;
  423. } else if (is_cpu_type(MXC_CPU_IMX8MM)) {
  424. if (wb == 'y' || wb == 'Y')
  425. tdx_hw_tag.prodid = VERDIN_IMX8MMQ_WIFI_BT_IT;
  426. else
  427. tdx_hw_tag.prodid = VERDIN_IMX8MMQ_IT;
  428. } else if (is_cpu_type(MXC_CPU_IMX8MN)) {
  429. tdx_hw_tag.prodid = VERDIN_IMX8MNQ_WIFI_BT;
  430. } else if (is_cpu_type(MXC_CPU_IMX8MP)) {
  431. if (wb == 'y' || wb == 'Y')
  432. tdx_hw_tag.prodid = VERDIN_IMX8MPQ_WIFI_BT_IT;
  433. else
  434. tdx_hw_tag.prodid = VERDIN_IMX8MPQ;
  435. } else if (!strcmp("tegra20", soc)) {
  436. if (it == 'y' || it == 'Y')
  437. if (gd->ram_size == 0x10000000)
  438. tdx_hw_tag.prodid = COLIBRI_T20_256MB_IT;
  439. else
  440. tdx_hw_tag.prodid = COLIBRI_T20_512MB_IT;
  441. else
  442. if (gd->ram_size == 0x10000000)
  443. tdx_hw_tag.prodid = COLIBRI_T20_256MB;
  444. else
  445. tdx_hw_tag.prodid = COLIBRI_T20_512MB;
  446. } else if (cpu_is_pxa27x()) {
  447. if (it == 'y' || it == 'Y')
  448. tdx_hw_tag.prodid = COLIBRI_PXA270_312MHZ;
  449. else
  450. tdx_hw_tag.prodid = COLIBRI_PXA270_520MHZ;
  451. }
  452. #ifdef CONFIG_MACH_TYPE
  453. else if (!strcmp("tegra30", soc)) {
  454. if (CONFIG_MACH_TYPE == MACH_TYPE_APALIS_T30) {
  455. if (it == 'y' || it == 'Y')
  456. tdx_hw_tag.prodid = APALIS_T30_IT;
  457. else
  458. if (gd->ram_size == 0x40000000)
  459. tdx_hw_tag.prodid = APALIS_T30_1GB;
  460. else
  461. tdx_hw_tag.prodid = APALIS_T30_2GB;
  462. } else {
  463. if (it == 'y' || it == 'Y')
  464. tdx_hw_tag.prodid = COLIBRI_T30_IT;
  465. else
  466. tdx_hw_tag.prodid = COLIBRI_T30;
  467. }
  468. }
  469. #endif /* CONFIG_MACH_TYPE */
  470. else if (!strcmp("tegra124", soc)) {
  471. tdx_hw_tag.prodid = APALIS_TK1_2GB;
  472. } else if (!strcmp("vf500", soc)) {
  473. if (it == 'y' || it == 'Y')
  474. tdx_hw_tag.prodid = COLIBRI_VF50_IT;
  475. else
  476. tdx_hw_tag.prodid = COLIBRI_VF50;
  477. } else if (!strcmp("vf610", soc)) {
  478. if (it == 'y' || it == 'Y')
  479. tdx_hw_tag.prodid = COLIBRI_VF61_IT;
  480. else
  481. tdx_hw_tag.prodid = COLIBRI_VF61;
  482. }
  483. if (!tdx_hw_tag.prodid) {
  484. printf("Module type not detectable due to unknown SoC\n");
  485. return -1;
  486. }
  487. while (len < 4) {
  488. sprintf(message, "Enter the module version (e.g. V1.1B): V");
  489. len = cli_readline(message);
  490. }
  491. tdx_hw_tag.ver_major = console_buffer[0] - '0';
  492. tdx_hw_tag.ver_minor = console_buffer[2] - '0';
  493. tdx_hw_tag.ver_assembly = console_buffer[3] - 'A';
  494. if (cpu_is_pxa27x() && tdx_hw_tag.ver_major == 1)
  495. tdx_hw_tag.prodid -= (COLIBRI_PXA270_312MHZ -
  496. COLIBRI_PXA270_V1_312MHZ);
  497. while (len < 8) {
  498. sprintf(message, "Enter module serial number: ");
  499. len = cli_readline(message);
  500. }
  501. tdx_serial = dectoul(console_buffer, NULL);
  502. return 0;
  503. }
  504. static int get_cfgblock_barcode(char *barcode, struct toradex_hw *tag,
  505. u32 *serial)
  506. {
  507. char revision[3] = {barcode[6], barcode[7], '\0'};
  508. if (strlen(barcode) < 16) {
  509. printf("Argument too short, barcode is 16 chars long\n");
  510. return -1;
  511. }
  512. /* Get hardware information from the first 8 digits */
  513. tag->ver_major = barcode[4] - '0';
  514. tag->ver_minor = barcode[5] - '0';
  515. tag->ver_assembly = dectoul(revision, NULL);
  516. barcode[4] = '\0';
  517. tag->prodid = dectoul(barcode, NULL);
  518. /* Parse second part of the barcode (serial number */
  519. barcode += 8;
  520. *serial = dectoul(barcode, NULL);
  521. return 0;
  522. }
  523. static int write_tag(u8 *config_block, int *offset, int tag_id,
  524. u8 *tag_data, size_t tag_data_size)
  525. {
  526. struct toradex_tag *tag;
  527. if (!offset || !config_block)
  528. return -EINVAL;
  529. tag = (struct toradex_tag *)(config_block + *offset);
  530. tag->id = tag_id;
  531. tag->flags = TAG_FLAG_VALID;
  532. /* len is provided as number of 32bit values after the tag */
  533. tag->len = (tag_data_size + sizeof(u32) - 1) / sizeof(u32);
  534. *offset += sizeof(struct toradex_tag);
  535. if (tag_data && tag_data_size) {
  536. memcpy(config_block + *offset, tag_data,
  537. tag_data_size);
  538. *offset += tag_data_size;
  539. }
  540. return 0;
  541. }
  542. #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
  543. int read_tdx_cfg_block_carrier(void)
  544. {
  545. int ret = 0;
  546. u8 *config_block = NULL;
  547. struct toradex_tag *tag;
  548. size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
  549. int offset;
  550. /* Allocate RAM area for carrier config block */
  551. config_block = memalign(ARCH_DMA_MINALIGN, size);
  552. if (!config_block) {
  553. printf("Not enough malloc space available!\n");
  554. return -ENOMEM;
  555. }
  556. memset(config_block, 0, size);
  557. ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
  558. size);
  559. if (ret)
  560. return ret;
  561. /* Expect a valid tag first */
  562. tag = (struct toradex_tag *)config_block;
  563. if (tag->flags != TAG_FLAG_VALID || tag->id != TAG_VALID) {
  564. valid_cfgblock_carrier = false;
  565. ret = -EINVAL;
  566. goto out;
  567. }
  568. valid_cfgblock_carrier = true;
  569. offset = 4;
  570. while (offset + sizeof(struct toradex_tag) +
  571. sizeof(struct toradex_hw) < TDX_CFG_BLOCK_MAX_SIZE) {
  572. tag = (struct toradex_tag *)(config_block + offset);
  573. offset += 4;
  574. if (tag->id == TAG_INVALID)
  575. break;
  576. if (tag->flags == TAG_FLAG_VALID) {
  577. switch (tag->id) {
  578. case TAG_CAR_SERIAL:
  579. memcpy(&tdx_car_serial, config_block + offset,
  580. sizeof(tdx_car_serial));
  581. break;
  582. case TAG_HW:
  583. memcpy(&tdx_car_hw_tag, config_block +
  584. offset, 8);
  585. break;
  586. }
  587. }
  588. /* Get to next tag according to current tags length */
  589. offset += tag->len * 4;
  590. }
  591. out:
  592. free(config_block);
  593. return ret;
  594. }
  595. int check_pid8_sanity(char *pid8)
  596. {
  597. char s_carrierid_verdin_dev[5];
  598. char s_carrierid_dahlia[5];
  599. sprintf(s_carrierid_verdin_dev, "0%d", VERDIN_DEVELOPMENT_BOARD);
  600. sprintf(s_carrierid_dahlia, "0%d", DAHLIA);
  601. /* sane value check, first 4 chars which represent carrier id */
  602. if (!strncmp(pid8, s_carrierid_verdin_dev, 4))
  603. return 0;
  604. if (!strncmp(pid8, s_carrierid_dahlia, 4))
  605. return 0;
  606. return -EINVAL;
  607. }
  608. int try_migrate_tdx_cfg_block_carrier(void)
  609. {
  610. char pid8[8];
  611. int offset = 0;
  612. int ret = CMD_RET_SUCCESS;
  613. size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
  614. u8 *config_block;
  615. memset(pid8, 0x0, 8);
  616. ret = read_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, (u8 *)pid8, 8);
  617. if (ret)
  618. return ret;
  619. if (check_pid8_sanity(pid8))
  620. return -EINVAL;
  621. /* Allocate RAM area for config block */
  622. config_block = memalign(ARCH_DMA_MINALIGN, size);
  623. if (!config_block) {
  624. printf("Not enough malloc space available!\n");
  625. return CMD_RET_FAILURE;
  626. }
  627. memset(config_block, 0xff, size);
  628. /* we try parse PID8 concatenating zeroed serial number */
  629. tdx_car_hw_tag.ver_major = pid8[4] - '0';
  630. tdx_car_hw_tag.ver_minor = pid8[5] - '0';
  631. tdx_car_hw_tag.ver_assembly = pid8[7] - '0';
  632. pid8[4] = '\0';
  633. tdx_car_hw_tag.prodid = dectoul(pid8, NULL);
  634. /* Valid Tag */
  635. write_tag(config_block, &offset, TAG_VALID, NULL, 0);
  636. /* Product Tag */
  637. write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
  638. sizeof(tdx_car_hw_tag));
  639. /* Serial Tag */
  640. write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
  641. sizeof(tdx_car_serial));
  642. memset(config_block + offset, 0, 32 - offset);
  643. ret = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
  644. size);
  645. if (ret) {
  646. printf("Failed to write Toradex Extra config block: %d\n",
  647. ret);
  648. ret = CMD_RET_FAILURE;
  649. goto out;
  650. }
  651. printf("Successfully migrated to Toradex Config Block from PID8\n");
  652. out:
  653. free(config_block);
  654. return ret;
  655. }
  656. static int get_cfgblock_carrier_interactive(void)
  657. {
  658. char message[CONFIG_SYS_CBSIZE];
  659. int len;
  660. printf("Supported carrier boards:\n");
  661. printf("CARRIER BOARD NAME\t\t [ID]\n");
  662. for (int i = 0; i < sizeof(toradex_carrier_boards) /
  663. sizeof(toradex_carrier_boards[0]); i++)
  664. if (toradex_carrier_boards[i])
  665. printf("%s \t\t [%d]\n", toradex_carrier_boards[i], i);
  666. sprintf(message, "Choose your carrier board (provide ID): ");
  667. len = cli_readline(message);
  668. tdx_car_hw_tag.prodid = dectoul(console_buffer, NULL);
  669. do {
  670. sprintf(message, "Enter carrier board version (e.g. V1.1B): V");
  671. len = cli_readline(message);
  672. } while (len < 4);
  673. tdx_car_hw_tag.ver_major = console_buffer[0] - '0';
  674. tdx_car_hw_tag.ver_minor = console_buffer[2] - '0';
  675. tdx_car_hw_tag.ver_assembly = console_buffer[3] - 'A';
  676. while (len < 8) {
  677. sprintf(message, "Enter carrier board serial number: ");
  678. len = cli_readline(message);
  679. }
  680. tdx_car_serial = dectoul(console_buffer, NULL);
  681. return 0;
  682. }
  683. static int do_cfgblock_carrier_create(struct cmd_tbl *cmdtp, int flag, int argc,
  684. char * const argv[])
  685. {
  686. u8 *config_block;
  687. size_t size = TDX_CFG_BLOCK_EXTRA_MAX_SIZE;
  688. int offset = 0;
  689. int ret = CMD_RET_SUCCESS;
  690. int err;
  691. int force_overwrite = 0;
  692. if (argc >= 3) {
  693. if (argv[2][0] == '-' && argv[2][1] == 'y')
  694. force_overwrite = 1;
  695. }
  696. /* Allocate RAM area for config block */
  697. config_block = memalign(ARCH_DMA_MINALIGN, size);
  698. if (!config_block) {
  699. printf("Not enough malloc space available!\n");
  700. return CMD_RET_FAILURE;
  701. }
  702. memset(config_block, 0xff, size);
  703. read_tdx_cfg_block_carrier();
  704. if (valid_cfgblock_carrier && !force_overwrite) {
  705. char message[CONFIG_SYS_CBSIZE];
  706. sprintf(message, "A valid Toradex Carrier config block is present, still recreate? [y/N] ");
  707. if (!cli_readline(message))
  708. goto out;
  709. if (console_buffer[0] != 'y' &&
  710. console_buffer[0] != 'Y')
  711. goto out;
  712. }
  713. if (argc < 3 || (force_overwrite && argc < 4)) {
  714. err = get_cfgblock_carrier_interactive();
  715. } else {
  716. if (force_overwrite)
  717. err = get_cfgblock_barcode(argv[3], &tdx_car_hw_tag,
  718. &tdx_car_serial);
  719. else
  720. err = get_cfgblock_barcode(argv[2], &tdx_car_hw_tag,
  721. &tdx_car_serial);
  722. }
  723. if (err) {
  724. ret = CMD_RET_FAILURE;
  725. goto out;
  726. }
  727. /* Valid Tag */
  728. write_tag(config_block, &offset, TAG_VALID, NULL, 0);
  729. /* Product Tag */
  730. write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_car_hw_tag,
  731. sizeof(tdx_car_hw_tag));
  732. /* Serial Tag */
  733. write_tag(config_block, &offset, TAG_CAR_SERIAL, (u8 *)&tdx_car_serial,
  734. sizeof(tdx_car_serial));
  735. memset(config_block + offset, 0, 32 - offset);
  736. err = write_tdx_eeprom_data(TDX_EEPROM_ID_CARRIER, 0x0, config_block,
  737. size);
  738. if (err) {
  739. printf("Failed to write Toradex Extra config block: %d\n",
  740. ret);
  741. ret = CMD_RET_FAILURE;
  742. goto out;
  743. }
  744. printf("Toradex Extra config block successfully written\n");
  745. out:
  746. free(config_block);
  747. return ret;
  748. }
  749. #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
  750. static int do_cfgblock_create(struct cmd_tbl *cmdtp, int flag, int argc,
  751. char * const argv[])
  752. {
  753. u8 *config_block;
  754. size_t size = TDX_CFG_BLOCK_MAX_SIZE;
  755. int offset = 0;
  756. int ret = CMD_RET_SUCCESS;
  757. int err;
  758. int force_overwrite = 0;
  759. if (argc >= 3) {
  760. #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
  761. if (!strcmp(argv[2], "carrier"))
  762. return do_cfgblock_carrier_create(cmdtp, flag,
  763. --argc, ++argv);
  764. #endif /* CONFIG_TDX_CFG_BLOCK_EXTRA */
  765. if (argv[2][0] == '-' && argv[2][1] == 'y')
  766. force_overwrite = 1;
  767. }
  768. /* Allocate RAM area for config block */
  769. config_block = memalign(ARCH_DMA_MINALIGN, size);
  770. if (!config_block) {
  771. printf("Not enough malloc space available!\n");
  772. return CMD_RET_FAILURE;
  773. }
  774. memset(config_block, 0xff, size);
  775. read_tdx_cfg_block();
  776. if (valid_cfgblock) {
  777. #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
  778. /*
  779. * On NAND devices, recreation is only allowed if the page is
  780. * empty (config block invalid...)
  781. */
  782. printf("NAND erase block %d need to be erased before creating a Toradex config block\n",
  783. CONFIG_TDX_CFG_BLOCK_OFFSET /
  784. get_nand_dev_by_index(0)->erasesize);
  785. goto out;
  786. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
  787. /*
  788. * On NOR devices, recreation is only allowed if the sector is
  789. * empty and write protection is off (config block invalid...)
  790. */
  791. printf("NOR sector at offset 0x%02x need to be erased and unprotected before creating a Toradex config block\n",
  792. CONFIG_TDX_CFG_BLOCK_OFFSET);
  793. goto out;
  794. #else
  795. if (!force_overwrite) {
  796. char message[CONFIG_SYS_CBSIZE];
  797. sprintf(message,
  798. "A valid Toradex config block is present, still recreate? [y/N] ");
  799. if (!cli_readline(message))
  800. goto out;
  801. if (console_buffer[0] != 'y' &&
  802. console_buffer[0] != 'Y')
  803. goto out;
  804. }
  805. #endif
  806. }
  807. /* Parse new Toradex config block data... */
  808. if (argc < 3 || (force_overwrite && argc < 4)) {
  809. err = get_cfgblock_interactive();
  810. } else {
  811. if (force_overwrite)
  812. err = get_cfgblock_barcode(argv[3], &tdx_hw_tag,
  813. &tdx_serial);
  814. else
  815. err = get_cfgblock_barcode(argv[2], &tdx_hw_tag,
  816. &tdx_serial);
  817. }
  818. if (err) {
  819. ret = CMD_RET_FAILURE;
  820. goto out;
  821. }
  822. /* Convert serial number to MAC address (the storage format) */
  823. tdx_eth_addr.oui = htonl(0x00142dUL << 8);
  824. tdx_eth_addr.nic = htonl(tdx_serial << 8);
  825. /* Valid Tag */
  826. write_tag(config_block, &offset, TAG_VALID, NULL, 0);
  827. /* Product Tag */
  828. write_tag(config_block, &offset, TAG_HW, (u8 *)&tdx_hw_tag,
  829. sizeof(tdx_hw_tag));
  830. /* MAC Tag */
  831. write_tag(config_block, &offset, TAG_MAC, (u8 *)&tdx_eth_addr,
  832. sizeof(tdx_eth_addr));
  833. memset(config_block + offset, 0, 32 - offset);
  834. #if defined(CONFIG_TDX_CFG_BLOCK_IS_IN_MMC)
  835. err = tdx_cfg_block_mmc_storage(config_block, 1);
  836. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NAND)
  837. err = write_tdx_cfg_block_to_nand(config_block);
  838. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_NOR)
  839. err = write_tdx_cfg_block_to_nor(config_block);
  840. #elif defined(CONFIG_TDX_CFG_BLOCK_IS_IN_EEPROM)
  841. err = write_tdx_cfg_block_to_eeprom(config_block);
  842. #else
  843. err = -EINVAL;
  844. #endif
  845. if (err) {
  846. printf("Failed to write Toradex config block: %d\n", ret);
  847. ret = CMD_RET_FAILURE;
  848. goto out;
  849. }
  850. printf("Toradex config block successfully written\n");
  851. out:
  852. free(config_block);
  853. return ret;
  854. }
  855. static int do_cfgblock(struct cmd_tbl *cmdtp, int flag, int argc,
  856. char *const argv[])
  857. {
  858. int ret;
  859. if (argc < 2)
  860. return CMD_RET_USAGE;
  861. if (!strcmp(argv[1], "create")) {
  862. return do_cfgblock_create(cmdtp, flag, argc, argv);
  863. } else if (!strcmp(argv[1], "reload")) {
  864. ret = read_tdx_cfg_block();
  865. if (ret) {
  866. printf("Failed to reload Toradex config block: %d\n",
  867. ret);
  868. return CMD_RET_FAILURE;
  869. }
  870. return CMD_RET_SUCCESS;
  871. }
  872. return CMD_RET_USAGE;
  873. }
  874. U_BOOT_CMD(
  875. cfgblock, 5, 0, do_cfgblock,
  876. "Toradex config block handling commands",
  877. "create [-y] [barcode] - (Re-)create Toradex config block\n"
  878. "create carrier [-y] [barcode] - (Re-)create Toradex Carrier config block\n"
  879. "cfgblock reload - Reload Toradex config block from flash"
  880. );