fec.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. /*
  2. * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
  3. * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
  4. *
  5. * This version of the driver is specific to the FADS implementation,
  6. * since the board contains control registers external to the processor
  7. * for the control of the LevelOne LXT970 transceiver. The MPC860T manual
  8. * describes connections using the internal parallel port I/O, which
  9. * is basically all of Port D.
  10. *
  11. * Includes support for the following PHYs: QS6612, LXT970, LXT971/2.
  12. *
  13. * Right now, I am very wasteful with the buffers. I allocate memory
  14. * pages and then divide them into 2K frame buffers. This way I know I
  15. * have buffers large enough to hold one frame within one buffer descriptor.
  16. * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  17. * will be much more memory efficient and will easily handle lots of
  18. * small packets.
  19. *
  20. * Much better multiple PHY support by Magnus Damm.
  21. * Copyright (c) 2000 Ericsson Radio Systems AB.
  22. *
  23. * Make use of MII for PHY control configurable.
  24. * Some fixes.
  25. * Copyright (c) 2000-2002 Wolfgang Denk, DENX Software Engineering.
  26. *
  27. * Support for AMD AM79C874 added.
  28. * Thomas Lange, thomas@corelatus.com
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/sched.h>
  32. #include <linux/string.h>
  33. #include <linux/ptrace.h>
  34. #include <linux/errno.h>
  35. #include <linux/ioport.h>
  36. #include <linux/slab.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/pci.h>
  39. #include <linux/init.h>
  40. #include <linux/delay.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/bitops.h>
  46. #ifdef CONFIG_FEC_PACKETHOOK
  47. #include <linux/pkthook.h>
  48. #endif
  49. #include <asm/8xx_immap.h>
  50. #include <asm/pgtable.h>
  51. #include <asm/mpc8xx.h>
  52. #include <asm/irq.h>
  53. #include <asm/uaccess.h>
  54. #include <asm/commproc.h>
  55. #ifdef CONFIG_USE_MDIO
  56. /* Forward declarations of some structures to support different PHYs
  57. */
  58. typedef struct {
  59. uint mii_data;
  60. void (*funct)(uint mii_reg, struct net_device *dev);
  61. } phy_cmd_t;
  62. typedef struct {
  63. uint id;
  64. char *name;
  65. const phy_cmd_t *config;
  66. const phy_cmd_t *startup;
  67. const phy_cmd_t *ack_int;
  68. const phy_cmd_t *shutdown;
  69. } phy_info_t;
  70. #endif /* CONFIG_USE_MDIO */
  71. /* The number of Tx and Rx buffers. These are allocated from the page
  72. * pool. The code may assume these are power of two, so it is best
  73. * to keep them that size.
  74. * We don't need to allocate pages for the transmitter. We just use
  75. * the skbuffer directly.
  76. */
  77. #ifdef CONFIG_ENET_BIG_BUFFERS
  78. #define FEC_ENET_RX_PAGES 16
  79. #define FEC_ENET_RX_FRSIZE 2048
  80. #define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
  81. #define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
  82. #define TX_RING_SIZE 16 /* Must be power of two */
  83. #define TX_RING_MOD_MASK 15 /* for this to work */
  84. #else
  85. #define FEC_ENET_RX_PAGES 4
  86. #define FEC_ENET_RX_FRSIZE 2048
  87. #define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
  88. #define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
  89. #define TX_RING_SIZE 8 /* Must be power of two */
  90. #define TX_RING_MOD_MASK 7 /* for this to work */
  91. #endif
  92. /* Interrupt events/masks.
  93. */
  94. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  95. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  96. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  97. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  98. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  99. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  100. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  101. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  102. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  103. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  104. /*
  105. */
  106. #define FEC_ECNTRL_PINMUX 0x00000004
  107. #define FEC_ECNTRL_ETHER_EN 0x00000002
  108. #define FEC_ECNTRL_RESET 0x00000001
  109. #define FEC_RCNTRL_BC_REJ 0x00000010
  110. #define FEC_RCNTRL_PROM 0x00000008
  111. #define FEC_RCNTRL_MII_MODE 0x00000004
  112. #define FEC_RCNTRL_DRT 0x00000002
  113. #define FEC_RCNTRL_LOOP 0x00000001
  114. #define FEC_TCNTRL_FDEN 0x00000004
  115. #define FEC_TCNTRL_HBC 0x00000002
  116. #define FEC_TCNTRL_GTS 0x00000001
  117. /* Delay to wait for FEC reset command to complete (in us)
  118. */
  119. #define FEC_RESET_DELAY 50
  120. /* The FEC stores dest/src/type, data, and checksum for receive packets.
  121. */
  122. #define PKT_MAXBUF_SIZE 1518
  123. #define PKT_MINBUF_SIZE 64
  124. #define PKT_MAXBLR_SIZE 1520
  125. /* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
  126. * tx_bd_base always point to the base of the buffer descriptors. The
  127. * cur_rx and cur_tx point to the currently available buffer.
  128. * The dirty_tx tracks the current buffer that is being sent by the
  129. * controller. The cur_tx and dirty_tx are equal under both completely
  130. * empty and completely full conditions. The empty/ready indicator in
  131. * the buffer descriptor determines the actual condition.
  132. */
  133. struct fec_enet_private {
  134. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  135. struct sk_buff* tx_skbuff[TX_RING_SIZE];
  136. ushort skb_cur;
  137. ushort skb_dirty;
  138. /* CPM dual port RAM relative addresses.
  139. */
  140. cbd_t *rx_bd_base; /* Address of Rx and Tx buffers. */
  141. cbd_t *tx_bd_base;
  142. cbd_t *cur_rx, *cur_tx; /* The next free ring entry */
  143. cbd_t *dirty_tx; /* The ring entries to be free()ed. */
  144. /* Virtual addresses for the receive buffers because we can't
  145. * do a __va() on them anymore.
  146. */
  147. unsigned char *rx_vaddr[RX_RING_SIZE];
  148. struct net_device_stats stats;
  149. uint tx_full;
  150. spinlock_t lock;
  151. #ifdef CONFIG_USE_MDIO
  152. uint phy_id;
  153. uint phy_id_done;
  154. uint phy_status;
  155. uint phy_speed;
  156. phy_info_t *phy;
  157. struct work_struct phy_task;
  158. struct net_device *dev;
  159. uint sequence_done;
  160. uint phy_addr;
  161. #endif /* CONFIG_USE_MDIO */
  162. int link;
  163. int old_link;
  164. int full_duplex;
  165. #ifdef CONFIG_FEC_PACKETHOOK
  166. unsigned long ph_lock;
  167. fec_ph_func *ph_rxhandler;
  168. fec_ph_func *ph_txhandler;
  169. __u16 ph_proto;
  170. volatile __u32 *ph_regaddr;
  171. void *ph_priv;
  172. #endif
  173. };
  174. static int fec_enet_open(struct net_device *dev);
  175. static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
  176. #ifdef CONFIG_USE_MDIO
  177. static void fec_enet_mii(struct net_device *dev);
  178. #endif /* CONFIG_USE_MDIO */
  179. static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
  180. #ifdef CONFIG_FEC_PACKETHOOK
  181. static void fec_enet_tx(struct net_device *dev, __u32 regval);
  182. static void fec_enet_rx(struct net_device *dev, __u32 regval);
  183. #else
  184. static void fec_enet_tx(struct net_device *dev);
  185. static void fec_enet_rx(struct net_device *dev);
  186. #endif
  187. static int fec_enet_close(struct net_device *dev);
  188. static struct net_device_stats *fec_enet_get_stats(struct net_device *dev);
  189. static void set_multicast_list(struct net_device *dev);
  190. static void fec_restart(struct net_device *dev, int duplex);
  191. static void fec_stop(struct net_device *dev);
  192. static ushort my_enet_addr[3];
  193. #ifdef CONFIG_USE_MDIO
  194. /* MII processing. We keep this as simple as possible. Requests are
  195. * placed on the list (if there is room). When the request is finished
  196. * by the MII, an optional function may be called.
  197. */
  198. typedef struct mii_list {
  199. uint mii_regval;
  200. void (*mii_func)(uint val, struct net_device *dev);
  201. struct mii_list *mii_next;
  202. } mii_list_t;
  203. #define NMII 20
  204. mii_list_t mii_cmds[NMII];
  205. mii_list_t *mii_free;
  206. mii_list_t *mii_head;
  207. mii_list_t *mii_tail;
  208. static int mii_queue(struct net_device *dev, int request,
  209. void (*func)(uint, struct net_device *));
  210. /* Make MII read/write commands for the FEC.
  211. */
  212. #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
  213. #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
  214. (VAL & 0xffff))
  215. #define mk_mii_end 0
  216. #endif /* CONFIG_USE_MDIO */
  217. /* Transmitter timeout.
  218. */
  219. #define TX_TIMEOUT (2*HZ)
  220. #ifdef CONFIG_USE_MDIO
  221. /* Register definitions for the PHY.
  222. */
  223. #define MII_REG_CR 0 /* Control Register */
  224. #define MII_REG_SR 1 /* Status Register */
  225. #define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
  226. #define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
  227. #define MII_REG_ANAR 4 /* A-N Advertisement Register */
  228. #define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
  229. #define MII_REG_ANER 6 /* A-N Expansion Register */
  230. #define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
  231. #define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
  232. /* values for phy_status */
  233. #define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
  234. #define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
  235. #define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
  236. #define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
  237. #define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
  238. #define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
  239. #define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
  240. #define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
  241. #define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
  242. #define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
  243. #define PHY_STAT_SPMASK 0xf000 /* mask for speed */
  244. #define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
  245. #define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
  246. #define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
  247. #define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
  248. #endif /* CONFIG_USE_MDIO */
  249. #ifdef CONFIG_FEC_PACKETHOOK
  250. int
  251. fec_register_ph(struct net_device *dev, fec_ph_func *rxfun, fec_ph_func *txfun,
  252. __u16 proto, volatile __u32 *regaddr, void *priv)
  253. {
  254. struct fec_enet_private *fep;
  255. int retval = 0;
  256. fep = dev->priv;
  257. if (test_and_set_bit(0, (void*)&fep->ph_lock) != 0) {
  258. /* Someone is messing with the packet hook */
  259. return -EAGAIN;
  260. }
  261. if (fep->ph_rxhandler != NULL || fep->ph_txhandler != NULL) {
  262. retval = -EBUSY;
  263. goto out;
  264. }
  265. fep->ph_rxhandler = rxfun;
  266. fep->ph_txhandler = txfun;
  267. fep->ph_proto = proto;
  268. fep->ph_regaddr = regaddr;
  269. fep->ph_priv = priv;
  270. out:
  271. fep->ph_lock = 0;
  272. return retval;
  273. }
  274. int
  275. fec_unregister_ph(struct net_device *dev)
  276. {
  277. struct fec_enet_private *fep;
  278. int retval = 0;
  279. fep = dev->priv;
  280. if (test_and_set_bit(0, (void*)&fep->ph_lock) != 0) {
  281. /* Someone is messing with the packet hook */
  282. return -EAGAIN;
  283. }
  284. fep->ph_rxhandler = fep->ph_txhandler = NULL;
  285. fep->ph_proto = 0;
  286. fep->ph_regaddr = NULL;
  287. fep->ph_priv = NULL;
  288. fep->ph_lock = 0;
  289. return retval;
  290. }
  291. EXPORT_SYMBOL(fec_register_ph);
  292. EXPORT_SYMBOL(fec_unregister_ph);
  293. #endif /* CONFIG_FEC_PACKETHOOK */
  294. static int
  295. fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  296. {
  297. struct fec_enet_private *fep;
  298. volatile fec_t *fecp;
  299. volatile cbd_t *bdp;
  300. fep = dev->priv;
  301. fecp = (volatile fec_t*)dev->base_addr;
  302. if (!fep->link) {
  303. /* Link is down or autonegotiation is in progress. */
  304. return 1;
  305. }
  306. /* Fill in a Tx ring entry */
  307. bdp = fep->cur_tx;
  308. #ifndef final_version
  309. if (bdp->cbd_sc & BD_ENET_TX_READY) {
  310. /* Ooops. All transmit buffers are full. Bail out.
  311. * This should not happen, since dev->tbusy should be set.
  312. */
  313. printk("%s: tx queue full!.\n", dev->name);
  314. return 1;
  315. }
  316. #endif
  317. /* Clear all of the status flags.
  318. */
  319. bdp->cbd_sc &= ~BD_ENET_TX_STATS;
  320. /* Set buffer length and buffer pointer.
  321. */
  322. bdp->cbd_bufaddr = __pa(skb->data);
  323. bdp->cbd_datlen = skb->len;
  324. /* Save skb pointer.
  325. */
  326. fep->tx_skbuff[fep->skb_cur] = skb;
  327. fep->stats.tx_bytes += skb->len;
  328. fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
  329. /* Push the data cache so the CPM does not get stale memory
  330. * data.
  331. */
  332. flush_dcache_range((unsigned long)skb->data,
  333. (unsigned long)skb->data + skb->len);
  334. /* disable interrupts while triggering transmit */
  335. spin_lock_irq(&fep->lock);
  336. /* Send it on its way. Tell FEC its ready, interrupt when done,
  337. * its the last BD of the frame, and to put the CRC on the end.
  338. */
  339. bdp->cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
  340. | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  341. dev->trans_start = jiffies;
  342. /* Trigger transmission start */
  343. fecp->fec_x_des_active = 0x01000000;
  344. /* If this was the last BD in the ring, start at the beginning again.
  345. */
  346. if (bdp->cbd_sc & BD_ENET_TX_WRAP) {
  347. bdp = fep->tx_bd_base;
  348. } else {
  349. bdp++;
  350. }
  351. if (bdp->cbd_sc & BD_ENET_TX_READY) {
  352. netif_stop_queue(dev);
  353. fep->tx_full = 1;
  354. }
  355. fep->cur_tx = (cbd_t *)bdp;
  356. spin_unlock_irq(&fep->lock);
  357. return 0;
  358. }
  359. static void
  360. fec_timeout(struct net_device *dev)
  361. {
  362. struct fec_enet_private *fep = dev->priv;
  363. printk("%s: transmit timed out.\n", dev->name);
  364. fep->stats.tx_errors++;
  365. #ifndef final_version
  366. {
  367. int i;
  368. cbd_t *bdp;
  369. printk("Ring data dump: cur_tx %lx%s, dirty_tx %lx cur_rx: %lx\n",
  370. (unsigned long)fep->cur_tx, fep->tx_full ? " (full)" : "",
  371. (unsigned long)fep->dirty_tx,
  372. (unsigned long)fep->cur_rx);
  373. bdp = fep->tx_bd_base;
  374. printk(" tx: %u buffers\n", TX_RING_SIZE);
  375. for (i = 0 ; i < TX_RING_SIZE; i++) {
  376. printk(" %08x: %04x %04x %08x\n",
  377. (uint) bdp,
  378. bdp->cbd_sc,
  379. bdp->cbd_datlen,
  380. bdp->cbd_bufaddr);
  381. bdp++;
  382. }
  383. bdp = fep->rx_bd_base;
  384. printk(" rx: %lu buffers\n", RX_RING_SIZE);
  385. for (i = 0 ; i < RX_RING_SIZE; i++) {
  386. printk(" %08x: %04x %04x %08x\n",
  387. (uint) bdp,
  388. bdp->cbd_sc,
  389. bdp->cbd_datlen,
  390. bdp->cbd_bufaddr);
  391. bdp++;
  392. }
  393. }
  394. #endif
  395. if (!fep->tx_full)
  396. netif_wake_queue(dev);
  397. }
  398. /* The interrupt handler.
  399. * This is called from the MPC core interrupt.
  400. */
  401. static irqreturn_t
  402. fec_enet_interrupt(int irq, void * dev_id)
  403. {
  404. struct net_device *dev = dev_id;
  405. volatile fec_t *fecp;
  406. uint int_events;
  407. #ifdef CONFIG_FEC_PACKETHOOK
  408. struct fec_enet_private *fep = dev->priv;
  409. __u32 regval;
  410. if (fep->ph_regaddr) regval = *fep->ph_regaddr;
  411. #endif
  412. fecp = (volatile fec_t*)dev->base_addr;
  413. /* Get the interrupt events that caused us to be here.
  414. */
  415. while ((int_events = fecp->fec_ievent) != 0) {
  416. fecp->fec_ievent = int_events;
  417. if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
  418. FEC_ENET_BABT | FEC_ENET_EBERR)) != 0) {
  419. printk("FEC ERROR %x\n", int_events);
  420. }
  421. /* Handle receive event in its own function.
  422. */
  423. if (int_events & FEC_ENET_RXF) {
  424. #ifdef CONFIG_FEC_PACKETHOOK
  425. fec_enet_rx(dev, regval);
  426. #else
  427. fec_enet_rx(dev);
  428. #endif
  429. }
  430. /* Transmit OK, or non-fatal error. Update the buffer
  431. descriptors. FEC handles all errors, we just discover
  432. them as part of the transmit process.
  433. */
  434. if (int_events & FEC_ENET_TXF) {
  435. #ifdef CONFIG_FEC_PACKETHOOK
  436. fec_enet_tx(dev, regval);
  437. #else
  438. fec_enet_tx(dev);
  439. #endif
  440. }
  441. if (int_events & FEC_ENET_MII) {
  442. #ifdef CONFIG_USE_MDIO
  443. fec_enet_mii(dev);
  444. #else
  445. printk("%s[%d] %s: unexpected FEC_ENET_MII event\n", __FILE__,__LINE__,__FUNCTION__);
  446. #endif /* CONFIG_USE_MDIO */
  447. }
  448. }
  449. return IRQ_RETVAL(IRQ_HANDLED);
  450. }
  451. static void
  452. #ifdef CONFIG_FEC_PACKETHOOK
  453. fec_enet_tx(struct net_device *dev, __u32 regval)
  454. #else
  455. fec_enet_tx(struct net_device *dev)
  456. #endif
  457. {
  458. struct fec_enet_private *fep;
  459. volatile cbd_t *bdp;
  460. struct sk_buff *skb;
  461. fep = dev->priv;
  462. /* lock while transmitting */
  463. spin_lock(&fep->lock);
  464. bdp = fep->dirty_tx;
  465. while ((bdp->cbd_sc&BD_ENET_TX_READY) == 0) {
  466. if (bdp == fep->cur_tx && fep->tx_full == 0) break;
  467. skb = fep->tx_skbuff[fep->skb_dirty];
  468. /* Check for errors. */
  469. if (bdp->cbd_sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  470. BD_ENET_TX_RL | BD_ENET_TX_UN |
  471. BD_ENET_TX_CSL)) {
  472. fep->stats.tx_errors++;
  473. if (bdp->cbd_sc & BD_ENET_TX_HB) /* No heartbeat */
  474. fep->stats.tx_heartbeat_errors++;
  475. if (bdp->cbd_sc & BD_ENET_TX_LC) /* Late collision */
  476. fep->stats.tx_window_errors++;
  477. if (bdp->cbd_sc & BD_ENET_TX_RL) /* Retrans limit */
  478. fep->stats.tx_aborted_errors++;
  479. if (bdp->cbd_sc & BD_ENET_TX_UN) /* Underrun */
  480. fep->stats.tx_fifo_errors++;
  481. if (bdp->cbd_sc & BD_ENET_TX_CSL) /* Carrier lost */
  482. fep->stats.tx_carrier_errors++;
  483. } else {
  484. #ifdef CONFIG_FEC_PACKETHOOK
  485. /* Packet hook ... */
  486. if (fep->ph_txhandler &&
  487. ((struct ethhdr *)skb->data)->h_proto
  488. == fep->ph_proto) {
  489. fep->ph_txhandler((__u8*)skb->data, skb->len,
  490. regval, fep->ph_priv);
  491. }
  492. #endif
  493. fep->stats.tx_packets++;
  494. }
  495. #ifndef final_version
  496. if (bdp->cbd_sc & BD_ENET_TX_READY)
  497. printk("HEY! Enet xmit interrupt and TX_READY.\n");
  498. #endif
  499. /* Deferred means some collisions occurred during transmit,
  500. * but we eventually sent the packet OK.
  501. */
  502. if (bdp->cbd_sc & BD_ENET_TX_DEF)
  503. fep->stats.collisions++;
  504. /* Free the sk buffer associated with this last transmit.
  505. */
  506. #if 0
  507. printk("TXI: %x %x %x\n", bdp, skb, fep->skb_dirty);
  508. #endif
  509. dev_kfree_skb_irq (skb/*, FREE_WRITE*/);
  510. fep->tx_skbuff[fep->skb_dirty] = NULL;
  511. fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
  512. /* Update pointer to next buffer descriptor to be transmitted.
  513. */
  514. if (bdp->cbd_sc & BD_ENET_TX_WRAP)
  515. bdp = fep->tx_bd_base;
  516. else
  517. bdp++;
  518. /* Since we have freed up a buffer, the ring is no longer
  519. * full.
  520. */
  521. if (fep->tx_full) {
  522. fep->tx_full = 0;
  523. if (netif_queue_stopped(dev))
  524. netif_wake_queue(dev);
  525. }
  526. #ifdef CONFIG_FEC_PACKETHOOK
  527. /* Re-read register. Not exactly guaranteed to be correct,
  528. but... */
  529. if (fep->ph_regaddr) regval = *fep->ph_regaddr;
  530. #endif
  531. }
  532. fep->dirty_tx = (cbd_t *)bdp;
  533. spin_unlock(&fep->lock);
  534. }
  535. /* During a receive, the cur_rx points to the current incoming buffer.
  536. * When we update through the ring, if the next incoming buffer has
  537. * not been given to the system, we just set the empty indicator,
  538. * effectively tossing the packet.
  539. */
  540. static void
  541. #ifdef CONFIG_FEC_PACKETHOOK
  542. fec_enet_rx(struct net_device *dev, __u32 regval)
  543. #else
  544. fec_enet_rx(struct net_device *dev)
  545. #endif
  546. {
  547. struct fec_enet_private *fep;
  548. volatile fec_t *fecp;
  549. volatile cbd_t *bdp;
  550. struct sk_buff *skb;
  551. ushort pkt_len;
  552. __u8 *data;
  553. fep = dev->priv;
  554. fecp = (volatile fec_t*)dev->base_addr;
  555. /* First, grab all of the stats for the incoming packet.
  556. * These get messed up if we get called due to a busy condition.
  557. */
  558. bdp = fep->cur_rx;
  559. while (!(bdp->cbd_sc & BD_ENET_RX_EMPTY)) {
  560. #ifndef final_version
  561. /* Since we have allocated space to hold a complete frame,
  562. * the last indicator should be set.
  563. */
  564. if ((bdp->cbd_sc & BD_ENET_RX_LAST) == 0)
  565. printk("FEC ENET: rcv is not +last\n");
  566. #endif
  567. /* Check for errors. */
  568. if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  569. BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  570. fep->stats.rx_errors++;
  571. if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
  572. /* Frame too long or too short. */
  573. fep->stats.rx_length_errors++;
  574. }
  575. if (bdp->cbd_sc & BD_ENET_RX_NO) /* Frame alignment */
  576. fep->stats.rx_frame_errors++;
  577. if (bdp->cbd_sc & BD_ENET_RX_CR) /* CRC Error */
  578. fep->stats.rx_crc_errors++;
  579. if (bdp->cbd_sc & BD_ENET_RX_OV) /* FIFO overrun */
  580. fep->stats.rx_crc_errors++;
  581. }
  582. /* Report late collisions as a frame error.
  583. * On this error, the BD is closed, but we don't know what we
  584. * have in the buffer. So, just drop this frame on the floor.
  585. */
  586. if (bdp->cbd_sc & BD_ENET_RX_CL) {
  587. fep->stats.rx_errors++;
  588. fep->stats.rx_frame_errors++;
  589. goto rx_processing_done;
  590. }
  591. /* Process the incoming frame.
  592. */
  593. fep->stats.rx_packets++;
  594. pkt_len = bdp->cbd_datlen;
  595. fep->stats.rx_bytes += pkt_len;
  596. data = fep->rx_vaddr[bdp - fep->rx_bd_base];
  597. #ifdef CONFIG_FEC_PACKETHOOK
  598. /* Packet hook ... */
  599. if (fep->ph_rxhandler) {
  600. if (((struct ethhdr *)data)->h_proto == fep->ph_proto) {
  601. switch (fep->ph_rxhandler(data, pkt_len, regval,
  602. fep->ph_priv)) {
  603. case 1:
  604. goto rx_processing_done;
  605. break;
  606. case 0:
  607. break;
  608. default:
  609. fep->stats.rx_errors++;
  610. goto rx_processing_done;
  611. }
  612. }
  613. }
  614. /* If it wasn't filtered - copy it to an sk buffer. */
  615. #endif
  616. /* This does 16 byte alignment, exactly what we need.
  617. * The packet length includes FCS, but we don't want to
  618. * include that when passing upstream as it messes up
  619. * bridging applications.
  620. */
  621. skb = dev_alloc_skb(pkt_len-4);
  622. if (skb == NULL) {
  623. printk("%s: Memory squeeze, dropping packet.\n", dev->name);
  624. fep->stats.rx_dropped++;
  625. } else {
  626. skb->dev = dev;
  627. skb_put(skb,pkt_len-4); /* Make room */
  628. eth_copy_and_sum(skb, data, pkt_len-4, 0);
  629. skb->protocol=eth_type_trans(skb,dev);
  630. netif_rx(skb);
  631. }
  632. rx_processing_done:
  633. /* Clear the status flags for this buffer.
  634. */
  635. bdp->cbd_sc &= ~BD_ENET_RX_STATS;
  636. /* Mark the buffer empty.
  637. */
  638. bdp->cbd_sc |= BD_ENET_RX_EMPTY;
  639. /* Update BD pointer to next entry.
  640. */
  641. if (bdp->cbd_sc & BD_ENET_RX_WRAP)
  642. bdp = fep->rx_bd_base;
  643. else
  644. bdp++;
  645. #if 1
  646. /* Doing this here will keep the FEC running while we process
  647. * incoming frames. On a heavily loaded network, we should be
  648. * able to keep up at the expense of system resources.
  649. */
  650. fecp->fec_r_des_active = 0x01000000;
  651. #endif
  652. #ifdef CONFIG_FEC_PACKETHOOK
  653. /* Re-read register. Not exactly guaranteed to be correct,
  654. but... */
  655. if (fep->ph_regaddr) regval = *fep->ph_regaddr;
  656. #endif
  657. } /* while (!(bdp->cbd_sc & BD_ENET_RX_EMPTY)) */
  658. fep->cur_rx = (cbd_t *)bdp;
  659. #if 0
  660. /* Doing this here will allow us to process all frames in the
  661. * ring before the FEC is allowed to put more there. On a heavily
  662. * loaded network, some frames may be lost. Unfortunately, this
  663. * increases the interrupt overhead since we can potentially work
  664. * our way back to the interrupt return only to come right back
  665. * here.
  666. */
  667. fecp->fec_r_des_active = 0x01000000;
  668. #endif
  669. }
  670. #ifdef CONFIG_USE_MDIO
  671. static void
  672. fec_enet_mii(struct net_device *dev)
  673. {
  674. struct fec_enet_private *fep;
  675. volatile fec_t *ep;
  676. mii_list_t *mip;
  677. uint mii_reg;
  678. fep = (struct fec_enet_private *)dev->priv;
  679. ep = &(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec);
  680. mii_reg = ep->fec_mii_data;
  681. if ((mip = mii_head) == NULL) {
  682. printk("MII and no head!\n");
  683. return;
  684. }
  685. if (mip->mii_func != NULL)
  686. (*(mip->mii_func))(mii_reg, dev);
  687. mii_head = mip->mii_next;
  688. mip->mii_next = mii_free;
  689. mii_free = mip;
  690. if ((mip = mii_head) != NULL) {
  691. ep->fec_mii_data = mip->mii_regval;
  692. }
  693. }
  694. static int
  695. mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
  696. {
  697. struct fec_enet_private *fep;
  698. unsigned long flags;
  699. mii_list_t *mip;
  700. int retval;
  701. /* Add PHY address to register command.
  702. */
  703. fep = dev->priv;
  704. regval |= fep->phy_addr << 23;
  705. retval = 0;
  706. /* lock while modifying mii_list */
  707. spin_lock_irqsave(&fep->lock, flags);
  708. if ((mip = mii_free) != NULL) {
  709. mii_free = mip->mii_next;
  710. mip->mii_regval = regval;
  711. mip->mii_func = func;
  712. mip->mii_next = NULL;
  713. if (mii_head) {
  714. mii_tail->mii_next = mip;
  715. mii_tail = mip;
  716. } else {
  717. mii_head = mii_tail = mip;
  718. (&(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec))->fec_mii_data = regval;
  719. }
  720. } else {
  721. retval = 1;
  722. }
  723. spin_unlock_irqrestore(&fep->lock, flags);
  724. return(retval);
  725. }
  726. static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
  727. {
  728. int k;
  729. if(!c)
  730. return;
  731. for(k = 0; (c+k)->mii_data != mk_mii_end; k++)
  732. mii_queue(dev, (c+k)->mii_data, (c+k)->funct);
  733. }
  734. static void mii_parse_sr(uint mii_reg, struct net_device *dev)
  735. {
  736. struct fec_enet_private *fep = dev->priv;
  737. volatile uint *s = &(fep->phy_status);
  738. *s &= ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
  739. if (mii_reg & 0x0004)
  740. *s |= PHY_STAT_LINK;
  741. if (mii_reg & 0x0010)
  742. *s |= PHY_STAT_FAULT;
  743. if (mii_reg & 0x0020)
  744. *s |= PHY_STAT_ANC;
  745. fep->link = (*s & PHY_STAT_LINK) ? 1 : 0;
  746. }
  747. static void mii_parse_cr(uint mii_reg, struct net_device *dev)
  748. {
  749. struct fec_enet_private *fep = dev->priv;
  750. volatile uint *s = &(fep->phy_status);
  751. *s &= ~(PHY_CONF_ANE | PHY_CONF_LOOP);
  752. if (mii_reg & 0x1000)
  753. *s |= PHY_CONF_ANE;
  754. if (mii_reg & 0x4000)
  755. *s |= PHY_CONF_LOOP;
  756. }
  757. static void mii_parse_anar(uint mii_reg, struct net_device *dev)
  758. {
  759. struct fec_enet_private *fep = dev->priv;
  760. volatile uint *s = &(fep->phy_status);
  761. *s &= ~(PHY_CONF_SPMASK);
  762. if (mii_reg & 0x0020)
  763. *s |= PHY_CONF_10HDX;
  764. if (mii_reg & 0x0040)
  765. *s |= PHY_CONF_10FDX;
  766. if (mii_reg & 0x0080)
  767. *s |= PHY_CONF_100HDX;
  768. if (mii_reg & 0x00100)
  769. *s |= PHY_CONF_100FDX;
  770. }
  771. #if 0
  772. static void mii_disp_reg(uint mii_reg, struct net_device *dev)
  773. {
  774. printk("reg %u = 0x%04x\n", (mii_reg >> 18) & 0x1f, mii_reg & 0xffff);
  775. }
  776. #endif
  777. /* ------------------------------------------------------------------------- */
  778. /* The Level one LXT970 is used by many boards */
  779. #ifdef CONFIG_FEC_LXT970
  780. #define MII_LXT970_MIRROR 16 /* Mirror register */
  781. #define MII_LXT970_IER 17 /* Interrupt Enable Register */
  782. #define MII_LXT970_ISR 18 /* Interrupt Status Register */
  783. #define MII_LXT970_CONFIG 19 /* Configuration Register */
  784. #define MII_LXT970_CSR 20 /* Chip Status Register */
  785. static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
  786. {
  787. struct fec_enet_private *fep = dev->priv;
  788. volatile uint *s = &(fep->phy_status);
  789. *s &= ~(PHY_STAT_SPMASK);
  790. if (mii_reg & 0x0800) {
  791. if (mii_reg & 0x1000)
  792. *s |= PHY_STAT_100FDX;
  793. else
  794. *s |= PHY_STAT_100HDX;
  795. }
  796. else {
  797. if (mii_reg & 0x1000)
  798. *s |= PHY_STAT_10FDX;
  799. else
  800. *s |= PHY_STAT_10HDX;
  801. }
  802. }
  803. static phy_info_t phy_info_lxt970 = {
  804. 0x07810000,
  805. "LXT970",
  806. (const phy_cmd_t []) { /* config */
  807. #if 0
  808. // { mk_mii_write(MII_REG_ANAR, 0x0021), NULL },
  809. /* Set default operation of 100-TX....for some reason
  810. * some of these bits are set on power up, which is wrong.
  811. */
  812. { mk_mii_write(MII_LXT970_CONFIG, 0), NULL },
  813. #endif
  814. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  815. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  816. { mk_mii_end, }
  817. },
  818. (const phy_cmd_t []) { /* startup - enable interrupts */
  819. { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
  820. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  821. { mk_mii_end, }
  822. },
  823. (const phy_cmd_t []) { /* ack_int */
  824. /* read SR and ISR to acknowledge */
  825. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  826. { mk_mii_read(MII_LXT970_ISR), NULL },
  827. /* find out the current status */
  828. { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
  829. { mk_mii_end, }
  830. },
  831. (const phy_cmd_t []) { /* shutdown - disable interrupts */
  832. { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
  833. { mk_mii_end, }
  834. },
  835. };
  836. #endif /* CONFIG_FEC_LXT970 */
  837. /* ------------------------------------------------------------------------- */
  838. /* The Level one LXT971 is used on some of my custom boards */
  839. #ifdef CONFIG_FEC_LXT971
  840. /* register definitions for the 971 */
  841. #define MII_LXT971_PCR 16 /* Port Control Register */
  842. #define MII_LXT971_SR2 17 /* Status Register 2 */
  843. #define MII_LXT971_IER 18 /* Interrupt Enable Register */
  844. #define MII_LXT971_ISR 19 /* Interrupt Status Register */
  845. #define MII_LXT971_LCR 20 /* LED Control Register */
  846. #define MII_LXT971_TCR 30 /* Transmit Control Register */
  847. /*
  848. * I had some nice ideas of running the MDIO faster...
  849. * The 971 should support 8MHz and I tried it, but things acted really
  850. * weird, so 2.5 MHz ought to be enough for anyone...
  851. */
  852. static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
  853. {
  854. struct fec_enet_private *fep = dev->priv;
  855. volatile uint *s = &(fep->phy_status);
  856. *s &= ~(PHY_STAT_SPMASK);
  857. if (mii_reg & 0x4000) {
  858. if (mii_reg & 0x0200)
  859. *s |= PHY_STAT_100FDX;
  860. else
  861. *s |= PHY_STAT_100HDX;
  862. }
  863. else {
  864. if (mii_reg & 0x0200)
  865. *s |= PHY_STAT_10FDX;
  866. else
  867. *s |= PHY_STAT_10HDX;
  868. }
  869. if (mii_reg & 0x0008)
  870. *s |= PHY_STAT_FAULT;
  871. }
  872. static phy_info_t phy_info_lxt971 = {
  873. 0x0001378e,
  874. "LXT971",
  875. (const phy_cmd_t []) { /* config */
  876. // { mk_mii_write(MII_REG_ANAR, 0x021), NULL }, /* 10 Mbps, HD */
  877. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  878. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  879. { mk_mii_end, }
  880. },
  881. (const phy_cmd_t []) { /* startup - enable interrupts */
  882. { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
  883. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  884. /* Somehow does the 971 tell me that the link is down
  885. * the first read after power-up.
  886. * read here to get a valid value in ack_int */
  887. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  888. { mk_mii_end, }
  889. },
  890. (const phy_cmd_t []) { /* ack_int */
  891. /* find out the current status */
  892. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  893. { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
  894. /* we only need to read ISR to acknowledge */
  895. { mk_mii_read(MII_LXT971_ISR), NULL },
  896. { mk_mii_end, }
  897. },
  898. (const phy_cmd_t []) { /* shutdown - disable interrupts */
  899. { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
  900. { mk_mii_end, }
  901. },
  902. };
  903. #endif /* CONFIG_FEC_LXT970 */
  904. /* ------------------------------------------------------------------------- */
  905. /* The Quality Semiconductor QS6612 is used on the RPX CLLF */
  906. #ifdef CONFIG_FEC_QS6612
  907. /* register definitions */
  908. #define MII_QS6612_MCR 17 /* Mode Control Register */
  909. #define MII_QS6612_FTR 27 /* Factory Test Register */
  910. #define MII_QS6612_MCO 28 /* Misc. Control Register */
  911. #define MII_QS6612_ISR 29 /* Interrupt Source Register */
  912. #define MII_QS6612_IMR 30 /* Interrupt Mask Register */
  913. #define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
  914. static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
  915. {
  916. struct fec_enet_private *fep = dev->priv;
  917. volatile uint *s = &(fep->phy_status);
  918. *s &= ~(PHY_STAT_SPMASK);
  919. switch((mii_reg >> 2) & 7) {
  920. case 1: *s |= PHY_STAT_10HDX; break;
  921. case 2: *s |= PHY_STAT_100HDX; break;
  922. case 5: *s |= PHY_STAT_10FDX; break;
  923. case 6: *s |= PHY_STAT_100FDX; break;
  924. }
  925. }
  926. static phy_info_t phy_info_qs6612 = {
  927. 0x00181440,
  928. "QS6612",
  929. (const phy_cmd_t []) { /* config */
  930. // { mk_mii_write(MII_REG_ANAR, 0x061), NULL }, /* 10 Mbps */
  931. /* The PHY powers up isolated on the RPX,
  932. * so send a command to allow operation.
  933. */
  934. { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
  935. /* parse cr and anar to get some info */
  936. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  937. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  938. { mk_mii_end, }
  939. },
  940. (const phy_cmd_t []) { /* startup - enable interrupts */
  941. { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
  942. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  943. { mk_mii_end, }
  944. },
  945. (const phy_cmd_t []) { /* ack_int */
  946. /* we need to read ISR, SR and ANER to acknowledge */
  947. { mk_mii_read(MII_QS6612_ISR), NULL },
  948. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  949. { mk_mii_read(MII_REG_ANER), NULL },
  950. /* read pcr to get info */
  951. { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
  952. { mk_mii_end, }
  953. },
  954. (const phy_cmd_t []) { /* shutdown - disable interrupts */
  955. { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
  956. { mk_mii_end, }
  957. },
  958. };
  959. #endif /* CONFIG_FEC_QS6612 */
  960. /* ------------------------------------------------------------------------- */
  961. /* The Advanced Micro Devices AM79C874 is used on the ICU862 */
  962. #ifdef CONFIG_FEC_AM79C874
  963. /* register definitions for the 79C874 */
  964. #define MII_AM79C874_MFR 16 /* Miscellaneous Features Register */
  965. #define MII_AM79C874_ICSR 17 /* Interrupt Control/Status Register */
  966. #define MII_AM79C874_DR 18 /* Diagnostic Register */
  967. #define MII_AM79C874_PMLR 19 /* Power Management & Loopback Register */
  968. #define MII_AM79C874_MCR 21 /* Mode Control Register */
  969. #define MII_AM79C874_DC 23 /* Disconnect Counter */
  970. #define MII_AM79C874_REC 24 /* Receiver Error Counter */
  971. static void mii_parse_amd79c874_dr(uint mii_reg, struct net_device *dev, uint data)
  972. {
  973. volatile struct fec_enet_private *fep = dev->priv;
  974. uint s = fep->phy_status;
  975. s &= ~(PHY_STAT_SPMASK);
  976. /* Register 18: Bit 10 is data rate, 11 is Duplex */
  977. switch ((mii_reg >> 10) & 3) {
  978. case 0: s |= PHY_STAT_10HDX; break;
  979. case 1: s |= PHY_STAT_100HDX; break;
  980. case 2: s |= PHY_STAT_10FDX; break;
  981. case 3: s |= PHY_STAT_100FDX; break;
  982. }
  983. fep->phy_status = s;
  984. }
  985. static phy_info_t phy_info_amd79c874 = {
  986. 0x00022561,
  987. "AM79C874",
  988. (const phy_cmd_t []) { /* config */
  989. // { mk_mii_write(MII_REG_ANAR, 0x021), NULL }, /* 10 Mbps, HD */
  990. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  991. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  992. { mk_mii_end, }
  993. },
  994. (const phy_cmd_t []) { /* startup - enable interrupts */
  995. { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
  996. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  997. { mk_mii_end, }
  998. },
  999. (const phy_cmd_t []) { /* ack_int */
  1000. /* find out the current status */
  1001. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  1002. { mk_mii_read(MII_AM79C874_DR), mii_parse_amd79c874_dr },
  1003. /* we only need to read ICSR to acknowledge */
  1004. { mk_mii_read(MII_AM79C874_ICSR), NULL },
  1005. { mk_mii_end, }
  1006. },
  1007. (const phy_cmd_t []) { /* shutdown - disable interrupts */
  1008. { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
  1009. { mk_mii_end, }
  1010. },
  1011. };
  1012. #endif /* CONFIG_FEC_AM79C874 */
  1013. static phy_info_t *phy_info[] = {
  1014. #ifdef CONFIG_FEC_LXT970
  1015. &phy_info_lxt970,
  1016. #endif /* CONFIG_FEC_LXT970 */
  1017. #ifdef CONFIG_FEC_LXT971
  1018. &phy_info_lxt971,
  1019. #endif /* CONFIG_FEC_LXT971 */
  1020. #ifdef CONFIG_FEC_QS6612
  1021. &phy_info_qs6612,
  1022. #endif /* CONFIG_FEC_QS6612 */
  1023. #ifdef CONFIG_FEC_AM79C874
  1024. &phy_info_amd79c874,
  1025. #endif /* CONFIG_FEC_AM79C874 */
  1026. NULL
  1027. };
  1028. static void mii_display_status(struct net_device *dev)
  1029. {
  1030. struct fec_enet_private *fep = dev->priv;
  1031. volatile uint *s = &(fep->phy_status);
  1032. if (!fep->link && !fep->old_link) {
  1033. /* Link is still down - don't print anything */
  1034. return;
  1035. }
  1036. printk("%s: status: ", dev->name);
  1037. if (!fep->link) {
  1038. printk("link down");
  1039. } else {
  1040. printk("link up");
  1041. switch(*s & PHY_STAT_SPMASK) {
  1042. case PHY_STAT_100FDX: printk(", 100 Mbps Full Duplex"); break;
  1043. case PHY_STAT_100HDX: printk(", 100 Mbps Half Duplex"); break;
  1044. case PHY_STAT_10FDX: printk(", 10 Mbps Full Duplex"); break;
  1045. case PHY_STAT_10HDX: printk(", 10 Mbps Half Duplex"); break;
  1046. default:
  1047. printk(", Unknown speed/duplex");
  1048. }
  1049. if (*s & PHY_STAT_ANC)
  1050. printk(", auto-negotiation complete");
  1051. }
  1052. if (*s & PHY_STAT_FAULT)
  1053. printk(", remote fault");
  1054. printk(".\n");
  1055. }
  1056. static void mii_display_config(struct work_struct *work)
  1057. {
  1058. struct fec_enet_private *fep =
  1059. container_of(work, struct fec_enet_private, phy_task);
  1060. struct net_device *dev = fep->dev;
  1061. volatile uint *s = &(fep->phy_status);
  1062. printk("%s: config: auto-negotiation ", dev->name);
  1063. if (*s & PHY_CONF_ANE)
  1064. printk("on");
  1065. else
  1066. printk("off");
  1067. if (*s & PHY_CONF_100FDX)
  1068. printk(", 100FDX");
  1069. if (*s & PHY_CONF_100HDX)
  1070. printk(", 100HDX");
  1071. if (*s & PHY_CONF_10FDX)
  1072. printk(", 10FDX");
  1073. if (*s & PHY_CONF_10HDX)
  1074. printk(", 10HDX");
  1075. if (!(*s & PHY_CONF_SPMASK))
  1076. printk(", No speed/duplex selected?");
  1077. if (*s & PHY_CONF_LOOP)
  1078. printk(", loopback enabled");
  1079. printk(".\n");
  1080. fep->sequence_done = 1;
  1081. }
  1082. static void mii_relink(struct work_struct *work)
  1083. {
  1084. struct fec_enet_private *fep =
  1085. container_of(work, struct fec_enet_private, phy_task);
  1086. struct net_device *dev = fep->dev;
  1087. int duplex;
  1088. fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
  1089. mii_display_status(dev);
  1090. fep->old_link = fep->link;
  1091. if (fep->link) {
  1092. duplex = 0;
  1093. if (fep->phy_status
  1094. & (PHY_STAT_100FDX | PHY_STAT_10FDX))
  1095. duplex = 1;
  1096. fec_restart(dev, duplex);
  1097. }
  1098. else
  1099. fec_stop(dev);
  1100. #if 0
  1101. enable_irq(fep->mii_irq);
  1102. #endif
  1103. }
  1104. static void mii_queue_relink(uint mii_reg, struct net_device *dev)
  1105. {
  1106. struct fec_enet_private *fep = dev->priv;
  1107. fep->dev = dev;
  1108. INIT_WORK(&fep->phy_task, mii_relink);
  1109. schedule_work(&fep->phy_task);
  1110. }
  1111. static void mii_queue_config(uint mii_reg, struct net_device *dev)
  1112. {
  1113. struct fec_enet_private *fep = dev->priv;
  1114. fep->dev = dev;
  1115. INIT_WORK(&fep->phy_task, mii_display_config);
  1116. schedule_work(&fep->phy_task);
  1117. }
  1118. phy_cmd_t phy_cmd_relink[] = { { mk_mii_read(MII_REG_CR), mii_queue_relink },
  1119. { mk_mii_end, } };
  1120. phy_cmd_t phy_cmd_config[] = { { mk_mii_read(MII_REG_CR), mii_queue_config },
  1121. { mk_mii_end, } };
  1122. /* Read remainder of PHY ID.
  1123. */
  1124. static void
  1125. mii_discover_phy3(uint mii_reg, struct net_device *dev)
  1126. {
  1127. struct fec_enet_private *fep;
  1128. int i;
  1129. fep = dev->priv;
  1130. fep->phy_id |= (mii_reg & 0xffff);
  1131. for(i = 0; phy_info[i]; i++)
  1132. if(phy_info[i]->id == (fep->phy_id >> 4))
  1133. break;
  1134. if(!phy_info[i])
  1135. panic("%s: PHY id 0x%08x is not supported!\n",
  1136. dev->name, fep->phy_id);
  1137. fep->phy = phy_info[i];
  1138. fep->phy_id_done = 1;
  1139. printk("%s: Phy @ 0x%x, type %s (0x%08x)\n",
  1140. dev->name, fep->phy_addr, fep->phy->name, fep->phy_id);
  1141. }
  1142. /* Scan all of the MII PHY addresses looking for someone to respond
  1143. * with a valid ID. This usually happens quickly.
  1144. */
  1145. static void
  1146. mii_discover_phy(uint mii_reg, struct net_device *dev)
  1147. {
  1148. struct fec_enet_private *fep;
  1149. uint phytype;
  1150. fep = dev->priv;
  1151. if ((phytype = (mii_reg & 0xffff)) != 0xffff) {
  1152. /* Got first part of ID, now get remainder.
  1153. */
  1154. fep->phy_id = phytype << 16;
  1155. mii_queue(dev, mk_mii_read(MII_REG_PHYIR2), mii_discover_phy3);
  1156. } else {
  1157. fep->phy_addr++;
  1158. if (fep->phy_addr < 32) {
  1159. mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
  1160. mii_discover_phy);
  1161. } else {
  1162. printk("fec: No PHY device found.\n");
  1163. }
  1164. }
  1165. }
  1166. #endif /* CONFIG_USE_MDIO */
  1167. /* This interrupt occurs when the PHY detects a link change.
  1168. */
  1169. static
  1170. #ifdef CONFIG_RPXCLASSIC
  1171. void mii_link_interrupt(void *dev_id)
  1172. #else
  1173. irqreturn_t mii_link_interrupt(int irq, void * dev_id)
  1174. #endif
  1175. {
  1176. #ifdef CONFIG_USE_MDIO
  1177. struct net_device *dev = dev_id;
  1178. struct fec_enet_private *fep = dev->priv;
  1179. volatile immap_t *immap = (immap_t *)IMAP_ADDR;
  1180. volatile fec_t *fecp = &(immap->im_cpm.cp_fec);
  1181. unsigned int ecntrl = fecp->fec_ecntrl;
  1182. /* We need the FEC enabled to access the MII
  1183. */
  1184. if ((ecntrl & FEC_ECNTRL_ETHER_EN) == 0) {
  1185. fecp->fec_ecntrl |= FEC_ECNTRL_ETHER_EN;
  1186. }
  1187. #endif /* CONFIG_USE_MDIO */
  1188. #if 0
  1189. disable_irq(fep->mii_irq); /* disable now, enable later */
  1190. #endif
  1191. #ifdef CONFIG_USE_MDIO
  1192. mii_do_cmd(dev, fep->phy->ack_int);
  1193. mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
  1194. if ((ecntrl & FEC_ECNTRL_ETHER_EN) == 0) {
  1195. fecp->fec_ecntrl = ecntrl; /* restore old settings */
  1196. }
  1197. #else
  1198. printk("%s[%d] %s: unexpected Link interrupt\n", __FILE__,__LINE__,__FUNCTION__);
  1199. #endif /* CONFIG_USE_MDIO */
  1200. #ifndef CONFIG_RPXCLASSIC
  1201. return IRQ_RETVAL(IRQ_HANDLED);
  1202. #endif /* CONFIG_RPXCLASSIC */
  1203. }
  1204. static int
  1205. fec_enet_open(struct net_device *dev)
  1206. {
  1207. struct fec_enet_private *fep = dev->priv;
  1208. /* I should reset the ring buffers here, but I don't yet know
  1209. * a simple way to do that.
  1210. */
  1211. #ifdef CONFIG_USE_MDIO
  1212. fep->sequence_done = 0;
  1213. fep->link = 0;
  1214. if (fep->phy) {
  1215. mii_do_cmd(dev, fep->phy->ack_int);
  1216. mii_do_cmd(dev, fep->phy->config);
  1217. mii_do_cmd(dev, phy_cmd_config); /* display configuration */
  1218. while(!fep->sequence_done)
  1219. schedule();
  1220. mii_do_cmd(dev, fep->phy->startup);
  1221. netif_start_queue(dev);
  1222. return 0; /* Success */
  1223. }
  1224. return -ENODEV; /* No PHY we understand */
  1225. #else
  1226. fep->link = 1;
  1227. netif_start_queue(dev);
  1228. return 0; /* Success */
  1229. #endif /* CONFIG_USE_MDIO */
  1230. }
  1231. static int
  1232. fec_enet_close(struct net_device *dev)
  1233. {
  1234. /* Don't know what to do yet.
  1235. */
  1236. netif_stop_queue(dev);
  1237. fec_stop(dev);
  1238. return 0;
  1239. }
  1240. static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
  1241. {
  1242. struct fec_enet_private *fep = (struct fec_enet_private *)dev->priv;
  1243. return &fep->stats;
  1244. }
  1245. /* Set or clear the multicast filter for this adaptor.
  1246. * Skeleton taken from sunlance driver.
  1247. * The CPM Ethernet implementation allows Multicast as well as individual
  1248. * MAC address filtering. Some of the drivers check to make sure it is
  1249. * a group multicast address, and discard those that are not. I guess I
  1250. * will do the same for now, but just remove the test if you want
  1251. * individual filtering as well (do the upper net layers want or support
  1252. * this kind of feature?).
  1253. */
  1254. static void set_multicast_list(struct net_device *dev)
  1255. {
  1256. struct fec_enet_private *fep;
  1257. volatile fec_t *ep;
  1258. fep = (struct fec_enet_private *)dev->priv;
  1259. ep = &(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec);
  1260. if (dev->flags&IFF_PROMISC) {
  1261. /* Log any net taps. */
  1262. printk("%s: Promiscuous mode enabled.\n", dev->name);
  1263. ep->fec_r_cntrl |= FEC_RCNTRL_PROM;
  1264. } else {
  1265. ep->fec_r_cntrl &= ~FEC_RCNTRL_PROM;
  1266. if (dev->flags & IFF_ALLMULTI) {
  1267. /* Catch all multicast addresses, so set the
  1268. * filter to all 1's.
  1269. */
  1270. ep->fec_hash_table_high = 0xffffffff;
  1271. ep->fec_hash_table_low = 0xffffffff;
  1272. }
  1273. #if 0
  1274. else {
  1275. /* Clear filter and add the addresses in the list.
  1276. */
  1277. ep->sen_gaddr1 = 0;
  1278. ep->sen_gaddr2 = 0;
  1279. ep->sen_gaddr3 = 0;
  1280. ep->sen_gaddr4 = 0;
  1281. dmi = dev->mc_list;
  1282. for (i=0; i<dev->mc_count; i++) {
  1283. /* Only support group multicast for now.
  1284. */
  1285. if (!(dmi->dmi_addr[0] & 1))
  1286. continue;
  1287. /* The address in dmi_addr is LSB first,
  1288. * and taddr is MSB first. We have to
  1289. * copy bytes MSB first from dmi_addr.
  1290. */
  1291. mcptr = (u_char *)dmi->dmi_addr + 5;
  1292. tdptr = (u_char *)&ep->sen_taddrh;
  1293. for (j=0; j<6; j++)
  1294. *tdptr++ = *mcptr--;
  1295. /* Ask CPM to run CRC and set bit in
  1296. * filter mask.
  1297. */
  1298. cpmp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SCC1, CPM_CR_SET_GADDR) | CPM_CR_FLG;
  1299. /* this delay is necessary here -- Cort */
  1300. udelay(10);
  1301. while (cpmp->cp_cpcr & CPM_CR_FLG);
  1302. }
  1303. }
  1304. #endif
  1305. }
  1306. }
  1307. /* Initialize the FEC Ethernet on 860T.
  1308. */
  1309. static int __init fec_enet_init(void)
  1310. {
  1311. struct net_device *dev;
  1312. struct fec_enet_private *fep;
  1313. int i, j, k, err;
  1314. unsigned char *eap, *iap, *ba;
  1315. dma_addr_t mem_addr;
  1316. volatile cbd_t *bdp;
  1317. cbd_t *cbd_base;
  1318. volatile immap_t *immap;
  1319. volatile fec_t *fecp;
  1320. bd_t *bd;
  1321. #ifdef CONFIG_SCC_ENET
  1322. unsigned char tmpaddr[6];
  1323. #endif
  1324. immap = (immap_t *)IMAP_ADDR; /* pointer to internal registers */
  1325. bd = (bd_t *)__res;
  1326. dev = alloc_etherdev(sizeof(*fep));
  1327. if (!dev)
  1328. return -ENOMEM;
  1329. fep = dev->priv;
  1330. fecp = &(immap->im_cpm.cp_fec);
  1331. /* Whack a reset. We should wait for this.
  1332. */
  1333. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  1334. for (i = 0;
  1335. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  1336. ++i) {
  1337. udelay(1);
  1338. }
  1339. if (i == FEC_RESET_DELAY) {
  1340. printk ("FEC Reset timeout!\n");
  1341. }
  1342. /* Set the Ethernet address. If using multiple Enets on the 8xx,
  1343. * this needs some work to get unique addresses.
  1344. */
  1345. eap = (unsigned char *)my_enet_addr;
  1346. iap = bd->bi_enetaddr;
  1347. #ifdef CONFIG_SCC_ENET
  1348. /*
  1349. * If a board has Ethernet configured both on a SCC and the
  1350. * FEC, it needs (at least) 2 MAC addresses (we know that Sun
  1351. * disagrees, but anyway). For the FEC port, we create
  1352. * another address by setting one of the address bits above
  1353. * something that would have (up to now) been allocated.
  1354. */
  1355. for (i=0; i<6; i++)
  1356. tmpaddr[i] = *iap++;
  1357. tmpaddr[3] |= 0x80;
  1358. iap = tmpaddr;
  1359. #endif
  1360. for (i=0; i<6; i++) {
  1361. dev->dev_addr[i] = *eap++ = *iap++;
  1362. }
  1363. /* Allocate memory for buffer descriptors.
  1364. */
  1365. if (((RX_RING_SIZE + TX_RING_SIZE) * sizeof(cbd_t)) > PAGE_SIZE) {
  1366. printk("FEC init error. Need more space.\n");
  1367. printk("FEC initialization failed.\n");
  1368. return 1;
  1369. }
  1370. cbd_base = (cbd_t *)dma_alloc_coherent(dev->class_dev.dev, PAGE_SIZE,
  1371. &mem_addr, GFP_KERNEL);
  1372. /* Set receive and transmit descriptor base.
  1373. */
  1374. fep->rx_bd_base = cbd_base;
  1375. fep->tx_bd_base = cbd_base + RX_RING_SIZE;
  1376. fep->skb_cur = fep->skb_dirty = 0;
  1377. /* Initialize the receive buffer descriptors.
  1378. */
  1379. bdp = fep->rx_bd_base;
  1380. k = 0;
  1381. for (i=0; i<FEC_ENET_RX_PAGES; i++) {
  1382. /* Allocate a page.
  1383. */
  1384. ba = (unsigned char *)dma_alloc_coherent(dev->class_dev.dev,
  1385. PAGE_SIZE,
  1386. &mem_addr,
  1387. GFP_KERNEL);
  1388. /* BUG: no check for failure */
  1389. /* Initialize the BD for every fragment in the page.
  1390. */
  1391. for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
  1392. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1393. bdp->cbd_bufaddr = mem_addr;
  1394. fep->rx_vaddr[k++] = ba;
  1395. mem_addr += FEC_ENET_RX_FRSIZE;
  1396. ba += FEC_ENET_RX_FRSIZE;
  1397. bdp++;
  1398. }
  1399. }
  1400. /* Set the last buffer to wrap.
  1401. */
  1402. bdp--;
  1403. bdp->cbd_sc |= BD_SC_WRAP;
  1404. #ifdef CONFIG_FEC_PACKETHOOK
  1405. fep->ph_lock = 0;
  1406. fep->ph_rxhandler = fep->ph_txhandler = NULL;
  1407. fep->ph_proto = 0;
  1408. fep->ph_regaddr = NULL;
  1409. fep->ph_priv = NULL;
  1410. #endif
  1411. /* Install our interrupt handler.
  1412. */
  1413. if (request_irq(FEC_INTERRUPT, fec_enet_interrupt, 0, "fec", dev) != 0)
  1414. panic("Could not allocate FEC IRQ!");
  1415. #ifdef CONFIG_RPXCLASSIC
  1416. /* Make Port C, bit 15 an input that causes interrupts.
  1417. */
  1418. immap->im_ioport.iop_pcpar &= ~0x0001;
  1419. immap->im_ioport.iop_pcdir &= ~0x0001;
  1420. immap->im_ioport.iop_pcso &= ~0x0001;
  1421. immap->im_ioport.iop_pcint |= 0x0001;
  1422. cpm_install_handler(CPMVEC_PIO_PC15, mii_link_interrupt, dev);
  1423. /* Make LEDS reflect Link status.
  1424. */
  1425. *((uint *) RPX_CSR_ADDR) &= ~BCSR2_FETHLEDMODE;
  1426. #endif
  1427. #ifdef PHY_INTERRUPT
  1428. ((immap_t *)IMAP_ADDR)->im_siu_conf.sc_siel |=
  1429. (0x80000000 >> PHY_INTERRUPT);
  1430. if (request_irq(PHY_INTERRUPT, mii_link_interrupt, 0, "mii", dev) != 0)
  1431. panic("Could not allocate MII IRQ!");
  1432. #endif
  1433. dev->base_addr = (unsigned long)fecp;
  1434. /* The FEC Ethernet specific entries in the device structure. */
  1435. dev->open = fec_enet_open;
  1436. dev->hard_start_xmit = fec_enet_start_xmit;
  1437. dev->tx_timeout = fec_timeout;
  1438. dev->watchdog_timeo = TX_TIMEOUT;
  1439. dev->stop = fec_enet_close;
  1440. dev->get_stats = fec_enet_get_stats;
  1441. dev->set_multicast_list = set_multicast_list;
  1442. #ifdef CONFIG_USE_MDIO
  1443. for (i=0; i<NMII-1; i++)
  1444. mii_cmds[i].mii_next = &mii_cmds[i+1];
  1445. mii_free = mii_cmds;
  1446. #endif /* CONFIG_USE_MDIO */
  1447. /* Configure all of port D for MII.
  1448. */
  1449. immap->im_ioport.iop_pdpar = 0x1fff;
  1450. /* Bits moved from Rev. D onward.
  1451. */
  1452. if ((mfspr(SPRN_IMMR) & 0xffff) < 0x0501)
  1453. immap->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  1454. else
  1455. immap->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  1456. #ifdef CONFIG_USE_MDIO
  1457. /* Set MII speed to 2.5 MHz
  1458. */
  1459. fecp->fec_mii_speed = fep->phy_speed =
  1460. (( (bd->bi_intfreq + 500000) / 2500000 / 2 ) & 0x3F ) << 1;
  1461. #else
  1462. fecp->fec_mii_speed = 0; /* turn off MDIO */
  1463. #endif /* CONFIG_USE_MDIO */
  1464. err = register_netdev(dev);
  1465. if (err) {
  1466. free_netdev(dev);
  1467. return err;
  1468. }
  1469. printk ("%s: FEC ENET Version 0.2, FEC irq %d"
  1470. #ifdef PHY_INTERRUPT
  1471. ", MII irq %d"
  1472. #endif
  1473. ", addr ",
  1474. dev->name, FEC_INTERRUPT
  1475. #ifdef PHY_INTERRUPT
  1476. , PHY_INTERRUPT
  1477. #endif
  1478. );
  1479. for (i=0; i<6; i++)
  1480. printk("%02x%c", dev->dev_addr[i], (i==5) ? '\n' : ':');
  1481. #ifdef CONFIG_USE_MDIO /* start in full duplex mode, and negotiate speed */
  1482. fec_restart (dev, 1);
  1483. #else /* always use half duplex mode only */
  1484. fec_restart (dev, 0);
  1485. #endif
  1486. #ifdef CONFIG_USE_MDIO
  1487. /* Queue up command to detect the PHY and initialize the
  1488. * remainder of the interface.
  1489. */
  1490. fep->phy_id_done = 0;
  1491. fep->phy_addr = 0;
  1492. mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
  1493. #endif /* CONFIG_USE_MDIO */
  1494. return 0;
  1495. }
  1496. module_init(fec_enet_init);
  1497. /* This function is called to start or restart the FEC during a link
  1498. * change. This only happens when switching between half and full
  1499. * duplex.
  1500. */
  1501. static void
  1502. fec_restart(struct net_device *dev, int duplex)
  1503. {
  1504. struct fec_enet_private *fep;
  1505. int i;
  1506. volatile cbd_t *bdp;
  1507. volatile immap_t *immap;
  1508. volatile fec_t *fecp;
  1509. immap = (immap_t *)IMAP_ADDR; /* pointer to internal registers */
  1510. fecp = &(immap->im_cpm.cp_fec);
  1511. fep = dev->priv;
  1512. /* Whack a reset. We should wait for this.
  1513. */
  1514. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  1515. for (i = 0;
  1516. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  1517. ++i) {
  1518. udelay(1);
  1519. }
  1520. if (i == FEC_RESET_DELAY) {
  1521. printk ("FEC Reset timeout!\n");
  1522. }
  1523. /* Set station address.
  1524. */
  1525. fecp->fec_addr_low = (my_enet_addr[0] << 16) | my_enet_addr[1];
  1526. fecp->fec_addr_high = my_enet_addr[2];
  1527. /* Reset all multicast.
  1528. */
  1529. fecp->fec_hash_table_high = 0;
  1530. fecp->fec_hash_table_low = 0;
  1531. /* Set maximum receive buffer size.
  1532. */
  1533. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  1534. fecp->fec_r_hash = PKT_MAXBUF_SIZE;
  1535. /* Set receive and transmit descriptor base.
  1536. */
  1537. fecp->fec_r_des_start = iopa((uint)(fep->rx_bd_base));
  1538. fecp->fec_x_des_start = iopa((uint)(fep->tx_bd_base));
  1539. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  1540. fep->cur_rx = fep->rx_bd_base;
  1541. /* Reset SKB transmit buffers.
  1542. */
  1543. fep->skb_cur = fep->skb_dirty = 0;
  1544. for (i=0; i<=TX_RING_MOD_MASK; i++) {
  1545. if (fep->tx_skbuff[i] != NULL) {
  1546. dev_kfree_skb(fep->tx_skbuff[i]);
  1547. fep->tx_skbuff[i] = NULL;
  1548. }
  1549. }
  1550. /* Initialize the receive buffer descriptors.
  1551. */
  1552. bdp = fep->rx_bd_base;
  1553. for (i=0; i<RX_RING_SIZE; i++) {
  1554. /* Initialize the BD for every fragment in the page.
  1555. */
  1556. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1557. bdp++;
  1558. }
  1559. /* Set the last buffer to wrap.
  1560. */
  1561. bdp--;
  1562. bdp->cbd_sc |= BD_SC_WRAP;
  1563. /* ...and the same for transmmit.
  1564. */
  1565. bdp = fep->tx_bd_base;
  1566. for (i=0; i<TX_RING_SIZE; i++) {
  1567. /* Initialize the BD for every fragment in the page.
  1568. */
  1569. bdp->cbd_sc = 0;
  1570. bdp->cbd_bufaddr = 0;
  1571. bdp++;
  1572. }
  1573. /* Set the last buffer to wrap.
  1574. */
  1575. bdp--;
  1576. bdp->cbd_sc |= BD_SC_WRAP;
  1577. /* Enable MII mode.
  1578. */
  1579. if (duplex) {
  1580. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE; /* MII enable */
  1581. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN; /* FD enable */
  1582. }
  1583. else {
  1584. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  1585. fecp->fec_x_cntrl = 0;
  1586. }
  1587. fep->full_duplex = duplex;
  1588. /* Enable big endian and don't care about SDMA FC.
  1589. */
  1590. fecp->fec_fun_code = 0x78000000;
  1591. #ifdef CONFIG_USE_MDIO
  1592. /* Set MII speed.
  1593. */
  1594. fecp->fec_mii_speed = fep->phy_speed;
  1595. #endif /* CONFIG_USE_MDIO */
  1596. /* Clear any outstanding interrupt.
  1597. */
  1598. fecp->fec_ievent = 0xffc0;
  1599. fecp->fec_ivec = (FEC_INTERRUPT/2) << 29;
  1600. /* Enable interrupts we wish to service.
  1601. */
  1602. fecp->fec_imask = ( FEC_ENET_TXF | FEC_ENET_TXB |
  1603. FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_MII );
  1604. /* And last, enable the transmit and receive processing.
  1605. */
  1606. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  1607. fecp->fec_r_des_active = 0x01000000;
  1608. }
  1609. static void
  1610. fec_stop(struct net_device *dev)
  1611. {
  1612. volatile immap_t *immap;
  1613. volatile fec_t *fecp;
  1614. struct fec_enet_private *fep;
  1615. int i;
  1616. immap = (immap_t *)IMAP_ADDR; /* pointer to internal registers */
  1617. fecp = &(immap->im_cpm.cp_fec);
  1618. if ((fecp->fec_ecntrl & FEC_ECNTRL_ETHER_EN) == 0)
  1619. return; /* already down */
  1620. fep = dev->priv;
  1621. fecp->fec_x_cntrl = 0x01; /* Graceful transmit stop */
  1622. for (i = 0;
  1623. ((fecp->fec_ievent & 0x10000000) == 0) && (i < FEC_RESET_DELAY);
  1624. ++i) {
  1625. udelay(1);
  1626. }
  1627. if (i == FEC_RESET_DELAY) {
  1628. printk ("FEC timeout on graceful transmit stop\n");
  1629. }
  1630. /* Clear outstanding MII command interrupts.
  1631. */
  1632. fecp->fec_ievent = FEC_ENET_MII;
  1633. /* Enable MII command finished interrupt
  1634. */
  1635. fecp->fec_ivec = (FEC_INTERRUPT/2) << 29;
  1636. fecp->fec_imask = FEC_ENET_MII;
  1637. #ifdef CONFIG_USE_MDIO
  1638. /* Set MII speed.
  1639. */
  1640. fecp->fec_mii_speed = fep->phy_speed;
  1641. #endif /* CONFIG_USE_MDIO */
  1642. /* Disable FEC
  1643. */
  1644. fecp->fec_ecntrl &= ~(FEC_ECNTRL_ETHER_EN);
  1645. }