4xx_enet.c 42 KB

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