uec_phy.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * Copyright (C) 2005,2010-2011 Freescale Semiconductor, Inc.
  3. *
  4. * Author: Shlomi Gridish
  5. *
  6. * Description: UCC GETH Driver -- PHY handling
  7. * Driver for UEC on QE
  8. * Based on 8260_io/fcc_enet.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include "common.h"
  17. #include "net.h"
  18. #include "malloc.h"
  19. #include "asm/errno.h"
  20. #include "asm/immap_qe.h"
  21. #include "asm/io.h"
  22. #include "qe.h"
  23. #include "uccf.h"
  24. #include "uec.h"
  25. #include "uec_phy.h"
  26. #include "miiphy.h"
  27. #define ugphy_printk(format, arg...) \
  28. printf(format "\n", ## arg)
  29. #define ugphy_dbg(format, arg...) \
  30. ugphy_printk(format , ## arg)
  31. #define ugphy_err(format, arg...) \
  32. ugphy_printk(format , ## arg)
  33. #define ugphy_info(format, arg...) \
  34. ugphy_printk(format , ## arg)
  35. #define ugphy_warn(format, arg...) \
  36. ugphy_printk(format , ## arg)
  37. #ifdef UEC_VERBOSE_DEBUG
  38. #define ugphy_vdbg ugphy_dbg
  39. #else
  40. #define ugphy_vdbg(ugeth, fmt, args...) do { } while (0)
  41. #endif /* UEC_VERBOSE_DEBUG */
  42. /*--------------------------------------------------------------------+
  43. * Fixed PHY (PHY-less) support for Ethernet Ports.
  44. *
  45. * Copied from arch/powerpc/cpu/ppc4xx/4xx_enet.c
  46. *--------------------------------------------------------------------*/
  47. /*
  48. * Some boards do not have a PHY for each ethernet port. These ports are known
  49. * as Fixed PHY (or PHY-less) ports. For such ports, set the appropriate
  50. * CONFIG_SYS_UECx_PHY_ADDR equal to CONFIG_FIXED_PHY_ADDR (an unused address)
  51. * When the drver tries to identify the PHYs, CONFIG_FIXED_PHY will be returned
  52. * and the driver will search CONFIG_SYS_FIXED_PHY_PORTS to find what network
  53. * speed and duplex should be for the port.
  54. *
  55. * Example board header configuration file:
  56. * #define CONFIG_FIXED_PHY 0xFFFFFFFF
  57. * #define CONFIG_SYS_FIXED_PHY_ADDR 0x1E (pick an unused phy address)
  58. *
  59. * #define CONFIG_SYS_UEC1_PHY_ADDR CONFIG_SYS_FIXED_PHY_ADDR
  60. * #define CONFIG_SYS_UEC2_PHY_ADDR 0x02
  61. * #define CONFIG_SYS_UEC3_PHY_ADDR CONFIG_SYS_FIXED_PHY_ADDR
  62. * #define CONFIG_SYS_UEC4_PHY_ADDR 0x04
  63. *
  64. * #define CONFIG_SYS_FIXED_PHY_PORT(name,speed,duplex) \
  65. * {name, speed, duplex},
  66. *
  67. * #define CONFIG_SYS_FIXED_PHY_PORTS \
  68. * CONFIG_SYS_FIXED_PHY_PORT("UEC0",SPEED_100,DUPLEX_FULL) \
  69. * CONFIG_SYS_FIXED_PHY_PORT("UEC2",SPEED_100,DUPLEX_HALF)
  70. */
  71. #ifndef CONFIG_FIXED_PHY
  72. #define CONFIG_FIXED_PHY 0xFFFFFFFF /* Fixed PHY (PHY-less) */
  73. #endif
  74. #ifndef CONFIG_SYS_FIXED_PHY_PORTS
  75. #define CONFIG_SYS_FIXED_PHY_PORTS /* default is an empty array */
  76. #endif
  77. struct fixed_phy_port {
  78. char name[NAMESIZE]; /* ethernet port name */
  79. unsigned int speed; /* specified speed 10,100 or 1000 */
  80. unsigned int duplex; /* specified duplex FULL or HALF */
  81. };
  82. static const struct fixed_phy_port fixed_phy_port[] = {
  83. CONFIG_SYS_FIXED_PHY_PORTS /* defined in board configuration file */
  84. };
  85. /*--------------------------------------------------------------------+
  86. * BitBang MII support for ethernet ports
  87. *
  88. * Based from MPC8560ADS implementation
  89. *--------------------------------------------------------------------*/
  90. /*
  91. * Example board header file to define bitbang ethernet ports:
  92. *
  93. * #define CONFIG_SYS_BITBANG_PHY_PORT(name) name,
  94. * #define CONFIG_SYS_BITBANG_PHY_PORTS CONFIG_SYS_BITBANG_PHY_PORT("UEC0")
  95. */
  96. #ifndef CONFIG_SYS_BITBANG_PHY_PORTS
  97. #define CONFIG_SYS_BITBANG_PHY_PORTS /* default is an empty array */
  98. #endif
  99. #if defined(CONFIG_BITBANGMII)
  100. static const char *bitbang_phy_port[] = {
  101. CONFIG_SYS_BITBANG_PHY_PORTS /* defined in board configuration file */
  102. };
  103. #endif /* CONFIG_BITBANGMII */
  104. static void config_genmii_advert (struct uec_mii_info *mii_info);
  105. static void genmii_setup_forced (struct uec_mii_info *mii_info);
  106. static void genmii_restart_aneg (struct uec_mii_info *mii_info);
  107. static int gbit_config_aneg (struct uec_mii_info *mii_info);
  108. static int genmii_config_aneg (struct uec_mii_info *mii_info);
  109. static int genmii_update_link (struct uec_mii_info *mii_info);
  110. static int genmii_read_status (struct uec_mii_info *mii_info);
  111. u16 uec_phy_read(struct uec_mii_info *mii_info, u16 regnum);
  112. void uec_phy_write(struct uec_mii_info *mii_info, u16 regnum, u16 val);
  113. /* Write value to the PHY for this device to the register at regnum, */
  114. /* waiting until the write is done before it returns. All PHY */
  115. /* configuration has to be done through the TSEC1 MIIM regs */
  116. void uec_write_phy_reg (struct eth_device *dev, int mii_id, int regnum, int value)
  117. {
  118. uec_private_t *ugeth = (uec_private_t *) dev->priv;
  119. uec_mii_t *ug_regs;
  120. enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
  121. u32 tmp_reg;
  122. #if defined(CONFIG_BITBANGMII)
  123. u32 i = 0;
  124. for (i = 0; i < ARRAY_SIZE(bitbang_phy_port); i++) {
  125. if (strncmp(dev->name, bitbang_phy_port[i],
  126. sizeof(dev->name)) == 0) {
  127. (void)bb_miiphy_write(NULL, mii_id, regnum, value);
  128. return;
  129. }
  130. }
  131. #endif /* CONFIG_BITBANGMII */
  132. ug_regs = ugeth->uec_mii_regs;
  133. /* Stop the MII management read cycle */
  134. out_be32 (&ug_regs->miimcom, 0);
  135. /* Setting up the MII Mangement Address Register */
  136. tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
  137. out_be32 (&ug_regs->miimadd, tmp_reg);
  138. /* Setting up the MII Mangement Control Register with the value */
  139. out_be32 (&ug_regs->miimcon, (u32) value);
  140. sync();
  141. /* Wait till MII management write is complete */
  142. while ((in_be32 (&ug_regs->miimind)) & MIIMIND_BUSY);
  143. }
  144. /* Reads from register regnum in the PHY for device dev, */
  145. /* returning the value. Clears miimcom first. All PHY */
  146. /* configuration has to be done through the TSEC1 MIIM regs */
  147. int uec_read_phy_reg (struct eth_device *dev, int mii_id, int regnum)
  148. {
  149. uec_private_t *ugeth = (uec_private_t *) dev->priv;
  150. uec_mii_t *ug_regs;
  151. enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
  152. u32 tmp_reg;
  153. u16 value;
  154. #if defined(CONFIG_BITBANGMII)
  155. u32 i = 0;
  156. for (i = 0; i < ARRAY_SIZE(bitbang_phy_port); i++) {
  157. if (strncmp(dev->name, bitbang_phy_port[i],
  158. sizeof(dev->name)) == 0) {
  159. (void)bb_miiphy_read(NULL, mii_id, regnum, &value);
  160. return (value);
  161. }
  162. }
  163. #endif /* CONFIG_BITBANGMII */
  164. ug_regs = ugeth->uec_mii_regs;
  165. /* Setting up the MII Mangement Address Register */
  166. tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
  167. out_be32 (&ug_regs->miimadd, tmp_reg);
  168. /* clear MII management command cycle */
  169. out_be32 (&ug_regs->miimcom, 0);
  170. sync();
  171. /* Perform an MII management read cycle */
  172. out_be32 (&ug_regs->miimcom, MIIMCOM_READ_CYCLE);
  173. /* Wait till MII management write is complete */
  174. while ((in_be32 (&ug_regs->miimind)) &
  175. (MIIMIND_NOT_VALID | MIIMIND_BUSY));
  176. /* Read MII management status */
  177. value = (u16) in_be32 (&ug_regs->miimstat);
  178. if (value == 0xffff)
  179. ugphy_vdbg
  180. ("read wrong value : mii_id %d,mii_reg %d, base %08x",
  181. mii_id, mii_reg, (u32) & (ug_regs->miimcfg));
  182. return (value);
  183. }
  184. void mii_clear_phy_interrupt (struct uec_mii_info *mii_info)
  185. {
  186. if (mii_info->phyinfo->ack_interrupt)
  187. mii_info->phyinfo->ack_interrupt (mii_info);
  188. }
  189. void mii_configure_phy_interrupt (struct uec_mii_info *mii_info,
  190. u32 interrupts)
  191. {
  192. mii_info->interrupts = interrupts;
  193. if (mii_info->phyinfo->config_intr)
  194. mii_info->phyinfo->config_intr (mii_info);
  195. }
  196. /* Writes MII_ADVERTISE with the appropriate values, after
  197. * sanitizing advertise to make sure only supported features
  198. * are advertised
  199. */
  200. static void config_genmii_advert (struct uec_mii_info *mii_info)
  201. {
  202. u32 advertise;
  203. u16 adv;
  204. /* Only allow advertising what this PHY supports */
  205. mii_info->advertising &= mii_info->phyinfo->features;
  206. advertise = mii_info->advertising;
  207. /* Setup standard advertisement */
  208. adv = uec_phy_read(mii_info, MII_ADVERTISE);
  209. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
  210. if (advertise & ADVERTISED_10baseT_Half)
  211. adv |= ADVERTISE_10HALF;
  212. if (advertise & ADVERTISED_10baseT_Full)
  213. adv |= ADVERTISE_10FULL;
  214. if (advertise & ADVERTISED_100baseT_Half)
  215. adv |= ADVERTISE_100HALF;
  216. if (advertise & ADVERTISED_100baseT_Full)
  217. adv |= ADVERTISE_100FULL;
  218. uec_phy_write(mii_info, MII_ADVERTISE, adv);
  219. }
  220. static void genmii_setup_forced (struct uec_mii_info *mii_info)
  221. {
  222. u16 ctrl;
  223. u32 features = mii_info->phyinfo->features;
  224. ctrl = uec_phy_read(mii_info, MII_BMCR);
  225. ctrl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 |
  226. BMCR_SPEED1000 | BMCR_ANENABLE);
  227. ctrl |= BMCR_RESET;
  228. switch (mii_info->speed) {
  229. case SPEED_1000:
  230. if (features & (SUPPORTED_1000baseT_Half
  231. | SUPPORTED_1000baseT_Full)) {
  232. ctrl |= BMCR_SPEED1000;
  233. break;
  234. }
  235. mii_info->speed = SPEED_100;
  236. case SPEED_100:
  237. if (features & (SUPPORTED_100baseT_Half
  238. | SUPPORTED_100baseT_Full)) {
  239. ctrl |= BMCR_SPEED100;
  240. break;
  241. }
  242. mii_info->speed = SPEED_10;
  243. case SPEED_10:
  244. if (features & (SUPPORTED_10baseT_Half
  245. | SUPPORTED_10baseT_Full))
  246. break;
  247. default: /* Unsupported speed! */
  248. ugphy_err ("%s: Bad speed!", mii_info->dev->name);
  249. break;
  250. }
  251. uec_phy_write(mii_info, MII_BMCR, ctrl);
  252. }
  253. /* Enable and Restart Autonegotiation */
  254. static void genmii_restart_aneg (struct uec_mii_info *mii_info)
  255. {
  256. u16 ctl;
  257. ctl = uec_phy_read(mii_info, MII_BMCR);
  258. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  259. uec_phy_write(mii_info, MII_BMCR, ctl);
  260. }
  261. static int gbit_config_aneg (struct uec_mii_info *mii_info)
  262. {
  263. u16 adv;
  264. u32 advertise;
  265. if (mii_info->autoneg) {
  266. /* Configure the ADVERTISE register */
  267. config_genmii_advert (mii_info);
  268. advertise = mii_info->advertising;
  269. adv = uec_phy_read(mii_info, MII_CTRL1000);
  270. adv &= ~(ADVERTISE_1000FULL |
  271. ADVERTISE_1000HALF);
  272. if (advertise & SUPPORTED_1000baseT_Half)
  273. adv |= ADVERTISE_1000HALF;
  274. if (advertise & SUPPORTED_1000baseT_Full)
  275. adv |= ADVERTISE_1000FULL;
  276. uec_phy_write(mii_info, MII_CTRL1000, adv);
  277. /* Start/Restart aneg */
  278. genmii_restart_aneg (mii_info);
  279. } else
  280. genmii_setup_forced (mii_info);
  281. return 0;
  282. }
  283. static int marvell_config_aneg (struct uec_mii_info *mii_info)
  284. {
  285. /* The Marvell PHY has an errata which requires
  286. * that certain registers get written in order
  287. * to restart autonegotiation */
  288. uec_phy_write(mii_info, MII_BMCR, BMCR_RESET);
  289. uec_phy_write(mii_info, 0x1d, 0x1f);
  290. uec_phy_write(mii_info, 0x1e, 0x200c);
  291. uec_phy_write(mii_info, 0x1d, 0x5);
  292. uec_phy_write(mii_info, 0x1e, 0);
  293. uec_phy_write(mii_info, 0x1e, 0x100);
  294. gbit_config_aneg (mii_info);
  295. return 0;
  296. }
  297. static int genmii_config_aneg (struct uec_mii_info *mii_info)
  298. {
  299. if (mii_info->autoneg) {
  300. /* Speed up the common case, if link is already up, speed and
  301. duplex match, skip auto neg as it already matches */
  302. if (!genmii_read_status(mii_info) && mii_info->link)
  303. if (mii_info->duplex == DUPLEX_FULL &&
  304. mii_info->speed == SPEED_100)
  305. if (mii_info->advertising &
  306. ADVERTISED_100baseT_Full)
  307. return 0;
  308. config_genmii_advert (mii_info);
  309. genmii_restart_aneg (mii_info);
  310. } else
  311. genmii_setup_forced (mii_info);
  312. return 0;
  313. }
  314. static int genmii_update_link (struct uec_mii_info *mii_info)
  315. {
  316. u16 status;
  317. /* Status is read once to clear old link state */
  318. uec_phy_read(mii_info, MII_BMSR);
  319. /*
  320. * Wait if the link is up, and autonegotiation is in progress
  321. * (ie - we're capable and it's not done)
  322. */
  323. status = uec_phy_read(mii_info, MII_BMSR);
  324. if ((status & BMSR_LSTATUS) && (status & BMSR_ANEGCAPABLE)
  325. && !(status & BMSR_ANEGCOMPLETE)) {
  326. int i = 0;
  327. while (!(status & BMSR_ANEGCOMPLETE)) {
  328. /*
  329. * Timeout reached ?
  330. */
  331. if (i > UGETH_AN_TIMEOUT) {
  332. mii_info->link = 0;
  333. return 0;
  334. }
  335. i++;
  336. udelay(1000); /* 1 ms */
  337. status = uec_phy_read(mii_info, MII_BMSR);
  338. }
  339. mii_info->link = 1;
  340. } else {
  341. if (status & BMSR_LSTATUS)
  342. mii_info->link = 1;
  343. else
  344. mii_info->link = 0;
  345. }
  346. return 0;
  347. }
  348. static int genmii_read_status (struct uec_mii_info *mii_info)
  349. {
  350. u16 status;
  351. int err;
  352. /* Update the link, but return if there
  353. * was an error */
  354. err = genmii_update_link (mii_info);
  355. if (err)
  356. return err;
  357. if (mii_info->autoneg) {
  358. status = uec_phy_read(mii_info, MII_STAT1000);
  359. if (status & (LPA_1000FULL | LPA_1000HALF)) {
  360. mii_info->speed = SPEED_1000;
  361. if (status & LPA_1000FULL)
  362. mii_info->duplex = DUPLEX_FULL;
  363. else
  364. mii_info->duplex = DUPLEX_HALF;
  365. } else {
  366. status = uec_phy_read(mii_info, MII_LPA);
  367. if (status & (LPA_10FULL | LPA_100FULL))
  368. mii_info->duplex = DUPLEX_FULL;
  369. else
  370. mii_info->duplex = DUPLEX_HALF;
  371. if (status & (LPA_100FULL | LPA_100HALF))
  372. mii_info->speed = SPEED_100;
  373. else
  374. mii_info->speed = SPEED_10;
  375. }
  376. mii_info->pause = 0;
  377. }
  378. /* On non-aneg, we assume what we put in BMCR is the speed,
  379. * though magic-aneg shouldn't prevent this case from occurring
  380. */
  381. return 0;
  382. }
  383. static int bcm_init(struct uec_mii_info *mii_info)
  384. {
  385. struct eth_device *edev = mii_info->dev;
  386. uec_private_t *uec = edev->priv;
  387. gbit_config_aneg(mii_info);
  388. if ((uec->uec_info->enet_interface_type == RGMII_RXID) &&
  389. (uec->uec_info->speed == 1000)) {
  390. u16 val;
  391. int cnt = 50;
  392. /* Wait for aneg to complete. */
  393. do
  394. val = uec_phy_read(mii_info, MII_BMSR);
  395. while (--cnt && !(val & BMSR_ANEGCOMPLETE));
  396. /* Set RDX clk delay. */
  397. uec_phy_write(mii_info, 0x18, 0x7 | (7 << 12));
  398. val = uec_phy_read(mii_info, 0x18);
  399. /* Set RDX-RXC skew. */
  400. val |= (1 << 8);
  401. val |= (7 | (7 << 12));
  402. /* Write bits 14:0. */
  403. val |= (1 << 15);
  404. uec_phy_write(mii_info, 0x18, val);
  405. }
  406. return 0;
  407. }
  408. static int uec_marvell_init(struct uec_mii_info *mii_info)
  409. {
  410. struct eth_device *edev = mii_info->dev;
  411. uec_private_t *uec = edev->priv;
  412. enum fsl_phy_enet_if iface = uec->uec_info->enet_interface_type;
  413. int speed = uec->uec_info->speed;
  414. if ((speed == 1000) &&
  415. (iface == RGMII_ID ||
  416. iface == RGMII_RXID ||
  417. iface == RGMII_TXID)) {
  418. int temp;
  419. temp = uec_phy_read(mii_info, MII_M1111_PHY_EXT_CR);
  420. if (iface == RGMII_ID) {
  421. temp |= MII_M1111_RX_DELAY | MII_M1111_TX_DELAY;
  422. } else if (iface == RGMII_RXID) {
  423. temp &= ~MII_M1111_TX_DELAY;
  424. temp |= MII_M1111_RX_DELAY;
  425. } else if (iface == RGMII_TXID) {
  426. temp &= ~MII_M1111_RX_DELAY;
  427. temp |= MII_M1111_TX_DELAY;
  428. }
  429. uec_phy_write(mii_info, MII_M1111_PHY_EXT_CR, temp);
  430. temp = uec_phy_read(mii_info, MII_M1111_PHY_EXT_SR);
  431. temp &= ~MII_M1111_HWCFG_MODE_MASK;
  432. temp |= MII_M1111_HWCFG_MODE_RGMII;
  433. uec_phy_write(mii_info, MII_M1111_PHY_EXT_SR, temp);
  434. uec_phy_write(mii_info, MII_BMCR, BMCR_RESET);
  435. }
  436. return 0;
  437. }
  438. static int marvell_read_status (struct uec_mii_info *mii_info)
  439. {
  440. u16 status;
  441. int err;
  442. /* Update the link, but return if there
  443. * was an error */
  444. err = genmii_update_link (mii_info);
  445. if (err)
  446. return err;
  447. /* If the link is up, read the speed and duplex */
  448. /* If we aren't autonegotiating, assume speeds
  449. * are as set */
  450. if (mii_info->autoneg && mii_info->link) {
  451. int speed;
  452. status = uec_phy_read(mii_info, MII_M1011_PHY_SPEC_STATUS);
  453. /* Get the duplexity */
  454. if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
  455. mii_info->duplex = DUPLEX_FULL;
  456. else
  457. mii_info->duplex = DUPLEX_HALF;
  458. /* Get the speed */
  459. speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
  460. switch (speed) {
  461. case MII_M1011_PHY_SPEC_STATUS_1000:
  462. mii_info->speed = SPEED_1000;
  463. break;
  464. case MII_M1011_PHY_SPEC_STATUS_100:
  465. mii_info->speed = SPEED_100;
  466. break;
  467. default:
  468. mii_info->speed = SPEED_10;
  469. break;
  470. }
  471. mii_info->pause = 0;
  472. }
  473. return 0;
  474. }
  475. static int marvell_ack_interrupt (struct uec_mii_info *mii_info)
  476. {
  477. /* Clear the interrupts by reading the reg */
  478. uec_phy_read(mii_info, MII_M1011_IEVENT);
  479. return 0;
  480. }
  481. static int marvell_config_intr (struct uec_mii_info *mii_info)
  482. {
  483. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  484. uec_phy_write(mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
  485. else
  486. uec_phy_write(mii_info, MII_M1011_IMASK,
  487. MII_M1011_IMASK_CLEAR);
  488. return 0;
  489. }
  490. static int dm9161_init (struct uec_mii_info *mii_info)
  491. {
  492. /* Reset the PHY */
  493. uec_phy_write(mii_info, MII_BMCR, uec_phy_read(mii_info, MII_BMCR) |
  494. BMCR_RESET);
  495. /* PHY and MAC connect */
  496. uec_phy_write(mii_info, MII_BMCR, uec_phy_read(mii_info, MII_BMCR) &
  497. ~BMCR_ISOLATE);
  498. uec_phy_write(mii_info, MII_DM9161_SCR, MII_DM9161_SCR_INIT);
  499. config_genmii_advert (mii_info);
  500. /* Start/restart aneg */
  501. genmii_config_aneg (mii_info);
  502. return 0;
  503. }
  504. static int dm9161_config_aneg (struct uec_mii_info *mii_info)
  505. {
  506. return 0;
  507. }
  508. static int dm9161_read_status (struct uec_mii_info *mii_info)
  509. {
  510. u16 status;
  511. int err;
  512. /* Update the link, but return if there was an error */
  513. err = genmii_update_link (mii_info);
  514. if (err)
  515. return err;
  516. /* If the link is up, read the speed and duplex
  517. If we aren't autonegotiating assume speeds are as set */
  518. if (mii_info->autoneg && mii_info->link) {
  519. status = uec_phy_read(mii_info, MII_DM9161_SCSR);
  520. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
  521. mii_info->speed = SPEED_100;
  522. else
  523. mii_info->speed = SPEED_10;
  524. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
  525. mii_info->duplex = DUPLEX_FULL;
  526. else
  527. mii_info->duplex = DUPLEX_HALF;
  528. }
  529. return 0;
  530. }
  531. static int dm9161_ack_interrupt (struct uec_mii_info *mii_info)
  532. {
  533. /* Clear the interrupt by reading the reg */
  534. uec_phy_read(mii_info, MII_DM9161_INTR);
  535. return 0;
  536. }
  537. static int dm9161_config_intr (struct uec_mii_info *mii_info)
  538. {
  539. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  540. uec_phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
  541. else
  542. uec_phy_write(mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
  543. return 0;
  544. }
  545. static void dm9161_close (struct uec_mii_info *mii_info)
  546. {
  547. }
  548. static int fixed_phy_aneg (struct uec_mii_info *mii_info)
  549. {
  550. mii_info->autoneg = 0; /* Turn off auto negotiation for fixed phy */
  551. return 0;
  552. }
  553. static int fixed_phy_read_status (struct uec_mii_info *mii_info)
  554. {
  555. int i = 0;
  556. for (i = 0; i < ARRAY_SIZE(fixed_phy_port); i++) {
  557. if (strncmp(mii_info->dev->name, fixed_phy_port[i].name,
  558. strlen(mii_info->dev->name)) == 0) {
  559. mii_info->speed = fixed_phy_port[i].speed;
  560. mii_info->duplex = fixed_phy_port[i].duplex;
  561. mii_info->link = 1; /* Link is always UP */
  562. mii_info->pause = 0;
  563. break;
  564. }
  565. }
  566. return 0;
  567. }
  568. static int smsc_config_aneg (struct uec_mii_info *mii_info)
  569. {
  570. return 0;
  571. }
  572. static int smsc_read_status (struct uec_mii_info *mii_info)
  573. {
  574. u16 status;
  575. int err;
  576. /* Update the link, but return if there
  577. * was an error */
  578. err = genmii_update_link (mii_info);
  579. if (err)
  580. return err;
  581. /* If the link is up, read the speed and duplex */
  582. /* If we aren't autonegotiating, assume speeds
  583. * are as set */
  584. if (mii_info->autoneg && mii_info->link) {
  585. int val;
  586. status = uec_phy_read(mii_info, 0x1f);
  587. val = (status & 0x1c) >> 2;
  588. switch (val) {
  589. case 1:
  590. mii_info->duplex = DUPLEX_HALF;
  591. mii_info->speed = SPEED_10;
  592. break;
  593. case 5:
  594. mii_info->duplex = DUPLEX_FULL;
  595. mii_info->speed = SPEED_10;
  596. break;
  597. case 2:
  598. mii_info->duplex = DUPLEX_HALF;
  599. mii_info->speed = SPEED_100;
  600. break;
  601. case 6:
  602. mii_info->duplex = DUPLEX_FULL;
  603. mii_info->speed = SPEED_100;
  604. break;
  605. }
  606. mii_info->pause = 0;
  607. }
  608. return 0;
  609. }
  610. static struct phy_info phy_info_dm9161 = {
  611. .phy_id = 0x0181b880,
  612. .phy_id_mask = 0x0ffffff0,
  613. .name = "Davicom DM9161E",
  614. .init = dm9161_init,
  615. .config_aneg = dm9161_config_aneg,
  616. .read_status = dm9161_read_status,
  617. .close = dm9161_close,
  618. };
  619. static struct phy_info phy_info_dm9161a = {
  620. .phy_id = 0x0181b8a0,
  621. .phy_id_mask = 0x0ffffff0,
  622. .name = "Davicom DM9161A",
  623. .features = MII_BASIC_FEATURES,
  624. .init = dm9161_init,
  625. .config_aneg = dm9161_config_aneg,
  626. .read_status = dm9161_read_status,
  627. .ack_interrupt = dm9161_ack_interrupt,
  628. .config_intr = dm9161_config_intr,
  629. .close = dm9161_close,
  630. };
  631. static struct phy_info phy_info_marvell = {
  632. .phy_id = 0x01410c00,
  633. .phy_id_mask = 0xffffff00,
  634. .name = "Marvell 88E11x1",
  635. .features = MII_GBIT_FEATURES,
  636. .init = &uec_marvell_init,
  637. .config_aneg = &marvell_config_aneg,
  638. .read_status = &marvell_read_status,
  639. .ack_interrupt = &marvell_ack_interrupt,
  640. .config_intr = &marvell_config_intr,
  641. };
  642. static struct phy_info phy_info_bcm5481 = {
  643. .phy_id = 0x0143bca0,
  644. .phy_id_mask = 0xffffff0,
  645. .name = "Broadcom 5481",
  646. .features = MII_GBIT_FEATURES,
  647. .read_status = genmii_read_status,
  648. .init = bcm_init,
  649. };
  650. static struct phy_info phy_info_fixedphy = {
  651. .phy_id = CONFIG_FIXED_PHY,
  652. .phy_id_mask = CONFIG_FIXED_PHY,
  653. .name = "Fixed PHY",
  654. .config_aneg = fixed_phy_aneg,
  655. .read_status = fixed_phy_read_status,
  656. };
  657. static struct phy_info phy_info_smsclan8700 = {
  658. .phy_id = 0x0007c0c0,
  659. .phy_id_mask = 0xfffffff0,
  660. .name = "SMSC LAN8700",
  661. .features = MII_BASIC_FEATURES,
  662. .config_aneg = smsc_config_aneg,
  663. .read_status = smsc_read_status,
  664. };
  665. static struct phy_info phy_info_genmii = {
  666. .phy_id = 0x00000000,
  667. .phy_id_mask = 0x00000000,
  668. .name = "Generic MII",
  669. .features = MII_BASIC_FEATURES,
  670. .config_aneg = genmii_config_aneg,
  671. .read_status = genmii_read_status,
  672. };
  673. static struct phy_info *phy_info[] = {
  674. &phy_info_dm9161,
  675. &phy_info_dm9161a,
  676. &phy_info_marvell,
  677. &phy_info_bcm5481,
  678. &phy_info_smsclan8700,
  679. &phy_info_fixedphy,
  680. &phy_info_genmii,
  681. NULL
  682. };
  683. u16 uec_phy_read(struct uec_mii_info *mii_info, u16 regnum)
  684. {
  685. return mii_info->mdio_read (mii_info->dev, mii_info->mii_id, regnum);
  686. }
  687. void uec_phy_write(struct uec_mii_info *mii_info, u16 regnum, u16 val)
  688. {
  689. mii_info->mdio_write (mii_info->dev, mii_info->mii_id, regnum, val);
  690. }
  691. /* Use the PHY ID registers to determine what type of PHY is attached
  692. * to device dev. return a struct phy_info structure describing that PHY
  693. */
  694. struct phy_info *uec_get_phy_info (struct uec_mii_info *mii_info)
  695. {
  696. u16 phy_reg;
  697. u32 phy_ID;
  698. int i;
  699. struct phy_info *theInfo = NULL;
  700. /* Grab the bits from PHYIR1, and put them in the upper half */
  701. phy_reg = uec_phy_read(mii_info, MII_PHYSID1);
  702. phy_ID = (phy_reg & 0xffff) << 16;
  703. /* Grab the bits from PHYIR2, and put them in the lower half */
  704. phy_reg = uec_phy_read(mii_info, MII_PHYSID2);
  705. phy_ID |= (phy_reg & 0xffff);
  706. /* loop through all the known PHY types, and find one that */
  707. /* matches the ID we read from the PHY. */
  708. for (i = 0; phy_info[i]; i++)
  709. if (phy_info[i]->phy_id ==
  710. (phy_ID & phy_info[i]->phy_id_mask)) {
  711. theInfo = phy_info[i];
  712. break;
  713. }
  714. /* This shouldn't happen, as we have generic PHY support */
  715. if (theInfo == NULL) {
  716. ugphy_info ("UEC: PHY id %x is not supported!", phy_ID);
  717. return NULL;
  718. } else {
  719. ugphy_info ("UEC: PHY is %s (%x)", theInfo->name, phy_ID);
  720. }
  721. return theInfo;
  722. }
  723. void marvell_phy_interface_mode (struct eth_device *dev,
  724. enum fsl_phy_enet_if type,
  725. int speed
  726. )
  727. {
  728. uec_private_t *uec = (uec_private_t *) dev->priv;
  729. struct uec_mii_info *mii_info;
  730. u16 status;
  731. if (!uec->mii_info) {
  732. printf ("%s: the PHY not initialized\n", __FUNCTION__);
  733. return;
  734. }
  735. mii_info = uec->mii_info;
  736. if (type == RGMII) {
  737. if (speed == 100) {
  738. uec_phy_write(mii_info, 0x00, 0x9140);
  739. uec_phy_write(mii_info, 0x1d, 0x001f);
  740. uec_phy_write(mii_info, 0x1e, 0x200c);
  741. uec_phy_write(mii_info, 0x1d, 0x0005);
  742. uec_phy_write(mii_info, 0x1e, 0x0000);
  743. uec_phy_write(mii_info, 0x1e, 0x0100);
  744. uec_phy_write(mii_info, 0x09, 0x0e00);
  745. uec_phy_write(mii_info, 0x04, 0x01e1);
  746. uec_phy_write(mii_info, 0x00, 0x9140);
  747. uec_phy_write(mii_info, 0x00, 0x1000);
  748. udelay (100000);
  749. uec_phy_write(mii_info, 0x00, 0x2900);
  750. uec_phy_write(mii_info, 0x14, 0x0cd2);
  751. uec_phy_write(mii_info, 0x00, 0xa100);
  752. uec_phy_write(mii_info, 0x09, 0x0000);
  753. uec_phy_write(mii_info, 0x1b, 0x800b);
  754. uec_phy_write(mii_info, 0x04, 0x05e1);
  755. uec_phy_write(mii_info, 0x00, 0xa100);
  756. uec_phy_write(mii_info, 0x00, 0x2100);
  757. udelay (1000000);
  758. } else if (speed == 10) {
  759. uec_phy_write(mii_info, 0x14, 0x8e40);
  760. uec_phy_write(mii_info, 0x1b, 0x800b);
  761. uec_phy_write(mii_info, 0x14, 0x0c82);
  762. uec_phy_write(mii_info, 0x00, 0x8100);
  763. udelay (1000000);
  764. }
  765. }
  766. /* handle 88e1111 rev.B2 erratum 5.6 */
  767. if (mii_info->autoneg) {
  768. status = uec_phy_read(mii_info, MII_BMCR);
  769. uec_phy_write(mii_info, MII_BMCR, status | BMCR_ANENABLE);
  770. }
  771. /* now the B2 will correctly report autoneg completion status */
  772. }
  773. void change_phy_interface_mode (struct eth_device *dev,
  774. enum fsl_phy_enet_if type, int speed)
  775. {
  776. #ifdef CONFIG_PHY_MODE_NEED_CHANGE
  777. marvell_phy_interface_mode (dev, type, speed);
  778. #endif
  779. }