]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp/lib/rpp/src/sys/ti_drv_mdio.c
Yet another place to fix
[pes-rpp/rpp-test-sw.git] / rpp / lib / 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     HWREG(baseAddr + MDIO_CONTROL) = (1 << MDIO_HIGHEST_USER_CHANNEL_sh) |
148             MDIO_CTRL_ENABLE_m | (0x60 & MDIO_CTRL_CLKDIV_m);
149 }
150
151 /***************************** End Of File ***********************************/