ne2000_base.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. Ported to U-Boot by Christian Pellegrin <chri@ascensit.com>
  3. Based on sources from the Linux kernel (pcnet_cs.c, 8390.h) and
  4. eCOS(if_dp83902a.c, if_dp83902a.h). Both of these 2 wonderful world
  5. are GPL, so this is, of course, GPL.
  6. ==========================================================================
  7. dev/if_dp83902a.c
  8. Ethernet device driver for NS DP83902a ethernet controller
  9. ==========================================================================
  10. ####ECOSGPLCOPYRIGHTBEGIN####
  11. -------------------------------------------
  12. This file is part of eCos, the Embedded Configurable Operating System.
  13. Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
  14. eCos is free software; you can redistribute it and/or modify it under
  15. the terms of the GNU General Public License as published by the Free
  16. Software Foundation; either version 2 or (at your option) any later version.
  17. eCos is distributed in the hope that it will be useful, but WITHOUT ANY
  18. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  20. for more details.
  21. You should have received a copy of the GNU General Public License along
  22. with eCos; if not, write to the Free Software Foundation, Inc.,
  23. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  24. As a special exception, if other files instantiate templates or use macros
  25. or inline functions from this file, or you compile this file and link it
  26. with other works to produce a work based on this file, this file does not
  27. by itself cause the resulting work to be covered by the GNU General Public
  28. License. However the source code for this file must still be made available
  29. in accordance with section (3) of the GNU General Public License.
  30. This exception does not invalidate any other reasons why a work based on
  31. this file might be covered by the GNU General Public License.
  32. Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
  33. at http://sources.redhat.com/ecos/ecos-license/
  34. -------------------------------------------
  35. ####ECOSGPLCOPYRIGHTEND####
  36. ####BSDCOPYRIGHTBEGIN####
  37. -------------------------------------------
  38. Portions of this software may have been derived from OpenBSD or other sources,
  39. and are covered by the appropriate copyright disclaimers included herein.
  40. -------------------------------------------
  41. ####BSDCOPYRIGHTEND####
  42. ==========================================================================
  43. #####DESCRIPTIONBEGIN####
  44. Author(s): gthomas
  45. Contributors: gthomas, jskov, rsandifo
  46. Date: 2001-06-13
  47. Purpose:
  48. Description:
  49. FIXME: Will fail if pinged with large packets (1520 bytes)
  50. Add promisc config
  51. Add SNMP
  52. ####DESCRIPTIONEND####
  53. ==========================================================================
  54. */
  55. #include <common.h>
  56. #include <command.h>
  57. #include <net.h>
  58. #include <malloc.h>
  59. /* forward definition of function used for the uboot interface */
  60. void uboot_push_packet_len(int len);
  61. void uboot_push_tx_done(int key, int val);
  62. /* NE2000 base header file */
  63. #include "ne2000_base.h"
  64. #if defined(CONFIG_DRIVER_AX88796L)
  65. /* AX88796L support */
  66. #include "ax88796.h"
  67. #else
  68. /* Basic NE2000 chip support */
  69. #include "ne2000.h"
  70. #endif
  71. static dp83902a_priv_data_t nic; /* just one instance of the card supported */
  72. static bool
  73. dp83902a_init(void)
  74. {
  75. dp83902a_priv_data_t *dp = &nic;
  76. u8* base;
  77. #if defined(NE2000_BASIC_INIT)
  78. int i;
  79. #endif
  80. DEBUG_FUNCTION();
  81. base = dp->base;
  82. if (!base)
  83. return false; /* No device found */
  84. DEBUG_LINE();
  85. #if defined(NE2000_BASIC_INIT)
  86. /* AX88796L doesn't need */
  87. /* Prepare ESA */
  88. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1); /* Select page 1 */
  89. /* Use the address from the serial EEPROM */
  90. for (i = 0; i < 6; i++)
  91. DP_IN(base, DP_P1_PAR0+i, dp->esa[i]);
  92. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0); /* Select page 0 */
  93. printf("NE2000 - %s ESA: %02x:%02x:%02x:%02x:%02x:%02x\n",
  94. "eeprom",
  95. dp->esa[0],
  96. dp->esa[1],
  97. dp->esa[2],
  98. dp->esa[3],
  99. dp->esa[4],
  100. dp->esa[5] );
  101. #endif /* NE2000_BASIC_INIT */
  102. return true;
  103. }
  104. static void
  105. dp83902a_stop(void)
  106. {
  107. dp83902a_priv_data_t *dp = &nic;
  108. u8 *base = dp->base;
  109. DEBUG_FUNCTION();
  110. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
  111. DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
  112. DP_OUT(base, DP_IMR, 0x00); /* Disable all interrupts */
  113. dp->running = false;
  114. }
  115. /*
  116. * This function is called to "start up" the interface. It may be called
  117. * multiple times, even when the hardware is already running. It will be
  118. * called whenever something "hardware oriented" changes and should leave
  119. * the hardware ready to send/receive packets.
  120. */
  121. static void
  122. dp83902a_start(u8 * enaddr)
  123. {
  124. dp83902a_priv_data_t *dp = &nic;
  125. u8 *base = dp->base;
  126. int i;
  127. DEBUG_FUNCTION();
  128. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
  129. DP_OUT(base, DP_DCR, DP_DCR_INIT);
  130. DP_OUT(base, DP_RBCH, 0); /* Remote byte count */
  131. DP_OUT(base, DP_RBCL, 0);
  132. DP_OUT(base, DP_RCR, DP_RCR_MON); /* Accept no packets */
  133. DP_OUT(base, DP_TCR, DP_TCR_LOCAL); /* Transmitter [virtually] off */
  134. DP_OUT(base, DP_TPSR, dp->tx_buf1); /* Transmitter start page */
  135. dp->tx1 = dp->tx2 = 0;
  136. dp->tx_next = dp->tx_buf1;
  137. dp->tx_started = false;
  138. dp->running = true;
  139. DP_OUT(base, DP_PSTART, dp->rx_buf_start); /* Receive ring start page */
  140. DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1); /* Receive ring boundary */
  141. DP_OUT(base, DP_PSTOP, dp->rx_buf_end); /* Receive ring end page */
  142. dp->rx_next = dp->rx_buf_start - 1;
  143. dp->running = true;
  144. DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
  145. DP_OUT(base, DP_IMR, DP_IMR_All); /* Enable all interrupts */
  146. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1 | DP_CR_STOP); /* Select page 1 */
  147. DP_OUT(base, DP_P1_CURP, dp->rx_buf_start); /* Current page - next free page for Rx */
  148. dp->running = true;
  149. for (i = 0; i < ETHER_ADDR_LEN; i++) {
  150. /* FIXME */
  151. /*((vu_short*)( base + ((DP_P1_PAR0 + i) * 2) +
  152. * 0x1400)) = enaddr[i];*/
  153. DP_OUT(base, DP_P1_PAR0+i, enaddr[i]);
  154. }
  155. /* Enable and start device */
  156. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  157. DP_OUT(base, DP_TCR, DP_TCR_NORMAL); /* Normal transmit operations */
  158. DP_OUT(base, DP_RCR, DP_RCR_AB); /* Accept broadcast, no errors, no multicast */
  159. dp->running = true;
  160. }
  161. /*
  162. * This routine is called to start the transmitter. It is split out from the
  163. * data handling routine so it may be called either when data becomes first
  164. * available or when an Tx interrupt occurs
  165. */
  166. static void
  167. dp83902a_start_xmit(int start_page, int len)
  168. {
  169. dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *) &nic;
  170. u8 *base = dp->base;
  171. DEBUG_FUNCTION();
  172. #if DEBUG & 1
  173. printf("Tx pkt %d len %d\n", start_page, len);
  174. if (dp->tx_started)
  175. printf("TX already started?!?\n");
  176. #endif
  177. DP_OUT(base, DP_ISR, (DP_ISR_TxP | DP_ISR_TxE));
  178. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  179. DP_OUT(base, DP_TBCL, len & 0xFF);
  180. DP_OUT(base, DP_TBCH, len >> 8);
  181. DP_OUT(base, DP_TPSR, start_page);
  182. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
  183. dp->tx_started = true;
  184. }
  185. /*
  186. * This routine is called to send data to the hardware. It is known a-priori
  187. * that there is free buffer space (dp->tx_next).
  188. */
  189. static void
  190. dp83902a_send(u8 *data, int total_len, u32 key)
  191. {
  192. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  193. u8 *base = dp->base;
  194. int len, start_page, pkt_len, i, isr;
  195. #if DEBUG & 4
  196. int dx;
  197. #endif
  198. DEBUG_FUNCTION();
  199. len = pkt_len = total_len;
  200. if (pkt_len < IEEE_8023_MIN_FRAME)
  201. pkt_len = IEEE_8023_MIN_FRAME;
  202. start_page = dp->tx_next;
  203. if (dp->tx_next == dp->tx_buf1) {
  204. dp->tx1 = start_page;
  205. dp->tx1_len = pkt_len;
  206. dp->tx1_key = key;
  207. dp->tx_next = dp->tx_buf2;
  208. } else {
  209. dp->tx2 = start_page;
  210. dp->tx2_len = pkt_len;
  211. dp->tx2_key = key;
  212. dp->tx_next = dp->tx_buf1;
  213. }
  214. #if DEBUG & 5
  215. printf("TX prep page %d len %d\n", start_page, pkt_len);
  216. #endif
  217. DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
  218. {
  219. /*
  220. * Dummy read. The manual sez something slightly different,
  221. * but the code is extended a bit to do what Hitachi's monitor
  222. * does (i.e., also read data).
  223. */
  224. u16 tmp;
  225. int len = 1;
  226. DP_OUT(base, DP_RSAL, 0x100 - len);
  227. DP_OUT(base, DP_RSAH, (start_page - 1) & 0xff);
  228. DP_OUT(base, DP_RBCL, len);
  229. DP_OUT(base, DP_RBCH, 0);
  230. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_RDMA | DP_CR_START);
  231. DP_IN_DATA(dp->data, tmp);
  232. }
  233. #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
  234. /*
  235. * Stall for a bit before continuing to work around random data
  236. * corruption problems on some platforms.
  237. */
  238. CYGACC_CALL_IF_DELAY_US(1);
  239. #endif
  240. /* Send data to device buffer(s) */
  241. DP_OUT(base, DP_RSAL, 0);
  242. DP_OUT(base, DP_RSAH, start_page);
  243. DP_OUT(base, DP_RBCL, pkt_len & 0xFF);
  244. DP_OUT(base, DP_RBCH, pkt_len >> 8);
  245. DP_OUT(base, DP_CR, DP_CR_WDMA | DP_CR_START);
  246. /* Put data into buffer */
  247. #if DEBUG & 4
  248. printf(" sg buf %08lx len %08x\n ", (u32)data, len);
  249. dx = 0;
  250. #endif
  251. while (len > 0) {
  252. #if DEBUG & 4
  253. printf(" %02x", *data);
  254. if (0 == (++dx % 16)) printf("\n ");
  255. #endif
  256. DP_OUT_DATA(dp->data, *data++);
  257. len--;
  258. }
  259. #if DEBUG & 4
  260. printf("\n");
  261. #endif
  262. if (total_len < pkt_len) {
  263. #if DEBUG & 4
  264. printf(" + %d bytes of padding\n", pkt_len - total_len);
  265. #endif
  266. /* Padding to 802.3 length was required */
  267. for (i = total_len; i < pkt_len;) {
  268. i++;
  269. DP_OUT_DATA(dp->data, 0);
  270. }
  271. }
  272. #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
  273. /*
  274. * After last data write, delay for a bit before accessing the
  275. * device again, or we may get random data corruption in the last
  276. * datum (on some platforms).
  277. */
  278. CYGACC_CALL_IF_DELAY_US(1);
  279. #endif
  280. /* Wait for DMA to complete */
  281. do {
  282. DP_IN(base, DP_ISR, isr);
  283. } while ((isr & DP_ISR_RDC) == 0);
  284. /* Then disable DMA */
  285. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  286. /* Start transmit if not already going */
  287. if (!dp->tx_started) {
  288. if (start_page == dp->tx1) {
  289. dp->tx_int = 1; /* Expecting interrupt from BUF1 */
  290. } else {
  291. dp->tx_int = 2; /* Expecting interrupt from BUF2 */
  292. }
  293. dp83902a_start_xmit(start_page, pkt_len);
  294. }
  295. }
  296. /*
  297. * This function is called when a packet has been received. It's job is
  298. * to prepare to unload the packet from the hardware. Once the length of
  299. * the packet is known, the upper layer of the driver can be told. When
  300. * the upper layer is ready to unload the packet, the internal function
  301. * 'dp83902a_recv' will be called to actually fetch it from the hardware.
  302. */
  303. static void
  304. dp83902a_RxEvent(void)
  305. {
  306. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  307. u8 *base = dp->base;
  308. u8 rsr;
  309. u8 rcv_hdr[4];
  310. int i, len, pkt, cur;
  311. DEBUG_FUNCTION();
  312. DP_IN(base, DP_RSR, rsr);
  313. while (true) {
  314. /* Read incoming packet header */
  315. DP_OUT(base, DP_CR, DP_CR_PAGE1 | DP_CR_NODMA | DP_CR_START);
  316. DP_IN(base, DP_P1_CURP, cur);
  317. DP_OUT(base, DP_P1_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  318. DP_IN(base, DP_BNDRY, pkt);
  319. pkt += 1;
  320. if (pkt == dp->rx_buf_end)
  321. pkt = dp->rx_buf_start;
  322. if (pkt == cur) {
  323. break;
  324. }
  325. DP_OUT(base, DP_RBCL, sizeof(rcv_hdr));
  326. DP_OUT(base, DP_RBCH, 0);
  327. DP_OUT(base, DP_RSAL, 0);
  328. DP_OUT(base, DP_RSAH, pkt);
  329. if (dp->rx_next == pkt) {
  330. if (cur == dp->rx_buf_start)
  331. DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
  332. else
  333. DP_OUT(base, DP_BNDRY, cur - 1); /* Update pointer */
  334. return;
  335. }
  336. dp->rx_next = pkt;
  337. DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
  338. DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
  339. #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
  340. CYGACC_CALL_IF_DELAY_US(10);
  341. #endif
  342. /* read header (get data size)*/
  343. for (i = 0; i < sizeof(rcv_hdr);) {
  344. DP_IN_DATA(dp->data, rcv_hdr[i++]);
  345. }
  346. #if DEBUG & 5
  347. printf("rx hdr %02x %02x %02x %02x\n",
  348. rcv_hdr[0], rcv_hdr[1], rcv_hdr[2], rcv_hdr[3]);
  349. #endif
  350. len = ((rcv_hdr[3] << 8) | rcv_hdr[2]) - sizeof(rcv_hdr);
  351. /* data read */
  352. uboot_push_packet_len(len);
  353. if (rcv_hdr[1] == dp->rx_buf_start)
  354. DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
  355. else
  356. DP_OUT(base, DP_BNDRY, rcv_hdr[1] - 1); /* Update pointer */
  357. }
  358. }
  359. /*
  360. * This function is called as a result of the "eth_drv_recv()" call above.
  361. * It's job is to actually fetch data for a packet from the hardware once
  362. * memory buffers have been allocated for the packet. Note that the buffers
  363. * may come in pieces, using a scatter-gather list. This allows for more
  364. * efficient processing in the upper layers of the stack.
  365. */
  366. static void
  367. dp83902a_recv(u8 *data, int len)
  368. {
  369. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  370. u8 *base = dp->base;
  371. int i, mlen;
  372. u8 saved_char = 0;
  373. bool saved;
  374. #if DEBUG & 4
  375. int dx;
  376. #endif
  377. DEBUG_FUNCTION();
  378. #if DEBUG & 5
  379. printf("Rx packet %d length %d\n", dp->rx_next, len);
  380. #endif
  381. /* Read incoming packet data */
  382. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  383. DP_OUT(base, DP_RBCL, len & 0xFF);
  384. DP_OUT(base, DP_RBCH, len >> 8);
  385. DP_OUT(base, DP_RSAL, 4); /* Past header */
  386. DP_OUT(base, DP_RSAH, dp->rx_next);
  387. DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
  388. DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
  389. #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
  390. CYGACC_CALL_IF_DELAY_US(10);
  391. #endif
  392. saved = false;
  393. for (i = 0; i < 1; i++) {
  394. if (data) {
  395. mlen = len;
  396. #if DEBUG & 4
  397. printf(" sg buf %08lx len %08x \n", (u32) data, mlen);
  398. dx = 0;
  399. #endif
  400. while (0 < mlen) {
  401. /* Saved byte from previous loop? */
  402. if (saved) {
  403. *data++ = saved_char;
  404. mlen--;
  405. saved = false;
  406. continue;
  407. }
  408. {
  409. u8 tmp;
  410. DP_IN_DATA(dp->data, tmp);
  411. #if DEBUG & 4
  412. printf(" %02x", tmp);
  413. if (0 == (++dx % 16)) printf("\n ");
  414. #endif
  415. *data++ = tmp;;
  416. mlen--;
  417. }
  418. }
  419. #if DEBUG & 4
  420. printf("\n");
  421. #endif
  422. }
  423. }
  424. }
  425. static void
  426. dp83902a_TxEvent(void)
  427. {
  428. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  429. u8 *base = dp->base;
  430. u8 tsr;
  431. u32 key;
  432. DEBUG_FUNCTION();
  433. DP_IN(base, DP_TSR, tsr);
  434. if (dp->tx_int == 1) {
  435. key = dp->tx1_key;
  436. dp->tx1 = 0;
  437. } else {
  438. key = dp->tx2_key;
  439. dp->tx2 = 0;
  440. }
  441. /* Start next packet if one is ready */
  442. dp->tx_started = false;
  443. if (dp->tx1) {
  444. dp83902a_start_xmit(dp->tx1, dp->tx1_len);
  445. dp->tx_int = 1;
  446. } else if (dp->tx2) {
  447. dp83902a_start_xmit(dp->tx2, dp->tx2_len);
  448. dp->tx_int = 2;
  449. } else {
  450. dp->tx_int = 0;
  451. }
  452. /* Tell higher level we sent this packet */
  453. uboot_push_tx_done(key, 0);
  454. }
  455. /*
  456. * Read the tally counters to clear them. Called in response to a CNT
  457. * interrupt.
  458. */
  459. static void
  460. dp83902a_ClearCounters(void)
  461. {
  462. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  463. u8 *base = dp->base;
  464. u8 cnt1, cnt2, cnt3;
  465. DP_IN(base, DP_FER, cnt1);
  466. DP_IN(base, DP_CER, cnt2);
  467. DP_IN(base, DP_MISSED, cnt3);
  468. DP_OUT(base, DP_ISR, DP_ISR_CNT);
  469. }
  470. /*
  471. * Deal with an overflow condition. This code follows the procedure set
  472. * out in section 7.0 of the datasheet.
  473. */
  474. static void
  475. dp83902a_Overflow(void)
  476. {
  477. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *)&nic;
  478. u8 *base = dp->base;
  479. u8 isr;
  480. /* Issue a stop command and wait 1.6ms for it to complete. */
  481. DP_OUT(base, DP_CR, DP_CR_STOP | DP_CR_NODMA);
  482. CYGACC_CALL_IF_DELAY_US(1600);
  483. /* Clear the remote byte counter registers. */
  484. DP_OUT(base, DP_RBCL, 0);
  485. DP_OUT(base, DP_RBCH, 0);
  486. /* Enter loopback mode while we clear the buffer. */
  487. DP_OUT(base, DP_TCR, DP_TCR_LOCAL);
  488. DP_OUT(base, DP_CR, DP_CR_START | DP_CR_NODMA);
  489. /*
  490. * Read in as many packets as we can and acknowledge any and receive
  491. * interrupts. Since the buffer has overflowed, a receive event of
  492. * some kind will have occured.
  493. */
  494. dp83902a_RxEvent();
  495. DP_OUT(base, DP_ISR, DP_ISR_RxP|DP_ISR_RxE);
  496. /* Clear the overflow condition and leave loopback mode. */
  497. DP_OUT(base, DP_ISR, DP_ISR_OFLW);
  498. DP_OUT(base, DP_TCR, DP_TCR_NORMAL);
  499. /*
  500. * If a transmit command was issued, but no transmit event has occured,
  501. * restart it here.
  502. */
  503. DP_IN(base, DP_ISR, isr);
  504. if (dp->tx_started && !(isr & (DP_ISR_TxP|DP_ISR_TxE))) {
  505. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
  506. }
  507. }
  508. static void
  509. dp83902a_poll(void)
  510. {
  511. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  512. u8 *base = dp->base;
  513. u8 isr;
  514. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0 | DP_CR_START);
  515. DP_IN(base, DP_ISR, isr);
  516. while (0 != isr) {
  517. /*
  518. * The CNT interrupt triggers when the MSB of one of the error
  519. * counters is set. We don't much care about these counters, but
  520. * we should read their values to reset them.
  521. */
  522. if (isr & DP_ISR_CNT) {
  523. dp83902a_ClearCounters();
  524. }
  525. /*
  526. * Check for overflow. It's a special case, since there's a
  527. * particular procedure that must be followed to get back into
  528. * a running state.a
  529. */
  530. if (isr & DP_ISR_OFLW) {
  531. dp83902a_Overflow();
  532. } else {
  533. /*
  534. * Other kinds of interrupts can be acknowledged simply by
  535. * clearing the relevant bits of the ISR. Do that now, then
  536. * handle the interrupts we care about.
  537. */
  538. DP_OUT(base, DP_ISR, isr); /* Clear set bits */
  539. if (!dp->running) break; /* Is this necessary? */
  540. /*
  541. * Check for tx_started on TX event since these may happen
  542. * spuriously it seems.
  543. */
  544. if (isr & (DP_ISR_TxP|DP_ISR_TxE) && dp->tx_started) {
  545. dp83902a_TxEvent();
  546. }
  547. if (isr & (DP_ISR_RxP|DP_ISR_RxE)) {
  548. dp83902a_RxEvent();
  549. }
  550. }
  551. DP_IN(base, DP_ISR, isr);
  552. }
  553. }
  554. /* U-boot specific routines */
  555. static u8 *pbuf = NULL;
  556. static int pkey = -1;
  557. static int initialized = 0;
  558. void uboot_push_packet_len(int len) {
  559. PRINTK("pushed len = %d\n", len);
  560. if (len >= 2000) {
  561. printf("NE2000: packet too big\n");
  562. return;
  563. }
  564. dp83902a_recv(&pbuf[0], len);
  565. /*Just pass it to the upper layer*/
  566. NetReceive(&pbuf[0], len);
  567. }
  568. void uboot_push_tx_done(int key, int val) {
  569. PRINTK("pushed key = %d\n", key);
  570. pkey = key;
  571. }
  572. int eth_init(bd_t *bd) {
  573. int r;
  574. u8 dev_addr[6];
  575. char ethaddr[20];
  576. PRINTK("### eth_init\n");
  577. if (!pbuf) {
  578. pbuf = malloc(2000);
  579. if (!pbuf) {
  580. printf("Cannot allocate rx buffer\n");
  581. return -1;
  582. }
  583. }
  584. #ifdef CONFIG_DRIVER_NE2000_CCR
  585. {
  586. vu_char *p = (vu_char *) CONFIG_DRIVER_NE2000_CCR;
  587. PRINTK("CCR before is %x\n", *p);
  588. *p = CONFIG_DRIVER_NE2000_VAL;
  589. PRINTK("CCR after is %x\n", *p);
  590. }
  591. #endif
  592. nic.base = (u8 *) CONFIG_DRIVER_NE2000_BASE;
  593. r = get_prom(dev_addr, nic.base);
  594. if (!r)
  595. return -1;
  596. sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  597. dev_addr[0], dev_addr[1],
  598. dev_addr[2], dev_addr[3],
  599. dev_addr[4], dev_addr[5]) ;
  600. PRINTK("Set environment from HW MAC addr = \"%s\"\n", ethaddr);
  601. setenv ("ethaddr", ethaddr);
  602. nic.data = nic.base + DP_DATA;
  603. nic.tx_buf1 = START_PG;
  604. nic.tx_buf2 = START_PG2;
  605. nic.rx_buf_start = RX_START;
  606. nic.rx_buf_end = RX_END;
  607. if (dp83902a_init() == false)
  608. return -1;
  609. dp83902a_start(dev_addr);
  610. initialized = 1;
  611. return 0;
  612. }
  613. void eth_halt() {
  614. PRINTK("### eth_halt\n");
  615. if(initialized)
  616. dp83902a_stop();
  617. initialized = 0;
  618. }
  619. int eth_rx() {
  620. dp83902a_poll();
  621. return 1;
  622. }
  623. int eth_send(volatile void *packet, int length) {
  624. int tmo;
  625. PRINTK("### eth_send\n");
  626. pkey = -1;
  627. dp83902a_send((u8 *) packet, length, 666);
  628. tmo = get_timer (0) + TOUT * CONFIG_SYS_HZ;
  629. while(1) {
  630. dp83902a_poll();
  631. if (pkey != -1) {
  632. PRINTK("Packet sucesfully sent\n");
  633. return 0;
  634. }
  635. if (get_timer (0) >= tmo) {
  636. printf("transmission error (timoeut)\n");
  637. return 0;
  638. }
  639. }
  640. return 0;
  641. }