* Patch by Stefan Roese, 06 Apr 2005:
[platform/kernel/u-boot.git] / cpu / ppc4xx / miiphy.c
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
51 #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
52   (defined(CONFIG_440) && !defined(CONFIG_NET_MULTI))
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
79 /***********************************************************/
80 /* read a phy reg and return the value with a rc           */
81 /* Note: We are referencing to EMAC_STACR register         */
82 /* @(EMAC_BASE + 92) because  of:                          */
83 /* - 405EP has only STACR for EMAC0 pinned out             */
84 /* - 405GP has onle one EMAC0                              */
85 /* - For 440 this module gets compiled only for            */
86 /*   !CONFIG_NET_MULTI, i.e. only EMAC0 is supported.      */
87 /***********************************************************/
88
89 int miiphy_read (unsigned char addr, unsigned char reg,
90                                  unsigned short *value)
91 {
92         unsigned long sta_reg;          /* STA scratch area */
93         unsigned long i;
94
95         /* see if it is ready for 1000 nsec */
96         i = 0;
97
98         /* see if it is ready for  sec */
99         while ((in32 (EMAC_STACR) & EMAC_STACR_OC) == 0) {
100                 udelay (7);
101                 if (i > 5) {
102 #if 0   /* test-only */
103                         printf ("read err 1\n");
104 #endif
105                         return -1;
106                 }
107                 i++;
108         }
109         sta_reg = reg;                          /* reg address */
110         /* set clock (50Mhz) and read flags */
111         sta_reg = (sta_reg | EMAC_STACR_READ) & ~EMAC_STACR_CLK_100MHZ;
112 #ifdef CONFIG_PHY_CLK_FREQ
113         sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ;
114 #endif
115         sta_reg = sta_reg | (addr << 5);        /* Phy address */
116
117         out32 (EMAC_STACR, sta_reg);
118 #if 0   /* test-only */
119         printf ("a2: write: EMAC_STACR=0x%0x\n", sta_reg);      /* test-only */
120 #endif
121
122 #ifdef CONFIG_PHY_CMD_DELAY
123         udelay (CONFIG_PHY_CMD_DELAY);          /* Intel LXT971A needs this */
124 #endif
125         sta_reg = in32 (EMAC_STACR);
126         i = 0;
127         while ((sta_reg & EMAC_STACR_OC) == 0) {
128                 udelay (7);
129                 if (i > 5) {
130 #if 0   /* test-only */
131                         printf ("read err 2\n");
132 #endif
133                         return -1;
134                 }
135                 i++;
136                 sta_reg = in32 (EMAC_STACR);
137         }
138         if ((sta_reg & EMAC_STACR_PHYE) != 0) {
139 #if 0   /* test-only */
140                 printf ("read err 3\n");
141                 printf ("a2: read: EMAC_STACR=0x%0lx, i=%d\n",
142                         sta_reg, (int) i);      /* test-only */
143 #endif
144                 return -1;
145         }
146
147         *value = *(short *) (&sta_reg);
148         return 0;
149
150
151 } /* phy_read */
152
153
154 /***********************************************************/
155 /* write a phy reg and return the value with a rc           */
156 /***********************************************************/
157
158 int miiphy_write (unsigned char addr, unsigned char reg,
159                   unsigned short value)
160 {
161         unsigned long sta_reg;          /* STA scratch area */
162         unsigned long i;
163
164         /* see if it is ready for 1000 nsec */
165         i = 0;
166
167         while ((in32 (EMAC_STACR) & EMAC_STACR_OC) == 0) {
168                 if (i > 5)
169                         return -1;
170                 udelay (7);
171                 i++;
172         }
173         sta_reg = 0;
174         sta_reg = reg;                          /* reg address */
175         /* set clock (50Mhz) and read flags */
176         sta_reg = (sta_reg | EMAC_STACR_WRITE) & ~EMAC_STACR_CLK_100MHZ;
177 #ifdef CONFIG_PHY_CLK_FREQ
178         sta_reg = sta_reg | CONFIG_PHY_CLK_FREQ; /* Set clock frequency (PLB freq. dependend) */
179 #endif
180         sta_reg = sta_reg | ((unsigned long) addr << 5);        /* Phy address */
181         memcpy (&sta_reg, &value, 2);   /* put in data */
182
183         out32 (EMAC_STACR, sta_reg);
184
185 #ifdef CONFIG_PHY_CMD_DELAY
186         udelay (CONFIG_PHY_CMD_DELAY);          /* Intel LXT971A needs this */
187 #endif
188         /* wait for completion */
189         i = 0;
190         sta_reg = in32 (EMAC_STACR);
191         while ((sta_reg & EMAC_STACR_OC) == 0) {
192                 udelay (7);
193                 if (i > 5)
194                         return -1;
195                 i++;
196                 sta_reg = in32 (EMAC_STACR);
197         }
198
199         if ((sta_reg & EMAC_STACR_PHYE) != 0)
200                 return -1;
201         return 0;
202
203 } /* phy_read */
204
205 #endif  /* CONFIG_405GP */