405gp_enet.c 31 KB

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