1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2006-2007 PA Semi, Inc
5 * Author: Olof Johansson, PA Semi
7 * Maintained by: Olof Johansson <olof@lixom.net>
9 * Based on drivers/net/fs_enet/mii-bitbang.c.
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/sched.h>
17 #include <linux/errno.h>
18 #include <linux/ioport.h>
19 #include <linux/interrupt.h>
20 #include <linux/phy.h>
21 #include <linux/of_address.h>
22 #include <linux/of_mdio.h>
23 #include <linux/of_platform.h>
27 static void __iomem
*gpio_regs
;
34 #define MDC_PIN(bus) (((struct gpio_priv *)bus->priv)->mdc_pin)
35 #define MDIO_PIN(bus) (((struct gpio_priv *)bus->priv)->mdio_pin)
37 static inline void mdio_lo(struct mii_bus
*bus
)
39 out_le32(gpio_regs
+0x10, 1 << MDIO_PIN(bus
));
42 static inline void mdio_hi(struct mii_bus
*bus
)
44 out_le32(gpio_regs
, 1 << MDIO_PIN(bus
));
47 static inline void mdc_lo(struct mii_bus
*bus
)
49 out_le32(gpio_regs
+0x10, 1 << MDC_PIN(bus
));
52 static inline void mdc_hi(struct mii_bus
*bus
)
54 out_le32(gpio_regs
, 1 << MDC_PIN(bus
));
57 static inline void mdio_active(struct mii_bus
*bus
)
59 out_le32(gpio_regs
+0x20, (1 << MDC_PIN(bus
)) | (1 << MDIO_PIN(bus
)));
62 static inline void mdio_tristate(struct mii_bus
*bus
)
64 out_le32(gpio_regs
+0x30, (1 << MDIO_PIN(bus
)));
67 static inline int mdio_read(struct mii_bus
*bus
)
69 return !!(in_le32(gpio_regs
+0x40) & (1 << MDIO_PIN(bus
)));
72 static void clock_out(struct mii_bus
*bus
, int bit
)
84 /* Utility to send the preamble, address, and register (common to read and write). */
85 static void bitbang_pre(struct mii_bus
*bus
, int read
, u8 addr
, u8 reg
)
89 /* CFE uses a really long preamble (40 bits). We'll do the same. */
91 for (i
= 0; i
< 40; i
++) {
95 /* send the start bit (01) and the read opcode (10) or write (10) */
100 clock_out(bus
, !read
);
102 /* send the PHY address */
103 for (i
= 0; i
< 5; i
++) {
104 clock_out(bus
, (addr
& 0x10) != 0);
108 /* send the register address */
109 for (i
= 0; i
< 5; i
++) {
110 clock_out(bus
, (reg
& 0x10) != 0);
115 static int gpio_mdio_read(struct mii_bus
*bus
, int phy_id
, int location
)
119 u8 addr
= phy_id
& 0xff;
120 u8 reg
= location
& 0xff;
122 bitbang_pre(bus
, 1, addr
, reg
);
124 /* tri-state our MDIO I/O pin so we can read */
131 /* read 16 bits of register data, MSB first */
133 for (i
= 0; i
< 16; i
++) {
141 rdreg
|= mdio_read(bus
);
154 static int gpio_mdio_write(struct mii_bus
*bus
, int phy_id
, int location
, u16 val
)
158 u8 addr
= phy_id
& 0xff;
159 u8 reg
= location
& 0xff;
160 u16 value
= val
& 0xffff;
162 bitbang_pre(bus
, 0, addr
, reg
);
164 /* send the turnaround (10) */
176 /* write 16 bits of register data, MSB first */
177 for (i
= 0; i
< 16; i
++) {
190 * Tri-state the MDIO line.
200 static int gpio_mdio_reset(struct mii_bus
*bus
)
202 /*nothing here - dunno how to reset it*/
207 static int gpio_mdio_probe(struct platform_device
*ofdev
)
209 struct device
*dev
= &ofdev
->dev
;
210 struct device_node
*np
= ofdev
->dev
.of_node
;
211 struct mii_bus
*new_bus
;
212 struct gpio_priv
*priv
;
213 const unsigned int *prop
;
217 priv
= kzalloc(sizeof(struct gpio_priv
), GFP_KERNEL
);
221 new_bus
= mdiobus_alloc();
226 new_bus
->name
= "pasemi gpio mdio bus";
227 new_bus
->read
= &gpio_mdio_read
;
228 new_bus
->write
= &gpio_mdio_write
;
229 new_bus
->reset
= &gpio_mdio_reset
;
231 prop
= of_get_property(np
, "reg", NULL
);
232 snprintf(new_bus
->id
, MII_BUS_ID_SIZE
, "%x", *prop
);
233 new_bus
->priv
= priv
;
235 prop
= of_get_property(np
, "mdc-pin", NULL
);
236 priv
->mdc_pin
= *prop
;
238 prop
= of_get_property(np
, "mdio-pin", NULL
);
239 priv
->mdio_pin
= *prop
;
241 new_bus
->parent
= dev
;
242 dev_set_drvdata(dev
, new_bus
);
244 err
= of_mdiobus_register(new_bus
, np
);
247 pr_err("%s: Cannot register as MDIO bus, err %d\n",
263 static int gpio_mdio_remove(struct platform_device
*dev
)
265 struct mii_bus
*bus
= dev_get_drvdata(&dev
->dev
);
267 mdiobus_unregister(bus
);
269 dev_set_drvdata(&dev
->dev
, NULL
);
278 static const struct of_device_id gpio_mdio_match
[] =
281 .compatible
= "gpio-mdio",
285 MODULE_DEVICE_TABLE(of
, gpio_mdio_match
);
287 static struct platform_driver gpio_mdio_driver
=
289 .probe
= gpio_mdio_probe
,
290 .remove
= gpio_mdio_remove
,
292 .name
= "gpio-mdio-bitbang",
293 .of_match_table
= gpio_mdio_match
,
297 static int gpio_mdio_init(void)
299 struct device_node
*np
;
301 np
= of_find_compatible_node(NULL
, NULL
, "1682m-gpio");
303 np
= of_find_compatible_node(NULL
, NULL
,
304 "pasemi,pwrficient-gpio");
307 gpio_regs
= of_iomap(np
, 0);
313 return platform_driver_register(&gpio_mdio_driver
);
315 module_init(gpio_mdio_init
);
317 static void gpio_mdio_exit(void)
319 platform_driver_unregister(&gpio_mdio_driver
);
323 module_exit(gpio_mdio_exit
);
325 MODULE_LICENSE("GPL");
326 MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
327 MODULE_DESCRIPTION("Driver for MDIO over GPIO on PA Semi PWRficient-based boards");