Linux Audio

Check our new training course

Loading...
v6.8
  1/*
  2 * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver
  3 *
  4 * Copyright (C) 2007  Domen Puncer, Telargo, Inc.
  5 * Copyright (C) 2008  Wolfram Sang, Pengutronix
  6 *
  7 * This file is licensed under the terms of the GNU General Public License
  8 * version 2. This program is licensed "as is" without any warranty of any
  9 * kind, whether express or implied.
 10 */
 11
 12#include <linux/kernel.h>
 13#include <linux/module.h>
 14#include <linux/netdevice.h>
 15#include <linux/phy.h>
 
 16#include <linux/slab.h>
 17#include <linux/of.h>
 18#include <linux/of_address.h>
 19#include <linux/of_mdio.h>
 20#include <linux/platform_device.h>
 21#include <asm/io.h>
 22#include <asm/mpc52xx.h>
 23#include "fec_mpc52xx.h"
 24
 25struct mpc52xx_fec_mdio_priv {
 26	struct mpc52xx_fec __iomem *regs;
 
 27};
 28
 29static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
 30		int reg, u32 value)
 31{
 32	struct mpc52xx_fec_mdio_priv *priv = bus->priv;
 33	struct mpc52xx_fec __iomem *fec = priv->regs;
 34	int tries = 3;
 35
 36	value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
 37	value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
 38
 39	out_be32(&fec->ievent, FEC_IEVENT_MII);
 40	out_be32(&fec->mii_data, value);
 41
 42	/* wait for it to finish, this takes about 23 us on lite5200b */
 43	while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
 44		msleep(1);
 45
 46	if (!tries)
 47		return -ETIMEDOUT;
 48
 49	return value & FEC_MII_DATA_OP_RD ?
 50		in_be32(&fec->mii_data) & FEC_MII_DATA_DATAMSK : 0;
 51}
 52
 53static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
 54{
 55	return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
 56}
 57
 58static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
 59		u16 data)
 60{
 61	return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
 62		data | FEC_MII_WRITE_FRAME);
 63}
 64
 65static int mpc52xx_fec_mdio_probe(struct platform_device *of)
 66{
 67	struct device *dev = &of->dev;
 68	struct device_node *np = of->dev.of_node;
 69	struct mii_bus *bus;
 70	struct mpc52xx_fec_mdio_priv *priv;
 71	struct resource res;
 72	int err;
 73
 74	bus = mdiobus_alloc();
 75	if (bus == NULL)
 76		return -ENOMEM;
 77	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
 78	if (priv == NULL) {
 79		err = -ENOMEM;
 80		goto out_free;
 81	}
 82
 83	bus->name = "mpc52xx MII bus";
 84	bus->read = mpc52xx_fec_mdio_read;
 85	bus->write = mpc52xx_fec_mdio_write;
 86
 
 
 
 87	/* setup registers */
 88	err = of_address_to_resource(np, 0, &res);
 89	if (err)
 90		goto out_free;
 91	priv->regs = ioremap(res.start, resource_size(&res));
 92	if (priv->regs == NULL) {
 93		err = -ENOMEM;
 94		goto out_free;
 95	}
 96
 97	snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
 98	bus->priv = priv;
 99
100	bus->parent = dev;
101	dev_set_drvdata(dev, bus);
102
103	/* set MII speed */
104	out_be32(&priv->regs->mii_speed, ((mpc5xxx_get_bus_frequency(dev) >> 20) / 5) << 1);
 
105
106	err = of_mdiobus_register(bus, np);
107	if (err)
108		goto out_unmap;
109
110	return 0;
111
112 out_unmap:
113	iounmap(priv->regs);
114 out_free:
115	kfree(priv);
116	mdiobus_free(bus);
117
118	return err;
119}
120
121static void mpc52xx_fec_mdio_remove(struct platform_device *of)
122{
123	struct mii_bus *bus = platform_get_drvdata(of);
124	struct mpc52xx_fec_mdio_priv *priv = bus->priv;
125
126	mdiobus_unregister(bus);
127	iounmap(priv->regs);
128	kfree(priv);
129	mdiobus_free(bus);
 
 
130}
131
132static const struct of_device_id mpc52xx_fec_mdio_match[] = {
133	{ .compatible = "fsl,mpc5200b-mdio", },
134	{ .compatible = "fsl,mpc5200-mdio", },
135	{ .compatible = "mpc5200b-fec-phy", },
136	{}
137};
138MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
139
140struct platform_driver mpc52xx_fec_mdio_driver = {
141	.driver = {
142		.name = "mpc5200b-fec-phy",
143		.owner = THIS_MODULE,
144		.of_match_table = mpc52xx_fec_mdio_match,
145	},
146	.probe = mpc52xx_fec_mdio_probe,
147	.remove_new = mpc52xx_fec_mdio_remove,
148};
149
150/* let fec driver call it, since this has to be registered before it */
151EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
152
153MODULE_LICENSE("Dual BSD/GPL");
v3.15
  1/*
  2 * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver
  3 *
  4 * Copyright (C) 2007  Domen Puncer, Telargo, Inc.
  5 * Copyright (C) 2008  Wolfram Sang, Pengutronix
  6 *
  7 * This file is licensed under the terms of the GNU General Public License
  8 * version 2. This program is licensed "as is" without any warranty of any
  9 * kind, whether express or implied.
 10 */
 11
 12#include <linux/kernel.h>
 13#include <linux/module.h>
 14#include <linux/netdevice.h>
 15#include <linux/phy.h>
 16#include <linux/of_platform.h>
 17#include <linux/slab.h>
 
 
 18#include <linux/of_mdio.h>
 
 19#include <asm/io.h>
 20#include <asm/mpc52xx.h>
 21#include "fec_mpc52xx.h"
 22
 23struct mpc52xx_fec_mdio_priv {
 24	struct mpc52xx_fec __iomem *regs;
 25	int mdio_irqs[PHY_MAX_ADDR];
 26};
 27
 28static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
 29		int reg, u32 value)
 30{
 31	struct mpc52xx_fec_mdio_priv *priv = bus->priv;
 32	struct mpc52xx_fec __iomem *fec = priv->regs;
 33	int tries = 3;
 34
 35	value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
 36	value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
 37
 38	out_be32(&fec->ievent, FEC_IEVENT_MII);
 39	out_be32(&fec->mii_data, value);
 40
 41	/* wait for it to finish, this takes about 23 us on lite5200b */
 42	while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
 43		msleep(1);
 44
 45	if (!tries)
 46		return -ETIMEDOUT;
 47
 48	return value & FEC_MII_DATA_OP_RD ?
 49		in_be32(&fec->mii_data) & FEC_MII_DATA_DATAMSK : 0;
 50}
 51
 52static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
 53{
 54	return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
 55}
 56
 57static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
 58		u16 data)
 59{
 60	return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
 61		data | FEC_MII_WRITE_FRAME);
 62}
 63
 64static int mpc52xx_fec_mdio_probe(struct platform_device *of)
 65{
 66	struct device *dev = &of->dev;
 67	struct device_node *np = of->dev.of_node;
 68	struct mii_bus *bus;
 69	struct mpc52xx_fec_mdio_priv *priv;
 70	struct resource res;
 71	int err;
 72
 73	bus = mdiobus_alloc();
 74	if (bus == NULL)
 75		return -ENOMEM;
 76	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
 77	if (priv == NULL) {
 78		err = -ENOMEM;
 79		goto out_free;
 80	}
 81
 82	bus->name = "mpc52xx MII bus";
 83	bus->read = mpc52xx_fec_mdio_read;
 84	bus->write = mpc52xx_fec_mdio_write;
 85
 86	/* setup irqs */
 87	bus->irq = priv->mdio_irqs;
 88
 89	/* setup registers */
 90	err = of_address_to_resource(np, 0, &res);
 91	if (err)
 92		goto out_free;
 93	priv->regs = ioremap(res.start, resource_size(&res));
 94	if (priv->regs == NULL) {
 95		err = -ENOMEM;
 96		goto out_free;
 97	}
 98
 99	snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
100	bus->priv = priv;
101
102	bus->parent = dev;
103	dev_set_drvdata(dev, bus);
104
105	/* set MII speed */
106	out_be32(&priv->regs->mii_speed,
107		((mpc5xxx_get_bus_frequency(of->dev.of_node) >> 20) / 5) << 1);
108
109	err = of_mdiobus_register(bus, np);
110	if (err)
111		goto out_unmap;
112
113	return 0;
114
115 out_unmap:
116	iounmap(priv->regs);
117 out_free:
118	kfree(priv);
119	mdiobus_free(bus);
120
121	return err;
122}
123
124static int mpc52xx_fec_mdio_remove(struct platform_device *of)
125{
126	struct mii_bus *bus = platform_get_drvdata(of);
127	struct mpc52xx_fec_mdio_priv *priv = bus->priv;
128
129	mdiobus_unregister(bus);
130	iounmap(priv->regs);
131	kfree(priv);
132	mdiobus_free(bus);
133
134	return 0;
135}
136
137static struct of_device_id mpc52xx_fec_mdio_match[] = {
138	{ .compatible = "fsl,mpc5200b-mdio", },
139	{ .compatible = "fsl,mpc5200-mdio", },
140	{ .compatible = "mpc5200b-fec-phy", },
141	{}
142};
143MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
144
145struct platform_driver mpc52xx_fec_mdio_driver = {
146	.driver = {
147		.name = "mpc5200b-fec-phy",
148		.owner = THIS_MODULE,
149		.of_match_table = mpc52xx_fec_mdio_match,
150	},
151	.probe = mpc52xx_fec_mdio_probe,
152	.remove = mpc52xx_fec_mdio_remove,
153};
154
155/* let fec driver call it, since this has to be registered before it */
156EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
157
158MODULE_LICENSE("Dual BSD/GPL");