fec.c 53 KB

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