Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * drivers/net/ethernet/ibm/emac/zmii.c
0004  *
0005  * Driver for PowerPC 4xx on-chip ethernet controller, ZMII bridge support.
0006  *
0007  * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
0008  *                <benh@kernel.crashing.org>
0009  *
0010  * Based on the arch/ppc version of the driver:
0011  *
0012  * Copyright (c) 2004, 2005 Zultys Technologies.
0013  * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
0014  *
0015  * Based on original work by
0016  *      Armin Kuster <akuster@mvista.com>
0017  *  Copyright 2001 MontaVista Softare Inc.
0018  */
0019 #include <linux/slab.h>
0020 #include <linux/kernel.h>
0021 #include <linux/ethtool.h>
0022 #include <linux/of_address.h>
0023 #include <asm/io.h>
0024 
0025 #include "emac.h"
0026 #include "core.h"
0027 
0028 /* ZMIIx_FER */
0029 #define ZMII_FER_MDI(idx)   (0x80000000 >> ((idx) * 4))
0030 #define ZMII_FER_MDI_ALL    (ZMII_FER_MDI(0) | ZMII_FER_MDI(1) | \
0031                  ZMII_FER_MDI(2) | ZMII_FER_MDI(3))
0032 
0033 #define ZMII_FER_SMII(idx)  (0x40000000 >> ((idx) * 4))
0034 #define ZMII_FER_RMII(idx)  (0x20000000 >> ((idx) * 4))
0035 #define ZMII_FER_MII(idx)   (0x10000000 >> ((idx) * 4))
0036 
0037 /* ZMIIx_SSR */
0038 #define ZMII_SSR_SCI(idx)   (0x40000000 >> ((idx) * 4))
0039 #define ZMII_SSR_FSS(idx)   (0x20000000 >> ((idx) * 4))
0040 #define ZMII_SSR_SP(idx)    (0x10000000 >> ((idx) * 4))
0041 
0042 /* ZMII only supports MII, RMII and SMII
0043  * we also support autodetection for backward compatibility
0044  */
0045 static inline int zmii_valid_mode(int mode)
0046 {
0047     return  mode == PHY_INTERFACE_MODE_MII ||
0048         mode == PHY_INTERFACE_MODE_RMII ||
0049         mode == PHY_INTERFACE_MODE_SMII ||
0050         mode == PHY_INTERFACE_MODE_NA;
0051 }
0052 
0053 static inline const char *zmii_mode_name(int mode)
0054 {
0055     switch (mode) {
0056     case PHY_INTERFACE_MODE_MII:
0057         return "MII";
0058     case PHY_INTERFACE_MODE_RMII:
0059         return "RMII";
0060     case PHY_INTERFACE_MODE_SMII:
0061         return "SMII";
0062     default:
0063         BUG();
0064     }
0065 }
0066 
0067 static inline u32 zmii_mode_mask(int mode, int input)
0068 {
0069     switch (mode) {
0070     case PHY_INTERFACE_MODE_MII:
0071         return ZMII_FER_MII(input);
0072     case PHY_INTERFACE_MODE_RMII:
0073         return ZMII_FER_RMII(input);
0074     case PHY_INTERFACE_MODE_SMII:
0075         return ZMII_FER_SMII(input);
0076     default:
0077         return 0;
0078     }
0079 }
0080 
0081 int zmii_attach(struct platform_device *ofdev, int input,
0082         phy_interface_t *mode)
0083 {
0084     struct zmii_instance *dev = platform_get_drvdata(ofdev);
0085     struct zmii_regs __iomem *p = dev->base;
0086 
0087     ZMII_DBG(dev, "init(%d, %d)" NL, input, *mode);
0088 
0089     if (!zmii_valid_mode(*mode)) {
0090         /* Probably an EMAC connected to RGMII,
0091          * but it still may need ZMII for MDIO so
0092          * we don't fail here.
0093          */
0094         dev->users++;
0095         return 0;
0096     }
0097 
0098     mutex_lock(&dev->lock);
0099 
0100     /* Autodetect ZMII mode if not specified.
0101      * This is only for backward compatibility with the old driver.
0102      * Please, always specify PHY mode in your board port to avoid
0103      * any surprises.
0104      */
0105     if (dev->mode == PHY_INTERFACE_MODE_NA) {
0106         if (*mode == PHY_INTERFACE_MODE_NA) {
0107             u32 r = dev->fer_save;
0108 
0109             ZMII_DBG(dev, "autodetecting mode, FER = 0x%08x" NL, r);
0110 
0111             if (r & (ZMII_FER_MII(0) | ZMII_FER_MII(1)))
0112                 dev->mode = PHY_INTERFACE_MODE_MII;
0113             else if (r & (ZMII_FER_RMII(0) | ZMII_FER_RMII(1)))
0114                 dev->mode = PHY_INTERFACE_MODE_RMII;
0115             else
0116                 dev->mode = PHY_INTERFACE_MODE_SMII;
0117         } else {
0118             dev->mode = *mode;
0119         }
0120         printk(KERN_NOTICE "%pOF: bridge in %s mode\n",
0121                ofdev->dev.of_node,
0122                zmii_mode_name(dev->mode));
0123     } else {
0124         /* All inputs must use the same mode */
0125         if (*mode != PHY_INTERFACE_MODE_NA && *mode != dev->mode) {
0126             printk(KERN_ERR
0127                    "%pOF: invalid mode %d specified for input %d\n",
0128                    ofdev->dev.of_node, *mode, input);
0129             mutex_unlock(&dev->lock);
0130             return -EINVAL;
0131         }
0132     }
0133 
0134     /* Report back correct PHY mode,
0135      * it may be used during PHY initialization.
0136      */
0137     *mode = dev->mode;
0138 
0139     /* Enable this input */
0140     out_be32(&p->fer, in_be32(&p->fer) | zmii_mode_mask(dev->mode, input));
0141     ++dev->users;
0142 
0143     mutex_unlock(&dev->lock);
0144 
0145     return 0;
0146 }
0147 
0148 void zmii_get_mdio(struct platform_device *ofdev, int input)
0149 {
0150     struct zmii_instance *dev = platform_get_drvdata(ofdev);
0151     u32 fer;
0152 
0153     ZMII_DBG2(dev, "get_mdio(%d)" NL, input);
0154 
0155     mutex_lock(&dev->lock);
0156 
0157     fer = in_be32(&dev->base->fer) & ~ZMII_FER_MDI_ALL;
0158     out_be32(&dev->base->fer, fer | ZMII_FER_MDI(input));
0159 }
0160 
0161 void zmii_put_mdio(struct platform_device *ofdev, int input)
0162 {
0163     struct zmii_instance *dev = platform_get_drvdata(ofdev);
0164 
0165     ZMII_DBG2(dev, "put_mdio(%d)" NL, input);
0166     mutex_unlock(&dev->lock);
0167 }
0168 
0169 
0170 void zmii_set_speed(struct platform_device *ofdev, int input, int speed)
0171 {
0172     struct zmii_instance *dev = platform_get_drvdata(ofdev);
0173     u32 ssr;
0174 
0175     mutex_lock(&dev->lock);
0176 
0177     ssr = in_be32(&dev->base->ssr);
0178 
0179     ZMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
0180 
0181     if (speed == SPEED_100)
0182         ssr |= ZMII_SSR_SP(input);
0183     else
0184         ssr &= ~ZMII_SSR_SP(input);
0185 
0186     out_be32(&dev->base->ssr, ssr);
0187 
0188     mutex_unlock(&dev->lock);
0189 }
0190 
0191 void zmii_detach(struct platform_device *ofdev, int input)
0192 {
0193     struct zmii_instance *dev = platform_get_drvdata(ofdev);
0194 
0195     BUG_ON(!dev || dev->users == 0);
0196 
0197     mutex_lock(&dev->lock);
0198 
0199     ZMII_DBG(dev, "detach(%d)" NL, input);
0200 
0201     /* Disable this input */
0202     out_be32(&dev->base->fer,
0203          in_be32(&dev->base->fer) & ~zmii_mode_mask(dev->mode, input));
0204 
0205     --dev->users;
0206 
0207     mutex_unlock(&dev->lock);
0208 }
0209 
0210 int zmii_get_regs_len(struct platform_device *ofdev)
0211 {
0212     return sizeof(struct emac_ethtool_regs_subhdr) +
0213         sizeof(struct zmii_regs);
0214 }
0215 
0216 void *zmii_dump_regs(struct platform_device *ofdev, void *buf)
0217 {
0218     struct zmii_instance *dev = platform_get_drvdata(ofdev);
0219     struct emac_ethtool_regs_subhdr *hdr = buf;
0220     struct zmii_regs *regs = (struct zmii_regs *)(hdr + 1);
0221 
0222     hdr->version = 0;
0223     hdr->index = 0; /* for now, are there chips with more than one
0224              * zmii ? if yes, then we'll add a cell_index
0225              * like we do for emac
0226              */
0227     memcpy_fromio(regs, dev->base, sizeof(struct zmii_regs));
0228     return regs + 1;
0229 }
0230 
0231 static int zmii_probe(struct platform_device *ofdev)
0232 {
0233     struct device_node *np = ofdev->dev.of_node;
0234     struct zmii_instance *dev;
0235     struct resource regs;
0236     int rc;
0237 
0238     rc = -ENOMEM;
0239     dev = kzalloc(sizeof(struct zmii_instance), GFP_KERNEL);
0240     if (dev == NULL)
0241         goto err_gone;
0242 
0243     mutex_init(&dev->lock);
0244     dev->ofdev = ofdev;
0245     dev->mode = PHY_INTERFACE_MODE_NA;
0246 
0247     rc = -ENXIO;
0248     if (of_address_to_resource(np, 0, &regs)) {
0249         printk(KERN_ERR "%pOF: Can't get registers address\n", np);
0250         goto err_free;
0251     }
0252 
0253     rc = -ENOMEM;
0254     dev->base = (struct zmii_regs __iomem *)ioremap(regs.start,
0255                         sizeof(struct zmii_regs));
0256     if (dev->base == NULL) {
0257         printk(KERN_ERR "%pOF: Can't map device registers!\n", np);
0258         goto err_free;
0259     }
0260 
0261     /* We may need FER value for autodetection later */
0262     dev->fer_save = in_be32(&dev->base->fer);
0263 
0264     /* Disable all inputs by default */
0265     out_be32(&dev->base->fer, 0);
0266 
0267     printk(KERN_INFO "ZMII %pOF initialized\n", ofdev->dev.of_node);
0268     wmb();
0269     platform_set_drvdata(ofdev, dev);
0270 
0271     return 0;
0272 
0273  err_free:
0274     kfree(dev);
0275  err_gone:
0276     return rc;
0277 }
0278 
0279 static int zmii_remove(struct platform_device *ofdev)
0280 {
0281     struct zmii_instance *dev = platform_get_drvdata(ofdev);
0282 
0283     WARN_ON(dev->users != 0);
0284 
0285     iounmap(dev->base);
0286     kfree(dev);
0287 
0288     return 0;
0289 }
0290 
0291 static const struct of_device_id zmii_match[] =
0292 {
0293     {
0294         .compatible = "ibm,zmii",
0295     },
0296     /* For backward compat with old DT */
0297     {
0298         .type       = "emac-zmii",
0299     },
0300     {},
0301 };
0302 
0303 static struct platform_driver zmii_driver = {
0304     .driver = {
0305         .name = "emac-zmii",
0306         .of_match_table = zmii_match,
0307     },
0308     .probe = zmii_probe,
0309     .remove = zmii_remove,
0310 };
0311 
0312 int __init zmii_init(void)
0313 {
0314     return platform_driver_register(&zmii_driver);
0315 }
0316 
0317 void zmii_exit(void)
0318 {
0319     platform_driver_unregister(&zmii_driver);
0320 }