xgmac_mdio.c 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * QorIQ 10G MDIO Controller
 *
 * Copyright 2012 Freescale Semiconductor, Inc.
 *
 * Authors: Andy Fleming <afleming@freescale.com>
 *          Timur Tabi <timur@freescale.com>
 *
 * This file is licensed under the terms of the GNU General Public License
 * version 2.  This program is licensed "as is" without any warranty of any
 * kind, whether express or implied.
 */

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/phy.h>
#include <linux/mdio.h>
20
#include <linux/of_address.h>
21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <linux/of_platform.h>
#include <linux/of_mdio.h>

/* Number of microseconds to wait for a register to respond */
#define TIMEOUT	1000

struct tgec_mdio_controller {
	__be32	reserved[12];
	__be32	mdio_stat;	/* MDIO configuration and status */
	__be32	mdio_ctl;	/* MDIO control */
	__be32	mdio_data;	/* MDIO data */
	__be32	mdio_addr;	/* MDIO address */
} __packed;

35
#define MDIO_STAT_ENC		BIT(6)
36
#define MDIO_STAT_CLKDIV(x)	(((x>>1) & 0xff) << 8)
37 38
#define MDIO_STAT_BSY		BIT(0)
#define MDIO_STAT_RD_ER		BIT(1)
39 40
#define MDIO_CTL_DEV_ADDR(x) 	(x & 0x1f)
#define MDIO_CTL_PORT_ADDR(x)	((x & 0x1f) << 5)
41 42 43 44
#define MDIO_CTL_PRE_DIS	BIT(10)
#define MDIO_CTL_SCAN_EN	BIT(11)
#define MDIO_CTL_POST_INC	BIT(14)
#define MDIO_CTL_READ		BIT(15)
45 46

#define MDIO_DATA(x)		(x & 0xffff)
47
#define MDIO_DATA_BSY		BIT(31)
48 49

/*
M
Madalin Bucur 已提交
50
 * Wait until the MDIO bus is free
51 52 53 54 55 56 57 58
 */
static int xgmac_wait_until_free(struct device *dev,
				 struct tgec_mdio_controller __iomem *regs)
{
	uint32_t status;

	/* Wait till the bus is free */
	status = spin_event_timeout(
59
		!((ioread32be(&regs->mdio_stat)) & MDIO_STAT_BSY), TIMEOUT, 0);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
	if (!status) {
		dev_err(dev, "timeout waiting for bus to be free\n");
		return -ETIMEDOUT;
	}

	return 0;
}

/*
 * Wait till the MDIO read or write operation is complete
 */
static int xgmac_wait_until_done(struct device *dev,
				 struct tgec_mdio_controller __iomem *regs)
{
	uint32_t status;

	/* Wait till the MDIO write is complete */
	status = spin_event_timeout(
78
		!((ioread32be(&regs->mdio_data)) & MDIO_DATA_BSY), TIMEOUT, 0);
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	if (!status) {
		dev_err(dev, "timeout waiting for operation to complete\n");
		return -ETIMEDOUT;
	}

	return 0;
}

/*
 * Write value to the PHY for this device to the register at regnum,waiting
 * until the write is done before it returns.  All PHY configuration has to be
 * done through the TSEC1 MIIM regs.
 */
static int xgmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 value)
{
	struct tgec_mdio_controller __iomem *regs = bus->priv;
95 96
	uint16_t dev_addr;
	u32 mdio_ctl, mdio_stat;
97 98
	int ret;

99
	mdio_stat = ioread32be(&regs->mdio_stat);
100 101 102 103 104 105 106 107 108
	if (regnum & MII_ADDR_C45) {
		/* Clause 45 (ie 10G) */
		dev_addr = (regnum >> 16) & 0x1f;
		mdio_stat |= MDIO_STAT_ENC;
	} else {
		/* Clause 22 (ie 1G) */
		dev_addr = regnum & 0x1f;
		mdio_stat &= ~MDIO_STAT_ENC;
	}
109

110
	iowrite32be(mdio_stat, &regs->mdio_stat);
111 112 113 114 115

	ret = xgmac_wait_until_free(&bus->dev, regs);
	if (ret)
		return ret;

116 117
	/* Set the port and dev addr */
	mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
118
	iowrite32be(mdio_ctl, &regs->mdio_ctl);
119 120 121

	/* Set the register address */
	if (regnum & MII_ADDR_C45) {
122
		iowrite32be(regnum & 0xffff, &regs->mdio_addr);
123 124 125 126 127 128

		ret = xgmac_wait_until_free(&bus->dev, regs);
		if (ret)
			return ret;
	}

129
	/* Write the value to the register */
130
	iowrite32be(MDIO_DATA(value), &regs->mdio_data);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

	ret = xgmac_wait_until_done(&bus->dev, regs);
	if (ret)
		return ret;

	return 0;
}

