tsec.c 39 KB

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