405gp_enet.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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 4000 /* 4000 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. /*-----------------------------------------------------------------------------+
  155. * Prototypes and externals.
  156. *-----------------------------------------------------------------------------*/
  157. static void enet_rcv (unsigned long malisr);
  158. static int enetInt(void);
  159. static void mal_err (unsigned long isr, unsigned long uic, unsigned long mal_def,
  160. unsigned long mal_errr);
  161. static void emac_err (unsigned long isr);
  162. static void ppc_4xx_eth_halt (struct eth_device *dev)
  163. {
  164. mtdcr (malier, 0x00000000); /* disable mal interrupts */
  165. out32 (EMAC_IER, 0x00000000); /* disable emac interrupts */
  166. /* 1st reset MAL */
  167. mtdcr (malmcr, MAL_CR_MMSR);
  168. /* wait for reset */
  169. while (mfdcr (malmcr) & MAL_CR_MMSR) {
  170. };
  171. /* EMAC RESET */
  172. out32 (EMAC_M0, EMAC_M0_SRST);
  173. print_speed = 1; /* print speed message again next time */
  174. }
  175. static int ppc_4xx_eth_init (struct eth_device *dev, bd_t * bis)
  176. {
  177. int i;
  178. unsigned long reg;
  179. unsigned long msr;
  180. unsigned long speed;
  181. unsigned long duplex;
  182. unsigned mode_reg;
  183. unsigned short reg_short;
  184. msr = mfmsr ();
  185. mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
  186. #ifdef INFO_405_ENET
  187. /* AS.HARNOIS
  188. * We should have :
  189. * packetHandled <= packetReceived <= packetHandled+PKTBUFSRX
  190. * In the most cases packetHandled = packetReceived, but it
  191. * is possible that new packets (without relationship with
  192. * current transfer) have got the time to arrived before
  193. * netloop calls eth_halt
  194. */
  195. printf ("About preceeding transfer:\n"
  196. "- Sent packet number %d\n"
  197. "- Received packet number %d\n"
  198. "- Handled packet number %d\n",
  199. packetSent, packetReceived, packetHandled);
  200. packetSent = 0;
  201. packetReceived = 0;
  202. packetHandled = 0;
  203. #endif
  204. /* MAL RESET */
  205. mtdcr (malmcr, MAL_CR_MMSR);
  206. /* wait for reset */
  207. while (mfdcr (malmcr) & MAL_CR_MMSR) {
  208. };
  209. tx_err_index = 0; /* Transmit Error Index for tx_err_log */
  210. rx_err_index = 0; /* Receive Error Index for rx_err_log */
  211. rx_slot = 0; /* MAL Receive Slot */
  212. rx_i_index = 0; /* Receive Interrupt Queue Index */
  213. rx_u_index = 0; /* Receive User Queue Index */
  214. tx_slot = 0; /* MAL Transmit Slot */
  215. tx_i_index = 0; /* Transmit Interrupt Queue Index */
  216. tx_u_index = 0; /* Transmit User Queue Index */
  217. #if defined(CONFIG_440)
  218. /* set RMII mode */
  219. out32 (ZMII_FER, ZMII_RMII | ZMII_MDI0);
  220. #endif /* CONFIG_440 */
  221. /* EMAC RESET */
  222. out32 (EMAC_M0, EMAC_M0_SRST);
  223. /* wait for PHY to complete auto negotiation */
  224. reg_short = 0;
  225. #ifndef CONFIG_CS8952_PHY
  226. miiphy_read (CONFIG_PHY_ADDR, PHY_BMSR, &reg_short);
  227. /*
  228. * Wait if PHY is able of autonegotiation and autonegotiation is not complete
  229. */
  230. if ((reg_short & PHY_BMSR_AUTN_ABLE)
  231. && !(reg_short & PHY_BMSR_AUTN_COMP)) {
  232. puts ("Waiting for PHY auto negotiation to complete");
  233. i = 0;
  234. while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
  235. /*
  236. * Timeout reached ?
  237. */
  238. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  239. puts (" TIMEOUT !\n");
  240. break;
  241. }
  242. if ((i++ % 1000) == 0)
  243. putc ('.');
  244. udelay (1000); /* 1 ms */
  245. miiphy_read (CONFIG_PHY_ADDR, PHY_BMSR, &reg_short);
  246. }
  247. puts (" done\n");
  248. udelay (500000); /* another 500 ms (results in faster booting) */
  249. }
  250. #endif
  251. speed = miiphy_speed (CONFIG_PHY_ADDR);
  252. duplex = miiphy_duplex (CONFIG_PHY_ADDR);
  253. if (print_speed) {
  254. print_speed = 0;
  255. printf ("ENET Speed is %d Mbps - %s duplex connection\n",
  256. (int) speed, (duplex == HALF) ? "HALF" : "FULL");
  257. }
  258. /* set the Mal configuration reg */
  259. #if defined(CONFIG_440)
  260. /* Errata 1.12: MAL_1 -- Disable MAL bursting */
  261. if( get_pvr() == PVR_440GP_RB )
  262. mtdcr (malmcr, MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
  263. else
  264. #else
  265. mtdcr (malmcr, MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
  266. #endif
  267. /* Free "old" buffers */
  268. if (alloc_tx_buf) free(alloc_tx_buf);
  269. if (alloc_rx_buf) free(alloc_rx_buf);
  270. /*
  271. * Malloc MAL buffer desciptors, make sure they are
  272. * aligned on cache line boundary size
  273. * (401/403/IOP480 = 16, 405 = 32)
  274. * and doesn't cross cache block boundaries.
  275. */
  276. alloc_tx_buf = (mal_desc_t *)malloc((sizeof(mal_desc_t) * NUM_TX_BUFF) +
  277. ((2 * CFG_CACHELINE_SIZE) - 2));
  278. if (((int)alloc_tx_buf & CACHELINE_MASK) != 0) {
  279. tx = (mal_desc_t *)((int)alloc_tx_buf + CFG_CACHELINE_SIZE -
  280. ((int)alloc_tx_buf & CACHELINE_MASK));
  281. } else {
  282. tx = alloc_tx_buf;
  283. }
  284. alloc_rx_buf = (mal_desc_t *)malloc((sizeof(mal_desc_t) * NUM_RX_BUFF) +
  285. ((2 * CFG_CACHELINE_SIZE) - 2));
  286. if (((int)alloc_rx_buf & CACHELINE_MASK) != 0) {
  287. rx = (mal_desc_t *)((int)alloc_rx_buf + CFG_CACHELINE_SIZE -
  288. ((int)alloc_rx_buf & CACHELINE_MASK));
  289. } else {
  290. rx = alloc_rx_buf;
  291. }
  292. for (i = 0; i < NUM_TX_BUFF; i++) {
  293. tx[i].ctrl = 0;
  294. tx[i].data_len = 0;
  295. if (first_init == 0)
  296. txbuf_ptr = (char *) malloc (ENET_MAX_MTU_ALIGNED);
  297. tx[i].data_ptr = txbuf_ptr;
  298. if ((NUM_TX_BUFF - 1) == i)
  299. tx[i].ctrl |= MAL_TX_CTRL_WRAP;
  300. tx_run[i] = -1;
  301. #if 0
  302. printf ("TX_BUFF %d @ 0x%08lx\n", i, (ulong) tx[i].data_ptr);
  303. #endif
  304. }
  305. for (i = 0; i < NUM_RX_BUFF; i++) {
  306. rx[i].ctrl = 0;
  307. rx[i].data_len = 0;
  308. /* rx[i].data_ptr = (char *) &rx_buff[i]; */
  309. rx[i].data_ptr = (char *) NetRxPackets[i];
  310. if ((NUM_RX_BUFF - 1) == i)
  311. rx[i].ctrl |= MAL_RX_CTRL_WRAP;
  312. rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
  313. rx_ready[i] = -1;
  314. #if 0
  315. printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) rx[i].data_ptr);
  316. #endif
  317. }
  318. memcpy (emac_hwd_addr, bis->bi_enetaddr, ENET_ADDR_LENGTH);
  319. reg = 0x00000000;
  320. reg |= emac_hwd_addr[0]; /* set high address */
  321. reg = reg << 8;
  322. reg |= emac_hwd_addr[1];
  323. out32 (EMAC_IAH, reg);
  324. reg = 0x00000000;
  325. reg |= emac_hwd_addr[2]; /* set low address */
  326. reg = reg << 8;
  327. reg |= emac_hwd_addr[3];
  328. reg = reg << 8;
  329. reg |= emac_hwd_addr[4];
  330. reg = reg << 8;
  331. reg |= emac_hwd_addr[5];
  332. out32 (EMAC_IAL, reg);
  333. /* setup MAL tx & rx channel pointers */
  334. mtdcr (maltxctp0r, tx);
  335. mtdcr (malrxctp0r, rx);
  336. /* Reset transmit and receive channels */
  337. mtdcr (malrxcarr, 0x80000000); /* 2 channels */
  338. mtdcr (maltxcarr, 0x80000000); /* 2 channels */
  339. /* Enable MAL transmit and receive channels */
  340. mtdcr (maltxcasr, 0x80000000); /* 1 channel */
  341. mtdcr (malrxcasr, 0x80000000); /* 1 channel */
  342. /* set RX buffer size */
  343. mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
  344. /* set transmit enable & receive enable */
  345. out32 (EMAC_M0, EMAC_M0_TXE | EMAC_M0_RXE);
  346. /* set receive fifo to 4k and tx fifo to 2k */
  347. mode_reg = EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
  348. /* set speed */
  349. if (speed == _100BASET)
  350. mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
  351. else
  352. mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
  353. if (duplex == FULL)
  354. mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
  355. out32 (EMAC_M1, mode_reg);
  356. /* Enable broadcast and indvidual address */
  357. out32 (EMAC_RXM, EMAC_RMR_BAE | EMAC_RMR_IAE
  358. /*| EMAC_RMR_ARRP| EMAC_RMR_SFCS | EMAC_RMR_SP */ );
  359. /* we probably need to set the tx mode1 reg? maybe at tx time */
  360. /* set transmit request threshold register */
  361. out32 (EMAC_TRTR, 0x18000000); /* 256 byte threshold */
  362. /* set receive low/high water mark register */
  363. #if defined(CONFIG_440)
  364. /* 440GP has a 64 byte burst length */
  365. out32 (EMAC_RX_HI_LO_WMARK, 0x80009000);
  366. out32 (EMAC_TXM1, 0xf8640000);
  367. #else /* CONFIG_440 */
  368. /* 405s have a 16 byte burst length */
  369. out32 (EMAC_RX_HI_LO_WMARK, 0x0f002000);
  370. #endif /* CONFIG_440 */
  371. /* Frame gap set */
  372. out32 (EMAC_I_FRAME_GAP_REG, 0x00000008);
  373. if (first_init == 0) {
  374. /*
  375. * Connect interrupt service routines
  376. */
  377. irq_install_handler (VECNUM_EWU0, (interrupt_handler_t *) enetInt, NULL);
  378. irq_install_handler (VECNUM_MS, (interrupt_handler_t *) enetInt, NULL);
  379. irq_install_handler (VECNUM_MTE, (interrupt_handler_t *) enetInt, NULL);
  380. irq_install_handler (VECNUM_MRE, (interrupt_handler_t *) enetInt, NULL);
  381. irq_install_handler (VECNUM_TXDE, (interrupt_handler_t *) enetInt, NULL);
  382. irq_install_handler (VECNUM_RXDE, (interrupt_handler_t *) enetInt, NULL);
  383. irq_install_handler (VECNUM_ETH0, (interrupt_handler_t *) enetInt, NULL);
  384. }
  385. /* set up interrupt handler */
  386. /* setup interrupt controler to take interrupts from the MAL &
  387. EMAC */
  388. mtdcr (uicsr, 0xffffffff); /* clear pending interrupts */
  389. mtdcr (uicer, mfdcr (uicer) | MAL_UIC_DEF | EMAC_UIC_DEF);
  390. /* set the MAL IER ??? names may change with new spec ??? */
  391. mal_ier = MAL_IER_DE | MAL_IER_NE | MAL_IER_TE | MAL_IER_OPBE |
  392. MAL_IER_PLBE;
  393. mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
  394. mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
  395. mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
  396. mtdcr (malier, mal_ier);
  397. /* Set EMAC IER */
  398. emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS |
  399. EMAC_ISR_PTLE | EMAC_ISR_ORE | EMAC_ISR_IRE;
  400. if (speed == _100BASET)
  401. emac_ier = emac_ier | EMAC_ISR_SYE;
  402. out32 (EMAC_ISR, 0xffffffff); /* clear pending interrupts */
  403. out32 (EMAC_IER, emac_ier);
  404. mtmsr (msr); /* enable interrupts again */
  405. bis_save = bis;
  406. first_init = 1;
  407. return (1);
  408. }
  409. static int ppc_4xx_eth_send (struct eth_device *dev, volatile void *ptr, int len)
  410. {
  411. struct enet_frame *ef_ptr;
  412. ulong time_start, time_now;
  413. unsigned long temp_txm0;
  414. ef_ptr = (struct enet_frame *) ptr;
  415. /*-----------------------------------------------------------------------+
  416. * Copy in our address into the frame.
  417. *-----------------------------------------------------------------------*/
  418. (void) memcpy (ef_ptr->source_addr, emac_hwd_addr, ENET_ADDR_LENGTH);
  419. /*-----------------------------------------------------------------------+
  420. * If frame is too long or too short, modify length.
  421. *-----------------------------------------------------------------------*/
  422. if (len > ENET_MAX_MTU)
  423. len = ENET_MAX_MTU;
  424. /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
  425. memcpy ((void *) txbuf_ptr, (const void *) ptr, len);
  426. /*-----------------------------------------------------------------------+
  427. * set TX Buffer busy, and send it
  428. *-----------------------------------------------------------------------*/
  429. tx[tx_slot].ctrl = (MAL_TX_CTRL_LAST |
  430. EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
  431. ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
  432. if ((NUM_TX_BUFF - 1) == tx_slot)
  433. tx[tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
  434. tx[tx_slot].data_len = (short) len;
  435. tx[tx_slot].ctrl |= MAL_TX_CTRL_READY;
  436. __asm__ volatile ("eieio");
  437. out32 (EMAC_TXM0, in32 (EMAC_TXM0) | EMAC_TXM0_GNP0);
  438. #ifdef INFO_405_ENET
  439. packetSent++;
  440. #endif
  441. /*-----------------------------------------------------------------------+
  442. * poll unitl the packet is sent and then make sure it is OK
  443. *-----------------------------------------------------------------------*/
  444. time_start = get_timer (0);
  445. while (1) {
  446. temp_txm0 = in32 (EMAC_TXM0);
  447. /* loop until either TINT turns on or 3 seconds elapse */
  448. if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
  449. /* transmit is done, so now check for errors
  450. * If there is an error, an interrupt should
  451. * happen when we return
  452. */
  453. time_now = get_timer (0);
  454. if ((time_now - time_start) > 3000) {
  455. return (-1);
  456. }
  457. } else {
  458. return (len);
  459. }
  460. }
  461. }
  462. #if defined(CONFIG_440)
  463. /*-----------------------------------------------------------------------------+
  464. | EnetInt.
  465. | EnetInt is the interrupt handler. It will determine the
  466. | cause of the interrupt and call the apporpriate servive
  467. | routine.
  468. +-----------------------------------------------------------------------------*/
  469. int enetInt ()
  470. {
  471. int serviced;
  472. int rc = -1; /* default to not us */
  473. unsigned long mal_isr;
  474. unsigned long emac_isr = 0;
  475. unsigned long mal_rx_eob;
  476. unsigned long my_uic0msr, my_uic1msr;
  477. /* enter loop that stays in interrupt code until nothing to service */
  478. do {
  479. serviced = 0;
  480. my_uic0msr = mfdcr (uic0msr);
  481. my_uic1msr = mfdcr (uic1msr);
  482. if (!(my_uic0msr & UIC_MRE)
  483. && !(my_uic1msr & (UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE))) {
  484. /* not for us */
  485. return (rc);
  486. }
  487. /* get and clear controller status interrupts */
  488. /* look at Mal and EMAC interrupts */
  489. if ((my_uic0msr & UIC_MRE)
  490. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  491. /* we have a MAL interrupt */
  492. mal_isr = mfdcr (malesr);
  493. /* look for mal error */
  494. if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
  495. mal_err (mal_isr, my_uic0msr, MAL_UIC_DEF, MAL_UIC_ERR);
  496. serviced = 1;
  497. rc = 0;
  498. }
  499. }
  500. if (UIC_ETH0 & my_uic1msr) { /* look for EMAC errors */
  501. emac_isr = in32 (EMAC_ISR);
  502. if ((emac_ier & emac_isr) != 0) {
  503. emac_err (emac_isr);
  504. serviced = 1;
  505. rc = 0;
  506. }
  507. }
  508. if ((emac_ier & emac_isr)
  509. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  510. mtdcr (uic0sr, UIC_MRE); /* Clear */
  511. mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  512. return (rc); /* we had errors so get out */
  513. }
  514. /* handle MAL RX EOB interupt from a receive */
  515. /* check for EOB on valid channels */
  516. if (my_uic0msr & UIC_MRE) {
  517. mal_rx_eob = mfdcr (malrxeobisr);
  518. if ((mal_rx_eob & 0x80000000) != 0) { /* call emac routine for channel 0 */
  519. /* clear EOB
  520. mtdcr(malrxeobisr, mal_rx_eob); */
  521. enet_rcv (emac_isr);
  522. /* indicate that we serviced an interrupt */
  523. serviced = 1;
  524. rc = 0;
  525. }
  526. }
  527. mtdcr (uic0sr, UIC_MRE); /* Clear */
  528. mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  529. } while (serviced);
  530. return (rc);
  531. }
  532. #else /* CONFIG_440 */
  533. /*-----------------------------------------------------------------------------+
  534. * EnetInt.
  535. * EnetInt is the interrupt handler. It will determine the
  536. * cause of the interrupt and call the apporpriate servive
  537. * routine.
  538. *-----------------------------------------------------------------------------*/
  539. int enetInt ()
  540. {
  541. int serviced;
  542. int rc = -1; /* default to not us */
  543. unsigned long mal_isr;
  544. unsigned long emac_isr = 0;
  545. unsigned long mal_rx_eob;
  546. unsigned long my_uicmsr;
  547. /* enter loop that stays in interrupt code until nothing to service */
  548. do {
  549. serviced = 0;
  550. my_uicmsr = mfdcr (uicmsr);
  551. if ((my_uicmsr & (MAL_UIC_DEF | EMAC_UIC_DEF)) == 0) { /* not for us */
  552. return (rc);
  553. }
  554. /* get and clear controller status interrupts */
  555. /* look at Mal and EMAC interrupts */
  556. if ((MAL_UIC_DEF & my_uicmsr) != 0) { /* we have a MAL interrupt */
  557. mal_isr = mfdcr (malesr);
  558. /* look for mal error */
  559. if ((my_uicmsr & MAL_UIC_ERR) != 0) {
  560. mal_err (mal_isr, my_uicmsr, MAL_UIC_DEF, MAL_UIC_ERR);
  561. serviced = 1;
  562. rc = 0;
  563. }
  564. }
  565. if ((EMAC_UIC_DEF & my_uicmsr) != 0) { /* look for EMAC errors */
  566. emac_isr = in32 (EMAC_ISR);
  567. if ((emac_ier & emac_isr) != 0) {
  568. emac_err (emac_isr);
  569. serviced = 1;
  570. rc = 0;
  571. }
  572. }
  573. if (((emac_ier & emac_isr) != 0) | ((MAL_UIC_ERR & my_uicmsr) != 0)) {
  574. mtdcr (uicsr, MAL_UIC_DEF | EMAC_UIC_DEF); /* Clear */
  575. return (rc); /* we had errors so get out */
  576. }
  577. /* handle MAL RX EOB interupt from a receive */
  578. /* check for EOB on valid channels */
  579. if ((my_uicmsr & UIC_MAL_RXEOB) != 0) {
  580. mal_rx_eob = mfdcr (malrxeobisr);
  581. if ((mal_rx_eob & 0x80000000) != 0) { /* call emac routine for channel 0 */
  582. /* clear EOB
  583. mtdcr(malrxeobisr, mal_rx_eob); */
  584. enet_rcv (emac_isr);
  585. /* indicate that we serviced an interrupt */
  586. serviced = 1;
  587. rc = 0;
  588. }
  589. }
  590. mtdcr (uicsr, MAL_UIC_DEF | EMAC_UIC_DEF); /* Clear */
  591. }
  592. while (serviced);
  593. return (rc);
  594. }
  595. #endif /* CONFIG_440 */
  596. /*-----------------------------------------------------------------------------+
  597. * MAL Error Routine
  598. *-----------------------------------------------------------------------------*/
  599. static void mal_err (unsigned long isr, unsigned long uic, unsigned long maldef,
  600. unsigned long mal_errr)
  601. {
  602. mtdcr (malesr, isr); /* clear interrupt */
  603. /* clear DE interrupt */
  604. mtdcr (maltxdeir, 0xC0000000);
  605. mtdcr (malrxdeir, 0x80000000);
  606. #ifdef INFO_405_ENET
  607. printf ("\nMAL error occured.... ISR = %lx UIC = = %lx MAL_DEF = %lx MAL_ERR= %lx \n",
  608. isr, uic, maldef, mal_errr);
  609. #else
  610. #if 0
  611. /*
  612. * MAL error is RX DE error (out of rx buffers)! This is OK here, upon
  613. * many incoming packets with only 4 rx buffers.
  614. */
  615. printf ("M"); /* just to see something upon mal error */
  616. #endif
  617. #endif
  618. eth_init (bis_save); /* start again... */
  619. }
  620. /*-----------------------------------------------------------------------------+
  621. * EMAC Error Routine
  622. *-----------------------------------------------------------------------------*/
  623. static void emac_err (unsigned long isr)
  624. {
  625. printf ("EMAC error occured.... ISR = %lx\n", isr);
  626. out32 (EMAC_ISR, isr);
  627. }
  628. /*-----------------------------------------------------------------------------+
  629. * enet_rcv() handles the ethernet receive data
  630. *-----------------------------------------------------------------------------*/
  631. static void enet_rcv (unsigned long malisr)
  632. {
  633. struct enet_frame *ef_ptr;
  634. unsigned long data_len;
  635. unsigned long rx_eob_isr;
  636. int handled = 0;
  637. int i;
  638. int loop_count = 0;
  639. rx_eob_isr = mfdcr (malrxeobisr);
  640. if ((0x80000000 >> (EMAC_RXCHL - 1)) & rx_eob_isr) {
  641. /* clear EOB */
  642. mtdcr (malrxeobisr, rx_eob_isr);
  643. /* EMAC RX done */
  644. while (1) { /* do all */
  645. i = rx_slot;
  646. if ((MAL_RX_CTRL_EMPTY & rx[i].ctrl)
  647. || (loop_count >= NUM_RX_BUFF))
  648. break;
  649. loop_count++;
  650. rx_slot++;
  651. if (NUM_RX_BUFF == rx_slot)
  652. rx_slot = 0;
  653. handled++;
  654. data_len = (unsigned long) rx[i].data_len; /* Get len */
  655. if (data_len) {
  656. if (data_len > ENET_MAX_MTU) /* Check len */
  657. data_len = 0;
  658. else {
  659. if (EMAC_RX_ERRORS & rx[i].ctrl) { /* Check Errors */
  660. data_len = 0;
  661. stats.rx_err_log[rx_err_index] = rx[i].ctrl;
  662. rx_err_index++;
  663. if (rx_err_index == MAX_ERR_LOG)
  664. rx_err_index = 0;
  665. } /* emac_erros */
  666. } /* data_len < max mtu */
  667. } /* if data_len */
  668. if (!data_len) { /* no data */
  669. rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
  670. stats.emac.data_len_err++; /* Error at Rx */
  671. }
  672. /* !data_len */
  673. /* AS.HARNOIS */
  674. /* Check if user has already eaten buffer */
  675. /* if not => ERROR */
  676. else if (rx_ready[rx_i_index] != -1) {
  677. if (is_receiving)
  678. printf ("ERROR : Receive buffers are full!\n");
  679. break;
  680. } else {
  681. stats.emac.rx_frames++;
  682. stats.emac.rx += data_len;
  683. ef_ptr = (struct enet_frame *) rx[i].data_ptr;
  684. #ifdef INFO_405_ENET
  685. packetReceived++;
  686. #endif
  687. /* AS.HARNOIS
  688. * use ring buffer
  689. */
  690. rx_ready[rx_i_index] = i;
  691. rx_i_index++;
  692. if (NUM_RX_BUFF == rx_i_index)
  693. rx_i_index = 0;
  694. /* printf("X"); /|* test-only *|/ */
  695. /* AS.HARNOIS
  696. * free receive buffer only when
  697. * buffer has been handled (eth_rx)
  698. rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
  699. */
  700. } /* if data_len */
  701. } /* while */
  702. } /* if EMACK_RXCHL */
  703. }
  704. static int ppc_4xx_eth_rx (struct eth_device *dev)
  705. {
  706. int length;
  707. int user_index;
  708. unsigned long msr;
  709. is_receiving = 1; /* tell driver */
  710. for (;;) {
  711. /* AS.HARNOIS
  712. * use ring buffer and
  713. * get index from rx buffer desciptor queue
  714. */
  715. user_index = rx_ready[rx_u_index];
  716. if (user_index == -1) {
  717. length = -1;
  718. break; /* nothing received - leave for() loop */
  719. }
  720. msr = mfmsr ();
  721. mtmsr (msr & ~(MSR_EE));
  722. length = rx[user_index].data_len;
  723. /* Pass the packet up to the protocol layers. */
  724. /* NetReceive(NetRxPackets[rxIdx], length - 4); */
  725. /* NetReceive(NetRxPackets[i], length); */
  726. NetReceive (NetRxPackets[user_index], length - 4);
  727. /* Free Recv Buffer */
  728. rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
  729. /* Free rx buffer descriptor queue */
  730. rx_ready[rx_u_index] = -1;
  731. rx_u_index++;
  732. if (NUM_RX_BUFF == rx_u_index)
  733. rx_u_index = 0;
  734. #ifdef INFO_405_ENET
  735. packetHandled++;
  736. #endif
  737. mtmsr (msr); /* Enable IRQ's */
  738. }
  739. is_receiving = 0; /* tell driver */
  740. return length;
  741. }
  742. #if defined(CONFIG_NET_MULTI)
  743. int ppc_4xx_eth_initialize(bd_t *bis)
  744. {
  745. struct eth_device *dev;
  746. int eth_num = 0;
  747. dev = malloc (sizeof *dev);
  748. if (dev == NULL) {
  749. printf(__FUNCTION__ ": Cannot allocate eth_device\n");
  750. return (-1);
  751. }
  752. sprintf(dev->name, "ppc_4xx_eth%d", eth_num);
  753. dev->priv = (void *) eth_num;
  754. dev->init = ppc_4xx_eth_init;
  755. dev->halt = ppc_4xx_eth_halt;
  756. dev->send = ppc_4xx_eth_send;
  757. dev->recv = ppc_4xx_eth_rx;
  758. eth_register (dev);
  759. }
  760. #else /* !defined(CONFIG_NET_MULTI) */
  761. void eth_halt (void)
  762. {
  763. ppc_4xx_eth_halt(NULL);
  764. }
  765. int eth_init (bd_t *bis)
  766. {
  767. return (ppc_4xx_eth_init(NULL, bis));
  768. }
  769. int eth_send(volatile void *packet, int length)
  770. {
  771. return (ppc_4xx_eth_send(NULL, packet, length));
  772. }
  773. int eth_rx(void)
  774. {
  775. return (ppc_4xx_eth_rx(NULL));
  776. }
  777. #endif /* !defined(CONFIG_NET_MULTI) */
  778. #endif /* CONFIG_405GP */