ns7520_eth.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /***********************************************************************
  2. *
  3. * Copyright (C) 2005 by Videon Central, Inc.
  4. *
  5. * $Id$
  6. * @Author: Arthur Shipkowski
  7. * @Descr: Ethernet driver for the NS7520. Uses polled Ethernet, like
  8. * the older netarmeth driver. Note that attempting to filter
  9. * broadcast and multicast out in the SAFR register will cause
  10. * bad things due to released errata.
  11. * @References: [1] NS7520 Hardware Reference, December 2003
  12. * [2] Intel LXT971 Datasheet #249414 Rev. 02
  13. *
  14. ***********************************************************************/
  15. #include <common.h>
  16. #include <net.h> /* NetSendPacket */
  17. #include <asm/arch/netarm_registers.h>
  18. #include <asm/arch/netarm_dma_module.h>
  19. #include "ns7520_eth.h" /* for Ethernet and PHY */
  20. /**
  21. * Send an error message to the terminal.
  22. */
  23. #define ERROR(x) \
  24. do { \
  25. char *__foo = strrchr(__FILE__, '/'); \
  26. \
  27. printf("%s: %d: %s(): ", (__foo == NULL ? __FILE__ : (__foo + 1)), \
  28. __LINE__, __FUNCTION__); \
  29. printf x; printf("\n"); \
  30. } while (0);
  31. /* some definition to make transistion to linux easier */
  32. #define NS7520_DRIVER_NAME "eth"
  33. #define KERN_WARNING "Warning:"
  34. #define KERN_ERR "Error:"
  35. #define KERN_INFO "Info:"
  36. #if 1
  37. # define DEBUG
  38. #endif
  39. #ifdef DEBUG
  40. # define printk printf
  41. # define DEBUG_INIT 0x0001
  42. # define DEBUG_MINOR 0x0002
  43. # define DEBUG_RX 0x0004
  44. # define DEBUG_TX 0x0008
  45. # define DEBUG_INT 0x0010
  46. # define DEBUG_POLL 0x0020
  47. # define DEBUG_LINK 0x0040
  48. # define DEBUG_MII 0x0100
  49. # define DEBUG_MII_LOW 0x0200
  50. # define DEBUG_MEM 0x0400
  51. # define DEBUG_ERROR 0x4000
  52. # define DEBUG_ERROR_CRIT 0x8000
  53. static int nDebugLvl = DEBUG_ERROR_CRIT;
  54. # define DEBUG_ARGS0( FLG, a0 ) if( ( nDebugLvl & (FLG) ) == (FLG) ) \
  55. printf("%s: " a0, __FUNCTION__, 0, 0, 0, 0, 0, 0 )
  56. # define DEBUG_ARGS1( FLG, a0, a1 ) if( ( nDebugLvl & (FLG) ) == (FLG)) \
  57. printf("%s: " a0, __FUNCTION__, (int)(a1), 0, 0, 0, 0, 0 )
  58. # define DEBUG_ARGS2( FLG, a0, a1, a2 ) if( (nDebugLvl & (FLG)) ==(FLG))\
  59. printf("%s: " a0, __FUNCTION__, (int)(a1), (int)(a2), 0, 0,0,0 )
  60. # define DEBUG_ARGS3( FLG, a0, a1, a2, a3 ) if((nDebugLvl &(FLG))==(FLG))\
  61. printf("%s: "a0,__FUNCTION__,(int)(a1),(int)(a2),(int)(a3),0,0,0)
  62. # define DEBUG_FN( FLG ) if( (nDebugLvl & (FLG)) == (FLG) ) \
  63. printf("\r%s:line %d\n", (int)__FUNCTION__, __LINE__, 0,0,0,0);
  64. # define ASSERT( expr, func ) if( !( expr ) ) { \
  65. printf( "Assertion failed! %s:line %d %s\n", \
  66. (int)__FUNCTION__,__LINE__,(int)(#expr),0,0,0); \
  67. func }
  68. #else /* DEBUG */
  69. # define printk(...)
  70. # define DEBUG_ARGS0( FLG, a0 )
  71. # define DEBUG_ARGS1( FLG, a0, a1 )
  72. # define DEBUG_ARGS2( FLG, a0, a1, a2 )
  73. # define DEBUG_ARGS3( FLG, a0, a1, a2, a3 )
  74. # define DEBUG_FN( n )
  75. # define ASSERT(expr, func)
  76. #endif /* DEBUG */
  77. #define NS7520_MII_NEG_DELAY (5*CFG_HZ) /* in s */
  78. #define TX_TIMEOUT (5*CFG_HZ) /* in s */
  79. #define RX_STALL_WORKAROUND_CNT 100
  80. static int ns7520_eth_reset(void);
  81. static void ns7520_link_auto_negotiate(void);
  82. static void ns7520_link_update_egcr(void);
  83. static void ns7520_link_print_changed(void);
  84. /* the PHY stuff */
  85. static char ns7520_mii_identify_phy(void);
  86. static unsigned short ns7520_mii_read(unsigned short uiRegister);
  87. static void ns7520_mii_write(unsigned short uiRegister,
  88. unsigned short uiData);
  89. static unsigned int ns7520_mii_get_clock_divisor(unsigned int
  90. unMaxMDIOClk);
  91. static unsigned int ns7520_mii_poll_busy(void);
  92. static unsigned int nPhyMaxMdioClock = PHY_MDIO_MAX_CLK;
  93. static unsigned int uiLastLinkStatus;
  94. static PhyType phyDetected = PHY_NONE;
  95. /***********************************************************************
  96. * @Function: eth_init
  97. * @Return: -1 on failure otherwise 0
  98. * @Descr: Initializes the ethernet engine and uses either FS Forth's default
  99. * MAC addr or the one in environment
  100. ***********************************************************************/
  101. int eth_init(bd_t * pbis)
  102. {
  103. unsigned char aucMACAddr[6];
  104. char *pcTmp = getenv("ethaddr");
  105. char *pcEnd;
  106. int i;
  107. DEBUG_FN(DEBUG_INIT);
  108. /* no need to check for hardware */
  109. if (!ns7520_eth_reset())
  110. return -1;
  111. if (NULL == pcTmp)
  112. return -1;
  113. for (i = 0; i < 6; i++) {
  114. aucMACAddr[i] =
  115. pcTmp ? simple_strtoul(pcTmp, &pcEnd, 16) : 0;
  116. pcTmp = (*pcTmp) ? pcEnd + 1 : pcEnd;
  117. }
  118. /* configure ethernet address */
  119. *get_eth_reg_addr(NS7520_ETH_SA1) =
  120. aucMACAddr[5] << 8 | aucMACAddr[4];
  121. *get_eth_reg_addr(NS7520_ETH_SA2) =
  122. aucMACAddr[3] << 8 | aucMACAddr[2];
  123. *get_eth_reg_addr(NS7520_ETH_SA3) =
  124. aucMACAddr[1] << 8 | aucMACAddr[0];
  125. /* enable hardware */
  126. *get_eth_reg_addr(NS7520_ETH_MAC1) = NS7520_ETH_MAC1_RXEN;
  127. *get_eth_reg_addr(NS7520_ETH_SUPP) = NS7520_ETH_SUPP_JABBER;
  128. *get_eth_reg_addr(NS7520_ETH_MAC1) = NS7520_ETH_MAC1_RXEN;
  129. /* the linux kernel may give packets < 60 bytes, for example arp */
  130. *get_eth_reg_addr(NS7520_ETH_MAC2) = NS7520_ETH_MAC2_CRCEN |
  131. NS7520_ETH_MAC2_PADEN | NS7520_ETH_MAC2_HUGE;
  132. /* Broadcast/multicast allowed; if you don't set this even unicast chokes */
  133. /* Based on NS7520 errata documentation */
  134. *get_eth_reg_addr(NS7520_ETH_SAFR) =
  135. NS7520_ETH_SAFR_BROAD | NS7520_ETH_SAFR_PRM;
  136. /* enable receive and transmit FIFO, use 10/100 Mbps MII */
  137. *get_eth_reg_addr(NS7520_ETH_EGCR) |=
  138. NS7520_ETH_EGCR_ETXWM_75 |
  139. NS7520_ETH_EGCR_ERX |
  140. NS7520_ETH_EGCR_ERXREG |
  141. NS7520_ETH_EGCR_ERXBR | NS7520_ETH_EGCR_ETX;
  142. return 0;
  143. }
  144. /***********************************************************************
  145. * @Function: eth_send
  146. * @Return: -1 on timeout otherwise 1
  147. * @Descr: sends one frame by DMA
  148. ***********************************************************************/
  149. int eth_send(volatile void *pPacket, int nLen)
  150. {
  151. int i, length32, retval = 1;
  152. char *pa;
  153. unsigned int *pa32, lastp = 0, rest;
  154. unsigned int status;
  155. pa = (char *) pPacket;
  156. pa32 = (unsigned int *) pPacket;
  157. length32 = nLen / 4;
  158. rest = nLen % 4;
  159. /* make sure there's no garbage in the last word */
  160. switch (rest) {
  161. case 0:
  162. lastp = pa32[length32 - 1];
  163. length32--;
  164. break;
  165. case 1:
  166. lastp = pa32[length32] & 0x000000ff;
  167. break;
  168. case 2:
  169. lastp = pa32[length32] & 0x0000ffff;
  170. break;
  171. case 3:
  172. lastp = pa32[length32] & 0x00ffffff;
  173. break;
  174. }
  175. while (((*get_eth_reg_addr(NS7520_ETH_EGSR)) &
  176. NS7520_ETH_EGSR_TXREGE)
  177. == 0) {
  178. }
  179. /* write to the fifo */
  180. for (i = 0; i < length32; i++)
  181. *get_eth_reg_addr(NS7520_ETH_FIFO) = pa32[i];
  182. /* the last word is written to an extra register, this
  183. starts the transmission */
  184. *get_eth_reg_addr(NS7520_ETH_FIFOL) = lastp;
  185. /* Wait for it to be done */
  186. while ((*get_eth_reg_addr(NS7520_ETH_EGSR) & NS7520_ETH_EGSR_TXBC)
  187. == 0) {
  188. }
  189. status = (*get_eth_reg_addr(NS7520_ETH_ETSR));
  190. *get_eth_reg_addr(NS7520_ETH_EGSR) = NS7520_ETH_EGSR_TXBC; /* Clear it now */
  191. if (status & NS7520_ETH_ETSR_TXOK) {
  192. retval = 0; /* We're OK! */
  193. } else if (status & NS7520_ETH_ETSR_TXDEF) {
  194. printf("Deferred, we'll see.\n");
  195. retval = 0;
  196. } else if (status & NS7520_ETH_ETSR_TXAL) {
  197. printf("Late collision error, %d collisions.\n",
  198. (*get_eth_reg_addr(NS7520_ETH_ETSR)) &
  199. NS7520_ETH_ETSR_TXCOLC);
  200. } else if (status & NS7520_ETH_ETSR_TXAEC) {
  201. printf("Excessive collisions: %d\n",
  202. (*get_eth_reg_addr(NS7520_ETH_ETSR)) &
  203. NS7520_ETH_ETSR_TXCOLC);
  204. } else if (status & NS7520_ETH_ETSR_TXAED) {
  205. printf("Excessive deferral on xmit.\n");
  206. } else if (status & NS7520_ETH_ETSR_TXAUR) {
  207. printf("Packet underrun.\n");
  208. } else if (status & NS7520_ETH_ETSR_TXAJ) {
  209. printf("Jumbo packet error.\n");
  210. } else {
  211. printf("Error: Should never get here.\n");
  212. }
  213. return (retval);
  214. }
  215. /***********************************************************************
  216. * @Function: eth_rx
  217. * @Return: size of last frame in bytes or 0 if no frame available
  218. * @Descr: gives one frame to U-Boot which has been copied by DMA engine already
  219. * to NetRxPackets[ 0 ].
  220. ***********************************************************************/
  221. int eth_rx(void)
  222. {
  223. int i;
  224. unsigned short rxlen;
  225. unsigned short totrxlen = 0;
  226. unsigned int *addr;
  227. unsigned int rxstatus, lastrxlen;
  228. char *pa;
  229. /* If RXBR is 1, data block was received */
  230. while (((*get_eth_reg_addr(NS7520_ETH_EGSR)) &
  231. NS7520_ETH_EGSR_RXBR) == NS7520_ETH_EGSR_RXBR) {
  232. /* get status register and the length of received block */
  233. rxstatus = *get_eth_reg_addr(NS7520_ETH_ERSR);
  234. rxlen = (rxstatus & NS7520_ETH_ERSR_RXSIZE) >> 16;
  235. /* clear RXBR to make fifo available */
  236. *get_eth_reg_addr(NS7520_ETH_EGSR) = NS7520_ETH_EGSR_RXBR;
  237. if (rxstatus & NS7520_ETH_ERSR_ROVER) {
  238. printf("Receive overrun, resetting FIFO.\n");
  239. *get_eth_reg_addr(NS7520_ETH_EGCR) &=
  240. ~NS7520_ETH_EGCR_ERX;
  241. udelay(20);
  242. *get_eth_reg_addr(NS7520_ETH_EGCR) |=
  243. NS7520_ETH_EGCR_ERX;
  244. }
  245. if (rxlen == 0) {
  246. printf("Nothing.\n");
  247. return 0;
  248. }
  249. addr = (unsigned int *) NetRxPackets[0];
  250. pa = (char *) NetRxPackets[0];
  251. /* read the fifo */
  252. for (i = 0; i < rxlen / 4; i++) {
  253. *addr = *get_eth_reg_addr(NS7520_ETH_FIFO);
  254. addr++;
  255. }
  256. if ((*get_eth_reg_addr(NS7520_ETH_EGSR)) &
  257. NS7520_ETH_EGSR_RXREGR) {
  258. /* RXFDB indicates wether the last word is 1,2,3 or 4 bytes long */
  259. lastrxlen =
  260. ((*get_eth_reg_addr(NS7520_ETH_EGSR)) &
  261. NS7520_ETH_EGSR_RXFDB_MA) >> 28;
  262. *addr = *get_eth_reg_addr(NS7520_ETH_FIFO);
  263. switch (lastrxlen) {
  264. case 1:
  265. *addr &= 0xff000000;
  266. break;
  267. case 2:
  268. *addr &= 0xffff0000;
  269. break;
  270. case 3:
  271. *addr &= 0xffffff00;
  272. break;
  273. }
  274. }
  275. /* Pass the packet up to the protocol layers. */
  276. NetReceive(NetRxPackets[0], rxlen - 4);
  277. totrxlen += rxlen - 4;
  278. }
  279. return totrxlen;
  280. }
  281. /***********************************************************************
  282. * @Function: eth_halt
  283. * @Return: n/a
  284. * @Descr: stops the ethernet engine
  285. ***********************************************************************/
  286. void eth_halt(void)
  287. {
  288. DEBUG_FN(DEBUG_INIT);
  289. *get_eth_reg_addr(NS7520_ETH_MAC1) &= ~NS7520_ETH_MAC1_RXEN;
  290. *get_eth_reg_addr(NS7520_ETH_EGCR) &= ~(NS7520_ETH_EGCR_ERX |
  291. NS7520_ETH_EGCR_ERXDMA |
  292. NS7520_ETH_EGCR_ERXREG |
  293. NS7520_ETH_EGCR_ERXBR |
  294. NS7520_ETH_EGCR_ETX |
  295. NS7520_ETH_EGCR_ETXDMA);
  296. }
  297. /***********************************************************************
  298. * @Function: ns7520_eth_reset
  299. * @Return: 0 on failure otherwise 1
  300. * @Descr: resets the ethernet interface and the PHY,
  301. * performs auto negotiation or fixed modes
  302. ***********************************************************************/
  303. static int ns7520_eth_reset(void)
  304. {
  305. DEBUG_FN(DEBUG_MINOR);
  306. /* Reset important registers */
  307. *get_eth_reg_addr(NS7520_ETH_EGCR) = 0; /* Null it out! */
  308. *get_eth_reg_addr(NS7520_ETH_MAC1) &= NS7520_ETH_MAC1_SRST;
  309. *get_eth_reg_addr(NS7520_ETH_MAC2) = 0;
  310. /* Reset MAC */
  311. *get_eth_reg_addr(NS7520_ETH_EGCR) |= NS7520_ETH_EGCR_MAC_RES;
  312. udelay(5);
  313. *get_eth_reg_addr(NS7520_ETH_EGCR) &= ~NS7520_ETH_EGCR_MAC_RES;
  314. /* reset and initialize PHY */
  315. *get_eth_reg_addr(NS7520_ETH_MAC1) &= ~NS7520_ETH_MAC1_SRST;
  316. /* we don't support hot plugging of PHY, therefore we don't reset
  317. phyDetected and nPhyMaxMdioClock here. The risk is if the setting is
  318. incorrect the first open
  319. may detect the PHY correctly but succeding will fail
  320. For reseting the PHY and identifying we have to use the standard
  321. MDIO CLOCK value 2.5 MHz only after hardware reset
  322. After having identified the PHY we will do faster */
  323. *get_eth_reg_addr(NS7520_ETH_MCFG) =
  324. ns7520_mii_get_clock_divisor(nPhyMaxMdioClock);
  325. /* reset PHY */
  326. ns7520_mii_write(PHY_BMCR, PHY_BMCR_RESET);
  327. ns7520_mii_write(PHY_BMCR, 0);
  328. udelay(3000); /* [2] p.70 says at least 300us reset recovery time. */
  329. /* MII clock has been setup to default, ns7520_mii_identify_phy should
  330. work for all */
  331. if (!ns7520_mii_identify_phy()) {
  332. printk(KERN_ERR NS7520_DRIVER_NAME
  333. ": Unsupported PHY, aborting\n");
  334. return 0;
  335. }
  336. /* now take the highest MDIO clock possible after detection */
  337. *get_eth_reg_addr(NS7520_ETH_MCFG) =
  338. ns7520_mii_get_clock_divisor(nPhyMaxMdioClock);
  339. /* PHY has been detected, so there can be no abort reason and we can
  340. finish initializing ethernet */
  341. uiLastLinkStatus = 0xff; /* undefined */
  342. ns7520_link_auto_negotiate();
  343. if (phyDetected == PHY_LXT971A)
  344. /* set LED2 to link mode */
  345. ns7520_mii_write(PHY_LXT971_LED_CFG,
  346. (PHY_LXT971_LED_CFG_LINK_ACT <<
  347. PHY_LXT971_LED_CFG_SHIFT_LED2) |
  348. (PHY_LXT971_LED_CFG_TRANSMIT <<
  349. PHY_LXT971_LED_CFG_SHIFT_LED1));
  350. return 1;
  351. }
  352. /***********************************************************************
  353. * @Function: ns7520_link_auto_negotiate
  354. * @Return: void
  355. * @Descr: performs auto-negotation of link.
  356. ***********************************************************************/
  357. static void ns7520_link_auto_negotiate(void)
  358. {
  359. unsigned long ulStartJiffies;
  360. unsigned short uiStatus;
  361. DEBUG_FN(DEBUG_LINK);
  362. /* run auto-negotation */
  363. /* define what we are capable of */
  364. ns7520_mii_write(PHY_ANAR,
  365. PHY_ANLPAR_TXFD |
  366. PHY_ANLPAR_TX |
  367. PHY_ANLPAR_10FD |
  368. PHY_ANLPAR_10 |
  369. PHY_ANLPAR_PSB_802_3);
  370. /* start auto-negotiation */
  371. ns7520_mii_write(PHY_BMCR, PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
  372. /* wait for completion */
  373. ulStartJiffies = get_timer(0);
  374. while (get_timer(0) < ulStartJiffies + NS7520_MII_NEG_DELAY) {
  375. uiStatus = ns7520_mii_read(PHY_BMSR);
  376. if ((uiStatus &
  377. (PHY_BMSR_AUTN_COMP | PHY_BMSR_LS)) ==
  378. (PHY_BMSR_AUTN_COMP | PHY_BMSR_LS)) {
  379. /* lucky we are, auto-negotiation succeeded */
  380. ns7520_link_print_changed();
  381. ns7520_link_update_egcr();
  382. return;
  383. }
  384. }
  385. DEBUG_ARGS0(DEBUG_LINK, "auto-negotiation timed out\n");
  386. /* ignore invalid link settings */
  387. }
  388. /***********************************************************************
  389. * @Function: ns7520_link_update_egcr
  390. * @Return: void
  391. * @Descr: updates the EGCR and MAC2 link status after mode change or
  392. * auto-negotation
  393. ***********************************************************************/
  394. static void ns7520_link_update_egcr(void)
  395. {
  396. unsigned int unEGCR;
  397. unsigned int unMAC2;
  398. unsigned int unIPGT;
  399. DEBUG_FN(DEBUG_LINK);
  400. unEGCR = *get_eth_reg_addr(NS7520_ETH_EGCR);
  401. unMAC2 = *get_eth_reg_addr(NS7520_ETH_MAC2);
  402. unIPGT =
  403. *get_eth_reg_addr(NS7520_ETH_IPGT) & ~NS7520_ETH_IPGT_IPGT;
  404. unEGCR &= ~NS7520_ETH_EGCR_EFULLD;
  405. unMAC2 &= ~NS7520_ETH_MAC2_FULLD;
  406. if ((uiLastLinkStatus & PHY_LXT971_STAT2_DUPLEX_MODE)
  407. == PHY_LXT971_STAT2_DUPLEX_MODE) {
  408. unEGCR |= NS7520_ETH_EGCR_EFULLD;
  409. unMAC2 |= NS7520_ETH_MAC2_FULLD;
  410. unIPGT |= 0x15; /* see [1] p. 167 */
  411. } else
  412. unIPGT |= 0x12; /* see [1] p. 167 */
  413. *get_eth_reg_addr(NS7520_ETH_MAC2) = unMAC2;
  414. *get_eth_reg_addr(NS7520_ETH_EGCR) = unEGCR;
  415. *get_eth_reg_addr(NS7520_ETH_IPGT) = unIPGT;
  416. }
  417. /***********************************************************************
  418. * @Function: ns7520_link_print_changed
  419. * @Return: void
  420. * @Descr: checks whether the link status has changed and if so prints
  421. * the new mode
  422. ***********************************************************************/
  423. static void ns7520_link_print_changed(void)
  424. {
  425. unsigned short uiStatus;
  426. unsigned short uiControl;
  427. DEBUG_FN(DEBUG_LINK);
  428. uiControl = ns7520_mii_read(PHY_BMCR);
  429. if ((uiControl & PHY_BMCR_AUTON) == PHY_BMCR_AUTON) {
  430. /* PHY_BMSR_LS is only set on autonegotiation */
  431. uiStatus = ns7520_mii_read(PHY_BMSR);
  432. if (!(uiStatus & PHY_BMSR_LS)) {
  433. printk(KERN_WARNING NS7520_DRIVER_NAME
  434. ": link down\n");
  435. /* @TODO Linux: carrier_off */
  436. } else {
  437. /* @TODO Linux: carrier_on */
  438. if (phyDetected == PHY_LXT971A) {
  439. uiStatus =
  440. ns7520_mii_read(PHY_LXT971_STAT2);
  441. uiStatus &=
  442. (PHY_LXT971_STAT2_100BTX |
  443. PHY_LXT971_STAT2_DUPLEX_MODE |
  444. PHY_LXT971_STAT2_AUTO_NEG);
  445. /* mask out all uninteresting parts */
  446. }
  447. /* other PHYs must store there link information in
  448. uiStatus as PHY_LXT971 */
  449. }
  450. } else {
  451. /* mode has been forced, so uiStatus should be the same as the
  452. last link status, enforce printing */
  453. uiStatus = uiLastLinkStatus;
  454. uiLastLinkStatus = 0xff;
  455. }
  456. if (uiStatus != uiLastLinkStatus) {
  457. /* save current link status */
  458. uiLastLinkStatus = uiStatus;
  459. /* print new link status */
  460. printk(KERN_INFO NS7520_DRIVER_NAME
  461. ": link mode %i Mbps %s duplex %s\n",
  462. (uiStatus & PHY_LXT971_STAT2_100BTX) ? 100 : 10,
  463. (uiStatus & PHY_LXT971_STAT2_DUPLEX_MODE) ? "full" :
  464. "half",
  465. (uiStatus & PHY_LXT971_STAT2_AUTO_NEG) ? "(auto)" :
  466. "");
  467. }
  468. }
  469. /***********************************************************************
  470. * the MII low level stuff
  471. ***********************************************************************/
  472. /***********************************************************************
  473. * @Function: ns7520_mii_identify_phy
  474. * @Return: 1 if supported PHY has been detected otherwise 0
  475. * @Descr: checks for supported PHY and prints the IDs.
  476. ***********************************************************************/
  477. static char ns7520_mii_identify_phy(void)
  478. {
  479. unsigned short uiID1;
  480. unsigned short uiID2;
  481. unsigned char *szName;
  482. char cRes = 0;
  483. DEBUG_FN(DEBUG_MII);
  484. phyDetected = (PhyType) uiID1 = ns7520_mii_read(PHY_PHYIDR1);
  485. switch (phyDetected) {
  486. case PHY_LXT971A:
  487. szName = "LXT971A";
  488. uiID2 = ns7520_mii_read(PHY_PHYIDR2);
  489. nPhyMaxMdioClock = PHY_LXT971_MDIO_MAX_CLK;
  490. cRes = 1;
  491. break;
  492. case PHY_NONE:
  493. default:
  494. /* in case uiID1 == 0 && uiID2 == 0 we may have the wrong
  495. address or reset sets the wrong NS7520_ETH_MCFG_CLKS */
  496. uiID2 = 0;
  497. szName = "unknown";
  498. nPhyMaxMdioClock = PHY_MDIO_MAX_CLK;
  499. phyDetected = PHY_NONE;
  500. }
  501. printk(KERN_INFO NS7520_DRIVER_NAME
  502. ": PHY (0x%x, 0x%x) = %s detected\n", uiID1, uiID2, szName);
  503. return cRes;
  504. }
  505. /***********************************************************************
  506. * @Function: ns7520_mii_read
  507. * @Return: the data read from PHY register uiRegister
  508. * @Descr: the data read may be invalid if timed out. If so, a message
  509. * is printed but the invalid data is returned.
  510. * The fixed device address is being used.
  511. ***********************************************************************/
  512. static unsigned short ns7520_mii_read(unsigned short uiRegister)
  513. {
  514. DEBUG_FN(DEBUG_MII_LOW);
  515. /* write MII register to be read */
  516. *get_eth_reg_addr(NS7520_ETH_MADR) =
  517. CONFIG_PHY_ADDR << 8 | uiRegister;
  518. *get_eth_reg_addr(NS7520_ETH_MCMD) = NS7520_ETH_MCMD_READ;
  519. if (!ns7520_mii_poll_busy())
  520. printk(KERN_WARNING NS7520_DRIVER_NAME
  521. ": MII still busy in read\n");
  522. /* continue to read */
  523. *get_eth_reg_addr(NS7520_ETH_MCMD) = 0;
  524. return (unsigned short) (*get_eth_reg_addr(NS7520_ETH_MRDD));
  525. }
  526. /***********************************************************************
  527. * @Function: ns7520_mii_write
  528. * @Return: nothing
  529. * @Descr: writes the data to the PHY register. In case of a timeout,
  530. * no special handling is performed but a message printed
  531. * The fixed device address is being used.
  532. ***********************************************************************/
  533. static void ns7520_mii_write(unsigned short uiRegister,
  534. unsigned short uiData)
  535. {
  536. DEBUG_FN(DEBUG_MII_LOW);
  537. /* write MII register to be written */
  538. *get_eth_reg_addr(NS7520_ETH_MADR) =
  539. CONFIG_PHY_ADDR << 8 | uiRegister;
  540. *get_eth_reg_addr(NS7520_ETH_MWTD) = uiData;
  541. if (!ns7520_mii_poll_busy()) {
  542. printf(KERN_WARNING NS7520_DRIVER_NAME
  543. ": MII still busy in write\n");
  544. }
  545. }
  546. /***********************************************************************
  547. * @Function: ns7520_mii_get_clock_divisor
  548. * @Return: the clock divisor that should be used in NS7520_ETH_MCFG_CLKS
  549. * @Descr: if no clock divisor can be calculated for the
  550. * current SYSCLK and the maximum MDIO Clock, a warning is printed
  551. * and the greatest divisor is taken
  552. ***********************************************************************/
  553. static unsigned int ns7520_mii_get_clock_divisor(unsigned int unMaxMDIOClk)
  554. {
  555. struct {
  556. unsigned int unSysClkDivisor;
  557. unsigned int unClks; /* field for NS7520_ETH_MCFG_CLKS */
  558. } PHYClockDivisors[] = {
  559. {
  560. 4, NS7520_ETH_MCFG_CLKS_4}, {
  561. 6, NS7520_ETH_MCFG_CLKS_6}, {
  562. 8, NS7520_ETH_MCFG_CLKS_8}, {
  563. 10, NS7520_ETH_MCFG_CLKS_10}, {
  564. 14, NS7520_ETH_MCFG_CLKS_14}, {
  565. 20, NS7520_ETH_MCFG_CLKS_20}, {
  566. 28, NS7520_ETH_MCFG_CLKS_28}
  567. };
  568. int nIndexSysClkDiv;
  569. int nArraySize =
  570. sizeof(PHYClockDivisors) / sizeof(PHYClockDivisors[0]);
  571. unsigned int unClks = NS7520_ETH_MCFG_CLKS_28; /* defaults to
  572. greatest div */
  573. DEBUG_FN(DEBUG_INIT);
  574. for (nIndexSysClkDiv = 0; nIndexSysClkDiv < nArraySize;
  575. nIndexSysClkDiv++) {
  576. /* find first sysclock divisor that isn't higher than 2.5 MHz
  577. clock */
  578. if (NETARM_XTAL_FREQ /
  579. PHYClockDivisors[nIndexSysClkDiv].unSysClkDivisor <=
  580. unMaxMDIOClk) {
  581. unClks = PHYClockDivisors[nIndexSysClkDiv].unClks;
  582. break;
  583. }
  584. }
  585. DEBUG_ARGS2(DEBUG_INIT,
  586. "Taking MDIO Clock bit mask 0x%0x for max clock %i\n",
  587. unClks, unMaxMDIOClk);
  588. /* return greatest divisor */
  589. return unClks;
  590. }
  591. /***********************************************************************
  592. * @Function: ns7520_mii_poll_busy
  593. * @Return: 0 if timed out otherwise the remaing timeout
  594. * @Descr: waits until the MII has completed a command or it times out
  595. * code may be interrupted by hard interrupts.
  596. * It is not checked what happens on multiple actions when
  597. * the first is still being busy and we timeout.
  598. ***********************************************************************/
  599. static unsigned int ns7520_mii_poll_busy(void)
  600. {
  601. unsigned int unTimeout = 1000;
  602. DEBUG_FN(DEBUG_MII_LOW);
  603. while (((*get_eth_reg_addr(NS7520_ETH_MIND) & NS7520_ETH_MIND_BUSY)
  604. == NS7520_ETH_MIND_BUSY) && unTimeout)
  605. unTimeout--;
  606. return unTimeout;
  607. }
  608. /* ----------------------------------------------------------------------------
  609. * Net+ARM ethernet MII functionality.
  610. */
  611. #if defined(CONFIG_MII)
  612. /**
  613. * Maximum MII address we support
  614. */
  615. #define MII_ADDRESS_MAX (31)
  616. /**
  617. * Maximum MII register address we support
  618. */
  619. #define MII_REGISTER_MAX (31)
  620. /**
  621. * Ethernet MII interface return values for public functions.
  622. */
  623. enum mii_status {
  624. MII_STATUS_SUCCESS = 0,
  625. MII_STATUS_FAILURE = 1,
  626. };
  627. /**
  628. * Read a 16-bit value from an MII register.
  629. */
  630. extern int ns7520_miiphy_read(char *devname, unsigned char const addr,
  631. unsigned char const reg, unsigned short *const value)
  632. {
  633. int ret = MII_STATUS_FAILURE;
  634. /* Parameter checks */
  635. if (addr > MII_ADDRESS_MAX) {
  636. ERROR(("invalid addr, 0x%02X", addr));
  637. goto miiphy_read_failed_0;
  638. }
  639. if (reg > MII_REGISTER_MAX) {
  640. ERROR(("invalid reg, 0x%02X", reg));
  641. goto miiphy_read_failed_0;
  642. }
  643. if (value == NULL) {
  644. ERROR(("NULL value"));
  645. goto miiphy_read_failed_0;
  646. }
  647. DEBUG_FN(DEBUG_MII_LOW);
  648. /* write MII register to be read */
  649. *get_eth_reg_addr(NS7520_ETH_MADR) = (addr << 8) | reg;
  650. *get_eth_reg_addr(NS7520_ETH_MCMD) = NS7520_ETH_MCMD_READ;
  651. if (!ns7520_mii_poll_busy())
  652. printk(KERN_WARNING NS7520_DRIVER_NAME
  653. ": MII still busy in read\n");
  654. /* continue to read */
  655. *get_eth_reg_addr(NS7520_ETH_MCMD) = 0;
  656. *value = (*get_eth_reg_addr(NS7520_ETH_MRDD));
  657. ret = MII_STATUS_SUCCESS;
  658. /* Fall through */
  659. miiphy_read_failed_0:
  660. return (ret);
  661. }
  662. /**
  663. * Write a 16-bit value to an MII register.
  664. */
  665. extern int ns7520_miiphy_write(char *devname, unsigned char const addr,
  666. unsigned char const reg, unsigned short const value)
  667. {
  668. int ret = MII_STATUS_FAILURE;
  669. /* Parameter checks */
  670. if (addr > MII_ADDRESS_MAX) {
  671. ERROR(("invalid addr, 0x%02X", addr));
  672. goto miiphy_write_failed_0;
  673. }
  674. if (reg > MII_REGISTER_MAX) {
  675. ERROR(("invalid reg, 0x%02X", reg));
  676. goto miiphy_write_failed_0;
  677. }
  678. /* write MII register to be written */
  679. *get_eth_reg_addr(NS7520_ETH_MADR) = (addr << 8) | reg;
  680. *get_eth_reg_addr(NS7520_ETH_MWTD) = value;
  681. if (!ns7520_mii_poll_busy()) {
  682. printf(KERN_WARNING NS7520_DRIVER_NAME
  683. ": MII still busy in write\n");
  684. }
  685. ret = MII_STATUS_SUCCESS;
  686. /* Fall through */
  687. miiphy_write_failed_0:
  688. return (ret);
  689. }
  690. #endif /* defined(CONFIG_MII) */
  691. int ns7520_miiphy_initialize(bd_t *bis)
  692. {
  693. #if defined(CONFIG_MII)
  694. miiphy_register("ns7520phy", ns7520_miiphy_read, ns7520_miiphy_write);
  695. #endif
  696. return 0;
  697. }