/*
 * Reads from register regnum in the PHY for device dev, returning the value.
 * Clears miimcom first.  All PHY configuration has to be done through the
 * TSEC1 MIIM regs.
 */
static int xgmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
{
	struct tgec_mdio_controller __iomem *regs = bus->priv;
147 148
	uint16_t dev_addr;
	uint32_t mdio_stat;
149 150 151 152
	uint32_t mdio_ctl;
	uint16_t value;
	int ret;

153
	mdio_stat = ioread32be(&regs->mdio_stat);
154 155 156 157 158
	if (regnum & MII_ADDR_C45) {
		dev_addr = (regnum >> 16) & 0x1f;
		mdio_stat |= MDIO_STAT_ENC;
	} else {
		dev_addr = regnum & 0x1f;
S
Shaohui Xie 已提交
159
		mdio_stat &= ~MDIO_STAT_ENC;
160 161
	}

162
	iowrite32be(mdio_stat, &regs->mdio_stat);
163 164 165 166 167

	ret = xgmac_wait_until_free(&bus->dev, regs);
	if (ret)
		return ret;

168 169
	/* Set the Port and Device Addrs */
	mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
170
	iowrite32be(mdio_ctl, &regs->mdio_ctl);
171 172

	/* Set the register address */
173
	if (regnum & MII_ADDR_C45) {
174
		iowrite32be(regnum & 0xffff, &regs->mdio_addr);
175

176 177 178 179
		ret = xgmac_wait_until_free(&bus->dev, regs);
		if (ret)
			return ret;
	}
180 181

	/* Initiate the read */
182
	iowrite32be(mdio_ctl | MDIO_CTL_READ, &regs->mdio_ctl);
183 184 185 186 187 188

	ret = xgmac_wait_until_done(&bus->dev, regs);
	if (ret)
		return ret;

	/* Return all Fs if nothing was there */
189
	if (ioread32be(&regs->mdio_stat) & MDIO_STAT_RD_ER) {
190
		dev_err(&bus->dev,
191
			"Error while reading PHY%d reg at %d.%hhu\n",
192
			phy_id, dev_addr, regnum);
193 194 195
		return 0xffff;
	}

196
	value = ioread32be(&regs->mdio_data) & 0xffff;
197 198 199 200 201
	dev_dbg(&bus->dev, "read %04x\n", value);

	return value;
}

202
static int xgmac_mdio_probe(struct platform_device *pdev)
203 204 205 206 207 208 209 210 211 212 213 214
{
	struct device_node *np = pdev->dev.of_node;
	struct mii_bus *bus;
	struct resource res;
	int ret;

	ret = of_address_to_resource(np, 0, &res);
	if (ret) {
		dev_err(&pdev->dev, "could not obtain address\n");
		return ret;
	}

215
	bus = mdiobus_alloc();
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	if (!bus)
		return -ENOMEM;

	bus->name = "Freescale XGMAC MDIO Bus";
	bus->read = xgmac_mdio_read;
	bus->write = xgmac_mdio_write;
	bus->parent = &pdev->dev;
	snprintf(bus->id, MII_BUS_ID_SIZE, "%llx", (unsigned long long)res.start);

	/* Set the PHY base address */
	bus->priv = of_iomap(np, 0);
	if (!bus->priv) {
		ret = -ENOMEM;
		goto err_ioremap;
	}

	ret = of_mdiobus_register(bus, np);
	if (ret) {
		dev_err(&pdev->dev, "cannot register MDIO bus\n");
		goto err_registration;
	}

238
	platform_set_drvdata(pdev, bus);
239 240 241 242 243 244 245 246 247 248 249 250

	return 0;

err_registration:
	iounmap(bus->priv);

err_ioremap:
	mdiobus_free(bus);

	return ret;
}

251
static int xgmac_mdio_remove(struct platform_device *pdev)
252
{
253
	struct mii_bus *bus = platform_get_drvdata(pdev);
254 255 256 257 258 259 260 261 262 263 264 265

	mdiobus_unregister(bus);
	iounmap(bus->priv);
	mdiobus_free(bus);

	return 0;
}

static struct of_device_id xgmac_mdio_match[] = {
	{
		.compatible = "fsl,fman-xmdio",
	},
266 267 268
	{
		.compatible = "fsl,fman-memac-mdio",
	},
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	{},
};
MODULE_DEVICE_TABLE(of, xgmac_mdio_match);

static struct platform_driver xgmac_mdio_driver = {
	.driver = {
		.name = "fsl-fman_xmdio",
		.of_match_table = xgmac_mdio_match,
	},
	.probe = xgmac_mdio_probe,
	.remove = xgmac_mdio_remove,
};

module_platform_driver(xgmac_mdio_driver);

MODULE_DESCRIPTION("Freescale QorIQ 10G MDIO Controller");
MODULE_LICENSE("GPL v2");