gianfar_mii.c 7.0 KB
Newer Older
1
/*
2 3 4 5 6 7
 * drivers/net/gianfar_mii.c
 *
 * Gianfar Ethernet Driver -- MIIM bus implementation
 * Provides Bus interface for MIIM regs
 *
 * Author: Andy Fleming
8
 * Maintainer: Kumar Gala
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Copyright (c) 2002-2004 Freescale Semiconductor, 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.
 *
 */

#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/unistd.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/module.h>
33
#include <linux/platform_device.h>
34 35 36 37 38 39 40 41 42 43 44
#include <linux/crc32.h>
#include <linux/mii.h>
#include <linux/phy.h>

#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>

#include "gianfar.h"
#include "gianfar_mii.h"

K
Kapil Juneja 已提交
45 46 47 48 49 50 51 52 53
/*
 * Write value to the PHY at mii_id at register regnum,
 * on the bus attached to the local interface, which may be different from the
 * generic mdio bus (tied to a single interface), waiting until the write is
 * done before returning. This is helpful in programming interfaces like
 * the TBI which control interfaces like onchip SERDES and are always tied to
 * the local mdio pins, which may not be the same as system mdio bus, used for
 * controlling the external PHYs, for example.
 */
A
Al Viro 已提交
54
int gfar_local_mdio_write(struct gfar_mii __iomem *regs, int mii_id,
K
Kapil Juneja 已提交
55
			  int regnum, u16 value)
56 57 58 59 60 61 62 63 64 65 66 67 68 69
{
	/* Set the PHY address and the register address we want to write */
	gfar_write(&regs->miimadd, (mii_id << 8) | regnum);

	/* Write out the value we want */
	gfar_write(&regs->miimcon, value);

	/* Wait for the transaction to finish */
	while (gfar_read(&regs->miimind) & MIIMIND_BUSY)
		cpu_relax();

	return 0;
}

K
Kapil Juneja 已提交
70 71 72 73 74 75 76 77 78 79
/*
 * Read the bus for PHY at addr mii_id, register regnum, and
 * return the value.  Clears miimcom first.  All PHY operation
 * done on the bus attached to the local interface,
 * which may be different from the generic mdio bus
 * This is helpful in programming interfaces like
 * the TBI which, inturn, control interfaces like onchip SERDES
 * and are always tied to the local mdio pins, which may not be the
 * same as system mdio bus, used for controlling the external PHYs, for eg.
 */
