dm9000x.c 17 KB

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