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