99 */
1010#include <rtthread.h>
1111#include <drivers/phy.h>
12+ #include <stdio.h>
1213#define DBG_TAG "rtdm.phy"
1314#define DBG_LVL DBG_INFO
1415#include <rtdbg.h>
@@ -104,48 +105,28 @@ rt_err_t rt_ofw_get_mac_addr(struct rt_ofw_node *np, rt_uint8_t *addr)
104105 return - RT_ERROR ;
105106}
106107
107- rt_err_t rt_ofw_get_phyid (struct rt_ofw_node * np ,rt_uint16_t * vendor , rt_uint16_t * device )
108+ rt_err_t rt_ofw_get_phyid (struct rt_ofw_node * np ,rt_uint32_t * id )
108109{
109- const char * phy_id ,* end ;
110- rt_ssize_t len ;
111-
112- phy_id = rt_ofw_prop_read_raw (np ,"compatible" ,& len );
113- if (!phy_id )
114- return - RT_ERROR ;
110+ const char * phy_id ;
111+ unsigned int upper , lower ;
112+ int ret ;
115113
116- end = phy_id + len ;
117- while (phy_id < end )
118- {
119- len = strlen (phy_id );
114+ ret = rt_ofw_prop_read_string (np ,"compatible" ,& phy_id );
115+ if (ret )
116+ return ret ;
120117
121- if (len >= strlen ("ethernet-phy-idVVVV.DDDD" ))
122- {
123- char * s = strstr (phy_id , "ethernet-phy-id" );
124-
125- /*
126- * check if the string is something like
127- * ethernet-phy-idVVVV.DDDD
128- */
129- if (s && s [19 ] == '.' )
130- {
131- s += strlen ("ethernet-phy-id" );
132- rt_memcpy (vendor ,s ,16 );
133- s += 5 ;
134- rt_memcpy (device ,s ,16 );
135-
136- return 0 ;
137- }
138- }
139- phy_id += (len + 1 );
140- }
141- return - RT_ERROR ;
118+ ret = sscanf (phy_id ,"ethernet-phy-id%4x.%4x" ,& upper , & lower );
119+ if (ret != 2 )
120+ return - RT_ERROR ;
121+
122+ * id = ((upper & 0xffff ) << 16 ) | (lower & 0xffff );
123+ return RT_EOK ;
142124
143125}
144126struct rt_phy_device * rt_ofw_create_phy (struct mii_bus * bus ,struct rt_ofw_node * np ,int phyaddr )
145127{
146128 struct rt_phy_device * dev = RT_NULL ;
147129 struct rt_ofw_node * phy_node ;
148- rt_uint16_t vendor , device ;
149130 int ret ;
150131 rt_uint32_t id = 0xffff ;
151132
@@ -156,13 +137,15 @@ struct rt_phy_device *rt_ofw_create_phy(struct mii_bus *bus,struct rt_ofw_node *
156137 return RT_NULL ;
157138 }
158139
159- ret = rt_ofw_get_phyid (np , & vendor , & device );
140+ ret = rt_ofw_get_phyid (np , & id );
160141 if (ret )
161142 {
162143 LOG_D ("Failed to read eth PHY id, err: %d\n" , ret );
163144 return RT_NULL ;
164145 }
165- id = vendor << 16 | device ;
146+
147+ LOG_D ("Found a PHY id: 0x%x\n" , id );
148+
166149 dev = rt_phy_device_create (bus , phyaddr , id , RT_FALSE );
167150
168151 if (dev )
0 commit comments