wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /*-----------------------------------------------------------------------------+ |
Josh Boyer | 471573b | 2009-08-07 13:53:20 -0400 | [diff] [blame] | 2 | | This source code is dual-licensed. You may use it under the terms of the |
| 3 | | GNU General Public License version 2, or under the license below. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 4 | | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 5 | | This source code has been made available to you by IBM on an AS-IS |
| 6 | | basis. Anyone receiving this source is licensed under IBM |
| 7 | | copyrights to use it in any way he or she deems fit, including |
| 8 | | copying it, modifying it, compiling it, and redistributing it either |
| 9 | | with or without modifications. No license under IBM patents or |
| 10 | | patent applications is to be implied by the copyright license. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 11 | | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 12 | | Any user of this software should understand that IBM cannot provide |
| 13 | | technical support for this software and will not be responsible for |
| 14 | | any consequences resulting from the use of this software. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 15 | | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 16 | | Any person who transfers this source code or any derivative work |
| 17 | | must include the IBM copyright notice, this paragraph, and the |
| 18 | | preceding two paragraphs in the transferred software. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 19 | | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 20 | | COPYRIGHT I B M CORPORATION 1995 |
| 21 | | LICENSED MATERIAL - PROGRAM PROPERTY OF I B M |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 22 | +-----------------------------------------------------------------------------*/ |
| 23 | /*-----------------------------------------------------------------------------+ |
| 24 | | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 25 | | File Name: miiphy.c |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 26 | | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 27 | | Function: This module has utilities for accessing the MII PHY through |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 28 | | the EMAC3 macro. |
| 29 | | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 30 | | Author: Mark Wisner |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 31 | | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 32 | +-----------------------------------------------------------------------------*/ |
| 33 | |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 34 | /* define DEBUG for debugging output (obviously ;-)) */ |
| 35 | #if 0 |
| 36 | #define DEBUG |
| 37 | #endif |
| 38 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 39 | #include <common.h> |
| 40 | #include <asm/processor.h> |
Stefan Roese | 69710095 | 2007-10-23 14:03:17 +0200 | [diff] [blame] | 41 | #include <asm/io.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 42 | #include <ppc_asm.tmpl> |
| 43 | #include <commproc.h> |
Stefan Roese | 247e9d7 | 2010-09-09 19:18:00 +0200 | [diff] [blame] | 44 | #include <asm/ppc4xx-emac.h> |
| 45 | #include <asm/ppc4xx-mal.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 46 | #include <miiphy.h> |
| 47 | |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 48 | #if !defined(CONFIG_PHY_CLK_FREQ) |
| 49 | #define CONFIG_PHY_CLK_FREQ 0 |
| 50 | #endif |
| 51 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 52 | /***********************************************************/ |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 53 | /* Dump out to the screen PHY regs */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 54 | /***********************************************************/ |
| 55 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 56 | void miiphy_dump (char *devname, unsigned char addr) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 57 | { |
| 58 | unsigned long i; |
| 59 | unsigned short data; |
| 60 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 61 | for (i = 0; i < 0x1A; i++) { |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 62 | if (miiphy_read (devname, addr, i, &data)) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 63 | printf ("read error for reg %lx\n", i); |
| 64 | return; |
| 65 | } |
| 66 | printf ("Phy reg %lx ==> %4x\n", i, data); |
| 67 | |
| 68 | /* jump to the next set of regs */ |
| 69 | if (i == 0x07) |
| 70 | i = 0x0f; |
| 71 | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 72 | } /* end for loop */ |
| 73 | } /* end dump */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 74 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 75 | /***********************************************************/ |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 76 | /* (Re)start autonegotiation */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 77 | /***********************************************************/ |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 78 | int phy_setup_aneg (char *devname, unsigned char addr) |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 79 | { |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 80 | u16 bmcr; |
| 81 | |
| 82 | #if defined(CONFIG_PHY_DYNAMIC_ANEG) |
| 83 | /* |
| 84 | * Set up advertisement based on capablilities reported by the PHY. |
| 85 | * This should work for both copper and fiber. |
| 86 | */ |
| 87 | u16 bmsr; |
| 88 | #if defined(CONFIG_PHY_GIGE) |
| 89 | u16 exsr = 0x0000; |
| 90 | #endif |
| 91 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 92 | miiphy_read (devname, addr, MII_BMSR, &bmsr); |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 93 | |
| 94 | #if defined(CONFIG_PHY_GIGE) |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 95 | if (bmsr & BMSR_ESTATEN) |
| 96 | miiphy_read (devname, addr, MII_ESTATUS, &exsr); |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 97 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 98 | if (exsr & (ESTATUS_1000XF | ESTATUS_1000XH)) { |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 99 | /* 1000BASE-X */ |
| 100 | u16 anar = 0x0000; |
| 101 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 102 | if (exsr & ESTATUS_1000XF) |
Stefan Roese | 772696c | 2011-01-10 12:56:13 +0100 | [diff] [blame] | 103 | anar |= ADVERTISE_1000XFULL; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 104 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 105 | if (exsr & ESTATUS_1000XH) |
| 106 | anar |= ADVERTISE_1000XHALF; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 107 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 108 | miiphy_write (devname, addr, MII_ADVERTISE, anar); |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 109 | } else |
| 110 | #endif |
| 111 | { |
| 112 | u16 anar, btcr; |
| 113 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 114 | miiphy_read (devname, addr, MII_ADVERTISE, &anar); |
| 115 | anar &= ~(0x5000 | LPA_100BASE4 | LPA_100FULL | |
| 116 | LPA_100HALF | LPA_10FULL | LPA_10HALF); |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 117 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 118 | miiphy_read (devname, addr, MII_CTRL1000, &btcr); |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 119 | btcr &= ~(0x00FF | PHY_1000BTCR_1000FD | PHY_1000BTCR_1000HD); |
| 120 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 121 | if (bmsr & BMSR_100BASE4) |
| 122 | anar |= LPA_100BASE4; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 123 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 124 | if (bmsr & BMSR_100FULL) |
| 125 | anar |= LPA_100FULL; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 126 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 127 | if (bmsr & BMSR_100HALF) |
| 128 | anar |= LPA_100HALF; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 129 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 130 | if (bmsr & BMSR_10FULL) |
| 131 | anar |= LPA_10FULL; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 132 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 133 | if (bmsr & BMSR_10HALF) |
| 134 | anar |= LPA_10HALF; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 135 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 136 | miiphy_write (devname, addr, MII_ADVERTISE, anar); |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 137 | |
| 138 | #if defined(CONFIG_PHY_GIGE) |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 139 | if (exsr & ESTATUS_1000_TFULL) |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 140 | btcr |= PHY_1000BTCR_1000FD; |
| 141 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 142 | if (exsr & ESTATUS_1000_THALF) |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 143 | btcr |= PHY_1000BTCR_1000HD; |
| 144 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 145 | miiphy_write (devname, addr, MII_CTRL1000, btcr); |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 146 | #endif |
| 147 | } |
| 148 | |
| 149 | #else /* defined(CONFIG_PHY_DYNAMIC_ANEG) */ |
| 150 | /* |
| 151 | * Set up standard advertisement |
| 152 | */ |
| 153 | u16 adv; |
| 154 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 155 | miiphy_read (devname, addr, MII_ADVERTISE, &adv); |
| 156 | adv |= (LPA_LPACK | LPA_100FULL | LPA_100HALF | |
| 157 | LPA_10FULL | LPA_10HALF); |
| 158 | miiphy_write (devname, addr, MII_ADVERTISE, adv); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 159 | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 160 | miiphy_read (devname, addr, MII_CTRL1000, &adv); |
Marian Balakowicz | 49d0eee | 2006-06-30 16:30:46 +0200 | [diff] [blame] | 161 | adv |= (0x0300); |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 162 | miiphy_write (devname, addr, MII_CTRL1000, adv); |
Marian Balakowicz | 49d0eee | 2006-06-30 16:30:46 +0200 | [diff] [blame] | 163 | |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 164 | #endif /* defined(CONFIG_PHY_DYNAMIC_ANEG) */ |
| 165 | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 166 | /* Start/Restart aneg */ |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 167 | miiphy_read (devname, addr, MII_BMCR, &bmcr); |
| 168 | bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART); |
| 169 | miiphy_write (devname, addr, MII_BMCR, bmcr); |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 174 | /***********************************************************/ |
| 175 | /* read a phy reg and return the value with a rc */ |
| 176 | /***********************************************************/ |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 177 | /* AMCC_TODO: |
| 178 | * Find out of the choice for the emac for MDIO is from the bridges, |
| 179 | * i.e. ZMII or RGMII as approporiate. If the bridges are not used |
| 180 | * to determine the emac for MDIO, then is the SDR0_ETH_CFG[MDIO_SEL] |
| 181 | * used? If so, then this routine below does not apply to the 460EX/GT. |
| 182 | * |
| 183 | * sr: Currently on 460EX only EMAC0 works with MDIO, so we always |
| 184 | * return EMAC0 offset here |
Victor Gallardo | 45a06ff | 2008-09-04 23:49:36 -0700 | [diff] [blame] | 185 | * vg: For 460EX/460GT if internal GPCS PHY address is specified |
| 186 | * return appropriate EMAC offset |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 187 | */ |
Victor Gallardo | 45a06ff | 2008-09-04 23:49:36 -0700 | [diff] [blame] | 188 | unsigned int miiphy_getemac_offset(u8 addr) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 189 | { |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 190 | #if (defined(CONFIG_440) && \ |
| 191 | !defined(CONFIG_440SP) && !defined(CONFIG_440SPE) && \ |
| 192 | !defined(CONFIG_460EX) && !defined(CONFIG_460GT)) && \ |
| 193 | defined(CONFIG_NET_MULTI) |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 194 | unsigned long zmii; |
| 195 | unsigned long eoffset; |
| 196 | |
| 197 | /* Need to find out which mdi port we're using */ |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 198 | zmii = in_be32((void *)ZMII0_FER); |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 199 | |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 200 | if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0))) |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 201 | /* using port 0 */ |
| 202 | eoffset = 0; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 203 | |
| 204 | else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1))) |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 205 | /* using port 1 */ |
| 206 | eoffset = 0x100; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 207 | |
| 208 | else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2))) |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 209 | /* using port 2 */ |
| 210 | eoffset = 0x400; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 211 | |
| 212 | else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3))) |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 213 | /* using port 3 */ |
| 214 | eoffset = 0x600; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 215 | |
| 216 | else { |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 217 | /* None of the mdi ports are enabled! */ |
| 218 | /* enable port 0 */ |
| 219 | zmii |= ZMII_FER_MDI << ZMII_FER_V (0); |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 220 | out_be32((void *)ZMII0_FER, zmii); |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 221 | eoffset = 0; |
| 222 | /* need to soft reset port 0 */ |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 223 | zmii = in_be32((void *)EMAC0_MR0); |
| 224 | zmii |= EMAC_MR0_SRST; |
| 225 | out_be32((void *)EMAC0_MR0, zmii); |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return (eoffset); |
| 229 | #else |
Stefan Roese | 153b3e2 | 2007-10-05 17:10:59 +0200 | [diff] [blame] | 230 | |
| 231 | #if defined(CONFIG_NET_MULTI) && defined(CONFIG_405EX) |
| 232 | unsigned long rgmii; |
| 233 | int devnum = 1; |
| 234 | |
Stefan Roese | 69710095 | 2007-10-23 14:03:17 +0200 | [diff] [blame] | 235 | rgmii = in_be32((void *)RGMII_FER); |
Stefan Roese | 153b3e2 | 2007-10-05 17:10:59 +0200 | [diff] [blame] | 236 | if (rgmii & (1 << (19 - devnum))) |
| 237 | return 0x100; |
| 238 | #endif |
| 239 | |
Victor Gallardo | 45a06ff | 2008-09-04 23:49:36 -0700 | [diff] [blame] | 240 | #if defined(CONFIG_460EX) || defined(CONFIG_460GT) |
Victor Gallardo | 45a06ff | 2008-09-04 23:49:36 -0700 | [diff] [blame] | 241 | u32 eoffset = 0; |
| 242 | |
| 243 | switch (addr) { |
| 244 | #if defined(CONFIG_HAS_ETH1) && defined(CONFIG_GPCS_PHY1_ADDR) |
| 245 | case CONFIG_GPCS_PHY1_ADDR: |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 246 | if (addr == EMAC_MR1_IPPA_GET(in_be32((void *)EMAC0_MR1 + 0x100))) |
Victor Gallardo | 45a06ff | 2008-09-04 23:49:36 -0700 | [diff] [blame] | 247 | eoffset = 0x100; |
| 248 | break; |
| 249 | #endif |
| 250 | #if defined(CONFIG_HAS_ETH2) && defined(CONFIG_GPCS_PHY2_ADDR) |
| 251 | case CONFIG_GPCS_PHY2_ADDR: |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 252 | if (addr == EMAC_MR1_IPPA_GET(in_be32((void *)EMAC0_MR1 + 0x300))) |
Victor Gallardo | 45a06ff | 2008-09-04 23:49:36 -0700 | [diff] [blame] | 253 | eoffset = 0x300; |
| 254 | break; |
| 255 | #endif |
| 256 | #if defined(CONFIG_HAS_ETH3) && defined(CONFIG_GPCS_PHY3_ADDR) |
| 257 | case CONFIG_GPCS_PHY3_ADDR: |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 258 | if (addr == EMAC_MR1_IPPA_GET(in_be32((void *)EMAC0_MR1 + 0x400))) |
Victor Gallardo | 45a06ff | 2008-09-04 23:49:36 -0700 | [diff] [blame] | 259 | eoffset = 0x400; |
| 260 | break; |
| 261 | #endif |
| 262 | default: |
| 263 | eoffset = 0; |
| 264 | break; |
| 265 | } |
| 266 | return eoffset; |
| 267 | #endif |
| 268 | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 269 | return 0; |
| 270 | #endif |
| 271 | } |
| 272 | |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 273 | static int emac_miiphy_wait(u32 emac_reg) |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 274 | { |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 275 | u32 sta_reg; |
| 276 | int i; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 277 | |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 278 | /* wait for completion */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 279 | i = 0; |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 280 | do { |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 281 | sta_reg = in_be32((void *)EMAC0_STACR + emac_reg); |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 282 | if (i++ > 5) { |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 283 | debug("%s [%d]: Timeout! EMAC0_STACR=0x%0x\n", __func__, |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 284 | __LINE__, sta_reg); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 285 | return -1; |
| 286 | } |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 287 | udelay(10); |
| 288 | } while ((sta_reg & EMAC_STACR_OC) == EMAC_STACR_OC_MASK); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static int emac_miiphy_command(u8 addr, u8 reg, int cmd, u16 value) |
| 294 | { |
| 295 | u32 emac_reg; |
| 296 | u32 sta_reg; |
| 297 | |
Victor Gallardo | 45a06ff | 2008-09-04 23:49:36 -0700 | [diff] [blame] | 298 | emac_reg = miiphy_getemac_offset(addr); |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 299 | |
| 300 | /* wait for completion */ |
| 301 | if (emac_miiphy_wait(emac_reg) != 0) |
| 302 | return -1; |
| 303 | |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 304 | sta_reg = reg; /* reg address */ |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 305 | |
Wolfgang Denk | af0501a | 2008-10-19 02:35:50 +0200 | [diff] [blame] | 306 | /* set clock (50MHz) and read flags */ |
Stefan Roese | 42fbddd | 2006-09-07 11:51:23 +0200 | [diff] [blame] | 307 | #if defined(CONFIG_440GX) || defined(CONFIG_440SPE) || \ |
Stefan Roese | 153b3e2 | 2007-10-05 17:10:59 +0200 | [diff] [blame] | 308 | defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \ |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 309 | defined(CONFIG_460EX) || defined(CONFIG_460GT) || \ |
Stefan Roese | 153b3e2 | 2007-10-05 17:10:59 +0200 | [diff] [blame] | 310 | defined(CONFIG_405EX) |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 311 | #if defined(CONFIG_IBM_EMAC4_V4) /* EMAC4 V4 changed bit setting */ |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 312 | sta_reg = (sta_reg & ~EMAC_STACR_OP_MASK) | cmd; |
Marian Balakowicz | 49d0eee | 2006-06-30 16:30:46 +0200 | [diff] [blame] | 313 | #else |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 314 | sta_reg |= cmd; |
Marian Balakowicz | 49d0eee | 2006-06-30 16:30:46 +0200 | [diff] [blame] | 315 | #endif |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 316 | #else |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 317 | sta_reg = (sta_reg | cmd) & ~EMAC_STACR_CLK_100MHZ; |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 318 | #endif |
| 319 | |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 320 | /* Some boards (mainly 405EP based) define the PHY clock freqency fixed */ |
wdenk | 232fe0b | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 321 | sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 322 | sta_reg = sta_reg | ((u32)addr << 5); /* Phy address */ |
Marian Balakowicz | 49d0eee | 2006-06-30 16:30:46 +0200 | [diff] [blame] | 323 | sta_reg = sta_reg | EMAC_STACR_OC_MASK; /* new IBM emac v4 */ |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 324 | if (cmd == EMAC_STACR_WRITE) |
| 325 | memcpy(&sta_reg, &value, 2); /* put in data */ |
| 326 | |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 327 | out_be32((void *)EMAC0_STACR + emac_reg, sta_reg); |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 328 | debug("%s [%d]: sta_reg=%08x\n", __func__, __LINE__, sta_reg); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 329 | |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 330 | /* wait for completion */ |
| 331 | if (emac_miiphy_wait(emac_reg) != 0) |
| 332 | return -1; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 333 | |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 334 | debug("%s [%d]: sta_reg=%08x\n", __func__, __LINE__, sta_reg); |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 335 | if ((sta_reg & EMAC_STACR_PHYE) != 0) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 336 | return -1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 337 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 338 | return 0; |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 339 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 340 | |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 341 | int emac4xx_miiphy_read (const char *devname, unsigned char addr, unsigned char reg, |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 342 | unsigned short *value) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 343 | { |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 344 | unsigned long sta_reg; |
Stefan Roese | 0c7ffc0 | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 345 | unsigned long emac_reg; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 346 | |
Victor Gallardo | 45a06ff | 2008-09-04 23:49:36 -0700 | [diff] [blame] | 347 | emac_reg = miiphy_getemac_offset(addr); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 348 | |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 349 | if (emac_miiphy_command(addr, reg, EMAC_STACR_READ, 0) != 0) |
| 350 | return -1; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 351 | |
Niklaus Giger | 728bd0a | 2009-10-04 20:04:20 +0200 | [diff] [blame] | 352 | sta_reg = in_be32((void *)EMAC0_STACR + emac_reg); |
Stefan Roese | 1fc08dd | 2009-09-07 10:52:24 +0200 | [diff] [blame] | 353 | *value = sta_reg >> 16; |
Larry Johnson | 50325a0 | 2007-12-27 10:50:55 -0500 | [diff] [blame] | 354 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 355 | return 0; |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 356 | } |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 357 | |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 358 | /***********************************************************/ |
| 359 | /* write a phy reg and return the value with a rc */ |
| 360 | /***********************************************************/ |
| 361 | |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 362 | int emac4xx_miiphy_write (const char *devname, unsigned char addr, unsigned char reg, |
Stefan Roese | 8b231b2 | 2008-02-19 21:58:25 +0100 | [diff] [blame] | 363 | unsigned short value) |
| 364 | { |
| 365 | return emac_miiphy_command(addr, reg, EMAC_STACR_WRITE, value); |
| 366 | } |