2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
10 * This file is licensed under the terms of the GNU General Public License
11 * version 2. This program is licensed "as is" without any warranty of any
12 * kind, whether express or implied.
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/netdevice.h>
21 #include <linux/etherdevice.h>
22 #include <linux/mii.h>
23 #include <linux/platform_device.h>
24 #include <linux/mdio-bitbang.h>
25 #include <linux/of_platform.h>
30 struct mdiobb_ctrl ctrl
;
37 /* FIXME: If any other users of GPIO crop up, then these will have to
38 * have some sort of global synchronization to avoid races with other
39 * pins on the same port. The ideal solution would probably be to
40 * bind the ports to a GPIO driver, and have this be a client of it.
42 static inline void bb_set(u32 __iomem
*p
, u32 m
)
44 out_be32(p
, in_be32(p
) | m
);
47 static inline void bb_clr(u32 __iomem
*p
, u32 m
)
49 out_be32(p
, in_be32(p
) & ~m
);
52 static inline int bb_read(u32 __iomem
*p
, u32 m
)
54 return (in_be32(p
) & m
) != 0;
57 static inline void mdio_dir(struct mdiobb_ctrl
*ctrl
, int dir
)
59 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
62 bb_set(bitbang
->dir
, bitbang
->mdio_msk
);
64 bb_clr(bitbang
->dir
, bitbang
->mdio_msk
);
66 /* Read back to flush the write. */
67 in_be32(bitbang
->dir
);
70 static inline int mdio_read(struct mdiobb_ctrl
*ctrl
)
72 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
73 return bb_read(bitbang
->dat
, bitbang
->mdio_msk
);
76 static inline void mdio(struct mdiobb_ctrl
*ctrl
, int what
)
78 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
81 bb_set(bitbang
->dat
, bitbang
->mdio_msk
);
83 bb_clr(bitbang
->dat
, bitbang
->mdio_msk
);
85 /* Read back to flush the write. */
86 in_be32(bitbang
->dat
);
89 static inline void mdc(struct mdiobb_ctrl
*ctrl
, int what
)
91 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
94 bb_set(bitbang
->dat
, bitbang
->mdc_msk
);
96 bb_clr(bitbang
->dat
, bitbang
->mdc_msk
);
98 /* Read back to flush the write. */
99 in_be32(bitbang
->dat
);
102 static struct mdiobb_ops bb_ops
= {
103 .owner
= THIS_MODULE
,
105 .set_mdio_dir
= mdio_dir
,
106 .set_mdio_data
= mdio
,
107 .get_mdio_data
= mdio_read
,
110 static int __devinit
fs_mii_bitbang_init(struct mii_bus
*bus
,
111 struct device_node
*np
)
115 int mdio_pin
, mdc_pin
, len
;
116 struct bb_info
*bitbang
= bus
->priv
;
118 int ret
= of_address_to_resource(np
, 0, &res
);
122 if (res
.end
- res
.start
< 13)
125 /* This should really encode the pin number as well, but all
126 * we get is an int, and the odds of multiple bitbang mdio buses
127 * is low enough that it's not worth going too crazy.
129 snprintf(bus
->id
, MII_BUS_ID_SIZE
, "%x", res
.start
);
131 data
= of_get_property(np
, "fsl,mdio-pin", &len
);
132 if (!data
|| len
!= 4)
136 data
= of_get_property(np
, "fsl,mdc-pin", &len
);
137 if (!data
|| len
!= 4)
141 bitbang
->dir
= ioremap(res
.start
, res
.end
- res
.start
+ 1);
145 bitbang
->dat
= bitbang
->dir
+ 4;
146 bitbang
->mdio_msk
= 1 << (31 - mdio_pin
);
147 bitbang
->mdc_msk
= 1 << (31 - mdc_pin
);
152 static void __devinit
add_phy(struct mii_bus
*bus
, struct device_node
*np
)
157 data
= of_get_property(np
, "reg", &len
);
158 if (!data
|| len
!= 4)
162 bus
->phy_mask
&= ~(1 << id
);
164 irq
= of_irq_to_resource(np
, 0, NULL
);
169 static int __devinit
fs_enet_mdio_probe(struct of_device
*ofdev
,
170 const struct of_device_id
*match
)
172 struct device_node
*np
= NULL
;
173 struct mii_bus
*new_bus
;
174 struct bb_info
*bitbang
;
178 bitbang
= kzalloc(sizeof(struct bb_info
), GFP_KERNEL
);
182 bitbang
->ctrl
.ops
= &bb_ops
;
184 new_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
188 new_bus
->name
= "CPM2 Bitbanged MII",
190 ret
= fs_mii_bitbang_init(new_bus
, ofdev
->node
);
194 new_bus
->phy_mask
= ~0;
195 new_bus
->irq
= kmalloc(sizeof(int) * PHY_MAX_ADDR
, GFP_KERNEL
);
199 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
200 new_bus
->irq
[i
] = -1;
202 while ((np
= of_get_next_child(ofdev
->node
, np
)))
203 if (!strcmp(np
->type
, "ethernet-phy"))
204 add_phy(new_bus
, np
);
206 new_bus
->dev
= &ofdev
->dev
;
207 dev_set_drvdata(&ofdev
->dev
, new_bus
);
209 ret
= mdiobus_register(new_bus
);
216 dev_set_drvdata(&ofdev
->dev
, NULL
);
219 iounmap(bitbang
->dir
);
223 free_mdio_bitbang(new_bus
);
228 static int fs_enet_mdio_remove(struct of_device
*ofdev
)
230 struct mii_bus
*bus
= dev_get_drvdata(&ofdev
->dev
);
231 struct bb_info
*bitbang
= bus
->priv
;
233 mdiobus_unregister(bus
);
234 free_mdio_bitbang(bus
);
235 dev_set_drvdata(&ofdev
->dev
, NULL
);
237 iounmap(bitbang
->dir
);
244 static struct of_device_id fs_enet_mdio_bb_match
[] = {
246 .compatible
= "fsl,cpm2-mdio-bitbang",
251 static struct of_platform_driver fs_enet_bb_mdio_driver
= {
252 .name
= "fsl-bb-mdio",
253 .match_table
= fs_enet_mdio_bb_match
,
254 .probe
= fs_enet_mdio_probe
,
255 .remove
= fs_enet_mdio_remove
,
258 static int fs_enet_mdio_bb_init(void)
260 return of_register_platform_driver(&fs_enet_bb_mdio_driver
);
263 static void fs_enet_mdio_bb_exit(void)
265 of_unregister_platform_driver(&fs_enet_bb_mdio_driver
);
268 module_init(fs_enet_mdio_bb_init
);
269 module_exit(fs_enet_mdio_bb_exit
);