]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/devs/eth/arm/netarm/v2_0/src/MII.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / devs / eth / arm / netarm / v2_0 / src / MII.c
1 //==========================================================================
2 //
3 //      MII.c
4 //
5 //      NetSilion NET+ARM PHY chip configuration
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 2005 eCosCentric Ltd.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //==========================================================================
39 //#####DESCRIPTIONBEGIN####
40 //
41 // Author(s):           Harald Brandl (harald.brandl@fh-joanneum.at)
42 // Contributors:        Harald Brandl
43 // Date:                01.08.2004
44 // Purpose:             PHY chip configuration
45 // Description:
46 //
47 //####DESCRIPTIONEND####
48 //
49 //==========================================================================
50
51 #include "eth_regs.h"
52
53
54 #define PHYS(_i_) (0x800 | _i_)
55
56 #define SysReg (unsigned *)0xffb00004   // System Status Register
57
58 /*  Function: void cyg_netarm_mii_poll_busy (void)
59  *
60  *  Description:
61  *     This routine is responsible for waiting for the current PHY
62  *     operation to complete.
63  *
64  *  Parameters:
65  *     none
66  */
67
68 static void
69 mii_poll_busy(void)
70 {
71         unsigned reg;
72      /* check to see if PHY is busy with read or write */
73     do
74     {
75                 HAL_READ_UINT32(MIIIR, reg);
76         }while (reg & 1);
77 }
78
79 /*  Function: void cyg_netarm_mii_reset (void)
80  *
81  *  Description:
82  *
83  *       This routine resets the PHY.
84  *
85  *  Return Values:
86  *      none
87  */
88
89 void
90 cyg_netarm_mii_reset(void)
91 {
92      HAL_WRITE_UINT32(MIIAR, PHYS(0));  // select command register
93      HAL_WRITE_UINT32(MIIWDR, 0x8000);  // reset
94      mii_poll_busy();
95 }
96
97 /*  Function: cyg_bool cyg_netarm_mii_negotiate (void)
98  *
99  *  Description:
100  *     This routine is responsible for causing the external Ethernet PHY
101  *     to begin the negotatiation process.
102  *
103  *  Parameters:
104  *     none
105  *
106  *  Return Value:
107  *     0: SUCCESS
108  *     1: ERROR
109  */
110
111 cyg_bool
112 cyg_netarm_mii_negotiate(void)
113 {
114      unsigned timeout = 100000, reg;
115
116      HAL_WRITE_UINT32(MIIAR, PHYS(4));
117
118      mii_poll_busy();
119
120      HAL_WRITE_UINT32(MIIAR, PHYS(0));
121      HAL_OR_UINT32(MIIWDR, 0x1200);
122
123      mii_poll_busy();
124
125      while(timeout)
126      {
127           HAL_WRITE_UINT32(MIIAR, PHYS(1));
128           HAL_WRITE_UINT32(MIICR, 1);
129
130           mii_poll_busy();
131
132           HAL_READ_UINT32(MIIRDR, reg);
133
134           if(0x24 == (reg & 0x24))
135                return 0;
136           else
137                timeout--;
138      }
139
140      return 1;
141 }
142
143
144 /*  Function: void cyg_netarm_mii_set_speed (cyg_bool speed, cyg_bool duplex)
145  *
146  *  Description:
147  *
148  *       This routine will set the speed and duplex of the external PHY.
149  *
150  *  Parameters:
151  *      Speed
152  *        0: 10Mbit
153  *        1: 100Mbit
154  *      Duplex
155  *        0: Half
156  *        1: Full
157  *
158  *  Return Values:
159  *      none
160  */
161
162 void
163 cyg_netarm_mii_set_speed(cyg_uint32 speed, cyg_bool duplex)
164 {
165      unsigned timeout = 1000000, reg;
166
167      HAL_WRITE_UINT32(MIIAR, PHYS(0));  // select command register
168      HAL_WRITE_UINT32(MIIWDR, (speed << 13) | (duplex << 8));   // set speed and duplex
169      mii_poll_busy();
170
171      while(timeout)
172      {
173           HAL_WRITE_UINT32(MIIAR, PHYS(1));     // select status register
174           HAL_WRITE_UINT32(MIICR, 1);
175           mii_poll_busy();
176           HAL_READ_UINT32(MIIRDR, reg);
177           if(reg & 0x4)
178                break;
179           timeout--;
180      }
181 }
182
183 /*  Function: cyg_bool cyg_netarm_mii_check_speed
184  *
185  *  Description:
186  *
187  *       This routine will check the operating speed of the ethernet
188  *       interface.
189  *
190  *  Parameters:
191  *      none
192  *
193  *  Return Values:
194  *      0: 10Mbit Speed
195  *      1: 100Mbit Speed
196  */
197
198 cyg_uint32
199 cyg_netarm_mii_check_speed(void)
200 {
201         unsigned reg;
202
203      HAL_WRITE_UINT32(MIIAR, PHYS(17));
204      HAL_WRITE_UINT32(MIICR, 1);
205      mii_poll_busy();
206      HAL_READ_UINT32(MIIRDR, reg);
207      return (reg >> 14) & 1;
208 }
209
210 /*  Function: void cyg_netarm_mii_check_duplex
211  *
212  *  Description:
213  *
214  *       This routine will check the operating duplex of the ethernet
215  *       interface.
216  *
217  *  Parameters:
218  *      none
219  *
220  *  Return Values:
221  *      0: Half Duplex
222  *      1: Full Duplex
223  */
224
225 cyg_bool
226 cyg_netarm_mii_check_duplex(void)
227 {
228         unsigned reg;
229
230      HAL_WRITE_UINT32(MIIAR, PHYS(17));
231      HAL_WRITE_UINT32(MIICR, 1);
232      mii_poll_busy();
233      HAL_READ_UINT32(MIIRDR, reg);
234      return (reg >> 9) & 1;
235 }