ne2000_base.c 19 KB

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