fsl_pq_mdio.c 10.7 KB
Newer Older
1 2 3 4 5
/*
 * Freescale PowerQUICC Ethernet Driver -- MIIM bus implementation
 * Provides Bus interface for MIIM regs
 *
 * Author: Andy Fleming <afleming@freescale.com>
6
 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
7
 *
8
 * Copyright 2002-2004, 2008-2009 Freescale Semiconductor, Inc.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * Based on gianfar_mii.c and ucc_geth_mii.c (Li Yang, Kim Phillips)
 *
 * 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>
#include <linux/platform_device.h>
#include <linux/crc32.h>
#include <linux/mii.h>
#include <linux/phy.h>
#include <linux/of.h>
38
#include <linux/of_address.h>
39
#include <linux/of_mdio.h>
40 41 42 43 44 45 46 47 48 49
#include <linux/of_platform.h>

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

#include "gianfar.h"
#include "fsl_pq_mdio.h"

50 51 52 53 54
struct fsl_pq_mdio_priv {
	void __iomem *map;
	struct fsl_pq_mdio __iomem *regs;
};

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
/*
 * 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.
 */
int fsl_pq_local_mdio_write(struct fsl_pq_mdio __iomem *regs, int mii_id,
		int regnum, u16 value)
{
	/* Set the PHY address and the register address we want to write */
	out_be32(&regs->miimadd, (mii_id << 8) | regnum);

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

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

	return 0;
}

/*
 * 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, in turn, 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.
 */
int fsl_pq_local_mdio_read(struct fsl_pq_mdio __iomem *regs,
		int mii_id, int regnum)
{
	u16 value;

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

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

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

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

	return value;
}

112 113
static struct fsl_pq_mdio __iomem *fsl_pq_mdio_get_regs(struct mii_bus *bus)
{
114 115 116
	struct fsl_pq_mdio_priv *priv = bus->priv;

	return priv->regs;
117 118
}

119 120 121 122 123 124
/*
 * Write value to the PHY at mii_id at register regnum,
 * on the bus, waiting until the write is done before returning.
 */
int fsl_pq_mdio_write(struct mii_bus *bus, int mii_id, int regnum, u16 value)
{
125
	struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
126 127

	/* Write to the local MII regs */
128
	return fsl_pq_local_mdio_write(regs, mii_id, regnum, value);
129 130 131 132 133 134 135 136
}

/*
 * Read the bus for PHY at addr mii_id, register regnum, and
 * return the value.  Clears miimcom first.
 */
int fsl_pq_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
{
137
	struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
138 139

	/* Read the local MII regs */
140
	return fsl_pq_local_mdio_read(regs, mii_id, regnum);
141 142 143 144 145
}

/* Reset the MIIM registers, and wait for the bus to free */
static int fsl_pq_mdio_reset(struct mii_bus *bus)
{
146
	struct fsl_pq_mdio __iomem *regs = fsl_pq_mdio_get_regs(bus);
147
	int timeout = PHY_INIT_TIMEOUT;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

	mutex_lock(&bus->mdio_lock);

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

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

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

	mutex_unlock(&bus->mdio_lock);

163
	if (timeout < 0) {
164 165 166 167 168 169 170 171 172 173
		printk(KERN_ERR "%s: The MII Bus is stuck!\n",
				bus->name);
		return -EBUSY;
	}

	return 0;
}

void fsl_pq_mdio_bus_name(char *name, struct device_node *np)
{
174 175
	const u32 *addr;
	u64 taddr = OF_BAD_ADDR;
176

177 178 179
	addr = of_get_address(np, 0, NULL, NULL);
	if (addr)
		taddr = of_translate_address(np, addr);
180

181 182
	snprintf(name, MII_BUS_ID_SIZE, "%s@%llx", np->name,
		(unsigned long long)taddr);
183
}
184
EXPORT_SYMBOL_GPL(fsl_pq_mdio_bus_name);
185 186


