OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [SuperH_SH7216_Renesas/] [RTOSDemo/] [RenesasCode/] [hwEthernetPhyRTL8201.c] - Blame information for rev 592

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 585 jeremybenn
/******************************************************************************
2
* File Name    : hwEthernetPhy.c
3
* Version      : 1.0
4
* Device(s)    : Renesas
5
* Tool-Chain   : Renesas SH2A V9+
6
* OS           : None
7
* H/W Platform : SH2A
8
* Description  : Hardware driver for the LAN8700 PHY
9
*******************************************************************************
10
* History      : DD.MM.YYYY Ver. Description
11
*              : 01.08.2009 1.00 MAB First Release
12
******************************************************************************/
13
 
14
/******************************************************************************
15
* DISCLAIMER
16
* This software is supplied by Renesas Technology Corp. and is only
17
* intended for use with Renesas products. No other uses are authorized.
18
* This software is owned by Renesas Technology Corp. and is protected under
19
* all applicable laws, including copyright laws.
20
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES
21
* REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY,
22
* INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23
* PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY
24
* DISCLAIMED.
25
* TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
26
* TECHNOLOGY CORP. NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
27
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
28
* FOR ANY REASON RELATED TO THE THIS SOFTWARE, EVEN IF RENESAS OR ITS
29
* AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
30
* Renesas reserves the right, without notice, to make changes to this
31
* software and to discontinue the availability of this software.
32
* By using this software, you agree to the additional terms and
33
* conditions found by accessing the following link:
34
* http://www.renesas.com/disclaimer
35
******************************************************************************/
36
/* Copyright (C) 2008. Renesas Technology Corp.,       All Rights Reserved.  */
37
/* Copyright (C) 2009. Renesas Technology Europe Ltd., All Rights Reserved.  */
38
/*****************************************************************************/
39
 
40
/*****************************************************************************
41
System Includes
42
******************************************************************************/
43
 
44
#include <stdio.h>
45
/* Header file for sleep() and nop() functions */
46
#include <machine.h>
47
 
48
/*****************************************************************************
49
User Includes
50
******************************************************************************/
51
 
52
/* Defines for I/O registers */
53
#include "iodefine.h"
54
/* rsk7216def.h provides common defines for widely used items. */
55
#include "rsk7216def.h"
56
/* Physical layer functions */
57
#include "hwEthernetPhy.h"
58
#include "Trace.h"
59
 
60
#include "FreeRTOS.h"
61
#include "task.h"
62
 
63
/*****************************************************************************
64
Constant Macros
65
******************************************************************************/
66
 
67
/* Preamble */
68
#define PHY_ST                              0x0001
69
/* Operation to be executed on PHY registers */
70
#define PHY_READ                            0x0002
71
#define PHY_WRITE                           0x0001
72
/* Physical address of PHY device */
73
#define PHY_ADDR                            0x001F
74
 
75
/* Description of PHY data registers */
76
#define PHY_BASIC_MODE_CONTROL              0x0000
77
#define PHY_BASIC_MODE_STATUS               0x0001
78
#define PHY_IDENTIFIER1                     0x0002
79
#define PHY_IDENTIFIER2                     0x0003
80
#define PHY_AN_ADVERTISEMENT                0x0004
81
#define PHY_AN_LINK_PARTNER_ABILITY         0x0005
82
 
83
/* Definitions of some configuration bits */
84
#define PHY_RESET                           0x8000
85
#define PHY_AN_ENABLE                       0x1200
86
/* Bits for auto negotiation for 100, 10 half and full duplex set */
87
#define PHY_AN_10_100_F_H                   0xDE1
88
/* Link partner ability register bits for establising the result of the
89
   auto negotiation */
90
#define PHY_AN_100F                         BIT_8
91
#define PHY_AN_100H                         BIT_7
92
#define PHY_AN_10F                          BIT_6
93
#define PHY_AN_10H                          BIT_5
94
 
95
/*****************************************************************************
96
Function Prototypes
97
******************************************************************************/
98
 
99
static USHORT phyReadReg(USHORT usRegAddr);
100
static void phyWriteReg(USHORT usRegAddr, USHORT usData);
101
static void phyPreamble(void);
102
static void phyMiiWrite1(void);
103
static void phyMiiWrite0(void);
104
static void phyRegSet(USHORT usRegAddr, long lOption);
105
static void phyRegRead(PUSHORT pusData);
106
static void phyRegWrite(USHORT usData);
107
static void phyTaZ0(void);
108
static void phyTa10(void);
109
static void phyDelay(void);
110
 
111
/*****************************************************************************
112
Public Functions
113
******************************************************************************/
114
 
