tsec.c 36 KB

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