Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Ethernet on Serial Communications Controller (SCC) driver for Motorola MPC8xx and MPC82xx.
0003  *
0004  * Copyright (c) 2003 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/ioport.h>
0022 #include <linux/interrupt.h>
0023 #include <linux/delay.h>
0024 #include <linux/netdevice.h>
0025 #include <linux/etherdevice.h>
0026 #include <linux/skbuff.h>
0027 #include <linux/spinlock.h>
0028 #include <linux/mii.h>
0029 #include <linux/ethtool.h>
0030 #include <linux/bitops.h>
0031 #include <linux/fs.h>
0032 #include <linux/platform_device.h>
0033 #include <linux/of_address.h>
0034 #include <linux/of_irq.h>
0035 #include <linux/of_platform.h>
0036 
0037 #include <asm/irq.h>
0038 #include <linux/uaccess.h>
0039 
0040 #include "fs_enet.h"
0041 
0042 /*************************************************/
0043 #if defined(CONFIG_CPM1)
0044 /* for a 8xx __raw_xxx's are sufficient */
0045 #define __fs_out32(addr, x) __raw_writel(x, addr)
0046 #define __fs_out16(addr, x) __raw_writew(x, addr)
0047 #define __fs_out8(addr, x)  __raw_writeb(x, addr)
0048 #define __fs_in32(addr) __raw_readl(addr)
0049 #define __fs_in16(addr) __raw_readw(addr)
0050 #define __fs_in8(addr)  __raw_readb(addr)
0051 #else
0052 /* for others play it safe */
0053 #define __fs_out32(addr, x) out_be32(addr, x)
0054 #define __fs_out16(addr, x) out_be16(addr, x)
0055 #define __fs_in32(addr) in_be32(addr)
0056 #define __fs_in16(addr) in_be16(addr)
0057 #define __fs_out8(addr, x)  out_8(addr, x)
0058 #define __fs_in8(addr)  in_8(addr)
0059 #endif
0060 
0061 /* write, read, set bits, clear bits */
0062 #define W32(_p, _m, _v) __fs_out32(&(_p)->_m, (_v))
0063 #define R32(_p, _m)     __fs_in32(&(_p)->_m)
0064 #define S32(_p, _m, _v) W32(_p, _m, R32(_p, _m) | (_v))
0065 #define C32(_p, _m, _v) W32(_p, _m, R32(_p, _m) & ~(_v))
0066 
0067 #define W16(_p, _m, _v) __fs_out16(&(_p)->_m, (_v))
0068 #define R16(_p, _m)     __fs_in16(&(_p)->_m)
0069 #define S16(_p, _m, _v) W16(_p, _m, R16(_p, _m) | (_v))
0070 #define C16(_p, _m, _v) W16(_p, _m, R16(_p, _m) & ~(_v))
0071 
0072 #define W8(_p, _m, _v)  __fs_out8(&(_p)->_m, (_v))
0073 #define R8(_p, _m)      __fs_in8(&(_p)->_m)
0074 #define S8(_p, _m, _v)  W8(_p, _m, R8(_p, _m) | (_v))
0075 #define C8(_p, _m, _v)  W8(_p, _m, R8(_p, _m) & ~(_v))
0076 
0077 #define SCC_MAX_MULTICAST_ADDRS 64
0078 
0079 /*
0080  * Delay to wait for SCC reset command to complete (in us)
0081  */
0082 #define SCC_RESET_DELAY     50
0083 
0084 static inline int scc_cr_cmd(struct fs_enet_private *fep, u32 op)
0085 {
0086     const struct fs_platform_info *fpi = fep->fpi;
0087 
0088     return cpm_command(fpi->cp_command, op);
0089 }
0090 
0091 static int do_pd_setup(struct fs_enet_private *fep)
0092 {
0093     struct platform_device *ofdev = to_platform_device(fep->dev);
0094 
0095     fep->interrupt = irq_of_parse_and_map(ofdev->dev.of_node, 0);
0096     if (!fep->interrupt)
0097         return -EINVAL;
0098 
0099     fep->scc.sccp = of_iomap(ofdev->dev.of_node, 0);
0100     if (!fep->scc.sccp)
0101         return -EINVAL;
0102 
0103     fep->scc.ep = of_iomap(ofdev->dev.of_node, 1);
0104     if (!fep->scc.ep) {
0105         iounmap(fep->scc.sccp);
0106         return -EINVAL;
0107     }
0108 
0109     return 0;
0110 }
0111 
0112 #define SCC_NAPI_EVENT_MSK  (SCCE_ENET_RXF | SCCE_ENET_RXB | SCCE_ENET_TXB)
0113 #define SCC_EVENT       (SCCE_ENET_RXF | SCCE_ENET_TXB)
0114 #define SCC_ERR_EVENT_MSK   (SCCE_ENET_TXE | SCCE_ENET_BSY)
0115 
0116 static int setup_data(struct net_device *dev)
0117 {
0118     struct fs_enet_private *fep = netdev_priv(dev);
0119 
0120     do_pd_setup(fep);
0121 
0122     fep->scc.hthi = 0;
0123     fep->scc.htlo = 0;
0124 
0125     fep->ev_napi = SCC_NAPI_EVENT_MSK;
0126     fep->ev = SCC_EVENT | SCCE_ENET_TXE;
0127     fep->ev_err = SCC_ERR_EVENT_MSK;
0128 
0129     return 0;
0130 }
0131 
0132 static int allocate_bd(struct net_device *dev)
0133 {
0134     struct fs_enet_private *fep = netdev_priv(dev);
0135     const struct fs_platform_info *fpi = fep->fpi;
0136 
0137     fep->ring_mem_addr = cpm_dpalloc((fpi->tx_ring + fpi->rx_ring) *
0138                      sizeof(cbd_t), 8);
0139     if (IS_ERR_VALUE(fep->ring_mem_addr))
0140         return -ENOMEM;
0141 
0142     fep->ring_base = (void __iomem __force*)
0143         cpm_dpram_addr(fep->ring_mem_addr);
0144 
0145     return 0;
0146 }
0147 
0148 static void free_bd(struct net_device *dev)
0149 {
0150     struct fs_enet_private *fep = netdev_priv(dev);
0151 
0152     if (fep->ring_base)
0153         cpm_dpfree(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     scc_t __iomem *sccp = fep->scc.sccp;
0165 
0166     S16(sccp, scc_psmr, SCC_PSMR_PRO);
0167 }
0168 
0169 static void set_multicast_start(struct net_device *dev)
0170 {
0171     struct fs_enet_private *fep = netdev_priv(dev);
0172     scc_enet_t __iomem *ep = fep->scc.ep;
0173 
0174     W16(ep, sen_gaddr1, 0);
0175     W16(ep, sen_gaddr2, 0);
0176     W16(ep, sen_gaddr3, 0);
0177     W16(ep, sen_gaddr4, 0);
0178 }
0179 
0180 static void set_multicast_one(struct net_device *dev, const u8 * mac)
0181 {
0182     struct fs_enet_private *fep = netdev_priv(dev);
0183     scc_enet_t __iomem *ep = fep->scc.ep;
0184     u16 taddrh, taddrm, taddrl;
0185 
0186     taddrh = ((u16) mac[5] << 8) | mac[4];
0187     taddrm = ((u16) mac[3] << 8) | mac[2];
0188     taddrl = ((u16) mac[1] << 8) | mac[0];
0189 
0190     W16(ep, sen_taddrh, taddrh);
0191     W16(ep, sen_taddrm, taddrm);
0192     W16(ep, sen_taddrl, taddrl);
0193     scc_cr_cmd(fep, CPM_CR_SET_GADDR);
0194 }
0195 
0196 static void set_multicast_finish(struct net_device *dev)
0197 {
0198     struct fs_enet_private *fep = netdev_priv(dev);
0199     scc_t __iomem *sccp = fep->scc.sccp;
0200     scc_enet_t __iomem *ep = fep->scc.ep;
0201 
0202     /* clear promiscuous always */
0203     C16(sccp, scc_psmr, SCC_PSMR_PRO);
0204 
0205     /* if all multi or too many multicasts; just enable all */
0206     if ((dev->flags & IFF_ALLMULTI) != 0 ||
0207         netdev_mc_count(dev) > SCC_MAX_MULTICAST_ADDRS) {
0208 
0209         W16(ep, sen_gaddr1, 0xffff);
0210         W16(ep, sen_gaddr2, 0xffff);
0211         W16(ep, sen_gaddr3, 0xffff);
0212         W16(ep, sen_gaddr4, 0xffff);
0213     }
0214 }
0215 
0216 static void set_multicast_list(struct net_device *dev)
0217 {
0218     struct netdev_hw_addr *ha;
0219 
0220     if ((dev->flags & IFF_PROMISC) == 0) {
0221         set_multicast_start(dev);
0222         netdev_for_each_mc_addr(ha, dev)
0223             set_multicast_one(dev, ha->addr);
0224         set_multicast_finish(dev);
0225     } else
0226         set_promiscuous_mode(dev);
0227 }
0228 
0229 /*
0230  * This function is called to start or restart the FEC during a link
0231  * change.  This only happens when switching between half and full
0232  * duplex.
0233  */
0234 static void restart(struct net_device *dev)
0235 {
0236     struct fs_enet_private *fep = netdev_priv(dev);
0237     scc_t __iomem *sccp = fep->scc.sccp;
0238     scc_enet_t __iomem *ep = fep->scc.ep;
0239     const struct fs_platform_info *fpi = fep->fpi;
0240     u16 paddrh, paddrm, paddrl;
0241     const unsigned char *mac;
0242     int i;
0243 
0244     C32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
0245 
0246     /* clear everything (slow & steady does it) */
0247     for (i = 0; i < sizeof(*ep); i++)
0248         __fs_out8((u8 __iomem *)ep + i, 0);
0249 
0250     /* point to bds */
0251     W16(ep, sen_genscc.scc_rbase, fep->ring_mem_addr);
0252     W16(ep, sen_genscc.scc_tbase,
0253         fep->ring_mem_addr + sizeof(cbd_t) * fpi->rx_ring);
0254 
0255     /* Initialize function code registers for big-endian.
0256      */
0257 #ifndef CONFIG_NOT_COHERENT_CACHE
0258     W8(ep, sen_genscc.scc_rfcr, SCC_EB | SCC_GBL);
0259     W8(ep, sen_genscc.scc_tfcr, SCC_EB | SCC_GBL);
0260 #else
0261     W8(ep, sen_genscc.scc_rfcr, SCC_EB);
0262     W8(ep, sen_genscc.scc_tfcr, SCC_EB);
0263 #endif
0264 
0265     /* Set maximum bytes per receive buffer.
0266      * This appears to be an Ethernet frame size, not the buffer
0267      * fragment size.  It must be a multiple of four.
0268      */
0269     W16(ep, sen_genscc.scc_mrblr, 0x5f0);
0270 
0271     /* Set CRC preset and mask.
0272      */
0273     W32(ep, sen_cpres, 0xffffffff);
0274     W32(ep, sen_cmask, 0xdebb20e3);
0275 
0276     W32(ep, sen_crcec, 0);  /* CRC Error counter */
0277     W32(ep, sen_alec, 0);   /* alignment error counter */
0278     W32(ep, sen_disfc, 0);  /* discard frame counter */
0279 
0280     W16(ep, sen_pads, 0x8888);  /* Tx short frame pad character */
0281     W16(ep, sen_retlim, 15);    /* Retry limit threshold */
0282 
0283     W16(ep, sen_maxflr, 0x5ee); /* maximum frame length register */
0284 
0285     W16(ep, sen_minflr, PKT_MINBUF_SIZE);   /* minimum frame length register */
0286 
0287     W16(ep, sen_maxd1, 0x000005f0); /* maximum DMA1 length */
0288     W16(ep, sen_maxd2, 0x000005f0); /* maximum DMA2 length */
0289 
0290     /* Clear hash tables.
0291      */
0292     W16(ep, sen_gaddr1, 0);
0293     W16(ep, sen_gaddr2, 0);
0294     W16(ep, sen_gaddr3, 0);
0295     W16(ep, sen_gaddr4, 0);
0296     W16(ep, sen_iaddr1, 0);
0297     W16(ep, sen_iaddr2, 0);
0298     W16(ep, sen_iaddr3, 0);
0299     W16(ep, sen_iaddr4, 0);
0300 
0301     /* set address
0302      */
0303     mac = dev->dev_addr;
0304     paddrh = ((u16) mac[5] << 8) | mac[4];
0305     paddrm = ((u16) mac[3] << 8) | mac[2];
0306     paddrl = ((u16) mac[1] << 8) | mac[0];
0307 
0308     W16(ep, sen_paddrh, paddrh);
0309     W16(ep, sen_paddrm, paddrm);
0310     W16(ep, sen_paddrl, paddrl);
0311 
0312     W16(ep, sen_pper, 0);
0313     W16(ep, sen_taddrl, 0);
0314     W16(ep, sen_taddrm, 0);
0315     W16(ep, sen_taddrh, 0);
0316 
0317     fs_init_bds(dev);
0318 
0319     scc_cr_cmd(fep, CPM_CR_INIT_TRX);
0320 
0321     W16(sccp, scc_scce, 0xffff);
0322 
0323     /* Enable interrupts we wish to service.
0324      */
0325     W16(sccp, scc_sccm, SCCE_ENET_TXE | SCCE_ENET_RXF | SCCE_ENET_TXB);
0326 
0327     /* Set GSMR_H to enable all normal operating modes.
0328      * Set GSMR_L to enable Ethernet to MC68160.
0329      */
0330     W32(sccp, scc_gsmrh, 0);
0331     W32(sccp, scc_gsmrl,
0332         SCC_GSMRL_TCI | SCC_GSMRL_TPL_48 | SCC_GSMRL_TPP_10 |
0333         SCC_GSMRL_MODE_ENET);
0334 
0335     /* Set sync/delimiters.
0336      */
0337     W16(sccp, scc_dsr, 0xd555);
0338 
0339     /* Set processing mode.  Use Ethernet CRC, catch broadcast, and
0340      * start frame search 22 bit times after RENA.
0341      */
0342     W16(sccp, scc_psmr, SCC_PSMR_ENCRC | SCC_PSMR_NIB22);
0343 
0344     /* Set full duplex mode if needed */
0345     if (dev->phydev->duplex)
0346         S16(sccp, scc_psmr, SCC_PSMR_LPB | SCC_PSMR_FDE);
0347 
0348     /* Restore multicast and promiscuous settings */
0349     set_multicast_list(dev);
0350 
0351     S32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
0352 }
0353 
0354 static void stop(struct net_device *dev)
0355 {
0356     struct fs_enet_private *fep = netdev_priv(dev);
0357     scc_t __iomem *sccp = fep->scc.sccp;
0358     int i;
0359 
0360     for (i = 0; (R16(sccp, scc_sccm) == 0) && i < SCC_RESET_DELAY; i++)
0361         udelay(1);
0362 
0363     if (i == SCC_RESET_DELAY)
0364         dev_warn(fep->dev, "SCC timeout on graceful transmit stop\n");
0365 
0366     W16(sccp, scc_sccm, 0);
0367     C32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
0368 
0369     fs_cleanup_bds(dev);
0370 }
0371 
0372 static void napi_clear_event_fs(struct net_device *dev)
0373 {
0374     struct fs_enet_private *fep = netdev_priv(dev);
0375     scc_t __iomem *sccp = fep->scc.sccp;
0376 
0377     W16(sccp, scc_scce, SCC_NAPI_EVENT_MSK);
0378 }
0379 
0380 static void napi_enable_fs(struct net_device *dev)
0381 {
0382     struct fs_enet_private *fep = netdev_priv(dev);
0383     scc_t __iomem *sccp = fep->scc.sccp;
0384 
0385     S16(sccp, scc_sccm, SCC_NAPI_EVENT_MSK);
0386 }
0387 
0388 static void napi_disable_fs(struct net_device *dev)
0389 {
0390     struct fs_enet_private *fep = netdev_priv(dev);
0391     scc_t __iomem *sccp = fep->scc.sccp;
0392 
0393     C16(sccp, scc_sccm, SCC_NAPI_EVENT_MSK);
0394 }
0395 
0396 static void rx_bd_done(struct net_device *dev)
0397 {
0398     /* nothing */
0399 }
0400 
0401 static void tx_kickstart(struct net_device *dev)
0402 {
0403     /* nothing */
0404 }
0405 
0406 static u32 get_int_events(struct net_device *dev)
0407 {
0408     struct fs_enet_private *fep = netdev_priv(dev);
0409     scc_t __iomem *sccp = fep->scc.sccp;
0410 
0411     return (u32) R16(sccp, scc_scce);
0412 }
0413 
0414 static void clear_int_events(struct net_device *dev, u32 int_events)
0415 {
0416     struct fs_enet_private *fep = netdev_priv(dev);
0417     scc_t __iomem *sccp = fep->scc.sccp;
0418 
0419     W16(sccp, scc_scce, int_events & 0xffff);
0420 }
0421 
0422 static void ev_error(struct net_device *dev, u32 int_events)
0423 {
0424     struct fs_enet_private *fep = netdev_priv(dev);
0425 
0426     dev_warn(fep->dev, "SCC ERROR(s) 0x%x\n", int_events);
0427 }
0428 
0429 static int get_regs(struct net_device *dev, void *p, int *sizep)
0430 {
0431     struct fs_enet_private *fep = netdev_priv(dev);
0432 
0433     if (*sizep < sizeof(scc_t) + sizeof(scc_enet_t __iomem *))
0434         return -EINVAL;
0435 
0436     memcpy_fromio(p, fep->scc.sccp, sizeof(scc_t));
0437     p = (char *)p + sizeof(scc_t);
0438 
0439     memcpy_fromio(p, fep->scc.ep, sizeof(scc_enet_t __iomem *));
0440 
0441     return 0;
0442 }
0443 
0444 static int get_regs_len(struct net_device *dev)
0445 {
0446     return sizeof(scc_t) + sizeof(scc_enet_t __iomem *);
0447 }
0448 
0449 static void tx_restart(struct net_device *dev)
0450 {
0451     struct fs_enet_private *fep = netdev_priv(dev);
0452 
0453     scc_cr_cmd(fep, CPM_CR_RESTART_TX);
0454 }
0455 
0456 
0457 
0458 /*************************************************************************/
0459 
0460 const struct fs_ops fs_scc_ops = {
0461     .setup_data     = setup_data,
0462     .cleanup_data       = cleanup_data,
0463     .set_multicast_list = set_multicast_list,
0464     .restart        = restart,
0465     .stop           = stop,
0466     .napi_clear_event   = napi_clear_event_fs,
0467     .napi_enable        = napi_enable_fs,
0468     .napi_disable       = napi_disable_fs,
0469     .rx_bd_done     = rx_bd_done,
0470     .tx_kickstart       = tx_kickstart,
0471     .get_int_events     = get_int_events,
0472     .clear_int_events   = clear_int_events,
0473     .ev_error       = ev_error,
0474     .get_regs       = get_regs,
0475     .get_regs_len       = get_regs_len,
0476     .tx_restart     = tx_restart,
0477     .allocate_bd        = allocate_bd,
0478     .free_bd        = free_bd,
0479 };