2 * Combined Ethernet 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 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
11 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/string.h>
25 #include <linux/ptrace.h>
26 #include <linux/errno.h>
27 #include <linux/ioport.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/pci.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/spinlock.h>
37 #include <linux/mii.h>
38 #include <linux/ethtool.h>
39 #include <linux/bitops.h>
41 #include <asm/pgtable.h>
43 #include <asm/uaccess.h>
47 /*************************************************/
50 * Generic PHY support.
51 * Should work for all PHYs, but link change is detected by polling
54 static void generic_timer_callback(unsigned long data
)
56 struct net_device
*dev
= (struct net_device
*)data
;
57 struct fs_enet_private
*fep
= netdev_priv(dev
);
59 fep
->phy_timer_list
.expires
= jiffies
+ HZ
/ 2;
61 add_timer(&fep
->phy_timer_list
);
63 fs_mii_link_status_change_check(dev
, 0);
66 static void generic_startup(struct net_device
*dev
)
68 struct fs_enet_private
*fep
= netdev_priv(dev
);
70 fep
->phy_timer_list
.expires
= jiffies
+ HZ
/ 2; /* every 500ms */
71 fep
->phy_timer_list
.data
= (unsigned long)dev
;
72 fep
->phy_timer_list
.function
= generic_timer_callback
;
73 add_timer(&fep
->phy_timer_list
);
76 static void generic_shutdown(struct net_device
*dev
)
78 struct fs_enet_private
*fep
= netdev_priv(dev
);
80 del_timer_sync(&fep
->phy_timer_list
);
83 /* ------------------------------------------------------------------------- */
84 /* The Davicom DM9161 is used on the NETTA board */
86 /* register definitions */
88 #define MII_DM9161_ANAR 4 /* Aux. Config Register */
89 #define MII_DM9161_ACR 16 /* Aux. Config Register */
90 #define MII_DM9161_ACSR 17 /* Aux. Config/Status Register */
91 #define MII_DM9161_10TCSR 18 /* 10BaseT Config/Status Reg. */
92 #define MII_DM9161_INTR 21 /* Interrupt Register */
93 #define MII_DM9161_RECR 22 /* Receive Error Counter Reg. */
94 #define MII_DM9161_DISCR 23 /* Disconnect Counter Register */
96 static void dm9161_startup(struct net_device
*dev
)
98 struct fs_enet_private
*fep
= netdev_priv(dev
);
100 fs_mii_write(dev
, fep
->mii_if
.phy_id
, MII_DM9161_INTR
, 0x0000);
101 /* Start autonegotiation */
102 fs_mii_write(dev
, fep
->mii_if
.phy_id
, MII_BMCR
, 0x1200);
104 set_current_state(TASK_UNINTERRUPTIBLE
);
105 schedule_timeout(HZ
*8);
108 static void dm9161_ack_int(struct net_device
*dev
)
110 struct fs_enet_private
*fep
= netdev_priv(dev
);
112 fs_mii_read(dev
, fep
->mii_if
.phy_id
, MII_DM9161_INTR
);
115 static void dm9161_shutdown(struct net_device
*dev
)
117 struct fs_enet_private
*fep
= netdev_priv(dev
);
119 fs_mii_write(dev
, fep
->mii_if
.phy_id
, MII_DM9161_INTR
, 0x0f00);
122 /**********************************************************************************/
124 static const struct phy_info phy_info
[] = {
128 .startup
= dm9161_startup
,
129 .ack_int
= dm9161_ack_int
,
130 .shutdown
= dm9161_shutdown
,
134 .startup
= generic_startup
,
135 .shutdown
= generic_shutdown
,
139 /**********************************************************************************/
141 static int phy_id_detect(struct net_device
*dev
)
143 struct fs_enet_private
*fep
= netdev_priv(dev
);
144 const struct fs_platform_info
*fpi
= fep
->fpi
;
145 struct fs_enet_mii_bus
*bus
= fep
->mii_bus
;
146 int i
, r
, start
, end
, phytype
, physubtype
;
147 const struct phy_info
*phy
;
148 int phy_hwid
, phy_id
;
154 if (fpi
->phy_addr
== -1) {
157 } else { /* direct */
158 start
= fpi
->phy_addr
;
162 for (phy_id
= start
; phy_id
< end
; phy_id
++) {
163 /* skip already used phy addresses on this bus */
164 if (bus
->usage_map
& (1 << phy_id
))
166 r
= fs_mii_read(dev
, phy_id
, MII_PHYSID1
);
167 if (r
== -1 || (phytype
= (r
& 0xffff)) == 0xffff)
169 r
= fs_mii_read(dev
, phy_id
, MII_PHYSID2
);
170 if (r
== -1 || (physubtype
= (r
& 0xffff)) == 0xffff)
172 phy_hwid
= (phytype
<< 16) | physubtype
;
177 if (phy_hwid
== -1) {
178 printk(KERN_ERR DRV_MODULE_NAME
179 ": %s No PHY detected! range=0x%02x-0x%02x\n",
180 dev
->name
, start
, end
);
184 for (i
= 0, phy
= phy_info
; i
< ARRAY_SIZE(phy_info
); i
++, phy
++)
185 if (phy
->id
== (phy_hwid
>> 4) || phy
->id
== 0)
188 if (i
>= ARRAY_SIZE(phy_info
)) {
189 printk(KERN_ERR DRV_MODULE_NAME
190 ": %s PHY id 0x%08x is not supported!\n",
191 dev
->name
, phy_hwid
);
197 /* mark this address as used */
198 bus
->usage_map
|= (1 << phy_id
);
200 printk(KERN_INFO DRV_MODULE_NAME
201 ": %s Phy @ 0x%x, type %s (0x%08x)%s\n",
202 dev
->name
, phy_id
, fep
->phy
->name
, phy_hwid
,
203 fpi
->phy_addr
== -1 ? " (auto-detected)" : "");
208 void fs_mii_startup(struct net_device
*dev
)
210 struct fs_enet_private
*fep
= netdev_priv(dev
);
212 if (fep
->phy
->startup
)
213 (*fep
->phy
->startup
) (dev
);
216 void fs_mii_shutdown(struct net_device
*dev
)
218 struct fs_enet_private
*fep
= netdev_priv(dev
);
220 if (fep
->phy
->shutdown
)
221 (*fep
->phy
->shutdown
) (dev
);
224 void fs_mii_ack_int(struct net_device
*dev
)
226 struct fs_enet_private
*fep
= netdev_priv(dev
);
228 if (fep
->phy
->ack_int
)
229 (*fep
->phy
->ack_int
) (dev
);
232 #define MII_LINK 0x0001
233 #define MII_HALF 0x0002
234 #define MII_FULL 0x0004
235 #define MII_BASE4 0x0008
236 #define MII_10M 0x0010
237 #define MII_100M 0x0020
238 #define MII_1G 0x0040
239 #define MII_10G 0x0080
241 /* return full mii info at one gulp, with a usable form */
242 static unsigned int mii_full_status(struct mii_if_info
*mii
)
245 int bmsr
, adv
, lpa
, neg
;
246 struct fs_enet_private
* fep
= netdev_priv(mii
->dev
);
248 /* first, a dummy read, needed to latch some MII phys */
249 (void)mii
->mdio_read(mii
->dev
, mii
->phy_id
, MII_BMSR
);
250 bmsr
= mii
->mdio_read(mii
->dev
, mii
->phy_id
, MII_BMSR
);
253 if ((bmsr
& BMSR_LSTATUS
) == 0)
258 /* Lets look what ANEG says if it's supported - otherwize we shall
259 take the right values from the platform info*/
260 if(!mii
->force_media
) {
261 /* autoneg not completed; don't bother */
262 if ((bmsr
& BMSR_ANEGCOMPLETE
) == 0)
265 adv
= (*mii
->mdio_read
)(mii
->dev
, mii
->phy_id
, MII_ADVERTISE
);
266 lpa
= (*mii
->mdio_read
)(mii
->dev
, mii
->phy_id
, MII_LPA
);
270 neg
= fep
->fpi
->bus_info
->lpa
;
273 if (neg
& LPA_100FULL
)
274 status
|= MII_FULL
| MII_100M
;
275 else if (neg
& LPA_100BASE4
)
276 status
|= MII_FULL
| MII_BASE4
| MII_100M
;
277 else if (neg
& LPA_100HALF
)
278 status
|= MII_HALF
| MII_100M
;
279 else if (neg
& LPA_10FULL
)
280 status
|= MII_FULL
| MII_10M
;
282 status
|= MII_HALF
| MII_10M
;
287 void fs_mii_link_status_change_check(struct net_device
*dev
, int init_media
)
289 struct fs_enet_private
*fep
= netdev_priv(dev
);
290 struct mii_if_info
*mii
= &fep
->mii_if
;
291 unsigned int mii_status
;
292 int ok_to_print
, link
, duplex
, speed
;
295 ok_to_print
= netif_msg_link(fep
);
297 mii_status
= mii_full_status(mii
);
299 if (!init_media
&& mii_status
== fep
->last_mii_status
)
302 fep
->last_mii_status
= mii_status
;
304 link
= !!(mii_status
& MII_LINK
);
305 duplex
= !!(mii_status
& MII_FULL
);
306 speed
= (mii_status
& MII_100M
) ? 100 : 10;
309 netif_carrier_off(mii
->dev
);
310 netif_stop_queue(dev
);
312 spin_lock_irqsave(&fep
->lock
, flags
);
313 (*fep
->ops
->stop
)(dev
);
314 spin_unlock_irqrestore(&fep
->lock
, flags
);
318 printk(KERN_INFO
"%s: link down\n", mii
->dev
->name
);
322 mii
->full_duplex
= duplex
;
324 netif_carrier_on(mii
->dev
);
326 spin_lock_irqsave(&fep
->lock
, flags
);
327 fep
->duplex
= duplex
;
329 (*fep
->ops
->restart
)(dev
);
330 spin_unlock_irqrestore(&fep
->lock
, flags
);
332 netif_start_queue(dev
);
335 printk(KERN_INFO
"%s: link up, %dMbps, %s-duplex\n",
336 dev
->name
, speed
, duplex
? "full" : "half");
340 /**********************************************************************************/
342 int fs_mii_read(struct net_device
*dev
, int phy_id
, int location
)
344 struct fs_enet_private
*fep
= netdev_priv(dev
);
345 struct fs_enet_mii_bus
*bus
= fep
->mii_bus
;
350 spin_lock_irqsave(&bus
->mii_lock
, flags
);
351 ret
= (*bus
->mii_read
)(bus
, phy_id
, location
);
352 spin_unlock_irqrestore(&bus
->mii_lock
, flags
);
357 void fs_mii_write(struct net_device
*dev
, int phy_id
, int location
, int value
)
359 struct fs_enet_private
*fep
= netdev_priv(dev
);
360 struct fs_enet_mii_bus
*bus
= fep
->mii_bus
;
363 spin_lock_irqsave(&bus
->mii_lock
, flags
);
364 (*bus
->mii_write
)(bus
, phy_id
, location
, value
);
365 spin_unlock_irqrestore(&bus
->mii_lock
, flags
);
368 /*****************************************************************************/
370 /* list of all registered mii buses */
371 static LIST_HEAD(fs_mii_bus_list
);
373 static struct fs_enet_mii_bus
*lookup_bus(int method
, int id
)
375 struct list_head
*ptr
;
376 struct fs_enet_mii_bus
*bus
;
378 list_for_each(ptr
, &fs_mii_bus_list
) {
379 bus
= list_entry(ptr
, struct fs_enet_mii_bus
, list
);
380 if (bus
->bus_info
->method
== method
&&
381 bus
->bus_info
->id
== id
)
387 static struct fs_enet_mii_bus
*create_bus(const struct fs_mii_bus_info
*bi
)
389 struct fs_enet_mii_bus
*bus
;
392 bus
= kmalloc(sizeof(*bus
), GFP_KERNEL
);
397 memset(bus
, 0, sizeof(*bus
));
398 spin_lock_init(&bus
->mii_lock
);
403 /* perform initialization */
404 switch (bi
->method
) {
407 ret
= fs_mii_fixed_init(bus
);
413 ret
= fs_mii_bitbang_init(bus
);
417 #ifdef CONFIG_FS_ENET_HAS_FEC
419 ret
= fs_mii_fec_init(bus
);
429 list_add(&bus
->list
, &fs_mii_bus_list
);
439 static void destroy_bus(struct fs_enet_mii_bus
*bus
)
441 /* remove from bus list */
442 list_del(&bus
->list
);
444 /* nothing more needed */
448 int fs_mii_connect(struct net_device
*dev
)
450 struct fs_enet_private
*fep
= netdev_priv(dev
);
451 const struct fs_platform_info
*fpi
= fep
->fpi
;
452 struct fs_enet_mii_bus
*bus
= NULL
;
454 /* check method validity */
455 switch (fpi
->bus_info
->method
) {
459 #ifdef CONFIG_FS_ENET_HAS_FEC
464 printk(KERN_ERR DRV_MODULE_NAME
465 ": %s Unknown MII bus method (%d)!\n",
466 dev
->name
, fpi
->bus_info
->method
);
470 bus
= lookup_bus(fpi
->bus_info
->method
, fpi
->bus_info
->id
);
472 /* if not found create new bus */
474 bus
= create_bus(fpi
->bus_info
);
476 printk(KERN_ERR DRV_MODULE_NAME
477 ": %s MII bus creation failure!\n", dev
->name
);
486 fep
->mii_if
.dev
= dev
;
487 fep
->mii_if
.phy_id_mask
= 0x1f;
488 fep
->mii_if
.reg_num_mask
= 0x1f;
489 fep
->mii_if
.mdio_read
= fs_mii_read
;
490 fep
->mii_if
.mdio_write
= fs_mii_write
;
491 fep
->mii_if
.force_media
= fpi
->bus_info
->disable_aneg
;
492 fep
->mii_if
.phy_id
= phy_id_detect(dev
);
497 void fs_mii_disconnect(struct net_device
*dev
)
499 struct fs_enet_private
*fep
= netdev_priv(dev
);
500 struct fs_enet_mii_bus
*bus
= NULL
;
505 if (--bus
->refs
<= 0)