This source file includes following definitions.
- axienet_mdio_wait_until_ready
- axienet_mdio_read
- axienet_mdio_write
- axienet_mdio_enable
- axienet_mdio_disable
- axienet_mdio_setup
- axienet_mdio_teardown
1
2
3
4
5
6
7
8
9
10
11
12 #include <linux/clk.h>
13 #include <linux/of_address.h>
14 #include <linux/of_mdio.h>
15 #include <linux/jiffies.h>
16 #include <linux/iopoll.h>
17
18 #include "xilinx_axienet.h"
19
20 #define MAX_MDIO_FREQ 2500000
21 #define DEFAULT_HOST_CLOCK 150000000
22
23
24 static int axienet_mdio_wait_until_ready(struct axienet_local *lp)
25 {
26 u32 val;
27
28 return readx_poll_timeout(axinet_ior_read_mcr, lp,
29 val, val & XAE_MDIO_MCR_READY_MASK,
30 1, 20000);
31 }
32
33
34
35
36
37
38
39
40
41
42
43
44
45 static int axienet_mdio_read(struct mii_bus *bus, int phy_id, int reg)
46 {
47 u32 rc;
48 int ret;
49 struct axienet_local *lp = bus->priv;
50
51 ret = axienet_mdio_wait_until_ready(lp);
52 if (ret < 0)
53 return ret;
54
55 axienet_iow(lp, XAE_MDIO_MCR_OFFSET,
56 (((phy_id << XAE_MDIO_MCR_PHYAD_SHIFT) &
57 XAE_MDIO_MCR_PHYAD_MASK) |
58 ((reg << XAE_MDIO_MCR_REGAD_SHIFT) &
59 XAE_MDIO_MCR_REGAD_MASK) |
60 XAE_MDIO_MCR_INITIATE_MASK |
61 XAE_MDIO_MCR_OP_READ_MASK));
62
63 ret = axienet_mdio_wait_until_ready(lp);
64 if (ret < 0)
65 return ret;
66
67 rc = axienet_ior(lp, XAE_MDIO_MRD_OFFSET) & 0x0000FFFF;
68
69 dev_dbg(lp->dev, "axienet_mdio_read(phy_id=%i, reg=%x) == %x\n",
70 phy_id, reg, rc);
71
72 return rc;
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 static int axienet_mdio_write(struct mii_bus *bus, int phy_id, int reg,
89 u16 val)
90 {
91 int ret;
92 struct axienet_local *lp = bus->priv;
93
94 dev_dbg(lp->dev, "axienet_mdio_write(phy_id=%i, reg=%x, val=%x)\n",
95 phy_id, reg, val);
96
97 ret = axienet_mdio_wait_until_ready(lp);
98 if (ret < 0)
99 return ret;
100
101 axienet_iow(lp, XAE_MDIO_MWD_OFFSET, (u32) val);
102 axienet_iow(lp, XAE_MDIO_MCR_OFFSET,
103 (((phy_id << XAE_MDIO_MCR_PHYAD_SHIFT) &
104 XAE_MDIO_MCR_PHYAD_MASK) |
105 ((reg << XAE_MDIO_MCR_REGAD_SHIFT) &
106 XAE_MDIO_MCR_REGAD_MASK) |
107 XAE_MDIO_MCR_INITIATE_MASK |
108 XAE_MDIO_MCR_OP_WRITE_MASK));
109
110 ret = axienet_mdio_wait_until_ready(lp);
111 if (ret < 0)
112 return ret;
113 return 0;
114 }
115
116
117
118
119
120
121
122
123
124
125 int axienet_mdio_enable(struct axienet_local *lp)
126 {
127 u32 clk_div, host_clock;
128
129 if (lp->clk) {
130 host_clock = clk_get_rate(lp->clk);
131 } else {
132 struct device_node *np1;
133
134
135
136
137 np1 = of_find_node_by_name(NULL, "cpu");
138 if (!np1) {
139 netdev_warn(lp->ndev, "Could not find CPU device node.\n");
140 host_clock = DEFAULT_HOST_CLOCK;
141 } else {
142 int ret = of_property_read_u32(np1, "clock-frequency",
143 &host_clock);
144 if (ret) {
145 netdev_warn(lp->ndev, "CPU clock-frequency property not found.\n");
146 host_clock = DEFAULT_HOST_CLOCK;
147 }
148 of_node_put(np1);
149 }
150 netdev_info(lp->ndev, "Setting assumed host clock to %u\n",
151 host_clock);
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 clk_div = (host_clock / (MAX_MDIO_FREQ * 2)) - 1;
180
181
182
183
184 if (host_clock % (MAX_MDIO_FREQ * 2))
185 clk_div++;
186
187 netdev_dbg(lp->ndev,
188 "Setting MDIO clock divisor to %u/%u Hz host clock.\n",
189 clk_div, host_clock);
190
191 axienet_iow(lp, XAE_MDIO_MC_OFFSET, clk_div | XAE_MDIO_MC_MDIOEN_MASK);
192
193 return axienet_mdio_wait_until_ready(lp);
194 }
195
196
197
198
199
200
201
202 void axienet_mdio_disable(struct axienet_local *lp)
203 {
204 axienet_iow(lp, XAE_MDIO_MC_OFFSET, 0);
205 }
206
207
208
209
210
211
212
213
214
215
216
217 int axienet_mdio_setup(struct axienet_local *lp)
218 {
219 struct device_node *mdio_node;
220 struct mii_bus *bus;
221 int ret;
222
223 ret = axienet_mdio_enable(lp);
224 if (ret < 0)
225 return ret;
226
227 bus = mdiobus_alloc();
228 if (!bus)
229 return -ENOMEM;
230
231 snprintf(bus->id, MII_BUS_ID_SIZE, "axienet-%.8llx",
232 (unsigned long long)lp->regs_start);
233
234 bus->priv = lp;
235 bus->name = "Xilinx Axi Ethernet MDIO";
236 bus->read = axienet_mdio_read;
237 bus->write = axienet_mdio_write;
238 bus->parent = lp->dev;
239 lp->mii_bus = bus;
240
241 mdio_node = of_get_child_by_name(lp->dev->of_node, "mdio");
242 ret = of_mdiobus_register(bus, mdio_node);
243 of_node_put(mdio_node);
244 if (ret) {
245 mdiobus_free(bus);
246 lp->mii_bus = NULL;
247 return ret;
248 }
249 return 0;
250 }
251
252
253
254
255
256
257
258 void axienet_mdio_teardown(struct axienet_local *lp)
259 {
260 mdiobus_unregister(lp->mii_bus);
261 mdiobus_free(lp->mii_bus);
262 lp->mii_bus = NULL;
263 }