187
static u32 __iomem *get_gfar_tbipa(struct fsl_pq_mdio __iomem *regs, struct device_node *np)
188
{
189
#if defined(CONFIG_GIANFAR) || defined(CONFIG_GIANFAR_MODULE)
190 191 192 193 194 195 196
	struct gfar __iomem *enet_regs;

	/*
	 * This is mildly evil, but so is our hardware for doing this.
	 * Also, we have to cast back to struct gfar because of
	 * definition weirdness done in gianfar.h.
	 */
197 198 199 200 201 202 203
	if(of_device_is_compatible(np, "fsl,gianfar-mdio") ||
		of_device_is_compatible(np, "fsl,gianfar-tbi") ||
		of_device_is_compatible(np, "gianfar")) {
		enet_regs = (struct gfar __iomem *)regs;
		return &enet_regs->tbipa;
	} else if (of_device_is_compatible(np, "fsl,etsec2-mdio") ||
			of_device_is_compatible(np, "fsl,etsec2-tbi")) {
204
		return of_iomap(np, 1);
205
	}
206
#endif
207 208
	return NULL;
}
209 210 211 212


static int get_ucc_id_for_range(u64 start, u64 end, u32 *ucc_id)
{
213
#if defined(CONFIG_UCC_GETH) || defined(CONFIG_UCC_GETH_MODULE)
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	struct device_node *np = NULL;
	int err = 0;

	for_each_compatible_node(np, NULL, "ucc_geth") {
		struct resource tempres;

		err = of_address_to_resource(np, 0, &tempres);
		if (err)
			continue;

		/* if our mdio regs fall within this UCC regs range */
		if ((start >= tempres.start) && (end <= tempres.end)) {
			/* Find the id of the UCC */
			const u32 *id;

			id = of_get_property(np, "cell-index", NULL);
			if (!id) {
				id = of_get_property(np, "device-id", NULL);
				if (!id)
					continue;
			}

			*ucc_id = *id;

			return 0;
		}
	}

	if (err)
		return err;
	else
		return -EINVAL;
246 247
#else
	return -ENODEV;
248
#endif
249
}
250

251
static int fsl_pq_mdio_probe(struct platform_device *ofdev)
252
{
253
	struct device_node *np = ofdev->dev.of_node;
254
	struct device_node *tbi;
255
	struct fsl_pq_mdio_priv *priv;
256
	struct fsl_pq_mdio __iomem *regs = NULL;
257
	void __iomem *map;
258 259 260
	u32 __iomem *tbipa;
	struct mii_bus *new_bus;
	int tbiaddr = -1;
261
	const u32 *addrp;
262
	u64 addr = 0, size = 0;
263
	int err;
264

265 266 267 268
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

269
	new_bus = mdiobus_alloc();
270 271
	if (!new_bus) {
		err = -ENOMEM;
272
		goto err_free_priv;
273
	}
274 275 276 277 278

	new_bus->name = "Freescale PowerQUICC MII Bus",
	new_bus->read = &fsl_pq_mdio_read,
	new_bus->write = &fsl_pq_mdio_write,
	new_bus->reset = &fsl_pq_mdio_reset,
279
	new_bus->priv = priv;
280 281
	fsl_pq_mdio_bus_name(new_bus->id, np);

282 283 284 285 286 287
	addrp = of_get_address(np, 0, &size, NULL);
	if (!addrp) {
		err = -EINVAL;
		goto err_free_bus;
	}

288
	/* Set the PHY base address */
289 290 291 292 293 294
	addr = of_translate_address(np, addrp);
	if (addr == OF_BAD_ADDR) {
		err = -EINVAL;
		goto err_free_bus;
	}

295 296
	map = ioremap(addr, size);
	if (!map) {
297 298 299
		err = -ENOMEM;
		goto err_free_bus;
	}
300
	priv->map = map;
301

302 303 304 305 306 307
	if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
			of_device_is_compatible(np, "fsl,gianfar-tbi") ||
			of_device_is_compatible(np, "fsl,ucc-mdio") ||
			of_device_is_compatible(np, "ucc_geth_phy"))
		map -= offsetof(struct fsl_pq_mdio, miimcfg);
	regs = map;
308
	priv->regs = regs;
309

310
	new_bus->irq = kcalloc(PHY_MAX_ADDR, sizeof(int), GFP_KERNEL);
