82559_eeprom.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright 1998-2001 by Donald Becker.
  3. * This software may be used and distributed according to the terms of
  4. * the GNU General Public License (GPL), incorporated herein by reference.
  5. * Contact the author for use under other terms.
  6. *
  7. * This program must be compiled with "-O"!
  8. * See the bottom of this file for the suggested compile-command.
  9. *
  10. * The author may be reached as becker@scyld.com, or C/O
  11. * Scyld Computing Corporation
  12. * 410 Severn Ave., Suite 210
  13. * Annapolis MD 21403
  14. *
  15. * Common-sense licensing statement: Using any portion of this program in
  16. * your own program means that you must give credit to the original author
  17. * and release the resulting code under the GPL.
  18. */
  19. #define _PPC_STRING_H_ /* avoid unnecessary str/mem functions */
  20. #define _LINUX_STRING_H_ /* avoid unnecessary str/mem functions */
  21. #include <common.h>
  22. #include <exports.h>
  23. #include <asm/io.h>
  24. /* Default EEPROM for i82559 */
  25. static unsigned short default_eeprom[64] = {
  26. 0x0100, 0x0302, 0x0504, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  27. 0xffff, 0xffff, 0x40c0, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff,
  28. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  29. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  30. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  31. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  32. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  33. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
  34. };
  35. static unsigned short eeprom[256];
  36. static int eeprom_size = 64;
  37. static int eeprom_addr_size = 6;
  38. static int debug = 0;
  39. static inline unsigned short swap16(unsigned short x)
  40. {
  41. return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
  42. }
  43. void * memcpy(void * dest,const void *src,size_t count)
  44. {
  45. char *tmp = (char *) dest, *s = (char *) src;
  46. while (count--)
  47. *tmp++ = *s++;
  48. return dest;
  49. }
  50. /* The EEPROM commands include the alway-set leading bit. */
  51. #define EE_WRITE_CMD (5)
  52. #define EE_READ_CMD (6)
  53. #define EE_ERASE_CMD (7)
  54. /* Serial EEPROM section. */
  55. #define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
  56. #define EE_CS 0x02 /* EEPROM chip select. */
  57. #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
  58. #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
  59. #define EE_ENB (0x4800 | EE_CS)
  60. #define EE_WRITE_0 0x4802
  61. #define EE_WRITE_1 0x4806
  62. #define EE_OFFSET 14
  63. /* Delay between EEPROM clock transitions. */
  64. #define eeprom_delay(ee_addr) inw(ee_addr)
  65. /* Wait for the EEPROM to finish the previous operation. */
  66. static int eeprom_busy_poll(long ee_ioaddr)
  67. {
  68. int i;
  69. outw(EE_ENB, ee_ioaddr);
  70. for (i = 0; i < 10000; i++) /* Typical 2000 ticks */
  71. if (inw(ee_ioaddr) & EE_DATA_READ)
  72. break;
  73. return i;
  74. }
  75. /* This executes a generic EEPROM command, typically a write or write enable.
  76. It returns the data output from the EEPROM, and thus may also be used for
  77. reads. */
  78. static int do_eeprom_cmd(long ioaddr, int cmd, int cmd_len)
  79. {
  80. unsigned retval = 0;
  81. long ee_addr = ioaddr + EE_OFFSET;
  82. if (debug > 1)
  83. printf(" EEPROM op 0x%x: ", cmd);
  84. outw(EE_ENB | EE_SHIFT_CLK, ee_addr);
  85. /* Shift the command bits out. */
  86. do {
  87. short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
  88. outw(dataval, ee_addr);
  89. eeprom_delay(ee_addr);
  90. if (debug > 2)
  91. printf("%X", inw(ee_addr) & 15);
  92. outw(dataval | EE_SHIFT_CLK, ee_addr);
  93. eeprom_delay(ee_addr);
  94. retval = (retval << 1) | ((inw(ee_addr) & EE_DATA_READ) ? 1 : 0);
  95. } while (--cmd_len >= 0);
  96. #if 0
  97. outw(EE_ENB, ee_addr);
  98. #endif
  99. /* Terminate the EEPROM access. */
  100. outw(EE_ENB & ~EE_CS, ee_addr);
  101. if (debug > 1)
  102. printf(" EEPROM result is 0x%5.5x.\n", retval);
  103. return retval;
  104. }
  105. static int read_eeprom(long ioaddr, int location, int addr_len)
  106. {
  107. return do_eeprom_cmd(ioaddr, ((EE_READ_CMD << addr_len) | location)
  108. << 16 , 3 + addr_len + 16) & 0xffff;
  109. }
  110. static void write_eeprom(long ioaddr, int index, int value, int addr_len)
  111. {
  112. long ee_ioaddr = ioaddr + EE_OFFSET;
  113. int i;
  114. /* Poll for previous op finished. */
  115. eeprom_busy_poll(ee_ioaddr); /* Typical 0 ticks */
  116. /* Enable programming modes. */
  117. do_eeprom_cmd(ioaddr, (0x4f << (addr_len-4)), 3 + addr_len);
  118. /* Do the actual write. */
  119. do_eeprom_cmd(ioaddr,
  120. (((EE_WRITE_CMD<<addr_len) | index)<<16) | (value & 0xffff),
  121. 3 + addr_len + 16);
  122. /* Poll for write finished. */
  123. i = eeprom_busy_poll(ee_ioaddr); /* Typical 2000 ticks */
  124. if (debug)
  125. printf(" Write finished after %d ticks.\n", i);
  126. /* Disable programming. This command is not instantaneous, so we check
  127. for busy before the next op. */
  128. do_eeprom_cmd(ioaddr, (0x40 << (addr_len-4)), 3 + addr_len);
  129. eeprom_busy_poll(ee_ioaddr);
  130. }
  131. static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr)
  132. {
  133. unsigned short checksum = 0;
  134. int size_test;
  135. int i;
  136. printf("Resetting i82559 EEPROM @ 0x%08lx ... ", ioaddr);
  137. size_test = do_eeprom_cmd(ioaddr, (EE_READ_CMD << 8) << 16, 27);
  138. eeprom_addr_size = (size_test & 0xffe0000) == 0xffe0000 ? 8 : 6;
  139. eeprom_size = 1 << eeprom_addr_size;
  140. memcpy(eeprom, default_eeprom, sizeof default_eeprom);
  141. for (i = 0; i < 3; i++)
  142. eeprom[i] = (hwaddr[i*2+1]<<8) + hwaddr[i*2];
  143. /* Recalculate the checksum. */
  144. for (i = 0; i < eeprom_size - 1; i++)
  145. checksum += eeprom[i];
  146. eeprom[i] = 0xBABA - checksum;
  147. for (i = 0; i < eeprom_size; i++)
  148. write_eeprom(ioaddr, i, eeprom[i], eeprom_addr_size);
  149. for (i = 0; i < eeprom_size; i++)
  150. if (read_eeprom(ioaddr, i, eeprom_addr_size) != eeprom[i]) {
  151. printf("failed\n");
  152. return 1;
  153. }
  154. printf("done\n");
  155. return 0;
  156. }
  157. static unsigned int hatoi(char *p, char **errp)
  158. {
  159. unsigned int res = 0;
  160. while (1) {
  161. switch (*p) {
  162. case 'a':
  163. case 'b':
  164. case 'c':
  165. case 'd':
  166. case 'e':
  167. case 'f':
  168. res |= (*p - 'a' + 10);
  169. break;
  170. case 'A':
  171. case 'B':
  172. case 'C':
  173. case 'D':
  174. case 'E':
  175. case 'F':
  176. res |= (*p - 'A' + 10);
  177. break;
  178. case '0':
  179. case '1':
  180. case '2':
  181. case '3':
  182. case '4':
  183. case '5':
  184. case '6':
  185. case '7':
  186. case '8':
  187. case '9':
  188. res |= (*p - '0');
  189. break;
  190. default:
  191. if (errp) {
  192. *errp = p;
  193. }
  194. return res;
  195. }
  196. p++;
  197. if (*p == 0) {
  198. break;
  199. }
  200. res <<= 4;
  201. }
  202. if (errp) {
  203. *errp = NULL;
  204. }
  205. return res;
  206. }
  207. static unsigned char *gethwaddr(char *in, unsigned char *out)
  208. {
  209. char tmp[3];
  210. int i;
  211. char *err;
  212. for (i=0;i<6;i++) {
  213. if (in[i*3+2] == 0 && i == 5) {
  214. out[i] = hatoi(&in[i*3], &err);
  215. if (err) {
  216. return NULL;
  217. }
  218. } else if (in[i*3+2] == ':' && i < 5) {
  219. tmp[0] = in[i*3];
  220. tmp[1] = in[i*3+1];
  221. tmp[2] = 0;
  222. out[i] = hatoi(tmp, &err);
  223. if (err) {
  224. return NULL;
  225. }
  226. } else {
  227. return NULL;
  228. }
  229. }
  230. return out;
  231. }
  232. static u32
  233. read_config_dword(int bus, int dev, int func, int reg)
  234. {
  235. u32 res;
  236. outl(0x80000000|(bus&0xff)<<16|(dev&0x1f)<<11|(func&7)<<8|(reg&0xfc),
  237. 0xcf8);
  238. res = inl(0xcfc);
  239. outl(0, 0xcf8);
  240. return res;
  241. }
  242. static u16
  243. read_config_word(int bus, int dev, int func, int reg)
  244. {
  245. u32 res;
  246. outl(0x80000000|(bus&0xff)<<16|(dev&0x1f)<<11|(func&7)<<8|(reg&0xfc),
  247. 0xcf8);
  248. res = inw(0xcfc + (reg & 2));
  249. outl(0, 0xcf8);
  250. return res;
  251. }
  252. static void
  253. write_config_word(int bus, int dev, int func, int reg, u16 data)
  254. {
  255. outl(0x80000000|(bus&0xff)<<16|(dev&0x1f)<<11|(func&7)<<8|(reg&0xfc),
  256. 0xcf8);
  257. outw(data, 0xcfc + (reg & 2));
  258. outl(0, 0xcf8);
  259. }
  260. int main (int argc, char *argv[])
  261. {
  262. unsigned char *eth_addr;
  263. uchar buf[6];
  264. int instance;
  265. app_startup(argv);
  266. if (argc != 2) {
  267. printf ("call with base Ethernet address\n");
  268. return 1;
  269. }
  270. eth_addr = gethwaddr(argv[1], buf);
  271. if (NULL == eth_addr) {
  272. printf ("Can not parse ethernet address\n");
  273. return 1;
  274. }
  275. if (eth_addr[5] & 0x01) {
  276. printf("Base Ethernet address must be even\n");
  277. }
  278. for (instance = 0; instance < 2; instance ++) {
  279. unsigned int io_addr;
  280. unsigned char mac[6];
  281. int bar1 = read_config_dword(0, 6+instance, 0, 0x14);
  282. if (! (bar1 & 1)) {
  283. printf("ETH%d is disabled %x\n", instance, bar1);
  284. } else {
  285. printf("ETH%d IO=0x%04x\n", instance, bar1 & ~3);
  286. }
  287. io_addr = (bar1 & (~3L));
  288. write_config_word(0, 6+instance, 0, 4,
  289. read_config_word(0, 6+instance, 0, 4) | 1);
  290. printf("ETH%d CMD %04x\n", instance,
  291. read_config_word(0, 6+instance, 0, 4));
  292. memcpy(mac, eth_addr, 6);
  293. mac[5] += instance;
  294. printf("got io=%04x, ha=%02x:%02x:%02x:%02x:%02x:%02x\n",
  295. io_addr, mac[0], mac[1], mac[2],
  296. mac[3], mac[4], mac[5]);
  297. reset_eeprom(io_addr, mac);
  298. }
  299. return 0;
  300. }