fec.c 54 KB

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