phyHandling.c 30 KB

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