4xx_enet.c 49 KB

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