1 //==========================================================================
5 // Ethernet device driver specifics for Analogue & Micro Adder (PPC850)
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
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.
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
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.
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.
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.
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####
43 // -------------------------------------------
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.
48 // -------------------------------------------
50 //####BSDCOPYRIGHTEND####
51 //==========================================================================
52 //#####DESCRIPTIONBEGIN####
55 // Contributors: gthomas
58 // Description: platform driver specifics for A&M Adder
61 //####DESCRIPTIONEND####
63 //==========================================================================
65 // Ethernet device driver support for PHY on Adder/MPC850
67 #include <pkgconf/system.h>
68 #include <cyg/infra/cyg_type.h>
69 #include <cyg/infra/diag.h>
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>
76 #include CYGDAT_DEVS_QUICC_ETH_INL // Platform specifics
77 #include <cyg/hal/quicc/ppc8xx.h> // QUICC structure definitions
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
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
100 #define PHY_BMSR 0x01 // Status register
101 #define PHY_BMSR_AUTO_NEG 0x0020
102 #define PHY_BMSR_LINK 0x0004
104 // Bits in port D - used for 2 wire MII interface
105 #define MII_DATA 0x1000
106 #define MII_CLOCK 0x0800
108 #define MII_SET_DATA(val) \
110 eppc->pio_pddat |= MII_DATA; \
112 eppc->pio_pddat &= ~MII_DATA; \
115 #define MII_GET_DATA() \
116 ((eppc->pio_pddat & MII_DATA) != 0)
118 #define MII_SET_CLOCK(val) \
120 eppc->pio_pddat |= MII_CLOCK; \
122 eppc->pio_pddat &= ~MII_CLOCK; \
126 phy_cmd(cyg_uint32 cmd)
128 volatile EPPC *eppc = (volatile EPPC *)eppc_base();
131 bool is_read = ((cmd & MII_Cmd) == MII_Read);
133 // Set both bits as output
134 eppc->pio_pddir |= MII_DATA | MII_CLOCK;
137 for (i = 0; i < 32; i++) {
140 CYGACC_CALL_IF_DELAY_US(1);
142 CYGACC_CALL_IF_DELAY_US(1);
146 for (i = 0, off = 31; i < (is_read ? 14 : 32); i++, --off) {
148 MII_SET_DATA((cmd >> off) & 0x00000001);
149 CYGACC_CALL_IF_DELAY_US(1);
151 CYGACC_CALL_IF_DELAY_US(1);
156 // If read, fetch data register
161 eppc->pio_pddir &= ~MII_DATA; // Data bit is now input
162 CYGACC_CALL_IF_DELAY_US(1);
164 CYGACC_CALL_IF_DELAY_US(1);
166 CYGACC_CALL_IF_DELAY_US(1);
168 for (i = 0, off = 15; i < 16; i++, off--) {
171 retval |= MII_GET_DATA();
172 CYGACC_CALL_IF_DELAY_US(1);
174 CYGACC_CALL_IF_DELAY_US(1);
178 // Set both bits as output
179 eppc->pio_pddir |= MII_DATA | MII_CLOCK;
182 for (i = 0; i < 32; i++) {
185 CYGACC_CALL_IF_DELAY_US(1);
187 CYGACC_CALL_IF_DELAY_US(1);
194 // PHY unit access (via MII channel)
197 phy_write(int reg, int addr, unsigned short data)
199 phy_cmd(MII_Start | MII_Write | MII_Phy(addr) | MII_Reg(reg) | MII_TA | data);
203 phy_read(int reg, int addr, unsigned short *val)
207 ret = phy_cmd(MII_Start | MII_Read | MII_Phy(addr) | MII_Reg(reg) | MII_TA);
213 _adder_reset_phy(void)
215 volatile EPPC *eppc = (volatile EPPC *)eppc_base();
216 int phy_timeout = 5*1000; // Wait 5 seconds max for link to clear
218 unsigned short phy_state = 0;
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
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) {
238 diag_printf("QUICC ETH - Can't locate PHY\n");
242 diag_printf("QUICC ETH - using PHY %d\n", phy_unit);
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);
252 if (!(phy_state & PHY_BMCR_RESET)) break;
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);
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)) {
265 CYGACC_CALL_IF_DELAY_US(10000); // 10ms
268 if (phy_timeout <= 0) {
269 diag_printf("** QUICC/ETH Warning: PHY LINK UP failed\n");
273 diag_printf("** QUICC/ETH Info: PHY LINK already UP \n");