tsec.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. /*
  2. * Freescale Three Speed Ethernet Controller driver
  3. *
  4. * This software may be used and distributed according to the
  5. * terms of the GNU Public License, Version 2, incorporated
  6. * herein by reference.
  7. *
  8. * Copyright 2004, 2007 Freescale Semiconductor, Inc.
  9. * (C) Copyright 2003, Motorola, Inc.
  10. * author Andy Fleming
  11. *
  12. */
  13. #include <config.h>
  14. #include <common.h>
  15. #include <malloc.h>
  16. #include <net.h>
  17. #include <command.h>
  18. #include <tsec.h>
  19. #include "miiphy.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. #define TX_BUF_CNT 2
  22. static uint rxIdx; /* index of the current RX buffer */
  23. static uint txIdx; /* index of the current TX buffer */
  24. typedef volatile struct rtxbd {
  25. txbd8_t txbd[TX_BUF_CNT];
  26. rxbd8_t rxbd[PKTBUFSRX];
  27. } RTXBD;
  28. #define MAXCONTROLLERS (8)
  29. static int relocated = 0;
  30. static struct tsec_private *privlist[MAXCONTROLLERS];
  31. static int num_tsecs = 0;
  32. #ifdef __GNUC__
  33. static RTXBD rtx __attribute__ ((aligned(8)));
  34. #else
  35. #error "rtx must be 64-bit aligned"
  36. #endif
  37. static int tsec_send(struct eth_device *dev,
  38. volatile void *packet, int length);
  39. static int tsec_recv(struct eth_device *dev);
  40. static int tsec_init(struct eth_device *dev, bd_t * bd);
  41. static void tsec_halt(struct eth_device *dev);
  42. static void init_registers(volatile tsec_t * regs);
  43. static void startup_tsec(struct eth_device *dev);
  44. static int init_phy(struct eth_device *dev);
  45. void write_phy_reg(struct tsec_private *priv, uint regnum, uint value);
  46. uint read_phy_reg(struct tsec_private *priv, uint regnum);
  47. struct phy_info *get_phy_info(struct eth_device *dev);
  48. void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd);
  49. static void adjust_link(struct eth_device *dev);
  50. static void relocate_cmds(void);
  51. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
  52. && !defined(BITBANGMII)
  53. static int tsec_miiphy_write(char *devname, unsigned char addr,
  54. unsigned char reg, unsigned short value);
  55. static int tsec_miiphy_read(char *devname, unsigned char addr,
  56. unsigned char reg, unsigned short *value);
  57. #endif
  58. #ifdef CONFIG_MCAST_TFTP
  59. static int tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set);
  60. #endif
  61. /* Default initializations for TSEC controllers. */
  62. static struct tsec_info_struct tsec_info[] = {
  63. #ifdef CONFIG_TSEC1
  64. STD_TSEC_INFO(1), /* TSEC1 */
  65. #endif
  66. #ifdef CONFIG_TSEC2
  67. STD_TSEC_INFO(2), /* TSEC2 */
  68. #endif
  69. #ifdef CONFIG_MPC85XX_FEC
  70. {
  71. .regs = (tsec_t *)(TSEC_BASE_ADDR + 0x2000),
  72. .miiregs = (tsec_t *)(TSEC_BASE_ADDR),
  73. .devname = CONFIG_MPC85XX_FEC_NAME,
  74. .phyaddr = FEC_PHY_ADDR,
  75. .flags = FEC_FLAGS
  76. }, /* FEC */
  77. #endif
  78. #ifdef CONFIG_TSEC3
  79. STD_TSEC_INFO(3), /* TSEC3 */
  80. #endif
  81. #ifdef CONFIG_TSEC4
  82. STD_TSEC_INFO(4), /* TSEC4 */
  83. #endif
  84. };
  85. int tsec_eth_init(bd_t *bis, struct tsec_info_struct *tsecs, int num)
  86. {
  87. int i;
  88. for (i = 0; i < num; i++)
  89. tsec_initialize(bis, &tsecs[i]);
  90. return 0;
  91. }
  92. int tsec_standard_init(bd_t *bis)
  93. {
  94. return tsec_eth_init(bis, tsec_info, ARRAY_SIZE(tsec_info));
  95. }
  96. /* Initialize device structure. Returns success if PHY
  97. * initialization succeeded (i.e. if it recognizes the PHY)
  98. */
  99. int tsec_initialize(bd_t * bis, struct tsec_info_struct *tsec_info)
  100. {
  101. struct eth_device *dev;
  102. int i;
  103. struct tsec_private *priv;
  104. dev = (struct eth_device *)malloc(sizeof *dev);
  105. if (NULL == dev)
  106. return 0;
  107. memset(dev, 0, sizeof *dev);
  108. priv = (struct tsec_private *)malloc(sizeof(*priv));
  109. if (NULL == priv)
  110. return 0;
  111. privlist[num_tsecs++] = priv;
  112. priv->regs = tsec_info->regs;
  113. priv->phyregs = tsec_info->miiregs;
  114. priv->phyaddr = tsec_info->phyaddr;
  115. priv->flags = tsec_info->flags;
  116. sprintf(dev->name, tsec_info->devname);
  117. dev->iobase = 0;
  118. dev->priv = priv;
  119. dev->init = tsec_init;
  120. dev->halt = tsec_halt;
  121. dev->send = tsec_send;
  122. dev->recv = tsec_recv;
  123. #ifdef CONFIG_MCAST_TFTP
  124. dev->mcast = tsec_mcast_addr;
  125. #endif
  126. /* Tell u-boot to get the addr from the env */
  127. for (i = 0; i < 6; i++)
  128. dev->enetaddr[i] = 0;
  129. eth_register(dev);
  130. /* Reset the MAC */
  131. priv->regs->maccfg1 |= MACCFG1_SOFT_RESET;
  132. priv->regs->maccfg1 &= ~(MACCFG1_SOFT_RESET);
  133. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
  134. && !defined(BITBANGMII)
  135. miiphy_register(dev->name, tsec_miiphy_read, tsec_miiphy_write);
  136. #endif
  137. /* Try to initialize PHY here, and return */
  138. return init_phy(dev);
  139. }
  140. /* Initializes data structures and registers for the controller,
  141. * and brings the interface up. Returns the link status, meaning
  142. * that it returns success if the link is up, failure otherwise.
  143. * This allows u-boot to find the first active controller.
  144. */
  145. int tsec_init(struct eth_device *dev, bd_t * bd)
  146. {
  147. uint tempval;
  148. char tmpbuf[MAC_ADDR_LEN];
  149. int i;
  150. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  151. volatile tsec_t *regs = priv->regs;
  152. /* Make sure the controller is stopped */
  153. tsec_halt(dev);
  154. /* Init MACCFG2. Defaults to GMII */
  155. regs->maccfg2 = MACCFG2_INIT_SETTINGS;
  156. /* Init ECNTRL */
  157. regs->ecntrl = ECNTRL_INIT_SETTINGS;
  158. /* Copy the station address into the address registers.
  159. * Backwards, because little endian MACS are dumb */
  160. for (i = 0; i < MAC_ADDR_LEN; i++) {
  161. tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
  162. }
  163. regs->macstnaddr1 = *((uint *) (tmpbuf));
  164. tempval = *((uint *) (tmpbuf + 4));
  165. regs->macstnaddr2 = tempval;
  166. /* reset the indices to zero */
  167. rxIdx = 0;
  168. txIdx = 0;
  169. /* Clear out (for the most part) the other registers */
  170. init_registers(regs);
  171. /* Ready the device for tx/rx */
  172. startup_tsec(dev);
  173. /* If there's no link, fail */
  174. return (priv->link ? 0 : -1);
  175. }
  176. /* Write value to the device's PHY through the registers
  177. * specified in priv, modifying the register specified in regnum.
  178. * It will wait for the write to be done (or for a timeout to
  179. * expire) before exiting
  180. */
  181. void write_any_phy_reg(struct tsec_private *priv, uint phyid, uint regnum, uint value)
  182. {
  183. volatile tsec_t *regbase = priv->phyregs;
  184. int timeout = 1000000;
  185. regbase->miimadd = (phyid << 8) | regnum;
  186. regbase->miimcon = value;
  187. asm("sync");
  188. timeout = 1000000;
  189. while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
  190. }
  191. /* #define to provide old write_phy_reg functionality without duplicating code */
  192. #define write_phy_reg(priv, regnum, value) write_any_phy_reg(priv,priv->phyaddr,regnum,value)
  193. /* Reads register regnum on the device's PHY through the
  194. * registers specified in priv. It lowers and raises the read
  195. * command, and waits for the data to become valid (miimind
  196. * notvalid bit cleared), and the bus to cease activity (miimind
  197. * busy bit cleared), and then returns the value
  198. */
  199. uint read_any_phy_reg(struct tsec_private *priv, uint phyid, uint regnum)
  200. {
  201. uint value;
  202. volatile tsec_t *regbase = priv->phyregs;
  203. /* Put the address of the phy, and the register
  204. * number into MIIMADD */
  205. regbase->miimadd = (phyid << 8) | regnum;
  206. /* Clear the command register, and wait */
  207. regbase->miimcom = 0;
  208. asm("sync");
  209. /* Initiate a read command, and wait */
  210. regbase->miimcom = MIIM_READ_COMMAND;
  211. asm("sync");
  212. /* Wait for the the indication that the read is done */
  213. while ((regbase->miimind & (MIIMIND_NOTVALID | MIIMIND_BUSY))) ;
  214. /* Grab the value read from the PHY */
  215. value = regbase->miimstat;
  216. return value;
  217. }
  218. /* #define to provide old read_phy_reg functionality without duplicating code */
  219. #define read_phy_reg(priv,regnum) read_any_phy_reg(priv,priv->phyaddr,regnum)
  220. /* Discover which PHY is attached to the device, and configure it
  221. * properly. If the PHY is not recognized, then return 0
  222. * (failure). Otherwise, return 1
  223. */
  224. static int init_phy(struct eth_device *dev)
  225. {
  226. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  227. struct phy_info *curphy;
  228. volatile tsec_t *regs = (volatile tsec_t *)(TSEC_BASE_ADDR);
  229. /* Assign a Physical address to the TBI */
  230. regs->tbipa = CFG_TBIPA_VALUE;
  231. regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
  232. regs->tbipa = CFG_TBIPA_VALUE;
  233. asm("sync");
  234. /* Reset MII (due to new addresses) */
  235. priv->phyregs->miimcfg = MIIMCFG_RESET;
  236. asm("sync");
  237. priv->phyregs->miimcfg = MIIMCFG_INIT_VALUE;
  238. asm("sync");
  239. while (priv->phyregs->miimind & MIIMIND_BUSY) ;
  240. if (0 == relocated)
  241. relocate_cmds();
  242. /* Get the cmd structure corresponding to the attached
  243. * PHY */
  244. curphy = get_phy_info(dev);
  245. if (curphy == NULL) {
  246. priv->phyinfo = NULL;
  247. printf("%s: No PHY found\n", dev->name);
  248. return 0;
  249. }
  250. priv->phyinfo = curphy;
  251. phy_run_commands(priv, priv->phyinfo->config);
  252. return 1;
  253. }
  254. /*
  255. * Returns which value to write to the control register.
  256. * For 10/100, the value is slightly different
  257. */
  258. uint mii_cr_init(uint mii_reg, struct tsec_private * priv)
  259. {
  260. if (priv->flags & TSEC_GIGABIT)
  261. return MIIM_CONTROL_INIT;
  262. else
  263. return MIIM_CR_INIT;
  264. }
  265. /* Parse the status register for link, and then do
  266. * auto-negotiation
  267. */
  268. uint mii_parse_sr(uint mii_reg, struct tsec_private * priv)
  269. {
  270. /*
  271. * Wait if the link is up, and autonegotiation is in progress
  272. * (ie - we're capable and it's not done)
  273. */
  274. mii_reg = read_phy_reg(priv, MIIM_STATUS);
  275. if ((mii_reg & MIIM_STATUS_LINK) && (mii_reg & PHY_BMSR_AUTN_ABLE)
  276. && !(mii_reg & PHY_BMSR_AUTN_COMP)) {
  277. int i = 0;
  278. puts("Waiting for PHY auto negotiation to complete");
  279. while (!(mii_reg & PHY_BMSR_AUTN_COMP)) {
  280. /*
  281. * Timeout reached ?
  282. */
  283. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  284. puts(" TIMEOUT !\n");
  285. priv->link = 0;
  286. return 0;
  287. }
  288. if ((i++ % 1000) == 0) {
  289. putc('.');
  290. }
  291. udelay(1000); /* 1 ms */
  292. mii_reg = read_phy_reg(priv, MIIM_STATUS);
  293. }
  294. puts(" done\n");
  295. priv->link = 1;
  296. udelay(500000); /* another 500 ms (results in faster booting) */
  297. } else {
  298. if (mii_reg & MIIM_STATUS_LINK)
  299. priv->link = 1;
  300. else
  301. priv->link = 0;
  302. }
  303. return 0;
  304. }
  305. /* Generic function which updates the speed and duplex. If
  306. * autonegotiation is enabled, it uses the AND of the link
  307. * partner's advertised capabilities and our advertised
  308. * capabilities. If autonegotiation is disabled, we use the
  309. * appropriate bits in the control register.
  310. *
  311. * Stolen from Linux's mii.c and phy_device.c
  312. */
  313. uint mii_parse_link(uint mii_reg, struct tsec_private *priv)
  314. {
  315. /* We're using autonegotiation */
  316. if (mii_reg & PHY_BMSR_AUTN_ABLE) {
  317. uint lpa = 0;
  318. uint gblpa = 0;
  319. /* Check for gigabit capability */
  320. if (mii_reg & PHY_BMSR_EXT) {
  321. /* We want a list of states supported by
  322. * both PHYs in the link
  323. */
  324. gblpa = read_phy_reg(priv, PHY_1000BTSR);
  325. gblpa &= read_phy_reg(priv, PHY_1000BTCR) << 2;
  326. }
  327. /* Set the baseline so we only have to set them
  328. * if they're different
  329. */
  330. priv->speed = 10;
  331. priv->duplexity = 0;
  332. /* Check the gigabit fields */
  333. if (gblpa & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
  334. priv->speed = 1000;
  335. if (gblpa & PHY_1000BTSR_1000FD)
  336. priv->duplexity = 1;
  337. /* We're done! */
  338. return 0;
  339. }
  340. lpa = read_phy_reg(priv, PHY_ANAR);
  341. lpa &= read_phy_reg(priv, PHY_ANLPAR);
  342. if (lpa & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX)) {
  343. priv->speed = 100;
  344. if (lpa & PHY_ANLPAR_TXFD)
  345. priv->duplexity = 1;
  346. } else if (lpa & PHY_ANLPAR_10FD)
  347. priv->duplexity = 1;
  348. } else {
  349. uint bmcr = read_phy_reg(priv, PHY_BMCR);
  350. priv->speed = 10;
  351. priv->duplexity = 0;
  352. if (bmcr & PHY_BMCR_DPLX)
  353. priv->duplexity = 1;
  354. if (bmcr & PHY_BMCR_1000_MBPS)
  355. priv->speed = 1000;
  356. else if (bmcr & PHY_BMCR_100_MBPS)
  357. priv->speed = 100;
  358. }
  359. return 0;
  360. }
  361. /*
  362. * Parse the BCM54xx status register for speed and duplex information.
  363. * The linux sungem_phy has this information, but in a table format.
  364. */
  365. uint mii_parse_BCM54xx_sr(uint mii_reg, struct tsec_private *priv)
  366. {
  367. switch((mii_reg & MIIM_BCM54xx_AUXSTATUS_LINKMODE_MASK) >> MIIM_BCM54xx_AUXSTATUS_LINKMODE_SHIFT){
  368. case 1:
  369. printf("Enet starting in 10BT/HD\n");
  370. priv->duplexity = 0;
  371. priv->speed = 10;
  372. break;
  373. case 2:
  374. printf("Enet starting in 10BT/FD\n");
  375. priv->duplexity = 1;
  376. priv->speed = 10;
  377. break;
  378. case 3:
  379. printf("Enet starting in 100BT/HD\n");
  380. priv->duplexity = 0;
  381. priv->speed = 100;
  382. break;
  383. case 5:
  384. printf("Enet starting in 100BT/FD\n");
  385. priv->duplexity = 1;
  386. priv->speed = 100;
  387. break;
  388. case 6:
  389. printf("Enet starting in 1000BT/HD\n");
  390. priv->duplexity = 0;
  391. priv->speed = 1000;
  392. break;
  393. case 7:
  394. printf("Enet starting in 1000BT/FD\n");
  395. priv->duplexity = 1;
  396. priv->speed = 1000;
  397. break;
  398. default:
  399. printf("Auto-neg error, defaulting to 10BT/HD\n");
  400. priv->duplexity = 0;
  401. priv->speed = 10;
  402. break;
  403. }
  404. return 0;
  405. }
  406. /* Parse the 88E1011's status register for speed and duplex
  407. * information
  408. */
  409. uint mii_parse_88E1011_psr(uint mii_reg, struct tsec_private * priv)
  410. {
  411. uint speed;
  412. mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
  413. if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
  414. !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
  415. int i = 0;
  416. puts("Waiting for PHY realtime link");
  417. while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
  418. /* Timeout reached ? */
  419. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  420. puts(" TIMEOUT !\n");
  421. priv->link = 0;
  422. break;
  423. }
  424. if ((i++ % 1000) == 0) {
  425. putc('.');
  426. }
  427. udelay(1000); /* 1 ms */
  428. mii_reg = read_phy_reg(priv, MIIM_88E1011_PHY_STATUS);
  429. }
  430. puts(" done\n");
  431. udelay(500000); /* another 500 ms (results in faster booting) */
  432. } else {
  433. if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
  434. priv->link = 1;
  435. else
  436. priv->link = 0;
  437. }
  438. if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
  439. priv->duplexity = 1;
  440. else
  441. priv->duplexity = 0;
  442. speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
  443. switch (speed) {
  444. case MIIM_88E1011_PHYSTAT_GBIT:
  445. priv->speed = 1000;
  446. break;
  447. case MIIM_88E1011_PHYSTAT_100:
  448. priv->speed = 100;
  449. break;
  450. default:
  451. priv->speed = 10;
  452. }
  453. return 0;
  454. }
  455. /* Parse the RTL8211B's status register for speed and duplex
  456. * information
  457. */
  458. uint mii_parse_RTL8211B_sr(uint mii_reg, struct tsec_private * priv)
  459. {
  460. uint speed;
  461. mii_reg = read_phy_reg(priv, MIIM_RTL8211B_PHY_STATUS);
  462. if (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
  463. int i = 0;
  464. /* in case of timeout ->link is cleared */
  465. priv->link = 1;
  466. puts("Waiting for PHY realtime link");
  467. while (!(mii_reg & MIIM_RTL8211B_PHYSTAT_SPDDONE)) {
  468. /* Timeout reached ? */
  469. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  470. puts(" TIMEOUT !\n");
  471. priv->link = 0;
  472. break;
  473. }
  474. if ((i++ % 1000) == 0) {
  475. putc('.');
  476. }
  477. udelay(1000); /* 1 ms */
  478. mii_reg = read_phy_reg(priv, MIIM_RTL8211B_PHY_STATUS);
  479. }
  480. puts(" done\n");
  481. udelay(500000); /* another 500 ms (results in faster booting) */
  482. } else {
  483. if (mii_reg & MIIM_RTL8211B_PHYSTAT_LINK)
  484. priv->link = 1;
  485. else
  486. priv->link = 0;
  487. }
  488. if (mii_reg & MIIM_RTL8211B_PHYSTAT_DUPLEX)
  489. priv->duplexity = 1;
  490. else
  491. priv->duplexity = 0;
  492. speed = (mii_reg & MIIM_RTL8211B_PHYSTAT_SPEED);
  493. switch (speed) {
  494. case MIIM_RTL8211B_PHYSTAT_GBIT:
  495. priv->speed = 1000;
  496. break;
  497. case MIIM_RTL8211B_PHYSTAT_100:
  498. priv->speed = 100;
  499. break;
  500. default:
  501. priv->speed = 10;
  502. }
  503. return 0;
  504. }
  505. /* Parse the cis8201's status register for speed and duplex
  506. * information
  507. */
  508. uint mii_parse_cis8201(uint mii_reg, struct tsec_private * priv)
  509. {
  510. uint speed;
  511. if (mii_reg & MIIM_CIS8201_AUXCONSTAT_DUPLEX)
  512. priv->duplexity = 1;
  513. else
  514. priv->duplexity = 0;
  515. speed = mii_reg & MIIM_CIS8201_AUXCONSTAT_SPEED;
  516. switch (speed) {
  517. case MIIM_CIS8201_AUXCONSTAT_GBIT:
  518. priv->speed = 1000;
  519. break;
  520. case MIIM_CIS8201_AUXCONSTAT_100:
  521. priv->speed = 100;
  522. break;
  523. default:
  524. priv->speed = 10;
  525. break;
  526. }
  527. return 0;
  528. }
  529. /* Parse the vsc8244's status register for speed and duplex
  530. * information
  531. */
  532. uint mii_parse_vsc8244(uint mii_reg, struct tsec_private * priv)
  533. {
  534. uint speed;
  535. if (mii_reg & MIIM_VSC8244_AUXCONSTAT_DUPLEX)
  536. priv->duplexity = 1;
  537. else
  538. priv->duplexity = 0;
  539. speed = mii_reg & MIIM_VSC8244_AUXCONSTAT_SPEED;
  540. switch (speed) {
  541. case MIIM_VSC8244_AUXCONSTAT_GBIT:
  542. priv->speed = 1000;
  543. break;
  544. case MIIM_VSC8244_AUXCONSTAT_100:
  545. priv->speed = 100;
  546. break;
  547. default:
  548. priv->speed = 10;
  549. break;
  550. }
  551. return 0;
  552. }
  553. /* Parse the DM9161's status register for speed and duplex
  554. * information
  555. */
  556. uint mii_parse_dm9161_scsr(uint mii_reg, struct tsec_private * priv)
  557. {
  558. if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_100H))
  559. priv->speed = 100;
  560. else
  561. priv->speed = 10;
  562. if (mii_reg & (MIIM_DM9161_SCSR_100F | MIIM_DM9161_SCSR_10F))
  563. priv->duplexity = 1;
  564. else
  565. priv->duplexity = 0;
  566. return 0;
  567. }
  568. /*
  569. * Hack to write all 4 PHYs with the LED values
  570. */
  571. uint mii_cis8204_fixled(uint mii_reg, struct tsec_private * priv)
  572. {
  573. uint phyid;
  574. volatile tsec_t *regbase = priv->phyregs;
  575. int timeout = 1000000;
  576. for (phyid = 0; phyid < 4; phyid++) {
  577. regbase->miimadd = (phyid << 8) | mii_reg;
  578. regbase->miimcon = MIIM_CIS8204_SLEDCON_INIT;
  579. asm("sync");
  580. timeout = 1000000;
  581. while ((regbase->miimind & MIIMIND_BUSY) && timeout--) ;
  582. }
  583. return MIIM_CIS8204_SLEDCON_INIT;
  584. }
  585. uint mii_cis8204_setmode(uint mii_reg, struct tsec_private * priv)
  586. {
  587. if (priv->flags & TSEC_REDUCED)
  588. return MIIM_CIS8204_EPHYCON_INIT | MIIM_CIS8204_EPHYCON_RGMII;
  589. else
  590. return MIIM_CIS8204_EPHYCON_INIT;
  591. }
  592. uint mii_m88e1111s_setmode(uint mii_reg, struct tsec_private *priv)
  593. {
  594. uint mii_data = read_phy_reg(priv, mii_reg);
  595. if (priv->flags & TSEC_REDUCED)
  596. mii_data = (mii_data & 0xfff0) | 0x000b;
  597. return mii_data;
  598. }
  599. /* Initialized required registers to appropriate values, zeroing
  600. * those we don't care about (unless zero is bad, in which case,
  601. * choose a more appropriate value)
  602. */
  603. static void init_registers(volatile tsec_t * regs)
  604. {
  605. /* Clear IEVENT */
  606. regs->ievent = IEVENT_INIT_CLEAR;
  607. regs->imask = IMASK_INIT_CLEAR;
  608. regs->hash.iaddr0 = 0;
  609. regs->hash.iaddr1 = 0;
  610. regs->hash.iaddr2 = 0;
  611. regs->hash.iaddr3 = 0;
  612. regs->hash.iaddr4 = 0;
  613. regs->hash.iaddr5 = 0;
  614. regs->hash.iaddr6 = 0;
  615. regs->hash.iaddr7 = 0;
  616. regs->hash.gaddr0 = 0;
  617. regs->hash.gaddr1 = 0;
  618. regs->hash.gaddr2 = 0;
  619. regs->hash.gaddr3 = 0;
  620. regs->hash.gaddr4 = 0;
  621. regs->hash.gaddr5 = 0;
  622. regs->hash.gaddr6 = 0;
  623. regs->hash.gaddr7 = 0;
  624. regs->rctrl = 0x00000000;
  625. /* Init RMON mib registers */
  626. memset((void *)&(regs->rmon), 0, sizeof(rmon_mib_t));
  627. regs->rmon.cam1 = 0xffffffff;
  628. regs->rmon.cam2 = 0xffffffff;
  629. regs->mrblr = MRBLR_INIT_SETTINGS;
  630. regs->minflr = MINFLR_INIT_SETTINGS;
  631. regs->attr = ATTR_INIT_SETTINGS;
  632. regs->attreli = ATTRELI_INIT_SETTINGS;
  633. }
  634. /* Configure maccfg2 based on negotiated speed and duplex
  635. * reported by PHY handling code
  636. */
  637. static void adjust_link(struct eth_device *dev)
  638. {
  639. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  640. volatile tsec_t *regs = priv->regs;
  641. if (priv->link) {
  642. if (priv->duplexity != 0)
  643. regs->maccfg2 |= MACCFG2_FULL_DUPLEX;
  644. else
  645. regs->maccfg2 &= ~(MACCFG2_FULL_DUPLEX);
  646. switch (priv->speed) {
  647. case 1000:
  648. regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
  649. | MACCFG2_GMII);
  650. break;
  651. case 100:
  652. case 10:
  653. regs->maccfg2 = ((regs->maccfg2 & ~(MACCFG2_IF))
  654. | MACCFG2_MII);
  655. /* Set R100 bit in all modes although
  656. * it is only used in RGMII mode
  657. */
  658. if (priv->speed == 100)
  659. regs->ecntrl |= ECNTRL_R100;
  660. else
  661. regs->ecntrl &= ~(ECNTRL_R100);
  662. break;
  663. default:
  664. printf("%s: Speed was bad\n", dev->name);
  665. break;
  666. }
  667. printf("Speed: %d, %s duplex\n", priv->speed,
  668. (priv->duplexity) ? "full" : "half");
  669. } else {
  670. printf("%s: No link.\n", dev->name);
  671. }
  672. }
  673. /* Set up the buffers and their descriptors, and bring up the
  674. * interface
  675. */
  676. static void startup_tsec(struct eth_device *dev)
  677. {
  678. int i;
  679. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  680. volatile tsec_t *regs = priv->regs;
  681. /* Point to the buffer descriptors */
  682. regs->tbase = (unsigned int)(&rtx.txbd[txIdx]);
  683. regs->rbase = (unsigned int)(&rtx.rxbd[rxIdx]);
  684. /* Initialize the Rx Buffer descriptors */
  685. for (i = 0; i < PKTBUFSRX; i++) {
  686. rtx.rxbd[i].status = RXBD_EMPTY;
  687. rtx.rxbd[i].length = 0;
  688. rtx.rxbd[i].bufPtr = (uint) NetRxPackets[i];
  689. }
  690. rtx.rxbd[PKTBUFSRX - 1].status |= RXBD_WRAP;
  691. /* Initialize the TX Buffer Descriptors */
  692. for (i = 0; i < TX_BUF_CNT; i++) {
  693. rtx.txbd[i].status = 0;
  694. rtx.txbd[i].length = 0;
  695. rtx.txbd[i].bufPtr = 0;
  696. }
  697. rtx.txbd[TX_BUF_CNT - 1].status |= TXBD_WRAP;
  698. /* Start up the PHY */
  699. if(priv->phyinfo)
  700. phy_run_commands(priv, priv->phyinfo->startup);
  701. adjust_link(dev);
  702. /* Enable Transmit and Receive */
  703. regs->maccfg1 |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  704. /* Tell the DMA it is clear to go */
  705. regs->dmactrl |= DMACTRL_INIT_SETTINGS;
  706. regs->tstat = TSTAT_CLEAR_THALT;
  707. regs->rstat = RSTAT_CLEAR_RHALT;
  708. regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
  709. }
  710. /* This returns the status bits of the device. The return value
  711. * is never checked, and this is what the 8260 driver did, so we
  712. * do the same. Presumably, this would be zero if there were no
  713. * errors
  714. */
  715. static int tsec_send(struct eth_device *dev, volatile void *packet, int length)
  716. {
  717. int i;
  718. int result = 0;
  719. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  720. volatile tsec_t *regs = priv->regs;
  721. /* Find an empty buffer descriptor */
  722. for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
  723. if (i >= TOUT_LOOP) {
  724. debug("%s: tsec: tx buffers full\n", dev->name);
  725. return result;
  726. }
  727. }
  728. rtx.txbd[txIdx].bufPtr = (uint) packet;
  729. rtx.txbd[txIdx].length = length;
  730. rtx.txbd[txIdx].status |=
  731. (TXBD_READY | TXBD_LAST | TXBD_CRC | TXBD_INTERRUPT);
  732. /* Tell the DMA to go */
  733. regs->tstat = TSTAT_CLEAR_THALT;
  734. /* Wait for buffer to be transmitted */
  735. for (i = 0; rtx.txbd[txIdx].status & TXBD_READY; i++) {
  736. if (i >= TOUT_LOOP) {
  737. debug("%s: tsec: tx error\n", dev->name);
  738. return result;
  739. }
  740. }
  741. txIdx = (txIdx + 1) % TX_BUF_CNT;
  742. result = rtx.txbd[txIdx].status & TXBD_STATS;
  743. return result;
  744. }
  745. static int tsec_recv(struct eth_device *dev)
  746. {
  747. int length;
  748. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  749. volatile tsec_t *regs = priv->regs;
  750. while (!(rtx.rxbd[rxIdx].status & RXBD_EMPTY)) {
  751. length = rtx.rxbd[rxIdx].length;
  752. /* Send the packet up if there were no errors */
  753. if (!(rtx.rxbd[rxIdx].status & RXBD_STATS)) {
  754. NetReceive(NetRxPackets[rxIdx], length - 4);
  755. } else {
  756. printf("Got error %x\n",
  757. (rtx.rxbd[rxIdx].status & RXBD_STATS));
  758. }
  759. rtx.rxbd[rxIdx].length = 0;
  760. /* Set the wrap bit if this is the last element in the list */
  761. rtx.rxbd[rxIdx].status =
  762. RXBD_EMPTY | (((rxIdx + 1) == PKTBUFSRX) ? RXBD_WRAP : 0);
  763. rxIdx = (rxIdx + 1) % PKTBUFSRX;
  764. }
  765. if (regs->ievent & IEVENT_BSY) {
  766. regs->ievent = IEVENT_BSY;
  767. regs->rstat = RSTAT_CLEAR_RHALT;
  768. }
  769. return -1;
  770. }
  771. /* Stop the interface */
  772. static void tsec_halt(struct eth_device *dev)
  773. {
  774. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  775. volatile tsec_t *regs = priv->regs;
  776. regs->dmactrl &= ~(DMACTRL_GRS | DMACTRL_GTS);
  777. regs->dmactrl |= (DMACTRL_GRS | DMACTRL_GTS);
  778. while (!(regs->ievent & (IEVENT_GRSC | IEVENT_GTSC))) ;
  779. regs->maccfg1 &= ~(MACCFG1_TX_EN | MACCFG1_RX_EN);
  780. /* Shut down the PHY, as needed */
  781. if(priv->phyinfo)
  782. phy_run_commands(priv, priv->phyinfo->shutdown);
  783. }
  784. struct phy_info phy_info_M88E1149S = {
  785. 0x1410ca,
  786. "Marvell 88E1149S",
  787. 4,
  788. (struct phy_cmd[]){ /* config */
  789. /* Reset and configure the PHY */
  790. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  791. {0x1d, 0x1f, NULL},
  792. {0x1e, 0x200c, NULL},
  793. {0x1d, 0x5, NULL},
  794. {0x1e, 0x0, NULL},
  795. {0x1e, 0x100, NULL},
  796. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  797. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  798. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  799. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  800. {miim_end,}
  801. },
  802. (struct phy_cmd[]){ /* startup */
  803. /* Status is read once to clear old link state */
  804. {MIIM_STATUS, miim_read, NULL},
  805. /* Auto-negotiate */
  806. {MIIM_STATUS, miim_read, &mii_parse_sr},
  807. /* Read the status */
  808. {MIIM_88E1011_PHY_STATUS, miim_read,
  809. &mii_parse_88E1011_psr},
  810. {miim_end,}
  811. },
  812. (struct phy_cmd[]){ /* shutdown */
  813. {miim_end,}
  814. },
  815. };
  816. /* The 5411 id is 0x206070, the 5421 is 0x2060e0 */
  817. struct phy_info phy_info_BCM5461S = {
  818. 0x02060c1, /* 5461 ID */
  819. "Broadcom BCM5461S",
  820. 0, /* not clear to me what minor revisions we can shift away */
  821. (struct phy_cmd[]) { /* config */
  822. /* Reset and configure the PHY */
  823. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  824. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  825. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  826. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  827. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  828. {miim_end,}
  829. },
  830. (struct phy_cmd[]) { /* startup */
  831. /* Status is read once to clear old link state */
  832. {MIIM_STATUS, miim_read, NULL},
  833. /* Auto-negotiate */
  834. {MIIM_STATUS, miim_read, &mii_parse_sr},
  835. /* Read the status */
  836. {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
  837. {miim_end,}
  838. },
  839. (struct phy_cmd[]) { /* shutdown */
  840. {miim_end,}
  841. },
  842. };
  843. struct phy_info phy_info_BCM5464S = {
  844. 0x02060b1, /* 5464 ID */
  845. "Broadcom BCM5464S",
  846. 0, /* not clear to me what minor revisions we can shift away */
  847. (struct phy_cmd[]) { /* config */
  848. /* Reset and configure the PHY */
  849. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  850. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  851. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  852. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  853. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  854. {miim_end,}
  855. },
  856. (struct phy_cmd[]) { /* startup */
  857. /* Status is read once to clear old link state */
  858. {MIIM_STATUS, miim_read, NULL},
  859. /* Auto-negotiate */
  860. {MIIM_STATUS, miim_read, &mii_parse_sr},
  861. /* Read the status */
  862. {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
  863. {miim_end,}
  864. },
  865. (struct phy_cmd[]) { /* shutdown */
  866. {miim_end,}
  867. },
  868. };
  869. struct phy_info phy_info_M88E1011S = {
  870. 0x01410c6,
  871. "Marvell 88E1011S",
  872. 4,
  873. (struct phy_cmd[]){ /* config */
  874. /* Reset and configure the PHY */
  875. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  876. {0x1d, 0x1f, NULL},
  877. {0x1e, 0x200c, NULL},
  878. {0x1d, 0x5, NULL},
  879. {0x1e, 0x0, NULL},
  880. {0x1e, 0x100, NULL},
  881. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  882. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  883. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  884. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  885. {miim_end,}
  886. },
  887. (struct phy_cmd[]){ /* startup */
  888. /* Status is read once to clear old link state */
  889. {MIIM_STATUS, miim_read, NULL},
  890. /* Auto-negotiate */
  891. {MIIM_STATUS, miim_read, &mii_parse_sr},
  892. /* Read the status */
  893. {MIIM_88E1011_PHY_STATUS, miim_read,
  894. &mii_parse_88E1011_psr},
  895. {miim_end,}
  896. },
  897. (struct phy_cmd[]){ /* shutdown */
  898. {miim_end,}
  899. },
  900. };
  901. struct phy_info phy_info_M88E1111S = {
  902. 0x01410cc,
  903. "Marvell 88E1111S",
  904. 4,
  905. (struct phy_cmd[]){ /* config */
  906. /* Reset and configure the PHY */
  907. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  908. {0x1b, 0x848f, &mii_m88e1111s_setmode},
  909. {0x14, 0x0cd2, NULL}, /* Delay RGMII TX and RX */
  910. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  911. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  912. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  913. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  914. {miim_end,}
  915. },
  916. (struct phy_cmd[]){ /* startup */
  917. /* Status is read once to clear old link state */
  918. {MIIM_STATUS, miim_read, NULL},
  919. /* Auto-negotiate */
  920. {MIIM_STATUS, miim_read, &mii_parse_sr},
  921. /* Read the status */
  922. {MIIM_88E1011_PHY_STATUS, miim_read,
  923. &mii_parse_88E1011_psr},
  924. {miim_end,}
  925. },
  926. (struct phy_cmd[]){ /* shutdown */
  927. {miim_end,}
  928. },
  929. };
  930. struct phy_info phy_info_M88E1118 = {
  931. 0x01410e1,
  932. "Marvell 88E1118",
  933. 4,
  934. (struct phy_cmd[]){ /* config */
  935. /* Reset and configure the PHY */
  936. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  937. {0x16, 0x0002, NULL}, /* Change Page Number */
  938. {0x15, 0x1070, NULL}, /* Delay RGMII TX and RX */
  939. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  940. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  941. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  942. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  943. {miim_end,}
  944. },
  945. (struct phy_cmd[]){ /* startup */
  946. {0x16, 0x0000, NULL}, /* Change Page Number */
  947. /* Status is read once to clear old link state */
  948. {MIIM_STATUS, miim_read, NULL},
  949. /* Auto-negotiate */
  950. /* Read the status */
  951. {MIIM_88E1011_PHY_STATUS, miim_read,
  952. &mii_parse_88E1011_psr},
  953. {miim_end,}
  954. },
  955. (struct phy_cmd[]){ /* shutdown */
  956. {miim_end,}
  957. },
  958. };
  959. /*
  960. * Since to access LED register we need do switch the page, we
  961. * do LED configuring in the miim_read-like function as follows
  962. */
  963. uint mii_88E1121_set_led (uint mii_reg, struct tsec_private *priv)
  964. {
  965. uint pg;
  966. /* Switch the page to access the led register */
  967. pg = read_phy_reg(priv, MIIM_88E1121_PHY_PAGE);
  968. write_phy_reg(priv, MIIM_88E1121_PHY_PAGE, MIIM_88E1121_PHY_LED_PAGE);
  969. /* Configure leds */
  970. write_phy_reg(priv, MIIM_88E1121_PHY_LED_CTRL,
  971. MIIM_88E1121_PHY_LED_DEF);
  972. /* Restore the page pointer */
  973. write_phy_reg(priv, MIIM_88E1121_PHY_PAGE, pg);
  974. return 0;
  975. }
  976. struct phy_info phy_info_M88E1121R = {
  977. 0x01410cb,
  978. "Marvell 88E1121R",
  979. 4,
  980. (struct phy_cmd[]){ /* config */
  981. /* Reset and configure the PHY */
  982. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  983. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  984. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  985. /* Configure leds */
  986. {MIIM_88E1121_PHY_LED_CTRL, miim_read,
  987. &mii_88E1121_set_led},
  988. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  989. {miim_end,}
  990. },
  991. (struct phy_cmd[]){ /* startup */
  992. /* Status is read once to clear old link state */
  993. {MIIM_STATUS, miim_read, NULL},
  994. {MIIM_STATUS, miim_read, &mii_parse_sr},
  995. {MIIM_STATUS, miim_read, &mii_parse_link},
  996. {miim_end,}
  997. },
  998. (struct phy_cmd[]){ /* shutdown */
  999. {miim_end,}
  1000. },
  1001. };
  1002. static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
  1003. {
  1004. uint mii_data = read_phy_reg(priv, mii_reg);
  1005. /* Setting MIIM_88E1145_PHY_EXT_CR */
  1006. if (priv->flags & TSEC_REDUCED)
  1007. return mii_data |
  1008. MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
  1009. else
  1010. return mii_data;
  1011. }
  1012. static struct phy_info phy_info_M88E1145 = {
  1013. 0x01410cd,
  1014. "Marvell 88E1145",
  1015. 4,
  1016. (struct phy_cmd[]){ /* config */
  1017. /* Reset the PHY */
  1018. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  1019. /* Errata E0, E1 */
  1020. {29, 0x001b, NULL},
  1021. {30, 0x418f, NULL},
  1022. {29, 0x0016, NULL},
  1023. {30, 0xa2da, NULL},
  1024. /* Configure the PHY */
  1025. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  1026. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  1027. {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO,
  1028. NULL},
  1029. {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
  1030. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  1031. {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
  1032. {miim_end,}
  1033. },
  1034. (struct phy_cmd[]){ /* startup */
  1035. /* Status is read once to clear old link state */
  1036. {MIIM_STATUS, miim_read, NULL},
  1037. /* Auto-negotiate */
  1038. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1039. {MIIM_88E1111_PHY_LED_CONTROL,
  1040. MIIM_88E1111_PHY_LED_DIRECT, NULL},
  1041. /* Read the Status */
  1042. {MIIM_88E1011_PHY_STATUS, miim_read,
  1043. &mii_parse_88E1011_psr},
  1044. {miim_end,}
  1045. },
  1046. (struct phy_cmd[]){ /* shutdown */
  1047. {miim_end,}
  1048. },
  1049. };
  1050. struct phy_info phy_info_cis8204 = {
  1051. 0x3f11,
  1052. "Cicada Cis8204",
  1053. 6,
  1054. (struct phy_cmd[]){ /* config */
  1055. /* Override PHY config settings */
  1056. {MIIM_CIS8201_AUX_CONSTAT,
  1057. MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  1058. /* Configure some basic stuff */
  1059. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  1060. {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
  1061. &mii_cis8204_fixled},
  1062. {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
  1063. &mii_cis8204_setmode},
  1064. {miim_end,}
  1065. },
  1066. (struct phy_cmd[]){ /* startup */
  1067. /* Read the Status (2x to make sure link is right) */
  1068. {MIIM_STATUS, miim_read, NULL},
  1069. /* Auto-negotiate */
  1070. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1071. /* Read the status */
  1072. {MIIM_CIS8201_AUX_CONSTAT, miim_read,
  1073. &mii_parse_cis8201},
  1074. {miim_end,}
  1075. },
  1076. (struct phy_cmd[]){ /* shutdown */
  1077. {miim_end,}
  1078. },
  1079. };
  1080. /* Cicada 8201 */
  1081. struct phy_info phy_info_cis8201 = {
  1082. 0xfc41,
  1083. "CIS8201",
  1084. 4,
  1085. (struct phy_cmd[]){ /* config */
  1086. /* Override PHY config settings */
  1087. {MIIM_CIS8201_AUX_CONSTAT,
  1088. MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  1089. /* Set up the interface mode */
  1090. {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT,
  1091. NULL},
  1092. /* Configure some basic stuff */
  1093. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  1094. {miim_end,}
  1095. },
  1096. (struct phy_cmd[]){ /* startup */
  1097. /* Read the Status (2x to make sure link is right) */
  1098. {MIIM_STATUS, miim_read, NULL},
  1099. /* Auto-negotiate */
  1100. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1101. /* Read the status */
  1102. {MIIM_CIS8201_AUX_CONSTAT, miim_read,
  1103. &mii_parse_cis8201},
  1104. {miim_end,}
  1105. },
  1106. (struct phy_cmd[]){ /* shutdown */
  1107. {miim_end,}
  1108. },
  1109. };
  1110. struct phy_info phy_info_VSC8244 = {
  1111. 0x3f1b,
  1112. "Vitesse VSC8244",
  1113. 6,
  1114. (struct phy_cmd[]){ /* config */
  1115. /* Override PHY config settings */
  1116. /* Configure some basic stuff */
  1117. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  1118. {miim_end,}
  1119. },
  1120. (struct phy_cmd[]){ /* startup */
  1121. /* Read the Status (2x to make sure link is right) */
  1122. {MIIM_STATUS, miim_read, NULL},
  1123. /* Auto-negotiate */
  1124. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1125. /* Read the status */
  1126. {MIIM_VSC8244_AUX_CONSTAT, miim_read,
  1127. &mii_parse_vsc8244},
  1128. {miim_end,}
  1129. },
  1130. (struct phy_cmd[]){ /* shutdown */
  1131. {miim_end,}
  1132. },
  1133. };
  1134. struct phy_info phy_info_VSC8601 = {
  1135. 0x00007042,
  1136. "Vitesse VSC8601",
  1137. 4,
  1138. (struct phy_cmd[]){ /* config */
  1139. /* Override PHY config settings */
  1140. /* Configure some basic stuff */
  1141. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  1142. #ifdef CFG_VSC8601_SKEWFIX
  1143. {MIIM_VSC8601_EPHY_CON,MIIM_VSC8601_EPHY_CON_INIT_SKEW,NULL},
  1144. #if defined(CFG_VSC8601_SKEW_TX) && defined(CFG_VSC8601_SKEW_RX)
  1145. {MIIM_EXT_PAGE_ACCESS,1,NULL},
  1146. #define VSC8101_SKEW (CFG_VSC8601_SKEW_TX<<14)|(CFG_VSC8601_SKEW_RX<<12)
  1147. {MIIM_VSC8601_SKEW_CTRL,VSC8101_SKEW,NULL},
  1148. {MIIM_EXT_PAGE_ACCESS,0,NULL},
  1149. #endif
  1150. #endif
  1151. {miim_end,}
  1152. },
  1153. (struct phy_cmd[]){ /* startup */
  1154. /* Read the Status (2x to make sure link is right) */
  1155. {MIIM_STATUS, miim_read, NULL},
  1156. /* Auto-negotiate */
  1157. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1158. /* Read the status */
  1159. {MIIM_VSC8244_AUX_CONSTAT, miim_read,
  1160. &mii_parse_vsc8244},
  1161. {miim_end,}
  1162. },
  1163. (struct phy_cmd[]){ /* shutdown */
  1164. {miim_end,}
  1165. },
  1166. };
  1167. struct phy_info phy_info_dm9161 = {
  1168. 0x0181b88,
  1169. "Davicom DM9161E",
  1170. 4,
  1171. (struct phy_cmd[]){ /* config */
  1172. {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
  1173. /* Do not bypass the scrambler/descrambler */
  1174. {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
  1175. /* Clear 10BTCSR to default */
  1176. {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT,
  1177. NULL},
  1178. /* Configure some basic stuff */
  1179. {MIIM_CONTROL, MIIM_CR_INIT, NULL},
  1180. /* Restart Auto Negotiation */
  1181. {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
  1182. {miim_end,}
  1183. },
  1184. (struct phy_cmd[]){ /* startup */
  1185. /* Status is read once to clear old link state */
  1186. {MIIM_STATUS, miim_read, NULL},
  1187. /* Auto-negotiate */
  1188. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1189. /* Read the status */
  1190. {MIIM_DM9161_SCSR, miim_read,
  1191. &mii_parse_dm9161_scsr},
  1192. {miim_end,}
  1193. },
  1194. (struct phy_cmd[]){ /* shutdown */
  1195. {miim_end,}
  1196. },
  1197. };
  1198. /* a generic flavor. */
  1199. struct phy_info phy_info_generic = {
  1200. 0,
  1201. "Unknown/Generic PHY",
  1202. 32,
  1203. (struct phy_cmd[]) { /* config */
  1204. {PHY_BMCR, PHY_BMCR_RESET, NULL},
  1205. {PHY_BMCR, PHY_BMCR_AUTON|PHY_BMCR_RST_NEG, NULL},
  1206. {miim_end,}
  1207. },
  1208. (struct phy_cmd[]) { /* startup */
  1209. {PHY_BMSR, miim_read, NULL},
  1210. {PHY_BMSR, miim_read, &mii_parse_sr},
  1211. {PHY_BMSR, miim_read, &mii_parse_link},
  1212. {miim_end,}
  1213. },
  1214. (struct phy_cmd[]) { /* shutdown */
  1215. {miim_end,}
  1216. }
  1217. };
  1218. uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
  1219. {
  1220. unsigned int speed;
  1221. if (priv->link) {
  1222. speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
  1223. switch (speed) {
  1224. case MIIM_LXT971_SR2_10HDX:
  1225. priv->speed = 10;
  1226. priv->duplexity = 0;
  1227. break;
  1228. case MIIM_LXT971_SR2_10FDX:
  1229. priv->speed = 10;
  1230. priv->duplexity = 1;
  1231. break;
  1232. case MIIM_LXT971_SR2_100HDX:
  1233. priv->speed = 100;
  1234. priv->duplexity = 0;
  1235. break;
  1236. default:
  1237. priv->speed = 100;
  1238. priv->duplexity = 1;
  1239. }
  1240. } else {
  1241. priv->speed = 0;
  1242. priv->duplexity = 0;
  1243. }
  1244. return 0;
  1245. }
  1246. static struct phy_info phy_info_lxt971 = {
  1247. 0x0001378e,
  1248. "LXT971",
  1249. 4,
  1250. (struct phy_cmd[]){ /* config */
  1251. {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
  1252. {miim_end,}
  1253. },
  1254. (struct phy_cmd[]){ /* startup - enable interrupts */
  1255. /* { 0x12, 0x00f2, NULL }, */
  1256. {MIIM_STATUS, miim_read, NULL},
  1257. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1258. {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
  1259. {miim_end,}
  1260. },
  1261. (struct phy_cmd[]){ /* shutdown - disable interrupts */
  1262. {miim_end,}
  1263. },
  1264. };
  1265. /* Parse the DP83865's link and auto-neg status register for speed and duplex
  1266. * information
  1267. */
  1268. uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
  1269. {
  1270. switch (mii_reg & MIIM_DP83865_SPD_MASK) {
  1271. case MIIM_DP83865_SPD_1000:
  1272. priv->speed = 1000;
  1273. break;
  1274. case MIIM_DP83865_SPD_100:
  1275. priv->speed = 100;
  1276. break;
  1277. default:
  1278. priv->speed = 10;
  1279. break;
  1280. }
  1281. if (mii_reg & MIIM_DP83865_DPX_FULL)
  1282. priv->duplexity = 1;
  1283. else
  1284. priv->duplexity = 0;
  1285. return 0;
  1286. }
  1287. struct phy_info phy_info_dp83865 = {
  1288. 0x20005c7,
  1289. "NatSemi DP83865",
  1290. 4,
  1291. (struct phy_cmd[]){ /* config */
  1292. {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
  1293. {miim_end,}
  1294. },
  1295. (struct phy_cmd[]){ /* startup */
  1296. /* Status is read once to clear old link state */
  1297. {MIIM_STATUS, miim_read, NULL},
  1298. /* Auto-negotiate */
  1299. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1300. /* Read the link and auto-neg status */
  1301. {MIIM_DP83865_LANR, miim_read,
  1302. &mii_parse_dp83865_lanr},
  1303. {miim_end,}
  1304. },
  1305. (struct phy_cmd[]){ /* shutdown */
  1306. {miim_end,}
  1307. },
  1308. };
  1309. struct phy_info phy_info_rtl8211b = {
  1310. 0x001cc91,
  1311. "RealTek RTL8211B",
  1312. 4,
  1313. (struct phy_cmd[]){ /* config */
  1314. /* Reset and configure the PHY */
  1315. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  1316. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  1317. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  1318. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  1319. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  1320. {miim_end,}
  1321. },
  1322. (struct phy_cmd[]){ /* startup */
  1323. /* Status is read once to clear old link state */
  1324. {MIIM_STATUS, miim_read, NULL},
  1325. /* Auto-negotiate */
  1326. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1327. /* Read the status */
  1328. {MIIM_RTL8211B_PHY_STATUS, miim_read, &mii_parse_RTL8211B_sr},
  1329. {miim_end,}
  1330. },
  1331. (struct phy_cmd[]){ /* shutdown */
  1332. {miim_end,}
  1333. },
  1334. };
  1335. struct phy_info *phy_info[] = {
  1336. &phy_info_cis8204,
  1337. &phy_info_cis8201,
  1338. &phy_info_BCM5461S,
  1339. &phy_info_BCM5464S,
  1340. &phy_info_M88E1011S,
  1341. &phy_info_M88E1111S,
  1342. &phy_info_M88E1118,
  1343. &phy_info_M88E1121R,
  1344. &phy_info_M88E1145,
  1345. &phy_info_M88E1149S,
  1346. &phy_info_dm9161,
  1347. &phy_info_lxt971,
  1348. &phy_info_VSC8244,
  1349. &phy_info_VSC8601,
  1350. &phy_info_dp83865,
  1351. &phy_info_rtl8211b,
  1352. &phy_info_generic,
  1353. NULL
  1354. };
  1355. /* Grab the identifier of the device's PHY, and search through
  1356. * all of the known PHYs to see if one matches. If so, return
  1357. * it, if not, return NULL
  1358. */
  1359. struct phy_info *get_phy_info(struct eth_device *dev)
  1360. {
  1361. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  1362. uint phy_reg, phy_ID;
  1363. int i;
  1364. struct phy_info *theInfo = NULL;
  1365. /* Grab the bits from PHYIR1, and put them in the upper half */
  1366. phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
  1367. phy_ID = (phy_reg & 0xffff) << 16;
  1368. /* Grab the bits from PHYIR2, and put them in the lower half */
  1369. phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
  1370. phy_ID |= (phy_reg & 0xffff);
  1371. /* loop through all the known PHY types, and find one that */
  1372. /* matches the ID we read from the PHY. */
  1373. for (i = 0; phy_info[i]; i++) {
  1374. if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
  1375. theInfo = phy_info[i];
  1376. break;
  1377. }
  1378. }
  1379. if (theInfo == NULL) {
  1380. printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
  1381. return NULL;
  1382. } else {
  1383. debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
  1384. }
  1385. return theInfo;
  1386. }
  1387. /* Execute the given series of commands on the given device's
  1388. * PHY, running functions as necessary
  1389. */
  1390. void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
  1391. {
  1392. int i;
  1393. uint result;
  1394. volatile tsec_t *phyregs = priv->phyregs;
  1395. phyregs->miimcfg = MIIMCFG_RESET;
  1396. phyregs->miimcfg = MIIMCFG_INIT_VALUE;
  1397. while (phyregs->miimind & MIIMIND_BUSY) ;
  1398. for (i = 0; cmd->mii_reg != miim_end; i++) {
  1399. if (cmd->mii_data == miim_read) {
  1400. result = read_phy_reg(priv, cmd->mii_reg);
  1401. if (cmd->funct != NULL)
  1402. (*(cmd->funct)) (result, priv);
  1403. } else {
  1404. if (cmd->funct != NULL)
  1405. result = (*(cmd->funct)) (cmd->mii_reg, priv);
  1406. else
  1407. result = cmd->mii_data;
  1408. write_phy_reg(priv, cmd->mii_reg, result);
  1409. }
  1410. cmd++;
  1411. }
  1412. }
  1413. /* Relocate the function pointers in the phy cmd lists */
  1414. static void relocate_cmds(void)
  1415. {
  1416. struct phy_cmd **cmdlistptr;
  1417. struct phy_cmd *cmd;
  1418. int i, j, k;
  1419. for (i = 0; phy_info[i]; i++) {
  1420. /* First thing's first: relocate the pointers to the
  1421. * PHY command structures (the structs were done) */
  1422. phy_info[i] = (struct phy_info *)((uint) phy_info[i]
  1423. + gd->reloc_off);
  1424. phy_info[i]->name += gd->reloc_off;
  1425. phy_info[i]->config =
  1426. (struct phy_cmd *)((uint) phy_info[i]->config
  1427. + gd->reloc_off);
  1428. phy_info[i]->startup =
  1429. (struct phy_cmd *)((uint) phy_info[i]->startup
  1430. + gd->reloc_off);
  1431. phy_info[i]->shutdown =
  1432. (struct phy_cmd *)((uint) phy_info[i]->shutdown
  1433. + gd->reloc_off);
  1434. cmdlistptr = &phy_info[i]->config;
  1435. j = 0;
  1436. for (; cmdlistptr <= &phy_info[i]->shutdown; cmdlistptr++) {
  1437. k = 0;
  1438. for (cmd = *cmdlistptr;
  1439. cmd->mii_reg != miim_end;
  1440. cmd++) {
  1441. /* Only relocate non-NULL pointers */
  1442. if (cmd->funct)
  1443. cmd->funct += gd->reloc_off;
  1444. k++;
  1445. }
  1446. j++;
  1447. }
  1448. }
  1449. relocated = 1;
  1450. }
  1451. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
  1452. && !defined(BITBANGMII)
  1453. /*
  1454. * Read a MII PHY register.
  1455. *
  1456. * Returns:
  1457. * 0 on success
  1458. */
  1459. static int tsec_miiphy_read(char *devname, unsigned char addr,
  1460. unsigned char reg, unsigned short *value)
  1461. {
  1462. unsigned short ret;
  1463. struct tsec_private *priv = privlist[0];
  1464. if (NULL == priv) {
  1465. printf("Can't read PHY at address %d\n", addr);
  1466. return -1;
  1467. }
  1468. ret = (unsigned short)read_any_phy_reg(priv, addr, reg);
  1469. *value = ret;
  1470. return 0;
  1471. }
  1472. /*
  1473. * Write a MII PHY register.
  1474. *
  1475. * Returns:
  1476. * 0 on success
  1477. */
  1478. static int tsec_miiphy_write(char *devname, unsigned char addr,
  1479. unsigned char reg, unsigned short value)
  1480. {
  1481. struct tsec_private *priv = privlist[0];
  1482. if (NULL == priv) {
  1483. printf("Can't write PHY at address %d\n", addr);
  1484. return -1;
  1485. }
  1486. write_any_phy_reg(priv, addr, reg, value);
  1487. return 0;
  1488. }
  1489. #endif
  1490. #ifdef CONFIG_MCAST_TFTP
  1491. /* CREDITS: linux gianfar driver, slightly adjusted... thanx. */
  1492. /* Set the appropriate hash bit for the given addr */
  1493. /* The algorithm works like so:
  1494. * 1) Take the Destination Address (ie the multicast address), and
  1495. * do a CRC on it (little endian), and reverse the bits of the
  1496. * result.
  1497. * 2) Use the 8 most significant bits as a hash into a 256-entry
  1498. * table. The table is controlled through 8 32-bit registers:
  1499. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
  1500. * gaddr7. This means that the 3 most significant bits in the
  1501. * hash index which gaddr register to use, and the 5 other bits
  1502. * indicate which bit (assuming an IBM numbering scheme, which
  1503. * for PowerPC (tm) is usually the case) in the tregister holds
  1504. * the entry. */
  1505. static int
  1506. tsec_mcast_addr (struct eth_device *dev, u8 mcast_mac, u8 set)
  1507. {
  1508. struct tsec_private *priv = privlist[1];
  1509. volatile tsec_t *regs = priv->regs;
  1510. volatile u32 *reg_array, value;
  1511. u8 result, whichbit, whichreg;
  1512. result = (u8)((ether_crc(MAC_ADDR_LEN,mcast_mac) >> 24) & 0xff);
  1513. whichbit = result & 0x1f; /* the 5 LSB = which bit to set */
  1514. whichreg = result >> 5; /* the 3 MSB = which reg to set it in */
  1515. value = (1 << (31-whichbit));
  1516. reg_array = &(regs->hash.gaddr0);
  1517. if (set) {
  1518. reg_array[whichreg] |= value;
  1519. } else {
  1520. reg_array[whichreg] &= ~value;
  1521. }
  1522. return 0;
  1523. }
  1524. #endif /* Multicast TFTP ? */