2 * Copyright (C) 2006-2007 PA Semi, Inc
4 * Author: Olof Johansson, PA Semi
6 * Maintained by: Olof Johansson <olof@lixom.net>
8 * Based on drivers/net/fs_enet/mii-bitbang.c.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/interrupt.h>
32 #include <linux/phy.h>
33 #include <linux/of_address.h>
34 #include <linux/of_mdio.h>
35 #include <linux/of_platform.h>
39 static void __iomem
*gpio_regs
;
44 int mdio_irqs
[PHY_MAX_ADDR
];
47 #define MDC_PIN(bus) (((struct gpio_priv *)bus->priv)->mdc_pin)
48 #define MDIO_PIN(bus) (((struct gpio_priv *)bus->priv)->mdio_pin)
50 static inline void mdio_lo(struct mii_bus
*bus
)
52 out_le32(gpio_regs
+0x10, 1 << MDIO_PIN(bus
));
55 static inline void mdio_hi(struct mii_bus
*bus
)
57 out_le32(gpio_regs
, 1 << MDIO_PIN(bus
));
60 static inline void mdc_lo(struct mii_bus
*bus
)
62 out_le32(gpio_regs
+0x10, 1 << MDC_PIN(bus
));
65 static inline void mdc_hi(struct mii_bus
*bus
)
67 out_le32(gpio_regs
, 1 << MDC_PIN(bus
));
70 static inline void mdio_active(struct mii_bus
*bus
)
72 out_le32(gpio_regs
+0x20, (1 << MDC_PIN(bus
)) | (1 << MDIO_PIN(bus
)));
75 static inline void mdio_tristate(struct mii_bus
*bus
)
77 out_le32(gpio_regs
+0x30, (1 << MDIO_PIN(bus
)));
80 static inline int mdio_read(struct mii_bus
*bus
)
82 return !!(in_le32(gpio_regs
+0x40) & (1 << MDIO_PIN(bus
)));
85 static void clock_out(struct mii_bus
*bus
, int bit
)
97 /* Utility to send the preamble, address, and register (common to read and write). */
98 static void bitbang_pre(struct mii_bus
*bus
, int read
, u8 addr
, u8 reg
)
102 /* CFE uses a really long preamble (40 bits). We'll do the same. */
104 for (i
= 0; i
< 40; i
++) {
108 /* send the start bit (01) and the read opcode (10) or write (10) */
112 clock_out(bus
, read
);
113 clock_out(bus
, !read
);
115 /* send the PHY address */
116 for (i
= 0; i
< 5; i
++) {
117 clock_out(bus
, (addr
& 0x10) != 0);
121 /* send the register address */
122 for (i
= 0; i
< 5; i
++) {
123 clock_out(bus
, (reg
& 0x10) != 0);
128 static int gpio_mdio_read(struct mii_bus
*bus
, int phy_id
, int location
)
132 u8 addr
= phy_id
& 0xff;
133 u8 reg
= location
& 0xff;
135 bitbang_pre(bus
, 1, addr
, reg
);
137 /* tri-state our MDIO I/O pin so we can read */
144 /* read 16 bits of register data, MSB first */
146 for (i
= 0; i
< 16; i
++) {
154 rdreg
|= mdio_read(bus
);
167 static int gpio_mdio_write(struct mii_bus
*bus
, int phy_id
, int location
, u16 val
)
171 u8 addr
= phy_id
& 0xff;
172 u8 reg
= location
& 0xff;
173 u16 value
= val
& 0xffff;
175 bitbang_pre(bus
, 0, addr
, reg
);
177 /* send the turnaround (10) */
189 /* write 16 bits of register data, MSB first */
190 for (i
= 0; i
< 16; i
++) {
203 * Tri-state the MDIO line.
213 static int gpio_mdio_reset(struct mii_bus
*bus
)
215 /*nothing here - dunno how to reset it*/
220 static int gpio_mdio_probe(struct platform_device
*ofdev
)
222 struct device
*dev
= &ofdev
->dev
;
223 struct device_node
*np
= ofdev
->dev
.of_node
;
224 struct mii_bus
*new_bus
;
225 struct gpio_priv
*priv
;
226 const unsigned int *prop
;
230 priv
= kzalloc(sizeof(struct gpio_priv
), GFP_KERNEL
);
234 new_bus
= mdiobus_alloc();
239 new_bus
->name
= "pasemi gpio mdio bus";
240 new_bus
->read
= &gpio_mdio_read
;
241 new_bus
->write
= &gpio_mdio_write
;
242 new_bus
->reset
= &gpio_mdio_reset
;
244 prop
= of_get_property(np
, "reg", NULL
);
245 snprintf(new_bus
->id
, MII_BUS_ID_SIZE
, "%x", *prop
);
246 new_bus
->priv
= priv
;
248 new_bus
->irq
= priv
->mdio_irqs
;
250 prop
= of_get_property(np
, "mdc-pin", NULL
);
251 priv
->mdc_pin
= *prop
;
253 prop
= of_get_property(np
, "mdio-pin", NULL
);
254 priv
->mdio_pin
= *prop
;
256 new_bus
->parent
= dev
;
257 dev_set_drvdata(dev
, new_bus
);
259 err
= of_mdiobus_register(new_bus
, np
);
262 printk(KERN_ERR
"%s: Cannot register as MDIO bus, err %d\n",
278 static int gpio_mdio_remove(struct platform_device
*dev
)
280 struct mii_bus
*bus
= dev_get_drvdata(&dev
->dev
);
282 mdiobus_unregister(bus
);
284 dev_set_drvdata(&dev
->dev
, NULL
);
293 static const struct of_device_id gpio_mdio_match
[] =
296 .compatible
= "gpio-mdio",
300 MODULE_DEVICE_TABLE(of
, gpio_mdio_match
);
302 static struct platform_driver gpio_mdio_driver
=
304 .probe
= gpio_mdio_probe
,
305 .remove
= gpio_mdio_remove
,
307 .name
= "gpio-mdio-bitbang",
308 .of_match_table
= gpio_mdio_match
,
312 int gpio_mdio_init(void)
314 struct device_node
*np
;
316 np
= of_find_compatible_node(NULL
, NULL
, "1682m-gpio");
318 np
= of_find_compatible_node(NULL
, NULL
,
319 "pasemi,pwrficient-gpio");
322 gpio_regs
= of_iomap(np
, 0);
328 return platform_driver_register(&gpio_mdio_driver
);
330 module_init(gpio_mdio_init
);
332 void gpio_mdio_exit(void)
334 platform_driver_unregister(&gpio_mdio_driver
);
338 module_exit(gpio_mdio_exit
);
340 MODULE_LICENSE("GPL");
341 MODULE_AUTHOR("Olof Johansson <olof@lixom.net>");
342 MODULE_DESCRIPTION("Driver for MDIO over GPIO on PA Semi PWRficient-based boards");