ne2000_base.c 20 KB

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