eepro100_eeprom.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr);
  24. int eepro100_eeprom(int argc, char *argv[])
  25. {
  26. int ret = 0;
  27. unsigned char hwaddr1[6] = { 0x00, 0x00, 0x02, 0x03, 0x04, 0x05 };
  28. unsigned char hwaddr2[6] = { 0x00, 0x00, 0x02, 0x03, 0x04, 0x06 };
  29. app_startup(argv);
  30. #if defined(CONFIG_OXC)
  31. ret |= reset_eeprom(0x80000000, hwaddr1);
  32. ret |= reset_eeprom(0x81000000, hwaddr2);
  33. #endif
  34. return ret;
  35. }
  36. /* Default EEPROM for i82559 */
  37. static unsigned short default_eeprom[64] = {
  38. 0x0100, 0x0302, 0x0504, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  39. 0xffff, 0xffff, 0x40c0, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff,
  40. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  41. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  42. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  43. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  44. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
  45. 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
  46. };
  47. static unsigned short eeprom[256];
  48. static int eeprom_size = 64;
  49. static int eeprom_addr_size = 6;
  50. static int debug = 0;
  51. static inline unsigned short swap16(unsigned short x)
  52. {
  53. return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
  54. }
  55. static inline void outw(short data, long addr)
  56. {
  57. *(volatile short *)(addr) = swap16(data);
  58. }
  59. static inline short inw(long addr)
  60. {
  61. return swap16(*(volatile short *)(addr));
  62. }
  63. static inline void *memcpy(void *dst, const void *src, unsigned int len)
  64. {
  65. char *ret = dst;
  66. while (len-- > 0) {
  67. *ret++ = *((char *)src);
  68. src++;
  69. }
  70. return (void *)ret;
  71. }
  72. /* The EEPROM commands include the alway-set leading bit. */
  73. #define EE_WRITE_CMD (5)
  74. #define EE_READ_CMD (6)
  75. #define EE_ERASE_CMD (7)
  76. /* Serial EEPROM section. */
  77. #define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
  78. #define EE_CS 0x02 /* EEPROM chip select. */
  79. #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
  80. #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
  81. #define EE_ENB (0x4800 | EE_CS)
  82. #define EE_WRITE_0 0x4802
  83. #define EE_WRITE_1 0x4806
  84. #define EE_OFFSET 14
  85. /* Delay between EEPROM clock transitions. */
  86. #define eeprom_delay(ee_addr) inw(ee_addr)
  87. /* Wait for the EEPROM to finish the previous operation. */
  88. static int eeprom_busy_poll(long ee_ioaddr)
  89. {
  90. int i;
  91. outw(EE_ENB, ee_ioaddr);
  92. for (i = 0; i < 10000; i++) /* Typical 2000 ticks */
  93. if (inw(ee_ioaddr) & EE_DATA_READ)
  94. break;
  95. return i;
  96. }
  97. /* This executes a generic EEPROM command, typically a write or write enable.
  98. It returns the data output from the EEPROM, and thus may also be used for
  99. reads. */
  100. static int do_eeprom_cmd(long ioaddr, int cmd, int cmd_len)
  101. {
  102. unsigned retval = 0;
  103. long ee_addr = ioaddr + EE_OFFSET;
  104. if (debug > 1)
  105. printf(" EEPROM op 0x%x: ", cmd);
  106. outw(EE_ENB | EE_SHIFT_CLK, ee_addr);
  107. /* Shift the command bits out. */
  108. do {
  109. short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
  110. outw(dataval, ee_addr);
  111. eeprom_delay(ee_addr);
  112. if (debug > 2)
  113. printf("%X", inw(ee_addr) & 15);
  114. outw(dataval | EE_SHIFT_CLK, ee_addr);
  115. eeprom_delay(ee_addr);
  116. retval = (retval << 1) | ((inw(ee_addr) & EE_DATA_READ) ? 1 : 0);
  117. } while (--cmd_len >= 0);
  118. #if 0
  119. outw(EE_ENB, ee_addr);
  120. #endif
  121. /* Terminate the EEPROM access. */
  122. outw(EE_ENB & ~EE_CS, ee_addr);
  123. if (debug > 1)
  124. printf(" EEPROM result is 0x%5.5x.\n", retval);
  125. return retval;
  126. }
  127. static int read_eeprom(long ioaddr, int location, int addr_len)
  128. {
  129. return do_eeprom_cmd(ioaddr, ((EE_READ_CMD << addr_len) | location)
  130. << 16 , 3 + addr_len + 16) & 0xffff;
  131. }
  132. static void write_eeprom(long ioaddr, int index, int value, int addr_len)
  133. {
  134. long ee_ioaddr = ioaddr + EE_OFFSET;
  135. int i;
  136. /* Poll for previous op finished. */
  137. eeprom_busy_poll(ee_ioaddr); /* Typical 0 ticks */
  138. /* Enable programming modes. */
  139. do_eeprom_cmd(ioaddr, (0x4f << (addr_len-4)), 3 + addr_len);
  140. /* Do the actual write. */
  141. do_eeprom_cmd(ioaddr,
  142. (((EE_WRITE_CMD<<addr_len) | index)<<16) | (value & 0xffff),
  143. 3 + addr_len + 16);
  144. /* Poll for write finished. */
  145. i = eeprom_busy_poll(ee_ioaddr); /* Typical 2000 ticks */
  146. if (debug)
  147. printf(" Write finished after %d ticks.\n", i);
  148. /* Disable programming. This command is not instantaneous, so we check
  149. for busy before the next op. */
  150. do_eeprom_cmd(ioaddr, (0x40 << (addr_len-4)), 3 + addr_len);
  151. eeprom_busy_poll(ee_ioaddr);
  152. }
  153. static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr)
  154. {
  155. unsigned short checksum = 0;
  156. int size_test;
  157. int i;
  158. printf("Resetting i82559 EEPROM @ 0x%08lX ... ", ioaddr);
  159. size_test = do_eeprom_cmd(ioaddr, (EE_READ_CMD << 8) << 16, 27);
  160. eeprom_addr_size = (size_test & 0xffe0000) == 0xffe0000 ? 8 : 6;
  161. eeprom_size = 1 << eeprom_addr_size;
  162. memcpy(eeprom, default_eeprom, sizeof default_eeprom);
  163. for (i = 0; i < 3; i++)
  164. eeprom[i] = (hwaddr[i*2+1]<<8) + hwaddr[i*2];
  165. /* Recalculate the checksum. */
  166. for (i = 0; i < eeprom_size - 1; i++)
  167. checksum += eeprom[i];
  168. eeprom[i] = 0xBABA - checksum;
  169. for (i = 0; i < eeprom_size; i++)
  170. write_eeprom(ioaddr, i, eeprom[i], eeprom_addr_size);
  171. for (i = 0; i < eeprom_size; i++)
  172. if (read_eeprom(ioaddr, i, eeprom_addr_size) != eeprom[i]) {
  173. printf("failed\n");
  174. return 1;
  175. }
  176. printf("done\n");
  177. return 0;
  178. }