405gp_enet.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. memset(dev, 0, sizeof(*dev));
  818. /* Allocate our private use data */
  819. hw = (EMAC_405_HW_PST) malloc (sizeof (*hw));
  820. if (hw == NULL) {
  821. printf ("ppc_405x_eth_initialize: "
  822. "Cannot allocate private hw data for eth_device %d",
  823. eth_num);
  824. free (dev);
  825. return (-1);
  826. }
  827. memset(hw, 0, sizeof(*hw));
  828. switch (eth_num) {
  829. case 0:
  830. hw->hw_addr = 0;
  831. memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
  832. break;
  833. #if defined(CONFIG_NET_MULTI)
  834. case 1:
  835. hw->hw_addr = 0x100;
  836. memcpy (dev->enetaddr, bis->bi_enet1addr, 6);
  837. break;
  838. #endif
  839. default:
  840. hw->hw_addr = 0;
  841. memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
  842. break;
  843. }
  844. hw->devnum = eth_num;
  845. hw->print_speed = 1;
  846. sprintf (dev->name, "ppc_405x_eth%d", eth_num);
  847. dev->priv = (void *) hw;
  848. dev->init = ppc_4xx_eth_init;
  849. dev->halt = ppc_4xx_eth_halt;
  850. dev->send = ppc_4xx_eth_send;
  851. dev->recv = ppc_4xx_eth_rx;
  852. if (0 == virgin) {
  853. /* set the MAL IER ??? names may change with new spec ??? */
  854. mal_ier =
  855. MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
  856. MAL_IER_OPBE | MAL_IER_PLBE;
  857. mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
  858. mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
  859. mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
  860. mtdcr (malier, mal_ier);
  861. #if defined(CONFIG_405EP)
  862. /* 405EP has one EWU interrupt */
  863. irq_install_handler (VECNUM_EWU0,
  864. (interrupt_handler_t *) enetInt,
  865. dev);
  866. #endif
  867. /* install MAL interrupt handler */
  868. irq_install_handler (VECNUM_MS,
  869. (interrupt_handler_t *) enetInt,
  870. dev);
  871. irq_install_handler (VECNUM_MTE,
  872. (interrupt_handler_t *) enetInt,
  873. dev);
  874. irq_install_handler (VECNUM_MRE,
  875. (interrupt_handler_t *) enetInt,
  876. dev);
  877. irq_install_handler (VECNUM_TXDE,
  878. (interrupt_handler_t *) enetInt,
  879. dev);
  880. irq_install_handler (VECNUM_RXDE,
  881. (interrupt_handler_t *) enetInt,
  882. dev);
  883. virgin = 1;
  884. }
  885. #if defined(CONFIG_NET_MULTI)
  886. eth_register (dev);
  887. #else
  888. emac0_dev = dev;
  889. #endif
  890. } /* end for each supported device */
  891. return (1);
  892. }
  893. #if !defined(CONFIG_NET_MULTI)
  894. void eth_halt (void) {
  895. if (emac0_dev) {
  896. ppc_4xx_eth_halt(emac0_dev);
  897. free(emac0_dev);
  898. emac0_dev = NULL;
  899. }
  900. }
  901. int eth_init (bd_t *bis)
  902. {
  903. ppc_4xx_eth_initialize(bis);
  904. return(ppc_4xx_eth_init(emac0_dev, bis));
  905. }
  906. int eth_send(volatile void *packet, int length)
  907. {
  908. return (ppc_4xx_eth_send(emac0_dev, packet, length));
  909. }
  910. int eth_rx(void)
  911. {
  912. return (ppc_4xx_eth_rx(emac0_dev));
  913. }
  914. #endif
  915. #endif /* CONFIG_405 */