ne2000_base.c 21 KB

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