]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/net/ethernet/freescale/fs_enet/mii-bitbang.c
drivers: clean-up prom.h implicit includes
[mirror_ubuntu-focal-kernel.git] / drivers / net / ethernet / freescale / fs_enet / mii-bitbang.c
CommitLineData
48257c4f
PA
1/*
2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
3 *
9b8ee8e7 4 * Copyright (c) 2003 Intracom S.A.
48257c4f 5 * by Pantelis Antoniou <panto@intracom.gr>
9b8ee8e7
VB
6 *
7 * 2005 (c) MontaVista Software, Inc.
48257c4f
PA
8 * Vitaly Bordug <vbordug@ru.mvista.com>
9 *
9b8ee8e7
VB
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
48257c4f
PA
12 * kind, whether express or implied.
13 */
14
48257c4f 15#include <linux/module.h>
48257c4f
PA
16#include <linux/ioport.h>
17#include <linux/slab.h>
48257c4f 18#include <linux/init.h>
2b5b3a60 19#include <linux/interrupt.h>
48257c4f
PA
20#include <linux/netdevice.h>
21#include <linux/etherdevice.h>
48257c4f 22#include <linux/mii.h>
5b4b8454 23#include <linux/platform_device.h>
2b5b3a60 24#include <linux/mdio-bitbang.h>
5af50730 25#include <linux/of_address.h>
aa73832c 26#include <linux/of_mdio.h>
976de6a8 27#include <linux/of_platform.h>
48257c4f
PA
28
29#include "fs_enet.h"
30
976de6a8 31struct bb_info {
2b5b3a60 32 struct mdiobb_ctrl ctrl;
976de6a8
SW
33 __be32 __iomem *dir;
34 __be32 __iomem *dat;
35 u32 mdio_msk;
36 u32 mdc_msk;
976de6a8
SW
37};
38
39/* FIXME: If any other users of GPIO crop up, then these will have to
40 * have some sort of global synchronization to avoid races with other
41 * pins on the same port. The ideal solution would probably be to
42 * bind the ports to a GPIO driver, and have this be a client of it.
43 */
44static inline void bb_set(u32 __iomem *p, u32 m)
48257c4f 45{
976de6a8 46 out_be32(p, in_be32(p) | m);
48257c4f
PA
47}
48
976de6a8 49static inline void bb_clr(u32 __iomem *p, u32 m)
48257c4f 50{
976de6a8 51 out_be32(p, in_be32(p) & ~m);
48257c4f
PA
52}
53
976de6a8 54static inline int bb_read(u32 __iomem *p, u32 m)
48257c4f 55{
976de6a8 56 return (in_be32(p) & m) != 0;
48257c4f
PA
57}
58
2b5b3a60 59static inline void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
48257c4f 60{
2b5b3a60 61 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
48257c4f 62
2b5b3a60
SW
63 if (dir)
64 bb_set(bitbang->dir, bitbang->mdio_msk);
65 else
66 bb_clr(bitbang->dir, bitbang->mdio_msk);
67
68 /* Read back to flush the write. */
69 in_be32(bitbang->dir);
48257c4f
PA
70}
71
2b5b3a60 72static inline int mdio_read(struct mdiobb_ctrl *ctrl)
48257c4f 73{
2b5b3a60 74 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
976de6a8 75 return bb_read(bitbang->dat, bitbang->mdio_msk);
48257c4f
PA
76}
77
2b5b3a60 78static inline void mdio(struct mdiobb_ctrl *ctrl, int what)
48257c4f 79{
2b5b3a60
SW
80 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
81
48257c4f 82 if (what)
976de6a8 83 bb_set(bitbang->dat, bitbang->mdio_msk);
48257c4f 84 else
976de6a8 85 bb_clr(bitbang->dat, bitbang->mdio_msk);
2b5b3a60
SW
86
87 /* Read back to flush the write. */
88 in_be32(bitbang->dat);
48257c4f
PA
89}
90
2b5b3a60 91static inline void mdc(struct mdiobb_ctrl *ctrl, int what)
48257c4f 92{
2b5b3a60
SW
93 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
94
48257c4f 95 if (what)
976de6a8 96 bb_set(bitbang->dat, bitbang->mdc_msk);
48257c4f 97 else
976de6a8 98 bb_clr(bitbang->dat, bitbang->mdc_msk);
48257c4f 99
2b5b3a60
SW
100 /* Read back to flush the write. */
101 in_be32(bitbang->dat);
48257c4f
PA
102}
103
2b5b3a60
SW
104static struct mdiobb_ops bb_ops = {
105 .owner = THIS_MODULE,
106 .set_mdc = mdc,
107 .set_mdio_dir = mdio_dir,
108 .set_mdio_data = mdio,
109 .get_mdio_data = mdio_read,
110};
5b4b8454 111
1dd06ae8 112static int fs_mii_bitbang_init(struct mii_bus *bus, struct device_node *np)
48257c4f 113{
976de6a8
SW
114 struct resource res;
115 const u32 *data;
116 int mdio_pin, mdc_pin, len;
117 struct bb_info *bitbang = bus->priv;
48257c4f 118
976de6a8
SW
119 int ret = of_address_to_resource(np, 0, &res);
120 if (ret)
121 return ret;
122
28f65c11 123 if (resource_size(&res) <= 13)
976de6a8
SW
124 return -ENODEV;
125
126 /* This should really encode the pin number as well, but all
127 * we get is an int, and the odds of multiple bitbang mdio buses
128 * is low enough that it's not worth going too crazy.
129 */
9d9326d3 130 snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start);
976de6a8
SW
131
132 data = of_get_property(np, "fsl,mdio-pin", &len);
133 if (!data || len != 4)
134 return -ENODEV;
135 mdio_pin = *data;
136
137 data = of_get_property(np, "fsl,mdc-pin", &len);
138 if (!data || len != 4)
139 return -ENODEV;
140 mdc_pin = *data;
141
28f65c11 142 bitbang->dir = ioremap(res.start, resource_size(&res));
976de6a8
SW
143 if (!bitbang->dir)
144 return -ENOMEM;
145
146 bitbang->dat = bitbang->dir + 4;
147 bitbang->mdio_msk = 1 << (31 - mdio_pin);
148 bitbang->mdc_msk = 1 << (31 - mdc_pin);
976de6a8
SW
149
150 return 0;
151}
152
4f2c53ea 153static int fs_enet_mdio_probe(struct platform_device *ofdev)
976de6a8 154{
976de6a8
SW
155 struct mii_bus *new_bus;
156 struct bb_info *bitbang;
157 int ret = -ENOMEM;
976de6a8 158
976de6a8
SW
159 bitbang = kzalloc(sizeof(struct bb_info), GFP_KERNEL);
160 if (!bitbang)
2b5b3a60
SW
161 goto out;
162
163 bitbang->ctrl.ops = &bb_ops;
164
165 new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
166 if (!new_bus)
167 goto out_free_priv;
976de6a8 168
976de6a8 169 new_bus->name = "CPM2 Bitbanged MII",
976de6a8 170
4eecb178 171 ret = fs_mii_bitbang_init(new_bus, ofdev->dev.of_node);
976de6a8 172 if (ret)
2b5b3a60 173 goto out_free_bus;
976de6a8
SW
174
175 new_bus->phy_mask = ~0;
176 new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
137bc99f
JL
177 if (!new_bus->irq) {
178 ret = -ENOMEM;
976de6a8 179 goto out_unmap_regs;
137bc99f 180 }
976de6a8 181
18ee49dd 182 new_bus->parent = &ofdev->dev;
8513fbd8 183 platform_set_drvdata(ofdev, new_bus);
976de6a8 184
4eecb178 185 ret = of_mdiobus_register(new_bus, ofdev->dev.of_node);
976de6a8
SW
186 if (ret)
187 goto out_free_irqs;
188
189 return 0;
190
191out_free_irqs:
976de6a8
SW
192 kfree(new_bus->irq);
193out_unmap_regs:
194 iounmap(bitbang->dir);
976de6a8 195out_free_bus:
2b5b3a60 196 free_mdio_bitbang(new_bus);
298cf9be
LB
197out_free_priv:
198 kfree(bitbang);
976de6a8
SW
199out:
200 return ret;
201}
202
2dc11581 203static int fs_enet_mdio_remove(struct platform_device *ofdev)
976de6a8 204{
8513fbd8 205 struct mii_bus *bus = platform_get_drvdata(ofdev);
976de6a8
SW
206 struct bb_info *bitbang = bus->priv;
207
208 mdiobus_unregister(bus);
976de6a8 209 kfree(bus->irq);
298cf9be 210 free_mdio_bitbang(bus);
976de6a8
SW
211 iounmap(bitbang->dir);
212 kfree(bitbang);
976de6a8
SW
213
214 return 0;
215}
216
217static struct of_device_id fs_enet_mdio_bb_match[] = {
218 {
219 .compatible = "fsl,cpm2-mdio-bitbang",
220 },
221 {},
222};
e72701ac 223MODULE_DEVICE_TABLE(of, fs_enet_mdio_bb_match);
976de6a8 224
74888760 225static struct platform_driver fs_enet_bb_mdio_driver = {
4018294b
GL
226 .driver = {
227 .name = "fsl-bb-mdio",
228 .owner = THIS_MODULE,
229 .of_match_table = fs_enet_mdio_bb_match,
230 },
976de6a8
SW
231 .probe = fs_enet_mdio_probe,
232 .remove = fs_enet_mdio_remove,
233};
234
db62f684 235module_platform_driver(fs_enet_bb_mdio_driver);