dc2114x.c 20 KB

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