]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/sys/phy_dp83848h.c
Add content to release
[pes-rpp/rpp-lib.git] / rpp / src / sys / phy_dp83848h.c
1 /*
2  * File : sys/phy_dp83848h.c
3  * Abstract:
4  *     PHY driver implementation.
5  *
6  * References:
7  *
8  *     for details see documentation on http://www.ti.com/product/DP83848H
9  */
10
11 #include "sys/ti_drv_mdio.h"
12 #include "sys/phy_dp83848h.h"
13
14 #ifndef TRUE
15 /**
16  * Boolean definition for TRUE
17  */
18 #define TRUE 1
19 #endif
20
21
22 #ifndef FALSE
23 /**
24  * Boolean definition for FALSE
25  */
26 #define FALSE 0
27 #endif
28
29 void PHY_reset(unsigned int mdioBaseAddr, unsigned int phyAddr)
30 {
31         volatile unsigned short regContent;
32         MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_BMCR, PHY_RESET_m);
33         while(MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMCR, &regContent) & PHY_RESET_m);
34 }
35
36 unsigned int PHY_partner_ability_get(unsigned int mdioBaseAddr, unsigned int phyAddr, unsigned short *regContent)
37 {
38         return (MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_ANLPAR, regContent));
39 }
40
41 unsigned int PHY_start_auto_negotiate(unsigned int mdioBaseAddr, unsigned int phyAddr, unsigned short advVal)
42 {
43         volatile unsigned short regContent = 0;
44
45         /* Enable Auto Negotiation */
46         if(MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMCR, &regContent) != TRUE)
47         {
48                 return FALSE;
49         }
50            regContent |= PHY_AUTONEG_EN_m;
51         MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_BMCR, regContent); /* originally ...HY_BMCR, PHY_RESET_m | PHY_AUTONEG_EN_m); */
52
53         /* Write Auto Negotiation capabilities */
54         if(MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_ANAR, &regContent) != TRUE)
55         {
56                 return FALSE;
57         }
58         regContent |= advVal;
59         MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_ANAR, regContent);
60
61         /* Start Auto Negotiation */
62         MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMCR, &regContent);
63         regContent |= PHY_AUTONEG_REST;
64         MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_BMCR, regContent);
65
66     return TRUE; /* request to PHY through EMAC for autonegotiation established */
67 }
68
69 unsigned int PHY_is_done_auto_negotiate(unsigned int mdioBaseAddr, unsigned int phyAddr)
70 {
71         volatile unsigned short regContent;
72
73         if(MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMSR, &regContent) != TRUE)
74         {
75                 return FALSE;
76         }
77         if((regContent & PHY_A_NEG_COMPLETE_m) == 0)return FALSE;
78         return TRUE;
79 }
80
81 unsigned int PHY_auto_negotiate(unsigned int mdioBaseAddr, unsigned int phyAddr, unsigned short advVal)
82 {
83         if(PHY_start_auto_negotiate(mdioBaseAddr, phyAddr, advVal) == FALSE) return FALSE;
84
85         while(PHY_is_done_auto_negotiate(mdioBaseAddr, phyAddr) == FALSE);
86
87         return TRUE;
88
89
90         /* original function body */
91 //      volatile unsigned short regContent = 0;
92
93         /* Enable Auto Negotiation */
94 //      if(MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMCR, &regContent) != TRUE)
95 //      {
96 //              return FALSE;
97 //      }
98 //    regContent |= PHY_AUTONEG_EN_m;
99 //      MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_BMCR, regContent); /* originally ...HY_BMCR, PHY_RESET_m | PHY_AUTONEG_EN_m); */
100
101         /* Write Auto Negotiation capabilities */
102 //      if(MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_ANAR, &regContent) != TRUE)
103 //      {
104 //              return FALSE;
105 //      }
106 //      regContent |= advVal;
107 //      MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_ANAR, regContent);
108
109         /* Start Auto Negotiation */
110 //      MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMCR, &regContent);
111 //      regContent |= PHY_AUTONEG_REST;
112 //      MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_BMCR, regContent);
113
114         /* Get the auto negotiation status*/
115         /* Wait till auto negotiation is complete */
116         /* Blocking if cable not connected */
117 //      do {
118 //              if(MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMSR, &regContent) != TRUE)
119 //              {
120 //                      return FALSE;
121 //              }
122
123 //      } while ((regContent & PHY_A_NEG_COMPLETE_m) == 0);
124
125 //      return TRUE;
126 }
127
128 unsigned int PHY_link_status_get(unsigned int mdioBaseAddr, unsigned int phyAddr, volatile unsigned int retries)
129 {
130     volatile unsigned short linkStatus;
131     volatile unsigned int retVal = TRUE;
132
133     while (retVal == TRUE)
134     {
135         /* Read the BSR of the PHY */
136         MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMSR, &linkStatus);
137
138         if(linkStatus & PHY_LINK_STATUS_m)
139         {
140             break;
141         }
142         else
143         {
144             (retries != 0) ? retries-- : (retVal = FALSE);
145         }
146     }
147
148     return retVal;
149 }
150
151 unsigned int PHY_RMII_mode_get(unsigned int mdioBaseAddr, unsigned int phyAddr)
152 {
153         volatile unsigned short regContent;
154     /* Read the RBR of the PHY */
155     MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_RBR, &regContent);
156     return (regContent & PHY_RMII_MODE);
157 }
158
159 void PHY_MII_mode_set(unsigned int mdioBaseAddr, unsigned int phyAddr, unsigned int mode)
160 {
161         volatile unsigned short regContent;
162     /* Read the RBR of the PHY */
163         MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_RBR, &regContent);
164         /* Write the RBR of the PHY */
165         regContent &= 0x1f;
166         regContent |= ( mode << 5 );
167         MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_RBR, regContent );
168 }
169
170 void PHY_Power_Down(unsigned int mdioBaseAddr, unsigned int phyAddr)
171 {
172         volatile unsigned short regContent;
173         MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMCR, &regContent);
174         MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_BMCR, regContent | PHY_POWERDOWN_m);
175 }
176
177 void PHY_Power_Up(unsigned int mdioBaseAddr, unsigned int phyAddr)
178 {
179         volatile unsigned short regContent;
180         MDIOPhyRegRead(mdioBaseAddr, phyAddr, PHY_BMCR, &regContent);
181         MDIOPhyRegWrite(mdioBaseAddr, phyAddr, PHY_BMCR, regContent & ~PHY_POWERDOWN_m);
182 }