blob: 65925b5a224f761afdb169762e376f7d4c3890a0 [file] [log] [blame]
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001/*
2 * Ethernet on Serial Communications Controller (SCC) 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
Pantelis Antoniou48257c42005-10-28 16:25:58 -040015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/types.h>
Pantelis Antoniou48257c42005-10-28 16:25:58 -040018#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>
24#include <linux/pci.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
29#include <linux/skbuff.h>
30#include <linux/spinlock.h>
31#include <linux/mii.h>
32#include <linux/ethtool.h>
33#include <linux/bitops.h>
34#include <linux/fs.h>
Marcelo Tosattif7b99962005-11-09 11:00:16 -020035#include <linux/platform_device.h>
Pantelis Antoniou48257c42005-10-28 16:25:58 -040036
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>
44#include <asm/commproc.h>
45#endif
46
47#include "fs_enet.h"
48
49/*************************************************/
50
51#if defined(CONFIG_CPM1)
52/* for a 8xx __raw_xxx's are sufficient */
53#define __fs_out32(addr, x) __raw_writel(x, addr)
54#define __fs_out16(addr, x) __raw_writew(x, addr)
55#define __fs_out8(addr, x) __raw_writeb(x, addr)
56#define __fs_in32(addr) __raw_readl(addr)
57#define __fs_in16(addr) __raw_readw(addr)
58#define __fs_in8(addr) __raw_readb(addr)
59#else
60/* for others play it safe */
61#define __fs_out32(addr, x) out_be32(addr, x)
62#define __fs_out16(addr, x) out_be16(addr, x)
63#define __fs_in32(addr) in_be32(addr)
64#define __fs_in16(addr) in_be16(addr)
65#endif
66
67/* write, read, set bits, clear bits */
68#define W32(_p, _m, _v) __fs_out32(&(_p)->_m, (_v))
69#define R32(_p, _m) __fs_in32(&(_p)->_m)
70#define S32(_p, _m, _v) W32(_p, _m, R32(_p, _m) | (_v))
71#define C32(_p, _m, _v) W32(_p, _m, R32(_p, _m) & ~(_v))
72
73#define W16(_p, _m, _v) __fs_out16(&(_p)->_m, (_v))
74#define R16(_p, _m) __fs_in16(&(_p)->_m)
75#define S16(_p, _m, _v) W16(_p, _m, R16(_p, _m) | (_v))
76#define C16(_p, _m, _v) W16(_p, _m, R16(_p, _m) & ~(_v))
77
78#define W8(_p, _m, _v) __fs_out8(&(_p)->_m, (_v))
79#define R8(_p, _m) __fs_in8(&(_p)->_m)
80#define S8(_p, _m, _v) W8(_p, _m, R8(_p, _m) | (_v))
81#define C8(_p, _m, _v) W8(_p, _m, R8(_p, _m) & ~(_v))
82
83#define SCC_MAX_MULTICAST_ADDRS 64
84
85/*
86 * Delay to wait for SCC reset command to complete (in us)
87 */
88#define SCC_RESET_DELAY 50
89#define MAX_CR_CMD_LOOPS 10000
90
91static inline int scc_cr_cmd(struct fs_enet_private *fep, u32 op)
92{
93 cpm8xx_t *cpmp = &((immap_t *)fs_enet_immap)->im_cpm;
94 u32 v, ch;
95 int i = 0;
96
97 ch = fep->scc.idx << 2;
98 v = mk_cr_cmd(ch, op);
99 W16(cpmp, cp_cpcr, v | CPM_CR_FLG);
100 for (i = 0; i < MAX_CR_CMD_LOOPS; i++)
101 if ((R16(cpmp, cp_cpcr) & CPM_CR_FLG) == 0)
102 break;
103
104 if (i >= MAX_CR_CMD_LOOPS) {
105 printk(KERN_ERR "%s(): Not able to issue CPM command\n",
106 __FUNCTION__);
107 return 1;
108 }
109 return 0;
110}
111
112static int do_pd_setup(struct fs_enet_private *fep)
113{
114 struct platform_device *pdev = to_platform_device(fep->dev);
115 struct resource *r;
116
117 /* Fill out IRQ field */
118 fep->interrupt = platform_get_irq_byname(pdev, "interrupt");
David Vrabel48944732006-01-19 17:56:29 +0000119 if (fep->interrupt < 0)
120 return -EINVAL;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400121
122 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
Vitaly Bordugb1f54ba2007-01-27 00:00:04 -0800123 fep->scc.sccp = ioremap(r->start, r->end - r->start + 1);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400124
125 if (fep->scc.sccp == NULL)
126 return -EINVAL;
127
128 r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pram");
Vitaly Bordugb1f54ba2007-01-27 00:00:04 -0800129 fep->scc.ep = ioremap(r->start, r->end - r->start + 1);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400130
131 if (fep->scc.ep == NULL)
132 return -EINVAL;
133
134 return 0;
135}
136
137#define SCC_NAPI_RX_EVENT_MSK (SCCE_ENET_RXF | SCCE_ENET_RXB)
138#define SCC_RX_EVENT (SCCE_ENET_RXF)
139#define SCC_TX_EVENT (SCCE_ENET_TXB)
140#define SCC_ERR_EVENT_MSK (SCCE_ENET_TXE | SCCE_ENET_BSY)
141
142static int setup_data(struct net_device *dev)
143{
144 struct fs_enet_private *fep = netdev_priv(dev);
145 const struct fs_platform_info *fpi = fep->fpi;
146
147 fep->scc.idx = fs_get_scc_index(fpi->fs_no);
148 if ((unsigned int)fep->fcc.idx > 4) /* max 4 SCCs */
149 return -EINVAL;
150
151 do_pd_setup(fep);
152
153 fep->scc.hthi = 0;
154 fep->scc.htlo = 0;
155
156 fep->ev_napi_rx = SCC_NAPI_RX_EVENT_MSK;
157 fep->ev_rx = SCC_RX_EVENT;
158 fep->ev_tx = SCC_TX_EVENT;
159 fep->ev_err = SCC_ERR_EVENT_MSK;
160
161 return 0;
162}
163
164static int allocate_bd(struct net_device *dev)
165{
166 struct fs_enet_private *fep = netdev_priv(dev);
167 const struct fs_platform_info *fpi = fep->fpi;
168
169 fep->ring_mem_addr = cpm_dpalloc((fpi->tx_ring + fpi->rx_ring) *
170 sizeof(cbd_t), 8);
171 if (IS_DPERR(fep->ring_mem_addr))
172 return -ENOMEM;
173
174 fep->ring_base = cpm_dpram_addr(fep->ring_mem_addr);
175
176 return 0;
177}
178
179static void free_bd(struct net_device *dev)
180{
181 struct fs_enet_private *fep = netdev_priv(dev);
182
183 if (fep->ring_base)
184 cpm_dpfree(fep->ring_mem_addr);
185}
186
187static void cleanup_data(struct net_device *dev)
188{
189 /* nothing */
190}
191
192static void set_promiscuous_mode(struct net_device *dev)
193{
194 struct fs_enet_private *fep = netdev_priv(dev);
195 scc_t *sccp = fep->scc.sccp;
196
197 S16(sccp, scc_psmr, SCC_PSMR_PRO);
198}
199
200static void set_multicast_start(struct net_device *dev)
201{
202 struct fs_enet_private *fep = netdev_priv(dev);
203 scc_enet_t *ep = fep->scc.ep;
204
205 W16(ep, sen_gaddr1, 0);
206 W16(ep, sen_gaddr2, 0);
207 W16(ep, sen_gaddr3, 0);
208 W16(ep, sen_gaddr4, 0);
209}
210
211static void set_multicast_one(struct net_device *dev, const u8 * mac)
212{
213 struct fs_enet_private *fep = netdev_priv(dev);
214 scc_enet_t *ep = fep->scc.ep;
215 u16 taddrh, taddrm, taddrl;
216
217 taddrh = ((u16) mac[5] << 8) | mac[4];
218 taddrm = ((u16) mac[3] << 8) | mac[2];
219 taddrl = ((u16) mac[1] << 8) | mac[0];
220
221 W16(ep, sen_taddrh, taddrh);
222 W16(ep, sen_taddrm, taddrm);
223 W16(ep, sen_taddrl, taddrl);
224 scc_cr_cmd(fep, CPM_CR_SET_GADDR);
225}
226
227static void set_multicast_finish(struct net_device *dev)
228{
229 struct fs_enet_private *fep = netdev_priv(dev);
230 scc_t *sccp = fep->scc.sccp;
231 scc_enet_t *ep = fep->scc.ep;
232
233 /* clear promiscuous always */
234 C16(sccp, scc_psmr, SCC_PSMR_PRO);
235
236 /* if all multi or too many multicasts; just enable all */
237 if ((dev->flags & IFF_ALLMULTI) != 0 ||
238 dev->mc_count > SCC_MAX_MULTICAST_ADDRS) {
239
240 W16(ep, sen_gaddr1, 0xffff);
241 W16(ep, sen_gaddr2, 0xffff);
242 W16(ep, sen_gaddr3, 0xffff);
243 W16(ep, sen_gaddr4, 0xffff);
244 }
245}
246
247static void set_multicast_list(struct net_device *dev)
248{
249 struct dev_mc_list *pmc;
250
251 if ((dev->flags & IFF_PROMISC) == 0) {
252 set_multicast_start(dev);
253 for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next)
254 set_multicast_one(dev, pmc->dmi_addr);
255 set_multicast_finish(dev);
256 } else
257 set_promiscuous_mode(dev);
258}
259
260/*
261 * This function is called to start or restart the FEC during a link
262 * change. This only happens when switching between half and full
263 * duplex.
264 */
265static void restart(struct net_device *dev)
266{
267 struct fs_enet_private *fep = netdev_priv(dev);
268 scc_t *sccp = fep->scc.sccp;
269 scc_enet_t *ep = fep->scc.ep;
270 const struct fs_platform_info *fpi = fep->fpi;
271 u16 paddrh, paddrm, paddrl;
272 const unsigned char *mac;
273 int i;
274
275 C32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
276
277 /* clear everything (slow & steady does it) */
278 for (i = 0; i < sizeof(*ep); i++)
279 __fs_out8((char *)ep + i, 0);
280
281 /* point to bds */
282 W16(ep, sen_genscc.scc_rbase, fep->ring_mem_addr);
283 W16(ep, sen_genscc.scc_tbase,
284 fep->ring_mem_addr + sizeof(cbd_t) * fpi->rx_ring);
285
286 /* Initialize function code registers for big-endian.
287 */
288 W8(ep, sen_genscc.scc_rfcr, SCC_EB);
289 W8(ep, sen_genscc.scc_tfcr, SCC_EB);
290
291 /* Set maximum bytes per receive buffer.
292 * This appears to be an Ethernet frame size, not the buffer
293 * fragment size. It must be a multiple of four.
294 */
295 W16(ep, sen_genscc.scc_mrblr, 0x5f0);
296
297 /* Set CRC preset and mask.
298 */
299 W32(ep, sen_cpres, 0xffffffff);
300 W32(ep, sen_cmask, 0xdebb20e3);
301
302 W32(ep, sen_crcec, 0); /* CRC Error counter */
303 W32(ep, sen_alec, 0); /* alignment error counter */
304 W32(ep, sen_disfc, 0); /* discard frame counter */
305
306 W16(ep, sen_pads, 0x8888); /* Tx short frame pad character */
307 W16(ep, sen_retlim, 15); /* Retry limit threshold */
308
309 W16(ep, sen_maxflr, 0x5ee); /* maximum frame length register */
310
311 W16(ep, sen_minflr, PKT_MINBUF_SIZE); /* minimum frame length register */
312
313 W16(ep, sen_maxd1, 0x000005f0); /* maximum DMA1 length */
314 W16(ep, sen_maxd2, 0x000005f0); /* maximum DMA2 length */
315
316 /* Clear hash tables.
317 */
318 W16(ep, sen_gaddr1, 0);
319 W16(ep, sen_gaddr2, 0);
320 W16(ep, sen_gaddr3, 0);
321 W16(ep, sen_gaddr4, 0);
322 W16(ep, sen_iaddr1, 0);
323 W16(ep, sen_iaddr2, 0);
324 W16(ep, sen_iaddr3, 0);
325 W16(ep, sen_iaddr4, 0);
326
327 /* set address
328 */
329 mac = dev->dev_addr;
330 paddrh = ((u16) mac[5] << 8) | mac[4];
331 paddrm = ((u16) mac[3] << 8) | mac[2];
332 paddrl = ((u16) mac[1] << 8) | mac[0];
333
334 W16(ep, sen_paddrh, paddrh);
335 W16(ep, sen_paddrm, paddrm);
336 W16(ep, sen_paddrl, paddrl);
337
338 W16(ep, sen_pper, 0);
339 W16(ep, sen_taddrl, 0);
340 W16(ep, sen_taddrm, 0);
341 W16(ep, sen_taddrh, 0);
342
343 fs_init_bds(dev);
344
345 scc_cr_cmd(fep, CPM_CR_INIT_TRX);
346
347 W16(sccp, scc_scce, 0xffff);
348
349 /* Enable interrupts we wish to service.
350 */
351 W16(sccp, scc_sccm, SCCE_ENET_TXE | SCCE_ENET_RXF | SCCE_ENET_TXB);
352
353 /* Set GSMR_H to enable all normal operating modes.
354 * Set GSMR_L to enable Ethernet to MC68160.
355 */
356 W32(sccp, scc_gsmrh, 0);
357 W32(sccp, scc_gsmrl,
358 SCC_GSMRL_TCI | SCC_GSMRL_TPL_48 | SCC_GSMRL_TPP_10 |
359 SCC_GSMRL_MODE_ENET);
360
361 /* Set sync/delimiters.
362 */
363 W16(sccp, scc_dsr, 0xd555);
364
365 /* Set processing mode. Use Ethernet CRC, catch broadcast, and
366 * start frame search 22 bit times after RENA.
367 */
368 W16(sccp, scc_psmr, SCC_PSMR_ENCRC | SCC_PSMR_NIB22);
369
370 /* Set full duplex mode if needed */
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700371 if (fep->phydev->duplex)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400372 S16(sccp, scc_psmr, SCC_PSMR_LPB | SCC_PSMR_FDE);
373
374 S32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
375}
376
377static void stop(struct net_device *dev)
378{
379 struct fs_enet_private *fep = netdev_priv(dev);
380 scc_t *sccp = fep->scc.sccp;
381 int i;
382
383 for (i = 0; (R16(sccp, scc_sccm) == 0) && i < SCC_RESET_DELAY; i++)
384 udelay(1);
385
386 if (i == SCC_RESET_DELAY)
387 printk(KERN_WARNING DRV_MODULE_NAME
388 ": %s SCC timeout on graceful transmit stop\n",
389 dev->name);
390
391 W16(sccp, scc_sccm, 0);
392 C32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
393
394 fs_cleanup_bds(dev);
395}
396
397static void pre_request_irq(struct net_device *dev, int irq)
398{
Vitaly Bordugb1f54ba2007-01-27 00:00:04 -0800399#ifndef CONFIG_PPC_MERGE
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400400 immap_t *immap = fs_enet_immap;
401 u32 siel;
402
403 /* SIU interrupt */
404 if (irq >= SIU_IRQ0 && irq < SIU_LEVEL7) {
405
406 siel = in_be32(&immap->im_siu_conf.sc_siel);
407 if ((irq & 1) == 0)
408 siel |= (0x80000000 >> irq);
409 else
410 siel &= ~(0x80000000 >> (irq & ~1));
411 out_be32(&immap->im_siu_conf.sc_siel, siel);
412 }
Vitaly Bordugb1f54ba2007-01-27 00:00:04 -0800413#endif
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400414}
415
416static void post_free_irq(struct net_device *dev, int irq)
417{
418 /* nothing */
419}
420
421static void napi_clear_rx_event(struct net_device *dev)
422{
423 struct fs_enet_private *fep = netdev_priv(dev);
424 scc_t *sccp = fep->scc.sccp;
425
426 W16(sccp, scc_scce, SCC_NAPI_RX_EVENT_MSK);
427}
428
429static void napi_enable_rx(struct net_device *dev)
430{
431 struct fs_enet_private *fep = netdev_priv(dev);
432 scc_t *sccp = fep->scc.sccp;
433
434 S16(sccp, scc_sccm, SCC_NAPI_RX_EVENT_MSK);
435}
436
437static void napi_disable_rx(struct net_device *dev)
438{
439 struct fs_enet_private *fep = netdev_priv(dev);
440 scc_t *sccp = fep->scc.sccp;
441
442 C16(sccp, scc_sccm, SCC_NAPI_RX_EVENT_MSK);
443}
444
445static void rx_bd_done(struct net_device *dev)
446{
447 /* nothing */
448}
449
450static void tx_kickstart(struct net_device *dev)
451{
452 /* nothing */
453}
454
455static u32 get_int_events(struct net_device *dev)
456{
457 struct fs_enet_private *fep = netdev_priv(dev);
458 scc_t *sccp = fep->scc.sccp;
459
460 return (u32) R16(sccp, scc_scce);
461}
462
463static void clear_int_events(struct net_device *dev, u32 int_events)
464{
465 struct fs_enet_private *fep = netdev_priv(dev);
466 scc_t *sccp = fep->scc.sccp;
467
468 W16(sccp, scc_scce, int_events & 0xffff);
469}
470
471static void ev_error(struct net_device *dev, u32 int_events)
472{
473 printk(KERN_WARNING DRV_MODULE_NAME
474 ": %s SCC ERROR(s) 0x%x\n", dev->name, int_events);
475}
476
477static int get_regs(struct net_device *dev, void *p, int *sizep)
478{
479 struct fs_enet_private *fep = netdev_priv(dev);
480
481 if (*sizep < sizeof(scc_t) + sizeof(scc_enet_t))
482 return -EINVAL;
483
484 memcpy_fromio(p, fep->scc.sccp, sizeof(scc_t));
485 p = (char *)p + sizeof(scc_t);
486
487 memcpy_fromio(p, fep->scc.ep, sizeof(scc_enet_t));
488
489 return 0;
490}
491
492static int get_regs_len(struct net_device *dev)
493{
494 return sizeof(scc_t) + sizeof(scc_enet_t);
495}
496
497static void tx_restart(struct net_device *dev)
498{
499 struct fs_enet_private *fep = netdev_priv(dev);
500
501 scc_cr_cmd(fep, CPM_CR_RESTART_TX);
502}
503
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700504
505
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400506/*************************************************************************/
507
508const struct fs_ops fs_scc_ops = {
509 .setup_data = setup_data,
510 .cleanup_data = cleanup_data,
511 .set_multicast_list = set_multicast_list,
512 .restart = restart,
513 .stop = stop,
514 .pre_request_irq = pre_request_irq,
515 .post_free_irq = post_free_irq,
516 .napi_clear_rx_event = napi_clear_rx_event,
517 .napi_enable_rx = napi_enable_rx,
518 .napi_disable_rx = napi_disable_rx,
519 .rx_bd_done = rx_bd_done,
520 .tx_kickstart = tx_kickstart,
521 .get_int_events = get_int_events,
522 .clear_int_events = clear_int_events,
523 .ev_error = ev_error,
524 .get_regs = get_regs,
525 .get_regs_len = get_regs_len,
526 .tx_restart = tx_restart,
527 .allocate_bd = allocate_bd,
528 .free_bd = free_bd,
529};