ne2000.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. /*
  64. * Debugging details
  65. *
  66. * Set to perms of:
  67. * 0 disables all debug output
  68. * 1 for process debug output
  69. * 2 for added data IO output: get_reg, put_reg
  70. * 4 for packet allocation/free output
  71. * 8 for only startup status, so we can tell we're installed OK
  72. */
  73. #if 0
  74. #define DEBUG 0xf
  75. #else
  76. #define DEBUG 0
  77. #endif
  78. #if DEBUG & 1
  79. #define DEBUG_FUNCTION() do { printf("%s\n", __FUNCTION__); } while (0)
  80. #define DEBUG_LINE() do { printf("%d\n", __LINE__); } while (0)
  81. #define PRINTK(args...) printf(args)
  82. #else
  83. #define DEBUG_FUNCTION() do {} while(0)
  84. #define DEBUG_LINE() do {} while(0)
  85. #define PRINTK(args...)
  86. #endif
  87. /* NE2000 base header file */
  88. #include "ne2000_base.h"
  89. #if defined(CONFIG_DRIVER_AX88796L)
  90. /* AX88796L support */
  91. #include "ax88796.h"
  92. #else
  93. /* Basic NE2000 chip support */
  94. #include "ne2000.h"
  95. #endif
  96. static dp83902a_priv_data_t nic; /* just one instance of the card supported */
  97. static bool
  98. dp83902a_init(void)
  99. {
  100. dp83902a_priv_data_t *dp = &nic;
  101. u8* base;
  102. #if defined(NE2000_BASIC_INIT)
  103. int i;
  104. #endif
  105. DEBUG_FUNCTION();
  106. base = dp->base;
  107. if (!base)
  108. return false; /* No device found */
  109. DEBUG_LINE();
  110. #if defined(NE2000_BASIC_INIT)
  111. /* AX88796L doesn't need */
  112. /* Prepare ESA */
  113. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1); /* Select page 1 */
  114. /* Use the address from the serial EEPROM */
  115. for (i = 0; i < 6; i++)
  116. DP_IN(base, DP_P1_PAR0+i, dp->esa[i]);
  117. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0); /* Select page 0 */
  118. printf("NE2000 - %s ESA: %02x:%02x:%02x:%02x:%02x:%02x\n",
  119. "eeprom",
  120. dp->esa[0],
  121. dp->esa[1],
  122. dp->esa[2],
  123. dp->esa[3],
  124. dp->esa[4],
  125. dp->esa[5] );
  126. #endif /* NE2000_BASIC_INIT */
  127. return true;
  128. }
  129. static void
  130. dp83902a_stop(void)
  131. {
  132. dp83902a_priv_data_t *dp = &nic;
  133. u8 *base = dp->base;
  134. DEBUG_FUNCTION();
  135. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
  136. DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
  137. DP_OUT(base, DP_IMR, 0x00); /* Disable all interrupts */
  138. dp->running = false;
  139. }
  140. /*
  141. * This function is called to "start up" the interface. It may be called
  142. * multiple times, even when the hardware is already running. It will be
  143. * called whenever something "hardware oriented" changes and should leave
  144. * the hardware ready to send/receive packets.
  145. */
  146. static void
  147. dp83902a_start(u8 * enaddr)
  148. {
  149. dp83902a_priv_data_t *dp = &nic;
  150. u8 *base = dp->base;
  151. int i;
  152. DEBUG_FUNCTION();
  153. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_STOP); /* Brutal */
  154. DP_OUT(base, DP_DCR, DP_DCR_INIT);
  155. DP_OUT(base, DP_RBCH, 0); /* Remote byte count */
  156. DP_OUT(base, DP_RBCL, 0);
  157. DP_OUT(base, DP_RCR, DP_RCR_MON); /* Accept no packets */
  158. DP_OUT(base, DP_TCR, DP_TCR_LOCAL); /* Transmitter [virtually] off */
  159. DP_OUT(base, DP_TPSR, dp->tx_buf1); /* Transmitter start page */
  160. dp->tx1 = dp->tx2 = 0;
  161. dp->tx_next = dp->tx_buf1;
  162. dp->tx_started = false;
  163. dp->running = true;
  164. DP_OUT(base, DP_PSTART, dp->rx_buf_start); /* Receive ring start page */
  165. DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1); /* Receive ring boundary */
  166. DP_OUT(base, DP_PSTOP, dp->rx_buf_end); /* Receive ring end page */
  167. dp->rx_next = dp->rx_buf_start - 1;
  168. dp->running = true;
  169. DP_OUT(base, DP_ISR, 0xFF); /* Clear any pending interrupts */
  170. DP_OUT(base, DP_IMR, DP_IMR_All); /* Enable all interrupts */
  171. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE1 | DP_CR_STOP); /* Select page 1 */
  172. DP_OUT(base, DP_P1_CURP, dp->rx_buf_start); /* Current page - next free page for Rx */
  173. dp->running = true;
  174. for (i = 0; i < ETHER_ADDR_LEN; i++) {
  175. /* FIXME */
  176. /*((vu_short*)( base + ((DP_P1_PAR0 + i) * 2) +
  177. * 0x1400)) = enaddr[i];*/
  178. DP_OUT(base, DP_P1_PAR0+i, enaddr[i]);
  179. }
  180. /* Enable and start device */
  181. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  182. DP_OUT(base, DP_TCR, DP_TCR_NORMAL); /* Normal transmit operations */
  183. DP_OUT(base, DP_RCR, DP_RCR_AB); /* Accept broadcast, no errors, no multicast */
  184. dp->running = true;
  185. }
  186. /*
  187. * This routine is called to start the transmitter. It is split out from the
  188. * data handling routine so it may be called either when data becomes first
  189. * available or when an Tx interrupt occurs
  190. */
  191. static void
  192. dp83902a_start_xmit(int start_page, int len)
  193. {
  194. dp83902a_priv_data_t *dp = (dp83902a_priv_data_t *) &nic;
  195. u8 *base = dp->base;
  196. DEBUG_FUNCTION();
  197. #if DEBUG & 1
  198. printf("Tx pkt %d len %d\n", start_page, len);
  199. if (dp->tx_started)
  200. printf("TX already started?!?\n");
  201. #endif
  202. DP_OUT(base, DP_ISR, (DP_ISR_TxP | DP_ISR_TxE));
  203. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  204. DP_OUT(base, DP_TBCL, len & 0xFF);
  205. DP_OUT(base, DP_TBCH, len >> 8);
  206. DP_OUT(base, DP_TPSR, start_page);
  207. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
  208. dp->tx_started = true;
  209. }
  210. /*
  211. * This routine is called to send data to the hardware. It is known a-priori
  212. * that there is free buffer space (dp->tx_next).
  213. */
  214. static void
  215. dp83902a_send(u8 *data, int total_len, u32 key)
  216. {
  217. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  218. u8 *base = dp->base;
  219. int len, start_page, pkt_len, i, isr;
  220. #if DEBUG & 4
  221. int dx;
  222. #endif
  223. DEBUG_FUNCTION();
  224. len = pkt_len = total_len;
  225. if (pkt_len < IEEE_8023_MIN_FRAME)
  226. pkt_len = IEEE_8023_MIN_FRAME;
  227. start_page = dp->tx_next;
  228. if (dp->tx_next == dp->tx_buf1) {
  229. dp->tx1 = start_page;
  230. dp->tx1_len = pkt_len;
  231. dp->tx1_key = key;
  232. dp->tx_next = dp->tx_buf2;
  233. } else {
  234. dp->tx2 = start_page;
  235. dp->tx2_len = pkt_len;
  236. dp->tx2_key = key;
  237. dp->tx_next = dp->tx_buf1;
  238. }
  239. #if DEBUG & 5
  240. printf("TX prep page %d len %d\n", start_page, pkt_len);
  241. #endif
  242. DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
  243. {
  244. /*
  245. * Dummy read. The manual sez something slightly different,
  246. * but the code is extended a bit to do what Hitachi's monitor
  247. * does (i.e., also read data).
  248. */
  249. u16 tmp;
  250. int len = 1;
  251. DP_OUT(base, DP_RSAL, 0x100 - len);
  252. DP_OUT(base, DP_RSAH, (start_page - 1) & 0xff);
  253. DP_OUT(base, DP_RBCL, len);
  254. DP_OUT(base, DP_RBCH, 0);
  255. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_RDMA | DP_CR_START);
  256. DP_IN_DATA(dp->data, tmp);
  257. }
  258. #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
  259. /*
  260. * Stall for a bit before continuing to work around random data
  261. * corruption problems on some platforms.
  262. */
  263. CYGACC_CALL_IF_DELAY_US(1);
  264. #endif
  265. /* Send data to device buffer(s) */
  266. DP_OUT(base, DP_RSAL, 0);
  267. DP_OUT(base, DP_RSAH, start_page);
  268. DP_OUT(base, DP_RBCL, pkt_len & 0xFF);
  269. DP_OUT(base, DP_RBCH, pkt_len >> 8);
  270. DP_OUT(base, DP_CR, DP_CR_WDMA | DP_CR_START);
  271. /* Put data into buffer */
  272. #if DEBUG & 4
  273. printf(" sg buf %08lx len %08x\n ", (u32)data, len);
  274. dx = 0;
  275. #endif
  276. while (len > 0) {
  277. #if DEBUG & 4
  278. printf(" %02x", *data);
  279. if (0 == (++dx % 16)) printf("\n ");
  280. #endif
  281. DP_OUT_DATA(dp->data, *data++);
  282. len--;
  283. }
  284. #if DEBUG & 4
  285. printf("\n");
  286. #endif
  287. if (total_len < pkt_len) {
  288. #if DEBUG & 4
  289. printf(" + %d bytes of padding\n", pkt_len - total_len);
  290. #endif
  291. /* Padding to 802.3 length was required */
  292. for (i = total_len; i < pkt_len;) {
  293. i++;
  294. DP_OUT_DATA(dp->data, 0);
  295. }
  296. }
  297. #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_TX_DMA
  298. /*
  299. * After last data write, delay for a bit before accessing the
  300. * device again, or we may get random data corruption in the last
  301. * datum (on some platforms).
  302. */
  303. CYGACC_CALL_IF_DELAY_US(1);
  304. #endif
  305. /* Wait for DMA to complete */
  306. do {
  307. DP_IN(base, DP_ISR, isr);
  308. } while ((isr & DP_ISR_RDC) == 0);
  309. /* Then disable DMA */
  310. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  311. /* Start transmit if not already going */
  312. if (!dp->tx_started) {
  313. if (start_page == dp->tx1) {
  314. dp->tx_int = 1; /* Expecting interrupt from BUF1 */
  315. } else {
  316. dp->tx_int = 2; /* Expecting interrupt from BUF2 */
  317. }
  318. dp83902a_start_xmit(start_page, pkt_len);
  319. }
  320. }
  321. /*
  322. * This function is called when a packet has been received. It's job is
  323. * to prepare to unload the packet from the hardware. Once the length of
  324. * the packet is known, the upper layer of the driver can be told. When
  325. * the upper layer is ready to unload the packet, the internal function
  326. * 'dp83902a_recv' will be called to actually fetch it from the hardware.
  327. */
  328. static void
  329. dp83902a_RxEvent(void)
  330. {
  331. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  332. u8 *base = dp->base;
  333. u8 rsr;
  334. u8 rcv_hdr[4];
  335. int i, len, pkt, cur;
  336. DEBUG_FUNCTION();
  337. DP_IN(base, DP_RSR, rsr);
  338. while (true) {
  339. /* Read incoming packet header */
  340. DP_OUT(base, DP_CR, DP_CR_PAGE1 | DP_CR_NODMA | DP_CR_START);
  341. DP_IN(base, DP_P1_CURP, cur);
  342. DP_OUT(base, DP_P1_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  343. DP_IN(base, DP_BNDRY, pkt);
  344. pkt += 1;
  345. if (pkt == dp->rx_buf_end)
  346. pkt = dp->rx_buf_start;
  347. if (pkt == cur) {
  348. break;
  349. }
  350. DP_OUT(base, DP_RBCL, sizeof(rcv_hdr));
  351. DP_OUT(base, DP_RBCH, 0);
  352. DP_OUT(base, DP_RSAL, 0);
  353. DP_OUT(base, DP_RSAH, pkt);
  354. if (dp->rx_next == pkt) {
  355. if (cur == dp->rx_buf_start)
  356. DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
  357. else
  358. DP_OUT(base, DP_BNDRY, cur - 1); /* Update pointer */
  359. return;
  360. }
  361. dp->rx_next = pkt;
  362. DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
  363. DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
  364. #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
  365. CYGACC_CALL_IF_DELAY_US(10);
  366. #endif
  367. /* read header (get data size)*/
  368. for (i = 0; i < sizeof(rcv_hdr);) {
  369. DP_IN_DATA(dp->data, rcv_hdr[i++]);
  370. }
  371. #if DEBUG & 5
  372. printf("rx hdr %02x %02x %02x %02x\n",
  373. rcv_hdr[0], rcv_hdr[1], rcv_hdr[2], rcv_hdr[3]);
  374. #endif
  375. len = ((rcv_hdr[3] << 8) | rcv_hdr[2]) - sizeof(rcv_hdr);
  376. /* data read */
  377. uboot_push_packet_len(len);
  378. if (rcv_hdr[1] == dp->rx_buf_start)
  379. DP_OUT(base, DP_BNDRY, dp->rx_buf_end - 1);
  380. else
  381. DP_OUT(base, DP_BNDRY, rcv_hdr[1] - 1); /* Update pointer */
  382. }
  383. }
  384. /*
  385. * This function is called as a result of the "eth_drv_recv()" call above.
  386. * It's job is to actually fetch data for a packet from the hardware once
  387. * memory buffers have been allocated for the packet. Note that the buffers
  388. * may come in pieces, using a scatter-gather list. This allows for more
  389. * efficient processing in the upper layers of the stack.
  390. */
  391. static void
  392. dp83902a_recv(u8 *data, int len)
  393. {
  394. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  395. u8 *base = dp->base;
  396. int i, mlen;
  397. u8 saved_char = 0;
  398. bool saved;
  399. #if DEBUG & 4
  400. int dx;
  401. #endif
  402. DEBUG_FUNCTION();
  403. #if DEBUG & 5
  404. printf("Rx packet %d length %d\n", dp->rx_next, len);
  405. #endif
  406. /* Read incoming packet data */
  407. DP_OUT(base, DP_CR, DP_CR_PAGE0 | DP_CR_NODMA | DP_CR_START);
  408. DP_OUT(base, DP_RBCL, len & 0xFF);
  409. DP_OUT(base, DP_RBCH, len >> 8);
  410. DP_OUT(base, DP_RSAL, 4); /* Past header */
  411. DP_OUT(base, DP_RSAH, dp->rx_next);
  412. DP_OUT(base, DP_ISR, DP_ISR_RDC); /* Clear end of DMA */
  413. DP_OUT(base, DP_CR, DP_CR_RDMA | DP_CR_START);
  414. #ifdef CYGHWR_NS_DP83902A_PLF_BROKEN_RX_DMA
  415. CYGACC_CALL_IF_DELAY_US(10);
  416. #endif
  417. saved = false;
  418. for (i = 0; i < 1; i++) {
  419. if (data) {
  420. mlen = len;
  421. #if DEBUG & 4
  422. printf(" sg buf %08lx len %08x \n", (u32) data, mlen);
  423. dx = 0;
  424. #endif
  425. while (0 < mlen) {
  426. /* Saved byte from previous loop? */
  427. if (saved) {
  428. *data++ = saved_char;
  429. mlen--;
  430. saved = false;
  431. continue;
  432. }
  433. {
  434. u8 tmp;
  435. DP_IN_DATA(dp->data, tmp);
  436. #if DEBUG & 4
  437. printf(" %02x", tmp);
  438. if (0 == (++dx % 16)) printf("\n ");
  439. #endif
  440. *data++ = tmp;;
  441. mlen--;
  442. }
  443. }
  444. #if DEBUG & 4
  445. printf("\n");
  446. #endif
  447. }
  448. }
  449. }
  450. static void
  451. dp83902a_TxEvent(void)
  452. {
  453. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  454. u8 *base = dp->base;
  455. u8 tsr;
  456. u32 key;
  457. DEBUG_FUNCTION();
  458. DP_IN(base, DP_TSR, tsr);
  459. if (dp->tx_int == 1) {
  460. key = dp->tx1_key;
  461. dp->tx1 = 0;
  462. } else {
  463. key = dp->tx2_key;
  464. dp->tx2 = 0;
  465. }
  466. /* Start next packet if one is ready */
  467. dp->tx_started = false;
  468. if (dp->tx1) {
  469. dp83902a_start_xmit(dp->tx1, dp->tx1_len);
  470. dp->tx_int = 1;
  471. } else if (dp->tx2) {
  472. dp83902a_start_xmit(dp->tx2, dp->tx2_len);
  473. dp->tx_int = 2;
  474. } else {
  475. dp->tx_int = 0;
  476. }
  477. /* Tell higher level we sent this packet */
  478. uboot_push_tx_done(key, 0);
  479. }
  480. /*
  481. * Read the tally counters to clear them. Called in response to a CNT
  482. * interrupt.
  483. */
  484. static void
  485. dp83902a_ClearCounters(void)
  486. {
  487. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  488. u8 *base = dp->base;
  489. u8 cnt1, cnt2, cnt3;
  490. DP_IN(base, DP_FER, cnt1);
  491. DP_IN(base, DP_CER, cnt2);
  492. DP_IN(base, DP_MISSED, cnt3);
  493. DP_OUT(base, DP_ISR, DP_ISR_CNT);
  494. }
  495. /*
  496. * Deal with an overflow condition. This code follows the procedure set
  497. * out in section 7.0 of the datasheet.
  498. */
  499. static void
  500. dp83902a_Overflow(void)
  501. {
  502. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *)&nic;
  503. u8 *base = dp->base;
  504. u8 isr;
  505. /* Issue a stop command and wait 1.6ms for it to complete. */
  506. DP_OUT(base, DP_CR, DP_CR_STOP | DP_CR_NODMA);
  507. CYGACC_CALL_IF_DELAY_US(1600);
  508. /* Clear the remote byte counter registers. */
  509. DP_OUT(base, DP_RBCL, 0);
  510. DP_OUT(base, DP_RBCH, 0);
  511. /* Enter loopback mode while we clear the buffer. */
  512. DP_OUT(base, DP_TCR, DP_TCR_LOCAL);
  513. DP_OUT(base, DP_CR, DP_CR_START | DP_CR_NODMA);
  514. /*
  515. * Read in as many packets as we can and acknowledge any and receive
  516. * interrupts. Since the buffer has overflowed, a receive event of
  517. * some kind will have occured.
  518. */
  519. dp83902a_RxEvent();
  520. DP_OUT(base, DP_ISR, DP_ISR_RxP|DP_ISR_RxE);
  521. /* Clear the overflow condition and leave loopback mode. */
  522. DP_OUT(base, DP_ISR, DP_ISR_OFLW);
  523. DP_OUT(base, DP_TCR, DP_TCR_NORMAL);
  524. /*
  525. * If a transmit command was issued, but no transmit event has occured,
  526. * restart it here.
  527. */
  528. DP_IN(base, DP_ISR, isr);
  529. if (dp->tx_started && !(isr & (DP_ISR_TxP|DP_ISR_TxE))) {
  530. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_TXPKT | DP_CR_START);
  531. }
  532. }
  533. static void
  534. dp83902a_poll(void)
  535. {
  536. struct dp83902a_priv_data *dp = (struct dp83902a_priv_data *) &nic;
  537. u8 *base = dp->base;
  538. u8 isr;
  539. DP_OUT(base, DP_CR, DP_CR_NODMA | DP_CR_PAGE0 | DP_CR_START);
  540. DP_IN(base, DP_ISR, isr);
  541. while (0 != isr) {
  542. /*
  543. * The CNT interrupt triggers when the MSB of one of the error
  544. * counters is set. We don't much care about these counters, but
  545. * we should read their values to reset them.
  546. */
  547. if (isr & DP_ISR_CNT) {
  548. dp83902a_ClearCounters();
  549. }
  550. /*
  551. * Check for overflow. It's a special case, since there's a
  552. * particular procedure that must be followed to get back into
  553. * a running state.a
  554. */
  555. if (isr & DP_ISR_OFLW) {
  556. dp83902a_Overflow();
  557. } else {
  558. /*
  559. * Other kinds of interrupts can be acknowledged simply by
  560. * clearing the relevant bits of the ISR. Do that now, then
  561. * handle the interrupts we care about.
  562. */
  563. DP_OUT(base, DP_ISR, isr); /* Clear set bits */
  564. if (!dp->running) break; /* Is this necessary? */
  565. /*
  566. * Check for tx_started on TX event since these may happen
  567. * spuriously it seems.
  568. */
  569. if (isr & (DP_ISR_TxP|DP_ISR_TxE) && dp->tx_started) {
  570. dp83902a_TxEvent();
  571. }
  572. if (isr & (DP_ISR_RxP|DP_ISR_RxE)) {
  573. dp83902a_RxEvent();
  574. }
  575. }
  576. DP_IN(base, DP_ISR, isr);
  577. }
  578. }
  579. /* find prom (taken from pc_net_cs.c from Linux) */
  580. #include "8390.h"
  581. /*
  582. typedef struct hw_info_t {
  583. u_int offset;
  584. u_char a0, a1, a2;
  585. u_int flags;
  586. } hw_info_t;
  587. */
  588. #define DELAY_OUTPUT 0x01
  589. #define HAS_MISC_REG 0x02
  590. #define USE_BIG_BUF 0x04
  591. #define HAS_IBM_MISC 0x08
  592. #define IS_DL10019 0x10
  593. #define IS_DL10022 0x20
  594. #define HAS_MII 0x40
  595. #define USE_SHMEM 0x80 /* autodetected */
  596. #define AM79C9XX_HOME_PHY 0x00006B90 /* HomePNA PHY */
  597. #define AM79C9XX_ETH_PHY 0x00006B70 /* 10baseT PHY */
  598. #define MII_PHYID_REV_MASK 0xfffffff0
  599. #define MII_PHYID_REG1 0x02
  600. #define MII_PHYID_REG2 0x03
  601. static hw_info_t hw_info[] = {
  602. { /* Accton EN2212 */ 0x0ff0, 0x00, 0x00, 0xe8, DELAY_OUTPUT },
  603. { /* Allied Telesis LA-PCM */ 0x0ff0, 0x00, 0x00, 0xf4, 0 },
  604. { /* APEX MultiCard */ 0x03f4, 0x00, 0x20, 0xe5, 0 },
  605. { /* ASANTE FriendlyNet */ 0x4910, 0x00, 0x00, 0x94,
  606. DELAY_OUTPUT | HAS_IBM_MISC },
  607. { /* Danpex EN-6200P2 */ 0x0110, 0x00, 0x40, 0xc7, 0 },
  608. { /* DataTrek NetCard */ 0x0ff0, 0x00, 0x20, 0xe8, 0 },
  609. { /* Dayna CommuniCard E */ 0x0110, 0x00, 0x80, 0x19, 0 },
  610. { /* D-Link DE-650 */ 0x0040, 0x00, 0x80, 0xc8, 0 },
  611. { /* EP-210 Ethernet */ 0x0110, 0x00, 0x40, 0x33, 0 },
  612. { /* EP4000 Ethernet */ 0x01c0, 0x00, 0x00, 0xb4, 0 },
  613. { /* Epson EEN10B */ 0x0ff0, 0x00, 0x00, 0x48,
  614. HAS_MISC_REG | HAS_IBM_MISC },
  615. { /* ELECOM Laneed LD-CDWA */ 0xb8, 0x08, 0x00, 0x42, 0 },
  616. { /* Hypertec Ethernet */ 0x01c0, 0x00, 0x40, 0x4c, 0 },
  617. { /* IBM CCAE */ 0x0ff0, 0x08, 0x00, 0x5a,
  618. HAS_MISC_REG | HAS_IBM_MISC },
  619. { /* IBM CCAE */ 0x0ff0, 0x00, 0x04, 0xac,
  620. HAS_MISC_REG | HAS_IBM_MISC },
  621. { /* IBM CCAE */ 0x0ff0, 0x00, 0x06, 0x29,
  622. HAS_MISC_REG | HAS_IBM_MISC },
  623. { /* IBM FME */ 0x0374, 0x08, 0x00, 0x5a,
  624. HAS_MISC_REG | HAS_IBM_MISC },
  625. { /* IBM FME */ 0x0374, 0x00, 0x04, 0xac,
  626. HAS_MISC_REG | HAS_IBM_MISC },
  627. { /* Kansai KLA-PCM/T */ 0x0ff0, 0x00, 0x60, 0x87,
  628. HAS_MISC_REG | HAS_IBM_MISC },
  629. { /* NSC DP83903 */ 0x0374, 0x08, 0x00, 0x17,
  630. HAS_MISC_REG | HAS_IBM_MISC },
  631. { /* NSC DP83903 */ 0x0374, 0x00, 0xc0, 0xa8,
  632. HAS_MISC_REG | HAS_IBM_MISC },
  633. { /* NSC DP83903 */ 0x0374, 0x00, 0xa0, 0xb0,
  634. HAS_MISC_REG | HAS_IBM_MISC },
  635. { /* NSC DP83903 */ 0x0198, 0x00, 0x20, 0xe0,
  636. HAS_MISC_REG | HAS_IBM_MISC },
  637. { /* I-O DATA PCLA/T */ 0x0ff0, 0x00, 0xa0, 0xb0, 0 },
  638. { /* Katron PE-520 */ 0x0110, 0x00, 0x40, 0xf6, 0 },
  639. { /* Kingston KNE-PCM/x */ 0x0ff0, 0x00, 0xc0, 0xf0,
  640. HAS_MISC_REG | HAS_IBM_MISC },
  641. { /* Kingston KNE-PCM/x */ 0x0ff0, 0xe2, 0x0c, 0x0f,
  642. HAS_MISC_REG | HAS_IBM_MISC },
  643. { /* Kingston KNE-PC2 */ 0x0180, 0x00, 0xc0, 0xf0, 0 },
  644. { /* Maxtech PCN2000 */ 0x5000, 0x00, 0x00, 0xe8, 0 },
  645. { /* NDC Instant-Link */ 0x003a, 0x00, 0x80, 0xc6, 0 },
  646. { /* NE2000 Compatible */ 0x0ff0, 0x00, 0xa0, 0x0c, 0 },
  647. { /* Network General Sniffer */ 0x0ff0, 0x00, 0x00, 0x65,
  648. HAS_MISC_REG | HAS_IBM_MISC },
  649. { /* Panasonic VEL211 */ 0x0ff0, 0x00, 0x80, 0x45,
  650. HAS_MISC_REG | HAS_IBM_MISC },
  651. { /* PreMax PE-200 */ 0x07f0, 0x00, 0x20, 0xe0, 0 },
  652. { /* RPTI EP400 */ 0x0110, 0x00, 0x40, 0x95, 0 },
  653. { /* SCM Ethernet */ 0x0ff0, 0x00, 0x20, 0xcb, 0 },
  654. { /* Socket EA */ 0x4000, 0x00, 0xc0, 0x1b,
  655. DELAY_OUTPUT | HAS_MISC_REG | USE_BIG_BUF },
  656. { /* Socket LP-E CF+ */ 0x01c0, 0x00, 0xc0, 0x1b, 0 },
  657. { /* SuperSocket RE450T */ 0x0110, 0x00, 0xe0, 0x98, 0 },
  658. { /* Volktek NPL-402CT */ 0x0060, 0x00, 0x40, 0x05, 0 },
  659. { /* NEC PC-9801N-J12 */ 0x0ff0, 0x00, 0x00, 0x4c, 0 },
  660. { /* PCMCIA Technology OEM */ 0x01c8, 0x00, 0xa0, 0x0c, 0 },
  661. { /* Qemu */ 0x0, 0x52, 0x54, 0x00, 0 }
  662. };
  663. #define NR_INFO (sizeof(hw_info)/sizeof(hw_info_t))
  664. u8 dev_addr[6];
  665. #define PCNET_CMD 0x00
  666. #define PCNET_DATAPORT 0x10 /* NatSemi-defined port window offset. */
  667. #define PCNET_RESET 0x1f /* Issue a read to reset, a write to clear. */
  668. #define PCNET_MISC 0x18 /* For IBM CCAE and Socket EA cards */
  669. static void pcnet_reset_8390(void)
  670. {
  671. int i, r;
  672. PRINTK("nic base is %lx\n", nic_base);
  673. n2k_outb(E8390_NODMA + E8390_PAGE0+E8390_STOP, E8390_CMD);
  674. PRINTK("cmd (at %lx) is %x\n", nic_base + E8390_CMD, n2k_inb(E8390_CMD));
  675. n2k_outb(E8390_NODMA+E8390_PAGE1+E8390_STOP, E8390_CMD);
  676. PRINTK("cmd (at %lx) is %x\n", nic_base + E8390_CMD, n2k_inb(E8390_CMD));
  677. n2k_outb(E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD);
  678. PRINTK("cmd (at %lx) is %x\n", nic_base + E8390_CMD, n2k_inb(E8390_CMD));
  679. n2k_outb(E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD);
  680. n2k_outb(n2k_inb(PCNET_RESET), PCNET_RESET);
  681. for (i = 0; i < 100; i++) {
  682. if ((r = (n2k_inb(EN0_ISR) & ENISR_RESET)) != 0)
  683. break;
  684. PRINTK("got %x in reset\n", r);
  685. udelay(100);
  686. }
  687. n2k_outb(ENISR_RESET, EN0_ISR); /* Ack intr. */
  688. if (i == 100)
  689. printf("pcnet_reset_8390() did not complete.\n");
  690. } /* pcnet_reset_8390 */
  691. int get_prom(u8* mac_addr) __attribute__ ((weak, alias ("__get_prom")));
  692. int __get_prom(u8* mac_addr)
  693. {
  694. u8 prom[32];
  695. int i, j;
  696. struct {
  697. u_char value, offset;
  698. } program_seq[] = {
  699. {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
  700. {0x48, EN0_DCFG}, /* Set byte-wide (0x48) access. */
  701. {0x00, EN0_RCNTLO}, /* Clear the count regs. */
  702. {0x00, EN0_RCNTHI},
  703. {0x00, EN0_IMR}, /* Mask completion irq. */
  704. {0xFF, EN0_ISR},
  705. {E8390_RXOFF, EN0_RXCR}, /* 0x20 Set to monitor */
  706. {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */
  707. {32, EN0_RCNTLO},
  708. {0x00, EN0_RCNTHI},
  709. {0x00, EN0_RSARLO}, /* DMA starting at 0x0000. */
  710. {0x00, EN0_RSARHI},
  711. {E8390_RREAD+E8390_START, E8390_CMD},
  712. };
  713. PRINTK ("trying to get MAC via prom reading\n");
  714. pcnet_reset_8390 ();
  715. mdelay (10);
  716. for (i = 0; i < sizeof (program_seq) / sizeof (program_seq[0]); i++)
  717. n2k_outb (program_seq[i].value, program_seq[i].offset);
  718. PRINTK ("PROM:");
  719. for (i = 0; i < 32; i++) {
  720. prom[i] = n2k_inb (PCNET_DATAPORT);
  721. PRINTK (" %02x", prom[i]);
  722. }
  723. PRINTK ("\n");
  724. for (i = 0; i < NR_INFO; i++) {
  725. if ((prom[0] == hw_info[i].a0) &&
  726. (prom[2] == hw_info[i].a1) &&
  727. (prom[4] == hw_info[i].a2)) {
  728. PRINTK ("matched board %d\n", i);
  729. break;
  730. }
  731. }
  732. if ((i < NR_INFO) || ((prom[28] == 0x57) && (prom[30] == 0x57))) {
  733. PRINTK ("on exit i is %d/%ld\n", i, NR_INFO);
  734. PRINTK ("MAC address is ");
  735. for (j = 0; j < 6; j++) {
  736. mac_addr[j] = prom[j << 1];
  737. PRINTK ("%02x:", mac_addr[i]);
  738. }
  739. PRINTK ("\n");
  740. return (i < NR_INFO) ? i : 0;
  741. }
  742. return 0;
  743. }
  744. u32 nic_base;
  745. /* U-boot specific routines */
  746. static u8 *pbuf = NULL;
  747. static int pkey = -1;
  748. static int initialized = 0;
  749. void uboot_push_packet_len(int len) {
  750. PRINTK("pushed len = %d\n", len);
  751. if (len >= 2000) {
  752. printf("NE2000: packet too big\n");
  753. return;
  754. }
  755. dp83902a_recv(&pbuf[0], len);
  756. /*Just pass it to the upper layer*/
  757. NetReceive(&pbuf[0], len);
  758. }
  759. void uboot_push_tx_done(int key, int val) {
  760. PRINTK("pushed key = %d\n", key);
  761. pkey = key;
  762. }
  763. int eth_init(bd_t *bd) {
  764. int r;
  765. char ethaddr[20];
  766. PRINTK("### eth_init\n");
  767. if (!pbuf) {
  768. pbuf = malloc(2000);
  769. if (!pbuf) {
  770. printf("Cannot allocate rx buffer\n");
  771. return -1;
  772. }
  773. }
  774. #ifdef CONFIG_DRIVER_NE2000_CCR
  775. {
  776. vu_char *p = (vu_char *) CONFIG_DRIVER_NE2000_CCR;
  777. PRINTK("CCR before is %x\n", *p);
  778. *p = CONFIG_DRIVER_NE2000_VAL;
  779. PRINTK("CCR after is %x\n", *p);
  780. }
  781. #endif
  782. nic_base = CONFIG_DRIVER_NE2000_BASE;
  783. nic.base = (u8 *) CONFIG_DRIVER_NE2000_BASE;
  784. r = get_prom(dev_addr);
  785. if (!r)
  786. return -1;
  787. sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  788. dev_addr[0], dev_addr[1],
  789. dev_addr[2], dev_addr[3],
  790. dev_addr[4], dev_addr[5]) ;
  791. PRINTK("Set environment from HW MAC addr = \"%s\"\n", ethaddr);
  792. setenv ("ethaddr", ethaddr);
  793. nic.data = nic.base + DP_DATA;
  794. nic.tx_buf1 = START_PG;
  795. nic.tx_buf2 = START_PG2;
  796. nic.rx_buf_start = RX_START;
  797. nic.rx_buf_end = RX_END;
  798. if (dp83902a_init() == false)
  799. return -1;
  800. dp83902a_start(dev_addr);
  801. initialized = 1;
  802. return 0;
  803. }
  804. void eth_halt() {
  805. PRINTK("### eth_halt\n");
  806. if(initialized)
  807. dp83902a_stop();
  808. initialized = 0;
  809. }
  810. int eth_rx() {
  811. dp83902a_poll();
  812. return 1;
  813. }
  814. int eth_send(volatile void *packet, int length) {
  815. int tmo;
  816. PRINTK("### eth_send\n");
  817. pkey = -1;
  818. dp83902a_send((u8 *) packet, length, 666);
  819. tmo = get_timer (0) + TOUT * CFG_HZ;
  820. while(1) {
  821. dp83902a_poll();
  822. if (pkey != -1) {
  823. PRINTK("Packet sucesfully sent\n");
  824. return 0;
  825. }
  826. if (get_timer (0) >= tmo) {
  827. printf("transmission error (timoeut)\n");
  828. return 0;
  829. }
  830. }
  831. return 0;
  832. }