tsec.c 39 KB

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