dm9000x.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * dm9000.c: Version 1.2 12/15/2003
  4. *
  5. * A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
  6. * Copyright (C) 1997 Sten Wang
  7. *
  8. * (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
  9. *
  10. * V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match
  11. * 06/22/2001 Support DM9801 progrmming
  12. * E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
  13. * E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
  14. * R17 = (R17 & 0xfff0) | NF + 3
  15. * E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
  16. * R17 = (R17 & 0xfff0) | NF
  17. *
  18. * v1.00 modify by simon 2001.9.5
  19. * change for kernel 2.4.x
  20. *
  21. * v1.1 11/09/2001 fix force mode bug
  22. *
  23. * v1.2 03/18/2003 Weilun Huang <weilun_huang@davicom.com.tw>:
  24. * Fixed phy reset.
  25. * Added tx/rx 32 bit mode.
  26. * Cleaned up for kernel merge.
  27. *
  28. * --------------------------------------
  29. *
  30. * 12/15/2003 Initial port to u-boot by
  31. * Sascha Hauer <saschahauer@web.de>
  32. *
  33. * 06/03/2008 Remy Bohmer <linux@bohmer.net>
  34. * - Fixed the driver to work with DM9000A.
  35. * (check on ISR receive status bit before reading the
  36. * FIFO as described in DM9000 programming guide and
  37. * application notes)
  38. * - Added autodetect of databus width.
  39. * - Made debug code compile again.
  40. * - Adapt eth_send such that it matches the DM9000*
  41. * application notes. Needed to make it work properly
  42. * for DM9000A.
  43. * - Adapted reset procedure to match DM9000 application
  44. * notes (i.e. double reset)
  45. * - some minor code cleanups
  46. * These changes are tested with DM9000{A,EP,E} together
  47. * with a 200MHz Atmel AT91SAM9261 core
  48. *
  49. * TODO: external MII is not functional, only internal at the moment.
  50. */
  51. #include <common.h>
  52. #include <command.h>
  53. #include <dm.h>
  54. #include <malloc.h>
  55. #include <net.h>
  56. #include <asm/io.h>
  57. #include <linux/delay.h>
  58. #include "dm9000x.h"
  59. /* Structure/enum declaration ------------------------------- */
  60. struct dm9000_priv {
  61. u32 runt_length_counter; /* counter: RX length < 64byte */
  62. u32 long_length_counter; /* counter: RX length > 1514byte */
  63. u32 reset_counter; /* counter: RESET */
  64. u32 reset_tx_timeout; /* RESET caused by TX Timeout */
  65. u32 reset_rx_status; /* RESET caused by RX Statsus wrong */
  66. u16 tx_pkt_cnt;
  67. u16 queue_start_addr;
  68. u16 dbug_cnt;
  69. u8 phy_addr;
  70. u8 device_wait_reset; /* device state */
  71. unsigned char srom[128];
  72. void (*outblk)(struct dm9000_priv *db, void *data_ptr, int count);
  73. void (*inblk)(struct dm9000_priv *db, void *data_ptr, int count);
  74. void (*rx_status)(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen);
  75. void __iomem *base_io;
  76. void __iomem *base_data;
  77. };
  78. /* DM9000 network board routine ---------------------------- */
  79. #ifndef CONFIG_DM9000_BYTE_SWAPPED
  80. #define dm9000_outb(d, r) writeb((d), (r))
  81. #define dm9000_outw(d, r) writew((d), (r))
  82. #define dm9000_outl(d, r) writel((d), (r))
  83. #define dm9000_inb(r) readb(r)
  84. #define dm9000_inw(r) readw(r)
  85. #define dm9000_inl(r) readl(r)
  86. #else
  87. #define dm9000_outb(d, r) __raw_writeb(d, r)
  88. #define dm9000_outw(d, r) __raw_writew(d, r)
  89. #define dm9000_outl(d, r) __raw_writel(d, r)
  90. #define dm9000_inb(r) __raw_readb(r)
  91. #define dm9000_inw(r) __raw_readw(r)
  92. #define dm9000_inl(r) __raw_readl(r)
  93. #endif
  94. #ifdef DEBUG
  95. static void dm9000_dump_packet(const char *func, u8 *packet, int length)
  96. {
  97. int i;
  98. printf("%s: length: %d\n", func, length);
  99. for (i = 0; i < length; i++) {
  100. if (i % 8 == 0)
  101. printf("\n%s: %02x: ", func, i);
  102. printf("%02x ", packet[i]);
  103. }
  104. printf("\n");
  105. }
  106. #else
  107. static void dm9000_dump_packet(const char *func, u8 *packet, int length) {}
  108. #endif
  109. static void dm9000_outblk_8bit(struct dm9000_priv *db, void *data_ptr, int count)
  110. {
  111. int i;
  112. for (i = 0; i < count; i++)
  113. dm9000_outb((((u8 *)data_ptr)[i] & 0xff), db->base_data);
  114. }
  115. static void dm9000_outblk_16bit(struct dm9000_priv *db, void *data_ptr, int count)
  116. {
  117. int i;
  118. u32 tmplen = (count + 1) / 2;
  119. for (i = 0; i < tmplen; i++)
  120. dm9000_outw(((u16 *)data_ptr)[i], db->base_data);
  121. }
  122. static void dm9000_outblk_32bit(struct dm9000_priv *db, void *data_ptr, int count)
  123. {
  124. int i;
  125. u32 tmplen = (count + 3) / 4;
  126. for (i = 0; i < tmplen; i++)
  127. dm9000_outl(((u32 *)data_ptr)[i], db->base_data);
  128. }
  129. static void dm9000_inblk_8bit(struct dm9000_priv *db, void *data_ptr, int count)
  130. {
  131. int i;
  132. for (i = 0; i < count; i++)
  133. ((u8 *)data_ptr)[i] = dm9000_inb(db->base_data);
  134. }
  135. static void dm9000_inblk_16bit(struct dm9000_priv *db, void *data_ptr, int count)
  136. {
  137. int i;
  138. u32 tmplen = (count + 1) / 2;
  139. for (i = 0; i < tmplen; i++)
  140. ((u16 *)data_ptr)[i] = dm9000_inw(db->base_data);
  141. }
  142. static void dm9000_inblk_32bit(struct dm9000_priv *db, void *data_ptr, int count)
  143. {
  144. int i;
  145. u32 tmplen = (count + 3) / 4;
  146. for (i = 0; i < tmplen; i++)
  147. ((u32 *)data_ptr)[i] = dm9000_inl(db->base_data);
  148. }
  149. static void dm9000_rx_status_32bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen)
  150. {
  151. u32 tmpdata;
  152. dm9000_outb(DM9000_MRCMD, db->base_io);
  153. tmpdata = dm9000_inl(db->base_data);
  154. *rxstatus = __le16_to_cpu(tmpdata);
  155. *rxlen = __le16_to_cpu(tmpdata >> 16);
  156. }
  157. static void dm9000_rx_status_16bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen)
  158. {
  159. dm9000_outb(DM9000_MRCMD, db->base_io);
  160. *rxstatus = __le16_to_cpu(dm9000_inw(db->base_data));
  161. *rxlen = __le16_to_cpu(dm9000_inw(db->base_data));
  162. }
  163. static void dm9000_rx_status_8bit(struct dm9000_priv *db, u16 *rxstatus, u16 *rxlen)
  164. {
  165. dm9000_outb(DM9000_MRCMD, db->base_io);
  166. *rxstatus =
  167. __le16_to_cpu(dm9000_inb(db->base_data) +
  168. (dm9000_inb(db->base_data) << 8));
  169. *rxlen =
  170. __le16_to_cpu(dm9000_inb(db->base_data) +
  171. (dm9000_inb(db->base_data) << 8));
  172. }
  173. /*
  174. * Read a byte from I/O port
  175. */
  176. static u8 dm9000_ior(struct dm9000_priv *db, int reg)
  177. {
  178. dm9000_outb(reg, db->base_io);
  179. return dm9000_inb(db->base_data);
  180. }
  181. /*
  182. * Write a byte to I/O port
  183. */
  184. static void dm9000_iow(struct dm9000_priv *db, int reg, u8 value)
  185. {
  186. dm9000_outb(reg, db->base_io);
  187. dm9000_outb(value, db->base_data);
  188. }
  189. /*
  190. * Read a word from phyxcer
  191. */
  192. static u16 dm9000_phy_read(struct dm9000_priv *db, int reg)
  193. {
  194. u16 val;
  195. /* Fill the phyxcer register into REG_0C */
  196. dm9000_iow(db, DM9000_EPAR, DM9000_PHY | reg);
  197. dm9000_iow(db, DM9000_EPCR, 0xc); /* Issue phyxcer read command */
  198. udelay(100); /* Wait read complete */
  199. dm9000_iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer read command */
  200. val = (dm9000_ior(db, DM9000_EPDRH) << 8) |
  201. dm9000_ior(db, DM9000_EPDRL);
  202. /* The read data keeps on REG_0D & REG_0E */
  203. debug("%s(0x%x): 0x%x\n", __func__, reg, val);
  204. return val;
  205. }
  206. /*
  207. * Write a word to phyxcer
  208. */
  209. static void dm9000_phy_write(struct dm9000_priv *db, int reg, u16 value)
  210. {
  211. /* Fill the phyxcer register into REG_0C */
  212. dm9000_iow(db, DM9000_EPAR, DM9000_PHY | reg);
  213. /* Fill the written data into REG_0D & REG_0E */
  214. dm9000_iow(db, DM9000_EPDRL, (value & 0xff));
  215. dm9000_iow(db, DM9000_EPDRH, ((value >> 8) & 0xff));
  216. dm9000_iow(db, DM9000_EPCR, 0xa); /* Issue phyxcer write command */
  217. udelay(500); /* Wait write complete */
  218. dm9000_iow(db, DM9000_EPCR, 0x0); /* Clear phyxcer write command */
  219. debug("%s(reg:0x%x, value:0x%x)\n", __func__, reg, value);
  220. }
  221. /*
  222. * Search DM9000 board, allocate space and register it
  223. */
  224. static int dm9000_probe(struct dm9000_priv *db)
  225. {
  226. u32 id_val;
  227. id_val = dm9000_ior(db, DM9000_VIDL);
  228. id_val |= dm9000_ior(db, DM9000_VIDH) << 8;
  229. id_val |= dm9000_ior(db, DM9000_PIDL) << 16;
  230. id_val |= dm9000_ior(db, DM9000_PIDH) << 24;
  231. if (id_val != DM9000_ID) {
  232. printf("dm9000 not found at 0x%p id: 0x%08x\n",
  233. db->base_io, id_val);
  234. return -1;
  235. }
  236. printf("dm9000 i/o: 0x%p, id: 0x%x\n", db->base_io, id_val);
  237. return 0;
  238. }
  239. /* General Purpose dm9000 reset routine */
  240. static void dm9000_reset(struct dm9000_priv *db)
  241. {
  242. debug("resetting DM9000\n");
  243. /*
  244. * Reset DM9000,
  245. * see DM9000 Application Notes V1.22 Jun 11, 2004 page 29
  246. */
  247. /* DEBUG: Make all GPIO0 outputs, all others inputs */
  248. dm9000_iow(db, DM9000_GPCR, GPCR_GPIO0_OUT);
  249. /* Step 1: Power internal PHY by writing 0 to GPIO0 pin */
  250. dm9000_iow(db, DM9000_GPR, 0);
  251. /* Step 2: Software reset */
  252. dm9000_iow(db, DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST));
  253. do {
  254. debug("resetting the DM9000, 1st reset\n");
  255. udelay(25); /* Wait at least 20 us */
  256. } while (dm9000_ior(db, DM9000_NCR) & 1);
  257. dm9000_iow(db, DM9000_NCR, 0);
  258. dm9000_iow(db, DM9000_NCR, (NCR_LBK_INT_MAC | NCR_RST)); /* Issue a second reset */
  259. do {
  260. debug("resetting the DM9000, 2nd reset\n");
  261. udelay(25); /* Wait at least 20 us */
  262. } while (dm9000_ior(db, DM9000_NCR) & 1);
  263. /* Check whether the ethernet controller is present */
  264. if ((dm9000_ior(db, DM9000_PIDL) != 0x0) ||
  265. (dm9000_ior(db, DM9000_PIDH) != 0x90))
  266. printf("ERROR: resetting DM9000 -> not responding\n");
  267. }
  268. /* Initialize dm9000 board */
  269. static int dm9000_init_common(struct dm9000_priv *db, u8 enetaddr[6])
  270. {
  271. int i, oft, lnk;
  272. u8 io_mode;
  273. /* RESET device */
  274. dm9000_reset(db);
  275. if (dm9000_probe(db) < 0)
  276. return -1;
  277. /* Auto-detect 8/16/32 bit mode, ISR Bit 6+7 indicate bus width */
  278. io_mode = dm9000_ior(db, DM9000_ISR) >> 6;
  279. switch (io_mode) {
  280. case 0x0: /* 16-bit mode */
  281. printf("DM9000: running in 16 bit mode\n");
  282. db->outblk = dm9000_outblk_16bit;
  283. db->inblk = dm9000_inblk_16bit;
  284. db->rx_status = dm9000_rx_status_16bit;
  285. break;
  286. case 0x01: /* 32-bit mode */
  287. printf("DM9000: running in 32 bit mode\n");
  288. db->outblk = dm9000_outblk_32bit;
  289. db->inblk = dm9000_inblk_32bit;
  290. db->rx_status = dm9000_rx_status_32bit;
  291. break;
  292. case 0x02: /* 8 bit mode */
  293. printf("DM9000: running in 8 bit mode\n");
  294. db->outblk = dm9000_outblk_8bit;
  295. db->inblk = dm9000_inblk_8bit;
  296. db->rx_status = dm9000_rx_status_8bit;
  297. break;
  298. default:
  299. /* Assume 8 bit mode, will probably not work anyway */
  300. printf("DM9000: Undefined IO-mode:0x%x\n", io_mode);
  301. db->outblk = dm9000_outblk_8bit;
  302. db->inblk = dm9000_inblk_8bit;
  303. db->rx_status = dm9000_rx_status_8bit;
  304. break;
  305. }
  306. /* Program operating register, only internal phy supported */
  307. dm9000_iow(db, DM9000_NCR, 0x0);
  308. /* TX Polling clear */
  309. dm9000_iow(db, DM9000_TCR, 0);
  310. /* Less 3Kb, 200us */
  311. dm9000_iow(db, DM9000_BPTR, BPTR_BPHW(3) | BPTR_JPT_600US);
  312. /* Flow Control : High/Low Water */
  313. dm9000_iow(db, DM9000_FCTR, FCTR_HWOT(3) | FCTR_LWOT(8));
  314. /* SH FIXME: This looks strange! Flow Control */
  315. dm9000_iow(db, DM9000_FCR, 0x0);
  316. /* Special Mode */
  317. dm9000_iow(db, DM9000_SMCR, 0);
  318. /* clear TX status */
  319. dm9000_iow(db, DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END);
  320. /* Clear interrupt status */
  321. dm9000_iow(db, DM9000_ISR, ISR_ROOS | ISR_ROS | ISR_PTS | ISR_PRS);
  322. printf("MAC: %pM\n", enetaddr);
  323. if (!is_valid_ethaddr(enetaddr))
  324. printf("WARNING: Bad MAC address (uninitialized EEPROM?)\n");
  325. /* fill device MAC address registers */
  326. for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
  327. dm9000_iow(db, oft, enetaddr[i]);
  328. for (i = 0, oft = 0x16; i < 8; i++, oft++)
  329. dm9000_iow(db, oft, 0xff);
  330. /* read back mac, just to be sure */
  331. for (i = 0, oft = 0x10; i < 6; i++, oft++)
  332. debug("%02x:", dm9000_ior(db, oft));
  333. debug("\n");
  334. /* Activate DM9000 */
  335. /* RX enable */
  336. dm9000_iow(db, DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN);
  337. /* Enable TX/RX interrupt mask */
  338. dm9000_iow(db, DM9000_IMR, IMR_PAR);
  339. i = 0;
  340. while (!(dm9000_phy_read(db, 1) & 0x20)) { /* autonegation complete bit */
  341. udelay(1000);
  342. i++;
  343. if (i == 10000) {
  344. printf("could not establish link\n");
  345. return 0;
  346. }
  347. }
  348. /* see what we've got */
  349. lnk = dm9000_phy_read(db, 17) >> 12;
  350. printf("operating at ");
  351. switch (lnk) {
  352. case 1:
  353. printf("10M half duplex ");
  354. break;
  355. case 2:
  356. printf("10M full duplex ");
  357. break;
  358. case 4:
  359. printf("100M half duplex ");
  360. break;
  361. case 8:
  362. printf("100M full duplex ");
  363. break;
  364. default:
  365. printf("unknown: %d ", lnk);
  366. break;
  367. }
  368. printf("mode\n");
  369. return 0;
  370. }
  371. /*
  372. * Hardware start transmission.
  373. * Send a packet to media from the upper layer.
  374. */
  375. static int dm9000_send_common(struct dm9000_priv *db, void *packet, int length)
  376. {
  377. int tmo;
  378. dm9000_dump_packet(__func__, packet, length);
  379. dm9000_iow(db, DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
  380. /* Move data to DM9000 TX RAM */
  381. dm9000_outb(DM9000_MWCMD, db->base_io); /* Prepare for TX-data */
  382. /* push the data to the TX-fifo */
  383. db->outblk(db, packet, length);
  384. /* Set TX length to DM9000 */
  385. dm9000_iow(db, DM9000_TXPLL, length & 0xff);
  386. dm9000_iow(db, DM9000_TXPLH, (length >> 8) & 0xff);
  387. /* Issue TX polling command */
  388. dm9000_iow(db, DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
  389. /* wait for end of transmission */
  390. tmo = get_timer(0) + 5 * CONFIG_SYS_HZ;
  391. while (!(dm9000_ior(db, DM9000_NSR) & (NSR_TX1END | NSR_TX2END)) ||
  392. !(dm9000_ior(db, DM9000_ISR) & IMR_PTM)) {
  393. if (get_timer(0) >= tmo) {
  394. printf("transmission timeout\n");
  395. break;
  396. }
  397. }
  398. dm9000_iow(db, DM9000_ISR, IMR_PTM); /* Clear Tx bit in ISR */
  399. debug("transmit done\n\n");
  400. return 0;
  401. }
  402. /*
  403. * Stop the interface.
  404. * The interface is stopped when it is brought.
  405. */
  406. static void dm9000_halt_common(struct dm9000_priv *db)
  407. {
  408. /* RESET device */
  409. dm9000_phy_write(db, 0, 0x8000); /* PHY RESET */
  410. dm9000_iow(db, DM9000_GPR, 0x01); /* Power-Down PHY */
  411. dm9000_iow(db, DM9000_IMR, 0x80); /* Disable all interrupt */
  412. dm9000_iow(db, DM9000_RCR, 0x00); /* Disable RX */
  413. }
  414. /*
  415. * Received a packet and pass to upper layer
  416. */
  417. static int dm9000_recv_common(struct dm9000_priv *db, uchar *rdptr)
  418. {
  419. u8 rxbyte;
  420. u16 rxstatus, rxlen = 0;
  421. /*
  422. * Check packet ready or not, we must check
  423. * the ISR status first for DM9000A
  424. */
  425. if (!(dm9000_ior(db, DM9000_ISR) & 0x01)) /* Rx-ISR bit must be set. */
  426. return 0;
  427. dm9000_iow(db, DM9000_ISR, 0x01); /* clear PR status latched in bit 0 */
  428. /* There is _at least_ 1 package in the fifo, read them all */
  429. dm9000_ior(db, DM9000_MRCMDX); /* Dummy read */
  430. /*
  431. * Get most updated data,
  432. * only look at bits 0:1, See application notes DM9000
  433. */
  434. rxbyte = dm9000_inb(db->base_data) & 0x03;
  435. /* Status check: this byte must be 0 or 1 */
  436. if (rxbyte > DM9000_PKT_RDY) {
  437. dm9000_iow(db, DM9000_RCR, 0x00); /* Stop Device */
  438. dm9000_iow(db, DM9000_ISR, 0x80); /* Stop INT request */
  439. printf("DM9000 error: status check fail: 0x%x\n",
  440. rxbyte);
  441. return -EINVAL;
  442. }
  443. if (rxbyte != DM9000_PKT_RDY)
  444. return 0; /* No packet received, ignore */
  445. debug("receiving packet\n");
  446. /* A packet ready now & Get status/length */
  447. db->rx_status(db, &rxstatus, &rxlen);
  448. debug("rx status: 0x%04x rx len: %d\n", rxstatus, rxlen);
  449. /* Move data from DM9000 */
  450. /* Read received packet from RX SRAM */
  451. db->inblk(db, rdptr, rxlen);
  452. if (rxstatus & 0xbf00 || rxlen < 0x40 || rxlen > DM9000_PKT_MAX) {
  453. if (rxstatus & 0x100)
  454. printf("rx fifo error\n");
  455. if (rxstatus & 0x200)
  456. printf("rx crc error\n");
  457. if (rxstatus & 0x8000)
  458. printf("rx length error\n");
  459. if (rxlen > DM9000_PKT_MAX) {
  460. printf("rx length too big\n");
  461. dm9000_reset(db);
  462. }
  463. return -EINVAL;
  464. }
  465. return rxlen;
  466. }
  467. /*
  468. * Read a word data from SROM
  469. */
  470. #if !defined(CONFIG_DM9000_NO_SROM)
  471. static void dm9000_read_srom_word(struct dm9000_priv *db, int offset, u8 *to)
  472. {
  473. dm9000_iow(db, DM9000_EPAR, offset);
  474. dm9000_iow(db, DM9000_EPCR, 0x4);
  475. mdelay(8);
  476. dm9000_iow(db, DM9000_EPCR, 0x0);
  477. to[0] = dm9000_ior(db, DM9000_EPDRL);
  478. to[1] = dm9000_ior(db, DM9000_EPDRH);
  479. }
  480. static void dm9000_get_enetaddr(struct dm9000_priv *db, u8 *enetaddr)
  481. {
  482. int i;
  483. for (i = 0; i < 3; i++)
  484. dm9000_read_srom_word(db, i, enetaddr + (2 * i));
  485. }
  486. #else
  487. static void dm9000_get_enetaddr(struct dm9000_priv *db, u8 *enetaddr) {}
  488. #endif
  489. static int dm9000_start(struct udevice *dev)
  490. {
  491. struct dm9000_priv *db = dev_get_priv(dev);
  492. struct eth_pdata *pdata = dev_get_plat(dev);
  493. return dm9000_init_common(db, pdata->enetaddr);
  494. }
  495. static void dm9000_stop(struct udevice *dev)
  496. {
  497. struct dm9000_priv *db = dev_get_priv(dev);
  498. dm9000_halt_common(db);
  499. }
  500. static int dm9000_send(struct udevice *dev, void *packet, int length)
  501. {
  502. struct dm9000_priv *db = dev_get_priv(dev);
  503. int ret;
  504. ret = dm9000_send_common(db, packet, length);
  505. return ret ? 0 : -ETIMEDOUT;
  506. }
  507. static int dm9000_recv(struct udevice *dev, int flags, uchar **packetp)
  508. {
  509. struct dm9000_priv *db = dev_get_priv(dev);
  510. uchar *data = net_rx_packets[0];
  511. int ret;
  512. ret = dm9000_recv_common(db, data);
  513. if (ret > 0)
  514. *packetp = (void *)data;
  515. return ret >= 0 ? ret : -EAGAIN;
  516. }
  517. static int dm9000_write_hwaddr(struct udevice *dev)
  518. {
  519. struct dm9000_priv *db = dev_get_priv(dev);
  520. struct eth_pdata *pdata = dev_get_plat(dev);
  521. int i, oft;
  522. /* fill device MAC address registers */
  523. for (i = 0, oft = DM9000_PAR; i < 6; i++, oft++)
  524. dm9000_iow(db, oft, pdata->enetaddr[i]);
  525. for (i = 0, oft = 0x16; i < 8; i++, oft++)
  526. dm9000_iow(db, oft, 0xff);
  527. /* read back mac, just to be sure */
  528. for (i = 0, oft = 0x10; i < 6; i++, oft++)
  529. debug("%02x:", dm9000_ior(db, oft));
  530. debug("\n");
  531. return 0;
  532. }
  533. static int dm9000_read_rom_hwaddr(struct udevice *dev)
  534. {
  535. struct dm9000_priv *db = dev_get_priv(dev);
  536. struct eth_pdata *pdata = dev_get_plat(dev);
  537. dm9000_get_enetaddr(db, pdata->enetaddr);
  538. return !is_valid_ethaddr(pdata->enetaddr);
  539. }
  540. static int dm9000_bind(struct udevice *dev)
  541. {
  542. return device_set_name(dev, dev->name);
  543. }
  544. static int dm9000_of_to_plat(struct udevice *dev)
  545. {
  546. struct dm9000_priv *db = dev_get_priv(dev);
  547. struct eth_pdata *pdata = dev_get_plat(dev);
  548. pdata->iobase = dev_read_addr_index(dev, 0);
  549. db->base_io = (void __iomem *)pdata->iobase;
  550. db->base_data = dev_read_addr_index_ptr(dev, 1);
  551. return 0;
  552. }
  553. static const struct eth_ops dm9000_ops = {
  554. .start = dm9000_start,
  555. .stop = dm9000_stop,
  556. .send = dm9000_send,
  557. .recv = dm9000_recv,
  558. .write_hwaddr = dm9000_write_hwaddr,
  559. .read_rom_hwaddr = dm9000_read_rom_hwaddr,
  560. };
  561. static const struct udevice_id dm9000_ids[] = {
  562. { .compatible = "davicom,dm9000" },
  563. { }
  564. };
  565. U_BOOT_DRIVER(dm9000) = {
  566. .name = "eth_dm9000",
  567. .id = UCLASS_ETH,
  568. .of_match = dm9000_ids,
  569. .bind = dm9000_bind,
  570. .of_to_plat = dm9000_of_to_plat,
  571. .ops = &dm9000_ops,
  572. .priv_auto = sizeof(struct dm9000_priv),
  573. .plat_auto = sizeof(struct eth_pdata),
  574. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  575. };