dc2114x.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * See file CREDITS for list of people who contributed to this
  3. * project.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) \
  22. && defined(CONFIG_TULIP)
  23. #include <malloc.h>
  24. #include <net.h>
  25. #include <pci.h>
  26. #undef DEBUG
  27. #undef DEBUG_SROM
  28. #undef DEBUG_SROM2
  29. #undef UPDATE_SROM
  30. /* PCI Registers.
  31. */
  32. #define PCI_CFDA_PSM 0x43
  33. #define CFRV_RN 0x000000f0 /* Revision Number */
  34. #define WAKEUP 0x00 /* Power Saving Wakeup */
  35. #define SLEEP 0x80 /* Power Saving Sleep Mode */
  36. #define DC2114x_BRK 0x0020 /* CFRV break between DC21142 & DC21143 */
  37. /* Ethernet chip registers.
  38. */
  39. #define DE4X5_BMR 0x000 /* Bus Mode Register */
  40. #define DE4X5_TPD 0x008 /* Transmit Poll Demand Reg */
  41. #define DE4X5_RRBA 0x018 /* RX Ring Base Address Reg */
  42. #define DE4X5_TRBA 0x020 /* TX Ring Base Address Reg */
  43. #define DE4X5_STS 0x028 /* Status Register */
  44. #define DE4X5_OMR 0x030 /* Operation Mode Register */
  45. #define DE4X5_SICR 0x068 /* SIA Connectivity Register */
  46. #define DE4X5_APROM 0x048 /* Ethernet Address PROM */
  47. /* Register bits.
  48. */
  49. #define BMR_SWR 0x00000001 /* Software Reset */
  50. #define STS_TS 0x00700000 /* Transmit Process State */
  51. #define STS_RS 0x000e0000 /* Receive Process State */
  52. #define OMR_ST 0x00002000 /* Start/Stop Transmission Command */
  53. #define OMR_SR 0x00000002 /* Start/Stop Receive */
  54. #define OMR_PS 0x00040000 /* Port Select */
  55. #define OMR_SDP 0x02000000 /* SD Polarity - MUST BE ASSERTED */
  56. #define OMR_PM 0x00000080 /* Pass All Multicast */
  57. /* Descriptor bits.
  58. */
  59. #define R_OWN 0x80000000 /* Own Bit */
  60. #define RD_RER 0x02000000 /* Receive End Of Ring */
  61. #define RD_LS 0x00000100 /* Last Descriptor */
  62. #define RD_ES 0x00008000 /* Error Summary */
  63. #define TD_TER 0x02000000 /* Transmit End Of Ring */
  64. #define T_OWN 0x80000000 /* Own Bit */
  65. #define TD_LS 0x40000000 /* Last Segment */
  66. #define TD_FS 0x20000000 /* First Segment */
  67. #define TD_ES 0x00008000 /* Error Summary */
  68. #define TD_SET 0x08000000 /* Setup Packet */
  69. /* The EEPROM commands include the alway-set leading bit. */
  70. #define SROM_WRITE_CMD 5
  71. #define SROM_READ_CMD 6
  72. #define SROM_ERASE_CMD 7
  73. #define SROM_HWADD 0x0014 /* Hardware Address offset in SROM */
  74. #define SROM_RD 0x00004000 /* Read from Boot ROM */
  75. #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
  76. #define EE_WRITE_0 0x4801
  77. #define EE_WRITE_1 0x4805
  78. #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
  79. #define SROM_SR 0x00000800 /* Select Serial ROM when set */
  80. #define DT_IN 0x00000004 /* Serial Data In */
  81. #define DT_CLK 0x00000002 /* Serial ROM Clock */
  82. #define DT_CS 0x00000001 /* Serial ROM Chip Select */
  83. #define POLL_DEMAND 1
  84. #define RESET_DE4X5(dev) {\
  85. int i;\
  86. i=INL(dev, DE4X5_BMR);\
  87. udelay(1000);\
  88. OUTL(dev, i | BMR_SWR, DE4X5_BMR);\
  89. udelay(1000);\
  90. OUTL(dev, i, DE4X5_BMR);\
  91. udelay(1000);\
  92. for (i=0;i<5;i++) {INL(dev, DE4X5_BMR); udelay(10000);}\
  93. udelay(1000);\
  94. }
  95. #define START_DE4X5(dev) {\
  96. s32 omr; \
  97. omr = INL(dev, DE4X5_OMR);\
  98. omr |= OMR_ST | OMR_SR;\
  99. OUTL(dev, omr, DE4X5_OMR); /* Enable the TX and/or RX */\
  100. }
  101. #define STOP_DE4X5(dev) {\
  102. s32 omr; \
  103. omr = INL(dev, DE4X5_OMR);\
  104. omr &= ~(OMR_ST|OMR_SR);\
  105. OUTL(dev, omr, DE4X5_OMR); /* Disable the TX and/or RX */ \
  106. }
  107. #define NUM_RX_DESC PKTBUFSRX
  108. #define NUM_TX_DESC 1 /* Number of TX descriptors */
  109. #define RX_BUFF_SZ PKTSIZE_ALIGN
  110. #define TOUT_LOOP 1000000
  111. #define SETUP_FRAME_LEN 192
  112. #define ETH_ALEN 6
  113. struct de4x5_desc {
  114. volatile s32 status;
  115. u32 des1;
  116. u32 buf;
  117. u32 next;
  118. };
  119. static struct de4x5_desc rx_ring[NUM_RX_DESC]; /* RX descriptor ring */
  120. static struct de4x5_desc tx_ring[NUM_TX_DESC]; /* TX descriptor ring */
  121. static int rx_new; /* RX descriptor ring pointer */
  122. static int tx_new; /* TX descriptor ring pointer */
  123. static char rxRingSize;
  124. static char txRingSize;
  125. static void sendto_srom(struct eth_device* dev, u_int command, u_long addr);
  126. static int getfrom_srom(struct eth_device* dev, u_long addr);
  127. static int do_eeprom_cmd(struct eth_device *dev, u_long ioaddr, int cmd, int cmd_len);
  128. static int do_read_eeprom(struct eth_device *dev, u_long ioaddr, int location, int addr_len);
  129. static int read_srom(struct eth_device *dev, u_long ioaddr, int index);
  130. #ifdef UPDATE_SROM
  131. static int write_srom(struct eth_device *dev, u_long ioaddr, int index, int new_value);
  132. static void update_srom(struct eth_device *dev, bd_t *bis);
  133. #endif
  134. static void read_hw_addr(struct eth_device* dev, bd_t * bis);
  135. static void send_setup_frame(struct eth_device* dev, bd_t * bis);
  136. static int dc21x4x_init(struct eth_device* dev, bd_t* bis);
  137. static int dc21x4x_send(struct eth_device* dev, volatile void *packet, int length);
  138. static int dc21x4x_recv(struct eth_device* dev);
  139. static void dc21x4x_halt(struct eth_device* dev);
  140. #ifdef CONFIG_TULIP_SELECT_MEDIA
  141. extern void dc21x4x_select_media(struct eth_device* dev);
  142. #endif
  143. #if defined(CONFIG_E500)
  144. #define phys_to_bus(a) (a)
  145. #else
  146. #define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, a)
  147. #endif
  148. static int INL(struct eth_device* dev, u_long addr)
  149. {
  150. return le32_to_cpu(*(volatile u_long *)(addr + dev->iobase));
  151. }
  152. static void OUTL(struct eth_device* dev, int command, u_long addr)
  153. {
  154. *(volatile u_long *)(addr + dev->iobase) = cpu_to_le32(command);
  155. }
  156. static struct pci_device_id supported[] = {
  157. { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_FAST },
  158. { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_21142 },
  159. { }
  160. };
  161. int dc21x4x_initialize(bd_t *bis)
  162. {
  163. int idx=0;
  164. int card_number = 0;
  165. int cfrv;
  166. unsigned char timer;
  167. pci_dev_t devbusfn;
  168. unsigned int iobase;
  169. unsigned short status;
  170. struct eth_device* dev;
  171. while(1) {
  172. devbusfn = pci_find_devices(supported, idx++);
  173. if (devbusfn == -1) {
  174. break;
  175. }
  176. /* Get the chip configuration revision register. */
  177. pci_read_config_dword(devbusfn, PCI_REVISION_ID, &cfrv);
  178. if ((cfrv & CFRV_RN) < DC2114x_BRK ) {
  179. printf("Error: The chip is not DC21143.\n");
  180. continue;
  181. }
  182. pci_read_config_word(devbusfn, PCI_COMMAND, &status);
  183. status |=
  184. #ifdef CONFIG_TULIP_USE_IO
  185. PCI_COMMAND_IO |
  186. #else
  187. PCI_COMMAND_MEMORY |
  188. #endif
  189. PCI_COMMAND_MASTER;
  190. pci_write_config_word(devbusfn, PCI_COMMAND, status);
  191. pci_read_config_word(devbusfn, PCI_COMMAND, &status);
  192. if (!(status & PCI_COMMAND_IO)) {
  193. printf("Error: Can not enable I/O access.\n");
  194. continue;
  195. }
  196. if (!(status & PCI_COMMAND_IO)) {
  197. printf("Error: Can not enable I/O access.\n");
  198. continue;
  199. }
  200. if (!(status & PCI_COMMAND_MASTER)) {
  201. printf("Error: Can not enable Bus Mastering.\n");
  202. continue;
  203. }
  204. /* Check the latency timer for values >= 0x60. */
  205. pci_read_config_byte(devbusfn, PCI_LATENCY_TIMER, &timer);
  206. if (timer < 0x60) {
  207. pci_write_config_byte(devbusfn, PCI_LATENCY_TIMER, 0x60);
  208. }
  209. #ifdef CONFIG_TULIP_USE_IO
  210. /* read BAR for memory space access */
  211. pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_0, &iobase);
  212. iobase &= PCI_BASE_ADDRESS_IO_MASK;
  213. #else
  214. /* read BAR for memory space access */
  215. pci_read_config_dword(devbusfn, PCI_BASE_ADDRESS_1, &iobase);
  216. iobase &= PCI_BASE_ADDRESS_MEM_MASK;
  217. #endif
  218. #ifdef DEBUG
  219. printf("dc21x4x: DEC 21142 PCI Device @0x%x\n", iobase);
  220. #endif
  221. dev = (struct eth_device*) malloc(sizeof *dev);
  222. sprintf(dev->name, "dc21x4x#%d", card_number);
  223. #ifdef CONFIG_TULIP_USE_IO
  224. dev->iobase = pci_io_to_phys(devbusfn, iobase);
  225. #else
  226. dev->iobase = pci_mem_to_phys(devbusfn, iobase);
  227. #endif
  228. dev->priv = (void*) devbusfn;
  229. dev->init = dc21x4x_init;
  230. dev->halt = dc21x4x_halt;
  231. dev->send = dc21x4x_send;
  232. dev->recv = dc21x4x_recv;
  233. /* Ensure we're not sleeping. */
  234. pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
  235. udelay(10 * 1000);
  236. read_hw_addr(dev, bis);
  237. eth_register(dev);
  238. card_number++;
  239. }
  240. return card_number;
  241. }
  242. static int dc21x4x_init(struct eth_device* dev, bd_t* bis)
  243. {
  244. int i;
  245. int devbusfn = (int) dev->priv;
  246. /* Ensure we're not sleeping. */
  247. pci_write_config_byte(devbusfn, PCI_CFDA_PSM, WAKEUP);
  248. RESET_DE4X5(dev);
  249. if ((INL(dev, DE4X5_STS) & (STS_TS | STS_RS)) != 0) {
  250. printf("Error: Cannot reset ethernet controller.\n");
  251. return 0;
  252. }
  253. #ifdef CONFIG_TULIP_SELECT_MEDIA
  254. dc21x4x_select_media(dev);
  255. #else
  256. OUTL(dev, OMR_SDP | OMR_PS | OMR_PM, DE4X5_OMR);
  257. #endif
  258. for (i = 0; i < NUM_RX_DESC; i++) {
  259. rx_ring[i].status = cpu_to_le32(R_OWN);
  260. rx_ring[i].des1 = cpu_to_le32(RX_BUFF_SZ);
  261. rx_ring[i].buf = cpu_to_le32(phys_to_bus((u32) NetRxPackets[i]));
  262. rx_ring[i].next = 0;
  263. }
  264. for (i=0; i < NUM_TX_DESC; i++) {
  265. tx_ring[i].status = 0;
  266. tx_ring[i].des1 = 0;
  267. tx_ring[i].buf = 0;
  268. tx_ring[i].next = 0;
  269. }
  270. rxRingSize = NUM_RX_DESC;
  271. txRingSize = NUM_TX_DESC;
  272. /* Write the end of list marker to the descriptor lists. */
  273. rx_ring[rxRingSize - 1].des1 |= cpu_to_le32(RD_RER);
  274. tx_ring[txRingSize - 1].des1 |= cpu_to_le32(TD_TER);
  275. /* Tell the adapter where the TX/RX rings are located. */
  276. OUTL(dev, phys_to_bus((u32) &rx_ring), DE4X5_RRBA);
  277. OUTL(dev, phys_to_bus((u32) &tx_ring), DE4X5_TRBA);
  278. START_DE4X5(dev);
  279. tx_new = 0;
  280. rx_new = 0;
  281. send_setup_frame(dev, bis);
  282. return 1;
  283. }
  284. static int dc21x4x_send(struct eth_device* dev, volatile void *packet, int length)
  285. {
  286. int status = -1;
  287. int i;
  288. if (length <= 0) {
  289. printf("%s: bad packet size: %d\n", dev->name, length);
  290. goto Done;
  291. }
  292. for(i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
  293. if (i >= TOUT_LOOP) {
  294. printf("%s: tx error buffer not ready\n", dev->name);
  295. goto Done;
  296. }
  297. }
  298. tx_ring[tx_new].buf = cpu_to_le32(phys_to_bus((u32) packet));
  299. tx_ring[tx_new].des1 = cpu_to_le32(TD_TER | TD_LS | TD_FS | length);
  300. tx_ring[tx_new].status = cpu_to_le32(T_OWN);
  301. OUTL(dev, POLL_DEMAND, DE4X5_TPD);
  302. for(i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
  303. if (i >= TOUT_LOOP) {
  304. printf(".%s: tx buffer not ready\n", dev->name);
  305. goto Done;
  306. }
  307. }
  308. if (le32_to_cpu(tx_ring[tx_new].status) & TD_ES) {
  309. #if 0 /* test-only */
  310. printf("TX error status = 0x%08X\n",
  311. le32_to_cpu(tx_ring[tx_new].status));
  312. #endif
  313. goto Done;
  314. }
  315. status = length;
  316. Done:
  317. return status;
  318. }
  319. static int dc21x4x_recv(struct eth_device* dev)
  320. {
  321. s32 status;
  322. int length = 0;
  323. for ( ; ; ) {
  324. status = (s32)le32_to_cpu(rx_ring[rx_new].status);
  325. if (status & R_OWN) {
  326. break;
  327. }
  328. if (status & RD_LS) {
  329. /* Valid frame status.
  330. */
  331. if (status & RD_ES) {
  332. /* There was an error.
  333. */
  334. printf("RX error status = 0x%08X\n", status);
  335. } else {
  336. /* A valid frame received.
  337. */
  338. length = (le32_to_cpu(rx_ring[rx_new].status) >> 16);
  339. /* Pass the packet up to the protocol
  340. * layers.
  341. */
  342. NetReceive(NetRxPackets[rx_new], length - 4);
  343. }
  344. /* Change buffer ownership for this frame, back
  345. * to the adapter.
  346. */
  347. rx_ring[rx_new].status = cpu_to_le32(R_OWN);
  348. }
  349. /* Update entry information.
  350. */
  351. rx_new = (rx_new + 1) % rxRingSize;
  352. }
  353. return length;
  354. }
  355. static void dc21x4x_halt(struct eth_device* dev)
  356. {
  357. int devbusfn = (int) dev->priv;
  358. STOP_DE4X5(dev);
  359. OUTL(dev, 0, DE4X5_SICR);
  360. pci_write_config_byte(devbusfn, PCI_CFDA_PSM, SLEEP);
  361. }
  362. static void send_setup_frame(struct eth_device* dev, bd_t *bis)
  363. {
  364. int i;
  365. char setup_frame[SETUP_FRAME_LEN];
  366. char *pa = &setup_frame[0];
  367. memset(pa, 0xff, SETUP_FRAME_LEN);
  368. for (i = 0; i < ETH_ALEN; i++) {
  369. *(pa + (i & 1)) = dev->enetaddr[i];
  370. if (i & 0x01) {
  371. pa += 4;
  372. }
  373. }
  374. for(i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
  375. if (i >= TOUT_LOOP) {
  376. printf("%s: tx error buffer not ready\n", dev->name);
  377. goto Done;
  378. }
  379. }
  380. tx_ring[tx_new].buf = cpu_to_le32(phys_to_bus((u32) &setup_frame[0]));
  381. tx_ring[tx_new].des1 = cpu_to_le32(TD_TER | TD_SET| SETUP_FRAME_LEN);
  382. tx_ring[tx_new].status = cpu_to_le32(T_OWN);
  383. OUTL(dev, POLL_DEMAND, DE4X5_TPD);
  384. for(i = 0; tx_ring[tx_new].status & cpu_to_le32(T_OWN); i++) {
  385. if (i >= TOUT_LOOP) {
  386. printf("%s: tx buffer not ready\n", dev->name);
  387. goto Done;
  388. }
  389. }
  390. if (le32_to_cpu(tx_ring[tx_new].status) != 0x7FFFFFFF) {
  391. printf("TX error status2 = 0x%08X\n", le32_to_cpu(tx_ring[tx_new].status));
  392. }
  393. Done:
  394. return;
  395. }
  396. /* SROM Read and write routines.
  397. */
  398. static void
  399. sendto_srom(struct eth_device* dev, u_int command, u_long addr)
  400. {
  401. OUTL(dev, command, addr);
  402. udelay(1);
  403. }
  404. static int
  405. getfrom_srom(struct eth_device* dev, u_long addr)
  406. {
  407. s32 tmp;
  408. tmp = INL(dev, addr);
  409. udelay(1);
  410. return tmp;
  411. }
  412. /* Note: this routine returns extra data bits for size detection. */
  413. static int do_read_eeprom(struct eth_device *dev, u_long ioaddr, int location, int addr_len)
  414. {
  415. int i;
  416. unsigned retval = 0;
  417. int read_cmd = location | (SROM_READ_CMD << addr_len);
  418. sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
  419. sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
  420. #ifdef DEBUG_SROM
  421. printf(" EEPROM read at %d ", location);
  422. #endif
  423. /* Shift the read command bits out. */
  424. for (i = 4 + addr_len; i >= 0; i--) {
  425. short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
  426. sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | dataval, ioaddr);
  427. udelay(10);
  428. sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | dataval | DT_CLK, ioaddr);
  429. udelay(10);
  430. #ifdef DEBUG_SROM2
  431. printf("%X", getfrom_srom(dev, ioaddr) & 15);
  432. #endif
  433. retval = (retval << 1) | ((getfrom_srom(dev, ioaddr) & EE_DATA_READ) ? 1 : 0);
  434. }
  435. sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
  436. #ifdef DEBUG_SROM2
  437. printf(" :%X:", getfrom_srom(dev, ioaddr) & 15);
  438. #endif
  439. for (i = 16; i > 0; i--) {
  440. sendto_srom(dev, SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
  441. udelay(10);
  442. #ifdef DEBUG_SROM2
  443. printf("%X", getfrom_srom(dev, ioaddr) & 15);
  444. #endif
  445. retval = (retval << 1) | ((getfrom_srom(dev, ioaddr) & EE_DATA_READ) ? 1 : 0);
  446. sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
  447. udelay(10);
  448. }
  449. /* Terminate the EEPROM access. */
  450. sendto_srom(dev, SROM_RD | SROM_SR, ioaddr);
  451. #ifdef DEBUG_SROM2
  452. printf(" EEPROM value at %d is %5.5x.\n", location, retval);
  453. #endif
  454. return retval;
  455. }
  456. /* This executes a generic EEPROM command, typically a write or write enable.
  457. It returns the data output from the EEPROM, and thus may also be used for
  458. reads. */
  459. static int do_eeprom_cmd(struct eth_device *dev, u_long ioaddr, int cmd, int cmd_len)
  460. {
  461. unsigned retval = 0;
  462. #ifdef DEBUG_SROM
  463. printf(" EEPROM op 0x%x: ", cmd);
  464. #endif
  465. sendto_srom(dev,SROM_RD | SROM_SR | DT_CS | DT_CLK, ioaddr);
  466. /* Shift the command bits out. */
  467. do {
  468. short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
  469. sendto_srom(dev,dataval, ioaddr);
  470. udelay(10);
  471. #ifdef DEBUG_SROM2
  472. printf("%X", getfrom_srom(dev,ioaddr) & 15);
  473. #endif
  474. sendto_srom(dev,dataval | DT_CLK, ioaddr);
  475. udelay(10);
  476. retval = (retval << 1) | ((getfrom_srom(dev,ioaddr) & EE_DATA_READ) ? 1 : 0);
  477. } while (--cmd_len >= 0);
  478. sendto_srom(dev,SROM_RD | SROM_SR | DT_CS, ioaddr);
  479. /* Terminate the EEPROM access. */
  480. sendto_srom(dev,SROM_RD | SROM_SR, ioaddr);
  481. #ifdef DEBUG_SROM
  482. printf(" EEPROM result is 0x%5.5x.\n", retval);
  483. #endif
  484. return retval;
  485. }
  486. static int read_srom(struct eth_device *dev, u_long ioaddr, int index)
  487. {
  488. int ee_addr_size = do_read_eeprom(dev, ioaddr, 0xff, 8) & 0x40000 ? 8 : 6;
  489. return do_eeprom_cmd(dev, ioaddr,
  490. (((SROM_READ_CMD << ee_addr_size) | index) << 16)
  491. | 0xffff, 3 + ee_addr_size + 16);
  492. }
  493. #ifdef UPDATE_SROM
  494. static int write_srom(struct eth_device *dev, u_long ioaddr, int index, int new_value)
  495. {
  496. int ee_addr_size = do_read_eeprom(dev, ioaddr, 0xff, 8) & 0x40000 ? 8 : 6;
  497. int i;
  498. unsigned short newval;
  499. udelay(10*1000); /* test-only */
  500. #ifdef DEBUG_SROM
  501. printf("ee_addr_size=%d.\n", ee_addr_size);
  502. printf("Writing new entry 0x%4.4x to offset %d.\n", new_value, index);
  503. #endif
  504. /* Enable programming modes. */
  505. do_eeprom_cmd(dev, ioaddr, (0x4f << (ee_addr_size-4)), 3+ee_addr_size);
  506. /* Do the actual write. */
  507. do_eeprom_cmd(dev, ioaddr,
  508. (((SROM_WRITE_CMD<<ee_addr_size)|index) << 16) | new_value,
  509. 3 + ee_addr_size + 16);
  510. /* Poll for write finished. */
  511. sendto_srom(dev, SROM_RD | SROM_SR | DT_CS, ioaddr);
  512. for (i = 0; i < 10000; i++) /* Typical 2000 ticks */
  513. if (getfrom_srom(dev, ioaddr) & EE_DATA_READ)
  514. break;
  515. #ifdef DEBUG_SROM
  516. printf(" Write finished after %d ticks.\n", i);
  517. #endif
  518. /* Disable programming. */
  519. do_eeprom_cmd(dev, ioaddr, (0x40 << (ee_addr_size-4)), 3 + ee_addr_size);
  520. /* And read the result. */
  521. newval = do_eeprom_cmd(dev, ioaddr,
  522. (((SROM_READ_CMD<<ee_addr_size)|index) << 16)
  523. | 0xffff, 3 + ee_addr_size + 16);
  524. #ifdef DEBUG_SROM
  525. printf(" New value at offset %d is %4.4x.\n", index, newval);
  526. #endif
  527. return 1;
  528. }
  529. #endif
  530. static void read_hw_addr(struct eth_device *dev, bd_t *bis)
  531. {
  532. u_short tmp, *p = (short *)(&dev->enetaddr[0]);
  533. int i, j = 0;
  534. for (i = 0; i < (ETH_ALEN >> 1); i++) {
  535. tmp = read_srom(dev, DE4X5_APROM, ((SROM_HWADD >> 1) + i));
  536. *p = le16_to_cpu(tmp);
  537. j += *p++;
  538. }
  539. if ((j == 0) || (j == 0x2fffd)) {
  540. memset (dev->enetaddr, 0, ETH_ALEN);
  541. #ifdef DEBUG
  542. printf("Warning: can't read HW address from SROM.\n");
  543. #endif
  544. goto Done;
  545. }
  546. return;
  547. Done:
  548. #ifdef UPDATE_SROM
  549. update_srom(dev, bis);
  550. #endif
  551. return;
  552. }
  553. #ifdef UPDATE_SROM
  554. static void update_srom(struct eth_device *dev, bd_t *bis)
  555. {
  556. int i;
  557. static unsigned short eeprom[0x40] = {
  558. 0x140b, 0x6610, 0x0000, 0x0000, /* 00 */
  559. 0x0000, 0x0000, 0x0000, 0x0000, /* 04 */
  560. 0x00a3, 0x0103, 0x0000, 0x0000, /* 08 */
  561. 0x0000, 0x1f00, 0x0000, 0x0000, /* 0c */
  562. 0x0108, 0x038d, 0x0000, 0x0000, /* 10 */
  563. 0xe078, 0x0001, 0x0040, 0x0018, /* 14 */
  564. 0x0000, 0x0000, 0x0000, 0x0000, /* 18 */
  565. 0x0000, 0x0000, 0x0000, 0x0000, /* 1c */
  566. 0x0000, 0x0000, 0x0000, 0x0000, /* 20 */
  567. 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
  568. 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
  569. 0x0000, 0x0000, 0x0000, 0x0000, /* 2c */
  570. 0x0000, 0x0000, 0x0000, 0x0000, /* 30 */
  571. 0x0000, 0x0000, 0x0000, 0x0000, /* 34 */
  572. 0x0000, 0x0000, 0x0000, 0x0000, /* 38 */
  573. 0x0000, 0x0000, 0x0000, 0x4e07, /* 3c */
  574. };
  575. /* Ethernet Addr... */
  576. eeprom[0x0a] = ((bis->bi_enetaddr[1] & 0xff) << 8) | (bis->bi_enetaddr[0] & 0xff);
  577. eeprom[0x0b] = ((bis->bi_enetaddr[3] & 0xff) << 8) | (bis->bi_enetaddr[2] & 0xff);
  578. eeprom[0x0c] = ((bis->bi_enetaddr[5] & 0xff) << 8) | (bis->bi_enetaddr[4] & 0xff);
  579. for (i=0; i<0x40; i++)
  580. {
  581. write_srom(dev, DE4X5_APROM, i, eeprom[i]);
  582. }
  583. }
  584. #endif
  585. #endif