115
/*****************************************************************************
116
Function Name:  phyReset
117
Description:    Executes software reset of PHY and sets to auto negotiate link
118
Parameters:     None
119
Return value:   0 for success -1 on error
120
******************************************************************************/
121
int phyReset(void)
122
{
123
        /* One second of attempting to reset the PHY */
124
    int iCount = 1000;
125
    /* Set software reset */
126
    phyWriteReg(PHY_BASIC_MODE_CONTROL, PHY_RESET);
127
    while (iCount--)
128
    {
129
        USHORT  usData;
130
 
131
                vTaskDelay( 2 / portTICK_RATE_MS );
132
 
133
                /* Read the status of the PHY */
134
        usData = phyReadReg(PHY_BASIC_MODE_CONTROL);
135
        /* Wait for the reset flag to be cleared */
136
        if ((usData & PHY_RESET) == 0)
137
        {
138
            /* Set auto negoatiation for 10,100 full and half duplex */
139
            phyWriteReg(PHY_AN_ADVERTISEMENT, PHY_AN_10_100_F_H);
140
            /* Set auto negotiate and restart auto negotiate bits */
141
            phyWriteReg(PHY_BASIC_MODE_CONTROL, PHY_AN_ENABLE);
142
 
143
            /* Auto negotiation will now take place wait for two seconds */
144
            vTaskDelay( 2000 / portTICK_RATE_MS );
145
 
146
                        /* Success */
147
            return 0;
148
        }
149
    }
150
    /* Phy did not respond to software reset */
151
    return -1;
152
}
153
/*****************************************************************************
154
End of function phyReset
155
 ******************************************************************************/
156
 
157
/*****************************************************************************
158
Function Name: phyStatus
159
Description:   Function to reurn the type of physical link
160
Parameters:    none
161
Return value:  The link type
162
*****************************************************************************/
163
NETLNK phyStatus(void)
164
{
165
    /* The state of this flag depens on the hardware connection to the MAC */
166
    if (!EtherC.PSR.BIT.LMON)
167
    {
168
        /* Read the auto negotiation link partner ability register to establish
169
           the type of link */
170
        USHORT  usData = phyReadReg(PHY_AN_LINK_PARTNER_ABILITY);
171
        if (usData & PHY_AN_100F)
172
        {
173
            return PHY_LINK_100F;
174
        }
175
        if (usData & PHY_AN_100H)
176
        {
177
            return PHY_LINK_100H;
178
        }
179
        if (usData & PHY_AN_10F)
180
        {
181
            return PHY_LINK_10F;
182
        }
183
        if (usData & PHY_AN_10H)
184
        {
185
            return PHY_LINK_10H;
186
        }
187
    }
188
    return PHY_NO_LINK;
189
}
190
/*****************************************************************************
191
End of function  phyStatus
192
******************************************************************************/
193
 
194
/*****************************************************************************
195
Private Functions
196
******************************************************************************/
197
 
198
/*****************************************************************************
199
Function Name:  phyReadReg
200
Description:    Reads data from a register with the address usRegAddr
201
Parameters:     (USHORT) usRegAddr - address to be read;
202
Return value:   (USHORT) - value from read register;
203
******************************************************************************/
204
static USHORT phyReadReg(USHORT usRegAddr)
205
{
206
    USHORT  usData;
207
    phyPreamble();
208
    phyRegSet(usRegAddr, PHY_READ);
209
    phyTaZ0();
210
    phyRegRead(&usData);
211
    phyTaZ0();
212
    return usData;
213
}
214
/*****************************************************************************
215
End of function phyReadReg
216
******************************************************************************/
217
 
218
/*****************************************************************************
219
Function Name:  phyWriteReg
220
Description:    Write data to register with the address usRegAddr
221
Parameters:     (USHORT) usRegAddr - address of register where to be written;
222
                (USHORT) usData - value to write;
223
Return value:   None
224
******************************************************************************/
225
static void phyWriteReg(USHORT usRegAddr, USHORT usData)
226
{
227
    phyPreamble();
228
    phyRegSet(usRegAddr, PHY_WRITE);
229
    phyTa10();
230
    phyRegWrite(usData);
231
    phyTaZ0();
232
}
233
/*****************************************************************************
234
End of function phyWriteReg
235
******************************************************************************/
236
 
237
/*****************************************************************************
238
Function Name:  phyPreamble
239
Description:    Writing 32 bits of '1'
240
Parameters:     None
241
Return value:   None
242
******************************************************************************/
243
static void phyPreamble(void)
244
{
245
    int iCount = 32;
246
    while (iCount--)
247
    {
248
        phyMiiWrite1();
249
    }
250
}
251
/*****************************************************************************
252
End of function phyPreamble
253
******************************************************************************/
254
 
255
/*****************************************************************************
256
Function Name:  phyRegSet
257
Description:    Sets the address of register
258
Parameters:     (USHORT) usRegAddr - address to be set;
259
                (long) lOption - PHY_READ or PHY_WRITE;
260
Return value:   None
261
******************************************************************************/
262
static void phyRegSet(USHORT usRegAddr, long lOption)
263
{
264
    int iBit = 14;
265
    USHORT usData;
266
 
267
        /* Format of PHY Address Set Transmission */
268
        /* ST R/W PAddress Address    */
269
        /* 1  10  11111    xxxx    00 */ //Read
270
        /* 1  01  11111    xxxx    00 */ //Write
271
 
272
    usData = 0;
273
    /* ST code */
274
    usData = (PHY_ST << 14);
275
    if (lOption == PHY_READ)
276
    {
277
        /* Option code (RD) */
278
        usData |= (PHY_READ << 12);
279
    }
280
    else
281
    {
282
        /* Option code (WT) */
283
        usData |= (PHY_WRITE << 12);
284
    }
285
    /* PHY Address  */
286
    usData |= ((BYTE)PHY_ADDR << 7);
287
    /* Reg Address  */
288
    usData |= (USHORT)(usRegAddr << 2);
289
 
290
    while (iBit--)
291
    {
292
        if ((usData & 0x8000) == 0)
293
        {
294
            phyMiiWrite0();
295
        }
296
        else
297
        {
298
            phyMiiWrite1();
299
        }
300
        usData <<= 1;
301
    }
302
}
303
/*****************************************************************************
304
End of function phyRegSet
305
******************************************************************************/
306
 
