factoryset.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. *
  4. * Read FactorySet information from EEPROM into global structure.
  5. * (C) Copyright 2013 Siemens Schweiz AG
  6. */
  7. #if !defined(CONFIG_SPL_BUILD)
  8. #include <common.h>
  9. #include <env.h>
  10. #include <dm.h>
  11. #include <env_internal.h>
  12. #include <i2c.h>
  13. #include <log.h>
  14. #include <asm/io.h>
  15. #if !CONFIG_IS_ENABLED(TARGET_GIEDI) && !CONFIG_IS_ENABLED(TARGET_DENEB)
  16. #include <asm/arch/cpu.h>
  17. #endif
  18. #include <asm/arch/sys_proto.h>
  19. #include <asm/unaligned.h>
  20. #include <net.h>
  21. #include <errno.h>
  22. #include <g_dnl.h>
  23. #include "factoryset.h"
  24. #define EEPR_PG_SZ 0x80
  25. #define EEPROM_FATORYSET_OFFSET 0x400
  26. #define OFF_PG EEPROM_FATORYSET_OFFSET/EEPR_PG_SZ
  27. /* Global variable that contains necessary information from FactorySet */
  28. struct factorysetcontainer factory_dat;
  29. #define fact_get_char(i) *((char *)&eeprom_buf[i])
  30. static int fact_match(unsigned char *eeprom_buf, uchar *s1, int i2)
  31. {
  32. if (s1 == NULL)
  33. return -1;
  34. while (*s1 == fact_get_char(i2++))
  35. if (*s1++ == '=')
  36. return i2;
  37. if (*s1 == '\0' && fact_get_char(i2-1) == '=')
  38. return i2;
  39. return -1;
  40. }
  41. static int get_factory_val(unsigned char *eeprom_buf, int size, uchar *name,
  42. uchar *buf, int len)
  43. {
  44. int i, nxt = 0;
  45. for (i = 0; fact_get_char(i) != '\0'; i = nxt + 1) {
  46. int val, n;
  47. for (nxt = i; fact_get_char(nxt) != '\0'; ++nxt) {
  48. if (nxt >= size)
  49. return -1;
  50. }
  51. val = fact_match(eeprom_buf, (uchar *)name, i);
  52. if (val < 0)
  53. continue;
  54. /* found; copy out */
  55. for (n = 0; n < len; ++n, ++buf) {
  56. *buf = fact_get_char(val++);
  57. if (*buf == '\0')
  58. return n;
  59. }
  60. if (n)
  61. *--buf = '\0';
  62. printf("env_buf [%d bytes] too small for value of \"%s\"\n",
  63. len, name);
  64. return n;
  65. }
  66. return -1;
  67. }
  68. static
  69. int get_factory_record_val(unsigned char *eeprom_buf, int size, uchar *record,
  70. uchar *name, uchar *buf, int len)
  71. {
  72. int ret = -1;
  73. int i, nxt = 0;
  74. int c;
  75. unsigned char end = 0xff;
  76. unsigned char tmp;
  77. for (i = 0; fact_get_char(i) != end; i = nxt) {
  78. nxt = i + 1;
  79. if (fact_get_char(i) == '>') {
  80. int pos;
  81. int endpos;
  82. int z;
  83. int level = 0;
  84. c = strncmp((char *)&eeprom_buf[i + 1], (char *)record,
  85. strlen((char *)record));
  86. if (c == 0) {
  87. /* record found */
  88. pos = i + strlen((char *)record) + 2;
  89. nxt = pos;
  90. /* search for "<" */
  91. c = -1;
  92. for (z = pos; fact_get_char(z) != end; z++) {
  93. if (fact_get_char(z) == '<') {
  94. if (level == 0) {
  95. endpos = z;
  96. nxt = endpos;
  97. c = 0;
  98. break;
  99. } else {
  100. level--;
  101. }
  102. }
  103. if (fact_get_char(z) == '>')
  104. level++;
  105. }
  106. } else {
  107. continue;
  108. }
  109. if (c == 0) {
  110. /* end found -> call get_factory_val */
  111. tmp = eeprom_buf[endpos];
  112. eeprom_buf[endpos] = end;
  113. ret = get_factory_val(&eeprom_buf[pos],
  114. endpos - pos, name, buf, len);
  115. /* fix buffer */
  116. eeprom_buf[endpos] = tmp;
  117. debug("%s: %s.%s = %s\n",
  118. __func__, record, name, buf);
  119. return ret;
  120. }
  121. }
  122. }
  123. return ret;
  124. }
  125. int factoryset_read_eeprom(int i2c_addr)
  126. {
  127. int i, pages = 0, size = 0;
  128. unsigned char eeprom_buf[0x3c00], hdr[4], buf[MAX_STRING_LENGTH];
  129. unsigned char *cp, *cp1;
  130. #if CONFIG_IS_ENABLED(DM_I2C)
  131. struct udevice *bus, *dev;
  132. int ret;
  133. #endif
  134. #if defined(CONFIG_DFU_OVER_USB)
  135. factory_dat.usb_vendor_id = CONFIG_USB_GADGET_VENDOR_NUM;
  136. factory_dat.usb_product_id = CONFIG_USB_GADGET_PRODUCT_NUM;
  137. #endif
  138. #if CONFIG_IS_ENABLED(DM_I2C)
  139. ret = uclass_get_device_by_seq(UCLASS_I2C, EEPROM_I2C_BUS, &bus);
  140. if (ret)
  141. goto err;
  142. ret = dm_i2c_probe(bus, i2c_addr, 0, &dev);
  143. if (ret)
  144. goto err;
  145. ret = i2c_set_chip_offset_len(dev, 2);
  146. if (ret)
  147. goto err;
  148. ret = dm_i2c_read(dev, EEPROM_FATORYSET_OFFSET, hdr, sizeof(hdr));
  149. if (ret)
  150. goto err;
  151. #else
  152. if (i2c_probe(i2c_addr))
  153. goto err;
  154. if (i2c_read(i2c_addr, EEPROM_FATORYSET_OFFSET, 2, hdr, sizeof(hdr)))
  155. goto err;
  156. #endif
  157. if ((hdr[0] != 0x99) || (hdr[1] != 0x80)) {
  158. printf("FactorySet is not right in eeprom.\n");
  159. return 1;
  160. }
  161. /* get FactorySet size */
  162. size = (hdr[2] << 8) + hdr[3] + sizeof(hdr);
  163. if (size > 0x3bfa)
  164. size = 0x3bfa;
  165. pages = size / EEPR_PG_SZ;
  166. /*
  167. * read the eeprom using i2c
  168. * I can not read entire eeprom in once, so separate into several
  169. * times. Furthermore, fetch eeprom take longer time, so we fetch
  170. * data after every time we got a record from eeprom
  171. */
  172. debug("Read eeprom page :\n");
  173. for (i = 0; i < pages; i++) {
  174. #if CONFIG_IS_ENABLED(DM_I2C)
  175. ret = dm_i2c_read(dev, (OFF_PG + i) * EEPR_PG_SZ,
  176. eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ);
  177. if (ret)
  178. goto err;
  179. #else
  180. if (i2c_read(i2c_addr, (OFF_PG + i) * EEPR_PG_SZ, 2,
  181. eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ))
  182. goto err;
  183. #endif
  184. }
  185. if (size % EEPR_PG_SZ) {
  186. #if CONFIG_IS_ENABLED(DM_I2C)
  187. ret = dm_i2c_read(dev, (OFF_PG + pages) * EEPR_PG_SZ,
  188. eeprom_buf + (pages * EEPR_PG_SZ),
  189. size % EEPR_PG_SZ);
  190. if (ret)
  191. goto err;
  192. #else
  193. if (i2c_read(i2c_addr, (OFF_PG + pages) * EEPR_PG_SZ, 2,
  194. eeprom_buf + (pages * EEPR_PG_SZ),
  195. (size % EEPR_PG_SZ)))
  196. goto err;
  197. #endif
  198. }
  199. /* we do below just for eeprom align */
  200. for (i = 0; i < size; i++)
  201. if (eeprom_buf[i] == '\n')
  202. eeprom_buf[i] = 0;
  203. /* skip header */
  204. size -= sizeof(hdr);
  205. cp = (uchar *)eeprom_buf + sizeof(hdr);
  206. /* get mac address */
  207. get_factory_record_val(cp, size, (uchar *)"ETH1", (uchar *)"mac",
  208. buf, MAX_STRING_LENGTH);
  209. cp1 = buf;
  210. for (i = 0; i < 6; i++) {
  211. factory_dat.mac[i] = hextoul((char *)cp1, NULL);
  212. cp1 += 3;
  213. }
  214. #if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
  215. /* get mac address for WLAN */
  216. ret = get_factory_record_val(cp, size, (uchar *)"WLAN1", (uchar *)"mac",
  217. buf, MAX_STRING_LENGTH);
  218. if (ret > 0) {
  219. cp1 = buf;
  220. for (i = 0; i < 6; i++) {
  221. factory_dat.mac_wlan[i] = hextoul((char *)cp1, NULL);
  222. cp1 += 3;
  223. }
  224. }
  225. #endif
  226. #if defined(CONFIG_DFU_OVER_USB)
  227. /* read vid and pid for dfu mode */
  228. if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
  229. (uchar *)"vid", buf,
  230. MAX_STRING_LENGTH)) {
  231. factory_dat.usb_vendor_id = hextoul((char *)buf, NULL);
  232. }
  233. if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
  234. (uchar *)"pid", buf,
  235. MAX_STRING_LENGTH)) {
  236. factory_dat.usb_product_id = hextoul((char *)buf, NULL);
  237. }
  238. printf("DFU USB: VID = 0x%4x, PID = 0x%4x\n", factory_dat.usb_vendor_id,
  239. factory_dat.usb_product_id);
  240. #endif
  241. #if defined(CONFIG_VIDEO)
  242. if (0 <= get_factory_record_val(cp, size, (uchar *)"DISP1",
  243. (uchar *)"name", factory_dat.disp_name,
  244. MAX_STRING_LENGTH)) {
  245. debug("display name: %s\n", factory_dat.disp_name);
  246. }
  247. #endif
  248. if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
  249. (uchar *)"num", factory_dat.serial,
  250. MAX_STRING_LENGTH)) {
  251. debug("serial number: %s\n", factory_dat.serial);
  252. }
  253. if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
  254. (uchar *)"ver", buf,
  255. MAX_STRING_LENGTH)) {
  256. factory_dat.version = hextoul((char *)buf, NULL);
  257. debug("version number: %d\n", factory_dat.version);
  258. }
  259. /* Get ASN from factory set if available */
  260. if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
  261. (uchar *)"id", factory_dat.asn,
  262. MAX_STRING_LENGTH)) {
  263. debug("factoryset asn: %s\n", factory_dat.asn);
  264. } else {
  265. factory_dat.asn[0] = 0;
  266. }
  267. /* Get COMP/ver from factory set if available */
  268. if (0 <= get_factory_record_val(cp, size, (uchar *)"COMP",
  269. (uchar *)"ver",
  270. factory_dat.comp_version,
  271. MAX_STRING_LENGTH)) {
  272. debug("factoryset COMP/ver: %s\n", factory_dat.comp_version);
  273. } else {
  274. strcpy((char *)factory_dat.comp_version, "1.0");
  275. }
  276. return 0;
  277. err:
  278. printf("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\n");
  279. return 1;
  280. }
  281. static int get_mac_from_efuse(uint8_t mac[6])
  282. {
  283. #ifdef CONFIG_AM33XX
  284. struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
  285. uint32_t mac_hi, mac_lo;
  286. mac_lo = readl(&cdev->macid0l);
  287. mac_hi = readl(&cdev->macid0h);
  288. mac[0] = mac_hi & 0xFF;
  289. mac[1] = (mac_hi & 0xFF00) >> 8;
  290. mac[2] = (mac_hi & 0xFF0000) >> 16;
  291. mac[3] = (mac_hi & 0xFF000000) >> 24;
  292. mac[4] = mac_lo & 0xFF;
  293. mac[5] = (mac_lo & 0xFF00) >> 8;
  294. #else
  295. /* unhandled */
  296. memset(mac, 0, 6);
  297. #endif
  298. if (!is_valid_ethaddr(mac)) {
  299. puts("Warning: ethaddr not set by FactorySet or E-fuse. ");
  300. puts("Set <ethaddr> variable to overcome this.\n");
  301. return -1;
  302. }
  303. return 0;
  304. }
  305. static int factoryset_mac_env_set(void)
  306. {
  307. uint8_t mac_addr[6];
  308. /* Set mac from factoryset or try reading E-fuse */
  309. debug("FactorySet: Set mac address\n");
  310. if (is_valid_ethaddr(factory_dat.mac)) {
  311. memcpy(mac_addr, factory_dat.mac, 6);
  312. } else {
  313. debug("Warning: FactorySet: <ethaddr> not set. Fallback to E-fuse\n");
  314. if (get_mac_from_efuse(mac_addr) < 0)
  315. return -1;
  316. }
  317. eth_env_set_enetaddr("ethaddr", mac_addr);
  318. #if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
  319. eth_env_set_enetaddr("eth1addr", mac_addr);
  320. /* wlan mac */
  321. if (is_valid_ethaddr(factory_dat.mac_wlan))
  322. eth_env_set_enetaddr("eth2addr", factory_dat.mac_wlan);
  323. #endif
  324. return 0;
  325. }
  326. static void factoryset_dtb_env_set(void)
  327. {
  328. /* Set ASN in environment*/
  329. if (factory_dat.asn[0] != 0) {
  330. env_set("dtb_name", (char *)factory_dat.asn);
  331. } else {
  332. /* dtb suffix gets added in load script */
  333. env_set("dtb_name", "default");
  334. }
  335. }
  336. int factoryset_env_set(void)
  337. {
  338. int ret = 0;
  339. factoryset_dtb_env_set();
  340. if (factoryset_mac_env_set() < 0)
  341. ret = -1;
  342. return ret;
  343. }
  344. int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
  345. {
  346. put_unaligned(factory_dat.usb_vendor_id, &dev->idVendor);
  347. put_unaligned(factory_dat.usb_product_id, &dev->idProduct);
  348. g_dnl_set_serialnumber((char *)factory_dat.serial);
  349. return 0;
  350. }
  351. int g_dnl_get_board_bcd_device_number(int gcnum)
  352. {
  353. return factory_dat.version;
  354. }
  355. #endif /* defined(CONFIG_SPL_BUILD) */