dm9000x.c 18 KB

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