2 * Freescale Ethernet controllers
4 * Copyright (c) 2005 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/sched.h>
19 #include <linux/string.h>
20 #include <linux/ptrace.h>
21 #include <linux/errno.h>
22 #include <linux/ioport.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/pci.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/spinlock.h>
32 #include <linux/mii.h>
33 #include <linux/ethtool.h>
34 #include <linux/bitops.h>
36 #include <linux/platform_device.h>
39 #include <asm/uaccess.h>
42 #include <asm/8xx_immap.h>
43 #include <asm/pgtable.h>
44 #include <asm/mpc8xx.h>
45 #include <asm/commproc.h>
50 /*************************************************/
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)
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)
67 #define FW(_fecp, _reg, _v) __fs_out32(&(_fecp)->fec_ ## _reg, (_v))
70 #define FR(_fecp, _reg) __fs_in32(&(_fecp)->fec_ ## _reg)
73 #define FS(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) | (_v))
76 #define FC(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) & ~(_v))
79 /* CRC polynomium used by the FEC for the multicast group filtering */
80 #define FEC_CRC_POLY 0x04C11DB7
82 #define FEC_MAX_MULTICAST_ADDRS 64
84 /* Interrupt events/masks.
86 #define FEC_ENET_HBERR 0x80000000U /* Heartbeat error */
87 #define FEC_ENET_BABR 0x40000000U /* Babbling receiver */
88 #define FEC_ENET_BABT 0x20000000U /* Babbling transmitter */
89 #define FEC_ENET_GRA 0x10000000U /* Graceful stop complete */
90 #define FEC_ENET_TXF 0x08000000U /* Full frame transmitted */
91 #define FEC_ENET_TXB 0x04000000U /* A buffer was transmitted */
92 #define FEC_ENET_RXF 0x02000000U /* Full frame received */
93 #define FEC_ENET_RXB 0x01000000U /* A buffer was received */
94 #define FEC_ENET_MII 0x00800000U /* MII interrupt */
95 #define FEC_ENET_EBERR 0x00400000U /* SDMA bus error */
97 #define FEC_ECNTRL_PINMUX 0x00000004
98 #define FEC_ECNTRL_ETHER_EN 0x00000002
99 #define FEC_ECNTRL_RESET 0x00000001
101 #define FEC_RCNTRL_BC_REJ 0x00000010
102 #define FEC_RCNTRL_PROM 0x00000008
103 #define FEC_RCNTRL_MII_MODE 0x00000004
104 #define FEC_RCNTRL_DRT 0x00000002
105 #define FEC_RCNTRL_LOOP 0x00000001
107 #define FEC_TCNTRL_FDEN 0x00000004
108 #define FEC_TCNTRL_HBC 0x00000002
109 #define FEC_TCNTRL_GTS 0x00000001
112 /* Make MII read/write commands for the FEC.
114 #define mk_mii_read(REG) (0x60020000 | ((REG & 0x1f) << 18))
115 #define mk_mii_write(REG, VAL) (0x50020000 | ((REG & 0x1f) << 18) | (VAL & 0xffff))
118 #define FEC_MII_LOOPS 10000
121 * Delay to wait for FEC reset command to complete (in us)
123 #define FEC_RESET_DELAY 50
125 static int whack_reset(fec_t
* fecp
)
129 FW(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_RESET
);
130 for (i
= 0; i
< FEC_RESET_DELAY
; i
++) {
131 if ((FR(fecp
, ecntrl
) & FEC_ECNTRL_RESET
) == 0)
139 static int do_pd_setup(struct fs_enet_private
*fep
)
141 struct platform_device
*pdev
= to_platform_device(fep
->dev
);
144 /* Fill out IRQ field */
145 fep
->interrupt
= platform_get_irq_byname(pdev
,"interrupt");
146 if (fep
->interrupt
< 0)
149 r
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
150 fep
->fec
.fecp
=(void*)r
->start
;
152 if(fep
->fec
.fecp
== NULL
)
159 #define FEC_NAPI_RX_EVENT_MSK (FEC_ENET_RXF | FEC_ENET_RXB)
160 #define FEC_RX_EVENT (FEC_ENET_RXF)
161 #define FEC_TX_EVENT (FEC_ENET_TXF)
162 #define FEC_ERR_EVENT_MSK (FEC_ENET_HBERR | FEC_ENET_BABR | \
163 FEC_ENET_BABT | FEC_ENET_EBERR)
165 static int setup_data(struct net_device
*dev
)
167 struct fs_enet_private
*fep
= netdev_priv(dev
);
169 if (do_pd_setup(fep
) != 0)
175 fep
->ev_napi_rx
= FEC_NAPI_RX_EVENT_MSK
;
176 fep
->ev_rx
= FEC_RX_EVENT
;
177 fep
->ev_tx
= FEC_TX_EVENT
;
178 fep
->ev_err
= FEC_ERR_EVENT_MSK
;
183 static int allocate_bd(struct net_device
*dev
)
185 struct fs_enet_private
*fep
= netdev_priv(dev
);
186 const struct fs_platform_info
*fpi
= fep
->fpi
;
188 fep
->ring_base
= dma_alloc_coherent(fep
->dev
,
189 (fpi
->tx_ring
+ fpi
->rx_ring
) *
190 sizeof(cbd_t
), &fep
->ring_mem_addr
,
192 if (fep
->ring_base
== NULL
)
198 static void free_bd(struct net_device
*dev
)
200 struct fs_enet_private
*fep
= netdev_priv(dev
);
201 const struct fs_platform_info
*fpi
= fep
->fpi
;
204 dma_free_coherent(fep
->dev
, (fpi
->tx_ring
+ fpi
->rx_ring
)
210 static void cleanup_data(struct net_device
*dev
)
215 static void set_promiscuous_mode(struct net_device
*dev
)
217 struct fs_enet_private
*fep
= netdev_priv(dev
);
218 fec_t
*fecp
= fep
->fec
.fecp
;
220 FS(fecp
, r_cntrl
, FEC_RCNTRL_PROM
);
223 static void set_multicast_start(struct net_device
*dev
)
225 struct fs_enet_private
*fep
= netdev_priv(dev
);
231 static void set_multicast_one(struct net_device
*dev
, const u8
*mac
)
233 struct fs_enet_private
*fep
= netdev_priv(dev
);
234 int temp
, hash_index
, i
, j
;
239 for (i
= 0; i
< 6; i
++) {
241 for (j
= 0; j
< 8; j
++) {
244 if (msb
^ (byte
& 0x1))
250 temp
= (crc
& 0x3f) >> 1;
251 hash_index
= ((temp
& 0x01) << 4) |
252 ((temp
& 0x02) << 2) |
254 ((temp
& 0x08) >> 2) |
255 ((temp
& 0x10) >> 4);
256 csrVal
= 1 << hash_index
;
258 fep
->fec
.hthi
|= csrVal
;
260 fep
->fec
.htlo
|= csrVal
;
263 static void set_multicast_finish(struct net_device
*dev
)
265 struct fs_enet_private
*fep
= netdev_priv(dev
);
266 fec_t
*fecp
= fep
->fec
.fecp
;
268 /* if all multi or too many multicasts; just enable all */
269 if ((dev
->flags
& IFF_ALLMULTI
) != 0 ||
270 dev
->mc_count
> FEC_MAX_MULTICAST_ADDRS
) {
271 fep
->fec
.hthi
= 0xffffffffU
;
272 fep
->fec
.htlo
= 0xffffffffU
;
275 FC(fecp
, r_cntrl
, FEC_RCNTRL_PROM
);
276 FW(fecp
, hash_table_high
, fep
->fec
.hthi
);
277 FW(fecp
, hash_table_low
, fep
->fec
.htlo
);
280 static void set_multicast_list(struct net_device
*dev
)
282 struct dev_mc_list
*pmc
;
284 if ((dev
->flags
& IFF_PROMISC
) == 0) {
285 set_multicast_start(dev
);
286 for (pmc
= dev
->mc_list
; pmc
!= NULL
; pmc
= pmc
->next
)
287 set_multicast_one(dev
, pmc
->dmi_addr
);
288 set_multicast_finish(dev
);
290 set_promiscuous_mode(dev
);
293 static void restart(struct net_device
*dev
)
296 immap_t
*immap
= fs_enet_immap
;
299 struct fs_enet_private
*fep
= netdev_priv(dev
);
300 fec_t
*fecp
= fep
->fec
.fecp
;
301 const struct fs_platform_info
*fpi
= fep
->fpi
;
302 dma_addr_t rx_bd_base_phys
, tx_bd_base_phys
;
306 r
= whack_reset(fep
->fec
.fecp
);
308 printk(KERN_ERR DRV_MODULE_NAME
309 ": %s FEC Reset FAILED!\n", dev
->name
);
312 * Set station address.
314 addrhi
= ((u32
) dev
->dev_addr
[0] << 24) |
315 ((u32
) dev
->dev_addr
[1] << 16) |
316 ((u32
) dev
->dev_addr
[2] << 8) |
317 (u32
) dev
->dev_addr
[3];
318 addrlo
= ((u32
) dev
->dev_addr
[4] << 24) |
319 ((u32
) dev
->dev_addr
[5] << 16);
320 FW(fecp
, addr_low
, addrhi
);
321 FW(fecp
, addr_high
, addrlo
);
324 * Reset all multicast.
326 FW(fecp
, hash_table_high
, fep
->fec
.hthi
);
327 FW(fecp
, hash_table_low
, fep
->fec
.htlo
);
330 * Set maximum receive buffer size.
332 FW(fecp
, r_buff_size
, PKT_MAXBLR_SIZE
);
333 FW(fecp
, r_hash
, PKT_MAXBUF_SIZE
);
335 /* get physical address */
336 rx_bd_base_phys
= fep
->ring_mem_addr
;
337 tx_bd_base_phys
= rx_bd_base_phys
+ sizeof(cbd_t
) * fpi
->rx_ring
;
340 * Set receive and transmit descriptor base.
342 FW(fecp
, r_des_start
, rx_bd_base_phys
);
343 FW(fecp
, x_des_start
, tx_bd_base_phys
);
348 * Enable big endian and don't care about SDMA FC.
350 FW(fecp
, fun_code
, 0x78000000);
355 FW(fecp
, mii_speed
, fep
->mii_bus
->fec
.mii_speed
);
358 * Clear any outstanding interrupt.
360 FW(fecp
, ievent
, 0xffc0);
361 FW(fecp
, ivec
, (fep
->interrupt
/ 2) << 29);
365 * adjust to speed (only for DUET & RMII)
369 cptr
= in_be32(&immap
->im_cpm
.cp_cptr
);
370 switch (fs_get_fec_index(fpi
->fs_no
)) {
373 if (fep
->speed
== 10)
375 else if (fep
->speed
== 100)
380 if (fep
->speed
== 10)
382 else if (fep
->speed
== 100)
386 BUG(); /* should never happen */
389 out_be32(&immap
->im_cpm
.cp_cptr
, cptr
);
393 FW(fecp
, r_cntrl
, FEC_RCNTRL_MII_MODE
); /* MII enable */
395 * adjust to duplex mode
398 FC(fecp
, r_cntrl
, FEC_RCNTRL_DRT
);
399 FS(fecp
, x_cntrl
, FEC_TCNTRL_FDEN
); /* FD enable */
401 FS(fecp
, r_cntrl
, FEC_RCNTRL_DRT
);
402 FC(fecp
, x_cntrl
, FEC_TCNTRL_FDEN
); /* FD disable */
406 * Enable interrupts we wish to service.
408 FW(fecp
, imask
, FEC_ENET_TXF
| FEC_ENET_TXB
|
409 FEC_ENET_RXF
| FEC_ENET_RXB
);
412 * And last, enable the transmit and receive processing.
414 FW(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_ETHER_EN
);
415 FW(fecp
, r_des_active
, 0x01000000);
418 static void stop(struct net_device
*dev
)
420 struct fs_enet_private
*fep
= netdev_priv(dev
);
421 fec_t
*fecp
= fep
->fec
.fecp
;
422 struct fs_enet_mii_bus
*bus
= fep
->mii_bus
;
423 const struct fs_mii_bus_info
*bi
= bus
->bus_info
;
426 if ((FR(fecp
, ecntrl
) & FEC_ECNTRL_ETHER_EN
) == 0)
427 return; /* already down */
429 FW(fecp
, x_cntrl
, 0x01); /* Graceful transmit stop */
430 for (i
= 0; ((FR(fecp
, ievent
) & 0x10000000) == 0) &&
431 i
< FEC_RESET_DELAY
; i
++)
434 if (i
== FEC_RESET_DELAY
)
435 printk(KERN_WARNING DRV_MODULE_NAME
436 ": %s FEC timeout on graceful transmit stop\n",
439 * Disable FEC. Let only MII interrupts.
442 FC(fecp
, ecntrl
, FEC_ECNTRL_ETHER_EN
);
446 /* shut down FEC1? that's where the mii bus is */
447 if (fep
->fec
.idx
== 0 && bus
->refs
> 1 && bi
->method
== fsmii_fec
) {
448 FS(fecp
, r_cntrl
, FEC_RCNTRL_MII_MODE
); /* MII enable */
449 FS(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_ETHER_EN
);
450 FW(fecp
, ievent
, FEC_ENET_MII
);
451 FW(fecp
, mii_speed
, bus
->fec
.mii_speed
);
455 static void pre_request_irq(struct net_device
*dev
, int irq
)
457 immap_t
*immap
= fs_enet_immap
;
461 if (irq
>= SIU_IRQ0
&& irq
< SIU_LEVEL7
) {
463 siel
= in_be32(&immap
->im_siu_conf
.sc_siel
);
465 siel
|= (0x80000000 >> irq
);
467 siel
&= ~(0x80000000 >> (irq
& ~1));
468 out_be32(&immap
->im_siu_conf
.sc_siel
, siel
);
472 static void post_free_irq(struct net_device
*dev
, int irq
)
477 static void napi_clear_rx_event(struct net_device
*dev
)
479 struct fs_enet_private
*fep
= netdev_priv(dev
);
480 fec_t
*fecp
= fep
->fec
.fecp
;
482 FW(fecp
, ievent
, FEC_NAPI_RX_EVENT_MSK
);
485 static void napi_enable_rx(struct net_device
*dev
)
487 struct fs_enet_private
*fep
= netdev_priv(dev
);
488 fec_t
*fecp
= fep
->fec
.fecp
;
490 FS(fecp
, imask
, FEC_NAPI_RX_EVENT_MSK
);
493 static void napi_disable_rx(struct net_device
*dev
)
495 struct fs_enet_private
*fep
= netdev_priv(dev
);
496 fec_t
*fecp
= fep
->fec
.fecp
;
498 FC(fecp
, imask
, FEC_NAPI_RX_EVENT_MSK
);
501 static void rx_bd_done(struct net_device
*dev
)
503 struct fs_enet_private
*fep
= netdev_priv(dev
);
504 fec_t
*fecp
= fep
->fec
.fecp
;
506 FW(fecp
, r_des_active
, 0x01000000);
509 static void tx_kickstart(struct net_device
*dev
)
511 struct fs_enet_private
*fep
= netdev_priv(dev
);
512 fec_t
*fecp
= fep
->fec
.fecp
;
514 FW(fecp
, x_des_active
, 0x01000000);
517 static u32
get_int_events(struct net_device
*dev
)
519 struct fs_enet_private
*fep
= netdev_priv(dev
);
520 fec_t
*fecp
= fep
->fec
.fecp
;
522 return FR(fecp
, ievent
) & FR(fecp
, imask
);
525 static void clear_int_events(struct net_device
*dev
, u32 int_events
)
527 struct fs_enet_private
*fep
= netdev_priv(dev
);
528 fec_t
*fecp
= fep
->fec
.fecp
;
530 FW(fecp
, ievent
, int_events
);
533 static void ev_error(struct net_device
*dev
, u32 int_events
)
535 printk(KERN_WARNING DRV_MODULE_NAME
536 ": %s FEC ERROR(s) 0x%x\n", dev
->name
, int_events
);
539 int get_regs(struct net_device
*dev
, void *p
, int *sizep
)
541 struct fs_enet_private
*fep
= netdev_priv(dev
);
543 if (*sizep
< sizeof(fec_t
))
546 memcpy_fromio(p
, fep
->fec
.fecp
, sizeof(fec_t
));
551 int get_regs_len(struct net_device
*dev
)
553 return sizeof(fec_t
);
556 void tx_restart(struct net_device
*dev
)
561 /*************************************************************************/
563 const struct fs_ops fs_fec_ops
= {
564 .setup_data
= setup_data
,
565 .cleanup_data
= cleanup_data
,
566 .set_multicast_list
= set_multicast_list
,
569 .pre_request_irq
= pre_request_irq
,
570 .post_free_irq
= post_free_irq
,
571 .napi_clear_rx_event
= napi_clear_rx_event
,
572 .napi_enable_rx
= napi_enable_rx
,
573 .napi_disable_rx
= napi_disable_rx
,
574 .rx_bd_done
= rx_bd_done
,
575 .tx_kickstart
= tx_kickstart
,
576 .get_int_events
= get_int_events
,
577 .clear_int_events
= clear_int_events
,
578 .ev_error
= ev_error
,
579 .get_regs
= get_regs
,
580 .get_regs_len
= get_regs_len
,
581 .tx_restart
= tx_restart
,
582 .allocate_bd
= allocate_bd
,
586 /***********************************************************************/
588 static int mii_read(struct fs_enet_mii_bus
*bus
, int phy_id
, int location
)
590 fec_t
*fecp
= bus
->fec
.fecp
;
593 if ((FR(fecp
, r_cntrl
) & FEC_RCNTRL_MII_MODE
) == 0)
596 /* Add PHY address to register command. */
597 FW(fecp
, mii_data
, (phy_id
<< 23) | mk_mii_read(location
));
599 for (i
= 0; i
< FEC_MII_LOOPS
; i
++)
600 if ((FR(fecp
, ievent
) & FEC_ENET_MII
) != 0)
603 if (i
< FEC_MII_LOOPS
) {
604 FW(fecp
, ievent
, FEC_ENET_MII
);
605 ret
= FR(fecp
, mii_data
) & 0xffff;
611 static void mii_write(struct fs_enet_mii_bus
*bus
, int phy_id
, int location
, int value
)
613 fec_t
*fecp
= bus
->fec
.fecp
;
616 /* this must never happen */
617 if ((FR(fecp
, r_cntrl
) & FEC_RCNTRL_MII_MODE
) == 0)
620 /* Add PHY address to register command. */
621 FW(fecp
, mii_data
, (phy_id
<< 23) | mk_mii_write(location
, value
));
623 for (i
= 0; i
< FEC_MII_LOOPS
; i
++)
624 if ((FR(fecp
, ievent
) & FEC_ENET_MII
) != 0)
627 if (i
< FEC_MII_LOOPS
)
628 FW(fecp
, ievent
, FEC_ENET_MII
);
631 int fs_mii_fec_init(struct fs_enet_mii_bus
*bus
)
633 bd_t
*bd
= (bd_t
*)__res
;
634 const struct fs_mii_bus_info
*bi
= bus
->bus_info
;
640 bus
->fec
.fecp
= &((immap_t
*)fs_enet_immap
)->im_cpm
.cp_fec
;
641 bus
->fec
.mii_speed
= ((((bd
->bi_intfreq
+ 4999999) / 2500000) / 2)
644 fecp
= bus
->fec
.fecp
;
646 FS(fecp
, r_cntrl
, FEC_RCNTRL_MII_MODE
); /* MII enable */
647 FS(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_ETHER_EN
);
648 FW(fecp
, ievent
, FEC_ENET_MII
);
649 FW(fecp
, mii_speed
, bus
->fec
.mii_speed
);
651 bus
->mii_read
= mii_read
;
652 bus
->mii_write
= mii_write
;