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