fec.c 55 KB

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