2 * drivers/net/gianfar_phy.c
4 * Gianfar Ethernet Driver -- PHY handling
5 * Driver for FEC on MPC8540 and TSEC on MPC8540/MPC8560
6 * Based on 8260_io/fcc_enet.c
9 * Maintainer: Kumar Gala (kumar.gala@freescale.com)
11 * Copyright (c) 2002-2004 Freescale Semiconductor, Inc.
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
20 #include <linux/config.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/spinlock.h>
37 #include <asm/uaccess.h>
38 #include <linux/module.h>
39 #include <linux/version.h>
40 #include <linux/crc32.h>
41 #include <linux/mii.h>
44 #include "gianfar_phy.h"
46 static void config_genmii_advert(struct gfar_mii_info *mii_info);
47 static void genmii_setup_forced(struct gfar_mii_info *mii_info);
48 static void genmii_restart_aneg(struct gfar_mii_info *mii_info);
49 static int gbit_config_aneg(struct gfar_mii_info *mii_info);
50 static int genmii_config_aneg(struct gfar_mii_info *mii_info);
51 static int genmii_update_link(struct gfar_mii_info *mii_info);
52 static int genmii_read_status(struct gfar_mii_info *mii_info);
53 u16 phy_read(struct gfar_mii_info *mii_info, u16 regnum);
54 void phy_write(struct gfar_mii_info *mii_info, u16 regnum, u16 val);
56 /* Write value to the PHY for this device to the register at regnum, */
57 /* waiting until the write is done before it returns. All PHY */
58 /* configuration has to be done through the TSEC1 MIIM regs */
59 void write_phy_reg(struct net_device *dev, int mii_id, int regnum, int value)
61 struct gfar_private *priv = netdev_priv(dev);
62 struct gfar *regbase = priv->phyregs;
64 /* Set the PHY address and the register address we want to write */
65 gfar_write(®base->miimadd, (mii_id << 8) | regnum);
67 /* Write out the value we want */
68 gfar_write(®base->miimcon, value);
70 /* Wait for the transaction to finish */
71 while (gfar_read(®base->miimind) & MIIMIND_BUSY)
75 /* Reads from register regnum in the PHY for device dev, */
76 /* returning the value. Clears miimcom first. All PHY */
77 /* configuration has to be done through the TSEC1 MIIM regs */
78 int read_phy_reg(struct net_device *dev, int mii_id, int regnum)
80 struct gfar_private *priv = netdev_priv(dev);
81 struct gfar *regbase = priv->phyregs;
84 /* Set the PHY address and the register address we want to read */
85 gfar_write(®base->miimadd, (mii_id << 8) | regnum);
87 /* Clear miimcom, and then initiate a read */
88 gfar_write(®base->miimcom, 0);
89 gfar_write(®base->miimcom, MII_READ_COMMAND);
91 /* Wait for the transaction to finish */
92 while (gfar_read(®base->miimind) & (MIIMIND_NOTVALID | MIIMIND_BUSY))
95 /* Grab the value of the register from miimstat */
96 value = gfar_read(®base->miimstat);
101 void mii_clear_phy_interrupt(struct gfar_mii_info *mii_info)
103 if(mii_info->phyinfo->ack_interrupt)
104 mii_info->phyinfo->ack_interrupt(mii_info);
108 void mii_configure_phy_interrupt(struct gfar_mii_info *mii_info, u32 interrupts)
110 mii_info->interrupts = interrupts;
111 if(mii_info->phyinfo->config_intr)
112 mii_info->phyinfo->config_intr(mii_info);
116 /* Writes MII_ADVERTISE with the appropriate values, after
117 * sanitizing advertise to make sure only supported features
120 static void config_genmii_advert(struct gfar_mii_info *mii_info)
125 /* Only allow advertising what this PHY supports */
126 mii_info->advertising &= mii_info->phyinfo->features;
127 advertise = mii_info->advertising;
129 /* Setup standard advertisement */
130 adv = phy_read(mii_info, MII_ADVERTISE);
131 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
132 if (advertise & ADVERTISED_10baseT_Half)
133 adv |= ADVERTISE_10HALF;
134 if (advertise & ADVERTISED_10baseT_Full)
135 adv |= ADVERTISE_10FULL;
136 if (advertise & ADVERTISED_100baseT_Half)
137 adv |= ADVERTISE_100HALF;
138 if (advertise & ADVERTISED_100baseT_Full)
139 adv |= ADVERTISE_100FULL;
140 phy_write(mii_info, MII_ADVERTISE, adv);
143 static void genmii_setup_forced(struct gfar_mii_info *mii_info)
146 u32 features = mii_info->phyinfo->features;
148 ctrl = phy_read(mii_info, MII_BMCR);
150 ctrl &= ~(BMCR_FULLDPLX|BMCR_SPEED100|BMCR_SPEED1000|BMCR_ANENABLE);
153 switch(mii_info->speed) {
155 if(features & (SUPPORTED_1000baseT_Half
156 | SUPPORTED_1000baseT_Full)) {
157 ctrl |= BMCR_SPEED1000;
160 mii_info->speed = SPEED_100;
162 if (features & (SUPPORTED_100baseT_Half
163 | SUPPORTED_100baseT_Full)) {
164 ctrl |= BMCR_SPEED100;
167 mii_info->speed = SPEED_10;
169 if (features & (SUPPORTED_10baseT_Half
170 | SUPPORTED_10baseT_Full))
172 default: /* Unsupported speed! */
173 printk(KERN_ERR "%s: Bad speed!\n",
174 mii_info->dev->name);
178 phy_write(mii_info, MII_BMCR, ctrl);
182 /* Enable and Restart Autonegotiation */
183 static void genmii_restart_aneg(struct gfar_mii_info *mii_info)
187 ctl = phy_read(mii_info, MII_BMCR);
188 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
189 phy_write(mii_info, MII_BMCR, ctl);
193 static int gbit_config_aneg(struct gfar_mii_info *mii_info)
198 if(mii_info->autoneg) {
199 /* Configure the ADVERTISE register */
200 config_genmii_advert(mii_info);
201 advertise = mii_info->advertising;
203 adv = phy_read(mii_info, MII_1000BASETCONTROL);
204 adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP |
205 MII_1000BASETCONTROL_HALFDUPLEXCAP);
206 if (advertise & SUPPORTED_1000baseT_Half)
207 adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
208 if (advertise & SUPPORTED_1000baseT_Full)
209 adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
210 phy_write(mii_info, MII_1000BASETCONTROL, adv);
212 /* Start/Restart aneg */
213 genmii_restart_aneg(mii_info);
215 genmii_setup_forced(mii_info);
220 static int marvell_config_aneg(struct gfar_mii_info *mii_info)
222 /* The Marvell PHY has an errata which requires
223 * that certain registers get written in order
224 * to restart autonegotiation */
225 phy_write(mii_info, MII_BMCR, BMCR_RESET);
227 phy_write(mii_info, 0x1d, 0x1f);
228 phy_write(mii_info, 0x1e, 0x200c);
229 phy_write(mii_info, 0x1d, 0x5);
230 phy_write(mii_info, 0x1e, 0);
231 phy_write(mii_info, 0x1e, 0x100);
233 gbit_config_aneg(mii_info);
237 static int genmii_config_aneg(struct gfar_mii_info *mii_info)
239 if (mii_info->autoneg) {
240 config_genmii_advert(mii_info);
241 genmii_restart_aneg(mii_info);
243 genmii_setup_forced(mii_info);
249 static int genmii_update_link(struct gfar_mii_info *mii_info)
254 phy_read(mii_info, MII_BMSR);
256 /* Read link and autonegotiation status */
257 status = phy_read(mii_info, MII_BMSR);
258 if ((status & BMSR_LSTATUS) == 0)
263 /* If we are autonegotiating, and not done,
265 if (mii_info->autoneg && !(status & BMSR_ANEGCOMPLETE))
271 static int genmii_read_status(struct gfar_mii_info *mii_info)
276 /* Update the link, but return if there
278 err = genmii_update_link(mii_info);
282 if (mii_info->autoneg) {
283 status = phy_read(mii_info, MII_LPA);
285 if (status & (LPA_10FULL | LPA_100FULL))
286 mii_info->duplex = DUPLEX_FULL;
288 mii_info->duplex = DUPLEX_HALF;
289 if (status & (LPA_100FULL | LPA_100HALF))
290 mii_info->speed = SPEED_100;
292 mii_info->speed = SPEED_10;
295 /* On non-aneg, we assume what we put in BMCR is the speed,
296 * though magic-aneg shouldn't prevent this case from occurring
301 static int marvell_read_status(struct gfar_mii_info *mii_info)
306 /* Update the link, but return if there
308 err = genmii_update_link(mii_info);
312 /* If the link is up, read the speed and duplex */
313 /* If we aren't autonegotiating, assume speeds
315 if (mii_info->autoneg && mii_info->link) {
317 status = phy_read(mii_info, MII_M1011_PHY_SPEC_STATUS);
320 /* If speed and duplex aren't resolved,
321 * return an error. Isn't this handled
324 if ((status & MII_M1011_PHY_SPEC_STATUS_RESOLVED) == 0)
328 /* Get the duplexity */
329 if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
330 mii_info->duplex = DUPLEX_FULL;
332 mii_info->duplex = DUPLEX_HALF;
335 speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
337 case MII_M1011_PHY_SPEC_STATUS_1000:
338 mii_info->speed = SPEED_1000;
340 case MII_M1011_PHY_SPEC_STATUS_100:
341 mii_info->speed = SPEED_100;
344 mii_info->speed = SPEED_10;
354 static int cis820x_read_status(struct gfar_mii_info *mii_info)
359 /* Update the link, but return if there
361 err = genmii_update_link(mii_info);
365 /* If the link is up, read the speed and duplex */
366 /* If we aren't autonegotiating, assume speeds
368 if (mii_info->autoneg && mii_info->link) {
371 status = phy_read(mii_info, MII_CIS8201_AUX_CONSTAT);
372 if (status & MII_CIS8201_AUXCONSTAT_DUPLEX)
373 mii_info->duplex = DUPLEX_FULL;
375 mii_info->duplex = DUPLEX_HALF;
377 speed = status & MII_CIS8201_AUXCONSTAT_SPEED;
380 case MII_CIS8201_AUXCONSTAT_GBIT:
381 mii_info->speed = SPEED_1000;
383 case MII_CIS8201_AUXCONSTAT_100:
384 mii_info->speed = SPEED_100;
387 mii_info->speed = SPEED_10;
395 static int marvell_ack_interrupt(struct gfar_mii_info *mii_info)
397 /* Clear the interrupts by reading the reg */
398 phy_read(mii_info, MII_M1011_IEVENT);
403 static int marvell_config_intr(struct gfar_mii_info *mii_info)
405 if(mii_info->interrupts == MII_INTERRUPT_ENABLED)
406 phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
408 phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
413 static int cis820x_init(struct gfar_mii_info *mii_info)
415 phy_write(mii_info, MII_CIS8201_AUX_CONSTAT,
416 MII_CIS8201_AUXCONSTAT_INIT);
417 phy_write(mii_info, MII_CIS8201_EXT_CON1,
418 MII_CIS8201_EXTCON1_INIT);
423 static int cis820x_ack_interrupt(struct gfar_mii_info *mii_info)
425 phy_read(mii_info, MII_CIS8201_ISTAT);
430 static int cis820x_config_intr(struct gfar_mii_info *mii_info)
432 if(mii_info->interrupts == MII_INTERRUPT_ENABLED)
433 phy_write(mii_info, MII_CIS8201_IMASK, MII_CIS8201_IMASK_MASK);
435 phy_write(mii_info, MII_CIS8201_IMASK, 0);
440 #define DM9161_DELAY 10
442 static int dm9161_read_status(struct gfar_mii_info *mii_info)
447 /* Update the link, but return if there
449 err = genmii_update_link(mii_info);
453 /* If the link is up, read the speed and duplex */
454 /* If we aren't autonegotiating, assume speeds
456 if (mii_info->autoneg && mii_info->link) {
457 status = phy_read(mii_info, MII_DM9161_SCSR);
458 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
459 mii_info->speed = SPEED_100;
461 mii_info->speed = SPEED_10;
463 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
464 mii_info->duplex = DUPLEX_FULL;
466 mii_info->duplex = DUPLEX_HALF;
473 static int dm9161_config_aneg(struct gfar_mii_info *mii_info)
475 struct dm9161_private *priv = mii_info->priv;
477 if(0 == priv->resetdone)
483 static void dm9161_timer(unsigned long data)
485 struct gfar_mii_info *mii_info = (struct gfar_mii_info *)data;
486 struct dm9161_private *priv = mii_info->priv;
487 u16 status = phy_read(mii_info, MII_BMSR);
489 if (status & BMSR_ANEGCOMPLETE) {
492 mod_timer(&priv->timer, jiffies + DM9161_DELAY * HZ);
495 static int dm9161_init(struct gfar_mii_info *mii_info)
497 struct dm9161_private *priv;
499 /* Allocate the private data structure */
500 priv = kmalloc(sizeof(struct dm9161_private), GFP_KERNEL);
505 mii_info->priv = priv;
507 /* Reset is not done yet */
510 /* Isolate the PHY */
511 phy_write(mii_info, MII_BMCR, BMCR_ISOLATE);
513 /* Do not bypass the scrambler/descrambler */
514 phy_write(mii_info, MII_DM9161_SCR, MII_DM9161_SCR_INIT);
516 /* Clear 10BTCSR to default */
517 phy_write(mii_info, MII_DM9161_10BTCSR, MII_DM9161_10BTCSR_INIT);
519 /* Reconnect the PHY, and enable Autonegotiation */
520 phy_write(mii_info, MII_BMCR, BMCR_ANENABLE);
522 /* Start a timer for DM9161_DELAY seconds to wait
523 * for the PHY to be ready */
524 init_timer(&priv->timer);
525 priv->timer.function = &dm9161_timer;
526 priv->timer.data = (unsigned long) mii_info;
527 mod_timer(&priv->timer, jiffies + DM9161_DELAY * HZ);
532 static void dm9161_close(struct gfar_mii_info *mii_info)
534 struct dm9161_private *priv = mii_info->priv;
536 del_timer_sync(&priv->timer);
541 static int dm9161_ack_interrupt(struct gfar_mii_info *mii_info)
543 phy_read(mii_info, MII_DM9161_INTR);
550 static struct phy_info phy_info_cis820x = {
554 .features = MII_GBIT_FEATURES,
555 .init = &cis820x_init,
556 .config_aneg = &gbit_config_aneg,
557 .read_status = &cis820x_read_status,
558 .ack_interrupt = &cis820x_ack_interrupt,
559 .config_intr = &cis820x_config_intr,
562 static struct phy_info phy_info_dm9161 = {
563 .phy_id = 0x0181b880,
564 .name = "Davicom DM9161E",
565 .phy_id_mask = 0x0ffffff0,
567 .config_aneg = dm9161_config_aneg,
568 .read_status = dm9161_read_status,
569 .close = dm9161_close,
572 static struct phy_info phy_info_marvell = {
573 .phy_id = 0x01410c00,
574 .phy_id_mask = 0xffffff00,
575 .name = "Marvell 88E1101",
576 .features = MII_GBIT_FEATURES,
577 .config_aneg = &marvell_config_aneg,
578 .read_status = &marvell_read_status,
579 .ack_interrupt = &marvell_ack_interrupt,
580 .config_intr = &marvell_config_intr,
583 static struct phy_info phy_info_genmii= {
584 .phy_id = 0x00000000,
585 .phy_id_mask = 0x00000000,
586 .name = "Generic MII",
587 .features = MII_BASIC_FEATURES,
588 .config_aneg = genmii_config_aneg,
589 .read_status = genmii_read_status,
592 static struct phy_info *phy_info[] = {
600 u16 phy_read(struct gfar_mii_info *mii_info, u16 regnum)
605 spin_lock_irqsave(&mii_info->mdio_lock, flags);
606 retval = mii_info->mdio_read(mii_info->dev, mii_info->mii_id, regnum);
607 spin_unlock_irqrestore(&mii_info->mdio_lock, flags);
612 void phy_write(struct gfar_mii_info *mii_info, u16 regnum, u16 val)
616 spin_lock_irqsave(&mii_info->mdio_lock, flags);
617 mii_info->mdio_write(mii_info->dev,
620 spin_unlock_irqrestore(&mii_info->mdio_lock, flags);
623 /* Use the PHY ID registers to determine what type of PHY is attached
624 * to device dev. return a struct phy_info structure describing that PHY
626 struct phy_info * get_phy_info(struct gfar_mii_info *mii_info)
631 struct phy_info *theInfo = NULL;
632 struct net_device *dev = mii_info->dev;
634 /* Grab the bits from PHYIR1, and put them in the upper half */
635 phy_reg = phy_read(mii_info, MII_PHYSID1);
636 phy_ID = (phy_reg & 0xffff) << 16;
638 /* Grab the bits from PHYIR2, and put them in the lower half */
639 phy_reg = phy_read(mii_info, MII_PHYSID2);
640 phy_ID |= (phy_reg & 0xffff);
642 /* loop through all the known PHY types, and find one that */
643 /* matches the ID we read from the PHY. */
644 for (i = 0; phy_info[i]; i++)
645 if (phy_info[i]->phy_id ==
646 (phy_ID & phy_info[i]->phy_id_mask)) {
647 theInfo = phy_info[i];
651 /* This shouldn't happen, as we have generic PHY support */
652 if (theInfo == NULL) {
653 printk("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
656 printk("%s: PHY is %s (%x)\n", dev->name, theInfo->name,