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/string.h>
19 #include <linux/ptrace.h>
20 #include <linux/errno.h>
21 #include <linux/crc32.h>
22 #include <linux/ioport.h>
23 #include <linux/interrupt.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
29 #include <linux/mii.h>
30 #include <linux/ethtool.h>
31 #include <linux/bitops.h>
33 #include <linux/platform_device.h>
34 #include <linux/of_address.h>
35 #include <linux/of_device.h>
36 #include <linux/of_irq.h>
37 #include <linux/gfp.h>
40 #include <linux/uaccess.h>
45 /*************************************************/
47 #if defined(CONFIG_CPM1)
48 /* for a CPM1 __raw_xxx's are sufficient */
49 #define __fs_out32(addr, x) __raw_writel(x, addr)
50 #define __fs_out16(addr, x) __raw_writew(x, addr)
51 #define __fs_in32(addr) __raw_readl(addr)
52 #define __fs_in16(addr) __raw_readw(addr)
54 /* for others play it safe */
55 #define __fs_out32(addr, x) out_be32(addr, x)
56 #define __fs_out16(addr, x) out_be16(addr, x)
57 #define __fs_in32(addr) in_be32(addr)
58 #define __fs_in16(addr) in_be16(addr)
62 #define FW(_fecp, _reg, _v) __fs_out32(&(_fecp)->fec_ ## _reg, (_v))
65 #define FR(_fecp, _reg) __fs_in32(&(_fecp)->fec_ ## _reg)
68 #define FS(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) | (_v))
71 #define FC(_fecp, _reg, _v) FW(_fecp, _reg, FR(_fecp, _reg) & ~(_v))
74 * Delay to wait for FEC reset command to complete (in us)
76 #define FEC_RESET_DELAY 50
78 static int whack_reset(struct fec __iomem
*fecp
)
82 FW(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_RESET
);
83 for (i
= 0; i
< FEC_RESET_DELAY
; i
++) {
84 if ((FR(fecp
, ecntrl
) & FEC_ECNTRL_RESET
) == 0)
92 static int do_pd_setup(struct fs_enet_private
*fep
)
94 struct platform_device
*ofdev
= to_platform_device(fep
->dev
);
96 fep
->interrupt
= irq_of_parse_and_map(ofdev
->dev
.of_node
, 0);
100 fep
->fec
.fecp
= of_iomap(ofdev
->dev
.of_node
, 0);
107 #define FEC_NAPI_EVENT_MSK (FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_TXF)
108 #define FEC_EVENT (FEC_ENET_RXF | FEC_ENET_TXF)
109 #define FEC_ERR_EVENT_MSK (FEC_ENET_HBERR | FEC_ENET_BABR | \
110 FEC_ENET_BABT | FEC_ENET_EBERR)
112 static int setup_data(struct net_device
*dev
)
114 struct fs_enet_private
*fep
= netdev_priv(dev
);
116 if (do_pd_setup(fep
) != 0)
122 fep
->ev_napi
= FEC_NAPI_EVENT_MSK
;
124 fep
->ev_err
= FEC_ERR_EVENT_MSK
;
129 static int allocate_bd(struct net_device
*dev
)
131 struct fs_enet_private
*fep
= netdev_priv(dev
);
132 const struct fs_platform_info
*fpi
= fep
->fpi
;
134 fep
->ring_base
= (void __force __iomem
*)dma_alloc_coherent(fep
->dev
,
135 (fpi
->tx_ring
+ fpi
->rx_ring
) *
136 sizeof(cbd_t
), &fep
->ring_mem_addr
,
138 if (fep
->ring_base
== NULL
)
144 static void free_bd(struct net_device
*dev
)
146 struct fs_enet_private
*fep
= netdev_priv(dev
);
147 const struct fs_platform_info
*fpi
= fep
->fpi
;
150 dma_free_coherent(fep
->dev
, (fpi
->tx_ring
+ fpi
->rx_ring
)
152 (void __force
*)fep
->ring_base
,
156 static void cleanup_data(struct net_device
*dev
)
161 static void set_promiscuous_mode(struct net_device
*dev
)
163 struct fs_enet_private
*fep
= netdev_priv(dev
);
164 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
166 FS(fecp
, r_cntrl
, FEC_RCNTRL_PROM
);
169 static void set_multicast_start(struct net_device
*dev
)
171 struct fs_enet_private
*fep
= netdev_priv(dev
);
177 static void set_multicast_one(struct net_device
*dev
, const u8
*mac
)
179 struct fs_enet_private
*fep
= netdev_priv(dev
);
180 int temp
, hash_index
;
183 crc
= ether_crc(6, mac
);
185 temp
= (crc
& 0x3f) >> 1;
186 hash_index
= ((temp
& 0x01) << 4) |
187 ((temp
& 0x02) << 2) |
189 ((temp
& 0x08) >> 2) |
190 ((temp
& 0x10) >> 4);
191 csrVal
= 1 << hash_index
;
193 fep
->fec
.hthi
|= csrVal
;
195 fep
->fec
.htlo
|= csrVal
;
198 static void set_multicast_finish(struct net_device
*dev
)
200 struct fs_enet_private
*fep
= netdev_priv(dev
);
201 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
203 /* if all multi or too many multicasts; just enable all */
204 if ((dev
->flags
& IFF_ALLMULTI
) != 0 ||
205 netdev_mc_count(dev
) > FEC_MAX_MULTICAST_ADDRS
) {
206 fep
->fec
.hthi
= 0xffffffffU
;
207 fep
->fec
.htlo
= 0xffffffffU
;
210 FC(fecp
, r_cntrl
, FEC_RCNTRL_PROM
);
211 FW(fecp
, grp_hash_table_high
, fep
->fec
.hthi
);
212 FW(fecp
, grp_hash_table_low
, fep
->fec
.htlo
);
215 static void set_multicast_list(struct net_device
*dev
)
217 struct netdev_hw_addr
*ha
;
219 if ((dev
->flags
& IFF_PROMISC
) == 0) {
220 set_multicast_start(dev
);
221 netdev_for_each_mc_addr(ha
, dev
)
222 set_multicast_one(dev
, ha
->addr
);
223 set_multicast_finish(dev
);
225 set_promiscuous_mode(dev
);
228 static void restart(struct net_device
*dev
)
230 struct fs_enet_private
*fep
= netdev_priv(dev
);
231 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
232 const struct fs_platform_info
*fpi
= fep
->fpi
;
233 dma_addr_t rx_bd_base_phys
, tx_bd_base_phys
;
237 struct mii_bus
*mii
= dev
->phydev
->mdio
.bus
;
238 struct fec_info
* fec_inf
= mii
->priv
;
240 r
= whack_reset(fep
->fec
.fecp
);
242 dev_err(fep
->dev
, "FEC Reset FAILED!\n");
244 * Set station address.
246 addrhi
= ((u32
) dev
->dev_addr
[0] << 24) |
247 ((u32
) dev
->dev_addr
[1] << 16) |
248 ((u32
) dev
->dev_addr
[2] << 8) |
249 (u32
) dev
->dev_addr
[3];
250 addrlo
= ((u32
) dev
->dev_addr
[4] << 24) |
251 ((u32
) dev
->dev_addr
[5] << 16);
252 FW(fecp
, addr_low
, addrhi
);
253 FW(fecp
, addr_high
, addrlo
);
256 * Reset all multicast.
258 FW(fecp
, grp_hash_table_high
, fep
->fec
.hthi
);
259 FW(fecp
, grp_hash_table_low
, fep
->fec
.htlo
);
262 * Set maximum receive buffer size.
264 FW(fecp
, r_buff_size
, PKT_MAXBLR_SIZE
);
265 #ifdef CONFIG_FS_ENET_MPC5121_FEC
266 FW(fecp
, r_cntrl
, PKT_MAXBUF_SIZE
<< 16);
268 FW(fecp
, r_hash
, PKT_MAXBUF_SIZE
);
271 /* get physical address */
272 rx_bd_base_phys
= fep
->ring_mem_addr
;
273 tx_bd_base_phys
= rx_bd_base_phys
+ sizeof(cbd_t
) * fpi
->rx_ring
;
276 * Set receive and transmit descriptor base.
278 FW(fecp
, r_des_start
, rx_bd_base_phys
);
279 FW(fecp
, x_des_start
, tx_bd_base_phys
);
284 * Enable big endian and don't care about SDMA FC.
286 #ifdef CONFIG_FS_ENET_MPC5121_FEC
287 FS(fecp
, dma_control
, 0xC0000000);
289 FW(fecp
, fun_code
, 0x78000000);
295 FW(fecp
, mii_speed
, fec_inf
->mii_speed
);
298 * Clear any outstanding interrupt.
300 FW(fecp
, ievent
, 0xffc0);
301 #ifndef CONFIG_FS_ENET_MPC5121_FEC
302 FW(fecp
, ivec
, (virq_to_hw(fep
->interrupt
) / 2) << 29);
304 FW(fecp
, r_cntrl
, FEC_RCNTRL_MII_MODE
); /* MII enable */
307 * Only set MII/RMII mode - do not touch maximum frame length
310 FS(fecp
, r_cntrl
, fpi
->use_rmii
?
311 FEC_RCNTRL_RMII_MODE
: FEC_RCNTRL_MII_MODE
);
314 * adjust to duplex mode
316 if (dev
->phydev
->duplex
) {
317 FC(fecp
, r_cntrl
, FEC_RCNTRL_DRT
);
318 FS(fecp
, x_cntrl
, FEC_TCNTRL_FDEN
); /* FD enable */
320 FS(fecp
, r_cntrl
, FEC_RCNTRL_DRT
);
321 FC(fecp
, x_cntrl
, FEC_TCNTRL_FDEN
); /* FD disable */
324 /* Restore multicast and promiscuous settings */
325 set_multicast_list(dev
);
328 * Enable interrupts we wish to service.
330 FW(fecp
, imask
, FEC_ENET_TXF
| FEC_ENET_TXB
|
331 FEC_ENET_RXF
| FEC_ENET_RXB
);
334 * And last, enable the transmit and receive processing.
336 FW(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_ETHER_EN
);
337 FW(fecp
, r_des_active
, 0x01000000);
340 static void stop(struct net_device
*dev
)
342 struct fs_enet_private
*fep
= netdev_priv(dev
);
343 const struct fs_platform_info
*fpi
= fep
->fpi
;
344 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
346 struct fec_info
*feci
= dev
->phydev
->mdio
.bus
->priv
;
350 if ((FR(fecp
, ecntrl
) & FEC_ECNTRL_ETHER_EN
) == 0)
351 return; /* already down */
353 FW(fecp
, x_cntrl
, 0x01); /* Graceful transmit stop */
354 for (i
= 0; ((FR(fecp
, ievent
) & 0x10000000) == 0) &&
355 i
< FEC_RESET_DELAY
; i
++)
358 if (i
== FEC_RESET_DELAY
)
359 dev_warn(fep
->dev
, "FEC timeout on graceful transmit stop\n");
361 * Disable FEC. Let only MII interrupts.
364 FC(fecp
, ecntrl
, FEC_ECNTRL_ETHER_EN
);
368 /* shut down FEC1? that's where the mii bus is */
370 FS(fecp
, r_cntrl
, fpi
->use_rmii
?
371 FEC_RCNTRL_RMII_MODE
:
372 FEC_RCNTRL_MII_MODE
); /* MII/RMII enable */
373 FS(fecp
, ecntrl
, FEC_ECNTRL_PINMUX
| FEC_ECNTRL_ETHER_EN
);
374 FW(fecp
, ievent
, FEC_ENET_MII
);
375 FW(fecp
, mii_speed
, feci
->mii_speed
);
379 static void napi_clear_event_fs(struct net_device
*dev
)
381 struct fs_enet_private
*fep
= netdev_priv(dev
);
382 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
384 FW(fecp
, ievent
, FEC_NAPI_EVENT_MSK
);
387 static void napi_enable_fs(struct net_device
*dev
)
389 struct fs_enet_private
*fep
= netdev_priv(dev
);
390 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
392 FS(fecp
, imask
, FEC_NAPI_EVENT_MSK
);
395 static void napi_disable_fs(struct net_device
*dev
)
397 struct fs_enet_private
*fep
= netdev_priv(dev
);
398 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
400 FC(fecp
, imask
, FEC_NAPI_EVENT_MSK
);
403 static void rx_bd_done(struct net_device
*dev
)
405 struct fs_enet_private
*fep
= netdev_priv(dev
);
406 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
408 FW(fecp
, r_des_active
, 0x01000000);
411 static void tx_kickstart(struct net_device
*dev
)
413 struct fs_enet_private
*fep
= netdev_priv(dev
);
414 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
416 FW(fecp
, x_des_active
, 0x01000000);
419 static u32
get_int_events(struct net_device
*dev
)
421 struct fs_enet_private
*fep
= netdev_priv(dev
);
422 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
424 return FR(fecp
, ievent
) & FR(fecp
, imask
);
427 static void clear_int_events(struct net_device
*dev
, u32 int_events
)
429 struct fs_enet_private
*fep
= netdev_priv(dev
);
430 struct fec __iomem
*fecp
= fep
->fec
.fecp
;
432 FW(fecp
, ievent
, int_events
);
435 static void ev_error(struct net_device
*dev
, u32 int_events
)
437 struct fs_enet_private
*fep
= netdev_priv(dev
);
439 dev_warn(fep
->dev
, "FEC ERROR(s) 0x%x\n", int_events
);
442 static int get_regs(struct net_device
*dev
, void *p
, int *sizep
)
444 struct fs_enet_private
*fep
= netdev_priv(dev
);
446 if (*sizep
< sizeof(struct fec
))
449 memcpy_fromio(p
, fep
->fec
.fecp
, sizeof(struct fec
));
454 static int get_regs_len(struct net_device
*dev
)
456 return sizeof(struct fec
);
459 static void tx_restart(struct net_device
*dev
)
464 /*************************************************************************/
466 const struct fs_ops fs_fec_ops
= {
467 .setup_data
= setup_data
,
468 .cleanup_data
= cleanup_data
,
469 .set_multicast_list
= set_multicast_list
,
472 .napi_clear_event
= napi_clear_event_fs
,
473 .napi_enable
= napi_enable_fs
,
474 .napi_disable
= napi_disable_fs
,
475 .rx_bd_done
= rx_bd_done
,
476 .tx_kickstart
= tx_kickstart
,
477 .get_int_events
= get_int_events
,
478 .clear_int_events
= clear_int_events
,
479 .ev_error
= ev_error
,
480 .get_regs
= get_regs
,
481 .get_regs_len
= get_regs_len
,
482 .tx_restart
= tx_restart
,
483 .allocate_bd
= allocate_bd
,