xref: /linux/arch/powerpc/platforms/pasemi/gpio_mdio.c (revision 1dd2d06c0459a2f1bffc56765e3cc57427818867)
1b97d2791SOlof Johansson /*
2b97d2791SOlof Johansson  * Copyright (C) 2006-2007 PA Semi, Inc
3b97d2791SOlof Johansson  *
4b97d2791SOlof Johansson  * Author: Olof Johansson, PA Semi
5b97d2791SOlof Johansson  *
6b97d2791SOlof Johansson  * Maintained by: Olof Johansson <olof@lixom.net>
7b97d2791SOlof Johansson  *
8b97d2791SOlof Johansson  * Based on drivers/net/fs_enet/mii-bitbang.c.
9b97d2791SOlof Johansson  *
10b97d2791SOlof Johansson  * This program is free software; you can redistribute it and/or modify
11b97d2791SOlof Johansson  * it under the terms of the GNU General Public License version 2 as
12b97d2791SOlof Johansson  * published by the Free Software Foundation.
13b97d2791SOlof Johansson  *
14b97d2791SOlof Johansson  * This program is distributed in the hope that it will be useful,
15b97d2791SOlof Johansson  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16b97d2791SOlof Johansson  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17b97d2791SOlof Johansson  * GNU General Public License for more details.
18b97d2791SOlof Johansson  *
19b97d2791SOlof Johansson  * You should have received a copy of the GNU General Public License
20b97d2791SOlof Johansson  * along with this program; if not, write to the Free Software
21b97d2791SOlof Johansson  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22b97d2791SOlof Johansson  */
23b97d2791SOlof Johansson 
24b97d2791SOlof Johansson #include <linux/io.h>
25b97d2791SOlof Johansson #include <linux/module.h>
26b97d2791SOlof Johansson #include <linux/types.h>
27b97d2791SOlof Johansson #include <linux/sched.h>
28b97d2791SOlof Johansson #include <linux/errno.h>
29b97d2791SOlof Johansson #include <linux/ioport.h>
30b97d2791SOlof Johansson #include <linux/interrupt.h>
31b97d2791SOlof Johansson #include <linux/phy.h>
32*1dd2d06cSGrant Likely #include <linux/of_mdio.h>
335f867dc7SJon Loeliger #include <linux/of_platform.h>
34b97d2791SOlof Johansson 
35b97d2791SOlof Johansson #define DELAY 1
36b97d2791SOlof Johansson 
37b97d2791SOlof Johansson static void __iomem *gpio_regs;
38b97d2791SOlof Johansson 
39b97d2791SOlof Johansson struct gpio_priv {
40b97d2791SOlof Johansson 	int mdc_pin;
41b97d2791SOlof Johansson 	int mdio_pin;
42*1dd2d06cSGrant Likely 	int mdio_irqs[PHY_MAX_ADDR];
43b97d2791SOlof Johansson };
44b97d2791SOlof Johansson 
45b97d2791SOlof Johansson #define MDC_PIN(bus)	(((struct gpio_priv *)bus->priv)->mdc_pin)
46b97d2791SOlof Johansson #define MDIO_PIN(bus)	(((struct gpio_priv *)bus->priv)->mdio_pin)
47b97d2791SOlof Johansson 
48b97d2791SOlof Johansson static inline void mdio_lo(struct mii_bus *bus)
49b97d2791SOlof Johansson {
50b97d2791SOlof Johansson 	out_le32(gpio_regs+0x10, 1 << MDIO_PIN(bus));
51b97d2791SOlof Johansson }
52b97d2791SOlof Johansson 
53b97d2791SOlof Johansson static inline void mdio_hi(struct mii_bus *bus)
54b97d2791SOlof Johansson {
55b97d2791SOlof Johansson 	out_le32(gpio_regs, 1 << MDIO_PIN(bus));
56b97d2791SOlof Johansson }
57b97d2791SOlof Johansson 
58b97d2791SOlof Johansson static inline void mdc_lo(struct mii_bus *bus)
59b97d2791SOlof Johansson {
60b97d2791SOlof Johansson 	out_le32(gpio_regs+0x10, 1 << MDC_PIN(bus));
61b97d2791SOlof Johansson }
62b97d2791SOlof Johansson 
63b97d2791SOlof Johansson static inline void mdc_hi(struct mii_bus *bus)
64b97d2791SOlof Johansson {
65b97d2791SOlof Johansson 	out_le32(gpio_regs, 1 << MDC_PIN(bus));
66b97d2791SOlof Johansson }
67b97d2791SOlof Johansson 
68b97d2791SOlof Johansson static inline void mdio_active(struct mii_bus *bus)
69b97d2791SOlof Johansson {
70b97d2791SOlof Johansson 	out_le32(gpio_regs+0x20, (1 << MDC_PIN(bus)) | (1 << MDIO_PIN(bus)));
71b97d2791SOlof Johansson }
72b97d2791SOlof Johansson 
73b97d2791SOlof Johansson static inline void mdio_tristate(struct mii_bus *bus)
74b97d2791SOlof Johansson {
75b97d2791SOlof Johansson 	out_le32(gpio_regs+0x30, (1 << MDIO_PIN(bus)));
76b97d2791SOlof Johansson }
77b97d2791SOlof Johansson 
78b97d2791SOlof Johansson static inline int mdio_read(struct mii_bus *bus)
79b97d2791SOlof Johansson {
80b97d2791SOlof Johansson 	return !!(in_le32(gpio_regs+0x40) & (1 << MDIO_PIN(bus)));
81b97d2791SOlof Johansson }
82b97d2791SOlof Johansson 
83b97d2791SOlof Johansson static void clock_out(struct mii_bus *bus, int bit)
84b97d2791SOlof Johansson {
85b97d2791SOlof Johansson 	if (bit)
86b97d2791SOlof Johansson 		mdio_hi(bus);
87b97d2791SOlof Johansson 	else
88b97d2791SOlof Johansson 		mdio_lo(bus);
89b97d2791SOlof Johansson 	udelay(DELAY);
90b97d2791SOlof Johansson 	mdc_hi(bus);
91b97d2791SOlof Johansson 	udelay(DELAY);
92b97d2791SOlof Johansson 	mdc_lo(bus);
93b97d2791SOlof Johansson }
94b97d2791SOlof Johansson 
95b97d2791SOlof Johansson /* Utility to send the preamble, address, and register (common to read and write). */
96b97d2791SOlof Johansson static void bitbang_pre(struct mii_bus *bus, int read, u8 addr, u8 reg)
97b97d2791SOlof Johansson {
98b97d2791SOlof Johansson 	int i;
99b97d2791SOlof Johansson 
100b97d2791SOlof Johansson 	/* CFE uses a really long preamble (40 bits). We'll do the same. */
101b97d2791SOlof Johansson 	mdio_active(bus);
102b97d2791SOlof Johansson 	for (i = 0; i < 40; i++) {
103b97d2791SOlof Johansson 		clock_out(bus, 1);
104b97d2791SOlof Johansson 	}
105b97d2791SOlof Johansson 
106b97d2791SOlof Johansson 	/* send the start bit (01) and the read opcode (10) or write (10) */
107b97d2791SOlof Johansson 	clock_out(bus, 0);
108b97d2791SOlof Johansson 	clock_out(bus, 1);
109b97d2791SOlof Johansson 
110b97d2791SOlof Johansson 	clock_out(bus, read);
111b97d2791SOlof Johansson 	clock_out(bus, !read);
112b97d2791SOlof Johansson 
113b97d2791SOlof Johansson 	/* send the PHY address */
114b97d2791SOlof Johansson 	for (i = 0; i < 5; i++) {
115b97d2791SOlof Johansson 		clock_out(bus, (addr & 0x10) != 0);
116b97d2791SOlof Johansson 		addr <<= 1;
117b97d2791SOlof Johansson 	}
118b97d2791SOlof Johansson 
119b97d2791SOlof Johansson 	/* send the register address */
120b97d2791SOlof Johansson 	for (i = 0; i < 5; i++) {
121b97d2791SOlof Johansson 		clock_out(bus, (reg & 0x10) != 0);
122b97d2791SOlof Johansson 		reg <<= 1;
123b97d2791SOlof Johansson 	}
124b97d2791SOlof Johansson }
125b97d2791SOlof Johansson 
126b97d2791SOlof Johansson static int gpio_mdio_read(struct mii_bus *bus, int phy_id, int location)
127b97d2791SOlof Johansson {
128b97d2791SOlof Johansson 	u16 rdreg;
129b97d2791SOlof Johansson 	int ret, i;
130b97d2791SOlof Johansson 	u8 addr = phy_id & 0xff;
131b97d2791SOlof Johansson 	u8 reg = location & 0xff;
132b97d2791SOlof Johansson 
133b97d2791SOlof Johansson 	bitbang_pre(bus, 1, addr, reg);
134b97d2791SOlof Johansson 
135b97d2791SOlof Johansson 	/* tri-state our MDIO I/O pin so we can read */
136b97d2791SOlof Johansson 	mdio_tristate(bus);
137b97d2791SOlof Johansson 	udelay(DELAY);
138b97d2791SOlof Johansson 	mdc_hi(bus);
139b97d2791SOlof Johansson 	udelay(DELAY);
140b97d2791SOlof Johansson 	mdc_lo(bus);
141b97d2791SOlof Johansson 
142b97d2791SOlof Johansson 	/* read 16 bits of register data, MSB first */
143b97d2791SOlof Johansson 	rdreg = 0;
144b97d2791SOlof Johansson 	for (i = 0; i < 16; i++) {
145b97d2791SOlof Johansson 		mdc_lo(bus);
146b97d2791SOlof Johansson 		udelay(DELAY);
147b97d2791SOlof Johansson 		mdc_hi(bus);
148b97d2791SOlof Johansson 		udelay(DELAY);
149b97d2791SOlof Johansson 		mdc_lo(bus);
150b97d2791SOlof Johansson 		udelay(DELAY);
151b97d2791SOlof Johansson 		rdreg <<= 1;
152b97d2791SOlof Johansson 		rdreg |= mdio_read(bus);
153b97d2791SOlof Johansson 	}
154b97d2791SOlof Johansson 
155b97d2791SOlof Johansson 	mdc_hi(bus);
156b97d2791SOlof Johansson 	udelay(DELAY);
157b97d2791SOlof Johansson 	mdc_lo(bus);
158b97d2791SOlof Johansson 	udelay(DELAY);
159b97d2791SOlof Johansson 
160b97d2791SOlof Johansson 	ret = rdreg;
161b97d2791SOlof Johansson 
162b97d2791SOlof Johansson 	return ret;
163b97d2791SOlof Johansson }
164b97d2791SOlof Johansson 
165b97d2791SOlof Johansson static int gpio_mdio_write(struct mii_bus *bus, int phy_id, int location, u16 val)
166b97d2791SOlof Johansson {
167b97d2791SOlof Johansson 	int i;
168b97d2791SOlof Johansson 
169b97d2791SOlof Johansson 	u8 addr = phy_id & 0xff;
170b97d2791SOlof Johansson 	u8 reg = location & 0xff;
171b97d2791SOlof Johansson 	u16 value = val & 0xffff;
172b97d2791SOlof Johansson 
173b97d2791SOlof Johansson 	bitbang_pre(bus, 0, addr, reg);
174b97d2791SOlof Johansson 
175b97d2791SOlof Johansson 	/* send the turnaround (10) */
176b97d2791SOlof Johansson 	mdc_lo(bus);
177b97d2791SOlof Johansson 	mdio_hi(bus);
178b97d2791SOlof Johansson 	udelay(DELAY);
179b97d2791SOlof Johansson 	mdc_hi(bus);
180b97d2791SOlof Johansson 	udelay(DELAY);
181b97d2791SOlof Johansson 	mdc_lo(bus);
182b97d2791SOlof Johansson 	mdio_lo(bus);
183b97d2791SOlof Johansson 	udelay(DELAY);
184b97d2791SOlof Johansson 	mdc_hi(bus);
185b97d2791SOlof Johansson 	udelay(DELAY);
186b97d2791SOlof Johansson 
187b97d2791SOlof Johansson 	/* write 16 bits of register data, MSB first */
188b97d2791SOlof Johansson 	for (i = 0; i < 16; i++) {
189b97d2791SOlof Johansson 		mdc_lo(bus);
190b97d2791SOlof Johansson 		if (value & 0x8000)
191b97d2791SOlof Johansson 			mdio_hi(bus);
192b97d2791SOlof Johansson 		else
193b97d2791SOlof Johansson 			mdio_lo(bus);
194b97d2791SOlof Johansson 		udelay(DELAY);
195b97d2791SOlof Johansson 		mdc_hi(bus);
196b97d2791SOlof Johansson 		udelay(DELAY);
197b97d2791SOlof Johansson 		value <<= 1;
198b97d2791SOlof Johansson 	}
199b97d2791SOlof Johansson 
200b97d2791SOlof Johansson 	/*
201b97d2791SOlof Johansson 	 * Tri-state the MDIO line.
202b97d2791SOlof Johansson 	 */
203b97d2791SOlof Johansson 	mdio_tristate(bus);
204b97d2791SOlof Johansson 	mdc_lo(bus);
205b97d2791SOlof Johansson 	udelay(DELAY);
206b97d2791SOlof Johansson 	mdc_hi(bus);
207b97d2791SOlof Johansson 	udelay(DELAY);
208b97d2791SOlof Johansson 	return 0;
209b97d2791SOlof Johansson }
210b97d2791SOlof Johansson 
211b97d2791SOlof Johansson static int gpio_mdio_reset(struct mii_bus *bus)
212b97d2791SOlof Johansson {
213b97d2791SOlof Johansson 	/*nothing here - dunno how to reset it*/
214b97d2791SOlof Johansson 	return 0;
215b97d2791SOlof Johansson }
216b97d2791SOlof Johansson 
217b97d2791SOlof Johansson 
218b97d2791SOlof Johansson static int __devinit gpio_mdio_probe(struct of_device *ofdev,
219b97d2791SOlof Johansson 				     const struct of_device_id *match)
220b97d2791SOlof Johansson {
221b97d2791SOlof Johansson 	struct device *dev = &ofdev->dev;
222*1dd2d06cSGrant Likely 	struct device_node *np = ofdev->node;
223b97d2791SOlof Johansson 	struct mii_bus *new_bus;
224b97d2791SOlof Johansson 	struct gpio_priv *priv;
225b97d2791SOlof Johansson 	const unsigned int *prop;
2262dd3c001SOlof Johansson 	int err;
227b97d2791SOlof Johansson 
2282dd3c001SOlof Johansson 	err = -ENOMEM;
229b97d2791SOlof Johansson 	priv = kzalloc(sizeof(struct gpio_priv), GFP_KERNEL);
2302dd3c001SOlof Johansson 	if (!priv)
2312dd3c001SOlof Johansson 		goto out;
232b97d2791SOlof Johansson 
233ec2a5652SLennert Buytenhek 	new_bus = mdiobus_alloc();
234b97d2791SOlof Johansson 
2352dd3c001SOlof Johansson 	if (!new_bus)
2362dd3c001SOlof Johansson 		goto out_free_priv;
237b97d2791SOlof Johansson 
2382dd3c001SOlof Johansson 	new_bus->name = "pasemi gpio mdio bus";
2392dd3c001SOlof Johansson 	new_bus->read = &gpio_mdio_read;
2402dd3c001SOlof Johansson 	new_bus->write = &gpio_mdio_write;
2412dd3c001SOlof Johansson 	new_bus->reset = &gpio_mdio_reset;
242b97d2791SOlof Johansson 
24312d371a6SStephen Rothwell 	prop = of_get_property(np, "reg", NULL);
2449d9326d3SAndy Fleming 	snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", *prop);
245b97d2791SOlof Johansson 	new_bus->priv = priv;
246b97d2791SOlof Johansson 
247*1dd2d06cSGrant Likely 	new_bus->irq = priv->mdio_irqs;
248b97d2791SOlof Johansson 
24912d371a6SStephen Rothwell 	prop = of_get_property(np, "mdc-pin", NULL);
250b97d2791SOlof Johansson 	priv->mdc_pin = *prop;
251b97d2791SOlof Johansson 
25212d371a6SStephen Rothwell 	prop = of_get_property(np, "mdio-pin", NULL);
253b97d2791SOlof Johansson 	priv->mdio_pin = *prop;
254b97d2791SOlof Johansson 
25518ee49ddSLennert Buytenhek 	new_bus->parent = dev;
256b97d2791SOlof Johansson 	dev_set_drvdata(dev, new_bus);
257b97d2791SOlof Johansson 
258*1dd2d06cSGrant Likely 	err = of_mdiobus_register(new_bus, np);
259b97d2791SOlof Johansson 
2602dd3c001SOlof Johansson 	if (err != 0) {
261b97d2791SOlof Johansson 		printk(KERN_ERR "%s: Cannot register as MDIO bus, err %d\n",
262b97d2791SOlof Johansson 				new_bus->name, err);
2632dd3c001SOlof Johansson 		goto out_free_irq;
264b97d2791SOlof Johansson 	}
265b97d2791SOlof Johansson 
266b97d2791SOlof Johansson 	return 0;
267b97d2791SOlof Johansson 
2682dd3c001SOlof Johansson out_free_irq:
269b97d2791SOlof Johansson 	kfree(new_bus);
2702dd3c001SOlof Johansson out_free_priv:
2712dd3c001SOlof Johansson 	kfree(priv);
2722dd3c001SOlof Johansson out:
273b97d2791SOlof Johansson 	return err;
274b97d2791SOlof Johansson }
275b97d2791SOlof Johansson 
276b97d2791SOlof Johansson 
277b97d2791SOlof Johansson static int gpio_mdio_remove(struct of_device *dev)
278b97d2791SOlof Johansson {
279b97d2791SOlof Johansson 	struct mii_bus *bus = dev_get_drvdata(&dev->dev);
280b97d2791SOlof Johansson 
281b97d2791SOlof Johansson 	mdiobus_unregister(bus);
282b97d2791SOlof Johansson 
283b97d2791SOlof Johansson 	dev_set_drvdata(&dev->dev, NULL);
284b97d2791SOlof Johansson 
285b97d2791SOlof Johansson 	kfree(bus->priv);
286b97d2791SOlof Johansson 	bus->priv = NULL;
287ec2a5652SLennert Buytenhek 	mdiobus_free(bus);
288b97d2791SOlof Johansson 
289b97d2791SOlof Johansson 	return 0;
290b97d2791SOlof Johansson }
291b97d2791SOlof Johansson 
292b97d2791SOlof Johansson static struct of_device_id gpio_mdio_match[] =
293b97d2791SOlof Johansson {
294b97d2791SOlof Johansson 	{
295b97d2791SOlof Johansson 		.compatible      = "gpio-mdio",
296b97d2791SOlof Johansson 	},
297b97d2791SOlof Johansson 	{},
298b97d2791SOlof Johansson };
2995619965fSOlof Johansson MODULE_DEVICE_TABLE(of, gpio_mdio_match);
300b97d2791SOlof Johansson 
301b97d2791SOlof Johansson static struct of_platform_driver gpio_mdio_driver =
302b97d2791SOlof Johansson {
303b97d2791SOlof Johansson 	.match_table	= gpio_mdio_match,
304b97d2791SOlof Johansson 	.probe		= gpio_mdio_probe,
305b97d2791SOlof Johansson 	.remove		= gpio_mdio_remove,
30684dd4676SStephen Rothwell 	.driver		= {
30784dd4676SStephen Rothwell 		.name	= "gpio-mdio-bitbang",
30884dd4676SStephen Rothwell 	},
309b97d2791SOlof Johansson };
310b97d2791SOlof Johansson 
311b97d2791SOlof Johansson int gpio_mdio_init(void)
312b97d2791SOlof Johansson {
3132dd3c001SOlof Johansson 	struct device_node *np;
3142dd3c001SOlof Johansson 
3150d08a847SOlof Johansson 	np = of_find_compatible_node(NULL, NULL, "1682m-gpio");
3160d08a847SOlof Johansson 	if (!np)
3170d08a847SOlof Johansson 		np = of_find_compatible_node(NULL, NULL,
3180d08a847SOlof Johansson 					     "pasemi,pwrficient-gpio");
3192dd3c001SOlof Johansson 	if (!np)
3202dd3c001SOlof Johansson 		return -ENODEV;
3212dd3c001SOlof Johansson 	gpio_regs = of_iomap(np, 0);
3222dd3c001SOlof Johansson 	of_node_put(np);
3232dd3c001SOlof Johansson 
3242dd3c001SOlof Johansson 	if (!gpio_regs)
3252dd3c001SOlof Johansson 		return -ENODEV;
3262dd3c001SOlof Johansson 
327b97d2791SOlof Johansson 	return of_register_platform_driver(&gpio_mdio_driver);
328b97d2791SOlof Johansson }
3292dd3c001SOlof Johansson module_init(gpio_mdio_init);
330b97d2791SOlof Johansson 
331b97d2791SOlof Johansson void gpio_mdio_exit(void)
332b97d2791SOlof Johansson {
333b97d2791SOlof Johansson 	of_unregister_platform_driver(&gpio_mdio_driver);
3342dd3c001SOlof Johansson 	if (gpio_regs)
3352dd3c001SOlof Johansson 		iounmap(gpio_regs);
336b97d2791SOlof Johansson }
3372dd3c001SOlof Johansson module_exit(gpio_mdio_exit);
3385619965fSOlof Johansson 
3395619965fSOlof Johansson MODULE_LICENSE("GPL");
3405619965fSOlof Johansson MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
3415619965fSOlof Johansson MODULE_DESCRIPTION("Driver for MDIO over GPIO on PA Semi PWRficient-based boards");
342