tsec.c 43 KB

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