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