dm9000x.c 19 KB

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