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);
71 u16 phy_read(struct ugeth_mii_info *mii_info, u16 regnum);
72 void phy_write(struct ugeth_mii_info *mii_info, u16 regnum, u16 val);
74 /* Write value to the PHY for this device to the register at regnum, */
75 /* waiting until the write is done before it returns. All PHY */
76 /* configuration has to be done through the TSEC1 MIIM regs */
77 void write_phy_reg(struct net_device *dev, int mii_id, int regnum, int value)
79 struct ucc_geth_private *ugeth = netdev_priv(dev);
80 struct ucc_mii_mng *mii_regs;
81 enum enet_tbi_mii_reg mii_reg = (enum enet_tbi_mii_reg) regnum;
84 ugphy_vdbg("%s: IN", __FUNCTION__);
86 spin_lock_irq(&ugeth->lock);
88 mii_regs = ugeth->mii_info->mii_regs;
90 /* Set this UCC to be the master of the MII managment */
91 ucc_set_qe_mux_mii_mng(ugeth->ug_info->uf_info.ucc_num);
93 /* Stop the MII management read cycle */
94 out_be32(&mii_regs->miimcom, 0);
95 /* Setting up the MII Mangement Address Register */
96 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
97 out_be32(&mii_regs->miimadd, tmp_reg);
99 /* Setting up the MII Mangement Control Register with the value */
100 out_be32(&mii_regs->miimcon, (u32) value);
102 /* Wait till MII management write is complete */
103 while ((in_be32(&mii_regs->miimind)) & MIIMIND_BUSY)
106 spin_unlock_irq(&ugeth->lock);
111 /* Reads from register regnum in the PHY for device dev, */
112 /* returning the value. Clears miimcom first. All PHY */
113 /* configuration has to be done through the TSEC1 MIIM regs */
114 int read_phy_reg(struct net_device *dev, int mii_id, int regnum)
116 struct ucc_geth_private *ugeth = netdev_priv(dev);
117 struct ucc_mii_mng *mii_regs;
118 enum enet_tbi_mii_reg mii_reg = (enum enet_tbi_mii_reg) regnum;
122 ugphy_vdbg("%s: IN", __FUNCTION__);
124 spin_lock_irq(&ugeth->lock);
126 mii_regs = ugeth->mii_info->mii_regs;
128 /* Setting up the MII Mangement Address Register */
129 tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
130 out_be32(&mii_regs->miimadd, tmp_reg);
132 /* Perform an MII management read cycle */
133 out_be32(&mii_regs->miimcom, MIIMCOM_READ_CYCLE);
135 /* Wait till MII management write is complete */
136 while ((in_be32(&mii_regs->miimind)) & MIIMIND_BUSY)
141 /* Read MII management status */
142 value = (u16) in_be32(&mii_regs->miimstat);
143 out_be32(&mii_regs->miimcom, 0);
145 ugphy_warn("read wrong value : mii_id %d,mii_reg %d, base %08x",
146 mii_id, mii_reg, (u32) & (mii_regs->miimcfg));
148 spin_unlock_irq(&ugeth->lock);
153 void mii_clear_phy_interrupt(struct ugeth_mii_info *mii_info)
155 ugphy_vdbg("%s: IN", __FUNCTION__);
157 if (mii_info->phyinfo->ack_interrupt)
158 mii_info->phyinfo->ack_interrupt(mii_info);
161 void mii_configure_phy_interrupt(struct ugeth_mii_info *mii_info,
164 ugphy_vdbg("%s: IN", __FUNCTION__);
166 mii_info->interrupts = interrupts;
167 if (mii_info->phyinfo->config_intr)
168 mii_info->phyinfo->config_intr(mii_info);
171 /* Writes MII_ADVERTISE with the appropriate values, after
172 * sanitizing advertise to make sure only supported features
175 static void config_genmii_advert(struct ugeth_mii_info *mii_info)
180 ugphy_vdbg("%s: IN", __FUNCTION__);
182 /* Only allow advertising what this PHY supports */
183 mii_info->advertising &= mii_info->phyinfo->features;
184 advertise = mii_info->advertising;
186 /* Setup standard advertisement */
187 adv = phy_read(mii_info, MII_ADVERTISE);
188 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
189 if (advertise & ADVERTISED_10baseT_Half)
190 adv |= ADVERTISE_10HALF;
191 if (advertise & ADVERTISED_10baseT_Full)
192 adv |= ADVERTISE_10FULL;
193 if (advertise & ADVERTISED_100baseT_Half)
194 adv |= ADVERTISE_100HALF;
195 if (advertise & ADVERTISED_100baseT_Full)
196 adv |= ADVERTISE_100FULL;
197 phy_write(mii_info, MII_ADVERTISE, adv);
200 static void genmii_setup_forced(struct ugeth_mii_info *mii_info)
203 u32 features = mii_info->phyinfo->features;
205 ugphy_vdbg("%s: IN", __FUNCTION__);
207 ctrl = phy_read(mii_info, MII_BMCR);
210 ~(BMCR_FULLDPLX | BMCR_SPEED100 | BMCR_SPEED1000 | BMCR_ANENABLE);
213 switch (mii_info->speed) {
215 if (features & (SUPPORTED_1000baseT_Half
216 | SUPPORTED_1000baseT_Full)) {
217 ctrl |= BMCR_SPEED1000;
220 mii_info->speed = SPEED_100;
222 if (features & (SUPPORTED_100baseT_Half
223 | SUPPORTED_100baseT_Full)) {
224 ctrl |= BMCR_SPEED100;
227 mii_info->speed = SPEED_10;
229 if (features & (SUPPORTED_10baseT_Half
230 | SUPPORTED_10baseT_Full))
232 default: /* Unsupported speed! */
233 ugphy_err("%s: Bad speed!", mii_info->dev->name);
237 phy_write(mii_info, MII_BMCR, ctrl);
240 /* Enable and Restart Autonegotiation */
241 static void genmii_restart_aneg(struct ugeth_mii_info *mii_info)
245 ugphy_vdbg("%s: IN", __FUNCTION__);
247 ctl = phy_read(mii_info, MII_BMCR);
248 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
249 phy_write(mii_info, MII_BMCR, ctl);
252 static int gbit_config_aneg(struct ugeth_mii_info *mii_info)
257 ugphy_vdbg("%s: IN", __FUNCTION__);
259 if (mii_info->autoneg) {
260 /* Configure the ADVERTISE register */
261 config_genmii_advert(mii_info);
262 advertise = mii_info->advertising;
264 adv = phy_read(mii_info, MII_1000BASETCONTROL);
265 adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP |
266 MII_1000BASETCONTROL_HALFDUPLEXCAP);
267 if (advertise & SUPPORTED_1000baseT_Half)
268 adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
269 if (advertise & SUPPORTED_1000baseT_Full)
270 adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
271 phy_write(mii_info, MII_1000BASETCONTROL, adv);
273 /* Start/Restart aneg */
274 genmii_restart_aneg(mii_info);
276 genmii_setup_forced(mii_info);
281 static int genmii_config_aneg(struct ugeth_mii_info *mii_info)
283 ugphy_vdbg("%s: IN", __FUNCTION__);
285 if (mii_info->autoneg) {
286 config_genmii_advert(mii_info);
287 genmii_restart_aneg(mii_info);
289 genmii_setup_forced(mii_info);
294 static int genmii_update_link(struct ugeth_mii_info *mii_info)
298 ugphy_vdbg("%s: IN", __FUNCTION__);
301 phy_read(mii_info, MII_BMSR);
303 /* Read link and autonegotiation status */
304 status = phy_read(mii_info, MII_BMSR);
305 if ((status & BMSR_LSTATUS) == 0)
310 /* If we are autonegotiating, and not done,
312 if (mii_info->autoneg && !(status & BMSR_ANEGCOMPLETE))
318 static int genmii_read_status(struct ugeth_mii_info *mii_info)
323 ugphy_vdbg("%s: IN", __FUNCTION__);
325 /* Update the link, but return if there
327 err = genmii_update_link(mii_info);
331 if (mii_info->autoneg) {
332 status = phy_read(mii_info, MII_LPA);
334 if (status & (LPA_10FULL | LPA_100FULL))
335 mii_info->duplex = DUPLEX_FULL;
337 mii_info->duplex = DUPLEX_HALF;
338 if (status & (LPA_100FULL | LPA_100HALF))
339 mii_info->speed = SPEED_100;
341 mii_info->speed = SPEED_10;
344 /* On non-aneg, we assume what we put in BMCR is the speed,
345 * though magic-aneg shouldn't prevent this case from occurring
351 static int marvell_init(struct ugeth_mii_info *mii_info)
353 ugphy_vdbg("%s: IN", __FUNCTION__);
355 phy_write(mii_info, 0x14, 0x0cd2);
356 phy_write(mii_info, MII_BMCR,
357 phy_read(mii_info, MII_BMCR) | BMCR_RESET);
363 static int marvell_config_aneg(struct ugeth_mii_info *mii_info)
365 ugphy_vdbg("%s: IN", __FUNCTION__);
367 /* The Marvell PHY has an errata which requires
368 * that certain registers get written in order
369 * to restart autonegotiation */
370 phy_write(mii_info, MII_BMCR, BMCR_RESET);
372 phy_write(mii_info, 0x1d, 0x1f);
373 phy_write(mii_info, 0x1e, 0x200c);
374 phy_write(mii_info, 0x1d, 0x5);
375 phy_write(mii_info, 0x1e, 0);
376 phy_write(mii_info, 0x1e, 0x100);
378 gbit_config_aneg(mii_info);
383 static int marvell_read_status(struct ugeth_mii_info *mii_info)
388 ugphy_vdbg("%s: IN", __FUNCTION__);
390 /* Update the link, but return if there
392 err = genmii_update_link(mii_info);
396 /* If the link is up, read the speed and duplex */
397 /* If we aren't autonegotiating, assume speeds
399 if (mii_info->autoneg && mii_info->link) {
401 status = phy_read(mii_info, MII_M1011_PHY_SPEC_STATUS);
403 /* Get the duplexity */
404 if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
405 mii_info->duplex = DUPLEX_FULL;
407 mii_info->duplex = DUPLEX_HALF;
410 speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
412 case MII_M1011_PHY_SPEC_STATUS_1000:
413 mii_info->speed = SPEED_1000;
415 case MII_M1011_PHY_SPEC_STATUS_100:
416 mii_info->speed = SPEED_100;
419 mii_info->speed = SPEED_10;
428 static int marvell_ack_interrupt(struct ugeth_mii_info *mii_info)
430 ugphy_vdbg("%s: IN", __FUNCTION__);
432 /* Clear the interrupts by reading the reg */
433 phy_read(mii_info, MII_M1011_IEVENT);
438 static int marvell_config_intr(struct ugeth_mii_info *mii_info)
440 ugphy_vdbg("%s: IN", __FUNCTION__);
442 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
443 phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
445 phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
450 static int cis820x_init(struct ugeth_mii_info *mii_info)
452 ugphy_vdbg("%s: IN", __FUNCTION__);
454 phy_write(mii_info, MII_CIS8201_AUX_CONSTAT,
455 MII_CIS8201_AUXCONSTAT_INIT);
456 phy_write(mii_info, MII_CIS8201_EXT_CON1, MII_CIS8201_EXTCON1_INIT);
461 static int cis820x_read_status(struct ugeth_mii_info *mii_info)
466 ugphy_vdbg("%s: IN", __FUNCTION__);
468 /* Update the link, but return if there
470 err = genmii_update_link(mii_info);
474 /* If the link is up, read the speed and duplex */
475 /* If we aren't autonegotiating, assume speeds
477 if (mii_info->autoneg && mii_info->link) {
480 status = phy_read(mii_info, MII_CIS8201_AUX_CONSTAT);
481 if (status & MII_CIS8201_AUXCONSTAT_DUPLEX)
482 mii_info->duplex = DUPLEX_FULL;
484 mii_info->duplex = DUPLEX_HALF;
486 speed = status & MII_CIS8201_AUXCONSTAT_SPEED;
489 case MII_CIS8201_AUXCONSTAT_GBIT:
490 mii_info->speed = SPEED_1000;
492 case MII_CIS8201_AUXCONSTAT_100:
493 mii_info->speed = SPEED_100;
496 mii_info->speed = SPEED_10;
504 static int cis820x_ack_interrupt(struct ugeth_mii_info *mii_info)
506 ugphy_vdbg("%s: IN", __FUNCTION__);
508 phy_read(mii_info, MII_CIS8201_ISTAT);
513 static int cis820x_config_intr(struct ugeth_mii_info *mii_info)
515 ugphy_vdbg("%s: IN", __FUNCTION__);
517 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
518 phy_write(mii_info, MII_CIS8201_IMASK, MII_CIS8201_IMASK_MASK);
520 phy_write(mii_info, MII_CIS8201_IMASK, 0);
525 #define DM9161_DELAY 10
527 static int dm9161_read_status(struct ugeth_mii_info *mii_info)
532 ugphy_vdbg("%s: IN", __FUNCTION__);
534 /* Update the link, but return if there
536 err = genmii_update_link(mii_info);
540 /* If the link is up, read the speed and duplex */
541 /* If we aren't autonegotiating, assume speeds
543 if (mii_info->autoneg && mii_info->link) {
544 status = phy_read(mii_info, MII_DM9161_SCSR);
545 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
546 mii_info->speed = SPEED_100;
548 mii_info->speed = SPEED_10;
550 if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
551 mii_info->duplex = DUPLEX_FULL;
553 mii_info->duplex = DUPLEX_HALF;
559 static int dm9161_config_aneg(struct ugeth_mii_info *mii_info)
561 struct dm9161_private *priv = mii_info->priv;
563 ugphy_vdbg("%s: IN", __FUNCTION__);
565 if (0 == priv->resetdone)
571 static void dm9161_timer(unsigned long data)
573 struct ugeth_mii_info *mii_info = (struct ugeth_mii_info *)data;
574 struct dm9161_private *priv = mii_info->priv;
575 u16 status = phy_read(mii_info, MII_BMSR);
577 ugphy_vdbg("%s: IN", __FUNCTION__);
579 if (status & BMSR_ANEGCOMPLETE) {
582 mod_timer(&priv->timer, jiffies + DM9161_DELAY * HZ);
585 static int dm9161_init(struct ugeth_mii_info *mii_info)
587 struct dm9161_private *priv;
589 ugphy_vdbg("%s: IN", __FUNCTION__);
591 /* Allocate the private data structure */
592 priv = kmalloc(sizeof(struct dm9161_private), GFP_KERNEL);
597 mii_info->priv = priv;
599 /* Reset is not done yet */
602 phy_write(mii_info, MII_BMCR,
603 phy_read(mii_info, MII_BMCR) | BMCR_RESET);
605 phy_write(mii_info, MII_BMCR,
606 phy_read(mii_info, MII_BMCR) & ~BMCR_ISOLATE);
608 config_genmii_advert(mii_info);
609 /* Start/Restart aneg */
610 genmii_config_aneg(mii_info);
612 /* Start a timer for DM9161_DELAY seconds to wait
613 * for the PHY to be ready */
614 init_timer(&priv->timer);
615 priv->timer.function = &dm9161_timer;
616 priv->timer.data = (unsigned long)mii_info;
617 mod_timer(&priv->timer, jiffies + DM9161_DELAY * HZ);
622 static void dm9161_close(struct ugeth_mii_info *mii_info)
624 struct dm9161_private *priv = mii_info->priv;
626 ugphy_vdbg("%s: IN", __FUNCTION__);
628 del_timer_sync(&priv->timer);
632 static int dm9161_ack_interrupt(struct ugeth_mii_info *mii_info)
634 ugphy_vdbg("%s: IN", __FUNCTION__);
636 /* Clear the interrupts by reading the reg */
637 phy_read(mii_info, MII_DM9161_INTR);
643 static int dm9161_config_intr(struct ugeth_mii_info *mii_info)
645 ugphy_vdbg("%s: IN", __FUNCTION__);
647 if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
648 phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
650 phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
656 static struct phy_info phy_info_cis820x = {
657 .phy_id = 0x000fc440,
658 .name = "Cicada Cis8204",
659 .phy_id_mask = 0x000fffc0,
660 .features = MII_GBIT_FEATURES,
661 .init = &cis820x_init,
662 .config_aneg = &gbit_config_aneg,
663 .read_status = &cis820x_read_status,
664 .ack_interrupt = &cis820x_ack_interrupt,
665 .config_intr = &cis820x_config_intr,
668 static struct phy_info phy_info_dm9161 = {
669 .phy_id = 0x0181b880,
670 .phy_id_mask = 0x0ffffff0,
671 .name = "Davicom DM9161E",
673 .config_aneg = dm9161_config_aneg,
674 .read_status = dm9161_read_status,
675 .close = dm9161_close,
678 static struct phy_info phy_info_dm9161a = {
679 .phy_id = 0x0181b8a0,
680 .phy_id_mask = 0x0ffffff0,
681 .name = "Davicom DM9161A",
682 .features = MII_BASIC_FEATURES,
684 .config_aneg = dm9161_config_aneg,
685 .read_status = dm9161_read_status,
686 .ack_interrupt = dm9161_ack_interrupt,
687 .config_intr = dm9161_config_intr,
688 .close = dm9161_close,
691 static struct phy_info phy_info_marvell = {
692 .phy_id = 0x01410c00,
693 .phy_id_mask = 0xffffff00,
694 .name = "Marvell 88E11x1",
695 .features = MII_GBIT_FEATURES,
696 .init = &marvell_init,
697 .config_aneg = &marvell_config_aneg,
698 .read_status = &marvell_read_status,
699 .ack_interrupt = &marvell_ack_interrupt,
700 .config_intr = &marvell_config_intr,
703 static struct phy_info phy_info_genmii = {
704 .phy_id = 0x00000000,
705 .phy_id_mask = 0x00000000,
706 .name = "Generic MII",
707 .features = MII_BASIC_FEATURES,
708 .config_aneg = genmii_config_aneg,
709 .read_status = genmii_read_status,
712 static struct phy_info *phy_info[] = {
721 u16 phy_read(struct ugeth_mii_info *mii_info, u16 regnum)
726 ugphy_vdbg("%s: IN", __FUNCTION__);
728 spin_lock_irqsave(&mii_info->mdio_lock, flags);
729 retval = mii_info->mdio_read(mii_info->dev, mii_info->mii_id, regnum);
730 spin_unlock_irqrestore(&mii_info->mdio_lock, flags);
735 void phy_write(struct ugeth_mii_info *mii_info, u16 regnum, u16 val)
739 ugphy_vdbg("%s: IN", __FUNCTION__);
741 spin_lock_irqsave(&mii_info->mdio_lock, flags);
742 mii_info->mdio_write(mii_info->dev, mii_info->mii_id, regnum, val);
743 spin_unlock_irqrestore(&mii_info->mdio_lock, flags);
746 /* Use the PHY ID registers to determine what type of PHY is attached
747 * to device dev. return a struct phy_info structure describing that PHY
749 struct phy_info *get_phy_info(struct ugeth_mii_info *mii_info)
754 struct phy_info *theInfo = NULL;
755 struct net_device *dev = mii_info->dev;
757 ugphy_vdbg("%s: IN", __FUNCTION__);
759 /* Grab the bits from PHYIR1, and put them in the upper half */
760 phy_reg = phy_read(mii_info, MII_PHYSID1);
761 phy_ID = (phy_reg & 0xffff) << 16;
763 /* Grab the bits from PHYIR2, and put them in the lower half */
764 phy_reg = phy_read(mii_info, MII_PHYSID2);
765 phy_ID |= (phy_reg & 0xffff);
767 /* loop through all the known PHY types, and find one that */
768 /* matches the ID we read from the PHY. */
769 for (i = 0; phy_info[i]; i++)
770 if (phy_info[i]->phy_id == (phy_ID & phy_info[i]->phy_id_mask)){
771 theInfo = phy_info[i];
775 /* This shouldn't happen, as we have generic PHY support */
776 if (theInfo == NULL) {
777 ugphy_info("%s: PHY id %x is not supported!", dev->name,
781 ugphy_info("%s: PHY is %s (%x)", dev->name, theInfo->name,