307
/*****************************************************************************
308
Function Name:  phyRegRead
309
Description:    Read data from register
310
Parameters:     IN  pusDest - value to be read;
311
Return value:   None
312
******************************************************************************/
313
static void phyRegRead(PUSHORT pusDest)
314
{
315
    USHORT usData = 0;
316
    int    iBit = 16;
317
    while (iBit--)
318
    {
319
        EtherC.PIR.LONG = 0x00UL;
320
        EtherC.PIR.LONG = 0x01UL;
321
        usData <<= 1;
322
 
323
        /* MDI read */
324
        usData |= (USHORT)((EtherC.PIR.LONG & 0x08UL) >> 3);
325
 
326
        EtherC.PIR.LONG = 0x01UL;
327
        EtherC.PIR.LONG = 0x00UL;
328
    }
329
    *pusDest = usData;
330
}
331
/*****************************************************************************
332
End of function phyRegRead
333
******************************************************************************/
334
 
335
/*****************************************************************************
336
Function Name:  phyRegWrite
337
Description:    Write 2 bytes (16 bit) to MII
338
Parameters:     IN usData - value to be written;
339
Return value:   None
340
******************************************************************************/
341
static void phyRegWrite(USHORT usData)
342
{
343
    int iBit = 16;
344
    while (iBit--)
345
    {
346
        if ((usData & 0x8000) == 0)
347
        {
348
            phyMiiWrite0();
349
        }
350
        else
351
        {
352
            phyMiiWrite1();
353
        }
354
        usData <<= 1;
355
    }
356
}
357
/*****************************************************************************
358
End of function phyRegWrite
359
******************************************************************************/
360
 
361
/*****************************************************************************
362
Function Name:  phyTaZ0
363
Description:    Set bus to high Z
364
Parameters:     None
365
Return value:   None
366
******************************************************************************/
367
static void phyTaZ0(void)
368
{
369
    EtherC.PIR.LONG = 0x00UL;
370
    EtherC.PIR.LONG = 0x01UL;
371
    EtherC.PIR.LONG = 0x01UL;
372
    EtherC.PIR.LONG = 0x00UL;
373
}
374
/*****************************************************************************
375
End of function phyTaZ0
376
******************************************************************************/
377
 
378
/*****************************************************************************
379
Function Name:  phyTa10
380
Description:    Set bus to output
381
Parameters:     None
382
Return value:   None
383
******************************************************************************/
384
static void phyTa10(void)
385
{
386
    EtherC.PIR.LONG = 0x06UL;
387
    EtherC.PIR.LONG = 0x07UL;
388
    EtherC.PIR.LONG = 0x07UL;
389
    EtherC.PIR.LONG = 0x06UL;
390
    EtherC.PIR.LONG = 0x02UL;
391
    EtherC.PIR.LONG = 0x03UL;
392
    EtherC.PIR.LONG = 0x03UL;
393
    EtherC.PIR.LONG = 0x02UL;
394
}
395
/*****************************************************************************
396
End of function phyTa10
397
******************************************************************************/
398
 
399
/*****************************************************************************
400
Function Name:  phyMiiWrite1
401
Description:    Write 1 to MII
402
Parameters:     None
403
Return value:   None
404
******************************************************************************/
405
static void phyMiiWrite1(void)
406
{
407
    EtherC.PIR.LONG = 0x06UL;
408
    EtherC.PIR.LONG = 0x07UL;
409
    EtherC.PIR.LONG = 0x07UL;
410
    EtherC.PIR.LONG = 0x06UL;
411
}
412
/*****************************************************************************
413
End of function phyMiiWrite1
414
******************************************************************************/
415
 
416
/*****************************************************************************
417
Function Name:  phyMiiWrite0
418
Description:    Write 0 to MII
419
Parameters:     None
420
Return value:   None
421
******************************************************************************/
422
static void phyMiiWrite0(void)
423
{
424
    EtherC.PIR.LONG = 0x02UL;
425
    EtherC.PIR.LONG = 0x03UL;
426
    EtherC.PIR.LONG = 0x03UL;
427
    EtherC.PIR.LONG = 0x02UL;
428
}
429
/*****************************************************************************
430
End of function phyMiiWrite0
431
******************************************************************************/
432
 
433
/*****************************************************************************
434
End  Of File
435
******************************************************************************/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.