fec.c 50 KB

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