fec.c 54 KB

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