treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / ethernet / ibm / emac / rgmii.c
blob242ef976fd15e6bf7a9ed9ffd4b87a2d4ccee694
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * drivers/net/ethernet/ibm/emac/rgmii.c
5 * Driver for PowerPC 4xx on-chip ethernet controller, RGMII bridge support.
7 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
8 * <benh@kernel.crashing.org>
10 * Based on the arch/ppc version of the driver:
12 * Copyright (c) 2004, 2005 Zultys Technologies.
13 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
15 * Based on original work by
16 * Matt Porter <mporter@kernel.crashing.org>
17 * Copyright 2004 MontaVista Software, Inc.
19 #include <linux/slab.h>
20 #include <linux/kernel.h>
21 #include <linux/ethtool.h>
22 #include <linux/of_address.h>
23 #include <asm/io.h>
25 #include "emac.h"
26 #include "debug.h"
28 // XXX FIXME: Axon seems to support a subset of the RGMII, we
29 // thus need to take that into account and possibly change some
30 // of the bit settings below that don't seem to quite match the
31 // AXON spec
33 /* RGMIIx_FER */
34 #define RGMII_FER_MASK(idx) (0x7 << ((idx) * 4))
35 #define RGMII_FER_RTBI(idx) (0x4 << ((idx) * 4))
36 #define RGMII_FER_RGMII(idx) (0x5 << ((idx) * 4))
37 #define RGMII_FER_TBI(idx) (0x6 << ((idx) * 4))
38 #define RGMII_FER_GMII(idx) (0x7 << ((idx) * 4))
39 #define RGMII_FER_MII(idx) RGMII_FER_GMII(idx)
41 /* RGMIIx_SSR */
42 #define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8))
43 #define RGMII_SSR_10(idx) (0x1 << ((idx) * 8))
44 #define RGMII_SSR_100(idx) (0x2 << ((idx) * 8))
45 #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8))
47 /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
48 static inline int rgmii_valid_mode(int phy_mode)
50 return phy_interface_mode_is_rgmii(phy_mode) ||
51 phy_mode == PHY_INTERFACE_MODE_GMII ||
52 phy_mode == PHY_INTERFACE_MODE_MII ||
53 phy_mode == PHY_INTERFACE_MODE_TBI ||
54 phy_mode == PHY_INTERFACE_MODE_RTBI;
57 static inline u32 rgmii_mode_mask(int mode, int input)
59 switch (mode) {
60 case PHY_INTERFACE_MODE_RGMII:
61 case PHY_INTERFACE_MODE_RGMII_ID:
62 case PHY_INTERFACE_MODE_RGMII_RXID:
63 case PHY_INTERFACE_MODE_RGMII_TXID:
64 return RGMII_FER_RGMII(input);
65 case PHY_INTERFACE_MODE_TBI:
66 return RGMII_FER_TBI(input);
67 case PHY_INTERFACE_MODE_GMII:
68 return RGMII_FER_GMII(input);
69 case PHY_INTERFACE_MODE_MII:
70 return RGMII_FER_MII(input);
71 case PHY_INTERFACE_MODE_RTBI:
72 return RGMII_FER_RTBI(input);
73 default:
74 BUG();
78 int rgmii_attach(struct platform_device *ofdev, int input, int mode)
80 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
81 struct rgmii_regs __iomem *p = dev->base;
83 RGMII_DBG(dev, "attach(%d)" NL, input);
85 /* Check if we need to attach to a RGMII */
86 if (input < 0 || !rgmii_valid_mode(mode)) {
87 printk(KERN_ERR "%pOF: unsupported settings !\n",
88 ofdev->dev.of_node);
89 return -ENODEV;
92 mutex_lock(&dev->lock);
94 /* Enable this input */
95 out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));
97 printk(KERN_NOTICE "%pOF: input %d in %s mode\n",
98 ofdev->dev.of_node, input, phy_modes(mode));
100 ++dev->users;
102 mutex_unlock(&dev->lock);
104 return 0;
107 void rgmii_set_speed(struct platform_device *ofdev, int input, int speed)
109 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
110 struct rgmii_regs __iomem *p = dev->base;
111 u32 ssr;
113 mutex_lock(&dev->lock);
115 ssr = in_be32(&p->ssr) & ~RGMII_SSR_MASK(input);
117 RGMII_DBG(dev, "speed(%d, %d)" NL, input, speed);
119 if (speed == SPEED_1000)
120 ssr |= RGMII_SSR_1000(input);
121 else if (speed == SPEED_100)
122 ssr |= RGMII_SSR_100(input);
123 else if (speed == SPEED_10)
124 ssr |= RGMII_SSR_10(input);
126 out_be32(&p->ssr, ssr);
128 mutex_unlock(&dev->lock);
131 void rgmii_get_mdio(struct platform_device *ofdev, int input)
133 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
134 struct rgmii_regs __iomem *p = dev->base;
135 u32 fer;
137 RGMII_DBG2(dev, "get_mdio(%d)" NL, input);
139 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
140 return;
142 mutex_lock(&dev->lock);
144 fer = in_be32(&p->fer);
145 fer |= 0x00080000u >> input;
146 out_be32(&p->fer, fer);
147 (void)in_be32(&p->fer);
149 DBG2(dev, " fer = 0x%08x\n", fer);
152 void rgmii_put_mdio(struct platform_device *ofdev, int input)
154 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
155 struct rgmii_regs __iomem *p = dev->base;
156 u32 fer;
158 RGMII_DBG2(dev, "put_mdio(%d)" NL, input);
160 if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
161 return;
163 fer = in_be32(&p->fer);
164 fer &= ~(0x00080000u >> input);
165 out_be32(&p->fer, fer);
166 (void)in_be32(&p->fer);
168 DBG2(dev, " fer = 0x%08x\n", fer);
170 mutex_unlock(&dev->lock);
173 void rgmii_detach(struct platform_device *ofdev, int input)
175 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
176 struct rgmii_regs __iomem *p;
178 BUG_ON(!dev || dev->users == 0);
179 p = dev->base;
181 mutex_lock(&dev->lock);
183 RGMII_DBG(dev, "detach(%d)" NL, input);
185 /* Disable this input */
186 out_be32(&p->fer, in_be32(&p->fer) & ~RGMII_FER_MASK(input));
188 --dev->users;
190 mutex_unlock(&dev->lock);
193 int rgmii_get_regs_len(struct platform_device *ofdev)
195 return sizeof(struct emac_ethtool_regs_subhdr) +
196 sizeof(struct rgmii_regs);
199 void *rgmii_dump_regs(struct platform_device *ofdev, void *buf)
201 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
202 struct emac_ethtool_regs_subhdr *hdr = buf;
203 struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);
205 hdr->version = 0;
206 hdr->index = 0; /* for now, are there chips with more than one
207 * rgmii ? if yes, then we'll add a cell_index
208 * like we do for emac
210 memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
211 return regs + 1;
215 static int rgmii_probe(struct platform_device *ofdev)
217 struct device_node *np = ofdev->dev.of_node;
218 struct rgmii_instance *dev;
219 struct resource regs;
220 int rc;
222 rc = -ENOMEM;
223 dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL);
224 if (dev == NULL)
225 goto err_gone;
227 mutex_init(&dev->lock);
228 dev->ofdev = ofdev;
230 rc = -ENXIO;
231 if (of_address_to_resource(np, 0, &regs)) {
232 printk(KERN_ERR "%pOF: Can't get registers address\n", np);
233 goto err_free;
236 rc = -ENOMEM;
237 dev->base = (struct rgmii_regs __iomem *)ioremap(regs.start,
238 sizeof(struct rgmii_regs));
239 if (dev->base == NULL) {
240 printk(KERN_ERR "%pOF: Can't map device registers!\n", np);
241 goto err_free;
244 /* Check for RGMII flags */
245 if (of_get_property(ofdev->dev.of_node, "has-mdio", NULL))
246 dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
248 /* CAB lacks the right properties, fix this up */
249 if (of_device_is_compatible(ofdev->dev.of_node, "ibm,rgmii-axon"))
250 dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
252 DBG2(dev, " Boot FER = 0x%08x, SSR = 0x%08x\n",
253 in_be32(&dev->base->fer), in_be32(&dev->base->ssr));
255 /* Disable all inputs by default */
256 out_be32(&dev->base->fer, 0);
258 printk(KERN_INFO
259 "RGMII %pOF initialized with%s MDIO support\n",
260 ofdev->dev.of_node,
261 (dev->flags & EMAC_RGMII_FLAG_HAS_MDIO) ? "" : "out");
263 wmb();
264 platform_set_drvdata(ofdev, dev);
266 return 0;
268 err_free:
269 kfree(dev);
270 err_gone:
271 return rc;
274 static int rgmii_remove(struct platform_device *ofdev)
276 struct rgmii_instance *dev = platform_get_drvdata(ofdev);
278 WARN_ON(dev->users != 0);
280 iounmap(dev->base);
281 kfree(dev);
283 return 0;
286 static const struct of_device_id rgmii_match[] =
289 .compatible = "ibm,rgmii",
292 .type = "emac-rgmii",
297 static struct platform_driver rgmii_driver = {
298 .driver = {
299 .name = "emac-rgmii",
300 .of_match_table = rgmii_match,
302 .probe = rgmii_probe,
303 .remove = rgmii_remove,
306 int __init rgmii_init(void)
308 return platform_driver_register(&rgmii_driver);
311 void rgmii_exit(void)
313 platform_driver_unregister(&rgmii_driver);