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/interrupt.h>
19 #include <linux/netdevice.h>
20 #include <linux/etherdevice.h>
21 #include <linux/mii.h>
22 #include <linux/platform_device.h>
23 #include <linux/mdio-bitbang.h>
24 #include <linux/of_address.h>
25 #include <linux/of_mdio.h>
26 #include <linux/of_platform.h>
31 struct mdiobb_ctrl ctrl
;
38 /* FIXME: If any other users of GPIO crop up, then these will have to
39 * have some sort of global synchronization to avoid races with other
40 * pins on the same port. The ideal solution would probably be to
41 * bind the ports to a GPIO driver, and have this be a client of it.
43 static inline void bb_set(u32 __iomem
*p
, u32 m
)
45 out_be32(p
, in_be32(p
) | m
);
48 static inline void bb_clr(u32 __iomem
*p
, u32 m
)
50 out_be32(p
, in_be32(p
) & ~m
);
53 static inline int bb_read(u32 __iomem
*p
, u32 m
)
55 return (in_be32(p
) & m
) != 0;
58 static inline void mdio_dir(struct mdiobb_ctrl
*ctrl
, int dir
)
60 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
63 bb_set(bitbang
->dir
, bitbang
->mdio_msk
);
65 bb_clr(bitbang
->dir
, bitbang
->mdio_msk
);
67 /* Read back to flush the write. */
68 in_be32(bitbang
->dir
);
71 static inline int mdio_read(struct mdiobb_ctrl
*ctrl
)
73 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
74 return bb_read(bitbang
->dat
, bitbang
->mdio_msk
);
77 static inline void mdio(struct mdiobb_ctrl
*ctrl
, int what
)
79 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
82 bb_set(bitbang
->dat
, bitbang
->mdio_msk
);
84 bb_clr(bitbang
->dat
, bitbang
->mdio_msk
);
86 /* Read back to flush the write. */
87 in_be32(bitbang
->dat
);
90 static inline void mdc(struct mdiobb_ctrl
*ctrl
, int what
)
92 struct bb_info
*bitbang
= container_of(ctrl
, struct bb_info
, ctrl
);
95 bb_set(bitbang
->dat
, bitbang
->mdc_msk
);
97 bb_clr(bitbang
->dat
, bitbang
->mdc_msk
);
99 /* Read back to flush the write. */
100 in_be32(bitbang
->dat
);
103 static const struct mdiobb_ops bb_ops
= {
104 .owner
= THIS_MODULE
,
106 .set_mdio_dir
= mdio_dir
,
107 .set_mdio_data
= mdio
,
108 .get_mdio_data
= mdio_read
,
111 static int fs_mii_bitbang_init(struct mii_bus
*bus
, 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 (resource_size(&res
) <= 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
, resource_size(&res
));
145 bitbang
->dat
= bitbang
->dir
+ 4;
146 bitbang
->mdio_msk
= 1 << (31 - mdio_pin
);
147 bitbang
->mdc_msk
= 1 << (31 - mdc_pin
);
152 static int fs_enet_mdio_probe(struct platform_device
*ofdev
)
154 struct mii_bus
*new_bus
;
155 struct bb_info
*bitbang
;
158 bitbang
= kzalloc(sizeof(struct bb_info
), GFP_KERNEL
);
162 bitbang
->ctrl
.ops
= &bb_ops
;
164 new_bus
= alloc_mdio_bitbang(&bitbang
->ctrl
);
168 new_bus
->name
= "CPM2 Bitbanged MII",
170 ret
= fs_mii_bitbang_init(new_bus
, ofdev
->dev
.of_node
);
174 new_bus
->phy_mask
= ~0;
176 new_bus
->parent
= &ofdev
->dev
;
177 platform_set_drvdata(ofdev
, new_bus
);
179 ret
= of_mdiobus_register(new_bus
, ofdev
->dev
.of_node
);
186 iounmap(bitbang
->dir
);
188 free_mdio_bitbang(new_bus
);
195 static int fs_enet_mdio_remove(struct platform_device
*ofdev
)
197 struct mii_bus
*bus
= platform_get_drvdata(ofdev
);
198 struct bb_info
*bitbang
= bus
->priv
;
200 mdiobus_unregister(bus
);
201 free_mdio_bitbang(bus
);
202 iounmap(bitbang
->dir
);
208 static const struct of_device_id fs_enet_mdio_bb_match
[] = {
210 .compatible
= "fsl,cpm2-mdio-bitbang",
214 MODULE_DEVICE_TABLE(of
, fs_enet_mdio_bb_match
);
216 static struct platform_driver fs_enet_bb_mdio_driver
= {
218 .name
= "fsl-bb-mdio",
219 .of_match_table
= fs_enet_mdio_bb_match
,
221 .probe
= fs_enet_mdio_probe
,
222 .remove
= fs_enet_mdio_remove
,
225 module_platform_driver(fs_enet_bb_mdio_driver
);