4xx_enet.c 43 KB

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