phyHandling.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * Handling of Ethernet PHY's
  3. * PHY's communicate with an EMAC either through
  4. * a Media-Independent Interface (MII), or a Reduced Media-Independent Interface (RMII).
  5. * The EMAC can poll for PHY ports on 32 different addresses. Each of the PHY ports
  6. * shall be treated independently.
  7. *
  8. */
  9. /* Standard includes. */
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. /* FreeRTOS includes. */
  14. #include "FreeRTOS.h"
  15. #include "task.h"
  16. #include "queue.h"
  17. #include "semphr.h"
  18. /* FreeRTOS+TCP includes. */
  19. #include "FreeRTOS_IP.h"
  20. #include "FreeRTOS_Sockets.h"
  21. #include "phyHandling.h"
  22. #define phyMIN_PHY_ADDRESS 1
  23. #define phyMAX_PHY_ADDRESS 31
  24. #define phyPHY_MAX_NEGOTIATE_TIME_MS 10000
  25. #if defined( PHY_LS_HIGH_CHECK_TIME_MS ) || defined( PHY_LS_LOW_CHECK_TIME_MS )
  26. #warning please use the new defines with 'ipconfig' prefix
  27. #endif
  28. #ifndef ipconfigPHY_LS_HIGH_CHECK_TIME_MS
  29. /* Check if the LinkStatus in the PHY is still high after 15 seconds of not
  30. * receiving packets. */
  31. #define ipconfigPHY_LS_HIGH_CHECK_TIME_MS 15000UL
  32. #endif
  33. #ifndef ipconfigPHY_LS_LOW_CHECK_TIME_MS
  34. /* Check if the LinkStatus in the PHY is still low every second. */
  35. #define ipconfigPHY_LS_LOW_CHECK_TIME_MS 1000UL
  36. #endif
  37. /* As the following 3 macro's are OK in most situations, and so they're not
  38. * included in 'FreeRTOSIPConfigDefaults.h'.
  39. * Users can change their values in the project's 'FreeRTOSIPConfig.h'. */
  40. #ifndef phyPHY_MAX_RESET_TIME_MS
  41. #define phyPHY_MAX_RESET_TIME_MS 1000UL
  42. #endif
  43. #ifndef phyPHY_MAX_NEGOTIATE_TIME_MS
  44. #define phyPHY_MAX_NEGOTIATE_TIME_MS 3000UL
  45. #endif
  46. #ifndef phySHORT_DELAY_MS
  47. #define phySHORT_DELAY_MS 50UL
  48. #endif
  49. /* Naming and numbering of basic PHY registers. */
  50. #define phyREG_00_BMCR 0x00U /* Basic Mode Control Register. */
  51. #define phyREG_01_BMSR 0x01U /* Basic Mode Status Register. */
  52. #define phyREG_02_PHYSID1 0x02U /* PHYS ID 1 */
  53. #define phyREG_03_PHYSID2 0x03U /* PHYS ID 2 */
  54. #define phyREG_04_ADVERTISE 0x04U /* Advertisement control reg */
  55. /* Naming and numbering of extended PHY registers. */
  56. #define PHYREG_10_PHYSTS 0x10U /* 16 PHY status register Offset */
  57. #define phyREG_19_PHYCR 0x19U /* 25 RW PHY Control Register */
  58. #define phyREG_1F_PHYSPCS 0x1FU /* 31 RW PHY Special Control Status */
  59. /* Bit fields for 'phyREG_00_BMCR', the 'Basic Mode Control Register'. */
  60. #define phyBMCR_FULL_DUPLEX 0x0100U /* Full duplex. */
  61. #define phyBMCR_AN_RESTART 0x0200U /* Auto negotiation restart. */
  62. #define phyBMCR_ISOLATE 0x0400U /* 1 = Isolates 0 = Normal operation. */
  63. #define phyBMCR_AN_ENABLE 0x1000U /* Enable auto negotiation. */
  64. #define phyBMCR_SPEED_100 0x2000U /* Select 100Mbps. */
  65. #define phyBMCR_RESET 0x8000U /* Reset the PHY. */
  66. /* Bit fields for 'phyREG_19_PHYCR', the 'PHY Control Register'. */
  67. #define PHYCR_MDIX_EN 0x8000U /* Enable Auto MDIX. */
  68. #define PHYCR_MDIX_FORCE 0x4000U /* Force MDIX crossed. */
  69. #define phyBMSR_AN_COMPLETE 0x0020U /* Auto-Negotiation process completed */
  70. #define phyBMSR_LINK_STATUS 0x0004U
  71. /* Bit fields for 'phyREG_1F_PHYSPCS
  72. * 001 = 10BASE-T half-duplex
  73. * 101 = 10BASE-T full-duplex
  74. * 010 = 100BASE-TX half-duplex
  75. * 110 = 100BASE-TX full-duplex
  76. */
  77. #define phyPHYSPCS_SPEED_MASK 0x000CU
  78. #define phyPHYSPCS_SPEED_10 0x0004U
  79. #define phyPHYSPCS_FULL_DUPLEX 0x0010U
  80. /*
  81. * Description of all capabilities that can be advertised to
  82. * the peer (usually a switch or router).
  83. */
  84. #define phyADVERTISE_CSMA 0x0001U /* Supports IEEE 802.3u: Fast Ethernet at 100 Mbit/s */
  85. #define phyADVERTISE_10HALF 0x0020U /* Try for 10mbps half-duplex. */
  86. #define phyADVERTISE_10FULL 0x0040U /* Try for 10mbps full-duplex. */
  87. #define phyADVERTISE_100HALF 0x0080U /* Try for 100mbps half-duplex. */
  88. #define phyADVERTISE_100FULL 0x0100U /* Try for 100mbps full-duplex. */
  89. #define phyADVERTISE_ALL \
  90. ( phyADVERTISE_10HALF | phyADVERTISE_10FULL | \
  91. phyADVERTISE_100HALF | phyADVERTISE_100FULL | \
  92. phyADVERTISE_CSMA )
  93. /* Send a reset command to a set of PHY-ports. */
  94. static uint32_t xPhyReset( EthernetPhy_t * pxPhyObject,
  95. uint32_t ulPhyMask );
  96. static BaseType_t xHas_1F_PHYSPCS( uint32_t ulPhyID )
  97. {
  98. BaseType_t xResult;
  99. switch( ulPhyID )
  100. {
  101. case PHY_ID_LAN8720:
  102. case PHY_ID_LAN8742A:
  103. case PHY_ID_KSZ8041:
  104. /*
  105. * case PHY_ID_KSZ8051: // same ID as 8041
  106. * case PHY_ID_KSZ8081: // same ID as 8041
  107. */
  108. case PHY_ID_KSZ8081MNXIA:
  109. case PHY_ID_KSZ8863:
  110. default:
  111. /* Most PHY's have a 1F_PHYSPCS */
  112. xResult = pdTRUE;
  113. break;
  114. case PHY_ID_DP83848I:
  115. xResult = pdFALSE;
  116. break;
  117. case PHY_ID_RTL8211FD:
  118. xResult = pdFALSE;
  119. break;
  120. }
  121. return xResult;
  122. }
  123. /*-----------------------------------------------------------*/
  124. static BaseType_t xHas_19_PHYCR( uint32_t ulPhyID )
  125. {
  126. BaseType_t xResult;
  127. switch( ulPhyID )
  128. {
  129. case PHY_ID_LAN8742A:
  130. case PHY_ID_DP83848I:
  131. xResult = pdTRUE;
  132. break;
  133. default:
  134. /* Most PHY's do not have a 19_PHYCR */
  135. xResult = pdFALSE;
  136. break;
  137. }
  138. return xResult;
  139. }
  140. /*-----------------------------------------------------------*/
  141. /* Initialise the struct and assign a PHY-read and -write function. */
  142. void vPhyInitialise( EthernetPhy_t * pxPhyObject,
  143. xApplicationPhyReadHook_t fnPhyRead,
  144. xApplicationPhyWriteHook_t fnPhyWrite )
  145. {
  146. memset( ( void * ) pxPhyObject, 0, sizeof( *pxPhyObject ) );
  147. pxPhyObject->fnPhyRead = fnPhyRead;
  148. pxPhyObject->fnPhyWrite = fnPhyWrite;
  149. }
  150. /*-----------------------------------------------------------*/
  151. /* Discover all PHY's connected by polling 32 indexes ( zero-based ) */
  152. BaseType_t xPhyDiscover( EthernetPhy_t * pxPhyObject )
  153. {
  154. BaseType_t xPhyAddress;
  155. pxPhyObject->xPortCount = 0;
  156. for( xPhyAddress = phyMIN_PHY_ADDRESS; xPhyAddress <= phyMAX_PHY_ADDRESS; xPhyAddress++ )
  157. {
  158. uint32_t ulLowerID;
  159. pxPhyObject->fnPhyRead(xPhyAddress, phyREG_03_PHYSID2, &ulLowerID);
  160. /* A valid PHY id can not be all zeros or all ones. */
  161. if( ( ulLowerID != ( uint16_t ) ~0U ) && ( ulLowerID != ( uint16_t ) 0U ) )
  162. {
  163. uint32_t ulUpperID;
  164. uint32_t ulPhyID;
  165. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_02_PHYSID1, &ulUpperID );
  166. ulPhyID = ( ( ( uint32_t ) ulUpperID ) << 16 ) | ( ulLowerID & 0xFFF0 );
  167. pxPhyObject->ucPhyIndexes[ pxPhyObject->xPortCount ] = xPhyAddress;
  168. pxPhyObject->ulPhyIDs[ pxPhyObject->xPortCount ] = ulPhyID;
  169. pxPhyObject->xPortCount++;
  170. /* See if there is more storage space. */
  171. if( pxPhyObject->xPortCount == ipconfigPHY_MAX_PORTS )
  172. {
  173. break;
  174. }
  175. }
  176. }
  177. if( pxPhyObject->xPortCount > 0 )
  178. {
  179. FreeRTOS_printf( ( "PHY ID %lX\n", pxPhyObject->ulPhyIDs[ 0 ] ) );
  180. }
  181. return pxPhyObject->xPortCount;
  182. }
  183. /*-----------------------------------------------------------*/
  184. /* Send a reset command to a set of PHY-ports. */
  185. static uint32_t xPhyReset( EthernetPhy_t * pxPhyObject,
  186. uint32_t ulPhyMask )
  187. {
  188. uint32_t ulDoneMask, ulConfig;
  189. TickType_t xRemainingTime;
  190. TimeOut_t xTimer;
  191. BaseType_t xPhyIndex;
  192. /* A bit-mask of PHY ports that are ready. */
  193. ulDoneMask = 0UL;
  194. /* Set the RESET bits high. */
  195. for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++ )
  196. {
  197. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  198. /* Read Control register. */
  199. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_00_BMCR, &ulConfig );
  200. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, ulConfig | phyBMCR_RESET );
  201. }
  202. xRemainingTime = ( TickType_t ) pdMS_TO_TICKS( phyPHY_MAX_RESET_TIME_MS );
  203. vTaskSetTimeOutState( &xTimer );
  204. /* The reset should last less than a second. */
  205. for( ; ; )
  206. {
  207. for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++ )
  208. {
  209. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  210. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_00_BMCR, &ulConfig );
  211. if( ( ulConfig & phyBMCR_RESET ) == 0 )
  212. {
  213. FreeRTOS_printf( ( "xPhyReset: phyBMCR_RESET %d ready\n", ( int ) xPhyIndex ) );
  214. ulDoneMask |= ( 1UL << xPhyIndex );
  215. }
  216. }
  217. if( ulDoneMask == ulPhyMask )
  218. {
  219. break;
  220. }
  221. if( xTaskCheckForTimeOut( &xTimer, &xRemainingTime ) != pdFALSE )
  222. {
  223. FreeRTOS_printf( ( "xPhyReset: phyBMCR_RESET timed out ( done 0x%02lX )\n", ulDoneMask ) );
  224. break;
  225. }
  226. /* Block for a while */
  227. vTaskDelay( pdMS_TO_TICKS( phySHORT_DELAY_MS ) );
  228. }
  229. /* Clear the reset bits. */
  230. for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++ )
  231. {
  232. if( ( ulDoneMask & ( 1UL << xPhyIndex ) ) == 0UL )
  233. {
  234. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  235. /* The reset operation timed out, clear the bit manually. */
  236. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_00_BMCR, &ulConfig );
  237. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, ulConfig & ~phyBMCR_RESET );
  238. }
  239. }
  240. vTaskDelay( pdMS_TO_TICKS( phySHORT_DELAY_MS ) );
  241. return ulDoneMask;
  242. }
  243. /*-----------------------------------------------------------*/
  244. BaseType_t xPhyConfigure( EthernetPhy_t * pxPhyObject,
  245. const PhyProperties_t * pxPhyProperties )
  246. {
  247. uint32_t ulConfig, ulAdvertise;
  248. BaseType_t xPhyIndex;
  249. if( pxPhyObject->xPortCount < 1 )
  250. {
  251. FreeRTOS_printf( ( "xPhyConfigure: No PHY's detected.\n" ) );
  252. return -1;
  253. }
  254. /* The expected ID for the 'LAN8742A' is 0x0007c130. */
  255. /* The expected ID for the 'LAN8720' is 0x0007c0f0. */
  256. /* The expected ID for the 'DP83848I' is 0x20005C90. */
  257. /* Set advertise register. */
  258. if( ( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_AUTO ) && ( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_AUTO ) )
  259. {
  260. ulAdvertise = phyADVERTISE_ALL;
  261. /* Reset auto-negotiation capability. */
  262. }
  263. else
  264. {
  265. /* Always select protocol 802.3u. */
  266. ulAdvertise = phyADVERTISE_CSMA;
  267. if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_AUTO )
  268. {
  269. if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL )
  270. {
  271. ulAdvertise |= phyADVERTISE_10FULL | phyADVERTISE_100FULL;
  272. }
  273. else
  274. {
  275. ulAdvertise |= phyADVERTISE_10HALF | phyADVERTISE_100HALF;
  276. }
  277. }
  278. else if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_AUTO )
  279. {
  280. if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_10 )
  281. {
  282. ulAdvertise |= phyADVERTISE_10FULL | phyADVERTISE_10HALF;
  283. }
  284. else
  285. {
  286. ulAdvertise |= phyADVERTISE_100FULL | phyADVERTISE_100HALF;
  287. }
  288. }
  289. else if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_100 )
  290. {
  291. if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL )
  292. {
  293. ulAdvertise |= phyADVERTISE_100FULL;
  294. }
  295. else
  296. {
  297. ulAdvertise |= phyADVERTISE_100HALF;
  298. }
  299. }
  300. else
  301. {
  302. if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL )
  303. {
  304. ulAdvertise |= phyADVERTISE_10FULL;
  305. }
  306. else
  307. {
  308. ulAdvertise |= phyADVERTISE_10HALF;
  309. }
  310. }
  311. }
  312. /* Send a reset command to a set of PHY-ports. */
  313. xPhyReset( pxPhyObject, xPhyGetMask( pxPhyObject ) );
  314. for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++ )
  315. {
  316. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  317. uint32_t ulPhyID = pxPhyObject->ulPhyIDs[ xPhyIndex ];
  318. /* Write advertise register. */
  319. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_04_ADVERTISE, ulAdvertise );
  320. /*
  321. * AN_EN AN1 AN0 Forced Mode
  322. * 0 0 0 10BASE-T, Half-Duplex
  323. * 0 0 1 10BASE-T, Full-Duplex
  324. * 0 1 0 100BASE-TX, Half-Duplex
  325. * 0 1 1 100BASE-TX, Full-Duplex
  326. * AN_EN AN1 AN0 Advertised Mode
  327. * 1 0 0 10BASE-T, Half/Full-Duplex
  328. * 1 0 1 100BASE-TX, Half/Full-Duplex
  329. * 1 1 0 10BASE-T Half-Duplex
  330. * 100BASE-TX, Half-Duplex
  331. * 1 1 1 10BASE-T, Half/Full-Duplex
  332. * 100BASE-TX, Half/Full-Duplex
  333. */
  334. /* Read Control register. */
  335. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_00_BMCR, &ulConfig );
  336. ulConfig &= ~( phyBMCR_SPEED_100 | phyBMCR_FULL_DUPLEX );
  337. ulConfig |= phyBMCR_AN_ENABLE;
  338. if( ( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_100 ) || ( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_AUTO ) )
  339. {
  340. ulConfig |= phyBMCR_SPEED_100;
  341. }
  342. else if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_10 )
  343. {
  344. ulConfig &= ~phyBMCR_SPEED_100;
  345. }
  346. if( ( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL ) || ( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_AUTO ) )
  347. {
  348. ulConfig |= phyBMCR_FULL_DUPLEX;
  349. }
  350. else if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_HALF )
  351. {
  352. ulConfig &= ~phyBMCR_FULL_DUPLEX;
  353. }
  354. if( xHas_19_PHYCR( ulPhyID ) )
  355. {
  356. uint32_t ulPhyControl;
  357. /* Read PHY Control register. */
  358. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_19_PHYCR, &ulPhyControl );
  359. /* Clear bits which might get set: */
  360. ulPhyControl &= ~( PHYCR_MDIX_EN | PHYCR_MDIX_FORCE );
  361. if( pxPhyProperties->ucMDI_X == PHY_MDIX_AUTO )
  362. {
  363. ulPhyControl |= PHYCR_MDIX_EN;
  364. }
  365. else if( pxPhyProperties->ucMDI_X == PHY_MDIX_CROSSED )
  366. {
  367. /* Force direct link = Use crossed RJ45 cable. */
  368. ulPhyControl &= ~PHYCR_MDIX_FORCE;
  369. }
  370. else
  371. {
  372. /* Force crossed link = Use direct RJ45 cable. */
  373. ulPhyControl |= PHYCR_MDIX_FORCE;
  374. }
  375. /* update PHY Control Register. */
  376. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_19_PHYCR, ulPhyControl );
  377. }
  378. FreeRTOS_printf( ( "+TCP: advertise: %04lX config %04lX\n", ulAdvertise, ulConfig ) );
  379. }
  380. /* Keep these values for later use. */
  381. pxPhyObject->ulBCRValue = ulConfig & ~phyBMCR_ISOLATE;
  382. pxPhyObject->ulACRValue = ulAdvertise;
  383. return 0;
  384. }
  385. /*-----------------------------------------------------------*/
  386. /* xPhyFixedValue(): this function is called in case auto-negotiation is disabled.
  387. * The caller has set the values in 'xPhyPreferences' (ucDuplex and ucSpeed).
  388. * The PHY register phyREG_00_BMCR will be set for every connected PHY that matches
  389. * with ulPhyMask. */
  390. BaseType_t xPhyFixedValue( EthernetPhy_t * pxPhyObject,
  391. uint32_t ulPhyMask )
  392. {
  393. BaseType_t xPhyIndex;
  394. uint32_t ulValue, ulBitMask = ( uint32_t ) 1U;
  395. TickType_t xRemainingTime;
  396. TimeOut_t xTimer;
  397. uint32_t ulDoneMask, ulRegValue;
  398. ulValue = ( uint32_t ) 0U;
  399. if( pxPhyObject->xPhyPreferences.ucDuplex == PHY_DUPLEX_FULL ) {
  400. ulValue |= phyBMCR_FULL_DUPLEX;
  401. }
  402. if( pxPhyObject->xPhyPreferences.ucSpeed == PHY_SPEED_100 ) {
  403. ulValue |= phyBMCR_SPEED_100;
  404. }
  405. for (xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1) {
  406. if (( ulPhyMask & ulBitMask ) != 0lu) {
  407. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  408. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, ulValue );
  409. }
  410. }
  411. xRemainingTime = ( TickType_t ) pdMS_TO_TICKS( phyPHY_MAX_NEGOTIATE_TIME_MS );
  412. vTaskSetTimeOutState( &xTimer );
  413. pxPhyObject->ulLinkStatusMask = 0;
  414. ulDoneMask = 0;
  415. for ( ; ; ) {
  416. ulBitMask = ( uint32_t ) 1U;
  417. for ( xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  418. {
  419. if ((ulPhyMask & ulBitMask) != 0lu) {
  420. if ((ulDoneMask & ulBitMask) == 0lu) {
  421. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[xPhyIndex];
  422. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulRegValue );
  423. if ((ulRegValue & phyBMSR_LINK_STATUS) != 0) {
  424. pxPhyObject->ulLinkStatusMask |= ulBitMask;
  425. ulDoneMask |= ulBitMask;
  426. }
  427. }
  428. }
  429. }
  430. if ( ulPhyMask == ulDoneMask ) {
  431. break;
  432. }
  433. if ( xTaskCheckForTimeOut( &xTimer, &xRemainingTime ) != pdFALSE ) {
  434. printf("xPhyFixedValue connect timeout!\n");
  435. break;
  436. }
  437. vTaskDelay( pdMS_TO_TICKS( phySHORT_DELAY_MS ) );
  438. }
  439. return 0;
  440. }
  441. /*-----------------------------------------------------------*/
  442. /* xPhyStartAutoNegotiation() is the alternative xPhyFixedValue():
  443. * It sets the BMCR_AN_RESTART bit and waits for the auto-negotiation completion
  444. * ( phyBMSR_AN_COMPLETE ). */
  445. BaseType_t xPhyStartAutoNegotiation( EthernetPhy_t * pxPhyObject,
  446. uint32_t ulPhyMask )
  447. {
  448. uint32_t xPhyIndex, ulDoneMask, ulBitMask;
  449. uint32_t ulPHYLinkStatus, ulRegValue;
  450. TickType_t xRemainingTime;
  451. TimeOut_t xTimer;
  452. uint8_t speed = PHY_SPEED_100;
  453. uint8_t duplex = PHY_DUPLEX_HALF;
  454. if (ulPhyMask == ( uint32_t ) 0U) {
  455. return 0;
  456. }
  457. for (xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++) {
  458. if ((ulPhyMask & ( 1lu << xPhyIndex)) != 0lu ) {
  459. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  460. /* Enable Auto-Negotiation. */
  461. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_04_ADVERTISE, pxPhyObject->ulACRValue );
  462. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, pxPhyObject->ulBCRValue | phyBMCR_AN_RESTART );
  463. }
  464. }
  465. xRemainingTime = ( TickType_t ) pdMS_TO_TICKS( phyPHY_MAX_NEGOTIATE_TIME_MS );
  466. vTaskSetTimeOutState( &xTimer );
  467. ulDoneMask = 0;
  468. /* Wait until the auto-negotiation will be completed */
  469. for ( ; ; ) {
  470. ulBitMask = ( uint32_t ) 1U;
  471. for ( xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  472. {
  473. if ((ulPhyMask & ulBitMask) != 0lu) {
  474. if ((ulDoneMask & ulBitMask) == 0lu) {
  475. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[xPhyIndex];
  476. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulRegValue);
  477. if ((ulRegValue & phyBMSR_AN_COMPLETE) != 0) {
  478. ulDoneMask |= ulBitMask;
  479. }
  480. }
  481. }
  482. }
  483. if ( ulPhyMask == ulDoneMask ) {
  484. break;
  485. }
  486. if ( xTaskCheckForTimeOut( &xTimer, &xRemainingTime ) != pdFALSE ) {
  487. FreeRTOS_printf(("xPhyStartAutoNegotiation: phyBMCR_RESET timed out ( done 0x%02lX )\n", \
  488. ulDoneMask ));
  489. break;
  490. }
  491. vTaskDelay( pdMS_TO_TICKS( phySHORT_DELAY_MS ) );
  492. }
  493. if (ulDoneMask != ( uint32_t ) 0U) {
  494. ulBitMask = ( uint32_t ) 1U;
  495. pxPhyObject->ulLinkStatusMask &= ~( ulDoneMask );
  496. for( xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  497. {
  498. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  499. uint32_t ulPhyID = pxPhyObject->ulPhyIDs[ xPhyIndex ];
  500. if( ( ulDoneMask & ulBitMask ) == ( uint32_t ) 0U ) {
  501. continue;
  502. }
  503. /* Clear the 'phyBMCR_AN_RESTART' bit. */
  504. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, pxPhyObject->ulBCRValue );
  505. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulRegValue );
  506. if ((ulRegValue & phyBMSR_LINK_STATUS) != 0) {
  507. ulPHYLinkStatus |= phyBMSR_LINK_STATUS;
  508. pxPhyObject->ulLinkStatusMask |= ulBitMask;
  509. } else {
  510. ulPHYLinkStatus &= ~( phyBMSR_LINK_STATUS );
  511. }
  512. if (ulPhyID == PHY_ID_KSZ8081MNXIA) {
  513. uint32_t ulControlStatus;
  514. pxPhyObject->fnPhyRead( xPhyAddress, 0x1E, &ulControlStatus );
  515. switch(ulControlStatus & 0x07) {
  516. case 0x01:
  517. case 0x05:
  518. /* [001] = 10BASE-T half-duplex */
  519. /* [101] = 10BASE-T full-duplex */
  520. /* 10 Mbps. */
  521. speed = PHY_SPEED_10;
  522. break;
  523. case 0x02:
  524. case 0x06:
  525. /* [010] = 100BASE-TX half-duplex */
  526. /* [110] = 100BASE-TX full-duplex */
  527. break;
  528. }
  529. switch( ulControlStatus & 0x07 )
  530. {
  531. case 0x05:
  532. case 0x06:
  533. /* [101] = 10BASE-T full-duplex */
  534. /* [110] = 100BASE-TX full-duplex */
  535. /* Full duplex. */
  536. duplex = PHY_DUPLEX_FULL;
  537. break;
  538. case 0x01:
  539. case 0x02:
  540. /* [001] = 10BASE-T half-duplex */
  541. /* [010] = 100BASE-TX half-duplex */
  542. break;
  543. }
  544. } else if(xHas_1F_PHYSPCS( ulPhyID)) {
  545. /* 31 RW PHY Special Control Status */
  546. uint32_t ulControlStatus;
  547. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_1F_PHYSPCS, &ulControlStatus );
  548. ulRegValue = 0;
  549. if( ( ulControlStatus & phyPHYSPCS_FULL_DUPLEX ) != 0 ) {
  550. duplex = PHY_DUPLEX_FULL;
  551. }
  552. if( ( ulControlStatus & phyPHYSPCS_SPEED_MASK ) == phyPHYSPCS_SPEED_10 ) {
  553. speed = PHY_SPEED_10;
  554. }
  555. } else if( ulPhyID == PHY_ID_RTL8211FD) {
  556. uint32_t ulPhyStatus;
  557. pxPhyObject->fnPhyWrite( xPhyAddress, 31, 0xa43);
  558. pxPhyObject->fnPhyRead( xPhyAddress, 0x1a, &ulPhyStatus);
  559. pxPhyObject->fnPhyWrite( xPhyAddress, 31, 0x0);
  560. ulRegValue = 0;
  561. if ((ulPhyStatus & (1 << 3)) != 0 ) {
  562. duplex = PHY_DUPLEX_FULL;
  563. }
  564. switch ((ulPhyStatus >> 4) & 0x3) {
  565. case 0:
  566. speed = PHY_SPEED_10;
  567. break;
  568. case 1:
  569. speed = PHY_SPEED_100;
  570. break;
  571. case 2:
  572. speed = PHY_SPEED_1000;
  573. break;
  574. }
  575. } else {
  576. /* Read the result of the auto-negotiation. */
  577. pxPhyObject->fnPhyRead( xPhyAddress, PHYREG_10_PHYSTS, &ulRegValue );
  578. }
  579. FreeRTOS_printf( ( "Autonego ready: %08lx: %s duplex %u mbit %s status\n",
  580. ulRegValue,
  581. (duplex == PHY_DUPLEX_FULL) ? "full" : "half",
  582. (speed == PHY_SPEED_10) ? 10 : ((speed == PHY_SPEED_100) ? 100 : 1000),
  583. ( ( ulPHYLinkStatus |= phyBMSR_LINK_STATUS ) != 0 ) ? "high" : "low" ) );
  584. pxPhyObject->xPhyProperties.ucDuplex = duplex;
  585. pxPhyObject->xPhyProperties.ucSpeed = speed;
  586. }
  587. } /* if( ulDoneMask != ( uint32_t) 0U ) */
  588. return 0;
  589. }
  590. /*-----------------------------------------------------------*/
  591. BaseType_t xPhyCheckLinkStatus( EthernetPhy_t * pxPhyObject,
  592. BaseType_t xHadReception )
  593. {
  594. uint32_t ulStatus, ulBitMask = 1U;
  595. BaseType_t xPhyIndex;
  596. BaseType_t xNeedCheck = pdFALSE;
  597. if( xHadReception > 0 )
  598. {
  599. /* A packet was received. No need to check for the PHY status now,
  600. * but set a timer to check it later on. */
  601. vTaskSetTimeOutState( &( pxPhyObject->xLinkStatusTimer ) );
  602. pxPhyObject->xLinkStatusRemaining = pdMS_TO_TICKS( ipconfigPHY_LS_HIGH_CHECK_TIME_MS );
  603. for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  604. {
  605. if( ( pxPhyObject->ulLinkStatusMask & ulBitMask ) == 0UL )
  606. {
  607. pxPhyObject->ulLinkStatusMask |= ulBitMask;
  608. FreeRTOS_printf( ( "xPhyCheckLinkStatus: PHY LS now %02lX\n", pxPhyObject->ulLinkStatusMask ) );
  609. xNeedCheck = pdTRUE;
  610. }
  611. }
  612. }
  613. else if( xTaskCheckForTimeOut( &( pxPhyObject->xLinkStatusTimer ), &( pxPhyObject->xLinkStatusRemaining ) ) != pdFALSE )
  614. {
  615. /* Frequent checking the PHY Link Status can affect for the performance of Ethernet controller.
  616. * As long as packets are received, no polling is needed.
  617. * Otherwise, polling will be done when the 'xLinkStatusTimer' expires. */
  618. for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  619. {
  620. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  621. if( pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulStatus ) == 0 )
  622. {
  623. if( !!( pxPhyObject->ulLinkStatusMask & ulBitMask ) != !!( ulStatus & phyBMSR_LINK_STATUS ) )
  624. {
  625. if( ( ulStatus & phyBMSR_LINK_STATUS ) != 0 )
  626. {
  627. pxPhyObject->ulLinkStatusMask |= ulBitMask;
  628. }
  629. else
  630. {
  631. pxPhyObject->ulLinkStatusMask &= ~( ulBitMask );
  632. }
  633. FreeRTOS_printf( ( "xPhyCheckLinkStatus: PHY LS now %02lX\n", pxPhyObject->ulLinkStatusMask ) );
  634. xNeedCheck = pdTRUE;
  635. }
  636. }
  637. }
  638. vTaskSetTimeOutState( &( pxPhyObject->xLinkStatusTimer ) );
  639. if( ( pxPhyObject->ulLinkStatusMask & ( ulBitMask >> 1 ) ) != 0 )
  640. {
  641. /* The link status is high, so don't poll the PHY too often. */
  642. pxPhyObject->xLinkStatusRemaining = pdMS_TO_TICKS( ipconfigPHY_LS_HIGH_CHECK_TIME_MS );
  643. }
  644. else
  645. {
  646. /* The link status is low, polling may be done more frequently. */
  647. pxPhyObject->xLinkStatusRemaining = pdMS_TO_TICKS( ipconfigPHY_LS_LOW_CHECK_TIME_MS );
  648. }
  649. }
  650. return xNeedCheck;
  651. }
  652. /*-----------------------------------------------------------*/