dm9000x.c 19 KB

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