1 |
585 |
jeremybenn |
/******************************************************************************
|
2 |
|
|
* DISCLAIMER
|
3 |
|
|
|
4 |
|
|
* This software is supplied by Renesas Technology Corp. and is only
|
5 |
|
|
* intended for use with Renesas products. No other uses are authorized.
|
6 |
|
|
|
7 |
|
|
* This software is owned by Renesas Technology Corp. and is protected under
|
8 |
|
|
* all applicable laws, including copyright laws.
|
9 |
|
|
|
10 |
|
|
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES
|
11 |
|
|
* REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY,
|
12 |
|
|
* INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
13 |
|
|
* PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY
|
14 |
|
|
* DISCLAIMED.
|
15 |
|
|
|
16 |
|
|
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
|
17 |
|
|
* TECHNOLOGY CORP. NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
|
18 |
|
|
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
|
19 |
|
|
* FOR ANY REASON RELATED TO THE THIS SOFTWARE, EVEN IF RENESAS OR ITS
|
20 |
|
|
* AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
21 |
|
|
|
22 |
|
|
* Renesas reserves the right, without notice, to make changes to this
|
23 |
|
|
* software and to discontinue the availability of this software.
|
24 |
|
|
* By using this software, you agree to the additional terms and
|
25 |
|
|
* conditions found by accessing the following link:
|
26 |
|
|
* http://www.renesas.com/disclaimer
|
27 |
|
|
******************************************************************************
|
28 |
|
|
* Copyright (C) 2008. Renesas Technology Corp., All Rights Reserved.
|
29 |
|
|
*******************************************************************************
|
30 |
|
|
* File Name : phy.c
|
31 |
|
|
* Version : 1.01
|
32 |
|
|
* Description : Ethernet PHY device driver
|
33 |
|
|
******************************************************************************
|
34 |
|
|
* History : DD.MM.YYYY Version Description
|
35 |
|
|
* : 15.02.2010 1.00 First Release
|
36 |
|
|
* : 06.04.2010 1.01 RX62N changes
|
37 |
|
|
******************************************************************************/
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
/******************************************************************************
|
41 |
|
|
Includes <System Includes> , "Project Includes"
|
42 |
|
|
******************************************************************************/
|
43 |
|
|
#include <iorx62n.h>
|
44 |
|
|
#include "r_ether.h"
|
45 |
|
|
#include "phy.h"
|
46 |
|
|
|
47 |
|
|
#include "FreeRTOS.h"
|
48 |
|
|
#include "task.h"
|
49 |
|
|
/******************************************************************************
|
50 |
|
|
Typedef definitions
|
51 |
|
|
******************************************************************************/
|
52 |
|
|
|
53 |
|
|
/******************************************************************************
|
54 |
|
|
Macro definitions
|
55 |
|
|
******************************************************************************/
|
56 |
|
|
|
57 |
|
|
/******************************************************************************
|
58 |
|
|
Imported global variables and functions (from other files)
|
59 |
|
|
******************************************************************************/
|
60 |
|
|
|
61 |
|
|
/******************************************************************************
|
62 |
|
|
Exported global variables and functions (to be accessed by other files)
|
63 |
|
|
******************************************************************************/
|
64 |
|
|
|
65 |
|
|
/******************************************************************************
|
66 |
|
|
Private global variables and functions
|
67 |
|
|
******************************************************************************/
|
68 |
|
|
uint16_t _phy_read( uint16_t reg_addr );
|
69 |
|
|
void _phy_write( uint16_t reg_addr, uint16_t data );
|
70 |
|
|
void _phy_preamble( void );
|
71 |
|
|
void _phy_reg_set( uint16_t reg_addr, int32_t option );
|
72 |
|
|
void _phy_reg_read( uint16_t *data );
|
73 |
|
|
void _phy_reg_write( uint16_t data );
|
74 |
|
|
void _phy_ta_z0( void );
|
75 |
|
|
void _phy_ta_10( void );
|
76 |
|
|
void _phy_mii_write_1( void );
|
77 |
|
|
void _phy_mii_write_0( void );
|
78 |
|
|
|
79 |
|
|
/**
|
80 |
|
|
* External functions
|
81 |
|
|
*/
|
82 |
|
|
|
83 |
|
|
/******************************************************************************
|
84 |
|
|
* Function Name: phy_init
|
85 |
|
|
* Description : Resets Ethernet PHY device
|
86 |
|
|
* Arguments : none
|
87 |
|
|
* Return Value : none
|
88 |
|
|
******************************************************************************/
|
89 |
|
|
int16_t phy_init( void )
|
90 |
|
|
{
|
91 |
|
|
uint16_t reg;
|
92 |
|
|
uint32_t count;
|
93 |
|
|
|
94 |
|
|
/* Reset PHY */
|
95 |
|
|
_phy_write(BASIC_MODE_CONTROL_REG, 0x8000);
|
96 |
|
|
|
97 |
|
|
count = 0;
|
98 |
|
|
|
99 |
|
|
do
|
100 |
|
|
{
|
101 |
|
|
vTaskDelay( 2 / portTICK_RATE_MS );
|
102 |
|
|
reg = _phy_read(BASIC_MODE_CONTROL_REG);
|
103 |
|
|
count++;
|
104 |
|
|
} while (reg & 0x8000 && count < PHY_RESET_WAIT);
|
105 |
|
|
|
106 |
|
|
if( count < PHY_RESET_WAIT )
|
107 |
|
|
{
|
108 |
|
|
return R_PHY_OK;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
return R_PHY_ERROR;
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
/******************************************************************************
|
115 |
|
|
* Function Name: phy_set_100full
|
116 |
|
|
* Description : Set Ethernet PHY device to 100 Mbps full duplex
|
117 |
|
|
* Arguments : none
|
118 |
|
|
* Return Value : none
|
119 |
|
|
******************************************************************************/
|
120 |
|
|
void phy_set_100full( void )
|
121 |
|
|
{
|
122 |
|
|
_phy_write(BASIC_MODE_CONTROL_REG, 0x2100);
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/******************************************************************************
|
126 |
|
|
* Function Name: phy_set_10half
|
127 |
|
|
* Description : Sets Ethernet PHY device to 10 Mbps half duplexR
|
128 |
|
|
* Arguments : none
|
129 |
|
|
* Return Value : none
|
130 |
|
|
******************************************************************************/
|
131 |
|
|
void phy_set_10half( void )
|
132 |
|
|
{
|
133 |
|
|
_phy_write(BASIC_MODE_CONTROL_REG, 0x0000);
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
/******************************************************************************
|
137 |
|
|
* Function Name: phy_set_autonegotiate
|
138 |
|
|
* Description : Starts autonegotiate and reports the other side's
|
139 |
|
|
* : physical capability
|
140 |
|
|
* Arguments : none
|
141 |
|
|
* Return Value : bit 8 - Full duplex 100 mbps
|
142 |
|
|
* : bit 7 - Half duplex 100 mbps
|
143 |
|
|
* : bit 6 - Full duplex 10 mbps
|
144 |
|
|
* : bit 5 - Half duplex 10 mbps
|
145 |
|
|
* : bit 4:0 - Always set to 00001 (IEEE 802.3)
|
146 |
|
|
* : -1 if error
|
147 |
|
|
******************************************************************************/
|
148 |
|
|
int16_t phy_set_autonegotiate( void )
|
149 |
|
|
{
|
150 |
|
|
uint16_t reg;
|
151 |
|
|
uint32_t count;
|
152 |
|
|
|
153 |
|
|
_phy_write(AN_ADVERTISEMENT_REG, 0x01E1);
|
154 |
|
|
_phy_write(BASIC_MODE_CONTROL_REG, 0x1200);
|
155 |
|
|
|
156 |
|
|
count = 0;
|
157 |
|
|
|
158 |
|
|
do
|
159 |
|
|
{
|
160 |
|
|
reg = _phy_read(BASIC_MODE_STATUS_REG);
|
161 |
|
|
count++;
|
162 |
|
|
vTaskDelay( 100 / portTICK_RATE_MS );
|
163 |
|
|
|
164 |
|
|
/* Make sure we don't break out if reg just contains 0xffff. */
|
165 |
|
|
if( reg == 0xffff )
|
166 |
|
|
{
|
167 |
|
|
reg = 0;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
} while (!(reg & 0x0020) && (count < PHY_AUTO_NEGOTIATON_WAIT));
|
171 |
|
|
|
172 |
|
|
if (count >= PHY_AUTO_NEGOTIATON_WAIT)
|
173 |
|
|
{
|
174 |
|
|
return R_PHY_ERROR;
|
175 |
|
|
}
|
176 |
|
|
else
|
177 |
|
|
{
|
178 |
|
|
/* Get the link partner response */
|
179 |
|
|
reg = (int16_t)_phy_read(AN_LINK_PARTNER_ABILITY_REG);
|
180 |
|
|
|
181 |
|
|
if (reg & ( 1 << 8 ) )
|
182 |
|
|
{
|
183 |
|
|
return PHY_LINK_100F;
|
184 |
|
|
}
|
185 |
|
|
if (reg & ( 1 << 7 ) )
|
186 |
|
|
{
|
187 |
|
|
return PHY_LINK_100H;
|
188 |
|
|
}
|
189 |
|
|
if (reg & ( 1 << 6 ) )
|
190 |
|
|
{
|
191 |
|
|
return PHY_LINK_10F;
|
192 |
|
|
}
|
193 |
|
|
if (reg & 1 << 5 )
|
194 |
|
|
{
|
195 |
|
|
return PHY_LINK_10H;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
return (-1);
|
199 |
|
|
}
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
/**
|
204 |
|
|
* Internal functions
|
205 |
|
|
*/
|
206 |
|
|
|
207 |
|
|
/******************************************************************************
|
208 |
|
|
* Function Name: _phy_read
|
209 |
|
|
* Description : Reads a PHY register
|
210 |
|
|
* Arguments : reg_addr - address of the PHY register
|
211 |
|
|
* Return Value : read value
|
212 |
|
|
******************************************************************************/
|
213 |
|
|
uint16_t _phy_read( uint16_t reg_addr )
|
214 |
|
|
{
|
215 |
|
|
uint16_t data;
|
216 |
|
|
|
217 |
|
|
_phy_preamble();
|
218 |
|
|
_phy_reg_set( reg_addr, PHY_READ );
|
219 |
|
|
_phy_ta_z0();
|
220 |
|
|
_phy_reg_read( &data );
|
221 |
|
|
_phy_ta_z0();
|
222 |
|
|
|
223 |
|
|
return( data );
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
/******************************************************************************
|
227 |
|
|
* Function Name: _phy_write
|
228 |
|
|
* Description : Writes to a PHY register
|
229 |
|
|
* Arguments : reg_addr - address of the PHY register
|
230 |
|
|
* : data - value
|
231 |
|
|
* Return Value : none
|
232 |
|
|
******************************************************************************/
|
233 |
|
|
void _phy_write( uint16_t reg_addr, uint16_t data )
|
234 |
|
|
{
|
235 |
|
|
_phy_preamble();
|
236 |
|
|
_phy_reg_set( reg_addr, PHY_WRITE );
|
237 |
|
|
_phy_ta_10();
|
238 |
|
|
_phy_reg_write( data );
|
239 |
|
|
_phy_ta_z0();
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
/******************************************************************************
|
243 |
|
|
* Function Name: _phy_preamble
|
244 |
|
|
* Description : As preliminary preparation for access to the PHY module register,
|
245 |
|
|
* "1" is output via the MII management interface.
|
246 |
|
|
* Arguments : none
|
247 |
|
|
* Return Value : none
|
248 |
|
|
******************************************************************************/
|
249 |
|
|
void _phy_preamble( void )
|
250 |
|
|
{
|
251 |
|
|
int16_t i;
|
252 |
|
|
|
253 |
|
|
i = 32;
|
254 |
|
|
while( i > 0 )
|
255 |
|
|
{
|
256 |
|
|
_phy_mii_write_1();
|
257 |
|
|
i--;
|
258 |
|
|
}
|
259 |
|
|
}
|
260 |
|
|
|
261 |
|
|
/******************************************************************************
|
262 |
|
|
* Function Name: _phy_reg_set
|
263 |
|
|
* Description : Sets a PHY device to read or write mode
|
264 |
|
|
* Arguments : reg_addr - address of the PHY register
|
265 |
|
|
* : option - mode
|
266 |
|
|
* Return Value : none
|
267 |
|
|
******************************************************************************/
|
268 |
|
|
void _phy_reg_set( uint16_t reg_addr, int32_t option )
|
269 |
|
|
{
|
270 |
|
|
int32_t i;
|
271 |
|
|
uint16_t data;
|
272 |
|
|
|
273 |
|
|
data = 0;
|
274 |
|
|
data = (PHY_ST << 14); /* ST code */
|
275 |
|
|
|
276 |
|
|
if( option == PHY_READ )
|
277 |
|
|
{
|
278 |
|
|
data |= (PHY_READ << 12); /* OP code(RD) */
|
279 |
|
|
}
|
280 |
|
|
else
|
281 |
|
|
{
|
282 |
|
|
data |= (PHY_WRITE << 12); /* OP code(WT) */
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
data |= (PHY_ADDR << 7); /* PHY Address */
|
286 |
|
|
data |= (reg_addr << 2); /* Reg Address */
|
287 |
|
|
|
288 |
|
|
i = 14;
|
289 |
|
|
while( i > 0 )
|
290 |
|
|
{
|
291 |
|
|
if( (data & 0x8000) == 0 )
|
292 |
|
|
{
|
293 |
|
|
_phy_mii_write_0();
|
294 |
|
|
}
|
295 |
|
|
else
|
296 |
|
|
{
|
297 |
|
|
_phy_mii_write_1();
|
298 |
|
|
}
|
299 |
|
|
data <<= 1;
|
300 |
|
|
i--;
|
301 |
|
|
}
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
/******************************************************************************
|
305 |
|
|
* Function Name: _phy_reg_read
|
306 |
|
|
* Description : Reads PHY register through MII interface
|
307 |
|
|
* Arguments : data - pointer to store the data read
|
308 |
|
|
* Return Value : none
|
309 |
|
|
******************************************************************************/
|
310 |
|
|
void _phy_reg_read( uint16_t *data )
|
311 |
|
|
{
|
312 |
|
|
int32_t i, j;
|
313 |
|
|
uint16_t reg_data;
|
314 |
|
|
|
315 |
|
|
reg_data = 0;
|
316 |
|
|
i = 16;
|
317 |
|
|
while( i > 0 )
|
318 |
|
|
{
|
319 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
320 |
|
|
{
|
321 |
|
|
ETHERC.PIR.LONG = 0x00000000;
|
322 |
|
|
}
|
323 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
324 |
|
|
{
|
325 |
|
|
ETHERC.PIR.LONG = 0x00000001;
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
reg_data <<= 1;
|
329 |
|
|
reg_data |= (uint16_t)((ETHERC.PIR.LONG & 0x00000008) >> 3); /* MDI read */
|
330 |
|
|
|
331 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
332 |
|
|
{
|
333 |
|
|
ETHERC.PIR.LONG = 0x00000001;
|
334 |
|
|
}
|
335 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
336 |
|
|
{
|
337 |
|
|
ETHERC.PIR.LONG = 0x00000000;
|
338 |
|
|
}
|
339 |
|
|
i--;
|
340 |
|
|
}
|
341 |
|
|
*data = reg_data;
|
342 |
|
|
}
|
343 |
|
|
|
344 |
|
|
/******************************************************************************
|
345 |
|
|
* Function Name: _phy_reg_write
|
346 |
|
|
* Description : Writes to PHY register through MII interface
|
347 |
|
|
* Arguments : data - value to write
|
348 |
|
|
* Return Value : none
|
349 |
|
|
******************************************************************************/
|
350 |
|
|
void _phy_reg_write( uint16_t data )
|
351 |
|
|
{
|
352 |
|
|
int32_t i;
|
353 |
|
|
|
354 |
|
|
i = 16;
|
355 |
|
|
while( i > 0 )
|
356 |
|
|
{
|
357 |
|
|
if( (data & 0x8000) == 0 )
|
358 |
|
|
{
|
359 |
|
|
_phy_mii_write_0();
|
360 |
|
|
}
|
361 |
|
|
else
|
362 |
|
|
{
|
363 |
|
|
_phy_mii_write_1();
|
364 |
|
|
}
|
365 |
|
|
i--;
|
366 |
|
|
data <<= 1;
|
367 |
|
|
}
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
/******************************************************************************
|
371 |
|
|
* Function Name: _phy_ta_z0
|
372 |
|
|
* Description : Performs bus release so that PHY can drive data
|
373 |
|
|
* : for read operation
|
374 |
|
|
* Arguments : none
|
375 |
|
|
* Return Value : none
|
376 |
|
|
******************************************************************************/
|
377 |
|
|
void _phy_ta_z0( void )
|
378 |
|
|
{
|
379 |
|
|
int32_t j;
|
380 |
|
|
|
381 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
382 |
|
|
{
|
383 |
|
|
ETHERC.PIR.LONG = 0x00000000;
|
384 |
|
|
}
|
385 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
386 |
|
|
{
|
387 |
|
|
ETHERC.PIR.LONG = 0x00000001;
|
388 |
|
|
}
|
389 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
390 |
|
|
{
|
391 |
|
|
ETHERC.PIR.LONG = 0x00000001;
|
392 |
|
|
}
|
393 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
394 |
|
|
{
|
395 |
|
|
ETHERC.PIR.LONG = 0x00000000;
|
396 |
|
|
}
|
397 |
|
|
}
|
398 |
|
|
|
399 |
|
|
/******************************************************************************
|
400 |
|
|
* Function Name: _phy_ta_10
|
401 |
|
|
* Description : Switches data bus so MII interface can drive data
|
402 |
|
|
* : for write operation
|
403 |
|
|
* Arguments : none
|
404 |
|
|
* Return Value : none
|
405 |
|
|
******************************************************************************/
|
406 |
|
|
void _phy_ta_10(void)
|
407 |
|
|
{
|
408 |
|
|
_phy_mii_write_1();
|
409 |
|
|
_phy_mii_write_0();
|
410 |
|
|
}
|
411 |
|
|
|
412 |
|
|
/******************************************************************************
|
413 |
|
|
* Function Name: _phy_mii_write_1
|
414 |
|
|
* Description : Outputs 1 to the MII interface
|
415 |
|
|
* Arguments : none
|
416 |
|
|
* Return Value : none
|
417 |
|
|
******************************************************************************/
|
418 |
|
|
void _phy_mii_write_1( void )
|
419 |
|
|
{
|
420 |
|
|
int32_t j;
|
421 |
|
|
|
422 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
423 |
|
|
{
|
424 |
|
|
ETHERC.PIR.LONG = 0x00000006;
|
425 |
|
|
}
|
426 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
427 |
|
|
{
|
428 |
|
|
ETHERC.PIR.LONG = 0x00000007;
|
429 |
|
|
}
|
430 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
431 |
|
|
{
|
432 |
|
|
ETHERC.PIR.LONG = 0x00000007;
|
433 |
|
|
}
|
434 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
435 |
|
|
{
|
436 |
|
|
ETHERC.PIR.LONG = 0x00000006;
|
437 |
|
|
}
|
438 |
|
|
}
|
439 |
|
|
|
440 |
|
|
/******************************************************************************
|
441 |
|
|
* Function Name: _phy_mii_write_0
|
442 |
|
|
* Description : Outputs 0 to the MII interface
|
443 |
|
|
* Arguments : none
|
444 |
|
|
* Return Value : none
|
445 |
|
|
******************************************************************************/
|
446 |
|
|
void _phy_mii_write_0( void )
|
447 |
|
|
{
|
448 |
|
|
int32_t j;
|
449 |
|
|
|
450 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
451 |
|
|
{
|
452 |
|
|
ETHERC.PIR.LONG = 0x00000002;
|
453 |
|
|
}
|
454 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
455 |
|
|
{
|
456 |
|
|
ETHERC.PIR.LONG = 0x00000003;
|
457 |
|
|
}
|
458 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
459 |
|
|
{
|
460 |
|
|
ETHERC.PIR.LONG = 0x00000003;
|
461 |
|
|
}
|
462 |
|
|
for(j = MDC_WAIT; j > 0; j--)
|
463 |
|
|
{
|
464 |
|
|
ETHERC.PIR.LONG = 0x00000002;
|
465 |
|
|
}
|
466 |
|
|
}
|
467 |
|
|
|
468 |
|
|
|