311 312 313 314 315 316 317 318 319 320

	if (NULL == new_bus->irq) {
		err = -ENOMEM;
		goto err_unmap_regs;
	}

	new_bus->parent = &ofdev->dev;
	dev_set_drvdata(&ofdev->dev, new_bus);

	if (of_device_is_compatible(np, "fsl,gianfar-mdio") ||
321
			of_device_is_compatible(np, "fsl,gianfar-tbi") ||
322 323
			of_device_is_compatible(np, "fsl,etsec2-mdio") ||
			of_device_is_compatible(np, "fsl,etsec2-tbi") ||
324
			of_device_is_compatible(np, "gianfar")) {
325 326 327 328 329
		tbipa = get_gfar_tbipa(regs, np);
		if (!tbipa) {
			err = -EINVAL;
			goto err_free_irqs;
		}
330 331 332
	} else if (of_device_is_compatible(np, "fsl,ucc-mdio") ||
			of_device_is_compatible(np, "ucc_geth_phy")) {
		u32 id;
333
		static u32 mii_mng_master;
334 335 336 337 338 339

		tbipa = &regs->utbipar;

		if ((err = get_ucc_id_for_range(addr, addr + size, &id)))
			goto err_free_irqs;

340 341 342 343
		if (!mii_mng_master) {
			mii_mng_master = id;
			ucc_set_qe_mux_mii_mng(id - 1);
		}
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	} else {
		err = -ENODEV;
		goto err_free_irqs;
	}

	for_each_child_of_node(np, tbi) {
		if (!strncmp(tbi->type, "tbi-phy", 8))
			break;
	}

	if (tbi) {
		const u32 *prop = of_get_property(tbi, "reg", NULL);

		if (prop)
			tbiaddr = *prop;

360 361
		if (tbiaddr == -1) {
			err = -EBUSY;
362

363 364 365 366
			goto err_free_irqs;
		} else {
			out_be32(tbipa, tbiaddr);
		}
367 368
	}

369
	err = of_mdiobus_register(new_bus, np);
370 371 372 373 374 375 376 377 378 379 380
	if (err) {
		printk (KERN_ERR "%s: Cannot register as MDIO bus\n",
				new_bus->name);
		goto err_free_irqs;
	}

	return 0;

err_free_irqs:
	kfree(new_bus->irq);
err_unmap_regs:
381
	iounmap(priv->map);
382 383
err_free_bus:
	kfree(new_bus);
384 385
err_free_priv:
	kfree(priv);
386 387 388 389
	return err;
}


390
static int fsl_pq_mdio_remove(struct platform_device *ofdev)
391 392 393
{
	struct device *device = &ofdev->dev;
	struct mii_bus *bus = dev_get_drvdata(device);
394
	struct fsl_pq_mdio_priv *priv = bus->priv;
395 396 397 398 399

	mdiobus_unregister(bus);

	dev_set_drvdata(device, NULL);

400
	iounmap(priv->map);
401 402
	bus->priv = NULL;
	mdiobus_free(bus);
403
	kfree(priv);
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425

	return 0;
}

static struct of_device_id fsl_pq_mdio_match[] = {
	{
		.type = "mdio",
		.compatible = "ucc_geth_phy",
	},
	{
		.type = "mdio",
		.compatible = "gianfar",
	},
	{
		.compatible = "fsl,ucc-mdio",
	},
	{
		.compatible = "fsl,gianfar-tbi",
	},
	{
		.compatible = "fsl,gianfar-mdio",
	},
426 427 428 429 430 431
	{
		.compatible = "fsl,etsec2-tbi",
	},
	{
		.compatible = "fsl,etsec2-mdio",
	},
432 433
	{},
};
434
MODULE_DEVICE_TABLE(of, fsl_pq_mdio_match);
435

436
static struct platform_driver fsl_pq_mdio_driver = {
437 438 439 440 441
	.driver = {
		.name = "fsl-pq_mdio",
		.owner = THIS_MODULE,
		.of_match_table = fsl_pq_mdio_match,
	},
442 443 444 445 446 447
	.probe = fsl_pq_mdio_probe,
	.remove = fsl_pq_mdio_remove,
};

int __init fsl_pq_mdio_init(void)
{
448
	return platform_driver_register(&fsl_pq_mdio_driver);
449
}
450
module_init(fsl_pq_mdio_init);
451 452 453

void fsl_pq_mdio_exit(void)
{
454
	platform_driver_unregister(&fsl_pq_mdio_driver);
455 456
}
module_exit(fsl_pq_mdio_exit);
457
MODULE_LICENSE("GPL");