4xx_enet.c 49 KB

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