Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Freescale Ethernet controllers
0003  *
0004  * Copyright (c) 2005 Intracom S.A.
0005  *  by Pantelis Antoniou <panto@intracom.gr>
0006  *
0007  * 2005 (c) MontaVista Software, Inc.
0008  * Vitaly Bordug <vbordug@ru.mvista.com>
0009  *
0010  * This file is licensed under the terms of the GNU General Public License
0011  * version 2. This program is licensed "as is" without any warranty of any
0012  * kind, whether express or implied.
0013  */
0014 
0015 #include <linux/module.h>
0016 #include <linux/kernel.h>
0017 #include <linux/types.h>
0018 #include <linux/string.h>
0019 #include <linux/ptrace.h>
0020 #include <linux/errno.h>
0021 #include <linux/crc32.h>
0022 #include <linux/ioport.h>
0023 #include <linux/interrupt.h>
0024 #include <linux/delay.h>
0025 #include <linux/netdevice.h>
0026 #include <linux/etherdevice.h>
0027 #include <linux/skbuff.h>
0028 #include <linux/spinlock.h>
0029 #include <linux/mii.h>
0030 #include <linux/ethtool.h>
0031 #include <linux/bitops.h>
0032 #include <linux/fs.h>
0033 #include <linux/platform_device.h>
0034 #include <linux/of_address.h>
0035 #include <linux/of_device.h>
0036 #include <linux/of_irq.h>
0037 #include <linux/gfp.h>
0038 
0039 #include <asm/irq.h>
0040 #include <linux/uaccess.h>
0041 
0042 #include "fs_enet.h"
0043 #include "fec.h"
0044 
0045 /*************************************************/
0046 
0047 #if defined(CONFIG_CPM1)
0048 /* for a CPM1 __raw_xxx's are sufficient */
0049 #define __fs_out32(addr, x) __raw_writel(x, addr)
0050 #define __fs_out16(addr, x) __raw_writew(x, addr)
0051 #define __fs_in32(addr) __raw_readl(addr)
0052 #define __fs_in16(addr) __raw_readw(addr)
0053 #else
0054 /* for others play it safe */
0055 #define __fs_out32(addr, x) out_be32(addr, x)
0056 #define __fs_out16(addr, x) out_be16(addr, x)
0057 #define __fs_in32(addr) in_be32(addr)
0058 #define __fs_in16(addr) in_be16(addr)
0059 #endif
0060 
0061 /* write */
0062 #define FW(_fecp, _reg, _v) __fs_out32(&(_fecp)->fec_ ## _reg, (_v))
0063 
0064 /* read */
0065 #define FR(_fecp, _reg) __fs_in32(&(_fecp)->fec_ ## _reg)
0066 
0067 /* set bits */
0068 #define FS(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) | (_v))
0069 
0070 /* clear bits */
0071 #define FC(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) & ~(_v))
0072 
0073 /*
0074  * Delay to wait for FEC reset command to complete (in us)
0075  */
0076 #define FEC_RESET_DELAY     50
0077 
0078 static int whack_reset(struct fec __iomem *fecp)
0079 {
0080     int i;
0081 
0082     FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
0083     for (i = 0; i < FEC_RESET_DELAY; i++) {
0084         if ((FR(fecp, ecntrl) & FEC_ECNTRL_RESET) == 0)
0085             return 0;   /* OK */
0086         udelay(1);
0087     }
0088 
0089     return -1;
0090 }
0091 
0092 static int do_pd_setup(struct fs_enet_private *fep)
0093 {
0094     struct platform_device *ofdev = to_platform_device(fep->dev);
0095 
0096     fep->interrupt = irq_of_parse_and_map(ofdev->dev.of_node, 0);
0097     if (!fep->interrupt)
0098         return -EINVAL;
0099 
0100     fep->fec.fecp = of_iomap(ofdev->dev.of_node, 0);
0101     if (!fep->fcc.fccp)
0102         return -EINVAL;
0103 
0104     return 0;
0105 }
0106 
0107 #define FEC_NAPI_EVENT_MSK  (FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_TXF)
0108 #define FEC_EVENT       (FEC_ENET_RXF | FEC_ENET_TXF)
0109 #define FEC_ERR_EVENT_MSK   (FEC_ENET_HBERR | FEC_ENET_BABR | \
0110                  FEC_ENET_BABT | FEC_ENET_EBERR)
0111 
0112 static int setup_data(struct net_device *dev)
0113 {
0114     struct fs_enet_private *fep = netdev_priv(dev);
0115 
0116     if (do_pd_setup(fep) != 0)
0117         return -EINVAL;
0118 
0119     fep->fec.hthi = 0;
0120     fep->fec.htlo = 0;
0121 
0122     fep->ev_napi = FEC_NAPI_EVENT_MSK;
0123     fep->ev = FEC_EVENT;
0124     fep->ev_err = FEC_ERR_EVENT_MSK;
0125 
0126     return 0;
0127 }
0128 
0129 static int allocate_bd(struct net_device *dev)
0130 {
0131     struct fs_enet_private *fep = netdev_priv(dev);
0132     const struct fs_platform_info *fpi = fep->fpi;
0133 
0134     fep->ring_base = (void __force __iomem *)dma_alloc_coherent(fep->dev,
0135                         (fpi->tx_ring + fpi->rx_ring) *
0136                         sizeof(cbd_t), &fep->ring_mem_addr,
0137                         GFP_KERNEL);
0138     if (fep->ring_base == NULL)
0139         return -ENOMEM;
0140 
0141     return 0;
0142 }
0143 
0144 static void free_bd(struct net_device *dev)
0145 {
0146     struct fs_enet_private *fep = netdev_priv(dev);
0147     const struct fs_platform_info *fpi = fep->fpi;
0148 
0149     if(fep->ring_base)
0150         dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring)
0151                     * sizeof(cbd_t),
0152                     (void __force *)fep->ring_base,
0153                     fep->ring_mem_addr);
0154 }
0155 
0156 static void cleanup_data(struct net_device *dev)
0157 {
0158     /* nothing */
0159 }
0160 
0161 static void set_promiscuous_mode(struct net_device *dev)
0162 {
0163     struct fs_enet_private *fep = netdev_priv(dev);
0164     struct fec __iomem *fecp = fep->fec.fecp;
0165 
0166     FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
0167 }
0168 
0169 static void set_multicast_start(struct net_device *dev)
0170 {
0171     struct fs_enet_private *fep = netdev_priv(dev);
0172 
0173     fep->fec.hthi = 0;
0174     fep->fec.htlo = 0;
0175 }
0176 
0177 static void set_multicast_one(struct net_device *dev, const u8 *mac)
0178 {
0179     struct fs_enet_private *fep = netdev_priv(dev);
0180     int temp, hash_index;
0181     u32 crc, csrVal;
0182 
0183     crc = ether_crc(6, mac);
0184 
0185     temp = (crc & 0x3f) >> 1;
0186     hash_index = ((temp & 0x01) << 4) |
0187              ((temp & 0x02) << 2) |
0188              ((temp & 0x04)) |
0189              ((temp & 0x08) >> 2) |
0190              ((temp & 0x10) >> 4);
0191     csrVal = 1 << hash_index;
0192     if (crc & 1)
0193         fep->fec.hthi |= csrVal;
0194     else
0195         fep->fec.htlo |= csrVal;
0196 }
0197 
0198 static void set_multicast_finish(struct net_device *dev)
0199 {
0200     struct fs_enet_private *fep = netdev_priv(dev);
0201     struct fec __iomem *fecp = fep->fec.fecp;
0202 
0203     /* if all multi or too many multicasts; just enable all */
0204     if ((dev->flags & IFF_ALLMULTI) != 0 ||
0205         netdev_mc_count(dev) > FEC_MAX_MULTICAST_ADDRS) {
0206         fep->fec.hthi = 0xffffffffU;
0207         fep->fec.htlo = 0xffffffffU;
0208     }
0209 
0210     FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
0211     FW(fecp, grp_hash_table_high, fep->fec.hthi);
0212     FW(fecp, grp_hash_table_low, fep->fec.htlo);
0213 }
0214 
0215 static void set_multicast_list(struct net_device *dev)
0216 {
0217     struct netdev_hw_addr *ha;
0218 
0219     if ((dev->flags & IFF_PROMISC) == 0) {
0220         set_multicast_start(dev);
0221         netdev_for_each_mc_addr(ha, dev)
0222             set_multicast_one(dev, ha->addr);
0223         set_multicast_finish(dev);
0224     } else
0225         set_promiscuous_mode(dev);
0226 }
0227 
0228 static void restart(struct net_device *dev)
0229 {
0230     struct fs_enet_private *fep = netdev_priv(dev);
0231     struct fec __iomem *fecp = fep->fec.fecp;
0232     const struct fs_platform_info *fpi = fep->fpi;
0233     dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
0234     int r;
0235     u32 addrhi, addrlo;
0236 
0237     struct mii_bus *mii = dev->phydev->mdio.bus;
0238     struct fec_info* fec_inf = mii->priv;
0239 
0240     r = whack_reset(fep->fec.fecp);
0241     if (r != 0)
0242         dev_err(fep->dev, "FEC Reset FAILED!\n");
0243     /*
0244      * Set station address.
0245      */
0246     addrhi = ((u32) dev->dev_addr[0] << 24) |
0247          ((u32) dev->dev_addr[1] << 16) |
0248          ((u32) dev->dev_addr[2] <<  8) |
0249           (u32) dev->dev_addr[3];
0250     addrlo = ((u32) dev->dev_addr[4] << 24) |
0251          ((u32) dev->dev_addr[5] << 16);
0252     FW(fecp, addr_low, addrhi);
0253     FW(fecp, addr_high, addrlo);
0254 
0255     /*
0256      * Reset all multicast.
0257      */
0258     FW(fecp, grp_hash_table_high, fep->fec.hthi);
0259     FW(fecp, grp_hash_table_low, fep->fec.htlo);
0260 
0261     /*
0262      * Set maximum receive buffer size.
0263      */
0264     FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
0265 #ifdef CONFIG_FS_ENET_MPC5121_FEC
0266     FW(fecp, r_cntrl, PKT_MAXBUF_SIZE << 16);
0267 #else
0268     FW(fecp, r_hash, PKT_MAXBUF_SIZE);
0269 #endif
0270 
0271     /* get physical address */
0272     rx_bd_base_phys = fep->ring_mem_addr;
0273     tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
0274 
0275     /*
0276      * Set receive and transmit descriptor base.
0277      */
0278     FW(fecp, r_des_start, rx_bd_base_phys);
0279     FW(fecp, x_des_start, tx_bd_base_phys);
0280 
0281     fs_init_bds(dev);
0282 
0283     /*
0284      * Enable big endian and don't care about SDMA FC.
0285      */
0286 #ifdef CONFIG_FS_ENET_MPC5121_FEC
0287     FS(fecp, dma_control, 0xC0000000);
0288 #else
0289     FW(fecp, fun_code, 0x78000000);
0290 #endif
0291 
0292     /*
0293      * Set MII speed.
0294      */
0295     FW(fecp, mii_speed, fec_inf->mii_speed);
0296 
0297     /*
0298      * Clear any outstanding interrupt.
0299      */
0300     FW(fecp, ievent, 0xffc0);
0301 #ifndef CONFIG_FS_ENET_MPC5121_FEC
0302     FW(fecp, ivec, (virq_to_hw(fep->interrupt) / 2) << 29);
0303 
0304     FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
0305 #else
0306     /*
0307      * Only set MII/RMII mode - do not touch maximum frame length
0308      * configured before.
0309      */
0310     FS(fecp, r_cntrl, fpi->use_rmii ?
0311             FEC_RCNTRL_RMII_MODE : FEC_RCNTRL_MII_MODE);
0312 #endif
0313     /*
0314      * adjust to duplex mode
0315      */
0316     if (dev->phydev->duplex) {
0317         FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
0318         FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
0319     } else {
0320         FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
0321         FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
0322     }
0323 
0324     /* Restore multicast and promiscuous settings */
0325     set_multicast_list(dev);
0326 
0327     /*
0328      * Enable interrupts we wish to service.
0329      */
0330     FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
0331        FEC_ENET_RXF | FEC_ENET_RXB);
0332 
0333     /*
0334      * And last, enable the transmit and receive processing.
0335      */
0336     FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
0337     FW(fecp, r_des_active, 0x01000000);
0338 }
0339 
0340 static void stop(struct net_device *dev)
0341 {
0342     struct fs_enet_private *fep = netdev_priv(dev);
0343     const struct fs_platform_info *fpi = fep->fpi;
0344     struct fec __iomem *fecp = fep->fec.fecp;
0345 
0346     struct fec_info *feci = dev->phydev->mdio.bus->priv;
0347 
0348     int i;
0349 
0350     if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
0351         return;     /* already down */
0352 
0353     FW(fecp, x_cntrl, 0x01);    /* Graceful transmit stop */
0354     for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
0355          i < FEC_RESET_DELAY; i++)
0356         udelay(1);
0357 
0358     if (i == FEC_RESET_DELAY)
0359         dev_warn(fep->dev, "FEC timeout on graceful transmit stop\n");
0360     /*
0361      * Disable FEC. Let only MII interrupts.
0362      */
0363     FW(fecp, imask, 0);
0364     FC(fecp, ecntrl, FEC_ECNTRL_ETHER_EN);
0365 
0366     fs_cleanup_bds(dev);
0367 
0368     /* shut down FEC1? that's where the mii bus is */
0369     if (fpi->has_phy) {
0370         FS(fecp, r_cntrl, fpi->use_rmii ?
0371                 FEC_RCNTRL_RMII_MODE :
0372                 FEC_RCNTRL_MII_MODE);   /* MII/RMII enable */
0373         FS(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
0374         FW(fecp, ievent, FEC_ENET_MII);
0375         FW(fecp, mii_speed, feci->mii_speed);
0376     }
0377 }
0378 
0379 static void napi_clear_event_fs(struct net_device *dev)
0380 {
0381     struct fs_enet_private *fep = netdev_priv(dev);
0382     struct fec __iomem *fecp = fep->fec.fecp;
0383 
0384     FW(fecp, ievent, FEC_NAPI_EVENT_MSK);
0385 }
0386 
0387 static void napi_enable_fs(struct net_device *dev)
0388 {
0389     struct fs_enet_private *fep = netdev_priv(dev);
0390     struct fec __iomem *fecp = fep->fec.fecp;
0391 
0392     FS(fecp, imask, FEC_NAPI_EVENT_MSK);
0393 }
0394 
0395 static void napi_disable_fs(struct net_device *dev)
0396 {
0397     struct fs_enet_private *fep = netdev_priv(dev);
0398     struct fec __iomem *fecp = fep->fec.fecp;
0399 
0400     FC(fecp, imask, FEC_NAPI_EVENT_MSK);
0401 }
0402 
0403 static void rx_bd_done(struct net_device *dev)
0404 {
0405     struct fs_enet_private *fep = netdev_priv(dev);
0406     struct fec __iomem *fecp = fep->fec.fecp;
0407 
0408     FW(fecp, r_des_active, 0x01000000);
0409 }
0410 
0411 static void tx_kickstart(struct net_device *dev)
0412 {
0413     struct fs_enet_private *fep = netdev_priv(dev);
0414     struct fec __iomem *fecp = fep->fec.fecp;
0415 
0416     FW(fecp, x_des_active, 0x01000000);
0417 }
0418 
0419 static u32 get_int_events(struct net_device *dev)
0420 {
0421     struct fs_enet_private *fep = netdev_priv(dev);
0422     struct fec __iomem *fecp = fep->fec.fecp;
0423 
0424     return FR(fecp, ievent) & FR(fecp, imask);
0425 }
0426 
0427 static void clear_int_events(struct net_device *dev, u32 int_events)
0428 {
0429     struct fs_enet_private *fep = netdev_priv(dev);
0430     struct fec __iomem *fecp = fep->fec.fecp;
0431 
0432     FW(fecp, ievent, int_events);
0433 }
0434 
0435 static void ev_error(struct net_device *dev, u32 int_events)
0436 {
0437     struct fs_enet_private *fep = netdev_priv(dev);
0438 
0439     dev_warn(fep->dev, "FEC ERROR(s) 0x%x\n", int_events);
0440 }
0441 
0442 static int get_regs(struct net_device *dev, void *p, int *sizep)
0443 {
0444     struct fs_enet_private *fep = netdev_priv(dev);
0445 
0446     if (*sizep < sizeof(struct fec))
0447         return -EINVAL;
0448 
0449     memcpy_fromio(p, fep->fec.fecp, sizeof(struct fec));
0450 
0451     return 0;
0452 }
0453 
0454 static int get_regs_len(struct net_device *dev)
0455 {
0456     return sizeof(struct fec);
0457 }
0458 
0459 static void tx_restart(struct net_device *dev)
0460 {
0461     /* nothing */
0462 }
0463 
0464 /*************************************************************************/
0465 
0466 const struct fs_ops fs_fec_ops = {
0467     .setup_data     = setup_data,
0468     .cleanup_data       = cleanup_data,
0469     .set_multicast_list = set_multicast_list,
0470     .restart        = restart,
0471     .stop           = stop,
0472     .napi_clear_event   = napi_clear_event_fs,
0473     .napi_enable        = napi_enable_fs,
0474     .napi_disable       = napi_disable_fs,
0475     .rx_bd_done     = rx_bd_done,
0476     .tx_kickstart       = tx_kickstart,
0477     .get_int_events     = get_int_events,
0478     .clear_int_events   = clear_int_events,
0479     .ev_error       = ev_error,
0480     .get_regs       = get_regs,
0481     .get_regs_len       = get_regs_len,
0482     .tx_restart     = tx_restart,
0483     .allocate_bd        = allocate_bd,
0484     .free_bd        = free_bd,
0485 };
0486