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> | |
b219108c | 34 | #include <linux/of_platform.h> |
5b4b8454 VB |
35 | |
36 | #include <asm/pgtable.h> | |
37 | #include <asm/irq.h> | |
38 | #include <asm/uaccess.h> | |
39 | ||
40 | #include "fs_enet.h" | |
41 | #include "fec.h" | |
42 | ||
43 | /* Make MII read/write commands for the FEC. | |
44 | */ | |
45 | #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18)) | |
46 | #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff)) | |
47 | #define mk_mii_end 0 | |
48 | ||
49 | #define FEC_MII_LOOPS 10000 | |
50 | ||
5b4b8454 VB |
51 | static int fs_enet_fec_mii_read(struct mii_bus *bus , int phy_id, int location) |
52 | { | |
53 | struct fec_info* fec = bus->priv; | |
31a5bb04 | 54 | fec_t __iomem *fecp = fec->fecp; |
5b4b8454 VB |
55 | int i, ret = -1; |
56 | ||
0ee904c3 | 57 | BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0); |
5b4b8454 VB |
58 | |
59 | /* Add PHY address to register command. */ | |
60 | out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_read(location)); | |
61 | ||
62 | for (i = 0; i < FEC_MII_LOOPS; i++) | |
63 | if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0) | |
64 | break; | |
65 | ||
66 | if (i < FEC_MII_LOOPS) { | |
67 | out_be32(&fecp->fec_ievent, FEC_ENET_MII); | |
68 | ret = in_be32(&fecp->fec_mii_data) & 0xffff; | |
69 | } | |
70 | ||
71 | return ret; | |
5b4b8454 VB |
72 | } |
73 | ||
74 | static int fs_enet_fec_mii_write(struct mii_bus *bus, int phy_id, int location, u16 val) | |
75 | { | |
76 | struct fec_info* fec = bus->priv; | |
31a5bb04 | 77 | fec_t __iomem *fecp = fec->fecp; |
5b4b8454 VB |
78 | int i; |
79 | ||
80 | /* this must never happen */ | |
0ee904c3 | 81 | BUG_ON((in_be32(&fecp->fec_r_cntrl) & FEC_RCNTRL_MII_MODE) == 0); |
5b4b8454 VB |
82 | |
83 | /* Add PHY address to register command. */ | |
84 | out_be32(&fecp->fec_mii_data, (phy_id << 23) | mk_mii_write(location, val)); | |
85 | ||
86 | for (i = 0; i < FEC_MII_LOOPS; i++) | |
87 | if ((in_be32(&fecp->fec_ievent) & FEC_ENET_MII) != 0) | |
88 | break; | |
89 | ||
90 | if (i < FEC_MII_LOOPS) | |
91 | out_be32(&fecp->fec_ievent, FEC_ENET_MII); | |
92 | ||
93 | return 0; | |
94 | ||
95 | } | |
96 | ||
97 | static int fs_enet_fec_mii_reset(struct mii_bus *bus) | |
98 | { | |
99 | /* nothing here - for now */ | |
100 | return 0; | |
101 | } | |
102 | ||
976de6a8 SW |
103 | static void __devinit add_phy(struct mii_bus *bus, struct device_node *np) |
104 | { | |
105 | const u32 *data; | |
106 | int len, id, irq; | |
107 | ||
108 | data = of_get_property(np, "reg", &len); | |
109 | if (!data || len != 4) | |
110 | return; | |
111 | ||
112 | id = *data; | |
113 | bus->phy_mask &= ~(1 << id); | |
114 | ||
115 | irq = of_irq_to_resource(np, 0, NULL); | |
116 | if (irq != NO_IRQ) | |
117 | bus->irq[id] = irq; | |
118 | } | |
119 | ||
120 | static int __devinit fs_enet_mdio_probe(struct of_device *ofdev, | |
121 | const struct of_device_id *match) | |
122 | { | |
123 | struct device_node *np = NULL; | |
124 | struct resource res; | |
125 | struct mii_bus *new_bus; | |
126 | struct fec_info *fec; | |
127 | int ret = -ENOMEM, i; | |
128 | ||
298cf9be | 129 | new_bus = mdiobus_alloc(); |
976de6a8 SW |
130 | if (!new_bus) |
131 | goto out; | |
132 | ||
133 | fec = kzalloc(sizeof(struct fec_info), GFP_KERNEL); | |
134 | if (!fec) | |
135 | goto out_mii; | |
136 | ||
137 | new_bus->priv = fec; | |
138 | new_bus->name = "FEC MII Bus"; | |
139 | new_bus->read = &fs_enet_fec_mii_read; | |
140 | new_bus->write = &fs_enet_fec_mii_write; | |
141 | new_bus->reset = &fs_enet_fec_mii_reset; | |
142 | ||
143 | ret = of_address_to_resource(ofdev->node, 0, &res); | |
144 | if (ret) | |
a86e2cbe | 145 | goto out_res; |
976de6a8 | 146 | |
9d9326d3 | 147 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", res.start); |
976de6a8 SW |
148 | |
149 | fec->fecp = ioremap(res.start, res.end - res.start + 1); | |
150 | if (!fec->fecp) | |
151 | goto out_fec; | |
152 | ||
153 | fec->mii_speed = ((ppc_proc_freq + 4999999) / 5000000) << 1; | |
154 | ||
155 | setbits32(&fec->fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE); | |
156 | setbits32(&fec->fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | | |
157 | FEC_ECNTRL_ETHER_EN); | |
158 | out_be32(&fec->fecp->fec_ievent, FEC_ENET_MII); | |
159 | out_be32(&fec->fecp->fec_mii_speed, fec->mii_speed); | |
160 | ||
161 | new_bus->phy_mask = ~0; | |
162 | new_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL); | |
163 | if (!new_bus->irq) | |
164 | goto out_unmap_regs; | |
165 | ||
166 | for (i = 0; i < PHY_MAX_ADDR; i++) | |
167 | new_bus->irq[i] = -1; | |
168 | ||
169 | while ((np = of_get_next_child(ofdev->node, np))) | |
170 | if (!strcmp(np->type, "ethernet-phy")) | |
171 | add_phy(new_bus, np); | |
172 | ||
18ee49dd | 173 | new_bus->parent = &ofdev->dev; |
976de6a8 SW |
174 | dev_set_drvdata(&ofdev->dev, new_bus); |
175 | ||
176 | ret = mdiobus_register(new_bus); | |
177 | if (ret) | |
178 | goto out_free_irqs; | |
179 | ||
180 | return 0; | |
181 | ||
182 | out_free_irqs: | |
183 | dev_set_drvdata(&ofdev->dev, NULL); | |
184 | kfree(new_bus->irq); | |
185 | out_unmap_regs: | |
186 | iounmap(fec->fecp); | |
a86e2cbe | 187 | out_res: |
976de6a8 SW |
188 | out_fec: |
189 | kfree(fec); | |
190 | out_mii: | |
298cf9be | 191 | mdiobus_free(new_bus); |
976de6a8 SW |
192 | out: |
193 | return ret; | |
194 | } | |
195 | ||
196 | static int fs_enet_mdio_remove(struct of_device *ofdev) | |
197 | { | |
198 | struct mii_bus *bus = dev_get_drvdata(&ofdev->dev); | |
199 | struct fec_info *fec = bus->priv; | |
200 | ||
201 | mdiobus_unregister(bus); | |
202 | dev_set_drvdata(&ofdev->dev, NULL); | |
203 | kfree(bus->irq); | |
204 | iounmap(fec->fecp); | |
205 | kfree(fec); | |
298cf9be | 206 | mdiobus_free(bus); |
976de6a8 SW |
207 | |
208 | return 0; | |
209 | } | |
210 | ||
211 | static struct of_device_id fs_enet_mdio_fec_match[] = { | |
212 | { | |
213 | .compatible = "fsl,pq1-fec-mdio", | |
214 | }, | |
215 | {}, | |
216 | }; | |
217 | ||
218 | static struct of_platform_driver fs_enet_fec_mdio_driver = { | |
219 | .name = "fsl-fec-mdio", | |
220 | .match_table = fs_enet_mdio_fec_match, | |
221 | .probe = fs_enet_mdio_probe, | |
222 | .remove = fs_enet_mdio_remove, | |
223 | }; | |
224 | ||
225 | static int fs_enet_mdio_fec_init(void) | |
226 | { | |
227 | return of_register_platform_driver(&fs_enet_fec_mdio_driver); | |
228 | } | |
229 | ||
230 | static void fs_enet_mdio_fec_exit(void) | |
231 | { | |
232 | of_unregister_platform_driver(&fs_enet_fec_mdio_driver); | |
233 | } | |
234 | ||
235 | module_init(fs_enet_mdio_fec_init); | |
236 | module_exit(fs_enet_mdio_fec_exit); |