tsec.c 41 KB

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