fec.c 58 KB

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