ne2000_base.c 20 KB

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