]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/sys/ti_drv_mdio.c
Set MDIO initial sequence
[pes-rpp/rpp-lib.git] / rpp / src / sys / ti_drv_mdio.c
1 /**
2  *  \file   mdio.c
3  *
4  *  \brief  MDIO APIs.
5  *
6  *   This file contains the device abstraction layer APIs for MDIO.
7  */
8
9 /* Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/
10  * ALL RIGHTS RESERVED
11  */
12
13
14 #include "base.h"
15 #include "sys/hw_reg_access.h"
16 #include "sys/ti_drv_mdio.h"
17 #include "sys/hw_mdio.h"
18
19 /*******************************************************************************
20 *                       INTERNAL MACRO DEFINITIONS
21 *******************************************************************************/
22 #define PHY_REG_MASK                             (0x1Fu)
23 #define PHY_ADDR_MASK                            (0x1Fu)
24 #define PHY_DATA_MASK                            (0xFFFFu)
25 #define PHY_REG_SHIFT                            (21u)
26 #define PHY_ADDR_SHIFT                           (16u)
27
28 /*******************************************************************************
29 *                        API FUNCTION DEFINITIONS
30 *******************************************************************************/
31
32 /**
33  * \brief   Reads a PHY register using MDIO.
34  *
35  * \param   baseAddr      Base Address of the MDIO Module Registers.
36  * \param   phyAddr       PHY Adress.
37  * \param   regNum        Register Number to be read.
38  * \param   dataPtr       Pointer where the read value shall be written.
39  *
40  * \return  status of the read \n
41  *          TRUE - read is successful.\n
42  *          FALSE - read is not acknowledged properly.
43  *
44  **/
45 unsigned int MDIOPhyRegRead(unsigned int baseAddr, unsigned int phyAddr,
46                             unsigned int regNum, volatile unsigned short *dataPtr)
47 {
48     /* Wait till transaction completion if any */
49     while(HWREG(baseAddr + MDIO_USERACCESS0) & MDIO_USERACCESS0_GO);
50
51     HWREG(baseAddr + MDIO_USERACCESS0)
52                            = (MDIO_USERACCESS0_READ | MDIO_USERACCESS0_GO
53                               |((regNum & PHY_REG_MASK) << PHY_REG_SHIFT)
54                               |((phyAddr & PHY_ADDR_MASK) << PHY_ADDR_SHIFT));
55
56     /* wait for command completion */
57     while(HWREG(baseAddr + MDIO_USERACCESS0) & MDIO_USERACCESS0_GO);
58
59     /* Store the data if the read is acknowledged */
60     if((HWREG(baseAddr + MDIO_USERACCESS0)) & MDIO_USERACCESS0_ACK)
61     {
62         *dataPtr = (unsigned short)((HWREG(baseAddr + MDIO_USERACCESS0))
63                                     & PHY_DATA_MASK);
64         return TRUE;
65     }
66
67     return FALSE;
68 }
69
70 /**
71  * \brief   Writes a PHY register using MDIO.
72  *
73  * \param   baseAddr      Base Address of the MDIO Module Registers.
74  * \param   phyAddr       PHY Adress.
75  * \param   regNum        Register Number to be read.
76  * \param   RegVal        Value to be written.
77  *
78  * \return  None
79  *
80  **/
81 void MDIOPhyRegWrite(unsigned int baseAddr, unsigned int phyAddr,
82                      unsigned int regNum, unsigned short RegVal)
83 {
84     /* Wait till transaction completion if any */
85     while(HWREG(baseAddr + MDIO_USERACCESS0) & MDIO_USERACCESS0_GO);
86
87     HWREG(baseAddr + MDIO_USERACCESS0) =
88             (MDIO_USERACCESS0_WRITE
89             | MDIO_USERACCESS0_GO
90             |((regNum & PHY_REG_MASK) << PHY_REG_SHIFT)
91             |((phyAddr & PHY_ADDR_MASK) << PHY_ADDR_SHIFT)
92             | RegVal);
93
94     /* wait for command completion*/
95     while(HWREG(baseAddr + MDIO_USERACCESS0) & MDIO_USERACCESS0_GO);
96 }
97 /**
98  * \brief   Reads the alive status of all PHY connected to this MDIO.
99  *          The bit correponding to the PHY address will be set if the PHY
100  *          is alive.
101  *
102  * \param   baseAddr      Base Address of the MDIO Module Registers.
103  *
104  * \return  MDIO alive register state
105  *
106  **/
107 unsigned int MDIOPhyAliveStatusGet(unsigned int baseAddr)
108 {
109     return (HWREG(baseAddr + MDIO_ALIVE));
110 }
111
112 /**
113  * \brief   Reads the link status of all PHY connected to this MDIO.
114  *          The bit correponding to the PHY address will be set if the PHY
115  *          link is active.
116  *
117  * \param   baseAddr      Base Address of the MDIO Module Registers.
118  *
119  * \return  MDIO link register state
120  *
121  **/
122 unsigned int MDIOPhyLinkStatusGet(unsigned int baseAddr)
123 {
124     return (HWREG(baseAddr + MDIO_LINK));
125 }
126
127 /**
128  * \brief   Initializes the MDIO peripheral. This enables the MDIO state
129  *          machine, uses standard pre-amble and set the clock divider value.
130  *
131  * \param   baseAddr       Base Address of the MDIO Module Registers.
132  * \param   mdioInputFreq  The clock input to the MDIO module
133  * \param   mdioOutputFreq The clock output required on the MDIO bus
134  * \return  None
135  *
136  **/
137 #define MDIO_CTRL_ENABLE_m          (1 << 30)
138 #define MDIO_CTRL_CLKDIV_m          (0xff)
139 #define MDIO_HIGHEST_USER_CHANNEL_m     (0xf)
140 #define MDIO_HIGHEST_USER_CHANNEL_sh        24
141
142 void MDIOInit(unsigned int baseAddr, unsigned int mdioInputFreq,
143               unsigned int mdioOutputFreq)
144 {
145    //HWREG(baseAddr + MDIO_CONTROL) = 0x41000020u;
146
147 #ifdef TARGET_TMS570_RPP
148     HWREG(baseAddr + MDIO_CONTROL) = (1 << MDIO_HIGHEST_USER_CHANNEL_sh) |
149             MDIO_CTRL_ENABLE_m | (0x60 & MDIO_CTRL_CLKDIV_m);
150 #else
151     /*Unlock the IOMM Register*/
152     *(int *) 0xFFFFEA38  = 0x83E70B13;  /* kicker 0 register, unlock CPU write access to PINMMR registers */
153     *(int *) 0xFFFFEA3C  = 0x95A4F1E0;  /* kicker 1 register, */
154
155     *(int *) 0xFFFFEB2C  = 0x00000400;
156     *(int *) 0xFFFFEB30  = 0x00000400;
157
158     *(int *) 0xFFFFEB38  &= 0xFFFFFF00; //P10[1]  //Mux 10 Rx_ER
159     *(int *) 0xFFFFEB38  |= (1 << 1);   //P10[1]  //Mux 10 Rx_ER
160
161     *(int *) 0xFFFFEB3C  &= 0x00FFFFFF; //P11[26]   //Mux 11 Rx[0]
162     *(int *) 0xFFFFEB3C  |= (1 << 26);  //P11[26]   //Mux 11 Rx[0]
163
164     *(int *) 0xFFFFEB40  &= 0x0000FF00;//P12[1,18,26]    //Mux 12 Rx[3],Rx[2],Rx[1]
165     *(int *) 0xFFFFEB40  |= ((1<<26) | (1<<18) | (1<<1));//P12[1,18,26]    //Mux 12 Rx[3],Rx[2],Rx[1]
166
167     *(int *) 0xFFFFEB44  &= 0x00000000;//P13[2, 10, 26,18]   //Mux 13 Tx[2],TxEn,Tx[1],Tx[0]
168     *(int *) 0xFFFFEB44  |= ((1<<26)|(1<<18)|(1<<10)|(1<<2)); //P13[2, 10, 26,18]   //Mux 13 Tx[2],TxEn,Tx[1],Tx[0]
169
170     *(int *) 0xFFFFEB48  &= 0xFFFF0000; //P14[9,2,11]   //Mux 14 Tx[3],RxClk
171     *(int *) 0xFFFFEB48  |= ((1<<9)|(1<<2));    //P14[9,2]   //Mux 14 Tx[3],RxClk
172
173     *(int *) 0xFFFFEB54  &= 0xFF00FF00      ;//P17[17,1,3]   //Mux 17 CRS,TxClk
174     *(int *) 0xFFFFEB54  |= ((1<<17)|(1<<1));          //P17[17,1]   //Mux 17 CRS,TxClk
175
176     *(int *) 0xFFFFEB5C  &= 0xFFFF00FF;  //P19[9]   //Mux 19 RxDV
177     *(int *) 0xFFFFEB5C  |= (1<<9);      //P19[9]   //Mux 19 RxDV
178
179     *(int *) 0xFFFFEB60  &= 0xFF00FFFF;  //P20[18]   //Mux 20 COL
180     *(int *) 0xFFFFEB60  |= (1<<18);     //P20[18]   //Mux 20 COL
181
182
183     *(int *) 0xFFFFEB84  &= 0x00FFFFFF;//P29[24]  //Mux 29 MII Select pin (24 bit - 0(MII),1(RMII))
184     *(int *) 0xFFFFEB84  |= (0<<24);   //P29[24]  //Mux 29 MII Select pin (24 bit - 0(MII),1(RMII))
185
186
187     /*lock the IOMM Register*/
188     *(int *) 0xFFFFEA38  = 0x00000000;  /* kicker 0 register, lock CPU write access to PINMMR registers */
189     *(int *) 0xFFFFEA3C  = 0x00000000;  /* kicker 1 register, */
190
191     HWREG(baseAddr + MDIO_CONTROL) = 0x41000020u;
192 #endif
193 }
194
195 /***************************** End Of File ***********************************/