tsec.c 39 KB

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