4xx_enet.c 43 KB

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