xref: /linux/drivers/net/ethernet/ibm/emac/rgmii.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29aa32835SJeff Kirsher /*
33396c782SPaul Gortmaker  * drivers/net/ethernet/ibm/emac/rgmii.c
49aa32835SJeff Kirsher  *
59aa32835SJeff Kirsher  * Driver for PowerPC 4xx on-chip ethernet controller, RGMII bridge support.
69aa32835SJeff Kirsher  *
79aa32835SJeff Kirsher  * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
89aa32835SJeff Kirsher  *                <benh@kernel.crashing.org>
99aa32835SJeff Kirsher  *
109aa32835SJeff Kirsher  * Based on the arch/ppc version of the driver:
119aa32835SJeff Kirsher  *
129aa32835SJeff Kirsher  * Copyright (c) 2004, 2005 Zultys Technologies.
139aa32835SJeff Kirsher  * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
149aa32835SJeff Kirsher  *
159aa32835SJeff Kirsher  * Based on original work by
169aa32835SJeff Kirsher  * 	Matt Porter <mporter@kernel.crashing.org>
179aa32835SJeff Kirsher  * 	Copyright 2004 MontaVista Software, Inc.
189aa32835SJeff Kirsher  */
199aa32835SJeff Kirsher #include <linux/slab.h>
209aa32835SJeff Kirsher #include <linux/kernel.h>
219aa32835SJeff Kirsher #include <linux/ethtool.h>
223d40aed8SRob Herring #include <linux/of.h>
235af50730SRob Herring #include <linux/of_address.h>
243d40aed8SRob Herring #include <linux/platform_device.h>
259aa32835SJeff Kirsher #include <asm/io.h>
269aa32835SJeff Kirsher 
279aa32835SJeff Kirsher #include "emac.h"
289aa32835SJeff Kirsher #include "debug.h"
299aa32835SJeff Kirsher 
309aa32835SJeff Kirsher // XXX FIXME: Axon seems to support a subset of the RGMII, we
319aa32835SJeff Kirsher // thus need to take that into account and possibly change some
329aa32835SJeff Kirsher // of the bit settings below that don't seem to quite match the
339aa32835SJeff Kirsher // AXON spec
349aa32835SJeff Kirsher 
359aa32835SJeff Kirsher /* RGMIIx_FER */
369aa32835SJeff Kirsher #define RGMII_FER_MASK(idx)	(0x7 << ((idx) * 4))
379aa32835SJeff Kirsher #define RGMII_FER_RTBI(idx)	(0x4 << ((idx) * 4))
389aa32835SJeff Kirsher #define RGMII_FER_RGMII(idx)	(0x5 << ((idx) * 4))
399aa32835SJeff Kirsher #define RGMII_FER_TBI(idx)	(0x6 << ((idx) * 4))
409aa32835SJeff Kirsher #define RGMII_FER_GMII(idx)	(0x7 << ((idx) * 4))
419aa32835SJeff Kirsher #define RGMII_FER_MII(idx)	RGMII_FER_GMII(idx)
429aa32835SJeff Kirsher 
439aa32835SJeff Kirsher /* RGMIIx_SSR */
449aa32835SJeff Kirsher #define RGMII_SSR_MASK(idx)	(0x7 << ((idx) * 8))
45faacd3afSIvan Mikhaylov #define RGMII_SSR_10(idx)	(0x1 << ((idx) * 8))
469aa32835SJeff Kirsher #define RGMII_SSR_100(idx)	(0x2 << ((idx) * 8))
479aa32835SJeff Kirsher #define RGMII_SSR_1000(idx)	(0x4 << ((idx) * 8))
489aa32835SJeff Kirsher 
499aa32835SJeff Kirsher /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
rgmii_valid_mode(int phy_mode)509aa32835SJeff Kirsher static inline int rgmii_valid_mode(int phy_mode)
519aa32835SJeff Kirsher {
5229635762SChristian Lamparter 	return  phy_interface_mode_is_rgmii(phy_mode) ||
5329635762SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_GMII ||
5478b69921SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_MII ||
5578b69921SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_TBI ||
5678b69921SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_RTBI;
579aa32835SJeff Kirsher }
589aa32835SJeff Kirsher 
rgmii_mode_mask(int mode,int input)599aa32835SJeff Kirsher static inline u32 rgmii_mode_mask(int mode, int input)
609aa32835SJeff Kirsher {
619aa32835SJeff Kirsher 	switch (mode) {
6278b69921SChristian Lamparter 	case PHY_INTERFACE_MODE_RGMII:
6329635762SChristian Lamparter 	case PHY_INTERFACE_MODE_RGMII_ID:
6429635762SChristian Lamparter 	case PHY_INTERFACE_MODE_RGMII_RXID:
6529635762SChristian Lamparter 	case PHY_INTERFACE_MODE_RGMII_TXID:
669aa32835SJeff Kirsher 		return RGMII_FER_RGMII(input);
6778b69921SChristian Lamparter 	case PHY_INTERFACE_MODE_TBI:
689aa32835SJeff Kirsher 		return RGMII_FER_TBI(input);
6978b69921SChristian Lamparter 	case PHY_INTERFACE_MODE_GMII:
709aa32835SJeff Kirsher 		return RGMII_FER_GMII(input);
7178b69921SChristian Lamparter 	case PHY_INTERFACE_MODE_MII:
729aa32835SJeff Kirsher 		return RGMII_FER_MII(input);
7378b69921SChristian Lamparter 	case PHY_INTERFACE_MODE_RTBI:
749aa32835SJeff Kirsher 		return RGMII_FER_RTBI(input);
759aa32835SJeff Kirsher 	default:
769aa32835SJeff Kirsher 		BUG();
779aa32835SJeff Kirsher 	}
789aa32835SJeff Kirsher }
799aa32835SJeff Kirsher 
rgmii_attach(struct platform_device * ofdev,int input,int mode)80fe17dc1eSBill Pemberton int rgmii_attach(struct platform_device *ofdev, int input, int mode)
819aa32835SJeff Kirsher {
828513fbd8SJingoo Han 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
839aa32835SJeff Kirsher 	struct rgmii_regs __iomem *p = dev->base;
849aa32835SJeff Kirsher 
859aa32835SJeff Kirsher 	RGMII_DBG(dev, "attach(%d)" NL, input);
869aa32835SJeff Kirsher 
879aa32835SJeff Kirsher 	/* Check if we need to attach to a RGMII */
889aa32835SJeff Kirsher 	if (input < 0 || !rgmii_valid_mode(mode)) {
89f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: unsupported settings !\n",
90f7ce9103SRob Herring 		       ofdev->dev.of_node);
919aa32835SJeff Kirsher 		return -ENODEV;
929aa32835SJeff Kirsher 	}
939aa32835SJeff Kirsher 
949aa32835SJeff Kirsher 	mutex_lock(&dev->lock);
959aa32835SJeff Kirsher 
969aa32835SJeff Kirsher 	/* Enable this input */
979aa32835SJeff Kirsher 	out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));
989aa32835SJeff Kirsher 
99f7ce9103SRob Herring 	printk(KERN_NOTICE "%pOF: input %d in %s mode\n",
10049dd19bfSChristian Lamparter 	       ofdev->dev.of_node, input, phy_modes(mode));
1019aa32835SJeff Kirsher 
1029aa32835SJeff Kirsher 	++dev->users;
1039aa32835SJeff Kirsher 
1049aa32835SJeff Kirsher 	mutex_unlock(&dev->lock);
1059aa32835SJeff Kirsher 
1069aa32835SJeff Kirsher 	return 0;
1079aa32835SJeff Kirsher }
1089aa32835SJeff Kirsher 
rgmii_set_speed(struct platform_device * ofdev,int input,int speed)1099aa32835SJeff Kirsher void rgmii_set_speed(struct platform_device *ofdev, int input, int speed)
1109aa32835SJeff Kirsher {
1118513fbd8SJingoo Han 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
1129aa32835SJeff Kirsher 	struct rgmii_regs __iomem *p = dev->base;
1139aa32835SJeff Kirsher 	u32 ssr;
1149aa32835SJeff Kirsher 
1159aa32835SJeff Kirsher 	mutex_lock(&dev->lock);
1169aa32835SJeff Kirsher 
1179aa32835SJeff Kirsher 	ssr = in_be32(&p->ssr) & ~RGMII_SSR_MASK(input);
1189aa32835SJeff Kirsher 
1199aa32835SJeff Kirsher 	RGMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
1209aa32835SJeff Kirsher 
1219aa32835SJeff Kirsher 	if (speed == SPEED_1000)
1229aa32835SJeff Kirsher 		ssr |= RGMII_SSR_1000(input);
1239aa32835SJeff Kirsher 	else if (speed == SPEED_100)
1249aa32835SJeff Kirsher 		ssr |= RGMII_SSR_100(input);
125faacd3afSIvan Mikhaylov 	else if (speed == SPEED_10)
126faacd3afSIvan Mikhaylov 		ssr |= RGMII_SSR_10(input);
1279aa32835SJeff Kirsher 
1289aa32835SJeff Kirsher 	out_be32(&p->ssr, ssr);
1299aa32835SJeff Kirsher 
1309aa32835SJeff Kirsher 	mutex_unlock(&dev->lock);
1319aa32835SJeff Kirsher }
1329aa32835SJeff Kirsher 
rgmii_get_mdio(struct platform_device * ofdev,int input)1339aa32835SJeff Kirsher void rgmii_get_mdio(struct platform_device *ofdev, int input)
1349aa32835SJeff Kirsher {
1358513fbd8SJingoo Han 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
1369aa32835SJeff Kirsher 	struct rgmii_regs __iomem *p = dev->base;
1379aa32835SJeff Kirsher 	u32 fer;
1389aa32835SJeff Kirsher 
1399aa32835SJeff Kirsher 	RGMII_DBG2(dev, "get_mdio(%d)" NL, input);
1409aa32835SJeff Kirsher 
1419aa32835SJeff Kirsher 	if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
1429aa32835SJeff Kirsher 		return;
1439aa32835SJeff Kirsher 
1449aa32835SJeff Kirsher 	mutex_lock(&dev->lock);
1459aa32835SJeff Kirsher 
1469aa32835SJeff Kirsher 	fer = in_be32(&p->fer);
1479aa32835SJeff Kirsher 	fer |= 0x00080000u >> input;
1489aa32835SJeff Kirsher 	out_be32(&p->fer, fer);
1499aa32835SJeff Kirsher 	(void)in_be32(&p->fer);
1509aa32835SJeff Kirsher 
1519aa32835SJeff Kirsher 	DBG2(dev, " fer = 0x%08x\n", fer);
1529aa32835SJeff Kirsher }
1539aa32835SJeff Kirsher 
rgmii_put_mdio(struct platform_device * ofdev,int input)1549aa32835SJeff Kirsher void rgmii_put_mdio(struct platform_device *ofdev, int input)
1559aa32835SJeff Kirsher {
1568513fbd8SJingoo Han 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
1579aa32835SJeff Kirsher 	struct rgmii_regs __iomem *p = dev->base;
1589aa32835SJeff Kirsher 	u32 fer;
1599aa32835SJeff Kirsher 
1609aa32835SJeff Kirsher 	RGMII_DBG2(dev, "put_mdio(%d)" NL, input);
1619aa32835SJeff Kirsher 
1629aa32835SJeff Kirsher 	if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
1639aa32835SJeff Kirsher 		return;
1649aa32835SJeff Kirsher 
1659aa32835SJeff Kirsher 	fer = in_be32(&p->fer);
1669aa32835SJeff Kirsher 	fer &= ~(0x00080000u >> input);
1679aa32835SJeff Kirsher 	out_be32(&p->fer, fer);
1689aa32835SJeff Kirsher 	(void)in_be32(&p->fer);
1699aa32835SJeff Kirsher 
1709aa32835SJeff Kirsher 	DBG2(dev, " fer = 0x%08x\n", fer);
1719aa32835SJeff Kirsher 
1729aa32835SJeff Kirsher 	mutex_unlock(&dev->lock);
1739aa32835SJeff Kirsher }
1749aa32835SJeff Kirsher 
rgmii_detach(struct platform_device * ofdev,int input)1759aa32835SJeff Kirsher void rgmii_detach(struct platform_device *ofdev, int input)
1769aa32835SJeff Kirsher {
1778513fbd8SJingoo Han 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
1789aa32835SJeff Kirsher 	struct rgmii_regs __iomem *p;
1799aa32835SJeff Kirsher 
1809aa32835SJeff Kirsher 	BUG_ON(!dev || dev->users == 0);
1819aa32835SJeff Kirsher 	p = dev->base;
1829aa32835SJeff Kirsher 
1839aa32835SJeff Kirsher 	mutex_lock(&dev->lock);
1849aa32835SJeff Kirsher 
1859aa32835SJeff Kirsher 	RGMII_DBG(dev, "detach(%d)" NL, input);
1869aa32835SJeff Kirsher 
1879aa32835SJeff Kirsher 	/* Disable this input */
1889aa32835SJeff Kirsher 	out_be32(&p->fer, in_be32(&p->fer) & ~RGMII_FER_MASK(input));
1899aa32835SJeff Kirsher 
1909aa32835SJeff Kirsher 	--dev->users;
1919aa32835SJeff Kirsher 
1929aa32835SJeff Kirsher 	mutex_unlock(&dev->lock);
1939aa32835SJeff Kirsher }
1949aa32835SJeff Kirsher 
rgmii_get_regs_len(struct platform_device * ofdev)1959aa32835SJeff Kirsher int rgmii_get_regs_len(struct platform_device *ofdev)
1969aa32835SJeff Kirsher {
1979aa32835SJeff Kirsher 	return sizeof(struct emac_ethtool_regs_subhdr) +
1989aa32835SJeff Kirsher 		sizeof(struct rgmii_regs);
1999aa32835SJeff Kirsher }
2009aa32835SJeff Kirsher 
rgmii_dump_regs(struct platform_device * ofdev,void * buf)2019aa32835SJeff Kirsher void *rgmii_dump_regs(struct platform_device *ofdev, void *buf)
2029aa32835SJeff Kirsher {
2038513fbd8SJingoo Han 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
2049aa32835SJeff Kirsher 	struct emac_ethtool_regs_subhdr *hdr = buf;
2059aa32835SJeff Kirsher 	struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);
2069aa32835SJeff Kirsher 
2079aa32835SJeff Kirsher 	hdr->version = 0;
2089aa32835SJeff Kirsher 	hdr->index = 0; /* for now, are there chips with more than one
2099aa32835SJeff Kirsher 			 * rgmii ? if yes, then we'll add a cell_index
2109aa32835SJeff Kirsher 			 * like we do for emac
2119aa32835SJeff Kirsher 			 */
2129aa32835SJeff Kirsher 	memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
2139aa32835SJeff Kirsher 	return regs + 1;
2149aa32835SJeff Kirsher }
2159aa32835SJeff Kirsher 
2169aa32835SJeff Kirsher 
rgmii_probe(struct platform_device * ofdev)217fe17dc1eSBill Pemberton static int rgmii_probe(struct platform_device *ofdev)
2189aa32835SJeff Kirsher {
2199aa32835SJeff Kirsher 	struct device_node *np = ofdev->dev.of_node;
2209aa32835SJeff Kirsher 	struct rgmii_instance *dev;
2219aa32835SJeff Kirsher 	struct resource regs;
2229aa32835SJeff Kirsher 	int rc;
2239aa32835SJeff Kirsher 
2249aa32835SJeff Kirsher 	rc = -ENOMEM;
2259aa32835SJeff Kirsher 	dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL);
226e404decbSJoe Perches 	if (dev == NULL)
2279aa32835SJeff Kirsher 		goto err_gone;
2289aa32835SJeff Kirsher 
2299aa32835SJeff Kirsher 	mutex_init(&dev->lock);
2309aa32835SJeff Kirsher 	dev->ofdev = ofdev;
2319aa32835SJeff Kirsher 
2329aa32835SJeff Kirsher 	rc = -ENXIO;
2339aa32835SJeff Kirsher 	if (of_address_to_resource(np, 0, &regs)) {
234f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: Can't get registers address\n", np);
2359aa32835SJeff Kirsher 		goto err_free;
2369aa32835SJeff Kirsher 	}
2379aa32835SJeff Kirsher 
2389aa32835SJeff Kirsher 	rc = -ENOMEM;
2399aa32835SJeff Kirsher 	dev->base = (struct rgmii_regs __iomem *)ioremap(regs.start,
2409aa32835SJeff Kirsher 						 sizeof(struct rgmii_regs));
2419aa32835SJeff Kirsher 	if (dev->base == NULL) {
242f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: Can't map device registers!\n", np);
2439aa32835SJeff Kirsher 		goto err_free;
2449aa32835SJeff Kirsher 	}
2459aa32835SJeff Kirsher 
2469aa32835SJeff Kirsher 	/* Check for RGMII flags */
2471a87e641SRob Herring 	if (of_property_read_bool(ofdev->dev.of_node, "has-mdio"))
2489aa32835SJeff Kirsher 		dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
2499aa32835SJeff Kirsher 
2509aa32835SJeff Kirsher 	/* CAB lacks the right properties, fix this up */
2519aa32835SJeff Kirsher 	if (of_device_is_compatible(ofdev->dev.of_node, "ibm,rgmii-axon"))
2529aa32835SJeff Kirsher 		dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
2539aa32835SJeff Kirsher 
2549aa32835SJeff Kirsher 	DBG2(dev, " Boot FER = 0x%08x, SSR = 0x%08x\n",
2559aa32835SJeff Kirsher 	     in_be32(&dev->base->fer), in_be32(&dev->base->ssr));
2569aa32835SJeff Kirsher 
2579aa32835SJeff Kirsher 	/* Disable all inputs by default */
2589aa32835SJeff Kirsher 	out_be32(&dev->base->fer, 0);
2599aa32835SJeff Kirsher 
2609aa32835SJeff Kirsher 	printk(KERN_INFO
261f7ce9103SRob Herring 	       "RGMII %pOF initialized with%s MDIO support\n",
262f7ce9103SRob Herring 	       ofdev->dev.of_node,
2639aa32835SJeff Kirsher 	       (dev->flags & EMAC_RGMII_FLAG_HAS_MDIO) ? "" : "out");
2649aa32835SJeff Kirsher 
2659aa32835SJeff Kirsher 	wmb();
2668513fbd8SJingoo Han 	platform_set_drvdata(ofdev, dev);
2679aa32835SJeff Kirsher 
2689aa32835SJeff Kirsher 	return 0;
2699aa32835SJeff Kirsher 
2709aa32835SJeff Kirsher  err_free:
2719aa32835SJeff Kirsher 	kfree(dev);
2729aa32835SJeff Kirsher  err_gone:
2739aa32835SJeff Kirsher 	return rc;
2749aa32835SJeff Kirsher }
2759aa32835SJeff Kirsher 
rgmii_remove(struct platform_device * ofdev)276*ac35a3c4SUwe Kleine-König static void rgmii_remove(struct platform_device *ofdev)
2779aa32835SJeff Kirsher {
2788513fbd8SJingoo Han 	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
2799aa32835SJeff Kirsher 
2809aa32835SJeff Kirsher 	WARN_ON(dev->users != 0);
2819aa32835SJeff Kirsher 
2829aa32835SJeff Kirsher 	iounmap(dev->base);
2839aa32835SJeff Kirsher 	kfree(dev);
2849aa32835SJeff Kirsher }
2859aa32835SJeff Kirsher 
28647b61667SFabian Frederick static const struct of_device_id rgmii_match[] =
2879aa32835SJeff Kirsher {
2889aa32835SJeff Kirsher 	{
2899aa32835SJeff Kirsher 		.compatible	= "ibm,rgmii",
2909aa32835SJeff Kirsher 	},
2919aa32835SJeff Kirsher 	{
2929aa32835SJeff Kirsher 		.type		= "emac-rgmii",
2939aa32835SJeff Kirsher 	},
2949aa32835SJeff Kirsher 	{},
2959aa32835SJeff Kirsher };
2969aa32835SJeff Kirsher 
2979aa32835SJeff Kirsher static struct platform_driver rgmii_driver = {
2989aa32835SJeff Kirsher 	.driver = {
2999aa32835SJeff Kirsher 		.name = "emac-rgmii",
3009aa32835SJeff Kirsher 		.of_match_table = rgmii_match,
3019aa32835SJeff Kirsher 	},
3029aa32835SJeff Kirsher 	.probe = rgmii_probe,
303*ac35a3c4SUwe Kleine-König 	.remove_new = rgmii_remove,
3049aa32835SJeff Kirsher };
3059aa32835SJeff Kirsher 
rgmii_init(void)3069aa32835SJeff Kirsher int __init rgmii_init(void)
3079aa32835SJeff Kirsher {
3089aa32835SJeff Kirsher 	return platform_driver_register(&rgmii_driver);
3099aa32835SJeff Kirsher }
3109aa32835SJeff Kirsher 
rgmii_exit(void)3119aa32835SJeff Kirsher void rgmii_exit(void)
3129aa32835SJeff Kirsher {
3139aa32835SJeff Kirsher 	platform_driver_unregister(&rgmii_driver);
3149aa32835SJeff Kirsher }
315