fec.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  1. /*
  2. * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
  3. * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
  4. *
  5. * Right now, I am very wasteful with the buffers. I allocate memory
  6. * pages and then divide them into 2K frame buffers. This way I know I
  7. * have buffers large enough to hold one frame within one buffer descriptor.
  8. * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  9. * will be much more memory efficient and will easily handle lots of
  10. * small packets.
  11. *
  12. * Much better multiple PHY support by Magnus Damm.
  13. * Copyright (c) 2000 Ericsson Radio Systems AB.
  14. *
  15. * Support for FEC controller of ColdFire processors.
  16. * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
  17. *
  18. * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
  19. * Copyright (c) 2004-2006 Macq Electronique SA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/string.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/errno.h>
  26. #include <linux/ioport.h>
  27. #include <linux/slab.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/pci.h>
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/etherdevice.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/bitops.h>
  38. #include <linux/io.h>
  39. #include <linux/irq.h>
  40. #include <linux/clk.h>
  41. #include <linux/platform_device.h>
  42. #include <asm/cacheflush.h>
  43. #ifndef CONFIG_ARCH_MXC
  44. #include <asm/coldfire.h>
  45. #include <asm/mcfsim.h>
  46. #endif
  47. #include "fec.h"
  48. #ifdef CONFIG_ARCH_MXC
  49. #include <mach/hardware.h>
  50. #define FEC_ALIGNMENT 0xf
  51. #else
  52. #define FEC_ALIGNMENT 0x3
  53. #endif
  54. /*
  55. * Define the fixed address of the FEC hardware.
  56. */
  57. #if defined(CONFIG_M5272)
  58. #define HAVE_mii_link_interrupt
  59. static unsigned char fec_mac_default[] = {
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. };
  62. /*
  63. * Some hardware gets it MAC address out of local flash memory.
  64. * if this is non-zero then assume it is the address to get MAC from.
  65. */
  66. #if defined(CONFIG_NETtel)
  67. #define FEC_FLASHMAC 0xf0006006
  68. #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
  69. #define FEC_FLASHMAC 0xf0006000
  70. #elif defined(CONFIG_CANCam)
  71. #define FEC_FLASHMAC 0xf0020000
  72. #elif defined (CONFIG_M5272C3)
  73. #define FEC_FLASHMAC (0xffe04000 + 4)
  74. #elif defined(CONFIG_MOD5272)
  75. #define FEC_FLASHMAC 0xffc0406b
  76. #else
  77. #define FEC_FLASHMAC 0
  78. #endif
  79. #endif /* CONFIG_M5272 */
  80. /* Forward declarations of some structures to support different PHYs */
  81. typedef struct {
  82. uint mii_data;
  83. void (*funct)(uint mii_reg, struct net_device *dev);
  84. } phy_cmd_t;
  85. typedef struct {
  86. uint id;
  87. char *name;
  88. const phy_cmd_t *config;
  89. const phy_cmd_t *startup;
  90. const phy_cmd_t *ack_int;
  91. const phy_cmd_t *shutdown;
  92. } phy_info_t;
  93. /* The number of Tx and Rx buffers. These are allocated from the page
  94. * pool. The code may assume these are power of two, so it it best
  95. * to keep them that size.
  96. * We don't need to allocate pages for the transmitter. We just use
  97. * the skbuffer directly.
  98. */
  99. #define FEC_ENET_RX_PAGES 8
  100. #define FEC_ENET_RX_FRSIZE 2048
  101. #define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
  102. #define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
  103. #define FEC_ENET_TX_FRSIZE 2048
  104. #define FEC_ENET_TX_FRPPG (PAGE_SIZE / FEC_ENET_TX_FRSIZE)
  105. #define TX_RING_SIZE 16 /* Must be power of two */
  106. #define TX_RING_MOD_MASK 15 /* for this to work */
  107. #if (((RX_RING_SIZE + TX_RING_SIZE) * 8) > PAGE_SIZE)
  108. #error "FEC: descriptor ring size constants too large"
  109. #endif
  110. /* Interrupt events/masks. */
  111. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  112. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  113. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  114. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  115. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  116. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  117. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  118. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  119. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  120. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  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. /*
  127. * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
  128. * size bits. Other FEC hardware does not, so we need to take that into
  129. * account when setting it.
  130. */
  131. #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
  132. defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARCH_MXC)
  133. #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16)
  134. #else
  135. #define OPT_FRAME_SIZE 0
  136. #endif
  137. /* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
  138. * tx_bd_base always point to the base of the buffer descriptors. The
  139. * cur_rx and cur_tx point to the currently available buffer.
  140. * The dirty_tx tracks the current buffer that is being sent by the
  141. * controller. The cur_tx and dirty_tx are equal under both completely
  142. * empty and completely full conditions. The empty/ready indicator in
  143. * the buffer descriptor determines the actual condition.
  144. */
  145. struct fec_enet_private {
  146. /* Hardware registers of the FEC device */
  147. void __iomem *hwp;
  148. struct net_device *netdev;
  149. struct clk *clk;
  150. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  151. unsigned char *tx_bounce[TX_RING_SIZE];
  152. struct sk_buff* tx_skbuff[TX_RING_SIZE];
  153. ushort skb_cur;
  154. ushort skb_dirty;
  155. /* CPM dual port RAM relative addresses */
  156. dma_addr_t bd_dma;
  157. /* Address of Rx and Tx buffers */
  158. struct bufdesc *rx_bd_base;
  159. struct bufdesc *tx_bd_base;
  160. /* The next free ring entry */
  161. struct bufdesc *cur_rx, *cur_tx;
  162. /* The ring entries to be free()ed */
  163. struct bufdesc *dirty_tx;
  164. uint tx_full;
  165. /* hold while accessing the HW like ringbuffer for tx/rx but not MAC */
  166. spinlock_t hw_lock;
  167. /* hold while accessing the mii_list_t() elements */
  168. spinlock_t mii_lock;
  169. uint phy_id;
  170. uint phy_id_done;
  171. uint phy_status;
  172. uint phy_speed;
  173. phy_info_t const *phy;
  174. struct work_struct phy_task;
  175. uint sequence_done;
  176. uint mii_phy_task_queued;
  177. uint phy_addr;
  178. int index;
  179. int opened;
  180. int link;
  181. int old_link;
  182. int full_duplex;
  183. };
  184. static void fec_enet_mii(struct net_device *dev);
  185. static irqreturn_t fec_enet_interrupt(int irq, void * dev_id);
  186. static void fec_enet_tx(struct net_device *dev);
  187. static void fec_enet_rx(struct net_device *dev);
  188. static int fec_enet_close(struct net_device *dev);
  189. static void fec_restart(struct net_device *dev, int duplex);
  190. static void fec_stop(struct net_device *dev);
  191. /* MII processing. We keep this as simple as possible. Requests are
  192. * placed on the list (if there is room). When the request is finished
  193. * by the MII, an optional function may be called.
  194. */
  195. typedef struct mii_list {
  196. uint mii_regval;
  197. void (*mii_func)(uint val, struct net_device *dev);
  198. struct mii_list *mii_next;
  199. } mii_list_t;
  200. #define NMII 20
  201. static mii_list_t mii_cmds[NMII];
  202. static mii_list_t *mii_free;
  203. static mii_list_t *mii_head;
  204. static mii_list_t *mii_tail;
  205. static int mii_queue(struct net_device *dev, int request,
  206. void (*func)(uint, struct net_device *));
  207. /* Make MII read/write commands for the FEC */
  208. #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
  209. #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
  210. (VAL & 0xffff))
  211. #define mk_mii_end 0
  212. /* Transmitter timeout */
  213. #define TX_TIMEOUT (2 * HZ)
  214. /* Register definitions for the PHY */
  215. #define MII_REG_CR 0 /* Control Register */
  216. #define MII_REG_SR 1 /* Status Register */
  217. #define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
  218. #define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
  219. #define MII_REG_ANAR 4 /* A-N Advertisement Register */
  220. #define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
  221. #define MII_REG_ANER 6 /* A-N Expansion Register */
  222. #define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
  223. #define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
  224. /* values for phy_status */
  225. #define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
  226. #define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
  227. #define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
  228. #define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
  229. #define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
  230. #define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
  231. #define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
  232. #define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
  233. #define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
  234. #define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
  235. #define PHY_STAT_SPMASK 0xf000 /* mask for speed */
  236. #define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
  237. #define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
  238. #define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
  239. #define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
  240. static int
  241. fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  242. {
  243. struct fec_enet_private *fep = netdev_priv(dev);
  244. struct bufdesc *bdp;
  245. unsigned short status;
  246. unsigned long flags;
  247. if (!fep->link) {
  248. /* Link is down or autonegotiation is in progress. */
  249. return 1;
  250. }
  251. spin_lock_irqsave(&fep->hw_lock, flags);
  252. /* Fill in a Tx ring entry */
  253. bdp = fep->cur_tx;
  254. status = bdp->cbd_sc;
  255. if (status & BD_ENET_TX_READY) {
  256. /* Ooops. All transmit buffers are full. Bail out.
  257. * This should not happen, since dev->tbusy should be set.
  258. */
  259. printk("%s: tx queue full!.\n", dev->name);
  260. spin_unlock_irqrestore(&fep->hw_lock, flags);
  261. return 1;
  262. }
  263. /* Clear all of the status flags */
  264. status &= ~BD_ENET_TX_STATS;
  265. /* Set buffer length and buffer pointer */
  266. bdp->cbd_bufaddr = __pa(skb->data);
  267. bdp->cbd_datlen = skb->len;
  268. /*
  269. * On some FEC implementations data must be aligned on
  270. * 4-byte boundaries. Use bounce buffers to copy data
  271. * and get it aligned. Ugh.
  272. */
  273. if (bdp->cbd_bufaddr & FEC_ALIGNMENT) {
  274. unsigned int index;
  275. index = bdp - fep->tx_bd_base;
  276. memcpy(fep->tx_bounce[index], (void *)skb->data, skb->len);
  277. bdp->cbd_bufaddr = __pa(fep->tx_bounce[index]);
  278. }
  279. /* Save skb pointer */
  280. fep->tx_skbuff[fep->skb_cur] = skb;
  281. dev->stats.tx_bytes += skb->len;
  282. fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
  283. /* Push the data cache so the CPM does not get stale memory
  284. * data.
  285. */
  286. dma_sync_single(NULL, bdp->cbd_bufaddr,
  287. bdp->cbd_datlen, DMA_TO_DEVICE);
  288. /* Send it on its way. Tell FEC it's ready, interrupt when done,
  289. * it's the last BD of the frame, and to put the CRC on the end.
  290. */
  291. status |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
  292. | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  293. bdp->cbd_sc = status;
  294. dev->trans_start = jiffies;
  295. /* Trigger transmission start */
  296. writel(0, fep->hwp + FEC_X_DES_ACTIVE);
  297. /* If this was the last BD in the ring, start at the beginning again. */
  298. if (status & BD_ENET_TX_WRAP)
  299. bdp = fep->tx_bd_base;
  300. else
  301. bdp++;
  302. if (bdp == fep->dirty_tx) {
  303. fep->tx_full = 1;
  304. netif_stop_queue(dev);
  305. }
  306. fep->cur_tx = bdp;
  307. spin_unlock_irqrestore(&fep->hw_lock, flags);
  308. return 0;
  309. }
  310. static void
  311. fec_timeout(struct net_device *dev)
  312. {
  313. struct fec_enet_private *fep = netdev_priv(dev);
  314. dev->stats.tx_errors++;
  315. fec_restart(dev, fep->full_duplex);
  316. netif_wake_queue(dev);
  317. }
  318. static irqreturn_t
  319. fec_enet_interrupt(int irq, void * dev_id)
  320. {
  321. struct net_device *dev = dev_id;
  322. struct fec_enet_private *fep = netdev_priv(dev);
  323. uint int_events;
  324. irqreturn_t ret = IRQ_NONE;
  325. do {
  326. int_events = readl(fep->hwp + FEC_IEVENT);
  327. writel(int_events, fep->hwp + FEC_IEVENT);
  328. if (int_events & FEC_ENET_RXF) {
  329. ret = IRQ_HANDLED;
  330. fec_enet_rx(dev);
  331. }
  332. /* Transmit OK, or non-fatal error. Update the buffer
  333. * descriptors. FEC handles all errors, we just discover
  334. * them as part of the transmit process.
  335. */
  336. if (int_events & FEC_ENET_TXF) {
  337. ret = IRQ_HANDLED;
  338. fec_enet_tx(dev);
  339. }
  340. if (int_events & FEC_ENET_MII) {
  341. ret = IRQ_HANDLED;
  342. fec_enet_mii(dev);
  343. }
  344. } while (int_events);
  345. return ret;
  346. }
  347. static void
  348. fec_enet_tx(struct net_device *dev)
  349. {
  350. struct fec_enet_private *fep;
  351. struct bufdesc *bdp;
  352. unsigned short status;
  353. struct sk_buff *skb;
  354. fep = netdev_priv(dev);
  355. spin_lock_irq(&fep->hw_lock);
  356. bdp = fep->dirty_tx;
  357. while (((status = bdp->cbd_sc) & BD_ENET_TX_READY) == 0) {
  358. if (bdp == fep->cur_tx && fep->tx_full == 0) break;
  359. skb = fep->tx_skbuff[fep->skb_dirty];
  360. /* Check for errors. */
  361. if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  362. BD_ENET_TX_RL | BD_ENET_TX_UN |
  363. BD_ENET_TX_CSL)) {
  364. dev->stats.tx_errors++;
  365. if (status & BD_ENET_TX_HB) /* No heartbeat */
  366. dev->stats.tx_heartbeat_errors++;
  367. if (status & BD_ENET_TX_LC) /* Late collision */
  368. dev->stats.tx_window_errors++;
  369. if (status & BD_ENET_TX_RL) /* Retrans limit */
  370. dev->stats.tx_aborted_errors++;
  371. if (status & BD_ENET_TX_UN) /* Underrun */
  372. dev->stats.tx_fifo_errors++;
  373. if (status & BD_ENET_TX_CSL) /* Carrier lost */
  374. dev->stats.tx_carrier_errors++;
  375. } else {
  376. dev->stats.tx_packets++;
  377. }
  378. if (status & BD_ENET_TX_READY)
  379. printk("HEY! Enet xmit interrupt and TX_READY.\n");
  380. /* Deferred means some collisions occurred during transmit,
  381. * but we eventually sent the packet OK.
  382. */
  383. if (status & BD_ENET_TX_DEF)
  384. dev->stats.collisions++;
  385. /* Free the sk buffer associated with this last transmit */
  386. dev_kfree_skb_any(skb);
  387. fep->tx_skbuff[fep->skb_dirty] = NULL;
  388. fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
  389. /* Update pointer to next buffer descriptor to be transmitted */
  390. if (status & BD_ENET_TX_WRAP)
  391. bdp = fep->tx_bd_base;
  392. else
  393. bdp++;
  394. /* Since we have freed up a buffer, the ring is no longer full
  395. */
  396. if (fep->tx_full) {
  397. fep->tx_full = 0;
  398. if (netif_queue_stopped(dev))
  399. netif_wake_queue(dev);
  400. }
  401. }
  402. fep->dirty_tx = bdp;
  403. spin_unlock_irq(&fep->hw_lock);
  404. }
  405. /* During a receive, the cur_rx points to the current incoming buffer.
  406. * When we update through the ring, if the next incoming buffer has
  407. * not been given to the system, we just set the empty indicator,
  408. * effectively tossing the packet.
  409. */
  410. static void
  411. fec_enet_rx(struct net_device *dev)
  412. {
  413. struct fec_enet_private *fep = netdev_priv(dev);
  414. struct bufdesc *bdp;
  415. unsigned short status;
  416. struct sk_buff *skb;
  417. ushort pkt_len;
  418. __u8 *data;
  419. #ifdef CONFIG_M532x
  420. flush_cache_all();
  421. #endif
  422. spin_lock_irq(&fep->hw_lock);
  423. /* First, grab all of the stats for the incoming packet.
  424. * These get messed up if we get called due to a busy condition.
  425. */
  426. bdp = fep->cur_rx;
  427. while (!((status = bdp->cbd_sc) & BD_ENET_RX_EMPTY)) {
  428. /* Since we have allocated space to hold a complete frame,
  429. * the last indicator should be set.
  430. */
  431. if ((status & BD_ENET_RX_LAST) == 0)
  432. printk("FEC ENET: rcv is not +last\n");
  433. if (!fep->opened)
  434. goto rx_processing_done;
  435. /* Check for errors. */
  436. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  437. BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  438. dev->stats.rx_errors++;
  439. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
  440. /* Frame too long or too short. */
  441. dev->stats.rx_length_errors++;
  442. }
  443. if (status & BD_ENET_RX_NO) /* Frame alignment */
  444. dev->stats.rx_frame_errors++;
  445. if (status & BD_ENET_RX_CR) /* CRC Error */
  446. dev->stats.rx_crc_errors++;
  447. if (status & BD_ENET_RX_OV) /* FIFO overrun */
  448. dev->stats.rx_fifo_errors++;
  449. }
  450. /* Report late collisions as a frame error.
  451. * On this error, the BD is closed, but we don't know what we
  452. * have in the buffer. So, just drop this frame on the floor.
  453. */
  454. if (status & BD_ENET_RX_CL) {
  455. dev->stats.rx_errors++;
  456. dev->stats.rx_frame_errors++;
  457. goto rx_processing_done;
  458. }
  459. /* Process the incoming frame. */
  460. dev->stats.rx_packets++;
  461. pkt_len = bdp->cbd_datlen;
  462. dev->stats.rx_bytes += pkt_len;
  463. data = (__u8*)__va(bdp->cbd_bufaddr);
  464. dma_sync_single(NULL, (unsigned long)__pa(data),
  465. pkt_len - 4, DMA_FROM_DEVICE);
  466. /* This does 16 byte alignment, exactly what we need.
  467. * The packet length includes FCS, but we don't want to
  468. * include that when passing upstream as it messes up
  469. * bridging applications.
  470. */
  471. skb = dev_alloc_skb(pkt_len - 4 + NET_IP_ALIGN);
  472. if (unlikely(!skb)) {
  473. printk("%s: Memory squeeze, dropping packet.\n",
  474. dev->name);
  475. dev->stats.rx_dropped++;
  476. } else {
  477. skb_reserve(skb, NET_IP_ALIGN);
  478. skb_put(skb, pkt_len - 4); /* Make room */
  479. skb_copy_to_linear_data(skb, data, pkt_len - 4);
  480. skb->protocol = eth_type_trans(skb, dev);
  481. netif_rx(skb);
  482. }
  483. rx_processing_done:
  484. /* Clear the status flags for this buffer */
  485. status &= ~BD_ENET_RX_STATS;
  486. /* Mark the buffer empty */
  487. status |= BD_ENET_RX_EMPTY;
  488. bdp->cbd_sc = status;
  489. /* Update BD pointer to next entry */
  490. if (status & BD_ENET_RX_WRAP)
  491. bdp = fep->rx_bd_base;
  492. else
  493. bdp++;
  494. /* Doing this here will keep the FEC running while we process
  495. * incoming frames. On a heavily loaded network, we should be
  496. * able to keep up at the expense of system resources.
  497. */
  498. writel(0, fep->hwp + FEC_R_DES_ACTIVE);
  499. }
  500. fep->cur_rx = bdp;
  501. spin_unlock_irq(&fep->hw_lock);
  502. }
  503. /* called from interrupt context */
  504. static void
  505. fec_enet_mii(struct net_device *dev)
  506. {
  507. struct fec_enet_private *fep;
  508. mii_list_t *mip;
  509. fep = netdev_priv(dev);
  510. spin_lock_irq(&fep->mii_lock);
  511. if ((mip = mii_head) == NULL) {
  512. printk("MII and no head!\n");
  513. goto unlock;
  514. }
  515. if (mip->mii_func != NULL)
  516. (*(mip->mii_func))(readl(fep->hwp + FEC_MII_DATA), dev);
  517. mii_head = mip->mii_next;
  518. mip->mii_next = mii_free;
  519. mii_free = mip;
  520. if ((mip = mii_head) != NULL)
  521. writel(mip->mii_regval, fep->hwp + FEC_MII_DATA);
  522. unlock:
  523. spin_unlock_irq(&fep->mii_lock);
  524. }
  525. static int
  526. mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
  527. {
  528. struct fec_enet_private *fep;
  529. unsigned long flags;
  530. mii_list_t *mip;
  531. int retval;
  532. /* Add PHY address to register command */
  533. fep = netdev_priv(dev);
  534. spin_lock_irqsave(&fep->mii_lock, flags);
  535. regval |= fep->phy_addr << 23;
  536. retval = 0;
  537. if ((mip = mii_free) != NULL) {
  538. mii_free = mip->mii_next;
  539. mip->mii_regval = regval;
  540. mip->mii_func = func;
  541. mip->mii_next = NULL;
  542. if (mii_head) {
  543. mii_tail->mii_next = mip;
  544. mii_tail = mip;
  545. } else {
  546. mii_head = mii_tail = mip;
  547. writel(regval, fep->hwp + FEC_MII_DATA);
  548. }
  549. } else {
  550. retval = 1;
  551. }
  552. spin_unlock_irqrestore(&fep->mii_lock, flags);
  553. return retval;
  554. }
  555. static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
  556. {
  557. if(!c)
  558. return;
  559. for (; c->mii_data != mk_mii_end; c++)
  560. mii_queue(dev, c->mii_data, c->funct);
  561. }
  562. static void mii_parse_sr(uint mii_reg, struct net_device *dev)
  563. {
  564. struct fec_enet_private *fep = netdev_priv(dev);
  565. volatile uint *s = &(fep->phy_status);
  566. uint status;
  567. status = *s & ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
  568. if (mii_reg & 0x0004)
  569. status |= PHY_STAT_LINK;
  570. if (mii_reg & 0x0010)
  571. status |= PHY_STAT_FAULT;
  572. if (mii_reg & 0x0020)
  573. status |= PHY_STAT_ANC;
  574. *s = status;
  575. }
  576. static void mii_parse_cr(uint mii_reg, struct net_device *dev)
  577. {
  578. struct fec_enet_private *fep = netdev_priv(dev);
  579. volatile uint *s = &(fep->phy_status);
  580. uint status;
  581. status = *s & ~(PHY_CONF_ANE | PHY_CONF_LOOP);
  582. if (mii_reg & 0x1000)
  583. status |= PHY_CONF_ANE;
  584. if (mii_reg & 0x4000)
  585. status |= PHY_CONF_LOOP;
  586. *s = status;
  587. }
  588. static void mii_parse_anar(uint mii_reg, struct net_device *dev)
  589. {
  590. struct fec_enet_private *fep = netdev_priv(dev);
  591. volatile uint *s = &(fep->phy_status);
  592. uint status;
  593. status = *s & ~(PHY_CONF_SPMASK);
  594. if (mii_reg & 0x0020)
  595. status |= PHY_CONF_10HDX;
  596. if (mii_reg & 0x0040)
  597. status |= PHY_CONF_10FDX;
  598. if (mii_reg & 0x0080)
  599. status |= PHY_CONF_100HDX;
  600. if (mii_reg & 0x00100)
  601. status |= PHY_CONF_100FDX;
  602. *s = status;
  603. }
  604. /* ------------------------------------------------------------------------- */
  605. /* The Level one LXT970 is used by many boards */
  606. #define MII_LXT970_MIRROR 16 /* Mirror register */
  607. #define MII_LXT970_IER 17 /* Interrupt Enable Register */
  608. #define MII_LXT970_ISR 18 /* Interrupt Status Register */
  609. #define MII_LXT970_CONFIG 19 /* Configuration Register */
  610. #define MII_LXT970_CSR 20 /* Chip Status Register */
  611. static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
  612. {
  613. struct fec_enet_private *fep = netdev_priv(dev);
  614. volatile uint *s = &(fep->phy_status);
  615. uint status;
  616. status = *s & ~(PHY_STAT_SPMASK);
  617. if (mii_reg & 0x0800) {
  618. if (mii_reg & 0x1000)
  619. status |= PHY_STAT_100FDX;
  620. else
  621. status |= PHY_STAT_100HDX;
  622. } else {
  623. if (mii_reg & 0x1000)
  624. status |= PHY_STAT_10FDX;
  625. else
  626. status |= PHY_STAT_10HDX;
  627. }
  628. *s = status;
  629. }
  630. static phy_cmd_t const phy_cmd_lxt970_config[] = {
  631. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  632. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  633. { mk_mii_end, }
  634. };
  635. static phy_cmd_t const phy_cmd_lxt970_startup[] = { /* enable interrupts */
  636. { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
  637. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  638. { mk_mii_end, }
  639. };
  640. static phy_cmd_t const phy_cmd_lxt970_ack_int[] = {
  641. /* read SR and ISR to acknowledge */
  642. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  643. { mk_mii_read(MII_LXT970_ISR), NULL },
  644. /* find out the current status */
  645. { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
  646. { mk_mii_end, }
  647. };
  648. static phy_cmd_t const phy_cmd_lxt970_shutdown[] = { /* disable interrupts */
  649. { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
  650. { mk_mii_end, }
  651. };
  652. static phy_info_t const phy_info_lxt970 = {
  653. .id = 0x07810000,
  654. .name = "LXT970",
  655. .config = phy_cmd_lxt970_config,
  656. .startup = phy_cmd_lxt970_startup,
  657. .ack_int = phy_cmd_lxt970_ack_int,
  658. .shutdown = phy_cmd_lxt970_shutdown
  659. };
  660. /* ------------------------------------------------------------------------- */
  661. /* The Level one LXT971 is used on some of my custom boards */
  662. /* register definitions for the 971 */
  663. #define MII_LXT971_PCR 16 /* Port Control Register */
  664. #define MII_LXT971_SR2 17 /* Status Register 2 */
  665. #define MII_LXT971_IER 18 /* Interrupt Enable Register */
  666. #define MII_LXT971_ISR 19 /* Interrupt Status Register */
  667. #define MII_LXT971_LCR 20 /* LED Control Register */
  668. #define MII_LXT971_TCR 30 /* Transmit Control Register */
  669. /*
  670. * I had some nice ideas of running the MDIO faster...
  671. * The 971 should support 8MHz and I tried it, but things acted really
  672. * weird, so 2.5 MHz ought to be enough for anyone...
  673. */
  674. static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
  675. {
  676. struct fec_enet_private *fep = netdev_priv(dev);
  677. volatile uint *s = &(fep->phy_status);
  678. uint status;
  679. status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
  680. if (mii_reg & 0x0400) {
  681. fep->link = 1;
  682. status |= PHY_STAT_LINK;
  683. } else {
  684. fep->link = 0;
  685. }
  686. if (mii_reg & 0x0080)
  687. status |= PHY_STAT_ANC;
  688. if (mii_reg & 0x4000) {
  689. if (mii_reg & 0x0200)
  690. status |= PHY_STAT_100FDX;
  691. else
  692. status |= PHY_STAT_100HDX;
  693. } else {
  694. if (mii_reg & 0x0200)
  695. status |= PHY_STAT_10FDX;
  696. else
  697. status |= PHY_STAT_10HDX;
  698. }
  699. if (mii_reg & 0x0008)
  700. status |= PHY_STAT_FAULT;
  701. *s = status;
  702. }
  703. static phy_cmd_t const phy_cmd_lxt971_config[] = {
  704. /* limit to 10MBit because my prototype board
  705. * doesn't work with 100. */
  706. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  707. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  708. { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
  709. { mk_mii_end, }
  710. };
  711. static phy_cmd_t const phy_cmd_lxt971_startup[] = { /* enable interrupts */
  712. { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
  713. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  714. { mk_mii_write(MII_LXT971_LCR, 0xd422), NULL }, /* LED config */
  715. /* Somehow does the 971 tell me that the link is down
  716. * the first read after power-up.
  717. * read here to get a valid value in ack_int */
  718. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  719. { mk_mii_end, }
  720. };
  721. static phy_cmd_t const phy_cmd_lxt971_ack_int[] = {
  722. /* acknowledge the int before reading status ! */
  723. { mk_mii_read(MII_LXT971_ISR), NULL },
  724. /* find out the current status */
  725. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  726. { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
  727. { mk_mii_end, }
  728. };
  729. static phy_cmd_t const phy_cmd_lxt971_shutdown[] = { /* disable interrupts */
  730. { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
  731. { mk_mii_end, }
  732. };
  733. static phy_info_t const phy_info_lxt971 = {
  734. .id = 0x0001378e,
  735. .name = "LXT971",
  736. .config = phy_cmd_lxt971_config,
  737. .startup = phy_cmd_lxt971_startup,
  738. .ack_int = phy_cmd_lxt971_ack_int,
  739. .shutdown = phy_cmd_lxt971_shutdown
  740. };
  741. /* ------------------------------------------------------------------------- */
  742. /* The Quality Semiconductor QS6612 is used on the RPX CLLF */
  743. /* register definitions */
  744. #define MII_QS6612_MCR 17 /* Mode Control Register */
  745. #define MII_QS6612_FTR 27 /* Factory Test Register */
  746. #define MII_QS6612_MCO 28 /* Misc. Control Register */
  747. #define MII_QS6612_ISR 29 /* Interrupt Source Register */
  748. #define MII_QS6612_IMR 30 /* Interrupt Mask Register */
  749. #define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
  750. static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
  751. {
  752. struct fec_enet_private *fep = netdev_priv(dev);
  753. volatile uint *s = &(fep->phy_status);
  754. uint status;
  755. status = *s & ~(PHY_STAT_SPMASK);
  756. switch((mii_reg >> 2) & 7) {
  757. case 1: status |= PHY_STAT_10HDX; break;
  758. case 2: status |= PHY_STAT_100HDX; break;
  759. case 5: status |= PHY_STAT_10FDX; break;
  760. case 6: status |= PHY_STAT_100FDX; break;
  761. }
  762. *s = status;
  763. }
  764. static phy_cmd_t const phy_cmd_qs6612_config[] = {
  765. /* The PHY powers up isolated on the RPX,
  766. * so send a command to allow operation.
  767. */
  768. { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
  769. /* parse cr and anar to get some info */
  770. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  771. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  772. { mk_mii_end, }
  773. };
  774. static phy_cmd_t const phy_cmd_qs6612_startup[] = { /* enable interrupts */
  775. { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
  776. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  777. { mk_mii_end, }
  778. };
  779. static phy_cmd_t const phy_cmd_qs6612_ack_int[] = {
  780. /* we need to read ISR, SR and ANER to acknowledge */
  781. { mk_mii_read(MII_QS6612_ISR), NULL },
  782. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  783. { mk_mii_read(MII_REG_ANER), NULL },
  784. /* read pcr to get info */
  785. { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
  786. { mk_mii_end, }
  787. };
  788. static phy_cmd_t const phy_cmd_qs6612_shutdown[] = { /* disable interrupts */
  789. { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
  790. { mk_mii_end, }
  791. };
  792. static phy_info_t const phy_info_qs6612 = {
  793. .id = 0x00181440,
  794. .name = "QS6612",
  795. .config = phy_cmd_qs6612_config,
  796. .startup = phy_cmd_qs6612_startup,
  797. .ack_int = phy_cmd_qs6612_ack_int,
  798. .shutdown = phy_cmd_qs6612_shutdown
  799. };
  800. /* ------------------------------------------------------------------------- */
  801. /* AMD AM79C874 phy */
  802. /* register definitions for the 874 */
  803. #define MII_AM79C874_MFR 16 /* Miscellaneous Feature Register */
  804. #define MII_AM79C874_ICSR 17 /* Interrupt/Status Register */
  805. #define MII_AM79C874_DR 18 /* Diagnostic Register */
  806. #define MII_AM79C874_PMLR 19 /* Power and Loopback Register */
  807. #define MII_AM79C874_MCR 21 /* ModeControl Register */
  808. #define MII_AM79C874_DC 23 /* Disconnect Counter */
  809. #define MII_AM79C874_REC 24 /* Recieve Error Counter */
  810. static void mii_parse_am79c874_dr(uint mii_reg, struct net_device *dev)
  811. {
  812. struct fec_enet_private *fep = netdev_priv(dev);
  813. volatile uint *s = &(fep->phy_status);
  814. uint status;
  815. status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_ANC);
  816. if (mii_reg & 0x0080)
  817. status |= PHY_STAT_ANC;
  818. if (mii_reg & 0x0400)
  819. status |= ((mii_reg & 0x0800) ? PHY_STAT_100FDX : PHY_STAT_100HDX);
  820. else
  821. status |= ((mii_reg & 0x0800) ? PHY_STAT_10FDX : PHY_STAT_10HDX);
  822. *s = status;
  823. }
  824. static phy_cmd_t const phy_cmd_am79c874_config[] = {
  825. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  826. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  827. { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
  828. { mk_mii_end, }
  829. };
  830. static phy_cmd_t const phy_cmd_am79c874_startup[] = { /* enable interrupts */
  831. { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
  832. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  833. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  834. { mk_mii_end, }
  835. };
  836. static phy_cmd_t const phy_cmd_am79c874_ack_int[] = {
  837. /* find out the current status */
  838. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  839. { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
  840. /* we only need to read ISR to acknowledge */
  841. { mk_mii_read(MII_AM79C874_ICSR), NULL },
  842. { mk_mii_end, }
  843. };
  844. static phy_cmd_t const phy_cmd_am79c874_shutdown[] = { /* disable interrupts */
  845. { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
  846. { mk_mii_end, }
  847. };
  848. static phy_info_t const phy_info_am79c874 = {
  849. .id = 0x00022561,
  850. .name = "AM79C874",
  851. .config = phy_cmd_am79c874_config,
  852. .startup = phy_cmd_am79c874_startup,
  853. .ack_int = phy_cmd_am79c874_ack_int,
  854. .shutdown = phy_cmd_am79c874_shutdown
  855. };
  856. /* ------------------------------------------------------------------------- */
  857. /* Kendin KS8721BL phy */
  858. /* register definitions for the 8721 */
  859. #define MII_KS8721BL_RXERCR 21
  860. #define MII_KS8721BL_ICSR 27
  861. #define MII_KS8721BL_PHYCR 31
  862. static phy_cmd_t const phy_cmd_ks8721bl_config[] = {
  863. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  864. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  865. { mk_mii_end, }
  866. };
  867. static phy_cmd_t const phy_cmd_ks8721bl_startup[] = { /* enable interrupts */
  868. { mk_mii_write(MII_KS8721BL_ICSR, 0xff00), NULL },
  869. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  870. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  871. { mk_mii_end, }
  872. };
  873. static phy_cmd_t const phy_cmd_ks8721bl_ack_int[] = {
  874. /* find out the current status */
  875. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  876. /* we only need to read ISR to acknowledge */
  877. { mk_mii_read(MII_KS8721BL_ICSR), NULL },
  878. { mk_mii_end, }
  879. };
  880. static phy_cmd_t const phy_cmd_ks8721bl_shutdown[] = { /* disable interrupts */
  881. { mk_mii_write(MII_KS8721BL_ICSR, 0x0000), NULL },
  882. { mk_mii_end, }
  883. };
  884. static phy_info_t const phy_info_ks8721bl = {
  885. .id = 0x00022161,
  886. .name = "KS8721BL",
  887. .config = phy_cmd_ks8721bl_config,
  888. .startup = phy_cmd_ks8721bl_startup,
  889. .ack_int = phy_cmd_ks8721bl_ack_int,
  890. .shutdown = phy_cmd_ks8721bl_shutdown
  891. };
  892. /* ------------------------------------------------------------------------- */
  893. /* register definitions for the DP83848 */
  894. #define MII_DP8384X_PHYSTST 16 /* PHY Status Register */
  895. static void mii_parse_dp8384x_sr2(uint mii_reg, struct net_device *dev)
  896. {
  897. struct fec_enet_private *fep = netdev_priv(dev);
  898. volatile uint *s = &(fep->phy_status);
  899. *s &= ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
  900. /* Link up */
  901. if (mii_reg & 0x0001) {
  902. fep->link = 1;
  903. *s |= PHY_STAT_LINK;
  904. } else
  905. fep->link = 0;
  906. /* Status of link */
  907. if (mii_reg & 0x0010) /* Autonegotioation complete */
  908. *s |= PHY_STAT_ANC;
  909. if (mii_reg & 0x0002) { /* 10MBps? */
  910. if (mii_reg & 0x0004) /* Full Duplex? */
  911. *s |= PHY_STAT_10FDX;
  912. else
  913. *s |= PHY_STAT_10HDX;
  914. } else { /* 100 Mbps? */
  915. if (mii_reg & 0x0004) /* Full Duplex? */
  916. *s |= PHY_STAT_100FDX;
  917. else
  918. *s |= PHY_STAT_100HDX;
  919. }
  920. if (mii_reg & 0x0008)
  921. *s |= PHY_STAT_FAULT;
  922. }
  923. static phy_info_t phy_info_dp83848= {
  924. 0x020005c9,
  925. "DP83848",
  926. (const phy_cmd_t []) { /* config */
  927. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  928. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  929. { mk_mii_read(MII_DP8384X_PHYSTST), mii_parse_dp8384x_sr2 },
  930. { mk_mii_end, }
  931. },
  932. (const phy_cmd_t []) { /* startup - enable interrupts */
  933. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  934. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  935. { mk_mii_end, }
  936. },
  937. (const phy_cmd_t []) { /* ack_int - never happens, no interrupt */
  938. { mk_mii_end, }
  939. },
  940. (const phy_cmd_t []) { /* shutdown */
  941. { mk_mii_end, }
  942. },
  943. };
  944. /* ------------------------------------------------------------------------- */
  945. static phy_info_t const * const phy_info[] = {
  946. &phy_info_lxt970,
  947. &phy_info_lxt971,
  948. &phy_info_qs6612,
  949. &phy_info_am79c874,
  950. &phy_info_ks8721bl,
  951. &phy_info_dp83848,
  952. NULL
  953. };
  954. /* ------------------------------------------------------------------------- */
  955. #ifdef HAVE_mii_link_interrupt
  956. static irqreturn_t
  957. mii_link_interrupt(int irq, void * dev_id);
  958. /*
  959. * This is specific to the MII interrupt setup of the M5272EVB.
  960. */
  961. static void __inline__ fec_request_mii_intr(struct net_device *dev)
  962. {
  963. if (request_irq(66, mii_link_interrupt, IRQF_DISABLED, "fec(MII)", dev) != 0)
  964. printk("FEC: Could not allocate fec(MII) IRQ(66)!\n");
  965. }
  966. static void __inline__ fec_disable_phy_intr(void)
  967. {
  968. volatile unsigned long *icrp;
  969. icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
  970. *icrp = 0x08000000;
  971. }
  972. static void __inline__ fec_phy_ack_intr(void)
  973. {
  974. volatile unsigned long *icrp;
  975. /* Acknowledge the interrupt */
  976. icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
  977. *icrp = 0x0d000000;
  978. }
  979. #ifdef CONFIG_M5272
  980. static void __inline__ fec_get_mac(struct net_device *dev)
  981. {
  982. struct fec_enet_private *fep = netdev_priv(dev);
  983. unsigned char *iap, tmpaddr[ETH_ALEN];
  984. if (FEC_FLASHMAC) {
  985. /*
  986. * Get MAC address from FLASH.
  987. * If it is all 1's or 0's, use the default.
  988. */
  989. iap = (unsigned char *)FEC_FLASHMAC;
  990. if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
  991. (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
  992. iap = fec_mac_default;
  993. if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
  994. (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
  995. iap = fec_mac_default;
  996. } else {
  997. *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
  998. *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
  999. iap = &tmpaddr[0];
  1000. }
  1001. memcpy(dev->dev_addr, iap, ETH_ALEN);
  1002. /* Adjust MAC if using default MAC address */
  1003. if (iap == fec_mac_default)
  1004. dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
  1005. }
  1006. #endif
  1007. /* ------------------------------------------------------------------------- */
  1008. static void mii_display_status(struct net_device *dev)
  1009. {
  1010. struct fec_enet_private *fep = netdev_priv(dev);
  1011. volatile uint *s = &(fep->phy_status);
  1012. if (!fep->link && !fep->old_link) {
  1013. /* Link is still down - don't print anything */
  1014. return;
  1015. }
  1016. printk("%s: status: ", dev->name);
  1017. if (!fep->link) {
  1018. printk("link down");
  1019. } else {
  1020. printk("link up");
  1021. switch(*s & PHY_STAT_SPMASK) {
  1022. case PHY_STAT_100FDX: printk(", 100MBit Full Duplex"); break;
  1023. case PHY_STAT_100HDX: printk(", 100MBit Half Duplex"); break;
  1024. case PHY_STAT_10FDX: printk(", 10MBit Full Duplex"); break;
  1025. case PHY_STAT_10HDX: printk(", 10MBit Half Duplex"); break;
  1026. default:
  1027. printk(", Unknown speed/duplex");
  1028. }
  1029. if (*s & PHY_STAT_ANC)
  1030. printk(", auto-negotiation complete");
  1031. }
  1032. if (*s & PHY_STAT_FAULT)
  1033. printk(", remote fault");
  1034. printk(".\n");
  1035. }
  1036. static void mii_display_config(struct work_struct *work)
  1037. {
  1038. struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
  1039. struct net_device *dev = fep->netdev;
  1040. uint status = fep->phy_status;
  1041. /*
  1042. ** When we get here, phy_task is already removed from
  1043. ** the workqueue. It is thus safe to allow to reuse it.
  1044. */
  1045. fep->mii_phy_task_queued = 0;
  1046. printk("%s: config: auto-negotiation ", dev->name);
  1047. if (status & PHY_CONF_ANE)
  1048. printk("on");
  1049. else
  1050. printk("off");
  1051. if (status & PHY_CONF_100FDX)
  1052. printk(", 100FDX");
  1053. if (status & PHY_CONF_100HDX)
  1054. printk(", 100HDX");
  1055. if (status & PHY_CONF_10FDX)
  1056. printk(", 10FDX");
  1057. if (status & PHY_CONF_10HDX)
  1058. printk(", 10HDX");
  1059. if (!(status & PHY_CONF_SPMASK))
  1060. printk(", No speed/duplex selected?");
  1061. if (status & PHY_CONF_LOOP)
  1062. printk(", loopback enabled");
  1063. printk(".\n");
  1064. fep->sequence_done = 1;
  1065. }
  1066. static void mii_relink(struct work_struct *work)
  1067. {
  1068. struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
  1069. struct net_device *dev = fep->netdev;
  1070. int duplex;
  1071. /*
  1072. ** When we get here, phy_task is already removed from
  1073. ** the workqueue. It is thus safe to allow to reuse it.
  1074. */
  1075. fep->mii_phy_task_queued = 0;
  1076. fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
  1077. mii_display_status(dev);
  1078. fep->old_link = fep->link;
  1079. if (fep->link) {
  1080. duplex = 0;
  1081. if (fep->phy_status
  1082. & (PHY_STAT_100FDX | PHY_STAT_10FDX))
  1083. duplex = 1;
  1084. fec_restart(dev, duplex);
  1085. } else
  1086. fec_stop(dev);
  1087. }
  1088. /* mii_queue_relink is called in interrupt context from mii_link_interrupt */
  1089. static void mii_queue_relink(uint mii_reg, struct net_device *dev)
  1090. {
  1091. struct fec_enet_private *fep = netdev_priv(dev);
  1092. /*
  1093. * We cannot queue phy_task twice in the workqueue. It
  1094. * would cause an endless loop in the workqueue.
  1095. * Fortunately, if the last mii_relink entry has not yet been
  1096. * executed now, it will do the job for the current interrupt,
  1097. * which is just what we want.
  1098. */
  1099. if (fep->mii_phy_task_queued)
  1100. return;
  1101. fep->mii_phy_task_queued = 1;
  1102. INIT_WORK(&fep->phy_task, mii_relink);
  1103. schedule_work(&fep->phy_task);
  1104. }
  1105. /* mii_queue_config is called in interrupt context from fec_enet_mii */
  1106. static void mii_queue_config(uint mii_reg, struct net_device *dev)
  1107. {
  1108. struct fec_enet_private *fep = netdev_priv(dev);
  1109. if (fep->mii_phy_task_queued)
  1110. return;
  1111. fep->mii_phy_task_queued = 1;
  1112. INIT_WORK(&fep->phy_task, mii_display_config);
  1113. schedule_work(&fep->phy_task);
  1114. }
  1115. phy_cmd_t const phy_cmd_relink[] = {
  1116. { mk_mii_read(MII_REG_CR), mii_queue_relink },
  1117. { mk_mii_end, }
  1118. };
  1119. phy_cmd_t const phy_cmd_config[] = {
  1120. { mk_mii_read(MII_REG_CR), mii_queue_config },
  1121. { mk_mii_end, }
  1122. };
  1123. /* Read remainder of PHY ID. */
  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 = netdev_priv(dev);
  1130. fep->phy_id |= (mii_reg & 0xffff);
  1131. printk("fec: PHY @ 0x%x, ID 0x%08x", fep->phy_addr, fep->phy_id);
  1132. for(i = 0; phy_info[i]; i++) {
  1133. if(phy_info[i]->id == (fep->phy_id >> 4))
  1134. break;
  1135. }
  1136. if (phy_info[i])
  1137. printk(" -- %s\n", phy_info[i]->name);
  1138. else
  1139. printk(" -- unknown PHY!\n");
  1140. fep->phy = phy_info[i];
  1141. fep->phy_id_done = 1;
  1142. }
  1143. /* Scan all of the MII PHY addresses looking for someone to respond
  1144. * with a valid ID. This usually happens quickly.
  1145. */
  1146. static void
  1147. mii_discover_phy(uint mii_reg, struct net_device *dev)
  1148. {
  1149. struct fec_enet_private *fep;
  1150. uint phytype;
  1151. fep = netdev_priv(dev);
  1152. if (fep->phy_addr < 32) {
  1153. if ((phytype = (mii_reg & 0xffff)) != 0xffff && phytype != 0) {
  1154. /* Got first part of ID, now get remainder */
  1155. fep->phy_id = phytype << 16;
  1156. mii_queue(dev, mk_mii_read(MII_REG_PHYIR2),
  1157. mii_discover_phy3);
  1158. } else {
  1159. fep->phy_addr++;
  1160. mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
  1161. mii_discover_phy);
  1162. }
  1163. } else {
  1164. printk("FEC: No PHY device found.\n");
  1165. /* Disable external MII interface */
  1166. writel(0, fep->hwp + FEC_MII_SPEED);
  1167. fep->phy_speed = 0;
  1168. #ifdef HAVE_mii_link_interrupt
  1169. fec_disable_phy_intr();
  1170. #endif
  1171. }
  1172. }
  1173. /* This interrupt occurs when the PHY detects a link change */
  1174. #ifdef HAVE_mii_link_interrupt
  1175. static irqreturn_t
  1176. mii_link_interrupt(int irq, void * dev_id)
  1177. {
  1178. struct net_device *dev = dev_id;
  1179. struct fec_enet_private *fep = netdev_priv(dev);
  1180. fec_phy_ack_intr();
  1181. mii_do_cmd(dev, fep->phy->ack_int);
  1182. mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
  1183. return IRQ_HANDLED;
  1184. }
  1185. #endif
  1186. static int
  1187. fec_enet_open(struct net_device *dev)
  1188. {
  1189. struct fec_enet_private *fep = netdev_priv(dev);
  1190. /* I should reset the ring buffers here, but I don't yet know
  1191. * a simple way to do that.
  1192. */
  1193. fep->sequence_done = 0;
  1194. fep->link = 0;
  1195. if (fep->phy) {
  1196. mii_do_cmd(dev, fep->phy->ack_int);
  1197. mii_do_cmd(dev, fep->phy->config);
  1198. mii_do_cmd(dev, phy_cmd_config); /* display configuration */
  1199. /* Poll until the PHY tells us its configuration
  1200. * (not link state).
  1201. * Request is initiated by mii_do_cmd above, but answer
  1202. * comes by interrupt.
  1203. * This should take about 25 usec per register at 2.5 MHz,
  1204. * and we read approximately 5 registers.
  1205. */
  1206. while(!fep->sequence_done)
  1207. schedule();
  1208. mii_do_cmd(dev, fep->phy->startup);
  1209. /* Set the initial link state to true. A lot of hardware
  1210. * based on this device does not implement a PHY interrupt,
  1211. * so we are never notified of link change.
  1212. */
  1213. fep->link = 1;
  1214. } else {
  1215. fep->link = 1; /* lets just try it and see */
  1216. /* no phy, go full duplex, it's most likely a hub chip */
  1217. fec_restart(dev, 1);
  1218. }
  1219. netif_start_queue(dev);
  1220. fep->opened = 1;
  1221. return 0;
  1222. }
  1223. static int
  1224. fec_enet_close(struct net_device *dev)
  1225. {
  1226. struct fec_enet_private *fep = netdev_priv(dev);
  1227. /* Don't know what to do yet. */
  1228. fep->opened = 0;
  1229. netif_stop_queue(dev);
  1230. fec_stop(dev);
  1231. return 0;
  1232. }
  1233. /* Set or clear the multicast filter for this adaptor.
  1234. * Skeleton taken from sunlance driver.
  1235. * The CPM Ethernet implementation allows Multicast as well as individual
  1236. * MAC address filtering. Some of the drivers check to make sure it is
  1237. * a group multicast address, and discard those that are not. I guess I
  1238. * will do the same for now, but just remove the test if you want
  1239. * individual filtering as well (do the upper net layers want or support
  1240. * this kind of feature?).
  1241. */
  1242. #define HASH_BITS 6 /* #bits in hash */
  1243. #define CRC32_POLY 0xEDB88320
  1244. static void set_multicast_list(struct net_device *dev)
  1245. {
  1246. struct fec_enet_private *fep = netdev_priv(dev);
  1247. struct dev_mc_list *dmi;
  1248. unsigned int i, j, bit, data, crc, tmp;
  1249. unsigned char hash;
  1250. if (dev->flags & IFF_PROMISC) {
  1251. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1252. tmp |= 0x8;
  1253. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1254. return;
  1255. }
  1256. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1257. tmp &= ~0x8;
  1258. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1259. if (dev->flags & IFF_ALLMULTI) {
  1260. /* Catch all multicast addresses, so set the
  1261. * filter to all 1's
  1262. */
  1263. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1264. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1265. return;
  1266. }
  1267. /* Clear filter and add the addresses in hash register
  1268. */
  1269. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1270. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1271. dmi = dev->mc_list;
  1272. for (j = 0; j < dev->mc_count; j++, dmi = dmi->next) {
  1273. /* Only support group multicast for now */
  1274. if (!(dmi->dmi_addr[0] & 1))
  1275. continue;
  1276. /* calculate crc32 value of mac address */
  1277. crc = 0xffffffff;
  1278. for (i = 0; i < dmi->dmi_addrlen; i++) {
  1279. data = dmi->dmi_addr[i];
  1280. for (bit = 0; bit < 8; bit++, data >>= 1) {
  1281. crc = (crc >> 1) ^
  1282. (((crc ^ data) & 1) ? CRC32_POLY : 0);
  1283. }
  1284. }
  1285. /* only upper 6 bits (HASH_BITS) are used
  1286. * which point to specific bit in he hash registers
  1287. */
  1288. hash = (crc >> (32 - HASH_BITS)) & 0x3f;
  1289. if (hash > 31) {
  1290. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1291. tmp |= 1 << (hash - 32);
  1292. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1293. } else {
  1294. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1295. tmp |= 1 << hash;
  1296. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1297. }
  1298. }
  1299. }
  1300. /* Set a MAC change in hardware. */
  1301. static int
  1302. fec_set_mac_address(struct net_device *dev, void *p)
  1303. {
  1304. struct fec_enet_private *fep = netdev_priv(dev);
  1305. struct sockaddr *addr = p;
  1306. if (!is_valid_ether_addr(addr->sa_data))
  1307. return -EADDRNOTAVAIL;
  1308. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1309. writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
  1310. (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
  1311. fep->hwp + FEC_ADDR_LOW);
  1312. writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
  1313. fep + FEC_ADDR_HIGH);
  1314. return 0;
  1315. }
  1316. static const struct net_device_ops fec_netdev_ops = {
  1317. .ndo_open = fec_enet_open,
  1318. .ndo_stop = fec_enet_close,
  1319. .ndo_start_xmit = fec_enet_start_xmit,
  1320. .ndo_set_multicast_list = set_multicast_list,
  1321. .ndo_validate_addr = eth_validate_addr,
  1322. .ndo_tx_timeout = fec_timeout,
  1323. .ndo_set_mac_address = fec_set_mac_address,
  1324. };
  1325. /*
  1326. * XXX: We need to clean up on failure exits here.
  1327. *
  1328. * index is only used in legacy code
  1329. */
  1330. int __init fec_enet_init(struct net_device *dev, int index)
  1331. {
  1332. struct fec_enet_private *fep = netdev_priv(dev);
  1333. unsigned long mem_addr;
  1334. struct bufdesc *bdp, *cbd_base;
  1335. int i, j;
  1336. /* Allocate memory for buffer descriptors. */
  1337. cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
  1338. GFP_KERNEL);
  1339. if (!cbd_base) {
  1340. printk("FEC: allocate descriptor memory failed?\n");
  1341. return -ENOMEM;
  1342. }
  1343. spin_lock_init(&fep->hw_lock);
  1344. spin_lock_init(&fep->mii_lock);
  1345. fep->index = index;
  1346. fep->hwp = (void __iomem *)dev->base_addr;
  1347. fep->netdev = dev;
  1348. /* Set the Ethernet address */
  1349. #ifdef CONFIG_M5272
  1350. fec_get_mac(dev);
  1351. #else
  1352. {
  1353. unsigned long l;
  1354. l = readl(fep->hwp + FEC_ADDR_LOW);
  1355. dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
  1356. dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
  1357. dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
  1358. dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
  1359. l = readl(fep->hwp + FEC_ADDR_HIGH);
  1360. dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
  1361. dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
  1362. }
  1363. #endif
  1364. /* Set receive and transmit descriptor base. */
  1365. fep->rx_bd_base = cbd_base;
  1366. fep->tx_bd_base = cbd_base + RX_RING_SIZE;
  1367. /* Initialize the receive buffer descriptors. */
  1368. bdp = fep->rx_bd_base;
  1369. for (i=0; i<FEC_ENET_RX_PAGES; i++) {
  1370. /* Allocate a page */
  1371. mem_addr = __get_free_page(GFP_KERNEL);
  1372. /* XXX: missing check for allocation failure */
  1373. /* Initialize the BD for every fragment in the page */
  1374. for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
  1375. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1376. bdp->cbd_bufaddr = __pa(mem_addr);
  1377. mem_addr += FEC_ENET_RX_FRSIZE;
  1378. bdp++;
  1379. }
  1380. }
  1381. /* Set the last buffer to wrap */
  1382. bdp--;
  1383. bdp->cbd_sc |= BD_SC_WRAP;
  1384. /* ...and the same for transmit */
  1385. bdp = fep->tx_bd_base;
  1386. for (i=0, j=FEC_ENET_TX_FRPPG; i<TX_RING_SIZE; i++) {
  1387. if (j >= FEC_ENET_TX_FRPPG) {
  1388. mem_addr = __get_free_page(GFP_KERNEL);
  1389. j = 1;
  1390. } else {
  1391. mem_addr += FEC_ENET_TX_FRSIZE;
  1392. j++;
  1393. }
  1394. fep->tx_bounce[i] = (unsigned char *) mem_addr;
  1395. /* Initialize the BD for every fragment in the page */
  1396. bdp->cbd_sc = 0;
  1397. bdp->cbd_bufaddr = 0;
  1398. bdp++;
  1399. }
  1400. /* Set the last buffer to wrap */
  1401. bdp--;
  1402. bdp->cbd_sc |= BD_SC_WRAP;
  1403. #ifdef HAVE_mii_link_interrupt
  1404. fec_request_mii_intr(dev);
  1405. #endif
  1406. /* The FEC Ethernet specific entries in the device structure */
  1407. dev->watchdog_timeo = TX_TIMEOUT;
  1408. dev->netdev_ops = &fec_netdev_ops;
  1409. for (i=0; i<NMII-1; i++)
  1410. mii_cmds[i].mii_next = &mii_cmds[i+1];
  1411. mii_free = mii_cmds;
  1412. /* Set MII speed to 2.5 MHz */
  1413. fep->phy_speed = ((((clk_get_rate(fep->clk) / 2 + 4999999)
  1414. / 2500000) / 2) & 0x3F) << 1;
  1415. fec_restart(dev, 0);
  1416. /* Queue up command to detect the PHY and initialize the
  1417. * remainder of the interface.
  1418. */
  1419. fep->phy_id_done = 0;
  1420. fep->phy_addr = 0;
  1421. mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
  1422. return 0;
  1423. }
  1424. /* This function is called to start or restart the FEC during a link
  1425. * change. This only happens when switching between half and full
  1426. * duplex.
  1427. */
  1428. static void
  1429. fec_restart(struct net_device *dev, int duplex)
  1430. {
  1431. struct fec_enet_private *fep = netdev_priv(dev);
  1432. struct bufdesc *bdp;
  1433. int i;
  1434. /* Whack a reset. We should wait for this. */
  1435. writel(1, fep->hwp + FEC_ECNTRL);
  1436. udelay(10);
  1437. /* Clear any outstanding interrupt. */
  1438. writel(0xffc00000, fep->hwp + FEC_IEVENT);
  1439. /* Reset all multicast. */
  1440. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1441. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1442. #ifndef CONFIG_M5272
  1443. writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
  1444. writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
  1445. #endif
  1446. /* Set maximum receive buffer size. */
  1447. writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
  1448. /* Set receive and transmit descriptor base. */
  1449. writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
  1450. writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
  1451. fep->hwp + FEC_X_DES_START);
  1452. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  1453. fep->cur_rx = fep->rx_bd_base;
  1454. /* Reset SKB transmit buffers. */
  1455. fep->skb_cur = fep->skb_dirty = 0;
  1456. for (i = 0; i <= TX_RING_MOD_MASK; i++) {
  1457. if (fep->tx_skbuff[i]) {
  1458. dev_kfree_skb_any(fep->tx_skbuff[i]);
  1459. fep->tx_skbuff[i] = NULL;
  1460. }
  1461. }
  1462. /* Initialize the receive buffer descriptors. */
  1463. bdp = fep->rx_bd_base;
  1464. for (i = 0; i < RX_RING_SIZE; i++) {
  1465. /* Initialize the BD for every fragment in the page. */
  1466. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1467. bdp++;
  1468. }
  1469. /* Set the last buffer to wrap */
  1470. bdp--;
  1471. bdp->cbd_sc |= BD_SC_WRAP;
  1472. /* ...and the same for transmit */
  1473. bdp = fep->tx_bd_base;
  1474. for (i = 0; i < TX_RING_SIZE; i++) {
  1475. /* Initialize the BD for every fragment in the page. */
  1476. bdp->cbd_sc = 0;
  1477. bdp->cbd_bufaddr = 0;
  1478. bdp++;
  1479. }
  1480. /* Set the last buffer to wrap */
  1481. bdp--;
  1482. bdp->cbd_sc |= BD_SC_WRAP;
  1483. /* Enable MII mode */
  1484. if (duplex) {
  1485. /* MII enable / FD enable */
  1486. writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
  1487. writel(0x04, fep->hwp + FEC_X_CNTRL);
  1488. } else {
  1489. /* MII enable / No Rcv on Xmit */
  1490. writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
  1491. writel(0x0, fep->hwp + FEC_X_CNTRL);
  1492. }
  1493. fep->full_duplex = duplex;
  1494. /* Set MII speed */
  1495. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1496. /* And last, enable the transmit and receive processing */
  1497. writel(2, fep->hwp + FEC_ECNTRL);
  1498. writel(0, fep->hwp + FEC_R_DES_ACTIVE);
  1499. /* Enable interrupts we wish to service */
  1500. writel(FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII,
  1501. fep->hwp + FEC_IMASK);
  1502. }
  1503. static void
  1504. fec_stop(struct net_device *dev)
  1505. {
  1506. struct fec_enet_private *fep = netdev_priv(dev);
  1507. /* We cannot expect a graceful transmit stop without link !!! */
  1508. if (fep->link) {
  1509. writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
  1510. udelay(10);
  1511. if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
  1512. printk("fec_stop : Graceful transmit stop did not complete !\n");
  1513. }
  1514. /* Whack a reset. We should wait for this. */
  1515. writel(1, fep->hwp + FEC_ECNTRL);
  1516. udelay(10);
  1517. /* Clear outstanding MII command interrupts. */
  1518. writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
  1519. writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
  1520. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1521. }
  1522. static int __devinit
  1523. fec_probe(struct platform_device *pdev)
  1524. {
  1525. struct fec_enet_private *fep;
  1526. struct net_device *ndev;
  1527. int i, irq, ret = 0;
  1528. struct resource *r;
  1529. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1530. if (!r)
  1531. return -ENXIO;
  1532. r = request_mem_region(r->start, resource_size(r), pdev->name);
  1533. if (!r)
  1534. return -EBUSY;
  1535. /* Init network device */
  1536. ndev = alloc_etherdev(sizeof(struct fec_enet_private));
  1537. if (!ndev)
  1538. return -ENOMEM;
  1539. SET_NETDEV_DEV(ndev, &pdev->dev);
  1540. /* setup board info structure */
  1541. fep = netdev_priv(ndev);
  1542. memset(fep, 0, sizeof(*fep));
  1543. ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
  1544. if (!ndev->base_addr) {
  1545. ret = -ENOMEM;
  1546. goto failed_ioremap;
  1547. }
  1548. platform_set_drvdata(pdev, ndev);
  1549. /* This device has up to three irqs on some platforms */
  1550. for (i = 0; i < 3; i++) {
  1551. irq = platform_get_irq(pdev, i);
  1552. if (i && irq < 0)
  1553. break;
  1554. ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
  1555. if (ret) {
  1556. while (i >= 0) {
  1557. irq = platform_get_irq(pdev, i);
  1558. free_irq(irq, ndev);
  1559. i--;
  1560. }
  1561. goto failed_irq;
  1562. }
  1563. }
  1564. fep->clk = clk_get(&pdev->dev, "fec_clk");
  1565. if (IS_ERR(fep->clk)) {
  1566. ret = PTR_ERR(fep->clk);
  1567. goto failed_clk;
  1568. }
  1569. clk_enable(fep->clk);
  1570. ret = fec_enet_init(ndev, 0);
  1571. if (ret)
  1572. goto failed_init;
  1573. ret = register_netdev(ndev);
  1574. if (ret)
  1575. goto failed_register;
  1576. return 0;
  1577. failed_register:
  1578. failed_init:
  1579. clk_disable(fep->clk);
  1580. clk_put(fep->clk);
  1581. failed_clk:
  1582. for (i = 0; i < 3; i++) {
  1583. irq = platform_get_irq(pdev, i);
  1584. if (irq > 0)
  1585. free_irq(irq, ndev);
  1586. }
  1587. failed_irq:
  1588. iounmap((void __iomem *)ndev->base_addr);
  1589. failed_ioremap:
  1590. free_netdev(ndev);
  1591. return ret;
  1592. }
  1593. static int __devexit
  1594. fec_drv_remove(struct platform_device *pdev)
  1595. {
  1596. struct net_device *ndev = platform_get_drvdata(pdev);
  1597. struct fec_enet_private *fep = netdev_priv(ndev);
  1598. platform_set_drvdata(pdev, NULL);
  1599. fec_stop(ndev);
  1600. clk_disable(fep->clk);
  1601. clk_put(fep->clk);
  1602. iounmap((void __iomem *)ndev->base_addr);
  1603. unregister_netdev(ndev);
  1604. free_netdev(ndev);
  1605. return 0;
  1606. }
  1607. static int
  1608. fec_suspend(struct platform_device *dev, pm_message_t state)
  1609. {
  1610. struct net_device *ndev = platform_get_drvdata(dev);
  1611. struct fec_enet_private *fep;
  1612. if (ndev) {
  1613. fep = netdev_priv(ndev);
  1614. if (netif_running(ndev)) {
  1615. netif_device_detach(ndev);
  1616. fec_stop(ndev);
  1617. }
  1618. }
  1619. return 0;
  1620. }
  1621. static int
  1622. fec_resume(struct platform_device *dev)
  1623. {
  1624. struct net_device *ndev = platform_get_drvdata(dev);
  1625. if (ndev) {
  1626. if (netif_running(ndev)) {
  1627. fec_enet_init(ndev, 0);
  1628. netif_device_attach(ndev);
  1629. }
  1630. }
  1631. return 0;
  1632. }
  1633. static struct platform_driver fec_driver = {
  1634. .driver = {
  1635. .name = "fec",
  1636. .owner = THIS_MODULE,
  1637. },
  1638. .probe = fec_probe,
  1639. .remove = __devexit_p(fec_drv_remove),
  1640. .suspend = fec_suspend,
  1641. .resume = fec_resume,
  1642. };
  1643. static int __init
  1644. fec_enet_module_init(void)
  1645. {
  1646. printk(KERN_INFO "FEC Ethernet Driver\n");
  1647. return platform_driver_register(&fec_driver);
  1648. }
  1649. static void __exit
  1650. fec_enet_cleanup(void)
  1651. {
  1652. platform_driver_unregister(&fec_driver);
  1653. }
  1654. module_exit(fec_enet_cleanup);
  1655. module_init(fec_enet_module_init);
  1656. MODULE_LICENSE("GPL");