dc2114x.c 20 KB

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