rgmii.c 7.4 KB
Newer Older
D
David Gibson 已提交
1
/*
2
 * drivers/net/ethernet/ibm/emac/rgmii.c
D
David Gibson 已提交
3 4 5
 *
 * Driver for PowerPC 4xx on-chip ethernet controller, RGMII bridge support.
 *
6 7 8 9 10
 * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
 *                <benh@kernel.crashing.org>
 *
 * Based on the arch/ppc version of the driver:
 *
D
David Gibson 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23
 * Copyright (c) 2004, 2005 Zultys Technologies.
 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
 *
 * Based on original work by
 * 	Matt Porter <mporter@kernel.crashing.org>
 * 	Copyright 2004 MontaVista Software, Inc.
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 */
24
#include <linux/slab.h>
D
David Gibson 已提交
25 26
#include <linux/kernel.h>
#include <linux/ethtool.h>
27
#include <linux/of_address.h>
D
David Gibson 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <asm/io.h>

#include "emac.h"
#include "debug.h"

// XXX FIXME: Axon seems to support a subset of the RGMII, we
// thus need to take that into account and possibly change some
// of the bit settings below that don't seem to quite match the
// AXON spec

/* RGMIIx_FER */
#define RGMII_FER_MASK(idx)	(0x7 << ((idx) * 4))
#define RGMII_FER_RTBI(idx)	(0x4 << ((idx) * 4))
#define RGMII_FER_RGMII(idx)	(0x5 << ((idx) * 4))
#define RGMII_FER_TBI(idx)	(0x6 << ((idx) * 4))
#define RGMII_FER_GMII(idx)	(0x7 << ((idx) * 4))
44
#define RGMII_FER_MII(idx)	RGMII_FER_GMII(idx)
D
David Gibson 已提交
45 46 47

/* RGMIIx_SSR */
#define RGMII_SSR_MASK(idx)	(0x7 << ((idx) * 8))
48
#define RGMII_SSR_10(idx)	(0x1 << ((idx) * 8))
D
David Gibson 已提交
49 50 51 52 53 54 55
#define RGMII_SSR_100(idx)	(0x2 << ((idx) * 8))
#define RGMII_SSR_1000(idx)	(0x4 << ((idx) * 8))

/* RGMII bridge supports only GMII/TBI and RGMII/RTBI PHYs */
static inline int rgmii_valid_mode(int phy_mode)
{
	return  phy_mode == PHY_MODE_GMII ||
56
		phy_mode == PHY_MODE_MII ||
D
David Gibson 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70
		phy_mode == PHY_MODE_RGMII ||
		phy_mode == PHY_MODE_TBI ||
		phy_mode == PHY_MODE_RTBI;
}

static inline u32 rgmii_mode_mask(int mode, int input)
{
	switch (mode) {
	case PHY_MODE_RGMII:
		return RGMII_FER_RGMII(input);
	case PHY_MODE_TBI:
		return RGMII_FER_TBI(input);
	case PHY_MODE_GMII:
		return RGMII_FER_GMII(input);
71 72
	case PHY_MODE_MII:
		return RGMII_FER_MII(input);
D
David Gibson 已提交
73 74 75 76 77 78 79
	case PHY_MODE_RTBI:
		return RGMII_FER_RTBI(input);
	default:
		BUG();
	}
}

80
int rgmii_attach(struct platform_device *ofdev, int input, int mode)
D
David Gibson 已提交
81
{
82
	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
83
	struct rgmii_regs __iomem *p = dev->base;
D
David Gibson 已提交
84 85 86 87 88

	RGMII_DBG(dev, "attach(%d)" NL, input);

	/* Check if we need to attach to a RGMII */
	if (input < 0 || !rgmii_valid_mode(mode)) {
89 90
		printk(KERN_ERR "%pOF: unsupported settings !\n",
		       ofdev->dev.of_node);
D
David Gibson 已提交
91 92 93 94 95 96 97 98
		return -ENODEV;
	}

	mutex_lock(&dev->lock);

	/* Enable this input */
	out_be32(&p->fer, in_be32(&p->fer) | rgmii_mode_mask(mode, input));

99
	printk(KERN_NOTICE "%pOF: input %d in %s mode\n",
100
	       ofdev->dev.of_node, input, phy_modes(mode));
D
David Gibson 已提交
101 102 103 104 105 106 107 108

	++dev->users;

	mutex_unlock(&dev->lock);

	return 0;
}

