tsec.c 36 KB

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