11#include <stdio.h>
22#include "app_phy.h"
33
4- #define PHY_BASE_ADDR 0x7
4+ #define PHY_BASE_ADDR 0x7
55
66#define PHY_REG_CONTROL 0x0
77#define PHY_REG_STATUS 0x1
88#define PHY_REG_ANE 0x6
99#define PHY_REG_SPEC_STATUS 0x11
10- #define PHY_REG_EXTEND_STATUS 0x1B
10+ #define PHY_REG_EXTEND_STATUS 0x1B
1111
12- #define PHY_BIT_CONTROL_RESET 0x8000 /*!< Control reg : reset */
13- #define PHY_BIT_CONTROL_ANEN 0x1000 /*!< Control reg : auto-negotiation enable */
14- #define PHY_BIT_CONTROL_RSAN 0x0200 /*!< Control reg : auto-negotiation restart */
12+ #define PHY_BIT_CONTROL_RESET 0x8000 /*!< Control reg : reset */
13+ #define PHY_BIT_CONTROL_ANEN 0x1000 /*!< Control reg : auto-negotiation enable */
14+ #define PHY_BIT_CONTROL_RSAN 0x0200 /*!< Control reg : auto-negotiation restart */
1515
16- #define PHY_BIT_STATUS_ANC 0x0020 /*!< Status reg : auto-negotiation complete */
17- #define PHY_BIT_STATUS_LINK 0x0004 /*!< Status reg : link is up */
16+ #define PHY_BIT_STATUS_ANC 0x0020 /*!< Status reg : auto-negotiation complete */
17+ #define PHY_BIT_STATUS_LINK 0x0004 /*!< Status reg : link is up */
1818
19- #define PHY_BIT_ANE_LPAN 0x0001 /*!< ANE reg : link partner can auto-neg */
19+ #define PHY_BIT_ANE_LPAN 0x0001 /*!< ANE reg : link partner can auto-neg */
2020
2121#define PHY_BIT_SPEED 0xC000 /*!< specific status reg : speed */
2222#define PHY_BIT_DUPLEX 0x2000 /*!< specific status reg : duplex */
2525#define PHY_BIT_AUTO_MEDIA_REG_DISABLE 0x0200 /*!< extended status reg : auto media register select disable */
2626
2727void phy_Reset () {
28- ETH_PhyWrite (PHY_BASE_ADDR , PHY_REG_CONTROL , PHY_BIT_CONTROL_RESET );
29-
30- while (1 ) {
31- uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_CONTROL );
32- if ((ret & PHY_BIT_CONTROL_RESET ) == 0 ) {
33- break ;
34- }
35- }
28+ ETH_PhyWrite (PHY_BASE_ADDR , PHY_REG_CONTROL , PHY_BIT_CONTROL_RESET );
29+
30+ while (1 ) {
31+ uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_CONTROL );
32+ if ((ret & PHY_BIT_CONTROL_RESET ) == 0 ) {
33+ break ;
34+ }
35+ }
3636}
3737
3838void phy_AutoMediaSelect () {
39- uint32_t data ;
39+ uint32_t data ;
4040
41- // auto media and auto media register selection
42- data = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_EXTEND_STATUS );
43- data &= ~PHY_BIT_AUTO_MEDIA_DISABLE ;
44- data &= ~PHY_BIT_AUTO_MEDIA_REG_DISABLE ;
45- ETH_PhyWrite (PHY_BASE_ADDR , PHY_REG_EXTEND_STATUS , data );
41+ // auto media and auto media register selection
42+ data = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_EXTEND_STATUS );
43+ data &= ~PHY_BIT_AUTO_MEDIA_DISABLE ;
44+ data &= ~PHY_BIT_AUTO_MEDIA_REG_DISABLE ;
45+ ETH_PhyWrite (PHY_BASE_ADDR , PHY_REG_EXTEND_STATUS , data );
4646}
4747
4848void phy_AutoNeg ()
@@ -65,61 +65,61 @@ void phy_AutoNeg()
6565}
6666
6767BOOL phy_IsLink () {
68- uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_STATUS );
69- return (ret & PHY_BIT_STATUS_LINK ) ? TRUE : FALSE;
68+ uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_STATUS );
69+ return (ret & PHY_BIT_STATUS_LINK ) ? TRUE : FALSE;
7070}
7171
7272BOOL phy_PartnerCanAutoNeg () {
73- uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_ANE );
74- return (ret & PHY_BIT_ANE_LPAN ) ? TRUE : FALSE;
73+ uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_ANE );
74+ return (ret & PHY_BIT_ANE_LPAN ) ? TRUE : FALSE;
7575}
7676
7777uint32_t phy_GetSpeed () {
78- uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_SPEC_STATUS );
79- return ((ret & PHY_BIT_SPEED ) >> 14 );
78+ uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_SPEC_STATUS );
79+ return ((ret & PHY_BIT_SPEED ) >> 14 );
8080}
8181
8282uint32_t phy_GetDuplex () {
83- uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_SPEC_STATUS );
84- return ((ret & PHY_BIT_DUPLEX ) >> 13 );
83+ uint32_t ret = ETH_PhyRead (PHY_BASE_ADDR , PHY_REG_SPEC_STATUS );
84+ return ((ret & PHY_BIT_DUPLEX ) >> 13 );
8585}
8686
8787BOOL phy_Init () {
88- phy_AutoMediaSelect ();
89- phy_AutoNeg ();
90-
91- if (!phy_PartnerCanAutoNeg ()) {
92- printf ("Warning:: PHY's partner can't do auto-negotiation\n" );
93- }
94-
95- if (!phy_IsLink ()) {
96- printf ("link is down\n" );
97- return FALSE;
98- }
99-
100- {
101- uint32_t speed = phy_GetSpeed ();
102- if (speed == PHY_SPEED_10 ) {
103- speed = 10 ;
104- } else if (speed == PHY_SPEED_100 ) {
105- speed = 100 ;
106- } else if (speed == PHY_SPEED_1000 ) {
107- speed = 1000 ;
108- }
109-
110- printf ("PHY runs in %dM speed %s duplex\n" ,
111- speed , (phy_GetDuplex () == PHY_DUPLEX_HALF ) ? "half" : "full" );
112- }
113-
114- // After auto-negcioation, Mawell PHY need some
115- // time to initial itself.
116- // So we have to delay some time since different
117- // connection way, such as direct wire, hub, switch.
118- // If not to delay, the first several sent frame
119- // may be lost.
120- // Please according to actual environment to tune
121- // this delay.
122- udelay (200000 );
123-
124- return TRUE;
88+ phy_AutoMediaSelect ();
89+ phy_AutoNeg ();
90+
91+ if (!phy_PartnerCanAutoNeg ()) {
92+ printf ("Warning:: PHY's partner can't do auto-negotiation\n" );
93+ }
94+
95+ if (!phy_IsLink ()) {
96+ printf ("link is down\n" );
97+ return FALSE;
98+ }
99+
100+ {
101+ uint32_t speed = phy_GetSpeed ();
102+ if (speed == PHY_SPEED_10 ) {
103+ speed = 10 ;
104+ } else if (speed == PHY_SPEED_100 ) {
105+ speed = 100 ;
106+ } else if (speed == PHY_SPEED_1000 ) {
107+ speed = 1000 ;
108+ }
109+
110+ printf ("PHY runs in %dM speed %s duplex\n" ,
111+ speed , (phy_GetDuplex () == PHY_DUPLEX_HALF ) ? "half" : "full" );
112+ }
113+
114+ // After auto-negcioation, Mawell PHY need some
115+ // time to initial itself.
116+ // So we have to delay some time since different
117+ // connection way, such as direct wire, hub, switch.
118+ // If not to delay, the first several sent frame
119+ // may be lost.
120+ // Please according to actual environment to tune
121+ // this delay.
122+ udelay (200000 );
123+
124+ return TRUE;
125125}
0 commit comments