1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Freescale Ethernet controllers
4 *
5 * Copyright (c) 2005 Intracom S.A.
6 * by Pantelis Antoniou <panto@intracom.gr>
7 *
8 * 2005 (c) MontaVista Software, Inc.
9 * Vitaly Bordug <vbordug@ru.mvista.com>
10 */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/types.h>
15 #include <linux/string.h>
16 #include <linux/ptrace.h>
17 #include <linux/errno.h>
18 #include <linux/crc32.h>
19 #include <linux/ioport.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/spinlock.h>
26 #include <linux/ethtool.h>
27 #include <linux/bitops.h>
28 #include <linux/fs.h>
29 #include <linux/platform_device.h>
30 #include <linux/of_address.h>
31 #include <linux/of_irq.h>
32 #include <linux/gfp.h>
33
34 #include <asm/irq.h>
35 #include <linux/uaccess.h>
36
37 #include "fs_enet.h"
38 #include "fec.h"
39
40 /*************************************************/
41
42 #if defined(CONFIG_CPM1)
43 /* for a CPM1 __raw_xxx's are sufficient */
44 #define __fs_out32(addr, x) __raw_writel(x, addr)
45 #define __fs_out16(addr, x) __raw_writew(x, addr)
46 #define __fs_in32(addr) __raw_readl(addr)
47 #define __fs_in16(addr) __raw_readw(addr)
48 #else
49 /* for others play it safe */
50 #define __fs_out32(addr, x) out_be32(addr, x)
51 #define __fs_out16(addr, x) out_be16(addr, x)
52 #define __fs_in32(addr) in_be32(addr)
53 #define __fs_in16(addr) in_be16(addr)
54 #endif
55
56 /* write */
57 #define FW(_fecp, _reg, _v) __fs_out32(&(_fecp)->fec_ ## _reg, (_v))
58
59 /* read */
60 #define FR(_fecp, _reg) __fs_in32(&(_fecp)->fec_ ## _reg)
61
62 /* set bits */
63 #define FS(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) | (_v))
64
65 /* clear bits */
66 #define FC(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) & ~(_v))
67
68 /*
69 * Delay to wait for FEC reset command to complete (in us)
70 */
71 #define FEC_RESET_DELAY 50
72
whack_reset(struct fec __iomem * fecp)73 static int whack_reset(struct fec __iomem *fecp)
74 {
75 int i;
76
77 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
78 for (i = 0; i < FEC_RESET_DELAY; i++) {
79 if ((FR(fecp, ecntrl) & FEC_ECNTRL_RESET) == 0)
80 return 0; /* OK */
81 udelay(1);
82 }
83
84 return -1;
85 }
86
do_pd_setup(struct fs_enet_private * fep)87 static int do_pd_setup(struct fs_enet_private *fep)
88 {
89 struct platform_device *ofdev = to_platform_device(fep->dev);
90
91 fep->interrupt = irq_of_parse_and_map(ofdev->dev.of_node, 0);
92 if (!fep->interrupt)
93 return -EINVAL;
94
95 fep->fec.fecp = of_iomap(ofdev->dev.of_node, 0);
96 if (!fep->fec.fecp)
97 return -EINVAL;
98
99 return 0;
100 }
101
102 #define FEC_NAPI_EVENT_MSK (FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_TXF)
103 #define FEC_EVENT (FEC_ENET_RXF | FEC_ENET_TXF)
104 #define FEC_ERR_EVENT_MSK (FEC_ENET_HBERR | FEC_ENET_BABR | \
105 FEC_ENET_BABT | FEC_ENET_EBERR)
106
setup_data(struct net_device * dev)107 static int setup_data(struct net_device *dev)
108 {
109 struct fs_enet_private *fep = netdev_priv(dev);
110
111 if (do_pd_setup(fep) != 0)
112 return -EINVAL;
113
114 fep->fec.hthi = 0;
115 fep->fec.htlo = 0;
116
117 fep->ev_napi = FEC_NAPI_EVENT_MSK;
118 fep->ev = FEC_EVENT;
119 fep->ev_err = FEC_ERR_EVENT_MSK;
120
121 return 0;
122 }
123
allocate_bd(struct net_device * dev)124 static int allocate_bd(struct net_device *dev)
125 {
126 struct fs_enet_private *fep = netdev_priv(dev);
127 const struct fs_platform_info *fpi = fep->fpi;
128
129 fep->ring_base = (void __force __iomem *)dma_alloc_coherent(fep->dev,
130 (fpi->tx_ring + fpi->rx_ring) *
131 sizeof(cbd_t), &fep->ring_mem_addr,
132 GFP_KERNEL);
133 if (fep->ring_base == NULL)
134 return -ENOMEM;
135
136 return 0;
137 }
138
free_bd(struct net_device * dev)139 static void free_bd(struct net_device *dev)
140 {
141 struct fs_enet_private *fep = netdev_priv(dev);
142 const struct fs_platform_info *fpi = fep->fpi;
143
144 if(fep->ring_base)
145 dma_free_coherent(fep->dev, (fpi->tx_ring + fpi->rx_ring)
146 * sizeof(cbd_t),
147 (void __force *)fep->ring_base,
148 fep->ring_mem_addr);
149 }
150
cleanup_data(struct net_device * dev)151 static void cleanup_data(struct net_device *dev)
152 {
153 /* nothing */
154 }
155
set_promiscuous_mode(struct net_device * dev)156 static void set_promiscuous_mode(struct net_device *dev)
157 {
158 struct fs_enet_private *fep = netdev_priv(dev);
159 struct fec __iomem *fecp = fep->fec.fecp;
160
161 FS(fecp, r_cntrl, FEC_RCNTRL_PROM);
162 }
163
set_multicast_start(struct net_device * dev)164 static void set_multicast_start(struct net_device *dev)
165 {
166 struct fs_enet_private *fep = netdev_priv(dev);
167
168 fep->fec.hthi = 0;
169 fep->fec.htlo = 0;
170 }
171
set_multicast_one(struct net_device * dev,const u8 * mac)172 static void set_multicast_one(struct net_device *dev, const u8 *mac)
173 {
174 struct fs_enet_private *fep = netdev_priv(dev);
175 int temp, hash_index;
176 u32 crc, csrVal;
177
178 crc = ether_crc(6, mac);
179
180 temp = (crc & 0x3f) >> 1;
181 hash_index = ((temp & 0x01) << 4) |
182 ((temp & 0x02) << 2) |
183 ((temp & 0x04)) |
184 ((temp & 0x08) >> 2) |
185 ((temp & 0x10) >> 4);
186 csrVal = 1 << hash_index;
187 if (crc & 1)
188 fep->fec.hthi |= csrVal;
189 else
190 fep->fec.htlo |= csrVal;
191 }
192
set_multicast_finish(struct net_device * dev)193 static void set_multicast_finish(struct net_device *dev)
194 {
195 struct fs_enet_private *fep = netdev_priv(dev);
196 struct fec __iomem *fecp = fep->fec.fecp;
197
198 /* if all multi or too many multicasts; just enable all */
199 if ((dev->flags & IFF_ALLMULTI) != 0 ||
200 netdev_mc_count(dev) > FEC_MAX_MULTICAST_ADDRS) {
201 fep->fec.hthi = 0xffffffffU;
202 fep->fec.htlo = 0xffffffffU;
203 }
204
205 FC(fecp, r_cntrl, FEC_RCNTRL_PROM);
206 FW(fecp, grp_hash_table_high, fep->fec.hthi);
207 FW(fecp, grp_hash_table_low, fep->fec.htlo);
208 }
209
set_multicast_list(struct net_device * dev)210 static void set_multicast_list(struct net_device *dev)
211 {
212 struct netdev_hw_addr *ha;
213
214 if ((dev->flags & IFF_PROMISC) == 0) {
215 set_multicast_start(dev);
216 netdev_for_each_mc_addr(ha, dev)
217 set_multicast_one(dev, ha->addr);
218 set_multicast_finish(dev);
219 } else
220 set_promiscuous_mode(dev);
221 }
222
restart(struct net_device * dev,phy_interface_t interface,int speed,int duplex)223 static void restart(struct net_device *dev, phy_interface_t interface,
224 int speed, int duplex)
225 {
226 struct fs_enet_private *fep = netdev_priv(dev);
227 struct fec __iomem *fecp = fep->fec.fecp;
228 const struct fs_platform_info *fpi = fep->fpi;
229 dma_addr_t rx_bd_base_phys, tx_bd_base_phys;
230 int r;
231 u32 addrhi, addrlo;
232
233 struct mii_bus *mii = dev->phydev->mdio.bus;
234 struct fec_info* fec_inf = mii->priv;
235
236 r = whack_reset(fep->fec.fecp);
237 if (r != 0)
238 dev_err(fep->dev, "FEC Reset FAILED!\n");
239 /*
240 * Set station address.
241 */
242 addrhi = ((u32) dev->dev_addr[0] << 24) |
243 ((u32) dev->dev_addr[1] << 16) |
244 ((u32) dev->dev_addr[2] << 8) |
245 (u32) dev->dev_addr[3];
246 addrlo = ((u32) dev->dev_addr[4] << 24) |
247 ((u32) dev->dev_addr[5] << 16);
248 FW(fecp, addr_low, addrhi);
249 FW(fecp, addr_high, addrlo);
250
251 /*
252 * Reset all multicast.
253 */
254 FW(fecp, grp_hash_table_high, fep->fec.hthi);
255 FW(fecp, grp_hash_table_low, fep->fec.htlo);
256
257 /*
258 * Set maximum receive buffer size.
259 */
260 FW(fecp, r_buff_size, PKT_MAXBLR_SIZE);
261 #ifdef CONFIG_FS_ENET_MPC5121_FEC
262 FW(fecp, r_cntrl, PKT_MAXBUF_SIZE << 16);
263 #else
264 FW(fecp, r_hash, PKT_MAXBUF_SIZE);
265 #endif
266
267 /* get physical address */
268 rx_bd_base_phys = fep->ring_mem_addr;
269 tx_bd_base_phys = rx_bd_base_phys + sizeof(cbd_t) * fpi->rx_ring;
270
271 /*
272 * Set receive and transmit descriptor base.
273 */
274 FW(fecp, r_des_start, rx_bd_base_phys);
275 FW(fecp, x_des_start, tx_bd_base_phys);
276
277 fs_init_bds(dev);
278
279 /*
280 * Enable big endian and don't care about SDMA FC.
281 */
282 #ifdef CONFIG_FS_ENET_MPC5121_FEC
283 FS(fecp, dma_control, 0xC0000000);
284 #else
285 FW(fecp, fun_code, 0x78000000);
286 #endif
287
288 /*
289 * Set MII speed.
290 */
291 FW(fecp, mii_speed, fec_inf->mii_speed);
292
293 /*
294 * Clear any outstanding interrupt.
295 */
296 FW(fecp, ievent, 0xffc0);
297 #ifndef CONFIG_FS_ENET_MPC5121_FEC
298 FW(fecp, ivec, (virq_to_hw(fep->interrupt) / 2) << 29);
299
300 FW(fecp, r_cntrl, FEC_RCNTRL_MII_MODE); /* MII enable */
301 #else
302 /*
303 * Only set MII/RMII mode - do not touch maximum frame length
304 * configured before.
305 */
306 FS(fecp, r_cntrl, interface == PHY_INTERFACE_MODE_RMII ?
307 FEC_RCNTRL_RMII_MODE : FEC_RCNTRL_MII_MODE);
308 #endif
309 /*
310 * adjust to duplex mode
311 */
312 if (duplex == DUPLEX_FULL) {
313 FC(fecp, r_cntrl, FEC_RCNTRL_DRT);
314 FS(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
315 } else {
316 FS(fecp, r_cntrl, FEC_RCNTRL_DRT);
317 FC(fecp, x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
318 }
319
320 /* Restore multicast and promiscuous settings */
321 set_multicast_list(dev);
322
323 /*
324 * Enable interrupts we wish to service.
325 */
326 FW(fecp, imask, FEC_ENET_TXF | FEC_ENET_TXB |
327 FEC_ENET_RXF | FEC_ENET_RXB);
328
329 /*
330 * And last, enable the transmit and receive processing.
331 */
332 FW(fecp, ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
333 FW(fecp, r_des_active, 0x01000000);
334 }
335
stop(struct net_device * dev)336 static void stop(struct net_device *dev)
337 {
338 struct fs_enet_private *fep = netdev_priv(dev);
339 struct fec __iomem *fecp = fep->fec.fecp;
340 int i;
341
342 if ((FR(fecp, ecntrl) & FEC_ECNTRL_ETHER_EN) == 0)
343 return; /* already down */
344
345 FW(fecp, x_cntrl, 0x01); /* Graceful transmit stop */
346 for (i = 0; ((FR(fecp, ievent) & 0x10000000) == 0) &&
347 i < FEC_RESET_DELAY; i++)
348 udelay(1);
349
350 if (i == FEC_RESET_DELAY)
351 dev_warn(fep->dev, "FEC timeout on graceful transmit stop\n");
352 /*
353 * Disable FEC. Let only MII interrupts.
354 */
355 FW(fecp, imask, 0);
356 FC(fecp, ecntrl, FEC_ECNTRL_ETHER_EN);
357
358 fs_cleanup_bds(dev);
359 }
360
napi_clear_event_fs(struct net_device * dev)361 static void napi_clear_event_fs(struct net_device *dev)
362 {
363 struct fs_enet_private *fep = netdev_priv(dev);
364 struct fec __iomem *fecp = fep->fec.fecp;
365
366 FW(fecp, ievent, FEC_NAPI_EVENT_MSK);
367 }
368
napi_enable_fs(struct net_device * dev)369 static void napi_enable_fs(struct net_device *dev)
370 {
371 struct fs_enet_private *fep = netdev_priv(dev);
372 struct fec __iomem *fecp = fep->fec.fecp;
373
374 FS(fecp, imask, FEC_NAPI_EVENT_MSK);
375 }
376
napi_disable_fs(struct net_device * dev)377 static void napi_disable_fs(struct net_device *dev)
378 {
379 struct fs_enet_private *fep = netdev_priv(dev);
380 struct fec __iomem *fecp = fep->fec.fecp;
381
382 FC(fecp, imask, FEC_NAPI_EVENT_MSK);
383 }
384
rx_bd_done(struct net_device * dev)385 static void rx_bd_done(struct net_device *dev)
386 {
387 struct fs_enet_private *fep = netdev_priv(dev);
388 struct fec __iomem *fecp = fep->fec.fecp;
389
390 FW(fecp, r_des_active, 0x01000000);
391 }
392
tx_kickstart(struct net_device * dev)393 static void tx_kickstart(struct net_device *dev)
394 {
395 struct fs_enet_private *fep = netdev_priv(dev);
396 struct fec __iomem *fecp = fep->fec.fecp;
397
398 FW(fecp, x_des_active, 0x01000000);
399 }
400
get_int_events(struct net_device * dev)401 static u32 get_int_events(struct net_device *dev)
402 {
403 struct fs_enet_private *fep = netdev_priv(dev);
404 struct fec __iomem *fecp = fep->fec.fecp;
405
406 return FR(fecp, ievent) & FR(fecp, imask);
407 }
408
clear_int_events(struct net_device * dev,u32 int_events)409 static void clear_int_events(struct net_device *dev, u32 int_events)
410 {
411 struct fs_enet_private *fep = netdev_priv(dev);
412 struct fec __iomem *fecp = fep->fec.fecp;
413
414 FW(fecp, ievent, int_events);
415 }
416
ev_error(struct net_device * dev,u32 int_events)417 static void ev_error(struct net_device *dev, u32 int_events)
418 {
419 struct fs_enet_private *fep = netdev_priv(dev);
420
421 dev_warn(fep->dev, "FEC ERROR(s) 0x%x\n", int_events);
422 }
423
get_regs(struct net_device * dev,void * p,int * sizep)424 static int get_regs(struct net_device *dev, void *p, int *sizep)
425 {
426 struct fs_enet_private *fep = netdev_priv(dev);
427
428 if (*sizep < sizeof(struct fec))
429 return -EINVAL;
430
431 memcpy_fromio(p, fep->fec.fecp, sizeof(struct fec));
432
433 return 0;
434 }
435
get_regs_len(struct net_device * dev)436 static int get_regs_len(struct net_device *dev)
437 {
438 return sizeof(struct fec);
439 }
440
tx_restart(struct net_device * dev)441 static void tx_restart(struct net_device *dev)
442 {
443 /* nothing */
444 }
445
446 /*************************************************************************/
447
448 const struct fs_ops fs_fec_ops = {
449 .setup_data = setup_data,
450 .cleanup_data = cleanup_data,
451 .set_multicast_list = set_multicast_list,
452 .restart = restart,
453 .stop = stop,
454 .napi_clear_event = napi_clear_event_fs,
455 .napi_enable = napi_enable_fs,
456 .napi_disable = napi_disable_fs,
457 .rx_bd_done = rx_bd_done,
458 .tx_kickstart = tx_kickstart,
459 .get_int_events = get_int_events,
460 .clear_int_events = clear_int_events,
461 .ev_error = ev_error,
462 .get_regs = get_regs,
463 .get_regs_len = get_regs_len,
464 .tx_restart = tx_restart,
465 .allocate_bd = allocate_bd,
466 .free_bd = free_bd,
467 };
468
469