405gp_enet.c 31 KB

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