405gp_enet.c 27 KB

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