dm9000x.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. dm9000.c: Version 1.2 12/15/2003
  3. A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
  4. Copyright (C) 1997 Sten Wang
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
  14. V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match
  15. 06/22/2001 Support DM9801 progrmming
  16. E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
  17. E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
  18. R17 = (R17 & 0xfff0) | NF + 3
  19. E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
  20. R17 = (R17 & 0xfff0) | NF
  21. v1.00 modify by simon 2001.9.5
  22. change for kernel 2.4.x
  23. v1.1 11/09/2001 fix force mode bug
  24. v1.2 03/18/2003 Weilun Huang <weilun_huang@davicom.com.tw>:
  25. Fixed phy reset.
  26. Added tx/rx 32 bit mode.
  27. Cleaned up for kernel merge.
  28. --------------------------------------
  29. 12/15/2003 Initial port to u-boot by
  30. Sascha Hauer <saschahauer@web.de>
  31. 06/03/2008 Remy Bohmer <linux@bohmer.net>
  32. - Added autodetect of databus width.
  33. - Made debug code compile again.
  34. These changes are tested with DM9000{A,EP,E} together
  35. with a 200MHz Atmel AT91SAM92161 core
  36. TODO: Homerun NIC and longrun NIC are not functional, only internal at the
  37. moment.
  38. */
  39. #include <common.h>
  40. #include <command.h>
  41. #include <net.h>
  42. #include <asm/io.h>
  43. #ifdef CONFIG_DRIVER_DM9000
  44. #include "dm9000x.h"
  45. /* Board/System/Debug information/definition ---------------- */
  46. #define DM9801_NOISE_FLOOR 0x08
  47. #define DM9802_NOISE_FLOOR 0x05
  48. /* #define CONFIG_DM9000_DEBUG */
  49. #ifdef CONFIG_DM9000_DEBUG
  50. #define DM9000_DBG(fmt,args...) printf(fmt, ##args)
  51. #define DM9000_DMP_PACKET(func,packet,length) \
  52. do { \
  53. int i; \
  54. printf(func ": length: %d\n", length); \
  55. for (i = 0; i < length; i++) { \
  56. if (i % 8 == 0) \
  57. printf("\n%s: %02x: ", func, i); \
  58. printf("%02x ", ((unsigned char *) packet)[i]); \
  59. } printf("\n"); \
  60. } while(0)
  61. #else
  62. #define DM9000_DBG(fmt,args...)
  63. #define DM9000_DMP_PACKET(func,packet,length)
  64. #endif
  65. enum DM9000_PHY_mode { DM9000_10MHD = 0, DM9000_100MHD =
  66. 1, DM9000_10MFD = 4, DM9000_100MFD = 5, DM9000_AUTO =
  67. 8, DM9000_1M_HPNA = 0x10
  68. };
  69. enum DM9000_NIC_TYPE { FASTETHER_NIC = 0, HOMERUN_NIC = 1, LONGRUN_NIC = 2
  70. };
  71. /* Structure/enum declaration ------------------------------- */
  72. typedef struct board_info {
  73. u32 runt_length_counter; /* counter: RX length < 64byte */
  74. u32 long_length_counter; /* counter: RX length > 1514byte */
  75. u32 reset_counter; /* counter: RESET */
  76. u32 reset_tx_timeout; /* RESET caused by TX Timeout */
  77. u32 reset_rx_status; /* RESET caused by RX Statsus wrong */
  78. u16 tx_pkt_cnt;
  79. u16 queue_start_addr;
  80. u16 dbug_cnt;
  81. u8 phy_addr;
  82. u8 device_wait_reset; /* device state */
  83. u8 nic_type; /* NIC type */
  84. unsigned char srom[128];
  85. void (*outblk)(void *data_ptr, int count);
  86. void (*inblk)(void *data_ptr, int count);
  87. void (*rx_status)(u16 *RxStatus, u16 *RxLen);
  88. } board_info_t;
  89. static board_info_t dm9000_info;
  90. /* For module input parameter */
  91. static int media_mode = DM9000_AUTO;
  92. static u8 nfloor = 0;
  93. /* function declaration ------------------------------------- */
  94. int eth_init(bd_t * bd);
  95. int eth_send(volatile void *, int);
  96. int eth_rx(void);
  97. void eth_halt(void);
  98. static int dm9000_probe(void);
  99. static u16 phy_read(int);
  100. static void phy_write(int, u16);
  101. u16 read_srom_word(int);
  102. static u8 DM9000_ior(int);
  103. static void DM9000_iow(int reg, u8 value);
  104. /* DM9000 network board routine ---------------------------- */
  105. #define DM9000_outb(d,r) ( *(volatile u8 *)r = d )
  106. #define DM9000_outw(d,r) ( *(volatile u16 *)r = d )
  107. #define DM9000_outl(d,r) ( *(volatile u32 *)r = d )
  108. #define DM9000_inb(r) (*(volatile u8 *)r)
  109. #define DM9000_inw(r) (*(volatile u16 *)r)
  110. #define DM9000_inl(r) (*(volatile u32 *)r)
  111. #ifdef CONFIG_DM9000_DEBUG
  112. static void
  113. dump_regs(void)
  114. {
  115. DM9000_DBG("\n");
  116. DM9000_DBG("NCR (0x00): %02x\n", DM9000_ior(0));
  117. DM9000_DBG("NSR (0x01): %02x\n", DM9000_ior(1));
  118. DM9000_DBG("TCR (0x02): %02x\n", DM9000_ior(2));
  119. DM9000_DBG("TSRI (0x03): %02x\n", DM9000_ior(3));
  120. DM9000_DBG("TSRII (0x04): %02x\n", DM9000_ior(4));
  121. DM9000_DBG("RCR (0x05): %02x\n", DM9000_ior(5));
  122. DM9000_DBG("RSR (0x06): %02x\n", DM9000_ior(6));
  123. DM9000_DBG("ISR (0xFE): %02x\n", DM9000_ior(DM9000_ISR));
  124. DM9000_DBG("\n");
  125. }
  126. #endif
  127. static void dm9000_outblk_8bit(void *data_ptr, int count)
  128. {
  129. int i;
  130. for (i = 0; i < count; i++)
  131. DM9000_outb((((u8 *) data_ptr)[i] & 0xff), DM9000_DATA);
  132. }
  133. static void dm9000_outblk_16bit(void *data_ptr, int count)
  134. {
  135. int i;
  136. u32 tmplen = (count + 1) / 2;
  137. for (i = 0; i < tmplen; i++)
  138. DM9000_outw(((u16 *) data_ptr)[i], DM9000_DATA);
  139. }
  140. static void dm9000_outblk_32bit(void *data_ptr, int count)
  141. {
  142. int i;
  143. u32 tmplen = (count + 3) / 4;
  144. for (i = 0; i < tmplen; i++)
  145. DM9000_outl(((u32 *) data_ptr)[i], DM9000_DATA);
  146. }
  147. static void dm9000_inblk_8bit(void *data_ptr, int count)
  148. {
  149. int i;
  150. for (i = 0; i < count; i++)
  151. ((u8 *) data_ptr)[i] = DM9000_inb(DM9000_DATA);
  152. }
  153. static void dm9000_inblk_16bit(void *data_ptr, int count)
  154. {
  155. int i;
  156. u32 tmplen = (count + 1) / 2;
  157. for (i = 0; i < tmplen; i++)
  158. ((u16 *) data_ptr)[i] = DM9000_inw(DM9000_DATA);
  159. }
  160. static void dm9000_inblk_32bit(void *data_ptr, int count)
  161. {
  162. int i;
  163. u32 tmplen = (count + 3) / 4;
  164. for (i = 0; i < tmplen; i++)
  165. ((u32 *) data_ptr)[i] = DM9000_inl(DM9000_DATA);
  166. }
  167. static void dm9000_rx_status_32bit(u16 *RxStatus, u16 *RxLen)
  168. {
  169. u32 tmpdata = DM9000_inl(DM9000_DATA);
  170. DM9000_outb(DM9000_MRCMD, DM9000_IO);
  171. *RxStatus = tmpdata;
  172. *RxLen = tmpdata >> 16;
  173. }
  174. static void dm9000_rx_status_16bit(u16 *RxStatus, u16 *RxLen)
  175. {
  176. DM9000_outb(DM9000_MRCMD, DM9000_IO);
  177. *RxStatus = DM9000_inw(DM9000_DATA);
  178. *RxLen = DM9000_inw(DM9000_DATA);
  179. }
  180. static void dm9000_rx_status_8bit(u16 *RxStatus, u16 *RxLen)
  181. {
  182. DM9000_outb(DM9000_MRCMD, DM9000_IO);
  183. *RxStatus = DM9000_inb(DM9000_DATA) + (DM9000_inb(DM9000_DATA) << 8);
  184. *RxLen = DM9000_inb(DM9000_DATA) + (DM9000_inb(DM9000_DATA) << 8);
  185. }
  186. /*
  187. Search DM9000 board, allocate space and register it
  188. */
  189. int
  190. dm9000_probe(void)
  191. {
  192. u32 id_val;
  193. id_val = DM9000_ior(DM9000_VIDL);
  194. id_val |= DM9000_ior(DM9000_VIDH) << 8;
  195. id_val |= DM9000_ior(DM9000_PIDL) << 16;
  196. id_val |= DM9000_ior(DM9000_PIDH) << 24;
  197. if (id_val == DM9000_ID) {
  198. printf("dm9000 i/o: 0x%x, id: 0x%x \n", CONFIG_DM9000_BASE,
  199. id_val);
  200. return 0;
  201. } else {
  202. printf("dm9000 not found at 0x%08x id: 0x%08x\n",
  203. CONFIG_DM9000_BASE, id_val);
  204. return -1;
  205. }
  206. }
  207. /* Set PHY operationg mode
  208. */
  209. static void
  210. set_PHY_mode(void)
  211. {
  212. u16 phy_reg4 = 0x01e1, phy_reg0 = 0x1000;
  213. if (!(media_mode & DM9000_AUTO)) {
  214. switch (media_mode) {
  215. case DM9000_10MHD:
  216. phy_reg4 = 0x21;
  217. phy_reg0 = 0x0000;
  218. break;
  219. case DM9000_10MFD:
  220. phy_reg4 = 0x41;
  221. phy_reg0 = 0x1100;
  222. break;
  223. case DM9000_100MHD:
  224. phy_reg4 = 0x81;
  225. phy_reg0 = 0x2000;
  226. break;
  227. case DM9000_100MFD:
  228. phy_reg4 = 0x101;
  229. phy_reg0 = 0x3100;
  230. break;
  231. }
  232. phy_write(4, phy_reg4); /* Set PHY media mode */
  233. phy_write(0, phy_reg0); /* Tmp */
  234. }
  235. DM9000_iow(DM9000_GPCR, 0x01); /* Let GPIO0 output */
  236. DM9000_iow(DM9000_GPR, 0x00); /* Enable PHY */
  237. }
  238. /*
  239. Init HomeRun DM9801
  240. */
  241. static void
  242. program_dm9801(u16 HPNA_rev)
  243. {
  244. __u16 reg16, reg17, reg24, reg25;
  245. if (!nfloor)
  246. nfloor = DM9801_NOISE_FLOOR;
  247. reg16 = phy_read(16);
  248. reg17 = phy_read(17);
  249. reg24 = phy_read(24);
  250. reg25 = phy_read(25);
  251. switch (HPNA_rev) {
  252. case 0xb900: /* DM9801 E3 */
  253. reg16 |= 0x1000;
  254. reg25 = ((reg24 + nfloor) & 0x00ff) | 0xf000;
  255. break;
  256. case 0xb901: /* DM9801 E4 */
  257. reg25 = ((reg24 + nfloor) & 0x00ff) | 0xc200;
  258. reg17 = (reg17 & 0xfff0) + nfloor + 3;
  259. break;
  260. case 0xb902: /* DM9801 E5 */
  261. case 0xb903: /* DM9801 E6 */
  262. default:
  263. reg16 |= 0x1000;
  264. reg25 = ((reg24 + nfloor - 3) & 0x00ff) | 0xc200;
  265. reg17 = (reg17 & 0xfff0) + nfloor;
  266. }
  267. phy_write(16, reg16);
  268. phy_write(17, reg17);
  269. phy_write(25, reg25);
  270. }
  271. /*
  272. Init LongRun DM9802
  273. */
  274. static void
  275. program_dm9802(void)
  276. {
  277. __u16 reg25;
  278. if (!nfloor)
  279. nfloor = DM9802_NOISE_FLOOR;
  280. reg25 = phy_read(25);
  281. reg25 = (reg25 & 0xff00) + nfloor;
  282. phy_write(25, reg25);
  283. }
  284. /* Identify NIC type
  285. */
  286. static void
  287. identify_nic(void)
  288. {
  289. struct board_info *db = &dm9000_info;
  290. u16 phy_reg3;
  291. DM9000_iow(DM9000_NCR, NCR_EXT_PHY);
  292. phy_reg3 = phy_read(3);
  293. switch (phy_reg3 & 0xfff0) {
  294. case 0xb900:
  295. if (phy_read(31) == 0x4404) {
  296. db->nic_type = HOMERUN_NIC;
  297. program_dm9801(phy_reg3);
  298. DM9000_DBG("found homerun NIC\n");
  299. } else {
  300. db->nic_type = LONGRUN_NIC;
  301. DM9000_DBG("found longrun NIC\n");
  302. program_dm9802();
  303. }
  304. break;
  305. default:
  306. db->nic_type = FASTETHER_NIC;
  307. break;
  308. }
  309. DM9000_iow(DM9000_NCR, 0);
  310. }
  311. /* General Purpose dm9000 reset routine */
  312. static void
  313. dm9000_reset(void)
  314. {
  315. DM9000_DBG("resetting\n");
  316. DM9000_iow(DM9000_NCR, NCR_RST);
  317. udelay(1000); /* delay 1ms */
  318. }
  319. /* Initilize dm9000 board
  320. */
  321. int
  322. eth_init(bd_t * bd)
  323. {
  324. int i, oft, lnk;
  325. u8 io_mode;
  326. struct board_info *db = &dm9000_info;
  327. DM9000_DBG("eth_init()\n");
  328. /* RESET device */
  329. dm9000_reset();
  330. dm9000_probe();
  331. /* Auto-detect 8/16/32 bit mode, ISR Bit 6+7 indicate bus width */
  332. io_mode = DM9000_ior(DM9000_ISR) >> 6;
  333. switch (io_mode) {
  334. case 0x0: /* 16-bit mode */
  335. printf("DM9000: running in 16 bit mode\n");
  336. db->outblk = dm9000_outblk_16bit;
  337. db->inblk = dm9000_inblk_16bit;
  338. db->rx_status = dm9000_rx_status_16bit;
  339. break;
  340. case 0x01: /* 32-bit mode */
  341. printf("DM9000: running in 32 bit mode\n");
  342. db->outblk = dm9000_outblk_32bit;
  343. db->inblk = dm9000_inblk_32bit;
  344. db->rx_status = dm9000_rx_status_32bit;
  345. break;
  346. case 0x02: /* 8 bit mode */
  347. printf("DM9000: running in 8 bit mode\n");
  348. db->outblk = dm9000_outblk_8bit;
  349. db->inblk = dm9000_inblk_8bit;
  350. db->rx_status = dm9000_rx_status_8bit;
  351. break;
  352. default:
  353. /* Assume 8 bit mode, will probably not work anyway */
  354. printf("DM9000: Undefined IO-mode:0x%x\n", io_mode);
  355. db->outblk = dm9000_outblk_8bit;
  356. db->inblk = dm9000_inblk_8bit;
  357. db->rx_status = dm9000_rx_status_8bit;
  358. break;
  359. }
  360. /* NIC Type: FASTETHER, HOMERUN, LONGRUN */
  361. identify_nic();
  362. /* GPIO0 on pre-activate PHY */
  363. DM9000_iow(DM9000_GPR, 0x00); /*REG_1F bit0 activate phyxcer */
  364. /* Set PHY */
  365. set_PHY_mode();
  366. /* Program operating register */
  367. DM9000_iow(DM9000_NCR, 0x0); /* only intern phy supported by now */
  368. DM9000_iow(DM9000_TCR, 0); /* TX Polling clear */
  369. DM9000_iow(DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
  370. DM9000_iow(DM9000_FCTR, FCTR_HWOT(3) | FCTR_LWOT(8)); /* Flow Control : High/Low Water */
  371. DM9000_iow(DM9000_FCR, 0x0); /* SH FIXME: This looks strange! Flow Control */
  372. DM9000_iow(DM9000_SMCR, 0); /* Special Mode */
  373. DM9000_iow(DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END); /* clear TX status */
  374. DM9000_iow(DM9000_ISR, 0x0f); /* Clear interrupt status */
  375. /* Set Node address */
  376. for (i = 0; i < 6; i++)
  377. ((u16 *) bd->bi_enetaddr)[i] = read_srom_word(i);
  378. if (is_zero_ether_addr(bd->bi_enetaddr) ||
  379. is_multicast_ether_addr(bd->bi_enetaddr)) {
  380. /* try reading from environment */
  381. u8 i;
  382. char *s, *e;
  383. s = getenv ("ethaddr");
  384. for (i = 0; i < 6; ++i) {
  385. bd->bi_enetaddr[i] = s ?
  386. simple_strtoul (s, &e, 16) : 0;
  387. if (s)
  388. s = (*e) ? e + 1 : e;
  389. }
  390. }
  391. printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", bd->bi_enetaddr[0],
  392. bd->bi_enetaddr[1], bd->bi_enetaddr[2], bd->bi_enetaddr[3],
  393. bd->bi_enetaddr[4], bd->bi_enetaddr[5]);
  394. for (i = 0, oft = 0x10; i < 6; i++, oft++)
  395. DM9000_iow(oft, bd->bi_enetaddr[i]);
  396. for (i = 0, oft = 0x16; i < 8; i++, oft++)
  397. DM9000_iow(oft, 0xff);
  398. /* read back mac, just to be sure */
  399. for (i = 0, oft = 0x10; i < 6; i++, oft++)
  400. DM9000_DBG("%02x:", DM9000_ior(oft));
  401. DM9000_DBG("\n");
  402. /* Activate DM9000 */
  403. DM9000_iow(DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN); /* RX enable */
  404. DM9000_iow(DM9000_IMR, IMR_PAR); /* Enable TX/RX interrupt mask */
  405. i = 0;
  406. while (!(phy_read(1) & 0x20)) { /* autonegation complete bit */
  407. udelay(1000);
  408. i++;
  409. if (i == 10000) {
  410. printf("could not establish link\n");
  411. return 0;
  412. }
  413. }
  414. /* see what we've got */
  415. lnk = phy_read(17) >> 12;
  416. printf("operating at ");
  417. switch (lnk) {
  418. case 1:
  419. printf("10M half duplex ");
  420. break;
  421. case 2:
  422. printf("10M full duplex ");
  423. break;
  424. case 4:
  425. printf("100M half duplex ");
  426. break;
  427. case 8:
  428. printf("100M full duplex ");
  429. break;
  430. default:
  431. printf("unknown: %d ", lnk);
  432. break;
  433. }
  434. printf("mode\n");
  435. return 0;
  436. }
  437. /*
  438. Hardware start transmission.
  439. Send a packet to media from the upper layer.
  440. */
  441. int
  442. eth_send(volatile void *packet, int length)
  443. {
  444. char *data_ptr;
  445. u32 tmplen, i;
  446. int tmo;
  447. struct board_info *db = &dm9000_info;
  448. DM9000_DMP_PACKET("eth_send", packet, length);
  449. /* Move data to DM9000 TX RAM */
  450. data_ptr = (char *) packet;
  451. DM9000_outb(DM9000_MWCMD, DM9000_IO);
  452. /* push the data to the TX-fifo */
  453. (db->outblk)(data_ptr, length);
  454. /* Set TX length to DM9000 */
  455. DM9000_iow(DM9000_TXPLL, length & 0xff);
  456. DM9000_iow(DM9000_TXPLH, (length >> 8) & 0xff);
  457. /* Issue TX polling command */
  458. DM9000_iow(DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
  459. /* wait for end of transmission */
  460. tmo = get_timer(0) + 5 * CFG_HZ;
  461. while (DM9000_ior(DM9000_TCR) & TCR_TXREQ) {
  462. if (get_timer(0) >= tmo) {
  463. printf("transmission timeout\n");
  464. break;
  465. }
  466. }
  467. DM9000_DBG("transmit done\n\n");
  468. return 0;
  469. }
  470. /*
  471. Stop the interface.
  472. The interface is stopped when it is brought.
  473. */
  474. void
  475. eth_halt(void)
  476. {
  477. DM9000_DBG("eth_halt\n");
  478. /* RESET devie */
  479. phy_write(0, 0x8000); /* PHY RESET */
  480. DM9000_iow(DM9000_GPR, 0x01); /* Power-Down PHY */
  481. DM9000_iow(DM9000_IMR, 0x80); /* Disable all interrupt */
  482. DM9000_iow(DM9000_RCR, 0x00); /* Disable RX */
  483. }
  484. /*
  485. Received a packet and pass to upper layer
  486. */
  487. int
  488. eth_rx(void)
  489. {
  490. u8 rxbyte, *rdptr = (u8 *) NetRxPackets[0];
  491. u16 RxStatus, RxLen = 0;
  492. struct board_info *db = &dm9000_info;
  493. /* Check packet ready or not */
  494. DM9000_ior(DM9000_MRCMDX); /* Dummy read */
  495. rxbyte = DM9000_inb(DM9000_DATA); /* Got most updated data */
  496. if (rxbyte == 0)
  497. return 0;
  498. /* Status check: this byte must be 0 or 1 */
  499. if (rxbyte > 1) {
  500. DM9000_iow(DM9000_RCR, 0x00); /* Stop Device */
  501. DM9000_iow(DM9000_ISR, 0x80); /* Stop INT request */
  502. DM9000_DBG("rx status check: %d\n", rxbyte);
  503. }
  504. DM9000_DBG("receiving packet\n");
  505. /* A packet ready now & Get status/length */
  506. DM9000_outb(DM9000_MRCMD, DM9000_IO);
  507. (db->rx_status)(&RxStatus, &RxLen);
  508. DM9000_DBG("rx status: 0x%04x rx len: %d\n", RxStatus, RxLen);
  509. /* Move data from DM9000 */
  510. /* Read received packet from RX SRAM */
  511. (db->inblk)(rdptr, RxLen);
  512. if ((RxStatus & 0xbf00) || (RxLen < 0x40)
  513. || (RxLen > DM9000_PKT_MAX)) {
  514. if (RxStatus & 0x100) {
  515. printf("rx fifo error\n");
  516. }
  517. if (RxStatus & 0x200) {
  518. printf("rx crc error\n");
  519. }
  520. if (RxStatus & 0x8000) {
  521. printf("rx length error\n");
  522. }
  523. if (RxLen > DM9000_PKT_MAX) {
  524. printf("rx length too big\n");
  525. dm9000_reset();
  526. }
  527. } else {
  528. DM9000_DMP_PACKET("eth_rx", rdptr, RxLen);
  529. /* Pass to upper layer */
  530. DM9000_DBG("passing packet to upper layer\n");
  531. NetReceive(NetRxPackets[0], RxLen);
  532. return RxLen;
  533. }
  534. return 0;
  535. }
  536. /*
  537. Read a word data from SROM
  538. */
  539. u16
  540. read_srom_word(int offset)
  541. {
  542. DM9000_iow(DM9000_EPAR, offset);
  543. DM9000_iow(DM9000_EPCR, 0x4);
  544. udelay(8000);
  545. DM9000_iow(DM9000_EPCR, 0x0);
  546. return (DM9000_ior(DM9000_EPDRL) + (DM9000_ior(DM9000_EPDRH) << 8));
  547. }
  548. void
  549. write_srom_word(int offset, u16 val)
  550. {
  551. DM9000_iow(DM9000_EPAR, offset);
  552. DM9000_iow(DM9000_EPDRH, ((val >> 8) & 0xff));
  553. DM9000_iow(DM9000_EPDRL, (val & 0xff));
  554. DM9000_iow(DM9000_EPCR, 0x12);
  555. udelay(8000);
  556. DM9000_iow(DM9000_EPCR, 0);
  557. }
  558. /*
  559. Read a byte from I/O port
  560. */
  561. static u8
  562. DM9000_ior(int reg)
  563. {
  564. DM9000_outb(reg, DM9000_IO);
  565. return DM9000_inb(DM9000_DATA);
  566. }
  567. /*
  568. Write a byte to I/O port
  569. */
  570. static void
  571. DM9000_iow(int reg, u8 value)
  572. {
  573. DM9000_outb(reg, DM9000_IO);
  574. DM9000_outb(value, DM9000_DATA);
  575. }
  576. /*
  577. Read a word from phyxcer
  578. */
  579. static u16
  580. phy_read(int reg)
  581. {
  582. u16 val;
  583. /* Fill the phyxcer register into REG_0C */
  584. DM9000_iow(DM9000_EPAR, DM9000_PHY | reg);
  585. DM9000_iow(DM9000_EPCR, 0xc); /* Issue phyxcer read command */
  586. udelay(100); /* Wait read complete */
  587. DM9000_iow(DM9000_EPCR, 0x0); /* Clear phyxcer read command */
  588. val = (DM9000_ior(DM9000_EPDRH) << 8) | DM9000_ior(DM9000_EPDRL);
  589. /* The read data keeps on REG_0D & REG_0E */
  590. DM9000_DBG("phy_read(0x%x): 0x%x\n", reg, val);
  591. return val;
  592. }
  593. /*
  594. Write a word to phyxcer
  595. */
  596. static void
  597. phy_write(int reg, u16 value)
  598. {
  599. /* Fill the phyxcer register into REG_0C */
  600. DM9000_iow(DM9000_EPAR, DM9000_PHY | reg);
  601. /* Fill the written data into REG_0D & REG_0E */
  602. DM9000_iow(DM9000_EPDRL, (value & 0xff));
  603. DM9000_iow(DM9000_EPDRH, ((value >> 8) & 0xff));
  604. DM9000_iow(DM9000_EPCR, 0xa); /* Issue phyxcer write command */
  605. udelay(500); /* Wait write complete */
  606. DM9000_iow(DM9000_EPCR, 0x0); /* Clear phyxcer write command */
  607. DM9000_DBG("phy_write(reg:0x%x, value:0x%x)\n", reg, value);
  608. }
  609. #endif /* CONFIG_DRIVER_DM9000 */