fec.c 58 KB

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