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