4xx_enet.c 42 KB

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