109
void rgmii_set_speed(struct platform_device *ofdev, int input, int speed)
D
David Gibson 已提交
110
{
111
	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
112
	struct rgmii_regs __iomem *p = dev->base;
D
David Gibson 已提交
113 114 115 116 117 118 119 120 121 122 123 124
	u32 ssr;

	mutex_lock(&dev->lock);

	ssr = in_be32(&p->ssr) & ~RGMII_SSR_MASK(input);

	RGMII_DBG(dev, "speed(%d, %d)" NL, input, speed);

	if (speed == SPEED_1000)
		ssr |= RGMII_SSR_1000(input);
	else if (speed == SPEED_100)
		ssr |= RGMII_SSR_100(input);
125 126
	else if (speed == SPEED_10)
		ssr |= RGMII_SSR_10(input);
D
David Gibson 已提交
127 128 129 130 131 132

	out_be32(&p->ssr, ssr);

	mutex_unlock(&dev->lock);
}

133
void rgmii_get_mdio(struct platform_device *ofdev, int input)
D
David Gibson 已提交
134
{
135
	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
136
	struct rgmii_regs __iomem *p = dev->base;
D
David Gibson 已提交
137 138 139 140
	u32 fer;

	RGMII_DBG2(dev, "get_mdio(%d)" NL, input);

141
	if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
D
David Gibson 已提交
142 143 144 145 146 147 148 149 150 151 152 153
		return;

	mutex_lock(&dev->lock);

	fer = in_be32(&p->fer);
	fer |= 0x00080000u >> input;
	out_be32(&p->fer, fer);
	(void)in_be32(&p->fer);

	DBG2(dev, " fer = 0x%08x\n", fer);
}

154
void rgmii_put_mdio(struct platform_device *ofdev, int input)
D
David Gibson 已提交
155
{
156
	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
157
	struct rgmii_regs __iomem *p = dev->base;
D
David Gibson 已提交
158 159 160 161
	u32 fer;

	RGMII_DBG2(dev, "put_mdio(%d)" NL, input);

162
	if (!(dev->flags & EMAC_RGMII_FLAG_HAS_MDIO))
D
David Gibson 已提交
163 164 165 166 167 168 169 170 171 172 173 174
		return;

	fer = in_be32(&p->fer);
	fer &= ~(0x00080000u >> input);
	out_be32(&p->fer, fer);
	(void)in_be32(&p->fer);

	DBG2(dev, " fer = 0x%08x\n", fer);

	mutex_unlock(&dev->lock);
}

175
void rgmii_detach(struct platform_device *ofdev, int input)
D
David Gibson 已提交
176
{
177
	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
178
	struct rgmii_regs __iomem *p;
D
David Gibson 已提交
179 180

	BUG_ON(!dev || dev->users == 0);
181 182 183
	p = dev->base;

	mutex_lock(&dev->lock);
D
David Gibson 已提交
184 185 186 187 188 189 190 191 192 193 194

	RGMII_DBG(dev, "detach(%d)" NL, input);

	/* Disable this input */
	out_be32(&p->fer, in_be32(&p->fer) & ~RGMII_FER_MASK(input));

	--dev->users;

	mutex_unlock(&dev->lock);
}

195
int rgmii_get_regs_len(struct platform_device *ofdev)
D
David Gibson 已提交
196 197 198 199 200
{
	return sizeof(struct emac_ethtool_regs_subhdr) +
		sizeof(struct rgmii_regs);
}

