fec.c 66 KB

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