2 * Ethernet on Serial Communications Controller (SCC) driver for Motorola MPC8xx and MPC82xx.
4 * Copyright (c) 2003 Intracom S.A.
5 * by Pantelis Antoniou <panto@intracom.gr>
7 * 2005 (c) MontaVista Software, Inc.
8 * Vitaly Bordug <vbordug@ru.mvista.com>
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.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
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>
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>
34 #include <linux/platform_device.h>
35 #include <linux/of_platform.h>
38 #include <asm/uaccess.h>
41 #include <asm/8xx_immap.h>
42 #include <asm/pgtable.h>
43 #include <asm/mpc8xx.h>
49 /*************************************************/
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)
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)
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))
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))
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))
83 #define SCC_MAX_MULTICAST_ADDRS 64
86 * Delay to wait for SCC reset command to complete (in us)
88 #define SCC_RESET_DELAY 50
90 static inline int scc_cr_cmd(struct fs_enet_private *fep, u32 op)
92 const struct fs_platform_info *fpi = fep->fpi;
94 return cpm_command(fpi->cp_command, op);
97 static int do_pd_setup(struct fs_enet_private *fep)
99 struct of_device *ofdev = to_of_device(fep->dev);
101 fep->interrupt = of_irq_to_resource(ofdev->node, 0, NULL);
102 if (fep->interrupt == NO_IRQ)
105 fep->scc.sccp = of_iomap(ofdev->node, 0);
109 fep->scc.ep = of_iomap(ofdev->node, 1);
111 iounmap(fep->scc.sccp);
118 #define SCC_NAPI_RX_EVENT_MSK (SCCE_ENET_RXF | SCCE_ENET_RXB)
119 #define SCC_RX_EVENT (SCCE_ENET_RXF)
120 #define SCC_TX_EVENT (SCCE_ENET_TXB)
121 #define SCC_ERR_EVENT_MSK (SCCE_ENET_TXE | SCCE_ENET_BSY)
123 static int setup_data(struct net_device *dev)
125 struct fs_enet_private *fep = netdev_priv(dev);
132 fep->ev_napi_rx = SCC_NAPI_RX_EVENT_MSK;
133 fep->ev_rx = SCC_RX_EVENT;
134 fep->ev_tx = SCC_TX_EVENT | SCCE_ENET_TXE;
135 fep->ev_err = SCC_ERR_EVENT_MSK;
140 static int allocate_bd(struct net_device *dev)
142 struct fs_enet_private *fep = netdev_priv(dev);
143 const struct fs_platform_info *fpi = fep->fpi;
145 fep->ring_mem_addr = cpm_dpalloc((fpi->tx_ring + fpi->rx_ring) *
147 if (IS_ERR_VALUE(fep->ring_mem_addr))
150 fep->ring_base = (void __iomem __force*)
151 cpm_dpram_addr(fep->ring_mem_addr);
156 static void free_bd(struct net_device *dev)
158 struct fs_enet_private *fep = netdev_priv(dev);
161 cpm_dpfree(fep->ring_mem_addr);
164 static void cleanup_data(struct net_device *dev)
169 static void set_promiscuous_mode(struct net_device *dev)
171 struct fs_enet_private *fep = netdev_priv(dev);
172 scc_t __iomem *sccp = fep->scc.sccp;
174 S16(sccp, scc_psmr, SCC_PSMR_PRO);
177 static void set_multicast_start(struct net_device *dev)
179 struct fs_enet_private *fep = netdev_priv(dev);
180 scc_enet_t __iomem *ep = fep->scc.ep;
182 W16(ep, sen_gaddr1, 0);
183 W16(ep, sen_gaddr2, 0);
184 W16(ep, sen_gaddr3, 0);
185 W16(ep, sen_gaddr4, 0);
188 static void set_multicast_one(struct net_device *dev, const u8 * mac)
190 struct fs_enet_private *fep = netdev_priv(dev);
191 scc_enet_t __iomem *ep = fep->scc.ep;
192 u16 taddrh, taddrm, taddrl;
194 taddrh = ((u16) mac[5] << 8) | mac[4];
195 taddrm = ((u16) mac[3] << 8) | mac[2];
196 taddrl = ((u16) mac[1] << 8) | mac[0];
198 W16(ep, sen_taddrh, taddrh);
199 W16(ep, sen_taddrm, taddrm);
200 W16(ep, sen_taddrl, taddrl);
201 scc_cr_cmd(fep, CPM_CR_SET_GADDR);
204 static void set_multicast_finish(struct net_device *dev)
206 struct fs_enet_private *fep = netdev_priv(dev);
207 scc_t __iomem *sccp = fep->scc.sccp;
208 scc_enet_t __iomem *ep = fep->scc.ep;
210 /* clear promiscuous always */
211 C16(sccp, scc_psmr, SCC_PSMR_PRO);
213 /* if all multi or too many multicasts; just enable all */
214 if ((dev->flags & IFF_ALLMULTI) != 0 ||
215 dev->mc_count > SCC_MAX_MULTICAST_ADDRS) {
217 W16(ep, sen_gaddr1, 0xffff);
218 W16(ep, sen_gaddr2, 0xffff);
219 W16(ep, sen_gaddr3, 0xffff);
220 W16(ep, sen_gaddr4, 0xffff);
224 static void set_multicast_list(struct net_device *dev)
226 struct dev_mc_list *pmc;
228 if ((dev->flags & IFF_PROMISC) == 0) {
229 set_multicast_start(dev);
230 for (pmc = dev->mc_list; pmc != NULL; pmc = pmc->next)
231 set_multicast_one(dev, pmc->dmi_addr);
232 set_multicast_finish(dev);
234 set_promiscuous_mode(dev);
238 * This function is called to start or restart the FEC during a link
239 * change. This only happens when switching between half and full
242 static void restart(struct net_device *dev)
244 struct fs_enet_private *fep = netdev_priv(dev);
245 scc_t __iomem *sccp = fep->scc.sccp;
246 scc_enet_t __iomem *ep = fep->scc.ep;
247 const struct fs_platform_info *fpi = fep->fpi;
248 u16 paddrh, paddrm, paddrl;
249 const unsigned char *mac;
252 C32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
254 /* clear everything (slow & steady does it) */
255 for (i = 0; i < sizeof(*ep); i++)
256 __fs_out8((u8 __iomem *)ep + i, 0);
259 W16(ep, sen_genscc.scc_rbase, fep->ring_mem_addr);
260 W16(ep, sen_genscc.scc_tbase,
261 fep->ring_mem_addr + sizeof(cbd_t) * fpi->rx_ring);
263 /* Initialize function code registers for big-endian.
265 W8(ep, sen_genscc.scc_rfcr, SCC_EB);
266 W8(ep, sen_genscc.scc_tfcr, SCC_EB);
268 /* Set maximum bytes per receive buffer.
269 * This appears to be an Ethernet frame size, not the buffer
270 * fragment size. It must be a multiple of four.
272 W16(ep, sen_genscc.scc_mrblr, 0x5f0);
274 /* Set CRC preset and mask.
276 W32(ep, sen_cpres, 0xffffffff);
277 W32(ep, sen_cmask, 0xdebb20e3);
279 W32(ep, sen_crcec, 0); /* CRC Error counter */
280 W32(ep, sen_alec, 0); /* alignment error counter */
281 W32(ep, sen_disfc, 0); /* discard frame counter */
283 W16(ep, sen_pads, 0x8888); /* Tx short frame pad character */
284 W16(ep, sen_retlim, 15); /* Retry limit threshold */
286 W16(ep, sen_maxflr, 0x5ee); /* maximum frame length register */
288 W16(ep, sen_minflr, PKT_MINBUF_SIZE); /* minimum frame length register */
290 W16(ep, sen_maxd1, 0x000005f0); /* maximum DMA1 length */
291 W16(ep, sen_maxd2, 0x000005f0); /* maximum DMA2 length */
293 /* Clear hash tables.
295 W16(ep, sen_gaddr1, 0);
296 W16(ep, sen_gaddr2, 0);
297 W16(ep, sen_gaddr3, 0);
298 W16(ep, sen_gaddr4, 0);
299 W16(ep, sen_iaddr1, 0);
300 W16(ep, sen_iaddr2, 0);
301 W16(ep, sen_iaddr3, 0);
302 W16(ep, sen_iaddr4, 0);
307 paddrh = ((u16) mac[5] << 8) | mac[4];
308 paddrm = ((u16) mac[3] << 8) | mac[2];
309 paddrl = ((u16) mac[1] << 8) | mac[0];
311 W16(ep, sen_paddrh, paddrh);
312 W16(ep, sen_paddrm, paddrm);
313 W16(ep, sen_paddrl, paddrl);
315 W16(ep, sen_pper, 0);
316 W16(ep, sen_taddrl, 0);
317 W16(ep, sen_taddrm, 0);
318 W16(ep, sen_taddrh, 0);
322 scc_cr_cmd(fep, CPM_CR_INIT_TRX);
324 W16(sccp, scc_scce, 0xffff);
326 /* Enable interrupts we wish to service.
328 W16(sccp, scc_sccm, SCCE_ENET_TXE | SCCE_ENET_RXF | SCCE_ENET_TXB);
330 /* Set GSMR_H to enable all normal operating modes.
331 * Set GSMR_L to enable Ethernet to MC68160.
333 W32(sccp, scc_gsmrh, 0);
335 SCC_GSMRL_TCI | SCC_GSMRL_TPL_48 | SCC_GSMRL_TPP_10 |
336 SCC_GSMRL_MODE_ENET);
338 /* Set sync/delimiters.
340 W16(sccp, scc_dsr, 0xd555);
342 /* Set processing mode. Use Ethernet CRC, catch broadcast, and
343 * start frame search 22 bit times after RENA.
345 W16(sccp, scc_psmr, SCC_PSMR_ENCRC | SCC_PSMR_NIB22);
347 /* Set full duplex mode if needed */
348 if (fep->phydev->duplex)
349 S16(sccp, scc_psmr, SCC_PSMR_LPB | SCC_PSMR_FDE);
351 S32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
354 static void stop(struct net_device *dev)
356 struct fs_enet_private *fep = netdev_priv(dev);
357 scc_t __iomem *sccp = fep->scc.sccp;
360 for (i = 0; (R16(sccp, scc_sccm) == 0) && i < SCC_RESET_DELAY; i++)
363 if (i == SCC_RESET_DELAY)
364 printk(KERN_WARNING DRV_MODULE_NAME
365 ": %s SCC timeout on graceful transmit stop\n",
368 W16(sccp, scc_sccm, 0);
369 C32(sccp, scc_gsmrl, SCC_GSMRL_ENR | SCC_GSMRL_ENT);
374 static void pre_request_irq(struct net_device *dev, int irq)
376 #ifndef CONFIG_PPC_MERGE
377 immap_t *immap = fs_enet_immap;
381 if (irq >= SIU_IRQ0 && irq < SIU_LEVEL7) {
383 siel = in_be32(&immap->im_siu_conf.sc_siel);
385 siel |= (0x80000000 >> irq);
387 siel &= ~(0x80000000 >> (irq & ~1));
388 out_be32(&immap->im_siu_conf.sc_siel, siel);
393 static void post_free_irq(struct net_device *dev, int irq)
398 static void napi_clear_rx_event(struct net_device *dev)
400 struct fs_enet_private *fep = netdev_priv(dev);
401 scc_t __iomem *sccp = fep->scc.sccp;
403 W16(sccp, scc_scce, SCC_NAPI_RX_EVENT_MSK);
406 static void napi_enable_rx(struct net_device *dev)
408 struct fs_enet_private *fep = netdev_priv(dev);
409 scc_t __iomem *sccp = fep->scc.sccp;
411 S16(sccp, scc_sccm, SCC_NAPI_RX_EVENT_MSK);
414 static void napi_disable_rx(struct net_device *dev)
416 struct fs_enet_private *fep = netdev_priv(dev);
417 scc_t __iomem *sccp = fep->scc.sccp;
419 C16(sccp, scc_sccm, SCC_NAPI_RX_EVENT_MSK);
422 static void rx_bd_done(struct net_device *dev)
427 static void tx_kickstart(struct net_device *dev)
432 static u32 get_int_events(struct net_device *dev)
434 struct fs_enet_private *fep = netdev_priv(dev);
435 scc_t __iomem *sccp = fep->scc.sccp;
437 return (u32) R16(sccp, scc_scce);
440 static void clear_int_events(struct net_device *dev, u32 int_events)
442 struct fs_enet_private *fep = netdev_priv(dev);
443 scc_t __iomem *sccp = fep->scc.sccp;
445 W16(sccp, scc_scce, int_events & 0xffff);
448 static void ev_error(struct net_device *dev, u32 int_events)
450 printk(KERN_WARNING DRV_MODULE_NAME
451 ": %s SCC ERROR(s) 0x%x\n", dev->name, int_events);
454 static int get_regs(struct net_device *dev, void *p, int *sizep)
456 struct fs_enet_private *fep = netdev_priv(dev);
458 if (*sizep < sizeof(scc_t) + sizeof(scc_enet_t __iomem *))
461 memcpy_fromio(p, fep->scc.sccp, sizeof(scc_t));
462 p = (char *)p + sizeof(scc_t);
464 memcpy_fromio(p, fep->scc.ep, sizeof(scc_enet_t __iomem *));
469 static int get_regs_len(struct net_device *dev)
471 return sizeof(scc_t) + sizeof(scc_enet_t __iomem *);
474 static void tx_restart(struct net_device *dev)
476 struct fs_enet_private *fep = netdev_priv(dev);
478 scc_cr_cmd(fep, CPM_CR_RESTART_TX);
483 /*************************************************************************/
485 const struct fs_ops fs_scc_ops = {
486 .setup_data = setup_data,
487 .cleanup_data = cleanup_data,
488 .set_multicast_list = set_multicast_list,
491 .pre_request_irq = pre_request_irq,
492 .post_free_irq = post_free_irq,
493 .napi_clear_rx_event = napi_clear_rx_event,
494 .napi_enable_rx = napi_enable_rx,
495 .napi_disable_rx = napi_disable_rx,
496 .rx_bd_done = rx_bd_done,
497 .tx_kickstart = tx_kickstart,
498 .get_int_events = get_int_events,
499 .clear_int_events = clear_int_events,
500 .ev_error = ev_error,
501 .get_regs = get_regs,
502 .get_regs_len = get_regs_len,
503 .tx_restart = tx_restart,
504 .allocate_bd = allocate_bd,