2 * Freescale Ethernet controllers
4 * Copyright (c) 2005 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
10 * This file is licensed under the terms of the GNU General Public License
11 * version 2. This program is licensed "as is" without any warranty of any
12 * kind, whether express or implied.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/string.h>
19 #include <linux/ptrace.h>
20 #include <linux/errno.h>
21 #include <linux/ioport.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/pci.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mii.h>
32 #include <linux/ethtool.h>
33 #include <linux/bitops.h>
35 #include <linux/platform_device.h>
38 #include <asm/uaccess.h>
41 #include <asm/8xx_immap.h>
42 #include <asm/pgtable.h>
43 #include <asm/mpc8xx.h>
44 #include <asm/commproc.h>
50 /*************************************************/
52 #if defined(CONFIG_CPM1)
53 /* for a CPM1 __raw_xxx's are sufficient */
54 #define __fs_out32(addr, x) __raw_writel(x, addr)
55 #define __fs_out16(addr, x) __raw_writew(x, addr)
56 #define __fs_in32(addr) __raw_readl(addr)
57 #define __fs_in16(addr) __raw_readw(addr)
59 /* for others play it safe */
60 #define __fs_out32(addr, x) out_be32(addr, x)
61 #define __fs_out16(addr, x) out_be16(addr, x)
62 #define __fs_in32(addr) in_be32(addr)
63 #define __fs_in16(addr) in_be16(addr)
67 #define FW(_fecp, _reg, _v) __fs_out32(&(_fecp)->fec_ ## _reg, (_v))
70 #define FR(_fecp, _reg) __fs_in32(&(_fecp)->fec_ ## _reg)
73 #define FS(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) | (_v))
76 #define FC(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) & ~(_v))
79 * Delay to wait for FEC reset command to complete (in us)
81 #define FEC_RESET_DELAY 50
83 static int whack_reset(fec_t * fecp)
87 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
88 for (i = 0; i < FEC_RESET_DELAY; i++) {
89 if ((FR(fecp, ecntrl) & FEC_ECNTRL_RESET) == 0)
97 static int do_pd_setup(struct fs_enet_private *fep)
99 struct platform_device *pdev = to_platform_device(fep->dev);
102 /* Fill out IRQ field */
103 fep->interrupt = platform_get_irq_byname(pdev,"interrupt");
104 if (fep->interrupt < 0)
107 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
108 fep->fec.fecp = ioremap(r->start, r->end - r->start + 1);
110 if(fep->fec.fecp == NULL)
117 #define FEC_NAPI_RX_EVENT_MSK (FEC_ENET_RXF | FEC_ENET_RXB)
118 #define FEC_RX_EVENT (FEC_ENET_RXF)
119 #define FEC_TX_EVENT (FEC_ENET_TXF)
120 #define FEC_ERR_EVENT_MSK (FEC_ENET_HBERR | FEC_ENET_BABR | \
121 FEC_ENET_BABT | FEC_ENET_EBERR)
123 static int setup_data(struct net_device *dev)
125 struct fs_enet_private *fep = netdev_priv(dev);
127 if (do_pd_setup(fep) != 0)
133 fep->ev_napi_rx = FEC_NAPI_RX_EVENT_MSK;
134 fep->ev_rx = FEC_RX_EVENT;
135 fep->ev_tx = FEC_TX_EVENT;
136 fep->ev_err = FEC_ERR_EVENT_MSK;
141 static int allocate_bd(struct net_device *dev)
143 struct fs_enet_private *fep = netdev_priv(dev);
144 const struct fs_platform_info *fpi = fep->fpi;
146 fep->ring_base = dma_alloc_coherent(fep->dev,
147 (fpi->tx_ring + fpi->rx_ring) *
148 sizeof(cbd_t), &fep->ring_mem_addr,
150 if (fep->ring_base == NULL)
156 static void free_bd(struct net_device *dev)
158 struct fs_enet_private *fep = netdev_priv(dev);
159 const struct fs_platform_info *fpi = fep->fpi;
162 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring)
168 static void cleanup_data(struct net_device *dev)
173 static void set_promiscuous_mode(struct net_device *dev)
175 struct fs_enet_private *fep = netdev_priv(dev);
176 fec_t *fecp = fep->fec.fecp;
178 FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
181 static void set_multicast_start(struct net_device *dev)
183 struct fs_enet_private *fep = netdev_priv(dev);
189 static void set_multicast_one(struct net_device *dev, const u8 *mac)
191 struct fs_enet_private *fep = netdev_priv(dev);
192 int temp, hash_index, i, j;
197 for (i = 0; i < 6; i++) {
199 for (j = 0; j < 8; j++) {
202 if (msb ^ (byte & 0x1))
208 temp = (crc & 0x3f) >> 1;
209 hash_index = ((temp & 0x01) << 4) |
210 ((temp & 0x02) << 2) |
212 ((temp & 0x08) >> 2) |
213 ((temp & 0x10) >> 4);
214 csrVal = 1 << hash_index;
216 fep->fec.hthi |= csrVal;
218 fep->fec.htlo |= csrVal;
221 static void set_multicast_finish(struct net_device *dev)
223 struct fs_enet_private *fep = netdev_priv(dev);
224 fec_t *fecp = fep->fec.fecp;
226 /* if all multi or too many multicasts; just enable all */
227 if ((dev->flags & IFF_ALLMULTI) != 0 ||
228 dev->mc_count > FEC_MAX_MULTICAST_ADDRS) {
229 fep->fec.hthi = 0xffffffffU;
230 fep->fec.htlo = 0xffffffffU;
233 FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
234 FW(fecp, hash_table_high, fep->fec.hthi);
235 FW(fecp, hash_table_low, fep->fec.htlo);
238 static void set_multicast_list(struct net_device *dev)
240 struct dev_mc_list *pmc;
242 if ((dev->flags & IFF_PROMISC) == 0) {
243 set_multicast_start(dev);
244 for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next)
245 set_multicast_one(dev, pmc->dmi_addr);
246 set_multicast_finish(dev);
248 set_promiscuous_mode(dev);
251 static void restart(struct net_device *dev)
254 immap_t *immap = fs_enet_immap;
257 struct fs_enet_private *fep = netdev_priv(dev);
258 fec_t *fecp = fep->fec.fecp;
259 const struct fs_platform_info *fpi = fep->fpi;
260 dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
264 struct mii_bus* mii = fep->phydev->bus;
265 struct fec_info* fec_inf = mii->priv;
267 r = whack_reset(fep->fec.fecp);
269 printk(KERN_ERR DRV_MODULE_NAME
270 ": %s FEC Reset FAILED!\n", dev->name);
272 * Set station address.
274 addrhi = ((u32) dev->dev_addr[0] << 24) |
275 ((u32) dev->dev_addr[1] << 16) |
276 ((u32) dev->dev_addr[2] << 8) |
277 (u32) dev->dev_addr[3];
278 addrlo = ((u32) dev->dev_addr[4] << 24) |
279 ((u32) dev->dev_addr[5] << 16);
280 FW(fecp, addr_low, addrhi);
281 FW(fecp, addr_high, addrlo);
284 * Reset all multicast.
286 FW(fecp, hash_table_high, fep->fec.hthi);
287 FW(fecp, hash_table_low, fep->fec.htlo);
290 * Set maximum receive buffer size.
292 FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
293 FW(fecp, r_hash, PKT_MAXBUF_SIZE);
295 /* get physical address */
296 rx_bd_base_phys = fep->ring_mem_addr;
297 tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
300 * Set receive and transmit descriptor base.
302 FW(fecp, r_des_start, rx_bd_base_phys);
303 FW(fecp, x_des_start, tx_bd_base_phys);
308 * Enable big endian and don't care about SDMA FC.
310 FW(fecp, fun_code, 0x78000000);
315 FW(fecp, mii_speed, fec_inf->mii_speed);
318 * Clear any outstanding interrupt.
320 FW(fecp, ievent, 0xffc0);
321 #ifndef CONFIG_PPC_MERGE
322 FW(fecp, ivec, (fep->interrupt / 2) << 29);
324 FW(fecp, ivec, (virq_to_hw(fep->interrupt) / 2) << 29);
328 * adjust to speed (only for DUET & RMII)
332 cptr = in_be32(&immap->im_cpm.cp_cptr);
333 switch (fs_get_fec_index(fpi->fs_no)) {
336 if (fep->speed == 10)
338 else if (fep->speed == 100)
343 if (fep->speed == 10)
345 else if (fep->speed == 100)
349 BUG(); /* should never happen */
352 out_be32(&immap->im_cpm.cp_cptr, cptr);
357 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
359 * adjust to duplex mode
361 if (fep->phydev->duplex) {
362 FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
363 FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
365 FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
366 FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
370 * Enable interrupts we wish to service.
372 FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
373 FEC_ENET_RXF | FEC_ENET_RXB);
376 * And last, enable the transmit and receive processing.
378 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
379 FW(fecp, r_des_active, 0x01000000);
382 static void stop(struct net_device *dev)
384 struct fs_enet_private *fep = netdev_priv(dev);
385 const struct fs_platform_info *fpi = fep->fpi;
386 fec_t *fecp = fep->fec.fecp;
388 struct fec_info* feci= fep->phydev->bus->priv;
392 if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
393 return; /* already down */
395 FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
396 for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
397 i < FEC_RESET_DELAY; i++)
400 if (i == FEC_RESET_DELAY)
401 printk(KERN_WARNING DRV_MODULE_NAME
402 ": %s FEC timeout on graceful transmit stop\n",
405 * Disable FEC. Let only MII interrupts.
408 FC(fecp, ecntrl, FEC_ECNTRL_ETHER_EN);
412 /* shut down FEC1? that's where the mii bus is */
414 FS(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
415 FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
416 FW(fecp, ievent, FEC_ENET_MII);
417 FW(fecp, mii_speed, feci->mii_speed);
421 static void pre_request_irq(struct net_device *dev, int irq)
423 #ifndef CONFIG_PPC_MERGE
424 immap_t *immap = fs_enet_immap;
428 if (irq >= SIU_IRQ0 && irq < SIU_LEVEL7) {
430 siel = in_be32(&immap->im_siu_conf.sc_siel);
432 siel |= (0x80000000 >> irq);
434 siel &= ~(0x80000000 >> (irq & ~1));
435 out_be32(&immap->im_siu_conf.sc_siel, siel);
440 static void post_free_irq(struct net_device *dev, int irq)
445 static void napi_clear_rx_event(struct net_device *dev)
447 struct fs_enet_private *fep = netdev_priv(dev);
448 fec_t *fecp = fep->fec.fecp;
450 FW(fecp, ievent, FEC_NAPI_RX_EVENT_MSK);
453 static void napi_enable_rx(struct net_device *dev)
455 struct fs_enet_private *fep = netdev_priv(dev);
456 fec_t *fecp = fep->fec.fecp;
458 FS(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
461 static void napi_disable_rx(struct net_device *dev)
463 struct fs_enet_private *fep = netdev_priv(dev);
464 fec_t *fecp = fep->fec.fecp;
466 FC(fecp, imask, FEC_NAPI_RX_EVENT_MSK);
469 static void rx_bd_done(struct net_device *dev)
471 struct fs_enet_private *fep = netdev_priv(dev);
472 fec_t *fecp = fep->fec.fecp;
474 FW(fecp, r_des_active, 0x01000000);
477 static void tx_kickstart(struct net_device *dev)
479 struct fs_enet_private *fep = netdev_priv(dev);
480 fec_t *fecp = fep->fec.fecp;
482 FW(fecp, x_des_active, 0x01000000);
485 static u32 get_int_events(struct net_device *dev)
487 struct fs_enet_private *fep = netdev_priv(dev);
488 fec_t *fecp = fep->fec.fecp;
490 return FR(fecp, ievent) & FR(fecp, imask);
493 static void clear_int_events(struct net_device *dev, u32 int_events)
495 struct fs_enet_private *fep = netdev_priv(dev);
496 fec_t *fecp = fep->fec.fecp;
498 FW(fecp, ievent, int_events);
501 static void ev_error(struct net_device *dev, u32 int_events)
503 printk(KERN_WARNING DRV_MODULE_NAME
504 ": %s FEC ERROR(s) 0x%x\n", dev->name, int_events);
507 int get_regs(struct net_device *dev, void *p, int *sizep)
509 struct fs_enet_private *fep = netdev_priv(dev);
511 if (*sizep < sizeof(fec_t))
514 memcpy_fromio(p, fep->fec.fecp, sizeof(fec_t));
519 int get_regs_len(struct net_device *dev)
521 return sizeof(fec_t);
524 void tx_restart(struct net_device *dev)
529 /*************************************************************************/
531 const struct fs_ops fs_fec_ops = {
532 .setup_data = setup_data,
533 .cleanup_data = cleanup_data,
534 .set_multicast_list = set_multicast_list,
537 .pre_request_irq = pre_request_irq,
538 .post_free_irq = post_free_irq,
539 .napi_clear_rx_event = napi_clear_rx_event,
540 .napi_enable_rx = napi_enable_rx,
541 .napi_disable_rx = napi_disable_rx,
542 .rx_bd_done = rx_bd_done,
543 .tx_kickstart = tx_kickstart,
544 .get_int_events = get_int_events,
545 .clear_int_events = clear_int_events,
546 .ev_error = ev_error,
547 .get_regs = get_regs,
548 .get_regs_len = get_regs_len,
549 .tx_restart = tx_restart,
550 .allocate_bd = allocate_bd,