2 * FCC driver for Motorola MPC82xx (PQ2).
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/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
27 #include <linux/spinlock.h>
28 #include <linux/mii.h>
29 #include <linux/ethtool.h>
30 #include <linux/bitops.h>
32 #include <linux/platform_device.h>
33 #include <linux/phy.h>
34 #include <linux/of_address.h>
35 #include <linux/of_device.h>
36 #include <linux/of_irq.h>
37 #include <linux/gfp.h>
39 #include <asm/immap_cpm2.h>
40 #include <asm/mpc8260.h>
43 #include <asm/pgtable.h>
45 #include <asm/uaccess.h>
49 /*************************************************/
51 /* FCC access macros */
53 /* write, read, set bits, clear bits */
54 #define W32(_p, _m, _v) out_be32(&(_p)->_m, (_v))
55 #define R32(_p, _m) in_be32(&(_p)->_m)
56 #define S32(_p, _m, _v) W32(_p, _m, R32(_p, _m) | (_v))
57 #define C32(_p, _m, _v) W32(_p, _m, R32(_p, _m) & ~(_v))
59 #define W16(_p, _m, _v) out_be16(&(_p)->_m, (_v))
60 #define R16(_p, _m) in_be16(&(_p)->_m)
61 #define S16(_p, _m, _v) W16(_p, _m, R16(_p, _m) | (_v))
62 #define C16(_p, _m, _v) W16(_p, _m, R16(_p, _m) & ~(_v))
64 #define W8(_p, _m, _v) out_8(&(_p)->_m, (_v))
65 #define R8(_p, _m) in_8(&(_p)->_m)
66 #define S8(_p, _m, _v) W8(_p, _m, R8(_p, _m) | (_v))
67 #define C8(_p, _m, _v) W8(_p, _m, R8(_p, _m) & ~(_v))
69 /*************************************************/
71 #define FCC_MAX_MULTICAST_ADDRS 64
73 #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
74 #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
77 #define MAX_CR_CMD_LOOPS 10000
79 static inline int fcc_cr_cmd(struct fs_enet_private
*fep
, u32 op
)
81 const struct fs_platform_info
*fpi
= fep
->fpi
;
83 return cpm_command(fpi
->cp_command
, op
);
86 static int do_pd_setup(struct fs_enet_private
*fep
)
88 struct platform_device
*ofdev
= to_platform_device(fep
->dev
);
89 struct fs_platform_info
*fpi
= fep
->fpi
;
92 fep
->interrupt
= irq_of_parse_and_map(ofdev
->dev
.of_node
, 0);
93 if (fep
->interrupt
== NO_IRQ
)
96 fep
->fcc
.fccp
= of_iomap(ofdev
->dev
.of_node
, 0);
100 fep
->fcc
.ep
= of_iomap(ofdev
->dev
.of_node
, 1);
104 fep
->fcc
.fcccp
= of_iomap(ofdev
->dev
.of_node
, 2);
108 fep
->fcc
.mem
= (void __iomem
*)cpm2_immr
;
109 fpi
->dpram_offset
= cpm_dpalloc(128, 32);
110 if (IS_ERR_VALUE(fpi
->dpram_offset
)) {
111 ret
= fpi
->dpram_offset
;
118 iounmap(fep
->fcc
.fcccp
);
120 iounmap(fep
->fcc
.ep
);
122 iounmap(fep
->fcc
.fccp
);
127 #define FCC_NAPI_RX_EVENT_MSK (FCC_ENET_RXF | FCC_ENET_RXB)
128 #define FCC_NAPI_TX_EVENT_MSK (FCC_ENET_TXB)
129 #define FCC_RX_EVENT (FCC_ENET_RXF)
130 #define FCC_TX_EVENT (FCC_ENET_TXB)
131 #define FCC_ERR_EVENT_MSK (FCC_ENET_TXE)
133 static int setup_data(struct net_device
*dev
)
135 struct fs_enet_private
*fep
= netdev_priv(dev
);
137 if (do_pd_setup(fep
) != 0)
140 fep
->ev_napi_rx
= FCC_NAPI_RX_EVENT_MSK
;
141 fep
->ev_napi_tx
= FCC_NAPI_TX_EVENT_MSK
;
142 fep
->ev_rx
= FCC_RX_EVENT
;
143 fep
->ev_tx
= FCC_TX_EVENT
;
144 fep
->ev_err
= FCC_ERR_EVENT_MSK
;
149 static int allocate_bd(struct net_device
*dev
)
151 struct fs_enet_private
*fep
= netdev_priv(dev
);
152 const struct fs_platform_info
*fpi
= fep
->fpi
;
154 fep
->ring_base
= (void __iomem __force
*)dma_alloc_coherent(fep
->dev
,
155 (fpi
->tx_ring
+ fpi
->rx_ring
) *
156 sizeof(cbd_t
), &fep
->ring_mem_addr
,
158 if (fep
->ring_base
== NULL
)
164 static void free_bd(struct net_device
*dev
)
166 struct fs_enet_private
*fep
= netdev_priv(dev
);
167 const struct fs_platform_info
*fpi
= fep
->fpi
;
170 dma_free_coherent(fep
->dev
,
171 (fpi
->tx_ring
+ fpi
->rx_ring
) * sizeof(cbd_t
),
172 (void __force
*)fep
->ring_base
, fep
->ring_mem_addr
);
175 static void cleanup_data(struct net_device
*dev
)
180 static void set_promiscuous_mode(struct net_device
*dev
)
182 struct fs_enet_private
*fep
= netdev_priv(dev
);
183 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
185 S32(fccp
, fcc_fpsmr
, FCC_PSMR_PRO
);
188 static void set_multicast_start(struct net_device
*dev
)
190 struct fs_enet_private
*fep
= netdev_priv(dev
);
191 fcc_enet_t __iomem
*ep
= fep
->fcc
.ep
;
193 W32(ep
, fen_gaddrh
, 0);
194 W32(ep
, fen_gaddrl
, 0);
197 static void set_multicast_one(struct net_device
*dev
, const u8
*mac
)
199 struct fs_enet_private
*fep
= netdev_priv(dev
);
200 fcc_enet_t __iomem
*ep
= fep
->fcc
.ep
;
201 u16 taddrh
, taddrm
, taddrl
;
203 taddrh
= ((u16
)mac
[5] << 8) | mac
[4];
204 taddrm
= ((u16
)mac
[3] << 8) | mac
[2];
205 taddrl
= ((u16
)mac
[1] << 8) | mac
[0];
207 W16(ep
, fen_taddrh
, taddrh
);
208 W16(ep
, fen_taddrm
, taddrm
);
209 W16(ep
, fen_taddrl
, taddrl
);
210 fcc_cr_cmd(fep
, CPM_CR_SET_GADDR
);
213 static void set_multicast_finish(struct net_device
*dev
)
215 struct fs_enet_private
*fep
= netdev_priv(dev
);
216 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
217 fcc_enet_t __iomem
*ep
= fep
->fcc
.ep
;
219 /* clear promiscuous always */
220 C32(fccp
, fcc_fpsmr
, FCC_PSMR_PRO
);
222 /* if all multi or too many multicasts; just enable all */
223 if ((dev
->flags
& IFF_ALLMULTI
) != 0 ||
224 netdev_mc_count(dev
) > FCC_MAX_MULTICAST_ADDRS
) {
226 W32(ep
, fen_gaddrh
, 0xffffffff);
227 W32(ep
, fen_gaddrl
, 0xffffffff);
231 fep
->fcc
.gaddrh
= R32(ep
, fen_gaddrh
);
232 fep
->fcc
.gaddrl
= R32(ep
, fen_gaddrl
);
235 static void set_multicast_list(struct net_device
*dev
)
237 struct netdev_hw_addr
*ha
;
239 if ((dev
->flags
& IFF_PROMISC
) == 0) {
240 set_multicast_start(dev
);
241 netdev_for_each_mc_addr(ha
, dev
)
242 set_multicast_one(dev
, ha
->addr
);
243 set_multicast_finish(dev
);
245 set_promiscuous_mode(dev
);
248 static void restart(struct net_device
*dev
)
250 struct fs_enet_private
*fep
= netdev_priv(dev
);
251 const struct fs_platform_info
*fpi
= fep
->fpi
;
252 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
253 fcc_c_t __iomem
*fcccp
= fep
->fcc
.fcccp
;
254 fcc_enet_t __iomem
*ep
= fep
->fcc
.ep
;
255 dma_addr_t rx_bd_base_phys
, tx_bd_base_phys
;
256 u16 paddrh
, paddrm
, paddrl
;
257 const unsigned char *mac
;
260 C32(fccp
, fcc_gfmr
, FCC_GFMR_ENR
| FCC_GFMR_ENT
);
262 /* clear everything (slow & steady does it) */
263 for (i
= 0; i
< sizeof(*ep
); i
++)
264 out_8((u8 __iomem
*)ep
+ i
, 0);
266 /* get physical address */
267 rx_bd_base_phys
= fep
->ring_mem_addr
;
268 tx_bd_base_phys
= rx_bd_base_phys
+ sizeof(cbd_t
) * fpi
->rx_ring
;
271 W32(ep
, fen_genfcc
.fcc_rbase
, rx_bd_base_phys
);
272 W32(ep
, fen_genfcc
.fcc_tbase
, tx_bd_base_phys
);
274 /* Set maximum bytes per receive buffer.
275 * It must be a multiple of 32.
277 W16(ep
, fen_genfcc
.fcc_mrblr
, PKT_MAXBLR_SIZE
);
279 W32(ep
, fen_genfcc
.fcc_rstate
, (CPMFCR_GBL
| CPMFCR_EB
) << 24);
280 W32(ep
, fen_genfcc
.fcc_tstate
, (CPMFCR_GBL
| CPMFCR_EB
) << 24);
282 /* Allocate space in the reserved FCC area of DPRAM for the
283 * internal buffers. No one uses this space (yet), so we
284 * can do this. Later, we will add resource management for
288 W16(ep
, fen_genfcc
.fcc_riptr
, fpi
->dpram_offset
);
289 W16(ep
, fen_genfcc
.fcc_tiptr
, fpi
->dpram_offset
+ 32);
291 W16(ep
, fen_padptr
, fpi
->dpram_offset
+ 64);
293 /* fill with special symbol... */
294 memset_io(fep
->fcc
.mem
+ fpi
->dpram_offset
+ 64, 0x88, 32);
296 W32(ep
, fen_genfcc
.fcc_rbptr
, 0);
297 W32(ep
, fen_genfcc
.fcc_tbptr
, 0);
298 W32(ep
, fen_genfcc
.fcc_rcrc
, 0);
299 W32(ep
, fen_genfcc
.fcc_tcrc
, 0);
300 W16(ep
, fen_genfcc
.fcc_res1
, 0);
301 W32(ep
, fen_genfcc
.fcc_res2
, 0);
304 W32(ep
, fen_camptr
, 0);
306 /* Set CRC preset and mask */
307 W32(ep
, fen_cmask
, 0xdebb20e3);
308 W32(ep
, fen_cpres
, 0xffffffff);
310 W32(ep
, fen_crcec
, 0); /* CRC Error counter */
311 W32(ep
, fen_alec
, 0); /* alignment error counter */
312 W32(ep
, fen_disfc
, 0); /* discard frame counter */
313 W16(ep
, fen_retlim
, 15); /* Retry limit threshold */
314 W16(ep
, fen_pper
, 0); /* Normal persistence */
316 /* set group address */
317 W32(ep
, fen_gaddrh
, fep
->fcc
.gaddrh
);
318 W32(ep
, fen_gaddrl
, fep
->fcc
.gaddrh
);
320 /* Clear hash filter tables */
321 W32(ep
, fen_iaddrh
, 0);
322 W32(ep
, fen_iaddrl
, 0);
324 /* Clear the Out-of-sequence TxBD */
325 W16(ep
, fen_tfcstat
, 0);
326 W16(ep
, fen_tfclen
, 0);
327 W32(ep
, fen_tfcptr
, 0);
329 W16(ep
, fen_mflr
, PKT_MAXBUF_SIZE
); /* maximum frame length register */
330 W16(ep
, fen_minflr
, PKT_MINBUF_SIZE
); /* minimum frame length register */
334 paddrh
= ((u16
)mac
[5] << 8) | mac
[4];
335 paddrm
= ((u16
)mac
[3] << 8) | mac
[2];
336 paddrl
= ((u16
)mac
[1] << 8) | mac
[0];
338 W16(ep
, fen_paddrh
, paddrh
);
339 W16(ep
, fen_paddrm
, paddrm
);
340 W16(ep
, fen_paddrl
, paddrl
);
342 W16(ep
, fen_taddrh
, 0);
343 W16(ep
, fen_taddrm
, 0);
344 W16(ep
, fen_taddrl
, 0);
346 W16(ep
, fen_maxd1
, 1520); /* maximum DMA1 length */
347 W16(ep
, fen_maxd2
, 1520); /* maximum DMA2 length */
349 /* Clear stat counters, in case we ever enable RMON */
350 W32(ep
, fen_octc
, 0);
351 W32(ep
, fen_colc
, 0);
352 W32(ep
, fen_broc
, 0);
353 W32(ep
, fen_mulc
, 0);
354 W32(ep
, fen_uspc
, 0);
355 W32(ep
, fen_frgc
, 0);
356 W32(ep
, fen_ospc
, 0);
357 W32(ep
, fen_jbrc
, 0);
358 W32(ep
, fen_p64c
, 0);
359 W32(ep
, fen_p65c
, 0);
360 W32(ep
, fen_p128c
, 0);
361 W32(ep
, fen_p256c
, 0);
362 W32(ep
, fen_p512c
, 0);
363 W32(ep
, fen_p1024c
, 0);
365 W16(ep
, fen_rfthr
, 0); /* Suggested by manual */
366 W16(ep
, fen_rfcnt
, 0);
367 W16(ep
, fen_cftype
, 0);
371 /* adjust to speed (for RMII mode) */
373 if (fep
->phydev
->speed
== 100)
374 C8(fcccp
, fcc_gfemr
, 0x20);
376 S8(fcccp
, fcc_gfemr
, 0x20);
379 fcc_cr_cmd(fep
, CPM_CR_INIT_TRX
);
382 W16(fccp
, fcc_fcce
, 0xffff);
384 /* Enable interrupts we wish to service */
385 W16(fccp
, fcc_fccm
, FCC_ENET_TXE
| FCC_ENET_RXF
| FCC_ENET_TXB
);
387 /* Set GFMR to enable Ethernet operating mode */
388 W32(fccp
, fcc_gfmr
, FCC_GFMR_TCI
| FCC_GFMR_MODE_ENET
);
390 /* set sync/delimiters */
391 W16(fccp
, fcc_fdsr
, 0xd555);
393 W32(fccp
, fcc_fpsmr
, FCC_PSMR_ENCRC
);
396 S32(fccp
, fcc_fpsmr
, FCC_PSMR_RMII
);
398 /* adjust to duplex mode */
399 if (fep
->phydev
->duplex
)
400 S32(fccp
, fcc_fpsmr
, FCC_PSMR_FDE
| FCC_PSMR_LPB
);
402 C32(fccp
, fcc_fpsmr
, FCC_PSMR_FDE
| FCC_PSMR_LPB
);
404 /* Restore multicast and promiscuous settings */
405 set_multicast_list(dev
);
407 S32(fccp
, fcc_gfmr
, FCC_GFMR_ENR
| FCC_GFMR_ENT
);
410 static void stop(struct net_device
*dev
)
412 struct fs_enet_private
*fep
= netdev_priv(dev
);
413 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
416 C32(fccp
, fcc_gfmr
, FCC_GFMR_ENR
| FCC_GFMR_ENT
);
419 W16(fccp
, fcc_fcce
, 0xffff);
421 /* clear interrupt mask */
422 W16(fccp
, fcc_fccm
, 0);
427 static void napi_clear_rx_event(struct net_device
*dev
)
429 struct fs_enet_private
*fep
= netdev_priv(dev
);
430 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
432 W16(fccp
, fcc_fcce
, FCC_NAPI_RX_EVENT_MSK
);
435 static void napi_enable_rx(struct net_device
*dev
)
437 struct fs_enet_private
*fep
= netdev_priv(dev
);
438 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
440 S16(fccp
, fcc_fccm
, FCC_NAPI_RX_EVENT_MSK
);
443 static void napi_disable_rx(struct net_device
*dev
)
445 struct fs_enet_private
*fep
= netdev_priv(dev
);
446 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
448 C16(fccp
, fcc_fccm
, FCC_NAPI_RX_EVENT_MSK
);
451 static void napi_clear_tx_event(struct net_device
*dev
)
453 struct fs_enet_private
*fep
= netdev_priv(dev
);
454 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
456 W16(fccp
, fcc_fcce
, FCC_NAPI_TX_EVENT_MSK
);
459 static void napi_enable_tx(struct net_device
*dev
)
461 struct fs_enet_private
*fep
= netdev_priv(dev
);
462 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
464 S16(fccp
, fcc_fccm
, FCC_NAPI_TX_EVENT_MSK
);
467 static void napi_disable_tx(struct net_device
*dev
)
469 struct fs_enet_private
*fep
= netdev_priv(dev
);
470 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
472 C16(fccp
, fcc_fccm
, FCC_NAPI_TX_EVENT_MSK
);
475 static void rx_bd_done(struct net_device
*dev
)
480 static void tx_kickstart(struct net_device
*dev
)
482 struct fs_enet_private
*fep
= netdev_priv(dev
);
483 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
485 S16(fccp
, fcc_ftodr
, 0x8000);
488 static u32
get_int_events(struct net_device
*dev
)
490 struct fs_enet_private
*fep
= netdev_priv(dev
);
491 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
493 return (u32
)R16(fccp
, fcc_fcce
);
496 static void clear_int_events(struct net_device
*dev
, u32 int_events
)
498 struct fs_enet_private
*fep
= netdev_priv(dev
);
499 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
501 W16(fccp
, fcc_fcce
, int_events
& 0xffff);
504 static void ev_error(struct net_device
*dev
, u32 int_events
)
506 struct fs_enet_private
*fep
= netdev_priv(dev
);
508 dev_warn(fep
->dev
, "FS_ENET ERROR(s) 0x%x\n", int_events
);
511 static int get_regs(struct net_device
*dev
, void *p
, int *sizep
)
513 struct fs_enet_private
*fep
= netdev_priv(dev
);
515 if (*sizep
< sizeof(fcc_t
) + sizeof(fcc_enet_t
) + 1)
518 memcpy_fromio(p
, fep
->fcc
.fccp
, sizeof(fcc_t
));
519 p
= (char *)p
+ sizeof(fcc_t
);
521 memcpy_fromio(p
, fep
->fcc
.ep
, sizeof(fcc_enet_t
));
522 p
= (char *)p
+ sizeof(fcc_enet_t
);
524 memcpy_fromio(p
, fep
->fcc
.fcccp
, 1);
528 static int get_regs_len(struct net_device
*dev
)
530 return sizeof(fcc_t
) + sizeof(fcc_enet_t
) + 1;
533 /* Some transmit errors cause the transmitter to shut
534 * down. We now issue a restart transmit.
535 * Also, to workaround 8260 device erratum CPM37, we must
536 * disable and then re-enable the transmitterfollowing a
537 * Late Collision, Underrun, or Retry Limit error.
538 * In addition, tbptr may point beyond BDs beyond still marked
539 * as ready due to internal pipelining, so we need to look back
540 * through the BDs and adjust tbptr to point to the last BD
541 * marked as ready. This may result in some buffers being
544 static void tx_restart(struct net_device
*dev
)
546 struct fs_enet_private
*fep
= netdev_priv(dev
);
547 fcc_t __iomem
*fccp
= fep
->fcc
.fccp
;
548 const struct fs_platform_info
*fpi
= fep
->fpi
;
549 fcc_enet_t __iomem
*ep
= fep
->fcc
.ep
;
550 cbd_t __iomem
*curr_tbptr
;
551 cbd_t __iomem
*recheck_bd
;
552 cbd_t __iomem
*prev_bd
;
553 cbd_t __iomem
*last_tx_bd
;
555 last_tx_bd
= fep
->tx_bd_base
+ ((fpi
->tx_ring
- 1) * sizeof(cbd_t
));
557 /* get the current bd held in TBPTR and scan back from this point */
558 recheck_bd
= curr_tbptr
= (cbd_t __iomem
*)
559 ((R32(ep
, fen_genfcc
.fcc_tbptr
) - fep
->ring_mem_addr
) +
562 prev_bd
= (recheck_bd
== fep
->tx_bd_base
) ? last_tx_bd
: recheck_bd
- 1;
564 /* Move through the bds in reverse, look for the earliest buffer
565 * that is not ready. Adjust TBPTR to the following buffer */
566 while ((CBDR_SC(prev_bd
) & BD_ENET_TX_READY
) != 0) {
567 /* Go back one buffer */
568 recheck_bd
= prev_bd
;
570 /* update the previous buffer */
571 prev_bd
= (prev_bd
== fep
->tx_bd_base
) ? last_tx_bd
: prev_bd
- 1;
573 /* We should never see all bds marked as ready, check anyway */
574 if (recheck_bd
== curr_tbptr
)
577 /* Now update the TBPTR and dirty flag to the current buffer */
578 W32(ep
, fen_genfcc
.fcc_tbptr
,
579 (uint
) (((void *)recheck_bd
- fep
->ring_base
) +
580 fep
->ring_mem_addr
));
581 fep
->dirty_tx
= recheck_bd
;
583 C32(fccp
, fcc_gfmr
, FCC_GFMR_ENT
);
585 S32(fccp
, fcc_gfmr
, FCC_GFMR_ENT
);
587 fcc_cr_cmd(fep
, CPM_CR_RESTART_TX
);
590 /*************************************************************************/
592 const struct fs_ops fs_fcc_ops
= {
593 .setup_data
= setup_data
,
594 .cleanup_data
= cleanup_data
,
595 .set_multicast_list
= set_multicast_list
,
598 .napi_clear_rx_event
= napi_clear_rx_event
,
599 .napi_enable_rx
= napi_enable_rx
,
600 .napi_disable_rx
= napi_disable_rx
,
601 .napi_clear_tx_event
= napi_clear_tx_event
,
602 .napi_enable_tx
= napi_enable_tx
,
603 .napi_disable_tx
= napi_disable_tx
,
604 .rx_bd_done
= rx_bd_done
,
605 .tx_kickstart
= tx_kickstart
,
606 .get_int_events
= get_int_events
,
607 .clear_int_events
= clear_int_events
,
608 .ev_error
= ev_error
,
609 .get_regs
= get_regs
,
610 .get_regs_len
= get_regs_len
,
611 .tx_restart
= tx_restart
,
612 .allocate_bd
= allocate_bd
,