]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/devs/eth/powerpc/adder/v2_0/src/adder_eth.c
Initial revision
[karo-tx-redboot.git] / packages / devs / eth / powerpc / adder / v2_0 / src / adder_eth.c
1 //==========================================================================
2 //
3 //      adder_eth.c
4 //
5 //      Ethernet device driver specifics for Analogue & Micro Adder (PPC850)
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 // Copyright (C) 2002 Gary Thomas
13 //
14 // eCos is free software; you can redistribute it and/or modify it under
15 // the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 or (at your option) any later version.
17 //
18 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21 // for more details.
22 //
23 // You should have received a copy of the GNU General Public License along
24 // with eCos; if not, write to the Free Software Foundation, Inc.,
25 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 //
27 // As a special exception, if other files instantiate templates or use macros
28 // or inline functions from this file, or you compile this file and link it
29 // with other works to produce a work based on this file, this file does not
30 // by itself cause the resulting work to be covered by the GNU General Public
31 // License. However the source code for this file must still be made available
32 // in accordance with section (3) of the GNU General Public License.
33 //
34 // This exception does not invalidate any other reasons why a work based on
35 // this file might be covered by the GNU General Public License.
36 //
37 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38 // at http://sources.redhat.com/ecos/ecos-license/
39 // -------------------------------------------
40 //####ECOSGPLCOPYRIGHTEND####
41 //####BSDCOPYRIGHTBEGIN####
42 //
43 // -------------------------------------------
44 //
45 // Portions of this software may have been derived from OpenBSD or other sources,
46 // and are covered by the appropriate copyright disclaimers included herein.
47 //
48 // -------------------------------------------
49 //
50 //####BSDCOPYRIGHTEND####
51 //==========================================================================
52 //#####DESCRIPTIONBEGIN####
53 //
54 // Author(s):    gthomas
55 // Contributors: gthomas
56 // Date:         2002-11-25
57 // Purpose:      
58 // Description:  platform driver specifics for A&M Adder
59 //              
60 //
61 //####DESCRIPTIONEND####
62 //
63 //==========================================================================
64
65 // Ethernet device driver support for PHY on Adder/MPC850
66
67 #include <pkgconf/system.h>
68 #include <cyg/infra/cyg_type.h>
69 #include <cyg/infra/diag.h>
70
71 #include <cyg/hal/hal_arch.h>
72 #include <cyg/hal/hal_cache.h>
73 #include <cyg/hal/hal_if.h>
74 #include <cyg/hal/drv_api.h>
75
76 #include CYGDAT_DEVS_QUICC_ETH_INL  // Platform specifics
77 #include <cyg/hal/quicc/ppc8xx.h>                  // QUICC structure definitions
78
79 // MII interface
80 #define MII_Start            0x40000000
81 #define MII_Read             0x20000000
82 #define MII_Write            0x10000000
83 #define MII_Cmd              0x30000000
84 #define MII_Phy(phy)         (phy << 23)
85 #define MII_Reg(reg)         (reg << 18)
86 #define MII_TA               0x00020000
87
88 // Transceiver mode
89 #define PHY_BMCR             0x00    // Register number
90 #define PHY_BMCR_RESET       0x8000
91 #define PHY_BMCR_LOOPBACK    0x4000
92 #define PHY_BMCR_100MB       0x2000
93 #define PHY_BMCR_AUTO_NEG    0x1000
94 #define PHY_BMCR_POWER_DOWN  0x0800
95 #define PHY_BMCR_ISOLATE     0x0400
96 #define PHY_BMCR_RESTART     0x0200
97 #define PHY_BMCR_FULL_DUPLEX 0x0100
98 #define PHY_BMCR_COLL_TEST   0x0080
99
100 #define PHY_BMSR             0x01    // Status register
101 #define PHY_BMSR_AUTO_NEG    0x0020  
102 #define PHY_BMSR_LINK        0x0004
103
104 // Bits in port D - used for 2 wire MII interface
105 #define MII_DATA             0x1000
106 #define MII_CLOCK            0x0800
107
108 #define MII_SET_DATA(val)                      \
109     if (val) {                                 \
110         eppc->pio_pddat |= MII_DATA;           \
111     } else {                                   \
112         eppc->pio_pddat &= ~MII_DATA;          \
113     }
114
115 #define MII_GET_DATA()                         \
116     ((eppc->pio_pddat & MII_DATA) != 0)
117
118 #define MII_SET_CLOCK(val)                     \
119     if (val) {                                 \
120         eppc->pio_pddat |= MII_CLOCK;          \
121     } else {                                   \
122         eppc->pio_pddat &= ~MII_CLOCK;         \
123     }
124
125 static cyg_uint32
126 phy_cmd(cyg_uint32 cmd)
127 {
128     volatile EPPC *eppc = (volatile EPPC *)eppc_base();
129     cyg_uint32  retval;
130     int         i, off;
131     bool        is_read = ((cmd & MII_Cmd) == MII_Read);
132
133     // Set both bits as output
134     eppc->pio_pddir |= MII_DATA | MII_CLOCK;
135
136     // Preamble
137     for (i = 0; i < 32; i++) {
138         MII_SET_CLOCK(0);
139         MII_SET_DATA(1);
140         CYGACC_CALL_IF_DELAY_US(1);
141         MII_SET_CLOCK(1);
142         CYGACC_CALL_IF_DELAY_US(1);
143     }
144
145     // Command/data
146     for (i = 0, off = 31; i < (is_read ? 14 : 32); i++, --off) {
147         MII_SET_CLOCK(0);
148         MII_SET_DATA((cmd >> off) & 0x00000001);
149         CYGACC_CALL_IF_DELAY_US(1);
150         MII_SET_CLOCK(1);
151         CYGACC_CALL_IF_DELAY_US(1);
152     }
153
154     retval = cmd;
155
156     // If read, fetch data register
157     if (is_read) {
158         retval >>= 16;
159
160         MII_SET_CLOCK(0);
161         eppc->pio_pddir &= ~MII_DATA;  // Data bit is now input
162         CYGACC_CALL_IF_DELAY_US(1);
163         MII_SET_CLOCK(1);
164         CYGACC_CALL_IF_DELAY_US(1);
165         MII_SET_CLOCK(0);
166         CYGACC_CALL_IF_DELAY_US(1);
167
168         for (i = 0, off = 15; i < 16; i++, off--) {
169             MII_SET_CLOCK(1);
170             retval <<= 1;
171             retval |= MII_GET_DATA();
172             CYGACC_CALL_IF_DELAY_US(1);
173             MII_SET_CLOCK(0);
174             CYGACC_CALL_IF_DELAY_US(1);
175         }
176     }
177
178     // Set both bits as output
179     eppc->pio_pddir |= MII_DATA | MII_CLOCK;
180
181     // Postamble
182     for (i = 0; i < 32; i++) {
183         MII_SET_CLOCK(0);
184         MII_SET_DATA(1);
185         CYGACC_CALL_IF_DELAY_US(1);
186         MII_SET_CLOCK(1);
187         CYGACC_CALL_IF_DELAY_US(1);
188     }
189
190     return retval;
191 }
192
193 //
194 // PHY unit access (via MII channel)
195 //
196 static void
197 phy_write(int reg, int addr, unsigned short data)
198 {
199     phy_cmd(MII_Start | MII_Write | MII_Phy(addr) | MII_Reg(reg) | MII_TA | data);
200 }
201
202 static bool
203 phy_read(int reg, int addr, unsigned short *val)
204 {
205     cyg_uint32 ret;
206
207     ret = phy_cmd(MII_Start | MII_Read | MII_Phy(addr) | MII_Reg(reg) | MII_TA);
208     *val = ret;
209     return true;
210 }
211
212 bool
213 _adder_reset_phy(void)
214 {
215     volatile EPPC *eppc = (volatile EPPC *)eppc_base();
216     int phy_timeout = 5*1000;  // Wait 5 seconds max for link to clear
217     bool phy_ok;
218     unsigned short phy_state = 0;
219     int phy_unit = -1;
220     int i;
221
222     // Reset PHY (transceiver)
223     eppc->pip_pbdat &= ~0x00004000;  // Reset PHY chip
224     CYGACC_CALL_IF_DELAY_US(15000);  // > 10ms
225     eppc->pip_pbdat |= 0x00004000;   // Enable PHY chip
226
227     phy_ok = false;
228     
229     // Try and discover how this PHY is wired
230     for (i = 0; i < 0x20; i++) {
231         phy_read(PHY_BMCR, i, &phy_state);
232         if ((phy_state & PHY_BMCR_RESET) == 0) {
233             phy_unit = i;
234             break;
235         }
236     }
237     if (phy_unit < 0) {
238         diag_printf("QUICC ETH - Can't locate PHY\n");
239         return false;
240     } else {
241 #if 0
242         diag_printf("QUICC ETH - using PHY %d\n", phy_unit);
243 #endif
244     }
245     if (phy_read(PHY_BMSR, phy_unit, &phy_state)) {
246         if ((phy_state & PHY_BMSR_LINK) !=  PHY_BMSR_LINK) {
247             unsigned short reset_mode;
248             phy_write(PHY_BMCR, phy_unit, PHY_BMCR_RESET);
249             for (i = 0;  i < 10;  i++) {
250                 phy_ok = phy_read(PHY_BMCR, phy_unit, &phy_state);
251                 if (!phy_ok) break;
252                 if (!(phy_state & PHY_BMCR_RESET)) break;
253             }
254             if (!phy_ok || (phy_state & PHY_BMCR_RESET)) {
255                 diag_printf("QUICC/ETH: Can't get PHY unit to soft reset: %x\n", phy_state);
256                 return false;
257             }
258             reset_mode = PHY_BMCR_RESTART | PHY_BMCR_AUTO_NEG | PHY_BMCR_FULL_DUPLEX;
259             phy_write(PHY_BMCR, phy_unit, reset_mode);
260             while (phy_timeout-- >= 0) {
261                 phy_ok = phy_read(PHY_BMSR, phy_unit, &phy_state);
262                 if (phy_ok && (phy_state & PHY_BMSR_LINK)) {
263                     break;
264                 } else {
265                     CYGACC_CALL_IF_DELAY_US(10000);   // 10ms
266                 }
267             }
268             if (phy_timeout <= 0) {
269                 diag_printf("** QUICC/ETH Warning: PHY LINK UP failed\n");
270             }
271         }
272         else {
273             diag_printf("** QUICC/ETH Info: PHY LINK already UP \n");
274         }
275     }
276
277     return phy_ok;
278 }
279