tsec.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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_TSEC1)
  61. #if defined(CONFIG_MPC8544DS) || defined(CONFIG_MPC8641HPCN)
  62. {TSEC1_PHY_ADDR, TSEC_GIGABIT | TSEC_REDUCED, TSEC1_PHYIDX},
  63. #else
  64. {TSEC1_PHY_ADDR, TSEC_GIGABIT, TSEC1_PHYIDX},
  65. #endif
  66. #else
  67. {0, 0, 0},
  68. #endif
  69. #if defined(CONFIG_TSEC2)
  70. #if defined(CONFIG_MPC8641HPCN)
  71. {TSEC2_PHY_ADDR, TSEC_GIGABIT | TSEC_REDUCED, TSEC2_PHYIDX},
  72. #else
  73. {TSEC2_PHY_ADDR, TSEC_GIGABIT, TSEC2_PHYIDX},
  74. #endif
  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_TSEC3)
  82. {TSEC3_PHY_ADDR, TSEC_GIGABIT | TSEC_REDUCED, TSEC3_PHYIDX},
  83. #else
  84. {0, 0, 0},
  85. #endif
  86. #if defined(CONFIG_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) || defined(CONFIG_CMD_MII) \
  156. && !defined(BITBANGMII)
  157. miiphy_register(dev->name, tsec_miiphy_read, tsec_miiphy_write);
  158. #endif
  159. /* Try to initialize PHY here, and return */
  160. return init_phy(dev);
  161. }
  162. /* Initializes data structures and registers for the controller,
  163. * and brings the interface up. Returns the link status, meaning
  164. * that it returns success if the link is up, failure otherwise.
  165. * This allows u-boot to find the first active controller.
  166. */
  167. int tsec_init(struct eth_device *dev, bd_t * bd)
  168. {
  169. uint tempval;
  170. char tmpbuf[MAC_ADDR_LEN];
  171. int i;
  172. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  173. volatile tsec_t *regs = priv->regs;
  174. /* Make sure the controller is stopped */
  175. tsec_halt(dev);
  176. /* Init MACCFG2. Defaults to GMII */
  177. regs->maccfg2 = MACCFG2_INIT_SETTINGS;
  178. /* Init ECNTRL */
  179. regs->ecntrl = ECNTRL_INIT_SETTINGS;
  180. /* Copy the station address into the address registers.
  181. * Backwards, because little endian MACS are dumb */
  182. for (i = 0; i < MAC_ADDR_LEN; i++) {
  183. tmpbuf[MAC_ADDR_LEN - 1 - i] = dev->enetaddr[i];
  184. }
  185. regs->macstnaddr1 = *((uint *) (tmpbuf));
  186. tempval = *((uint *) (tmpbuf + 4));
  187. regs->macstnaddr2 = tempval;
  188. /* reset the indices to zero */
  189. rxIdx = 0;
  190. txIdx = 0;
  191. /* Clear out (for the most part) the other registers */
  192. init_registers(regs);
  193. /* Ready the device for tx/rx */
  194. startup_tsec(dev);
  195. /* If there's no link, fail */
  196. return priv->link;
  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 = CFG_TBIPA_VALUE;
  251. regs = (volatile tsec_t *)(TSEC_BASE_ADDR + TSEC_SIZE);
  252. regs->tbipa = CFG_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. struct phy_info phy_info_M88E1149S = {
  743. 0x1410ca,
  744. "Marvell 88E1149S",
  745. 4,
  746. (struct phy_cmd[]){ /* config */
  747. /* Reset and configure the PHY */
  748. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  749. {0x1d, 0x1f, NULL},
  750. {0x1e, 0x200c, NULL},
  751. {0x1d, 0x5, NULL},
  752. {0x1e, 0x0, NULL},
  753. {0x1e, 0x100, NULL},
  754. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  755. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  756. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  757. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  758. {miim_end,}
  759. },
  760. (struct phy_cmd[]){ /* startup */
  761. /* Status is read once to clear old link state */
  762. {MIIM_STATUS, miim_read, NULL},
  763. /* Auto-negotiate */
  764. {MIIM_STATUS, miim_read, &mii_parse_sr},
  765. /* Read the status */
  766. {MIIM_88E1011_PHY_STATUS, miim_read,
  767. &mii_parse_88E1011_psr},
  768. {miim_end,}
  769. },
  770. (struct phy_cmd[]){ /* shutdown */
  771. {miim_end,}
  772. },
  773. };
  774. /* The 5411 id is 0x206070, the 5421 is 0x2060e0 */
  775. struct phy_info phy_info_BCM5461S = {
  776. 0x02060c1, /* 5461 ID */
  777. "Broadcom BCM5461S",
  778. 0, /* not clear to me what minor revisions we can shift away */
  779. (struct phy_cmd[]) { /* config */
  780. /* Reset and configure the PHY */
  781. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  782. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  783. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  784. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  785. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  786. {miim_end,}
  787. },
  788. (struct phy_cmd[]) { /* startup */
  789. /* Status is read once to clear old link state */
  790. {MIIM_STATUS, miim_read, NULL},
  791. /* Auto-negotiate */
  792. {MIIM_STATUS, miim_read, &mii_parse_sr},
  793. /* Read the status */
  794. {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
  795. {miim_end,}
  796. },
  797. (struct phy_cmd[]) { /* shutdown */
  798. {miim_end,}
  799. },
  800. };
  801. struct phy_info phy_info_BCM5464S = {
  802. 0x02060b1, /* 5464 ID */
  803. "Broadcom BCM5464S",
  804. 0, /* not clear to me what minor revisions we can shift away */
  805. (struct phy_cmd[]) { /* config */
  806. /* Reset and configure the PHY */
  807. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  808. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  809. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  810. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  811. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  812. {miim_end,}
  813. },
  814. (struct phy_cmd[]) { /* startup */
  815. /* Status is read once to clear old link state */
  816. {MIIM_STATUS, miim_read, NULL},
  817. /* Auto-negotiate */
  818. {MIIM_STATUS, miim_read, &mii_parse_sr},
  819. /* Read the status */
  820. {MIIM_BCM54xx_AUXSTATUS, miim_read, &mii_parse_BCM54xx_sr},
  821. {miim_end,}
  822. },
  823. (struct phy_cmd[]) { /* shutdown */
  824. {miim_end,}
  825. },
  826. };
  827. struct phy_info phy_info_M88E1011S = {
  828. 0x01410c6,
  829. "Marvell 88E1011S",
  830. 4,
  831. (struct phy_cmd[]){ /* config */
  832. /* Reset and configure the PHY */
  833. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  834. {0x1d, 0x1f, NULL},
  835. {0x1e, 0x200c, NULL},
  836. {0x1d, 0x5, NULL},
  837. {0x1e, 0x0, NULL},
  838. {0x1e, 0x100, NULL},
  839. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  840. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  841. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  842. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  843. {miim_end,}
  844. },
  845. (struct phy_cmd[]){ /* startup */
  846. /* Status is read once to clear old link state */
  847. {MIIM_STATUS, miim_read, NULL},
  848. /* Auto-negotiate */
  849. {MIIM_STATUS, miim_read, &mii_parse_sr},
  850. /* Read the status */
  851. {MIIM_88E1011_PHY_STATUS, miim_read,
  852. &mii_parse_88E1011_psr},
  853. {miim_end,}
  854. },
  855. (struct phy_cmd[]){ /* shutdown */
  856. {miim_end,}
  857. },
  858. };
  859. struct phy_info phy_info_M88E1111S = {
  860. 0x01410cc,
  861. "Marvell 88E1111S",
  862. 4,
  863. (struct phy_cmd[]){ /* config */
  864. /* Reset and configure the PHY */
  865. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  866. {0x14, 0x0cd2, NULL}, /* Delay RGMII TX and RX */
  867. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  868. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  869. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  870. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  871. {miim_end,}
  872. },
  873. (struct phy_cmd[]){ /* startup */
  874. /* Status is read once to clear old link state */
  875. {MIIM_STATUS, miim_read, NULL},
  876. /* Auto-negotiate */
  877. {MIIM_STATUS, miim_read, &mii_parse_sr},
  878. /* Read the status */
  879. {MIIM_88E1011_PHY_STATUS, miim_read,
  880. &mii_parse_88E1011_psr},
  881. {miim_end,}
  882. },
  883. (struct phy_cmd[]){ /* shutdown */
  884. {miim_end,}
  885. },
  886. };
  887. static unsigned int m88e1145_setmode(uint mii_reg, struct tsec_private *priv)
  888. {
  889. uint mii_data = read_phy_reg(priv, mii_reg);
  890. /* Setting MIIM_88E1145_PHY_EXT_CR */
  891. if (priv->flags & TSEC_REDUCED)
  892. return mii_data |
  893. MIIM_M88E1145_RGMII_RX_DELAY | MIIM_M88E1145_RGMII_TX_DELAY;
  894. else
  895. return mii_data;
  896. }
  897. static struct phy_info phy_info_M88E1145 = {
  898. 0x01410cd,
  899. "Marvell 88E1145",
  900. 4,
  901. (struct phy_cmd[]){ /* config */
  902. /* Reset the PHY */
  903. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  904. /* Errata E0, E1 */
  905. {29, 0x001b, NULL},
  906. {30, 0x418f, NULL},
  907. {29, 0x0016, NULL},
  908. {30, 0xa2da, NULL},
  909. /* Configure the PHY */
  910. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  911. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  912. {MIIM_88E1011_PHY_SCR, MIIM_88E1011_PHY_MDI_X_AUTO,
  913. NULL},
  914. {MIIM_88E1145_PHY_EXT_CR, 0, &m88e1145_setmode},
  915. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  916. {MIIM_CONTROL, MIIM_CONTROL_INIT, NULL},
  917. {miim_end,}
  918. },
  919. (struct phy_cmd[]){ /* startup */
  920. /* Status is read once to clear old link state */
  921. {MIIM_STATUS, miim_read, NULL},
  922. /* Auto-negotiate */
  923. {MIIM_STATUS, miim_read, &mii_parse_sr},
  924. {MIIM_88E1111_PHY_LED_CONTROL,
  925. MIIM_88E1111_PHY_LED_DIRECT, NULL},
  926. /* Read the Status */
  927. {MIIM_88E1011_PHY_STATUS, miim_read,
  928. &mii_parse_88E1011_psr},
  929. {miim_end,}
  930. },
  931. (struct phy_cmd[]){ /* shutdown */
  932. {miim_end,}
  933. },
  934. };
  935. struct phy_info phy_info_cis8204 = {
  936. 0x3f11,
  937. "Cicada Cis8204",
  938. 6,
  939. (struct phy_cmd[]){ /* config */
  940. /* Override PHY config settings */
  941. {MIIM_CIS8201_AUX_CONSTAT,
  942. MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  943. /* Configure some basic stuff */
  944. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  945. {MIIM_CIS8204_SLED_CON, MIIM_CIS8204_SLEDCON_INIT,
  946. &mii_cis8204_fixled},
  947. {MIIM_CIS8204_EPHY_CON, MIIM_CIS8204_EPHYCON_INIT,
  948. &mii_cis8204_setmode},
  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_CIS8201_AUX_CONSTAT, miim_read,
  958. &mii_parse_cis8201},
  959. {miim_end,}
  960. },
  961. (struct phy_cmd[]){ /* shutdown */
  962. {miim_end,}
  963. },
  964. };
  965. /* Cicada 8201 */
  966. struct phy_info phy_info_cis8201 = {
  967. 0xfc41,
  968. "CIS8201",
  969. 4,
  970. (struct phy_cmd[]){ /* config */
  971. /* Override PHY config settings */
  972. {MIIM_CIS8201_AUX_CONSTAT,
  973. MIIM_CIS8201_AUXCONSTAT_INIT, NULL},
  974. /* Set up the interface mode */
  975. {MIIM_CIS8201_EXT_CON1, MIIM_CIS8201_EXTCON1_INIT,
  976. NULL},
  977. /* Configure some basic stuff */
  978. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  979. {miim_end,}
  980. },
  981. (struct phy_cmd[]){ /* startup */
  982. /* Read the Status (2x to make sure link is right) */
  983. {MIIM_STATUS, miim_read, NULL},
  984. /* Auto-negotiate */
  985. {MIIM_STATUS, miim_read, &mii_parse_sr},
  986. /* Read the status */
  987. {MIIM_CIS8201_AUX_CONSTAT, miim_read,
  988. &mii_parse_cis8201},
  989. {miim_end,}
  990. },
  991. (struct phy_cmd[]){ /* shutdown */
  992. {miim_end,}
  993. },
  994. };
  995. struct phy_info phy_info_VSC8244 = {
  996. 0x3f1b,
  997. "Vitesse VSC8244",
  998. 6,
  999. (struct phy_cmd[]){ /* config */
  1000. /* Override PHY config settings */
  1001. /* Configure some basic stuff */
  1002. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  1003. {miim_end,}
  1004. },
  1005. (struct phy_cmd[]){ /* startup */
  1006. /* Read the Status (2x to make sure link is right) */
  1007. {MIIM_STATUS, miim_read, NULL},
  1008. /* Auto-negotiate */
  1009. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1010. /* Read the status */
  1011. {MIIM_VSC8244_AUX_CONSTAT, miim_read,
  1012. &mii_parse_vsc8244},
  1013. {miim_end,}
  1014. },
  1015. (struct phy_cmd[]){ /* shutdown */
  1016. {miim_end,}
  1017. },
  1018. };
  1019. struct phy_info phy_info_dm9161 = {
  1020. 0x0181b88,
  1021. "Davicom DM9161E",
  1022. 4,
  1023. (struct phy_cmd[]){ /* config */
  1024. {MIIM_CONTROL, MIIM_DM9161_CR_STOP, NULL},
  1025. /* Do not bypass the scrambler/descrambler */
  1026. {MIIM_DM9161_SCR, MIIM_DM9161_SCR_INIT, NULL},
  1027. /* Clear 10BTCSR to default */
  1028. {MIIM_DM9161_10BTCSR, MIIM_DM9161_10BTCSR_INIT,
  1029. NULL},
  1030. /* Configure some basic stuff */
  1031. {MIIM_CONTROL, MIIM_CR_INIT, NULL},
  1032. /* Restart Auto Negotiation */
  1033. {MIIM_CONTROL, MIIM_DM9161_CR_RSTAN, NULL},
  1034. {miim_end,}
  1035. },
  1036. (struct phy_cmd[]){ /* startup */
  1037. /* Status is read once to clear old link state */
  1038. {MIIM_STATUS, miim_read, NULL},
  1039. /* Auto-negotiate */
  1040. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1041. /* Read the status */
  1042. {MIIM_DM9161_SCSR, miim_read,
  1043. &mii_parse_dm9161_scsr},
  1044. {miim_end,}
  1045. },
  1046. (struct phy_cmd[]){ /* shutdown */
  1047. {miim_end,}
  1048. },
  1049. };
  1050. /* a generic flavor. */
  1051. struct phy_info phy_info_generic = {
  1052. 0,
  1053. "Unknown/Generic PHY",
  1054. 32,
  1055. (struct phy_cmd[]) { /* config */
  1056. {PHY_BMCR, PHY_BMCR_RESET, NULL},
  1057. {PHY_BMCR, PHY_BMCR_AUTON|PHY_BMCR_RST_NEG, NULL},
  1058. {miim_end,}
  1059. },
  1060. (struct phy_cmd[]) { /* startup */
  1061. {PHY_BMSR, miim_read, NULL},
  1062. {PHY_BMSR, miim_read, &mii_parse_sr},
  1063. {PHY_BMSR, miim_read, &mii_parse_link},
  1064. {miim_end,}
  1065. },
  1066. (struct phy_cmd[]) { /* shutdown */
  1067. {miim_end,}
  1068. }
  1069. };
  1070. uint mii_parse_lxt971_sr2(uint mii_reg, struct tsec_private *priv)
  1071. {
  1072. unsigned int speed;
  1073. if (priv->link) {
  1074. speed = mii_reg & MIIM_LXT971_SR2_SPEED_MASK;
  1075. switch (speed) {
  1076. case MIIM_LXT971_SR2_10HDX:
  1077. priv->speed = 10;
  1078. priv->duplexity = 0;
  1079. break;
  1080. case MIIM_LXT971_SR2_10FDX:
  1081. priv->speed = 10;
  1082. priv->duplexity = 1;
  1083. break;
  1084. case MIIM_LXT971_SR2_100HDX:
  1085. priv->speed = 100;
  1086. priv->duplexity = 0;
  1087. default:
  1088. priv->speed = 100;
  1089. priv->duplexity = 1;
  1090. break;
  1091. }
  1092. } else {
  1093. priv->speed = 0;
  1094. priv->duplexity = 0;
  1095. }
  1096. return 0;
  1097. }
  1098. static struct phy_info phy_info_lxt971 = {
  1099. 0x0001378e,
  1100. "LXT971",
  1101. 4,
  1102. (struct phy_cmd[]){ /* config */
  1103. {MIIM_CR, MIIM_CR_INIT, mii_cr_init}, /* autonegotiate */
  1104. {miim_end,}
  1105. },
  1106. (struct phy_cmd[]){ /* startup - enable interrupts */
  1107. /* { 0x12, 0x00f2, NULL }, */
  1108. {MIIM_STATUS, miim_read, NULL},
  1109. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1110. {MIIM_LXT971_SR2, miim_read, &mii_parse_lxt971_sr2},
  1111. {miim_end,}
  1112. },
  1113. (struct phy_cmd[]){ /* shutdown - disable interrupts */
  1114. {miim_end,}
  1115. },
  1116. };
  1117. /* Parse the DP83865's link and auto-neg status register for speed and duplex
  1118. * information
  1119. */
  1120. uint mii_parse_dp83865_lanr(uint mii_reg, struct tsec_private *priv)
  1121. {
  1122. switch (mii_reg & MIIM_DP83865_SPD_MASK) {
  1123. case MIIM_DP83865_SPD_1000:
  1124. priv->speed = 1000;
  1125. break;
  1126. case MIIM_DP83865_SPD_100:
  1127. priv->speed = 100;
  1128. break;
  1129. default:
  1130. priv->speed = 10;
  1131. break;
  1132. }
  1133. if (mii_reg & MIIM_DP83865_DPX_FULL)
  1134. priv->duplexity = 1;
  1135. else
  1136. priv->duplexity = 0;
  1137. return 0;
  1138. }
  1139. struct phy_info phy_info_dp83865 = {
  1140. 0x20005c7,
  1141. "NatSemi DP83865",
  1142. 4,
  1143. (struct phy_cmd[]){ /* config */
  1144. {MIIM_CONTROL, MIIM_DP83865_CR_INIT, NULL},
  1145. {miim_end,}
  1146. },
  1147. (struct phy_cmd[]){ /* startup */
  1148. /* Status is read once to clear old link state */
  1149. {MIIM_STATUS, miim_read, NULL},
  1150. /* Auto-negotiate */
  1151. {MIIM_STATUS, miim_read, &mii_parse_sr},
  1152. /* Read the link and auto-neg status */
  1153. {MIIM_DP83865_LANR, miim_read,
  1154. &mii_parse_dp83865_lanr},
  1155. {miim_end,}
  1156. },
  1157. (struct phy_cmd[]){ /* shutdown */
  1158. {miim_end,}
  1159. },
  1160. };
  1161. struct phy_info *phy_info[] = {
  1162. &phy_info_cis8204,
  1163. &phy_info_cis8201,
  1164. &phy_info_BCM5461S,
  1165. &phy_info_BCM5464S,
  1166. &phy_info_M88E1011S,
  1167. &phy_info_M88E1111S,
  1168. &phy_info_M88E1145,
  1169. &phy_info_M88E1149S,
  1170. &phy_info_dm9161,
  1171. &phy_info_lxt971,
  1172. &phy_info_VSC8244,
  1173. &phy_info_dp83865,
  1174. &phy_info_generic,
  1175. NULL
  1176. };
  1177. /* Grab the identifier of the device's PHY, and search through
  1178. * all of the known PHYs to see if one matches. If so, return
  1179. * it, if not, return NULL
  1180. */
  1181. struct phy_info *get_phy_info(struct eth_device *dev)
  1182. {
  1183. struct tsec_private *priv = (struct tsec_private *)dev->priv;
  1184. uint phy_reg, phy_ID;
  1185. int i;
  1186. struct phy_info *theInfo = NULL;
  1187. /* Grab the bits from PHYIR1, and put them in the upper half */
  1188. phy_reg = read_phy_reg(priv, MIIM_PHYIR1);
  1189. phy_ID = (phy_reg & 0xffff) << 16;
  1190. /* Grab the bits from PHYIR2, and put them in the lower half */
  1191. phy_reg = read_phy_reg(priv, MIIM_PHYIR2);
  1192. phy_ID |= (phy_reg & 0xffff);
  1193. /* loop through all the known PHY types, and find one that */
  1194. /* matches the ID we read from the PHY. */
  1195. for (i = 0; phy_info[i]; i++) {
  1196. if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
  1197. theInfo = phy_info[i];
  1198. break;
  1199. }
  1200. }
  1201. if (theInfo == NULL) {
  1202. printf("%s: PHY id %x is not supported!\n", dev->name, phy_ID);
  1203. return NULL;
  1204. } else {
  1205. debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
  1206. }
  1207. return theInfo;
  1208. }
  1209. /* Execute the given series of commands on the given device's
  1210. * PHY, running functions as necessary
  1211. */
  1212. void phy_run_commands(struct tsec_private *priv, struct phy_cmd *cmd)
  1213. {
  1214. int i;
  1215. uint result;
  1216. volatile tsec_t *phyregs = priv->phyregs;
  1217. phyregs->miimcfg = MIIMCFG_RESET;
  1218. phyregs->miimcfg = MIIMCFG_INIT_VALUE;
  1219. while (phyregs->miimind & MIIMIND_BUSY) ;
  1220. for (i = 0; cmd->mii_reg != miim_end; i++) {
  1221. if (cmd->mii_data == miim_read) {
  1222. result = read_phy_reg(priv, cmd->mii_reg);
  1223. if (cmd->funct != NULL)
  1224. (*(cmd->funct)) (result, priv);
  1225. } else {
  1226. if (cmd->funct != NULL)
  1227. result = (*(cmd->funct)) (cmd->mii_reg, priv);
  1228. else
  1229. result = cmd->mii_data;
  1230. write_phy_reg(priv, cmd->mii_reg, result);
  1231. }
  1232. cmd++;
  1233. }
  1234. }
  1235. /* Relocate the function pointers in the phy cmd lists */
  1236. static void relocate_cmds(void)
  1237. {
  1238. struct phy_cmd **cmdlistptr;
  1239. struct phy_cmd *cmd;
  1240. int i, j, k;
  1241. for (i = 0; phy_info[i]; i++) {
  1242. /* First thing's first: relocate the pointers to the
  1243. * PHY command structures (the structs were done) */
  1244. phy_info[i] = (struct phy_info *)((uint) phy_info[i]
  1245. + gd->reloc_off);
  1246. phy_info[i]->name += gd->reloc_off;
  1247. phy_info[i]->config =
  1248. (struct phy_cmd *)((uint) phy_info[i]->config
  1249. + gd->reloc_off);
  1250. phy_info[i]->startup =
  1251. (struct phy_cmd *)((uint) phy_info[i]->startup
  1252. + gd->reloc_off);
  1253. phy_info[i]->shutdown =
  1254. (struct phy_cmd *)((uint) phy_info[i]->shutdown
  1255. + gd->reloc_off);
  1256. cmdlistptr = &phy_info[i]->config;
  1257. j = 0;
  1258. for (; cmdlistptr <= &phy_info[i]->shutdown; cmdlistptr++) {
  1259. k = 0;
  1260. for (cmd = *cmdlistptr;
  1261. cmd->mii_reg != miim_end;
  1262. cmd++) {
  1263. /* Only relocate non-NULL pointers */
  1264. if (cmd->funct)
  1265. cmd->funct += gd->reloc_off;
  1266. k++;
  1267. }
  1268. j++;
  1269. }
  1270. }
  1271. relocated = 1;
  1272. }
  1273. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) \
  1274. && !defined(BITBANGMII)
  1275. struct tsec_private *get_priv_for_phy(unsigned char phyaddr)
  1276. {
  1277. int i;
  1278. for (i = 0; i < MAXCONTROLLERS; i++) {
  1279. if (privlist[i]->phyaddr == phyaddr)
  1280. return privlist[i];
  1281. }
  1282. return NULL;
  1283. }
  1284. /*
  1285. * Read a MII PHY register.
  1286. *
  1287. * Returns:
  1288. * 0 on success
  1289. */
  1290. static int tsec_miiphy_read(char *devname, unsigned char addr,
  1291. unsigned char reg, unsigned short *value)
  1292. {
  1293. unsigned short ret;
  1294. struct tsec_private *priv = get_priv_for_phy(addr);
  1295. if (NULL == priv) {
  1296. printf("Can't read PHY at address %d\n", addr);
  1297. return -1;
  1298. }
  1299. ret = (unsigned short)read_phy_reg(priv, reg);
  1300. *value = ret;
  1301. return 0;
  1302. }
  1303. /*
  1304. * Write a MII PHY register.
  1305. *
  1306. * Returns:
  1307. * 0 on success
  1308. */
  1309. static int tsec_miiphy_write(char *devname, unsigned char addr,
  1310. unsigned char reg, unsigned short value)
  1311. {
  1312. struct tsec_private *priv = get_priv_for_phy(addr);
  1313. if (NULL == priv) {
  1314. printf("Can't write PHY at address %d\n", addr);
  1315. return -1;
  1316. }
  1317. write_phy_reg(priv, reg, value);
  1318. return 0;
  1319. }
  1320. #endif
  1321. #endif /* CONFIG_TSEC_ENET */