xref: /linux/arch/powerpc/platforms/pasemi/gpio_mdio.c (revision 81d7cac4d11cc65f29be68c72759429d5194347a)
145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b97d2791SOlof Johansson /*
3b97d2791SOlof Johansson  * Copyright (C) 2006-2007 PA Semi, Inc
4b97d2791SOlof Johansson  *
5b97d2791SOlof Johansson  * Author: Olof Johansson, PA Semi
6b97d2791SOlof Johansson  *
7b97d2791SOlof Johansson  * Maintained by: Olof Johansson <olof@lixom.net>
8b97d2791SOlof Johansson  *
9b97d2791SOlof Johansson  * Based on drivers/net/fs_enet/mii-bitbang.c.
10b97d2791SOlof Johansson  */
11b97d2791SOlof Johansson 
12b97d2791SOlof Johansson #include <linux/io.h>
13b97d2791SOlof Johansson #include <linux/module.h>
14b97d2791SOlof Johansson #include <linux/types.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
16b97d2791SOlof Johansson #include <linux/sched.h>
17b97d2791SOlof Johansson #include <linux/errno.h>
18b97d2791SOlof Johansson #include <linux/ioport.h>
19b97d2791SOlof Johansson #include <linux/interrupt.h>
20b97d2791SOlof Johansson #include <linux/phy.h>
2126a2056eSRob Herring #include <linux/of_address.h>
221dd2d06cSGrant Likely #include <linux/of_mdio.h>
23*81d7cac4SRob Herring #include <linux/platform_device.h>
24b97d2791SOlof Johansson 
25b97d2791SOlof Johansson #define DELAY 1
26b97d2791SOlof Johansson 
27b97d2791SOlof Johansson static void __iomem *gpio_regs;
28b97d2791SOlof Johansson 
29b97d2791SOlof Johansson struct gpio_priv {
30b97d2791SOlof Johansson 	int mdc_pin;
31b97d2791SOlof Johansson 	int mdio_pin;
32b97d2791SOlof Johansson };
33b97d2791SOlof Johansson 
34b97d2791SOlof Johansson #define MDC_PIN(bus)	(((struct gpio_priv *)bus->priv)->mdc_pin)
35b97d2791SOlof Johansson #define MDIO_PIN(bus)	(((struct gpio_priv *)bus->priv)->mdio_pin)
36b97d2791SOlof Johansson 
37b97d2791SOlof Johansson static inline void mdio_lo(struct mii_bus *bus)
38b97d2791SOlof Johansson {
39b97d2791SOlof Johansson 	out_le32(gpio_regs+0x10, 1 << MDIO_PIN(bus));
40b97d2791SOlof Johansson }
41b97d2791SOlof Johansson 
42b97d2791SOlof Johansson static inline void mdio_hi(struct mii_bus *bus)
43b97d2791SOlof Johansson {
44b97d2791SOlof Johansson 	out_le32(gpio_regs, 1 << MDIO_PIN(bus));
45b97d2791SOlof Johansson }
46b97d2791SOlof Johansson 
47b97d2791SOlof Johansson static inline void mdc_lo(struct mii_bus *bus)
48b97d2791SOlof Johansson {
49b97d2791SOlof Johansson 	out_le32(gpio_regs+0x10, 1 << MDC_PIN(bus));
50b97d2791SOlof Johansson }
51b97d2791SOlof Johansson 
52b97d2791SOlof Johansson static inline void mdc_hi(struct mii_bus *bus)
53b97d2791SOlof Johansson {
54b97d2791SOlof Johansson 	out_le32(gpio_regs, 1 << MDC_PIN(bus));
55b97d2791SOlof Johansson }
56b97d2791SOlof Johansson 
57b97d2791SOlof Johansson static inline void mdio_active(struct mii_bus *bus)
58b97d2791SOlof Johansson {
59b97d2791SOlof Johansson 	out_le32(gpio_regs+0x20, (1 << MDC_PIN(bus)) | (1 << MDIO_PIN(bus)));
60b97d2791SOlof Johansson }
61b97d2791SOlof Johansson 
62b97d2791SOlof Johansson static inline void mdio_tristate(struct mii_bus *bus)
63b97d2791SOlof Johansson {
64b97d2791SOlof Johansson 	out_le32(gpio_regs+0x30, (1 << MDIO_PIN(bus)));
65b97d2791SOlof Johansson }
66b97d2791SOlof Johansson 
67b97d2791SOlof Johansson static inline int mdio_read(struct mii_bus *bus)
68b97d2791SOlof Johansson {
69b97d2791SOlof Johansson 	return !!(in_le32(gpio_regs+0x40) & (1 << MDIO_PIN(bus)));
70b97d2791SOlof Johansson }
71b97d2791SOlof Johansson 
72b97d2791SOlof Johansson static void clock_out(struct mii_bus *bus, int bit)
73b97d2791SOlof Johansson {
74b97d2791SOlof Johansson 	if (bit)
75b97d2791SOlof Johansson 		mdio_hi(bus);
76b97d2791SOlof Johansson 	else
77b97d2791SOlof Johansson 		mdio_lo(bus);
78b97d2791SOlof Johansson 	udelay(DELAY);
79b97d2791SOlof Johansson 	mdc_hi(bus);
80b97d2791SOlof Johansson 	udelay(DELAY);
81b97d2791SOlof Johansson 	mdc_lo(bus);
82b97d2791SOlof Johansson }
83b97d2791SOlof Johansson 
84b97d2791SOlof Johansson /* Utility to send the preamble, address, and register (common to read and write). */
85b97d2791SOlof Johansson static void bitbang_pre(struct mii_bus *bus, int read, u8 addr, u8 reg)
86b97d2791SOlof Johansson {
87b97d2791SOlof Johansson 	int i;
88b97d2791SOlof Johansson 
89b97d2791SOlof Johansson 	/* CFE uses a really long preamble (40 bits). We'll do the same. */
90b97d2791SOlof Johansson 	mdio_active(bus);
91b97d2791SOlof Johansson 	for (i = 0; i < 40; i++) {
92b97d2791SOlof Johansson 		clock_out(bus, 1);
93b97d2791SOlof Johansson 	}
94b97d2791SOlof Johansson 
95b97d2791SOlof Johansson 	/* send the start bit (01) and the read opcode (10) or write (10) */
96b97d2791SOlof Johansson 	clock_out(bus, 0);
97b97d2791SOlof Johansson 	clock_out(bus, 1);
98b97d2791SOlof Johansson 
99b97d2791SOlof Johansson 	clock_out(bus, read);
100b97d2791SOlof Johansson 	clock_out(bus, !read);
101b97d2791SOlof Johansson 
102b97d2791SOlof Johansson 	/* send the PHY address */
103b97d2791SOlof Johansson 	for (i = 0; i < 5; i++) {
104b97d2791SOlof Johansson 		clock_out(bus, (addr & 0x10) != 0);
105b97d2791SOlof Johansson 		addr <<= 1;
106b97d2791SOlof Johansson 	}
107b97d2791SOlof Johansson 
108b97d2791SOlof Johansson 	/* send the register address */
109b97d2791SOlof Johansson 	for (i = 0; i < 5; i++) {
110b97d2791SOlof Johansson 		clock_out(bus, (reg & 0x10) != 0);
111b97d2791SOlof Johansson 		reg <<= 1;
112b97d2791SOlof Johansson 	}
113b97d2791SOlof Johansson }
114b97d2791SOlof Johansson 
115b97d2791SOlof Johansson static int gpio_mdio_read(struct mii_bus *bus, int phy_id, int location)
116b97d2791SOlof Johansson {
117b97d2791SOlof Johansson 	u16 rdreg;
118b97d2791SOlof Johansson 	int ret, i;
119b97d2791SOlof Johansson 	u8 addr = phy_id & 0xff;
120b97d2791SOlof Johansson 	u8 reg = location & 0xff;
121b97d2791SOlof Johansson 
122b97d2791SOlof Johansson 	bitbang_pre(bus, 1, addr, reg);
123b97d2791SOlof Johansson 
124b97d2791SOlof Johansson 	/* tri-state our MDIO I/O pin so we can read */
125b97d2791SOlof Johansson 	mdio_tristate(bus);
126b97d2791SOlof Johansson 	udelay(DELAY);
127b97d2791SOlof Johansson 	mdc_hi(bus);
128b97d2791SOlof Johansson 	udelay(DELAY);
129b97d2791SOlof Johansson 	mdc_lo(bus);
130b97d2791SOlof Johansson 
131b97d2791SOlof Johansson 	/* read 16 bits of register data, MSB first */
132b97d2791SOlof Johansson 	rdreg = 0;
133b97d2791SOlof Johansson 	for (i = 0; i < 16; i++) {
134b97d2791SOlof Johansson 		mdc_lo(bus);
135b97d2791SOlof Johansson 		udelay(DELAY);
136b97d2791SOlof Johansson 		mdc_hi(bus);
137b97d2791SOlof Johansson 		udelay(DELAY);
138b97d2791SOlof Johansson 		mdc_lo(bus);
139b97d2791SOlof Johansson 		udelay(DELAY);
140b97d2791SOlof Johansson 		rdreg <<= 1;
141b97d2791SOlof Johansson 		rdreg |= mdio_read(bus);
142b97d2791SOlof Johansson 	}
143b97d2791SOlof Johansson 
144b97d2791SOlof Johansson 	mdc_hi(bus);
145b97d2791SOlof Johansson 	udelay(DELAY);
146b97d2791SOlof Johansson 	mdc_lo(bus);
147b97d2791SOlof Johansson 	udelay(DELAY);
148b97d2791SOlof Johansson 
149b97d2791SOlof Johansson 	ret = rdreg;
150b97d2791SOlof Johansson 
151b97d2791SOlof Johansson 	return ret;
152b97d2791SOlof Johansson }
153b97d2791SOlof Johansson 
154b97d2791SOlof Johansson static int gpio_mdio_write(struct mii_bus *bus, int phy_id, int location, u16 val)
155b97d2791SOlof Johansson {
156b97d2791SOlof Johansson 	int i;
157b97d2791SOlof Johansson 
158b97d2791SOlof Johansson 	u8 addr = phy_id & 0xff;
159b97d2791SOlof Johansson 	u8 reg = location & 0xff;
160b97d2791SOlof Johansson 	u16 value = val & 0xffff;
161b97d2791SOlof Johansson 
162b97d2791SOlof Johansson 	bitbang_pre(bus, 0, addr, reg);
163b97d2791SOlof Johansson 
164b97d2791SOlof Johansson 	/* send the turnaround (10) */
165b97d2791SOlof Johansson 	mdc_lo(bus);
166b97d2791SOlof Johansson 	mdio_hi(bus);
167b97d2791SOlof Johansson 	udelay(DELAY);
168b97d2791SOlof Johansson 	mdc_hi(bus);
169b97d2791SOlof Johansson 	udelay(DELAY);
170b97d2791SOlof Johansson 	mdc_lo(bus);
171b97d2791SOlof Johansson 	mdio_lo(bus);
172b97d2791SOlof Johansson 	udelay(DELAY);
173b97d2791SOlof Johansson 	mdc_hi(bus);
174b97d2791SOlof Johansson 	udelay(DELAY);
175b97d2791SOlof Johansson 
176b97d2791SOlof Johansson 	/* write 16 bits of register data, MSB first */
177b97d2791SOlof Johansson 	for (i = 0; i < 16; i++) {
178b97d2791SOlof Johansson 		mdc_lo(bus);
179b97d2791SOlof Johansson 		if (value & 0x8000)
180b97d2791SOlof Johansson 			mdio_hi(bus);
181b97d2791SOlof Johansson 		else
182b97d2791SOlof Johansson 			mdio_lo(bus);
183b97d2791SOlof Johansson 		udelay(DELAY);
184b97d2791SOlof Johansson 		mdc_hi(bus);
185b97d2791SOlof Johansson 		udelay(DELAY);
186b97d2791SOlof Johansson 		value <<= 1;
187b97d2791SOlof Johansson 	}
188b97d2791SOlof Johansson 
189b97d2791SOlof Johansson 	/*
190b97d2791SOlof Johansson 	 * Tri-state the MDIO line.
191b97d2791SOlof Johansson 	 */
192b97d2791SOlof Johansson 	mdio_tristate(bus);
193b97d2791SOlof Johansson 	mdc_lo(bus);
194b97d2791SOlof Johansson 	udelay(DELAY);
195b97d2791SOlof Johansson 	mdc_hi(bus);
196b97d2791SOlof Johansson 	udelay(DELAY);
197b97d2791SOlof Johansson 	return 0;
198b97d2791SOlof Johansson }
199b97d2791SOlof Johansson 
200b97d2791SOlof Johansson static int gpio_mdio_reset(struct mii_bus *bus)
201b97d2791SOlof Johansson {
202b97d2791SOlof Johansson 	/*nothing here - dunno how to reset it*/
203b97d2791SOlof Johansson 	return 0;
204b97d2791SOlof Johansson }
205b97d2791SOlof Johansson 
206b97d2791SOlof Johansson 
207cad5cef6SGreg Kroah-Hartman static int gpio_mdio_probe(struct platform_device *ofdev)
208b97d2791SOlof Johansson {
209b97d2791SOlof Johansson 	struct device *dev = &ofdev->dev;
21061c7a080SGrant Likely 	struct device_node *np = ofdev->dev.of_node;
211b97d2791SOlof Johansson 	struct mii_bus *new_bus;
212b97d2791SOlof Johansson 	struct gpio_priv *priv;
213b97d2791SOlof Johansson 	const unsigned int *prop;
2142dd3c001SOlof Johansson 	int err;
215b97d2791SOlof Johansson 
2162dd3c001SOlof Johansson 	err = -ENOMEM;
217b97d2791SOlof Johansson 	priv = kzalloc(sizeof(struct gpio_priv), GFP_KERNEL);
2182dd3c001SOlof Johansson 	if (!priv)
2192dd3c001SOlof Johansson 		goto out;
220b97d2791SOlof Johansson 
221ec2a5652SLennert Buytenhek 	new_bus = mdiobus_alloc();
222b97d2791SOlof Johansson 
2232dd3c001SOlof Johansson 	if (!new_bus)
2242dd3c001SOlof Johansson 		goto out_free_priv;
225b97d2791SOlof Johansson 
2262dd3c001SOlof Johansson 	new_bus->name = "pasemi gpio mdio bus";
2272dd3c001SOlof Johansson 	new_bus->read = &gpio_mdio_read;
2282dd3c001SOlof Johansson 	new_bus->write = &gpio_mdio_write;
2292dd3c001SOlof Johansson 	new_bus->reset = &gpio_mdio_reset;
230b97d2791SOlof Johansson 
23112d371a6SStephen Rothwell 	prop = of_get_property(np, "reg", NULL);
2329d9326d3SAndy Fleming 	snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", *prop);
233b97d2791SOlof Johansson 	new_bus->priv = priv;
234b97d2791SOlof Johansson 
23512d371a6SStephen Rothwell 	prop = of_get_property(np, "mdc-pin", NULL);
236b97d2791SOlof Johansson 	priv->mdc_pin = *prop;
237b97d2791SOlof Johansson 
23812d371a6SStephen Rothwell 	prop = of_get_property(np, "mdio-pin", NULL);
239b97d2791SOlof Johansson 	priv->mdio_pin = *prop;
240b97d2791SOlof Johansson 
24118ee49ddSLennert Buytenhek 	new_bus->parent = dev;
242b97d2791SOlof Johansson 	dev_set_drvdata(dev, new_bus);
243b97d2791SOlof Johansson 
2441dd2d06cSGrant Likely 	err = of_mdiobus_register(new_bus, np);
245b97d2791SOlof Johansson 
2462dd3c001SOlof Johansson 	if (err != 0) {
247e13606d7SDarren Stevens 		pr_err("%s: Cannot register as MDIO bus, err %d\n",
248b97d2791SOlof Johansson 				new_bus->name, err);
2492dd3c001SOlof Johansson 		goto out_free_irq;
250b97d2791SOlof Johansson 	}
251b97d2791SOlof Johansson 
252b97d2791SOlof Johansson 	return 0;
253b97d2791SOlof Johansson 
2542dd3c001SOlof Johansson out_free_irq:
255b97d2791SOlof Johansson 	kfree(new_bus);
2562dd3c001SOlof Johansson out_free_priv:
2572dd3c001SOlof Johansson 	kfree(priv);
2582dd3c001SOlof Johansson out:
259b97d2791SOlof Johansson 	return err;
260b97d2791SOlof Johansson }
261b97d2791SOlof Johansson 
262b97d2791SOlof Johansson 
263a454dc50SGrant Likely static int gpio_mdio_remove(struct platform_device *dev)
264b97d2791SOlof Johansson {
265b97d2791SOlof Johansson 	struct mii_bus *bus = dev_get_drvdata(&dev->dev);
266b97d2791SOlof Johansson 
267b97d2791SOlof Johansson 	mdiobus_unregister(bus);
268b97d2791SOlof Johansson 
269b97d2791SOlof Johansson 	dev_set_drvdata(&dev->dev, NULL);
270b97d2791SOlof Johansson 
271b97d2791SOlof Johansson 	kfree(bus->priv);
272b97d2791SOlof Johansson 	bus->priv = NULL;
273ec2a5652SLennert Buytenhek 	mdiobus_free(bus);
274b97d2791SOlof Johansson 
275b97d2791SOlof Johansson 	return 0;
276b97d2791SOlof Johansson }
277b97d2791SOlof Johansson 
278ce6d73c9SUwe Kleine-König static const struct of_device_id gpio_mdio_match[] =
279b97d2791SOlof Johansson {
280b97d2791SOlof Johansson 	{
281b97d2791SOlof Johansson 		.compatible      = "gpio-mdio",
282b97d2791SOlof Johansson 	},
283b97d2791SOlof Johansson 	{},
284b97d2791SOlof Johansson };
2855619965fSOlof Johansson MODULE_DEVICE_TABLE(of, gpio_mdio_match);
286b97d2791SOlof Johansson 
28700006124SGrant Likely static struct platform_driver gpio_mdio_driver =
288b97d2791SOlof Johansson {
289b97d2791SOlof Johansson 	.probe		= gpio_mdio_probe,
290b97d2791SOlof Johansson 	.remove		= gpio_mdio_remove,
29184dd4676SStephen Rothwell 	.driver = {
29284dd4676SStephen Rothwell 		.name = "gpio-mdio-bitbang",
2934018294bSGrant Likely 		.of_match_table = gpio_mdio_match,
29484dd4676SStephen Rothwell 	},
295b97d2791SOlof Johansson };
296b97d2791SOlof Johansson 
297d87a2337SXiu Jianfeng static int __init gpio_mdio_init(void)
298b97d2791SOlof Johansson {
2992dd3c001SOlof Johansson 	struct device_node *np;
3002dd3c001SOlof Johansson 
3010d08a847SOlof Johansson 	np = of_find_compatible_node(NULL, NULL, "1682m-gpio");
3020d08a847SOlof Johansson 	if (!np)
3030d08a847SOlof Johansson 		np = of_find_compatible_node(NULL, NULL,
3040d08a847SOlof Johansson 					     "pasemi,pwrficient-gpio");
3052dd3c001SOlof Johansson 	if (!np)
3062dd3c001SOlof Johansson 		return -ENODEV;
3072dd3c001SOlof Johansson 	gpio_regs = of_iomap(np, 0);
3082dd3c001SOlof Johansson 	of_node_put(np);
3092dd3c001SOlof Johansson 
3102dd3c001SOlof Johansson 	if (!gpio_regs)
3112dd3c001SOlof Johansson 		return -ENODEV;
3122dd3c001SOlof Johansson 
31300006124SGrant Likely 	return platform_driver_register(&gpio_mdio_driver);
314b97d2791SOlof Johansson }
3152dd3c001SOlof Johansson module_init(gpio_mdio_init);
316b97d2791SOlof Johansson 
317d87a2337SXiu Jianfeng static void __exit gpio_mdio_exit(void)
318b97d2791SOlof Johansson {
31900006124SGrant Likely 	platform_driver_unregister(&gpio_mdio_driver);
3202dd3c001SOlof Johansson 	if (gpio_regs)
3212dd3c001SOlof Johansson 		iounmap(gpio_regs);
322b97d2791SOlof Johansson }
3232dd3c001SOlof Johansson module_exit(gpio_mdio_exit);
3245619965fSOlof Johansson 
3255619965fSOlof Johansson MODULE_LICENSE("GPL");
3265619965fSOlof Johansson MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
3275619965fSOlof Johansson MODULE_DESCRIPTION("Driver for MDIO over GPIO on PA Semi PWRficient-based boards");
328