wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /*-----------------------------------------------------------------------------+ |
| 2 | | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 3 | | This source code has been made available to you by IBM on an AS-IS |
| 4 | | basis. Anyone receiving this source is licensed under IBM |
| 5 | | copyrights to use it in any way he or she deems fit, including |
| 6 | | copying it, modifying it, compiling it, and redistributing it either |
| 7 | | with or without modifications. No license under IBM patents or |
| 8 | | patent applications is to be implied by the copyright license. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 9 | | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 10 | | Any user of this software should understand that IBM cannot provide |
| 11 | | technical support for this software and will not be responsible for |
| 12 | | any consequences resulting from the use of this software. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 13 | | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 14 | | Any person who transfers this source code or any derivative work |
| 15 | | must include the IBM copyright notice, this paragraph, and the |
| 16 | | preceding two paragraphs in the transferred software. |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 17 | | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 18 | | COPYRIGHT I B M CORPORATION 1995 |
| 19 | | LICENSED MATERIAL - PROGRAM PROPERTY OF I B M |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 20 | +-----------------------------------------------------------------------------*/ |
| 21 | /*-----------------------------------------------------------------------------+ |
| 22 | | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 23 | | File Name: miiphy.c |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 24 | | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 25 | | Function: This module has utilities for accessing the MII PHY through |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 26 | | the EMAC3 macro. |
| 27 | | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 28 | | Author: Mark Wisner |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 29 | | |
| 30 | | Change Activity- |
| 31 | | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 32 | | Date Description of Change BY |
| 33 | | --------- --------------------- --- |
| 34 | | 05-May-99 Created MKW |
| 35 | | 01-Jul-99 Changed clock setting of sta_reg from 66Mhz to 50Mhz to |
| 36 | | better match OPB speed. Also modified delay times. JWB |
| 37 | | 29-Jul-99 Added Full duplex support MKW |
| 38 | | 24-Aug-99 Removed printf from dp83843_duplex() JWB |
| 39 | | 19-Jul-00 Ported to esd cpci405 sr |
| 40 | | 23-Dec-03 Ported from miiphy.c to 440GX Travis Sawyer TBS |
| 41 | | <travis.sawyer@sandburst.com> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 42 | | |
| 43 | +-----------------------------------------------------------------------------*/ |
| 44 | |
| 45 | #include <common.h> |
| 46 | #include <asm/processor.h> |
| 47 | #include <ppc_asm.tmpl> |
| 48 | #include <commproc.h> |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 49 | #include <ppc4xx_enet.h> |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 50 | #include <405_mal.h> |
| 51 | #include <miiphy.h> |
| 52 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 53 | |
| 54 | /***********************************************************/ |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 55 | /* Dump out to the screen PHY regs */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 56 | /***********************************************************/ |
| 57 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 58 | void miiphy_dump (char *devname, unsigned char addr) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 59 | { |
| 60 | unsigned long i; |
| 61 | unsigned short data; |
| 62 | |
| 63 | |
| 64 | for (i = 0; i < 0x1A; i++) { |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 65 | if (miiphy_read (devname, addr, i, &data)) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 66 | printf ("read error for reg %lx\n", i); |
| 67 | return; |
| 68 | } |
| 69 | printf ("Phy reg %lx ==> %4x\n", i, data); |
| 70 | |
| 71 | /* jump to the next set of regs */ |
| 72 | if (i == 0x07) |
| 73 | i = 0x0f; |
| 74 | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 75 | } /* end for loop */ |
| 76 | } /* end dump */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 77 | |
| 78 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 79 | /***********************************************************/ |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 80 | /* (Re)start autonegotiation */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 81 | /***********************************************************/ |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 82 | int phy_setup_aneg (char *devname, unsigned char addr) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 83 | { |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 84 | unsigned short ctl, adv; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 85 | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 86 | /* Setup standard advertise */ |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 87 | miiphy_read (devname, addr, PHY_ANAR, &adv); |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 88 | adv |= (PHY_ANLPAR_ACK | PHY_ANLPAR_RF | PHY_ANLPAR_T4 | |
| 89 | PHY_ANLPAR_TXFD | PHY_ANLPAR_TX | PHY_ANLPAR_10FD | |
| 90 | PHY_ANLPAR_10); |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 91 | miiphy_write (devname, addr, PHY_ANAR, adv); |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 92 | |
| 93 | /* Start/Restart aneg */ |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 94 | miiphy_read (devname, addr, PHY_BMCR, &ctl); |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 95 | ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG); |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 96 | miiphy_write (devname, addr, PHY_BMCR, ctl); |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /***********************************************************/ |
| 103 | /* read a phy reg and return the value with a rc */ |
| 104 | /***********************************************************/ |
| 105 | unsigned int miiphy_getemac_offset (void) |
| 106 | { |
Stefan Roese | 6e7fb6e | 2005-11-29 18:18:21 +0100 | [diff] [blame] | 107 | #if (defined(CONFIG_440) && !defined(CONFIG_440SP)) && defined(CONFIG_NET_MULTI) |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 108 | unsigned long zmii; |
| 109 | unsigned long eoffset; |
| 110 | |
| 111 | /* Need to find out which mdi port we're using */ |
| 112 | zmii = in32 (ZMII_FER); |
| 113 | |
| 114 | if (zmii & (ZMII_FER_MDI << ZMII_FER_V (0))) { |
| 115 | /* using port 0 */ |
| 116 | eoffset = 0; |
| 117 | } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (1))) { |
| 118 | /* using port 1 */ |
| 119 | eoffset = 0x100; |
| 120 | } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (2))) { |
| 121 | /* using port 2 */ |
| 122 | eoffset = 0x400; |
| 123 | } else if (zmii & (ZMII_FER_MDI << ZMII_FER_V (3))) { |
| 124 | /* using port 3 */ |
| 125 | eoffset = 0x600; |
| 126 | } else { |
| 127 | /* None of the mdi ports are enabled! */ |
| 128 | /* enable port 0 */ |
| 129 | zmii |= ZMII_FER_MDI << ZMII_FER_V (0); |
| 130 | out32 (ZMII_FER, zmii); |
| 131 | eoffset = 0; |
| 132 | /* need to soft reset port 0 */ |
| 133 | zmii = in32 (EMAC_M0); |
| 134 | zmii |= EMAC_M0_SRST; |
| 135 | out32 (EMAC_M0, zmii); |
| 136 | } |
| 137 | |
| 138 | return (eoffset); |
| 139 | #else |
| 140 | return 0; |
| 141 | #endif |
| 142 | } |
| 143 | |
| 144 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 145 | int emac4xx_miiphy_read (char *devname, unsigned char addr, |
| 146 | unsigned char reg, unsigned short *value) |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 147 | { |
| 148 | unsigned long sta_reg; /* STA scratch area */ |
| 149 | unsigned long i; |
| 150 | unsigned long emac_reg; |
| 151 | |
| 152 | |
| 153 | emac_reg = miiphy_getemac_offset (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 154 | /* see if it is ready for 1000 nsec */ |
| 155 | i = 0; |
| 156 | |
| 157 | /* see if it is ready for sec */ |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 158 | while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 159 | udelay (7); |
| 160 | if (i > 5) { |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 161 | #if 0 |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 162 | printf ("read err 1\n"); |
stroese | 38a9519 | 2003-12-09 14:57:03 +0000 | [diff] [blame] | 163 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 164 | return -1; |
| 165 | } |
| 166 | i++; |
| 167 | } |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 168 | sta_reg = reg; /* reg address */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 169 | /* set clock (50Mhz) and read flags */ |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 170 | #if defined(CONFIG_440GX) |
| 171 | sta_reg |= EMAC_STACR_READ; |
| 172 | #else |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 173 | sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ; |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 174 | #endif |
| 175 | |
| 176 | #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) |
wdenk | 12f3424 | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 177 | sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; |
wdenk | 093ae27 | 2003-09-02 23:08:13 +0000 | [diff] [blame] | 178 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 179 | sta_reg = sta_reg | (addr << 5); /* Phy address */ |
| 180 | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 181 | out32 (EMAC_STACR + emac_reg, sta_reg); |
| 182 | #if 0 /* test-only */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 183 | printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg); /* test-only */ |
| 184 | #endif |
| 185 | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 186 | sta_reg = in32 (EMAC_STACR + emac_reg); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 187 | i = 0; |
| 188 | while ((sta_reg & EMAC_STACR_OC) == 0) { |
| 189 | udelay (7); |
| 190 | if (i > 5) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 191 | return -1; |
| 192 | } |
| 193 | i++; |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 194 | sta_reg = in32 (EMAC_STACR + emac_reg); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 195 | } |
| 196 | if ((sta_reg & EMAC_STACR_PHYE) != 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 197 | return -1; |
| 198 | } |
| 199 | |
| 200 | *value = *(short *) (&sta_reg); |
| 201 | return 0; |
| 202 | |
| 203 | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 204 | } /* phy_read */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 205 | |
| 206 | |
| 207 | /***********************************************************/ |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 208 | /* write a phy reg and return the value with a rc */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 209 | /***********************************************************/ |
| 210 | |
Marian Balakowicz | 63ff004 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 211 | int emac4xx_miiphy_write (char *devname, unsigned char addr, |
| 212 | unsigned char reg, unsigned short value) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 213 | { |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 214 | unsigned long sta_reg; /* STA scratch area */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 215 | unsigned long i; |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 216 | unsigned long emac_reg; |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 217 | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 218 | emac_reg = miiphy_getemac_offset (); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 219 | /* see if it is ready for 1000 nsec */ |
| 220 | i = 0; |
| 221 | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 222 | while ((in32 (EMAC_STACR + emac_reg) & EMAC_STACR_OC) == 0) { |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 223 | if (i > 5) |
| 224 | return -1; |
| 225 | udelay (7); |
| 226 | i++; |
| 227 | } |
| 228 | sta_reg = 0; |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 229 | sta_reg = reg; /* reg address */ |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 230 | /* set clock (50Mhz) and read flags */ |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 231 | #if defined(CONFIG_440GX) |
| 232 | sta_reg |= EMAC_STACR_WRITE; |
| 233 | #else |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 234 | sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ; |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 235 | #endif |
| 236 | |
| 237 | #if defined(CONFIG_PHY_CLK_FREQ) && !defined(CONFIG_440GX) |
| 238 | sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; /* Set clock frequency (PLB freq. dependend) */ |
wdenk | 093ae27 | 2003-09-02 23:08:13 +0000 | [diff] [blame] | 239 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 240 | sta_reg = sta_reg | ((unsigned long) addr << 5); /* Phy address */ |
| 241 | memcpy (&sta_reg, &value, 2); /* put in data */ |
| 242 | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 243 | out32 (EMAC_STACR + emac_reg, sta_reg); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 244 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 245 | /* wait for completion */ |
| 246 | i = 0; |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 247 | sta_reg = in32 (EMAC_STACR + emac_reg); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 248 | while ((sta_reg & EMAC_STACR_OC) == 0) { |
| 249 | udelay (7); |
| 250 | if (i > 5) |
| 251 | return -1; |
| 252 | i++; |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 253 | sta_reg = in32 (EMAC_STACR + emac_reg); |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if ((sta_reg & EMAC_STACR_PHYE) != 0) |
| 257 | return -1; |
| 258 | return 0; |
| 259 | |
Stefan Roese | d6c61aa | 2005-08-16 18:18:00 +0200 | [diff] [blame] | 260 | } /* phy_write */ |