fec.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  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_irq(&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_irq(&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_irq(&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_irq(&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_irq(&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_irq(&fep->mii_lock);
  531. }
  532. static int
  533. mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
  534. {
  535. struct fec_enet_private *fep;
  536. unsigned long flags;
  537. mii_list_t *mip;
  538. int retval;
  539. /* Add PHY address to register command */
  540. fep = netdev_priv(dev);
  541. spin_lock_irqsave(&fep->mii_lock, flags);
  542. regval |= fep->phy_addr << 23;
  543. retval = 0;
  544. if ((mip = mii_free) != NULL) {
  545. mii_free = mip->mii_next;
  546. mip->mii_regval = regval;
  547. mip->mii_func = func;
  548. mip->mii_next = NULL;
  549. if (mii_head) {
  550. mii_tail->mii_next = mip;
  551. mii_tail = mip;
  552. } else {
  553. mii_head = mii_tail = mip;
  554. writel(regval, fep->hwp + FEC_MII_DATA);
  555. }
  556. } else {
  557. retval = 1;
  558. }
  559. spin_unlock_irqrestore(&fep->mii_lock, flags);
  560. return retval;
  561. }
  562. static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
  563. {
  564. if(!c)
  565. return;
  566. for (; c->mii_data != mk_mii_end; c++)
  567. mii_queue(dev, c->mii_data, c->funct);
  568. }
  569. static void mii_parse_sr(uint mii_reg, struct net_device *dev)
  570. {
  571. struct fec_enet_private *fep = netdev_priv(dev);
  572. volatile uint *s = &(fep->phy_status);
  573. uint status;
  574. status = *s & ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
  575. if (mii_reg & 0x0004)
  576. status |= PHY_STAT_LINK;
  577. if (mii_reg & 0x0010)
  578. status |= PHY_STAT_FAULT;
  579. if (mii_reg & 0x0020)
  580. status |= PHY_STAT_ANC;
  581. *s = status;
  582. }
  583. static void mii_parse_cr(uint mii_reg, struct net_device *dev)
  584. {
  585. struct fec_enet_private *fep = netdev_priv(dev);
  586. volatile uint *s = &(fep->phy_status);
  587. uint status;
  588. status = *s & ~(PHY_CONF_ANE | PHY_CONF_LOOP);
  589. if (mii_reg & 0x1000)
  590. status |= PHY_CONF_ANE;
  591. if (mii_reg & 0x4000)
  592. status |= PHY_CONF_LOOP;
  593. *s = status;
  594. }
  595. static void mii_parse_anar(uint mii_reg, struct net_device *dev)
  596. {
  597. struct fec_enet_private *fep = netdev_priv(dev);
  598. volatile uint *s = &(fep->phy_status);
  599. uint status;
  600. status = *s & ~(PHY_CONF_SPMASK);
  601. if (mii_reg & 0x0020)
  602. status |= PHY_CONF_10HDX;
  603. if (mii_reg & 0x0040)
  604. status |= PHY_CONF_10FDX;
  605. if (mii_reg & 0x0080)
  606. status |= PHY_CONF_100HDX;
  607. if (mii_reg & 0x00100)
  608. status |= PHY_CONF_100FDX;
  609. *s = status;
  610. }
  611. /* ------------------------------------------------------------------------- */
  612. /* The Level one LXT970 is used by many boards */
  613. #define MII_LXT970_MIRROR 16 /* Mirror register */
  614. #define MII_LXT970_IER 17 /* Interrupt Enable Register */
  615. #define MII_LXT970_ISR 18 /* Interrupt Status Register */
  616. #define MII_LXT970_CONFIG 19 /* Configuration Register */
  617. #define MII_LXT970_CSR 20 /* Chip Status Register */
  618. static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
  619. {
  620. struct fec_enet_private *fep = netdev_priv(dev);
  621. volatile uint *s = &(fep->phy_status);
  622. uint status;
  623. status = *s & ~(PHY_STAT_SPMASK);
  624. if (mii_reg & 0x0800) {
  625. if (mii_reg & 0x1000)
  626. status |= PHY_STAT_100FDX;
  627. else
  628. status |= PHY_STAT_100HDX;
  629. } else {
  630. if (mii_reg & 0x1000)
  631. status |= PHY_STAT_10FDX;
  632. else
  633. status |= PHY_STAT_10HDX;
  634. }
  635. *s = status;
  636. }
  637. static phy_cmd_t const phy_cmd_lxt970_config[] = {
  638. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  639. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  640. { mk_mii_end, }
  641. };
  642. static phy_cmd_t const phy_cmd_lxt970_startup[] = { /* enable interrupts */
  643. { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
  644. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  645. { mk_mii_end, }
  646. };
  647. static phy_cmd_t const phy_cmd_lxt970_ack_int[] = {
  648. /* read SR and ISR to acknowledge */
  649. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  650. { mk_mii_read(MII_LXT970_ISR), NULL },
  651. /* find out the current status */
  652. { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
  653. { mk_mii_end, }
  654. };
  655. static phy_cmd_t const phy_cmd_lxt970_shutdown[] = { /* disable interrupts */
  656. { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
  657. { mk_mii_end, }
  658. };
  659. static phy_info_t const phy_info_lxt970 = {
  660. .id = 0x07810000,
  661. .name = "LXT970",
  662. .config = phy_cmd_lxt970_config,
  663. .startup = phy_cmd_lxt970_startup,
  664. .ack_int = phy_cmd_lxt970_ack_int,
  665. .shutdown = phy_cmd_lxt970_shutdown
  666. };
  667. /* ------------------------------------------------------------------------- */
  668. /* The Level one LXT971 is used on some of my custom boards */
  669. /* register definitions for the 971 */
  670. #define MII_LXT971_PCR 16 /* Port Control Register */
  671. #define MII_LXT971_SR2 17 /* Status Register 2 */
  672. #define MII_LXT971_IER 18 /* Interrupt Enable Register */
  673. #define MII_LXT971_ISR 19 /* Interrupt Status Register */
  674. #define MII_LXT971_LCR 20 /* LED Control Register */
  675. #define MII_LXT971_TCR 30 /* Transmit Control Register */
  676. /*
  677. * I had some nice ideas of running the MDIO faster...
  678. * The 971 should support 8MHz and I tried it, but things acted really
  679. * weird, so 2.5 MHz ought to be enough for anyone...
  680. */
  681. static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
  682. {
  683. struct fec_enet_private *fep = netdev_priv(dev);
  684. volatile uint *s = &(fep->phy_status);
  685. uint status;
  686. status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
  687. if (mii_reg & 0x0400) {
  688. fep->link = 1;
  689. status |= PHY_STAT_LINK;
  690. } else {
  691. fep->link = 0;
  692. }
  693. if (mii_reg & 0x0080)
  694. status |= PHY_STAT_ANC;
  695. if (mii_reg & 0x4000) {
  696. if (mii_reg & 0x0200)
  697. status |= PHY_STAT_100FDX;
  698. else
  699. status |= PHY_STAT_100HDX;
  700. } else {
  701. if (mii_reg & 0x0200)
  702. status |= PHY_STAT_10FDX;
  703. else
  704. status |= PHY_STAT_10HDX;
  705. }
  706. if (mii_reg & 0x0008)
  707. status |= PHY_STAT_FAULT;
  708. *s = status;
  709. }
  710. static phy_cmd_t const phy_cmd_lxt971_config[] = {
  711. /* limit to 10MBit because my prototype board
  712. * doesn't work with 100. */
  713. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  714. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  715. { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
  716. { mk_mii_end, }
  717. };
  718. static phy_cmd_t const phy_cmd_lxt971_startup[] = { /* enable interrupts */
  719. { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
  720. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  721. { mk_mii_write(MII_LXT971_LCR, 0xd422), NULL }, /* LED config */
  722. /* Somehow does the 971 tell me that the link is down
  723. * the first read after power-up.
  724. * read here to get a valid value in ack_int */
  725. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  726. { mk_mii_end, }
  727. };
  728. static phy_cmd_t const phy_cmd_lxt971_ack_int[] = {
  729. /* acknowledge the int before reading status ! */
  730. { mk_mii_read(MII_LXT971_ISR), NULL },
  731. /* find out the current status */
  732. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  733. { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
  734. { mk_mii_end, }
  735. };
  736. static phy_cmd_t const phy_cmd_lxt971_shutdown[] = { /* disable interrupts */
  737. { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
  738. { mk_mii_end, }
  739. };
  740. static phy_info_t const phy_info_lxt971 = {
  741. .id = 0x0001378e,
  742. .name = "LXT971",
  743. .config = phy_cmd_lxt971_config,
  744. .startup = phy_cmd_lxt971_startup,
  745. .ack_int = phy_cmd_lxt971_ack_int,
  746. .shutdown = phy_cmd_lxt971_shutdown
  747. };
  748. /* ------------------------------------------------------------------------- */
  749. /* The Quality Semiconductor QS6612 is used on the RPX CLLF */
  750. /* register definitions */
  751. #define MII_QS6612_MCR 17 /* Mode Control Register */
  752. #define MII_QS6612_FTR 27 /* Factory Test Register */
  753. #define MII_QS6612_MCO 28 /* Misc. Control Register */
  754. #define MII_QS6612_ISR 29 /* Interrupt Source Register */
  755. #define MII_QS6612_IMR 30 /* Interrupt Mask Register */
  756. #define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
  757. static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
  758. {
  759. struct fec_enet_private *fep = netdev_priv(dev);
  760. volatile uint *s = &(fep->phy_status);
  761. uint status;
  762. status = *s & ~(PHY_STAT_SPMASK);
  763. switch((mii_reg >> 2) & 7) {
  764. case 1: status |= PHY_STAT_10HDX; break;
  765. case 2: status |= PHY_STAT_100HDX; break;
  766. case 5: status |= PHY_STAT_10FDX; break;
  767. case 6: status |= PHY_STAT_100FDX; break;
  768. }
  769. *s = status;
  770. }
  771. static phy_cmd_t const phy_cmd_qs6612_config[] = {
  772. /* The PHY powers up isolated on the RPX,
  773. * so send a command to allow operation.
  774. */
  775. { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
  776. /* parse cr and anar to get some info */
  777. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  778. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  779. { mk_mii_end, }
  780. };
  781. static phy_cmd_t const phy_cmd_qs6612_startup[] = { /* enable interrupts */
  782. { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
  783. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  784. { mk_mii_end, }
  785. };
  786. static phy_cmd_t const phy_cmd_qs6612_ack_int[] = {
  787. /* we need to read ISR, SR and ANER to acknowledge */
  788. { mk_mii_read(MII_QS6612_ISR), NULL },
  789. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  790. { mk_mii_read(MII_REG_ANER), NULL },
  791. /* read pcr to get info */
  792. { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
  793. { mk_mii_end, }
  794. };
  795. static phy_cmd_t const phy_cmd_qs6612_shutdown[] = { /* disable interrupts */
  796. { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
  797. { mk_mii_end, }
  798. };
  799. static phy_info_t const phy_info_qs6612 = {
  800. .id = 0x00181440,
  801. .name = "QS6612",
  802. .config = phy_cmd_qs6612_config,
  803. .startup = phy_cmd_qs6612_startup,
  804. .ack_int = phy_cmd_qs6612_ack_int,
  805. .shutdown = phy_cmd_qs6612_shutdown
  806. };
  807. /* ------------------------------------------------------------------------- */
  808. /* AMD AM79C874 phy */
  809. /* register definitions for the 874 */
  810. #define MII_AM79C874_MFR 16 /* Miscellaneous Feature Register */
  811. #define MII_AM79C874_ICSR 17 /* Interrupt/Status Register */
  812. #define MII_AM79C874_DR 18 /* Diagnostic Register */
  813. #define MII_AM79C874_PMLR 19 /* Power and Loopback Register */
  814. #define MII_AM79C874_MCR 21 /* ModeControl Register */
  815. #define MII_AM79C874_DC 23 /* Disconnect Counter */
  816. #define MII_AM79C874_REC 24 /* Recieve Error Counter */
  817. static void mii_parse_am79c874_dr(uint mii_reg, struct net_device *dev)
  818. {
  819. struct fec_enet_private *fep = netdev_priv(dev);
  820. volatile uint *s = &(fep->phy_status);
  821. uint status;
  822. status = *s & ~(PHY_STAT_SPMASK | PHY_STAT_ANC);
  823. if (mii_reg & 0x0080)
  824. status |= PHY_STAT_ANC;
  825. if (mii_reg & 0x0400)
  826. status |= ((mii_reg & 0x0800) ? PHY_STAT_100FDX : PHY_STAT_100HDX);
  827. else
  828. status |= ((mii_reg & 0x0800) ? PHY_STAT_10FDX : PHY_STAT_10HDX);
  829. *s = status;
  830. }
  831. static phy_cmd_t const phy_cmd_am79c874_config[] = {
  832. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  833. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  834. { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
  835. { mk_mii_end, }
  836. };
  837. static phy_cmd_t const phy_cmd_am79c874_startup[] = { /* enable interrupts */
  838. { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
  839. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  840. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  841. { mk_mii_end, }
  842. };
  843. static phy_cmd_t const phy_cmd_am79c874_ack_int[] = {
  844. /* find out the current status */
  845. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  846. { mk_mii_read(MII_AM79C874_DR), mii_parse_am79c874_dr },
  847. /* we only need to read ISR to acknowledge */
  848. { mk_mii_read(MII_AM79C874_ICSR), NULL },
  849. { mk_mii_end, }
  850. };
  851. static phy_cmd_t const phy_cmd_am79c874_shutdown[] = { /* disable interrupts */
  852. { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
  853. { mk_mii_end, }
  854. };
  855. static phy_info_t const phy_info_am79c874 = {
  856. .id = 0x00022561,
  857. .name = "AM79C874",
  858. .config = phy_cmd_am79c874_config,
  859. .startup = phy_cmd_am79c874_startup,
  860. .ack_int = phy_cmd_am79c874_ack_int,
  861. .shutdown = phy_cmd_am79c874_shutdown
  862. };
  863. /* ------------------------------------------------------------------------- */
  864. /* Kendin KS8721BL phy */
  865. /* register definitions for the 8721 */
  866. #define MII_KS8721BL_RXERCR 21
  867. #define MII_KS8721BL_ICSR 27
  868. #define MII_KS8721BL_PHYCR 31
  869. static phy_cmd_t const phy_cmd_ks8721bl_config[] = {
  870. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  871. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  872. { mk_mii_end, }
  873. };
  874. static phy_cmd_t const phy_cmd_ks8721bl_startup[] = { /* enable interrupts */
  875. { mk_mii_write(MII_KS8721BL_ICSR, 0xff00), NULL },
  876. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  877. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  878. { mk_mii_end, }
  879. };
  880. static phy_cmd_t const phy_cmd_ks8721bl_ack_int[] = {
  881. /* find out the current status */
  882. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  883. /* we only need to read ISR to acknowledge */
  884. { mk_mii_read(MII_KS8721BL_ICSR), NULL },
  885. { mk_mii_end, }
  886. };
  887. static phy_cmd_t const phy_cmd_ks8721bl_shutdown[] = { /* disable interrupts */
  888. { mk_mii_write(MII_KS8721BL_ICSR, 0x0000), NULL },
  889. { mk_mii_end, }
  890. };
  891. static phy_info_t const phy_info_ks8721bl = {
  892. .id = 0x00022161,
  893. .name = "KS8721BL",
  894. .config = phy_cmd_ks8721bl_config,
  895. .startup = phy_cmd_ks8721bl_startup,
  896. .ack_int = phy_cmd_ks8721bl_ack_int,
  897. .shutdown = phy_cmd_ks8721bl_shutdown
  898. };
  899. /* ------------------------------------------------------------------------- */
  900. /* register definitions for the DP83848 */
  901. #define MII_DP8384X_PHYSTST 16 /* PHY Status Register */
  902. static void mii_parse_dp8384x_sr2(uint mii_reg, struct net_device *dev)
  903. {
  904. struct fec_enet_private *fep = netdev_priv(dev);
  905. volatile uint *s = &(fep->phy_status);
  906. *s &= ~(PHY_STAT_SPMASK | PHY_STAT_LINK | PHY_STAT_ANC);
  907. /* Link up */
  908. if (mii_reg & 0x0001) {
  909. fep->link = 1;
  910. *s |= PHY_STAT_LINK;
  911. } else
  912. fep->link = 0;
  913. /* Status of link */
  914. if (mii_reg & 0x0010) /* Autonegotioation complete */
  915. *s |= PHY_STAT_ANC;
  916. if (mii_reg & 0x0002) { /* 10MBps? */
  917. if (mii_reg & 0x0004) /* Full Duplex? */
  918. *s |= PHY_STAT_10FDX;
  919. else
  920. *s |= PHY_STAT_10HDX;
  921. } else { /* 100 Mbps? */
  922. if (mii_reg & 0x0004) /* Full Duplex? */
  923. *s |= PHY_STAT_100FDX;
  924. else
  925. *s |= PHY_STAT_100HDX;
  926. }
  927. if (mii_reg & 0x0008)
  928. *s |= PHY_STAT_FAULT;
  929. }
  930. static phy_info_t phy_info_dp83848= {
  931. 0x020005c9,
  932. "DP83848",
  933. (const phy_cmd_t []) { /* config */
  934. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  935. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  936. { mk_mii_read(MII_DP8384X_PHYSTST), mii_parse_dp8384x_sr2 },
  937. { mk_mii_end, }
  938. },
  939. (const phy_cmd_t []) { /* startup - enable interrupts */
  940. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  941. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  942. { mk_mii_end, }
  943. },
  944. (const phy_cmd_t []) { /* ack_int - never happens, no interrupt */
  945. { mk_mii_end, }
  946. },
  947. (const phy_cmd_t []) { /* shutdown */
  948. { mk_mii_end, }
  949. },
  950. };
  951. /* ------------------------------------------------------------------------- */
  952. static phy_info_t const * const phy_info[] = {
  953. &phy_info_lxt970,
  954. &phy_info_lxt971,
  955. &phy_info_qs6612,
  956. &phy_info_am79c874,
  957. &phy_info_ks8721bl,
  958. &phy_info_dp83848,
  959. NULL
  960. };
  961. /* ------------------------------------------------------------------------- */
  962. #ifdef HAVE_mii_link_interrupt
  963. static irqreturn_t
  964. mii_link_interrupt(int irq, void * dev_id);
  965. /*
  966. * This is specific to the MII interrupt setup of the M5272EVB.
  967. */
  968. static void __inline__ fec_request_mii_intr(struct net_device *dev)
  969. {
  970. if (request_irq(66, mii_link_interrupt, IRQF_DISABLED, "fec(MII)", dev) != 0)
  971. printk("FEC: Could not allocate fec(MII) IRQ(66)!\n");
  972. }
  973. static void __inline__ fec_disable_phy_intr(void)
  974. {
  975. volatile unsigned long *icrp;
  976. icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
  977. *icrp = 0x08000000;
  978. }
  979. static void __inline__ fec_phy_ack_intr(void)
  980. {
  981. volatile unsigned long *icrp;
  982. /* Acknowledge the interrupt */
  983. icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR1);
  984. *icrp = 0x0d000000;
  985. }
  986. #endif
  987. #ifdef CONFIG_M5272
  988. static void __inline__ fec_get_mac(struct net_device *dev)
  989. {
  990. struct fec_enet_private *fep = netdev_priv(dev);
  991. unsigned char *iap, tmpaddr[ETH_ALEN];
  992. if (FEC_FLASHMAC) {
  993. /*
  994. * Get MAC address from FLASH.
  995. * If it is all 1's or 0's, use the default.
  996. */
  997. iap = (unsigned char *)FEC_FLASHMAC;
  998. if ((iap[0] == 0) && (iap[1] == 0) && (iap[2] == 0) &&
  999. (iap[3] == 0) && (iap[4] == 0) && (iap[5] == 0))
  1000. iap = fec_mac_default;
  1001. if ((iap[0] == 0xff) && (iap[1] == 0xff) && (iap[2] == 0xff) &&
  1002. (iap[3] == 0xff) && (iap[4] == 0xff) && (iap[5] == 0xff))
  1003. iap = fec_mac_default;
  1004. } else {
  1005. *((unsigned long *) &tmpaddr[0]) = readl(fep->hwp + FEC_ADDR_LOW);
  1006. *((unsigned short *) &tmpaddr[4]) = (readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
  1007. iap = &tmpaddr[0];
  1008. }
  1009. memcpy(dev->dev_addr, iap, ETH_ALEN);
  1010. /* Adjust MAC if using default MAC address */
  1011. if (iap == fec_mac_default)
  1012. dev->dev_addr[ETH_ALEN-1] = fec_mac_default[ETH_ALEN-1] + fep->index;
  1013. }
  1014. #endif
  1015. /* ------------------------------------------------------------------------- */
  1016. static void mii_display_status(struct net_device *dev)
  1017. {
  1018. struct fec_enet_private *fep = netdev_priv(dev);
  1019. volatile uint *s = &(fep->phy_status);
  1020. if (!fep->link && !fep->old_link) {
  1021. /* Link is still down - don't print anything */
  1022. return;
  1023. }
  1024. printk("%s: status: ", dev->name);
  1025. if (!fep->link) {
  1026. printk("link down");
  1027. } else {
  1028. printk("link up");
  1029. switch(*s & PHY_STAT_SPMASK) {
  1030. case PHY_STAT_100FDX: printk(", 100MBit Full Duplex"); break;
  1031. case PHY_STAT_100HDX: printk(", 100MBit Half Duplex"); break;
  1032. case PHY_STAT_10FDX: printk(", 10MBit Full Duplex"); break;
  1033. case PHY_STAT_10HDX: printk(", 10MBit Half Duplex"); break;
  1034. default:
  1035. printk(", Unknown speed/duplex");
  1036. }
  1037. if (*s & PHY_STAT_ANC)
  1038. printk(", auto-negotiation complete");
  1039. }
  1040. if (*s & PHY_STAT_FAULT)
  1041. printk(", remote fault");
  1042. printk(".\n");
  1043. }
  1044. static void mii_display_config(struct work_struct *work)
  1045. {
  1046. struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
  1047. struct net_device *dev = fep->netdev;
  1048. uint status = fep->phy_status;
  1049. /*
  1050. ** When we get here, phy_task is already removed from
  1051. ** the workqueue. It is thus safe to allow to reuse it.
  1052. */
  1053. fep->mii_phy_task_queued = 0;
  1054. printk("%s: config: auto-negotiation ", dev->name);
  1055. if (status & PHY_CONF_ANE)
  1056. printk("on");
  1057. else
  1058. printk("off");
  1059. if (status & PHY_CONF_100FDX)
  1060. printk(", 100FDX");
  1061. if (status & PHY_CONF_100HDX)
  1062. printk(", 100HDX");
  1063. if (status & PHY_CONF_10FDX)
  1064. printk(", 10FDX");
  1065. if (status & PHY_CONF_10HDX)
  1066. printk(", 10HDX");
  1067. if (!(status & PHY_CONF_SPMASK))
  1068. printk(", No speed/duplex selected?");
  1069. if (status & PHY_CONF_LOOP)
  1070. printk(", loopback enabled");
  1071. printk(".\n");
  1072. fep->sequence_done = 1;
  1073. }
  1074. static void mii_relink(struct work_struct *work)
  1075. {
  1076. struct fec_enet_private *fep = container_of(work, struct fec_enet_private, phy_task);
  1077. struct net_device *dev = fep->netdev;
  1078. int duplex;
  1079. /*
  1080. ** When we get here, phy_task is already removed from
  1081. ** the workqueue. It is thus safe to allow to reuse it.
  1082. */
  1083. fep->mii_phy_task_queued = 0;
  1084. fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
  1085. mii_display_status(dev);
  1086. fep->old_link = fep->link;
  1087. if (fep->link) {
  1088. duplex = 0;
  1089. if (fep->phy_status
  1090. & (PHY_STAT_100FDX | PHY_STAT_10FDX))
  1091. duplex = 1;
  1092. fec_restart(dev, duplex);
  1093. } else
  1094. fec_stop(dev);
  1095. }
  1096. /* mii_queue_relink is called in interrupt context from mii_link_interrupt */
  1097. static void mii_queue_relink(uint mii_reg, struct net_device *dev)
  1098. {
  1099. struct fec_enet_private *fep = netdev_priv(dev);
  1100. /*
  1101. * We cannot queue phy_task twice in the workqueue. It
  1102. * would cause an endless loop in the workqueue.
  1103. * Fortunately, if the last mii_relink entry has not yet been
  1104. * executed now, it will do the job for the current interrupt,
  1105. * which is just what we want.
  1106. */
  1107. if (fep->mii_phy_task_queued)
  1108. return;
  1109. fep->mii_phy_task_queued = 1;
  1110. INIT_WORK(&fep->phy_task, mii_relink);
  1111. schedule_work(&fep->phy_task);
  1112. }
  1113. /* mii_queue_config is called in interrupt context from fec_enet_mii */
  1114. static void mii_queue_config(uint mii_reg, struct net_device *dev)
  1115. {
  1116. struct fec_enet_private *fep = netdev_priv(dev);
  1117. if (fep->mii_phy_task_queued)
  1118. return;
  1119. fep->mii_phy_task_queued = 1;
  1120. INIT_WORK(&fep->phy_task, mii_display_config);
  1121. schedule_work(&fep->phy_task);
  1122. }
  1123. phy_cmd_t const phy_cmd_relink[] = {
  1124. { mk_mii_read(MII_REG_CR), mii_queue_relink },
  1125. { mk_mii_end, }
  1126. };
  1127. phy_cmd_t const phy_cmd_config[] = {
  1128. { mk_mii_read(MII_REG_CR), mii_queue_config },
  1129. { mk_mii_end, }
  1130. };
  1131. /* Read remainder of PHY ID. */
  1132. static void
  1133. mii_discover_phy3(uint mii_reg, struct net_device *dev)
  1134. {
  1135. struct fec_enet_private *fep;
  1136. int i;
  1137. fep = netdev_priv(dev);
  1138. fep->phy_id |= (mii_reg & 0xffff);
  1139. printk("fec: PHY @ 0x%x, ID 0x%08x", fep->phy_addr, fep->phy_id);
  1140. for(i = 0; phy_info[i]; i++) {
  1141. if(phy_info[i]->id == (fep->phy_id >> 4))
  1142. break;
  1143. }
  1144. if (phy_info[i])
  1145. printk(" -- %s\n", phy_info[i]->name);
  1146. else
  1147. printk(" -- unknown PHY!\n");
  1148. fep->phy = phy_info[i];
  1149. fep->phy_id_done = 1;
  1150. }
  1151. /* Scan all of the MII PHY addresses looking for someone to respond
  1152. * with a valid ID. This usually happens quickly.
  1153. */
  1154. static void
  1155. mii_discover_phy(uint mii_reg, struct net_device *dev)
  1156. {
  1157. struct fec_enet_private *fep;
  1158. uint phytype;
  1159. fep = netdev_priv(dev);
  1160. if (fep->phy_addr < 32) {
  1161. if ((phytype = (mii_reg & 0xffff)) != 0xffff && phytype != 0) {
  1162. /* Got first part of ID, now get remainder */
  1163. fep->phy_id = phytype << 16;
  1164. mii_queue(dev, mk_mii_read(MII_REG_PHYIR2),
  1165. mii_discover_phy3);
  1166. } else {
  1167. fep->phy_addr++;
  1168. mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
  1169. mii_discover_phy);
  1170. }
  1171. } else {
  1172. printk("FEC: No PHY device found.\n");
  1173. /* Disable external MII interface */
  1174. writel(0, fep->hwp + FEC_MII_SPEED);
  1175. fep->phy_speed = 0;
  1176. #ifdef HAVE_mii_link_interrupt
  1177. fec_disable_phy_intr();
  1178. #endif
  1179. }
  1180. }
  1181. /* This interrupt occurs when the PHY detects a link change */
  1182. #ifdef HAVE_mii_link_interrupt
  1183. static irqreturn_t
  1184. mii_link_interrupt(int irq, void * dev_id)
  1185. {
  1186. struct net_device *dev = dev_id;
  1187. struct fec_enet_private *fep = netdev_priv(dev);
  1188. fec_phy_ack_intr();
  1189. mii_do_cmd(dev, fep->phy->ack_int);
  1190. mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
  1191. return IRQ_HANDLED;
  1192. }
  1193. #endif
  1194. static void fec_enet_free_buffers(struct net_device *dev)
  1195. {
  1196. struct fec_enet_private *fep = netdev_priv(dev);
  1197. int i;
  1198. struct sk_buff *skb;
  1199. struct bufdesc *bdp;
  1200. bdp = fep->rx_bd_base;
  1201. for (i = 0; i < RX_RING_SIZE; i++) {
  1202. skb = fep->rx_skbuff[i];
  1203. if (bdp->cbd_bufaddr)
  1204. dma_unmap_single(&dev->dev, bdp->cbd_bufaddr,
  1205. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  1206. if (skb)
  1207. dev_kfree_skb(skb);
  1208. bdp++;
  1209. }
  1210. bdp = fep->tx_bd_base;
  1211. for (i = 0; i < TX_RING_SIZE; i++)
  1212. kfree(fep->tx_bounce[i]);
  1213. }
  1214. static int fec_enet_alloc_buffers(struct net_device *dev)
  1215. {
  1216. struct fec_enet_private *fep = netdev_priv(dev);
  1217. int i;
  1218. struct sk_buff *skb;
  1219. struct bufdesc *bdp;
  1220. bdp = fep->rx_bd_base;
  1221. for (i = 0; i < RX_RING_SIZE; i++) {
  1222. skb = dev_alloc_skb(FEC_ENET_RX_FRSIZE);
  1223. if (!skb) {
  1224. fec_enet_free_buffers(dev);
  1225. return -ENOMEM;
  1226. }
  1227. fep->rx_skbuff[i] = skb;
  1228. bdp->cbd_bufaddr = dma_map_single(&dev->dev, skb->data,
  1229. FEC_ENET_RX_FRSIZE, DMA_FROM_DEVICE);
  1230. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1231. bdp++;
  1232. }
  1233. /* Set the last buffer to wrap. */
  1234. bdp--;
  1235. bdp->cbd_sc |= BD_SC_WRAP;
  1236. bdp = fep->tx_bd_base;
  1237. for (i = 0; i < TX_RING_SIZE; i++) {
  1238. fep->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
  1239. bdp->cbd_sc = 0;
  1240. bdp->cbd_bufaddr = 0;
  1241. bdp++;
  1242. }
  1243. /* Set the last buffer to wrap. */
  1244. bdp--;
  1245. bdp->cbd_sc |= BD_SC_WRAP;
  1246. return 0;
  1247. }
  1248. static int
  1249. fec_enet_open(struct net_device *dev)
  1250. {
  1251. struct fec_enet_private *fep = netdev_priv(dev);
  1252. int ret;
  1253. /* I should reset the ring buffers here, but I don't yet know
  1254. * a simple way to do that.
  1255. */
  1256. ret = fec_enet_alloc_buffers(dev);
  1257. if (ret)
  1258. return ret;
  1259. fep->sequence_done = 0;
  1260. fep->link = 0;
  1261. fec_restart(dev, 1);
  1262. if (fep->phy) {
  1263. mii_do_cmd(dev, fep->phy->ack_int);
  1264. mii_do_cmd(dev, fep->phy->config);
  1265. mii_do_cmd(dev, phy_cmd_config); /* display configuration */
  1266. /* Poll until the PHY tells us its configuration
  1267. * (not link state).
  1268. * Request is initiated by mii_do_cmd above, but answer
  1269. * comes by interrupt.
  1270. * This should take about 25 usec per register at 2.5 MHz,
  1271. * and we read approximately 5 registers.
  1272. */
  1273. while(!fep->sequence_done)
  1274. schedule();
  1275. mii_do_cmd(dev, fep->phy->startup);
  1276. }
  1277. /* Set the initial link state to true. A lot of hardware
  1278. * based on this device does not implement a PHY interrupt,
  1279. * so we are never notified of link change.
  1280. */
  1281. fep->link = 1;
  1282. netif_start_queue(dev);
  1283. fep->opened = 1;
  1284. return 0;
  1285. }
  1286. static int
  1287. fec_enet_close(struct net_device *dev)
  1288. {
  1289. struct fec_enet_private *fep = netdev_priv(dev);
  1290. /* Don't know what to do yet. */
  1291. fep->opened = 0;
  1292. netif_stop_queue(dev);
  1293. fec_stop(dev);
  1294. fec_enet_free_buffers(dev);
  1295. return 0;
  1296. }
  1297. /* Set or clear the multicast filter for this adaptor.
  1298. * Skeleton taken from sunlance driver.
  1299. * The CPM Ethernet implementation allows Multicast as well as individual
  1300. * MAC address filtering. Some of the drivers check to make sure it is
  1301. * a group multicast address, and discard those that are not. I guess I
  1302. * will do the same for now, but just remove the test if you want
  1303. * individual filtering as well (do the upper net layers want or support
  1304. * this kind of feature?).
  1305. */
  1306. #define HASH_BITS 6 /* #bits in hash */
  1307. #define CRC32_POLY 0xEDB88320
  1308. static void set_multicast_list(struct net_device *dev)
  1309. {
  1310. struct fec_enet_private *fep = netdev_priv(dev);
  1311. struct dev_mc_list *dmi;
  1312. unsigned int i, j, bit, data, crc, tmp;
  1313. unsigned char hash;
  1314. if (dev->flags & IFF_PROMISC) {
  1315. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1316. tmp |= 0x8;
  1317. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1318. return;
  1319. }
  1320. tmp = readl(fep->hwp + FEC_R_CNTRL);
  1321. tmp &= ~0x8;
  1322. writel(tmp, fep->hwp + FEC_R_CNTRL);
  1323. if (dev->flags & IFF_ALLMULTI) {
  1324. /* Catch all multicast addresses, so set the
  1325. * filter to all 1's
  1326. */
  1327. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1328. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1329. return;
  1330. }
  1331. /* Clear filter and add the addresses in hash register
  1332. */
  1333. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1334. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1335. dmi = dev->mc_list;
  1336. for (j = 0; j < dev->mc_count; j++, dmi = dmi->next) {
  1337. /* Only support group multicast for now */
  1338. if (!(dmi->dmi_addr[0] & 1))
  1339. continue;
  1340. /* calculate crc32 value of mac address */
  1341. crc = 0xffffffff;
  1342. for (i = 0; i < dmi->dmi_addrlen; i++) {
  1343. data = dmi->dmi_addr[i];
  1344. for (bit = 0; bit < 8; bit++, data >>= 1) {
  1345. crc = (crc >> 1) ^
  1346. (((crc ^ data) & 1) ? CRC32_POLY : 0);
  1347. }
  1348. }
  1349. /* only upper 6 bits (HASH_BITS) are used
  1350. * which point to specific bit in he hash registers
  1351. */
  1352. hash = (crc >> (32 - HASH_BITS)) & 0x3f;
  1353. if (hash > 31) {
  1354. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1355. tmp |= 1 << (hash - 32);
  1356. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1357. } else {
  1358. tmp = readl(fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1359. tmp |= 1 << hash;
  1360. writel(tmp, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1361. }
  1362. }
  1363. }
  1364. /* Set a MAC change in hardware. */
  1365. static int
  1366. fec_set_mac_address(struct net_device *dev, void *p)
  1367. {
  1368. struct fec_enet_private *fep = netdev_priv(dev);
  1369. struct sockaddr *addr = p;
  1370. if (!is_valid_ether_addr(addr->sa_data))
  1371. return -EADDRNOTAVAIL;
  1372. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1373. writel(dev->dev_addr[3] | (dev->dev_addr[2] << 8) |
  1374. (dev->dev_addr[1] << 16) | (dev->dev_addr[0] << 24),
  1375. fep->hwp + FEC_ADDR_LOW);
  1376. writel((dev->dev_addr[5] << 16) | (dev->dev_addr[4] << 24),
  1377. fep + FEC_ADDR_HIGH);
  1378. return 0;
  1379. }
  1380. static const struct net_device_ops fec_netdev_ops = {
  1381. .ndo_open = fec_enet_open,
  1382. .ndo_stop = fec_enet_close,
  1383. .ndo_start_xmit = fec_enet_start_xmit,
  1384. .ndo_set_multicast_list = set_multicast_list,
  1385. .ndo_change_mtu = eth_change_mtu,
  1386. .ndo_validate_addr = eth_validate_addr,
  1387. .ndo_tx_timeout = fec_timeout,
  1388. .ndo_set_mac_address = fec_set_mac_address,
  1389. };
  1390. /*
  1391. * XXX: We need to clean up on failure exits here.
  1392. *
  1393. * index is only used in legacy code
  1394. */
  1395. int __init fec_enet_init(struct net_device *dev, int index)
  1396. {
  1397. struct fec_enet_private *fep = netdev_priv(dev);
  1398. struct bufdesc *cbd_base;
  1399. int i;
  1400. /* Allocate memory for buffer descriptors. */
  1401. cbd_base = dma_alloc_coherent(NULL, PAGE_SIZE, &fep->bd_dma,
  1402. GFP_KERNEL);
  1403. if (!cbd_base) {
  1404. printk("FEC: allocate descriptor memory failed?\n");
  1405. return -ENOMEM;
  1406. }
  1407. spin_lock_init(&fep->hw_lock);
  1408. spin_lock_init(&fep->mii_lock);
  1409. fep->index = index;
  1410. fep->hwp = (void __iomem *)dev->base_addr;
  1411. fep->netdev = dev;
  1412. /* Set the Ethernet address */
  1413. #ifdef CONFIG_M5272
  1414. fec_get_mac(dev);
  1415. #else
  1416. {
  1417. unsigned long l;
  1418. l = readl(fep->hwp + FEC_ADDR_LOW);
  1419. dev->dev_addr[0] = (unsigned char)((l & 0xFF000000) >> 24);
  1420. dev->dev_addr[1] = (unsigned char)((l & 0x00FF0000) >> 16);
  1421. dev->dev_addr[2] = (unsigned char)((l & 0x0000FF00) >> 8);
  1422. dev->dev_addr[3] = (unsigned char)((l & 0x000000FF) >> 0);
  1423. l = readl(fep->hwp + FEC_ADDR_HIGH);
  1424. dev->dev_addr[4] = (unsigned char)((l & 0xFF000000) >> 24);
  1425. dev->dev_addr[5] = (unsigned char)((l & 0x00FF0000) >> 16);
  1426. }
  1427. #endif
  1428. /* Set receive and transmit descriptor base. */
  1429. fep->rx_bd_base = cbd_base;
  1430. fep->tx_bd_base = cbd_base + RX_RING_SIZE;
  1431. #ifdef HAVE_mii_link_interrupt
  1432. fec_request_mii_intr(dev);
  1433. #endif
  1434. /* The FEC Ethernet specific entries in the device structure */
  1435. dev->watchdog_timeo = TX_TIMEOUT;
  1436. dev->netdev_ops = &fec_netdev_ops;
  1437. for (i=0; i<NMII-1; i++)
  1438. mii_cmds[i].mii_next = &mii_cmds[i+1];
  1439. mii_free = mii_cmds;
  1440. /* Set MII speed to 2.5 MHz */
  1441. fep->phy_speed = ((((clk_get_rate(fep->clk) / 2 + 4999999)
  1442. / 2500000) / 2) & 0x3F) << 1;
  1443. fec_restart(dev, 0);
  1444. /* Queue up command to detect the PHY and initialize the
  1445. * remainder of the interface.
  1446. */
  1447. fep->phy_id_done = 0;
  1448. fep->phy_addr = 0;
  1449. mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
  1450. return 0;
  1451. }
  1452. /* This function is called to start or restart the FEC during a link
  1453. * change. This only happens when switching between half and full
  1454. * duplex.
  1455. */
  1456. static void
  1457. fec_restart(struct net_device *dev, int duplex)
  1458. {
  1459. struct fec_enet_private *fep = netdev_priv(dev);
  1460. struct bufdesc *bdp;
  1461. int i;
  1462. /* Whack a reset. We should wait for this. */
  1463. writel(1, fep->hwp + FEC_ECNTRL);
  1464. udelay(10);
  1465. /* Clear any outstanding interrupt. */
  1466. writel(0xffc00000, fep->hwp + FEC_IEVENT);
  1467. /* Reset all multicast. */
  1468. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  1469. writel(0, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  1470. #ifndef CONFIG_M5272
  1471. writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
  1472. writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
  1473. #endif
  1474. /* Set maximum receive buffer size. */
  1475. writel(PKT_MAXBLR_SIZE, fep->hwp + FEC_R_BUFF_SIZE);
  1476. /* Set receive and transmit descriptor base. */
  1477. writel(fep->bd_dma, fep->hwp + FEC_R_DES_START);
  1478. writel((unsigned long)fep->bd_dma + sizeof(struct bufdesc) * RX_RING_SIZE,
  1479. fep->hwp + FEC_X_DES_START);
  1480. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  1481. fep->cur_rx = fep->rx_bd_base;
  1482. /* Reset SKB transmit buffers. */
  1483. fep->skb_cur = fep->skb_dirty = 0;
  1484. for (i = 0; i <= TX_RING_MOD_MASK; i++) {
  1485. if (fep->tx_skbuff[i]) {
  1486. dev_kfree_skb_any(fep->tx_skbuff[i]);
  1487. fep->tx_skbuff[i] = NULL;
  1488. }
  1489. }
  1490. /* Initialize the receive buffer descriptors. */
  1491. bdp = fep->rx_bd_base;
  1492. for (i = 0; i < RX_RING_SIZE; i++) {
  1493. /* Initialize the BD for every fragment in the page. */
  1494. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1495. bdp++;
  1496. }
  1497. /* Set the last buffer to wrap */
  1498. bdp--;
  1499. bdp->cbd_sc |= BD_SC_WRAP;
  1500. /* ...and the same for transmit */
  1501. bdp = fep->tx_bd_base;
  1502. for (i = 0; i < TX_RING_SIZE; i++) {
  1503. /* Initialize the BD for every fragment in the page. */
  1504. bdp->cbd_sc = 0;
  1505. bdp->cbd_bufaddr = 0;
  1506. bdp++;
  1507. }
  1508. /* Set the last buffer to wrap */
  1509. bdp--;
  1510. bdp->cbd_sc |= BD_SC_WRAP;
  1511. /* Enable MII mode */
  1512. if (duplex) {
  1513. /* MII enable / FD enable */
  1514. writel(OPT_FRAME_SIZE | 0x04, fep->hwp + FEC_R_CNTRL);
  1515. writel(0x04, fep->hwp + FEC_X_CNTRL);
  1516. } else {
  1517. /* MII enable / No Rcv on Xmit */
  1518. writel(OPT_FRAME_SIZE | 0x06, fep->hwp + FEC_R_CNTRL);
  1519. writel(0x0, fep->hwp + FEC_X_CNTRL);
  1520. }
  1521. fep->full_duplex = duplex;
  1522. /* Set MII speed */
  1523. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1524. /* And last, enable the transmit and receive processing */
  1525. writel(2, fep->hwp + FEC_ECNTRL);
  1526. writel(0, fep->hwp + FEC_R_DES_ACTIVE);
  1527. /* Enable interrupts we wish to service */
  1528. writel(FEC_ENET_TXF | FEC_ENET_RXF | FEC_ENET_MII,
  1529. fep->hwp + FEC_IMASK);
  1530. }
  1531. static void
  1532. fec_stop(struct net_device *dev)
  1533. {
  1534. struct fec_enet_private *fep = netdev_priv(dev);
  1535. /* We cannot expect a graceful transmit stop without link !!! */
  1536. if (fep->link) {
  1537. writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
  1538. udelay(10);
  1539. if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
  1540. printk("fec_stop : Graceful transmit stop did not complete !\n");
  1541. }
  1542. /* Whack a reset. We should wait for this. */
  1543. writel(1, fep->hwp + FEC_ECNTRL);
  1544. udelay(10);
  1545. /* Clear outstanding MII command interrupts. */
  1546. writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
  1547. writel(FEC_ENET_MII, fep->hwp + FEC_IMASK);
  1548. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1549. }
  1550. static int __devinit
  1551. fec_probe(struct platform_device *pdev)
  1552. {
  1553. struct fec_enet_private *fep;
  1554. struct net_device *ndev;
  1555. int i, irq, ret = 0;
  1556. struct resource *r;
  1557. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1558. if (!r)
  1559. return -ENXIO;
  1560. r = request_mem_region(r->start, resource_size(r), pdev->name);
  1561. if (!r)
  1562. return -EBUSY;
  1563. /* Init network device */
  1564. ndev = alloc_etherdev(sizeof(struct fec_enet_private));
  1565. if (!ndev)
  1566. return -ENOMEM;
  1567. SET_NETDEV_DEV(ndev, &pdev->dev);
  1568. /* setup board info structure */
  1569. fep = netdev_priv(ndev);
  1570. memset(fep, 0, sizeof(*fep));
  1571. ndev->base_addr = (unsigned long)ioremap(r->start, resource_size(r));
  1572. if (!ndev->base_addr) {
  1573. ret = -ENOMEM;
  1574. goto failed_ioremap;
  1575. }
  1576. platform_set_drvdata(pdev, ndev);
  1577. /* This device has up to three irqs on some platforms */
  1578. for (i = 0; i < 3; i++) {
  1579. irq = platform_get_irq(pdev, i);
  1580. if (i && irq < 0)
  1581. break;
  1582. ret = request_irq(irq, fec_enet_interrupt, IRQF_DISABLED, pdev->name, ndev);
  1583. if (ret) {
  1584. while (i >= 0) {
  1585. irq = platform_get_irq(pdev, i);
  1586. free_irq(irq, ndev);
  1587. i--;
  1588. }
  1589. goto failed_irq;
  1590. }
  1591. }
  1592. fep->clk = clk_get(&pdev->dev, "fec_clk");
  1593. if (IS_ERR(fep->clk)) {
  1594. ret = PTR_ERR(fep->clk);
  1595. goto failed_clk;
  1596. }
  1597. clk_enable(fep->clk);
  1598. ret = fec_enet_init(ndev, 0);
  1599. if (ret)
  1600. goto failed_init;
  1601. ret = register_netdev(ndev);
  1602. if (ret)
  1603. goto failed_register;
  1604. return 0;
  1605. failed_register:
  1606. failed_init:
  1607. clk_disable(fep->clk);
  1608. clk_put(fep->clk);
  1609. failed_clk:
  1610. for (i = 0; i < 3; i++) {
  1611. irq = platform_get_irq(pdev, i);
  1612. if (irq > 0)
  1613. free_irq(irq, ndev);
  1614. }
  1615. failed_irq:
  1616. iounmap((void __iomem *)ndev->base_addr);
  1617. failed_ioremap:
  1618. free_netdev(ndev);
  1619. return ret;
  1620. }
  1621. static int __devexit
  1622. fec_drv_remove(struct platform_device *pdev)
  1623. {
  1624. struct net_device *ndev = platform_get_drvdata(pdev);
  1625. struct fec_enet_private *fep = netdev_priv(ndev);
  1626. platform_set_drvdata(pdev, NULL);
  1627. fec_stop(ndev);
  1628. clk_disable(fep->clk);
  1629. clk_put(fep->clk);
  1630. iounmap((void __iomem *)ndev->base_addr);
  1631. unregister_netdev(ndev);
  1632. free_netdev(ndev);
  1633. return 0;
  1634. }
  1635. static int
  1636. fec_suspend(struct platform_device *dev, pm_message_t state)
  1637. {
  1638. struct net_device *ndev = platform_get_drvdata(dev);
  1639. struct fec_enet_private *fep;
  1640. if (ndev) {
  1641. fep = netdev_priv(ndev);
  1642. if (netif_running(ndev)) {
  1643. netif_device_detach(ndev);
  1644. fec_stop(ndev);
  1645. }
  1646. }
  1647. return 0;
  1648. }
  1649. static int
  1650. fec_resume(struct platform_device *dev)
  1651. {
  1652. struct net_device *ndev = platform_get_drvdata(dev);
  1653. if (ndev) {
  1654. if (netif_running(ndev)) {
  1655. fec_enet_init(ndev, 0);
  1656. netif_device_attach(ndev);
  1657. }
  1658. }
  1659. return 0;
  1660. }
  1661. static struct platform_driver fec_driver = {
  1662. .driver = {
  1663. .name = "fec",
  1664. .owner = THIS_MODULE,
  1665. },
  1666. .probe = fec_probe,
  1667. .remove = __devexit_p(fec_drv_remove),
  1668. .suspend = fec_suspend,
  1669. .resume = fec_resume,
  1670. };
  1671. static int __init
  1672. fec_enet_module_init(void)
  1673. {
  1674. printk(KERN_INFO "FEC Ethernet Driver\n");
  1675. return platform_driver_register(&fec_driver);
  1676. }
  1677. static void __exit
  1678. fec_enet_cleanup(void)
  1679. {
  1680. platform_driver_unregister(&fec_driver);
  1681. }
  1682. module_exit(fec_enet_cleanup);
  1683. module_init(fec_enet_module_init);
  1684. MODULE_LICENSE("GPL");