440gx_enet.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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. *-----------------------------------------------------------------------------*
  71. * 17-Nov-03 travis.sawyer@sandburst.com
  72. * - ported from 405gp_enet.c to utilized upto 4 EMAC ports
  73. * in the 440GX. This port should work with the 440GP
  74. * (2 EMACs) also
  75. *-----------------------------------------------------------------------------*/
  76. #include <config.h>
  77. #if defined(CONFIG_440) && defined(CONFIG_NET_MULTI)
  78. #include <common.h>
  79. #include <net.h>
  80. #include <asm/processor.h>
  81. #include <ppc440.h>
  82. #include <commproc.h>
  83. #include <440gx_enet.h>
  84. #include <405_mal.h>
  85. #include <miiphy.h>
  86. #include <malloc.h>
  87. #include "vecnum.h"
  88. #define EMAC_RESET_TIMEOUT 1000 /* 1000 ms reset timeout */
  89. #define PHY_AUTONEGOTIATE_TIMEOUT 4000 /* 4000 ms autonegotiate timeout */
  90. /* Ethernet Transmit and Receive Buffers */
  91. /* AS.HARNOIS
  92. * In the same way ENET_MAX_MTU and ENET_MAX_MTU_ALIGNED are set from
  93. * PKTSIZE and PKTSIZE_ALIGN (include/net.h)
  94. */
  95. #define ENET_MAX_MTU PKTSIZE
  96. #define ENET_MAX_MTU_ALIGNED PKTSIZE_ALIGN
  97. /* define the number of channels implemented */
  98. #define EMAC_RXCHL EMAC_NUM_DEV
  99. #define EMAC_TXCHL EMAC_NUM_DEV
  100. /*-----------------------------------------------------------------------------+
  101. * Defines for MAL/EMAC interrupt conditions as reported in the UIC (Universal
  102. * Interrupt Controller).
  103. *-----------------------------------------------------------------------------*/
  104. #define MAL_UIC_ERR ( UIC_MAL_SERR | UIC_MAL_TXDE | UIC_MAL_RXDE)
  105. #define MAL_UIC_DEF (UIC_MAL_RXEOB | MAL_UIC_ERR)
  106. #define EMAC_UIC_DEF UIC_ENET
  107. #undef INFO_440_ENET
  108. #define BI_PHYMODE_NONE 0
  109. #define BI_PHYMODE_ZMII 1
  110. #define BI_PHYMODE_RGMII 2
  111. /*-----------------------------------------------------------------------------+
  112. * Global variables. TX and RX descriptors and buffers.
  113. *-----------------------------------------------------------------------------*/
  114. /* IER globals */
  115. static uint32_t mal_ier;
  116. /*-----------------------------------------------------------------------------+
  117. * Prototypes and externals.
  118. *-----------------------------------------------------------------------------*/
  119. static void enet_rcv (struct eth_device *dev, unsigned long malisr);
  120. int enetInt (struct eth_device *dev);
  121. static void mal_err (struct eth_device *dev, unsigned long isr,
  122. unsigned long uic, unsigned long maldef,
  123. unsigned long mal_errr);
  124. static void emac_err (struct eth_device *dev, unsigned long isr);
  125. /*-----------------------------------------------------------------------------+
  126. | ppc_440x_eth_halt
  127. | Disable MAL channel, and EMACn
  128. |
  129. |
  130. +-----------------------------------------------------------------------------*/
  131. static void ppc_440x_eth_halt (struct eth_device *dev)
  132. {
  133. EMAC_440GX_HW_PST hw_p = dev->priv;
  134. uint32_t failsafe = 10000;
  135. out32 (EMAC_IER + hw_p->hw_addr, 0x00000000); /* disable emac interrupts */
  136. /* 1st reset MAL channel */
  137. /* Note: writing a 0 to a channel has no effect */
  138. mtdcr (maltxcarr, (MAL_CR_MMSR >> hw_p->devnum));
  139. mtdcr (malrxcarr, (MAL_CR_MMSR >> hw_p->devnum));
  140. /* wait for reset */
  141. while (mfdcr (maltxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
  142. udelay (1000); /* Delay 1 MS so as not to hammer the register */
  143. failsafe--;
  144. if (failsafe == 0)
  145. break;
  146. }
  147. /* EMAC RESET */
  148. out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
  149. hw_p->print_speed = 1; /* print speed message again next time */
  150. return;
  151. }
  152. extern int phy_setup_aneg (unsigned char addr);
  153. extern int miiphy_reset (unsigned char addr);
  154. #if defined (CONFIG_440_GX)
  155. int ppc_440x_eth_setup_bridge(int devnum, bd_t * bis)
  156. {
  157. unsigned long pfc1;
  158. unsigned long zmiifer;
  159. unsigned long rmiifer;
  160. mfsdr(sdr_pfc1, pfc1);
  161. pfc1 = SDR0_PFC1_EPS_DECODE(pfc1);
  162. zmiifer = 0;
  163. rmiifer = 0;
  164. switch (pfc1) {
  165. case 1:
  166. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
  167. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(1);
  168. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(2);
  169. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(3);
  170. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  171. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  172. bis->bi_phymode[2] = BI_PHYMODE_ZMII;
  173. bis->bi_phymode[3] = BI_PHYMODE_ZMII;
  174. break;
  175. case 2:
  176. zmiifer = ZMII_FER_SMII << ZMII_FER_V(0);
  177. zmiifer = ZMII_FER_SMII << ZMII_FER_V(1);
  178. zmiifer = ZMII_FER_SMII << ZMII_FER_V(2);
  179. zmiifer = ZMII_FER_SMII << ZMII_FER_V(3);
  180. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  181. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  182. bis->bi_phymode[2] = BI_PHYMODE_ZMII;
  183. bis->bi_phymode[3] = BI_PHYMODE_ZMII;
  184. break;
  185. case 3:
  186. zmiifer |= ZMII_FER_RMII << ZMII_FER_V(0);
  187. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
  188. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  189. bis->bi_phymode[1] = BI_PHYMODE_NONE;
  190. bis->bi_phymode[2] = BI_PHYMODE_RGMII;
  191. bis->bi_phymode[3] = BI_PHYMODE_NONE;
  192. break;
  193. case 4:
  194. zmiifer |= ZMII_FER_SMII << ZMII_FER_V(0);
  195. zmiifer |= ZMII_FER_SMII << ZMII_FER_V(1);
  196. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (2);
  197. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V (3);
  198. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  199. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  200. bis->bi_phymode[2] = BI_PHYMODE_RGMII;
  201. bis->bi_phymode[3] = BI_PHYMODE_RGMII;
  202. break;
  203. case 5:
  204. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
  205. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
  206. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (2);
  207. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
  208. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  209. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  210. bis->bi_phymode[2] = BI_PHYMODE_ZMII;
  211. bis->bi_phymode[3] = BI_PHYMODE_RGMII;
  212. break;
  213. case 6:
  214. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (0);
  215. zmiifer |= ZMII_FER_SMII << ZMII_FER_V (1);
  216. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(2);
  217. rmiifer |= RGMII_FER_RGMII << RGMII_FER_V(3);
  218. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  219. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  220. bis->bi_phymode[2] = BI_PHYMODE_RGMII;
  221. bis->bi_phymode[3] = BI_PHYMODE_RGMII;
  222. break;
  223. case 0:
  224. default:
  225. zmiifer = ZMII_FER_MII << ZMII_FER_V(devnum);
  226. rmiifer = 0x0;
  227. bis->bi_phymode[0] = BI_PHYMODE_ZMII;
  228. bis->bi_phymode[1] = BI_PHYMODE_ZMII;
  229. bis->bi_phymode[2] = BI_PHYMODE_ZMII;
  230. bis->bi_phymode[3] = BI_PHYMODE_ZMII;
  231. break;
  232. }
  233. /* Ensure we setup mdio for this devnum and ONLY this devnum */
  234. zmiifer |= (ZMII_FER_MDI) << ZMII_FER_V(devnum);
  235. out32 (ZMII_FER, zmiifer);
  236. out32 (RGMII_FER, rmiifer);
  237. return ((int)pfc1);
  238. }
  239. #endif
  240. static int ppc_440x_eth_init (struct eth_device *dev, bd_t * bis)
  241. {
  242. int i;
  243. unsigned long reg;
  244. unsigned long msr;
  245. unsigned long speed;
  246. unsigned long duplex;
  247. unsigned long failsafe;
  248. unsigned mode_reg;
  249. unsigned short devnum;
  250. unsigned short reg_short;
  251. sys_info_t sysinfo;
  252. int ethgroup;
  253. EMAC_440GX_HW_PST hw_p = dev->priv;
  254. /* before doing anything, figure out if we have a MAC address */
  255. /* if not, bail */
  256. if (memcmp (dev->enetaddr, "\0\0\0\0\0\0", 6) == 0)
  257. return -1;
  258. /* Need to get the OPB frequency so we can access the PHY */
  259. get_sys_info (&sysinfo);
  260. msr = mfmsr ();
  261. mtmsr (msr & ~(MSR_EE)); /* disable interrupts */
  262. devnum = hw_p->devnum;
  263. #ifdef INFO_440_ENET
  264. /* AS.HARNOIS
  265. * We should have :
  266. * hw_p->stats.pkts_handled <= hw_p->stats.pkts_rx <= hw_p->stats.pkts_handled+PKTBUFSRX
  267. * In the most cases hw_p->stats.pkts_handled = hw_p->stats.pkts_rx, but it
  268. * is possible that new packets (without relationship with
  269. * current transfer) have got the time to arrived before
  270. * netloop calls eth_halt
  271. */
  272. printf ("About preceeding transfer (eth%d):\n"
  273. "- Sent packet number %d\n"
  274. "- Received packet number %d\n"
  275. "- Handled packet number %d\n",
  276. hw_p->devnum,
  277. hw_p->stats.pkts_tx,
  278. hw_p->stats.pkts_rx, hw_p->stats.pkts_handled);
  279. hw_p->stats.pkts_tx = 0;
  280. hw_p->stats.pkts_rx = 0;
  281. hw_p->stats.pkts_handled = 0;
  282. #endif
  283. /* MAL Channel RESET */
  284. /* 1st reset MAL channel */
  285. /* Note: writing a 0 to a channel has no effect */
  286. mtdcr (maltxcarr, (MAL_TXRX_CASR >> hw_p->devnum));
  287. mtdcr (malrxcarr, (MAL_TXRX_CASR >> hw_p->devnum));
  288. /* wait for reset */
  289. /* TBS: should have udelay and failsafe here */
  290. failsafe = 10000;
  291. /* wait for reset */
  292. while (mfdcr (maltxcasr) & (MAL_CR_MMSR >> hw_p->devnum)) {
  293. udelay (1000); /* Delay 1 MS so as not to hammer the register */
  294. failsafe--;
  295. if (failsafe == 0)
  296. break;
  297. }
  298. hw_p->tx_err_index = 0; /* Transmit Error Index for tx_err_log */
  299. hw_p->rx_err_index = 0; /* Receive Error Index for rx_err_log */
  300. hw_p->rx_slot = 0; /* MAL Receive Slot */
  301. hw_p->rx_i_index = 0; /* Receive Interrupt Queue Index */
  302. hw_p->rx_u_index = 0; /* Receive User Queue Index */
  303. hw_p->tx_slot = 0; /* MAL Transmit Slot */
  304. hw_p->tx_i_index = 0; /* Transmit Interrupt Queue Index */
  305. hw_p->tx_u_index = 0; /* Transmit User Queue Index */
  306. /* set RMII mode */
  307. /* NOTE: 440GX spec states that mode is mutually exclusive */
  308. /* NOTE: Therefore, disable all other EMACS, since we handle */
  309. /* NOTE: only one emac at a time */
  310. reg = 0;
  311. out32 (ZMII_FER, 0);
  312. udelay (100);
  313. #if defined(CONFIG_440_GX)
  314. ethgroup = ppc_440x_eth_setup_bridge(devnum, bis);
  315. #else
  316. if ((devnum == 0) || (devnum == 1)) {
  317. out32 (ZMII_FER, (ZMII_FER_SMII | ZMII_FER_MDI) << ZMII_FER_V (devnum));
  318. }
  319. else { /* ((devnum == 2) || (devnum == 3)) */
  320. out32 (ZMII_FER, ZMII_FER_MDI << ZMII_FER_V (devnum));
  321. out32 (RGMII_FER, ((RGMII_FER_RGMII << RGMII_FER_V (2)) |
  322. (RGMII_FER_RGMII << RGMII_FER_V (3))));
  323. }
  324. #endif
  325. out32 (ZMII_SSR, ZMII_SSR_SP << ZMII_SSR_V(devnum));
  326. __asm__ volatile ("eieio");
  327. /* reset emac so we have access to the phy */
  328. out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_SRST);
  329. __asm__ volatile ("eieio");
  330. failsafe = 1000;
  331. while ((in32 (EMAC_M0 + hw_p->hw_addr) & (EMAC_M0_SRST)) && failsafe) {
  332. udelay (1000);
  333. failsafe--;
  334. }
  335. /* Whack the M1 register */
  336. mode_reg = 0x0;
  337. mode_reg &= ~0x00000038;
  338. if (sysinfo.freqOPB <= 50000000);
  339. else if (sysinfo.freqOPB <= 66666667)
  340. mode_reg |= EMAC_M1_OBCI_66;
  341. else if (sysinfo.freqOPB <= 83333333)
  342. mode_reg |= EMAC_M1_OBCI_83;
  343. else if (sysinfo.freqOPB <= 100000000)
  344. mode_reg |= EMAC_M1_OBCI_100;
  345. else
  346. mode_reg |= EMAC_M1_OBCI_GT100;
  347. out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
  348. /* wait for PHY to complete auto negotiation */
  349. reg_short = 0;
  350. #ifndef CONFIG_CS8952_PHY
  351. switch (devnum) {
  352. case 0:
  353. reg = CONFIG_PHY_ADDR;
  354. break;
  355. case 1:
  356. reg = CONFIG_PHY1_ADDR;
  357. break;
  358. #if defined (CONFIG_440_GX)
  359. case 2:
  360. reg = CONFIG_PHY2_ADDR;
  361. break;
  362. case 3:
  363. reg = CONFIG_PHY3_ADDR;
  364. break;
  365. #endif
  366. default:
  367. reg = CONFIG_PHY_ADDR;
  368. break;
  369. }
  370. bis->bi_phynum[devnum] = reg;
  371. /* Reset the phy */
  372. miiphy_reset (reg);
  373. #if defined(CONFIG_440_GX)
  374. #if defined(CONFIG_CIS8201_PHY)
  375. /*
  376. * Cicada 8201 PHY needs to have an extended register whacked
  377. * for RGMII mode.
  378. */
  379. if ( ((devnum == 2) || (devnum ==3)) && (4 == ethgroup) ) {
  380. miiphy_write (reg, 23, 0x1200);
  381. /*
  382. * Vitesse VSC8201/Cicada CIS8201 errata:
  383. * Interoperability problem with Intel 82547EI phys
  384. * This work around (provided by Vitesse) changes
  385. * the default timer convergence from 8ms to 12ms
  386. */
  387. miiphy_write (reg, 0x1f, 0x2a30);
  388. miiphy_write (reg, 0x08, 0x0200);
  389. miiphy_write (reg, 0x1f, 0x52b5);
  390. miiphy_write (reg, 0x02, 0x0004);
  391. miiphy_write (reg, 0x01, 0x0671);
  392. miiphy_write (reg, 0x00, 0x8fae);
  393. miiphy_write (reg, 0x1f, 0x2a30);
  394. miiphy_write (reg, 0x08, 0x0000);
  395. miiphy_write (reg, 0x1f, 0x0000);
  396. /* end Vitesse/Cicada errata */
  397. }
  398. #endif
  399. #endif
  400. /* Start/Restart autonegotiation */
  401. phy_setup_aneg (reg);
  402. udelay (1000);
  403. miiphy_read (reg, PHY_BMSR, &reg_short);
  404. /*
  405. * Wait if PHY is capable of autonegotiation and autonegotiation is not complete
  406. */
  407. if ((reg_short & PHY_BMSR_AUTN_ABLE)
  408. && !(reg_short & PHY_BMSR_AUTN_COMP)) {
  409. puts ("Waiting for PHY auto negotiation to complete");
  410. i = 0;
  411. while (!(reg_short & PHY_BMSR_AUTN_COMP)) {
  412. /*
  413. * Timeout reached ?
  414. */
  415. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  416. puts (" TIMEOUT !\n");
  417. break;
  418. }
  419. if ((i++ % 1000) == 0) {
  420. putc ('.');
  421. }
  422. udelay (1000); /* 1 ms */
  423. miiphy_read (reg, PHY_BMSR, &reg_short);
  424. }
  425. puts (" done\n");
  426. udelay (500000); /* another 500 ms (results in faster booting) */
  427. }
  428. #endif
  429. speed = miiphy_speed (reg);
  430. duplex = miiphy_duplex (reg);
  431. if (hw_p->print_speed) {
  432. hw_p->print_speed = 0;
  433. printf ("ENET Speed is %d Mbps - %s duplex connection\n",
  434. (int) speed, (duplex == HALF) ? "HALF" : "FULL");
  435. }
  436. /* Set ZMII/RGMII speed according to the phy link speed */
  437. reg = in32 (ZMII_SSR);
  438. if ( (speed == 100) || (speed == 1000) )
  439. out32 (ZMII_SSR, reg | (ZMII_SSR_SP << ZMII_SSR_V (devnum)));
  440. else
  441. out32 (ZMII_SSR,
  442. reg & (~(ZMII_SSR_SP << ZMII_SSR_V (devnum))));
  443. if ((devnum == 2) || (devnum == 3)) {
  444. if (speed == 1000)
  445. reg = (RGMII_SSR_SP_1000MBPS << RGMII_SSR_V (devnum));
  446. else if (speed == 100)
  447. reg = (RGMII_SSR_SP_100MBPS << RGMII_SSR_V (devnum));
  448. else
  449. reg = (RGMII_SSR_SP_10MBPS << RGMII_SSR_V (devnum));
  450. out32 (RGMII_SSR, reg);
  451. }
  452. /* set the Mal configuration reg */
  453. /* Errata 1.12: MAL_1 -- Disable MAL bursting */
  454. if (get_pvr () == PVR_440GP_RB)
  455. mtdcr (malmcr,
  456. MAL_CR_OPBBL | MAL_CR_LEA | MAL_CR_PLBLT_DEFAULT);
  457. else
  458. mtdcr (malmcr,
  459. MAL_CR_PLBB | MAL_CR_OPBBL | MAL_CR_LEA |
  460. MAL_CR_PLBLT_DEFAULT | MAL_CR_EOPIE | 0x00330000);
  461. /* Free "old" buffers */
  462. if (hw_p->alloc_tx_buf)
  463. free (hw_p->alloc_tx_buf);
  464. if (hw_p->alloc_rx_buf)
  465. free (hw_p->alloc_rx_buf);
  466. /*
  467. * Malloc MAL buffer desciptors, make sure they are
  468. * aligned on cache line boundary size
  469. * (401/403/IOP480 = 16, 405 = 32)
  470. * and doesn't cross cache block boundaries.
  471. */
  472. hw_p->alloc_tx_buf =
  473. (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_TX_BUFF) +
  474. ((2 * CFG_CACHELINE_SIZE) - 2));
  475. if (((int) hw_p->alloc_tx_buf & CACHELINE_MASK) != 0) {
  476. hw_p->tx =
  477. (mal_desc_t *) ((int) hw_p->alloc_tx_buf +
  478. CFG_CACHELINE_SIZE -
  479. ((int) hw_p->
  480. alloc_tx_buf & CACHELINE_MASK));
  481. } else {
  482. hw_p->tx = hw_p->alloc_tx_buf;
  483. }
  484. hw_p->alloc_rx_buf =
  485. (mal_desc_t *) malloc ((sizeof (mal_desc_t) * NUM_RX_BUFF) +
  486. ((2 * CFG_CACHELINE_SIZE) - 2));
  487. if (((int) hw_p->alloc_rx_buf & CACHELINE_MASK) != 0) {
  488. hw_p->rx =
  489. (mal_desc_t *) ((int) hw_p->alloc_rx_buf +
  490. CFG_CACHELINE_SIZE -
  491. ((int) hw_p->
  492. alloc_rx_buf & CACHELINE_MASK));
  493. } else {
  494. hw_p->rx = hw_p->alloc_rx_buf;
  495. }
  496. for (i = 0; i < NUM_TX_BUFF; i++) {
  497. hw_p->tx[i].ctrl = 0;
  498. hw_p->tx[i].data_len = 0;
  499. if (hw_p->first_init == 0)
  500. hw_p->txbuf_ptr =
  501. (char *) malloc (ENET_MAX_MTU_ALIGNED);
  502. hw_p->tx[i].data_ptr = hw_p->txbuf_ptr;
  503. if ((NUM_TX_BUFF - 1) == i)
  504. hw_p->tx[i].ctrl |= MAL_TX_CTRL_WRAP;
  505. hw_p->tx_run[i] = -1;
  506. #if 0
  507. printf ("TX_BUFF %d @ 0x%08lx\n", i,
  508. (ulong) hw_p->tx[i].data_ptr);
  509. #endif
  510. }
  511. for (i = 0; i < NUM_RX_BUFF; i++) {
  512. hw_p->rx[i].ctrl = 0;
  513. hw_p->rx[i].data_len = 0;
  514. /* rx[i].data_ptr = (char *) &rx_buff[i]; */
  515. hw_p->rx[i].data_ptr = (char *) NetRxPackets[i];
  516. if ((NUM_RX_BUFF - 1) == i)
  517. hw_p->rx[i].ctrl |= MAL_RX_CTRL_WRAP;
  518. hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY | MAL_RX_CTRL_INTR;
  519. hw_p->rx_ready[i] = -1;
  520. #if 0
  521. printf ("RX_BUFF %d @ 0x%08lx\n", i, (ulong) rx[i].data_ptr);
  522. #endif
  523. }
  524. reg = 0x00000000;
  525. reg |= dev->enetaddr[0]; /* set high address */
  526. reg = reg << 8;
  527. reg |= dev->enetaddr[1];
  528. out32 (EMAC_IAH + hw_p->hw_addr, reg);
  529. reg = 0x00000000;
  530. reg |= dev->enetaddr[2]; /* set low address */
  531. reg = reg << 8;
  532. reg |= dev->enetaddr[3];
  533. reg = reg << 8;
  534. reg |= dev->enetaddr[4];
  535. reg = reg << 8;
  536. reg |= dev->enetaddr[5];
  537. out32 (EMAC_IAL + hw_p->hw_addr, reg);
  538. switch (devnum) {
  539. case 1:
  540. /* setup MAL tx & rx channel pointers */
  541. mtdcr (maltxbattr, 0x0);
  542. mtdcr (maltxctp1r, hw_p->tx);
  543. mtdcr (malrxbattr, 0x0);
  544. mtdcr (malrxctp1r, hw_p->rx);
  545. /* set RX buffer size */
  546. mtdcr (malrcbs1, ENET_MAX_MTU_ALIGNED / 16);
  547. break;
  548. #if defined (CONFIG_440_GX)
  549. case 2:
  550. /* setup MAL tx & rx channel pointers */
  551. mtdcr (maltxbattr, 0x0);
  552. mtdcr (maltxctp2r, hw_p->tx);
  553. mtdcr (malrxbattr, 0x0);
  554. mtdcr (malrxctp2r, hw_p->rx);
  555. /* set RX buffer size */
  556. mtdcr (malrcbs2, ENET_MAX_MTU_ALIGNED / 16);
  557. break;
  558. case 3:
  559. /* setup MAL tx & rx channel pointers */
  560. mtdcr (maltxbattr, 0x0);
  561. mtdcr (maltxctp3r, hw_p->tx);
  562. mtdcr (malrxbattr, 0x0);
  563. mtdcr (malrxctp3r, hw_p->rx);
  564. /* set RX buffer size */
  565. mtdcr (malrcbs3, ENET_MAX_MTU_ALIGNED / 16);
  566. break;
  567. #endif /*CONFIG_440_GX */
  568. case 0:
  569. default:
  570. /* setup MAL tx & rx channel pointers */
  571. mtdcr (maltxbattr, 0x0);
  572. mtdcr (maltxctp0r, hw_p->tx);
  573. mtdcr (malrxbattr, 0x0);
  574. mtdcr (malrxctp0r, hw_p->rx);
  575. /* set RX buffer size */
  576. mtdcr (malrcbs0, ENET_MAX_MTU_ALIGNED / 16);
  577. break;
  578. }
  579. /* Enable MAL transmit and receive channels */
  580. mtdcr (maltxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
  581. mtdcr (malrxcasr, (MAL_TXRX_CASR >> hw_p->devnum));
  582. /* set transmit enable & receive enable */
  583. out32 (EMAC_M0 + hw_p->hw_addr, EMAC_M0_TXE | EMAC_M0_RXE);
  584. /* set receive fifo to 4k and tx fifo to 2k */
  585. mode_reg = in32 (EMAC_M1 + hw_p->hw_addr);
  586. mode_reg |= EMAC_M1_RFS_4K | EMAC_M1_TX_FIFO_2K;
  587. /* set speed */
  588. if (speed == _1000BASET)
  589. mode_reg = mode_reg | EMAC_M1_MF_1000MBPS | EMAC_M1_IST;
  590. else if (speed == _100BASET)
  591. mode_reg = mode_reg | EMAC_M1_MF_100MBPS | EMAC_M1_IST;
  592. else
  593. mode_reg = mode_reg & ~0x00C00000; /* 10 MBPS */
  594. if (duplex == FULL)
  595. mode_reg = mode_reg | 0x80000000 | EMAC_M1_IST;
  596. out32 (EMAC_M1 + hw_p->hw_addr, mode_reg);
  597. /* Enable broadcast and indvidual address */
  598. /* TBS: enabling runts as some misbehaved nics will send runts */
  599. out32 (EMAC_RXM + hw_p->hw_addr, EMAC_RMR_BAE | EMAC_RMR_IAE);
  600. /* we probably need to set the tx mode1 reg? maybe at tx time */
  601. /* set transmit request threshold register */
  602. out32 (EMAC_TRTR + hw_p->hw_addr, 0x18000000); /* 256 byte threshold */
  603. /* set receive low/high water mark register */
  604. /* 440GP has a 64 byte burst length */
  605. out32 (EMAC_RX_HI_LO_WMARK + hw_p->hw_addr, 0x80009000);
  606. out32 (EMAC_TXM1 + hw_p->hw_addr, 0xf8640000);
  607. /* Set fifo limit entry in tx mode 0 */
  608. out32 (EMAC_TXM0 + hw_p->hw_addr, 0x00000003);
  609. /* Frame gap set */
  610. out32 (EMAC_I_FRAME_GAP_REG + hw_p->hw_addr, 0x00000008);
  611. /* Set EMAC IER */
  612. hw_p->emac_ier = EMAC_ISR_PTLE | EMAC_ISR_BFCS |
  613. EMAC_ISR_PTLE | EMAC_ISR_ORE | EMAC_ISR_IRE;
  614. if (speed == _100BASET)
  615. hw_p->emac_ier = hw_p->emac_ier | EMAC_ISR_SYE;
  616. out32 (EMAC_ISR + hw_p->hw_addr, 0xffffffff); /* clear pending interrupts */
  617. out32 (EMAC_IER + hw_p->hw_addr, hw_p->emac_ier);
  618. if (hw_p->first_init == 0) {
  619. /*
  620. * Connect interrupt service routines
  621. */
  622. irq_install_handler (VECNUM_EWU0 + (hw_p->devnum * 2),
  623. (interrupt_handler_t *) enetInt, dev);
  624. irq_install_handler (VECNUM_ETH0 + (hw_p->devnum * 2),
  625. (interrupt_handler_t *) enetInt, dev);
  626. }
  627. mtmsr (msr); /* enable interrupts again */
  628. hw_p->bis = bis;
  629. hw_p->first_init = 1;
  630. return (1);
  631. }
  632. static int ppc_440x_eth_send (struct eth_device *dev, volatile void *ptr,
  633. int len)
  634. {
  635. struct enet_frame *ef_ptr;
  636. ulong time_start, time_now;
  637. unsigned long temp_txm0;
  638. EMAC_440GX_HW_PST hw_p = dev->priv;
  639. ef_ptr = (struct enet_frame *) ptr;
  640. /*-----------------------------------------------------------------------+
  641. * Copy in our address into the frame.
  642. *-----------------------------------------------------------------------*/
  643. (void) memcpy (ef_ptr->source_addr, dev->enetaddr, ENET_ADDR_LENGTH);
  644. /*-----------------------------------------------------------------------+
  645. * If frame is too long or too short, modify length.
  646. *-----------------------------------------------------------------------*/
  647. /* TBS: where does the fragment go???? */
  648. if (len > ENET_MAX_MTU)
  649. len = ENET_MAX_MTU;
  650. /* memcpy ((void *) &tx_buff[tx_slot], (const void *) ptr, len); */
  651. memcpy ((void *) hw_p->txbuf_ptr, (const void *) ptr, len);
  652. /*-----------------------------------------------------------------------+
  653. * set TX Buffer busy, and send it
  654. *-----------------------------------------------------------------------*/
  655. hw_p->tx[hw_p->tx_slot].ctrl = (MAL_TX_CTRL_LAST |
  656. EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP) &
  657. ~(EMAC_TX_CTRL_ISA | EMAC_TX_CTRL_RSA);
  658. if ((NUM_TX_BUFF - 1) == hw_p->tx_slot)
  659. hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_WRAP;
  660. hw_p->tx[hw_p->tx_slot].data_len = (short) len;
  661. hw_p->tx[hw_p->tx_slot].ctrl |= MAL_TX_CTRL_READY;
  662. __asm__ volatile ("eieio");
  663. out32 (EMAC_TXM0 + hw_p->hw_addr,
  664. in32 (EMAC_TXM0 + hw_p->hw_addr) | EMAC_TXM0_GNP0);
  665. #ifdef INFO_440_ENET
  666. hw_p->stats.pkts_tx++;
  667. #endif
  668. /*-----------------------------------------------------------------------+
  669. * poll unitl the packet is sent and then make sure it is OK
  670. *-----------------------------------------------------------------------*/
  671. time_start = get_timer (0);
  672. while (1) {
  673. temp_txm0 = in32 (EMAC_TXM0 + hw_p->hw_addr);
  674. /* loop until either TINT turns on or 3 seconds elapse */
  675. if ((temp_txm0 & EMAC_TXM0_GNP0) != 0) {
  676. /* transmit is done, so now check for errors
  677. * If there is an error, an interrupt should
  678. * happen when we return
  679. */
  680. time_now = get_timer (0);
  681. if ((time_now - time_start) > 3000) {
  682. return (-1);
  683. }
  684. } else {
  685. return (len);
  686. }
  687. }
  688. }
  689. int enetInt (struct eth_device *dev)
  690. {
  691. int serviced;
  692. int rc = -1; /* default to not us */
  693. unsigned long mal_isr;
  694. unsigned long emac_isr = 0;
  695. unsigned long mal_rx_eob;
  696. unsigned long my_uic0msr, my_uic1msr;
  697. #if defined(CONFIG_440_GX)
  698. unsigned long my_uic2msr;
  699. #endif
  700. EMAC_440GX_HW_PST hw_p;
  701. /*
  702. * Because the mal is generic, we need to get the current
  703. * eth device
  704. */
  705. dev = eth_get_dev ();
  706. hw_p = dev->priv;
  707. /* enter loop that stays in interrupt code until nothing to service */
  708. do {
  709. serviced = 0;
  710. my_uic0msr = mfdcr (uic0msr);
  711. my_uic1msr = mfdcr (uic1msr);
  712. #if defined(CONFIG_440_GX)
  713. my_uic2msr = mfdcr (uic2msr);
  714. #endif
  715. if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
  716. && !(my_uic1msr &
  717. (UIC_ETH0 | UIC_ETH1 | UIC_MS | UIC_MTDE |
  718. UIC_MRDE))) {
  719. /* not for us */
  720. return (rc);
  721. }
  722. #if defined (CONFIG_440_GX)
  723. if (!(my_uic0msr & (UIC_MRE | UIC_MTE))
  724. && !(my_uic2msr & (UIC_ETH2 | UIC_ETH3))) {
  725. /* not for us */
  726. return (rc);
  727. }
  728. #endif
  729. /* get and clear controller status interrupts */
  730. /* look at Mal and EMAC interrupts */
  731. if ((my_uic0msr & (UIC_MRE | UIC_MTE))
  732. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  733. /* we have a MAL interrupt */
  734. mal_isr = mfdcr (malesr);
  735. /* look for mal error */
  736. if (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE)) {
  737. mal_err (dev, mal_isr, my_uic0msr,
  738. MAL_UIC_DEF, MAL_UIC_ERR);
  739. serviced = 1;
  740. rc = 0;
  741. }
  742. }
  743. /* port by port dispatch of emac interrupts */
  744. if (hw_p->devnum == 0) {
  745. if (UIC_ETH0 & my_uic1msr) { /* look for EMAC errors */
  746. emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
  747. if ((hw_p->emac_ier & emac_isr) != 0) {
  748. emac_err (dev, emac_isr);
  749. serviced = 1;
  750. rc = 0;
  751. }
  752. }
  753. if ((hw_p->emac_ier & emac_isr)
  754. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  755. mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
  756. mtdcr (uic1sr, UIC_ETH0 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  757. return (rc); /* we had errors so get out */
  758. }
  759. }
  760. if (hw_p->devnum == 1) {
  761. if (UIC_ETH1 & my_uic1msr) { /* look for EMAC errors */
  762. emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
  763. if ((hw_p->emac_ier & emac_isr) != 0) {
  764. emac_err (dev, emac_isr);
  765. serviced = 1;
  766. rc = 0;
  767. }
  768. }
  769. if ((hw_p->emac_ier & emac_isr)
  770. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  771. mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
  772. mtdcr (uic1sr, UIC_ETH1 | UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  773. return (rc); /* we had errors so get out */
  774. }
  775. }
  776. #if defined (CONFIG_440_GX)
  777. if (hw_p->devnum == 2) {
  778. if (UIC_ETH2 & my_uic2msr) { /* look for EMAC errors */
  779. emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
  780. if ((hw_p->emac_ier & emac_isr) != 0) {
  781. emac_err (dev, emac_isr);
  782. serviced = 1;
  783. rc = 0;
  784. }
  785. }
  786. if ((hw_p->emac_ier & emac_isr)
  787. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  788. mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
  789. mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  790. mtdcr (uic2sr, UIC_ETH2);
  791. return (rc); /* we had errors so get out */
  792. }
  793. }
  794. if (hw_p->devnum == 3) {
  795. if (UIC_ETH3 & my_uic2msr) { /* look for EMAC errors */
  796. emac_isr = in32 (EMAC_ISR + hw_p->hw_addr);
  797. if ((hw_p->emac_ier & emac_isr) != 0) {
  798. emac_err (dev, emac_isr);
  799. serviced = 1;
  800. rc = 0;
  801. }
  802. }
  803. if ((hw_p->emac_ier & emac_isr)
  804. || (my_uic1msr & (UIC_MS | UIC_MTDE | UIC_MRDE))) {
  805. mtdcr (uic0sr, UIC_MRE | UIC_MTE); /* Clear */
  806. mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  807. mtdcr (uic2sr, UIC_ETH3);
  808. return (rc); /* we had errors so get out */
  809. }
  810. }
  811. #endif /* CONFIG_440_GX */
  812. /* handle MAX TX EOB interrupt from a tx */
  813. if (my_uic0msr & UIC_MTE) {
  814. mal_rx_eob = mfdcr (maltxeobisr);
  815. mtdcr (maltxeobisr, mal_rx_eob);
  816. mtdcr (uic0sr, UIC_MTE);
  817. }
  818. /* handle MAL RX EOB interupt from a receive */
  819. /* check for EOB on valid channels */
  820. if (my_uic0msr & UIC_MRE) {
  821. mal_rx_eob = mfdcr (malrxeobisr);
  822. if ((mal_rx_eob & (0x80000000 >> hw_p->devnum)) != 0) { /* call emac routine for channel x */
  823. /* clear EOB
  824. mtdcr(malrxeobisr, mal_rx_eob); */
  825. enet_rcv (dev, emac_isr);
  826. /* indicate that we serviced an interrupt */
  827. serviced = 1;
  828. rc = 0;
  829. }
  830. }
  831. mtdcr (uic0sr, UIC_MRE); /* Clear */
  832. mtdcr (uic1sr, UIC_MS | UIC_MTDE | UIC_MRDE); /* Clear */
  833. switch (hw_p->devnum) {
  834. case 0:
  835. mtdcr (uic1sr, UIC_ETH0);
  836. break;
  837. case 1:
  838. mtdcr (uic1sr, UIC_ETH1);
  839. break;
  840. #if defined (CONFIG_440_GX)
  841. case 2:
  842. mtdcr (uic2sr, UIC_ETH2);
  843. break;
  844. case 3:
  845. mtdcr (uic2sr, UIC_ETH3);
  846. break;
  847. #endif /* CONFIG_440_GX */
  848. default:
  849. break;
  850. }
  851. } while (serviced);
  852. return (rc);
  853. }
  854. /*-----------------------------------------------------------------------------+
  855. * MAL Error Routine
  856. *-----------------------------------------------------------------------------*/
  857. static void mal_err (struct eth_device *dev, unsigned long isr,
  858. unsigned long uic, unsigned long maldef,
  859. unsigned long mal_errr)
  860. {
  861. EMAC_440GX_HW_PST hw_p = dev->priv;
  862. mtdcr (malesr, isr); /* clear interrupt */
  863. /* clear DE interrupt */
  864. mtdcr (maltxdeir, 0xC0000000);
  865. mtdcr (malrxdeir, 0x80000000);
  866. #ifdef INFO_440_ENET
  867. printf ("\nMAL error occured.... ISR = %lx UIC = = %lx MAL_DEF = %lx MAL_ERR= %lx \n", isr, uic, maldef, mal_errr);
  868. #endif
  869. eth_init (hw_p->bis); /* start again... */
  870. }
  871. /*-----------------------------------------------------------------------------+
  872. * EMAC Error Routine
  873. *-----------------------------------------------------------------------------*/
  874. static void emac_err (struct eth_device *dev, unsigned long isr)
  875. {
  876. EMAC_440GX_HW_PST hw_p = dev->priv;
  877. printf ("EMAC%d error occured.... ISR = %lx\n", hw_p->devnum, isr);
  878. out32 (EMAC_ISR + hw_p->hw_addr, isr);
  879. }
  880. /*-----------------------------------------------------------------------------+
  881. * enet_rcv() handles the ethernet receive data
  882. *-----------------------------------------------------------------------------*/
  883. static void enet_rcv (struct eth_device *dev, unsigned long malisr)
  884. {
  885. struct enet_frame *ef_ptr;
  886. unsigned long data_len;
  887. unsigned long rx_eob_isr;
  888. EMAC_440GX_HW_PST hw_p = dev->priv;
  889. int handled = 0;
  890. int i;
  891. int loop_count = 0;
  892. rx_eob_isr = mfdcr (malrxeobisr);
  893. if ((0x80000000 >> hw_p->devnum) & rx_eob_isr) {
  894. /* clear EOB */
  895. mtdcr (malrxeobisr, rx_eob_isr);
  896. /* EMAC RX done */
  897. while (1) { /* do all */
  898. i = hw_p->rx_slot;
  899. if ((MAL_RX_CTRL_EMPTY & hw_p->rx[i].ctrl)
  900. || (loop_count >= NUM_RX_BUFF))
  901. break;
  902. loop_count++;
  903. hw_p->rx_slot++;
  904. if (NUM_RX_BUFF == hw_p->rx_slot)
  905. hw_p->rx_slot = 0;
  906. handled++;
  907. data_len = (unsigned long) hw_p->rx[i].data_len; /* Get len */
  908. if (data_len) {
  909. if (data_len > ENET_MAX_MTU) /* Check len */
  910. data_len = 0;
  911. else {
  912. if (EMAC_RX_ERRORS & hw_p->rx[i].ctrl) { /* Check Errors */
  913. data_len = 0;
  914. hw_p->stats.rx_err_log[hw_p->
  915. rx_err_index]
  916. = hw_p->rx[i].ctrl;
  917. hw_p->rx_err_index++;
  918. if (hw_p->rx_err_index ==
  919. MAX_ERR_LOG)
  920. hw_p->rx_err_index =
  921. 0;
  922. } /* emac_erros */
  923. } /* data_len < max mtu */
  924. } /* if data_len */
  925. if (!data_len) { /* no data */
  926. hw_p->rx[i].ctrl |= MAL_RX_CTRL_EMPTY; /* Free Recv Buffer */
  927. hw_p->stats.data_len_err++; /* Error at Rx */
  928. }
  929. /* !data_len */
  930. /* AS.HARNOIS */
  931. /* Check if user has already eaten buffer */
  932. /* if not => ERROR */
  933. else if (hw_p->rx_ready[hw_p->rx_i_index] != -1) {
  934. if (hw_p->is_receiving)
  935. printf ("ERROR : Receive buffers are full!\n");
  936. break;
  937. } else {
  938. hw_p->stats.rx_frames++;
  939. hw_p->stats.rx += data_len;
  940. ef_ptr = (struct enet_frame *) hw_p->rx[i].
  941. data_ptr;
  942. #ifdef INFO_440_ENET
  943. hw_p->stats.pkts_rx++;
  944. #endif
  945. /* AS.HARNOIS
  946. * use ring buffer
  947. */
  948. hw_p->rx_ready[hw_p->rx_i_index] = i;
  949. hw_p->rx_i_index++;
  950. if (NUM_RX_BUFF == hw_p->rx_i_index)
  951. hw_p->rx_i_index = 0;
  952. /* printf("X"); /|* test-only *|/ */
  953. /* AS.HARNOIS
  954. * free receive buffer only when
  955. * buffer has been handled (eth_rx)
  956. rx[i].ctrl |= MAL_RX_CTRL_EMPTY;
  957. */
  958. } /* if data_len */
  959. } /* while */
  960. } /* if EMACK_RXCHL */
  961. }
  962. static int ppc_440x_eth_rx (struct eth_device *dev)
  963. {
  964. int length;
  965. int user_index;
  966. unsigned long msr;
  967. EMAC_440GX_HW_PST hw_p = dev->priv;
  968. hw_p->is_receiving = 1; /* tell driver */
  969. for (;;) {
  970. /* AS.HARNOIS
  971. * use ring buffer and
  972. * get index from rx buffer desciptor queue
  973. */
  974. user_index = hw_p->rx_ready[hw_p->rx_u_index];
  975. if (user_index == -1) {
  976. length = -1;
  977. break; /* nothing received - leave for() loop */
  978. }
  979. msr = mfmsr ();
  980. mtmsr (msr & ~(MSR_EE));
  981. length = hw_p->rx[user_index].data_len;
  982. /* Pass the packet up to the protocol layers. */
  983. /* NetReceive(NetRxPackets[rxIdx], length - 4); */
  984. /* NetReceive(NetRxPackets[i], length); */
  985. NetReceive (NetRxPackets[user_index], length - 4);
  986. /* Free Recv Buffer */
  987. hw_p->rx[user_index].ctrl |= MAL_RX_CTRL_EMPTY;
  988. /* Free rx buffer descriptor queue */
  989. hw_p->rx_ready[hw_p->rx_u_index] = -1;
  990. hw_p->rx_u_index++;
  991. if (NUM_RX_BUFF == hw_p->rx_u_index)
  992. hw_p->rx_u_index = 0;
  993. #ifdef INFO_440_ENET
  994. hw_p->stats.pkts_handled++;
  995. #endif
  996. mtmsr (msr); /* Enable IRQ's */
  997. }
  998. hw_p->is_receiving = 0; /* tell driver */
  999. return length;
  1000. }
  1001. int ppc_440x_eth_initialize (bd_t * bis)
  1002. {
  1003. static int virgin = 0;
  1004. unsigned long pfc1;
  1005. struct eth_device *dev;
  1006. int eth_num = 0;
  1007. EMAC_440GX_HW_PST hw = NULL;
  1008. mfsdr (sdr_pfc1, pfc1);
  1009. pfc1 &= ~(0x01e00000);
  1010. pfc1 |= 0x01200000;
  1011. mtsdr (sdr_pfc1, pfc1);
  1012. /* set phy num and mode */
  1013. bis->bi_phynum[0] = CONFIG_PHY_ADDR;
  1014. bis->bi_phynum[1] = CONFIG_PHY1_ADDR;
  1015. bis->bi_phynum[2] = CONFIG_PHY2_ADDR;
  1016. bis->bi_phynum[3] = CONFIG_PHY3_ADDR;
  1017. bis->bi_phymode[0] = 0;
  1018. bis->bi_phymode[1] = 0;
  1019. bis->bi_phymode[2] = 2;
  1020. bis->bi_phymode[3] = 2;
  1021. for (eth_num = 0; eth_num < EMAC_NUM_DEV; eth_num++) {
  1022. /* See if we can actually bring up the interface, otherwise, skip it */
  1023. switch (eth_num) {
  1024. case 0:
  1025. if (memcmp (bis->bi_enetaddr, "\0\0\0\0\0\0", 6) == 0) {
  1026. bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
  1027. continue;
  1028. }
  1029. break;
  1030. case 1:
  1031. if (memcmp (bis->bi_enet1addr, "\0\0\0\0\0\0", 6) == 0) {
  1032. bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
  1033. continue;
  1034. }
  1035. break;
  1036. case 2:
  1037. if (memcmp (bis->bi_enet2addr, "\0\0\0\0\0\0", 6) == 0) {
  1038. bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
  1039. continue;
  1040. }
  1041. break;
  1042. case 3:
  1043. if (memcmp (bis->bi_enet3addr, "\0\0\0\0\0\0", 6) == 0) {
  1044. bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
  1045. continue;
  1046. }
  1047. break;
  1048. default:
  1049. if (memcmp (bis->bi_enetaddr, "\0\0\0\0\0\0", 6) == 0) {
  1050. bis->bi_phymode[eth_num] = BI_PHYMODE_NONE;
  1051. continue;
  1052. }
  1053. break;
  1054. }
  1055. /* Allocate device structure */
  1056. dev = (struct eth_device *) malloc (sizeof (*dev));
  1057. if (dev == NULL) {
  1058. printf ("ppc_440x_eth_initialize: "
  1059. "Cannot allocate eth_device %d\n", eth_num);
  1060. return (-1);
  1061. }
  1062. /* Allocate our private use data */
  1063. hw = (EMAC_440GX_HW_PST) malloc (sizeof (*hw));
  1064. if (hw == NULL) {
  1065. printf ("ppc_440x_eth_initialize: "
  1066. "Cannot allocate private hw data for eth_device %d",
  1067. eth_num);
  1068. free (dev);
  1069. return (-1);
  1070. }
  1071. switch (eth_num) {
  1072. case 0:
  1073. hw->hw_addr = 0;
  1074. memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
  1075. break;
  1076. case 1:
  1077. hw->hw_addr = 0x100;
  1078. memcpy (dev->enetaddr, bis->bi_enet1addr, 6);
  1079. break;
  1080. case 2:
  1081. hw->hw_addr = 0x400;
  1082. memcpy (dev->enetaddr, bis->bi_enet2addr, 6);
  1083. break;
  1084. case 3:
  1085. hw->hw_addr = 0x600;
  1086. memcpy (dev->enetaddr, bis->bi_enet3addr, 6);
  1087. break;
  1088. default:
  1089. hw->hw_addr = 0;
  1090. memcpy (dev->enetaddr, bis->bi_enetaddr, 6);
  1091. break;
  1092. }
  1093. hw->devnum = eth_num;
  1094. sprintf (dev->name, "ppc_440x_eth%d", eth_num);
  1095. dev->priv = (void *) hw;
  1096. dev->init = ppc_440x_eth_init;
  1097. dev->halt = ppc_440x_eth_halt;
  1098. dev->send = ppc_440x_eth_send;
  1099. dev->recv = ppc_440x_eth_rx;
  1100. if (0 == virgin) {
  1101. /* set the MAL IER ??? names may change with new spec ??? */
  1102. mal_ier =
  1103. MAL_IER_DE | MAL_IER_NE | MAL_IER_TE |
  1104. MAL_IER_OPBE | MAL_IER_PLBE;
  1105. mtdcr (malesr, 0xffffffff); /* clear pending interrupts */
  1106. mtdcr (maltxdeir, 0xffffffff); /* clear pending interrupts */
  1107. mtdcr (malrxdeir, 0xffffffff); /* clear pending interrupts */
  1108. mtdcr (malier, mal_ier);
  1109. /* install MAL interrupt handler */
  1110. irq_install_handler (VECNUM_MS,
  1111. (interrupt_handler_t *) enetInt,
  1112. dev);
  1113. irq_install_handler (VECNUM_MTE,
  1114. (interrupt_handler_t *) enetInt,
  1115. dev);
  1116. irq_install_handler (VECNUM_MRE,
  1117. (interrupt_handler_t *) enetInt,
  1118. dev);
  1119. irq_install_handler (VECNUM_TXDE,
  1120. (interrupt_handler_t *) enetInt,
  1121. dev);
  1122. irq_install_handler (VECNUM_RXDE,
  1123. (interrupt_handler_t *) enetInt,
  1124. dev);
  1125. virgin = 1;
  1126. }
  1127. eth_register (dev);
  1128. } /* end for each supported device */
  1129. return (1);
  1130. }
  1131. #endif /* CONFIG_440 && CONFIG_NET_MULTI */