201
void *rgmii_dump_regs(struct platform_device *ofdev, void *buf)
D
David Gibson 已提交
202
{
203
	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
D
David Gibson 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216
	struct emac_ethtool_regs_subhdr *hdr = buf;
	struct rgmii_regs *regs = (struct rgmii_regs *)(hdr + 1);

	hdr->version = 0;
	hdr->index = 0; /* for now, are there chips with more than one
			 * rgmii ? if yes, then we'll add a cell_index
			 * like we do for emac
			 */
	memcpy_fromio(regs, dev->base, sizeof(struct rgmii_regs));
	return regs + 1;
}


217
static int rgmii_probe(struct platform_device *ofdev)
D
David Gibson 已提交
218
{
219
	struct device_node *np = ofdev->dev.of_node;
D
David Gibson 已提交
220 221 222 223 224 225
	struct rgmii_instance *dev;
	struct resource regs;
	int rc;

	rc = -ENOMEM;
	dev = kzalloc(sizeof(struct rgmii_instance), GFP_KERNEL);
226
	if (dev == NULL)
D
David Gibson 已提交
227 228 229 230 231 232 233
		goto err_gone;

	mutex_init(&dev->lock);
	dev->ofdev = ofdev;

	rc = -ENXIO;
	if (of_address_to_resource(np, 0, &regs)) {
234
		printk(KERN_ERR "%pOF: Can't get registers address\n", np);
D
David Gibson 已提交
235 236 237 238
		goto err_free;
	}

	rc = -ENOMEM;
239
	dev->base = (struct rgmii_regs __iomem *)ioremap(regs.start,
D
David Gibson 已提交
240 241
						 sizeof(struct rgmii_regs));
	if (dev->base == NULL) {
242
		printk(KERN_ERR "%pOF: Can't map device registers!\n", np);
D
David Gibson 已提交
243 244 245
		goto err_free;
	}

246
	/* Check for RGMII flags */
247
	if (of_get_property(ofdev->dev.of_node, "has-mdio", NULL))
248 249 250
		dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;

	/* CAB lacks the right properties, fix this up */
251
	if (of_device_is_compatible(ofdev->dev.of_node, "ibm,rgmii-axon"))
252
		dev->flags |= EMAC_RGMII_FLAG_HAS_MDIO;
D
David Gibson 已提交
253 254 255 256 257 258 259 260

	DBG2(dev, " Boot FER = 0x%08x, SSR = 0x%08x\n",
	     in_be32(&dev->base->fer), in_be32(&dev->base->ssr));

	/* Disable all inputs by default */
	out_be32(&dev->base->fer, 0);

	printk(KERN_INFO
261 262
	       "RGMII %pOF initialized with%s MDIO support\n",
	       ofdev->dev.of_node,
263
	       (dev->flags & EMAC_RGMII_FLAG_HAS_MDIO) ? "" : "out");
D
David Gibson 已提交
264 265

	wmb();
266
	platform_set_drvdata(ofdev, dev);
D
David Gibson 已提交
267 268 269 270 271 272 273 274 275

	return 0;

 err_free:
	kfree(dev);
 err_gone:
	return rc;
}

276
static int rgmii_remove(struct platform_device *ofdev)
D
David Gibson 已提交
277
{
278
	struct rgmii_instance *dev = platform_get_drvdata(ofdev);
D
David Gibson 已提交
279 280 281 282 283 284 285 286 287

	WARN_ON(dev->users != 0);

	iounmap(dev->base);
	kfree(dev);

	return 0;
}

288
static const struct of_device_id rgmii_match[] =
D
David Gibson 已提交
289 290 291 292 293 294 295 296 297 298
{
	{
		.compatible	= "ibm,rgmii",
	},
	{
		.type		= "emac-rgmii",
	},
	{},
};

299
static struct platform_driver rgmii_driver = {
300 301 302 303
	.driver = {
		.name = "emac-rgmii",
		.of_match_table = rgmii_match,
	},
D
David Gibson 已提交
304 305 306 307 308 309
	.probe = rgmii_probe,
	.remove = rgmii_remove,
};

int __init rgmii_init(void)
{
310
	return platform_driver_register(&rgmii_driver);
D
David Gibson 已提交
311 312 313 314
}

void rgmii_exit(void)
{
315
	platform_driver_unregister(&rgmii_driver);
D
David Gibson 已提交
316
}