440gx_enet.c 39 KB

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