wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 1 | /*-----------------------------------------------------------------------------+ |
| 2 | | |
| 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. |
| 9 | | |
| 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. |
| 13 | | |
| 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. |
| 17 | | |
| 18 | | COPYRIGHT I B M CORPORATION 1995 |
| 19 | | LICENSED MATERIAL - PROGRAM PROPERTY OF I B M |
| 20 | +-----------------------------------------------------------------------------*/ |
| 21 | /*-----------------------------------------------------------------------------+ |
| 22 | | |
| 23 | | File Name: miiphy.c |
| 24 | | |
| 25 | | Function: This module has utilities for accessing the MII PHY through |
| 26 | | the EMAC3 macro. |
| 27 | | |
| 28 | | Author: Mark Wisner |
| 29 | | |
| 30 | | Change Activity- |
| 31 | | |
| 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 | | |
| 41 | +-----------------------------------------------------------------------------*/ |
| 42 | |
| 43 | #include <common.h> |
| 44 | #include <asm/processor.h> |
| 45 | #include <ppc_asm.tmpl> |
| 46 | #include <commproc.h> |
| 47 | #include <405gp_enet.h> |
| 48 | #include <405_mal.h> |
| 49 | #include <miiphy.h> |
| 50 | |
stroese | b867d70 | 2003-05-23 11:18:02 +0000 | [diff] [blame] | 51 | #if defined(CONFIG_405GP) || defined(CONFIG_440) || defined(CONFIG_405EP) |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | /***********************************************************/ |
| 55 | /* Dump out to the screen PHY regs */ |
| 56 | /***********************************************************/ |
| 57 | |
| 58 | void miiphy_dump (unsigned char addr) |
| 59 | { |
| 60 | unsigned long i; |
| 61 | unsigned short data; |
| 62 | |
| 63 | |
| 64 | for (i = 0; i < 0x1A; i++) { |
| 65 | if (miiphy_read (addr, i, &data)) { |
| 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 | |
| 75 | } /* end for loop */ |
| 76 | } /* end dump */ |
| 77 | |
| 78 | |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 79 | /***********************************************************/ |
| 80 | /* read a phy reg and return the value with a rc */ |
| 81 | /***********************************************************/ |
| 82 | |
| 83 | int miiphy_read (unsigned char addr, unsigned char reg, |
| 84 | unsigned short *value) |
| 85 | { |
| 86 | unsigned long sta_reg; /* STA scratch area */ |
| 87 | unsigned long i; |
| 88 | |
| 89 | /* see if it is ready for 1000 nsec */ |
| 90 | i = 0; |
| 91 | |
| 92 | /* see if it is ready for sec */ |
| 93 | while ((in32 (EMAC_STACR) & EMAC_STACR_OC) == 0) { |
| 94 | udelay (7); |
| 95 | if (i > 5) { |
| 96 | printf ("read err 1\n"); |
| 97 | return -1; |
| 98 | } |
| 99 | i++; |
| 100 | } |
| 101 | sta_reg = reg; /* reg address */ |
| 102 | /* set clock (50Mhz) and read flags */ |
| 103 | sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ; |
wdenk | 093ae27 | 2003-09-02 23:08:13 +0000 | [diff] [blame] | 104 | #ifdef CONFIG_PHY_CLK_FREQ |
wdenk | 12f3424 | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 105 | sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; |
wdenk | 093ae27 | 2003-09-02 23:08:13 +0000 | [diff] [blame] | 106 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 107 | sta_reg = sta_reg | (addr << 5); /* Phy address */ |
| 108 | |
| 109 | out32 (EMAC_STACR, sta_reg); |
| 110 | #if 0 /* test-only */ |
| 111 | printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg); /* test-only */ |
| 112 | #endif |
| 113 | |
| 114 | sta_reg = in32 (EMAC_STACR); |
| 115 | i = 0; |
| 116 | while ((sta_reg & EMAC_STACR_OC) == 0) { |
| 117 | udelay (7); |
| 118 | if (i > 5) { |
| 119 | printf ("read err 2\n"); |
| 120 | return -1; |
| 121 | } |
| 122 | i++; |
| 123 | sta_reg = in32 (EMAC_STACR); |
| 124 | } |
| 125 | if ((sta_reg & EMAC_STACR_PHYE) != 0) { |
| 126 | printf ("read err 3\n"); |
| 127 | printf ("a2: read: EMAC_STACR=0x%0lx, i=%d\n", |
| 128 | sta_reg, (int) i); /* test-only */ |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | *value = *(short *) (&sta_reg); |
| 133 | return 0; |
| 134 | |
| 135 | |
| 136 | } /* phy_read */ |
| 137 | |
| 138 | |
| 139 | /***********************************************************/ |
| 140 | /* write a phy reg and return the value with a rc */ |
| 141 | /***********************************************************/ |
| 142 | |
| 143 | int miiphy_write (unsigned char addr, unsigned char reg, |
| 144 | unsigned short value) |
| 145 | { |
| 146 | unsigned long sta_reg; /* STA scratch area */ |
| 147 | unsigned long i; |
| 148 | |
| 149 | /* see if it is ready for 1000 nsec */ |
| 150 | i = 0; |
| 151 | |
| 152 | while ((in32 (EMAC_STACR) & EMAC_STACR_OC) == 0) { |
| 153 | if (i > 5) |
| 154 | return -1; |
| 155 | udelay (7); |
| 156 | i++; |
| 157 | } |
| 158 | sta_reg = 0; |
| 159 | sta_reg = reg; /* reg address */ |
| 160 | /* set clock (50Mhz) and read flags */ |
| 161 | sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ; |
wdenk | 093ae27 | 2003-09-02 23:08:13 +0000 | [diff] [blame] | 162 | #ifdef CONFIG_PHY_CLK_FREQ |
wdenk | 12f3424 | 2003-09-02 22:48:03 +0000 | [diff] [blame] | 163 | 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] | 164 | #endif |
wdenk | affae2b | 2002-08-17 09:36:01 +0000 | [diff] [blame] | 165 | sta_reg = sta_reg | ((unsigned long) addr << 5); /* Phy address */ |
| 166 | memcpy (&sta_reg, &value, 2); /* put in data */ |
| 167 | |
| 168 | out32 (EMAC_STACR, sta_reg); |
| 169 | |
| 170 | /* wait for completion */ |
| 171 | i = 0; |
| 172 | sta_reg = in32 (EMAC_STACR); |
| 173 | while ((sta_reg & EMAC_STACR_OC) == 0) { |
| 174 | udelay (7); |
| 175 | if (i > 5) |
| 176 | return -1; |
| 177 | i++; |
| 178 | sta_reg = in32 (EMAC_STACR); |
| 179 | } |
| 180 | |
| 181 | if ((sta_reg & EMAC_STACR_PHYE) != 0) |
| 182 | return -1; |
| 183 | return 0; |
| 184 | |
| 185 | } /* phy_read */ |
| 186 | |
| 187 | #endif /* CONFIG_405GP */ |