dm9000x.c 19 KB

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