405gp_enet.c 31 KB

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