Commit | Line | Data |
---|---|---|
5b4b8454 VB |
1 | /* |
2 | * Combined Ethernet driver for Motorola MPC8xx and MPC82xx. | |
3 | * | |
4 | * Copyright (c) 2003 Intracom S.A. | |
5 | * by Pantelis Antoniou <panto@intracom.gr> | |
6 | * | |
7 | * 2005 (c) MontaVista Software, Inc. | |
8 | * Vitaly Bordug <vbordug@ru.mvista.com> | |
9 | * | |
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. | |
13 | */ | |
14 | ||
5b4b8454 VB |
15 | #include <linux/module.h> |
16 | #include <linux/types.h> | |
17 | #include <linux/kernel.h> | |
5b4b8454 VB |
18 | #include <linux/string.h> |
19 | #include <linux/ptrace.h> | |
20 | #include <linux/errno.h> | |
21 | #include <linux/ioport.h> | |
22 | #include <linux/slab.h> | |
23 | #include <linux/interrupt.h> | |
5b4b8454 VB |
24 | #include <linux/init.h> |
25 | #include <linux/delay.h> | |
26 | #include <linux/netdevice.h> | |
27 | #include <linux/etherdevice.h> | |
28 | #include <linux/skbuff.h> | |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/mii.h> | |
31 | #include <linux/ethtool.h> | |
32 | #include <linux/bitops.h> | |
33 | #include <linux/platform_device.h> | |
34 | ||
35 | #include <asm/pgtable.h> | |
36 | #include <asm/irq.h> | |
37 | #include <asm/uaccess.h> | |
38 | ||
976de6a8 SW |
39 | #ifdef CONFIG_PPC_CPM_NEW_BINDING |
40 | #include <asm/of_platform.h> | |
41 | #endif | |
42 | ||
5b4b8454 VB |
43 | #include "fs_enet.h" |
44 | #include "fec.h" | |
45 | ||
46 | /* Make MII read/write commands for the FEC. | |
47 | */ | |
48 | #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18)) | |
49 | #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff)) | |
50 | #define mk_mii_end 0 | |
51 | ||
52 | #define FEC_MII_LOOPS 10000 | |
53 | ||
976de6a8 | 54 | #ifndef CONFIG_PPC_CPM_NEW_BINDING |
5b4b8454 VB |
55 | static int match_has_phy (struct device *dev, void* data) |
56 | { | |
57 | struct platform_device* pdev = container_of(dev, struct platform_device, dev); | |
58 | struct fs_platform_info* fpi; | |
59 | if(strcmp(pdev->name, (char*)data)) | |
60 | { | |
61 | return 0; | |
62 | } | |
63 | ||
64 | fpi = pdev->dev.platform_data; | |
65 | if((fpi)&&(fpi->has_phy)) | |
66 | return 1; | |
67 | return 0; | |
68 | } | |
69 | ||
70 | static int fs_mii_fec_init(struct fec_info* fec, struct fs_mii_fec_platform_info *fmpi) | |
71 | { | |
72 | struct resource *r; | |
73 | fec_t *fecp; | |
74 | char* name = "fsl-cpm-fec"; | |
75 | ||
76 | /* we need fec in order to be useful */ | |
77 | struct platform_device *fec_pdev = | |
78 | container_of(bus_find_device(&platform_bus_type, NULL, name, match_has_phy), | |
79 | struct platform_device, dev); | |
80 | ||
81 | if(fec_pdev == NULL) { | |
82 | printk(KERN_ERR"Unable to find PHY for %s", name); | |
83 | return -ENODEV; | |
84 | } | |
85 | ||
86 | r = platform_get_resource_byname(fec_pdev, IORESOURCE_MEM, "regs"); | |
87 | ||
88 | fec->fecp = fecp = (fec_t*)ioremap(r->start,sizeof(fec_t)); | |
89 | fec->mii_speed = fmpi->mii_speed; | |
90 | ||
91 | setbits32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */ | |
92 | setbits32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN); | |
93 | out_be32(&fecp->fec_ievent, FEC_ENET_MII); | |
94 | out_be32(&fecp->fec_mii_speed, fec->mii_speed); | |
95 | ||
96 | return 0; | |
97 | } | |
976de6a8 | 98 | #endif |
5b4b8454 VB |
99 | |
100 | static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location) | |
101 | { | |
102 | struct fec_info* fec = bus->priv; | |
103 | fec_t *fecp = fec->fecp; | |
104 | int i, ret = -1; | |
105 | ||
106 | if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0) | |
107 | BUG(); | |
108 | ||
109 | /* Add PHY address to register command. */ | |
110 | out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location)); | |
111 | ||
112 | for (i = 0; i < FEC_MII_LOOPS; i++) | |
113 | if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0) | |
114 | break; | |
115 | ||
116 | if (i < FEC_MII_LOOPS) { | |
117 | out_be32(&fecp->fec_ievent, FEC_ENET_MII); | |
118 | ret = in_be32(&fecp->fec_mii_data) & 0xffff; | |
119 | } | |
120 | ||
121 | return ret; | |
5b4b8454 VB |
122 | } |
123 | ||
124 | static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val) | |
125 | { | |
126 | struct fec_info* fec = bus->priv; | |
127 | fec_t *fecp = fec->fecp; | |
128 | int i; | |
129 | ||
130 | /* this must never happen */ | |
131 | if ((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0) | |
132 | BUG(); | |
133 | ||
134 | /* Add PHY address to register command. */ | |
135 | out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val)); | |
136 | ||
137 | for (i = 0; i < FEC_MII_LOOPS; i++) | |
138 | if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0) | |
139 | break; | |
140 | ||
141 | if (i < FEC_MII_LOOPS) | |
142 | out_be32(&fecp->fec_ievent, FEC_ENET_MII); | |
143 | ||
144 | return 0; | |
145 | ||
146 | } | |
147 | ||
148 | static int fs_enet_fec_mii_reset(struct mii_bus *bus) | |
149 | { | |
150 | /* nothing here - for now */ | |
151 | return 0; | |
152 | } | |
153 | ||
976de6a8 SW |
154 | #ifdef CONFIG_PPC_CPM_NEW_BINDING |
155 | static void __devinit add_phy(struct mii_bus *bus, struct device_node *np) | |
156 | { | |
157 | const u32 *data; | |
158 | int len, id, irq; | |
159 | ||
160 | data = of_get_property(np, "reg", &len); | |
161 | if (!data || len != 4) | |
162 | return; | |
163 | ||
164 | id = *data; | |
165 | bus->phy_mask &= ~(1 << id); | |
166 | ||
167 | irq = of_irq_to_resource(np, 0, NULL); | |
168 | if (irq != NO_IRQ) | |
169 | bus->irq[id] = irq; | |
170 | } | |
171 | ||
172 | static int __devinit fs_enet_mdio_probe(struct of_device *ofdev, | |
173 | const struct of_device_id *match) | |
174 | { | |
175 | struct device_node *np = NULL; | |
176 | struct resource res; | |
177 | struct mii_bus *new_bus; | |
178 | struct fec_info *fec; | |
179 | int ret = -ENOMEM, i; | |
180 | ||
181 | new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL); | |
182 | if (!new_bus) | |
183 | goto out; | |
184 | ||
185 | fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL); | |
186 | if (!fec) | |
187 | goto out_mii; | |
188 | ||
189 | new_bus->priv = fec; | |
190 | new_bus->name = "FEC MII Bus"; | |
191 | new_bus->read = &fs_enet_fec_mii_read; | |
192 | new_bus->write = &fs_enet_fec_mii_write; | |
193 | new_bus->reset = &fs_enet_fec_mii_reset; | |
194 | ||
195 | ret = of_address_to_resource(ofdev->node, 0, &res); | |
196 | if (ret) | |
197 | return ret; | |
198 | ||
199 | new_bus->id = res.start; | |
200 | ||
201 | fec->fecp = ioremap(res.start, res.end - res.start + 1); | |
202 | if (!fec->fecp) | |
203 | goto out_fec; | |
204 | ||
205 | fec->mii_speed = ((ppc_proc_freq + 4999999) / 5000000) << 1; | |
206 | ||
207 | setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); | |
208 | setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | | |
209 | FEC_ECNTRL_ETHER_EN); | |
210 | out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII); | |
211 | out_be32(&fec->fecp->fec_mii_speed, fec->mii_speed); | |
212 | ||
213 | new_bus->phy_mask = ~0; | |
214 | new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); | |
215 | if (!new_bus->irq) | |
216 | goto out_unmap_regs; | |
217 | ||
218 | for (i = 0; i < PHY_MAX_ADDR; i++) | |
219 | new_bus->irq[i] = -1; | |
220 | ||
221 | while ((np = of_get_next_child(ofdev->node, np))) | |
222 | if (!strcmp(np->type, "ethernet-phy")) | |
223 | add_phy(new_bus, np); | |
224 | ||
225 | new_bus->dev = &ofdev->dev; | |
226 | dev_set_drvdata(&ofdev->dev, new_bus); | |
227 | ||
228 | ret = mdiobus_register(new_bus); | |
229 | if (ret) | |
230 | goto out_free_irqs; | |
231 | ||
232 | return 0; | |
233 | ||
234 | out_free_irqs: | |
235 | dev_set_drvdata(&ofdev->dev, NULL); | |
236 | kfree(new_bus->irq); | |
237 | out_unmap_regs: | |
238 | iounmap(fec->fecp); | |
239 | out_fec: | |
240 | kfree(fec); | |
241 | out_mii: | |
242 | kfree(new_bus); | |
243 | out: | |
244 | return ret; | |
245 | } | |
246 | ||
247 | static int fs_enet_mdio_remove(struct of_device *ofdev) | |
248 | { | |
249 | struct mii_bus *bus = dev_get_drvdata(&ofdev->dev); | |
250 | struct fec_info *fec = bus->priv; | |
251 | ||
252 | mdiobus_unregister(bus); | |
253 | dev_set_drvdata(&ofdev->dev, NULL); | |
254 | kfree(bus->irq); | |
255 | iounmap(fec->fecp); | |
256 | kfree(fec); | |
257 | kfree(bus); | |
258 | ||
259 | return 0; | |
260 | } | |
261 | ||
262 | static struct of_device_id fs_enet_mdio_fec_match[] = { | |
263 | { | |
264 | .compatible = "fsl,pq1-fec-mdio", | |
265 | }, | |
266 | {}, | |
267 | }; | |
268 | ||
269 | static struct of_platform_driver fs_enet_fec_mdio_driver = { | |
270 | .name = "fsl-fec-mdio", | |
271 | .match_table = fs_enet_mdio_fec_match, | |
272 | .probe = fs_enet_mdio_probe, | |
273 | .remove = fs_enet_mdio_remove, | |
274 | }; | |
275 | ||
276 | static int fs_enet_mdio_fec_init(void) | |
277 | { | |
278 | return of_register_platform_driver(&fs_enet_fec_mdio_driver); | |
279 | } | |
280 | ||
281 | static void fs_enet_mdio_fec_exit(void) | |
282 | { | |
283 | of_unregister_platform_driver(&fs_enet_fec_mdio_driver); | |
284 | } | |
285 | ||
286 | module_init(fs_enet_mdio_fec_init); | |
287 | module_exit(fs_enet_mdio_fec_exit); | |
288 | #else | |
5b4b8454 VB |
289 | static int __devinit fs_enet_fec_mdio_probe(struct device *dev) |
290 | { | |
291 | struct platform_device *pdev = to_platform_device(dev); | |
292 | struct fs_mii_fec_platform_info *pdata; | |
293 | struct mii_bus *new_bus; | |
294 | struct fec_info *fec; | |
295 | int err = 0; | |
296 | if (NULL == dev) | |
297 | return -EINVAL; | |
298 | new_bus = kzalloc(sizeof(struct mii_bus), GFP_KERNEL); | |
299 | ||
300 | if (NULL == new_bus) | |
301 | return -ENOMEM; | |
302 | ||
303 | fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL); | |
304 | ||
305 | if (NULL == fec) | |
306 | return -ENOMEM; | |
307 | ||
308 | new_bus->name = "FEC MII Bus", | |
309 | new_bus->read = &fs_enet_fec_mii_read, | |
310 | new_bus->write = &fs_enet_fec_mii_write, | |
311 | new_bus->reset = &fs_enet_fec_mii_reset, | |
312 | new_bus->id = pdev->id; | |
313 | ||
314 | pdata = (struct fs_mii_fec_platform_info *)pdev->dev.platform_data; | |
315 | ||
316 | if (NULL == pdata) { | |
317 | printk(KERN_ERR "fs_enet FEC mdio %d: Missing platform data!\n", pdev->id); | |
318 | return -ENODEV; | |
319 | } | |
320 | ||
321 | /*set up workspace*/ | |
322 | ||
323 | fs_mii_fec_init(fec, pdata); | |
324 | new_bus->priv = fec; | |
325 | ||
326 | new_bus->irq = pdata->irq; | |
327 | ||
328 | new_bus->dev = dev; | |
329 | dev_set_drvdata(dev, new_bus); | |
330 | ||
331 | err = mdiobus_register(new_bus); | |
332 | ||
333 | if (0 != err) { | |
334 | printk (KERN_ERR "%s: Cannot register as MDIO bus\n", | |
335 | new_bus->name); | |
336 | goto bus_register_fail; | |
337 | } | |
338 | ||
339 | return 0; | |
340 | ||
341 | bus_register_fail: | |
342 | kfree(new_bus); | |
343 | ||
344 | return err; | |
345 | } | |
346 | ||
347 | ||
348 | static int fs_enet_fec_mdio_remove(struct device *dev) | |
349 | { | |
350 | struct mii_bus *bus = dev_get_drvdata(dev); | |
351 | ||
352 | mdiobus_unregister(bus); | |
353 | ||
354 | dev_set_drvdata(dev, NULL); | |
355 | kfree(bus->priv); | |
356 | ||
357 | bus->priv = NULL; | |
358 | kfree(bus); | |
359 | ||
360 | return 0; | |
361 | } | |
362 | ||
363 | static struct device_driver fs_enet_fec_mdio_driver = { | |
364 | .name = "fsl-cpm-fec-mdio", | |
365 | .bus = &platform_bus_type, | |
366 | .probe = fs_enet_fec_mdio_probe, | |
367 | .remove = fs_enet_fec_mdio_remove, | |
368 | }; | |
369 | ||
370 | int fs_enet_mdio_fec_init(void) | |
371 | { | |
372 | return driver_register(&fs_enet_fec_mdio_driver); | |
373 | } | |
374 | ||
375 | void fs_enet_mdio_fec_exit(void) | |
376 | { | |
377 | driver_unregister(&fs_enet_fec_mdio_driver); | |
378 | } | |
976de6a8 | 379 | #endif |