• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 struct mpc52xx_fec_mdio_priv {
24 	struct mpc52xx_fec __iomem *regs;
25 };
26 
mpc52xx_fec_mdio_transfer(struct mii_bus * bus,int phy_id,int reg,u32 value)27 static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id,
28 		int reg, u32 value)
29 {
30 	struct mpc52xx_fec_mdio_priv *priv = bus->priv;
31 	struct mpc52xx_fec __iomem *fec = priv->regs;
32 	int tries = 3;
33 
34 	value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK;
35 	value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK;
36 
37 	out_be32(&fec->ievent, FEC_IEVENT_MII);
38 	out_be32(&fec->mii_data, value);
39 
40 	/* wait for it to finish, this takes about 23 us on lite5200b */
41 	while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries)
42 		msleep(1);
43 
44 	if (!tries)
45 		return -ETIMEDOUT;
46 
47 	return value & FEC_MII_DATA_OP_RD ?
48 		in_be32(&fec->mii_data) & FEC_MII_DATA_DATAMSK : 0;
49 }
50 
mpc52xx_fec_mdio_read(struct mii_bus * bus,int phy_id,int reg)51 static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg)
52 {
53 	return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME);
54 }
55 
mpc52xx_fec_mdio_write(struct mii_bus * bus,int phy_id,int reg,u16 data)56 static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg,
57 		u16 data)
58 {
59 	return mpc52xx_fec_mdio_transfer(bus, phy_id, reg,
60 		data | FEC_MII_WRITE_FRAME);
61 }
62 
mpc52xx_fec_mdio_probe(struct platform_device * of)63 static int mpc52xx_fec_mdio_probe(struct platform_device *of)
64 {
65 	struct device *dev = &of->dev;
66 	struct device_node *np = of->dev.of_node;
67 	struct mii_bus *bus;
68 	struct mpc52xx_fec_mdio_priv *priv;
69 	struct resource res;
70 	int err;
71 
72 	bus = mdiobus_alloc();
73 	if (bus == NULL)
74 		return -ENOMEM;
75 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
76 	if (priv == NULL) {
77 		err = -ENOMEM;
78 		goto out_free;
79 	}
80 
81 	bus->name = "mpc52xx MII bus";
82 	bus->read = mpc52xx_fec_mdio_read;
83 	bus->write = mpc52xx_fec_mdio_write;
84 
85 	/* setup registers */
86 	err = of_address_to_resource(np, 0, &res);
87 	if (err)
88 		goto out_free;
89 	priv->regs = ioremap(res.start, resource_size(&res));
90 	if (priv->regs == NULL) {
91 		err = -ENOMEM;
92 		goto out_free;
93 	}
94 
95 	snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
96 	bus->priv = priv;
97 
98 	bus->parent = dev;
99 	dev_set_drvdata(dev, bus);
100 
101 	/* set MII speed */
102 	out_be32(&priv->regs->mii_speed,
103 		((mpc5xxx_get_bus_frequency(of->dev.of_node) >> 20) / 5) << 1);
104 
105 	err = of_mdiobus_register(bus, np);
106 	if (err)
107 		goto out_unmap;
108 
109 	return 0;
110 
111  out_unmap:
112 	iounmap(priv->regs);
113  out_free:
114 	kfree(priv);
115 	mdiobus_free(bus);
116 
117 	return err;
118 }
119 
mpc52xx_fec_mdio_remove(struct platform_device * of)120 static int mpc52xx_fec_mdio_remove(struct platform_device *of)
121 {
122 	struct mii_bus *bus = platform_get_drvdata(of);
123 	struct mpc52xx_fec_mdio_priv *priv = bus->priv;
124 
125 	mdiobus_unregister(bus);
126 	iounmap(priv->regs);
127 	kfree(priv);
128 	mdiobus_free(bus);
129 
130 	return 0;
131 }
132 
133 static const struct of_device_id mpc52xx_fec_mdio_match[] = {
134 	{ .compatible = "fsl,mpc5200b-mdio", },
135 	{ .compatible = "fsl,mpc5200-mdio", },
136 	{ .compatible = "mpc5200b-fec-phy", },
137 	{}
138 };
139 MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
140 
141 struct platform_driver mpc52xx_fec_mdio_driver = {
142 	.driver = {
143 		.name = "mpc5200b-fec-phy",
144 		.owner = THIS_MODULE,
145 		.of_match_table = mpc52xx_fec_mdio_match,
146 	},
147 	.probe = mpc52xx_fec_mdio_probe,
148 	.remove = mpc52xx_fec_mdio_remove,
149 };
150 
151 /* let fec driver call it, since this has to be registered before it */
152 EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
153 
154 MODULE_LICENSE("Dual BSD/GPL");
155