2 * Copyright (C) Freescale Semicondutor, Inc. 2006. All rights reserved.
4 * Author: Shlomi Gridish <gridish@freescale.com>
7 * UCC GETH Driver -- PHY handling
10 * Jun 28, 2006 Li Yang <LeoLi@freescale.com>
11 * - Rearrange code and style fixes
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/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.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>
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/crc32.h>
36 #include <linux/mii.h>
37 #include <linux/ethtool.h>
41 #include <asm/uaccess.h>
44 #include "ucc_geth_phy.h"
46 #define ugphy_printk(level, format, arg...) \
47 printk(level format "\n", ## arg)
49 #define ugphy_dbg(format, arg...) \
50 ugphy_printk(KERN_DEBUG, format , ## arg)
51 #define ugphy_err(format, arg...) \
52 ugphy_printk(KERN_ERR, format , ## arg)
53 #define ugphy_info(format, arg...) \
54 ugphy_printk(KERN_INFO, format , ## arg)
55 #define ugphy_warn(format, arg...) \
56 ugphy_printk(KERN_WARNING, format , ## arg)
58 #ifdef UGETH_VERBOSE_DEBUG
59 #define ugphy_vdbg ugphy_dbg
61 #define ugphy_vdbg(fmt, args...) do { } while (0)
62 #endif /* UGETH_VERBOSE_DEBUG */
64 static void config_genmii_advert(struct ugeth_mii_info *mii_info);
65 static void genmii_setup_forced(struct ugeth_mii_info *mii_info);
66 static void genmii_restart_aneg(struct ugeth_mii_info *mii_info);
67 static int gbit_config_aneg(struct ugeth_mii_info *mii_info);
68 static int genmii_config_aneg(struct ugeth_mii_info *mii_info);
69 static int genmii_update_link(struct ugeth_mii_info *mii_info);
70 static int genmii_read_status(struct ugeth_mii_info *mii_info);
72 static u16 ucc_geth_phy_read(struct ugeth_mii_info *mii_info, u16 regnum)
77 ugphy_vdbg("%s: IN", __FUNCTION__);
79 spin_lock_irqsave(&mii_info->mdio_lock, flags);
80 retval = mii_info->mdio_read(mii_info->dev, mii_info->mii_id, regnum);
81 spin_unlock_irqrestore(&mii_info->mdio_lock, flags);
86 static void ucc_geth_phy_write(struct ugeth_mii_info *mii_info, u16 regnum, u16 val)
90 ugphy_vdbg("%s: IN", __FUNCTION__);
92 spin_lock_irqsave(&mii_info->mdio_lock, flags);
93 mii_info->mdio_write(mii_info->dev, mii_info->mii_id, regnum, val);
94 spin_unlock_irqrestore(&mii_info->mdio_lock, flags);
97 /* Write value to the PHY for this device to the register at regnum, */
98 /* waiting until the write is done before it returns. All PHY */
99 /* configuration has to be done through the TSEC1 MIIM regs */
100 void write_phy_reg(struct net_device *dev, int mii_id, int regnum, int value)
102 struct ucc_geth_private *ugeth = netdev_priv(dev);
103 struct ucc_mii_mng *mii_regs;
104 enum enet_tbi_mii_reg mii_reg = (enum enet_tbi_mii_reg) regnum;
107 ugphy_vdbg("%s: IN", __FUNCTION__);
109 spin_lock_irq(&ugeth->lock);
111 mii_regs = ugeth->mii_info->mii_regs;
113 /* Set this UCC to be the master of the MII managment */
114 ucc_set_qe_mux_mii_mng(ugeth->ug_info->uf_info.ucc_num);
116 /* Stop the MII management read cycle */
117 out_be32(&mii_regs->miimcom, 0);
118 /* Setting up the MII Mangement Address Register */
119 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
120 out_be32(&mii_regs->miimadd, tmp_reg);
122 /* Setting up the MII Mangement Control Register with the value */
123 out_be32(&mii_regs->miimcon, (u32) value);
125 /* Wait till MII management write is complete */
126 while ((in_be32(&mii_regs->miimind)) & MIIMIND_BUSY)
129 spin_unlock_irq(&ugeth->lock);
134 /* Reads from register regnum in the PHY for device dev, */
135 /* returning the value. Clears miimcom first. All PHY */
136 /* configuration has to be done through the TSEC1 MIIM regs */
137 int read_phy_reg(struct net_device *dev, int mii_id, int regnum)
139 struct ucc_geth_private *ugeth = netdev_priv(dev);
140 struct ucc_mii_mng *mii_regs;
141 enum enet_tbi_mii_reg mii_reg = (enum enet_tbi_mii_reg) regnum;
145 ugphy_vdbg("%s: IN", __FUNCTION__);
147 spin_lock_irq(&ugeth->lock);
149 mii_regs = ugeth->mii_info->mii_regs;
151 /* Setting up the MII Mangement Address Register */
152 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
153 out_be32(&mii_regs->miimadd, tmp_reg);
155 /* Perform an MII management read cycle */
156 out_be32(&mii_regs->miimcom, MIIMCOM_READ_CYCLE);
158 /* Wait till MII management write is complete */
159 while ((in_be32(&mii_regs->miimind)) & MIIMIND_BUSY)
164 /* Read MII management status */
165 value = (u16) in_be32(&mii_regs->miimstat);
166 out_be32(&mii_regs->miimcom, 0);
168 ugphy_warn("read wrong value : mii_id %d,mii_reg %d, base %08x",
169 mii_id, mii_reg, (u32) & (mii_regs->miimcfg));
171 spin_unlock_irq(&ugeth->lock);
176 void mii_clear_phy_interrupt(struct ugeth_mii_info *mii_info)
178 ugphy_vdbg("%s: IN", __FUNCTION__);
180 if (mii_info->phyinfo->ack_interrupt)
181 mii_info->phyinfo->ack_interrupt(mii_info);
184 void mii_configure_phy_interrupt(struct ugeth_mii_info *mii_info,
187 ugphy_vdbg("%s: IN", __FUNCTION__);
189 mii_info->interrupts = interrupts;
190 if (mii_info->phyinfo->config_intr)
191 mii_info->phyinfo->config_intr(mii_info);
194 /* Writes MII_ADVERTISE with the appropriate values, after
195 * sanitizing advertise to make sure only supported features
198 static void config_genmii_advert(struct ugeth_mii_info *mii_info)
203 ugphy_vdbg("%s: IN", __FUNCTION__);
205 /* Only allow advertising what this PHY supports */
206 mii_info->advertising &= mii_info->phyinfo->features;
207 advertise = mii_info->advertising;
209 /* Setup standard advertisement */
210 adv = ucc_geth_phy_read(mii_info, MII_ADVERTISE);
211 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
212 if (advertise & ADVERTISED_10baseT_Half)
213 adv |= ADVERTISE_10HALF;
214 if (advertise & ADVERTISED_10baseT_Full)
215 adv |= ADVERTISE_10FULL;
216 if (advertise & ADVERTISED_100baseT_Half)
217 adv |= ADVERTISE_100HALF;
218 if (advertise & ADVERTISED_100baseT_Full)
219 adv |= ADVERTISE_100FULL;
220 ucc_geth_phy_write(mii_info, MII_ADVERTISE, adv);
223 static void genmii_setup_forced(struct ugeth_mii_info *mii_info)
226 u32 features = mii_info->phyinfo->features;
228 ugphy_vdbg("%s: IN", __FUNCTION__);
230 ctrl = ucc_geth_phy_read(mii_info, MII_BMCR);
233 ~(BMCR_FULLDPLX | BMCR_SPEED100 | BMCR_SPEED1000 | BMCR_ANENABLE);
236 switch (mii_info->speed) {
238 if (features & (SUPPORTED_1000baseT_Half
239 | SUPPORTED_1000baseT_Full)) {
240 ctrl |= BMCR_SPEED1000;
243 mii_info->speed = SPEED_100;
245 if (features & (SUPPORTED_100baseT_Half
246 | SUPPORTED_100baseT_Full)) {
247 ctrl |= BMCR_SPEED100;
250 mii_info->speed = SPEED_10;
252 if (features & (SUPPORTED_10baseT_Half
253 | SUPPORTED_10baseT_Full))
255 default: /* Unsupported speed! */
256 ugphy_err("%s: Bad speed!", mii_info->dev->name);
260 ucc_geth_phy_write(mii_info, MII_BMCR, ctrl);
263 /* Enable and Restart Autonegotiation */
264 static void genmii_restart_aneg(struct ugeth_mii_info *mii_info)
268 ugphy_vdbg("%s: IN", __FUNCTION__);
270 ctl = ucc_geth_phy_read(mii_info, MII_BMCR);
271 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
272 ucc_geth_phy_write(mii_info, MII_BMCR, ctl);
275 static int gbit_config_aneg(struct ugeth_mii_info *mii_info)
280 ugphy_vdbg("%s: IN", __FUNCTION__);
282 if (mii_info->autoneg) {
283 /* Configure the ADVERTISE register */
284 config_genmii_advert(mii_info);
285 advertise = mii_info->advertising;
287 adv = ucc_geth_phy_read(mii_info, MII_1000BASETCONTROL);
288 adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP |
289 MII_1000BASETCONTROL_HALFDUPLEXCAP);
290 if (advertise & SUPPORTED_1000baseT_Half)
291 adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
292 if (advertise & SUPPORTED_1000baseT_Full)
293 adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
294 ucc_geth_phy_write(mii_info, MII_1000BASETCONTROL, adv);
296 /* Start/Restart aneg */
297 genmii_restart_aneg(mii_info);
299 genmii_setup_forced(mii_info);
304 static int genmii_config_aneg(struct ugeth_mii_info *mii_info)
306 ugphy_vdbg("%s: IN", __FUNCTION__);
308 if (mii_info->autoneg) {
309 config_genmii_advert(mii_info);
310 genmii_restart_aneg(mii_info);
312 genmii_setup_forced(mii_info);
317 static int genmii_update_link(struct ugeth_mii_info *mii_info)
321 ugphy_vdbg("%s: IN", __FUNCTION__);
324 ucc_geth_phy_read(mii_info, MII_BMSR);
326 /* Read link and autonegotiation status */
327 status = ucc_geth_phy_read(mii_info, MII_BMSR);
328 if ((status & BMSR_LSTATUS) == 0)
333 /* If we are autonegotiating, and not done,
335 if (mii_info->autoneg && !(status & BMSR_ANEGCOMPLETE))
341 static int genmii_read_status(struct ugeth_mii_info *mii_info)
346 ugphy_vdbg("%s: IN", __FUNCTION__);
348 /* Update the link, but return if there
350 err = genmii_update_link(mii_info);
354 if (mii_info->autoneg) {
355 status = ucc_geth_phy_read(mii_info, MII_LPA);
357 if (status & (LPA_10FULL | LPA_100FULL))
358 mii_info->duplex = DUPLEX_FULL;
360 mii_info->duplex = DUPLEX_HALF;
361 if (status & (LPA_100FULL | LPA_100HALF))
362 mii_info->speed = SPEED_100;
364 mii_info->speed = SPEED_10;
367 /* On non-aneg, we assume what we put in BMCR is the speed,
368 * though magic-aneg shouldn't prevent this case from occurring
374 static int marvell_init(struct ugeth_mii_info *mii_info)
376 ugphy_vdbg("%s: IN", __FUNCTION__);
378 ucc_geth_phy_write(mii_info, 0x14, 0x0cd2);
379 ucc_geth_phy_write(mii_info, MII_BMCR,
380 ucc_geth_phy_read(mii_info, MII_BMCR) | BMCR_RESET);
386 static int marvell_config_aneg(struct ugeth_mii_info *mii_info)
388 ugphy_vdbg("%s: IN", __FUNCTION__);
390 /* The Marvell PHY has an errata which requires
391 * that certain registers get written in order
392 * to restart autonegotiation */
393 ucc_geth_phy_write(mii_info, MII_BMCR, BMCR_RESET);
395 ucc_geth_phy_write(mii_info, 0x1d, 0x1f);
396 ucc_geth_phy_write(mii_info, 0x1e, 0x200c);
397 ucc_geth_phy_write(mii_info, 0x1d, 0x5);
398 ucc_geth_phy_write(mii_info, 0x1e, 0);
399 ucc_geth_phy_write(mii_info, 0x1e, 0x100);
401 gbit_config_aneg(mii_info);
406 static int marvell_read_status(struct ugeth_mii_info *mii_info)
411 ugphy_vdbg("%s: IN", __FUNCTION__);
413 /* Update the link, but return if there
415 err = genmii_update_link(mii_info);
419 /* If the link is up, read the speed and duplex */
420 /* If we aren't autonegotiating, assume speeds
422 if (mii_info->autoneg && mii_info->link) {
424 status = ucc_geth_phy_read(mii_info, MII_M1011_PHY_SPEC_STATUS);
426 /* Get the duplexity */
427 if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
428 mii_info->duplex = DUPLEX_FULL;
430 mii_info->duplex = DUPLEX_HALF;
433 speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
435 case MII_M1011_PHY_SPEC_STATUS_1000:
436 mii_info->speed = SPEED_1000;
438 case MII_M1011_PHY_SPEC_STATUS_100:
439 mii_info->speed = SPEED_100;
442 mii_info->speed = SPEED_10;
451 static int marvell_ack_interrupt(struct ugeth_mii_info *mii_info)
453 ugphy_vdbg("%s: IN", __FUNCTION__);
455 /* Clear the interrupts by reading the reg */
456 ucc_geth_phy_read(mii_info, MII_M1011_IEVENT);
461 static int marvell_config_intr(struct ugeth_mii_info *mii_info)
463 ugphy_vdbg("%s: IN", __FUNCTION__);
465 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
466 ucc_geth_phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
468 ucc_geth_phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
473 static int cis820x_init(struct ugeth_mii_info *mii_info)
475 ugphy_vdbg("%s: IN", __FUNCTION__);
477 ucc_geth_phy_write(mii_info, MII_CIS8201_AUX_CONSTAT,
478 MII_CIS8201_AUXCONSTAT_INIT);
479 ucc_geth_phy_write(mii_info, MII_CIS8201_EXT_CON1, MII_CIS8201_EXTCON1_INIT);
484 static int cis820x_read_status(struct ugeth_mii_info *mii_info)
489 ugphy_vdbg("%s: IN", __FUNCTION__);
491 /* Update the link, but return if there
493 err = genmii_update_link(mii_info);
497 /* If the link is up, read the speed and duplex */
498 /* If we aren't autonegotiating, assume speeds
500 if (mii_info->autoneg && mii_info->link) {
503 status = ucc_geth_phy_read(mii_info, MII_CIS8201_AUX_CONSTAT);
504 if (status & MII_CIS8201_AUXCONSTAT_DUPLEX)
505 mii_info->duplex = DUPLEX_FULL;
507 mii_info->duplex = DUPLEX_HALF;
509 speed = status & MII_CIS8201_AUXCONSTAT_SPEED;
512 case MII_CIS8201_AUXCONSTAT_GBIT:
513 mii_info->speed = SPEED_1000;
515 case MII_CIS8201_AUXCONSTAT_100:
516 mii_info->speed = SPEED_100;
519 mii_info->speed = SPEED_10;
527 static int cis820x_ack_interrupt(struct ugeth_mii_info *mii_info)
529 ugphy_vdbg("%s: IN", __FUNCTION__);
531 ucc_geth_phy_read(mii_info, MII_CIS8201_ISTAT);
536 static int cis820x_config_intr(struct ugeth_mii_info *mii_info)
538 ugphy_vdbg("%s: IN", __FUNCTION__);
540 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
541 ucc_geth_phy_write(mii_info, MII_CIS8201_IMASK, MII_CIS8201_IMASK_MASK);
543 ucc_geth_phy_write(mii_info, MII_CIS8201_IMASK, 0);
548 #define DM9161_DELAY 10
550 static int dm9161_read_status(struct ugeth_mii_info *mii_info)
555 ugphy_vdbg("%s: IN", __FUNCTION__);
557 /* Update the link, but return if there
559 err = genmii_update_link(mii_info);
563 /* If the link is up, read the speed and duplex */
564 /* If we aren't autonegotiating, assume speeds
566 if (mii_info->autoneg && mii_info->link) {
567 status = ucc_geth_phy_read(mii_info, MII_DM9161_SCSR);
568 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
569 mii_info->speed = SPEED_100;
571 mii_info->speed = SPEED_10;
573 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
574 mii_info->duplex = DUPLEX_FULL;
576 mii_info->duplex = DUPLEX_HALF;
582 static int dm9161_config_aneg(struct ugeth_mii_info *mii_info)
584 struct dm9161_private *priv = mii_info->priv;
586 ugphy_vdbg("%s: IN", __FUNCTION__);
588 if (0 == priv->resetdone)
594 static void dm9161_timer(unsigned long data)
596 struct ugeth_mii_info *mii_info = (struct ugeth_mii_info *)data;
597 struct dm9161_private *priv = mii_info->priv;
598 u16 status = ucc_geth_phy_read(mii_info, MII_BMSR);
600 ugphy_vdbg("%s: IN", __FUNCTION__);
602 if (status & BMSR_ANEGCOMPLETE) {
605 mod_timer(&priv->timer, jiffies + DM9161_DELAY * HZ);
608 static int dm9161_init(struct ugeth_mii_info *mii_info)
610 struct dm9161_private *priv;
612 ugphy_vdbg("%s: IN", __FUNCTION__);
614 /* Allocate the private data structure */
615 priv = kmalloc(sizeof(struct dm9161_private), GFP_KERNEL);
620 mii_info->priv = priv;
622 /* Reset is not done yet */
625 ucc_geth_phy_write(mii_info, MII_BMCR,
626 ucc_geth_phy_read(mii_info, MII_BMCR) | BMCR_RESET);
628 ucc_geth_phy_write(mii_info, MII_BMCR,
629 ucc_geth_phy_read(mii_info, MII_BMCR) & ~BMCR_ISOLATE);
631 config_genmii_advert(mii_info);
632 /* Start/Restart aneg */
633 genmii_config_aneg(mii_info);
635 /* Start a timer for DM9161_DELAY seconds to wait
636 * for the PHY to be ready */
637 init_timer(&priv->timer);
638 priv->timer.function = &dm9161_timer;
639 priv->timer.data = (unsigned long)mii_info;
640 mod_timer(&priv->timer, jiffies + DM9161_DELAY * HZ);
645 static void dm9161_close(struct ugeth_mii_info *mii_info)
647 struct dm9161_private *priv = mii_info->priv;
649 ugphy_vdbg("%s: IN", __FUNCTION__);
651 del_timer_sync(&priv->timer);
655 static int dm9161_ack_interrupt(struct ugeth_mii_info *mii_info)
657 ugphy_vdbg("%s: IN", __FUNCTION__);
659 /* Clear the interrupts by reading the reg */
660 ucc_geth_phy_read(mii_info, MII_DM9161_INTR);
666 static int dm9161_config_intr(struct ugeth_mii_info *mii_info)
668 ugphy_vdbg("%s: IN", __FUNCTION__);
670 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
671 ucc_geth_phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
673 ucc_geth_phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
679 static struct phy_info phy_info_cis820x = {
680 .phy_id = 0x000fc440,
681 .name = "Cicada Cis8204",
682 .phy_id_mask = 0x000fffc0,
683 .features = MII_GBIT_FEATURES,
684 .init = &cis820x_init,
685 .config_aneg = &gbit_config_aneg,
686 .read_status = &cis820x_read_status,
687 .ack_interrupt = &cis820x_ack_interrupt,
688 .config_intr = &cis820x_config_intr,
691 static struct phy_info phy_info_dm9161 = {
692 .phy_id = 0x0181b880,
693 .phy_id_mask = 0x0ffffff0,
694 .name = "Davicom DM9161E",
696 .config_aneg = dm9161_config_aneg,
697 .read_status = dm9161_read_status,
698 .close = dm9161_close,
701 static struct phy_info phy_info_dm9161a = {
702 .phy_id = 0x0181b8a0,
703 .phy_id_mask = 0x0ffffff0,
704 .name = "Davicom DM9161A",
705 .features = MII_BASIC_FEATURES,
707 .config_aneg = dm9161_config_aneg,
708 .read_status = dm9161_read_status,
709 .ack_interrupt = dm9161_ack_interrupt,
710 .config_intr = dm9161_config_intr,
711 .close = dm9161_close,
714 static struct phy_info phy_info_marvell = {
715 .phy_id = 0x01410c00,
716 .phy_id_mask = 0xffffff00,
717 .name = "Marvell 88E11x1",
718 .features = MII_GBIT_FEATURES,
719 .init = &marvell_init,
720 .config_aneg = &marvell_config_aneg,
721 .read_status = &marvell_read_status,
722 .ack_interrupt = &marvell_ack_interrupt,
723 .config_intr = &marvell_config_intr,
726 static struct phy_info phy_info_genmii = {
727 .phy_id = 0x00000000,
728 .phy_id_mask = 0x00000000,
729 .name = "Generic MII",
730 .features = MII_BASIC_FEATURES,
731 .config_aneg = genmii_config_aneg,
732 .read_status = genmii_read_status,
735 static struct phy_info *phy_info[] = {
744 /* Use the PHY ID registers to determine what type of PHY is attached
745 * to device dev. return a struct phy_info structure describing that PHY
747 struct phy_info *get_phy_info(struct ugeth_mii_info *mii_info)
752 struct phy_info *theInfo = NULL;
753 struct net_device *dev = mii_info->dev;
755 ugphy_vdbg("%s: IN", __FUNCTION__);
757 /* Grab the bits from PHYIR1, and put them in the upper half */
758 phy_reg = ucc_geth_phy_read(mii_info, MII_PHYSID1);
759 phy_ID = (phy_reg & 0xffff) << 16;
761 /* Grab the bits from PHYIR2, and put them in the lower half */
762 phy_reg = ucc_geth_phy_read(mii_info, MII_PHYSID2);
763 phy_ID |= (phy_reg & 0xffff);
765 /* loop through all the known PHY types, and find one that */
766 /* matches the ID we read from the PHY. */
767 for (i = 0; phy_info[i]; i++)
768 if (phy_info[i]->phy_id == (phy_ID & phy_info[i]->phy_id_mask)){
769 theInfo = phy_info[i];
773 /* This shouldn't happen, as we have generic PHY support */
774 if (theInfo == NULL) {
775 ugphy_info("%s: PHY id %x is not supported!", dev->name,
779 ugphy_info("%s: PHY is %s (%x)", dev->name, theInfo->name,