David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/ibm_newemac/rgmii.c |
| 3 | * |
| 4 | * Driver for PowerPC 4xx on-chip ethernet controller, RGMII bridge support. |
| 5 | * |
Benjamin Herrenschmidt | 17cf803 | 2007-12-05 11:14:33 +1100 | [diff] [blame] | 6 | * Copyright 2007 Benjamin Herrenschmidt, IBM Corp. |
| 7 | * <benh@kernel.crashing.org> |
| 8 | * |
| 9 | * Based on the arch/ppc version of the driver: |
| 10 | * |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 11 | * Copyright (c) 2004, 2005 Zultys Technologies. |
| 12 | * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net> |
| 13 | * |
| 14 | * Based on original work by |
| 15 | * Matt Porter <mporter@kernel.crashing.org> |
| 16 | * Copyright 2004 MontaVista Software, Inc. |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or modify it |
| 19 | * under the terms of the GNU General Public License as published by the |
| 20 | * Free Software Foundation; either version 2 of the License, or (at your |
| 21 | * option) any later version. |
| 22 | * |
| 23 | */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 25 | #include <linux/kernel.h> |
| 26 | #include <linux/ethtool.h> |
| 27 | #include <asm/io.h> |
| 28 | |
| 29 | #include "emac.h" |
| 30 | #include "debug.h" |
| 31 | |
| 32 | // XXX FIXME: Axon seems to support a subset of the RGMII, we |
| 33 | // thus need to take that into account and possibly change some |
| 34 | // of the bit settings below that don't seem to quite match the |
| 35 | // AXON spec |
| 36 | |
| 37 | /* RGMIIx_FER */ |
| 38 | #define RGMII_FER_MASK(idx) (0x7 << ((idx) * 4)) |
| 39 | #define RGMII_FER_RTBI(idx) (0x4 << ((idx) * 4)) |
| 40 | #define RGMII_FER_RGMII(idx) (0x5 << ((idx) * 4)) |
| 41 | #define RGMII_FER_TBI(idx) (0x6 << ((idx) * 4)) |
| 42 | #define RGMII_FER_GMII(idx) (0x7 << ((idx) * 4)) |
Grant Erickson | 004ea68 | 2008-07-09 01:03:06 +1000 | [diff] [blame] | 43 | #define RGMII_FER_MII(idx) RGMII_FER_GMII(idx) |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 44 | |
| 45 | /* RGMIIx_SSR */ |
| 46 | #define RGMII_SSR_MASK(idx) (0x7 << ((idx) * 8)) |
| 47 | #define RGMII_SSR_100(idx) (0x2 << ((idx) * 8)) |
| 48 | #define RGMII_SSR_1000(idx) (0x4 << ((idx) * 8)) |
| 49 | |
| 50 | /* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */ |
| 51 | static inline int rgmii_valid_mode(int phy_mode) |
| 52 | { |
| 53 | return phy_mode == PHY_MODE_GMII || |
Grant Erickson | 004ea68 | 2008-07-09 01:03:06 +1000 | [diff] [blame] | 54 | phy_mode == PHY_MODE_MII || |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 55 | phy_mode == PHY_MODE_RGMII || |
| 56 | phy_mode == PHY_MODE_TBI || |
| 57 | phy_mode == PHY_MODE_RTBI; |
| 58 | } |
| 59 | |
| 60 | static inline const char *rgmii_mode_name(int mode) |
| 61 | { |
| 62 | switch (mode) { |
| 63 | case PHY_MODE_RGMII: |
| 64 | return "RGMII"; |
| 65 | case PHY_MODE_TBI: |
| 66 | return "TBI"; |
| 67 | case PHY_MODE_GMII: |
| 68 | return "GMII"; |
Grant Erickson | 004ea68 | 2008-07-09 01:03:06 +1000 | [diff] [blame] | 69 | case PHY_MODE_MII: |
| 70 | return "MII"; |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 71 | case PHY_MODE_RTBI: |
| 72 | return "RTBI"; |
| 73 | default: |
| 74 | BUG(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static inline u32 rgmii_mode_mask(int mode, int input) |
| 79 | { |
| 80 | switch (mode) { |
| 81 | case PHY_MODE_RGMII: |
| 82 | return RGMII_FER_RGMII(input); |
| 83 | case PHY_MODE_TBI: |
| 84 | return RGMII_FER_TBI(input); |
| 85 | case PHY_MODE_GMII: |
| 86 | return RGMII_FER_GMII(input); |
Grant Erickson | 004ea68 | 2008-07-09 01:03:06 +1000 | [diff] [blame] | 87 | case PHY_MODE_MII: |
| 88 | return RGMII_FER_MII(input); |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 89 | case PHY_MODE_RTBI: |
| 90 | return RGMII_FER_RTBI(input); |
| 91 | default: |
| 92 | BUG(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int __devinit rgmii_attach(struct of_device *ofdev, int input, int mode) |
| 97 | { |
| 98 | struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev); |
Al Viro | eb4d84f | 2007-10-14 19:36:10 +0100 | [diff] [blame] | 99 | struct rgmii_regs __iomem *p = dev->base; |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 100 | |
| 101 | RGMII_DBG(dev, "attach(%d)" NL, input); |
| 102 | |
| 103 | /* Check if we need to attach to a RGMII */ |
| 104 | if (input < 0 || !rgmii_valid_mode(mode)) { |
| 105 | printk(KERN_ERR "%s: unsupported settings !\n", |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 106 | ofdev->dev.of_node->full_name); |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 107 | return -ENODEV; |
| 108 | } |
| 109 | |
| 110 | mutex_lock(&dev->lock); |
| 111 | |
| 112 | /* Enable this input */ |
| 113 | out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input)); |
| 114 | |
| 115 | printk(KERN_NOTICE "%s: input %d in %s mode\n", |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 116 | ofdev->dev.of_node->full_name, input, rgmii_mode_name(mode)); |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 117 | |
| 118 | ++dev->users; |
| 119 | |
| 120 | mutex_unlock(&dev->lock); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | void rgmii_set_speed(struct of_device *ofdev, int input, int speed) |
| 126 | { |
| 127 | struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev); |
Al Viro | eb4d84f | 2007-10-14 19:36:10 +0100 | [diff] [blame] | 128 | struct rgmii_regs __iomem *p = dev->base; |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 129 | u32 ssr; |
| 130 | |
| 131 | mutex_lock(&dev->lock); |
| 132 | |
| 133 | ssr = in_be32(&p->ssr) & ~RGMII_SSR_MASK(input); |
| 134 | |
| 135 | RGMII_DBG(dev, "speed(%d, %d)" NL, input, speed); |
| 136 | |
| 137 | if (speed == SPEED_1000) |
| 138 | ssr |= RGMII_SSR_1000(input); |
| 139 | else if (speed == SPEED_100) |
| 140 | ssr |= RGMII_SSR_100(input); |
| 141 | |
| 142 | out_be32(&p->ssr, ssr); |
| 143 | |
| 144 | mutex_unlock(&dev->lock); |
| 145 | } |
| 146 | |
| 147 | void rgmii_get_mdio(struct of_device *ofdev, int input) |
| 148 | { |
| 149 | struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev); |
Al Viro | eb4d84f | 2007-10-14 19:36:10 +0100 | [diff] [blame] | 150 | struct rgmii_regs __iomem *p = dev->base; |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 151 | u32 fer; |
| 152 | |
| 153 | RGMII_DBG2(dev, "get_mdio(%d)" NL, input); |
| 154 | |
Benjamin Herrenschmidt | 1f57877 | 2007-12-05 11:14:28 +1100 | [diff] [blame] | 155 | if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO)) |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 156 | return; |
| 157 | |
| 158 | mutex_lock(&dev->lock); |
| 159 | |
| 160 | fer = in_be32(&p->fer); |
| 161 | fer |= 0x00080000u >> input; |
| 162 | out_be32(&p->fer, fer); |
| 163 | (void)in_be32(&p->fer); |
| 164 | |
| 165 | DBG2(dev, " fer = 0x%08x\n", fer); |
| 166 | } |
| 167 | |
| 168 | void rgmii_put_mdio(struct of_device *ofdev, int input) |
| 169 | { |
| 170 | struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev); |
Al Viro | eb4d84f | 2007-10-14 19:36:10 +0100 | [diff] [blame] | 171 | struct rgmii_regs __iomem *p = dev->base; |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 172 | u32 fer; |
| 173 | |
| 174 | RGMII_DBG2(dev, "put_mdio(%d)" NL, input); |
| 175 | |
Benjamin Herrenschmidt | 1f57877 | 2007-12-05 11:14:28 +1100 | [diff] [blame] | 176 | if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO)) |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 177 | return; |
| 178 | |
| 179 | fer = in_be32(&p->fer); |
| 180 | fer &= ~(0x00080000u >> input); |
| 181 | out_be32(&p->fer, fer); |
| 182 | (void)in_be32(&p->fer); |
| 183 | |
| 184 | DBG2(dev, " fer = 0x%08x\n", fer); |
| 185 | |
| 186 | mutex_unlock(&dev->lock); |
| 187 | } |
| 188 | |
Josh Boyer | 51d4a1c | 2008-04-22 10:46:43 +1000 | [diff] [blame] | 189 | void rgmii_detach(struct of_device *ofdev, int input) |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 190 | { |
| 191 | struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev); |
Julia Lawall | 0021195 | 2009-07-19 06:13:04 +0000 | [diff] [blame] | 192 | struct rgmii_regs __iomem *p; |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 193 | |
| 194 | BUG_ON(!dev || dev->users == 0); |
Julia Lawall | 0021195 | 2009-07-19 06:13:04 +0000 | [diff] [blame] | 195 | p = dev->base; |
| 196 | |
| 197 | mutex_lock(&dev->lock); |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 198 | |
| 199 | RGMII_DBG(dev, "detach(%d)" NL, input); |
| 200 | |
| 201 | /* Disable this input */ |
| 202 | out_be32(&p->fer, in_be32(&p->fer) & ~RGMII_FER_MASK(input)); |
| 203 | |
| 204 | --dev->users; |
| 205 | |
| 206 | mutex_unlock(&dev->lock); |
| 207 | } |
| 208 | |
| 209 | int rgmii_get_regs_len(struct of_device *ofdev) |
| 210 | { |
| 211 | return sizeof(struct emac_ethtool_regs_subhdr) + |
| 212 | sizeof(struct rgmii_regs); |
| 213 | } |
| 214 | |
| 215 | void *rgmii_dump_regs(struct of_device *ofdev, void *buf) |
| 216 | { |
| 217 | struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev); |
| 218 | struct emac_ethtool_regs_subhdr *hdr = buf; |
| 219 | struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1); |
| 220 | |
| 221 | hdr->version = 0; |
| 222 | hdr->index = 0; /* for now, are there chips with more than one |
| 223 | * rgmii ? if yes, then we'll add a cell_index |
| 224 | * like we do for emac |
| 225 | */ |
| 226 | memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs)); |
| 227 | return regs + 1; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | static int __devinit rgmii_probe(struct of_device *ofdev, |
| 232 | const struct of_device_id *match) |
| 233 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 234 | struct device_node *np = ofdev->dev.of_node; |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 235 | struct rgmii_instance *dev; |
| 236 | struct resource regs; |
| 237 | int rc; |
| 238 | |
| 239 | rc = -ENOMEM; |
| 240 | dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL); |
| 241 | if (dev == NULL) { |
| 242 | printk(KERN_ERR "%s: could not allocate RGMII device!\n", |
| 243 | np->full_name); |
| 244 | goto err_gone; |
| 245 | } |
| 246 | |
| 247 | mutex_init(&dev->lock); |
| 248 | dev->ofdev = ofdev; |
| 249 | |
| 250 | rc = -ENXIO; |
| 251 | if (of_address_to_resource(np, 0, ®s)) { |
| 252 | printk(KERN_ERR "%s: Can't get registers address\n", |
| 253 | np->full_name); |
| 254 | goto err_free; |
| 255 | } |
| 256 | |
| 257 | rc = -ENOMEM; |
Al Viro | eb4d84f | 2007-10-14 19:36:10 +0100 | [diff] [blame] | 258 | dev->base = (struct rgmii_regs __iomem *)ioremap(regs.start, |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 259 | sizeof(struct rgmii_regs)); |
| 260 | if (dev->base == NULL) { |
| 261 | printk(KERN_ERR "%s: Can't map device registers!\n", |
| 262 | np->full_name); |
| 263 | goto err_free; |
| 264 | } |
| 265 | |
Benjamin Herrenschmidt | 1f57877 | 2007-12-05 11:14:28 +1100 | [diff] [blame] | 266 | /* Check for RGMII flags */ |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 267 | if (of_get_property(ofdev->dev.of_node, "has-mdio", NULL)) |
Benjamin Herrenschmidt | 1f57877 | 2007-12-05 11:14:28 +1100 | [diff] [blame] | 268 | dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO; |
| 269 | |
| 270 | /* CAB lacks the right properties, fix this up */ |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 271 | if (of_device_is_compatible(ofdev->dev.of_node, "ibm,rgmii-axon")) |
Benjamin Herrenschmidt | 1f57877 | 2007-12-05 11:14:28 +1100 | [diff] [blame] | 272 | dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO; |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 273 | |
| 274 | DBG2(dev, " Boot FER = 0x%08x, SSR = 0x%08x\n", |
| 275 | in_be32(&dev->base->fer), in_be32(&dev->base->ssr)); |
| 276 | |
| 277 | /* Disable all inputs by default */ |
| 278 | out_be32(&dev->base->fer, 0); |
| 279 | |
| 280 | printk(KERN_INFO |
Benjamin Herrenschmidt | 1f57877 | 2007-12-05 11:14:28 +1100 | [diff] [blame] | 281 | "RGMII %s initialized with%s MDIO support\n", |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 282 | ofdev->dev.of_node->full_name, |
Benjamin Herrenschmidt | 1f57877 | 2007-12-05 11:14:28 +1100 | [diff] [blame] | 283 | (dev->flags & EMAC_RGMII_FLAG_HAS_MDIO) ? "" : "out"); |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 284 | |
| 285 | wmb(); |
| 286 | dev_set_drvdata(&ofdev->dev, dev); |
| 287 | |
| 288 | return 0; |
| 289 | |
| 290 | err_free: |
| 291 | kfree(dev); |
| 292 | err_gone: |
| 293 | return rc; |
| 294 | } |
| 295 | |
| 296 | static int __devexit rgmii_remove(struct of_device *ofdev) |
| 297 | { |
| 298 | struct rgmii_instance *dev = dev_get_drvdata(&ofdev->dev); |
| 299 | |
| 300 | dev_set_drvdata(&ofdev->dev, NULL); |
| 301 | |
| 302 | WARN_ON(dev->users != 0); |
| 303 | |
| 304 | iounmap(dev->base); |
| 305 | kfree(dev); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static struct of_device_id rgmii_match[] = |
| 311 | { |
| 312 | { |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 313 | .compatible = "ibm,rgmii", |
| 314 | }, |
| 315 | { |
| 316 | .type = "emac-rgmii", |
| 317 | }, |
| 318 | {}, |
| 319 | }; |
| 320 | |
| 321 | static struct of_platform_driver rgmii_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 322 | .driver = { |
| 323 | .name = "emac-rgmii", |
| 324 | .owner = THIS_MODULE, |
| 325 | .of_match_table = rgmii_match, |
| 326 | }, |
David Gibson | 1d3bb99 | 2007-08-23 13:56:01 +1000 | [diff] [blame] | 327 | .probe = rgmii_probe, |
| 328 | .remove = rgmii_remove, |
| 329 | }; |
| 330 | |
| 331 | int __init rgmii_init(void) |
| 332 | { |
| 333 | return of_register_platform_driver(&rgmii_driver); |
| 334 | } |
| 335 | |
| 336 | void rgmii_exit(void) |
| 337 | { |
| 338 | of_unregister_platform_driver(&rgmii_driver); |
| 339 | } |