phyHandling.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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. uint32_t regv;
  298. if( pxPhyObject->xPortCount < 1 ) {
  299. FreeRTOS_printf( ( "xPhyConfigure: No PHY's detected.\n" ) );
  300. return -1;
  301. }
  302. /* The expected ID for the 'LAN8742A' is 0x0007c130. */
  303. /* The expected ID for the 'LAN8720' is 0x0007c0f0. */
  304. if( ( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_AUTO ) && ( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_AUTO ) )
  305. {
  306. ulAdvertise = phyADVERTISE_ALL;
  307. }
  308. else
  309. {
  310. ulAdvertise = phyADVERTISE_CSMA;
  311. if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_AUTO )
  312. {
  313. if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL )
  314. {
  315. ulAdvertise |= phyADVERTISE_10FULL | phyADVERTISE_100FULL;
  316. }
  317. else
  318. {
  319. ulAdvertise |= phyADVERTISE_10HALF | phyADVERTISE_100HALF;
  320. }
  321. }
  322. else if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_AUTO )
  323. {
  324. if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_10 )
  325. {
  326. ulAdvertise |= phyADVERTISE_10FULL | phyADVERTISE_10HALF;
  327. }
  328. else
  329. {
  330. ulAdvertise |= phyADVERTISE_100FULL | phyADVERTISE_100HALF;
  331. }
  332. }
  333. else if( pxPhyProperties->ucSpeed == ( uint8_t ) PHY_SPEED_100 )
  334. {
  335. if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL )
  336. {
  337. ulAdvertise |= phyADVERTISE_100FULL;
  338. }
  339. else
  340. {
  341. ulAdvertise |= phyADVERTISE_100HALF;
  342. }
  343. }
  344. else
  345. {
  346. if( pxPhyProperties->ucDuplex == ( uint8_t ) PHY_DUPLEX_FULL )
  347. {
  348. ulAdvertise |= phyADVERTISE_10FULL;
  349. }
  350. else
  351. {
  352. ulAdvertise |= phyADVERTISE_10HALF;
  353. }
  354. }
  355. }
  356. /* Send a reset command to a set of PHY-ports. */
  357. xPhyReset( pxPhyObject, xPhyGetMask( pxPhyObject ) );
  358. for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++ ) {
  359. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[xPhyIndex];
  360. uint32_t ulPhyID = pxPhyObject->ulPhyIDs[xPhyIndex];
  361. /* Write advertise register. */
  362. #if CLAUSE45_ENABLE
  363. xPhyC45Write(pxPhyObject, xPhyAddress, 1, phyREG_04_ADVERTISE, ulAdvertise);
  364. #else
  365. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_04_ADVERTISE, ulAdvertise );
  366. #endif
  367. /*
  368. * AN_EN AN1 AN0 Forced Mode
  369. * 0 0 0 10BASE-T, Half-Duplex
  370. * 0 0 1 10BASE-T, Full-Duplex
  371. * 0 1 0 100BASE-TX, Half-Duplex
  372. * 0 1 1 100BASE-TX, Full-Duplex
  373. * AN_EN AN1 AN0 Advertised Mode
  374. * 1 0 0 10BASE-T, Half/Full-Duplex
  375. * 1 0 1 100BASE-TX, Half/Full-Duplex
  376. * 1 1 0 10BASE-T Half-Duplex
  377. * 100BASE-TX, Half-Duplex
  378. * 1 1 1 10BASE-T, Half/Full-Duplex
  379. * 100BASE-TX, Half/Full-Duplex
  380. */
  381. /* Read Control register. */
  382. #if CLAUSE45_ENABLE
  383. xPhyC45Read(pxPhyObject, xPhyAddress, 1, phyREG_00_BMCR, &ulConfig);
  384. #else
  385. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_00_BMCR, &ulConfig );
  386. #endif
  387. ulConfig &= ~( phyBMCR_SPEED_100 | phyBMCR_FULL_DUPLEX );
  388. ulConfig |= phyBMCR_AN_ENABLE;
  389. if ((pxPhyProperties->ucSpeed == (uint8_t) PHY_SPEED_100) || (pxPhyProperties->ucSpeed == (uint8_t) PHY_SPEED_AUTO)) {
  390. ulConfig |= phyBMCR_SPEED_100;
  391. } else if (pxPhyProperties->ucSpeed == (uint8_t)PHY_SPEED_10) {
  392. ulConfig &= ~phyBMCR_SPEED_100;
  393. }
  394. if ((pxPhyProperties->ucDuplex == (uint8_t) PHY_DUPLEX_FULL) || (pxPhyProperties->ucDuplex == (uint8_t)PHY_DUPLEX_AUTO)) {
  395. ulConfig |= phyBMCR_FULL_DUPLEX;
  396. } else if(pxPhyProperties->ucDuplex == (uint8_t)PHY_DUPLEX_HALF) {
  397. ulConfig &= ~phyBMCR_FULL_DUPLEX;
  398. }
  399. /* ÅäÖÃMAC½Ó¿ÚÀàÐÍ */
  400. if (pxPhyProperties->ucMacIf == PHY_MACIF_MII) {
  401. if (ulPhyID == PHY_ID_JL3101) {
  402. xPhyC45Read(pxPhyObject, xPhyAddress, 3, 0x8000, &regv);
  403. if ((regv & 0xf) != 2) {
  404. regv &= ~0xf;
  405. regv |= 0x2;
  406. xPhyC45Write(pxPhyObject, xPhyAddress, 3, 0x8000, regv);
  407. xPhyC45Read(pxPhyObject, xPhyAddress, 1, 0x0, &regv);
  408. xPhyC45Write(pxPhyObject, xPhyAddress, 1, 0x0, regv | (1 << 15));
  409. mdelay(4);
  410. }
  411. }
  412. } else if (pxPhyProperties->ucMacIf == PHY_MACIF_RMII) {
  413. }
  414. /* ÅäÖÃÖ÷´Ó */
  415. if (pxPhyProperties->ucRole == PHY_ROLE_MASTER) {
  416. if (ulPhyID == PHY_ID_JL3101) {
  417. xPhyC45Read(pxPhyObject, xPhyAddress, 1, 0x834, &regv);
  418. regv |= (1UL << 14);
  419. xPhyC45Write(pxPhyObject, xPhyAddress, 1, 0x834, regv);
  420. }
  421. } else if (pxPhyProperties->ucRole == PHY_ROLE_SLAVE) {
  422. if (ulPhyID == PHY_ID_JL3101) {
  423. xPhyC45Read(pxPhyObject, xPhyAddress, 1, 0x834, &regv);
  424. regv &= ~(1UL << 14);
  425. xPhyC45Write(pxPhyObject, xPhyAddress, 1, 0x834, regv);
  426. }
  427. } else {
  428. }
  429. /* ½»²æÏßÅäÖà */
  430. if (xHas_19_PHYCR(ulPhyID)) {
  431. uint32_t ulPhyControl;
  432. /* Read PHY Control register. */
  433. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_19_PHYCR, &ulPhyControl );
  434. /* Clear bits which might get set: */
  435. ulPhyControl &= ~( PHYCR_MDIX_EN | PHYCR_MDIX_FORCE );
  436. if( pxPhyProperties->ucMDI_X == PHY_MDIX_AUTO ) {
  437. ulPhyControl |= PHYCR_MDIX_EN;
  438. } else if( pxPhyProperties->ucMDI_X == PHY_MDIX_CROSSED ) {
  439. /* Force direct link = Use crossed RJ45 cable. */
  440. ulPhyControl &= ~PHYCR_MDIX_FORCE;
  441. } else {
  442. /* Force crossed link = Use direct RJ45 cable. */
  443. ulPhyControl |= PHYCR_MDIX_FORCE;
  444. }
  445. /* update PHY Control Register. */
  446. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_19_PHYCR, ulPhyControl );
  447. } else {
  448. if (pxPhyProperties->ucMDI_X == PHY_MDIX_DIRECT) {
  449. if (ulPhyID == PHY_ID_JL3101) {
  450. xPhyC45Read(pxPhyObject, xPhyAddress, 3, 0x8100, &regv);
  451. regv &= ~(1UL << 1);
  452. regv &= ~(1UL << 0);
  453. xPhyC45Write(pxPhyObject, xPhyAddress, 3, 0x8100, regv);
  454. xPhyC45Read(pxPhyObject, xPhyAddress, 3, 0x8200, &regv);
  455. regv &= ~(1UL << 1);
  456. regv &= ~(1UL << 0);
  457. xPhyC45Write(pxPhyObject, xPhyAddress, 3, 0x8200, regv);
  458. xPhyC45Read(pxPhyObject, xPhyAddress, 1, 0x0, &regv);
  459. regv |= (1UL << 15);
  460. xPhyC45Write(pxPhyObject, xPhyAddress, 1, 0x0, regv);
  461. }
  462. } else if (pxPhyProperties->ucMDI_X == PHY_MDIX_CROSSED) {
  463. if (ulPhyID == PHY_ID_JL3101) {
  464. xPhyC45Read(pxPhyObject, xPhyAddress, 3, 0x8100, &regv);
  465. regv &= ~(1UL << 1);
  466. regv |= (1UL << 0);
  467. xPhyC45Write(pxPhyObject, xPhyAddress, 3, 0x8100, regv);
  468. xPhyC45Read(pxPhyObject, xPhyAddress, 3, 0x8200, &regv);
  469. regv &= ~(1UL << 1);
  470. regv &= ~(1UL << 0);
  471. xPhyC45Write(pxPhyObject, xPhyAddress, 3, 0x8200, regv);
  472. xPhyC45Read(pxPhyObject, xPhyAddress, 1, 0x0, &regv);
  473. regv |= (1UL << 15);
  474. xPhyC45Write(pxPhyObject, xPhyAddress, 1, 0x0, regv);
  475. }
  476. } else {
  477. }
  478. }
  479. FreeRTOS_printf( ( "+TCP: advertise: %04lX config %04lX\n", ulAdvertise, ulConfig ) );
  480. }
  481. /* Keep these values for later use. */
  482. pxPhyObject->ulBCRValue = ulConfig & ~phyBMCR_ISOLATE;
  483. pxPhyObject->ulACRValue = ulAdvertise;
  484. return 0;
  485. }
  486. /*-----------------------------------------------------------*/
  487. /* xPhyFixedValue(): this function is called in case auto-negotiation is disabled.
  488. * The caller has set the values in 'xPhyPreferences' (ucDuplex and ucSpeed).
  489. * The PHY register phyREG_00_BMCR will be set for every connected PHY that matches
  490. * with ulPhyMask. */
  491. BaseType_t xPhyFixedValue( EthernetPhy_t * pxPhyObject,
  492. uint32_t ulPhyMask )
  493. {
  494. BaseType_t xPhyIndex;
  495. uint32_t ulValue, ulBitMask = ( uint32_t ) 1U;
  496. TickType_t xRemainingTime;
  497. TimeOut_t xTimer;
  498. uint32_t ulDoneMask, ulRegValue;
  499. ulValue = ( uint32_t ) 0U;
  500. if( pxPhyObject->xPhyPreferences.ucDuplex == PHY_DUPLEX_FULL ) {
  501. ulValue |= phyBMCR_FULL_DUPLEX;
  502. }
  503. if( pxPhyObject->xPhyPreferences.ucSpeed == PHY_SPEED_100 ) {
  504. ulValue |= phyBMCR_SPEED_100;
  505. }
  506. for (xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1) {
  507. if (( ulPhyMask & ulBitMask ) != 0lu) {
  508. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  509. #if CLAUSE45_ENABLE
  510. xPhyC45Write(pxPhyObject, xPhyAddress, 1, phyREG_00_BMCR, ulValue);
  511. #else
  512. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, ulValue );
  513. #endif
  514. }
  515. }
  516. xRemainingTime = ( TickType_t ) pdMS_TO_TICKS( phyPHY_MAX_NEGOTIATE_TIME_MS );
  517. vTaskSetTimeOutState( &xTimer );
  518. pxPhyObject->ulLinkStatusMask = 0;
  519. ulDoneMask = 0;
  520. for ( ; ; ) {
  521. ulBitMask = ( uint32_t ) 1U;
  522. for ( xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  523. {
  524. if ((ulPhyMask & ulBitMask) != 0lu) {
  525. if ((ulDoneMask & ulBitMask) == 0lu) {
  526. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[xPhyIndex];
  527. #if CLAUSE45_ENABLE
  528. xPhyC45Read(pxPhyObject, xPhyAddress, 1, phyREG_01_BMSR, &ulRegValue);
  529. #else
  530. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulRegValue );
  531. #endif
  532. if ((ulRegValue & phyBMSR_LINK_STATUS) != 0) {
  533. pxPhyObject->ulLinkStatusMask |= ulBitMask;
  534. ulDoneMask |= ulBitMask;
  535. }
  536. }
  537. }
  538. }
  539. if ( ulPhyMask == ulDoneMask ) {
  540. break;
  541. }
  542. if ( xTaskCheckForTimeOut( &xTimer, &xRemainingTime ) != pdFALSE ) {
  543. printf("xPhyFixedValue connect timeout!\n");
  544. break;
  545. }
  546. vTaskDelay( pdMS_TO_TICKS( phySHORT_DELAY_MS ) );
  547. }
  548. return 0;
  549. }
  550. /*-----------------------------------------------------------*/
  551. /* xPhyStartAutoNegotiation() is the alternative xPhyFixedValue():
  552. * It sets the BMCR_AN_RESTART bit and waits for the auto-negotiation completion
  553. * ( phyBMSR_AN_COMPLETE ). */
  554. BaseType_t xPhyStartAutoNegotiation( EthernetPhy_t * pxPhyObject,
  555. uint32_t ulPhyMask )
  556. {
  557. uint32_t xPhyIndex, ulDoneMask, ulBitMask;
  558. uint32_t ulPHYLinkStatus, ulRegValue;
  559. TickType_t xRemainingTime;
  560. TimeOut_t xTimer;
  561. uint8_t speed = PHY_SPEED_100;
  562. uint8_t duplex = PHY_DUPLEX_FULL;
  563. if (ulPhyMask == ( uint32_t ) 0U) {
  564. return 0;
  565. }
  566. for (xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++) {
  567. if ((ulPhyMask & ( 1lu << xPhyIndex)) != 0lu ) {
  568. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  569. /* Enable Auto-Negotiation. */
  570. #if CLAUSE45_ENABLE
  571. xPhyC45Write(pxPhyObject, xPhyAddress, 1, phyREG_04_ADVERTISE, pxPhyObject->ulACRValue);
  572. xPhyC45Write(pxPhyObject, xPhyAddress, 1, phyREG_00_BMCR, pxPhyObject->ulBCRValue | phyBMCR_AN_RESTART);
  573. #else
  574. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_04_ADVERTISE, pxPhyObject->ulACRValue );
  575. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, pxPhyObject->ulBCRValue | phyBMCR_AN_RESTART );
  576. #endif
  577. }
  578. }
  579. xRemainingTime = ( TickType_t ) pdMS_TO_TICKS( phyPHY_MAX_NEGOTIATE_TIME_MS );
  580. vTaskSetTimeOutState( &xTimer );
  581. ulDoneMask = 0;
  582. /* Wait until the auto-negotiation will be completed */
  583. for ( ; ; ) {
  584. ulBitMask = ( uint32_t ) 1U;
  585. for ( xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  586. {
  587. if ((ulPhyMask & ulBitMask) != 0lu) {
  588. if ((ulDoneMask & ulBitMask) == 0lu) {
  589. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[xPhyIndex];
  590. #if CLAUSE45_ENABLE
  591. xPhyC45Read(pxPhyObject, xPhyAddress, 1, phyREG_01_BMSR, &ulRegValue);
  592. #else
  593. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulRegValue);
  594. #endif
  595. if ((ulRegValue & phyBMSR_AN_COMPLETE) != 0) {
  596. ulDoneMask |= ulBitMask;
  597. }
  598. }
  599. }
  600. }
  601. if ( ulPhyMask == ulDoneMask ) {
  602. break;
  603. }
  604. if ( xTaskCheckForTimeOut( &xTimer, &xRemainingTime ) != pdFALSE ) {
  605. FreeRTOS_printf(("xPhyStartAutoNegotiation: phyBMCR_RESET timed out ( done 0x%02lX )\n", \
  606. ulDoneMask ));
  607. break;
  608. }
  609. vTaskDelay( pdMS_TO_TICKS( phySHORT_DELAY_MS ) );
  610. }
  611. if (ulDoneMask != ( uint32_t ) 0U) {
  612. ulBitMask = ( uint32_t ) 1U;
  613. pxPhyObject->ulLinkStatusMask &= ~( ulDoneMask );
  614. for( xPhyIndex = 0; xPhyIndex < ( uint32_t ) pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  615. {
  616. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  617. uint32_t ulPhyID = pxPhyObject->ulPhyIDs[ xPhyIndex ];
  618. if( ( ulDoneMask & ulBitMask ) == ( uint32_t ) 0U ) {
  619. continue;
  620. }
  621. /* Clear the 'phyBMCR_AN_RESTART' bit. */
  622. #if CLAUSE45_ENABLE
  623. xPhyC45Write(pxPhyObject, xPhyAddress, 1, phyREG_00_BMCR, pxPhyObject->ulBCRValue);
  624. xPhyC45Read(pxPhyObject, xPhyAddress, 1, phyREG_01_BMSR, &ulRegValue);
  625. #else
  626. pxPhyObject->fnPhyWrite( xPhyAddress, phyREG_00_BMCR, pxPhyObject->ulBCRValue );
  627. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulRegValue );
  628. #endif
  629. if ((ulRegValue & phyBMSR_LINK_STATUS) != 0) {
  630. ulPHYLinkStatus |= phyBMSR_LINK_STATUS;
  631. pxPhyObject->ulLinkStatusMask |= ulBitMask;
  632. } else {
  633. ulPHYLinkStatus &= ~( phyBMSR_LINK_STATUS );
  634. }
  635. if (ulPhyID == PHY_ID_KSZ8081MNXIA) {
  636. uint32_t ulControlStatus;
  637. pxPhyObject->fnPhyRead( xPhyAddress, 0x1E, &ulControlStatus );
  638. switch(ulControlStatus & 0x07) {
  639. case 0x01:
  640. case 0x05:
  641. speed = PHY_SPEED_10;
  642. break;
  643. case 0x02:
  644. case 0x06:
  645. break;
  646. }
  647. switch( ulControlStatus & 0x07 )
  648. {
  649. case 0x05:
  650. case 0x06:
  651. duplex = PHY_DUPLEX_FULL;
  652. break;
  653. case 0x01:
  654. case 0x02:
  655. break;
  656. }
  657. } else if(xHas_1F_PHYSPCS( ulPhyID)) {
  658. /* 31 RW PHY Special Control Status */
  659. uint32_t ulControlStatus;
  660. pxPhyObject->fnPhyRead( xPhyAddress, phyREG_1F_PHYSPCS, &ulControlStatus );
  661. ulRegValue = 0;
  662. if( ( ulControlStatus & phyPHYSPCS_FULL_DUPLEX ) != 0 ) {
  663. duplex = PHY_DUPLEX_FULL;
  664. }
  665. if( ( ulControlStatus & phyPHYSPCS_SPEED_MASK ) == phyPHYSPCS_SPEED_10 ) {
  666. speed = PHY_SPEED_10;
  667. }
  668. } else if( ulPhyID == PHY_ID_RTL8211FD) {
  669. uint32_t ulPhyStatus;
  670. pxPhyObject->fnPhyWrite( xPhyAddress, 31, 0xa43);
  671. pxPhyObject->fnPhyRead( xPhyAddress, 0x1a, &ulPhyStatus);
  672. pxPhyObject->fnPhyWrite( xPhyAddress, 31, 0x0);
  673. ulRegValue = 0;
  674. if ((ulPhyStatus & (1 << 3)) != 0 ) {
  675. duplex = PHY_DUPLEX_FULL;
  676. }
  677. switch ((ulPhyStatus >> 4) & 0x3) {
  678. case 0:
  679. speed = PHY_SPEED_10;
  680. break;
  681. case 1:
  682. speed = PHY_SPEED_100;
  683. break;
  684. case 2:
  685. speed = PHY_SPEED_1000;
  686. break;
  687. }
  688. } else {
  689. /* Read the result of the auto-negotiation. */
  690. pxPhyObject->fnPhyRead( xPhyAddress, PHYREG_10_PHYSTS, &ulRegValue );
  691. }
  692. FreeRTOS_printf( ( "Autonego ready: %08lx: %s duplex %u mbit %s status\n",
  693. ulRegValue,
  694. (duplex == PHY_DUPLEX_FULL) ? "full" : "half",
  695. (speed == PHY_SPEED_10) ? 10 : ((speed == PHY_SPEED_100) ? 100 : 1000),
  696. ( ( ulPHYLinkStatus |= phyBMSR_LINK_STATUS ) != 0 ) ? "high" : "low" ) );
  697. pxPhyObject->xPhyProperties.ucDuplex = duplex;
  698. pxPhyObject->xPhyProperties.ucSpeed = speed;
  699. }
  700. } /* if( ulDoneMask != ( uint32_t) 0U ) */
  701. return 0;
  702. }
  703. /*-----------------------------------------------------------*/
  704. BaseType_t xPhyCheckLinkStatus( EthernetPhy_t * pxPhyObject,
  705. BaseType_t xHadReception )
  706. {
  707. uint32_t ulStatus, ulBitMask = 1U;
  708. BaseType_t xPhyIndex;
  709. BaseType_t xNeedCheck = pdFALSE;
  710. if( xHadReception > 0 )
  711. {
  712. /* A packet was received. No need to check for the PHY status now,
  713. * but set a timer to check it later on. */
  714. vTaskSetTimeOutState( &( pxPhyObject->xLinkStatusTimer ) );
  715. pxPhyObject->xLinkStatusRemaining = pdMS_TO_TICKS( ipconfigPHY_LS_HIGH_CHECK_TIME_MS );
  716. for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  717. {
  718. if( ( pxPhyObject->ulLinkStatusMask & ulBitMask ) == 0UL )
  719. {
  720. pxPhyObject->ulLinkStatusMask |= ulBitMask;
  721. FreeRTOS_printf( ( "xPhyCheckLinkStatus: PHY LS now %02lX\n", pxPhyObject->ulLinkStatusMask ) );
  722. xNeedCheck = pdTRUE;
  723. }
  724. }
  725. }
  726. else if( xTaskCheckForTimeOut( &( pxPhyObject->xLinkStatusTimer ), &( pxPhyObject->xLinkStatusRemaining ) ) != pdFALSE )
  727. {
  728. /* Frequent checking the PHY Link Status can affect for the performance of Ethernet controller.
  729. * As long as packets are received, no polling is needed.
  730. * Otherwise, polling will be done when the 'xLinkStatusTimer' expires. */
  731. for( xPhyIndex = 0; xPhyIndex < pxPhyObject->xPortCount; xPhyIndex++, ulBitMask <<= 1 )
  732. {
  733. BaseType_t xPhyAddress = pxPhyObject->ucPhyIndexes[ xPhyIndex ];
  734. #if CLAUSE45_ENABLE
  735. if(xPhyC45Read(pxPhyObject, xPhyAddress, 1, phyREG_01_BMSR, &ulStatus ) == 0 )
  736. #else
  737. if( pxPhyObject->fnPhyRead( xPhyAddress, phyREG_01_BMSR, &ulStatus ) == 0 )
  738. #endif
  739. {
  740. if( !!( pxPhyObject->ulLinkStatusMask & ulBitMask ) != !!( ulStatus & phyBMSR_LINK_STATUS ) )
  741. {
  742. if( ( ulStatus & phyBMSR_LINK_STATUS ) != 0 )
  743. {
  744. pxPhyObject->ulLinkStatusMask |= ulBitMask;
  745. }
  746. else
  747. {
  748. pxPhyObject->ulLinkStatusMask &= ~( ulBitMask );
  749. }
  750. FreeRTOS_printf( ( "xPhyCheckLinkStatus: PHY LS now %02lX\n", pxPhyObject->ulLinkStatusMask ) );
  751. xNeedCheck = pdTRUE;
  752. }
  753. }
  754. }
  755. vTaskSetTimeOutState( &( pxPhyObject->xLinkStatusTimer ) );
  756. if( ( pxPhyObject->ulLinkStatusMask & ( ulBitMask >> 1 ) ) != 0 )
  757. {
  758. /* The link status is high, so don't poll the PHY too often. */
  759. pxPhyObject->xLinkStatusRemaining = pdMS_TO_TICKS( ipconfigPHY_LS_HIGH_CHECK_TIME_MS );
  760. }
  761. else
  762. {
  763. /* The link status is low, polling may be done more frequently. */
  764. pxPhyObject->xLinkStatusRemaining = pdMS_TO_TICKS( ipconfigPHY_LS_LOW_CHECK_TIME_MS );
  765. }
  766. }
  767. return xNeedCheck;
  768. }
  769. /*-----------------------------------------------------------*/