tsec.c 37 KB

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