A
Al Viro 已提交
80
int gfar_local_mdio_read(struct gfar_mii __iomem *regs, int mii_id, int regnum)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
{
	u16 value;

	/* Set the PHY address and the register address we want to read */
	gfar_write(&regs->miimadd, (mii_id << 8) | regnum);

	/* Clear miimcom, and then initiate a read */
	gfar_write(&regs->miimcom, 0);
	gfar_write(&regs->miimcom, MII_READ_COMMAND);

	/* Wait for the transaction to finish */
	while (gfar_read(&regs->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
		cpu_relax();

	/* Grab the value of the register from miimstat */
	value = gfar_read(&regs->miimstat);

	return value;
}

K
Kapil Juneja 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/* Write value to the PHY at mii_id at register regnum,
 * on the bus, waiting until the write is done before returning.
 * All PHY configuration is done through the TSEC1 MIIM regs */
int gfar_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
{
	struct gfar_mii __iomem *regs = (void __iomem *)bus->priv;

	/* Write to the local MII regs */
	return(gfar_local_mdio_write(regs, mii_id, regnum, value));
}

/* Read the bus for PHY at addr mii_id, register regnum, and
 * return the value.  Clears miimcom first.  All PHY
 * configuration has to be done through the TSEC1 MIIM regs */
int gfar_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
{
	struct gfar_mii __iomem *regs = (void __iomem *)bus->priv;

	/* Read the local MII regs */
	return(gfar_local_mdio_read(regs, mii_id, regnum));
}
122 123

/* Reset the MIIM registers, and wait for the bus to free */
124
static int gfar_mdio_reset(struct mii_bus *bus)
125
{
126
	struct gfar_mii __iomem *regs = (void __iomem *)bus->priv;
127 128
	unsigned int timeout = PHY_INIT_TIMEOUT;

129
	mutex_lock(&bus->mdio_lock);
130 131 132 133 134 135 136 137 138

	/* Reset the management interface */
	gfar_write(&regs->miimcfg, MIIMCFG_RESET);

	/* Setup the MII Mgmt clock speed */
	gfar_write(&regs->miimcfg, MIIMCFG_INIT_VALUE);

	/* Wait until the bus is free */
	while ((gfar_read(&regs->miimind) & MIIMIND_BUSY) &&
139
			--timeout)
140 141
		cpu_relax();

142
	mutex_unlock(&bus->mdio_lock);
143

144
	if(timeout == 0) {
145 146 147 148 149 150 151 152 153
		printk(KERN_ERR "%s: The MII Bus is stuck!\n",
				bus->name);
		return -EBUSY;
	}

	return 0;
}


154
static int gfar_mdio_probe(struct device *dev)
155 156 157
{
	struct platform_device *pdev = to_platform_device(dev);
	struct gianfar_mdio_data *pdata;
158
	struct gfar_mii __iomem *regs;
159
	struct gfar __iomem *enet_regs;
160
	struct mii_bus *new_bus;
161
	struct resource *r;
162
	int i, err = 0;
163 164 165 166

	if (NULL == dev)
		return -EINVAL;

167
	new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL);
168 169 170 171 172 173 174 175

	if (NULL == new_bus)
		return -ENOMEM;

	new_bus->name = "Gianfar MII Bus",
	new_bus->read = &gfar_mdio_read,
	new_bus->write = &gfar_mdio_write,
	new_bus->reset = &gfar_mdio_reset,
176
	snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
177 178 179 180 181 182 183 184

	pdata = (struct gianfar_mdio_data *)pdev->dev.platform_data;

	if (NULL == pdata) {
		printk(KERN_ERR "gfar mdio %d: Missing platform data!\n", pdev->id);
		return -ENODEV;
	}

185 186
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);

187
	/* Set the PHY base address */
188
	regs = ioremap(r->start, sizeof (struct gfar_mii));
189 190 191 192 193 194

	if (NULL == regs) {
		err = -ENOMEM;
		goto reg_map_fail;
	}

195
	new_bus->priv = (void __force *)regs;
196 197 198 199 200 201

	new_bus->irq = pdata->irq;

	new_bus->dev = dev;
	dev_set_drvdata(dev, new_bus);

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	/*
	 * This is mildly evil, but so is our hardware for doing this.
	 * Also, we have to cast back to struct gfar_mii because of
	 * definition weirdness done in gianfar.h.
	 */
	enet_regs = (struct gfar __iomem *)
		((char *)regs - offsetof(struct gfar, gfar_mii_regs));

	/* Scan the bus, looking for an empty spot for TBIPA */
	gfar_write(&enet_regs->tbipa, 0);
	for (i = PHY_MAX_ADDR; i > 0; i--) {
		u32 phy_id;
		int r;

		r = get_phy_id(new_bus, i, &phy_id);
		if (r)
			return r;

		if (phy_id == 0xffffffff)
			break;
	}

	/* The bus is full.  We don't support using 31 PHYs, sorry */
	if (i == 0)
		return -EBUSY;

	gfar_write(&enet_regs->tbipa, i);

230 231 232
	err = mdiobus_register(new_bus);

	if (0 != err) {
233
		printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
234 235 236 237 238 239 240
				new_bus->name);
		goto bus_register_fail;
	}

	return 0;

bus_register_fail:
241
	iounmap(regs);
242 243 244 245 246 247 248
reg_map_fail:
	kfree(new_bus);

	return err;
}


249
static int gfar_mdio_remove(struct device *dev)
250 251 252 253 254 255 256
{
	struct mii_bus *bus = dev_get_drvdata(dev);

	mdiobus_unregister(bus);

	dev_set_drvdata(dev, NULL);

257
	iounmap((void __iomem *)bus->priv);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	bus->priv = NULL;
	kfree(bus);

	return 0;
}

static struct device_driver gianfar_mdio_driver = {
	.name = "fsl-gianfar_mdio",
	.bus = &platform_bus_type,
	.probe = gfar_mdio_probe,
	.remove = gfar_mdio_remove,
};

int __init gfar_mdio_init(void)
{
	return driver_register(&gianfar_mdio_driver);
}

K
Kumar Gala 已提交
276
void gfar_mdio_exit(void)
277 278 279
{
	driver_unregister(&gianfar_mdio_driver);
}