fec.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  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. * Includes support for the following PHYs: QS6612, LXT970, LXT971/2.
  12. *
  13. * Right now, I am very wasteful with the buffers. I allocate memory
  14. * pages and then divide them into 2K frame buffers. This way I know I
  15. * have buffers large enough to hold one frame within one buffer descriptor.
  16. * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  17. * will be much more memory efficient and will easily handle lots of
  18. * small packets.
  19. *
  20. * Much better multiple PHY support by Magnus Damm.
  21. * Copyright (c) 2000 Ericsson Radio Systems AB.
  22. *
  23. * Make use of MII for PHY control configurable.
  24. * Some fixes.
  25. * Copyright (c) 2000-2002 Wolfgang Denk, DENX Software Engineering.
  26. *
  27. * Support for AMD AM79C874 added.
  28. * Thomas Lange, thomas@corelatus.com
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/sched.h>
  32. #include <linux/string.h>
  33. #include <linux/ptrace.h>
  34. #include <linux/errno.h>
  35. #include <linux/ioport.h>
  36. #include <linux/slab.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/pci.h>
  39. #include <linux/init.h>
  40. #include <linux/delay.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/bitops.h>
  46. #ifdef CONFIG_FEC_PACKETHOOK
  47. #include <linux/pkthook.h>
  48. #endif
  49. #include <asm/8xx_immap.h>
  50. #include <asm/pgtable.h>
  51. #include <asm/mpc8xx.h>
  52. #include <asm/irq.h>
  53. #include <asm/uaccess.h>
  54. #include <asm/cpm1.h>
  55. #ifdef CONFIG_USE_MDIO
  56. /* Forward declarations of some structures to support different PHYs
  57. */
  58. typedef struct {
  59. uint mii_data;
  60. void (*funct)(uint mii_reg, struct net_device *dev);
  61. } phy_cmd_t;
  62. typedef struct {
  63. uint id;
  64. char *name;
  65. const phy_cmd_t *config;
  66. const phy_cmd_t *startup;
  67. const phy_cmd_t *ack_int;
  68. const phy_cmd_t *shutdown;
  69. } phy_info_t;
  70. #endif /* CONFIG_USE_MDIO */
  71. /* The number of Tx and Rx buffers. These are allocated from the page
  72. * pool. The code may assume these are power of two, so it is best
  73. * to keep them that size.
  74. * We don't need to allocate pages for the transmitter. We just use
  75. * the skbuffer directly.
  76. */
  77. #ifdef CONFIG_ENET_BIG_BUFFERS
  78. #define FEC_ENET_RX_PAGES 16
  79. #define FEC_ENET_RX_FRSIZE 2048
  80. #define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
  81. #define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
  82. #define TX_RING_SIZE 16 /* Must be power of two */
  83. #define TX_RING_MOD_MASK 15 /* for this to work */
  84. #else
  85. #define FEC_ENET_RX_PAGES 4
  86. #define FEC_ENET_RX_FRSIZE 2048
  87. #define FEC_ENET_RX_FRPPG (PAGE_SIZE / FEC_ENET_RX_FRSIZE)
  88. #define RX_RING_SIZE (FEC_ENET_RX_FRPPG * FEC_ENET_RX_PAGES)
  89. #define TX_RING_SIZE 8 /* Must be power of two */
  90. #define TX_RING_MOD_MASK 7 /* for this to work */
  91. #endif
  92. /* Interrupt events/masks.
  93. */
  94. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  95. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  96. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  97. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  98. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  99. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  100. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  101. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  102. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  103. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  104. /*
  105. */
  106. #define FEC_ECNTRL_PINMUX 0x00000004
  107. #define FEC_ECNTRL_ETHER_EN 0x00000002
  108. #define FEC_ECNTRL_RESET 0x00000001
  109. #define FEC_RCNTRL_BC_REJ 0x00000010
  110. #define FEC_RCNTRL_PROM 0x00000008
  111. #define FEC_RCNTRL_MII_MODE 0x00000004
  112. #define FEC_RCNTRL_DRT 0x00000002
  113. #define FEC_RCNTRL_LOOP 0x00000001
  114. #define FEC_TCNTRL_FDEN 0x00000004
  115. #define FEC_TCNTRL_HBC 0x00000002
  116. #define FEC_TCNTRL_GTS 0x00000001
  117. /* Delay to wait for FEC reset command to complete (in us)
  118. */
  119. #define FEC_RESET_DELAY 50
  120. /* The FEC stores dest/src/type, data, and checksum for receive packets.
  121. */
  122. #define PKT_MAXBUF_SIZE 1518
  123. #define PKT_MINBUF_SIZE 64
  124. #define PKT_MAXBLR_SIZE 1520
  125. /* The FEC buffer descriptors track the ring buffers. The rx_bd_base and
  126. * tx_bd_base always point to the base of the buffer descriptors. The
  127. * cur_rx and cur_tx point to the currently available buffer.
  128. * The dirty_tx tracks the current buffer that is being sent by the
  129. * controller. The cur_tx and dirty_tx are equal under both completely
  130. * empty and completely full conditions. The empty/ready indicator in
  131. * the buffer descriptor determines the actual condition.
  132. */
  133. struct fec_enet_private {
  134. /* The saved address of a sent-in-place packet/buffer, for skfree(). */
  135. struct sk_buff* tx_skbuff[TX_RING_SIZE];
  136. ushort skb_cur;
  137. ushort skb_dirty;
  138. /* CPM dual port RAM relative addresses.
  139. */
  140. cbd_t *rx_bd_base; /* Address of Rx and Tx buffers. */
  141. cbd_t *tx_bd_base;
  142. cbd_t *cur_rx, *cur_tx; /* The next free ring entry */
  143. cbd_t *dirty_tx; /* The ring entries to be free()ed. */
  144. /* Virtual addresses for the receive buffers because we can't
  145. * do a __va() on them anymore.
  146. */
  147. unsigned char *rx_vaddr[RX_RING_SIZE];
  148. struct net_device_stats stats;
  149. uint tx_full;
  150. spinlock_t lock;
  151. #ifdef CONFIG_USE_MDIO
  152. uint phy_id;
  153. uint phy_id_done;
  154. uint phy_status;
  155. uint phy_speed;
  156. phy_info_t *phy;
  157. struct work_struct phy_task;
  158. struct net_device *dev;
  159. uint sequence_done;
  160. uint phy_addr;
  161. #endif /* CONFIG_USE_MDIO */
  162. int link;
  163. int old_link;
  164. int full_duplex;
  165. #ifdef CONFIG_FEC_PACKETHOOK
  166. unsigned long ph_lock;
  167. fec_ph_func *ph_rxhandler;
  168. fec_ph_func *ph_txhandler;
  169. __u16 ph_proto;
  170. volatile __u32 *ph_regaddr;
  171. void *ph_priv;
  172. #endif
  173. };
  174. static int fec_enet_open(struct net_device *dev);
  175. static int fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev);
  176. #ifdef CONFIG_USE_MDIO
  177. static void fec_enet_mii(struct net_device *dev);
  178. #endif /* CONFIG_USE_MDIO */
  179. #ifdef CONFIG_FEC_PACKETHOOK
  180. static void fec_enet_tx(struct net_device *dev, __u32 regval);
  181. static void fec_enet_rx(struct net_device *dev, __u32 regval);
  182. #else
  183. static void fec_enet_tx(struct net_device *dev);
  184. static void fec_enet_rx(struct net_device *dev);
  185. #endif
  186. static int fec_enet_close(struct net_device *dev);
  187. static struct net_device_stats *fec_enet_get_stats(struct net_device *dev);
  188. static void set_multicast_list(struct net_device *dev);
  189. static void fec_restart(struct net_device *dev, int duplex);
  190. static void fec_stop(struct net_device *dev);
  191. static ushort my_enet_addr[3];
  192. #ifdef CONFIG_USE_MDIO
  193. /* MII processing. We keep this as simple as possible. Requests are
  194. * placed on the list (if there is room). When the request is finished
  195. * by the MII, an optional function may be called.
  196. */
  197. typedef struct mii_list {
  198. uint mii_regval;
  199. void (*mii_func)(uint val, struct net_device *dev);
  200. struct mii_list *mii_next;
  201. } mii_list_t;
  202. #define NMII 20
  203. mii_list_t mii_cmds[NMII];
  204. mii_list_t *mii_free;
  205. mii_list_t *mii_head;
  206. mii_list_t *mii_tail;
  207. static int mii_queue(struct net_device *dev, int request,
  208. void (*func)(uint, struct net_device *));
  209. /* Make MII read/write commands for the FEC.
  210. */
  211. #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
  212. #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | \
  213. (VAL & 0xffff))
  214. #define mk_mii_end 0
  215. #endif /* CONFIG_USE_MDIO */
  216. /* Transmitter timeout.
  217. */
  218. #define TX_TIMEOUT (2*HZ)
  219. #ifdef CONFIG_USE_MDIO
  220. /* Register definitions for the PHY.
  221. */
  222. #define MII_REG_CR 0 /* Control Register */
  223. #define MII_REG_SR 1 /* Status Register */
  224. #define MII_REG_PHYIR1 2 /* PHY Identification Register 1 */
  225. #define MII_REG_PHYIR2 3 /* PHY Identification Register 2 */
  226. #define MII_REG_ANAR 4 /* A-N Advertisement Register */
  227. #define MII_REG_ANLPAR 5 /* A-N Link Partner Ability Register */
  228. #define MII_REG_ANER 6 /* A-N Expansion Register */
  229. #define MII_REG_ANNPTR 7 /* A-N Next Page Transmit Register */
  230. #define MII_REG_ANLPRNPR 8 /* A-N Link Partner Received Next Page Reg. */
  231. /* values for phy_status */
  232. #define PHY_CONF_ANE 0x0001 /* 1 auto-negotiation enabled */
  233. #define PHY_CONF_LOOP 0x0002 /* 1 loopback mode enabled */
  234. #define PHY_CONF_SPMASK 0x00f0 /* mask for speed */
  235. #define PHY_CONF_10HDX 0x0010 /* 10 Mbit half duplex supported */
  236. #define PHY_CONF_10FDX 0x0020 /* 10 Mbit full duplex supported */
  237. #define PHY_CONF_100HDX 0x0040 /* 100 Mbit half duplex supported */
  238. #define PHY_CONF_100FDX 0x0080 /* 100 Mbit full duplex supported */
  239. #define PHY_STAT_LINK 0x0100 /* 1 up - 0 down */
  240. #define PHY_STAT_FAULT 0x0200 /* 1 remote fault */
  241. #define PHY_STAT_ANC 0x0400 /* 1 auto-negotiation complete */
  242. #define PHY_STAT_SPMASK 0xf000 /* mask for speed */
  243. #define PHY_STAT_10HDX 0x1000 /* 10 Mbit half duplex selected */
  244. #define PHY_STAT_10FDX 0x2000 /* 10 Mbit full duplex selected */
  245. #define PHY_STAT_100HDX 0x4000 /* 100 Mbit half duplex selected */
  246. #define PHY_STAT_100FDX 0x8000 /* 100 Mbit full duplex selected */
  247. #endif /* CONFIG_USE_MDIO */
  248. #ifdef CONFIG_FEC_PACKETHOOK
  249. int
  250. fec_register_ph(struct net_device *dev, fec_ph_func *rxfun, fec_ph_func *txfun,
  251. __u16 proto, volatile __u32 *regaddr, void *priv)
  252. {
  253. struct fec_enet_private *fep;
  254. int retval = 0;
  255. fep = dev->priv;
  256. if (test_and_set_bit(0, (void*)&fep->ph_lock) != 0) {
  257. /* Someone is messing with the packet hook */
  258. return -EAGAIN;
  259. }
  260. if (fep->ph_rxhandler != NULL || fep->ph_txhandler != NULL) {
  261. retval = -EBUSY;
  262. goto out;
  263. }
  264. fep->ph_rxhandler = rxfun;
  265. fep->ph_txhandler = txfun;
  266. fep->ph_proto = proto;
  267. fep->ph_regaddr = regaddr;
  268. fep->ph_priv = priv;
  269. out:
  270. fep->ph_lock = 0;
  271. return retval;
  272. }
  273. int
  274. fec_unregister_ph(struct net_device *dev)
  275. {
  276. struct fec_enet_private *fep;
  277. int retval = 0;
  278. fep = dev->priv;
  279. if (test_and_set_bit(0, (void*)&fep->ph_lock) != 0) {
  280. /* Someone is messing with the packet hook */
  281. return -EAGAIN;
  282. }
  283. fep->ph_rxhandler = fep->ph_txhandler = NULL;
  284. fep->ph_proto = 0;
  285. fep->ph_regaddr = NULL;
  286. fep->ph_priv = NULL;
  287. fep->ph_lock = 0;
  288. return retval;
  289. }
  290. EXPORT_SYMBOL(fec_register_ph);
  291. EXPORT_SYMBOL(fec_unregister_ph);
  292. #endif /* CONFIG_FEC_PACKETHOOK */
  293. static int
  294. fec_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  295. {
  296. struct fec_enet_private *fep;
  297. volatile fec_t *fecp;
  298. volatile cbd_t *bdp;
  299. fep = dev->priv;
  300. fecp = (volatile fec_t*)dev->base_addr;
  301. if (!fep->link) {
  302. /* Link is down or autonegotiation is in progress. */
  303. return 1;
  304. }
  305. /* Fill in a Tx ring entry */
  306. bdp = fep->cur_tx;
  307. #ifndef final_version
  308. if (bdp->cbd_sc & BD_ENET_TX_READY) {
  309. /* Ooops. All transmit buffers are full. Bail out.
  310. * This should not happen, since dev->tbusy should be set.
  311. */
  312. printk("%s: tx queue full!.\n", dev->name);
  313. return 1;
  314. }
  315. #endif
  316. /* Clear all of the status flags.
  317. */
  318. bdp->cbd_sc &= ~BD_ENET_TX_STATS;
  319. /* Set buffer length and buffer pointer.
  320. */
  321. bdp->cbd_bufaddr = __pa(skb->data);
  322. bdp->cbd_datlen = skb->len;
  323. /* Save skb pointer.
  324. */
  325. fep->tx_skbuff[fep->skb_cur] = skb;
  326. fep->stats.tx_bytes += skb->len;
  327. fep->skb_cur = (fep->skb_cur+1) & TX_RING_MOD_MASK;
  328. /* Push the data cache so the CPM does not get stale memory
  329. * data.
  330. */
  331. flush_dcache_range((unsigned long)skb->data,
  332. (unsigned long)skb->data + skb->len);
  333. /* disable interrupts while triggering transmit */
  334. spin_lock_irq(&fep->lock);
  335. /* Send it on its way. Tell FEC its ready, interrupt when done,
  336. * its the last BD of the frame, and to put the CRC on the end.
  337. */
  338. bdp->cbd_sc |= (BD_ENET_TX_READY | BD_ENET_TX_INTR
  339. | BD_ENET_TX_LAST | BD_ENET_TX_TC);
  340. dev->trans_start = jiffies;
  341. /* Trigger transmission start */
  342. fecp->fec_x_des_active = 0x01000000;
  343. /* If this was the last BD in the ring, start at the beginning again.
  344. */
  345. if (bdp->cbd_sc & BD_ENET_TX_WRAP) {
  346. bdp = fep->tx_bd_base;
  347. } else {
  348. bdp++;
  349. }
  350. if (bdp->cbd_sc & BD_ENET_TX_READY) {
  351. netif_stop_queue(dev);
  352. fep->tx_full = 1;
  353. }
  354. fep->cur_tx = (cbd_t *)bdp;
  355. spin_unlock_irq(&fep->lock);
  356. return 0;
  357. }
  358. static void
  359. fec_timeout(struct net_device *dev)
  360. {
  361. struct fec_enet_private *fep = dev->priv;
  362. printk("%s: transmit timed out.\n", dev->name);
  363. fep->stats.tx_errors++;
  364. #ifndef final_version
  365. {
  366. int i;
  367. cbd_t *bdp;
  368. printk("Ring data dump: cur_tx %lx%s, dirty_tx %lx cur_rx: %lx\n",
  369. (unsigned long)fep->cur_tx, fep->tx_full ? " (full)" : "",
  370. (unsigned long)fep->dirty_tx,
  371. (unsigned long)fep->cur_rx);
  372. bdp = fep->tx_bd_base;
  373. printk(" tx: %u buffers\n", TX_RING_SIZE);
  374. for (i = 0 ; i < TX_RING_SIZE; i++) {
  375. printk(" %08x: %04x %04x %08x\n",
  376. (uint) bdp,
  377. bdp->cbd_sc,
  378. bdp->cbd_datlen,
  379. bdp->cbd_bufaddr);
  380. bdp++;
  381. }
  382. bdp = fep->rx_bd_base;
  383. printk(" rx: %lu buffers\n", RX_RING_SIZE);
  384. for (i = 0 ; i < RX_RING_SIZE; i++) {
  385. printk(" %08x: %04x %04x %08x\n",
  386. (uint) bdp,
  387. bdp->cbd_sc,
  388. bdp->cbd_datlen,
  389. bdp->cbd_bufaddr);
  390. bdp++;
  391. }
  392. }
  393. #endif
  394. if (!fep->tx_full)
  395. netif_wake_queue(dev);
  396. }
  397. /* The interrupt handler.
  398. * This is called from the MPC core interrupt.
  399. */
  400. static irqreturn_t
  401. fec_enet_interrupt(int irq, void *dev_id)
  402. {
  403. struct net_device *dev = dev_id;
  404. volatile fec_t *fecp;
  405. uint int_events;
  406. #ifdef CONFIG_FEC_PACKETHOOK
  407. struct fec_enet_private *fep = dev->priv;
  408. __u32 regval;
  409. if (fep->ph_regaddr) regval = *fep->ph_regaddr;
  410. #endif
  411. fecp = (volatile fec_t*)dev->base_addr;
  412. /* Get the interrupt events that caused us to be here.
  413. */
  414. while ((int_events = fecp->fec_ievent) != 0) {
  415. fecp->fec_ievent = int_events;
  416. if ((int_events & (FEC_ENET_HBERR | FEC_ENET_BABR |
  417. FEC_ENET_BABT | FEC_ENET_EBERR)) != 0) {
  418. printk("FEC ERROR %x\n", int_events);
  419. }
  420. /* Handle receive event in its own function.
  421. */
  422. if (int_events & FEC_ENET_RXF) {
  423. #ifdef CONFIG_FEC_PACKETHOOK
  424. fec_enet_rx(dev, regval);
  425. #else
  426. fec_enet_rx(dev);
  427. #endif
  428. }
  429. /* Transmit OK, or non-fatal error. Update the buffer
  430. descriptors. FEC handles all errors, we just discover
  431. them as part of the transmit process.
  432. */
  433. if (int_events & FEC_ENET_TXF) {
  434. #ifdef CONFIG_FEC_PACKETHOOK
  435. fec_enet_tx(dev, regval);
  436. #else
  437. fec_enet_tx(dev);
  438. #endif
  439. }
  440. if (int_events & FEC_ENET_MII) {
  441. #ifdef CONFIG_USE_MDIO
  442. fec_enet_mii(dev);
  443. #else
  444. printk("%s[%d] %s: unexpected FEC_ENET_MII event\n", __FILE__, __LINE__, __func__);
  445. #endif /* CONFIG_USE_MDIO */
  446. }
  447. }
  448. return IRQ_RETVAL(IRQ_HANDLED);
  449. }
  450. static void
  451. #ifdef CONFIG_FEC_PACKETHOOK
  452. fec_enet_tx(struct net_device *dev, __u32 regval)
  453. #else
  454. fec_enet_tx(struct net_device *dev)
  455. #endif
  456. {
  457. struct fec_enet_private *fep;
  458. volatile cbd_t *bdp;
  459. struct sk_buff *skb;
  460. fep = dev->priv;
  461. /* lock while transmitting */
  462. spin_lock(&fep->lock);
  463. bdp = fep->dirty_tx;
  464. while ((bdp->cbd_sc&BD_ENET_TX_READY) == 0) {
  465. if (bdp == fep->cur_tx && fep->tx_full == 0) break;
  466. skb = fep->tx_skbuff[fep->skb_dirty];
  467. /* Check for errors. */
  468. if (bdp->cbd_sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  469. BD_ENET_TX_RL | BD_ENET_TX_UN |
  470. BD_ENET_TX_CSL)) {
  471. fep->stats.tx_errors++;
  472. if (bdp->cbd_sc & BD_ENET_TX_HB) /* No heartbeat */
  473. fep->stats.tx_heartbeat_errors++;
  474. if (bdp->cbd_sc & BD_ENET_TX_LC) /* Late collision */
  475. fep->stats.tx_window_errors++;
  476. if (bdp->cbd_sc & BD_ENET_TX_RL) /* Retrans limit */
  477. fep->stats.tx_aborted_errors++;
  478. if (bdp->cbd_sc & BD_ENET_TX_UN) /* Underrun */
  479. fep->stats.tx_fifo_errors++;
  480. if (bdp->cbd_sc & BD_ENET_TX_CSL) /* Carrier lost */
  481. fep->stats.tx_carrier_errors++;
  482. } else {
  483. #ifdef CONFIG_FEC_PACKETHOOK
  484. /* Packet hook ... */
  485. if (fep->ph_txhandler &&
  486. ((struct ethhdr *)skb->data)->h_proto
  487. == fep->ph_proto) {
  488. fep->ph_txhandler((__u8*)skb->data, skb->len,
  489. regval, fep->ph_priv);
  490. }
  491. #endif
  492. fep->stats.tx_packets++;
  493. }
  494. #ifndef final_version
  495. if (bdp->cbd_sc & BD_ENET_TX_READY)
  496. printk("HEY! Enet xmit interrupt and TX_READY.\n");
  497. #endif
  498. /* Deferred means some collisions occurred during transmit,
  499. * but we eventually sent the packet OK.
  500. */
  501. if (bdp->cbd_sc & BD_ENET_TX_DEF)
  502. fep->stats.collisions++;
  503. /* Free the sk buffer associated with this last transmit.
  504. */
  505. #if 0
  506. printk("TXI: %x %x %x\n", bdp, skb, fep->skb_dirty);
  507. #endif
  508. dev_kfree_skb_irq (skb/*, FREE_WRITE*/);
  509. fep->tx_skbuff[fep->skb_dirty] = NULL;
  510. fep->skb_dirty = (fep->skb_dirty + 1) & TX_RING_MOD_MASK;
  511. /* Update pointer to next buffer descriptor to be transmitted.
  512. */
  513. if (bdp->cbd_sc & BD_ENET_TX_WRAP)
  514. bdp = fep->tx_bd_base;
  515. else
  516. bdp++;
  517. /* Since we have freed up a buffer, the ring is no longer
  518. * full.
  519. */
  520. if (fep->tx_full) {
  521. fep->tx_full = 0;
  522. if (netif_queue_stopped(dev))
  523. netif_wake_queue(dev);
  524. }
  525. #ifdef CONFIG_FEC_PACKETHOOK
  526. /* Re-read register. Not exactly guaranteed to be correct,
  527. but... */
  528. if (fep->ph_regaddr) regval = *fep->ph_regaddr;
  529. #endif
  530. }
  531. fep->dirty_tx = (cbd_t *)bdp;
  532. spin_unlock(&fep->lock);
  533. }
  534. /* During a receive, the cur_rx points to the current incoming buffer.
  535. * When we update through the ring, if the next incoming buffer has
  536. * not been given to the system, we just set the empty indicator,
  537. * effectively tossing the packet.
  538. */
  539. static void
  540. #ifdef CONFIG_FEC_PACKETHOOK
  541. fec_enet_rx(struct net_device *dev, __u32 regval)
  542. #else
  543. fec_enet_rx(struct net_device *dev)
  544. #endif
  545. {
  546. struct fec_enet_private *fep;
  547. volatile fec_t *fecp;
  548. volatile cbd_t *bdp;
  549. struct sk_buff *skb;
  550. ushort pkt_len;
  551. __u8 *data;
  552. fep = dev->priv;
  553. fecp = (volatile fec_t*)dev->base_addr;
  554. /* First, grab all of the stats for the incoming packet.
  555. * These get messed up if we get called due to a busy condition.
  556. */
  557. bdp = fep->cur_rx;
  558. while (!(bdp->cbd_sc & BD_ENET_RX_EMPTY)) {
  559. #ifndef final_version
  560. /* Since we have allocated space to hold a complete frame,
  561. * the last indicator should be set.
  562. */
  563. if ((bdp->cbd_sc & BD_ENET_RX_LAST) == 0)
  564. printk("FEC ENET: rcv is not +last\n");
  565. #endif
  566. /* Check for errors. */
  567. if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  568. BD_ENET_RX_CR | BD_ENET_RX_OV)) {
  569. fep->stats.rx_errors++;
  570. if (bdp->cbd_sc & (BD_ENET_RX_LG | BD_ENET_RX_SH)) {
  571. /* Frame too long or too short. */
  572. fep->stats.rx_length_errors++;
  573. }
  574. if (bdp->cbd_sc & BD_ENET_RX_NO) /* Frame alignment */
  575. fep->stats.rx_frame_errors++;
  576. if (bdp->cbd_sc & BD_ENET_RX_CR) /* CRC Error */
  577. fep->stats.rx_crc_errors++;
  578. if (bdp->cbd_sc & BD_ENET_RX_OV) /* FIFO overrun */
  579. fep->stats.rx_crc_errors++;
  580. }
  581. /* Report late collisions as a frame error.
  582. * On this error, the BD is closed, but we don't know what we
  583. * have in the buffer. So, just drop this frame on the floor.
  584. */
  585. if (bdp->cbd_sc & BD_ENET_RX_CL) {
  586. fep->stats.rx_errors++;
  587. fep->stats.rx_frame_errors++;
  588. goto rx_processing_done;
  589. }
  590. /* Process the incoming frame.
  591. */
  592. fep->stats.rx_packets++;
  593. pkt_len = bdp->cbd_datlen;
  594. fep->stats.rx_bytes += pkt_len;
  595. data = fep->rx_vaddr[bdp - fep->rx_bd_base];
  596. #ifdef CONFIG_FEC_PACKETHOOK
  597. /* Packet hook ... */
  598. if (fep->ph_rxhandler) {
  599. if (((struct ethhdr *)data)->h_proto == fep->ph_proto) {
  600. switch (fep->ph_rxhandler(data, pkt_len, regval,
  601. fep->ph_priv)) {
  602. case 1:
  603. goto rx_processing_done;
  604. break;
  605. case 0:
  606. break;
  607. default:
  608. fep->stats.rx_errors++;
  609. goto rx_processing_done;
  610. }
  611. }
  612. }
  613. /* If it wasn't filtered - copy it to an sk buffer. */
  614. #endif
  615. /* This does 16 byte alignment, exactly what we need.
  616. * The packet length includes FCS, but we don't want to
  617. * include that when passing upstream as it messes up
  618. * bridging applications.
  619. */
  620. skb = dev_alloc_skb(pkt_len-4);
  621. if (skb == NULL) {
  622. printk("%s: Memory squeeze, dropping packet.\n", dev->name);
  623. fep->stats.rx_dropped++;
  624. } else {
  625. skb_put(skb,pkt_len-4); /* Make room */
  626. skb_copy_to_linear_data(skb, data, pkt_len-4);
  627. skb->protocol=eth_type_trans(skb,dev);
  628. netif_rx(skb);
  629. }
  630. rx_processing_done:
  631. /* Clear the status flags for this buffer.
  632. */
  633. bdp->cbd_sc &= ~BD_ENET_RX_STATS;
  634. /* Mark the buffer empty.
  635. */
  636. bdp->cbd_sc |= BD_ENET_RX_EMPTY;
  637. /* Update BD pointer to next entry.
  638. */
  639. if (bdp->cbd_sc & BD_ENET_RX_WRAP)
  640. bdp = fep->rx_bd_base;
  641. else
  642. bdp++;
  643. #if 1
  644. /* Doing this here will keep the FEC running while we process
  645. * incoming frames. On a heavily loaded network, we should be
  646. * able to keep up at the expense of system resources.
  647. */
  648. fecp->fec_r_des_active = 0x01000000;
  649. #endif
  650. #ifdef CONFIG_FEC_PACKETHOOK
  651. /* Re-read register. Not exactly guaranteed to be correct,
  652. but... */
  653. if (fep->ph_regaddr) regval = *fep->ph_regaddr;
  654. #endif
  655. } /* while (!(bdp->cbd_sc & BD_ENET_RX_EMPTY)) */
  656. fep->cur_rx = (cbd_t *)bdp;
  657. #if 0
  658. /* Doing this here will allow us to process all frames in the
  659. * ring before the FEC is allowed to put more there. On a heavily
  660. * loaded network, some frames may be lost. Unfortunately, this
  661. * increases the interrupt overhead since we can potentially work
  662. * our way back to the interrupt return only to come right back
  663. * here.
  664. */
  665. fecp->fec_r_des_active = 0x01000000;
  666. #endif
  667. }
  668. #ifdef CONFIG_USE_MDIO
  669. static void
  670. fec_enet_mii(struct net_device *dev)
  671. {
  672. struct fec_enet_private *fep;
  673. volatile fec_t *ep;
  674. mii_list_t *mip;
  675. uint mii_reg;
  676. fep = (struct fec_enet_private *)dev->priv;
  677. ep = &(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec);
  678. mii_reg = ep->fec_mii_data;
  679. if ((mip = mii_head) == NULL) {
  680. printk("MII and no head!\n");
  681. return;
  682. }
  683. if (mip->mii_func != NULL)
  684. (*(mip->mii_func))(mii_reg, dev);
  685. mii_head = mip->mii_next;
  686. mip->mii_next = mii_free;
  687. mii_free = mip;
  688. if ((mip = mii_head) != NULL) {
  689. ep->fec_mii_data = mip->mii_regval;
  690. }
  691. }
  692. static int
  693. mii_queue(struct net_device *dev, int regval, void (*func)(uint, struct net_device *))
  694. {
  695. struct fec_enet_private *fep;
  696. unsigned long flags;
  697. mii_list_t *mip;
  698. int retval;
  699. /* Add PHY address to register command.
  700. */
  701. fep = dev->priv;
  702. regval |= fep->phy_addr << 23;
  703. retval = 0;
  704. /* lock while modifying mii_list */
  705. spin_lock_irqsave(&fep->lock, flags);
  706. if ((mip = mii_free) != NULL) {
  707. mii_free = mip->mii_next;
  708. mip->mii_regval = regval;
  709. mip->mii_func = func;
  710. mip->mii_next = NULL;
  711. if (mii_head) {
  712. mii_tail->mii_next = mip;
  713. mii_tail = mip;
  714. } else {
  715. mii_head = mii_tail = mip;
  716. (&(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec))->fec_mii_data = regval;
  717. }
  718. } else {
  719. retval = 1;
  720. }
  721. spin_unlock_irqrestore(&fep->lock, flags);
  722. return(retval);
  723. }
  724. static void mii_do_cmd(struct net_device *dev, const phy_cmd_t *c)
  725. {
  726. int k;
  727. if(!c)
  728. return;
  729. for(k = 0; (c+k)->mii_data != mk_mii_end; k++)
  730. mii_queue(dev, (c+k)->mii_data, (c+k)->funct);
  731. }
  732. static void mii_parse_sr(uint mii_reg, struct net_device *dev)
  733. {
  734. struct fec_enet_private *fep = dev->priv;
  735. volatile uint *s = &(fep->phy_status);
  736. *s &= ~(PHY_STAT_LINK | PHY_STAT_FAULT | PHY_STAT_ANC);
  737. if (mii_reg & 0x0004)
  738. *s |= PHY_STAT_LINK;
  739. if (mii_reg & 0x0010)
  740. *s |= PHY_STAT_FAULT;
  741. if (mii_reg & 0x0020)
  742. *s |= PHY_STAT_ANC;
  743. fep->link = (*s & PHY_STAT_LINK) ? 1 : 0;
  744. }
  745. static void mii_parse_cr(uint mii_reg, struct net_device *dev)
  746. {
  747. struct fec_enet_private *fep = dev->priv;
  748. volatile uint *s = &(fep->phy_status);
  749. *s &= ~(PHY_CONF_ANE | PHY_CONF_LOOP);
  750. if (mii_reg & 0x1000)
  751. *s |= PHY_CONF_ANE;
  752. if (mii_reg & 0x4000)
  753. *s |= PHY_CONF_LOOP;
  754. }
  755. static void mii_parse_anar(uint mii_reg, struct net_device *dev)
  756. {
  757. struct fec_enet_private *fep = dev->priv;
  758. volatile uint *s = &(fep->phy_status);
  759. *s &= ~(PHY_CONF_SPMASK);
  760. if (mii_reg & 0x0020)
  761. *s |= PHY_CONF_10HDX;
  762. if (mii_reg & 0x0040)
  763. *s |= PHY_CONF_10FDX;
  764. if (mii_reg & 0x0080)
  765. *s |= PHY_CONF_100HDX;
  766. if (mii_reg & 0x00100)
  767. *s |= PHY_CONF_100FDX;
  768. }
  769. #if 0
  770. static void mii_disp_reg(uint mii_reg, struct net_device *dev)
  771. {
  772. printk("reg %u = 0x%04x\n", (mii_reg >> 18) & 0x1f, mii_reg & 0xffff);
  773. }
  774. #endif
  775. /* ------------------------------------------------------------------------- */
  776. /* The Level one LXT970 is used by many boards */
  777. #ifdef CONFIG_FEC_LXT970
  778. #define MII_LXT970_MIRROR 16 /* Mirror register */
  779. #define MII_LXT970_IER 17 /* Interrupt Enable Register */
  780. #define MII_LXT970_ISR 18 /* Interrupt Status Register */
  781. #define MII_LXT970_CONFIG 19 /* Configuration Register */
  782. #define MII_LXT970_CSR 20 /* Chip Status Register */
  783. static void mii_parse_lxt970_csr(uint mii_reg, struct net_device *dev)
  784. {
  785. struct fec_enet_private *fep = dev->priv;
  786. volatile uint *s = &(fep->phy_status);
  787. *s &= ~(PHY_STAT_SPMASK);
  788. if (mii_reg & 0x0800) {
  789. if (mii_reg & 0x1000)
  790. *s |= PHY_STAT_100FDX;
  791. else
  792. *s |= PHY_STAT_100HDX;
  793. }
  794. else {
  795. if (mii_reg & 0x1000)
  796. *s |= PHY_STAT_10FDX;
  797. else
  798. *s |= PHY_STAT_10HDX;
  799. }
  800. }
  801. static phy_info_t phy_info_lxt970 = {
  802. 0x07810000,
  803. "LXT970",
  804. (const phy_cmd_t []) { /* config */
  805. #if 0
  806. // { mk_mii_write(MII_REG_ANAR, 0x0021), NULL },
  807. /* Set default operation of 100-TX....for some reason
  808. * some of these bits are set on power up, which is wrong.
  809. */
  810. { mk_mii_write(MII_LXT970_CONFIG, 0), NULL },
  811. #endif
  812. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  813. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  814. { mk_mii_end, }
  815. },
  816. (const phy_cmd_t []) { /* startup - enable interrupts */
  817. { mk_mii_write(MII_LXT970_IER, 0x0002), NULL },
  818. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  819. { mk_mii_end, }
  820. },
  821. (const phy_cmd_t []) { /* ack_int */
  822. /* read SR and ISR to acknowledge */
  823. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  824. { mk_mii_read(MII_LXT970_ISR), NULL },
  825. /* find out the current status */
  826. { mk_mii_read(MII_LXT970_CSR), mii_parse_lxt970_csr },
  827. { mk_mii_end, }
  828. },
  829. (const phy_cmd_t []) { /* shutdown - disable interrupts */
  830. { mk_mii_write(MII_LXT970_IER, 0x0000), NULL },
  831. { mk_mii_end, }
  832. },
  833. };
  834. #endif /* CONFIG_FEC_LXT970 */
  835. /* ------------------------------------------------------------------------- */
  836. /* The Level one LXT971 is used on some of my custom boards */
  837. #ifdef CONFIG_FEC_LXT971
  838. /* register definitions for the 971 */
  839. #define MII_LXT971_PCR 16 /* Port Control Register */
  840. #define MII_LXT971_SR2 17 /* Status Register 2 */
  841. #define MII_LXT971_IER 18 /* Interrupt Enable Register */
  842. #define MII_LXT971_ISR 19 /* Interrupt Status Register */
  843. #define MII_LXT971_LCR 20 /* LED Control Register */
  844. #define MII_LXT971_TCR 30 /* Transmit Control Register */
  845. /*
  846. * I had some nice ideas of running the MDIO faster...
  847. * The 971 should support 8MHz and I tried it, but things acted really
  848. * weird, so 2.5 MHz ought to be enough for anyone...
  849. */
  850. static void mii_parse_lxt971_sr2(uint mii_reg, struct net_device *dev)
  851. {
  852. struct fec_enet_private *fep = dev->priv;
  853. volatile uint *s = &(fep->phy_status);
  854. *s &= ~(PHY_STAT_SPMASK);
  855. if (mii_reg & 0x4000) {
  856. if (mii_reg & 0x0200)
  857. *s |= PHY_STAT_100FDX;
  858. else
  859. *s |= PHY_STAT_100HDX;
  860. }
  861. else {
  862. if (mii_reg & 0x0200)
  863. *s |= PHY_STAT_10FDX;
  864. else
  865. *s |= PHY_STAT_10HDX;
  866. }
  867. if (mii_reg & 0x0008)
  868. *s |= PHY_STAT_FAULT;
  869. }
  870. static phy_info_t phy_info_lxt971 = {
  871. 0x0001378e,
  872. "LXT971",
  873. (const phy_cmd_t []) { /* config */
  874. // { mk_mii_write(MII_REG_ANAR, 0x021), NULL }, /* 10 Mbps, HD */
  875. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  876. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  877. { mk_mii_end, }
  878. },
  879. (const phy_cmd_t []) { /* startup - enable interrupts */
  880. { mk_mii_write(MII_LXT971_IER, 0x00f2), NULL },
  881. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  882. /* Somehow does the 971 tell me that the link is down
  883. * the first read after power-up.
  884. * read here to get a valid value in ack_int */
  885. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  886. { mk_mii_end, }
  887. },
  888. (const phy_cmd_t []) { /* ack_int */
  889. /* find out the current status */
  890. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  891. { mk_mii_read(MII_LXT971_SR2), mii_parse_lxt971_sr2 },
  892. /* we only need to read ISR to acknowledge */
  893. { mk_mii_read(MII_LXT971_ISR), NULL },
  894. { mk_mii_end, }
  895. },
  896. (const phy_cmd_t []) { /* shutdown - disable interrupts */
  897. { mk_mii_write(MII_LXT971_IER, 0x0000), NULL },
  898. { mk_mii_end, }
  899. },
  900. };
  901. #endif /* CONFIG_FEC_LXT970 */
  902. /* ------------------------------------------------------------------------- */
  903. /* The Quality Semiconductor QS6612 is used on the RPX CLLF */
  904. #ifdef CONFIG_FEC_QS6612
  905. /* register definitions */
  906. #define MII_QS6612_MCR 17 /* Mode Control Register */
  907. #define MII_QS6612_FTR 27 /* Factory Test Register */
  908. #define MII_QS6612_MCO 28 /* Misc. Control Register */
  909. #define MII_QS6612_ISR 29 /* Interrupt Source Register */
  910. #define MII_QS6612_IMR 30 /* Interrupt Mask Register */
  911. #define MII_QS6612_PCR 31 /* 100BaseTx PHY Control Reg. */
  912. static void mii_parse_qs6612_pcr(uint mii_reg, struct net_device *dev)
  913. {
  914. struct fec_enet_private *fep = dev->priv;
  915. volatile uint *s = &(fep->phy_status);
  916. *s &= ~(PHY_STAT_SPMASK);
  917. switch((mii_reg >> 2) & 7) {
  918. case 1: *s |= PHY_STAT_10HDX; break;
  919. case 2: *s |= PHY_STAT_100HDX; break;
  920. case 5: *s |= PHY_STAT_10FDX; break;
  921. case 6: *s |= PHY_STAT_100FDX; break;
  922. }
  923. }
  924. static phy_info_t phy_info_qs6612 = {
  925. 0x00181440,
  926. "QS6612",
  927. (const phy_cmd_t []) { /* config */
  928. // { mk_mii_write(MII_REG_ANAR, 0x061), NULL }, /* 10 Mbps */
  929. /* The PHY powers up isolated on the RPX,
  930. * so send a command to allow operation.
  931. */
  932. { mk_mii_write(MII_QS6612_PCR, 0x0dc0), NULL },
  933. /* parse cr and anar to get some info */
  934. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  935. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  936. { mk_mii_end, }
  937. },
  938. (const phy_cmd_t []) { /* startup - enable interrupts */
  939. { mk_mii_write(MII_QS6612_IMR, 0x003a), NULL },
  940. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  941. { mk_mii_end, }
  942. },
  943. (const phy_cmd_t []) { /* ack_int */
  944. /* we need to read ISR, SR and ANER to acknowledge */
  945. { mk_mii_read(MII_QS6612_ISR), NULL },
  946. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  947. { mk_mii_read(MII_REG_ANER), NULL },
  948. /* read pcr to get info */
  949. { mk_mii_read(MII_QS6612_PCR), mii_parse_qs6612_pcr },
  950. { mk_mii_end, }
  951. },
  952. (const phy_cmd_t []) { /* shutdown - disable interrupts */
  953. { mk_mii_write(MII_QS6612_IMR, 0x0000), NULL },
  954. { mk_mii_end, }
  955. },
  956. };
  957. #endif /* CONFIG_FEC_QS6612 */
  958. /* ------------------------------------------------------------------------- */
  959. /* The Advanced Micro Devices AM79C874 is used on the ICU862 */
  960. #ifdef CONFIG_FEC_AM79C874
  961. /* register definitions for the 79C874 */
  962. #define MII_AM79C874_MFR 16 /* Miscellaneous Features Register */
  963. #define MII_AM79C874_ICSR 17 /* Interrupt Control/Status Register */
  964. #define MII_AM79C874_DR 18 /* Diagnostic Register */
  965. #define MII_AM79C874_PMLR 19 /* Power Management & Loopback Register */
  966. #define MII_AM79C874_MCR 21 /* Mode Control Register */
  967. #define MII_AM79C874_DC 23 /* Disconnect Counter */
  968. #define MII_AM79C874_REC 24 /* Receiver Error Counter */
  969. static void mii_parse_amd79c874_dr(uint mii_reg, struct net_device *dev, uint data)
  970. {
  971. volatile struct fec_enet_private *fep = dev->priv;
  972. uint s = fep->phy_status;
  973. s &= ~(PHY_STAT_SPMASK);
  974. /* Register 18: Bit 10 is data rate, 11 is Duplex */
  975. switch ((mii_reg >> 10) & 3) {
  976. case 0: s |= PHY_STAT_10HDX; break;
  977. case 1: s |= PHY_STAT_100HDX; break;
  978. case 2: s |= PHY_STAT_10FDX; break;
  979. case 3: s |= PHY_STAT_100FDX; break;
  980. }
  981. fep->phy_status = s;
  982. }
  983. static phy_info_t phy_info_amd79c874 = {
  984. 0x00022561,
  985. "AM79C874",
  986. (const phy_cmd_t []) { /* config */
  987. // { mk_mii_write(MII_REG_ANAR, 0x021), NULL }, /* 10 Mbps, HD */
  988. { mk_mii_read(MII_REG_CR), mii_parse_cr },
  989. { mk_mii_read(MII_REG_ANAR), mii_parse_anar },
  990. { mk_mii_end, }
  991. },
  992. (const phy_cmd_t []) { /* startup - enable interrupts */
  993. { mk_mii_write(MII_AM79C874_ICSR, 0xff00), NULL },
  994. { mk_mii_write(MII_REG_CR, 0x1200), NULL }, /* autonegotiate */
  995. { mk_mii_end, }
  996. },
  997. (const phy_cmd_t []) { /* ack_int */
  998. /* find out the current status */
  999. { mk_mii_read(MII_REG_SR), mii_parse_sr },
  1000. { mk_mii_read(MII_AM79C874_DR), mii_parse_amd79c874_dr },
  1001. /* we only need to read ICSR to acknowledge */
  1002. { mk_mii_read(MII_AM79C874_ICSR), NULL },
  1003. { mk_mii_end, }
  1004. },
  1005. (const phy_cmd_t []) { /* shutdown - disable interrupts */
  1006. { mk_mii_write(MII_AM79C874_ICSR, 0x0000), NULL },
  1007. { mk_mii_end, }
  1008. },
  1009. };
  1010. #endif /* CONFIG_FEC_AM79C874 */
  1011. static phy_info_t *phy_info[] = {
  1012. #ifdef CONFIG_FEC_LXT970
  1013. &phy_info_lxt970,
  1014. #endif /* CONFIG_FEC_LXT970 */
  1015. #ifdef CONFIG_FEC_LXT971
  1016. &phy_info_lxt971,
  1017. #endif /* CONFIG_FEC_LXT971 */
  1018. #ifdef CONFIG_FEC_QS6612
  1019. &phy_info_qs6612,
  1020. #endif /* CONFIG_FEC_QS6612 */
  1021. #ifdef CONFIG_FEC_AM79C874
  1022. &phy_info_amd79c874,
  1023. #endif /* CONFIG_FEC_AM79C874 */
  1024. NULL
  1025. };
  1026. static void mii_display_status(struct net_device *dev)
  1027. {
  1028. struct fec_enet_private *fep = dev->priv;
  1029. volatile uint *s = &(fep->phy_status);
  1030. if (!fep->link && !fep->old_link) {
  1031. /* Link is still down - don't print anything */
  1032. return;
  1033. }
  1034. printk("%s: status: ", dev->name);
  1035. if (!fep->link) {
  1036. printk("link down");
  1037. } else {
  1038. printk("link up");
  1039. switch(*s & PHY_STAT_SPMASK) {
  1040. case PHY_STAT_100FDX: printk(", 100 Mbps Full Duplex"); break;
  1041. case PHY_STAT_100HDX: printk(", 100 Mbps Half Duplex"); break;
  1042. case PHY_STAT_10FDX: printk(", 10 Mbps Full Duplex"); break;
  1043. case PHY_STAT_10HDX: printk(", 10 Mbps Half Duplex"); break;
  1044. default:
  1045. printk(", Unknown speed/duplex");
  1046. }
  1047. if (*s & PHY_STAT_ANC)
  1048. printk(", auto-negotiation complete");
  1049. }
  1050. if (*s & PHY_STAT_FAULT)
  1051. printk(", remote fault");
  1052. printk(".\n");
  1053. }
  1054. static void mii_display_config(struct work_struct *work)
  1055. {
  1056. struct fec_enet_private *fep =
  1057. container_of(work, struct fec_enet_private, phy_task);
  1058. struct net_device *dev = fep->dev;
  1059. volatile uint *s = &(fep->phy_status);
  1060. printk("%s: config: auto-negotiation ", dev->name);
  1061. if (*s & PHY_CONF_ANE)
  1062. printk("on");
  1063. else
  1064. printk("off");
  1065. if (*s & PHY_CONF_100FDX)
  1066. printk(", 100FDX");
  1067. if (*s & PHY_CONF_100HDX)
  1068. printk(", 100HDX");
  1069. if (*s & PHY_CONF_10FDX)
  1070. printk(", 10FDX");
  1071. if (*s & PHY_CONF_10HDX)
  1072. printk(", 10HDX");
  1073. if (!(*s & PHY_CONF_SPMASK))
  1074. printk(", No speed/duplex selected?");
  1075. if (*s & PHY_CONF_LOOP)
  1076. printk(", loopback enabled");
  1077. printk(".\n");
  1078. fep->sequence_done = 1;
  1079. }
  1080. static void mii_relink(struct work_struct *work)
  1081. {
  1082. struct fec_enet_private *fep =
  1083. container_of(work, struct fec_enet_private, phy_task);
  1084. struct net_device *dev = fep->dev;
  1085. int duplex;
  1086. fep->link = (fep->phy_status & PHY_STAT_LINK) ? 1 : 0;
  1087. mii_display_status(dev);
  1088. fep->old_link = fep->link;
  1089. if (fep->link) {
  1090. duplex = 0;
  1091. if (fep->phy_status
  1092. & (PHY_STAT_100FDX | PHY_STAT_10FDX))
  1093. duplex = 1;
  1094. fec_restart(dev, duplex);
  1095. }
  1096. else
  1097. fec_stop(dev);
  1098. #if 0
  1099. enable_irq(fep->mii_irq);
  1100. #endif
  1101. }
  1102. static void mii_queue_relink(uint mii_reg, struct net_device *dev)
  1103. {
  1104. struct fec_enet_private *fep = dev->priv;
  1105. fep->dev = dev;
  1106. INIT_WORK(&fep->phy_task, mii_relink);
  1107. schedule_work(&fep->phy_task);
  1108. }
  1109. static void mii_queue_config(uint mii_reg, struct net_device *dev)
  1110. {
  1111. struct fec_enet_private *fep = dev->priv;
  1112. fep->dev = dev;
  1113. INIT_WORK(&fep->phy_task, mii_display_config);
  1114. schedule_work(&fep->phy_task);
  1115. }
  1116. phy_cmd_t phy_cmd_relink[] = { { mk_mii_read(MII_REG_CR), mii_queue_relink },
  1117. { mk_mii_end, } };
  1118. phy_cmd_t phy_cmd_config[] = { { mk_mii_read(MII_REG_CR), mii_queue_config },
  1119. { mk_mii_end, } };
  1120. /* Read remainder of PHY ID.
  1121. */
  1122. static void
  1123. mii_discover_phy3(uint mii_reg, struct net_device *dev)
  1124. {
  1125. struct fec_enet_private *fep;
  1126. int i;
  1127. fep = dev->priv;
  1128. fep->phy_id |= (mii_reg & 0xffff);
  1129. for(i = 0; phy_info[i]; i++)
  1130. if(phy_info[i]->id == (fep->phy_id >> 4))
  1131. break;
  1132. if(!phy_info[i])
  1133. panic("%s: PHY id 0x%08x is not supported!\n",
  1134. dev->name, fep->phy_id);
  1135. fep->phy = phy_info[i];
  1136. fep->phy_id_done = 1;
  1137. printk("%s: Phy @ 0x%x, type %s (0x%08x)\n",
  1138. dev->name, fep->phy_addr, fep->phy->name, fep->phy_id);
  1139. }
  1140. /* Scan all of the MII PHY addresses looking for someone to respond
  1141. * with a valid ID. This usually happens quickly.
  1142. */
  1143. static void
  1144. mii_discover_phy(uint mii_reg, struct net_device *dev)
  1145. {
  1146. struct fec_enet_private *fep;
  1147. uint phytype;
  1148. fep = dev->priv;
  1149. if ((phytype = (mii_reg & 0xffff)) != 0xffff) {
  1150. /* Got first part of ID, now get remainder.
  1151. */
  1152. fep->phy_id = phytype << 16;
  1153. mii_queue(dev, mk_mii_read(MII_REG_PHYIR2), mii_discover_phy3);
  1154. } else {
  1155. fep->phy_addr++;
  1156. if (fep->phy_addr < 32) {
  1157. mii_queue(dev, mk_mii_read(MII_REG_PHYIR1),
  1158. mii_discover_phy);
  1159. } else {
  1160. printk("fec: No PHY device found.\n");
  1161. }
  1162. }
  1163. }
  1164. #endif /* CONFIG_USE_MDIO */
  1165. /* This interrupt occurs when the PHY detects a link change.
  1166. */
  1167. static
  1168. #ifdef CONFIG_RPXCLASSIC
  1169. void mii_link_interrupt(void *dev_id)
  1170. #else
  1171. irqreturn_t mii_link_interrupt(int irq, void * dev_id)
  1172. #endif
  1173. {
  1174. #ifdef CONFIG_USE_MDIO
  1175. struct net_device *dev = dev_id;
  1176. struct fec_enet_private *fep = dev->priv;
  1177. volatile immap_t *immap = (immap_t *)IMAP_ADDR;
  1178. volatile fec_t *fecp = &(immap->im_cpm.cp_fec);
  1179. unsigned int ecntrl = fecp->fec_ecntrl;
  1180. /* We need the FEC enabled to access the MII
  1181. */
  1182. if ((ecntrl & FEC_ECNTRL_ETHER_EN) == 0) {
  1183. fecp->fec_ecntrl |= FEC_ECNTRL_ETHER_EN;
  1184. }
  1185. #endif /* CONFIG_USE_MDIO */
  1186. #if 0
  1187. disable_irq(fep->mii_irq); /* disable now, enable later */
  1188. #endif
  1189. #ifdef CONFIG_USE_MDIO
  1190. mii_do_cmd(dev, fep->phy->ack_int);
  1191. mii_do_cmd(dev, phy_cmd_relink); /* restart and display status */
  1192. if ((ecntrl & FEC_ECNTRL_ETHER_EN) == 0) {
  1193. fecp->fec_ecntrl = ecntrl; /* restore old settings */
  1194. }
  1195. #else
  1196. printk("%s[%d] %s: unexpected Link interrupt\n", __FILE__, __LINE__, __func__);
  1197. #endif /* CONFIG_USE_MDIO */
  1198. #ifndef CONFIG_RPXCLASSIC
  1199. return IRQ_RETVAL(IRQ_HANDLED);
  1200. #endif /* CONFIG_RPXCLASSIC */
  1201. }
  1202. static int
  1203. fec_enet_open(struct net_device *dev)
  1204. {
  1205. struct fec_enet_private *fep = dev->priv;
  1206. /* I should reset the ring buffers here, but I don't yet know
  1207. * a simple way to do that.
  1208. */
  1209. #ifdef CONFIG_USE_MDIO
  1210. fep->sequence_done = 0;
  1211. fep->link = 0;
  1212. if (fep->phy) {
  1213. mii_do_cmd(dev, fep->phy->ack_int);
  1214. mii_do_cmd(dev, fep->phy->config);
  1215. mii_do_cmd(dev, phy_cmd_config); /* display configuration */
  1216. while(!fep->sequence_done)
  1217. schedule();
  1218. mii_do_cmd(dev, fep->phy->startup);
  1219. netif_start_queue(dev);
  1220. return 0; /* Success */
  1221. }
  1222. return -ENODEV; /* No PHY we understand */
  1223. #else
  1224. fep->link = 1;
  1225. netif_start_queue(dev);
  1226. return 0; /* Success */
  1227. #endif /* CONFIG_USE_MDIO */
  1228. }
  1229. static int
  1230. fec_enet_close(struct net_device *dev)
  1231. {
  1232. /* Don't know what to do yet.
  1233. */
  1234. netif_stop_queue(dev);
  1235. fec_stop(dev);
  1236. return 0;
  1237. }
  1238. static struct net_device_stats *fec_enet_get_stats(struct net_device *dev)
  1239. {
  1240. struct fec_enet_private *fep = (struct fec_enet_private *)dev->priv;
  1241. return &fep->stats;
  1242. }
  1243. /* Set or clear the multicast filter for this adaptor.
  1244. * Skeleton taken from sunlance driver.
  1245. * The CPM Ethernet implementation allows Multicast as well as individual
  1246. * MAC address filtering. Some of the drivers check to make sure it is
  1247. * a group multicast address, and discard those that are not. I guess I
  1248. * will do the same for now, but just remove the test if you want
  1249. * individual filtering as well (do the upper net layers want or support
  1250. * this kind of feature?).
  1251. */
  1252. static void set_multicast_list(struct net_device *dev)
  1253. {
  1254. struct fec_enet_private *fep;
  1255. volatile fec_t *ep;
  1256. fep = (struct fec_enet_private *)dev->priv;
  1257. ep = &(((immap_t *)IMAP_ADDR)->im_cpm.cp_fec);
  1258. if (dev->flags&IFF_PROMISC) {
  1259. /* Log any net taps. */
  1260. printk("%s: Promiscuous mode enabled.\n", dev->name);
  1261. ep->fec_r_cntrl |= FEC_RCNTRL_PROM;
  1262. } else {
  1263. ep->fec_r_cntrl &= ~FEC_RCNTRL_PROM;
  1264. if (dev->flags & IFF_ALLMULTI) {
  1265. /* Catch all multicast addresses, so set the
  1266. * filter to all 1's.
  1267. */
  1268. ep->fec_hash_table_high = 0xffffffff;
  1269. ep->fec_hash_table_low = 0xffffffff;
  1270. }
  1271. #if 0
  1272. else {
  1273. /* Clear filter and add the addresses in the list.
  1274. */
  1275. ep->sen_gaddr1 = 0;
  1276. ep->sen_gaddr2 = 0;
  1277. ep->sen_gaddr3 = 0;
  1278. ep->sen_gaddr4 = 0;
  1279. dmi = dev->mc_list;
  1280. for (i=0; i<dev->mc_count; i++) {
  1281. /* Only support group multicast for now.
  1282. */
  1283. if (!(dmi->dmi_addr[0] & 1))
  1284. continue;
  1285. /* The address in dmi_addr is LSB first,
  1286. * and taddr is MSB first. We have to
  1287. * copy bytes MSB first from dmi_addr.
  1288. */
  1289. mcptr = (u_char *)dmi->dmi_addr + 5;
  1290. tdptr = (u_char *)&ep->sen_taddrh;
  1291. for (j=0; j<6; j++)
  1292. *tdptr++ = *mcptr--;
  1293. /* Ask CPM to run CRC and set bit in
  1294. * filter mask.
  1295. */
  1296. cpmp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SCC1, CPM_CR_SET_GADDR) | CPM_CR_FLG;
  1297. /* this delay is necessary here -- Cort */
  1298. udelay(10);
  1299. while (cpmp->cp_cpcr & CPM_CR_FLG);
  1300. }
  1301. }
  1302. #endif
  1303. }
  1304. }
  1305. /* Initialize the FEC Ethernet on 860T.
  1306. */
  1307. static int __init fec_enet_init(void)
  1308. {
  1309. struct net_device *dev;
  1310. struct fec_enet_private *fep;
  1311. int i, j, k, err;
  1312. unsigned char *eap, *iap, *ba;
  1313. dma_addr_t mem_addr;
  1314. volatile cbd_t *bdp;
  1315. cbd_t *cbd_base;
  1316. volatile immap_t *immap;
  1317. volatile fec_t *fecp;
  1318. bd_t *bd;
  1319. #ifdef CONFIG_SCC_ENET
  1320. unsigned char tmpaddr[6];
  1321. #endif
  1322. immap = (immap_t *)IMAP_ADDR; /* pointer to internal registers */
  1323. bd = (bd_t *)__res;
  1324. dev = alloc_etherdev(sizeof(*fep));
  1325. if (!dev)
  1326. return -ENOMEM;
  1327. fep = dev->priv;
  1328. fecp = &(immap->im_cpm.cp_fec);
  1329. /* Whack a reset. We should wait for this.
  1330. */
  1331. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  1332. for (i = 0;
  1333. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  1334. ++i) {
  1335. udelay(1);
  1336. }
  1337. if (i == FEC_RESET_DELAY) {
  1338. printk ("FEC Reset timeout!\n");
  1339. }
  1340. /* Set the Ethernet address. If using multiple Enets on the 8xx,
  1341. * this needs some work to get unique addresses.
  1342. */
  1343. eap = (unsigned char *)my_enet_addr;
  1344. iap = bd->bi_enetaddr;
  1345. #ifdef CONFIG_SCC_ENET
  1346. /*
  1347. * If a board has Ethernet configured both on a SCC and the
  1348. * FEC, it needs (at least) 2 MAC addresses (we know that Sun
  1349. * disagrees, but anyway). For the FEC port, we create
  1350. * another address by setting one of the address bits above
  1351. * something that would have (up to now) been allocated.
  1352. */
  1353. for (i=0; i<6; i++)
  1354. tmpaddr[i] = *iap++;
  1355. tmpaddr[3] |= 0x80;
  1356. iap = tmpaddr;
  1357. #endif
  1358. for (i=0; i<6; i++) {
  1359. dev->dev_addr[i] = *eap++ = *iap++;
  1360. }
  1361. /* Allocate memory for buffer descriptors.
  1362. */
  1363. if (((RX_RING_SIZE + TX_RING_SIZE) * sizeof(cbd_t)) > PAGE_SIZE) {
  1364. printk("FEC init error. Need more space.\n");
  1365. printk("FEC initialization failed.\n");
  1366. return 1;
  1367. }
  1368. cbd_base = (cbd_t *)dma_alloc_coherent(dev->class_dev.dev, PAGE_SIZE,
  1369. &mem_addr, GFP_KERNEL);
  1370. /* Set receive and transmit descriptor base.
  1371. */
  1372. fep->rx_bd_base = cbd_base;
  1373. fep->tx_bd_base = cbd_base + RX_RING_SIZE;
  1374. fep->skb_cur = fep->skb_dirty = 0;
  1375. /* Initialize the receive buffer descriptors.
  1376. */
  1377. bdp = fep->rx_bd_base;
  1378. k = 0;
  1379. for (i=0; i<FEC_ENET_RX_PAGES; i++) {
  1380. /* Allocate a page.
  1381. */
  1382. ba = (unsigned char *)dma_alloc_coherent(dev->class_dev.dev,
  1383. PAGE_SIZE,
  1384. &mem_addr,
  1385. GFP_KERNEL);
  1386. /* BUG: no check for failure */
  1387. /* Initialize the BD for every fragment in the page.
  1388. */
  1389. for (j=0; j<FEC_ENET_RX_FRPPG; j++) {
  1390. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1391. bdp->cbd_bufaddr = mem_addr;
  1392. fep->rx_vaddr[k++] = ba;
  1393. mem_addr += FEC_ENET_RX_FRSIZE;
  1394. ba += FEC_ENET_RX_FRSIZE;
  1395. bdp++;
  1396. }
  1397. }
  1398. /* Set the last buffer to wrap.
  1399. */
  1400. bdp--;
  1401. bdp->cbd_sc |= BD_SC_WRAP;
  1402. #ifdef CONFIG_FEC_PACKETHOOK
  1403. fep->ph_lock = 0;
  1404. fep->ph_rxhandler = fep->ph_txhandler = NULL;
  1405. fep->ph_proto = 0;
  1406. fep->ph_regaddr = NULL;
  1407. fep->ph_priv = NULL;
  1408. #endif
  1409. /* Install our interrupt handler.
  1410. */
  1411. if (request_irq(FEC_INTERRUPT, fec_enet_interrupt, 0, "fec", dev) != 0)
  1412. panic("Could not allocate FEC IRQ!");
  1413. #ifdef CONFIG_RPXCLASSIC
  1414. /* Make Port C, bit 15 an input that causes interrupts.
  1415. */
  1416. immap->im_ioport.iop_pcpar &= ~0x0001;
  1417. immap->im_ioport.iop_pcdir &= ~0x0001;
  1418. immap->im_ioport.iop_pcso &= ~0x0001;
  1419. immap->im_ioport.iop_pcint |= 0x0001;
  1420. cpm_install_handler(CPMVEC_PIO_PC15, mii_link_interrupt, dev);
  1421. /* Make LEDS reflect Link status.
  1422. */
  1423. *((uint *) RPX_CSR_ADDR) &= ~BCSR2_FETHLEDMODE;
  1424. #endif
  1425. #ifdef PHY_INTERRUPT
  1426. ((immap_t *)IMAP_ADDR)->im_siu_conf.sc_siel |=
  1427. (0x80000000 >> PHY_INTERRUPT);
  1428. if (request_irq(PHY_INTERRUPT, mii_link_interrupt, 0, "mii", dev) != 0)
  1429. panic("Could not allocate MII IRQ!");
  1430. #endif
  1431. dev->base_addr = (unsigned long)fecp;
  1432. /* The FEC Ethernet specific entries in the device structure. */
  1433. dev->open = fec_enet_open;
  1434. dev->hard_start_xmit = fec_enet_start_xmit;
  1435. dev->tx_timeout = fec_timeout;
  1436. dev->watchdog_timeo = TX_TIMEOUT;
  1437. dev->stop = fec_enet_close;
  1438. dev->get_stats = fec_enet_get_stats;
  1439. dev->set_multicast_list = set_multicast_list;
  1440. #ifdef CONFIG_USE_MDIO
  1441. for (i=0; i<NMII-1; i++)
  1442. mii_cmds[i].mii_next = &mii_cmds[i+1];
  1443. mii_free = mii_cmds;
  1444. #endif /* CONFIG_USE_MDIO */
  1445. /* Configure all of port D for MII.
  1446. */
  1447. immap->im_ioport.iop_pdpar = 0x1fff;
  1448. /* Bits moved from Rev. D onward.
  1449. */
  1450. if ((mfspr(SPRN_IMMR) & 0xffff) < 0x0501)
  1451. immap->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  1452. else
  1453. immap->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  1454. #ifdef CONFIG_USE_MDIO
  1455. /* Set MII speed to 2.5 MHz
  1456. */
  1457. fecp->fec_mii_speed = fep->phy_speed =
  1458. (( (bd->bi_intfreq + 500000) / 2500000 / 2 ) & 0x3F ) << 1;
  1459. #else
  1460. fecp->fec_mii_speed = 0; /* turn off MDIO */
  1461. #endif /* CONFIG_USE_MDIO */
  1462. err = register_netdev(dev);
  1463. if (err) {
  1464. free_netdev(dev);
  1465. return err;
  1466. }
  1467. printk ("%s: FEC ENET Version 0.2, FEC irq %d"
  1468. #ifdef PHY_INTERRUPT
  1469. ", MII irq %d"
  1470. #endif
  1471. ", addr ",
  1472. dev->name, FEC_INTERRUPT
  1473. #ifdef PHY_INTERRUPT
  1474. , PHY_INTERRUPT
  1475. #endif
  1476. );
  1477. for (i=0; i<6; i++)
  1478. printk("%02x%c", dev->dev_addr[i], (i==5) ? '\n' : ':');
  1479. #ifdef CONFIG_USE_MDIO /* start in full duplex mode, and negotiate speed */
  1480. fec_restart (dev, 1);
  1481. #else /* always use half duplex mode only */
  1482. fec_restart (dev, 0);
  1483. #endif
  1484. #ifdef CONFIG_USE_MDIO
  1485. /* Queue up command to detect the PHY and initialize the
  1486. * remainder of the interface.
  1487. */
  1488. fep->phy_id_done = 0;
  1489. fep->phy_addr = 0;
  1490. mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);
  1491. #endif /* CONFIG_USE_MDIO */
  1492. return 0;
  1493. }
  1494. module_init(fec_enet_init);
  1495. /* This function is called to start or restart the FEC during a link
  1496. * change. This only happens when switching between half and full
  1497. * duplex.
  1498. */
  1499. static void
  1500. fec_restart(struct net_device *dev, int duplex)
  1501. {
  1502. struct fec_enet_private *fep;
  1503. int i;
  1504. volatile cbd_t *bdp;
  1505. volatile immap_t *immap;
  1506. volatile fec_t *fecp;
  1507. immap = (immap_t *)IMAP_ADDR; /* pointer to internal registers */
  1508. fecp = &(immap->im_cpm.cp_fec);
  1509. fep = dev->priv;
  1510. /* Whack a reset. We should wait for this.
  1511. */
  1512. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  1513. for (i = 0;
  1514. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  1515. ++i) {
  1516. udelay(1);
  1517. }
  1518. if (i == FEC_RESET_DELAY) {
  1519. printk ("FEC Reset timeout!\n");
  1520. }
  1521. /* Set station address.
  1522. */
  1523. fecp->fec_addr_low = (my_enet_addr[0] << 16) | my_enet_addr[1];
  1524. fecp->fec_addr_high = my_enet_addr[2];
  1525. /* Reset all multicast.
  1526. */
  1527. fecp->fec_hash_table_high = 0;
  1528. fecp->fec_hash_table_low = 0;
  1529. /* Set maximum receive buffer size.
  1530. */
  1531. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  1532. fecp->fec_r_hash = PKT_MAXBUF_SIZE;
  1533. /* Set receive and transmit descriptor base.
  1534. */
  1535. fecp->fec_r_des_start = iopa((uint)(fep->rx_bd_base));
  1536. fecp->fec_x_des_start = iopa((uint)(fep->tx_bd_base));
  1537. fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
  1538. fep->cur_rx = fep->rx_bd_base;
  1539. /* Reset SKB transmit buffers.
  1540. */
  1541. fep->skb_cur = fep->skb_dirty = 0;
  1542. for (i=0; i<=TX_RING_MOD_MASK; i++) {
  1543. if (fep->tx_skbuff[i] != NULL) {
  1544. dev_kfree_skb(fep->tx_skbuff[i]);
  1545. fep->tx_skbuff[i] = NULL;
  1546. }
  1547. }
  1548. /* Initialize the receive buffer descriptors.
  1549. */
  1550. bdp = fep->rx_bd_base;
  1551. for (i=0; i<RX_RING_SIZE; i++) {
  1552. /* Initialize the BD for every fragment in the page.
  1553. */
  1554. bdp->cbd_sc = BD_ENET_RX_EMPTY;
  1555. bdp++;
  1556. }
  1557. /* Set the last buffer to wrap.
  1558. */
  1559. bdp--;
  1560. bdp->cbd_sc |= BD_SC_WRAP;
  1561. /* ...and the same for transmit.
  1562. */
  1563. bdp = fep->tx_bd_base;
  1564. for (i=0; i<TX_RING_SIZE; i++) {
  1565. /* Initialize the BD for every fragment in the page.
  1566. */
  1567. bdp->cbd_sc = 0;
  1568. bdp->cbd_bufaddr = 0;
  1569. bdp++;
  1570. }
  1571. /* Set the last buffer to wrap.
  1572. */
  1573. bdp--;
  1574. bdp->cbd_sc |= BD_SC_WRAP;
  1575. /* Enable MII mode.
  1576. */
  1577. if (duplex) {
  1578. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE; /* MII enable */
  1579. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN; /* FD enable */
  1580. }
  1581. else {
  1582. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  1583. fecp->fec_x_cntrl = 0;
  1584. }
  1585. fep->full_duplex = duplex;
  1586. /* Enable big endian and don't care about SDMA FC.
  1587. */
  1588. fecp->fec_fun_code = 0x78000000;
  1589. #ifdef CONFIG_USE_MDIO
  1590. /* Set MII speed.
  1591. */
  1592. fecp->fec_mii_speed = fep->phy_speed;
  1593. #endif /* CONFIG_USE_MDIO */
  1594. /* Clear any outstanding interrupt.
  1595. */
  1596. fecp->fec_ievent = 0xffc0;
  1597. fecp->fec_ivec = (FEC_INTERRUPT/2) << 29;
  1598. /* Enable interrupts we wish to service.
  1599. */
  1600. fecp->fec_imask = ( FEC_ENET_TXF | FEC_ENET_TXB |
  1601. FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_MII );
  1602. /* And last, enable the transmit and receive processing.
  1603. */
  1604. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  1605. fecp->fec_r_des_active = 0x01000000;
  1606. }
  1607. static void
  1608. fec_stop(struct net_device *dev)
  1609. {
  1610. volatile immap_t *immap;
  1611. volatile fec_t *fecp;
  1612. struct fec_enet_private *fep;
  1613. int i;
  1614. immap = (immap_t *)IMAP_ADDR; /* pointer to internal registers */
  1615. fecp = &(immap->im_cpm.cp_fec);
  1616. if ((fecp->fec_ecntrl & FEC_ECNTRL_ETHER_EN) == 0)
  1617. return; /* already down */
  1618. fep = dev->priv;
  1619. fecp->fec_x_cntrl = 0x01; /* Graceful transmit stop */
  1620. for (i = 0;
  1621. ((fecp->fec_ievent & 0x10000000) == 0) && (i < FEC_RESET_DELAY);
  1622. ++i) {
  1623. udelay(1);
  1624. }
  1625. if (i == FEC_RESET_DELAY) {
  1626. printk ("FEC timeout on graceful transmit stop\n");
  1627. }
  1628. /* Clear outstanding MII command interrupts.
  1629. */
  1630. fecp->fec_ievent = FEC_ENET_MII;
  1631. /* Enable MII command finished interrupt
  1632. */
  1633. fecp->fec_ivec = (FEC_INTERRUPT/2) << 29;
  1634. fecp->fec_imask = FEC_ENET_MII;
  1635. #ifdef CONFIG_USE_MDIO
  1636. /* Set MII speed.
  1637. */
  1638. fecp->fec_mii_speed = fep->phy_speed;
  1639. #endif /* CONFIG_USE_MDIO */
  1640. /* Disable FEC
  1641. */
  1642. fecp->fec_ecntrl &= ~(FEC_ECNTRL_ETHER_EN);
  1643. }