fec.c 67 KB

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