82559_eeprom.c 8.2 KB

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