factoryset.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. *
  3. * Read FactorySet information from EEPROM into global structure.
  4. * (C) Copyright 2013 Siemens Schweiz AG
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #if !defined(CONFIG_SPL_BUILD)
  9. #include <common.h>
  10. #include <i2c.h>
  11. #include <asm/io.h>
  12. #include <asm/arch/cpu.h>
  13. #include <asm/arch/sys_proto.h>
  14. #include <asm/unaligned.h>
  15. #include <net.h>
  16. #include <usbdescriptors.h>
  17. #include "factoryset.h"
  18. #define EEPR_PG_SZ 0x80
  19. #define EEPROM_FATORYSET_OFFSET 0x400
  20. #define OFF_PG EEPROM_FATORYSET_OFFSET/EEPR_PG_SZ
  21. /* Global variable that contains necessary information from FactorySet */
  22. struct factorysetcontainer factory_dat;
  23. #define fact_get_char(i) *((char *)&eeprom_buf[i])
  24. static int fact_match(unsigned char *eeprom_buf, uchar *s1, int i2)
  25. {
  26. if (s1 == NULL)
  27. return -1;
  28. while (*s1 == fact_get_char(i2++))
  29. if (*s1++ == '=')
  30. return i2;
  31. if (*s1 == '\0' && fact_get_char(i2-1) == '=')
  32. return i2;
  33. return -1;
  34. }
  35. static int get_factory_val(unsigned char *eeprom_buf, int size, uchar *name,
  36. uchar *buf, int len)
  37. {
  38. int i, nxt = 0;
  39. for (i = 0; fact_get_char(i) != '\0'; i = nxt + 1) {
  40. int val, n;
  41. for (nxt = i; fact_get_char(nxt) != '\0'; ++nxt) {
  42. if (nxt >= size)
  43. return -1;
  44. }
  45. val = fact_match(eeprom_buf, (uchar *)name, i);
  46. if (val < 0)
  47. continue;
  48. /* found; copy out */
  49. for (n = 0; n < len; ++n, ++buf) {
  50. *buf = fact_get_char(val++);
  51. if (*buf == '\0')
  52. return n;
  53. }
  54. if (n)
  55. *--buf = '\0';
  56. printf("env_buf [%d bytes] too small for value of \"%s\"\n",
  57. len, name);
  58. return n;
  59. }
  60. return -1;
  61. }
  62. static
  63. int get_factory_record_val(unsigned char *eeprom_buf, int size, uchar *record,
  64. uchar *name, uchar *buf, int len)
  65. {
  66. int ret = -1;
  67. int i, nxt = 0;
  68. int c;
  69. unsigned char end = 0xff;
  70. for (i = 0; fact_get_char(i) != end; i = nxt) {
  71. nxt = i + 1;
  72. if (fact_get_char(i) == '>') {
  73. int pos;
  74. int endpos;
  75. int z;
  76. c = strncmp((char *)&eeprom_buf[i + 1], (char *)record,
  77. strlen((char *)record));
  78. if (c == 0) {
  79. /* record found */
  80. pos = i + strlen((char *)record) + 2;
  81. nxt = pos;
  82. /* search for "<" */
  83. c = -1;
  84. for (z = pos; fact_get_char(z) != end; z++) {
  85. if ((fact_get_char(z) == '<') ||
  86. (fact_get_char(z) == '>')) {
  87. endpos = z;
  88. nxt = endpos;
  89. c = 0;
  90. break;
  91. }
  92. }
  93. }
  94. if (c == 0) {
  95. /* end found -> call get_factory_val */
  96. eeprom_buf[endpos] = end;
  97. ret = get_factory_val(&eeprom_buf[pos],
  98. size - pos, name, buf, len);
  99. /* fix buffer */
  100. eeprom_buf[endpos] = '<';
  101. debug("%s: %s.%s = %s\n",
  102. __func__, record, name, buf);
  103. return ret;
  104. }
  105. }
  106. }
  107. return ret;
  108. }
  109. int factoryset_read_eeprom(int i2c_addr)
  110. {
  111. int i, pages = 0, size = 0;
  112. unsigned char eeprom_buf[0x3c00], hdr[4], buf[MAX_STRING_LENGTH];
  113. unsigned char *cp, *cp1;
  114. #if defined(CONFIG_DFU_FUNCTION)
  115. factory_dat.usb_vendor_id = CONFIG_G_DNL_VENDOR_NUM;
  116. factory_dat.usb_product_id = CONFIG_G_DNL_PRODUCT_NUM;
  117. #endif
  118. if (i2c_probe(i2c_addr))
  119. goto err;
  120. if (i2c_read(i2c_addr, EEPROM_FATORYSET_OFFSET, 2, hdr, sizeof(hdr)))
  121. goto err;
  122. if ((hdr[0] != 0x99) || (hdr[1] != 0x80)) {
  123. printf("FactorySet is not right in eeprom.\n");
  124. return 1;
  125. }
  126. /* get FactorySet size */
  127. size = (hdr[2] << 8) + hdr[3] + sizeof(hdr);
  128. if (size > 0x3bfa)
  129. size = 0x3bfa;
  130. pages = size / EEPR_PG_SZ;
  131. /*
  132. * read the eeprom using i2c
  133. * I can not read entire eeprom in once, so separate into several
  134. * times. Furthermore, fetch eeprom take longer time, so we fetch
  135. * data after every time we got a record from eeprom
  136. */
  137. debug("Read eeprom page :\n");
  138. for (i = 0; i < pages; i++)
  139. if (i2c_read(i2c_addr, (OFF_PG + i) * EEPR_PG_SZ, 2,
  140. eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ))
  141. goto err;
  142. if (size % EEPR_PG_SZ)
  143. if (i2c_read(i2c_addr, (OFF_PG + pages) * EEPR_PG_SZ, 2,
  144. eeprom_buf + (pages * EEPR_PG_SZ),
  145. (size % EEPR_PG_SZ)))
  146. goto err;
  147. /* we do below just for eeprom align */
  148. for (i = 0; i < size; i++)
  149. if (eeprom_buf[i] == '\n')
  150. eeprom_buf[i] = 0;
  151. /* skip header */
  152. size -= sizeof(hdr);
  153. cp = (uchar *)eeprom_buf + sizeof(hdr);
  154. /* get mac address */
  155. get_factory_record_val(cp, size, (uchar *)"ETH1", (uchar *)"mac",
  156. buf, MAX_STRING_LENGTH);
  157. cp1 = buf;
  158. for (i = 0; i < 6; i++) {
  159. factory_dat.mac[i] = simple_strtoul((char *)cp1, NULL, 16);
  160. cp1 += 3;
  161. }
  162. #if defined(CONFIG_DFU_FUNCTION)
  163. /* read vid and pid for dfu mode */
  164. if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
  165. (uchar *)"vid", buf,
  166. MAX_STRING_LENGTH)) {
  167. factory_dat.usb_vendor_id = simple_strtoul((char *)buf,
  168. NULL, 16);
  169. }
  170. if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
  171. (uchar *)"pid", buf,
  172. MAX_STRING_LENGTH)) {
  173. factory_dat.usb_product_id = simple_strtoul((char *)buf,
  174. NULL, 16);
  175. }
  176. printf("DFU USB: VID = 0x%4x, PID = 0x%4x\n", factory_dat.usb_vendor_id,
  177. factory_dat.usb_product_id);
  178. #endif
  179. if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
  180. (uchar *)"id", buf,
  181. MAX_STRING_LENGTH)) {
  182. if (strncmp((const char *)buf, "PXM50", 5) == 0)
  183. factory_dat.pxm50 = 1;
  184. else
  185. factory_dat.pxm50 = 0;
  186. }
  187. debug("PXM50: %d\n", factory_dat.pxm50);
  188. #if defined(CONFIG_VIDEO)
  189. if (0 <= get_factory_record_val(cp, size, (uchar *)"DISP1",
  190. (uchar *)"name", factory_dat.disp_name,
  191. MAX_STRING_LENGTH)) {
  192. debug("display name: %s\n", factory_dat.disp_name);
  193. }
  194. #endif
  195. return 0;
  196. err:
  197. printf("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\n");
  198. return 1;
  199. }
  200. static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
  201. static int factoryset_mac_setenv(void)
  202. {
  203. uint8_t mac_addr[6];
  204. debug("FactorySet: Set mac address\n");
  205. if (is_valid_ether_addr(factory_dat.mac)) {
  206. memcpy(mac_addr, factory_dat.mac, 6);
  207. } else {
  208. uint32_t mac_hi, mac_lo;
  209. debug("Warning: FactorySet: <ethaddr> not set. Fallback to E-fuse\n");
  210. mac_lo = readl(&cdev->macid0l);
  211. mac_hi = readl(&cdev->macid0h);
  212. mac_addr[0] = mac_hi & 0xFF;
  213. mac_addr[1] = (mac_hi & 0xFF00) >> 8;
  214. mac_addr[2] = (mac_hi & 0xFF0000) >> 16;
  215. mac_addr[3] = (mac_hi & 0xFF000000) >> 24;
  216. mac_addr[4] = mac_lo & 0xFF;
  217. mac_addr[5] = (mac_lo & 0xFF00) >> 8;
  218. if (!is_valid_ether_addr(mac_addr)) {
  219. printf("Warning: ethaddr not set by FactorySet or E-fuse. Set <ethaddr> variable to overcome this.\n");
  220. return -1;
  221. }
  222. }
  223. eth_setenv_enetaddr("ethaddr", mac_addr);
  224. return 0;
  225. }
  226. int factoryset_setenv(void)
  227. {
  228. int ret = 0;
  229. if (factoryset_mac_setenv() < 0)
  230. ret = -1;
  231. return ret;
  232. }
  233. int g_dnl_bind_fixup(struct usb_device_descriptor *dev)
  234. {
  235. put_unaligned(factory_dat.usb_vendor_id, &dev->idVendor);
  236. put_unaligned(factory_dat.usb_product_id, &dev->idProduct);
  237. return 0;
  238. }
  239. #endif /* defined(CONFIG_SPL_BUILD) */