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_address.h>
19 #include <linux/of_mdio.h>
20 #include <asm/io.h>
21 #include <asm/mpc52xx.h>
22 #include "fec_mpc52xx.h"
23 
24 struct mpc52xx_fec_mdio_priv {
25 	struct mpc52xx_fec __iomem *regs;
26 };
27 
28 static 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 
52 static 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 
57 static 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 
64 static 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 registers */
87 	err = of_address_to_resource(np, 0, &res);
88 	if (err)
89 		goto out_free;
90 	priv->regs = ioremap(res.start, resource_size(&res));
91 	if (priv->regs == NULL) {
92 		err = -ENOMEM;
93 		goto out_free;
94 	}
95 
96 	snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
97 	bus->priv = priv;
98 
99 	bus->parent = dev;
100 	dev_set_drvdata(dev, bus);
101 
102 	/* set MII speed */
103 	out_be32(&priv->regs->mii_speed,
104 		((mpc5xxx_get_bus_frequency(of->dev.of_node) >> 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 
121 static int 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 	return 0;
132 }
133 
134 static const struct of_device_id mpc52xx_fec_mdio_match[] = {
135 	{ .compatible = "fsl,mpc5200b-mdio", },
136 	{ .compatible = "fsl,mpc5200-mdio", },
137 	{ .compatible = "mpc5200b-fec-phy", },
138 	{}
139 };
140 MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match);
141 
142 struct platform_driver mpc52xx_fec_mdio_driver = {
143 	.driver = {
144 		.name = "mpc5200b-fec-phy",
145 		.owner = THIS_MODULE,
146 		.of_match_table = mpc52xx_fec_mdio_match,
147 	},
148 	.probe = mpc52xx_fec_mdio_probe,
149 	.remove = mpc52xx_fec_mdio_remove,
150 };
151 
152 /* let fec driver call it, since this has to be registered before it */
153 EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver);
154 
155 MODULE_LICENSE("Dual BSD/GPL");
156