Commit | Line | Data |
---|---|---|
5d031e9e DP |
1 | /* |
2 | * Driver for the MPC5200 Fast Ethernet Controller - MDIO bus driver | |
3 | * | |
4 | * Copyright (C) 2007 Domen Puncer, Telargo, Inc. | |
51d9861e | 5 | * Copyright (C) 2008 Wolfram Sang, Pengutronix |
5d031e9e DP |
6 | * |
7 | * This file is licensed under the terms of the GNU General Public License | |
8 | * version 2. This program is licensed "as is" without any warranty of any | |
9 | * kind, whether express or implied. | |
10 | */ | |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/netdevice.h> | |
15 | #include <linux/phy.h> | |
16 | #include <linux/of_platform.h> | |
ca816d98 | 17 | #include <linux/of_mdio.h> |
5d031e9e DP |
18 | #include <asm/io.h> |
19 | #include <asm/mpc52xx.h> | |
20 | #include "fec_mpc52xx.h" | |
21 | ||
22 | struct mpc52xx_fec_mdio_priv { | |
23 | struct mpc52xx_fec __iomem *regs; | |
ca816d98 | 24 | int mdio_irqs[PHY_MAX_ADDR]; |
5d031e9e DP |
25 | }; |
26 | ||
51d9861e WS |
27 | static int mpc52xx_fec_mdio_transfer(struct mii_bus *bus, int phy_id, |
28 | int reg, u32 value) | |
5d031e9e DP |
29 | { |
30 | struct mpc52xx_fec_mdio_priv *priv = bus->priv; | |
31 | struct mpc52xx_fec __iomem *fec; | |
ca816d98 | 32 | int tries = 3; |
51d9861e WS |
33 | |
34 | value |= (phy_id << FEC_MII_DATA_PA_SHIFT) & FEC_MII_DATA_PA_MSK; | |
35 | value |= (reg << FEC_MII_DATA_RA_SHIFT) & FEC_MII_DATA_RA_MSK; | |
5d031e9e DP |
36 | |
37 | fec = priv->regs; | |
38 | out_be32(&fec->ievent, FEC_IEVENT_MII); | |
51d9861e | 39 | out_be32(&priv->regs->mii_data, value); |
5d031e9e DP |
40 | |
41 | /* wait for it to finish, this takes about 23 us on lite5200b */ | |
42 | while (!(in_be32(&fec->ievent) & FEC_IEVENT_MII) && --tries) | |
ca816d98 | 43 | msleep(1); |
5d031e9e | 44 | |
51d9861e | 45 | if (!tries) |
5d031e9e DP |
46 | return -ETIMEDOUT; |
47 | ||
51d9861e WS |
48 | return value & FEC_MII_DATA_OP_RD ? |
49 | in_be32(&priv->regs->mii_data) & FEC_MII_DATA_DATAMSK : 0; | |
5d031e9e DP |
50 | } |
51 | ||
51d9861e | 52 | static int mpc52xx_fec_mdio_read(struct mii_bus *bus, int phy_id, int reg) |
5d031e9e | 53 | { |
51d9861e WS |
54 | return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, FEC_MII_READ_FRAME); |
55 | } | |
5d031e9e | 56 | |
51d9861e WS |
57 | static int mpc52xx_fec_mdio_write(struct mii_bus *bus, int phy_id, int reg, |
58 | u16 data) | |
59 | { | |
60 | return mpc52xx_fec_mdio_transfer(bus, phy_id, reg, | |
61 | data | FEC_MII_WRITE_FRAME); | |
5d031e9e DP |
62 | } |
63 | ||
51d9861e WS |
64 | static int mpc52xx_fec_mdio_probe(struct of_device *of, |
65 | const struct of_device_id *match) | |
5d031e9e DP |
66 | { |
67 | struct device *dev = &of->dev; | |
68 | struct device_node *np = of->node; | |
5d031e9e DP |
69 | struct mii_bus *bus; |
70 | struct mpc52xx_fec_mdio_priv *priv; | |
71 | struct resource res = {}; | |
72 | int err; | |
73 | int i; | |
74 | ||
298cf9be | 75 | bus = mdiobus_alloc(); |
5d031e9e DP |
76 | if (bus == NULL) |
77 | return -ENOMEM; | |
78 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
79 | if (priv == NULL) { | |
80 | err = -ENOMEM; | |
81 | goto out_free; | |
82 | } | |
83 | ||
84 | bus->name = "mpc52xx MII bus"; | |
85 | bus->read = mpc52xx_fec_mdio_read; | |
86 | bus->write = mpc52xx_fec_mdio_write; | |
87 | ||
88 | /* setup irqs */ | |
ca816d98 | 89 | bus->irq = priv->mdio_irqs; |
5d031e9e DP |
90 | |
91 | /* setup registers */ | |
92 | err = of_address_to_resource(np, 0, &res); | |
93 | if (err) | |
94 | goto out_free; | |
95 | priv->regs = ioremap(res.start, res.end - res.start + 1); | |
96 | if (priv->regs == NULL) { | |
97 | err = -ENOMEM; | |
98 | goto out_free; | |
99 | } | |
100 | ||
9d9326d3 | 101 | snprintf(bus->id, MII_BUS_ID_SIZE, "%x", res.start); |
5d031e9e DP |
102 | bus->priv = priv; |
103 | ||
18ee49dd | 104 | bus->parent = dev; |
5d031e9e DP |
105 | dev_set_drvdata(dev, bus); |
106 | ||
107 | /* set MII speed */ | |
51d9861e | 108 | out_be32(&priv->regs->mii_speed, |
87c441e5 | 109 | ((mpc5xxx_get_bus_frequency(of->node) >> 20) / 5) << 1); |
5d031e9e | 110 | |
ca816d98 | 111 | err = of_mdiobus_register(bus, np); |
5d031e9e DP |
112 | if (err) |
113 | goto out_unmap; | |
114 | ||
115 | return 0; | |
116 | ||
117 | out_unmap: | |
118 | iounmap(priv->regs); | |
119 | out_free: | |
120 | for (i=0; i<PHY_MAX_ADDR; i++) | |
121 | if (bus->irq[i] != PHY_POLL) | |
122 | irq_dispose_mapping(bus->irq[i]); | |
123 | kfree(bus->irq); | |
124 | kfree(priv); | |
298cf9be | 125 | mdiobus_free(bus); |
5d031e9e DP |
126 | |
127 | return err; | |
128 | } | |
129 | ||
130 | static int mpc52xx_fec_mdio_remove(struct of_device *of) | |
131 | { | |
132 | struct device *dev = &of->dev; | |
133 | struct mii_bus *bus = dev_get_drvdata(dev); | |
134 | struct mpc52xx_fec_mdio_priv *priv = bus->priv; | |
135 | int i; | |
136 | ||
137 | mdiobus_unregister(bus); | |
138 | dev_set_drvdata(dev, NULL); | |
139 | ||
140 | iounmap(priv->regs); | |
141 | for (i=0; i<PHY_MAX_ADDR; i++) | |
e51f47a5 | 142 | if (bus->irq[i] != PHY_POLL) |
5d031e9e DP |
143 | irq_dispose_mapping(bus->irq[i]); |
144 | kfree(priv); | |
145 | kfree(bus->irq); | |
298cf9be | 146 | mdiobus_free(bus); |
5d031e9e DP |
147 | |
148 | return 0; | |
149 | } | |
150 | ||
151 | ||
152 | static struct of_device_id mpc52xx_fec_mdio_match[] = { | |
66ffbe49 | 153 | { .compatible = "fsl,mpc5200b-mdio", }, |
8d813941 | 154 | { .compatible = "fsl,mpc5200-mdio", }, |
66ffbe49 GL |
155 | { .compatible = "mpc5200b-fec-phy", }, |
156 | {} | |
5d031e9e | 157 | }; |
e72701ac | 158 | MODULE_DEVICE_TABLE(of, mpc52xx_fec_mdio_match); |
5d031e9e DP |
159 | |
160 | struct of_platform_driver mpc52xx_fec_mdio_driver = { | |
161 | .name = "mpc5200b-fec-phy", | |
162 | .probe = mpc52xx_fec_mdio_probe, | |
163 | .remove = mpc52xx_fec_mdio_remove, | |
164 | .match_table = mpc52xx_fec_mdio_match, | |
165 | }; | |
166 | ||
167 | /* let fec driver call it, since this has to be registered before it */ | |
168 | EXPORT_SYMBOL_GPL(mpc52xx_fec_mdio_driver); | |
169 | ||
170 | ||
171 | MODULE_LICENSE("Dual BSD/GPL"); |