fec.c 50 KB

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