405gp_enet.c 28 KB

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