tsec.c 39 KB

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