405gp_enet.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*-----------------------------------------------------------------------------+
  2. *
  3. * This source code has been made available to you by IBM on an AS-IS
  4. * basis. Anyone receiving this source is licensed under IBM
  5. * copyrights to use it in any way he or she deems fit, including
  6. * copying it, modifying it, compiling it, and redistributing it either
  7. * with or without modifications. No license under IBM patents or
  8. * patent applications is to be implied by the copyright license.
  9. *
  10. * Any user of this software should understand that IBM cannot provide
  11. * technical support for this software and will not be responsible for
  12. * any consequences resulting from the use of this software.
  13. *
  14. * Any person who transfers this source code or any derivative work
  15. * must include the IBM copyright notice, this paragraph, and the
  16. * preceding two paragraphs in the transferred software.
  17. *
  18. * COPYRIGHT I B M CORPORATION 1995
  19. * LICENSED MATERIAL - PROGRAM PROPERTY OF I B M
  20. *-----------------------------------------------------------------------------*/
  21. /*-----------------------------------------------------------------------------+
  22. *
  23. * File Name: enetemac.c
  24. *
  25. * Function: Device driver for the ethernet EMAC3 macro on the 405GP.
  26. *
  27. * Author: Mark Wisner
  28. *
  29. * Change Activity-
  30. *
  31. * Date Description of Change BY
  32. * --------- --------------------- ---
  33. * 05-May-99 Created MKW
  34. * 27-Jun-99 Clean up JWB
  35. * 16-Jul-99 Added MAL error recovery and better IP packet handling MKW
  36. * 29-Jul-99 Added Full duplex support MKW
  37. * 06-Aug-99 Changed names for Mal CR reg MKW
  38. * 23-Aug-99 Turned off SYE when running at 10Mbs MKW
  39. * 24-Aug-99 Marked descriptor empty after call_xlc MKW
  40. * 07-Sep-99 Set MAL RX buffer size reg to ENET_MAX_MTU_ALIGNED / 16 MCG
  41. * to avoid chaining maximum sized packets. Push starting
  42. * RX descriptor address up to the next cache line boundary.
  43. * 16-Jan-00 Added support for booting with IP of 0x0 MKW
  44. * 15-Mar-00 Updated enetInit() to enable broadcast addresses in the
  45. * EMAC_RXM register. JWB
  46. * 12-Mar-01 anne-sophie.harnois@nextream.fr
  47. * - Variables are compatible with those already defined in
  48. * include/net.h
  49. * - Receive buffer descriptor ring is used to send buffers
  50. * to the user
  51. * - Info print about send/received/handled packet number if
  52. * INFO_405_ENET is set
  53. * 17-Apr-01 stefan.roese@esd-electronics.com
  54. * - MAL reset in "eth_halt" included
  55. * - Enet speed and duplex output now in one line
  56. * 08-May-01 stefan.roese@esd-electronics.com
  57. * - MAL error handling added (eth_init called again)
  58. * 13-Nov-01 stefan.roese@esd-electronics.com
  59. * - Set IST bit in EMAC_M1 reg upon 100MBit or full duplex
  60. * 04-Jan-02 stefan.roese@esd-electronics.com
  61. * - Wait for PHY auto negotiation to complete added
  62. * 06-Feb-02 stefan.roese@esd-electronics.com
  63. * - Bug fixed in waiting for auto negotiation to complete
  64. * 26-Feb-02 stefan.roese@esd-electronics.com
  65. * - rx and tx buffer descriptors now allocated (no fixed address
  66. * used anymore)
  67. * 17-Jun-02 stefan.roese@esd-electronics.com
  68. * - MAL error debug printf 'M' removed (rx de interrupt may
  69. * occur upon many incoming packets with only 4 rx buffers).
  70. *-----------------------------------------------------------------------------*/
  71. #include <common.h>
  72. #include <asm/processor.h>
  73. #include <ppc4xx.h>
  74. #include <commproc.h>
  75. #include <405gp_enet.h>
  76. #include <405_mal.h>
  77. #include <miiphy.h>
  78. #include <net.h>
  79. #include <malloc.h>
  80. #include "vecnum.h"
  81. #if defined(CONFIG_405GP) || defined(CONFIG_440) || defined(CONFIG_405EP)
  82. #define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
  83. #define PHY_AUTONEGOTIATE_TIMEOUT 2000 /* 2000 ms autonegotiate timeout */
  84. #define NUM_TX_BUFF 1
  85. /* AS.HARNOIS
  86. * Use PKTBUFSRX (include/net.h) instead of setting NUM_RX_BUFF again
  87. * These both variables are used to define the same thing!
  88. * #define NUM_RX_BUFF 4
  89. */
  90. #define NUM_RX_BUFF PKTBUFSRX
  91. /* Ethernet Transmit and Receive Buffers */
  92. /* AS.HARNOIS
  93. * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
  94. * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
  95. */
  96. #define ENET_MAX_MTU PKTSIZE
  97. #define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
  98. static char *txbuf_ptr;
  99. /* define the number of channels implemented */
  100. #define EMAC_RXCHL 1
  101. #define EMAC_TXCHL 1
  102. /*-----------------------------------------------------------------------------+
  103. * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
  104. * Interrupt Controller).
  105. *-----------------------------------------------------------------------------*/
  106. #define MAL_UIC_ERR ( UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
  107. #define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
  108. #define EMAC_UIC_DEF UIC_ENET
  109. /*-----------------------------------------------------------------------------+
  110. * Global variables. TX and RX descriptors and buffers.
  111. *-----------------------------------------------------------------------------*/
  112. static volatile mal_desc_t *tx;
  113. static volatile mal_desc_t *rx;
  114. static mal_desc_t *alloc_tx_buf = NULL;
  115. static mal_desc_t *alloc_rx_buf = NULL;
  116. /* IER globals */
  117. static unsigned long emac_ier;
  118. static unsigned long mal_ier;
  119. /* Statistic Areas */
  120. #define MAX_ERR_LOG 10
  121. struct emac_stats {
  122. int data_len_err;
  123. int rx_frames;
  124. int rx;
  125. int rx_prot_err;
  126. };
  127. static struct stats { /* Statistic Block */
  128. struct emac_stats emac;
  129. int int_err;
  130. short tx_err_log[MAX_ERR_LOG];
  131. short rx_err_log[MAX_ERR_LOG];
  132. } stats;
  133. static int first_init = 0;
  134. static int tx_err_index = 0; /* Transmit Error Index for tx_err_log */
  135. static int rx_err_index = 0; /* Receive Error Index for rx_err_log */
  136. static int rx_slot = 0; /* MAL Receive Slot */
  137. static int rx_i_index = 0; /* Receive Interrupt Queue Index */
  138. static int rx_u_index = 0; /* Receive User Queue Index */
  139. static int rx_ready[NUM_RX_BUFF]; /* Receive Ready Queue */
  140. static int tx_slot = 0; /* MAL Transmit Slot */
  141. static int tx_i_index = 0; /* Transmit Interrupt Queue Index */
  142. static int tx_u_index = 0; /* Transmit User Queue Index */
  143. static int tx_run[NUM_TX_BUFF]; /* Transmit Running Queue */
  144. #undef INFO_405_ENET 1
  145. #ifdef INFO_405_ENET
  146. static int packetSent = 0;
  147. static int packetReceived = 0;
  148. static int packetHandled = 0;
  149. #endif
  150. static char emac_hwd_addr[ENET_ADDR_LENGTH];
  151. static bd_t *bis_save = NULL; /* for eth_init upon mal error */
  152. static int is_receiving = 0; /* sync with eth interrupt */
  153. static int print_speed = 1; /* print speed message upon start */
  154. static void enet_rcv (unsigned long malisr);
  155. /*-----------------------------------------------------------------------------+
  156. * Prototypes and externals.
  157. *-----------------------------------------------------------------------------*/
  158. void mal_err (unsigned long isr, unsigned long uic, unsigned long mal_def,
  159. unsigned long mal_errr);
  160. void emac_err (unsigned long isr);
  161. void eth_halt (void)
  162. {
  163. mtdcr (malier, 0x00000000); /* disable mal interrupts */
  164. out32 (EMAC_IER, 0x00000000); /* disable emac interrupts */
  165. /* 1st reset MAL */
  166. mtdcr (malmcr, MAL_CR_MMSR);
  167. /* wait for reset */
  168. while (mfdcr (malmcr) & MAL_CR_MMSR) {
  169. };
  170. /* EMAC RESET */
  171. out32 (EMAC_M0, EMAC_M0_SRST);
  172. print_speed = 1; /* print speed message again next time */
  173. }
  174. int eth_init (bd_t * bis)
  175. {
  176. int i;
  177. unsigned long reg;
  178. unsigned long msr;
  179. unsigned long speed;
  180. unsigned long duplex;
  181. unsigned mode_reg;
  182. unsigned short reg_short;
  183. msr = mfmsr ();
  184. mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
  185. #ifdef INFO_405_ENET
  186. /* AS.HARNOIS
  187. * We should have :
  188. * packetHandled <= packetReceived <= packetHandled+PKTBUFSRX
  189. * In the most cases packetHandled = packetReceived, but it
  190. * is possible that new packets (without relationship with
  191. * current transfer) have got the time to arrived before
  192. * netloop calls eth_halt
  193. */
  194. printf ("About preceeding transfer:\n"
  195. "- Sent packet number %d\n"
  196. "- Received packet number %d\n"
  197. "- Handled packet number %d\n",
  198. packetSent, packetReceived, packetHandled);
  199. packetSent = 0;
  200. packetReceived = 0;
  201. packetHandled = 0;
  202. #endif
  203. /* MAL RESET */
  204. mtdcr (malmcr, MAL_CR_MMSR);
  205. /* wait for reset */
  206. while (mfdcr (malmcr) & MAL_CR_MMSR) {
  207. };
  208. tx_err_index = 0; /* Transmit Error Index for tx_err_log */
  209. rx_err_index = 0; /* Receive Error Index for rx_err_log */
  210. rx_slot = 0; /* MAL Receive Slot */
  211. rx_i_index = 0; /* Receive Interrupt Queue Index */
  212. rx_u_index = 0; /* Receive User Queue Index */
  213. tx_slot = 0; /* MAL Transmit Slot */
  214. tx_i_index = 0; /* Transmit Interrupt Queue Index */
  215. tx_u_index = 0; /* Transmit User Queue Index */
  216. #if defined(CONFIG_440)
  217. /* set RMII mode */
  218. out32 (ZMII_FER, ZMII_RMII | ZMII_MDI0);
  219. #endif /* CONFIG_440 */
  220. /* EMAC RESET */
  221. out32 (EMAC_M0, EMAC_M0_SRST);
  222. /* wait for PHY to complete auto negotiation */
  223. reg_short = 0;
  224. #ifndef CONFIG_CS8952_PHY
  225. miiphy_read (CONFIG_PHY_ADDR, PHY_BMSR, &reg_short);
  226. /*
  227. * Wait if PHY is able of autonegotiation and autonegotiation is not complete
  228. */
  229. if ((reg_short & PHY_BMSR_AUTN_ABLE)
  230. && !(reg_short & PHY_BMSR_AUTN_COMP)) {
  231. puts ("Waiting for PHY auto negotiation to complete");
  232. i = 0;
  233. while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
  234. if ((i++ % 100) == 0)
  235. putc ('.');
  236. udelay (10000); /* 10 ms */
  237. miiphy_read (CONFIG_PHY_ADDR, PHY_BMSR, &reg_short);
  238. /*
  239. * Timeout reached ?
  240. */
  241. if (i * 10 > PHY_AUTONEGOTIATE_TIMEOUT) {
  242. puts (" TIMEOUT !\n");
  243. break;
  244. }
  245. }
  246. puts (" done\n");
  247. udelay (500000); /* another 500 ms (results in faster booting) */
  248. }
  249. #endif
  250. speed = miiphy_speed (CONFIG_PHY_ADDR);
  251. duplex = miiphy_duplex (CONFIG_PHY_ADDR);
  252. if (print_speed) {
  253. print_speed = 0;
  254. printf ("ENET Speed is %d Mbps - %s duplex connection\n",
  255. (int) speed, (duplex == HALF) ? "HALF" : "FULL");
  256. }
  257. /* set the Mal configuration reg */
  258. #if defined(CONFIG_440)
  259. /* Errata 1.12: MAL_1 -- Disable MAL bursting */
  260. if( get_pvr() == PVR_440GP_RB )
  261. mtdcr (malmcr, MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
  262. else
  263. #else
  264. mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
  265. #endif
  266. /* Free "old" buffers */
  267. if (alloc_tx_buf) free(alloc_tx_buf);
  268. if (alloc_rx_buf) free(alloc_rx_buf);
  269. /*
  270. * Malloc MAL buffer desciptors, make sure they are
  271. * aligned on cache line boundary size
  272. * (401/403/IOP480 = 16, 405 = 32)
  273. * and doesn't cross cache block boundaries.
  274. */
  275. alloc_tx_buf = (mal_desc_t *)malloc((sizeof(mal_desc_t) * NUM_TX_BUFF) +
  276. ((2 * CFG_CACHELINE_SIZE) - 2));
  277. if (((int)alloc_tx_buf & CACHELINE_MASK) != 0) {
  278. tx = (mal_desc_t *)((int)alloc_tx_buf + CFG_CACHELINE_SIZE -
  279. ((int)alloc_tx_buf & CACHELINE_MASK));
  280. } else {
  281. tx = alloc_tx_buf;
  282. }
  283. alloc_rx_buf = (mal_desc_t *)malloc((sizeof(mal_desc_t) * NUM_RX_BUFF) +
  284. ((2 * CFG_CACHELINE_SIZE) - 2));
  285. if (((int)alloc_rx_buf & CACHELINE_MASK) != 0) {
  286. rx = (mal_desc_t *)((int)alloc_rx_buf + CFG_CACHELINE_SIZE -
  287. ((int)alloc_rx_buf & CACHELINE_MASK));
  288. } else {
  289. rx = alloc_rx_buf;
  290. }
  291. for (i = 0; i < NUM_TX_BUFF; i++) {
  292. tx[i].ctrl = 0;
  293. tx[i].data_len = 0;
  294. if (first_init == 0)
  295. txbuf_ptr = (char *) malloc (ENET_MAX_MTU_ALIGNED);
  296. tx[i].data_ptr = txbuf_ptr;
  297. if ((NUM_TX_BUFF - 1) == i)
  298. tx[i].ctrl |= MAL_TX_CTRL_WRAP;
  299. tx_run[i] = -1;
  300. #if 0
  301. printf ("TX_BUFF %d @ 0x%08lx\n", i, (ulong) tx[i].data_ptr);
  302. #endif
  303. }
  304. for (i = 0; i < NUM_RX_BUFF; i++) {
  305. rx[i].ctrl = 0;
  306. rx[i].data_len = 0;
  307. /* rx[i].data_ptr = (char *) &rx_buff[i]; */
  308. rx[i].data_ptr = (char *) NetRxPackets[i];
  309. if ((NUM_RX_BUFF - 1) == i)
  310. rx[i].ctrl |= MAL_RX_CTRL_WRAP;
  311. rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
  312. rx_ready[i] = -1;
  313. #if 0
  314. printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) rx[i].data_ptr);
  315. #endif
  316. }
  317. memcpy (emac_hwd_addr, bis->bi_enetaddr, ENET_ADDR_LENGTH);
  318. reg = 0x00000000;
  319. reg |= emac_hwd_addr[0]; /* set high address */
  320. reg = reg << 8;
  321. reg |= emac_hwd_addr[1];
  322. out32 (EMAC_IAH, reg);
  323. reg = 0x00000000;
  324. reg |= emac_hwd_addr[2]; /* set low address */
  325. reg = reg << 8;
  326. reg |= emac_hwd_addr[3];
  327. reg = reg << 8;
  328. reg |= emac_hwd_addr[4];
  329. reg = reg << 8;
  330. reg |= emac_hwd_addr[5];
  331. out32 (EMAC_IAL, reg);
  332. /* setup MAL tx & rx channel pointers */
  333. mtdcr (maltxctp0r, tx);
  334. mtdcr (malrxctp0r, rx);
  335. /* Reset transmit and receive channels */
  336. mtdcr (malrxcarr, 0x80000000); /* 2 channels */
  337. mtdcr (maltxcarr, 0x80000000); /* 2 channels */
  338. /* Enable MAL transmit and receive channels */
  339. mtdcr (maltxcasr, 0x80000000); /* 1 channel */
  340. mtdcr (malrxcasr, 0x80000000); /* 1 channel */
  341. /* set RX buffer size */
  342. mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
  343. /* set transmit enable & receive enable */
  344. out32 (EMAC_M0, EMAC_M0_TXE | EMAC_M0_RXE);
  345. /* set receive fifo to 4k and tx fifo to 2k */
  346. mode_reg = EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
  347. /* set speed */
  348. if (speed == _100BASET)
  349. mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
  350. else
  351. mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
  352. if (duplex == FULL)
  353. mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
  354. out32 (EMAC_M1, mode_reg);
  355. /* Enable broadcast and indvidual address */
  356. out32 (EMAC_RXM, EMAC_RMR_BAE | EMAC_RMR_IAE
  357. /*| EMAC_RMR_ARRP| EMAC_RMR_SFCS | EMAC_RMR_SP */ );
  358. /* we probably need to set the tx mode1 reg? maybe at tx time */
  359. /* set transmit request threshold register */
  360. out32 (EMAC_TRTR, 0x18000000); /* 256 byte threshold */
  361. /* set receive low/high water mark register */
  362. #if defined(CONFIG_440)
  363. /* 440GP has a 64 byte burst length */
  364. out32 (EMAC_RX_HI_LO_WMARK, 0x80009000);
  365. out32 (EMAC_TXM1, 0xf8640000);
  366. #else /* CONFIG_440 */
  367. /* 405s have a 16 byte burst length */
  368. out32 (EMAC_RX_HI_LO_WMARK, 0x0f002000);
  369. #endif /* CONFIG_440 */
  370. /* Frame gap set */
  371. out32 (EMAC_I_FRAME_GAP_REG, 0x00000008);
  372. if (first_init == 0) {
  373. /*
  374. * Connect interrupt service routines
  375. */
  376. irq_install_handler (VECNUM_EWU0, (interrupt_handler_t *) enetInt, NULL);
  377. irq_install_handler (VECNUM_MS, (interrupt_handler_t *) enetInt, NULL);
  378. irq_install_handler (VECNUM_MTE, (interrupt_handler_t *) enetInt, NULL);
  379. irq_install_handler (VECNUM_MRE, (interrupt_handler_t *) enetInt, NULL);
  380. irq_install_handler (VECNUM_TXDE, (interrupt_handler_t *) enetInt, NULL);
  381. irq_install_handler (VECNUM_RXDE, (interrupt_handler_t *) enetInt, NULL);
  382. irq_install_handler (VECNUM_ETH0, (interrupt_handler_t *) enetInt, NULL);
  383. }
  384. /* set up interrupt handler */
  385. /* setup interrupt controler to take interrupts from the MAL &
  386. EMAC */
  387. mtdcr (uicsr, 0xffffffff); /* clear pending interrupts */
  388. mtdcr (uicer, mfdcr (uicer) | MAL_UIC_DEF | EMAC_UIC_DEF);
  389. /* set the MAL IER ??? names may change with new spec ??? */
  390. mal_ier = MAL_IER_DE | MAL_IER_NE | MAL_IER_TE | MAL_IER_OPBE |
  391. MAL_IER_PLBE;
  392. mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
  393. mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
  394. mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
  395. mtdcr (malier, mal_ier);
  396. /* Set EMAC IER */
  397. emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS |
  398. EMAC_ISR_PTLE | EMAC_ISR_ORE | EMAC_ISR_IRE;
  399. if (speed == _100BASET)
  400. emac_ier = emac_ier | EMAC_ISR_SYE;
  401. out32 (EMAC_ISR, 0xffffffff); /* clear pending interrupts */
  402. out32 (EMAC_IER, emac_ier);
  403. mtmsr (msr); /* enable interrupts again */
  404. bis_save = bis;
  405. first_init = 1;
  406. return (0);
  407. }
  408. int eth_send (volatile void *ptr, int len)
  409. {
  410. struct enet_frame *ef_ptr;
  411. ulong time_start, time_now;
  412. unsigned long temp_txm0;
  413. ef_ptr = (struct enet_frame *) ptr;
  414. /*-----------------------------------------------------------------------+
  415. * Copy in our address into the frame.
  416. *-----------------------------------------------------------------------*/
  417. (void) memcpy (ef_ptr->source_addr, emac_hwd_addr, ENET_ADDR_LENGTH);
  418. /*-----------------------------------------------------------------------+
  419. * If frame is too long or too short, modify length.
  420. *-----------------------------------------------------------------------*/
  421. if (len > ENET_MAX_MTU)
  422. len = ENET_MAX_MTU;
  423. /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
  424. memcpy ((void *) txbuf_ptr, (const void *) ptr, len);
  425. /*-----------------------------------------------------------------------+
  426. * set TX Buffer busy, and send it
  427. *-----------------------------------------------------------------------*/
  428. tx[tx_slot].ctrl = (MAL_TX_CTRL_LAST |
  429. EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
  430. ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
  431. if ((NUM_TX_BUFF - 1) == tx_slot)
  432. tx[tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
  433. tx[tx_slot].data_len = (short) len;
  434. tx[tx_slot].ctrl |= MAL_TX_CTRL_READY;
  435. __asm__ volatile ("eieio");
  436. out32 (EMAC_TXM0, in32 (EMAC_TXM0) | EMAC_TXM0_GNP0);
  437. #ifdef INFO_405_ENET
  438. packetSent++;
  439. #endif
  440. /*-----------------------------------------------------------------------+
  441. * poll unitl the packet is sent and then make sure it is OK
  442. *-----------------------------------------------------------------------*/
  443. time_start = get_timer (0);
  444. while (1) {
  445. temp_txm0 = in32 (EMAC_TXM0);
  446. /* loop until either TINT turns on or 3 seconds elapse */
  447. if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
  448. /* transmit is done, so now check for errors
  449. * If there is an error, an interrupt should
  450. * happen when we return
  451. */
  452. time_now = get_timer (0);
  453. if ((time_now - time_start) > 3000) {
  454. return (-1);
  455. }
  456. } else {
  457. return (0);
  458. }
  459. }
  460. }
  461. #if defined(CONFIG_440)
  462. /*-----------------------------------------------------------------------------+
  463. | EnetInt.
  464. | EnetInt is the interrupt handler. It will determine the
  465. | cause of the interrupt and call the apporpriate servive
  466. | routine.
  467. +-----------------------------------------------------------------------------*/
  468. int enetInt ()
  469. {
  470. int serviced;
  471. int rc = -1; /* default to not us */
  472. unsigned long mal_isr;
  473. unsigned long emac_isr = 0;
  474. unsigned long mal_rx_eob;
  475. unsigned long my_uic0msr, my_uic1msr;
  476. /* enter loop that stays in interrupt code until nothing to service */
  477. do {
  478. serviced = 0;
  479. my_uic0msr = mfdcr (uic0msr);
  480. my_uic1msr = mfdcr (uic1msr);
  481. if (!(my_uic0msr & UIC_MRE)
  482. && !(my_uic1msr & (UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE))) {
  483. /* not for us */
  484. return (rc);
  485. }
  486. /* get and clear controller status interrupts */
  487. /* look at Mal and EMAC interrupts */
  488. if ((my_uic0msr & UIC_MRE)
  489. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  490. /* we have a MAL interrupt */
  491. mal_isr = mfdcr (malesr);
  492. /* look for mal error */
  493. if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
  494. mal_err (mal_isr, my_uic0msr, MAL_UIC_DEF, MAL_UIC_ERR);
  495. serviced = 1;
  496. rc = 0;
  497. }
  498. }
  499. if (UIC_ETH0 & my_uic1msr) { /* look for EMAC errors */
  500. emac_isr = in32 (EMAC_ISR);
  501. if ((emac_ier & emac_isr) != 0) {
  502. emac_err (emac_isr);
  503. serviced = 1;
  504. rc = 0;
  505. }
  506. }
  507. if ((emac_ier & emac_isr)
  508. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  509. mtdcr (uic0sr, UIC_MRE); /* Clear */
  510. mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  511. return (rc); /* we had errors so get out */
  512. }
  513. /* handle MAL RX EOB interupt from a receive */
  514. /* check for EOB on valid channels */
  515. if (my_uic0msr & UIC_MRE) {
  516. mal_rx_eob = mfdcr (malrxeobisr);
  517. if ((mal_rx_eob & 0x80000000) != 0) { /* call emac routine for channel 0 */
  518. /* clear EOB
  519. mtdcr(malrxeobisr, mal_rx_eob); */
  520. enet_rcv (emac_isr);
  521. /* indicate that we serviced an interrupt */
  522. serviced = 1;
  523. rc = 0;
  524. }
  525. }
  526. mtdcr (uic0sr, UIC_MRE); /* Clear */
  527. mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  528. } while (serviced);
  529. return (rc);
  530. }
  531. #else /* CONFIG_440 */
  532. /*-----------------------------------------------------------------------------+
  533. * EnetInt.
  534. * EnetInt is the interrupt handler. It will determine the
  535. * cause of the interrupt and call the apporpriate servive
  536. * routine.
  537. *-----------------------------------------------------------------------------*/
  538. int enetInt ()
  539. {
  540. int serviced;
  541. int rc = -1; /* default to not us */
  542. unsigned long mal_isr;
  543. unsigned long emac_isr = 0;
  544. unsigned long mal_rx_eob;
  545. unsigned long my_uicmsr;
  546. /* enter loop that stays in interrupt code until nothing to service */
  547. do {
  548. serviced = 0;
  549. my_uicmsr = mfdcr (uicmsr);
  550. if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
  551. return (rc);
  552. }
  553. /* get and clear controller status interrupts */
  554. /* look at Mal and EMAC interrupts */
  555. if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
  556. mal_isr = mfdcr (malesr);
  557. /* look for mal error */
  558. if ((my_uicmsr & MAL_UIC_ERR) != 0) {
  559. mal_err (mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
  560. serviced = 1;
  561. rc = 0;
  562. }
  563. }
  564. if ((EMAC_UIC_DEF & my_uicmsr) != 0) { /* look for EMAC errors */
  565. emac_isr = in32 (EMAC_ISR);
  566. if ((emac_ier & emac_isr) != 0) {
  567. emac_err (emac_isr);
  568. serviced = 1;
  569. rc = 0;
  570. }
  571. }
  572. if (((emac_ier & emac_isr) != 0) | ((MAL_UIC_ERR & my_uicmsr) != 0)) {
  573. mtdcr (uicsr, MAL_UIC_DEF | EMAC_UIC_DEF); /* Clear */
  574. return (rc); /* we had errors so get out */
  575. }
  576. /* handle MAL RX EOB interupt from a receive */
  577. /* check for EOB on valid channels */
  578. if ((my_uicmsr & UIC_MAL_RXEOB) != 0) {
  579. mal_rx_eob = mfdcr (malrxeobisr);
  580. if ((mal_rx_eob & 0x80000000) != 0) { /* call emac routine for channel 0 */
  581. /* clear EOB
  582. mtdcr(malrxeobisr, mal_rx_eob); */
  583. enet_rcv (emac_isr);
  584. /* indicate that we serviced an interrupt */
  585. serviced = 1;
  586. rc = 0;
  587. }
  588. }
  589. mtdcr (uicsr, MAL_UIC_DEF | EMAC_UIC_DEF); /* Clear */
  590. }
  591. while (serviced);
  592. return (rc);
  593. }
  594. #endif /* CONFIG_440 */
  595. /*-----------------------------------------------------------------------------+
  596. * MAL Error Routine
  597. *-----------------------------------------------------------------------------*/
  598. void mal_err (unsigned long isr, unsigned long uic, unsigned long maldef,
  599. unsigned long mal_errr)
  600. {
  601. mtdcr (malesr, isr); /* clear interrupt */
  602. /* clear DE interrupt */
  603. mtdcr (maltxdeir, 0xC0000000);
  604. mtdcr (malrxdeir, 0x80000000);
  605. #ifdef INFO_405_ENET
  606. printf ("\nMAL error occured.... ISR = %lx UIC = = %lx MAL_DEF = %lx MAL_ERR= %lx \n",
  607. isr, uic, maldef, mal_errr);
  608. #else
  609. #if 0
  610. /*
  611. * MAL error is RX DE error (out of rx buffers)! This is OK here, upon
  612. * many incoming packets with only 4 rx buffers.
  613. */
  614. printf ("M"); /* just to see something upon mal error */
  615. #endif
  616. #endif
  617. eth_init (bis_save); /* start again... */
  618. }
  619. /*-----------------------------------------------------------------------------+
  620. * EMAC Error Routine
  621. *-----------------------------------------------------------------------------*/
  622. void emac_err (unsigned long isr)
  623. {
  624. printf ("EMAC error occured.... ISR = %lx\n", isr);
  625. out32 (EMAC_ISR, isr);
  626. }
  627. /*-----------------------------------------------------------------------------+
  628. * enet_rcv() handles the ethernet receive data
  629. *-----------------------------------------------------------------------------*/
  630. static void enet_rcv (unsigned long malisr)
  631. {
  632. struct enet_frame *ef_ptr;
  633. unsigned long data_len;
  634. unsigned long rx_eob_isr;
  635. int handled = 0;
  636. int i;
  637. int loop_count = 0;
  638. rx_eob_isr = mfdcr (malrxeobisr);
  639. if ((0x80000000 >> (EMAC_RXCHL - 1)) & rx_eob_isr) {
  640. /* clear EOB */
  641. mtdcr (malrxeobisr, rx_eob_isr);
  642. /* EMAC RX done */
  643. while (1) { /* do all */
  644. i = rx_slot;
  645. if ((MAL_RX_CTRL_EMPTY & rx[i].ctrl)
  646. || (loop_count >= NUM_RX_BUFF))
  647. break;
  648. loop_count++;
  649. rx_slot++;
  650. if (NUM_RX_BUFF == rx_slot)
  651. rx_slot = 0;
  652. handled++;
  653. data_len = (unsigned long) rx[i].data_len; /* Get len */
  654. if (data_len) {
  655. if (data_len > ENET_MAX_MTU) /* Check len */
  656. data_len = 0;
  657. else {
  658. if (EMAC_RX_ERRORS & rx[i].ctrl) { /* Check Errors */
  659. data_len = 0;
  660. stats.rx_err_log[rx_err_index] = rx[i].ctrl;
  661. rx_err_index++;
  662. if (rx_err_index == MAX_ERR_LOG)
  663. rx_err_index = 0;
  664. } /* emac_erros */
  665. } /* data_len < max mtu */
  666. } /* if data_len */
  667. if (!data_len) { /* no data */
  668. rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
  669. stats.emac.data_len_err++; /* Error at Rx */
  670. }
  671. /* !data_len */
  672. /* AS.HARNOIS */
  673. /* Check if user has already eaten buffer */
  674. /* if not => ERROR */
  675. else if (rx_ready[rx_i_index] != -1) {
  676. if (is_receiving)
  677. printf ("ERROR : Receive buffers are full!\n");
  678. break;
  679. } else {
  680. stats.emac.rx_frames++;
  681. stats.emac.rx += data_len;
  682. ef_ptr = (struct enet_frame *) rx[i].data_ptr;
  683. #ifdef INFO_405_ENET
  684. packetReceived++;
  685. #endif
  686. /* AS.HARNOIS
  687. * use ring buffer
  688. */
  689. rx_ready[rx_i_index] = i;
  690. rx_i_index++;
  691. if (NUM_RX_BUFF == rx_i_index)
  692. rx_i_index = 0;
  693. /* printf("X"); /|* test-only *|/ */
  694. /* AS.HARNOIS
  695. * free receive buffer only when
  696. * buffer has been handled (eth_rx)
  697. rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
  698. */
  699. } /* if data_len */
  700. } /* while */
  701. } /* if EMACK_RXCHL */
  702. }
  703. int eth_rx (void)
  704. {
  705. int length;
  706. int user_index;
  707. unsigned long msr;
  708. is_receiving = 1; /* tell driver */
  709. for (;;) {
  710. /* AS.HARNOIS
  711. * use ring buffer and
  712. * get index from rx buffer desciptor queue
  713. */
  714. user_index = rx_ready[rx_u_index];
  715. if (user_index == -1) {
  716. length = -1;
  717. break; /* nothing received - leave for() loop */
  718. }
  719. msr = mfmsr ();
  720. mtmsr (msr & ~(MSR_EE));
  721. length = rx[user_index].data_len;
  722. /* Pass the packet up to the protocol layers. */
  723. /* NetReceive(NetRxPackets[rxIdx], length - 4); */
  724. /* NetReceive(NetRxPackets[i], length); */
  725. NetReceive (NetRxPackets[user_index], length - 4);
  726. /* Free Recv Buffer */
  727. rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
  728. /* Free rx buffer descriptor queue */
  729. rx_ready[rx_u_index] = -1;
  730. rx_u_index++;
  731. if (NUM_RX_BUFF == rx_u_index)
  732. rx_u_index = 0;
  733. #ifdef INFO_405_ENET
  734. packetHandled++;
  735. #endif
  736. mtmsr (msr); /* Enable IRQ's */
  737. }
  738. is_receiving = 0; /* tell driver */
  739. return length;
  740. }
  741. #endif /* CONFIG_405GP */