fec.c 55 KB

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