uec_phy.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. * Copyright (C) 2005 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 phy_read (struct uec_mii_info *mii_info, u16 regnum);
  112. void 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 = phy_read (mii_info, PHY_ANAR);
  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. phy_write (mii_info, PHY_ANAR, 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 = phy_read (mii_info, PHY_BMCR);
  225. ctrl &= ~(PHY_BMCR_DPLX | PHY_BMCR_100_MBPS |
  226. PHY_BMCR_1000_MBPS | PHY_BMCR_AUTON);
  227. ctrl |= PHY_BMCR_RESET;
  228. switch (mii_info->speed) {
  229. case SPEED_1000:
  230. if (features & (SUPPORTED_1000baseT_Half
  231. | SUPPORTED_1000baseT_Full)) {
  232. ctrl |= PHY_BMCR_1000_MBPS;
  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 |= PHY_BMCR_100_MBPS;
  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. phy_write (mii_info, PHY_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 = phy_read (mii_info, PHY_BMCR);
  258. ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
  259. phy_write (mii_info, PHY_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 = phy_read (mii_info, MII_1000BASETCONTROL);
  270. adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP |
  271. MII_1000BASETCONTROL_HALFDUPLEXCAP);
  272. if (advertise & SUPPORTED_1000baseT_Half)
  273. adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
  274. if (advertise & SUPPORTED_1000baseT_Full)
  275. adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
  276. phy_write (mii_info, MII_1000BASETCONTROL, 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. phy_write (mii_info, PHY_BMCR, PHY_BMCR_RESET);
  289. phy_write (mii_info, 0x1d, 0x1f);
  290. phy_write (mii_info, 0x1e, 0x200c);
  291. phy_write (mii_info, 0x1d, 0x5);
  292. phy_write (mii_info, 0x1e, 0);
  293. 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. phy_read (mii_info, PHY_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 = phy_read(mii_info, PHY_BMSR);
  324. if ((status & PHY_BMSR_LS) && (status & PHY_BMSR_AUTN_ABLE)
  325. && !(status & PHY_BMSR_AUTN_COMP)) {
  326. int i = 0;
  327. while (!(status & PHY_BMSR_AUTN_COMP)) {
  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 = phy_read(mii_info, PHY_BMSR);
  338. }
  339. mii_info->link = 1;
  340. } else {
  341. if (status & PHY_BMSR_LS)
  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 = phy_read(mii_info, MII_1000BASETSTATUS);
  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 = phy_read(mii_info, PHY_ANLPAR);
  367. if (status & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD))
  368. mii_info->duplex = DUPLEX_FULL;
  369. else
  370. mii_info->duplex = DUPLEX_HALF;
  371. if (status & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX))
  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 = phy_read(mii_info, PHY_BMSR);
  395. while (--cnt && !(val & PHY_BMSR_AUTN_COMP));
  396. /* Set RDX clk delay. */
  397. phy_write(mii_info, 0x18, 0x7 | (7 << 12));
  398. val = 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. phy_write(mii_info, 0x18, val);
  405. }
  406. return 0;
  407. }
  408. static int 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 enet_interface_type 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 = 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. phy_write(mii_info, MII_M1111_PHY_EXT_CR, temp);
  430. temp = phy_read(mii_info, MII_M1111_PHY_EXT_SR);
  431. temp &= ~MII_M1111_HWCFG_MODE_MASK;
  432. temp |= MII_M1111_HWCFG_MODE_RGMII;
  433. phy_write(mii_info, MII_M1111_PHY_EXT_SR, temp);
  434. phy_write(mii_info, PHY_BMCR, PHY_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 = 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. 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. phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
  485. else
  486. phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
  487. return 0;
  488. }
  489. static int dm9161_init (struct uec_mii_info *mii_info)
  490. {
  491. /* Reset the PHY */
  492. phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) |
  493. PHY_BMCR_RESET);
  494. /* PHY and MAC connect */
  495. phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) &
  496. ~PHY_BMCR_ISO);
  497. phy_write (mii_info, MII_DM9161_SCR, MII_DM9161_SCR_INIT);
  498. config_genmii_advert (mii_info);
  499. /* Start/restart aneg */
  500. genmii_config_aneg (mii_info);
  501. return 0;
  502. }
  503. static int dm9161_config_aneg (struct uec_mii_info *mii_info)
  504. {
  505. return 0;
  506. }
  507. static int dm9161_read_status (struct uec_mii_info *mii_info)
  508. {
  509. u16 status;
  510. int err;
  511. /* Update the link, but return if there was an error */
  512. err = genmii_update_link (mii_info);
  513. if (err)
  514. return err;
  515. /* If the link is up, read the speed and duplex
  516. If we aren't autonegotiating assume speeds are as set */
  517. if (mii_info->autoneg && mii_info->link) {
  518. status = phy_read (mii_info, MII_DM9161_SCSR);
  519. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
  520. mii_info->speed = SPEED_100;
  521. else
  522. mii_info->speed = SPEED_10;
  523. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
  524. mii_info->duplex = DUPLEX_FULL;
  525. else
  526. mii_info->duplex = DUPLEX_HALF;
  527. }
  528. return 0;
  529. }
  530. static int dm9161_ack_interrupt (struct uec_mii_info *mii_info)
  531. {
  532. /* Clear the interrupt by reading the reg */
  533. phy_read (mii_info, MII_DM9161_INTR);
  534. return 0;
  535. }
  536. static int dm9161_config_intr (struct uec_mii_info *mii_info)
  537. {
  538. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  539. phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
  540. else
  541. phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
  542. return 0;
  543. }
  544. static void dm9161_close (struct uec_mii_info *mii_info)
  545. {
  546. }
  547. static int fixed_phy_aneg (struct uec_mii_info *mii_info)
  548. {
  549. mii_info->autoneg = 0; /* Turn off auto negotiation for fixed phy */
  550. return 0;
  551. }
  552. static int fixed_phy_read_status (struct uec_mii_info *mii_info)
  553. {
  554. int i = 0;
  555. for (i = 0; i < ARRAY_SIZE(fixed_phy_port); i++) {
  556. if (strncmp(mii_info->dev->name, fixed_phy_port[i].name,
  557. strlen(mii_info->dev->name)) == 0) {
  558. mii_info->speed = fixed_phy_port[i].speed;
  559. mii_info->duplex = fixed_phy_port[i].duplex;
  560. mii_info->link = 1; /* Link is always UP */
  561. mii_info->pause = 0;
  562. break;
  563. }
  564. }
  565. return 0;
  566. }
  567. static int smsc_config_aneg (struct uec_mii_info *mii_info)
  568. {
  569. return 0;
  570. }
  571. static int smsc_read_status (struct uec_mii_info *mii_info)
  572. {
  573. u16 status;
  574. int err;
  575. /* Update the link, but return if there
  576. * was an error */
  577. err = genmii_update_link (mii_info);
  578. if (err)
  579. return err;
  580. /* If the link is up, read the speed and duplex */
  581. /* If we aren't autonegotiating, assume speeds
  582. * are as set */
  583. if (mii_info->autoneg && mii_info->link) {
  584. int val;
  585. status = phy_read (mii_info, 0x1f);
  586. val = (status & 0x1c) >> 2;
  587. switch (val) {
  588. case 1:
  589. mii_info->duplex = DUPLEX_HALF;
  590. mii_info->speed = SPEED_10;
  591. break;
  592. case 5:
  593. mii_info->duplex = DUPLEX_FULL;
  594. mii_info->speed = SPEED_10;
  595. break;
  596. case 2:
  597. mii_info->duplex = DUPLEX_HALF;
  598. mii_info->speed = SPEED_100;
  599. break;
  600. case 6:
  601. mii_info->duplex = DUPLEX_FULL;
  602. mii_info->speed = SPEED_100;
  603. break;
  604. }
  605. mii_info->pause = 0;
  606. }
  607. return 0;
  608. }
  609. static struct phy_info phy_info_dm9161 = {
  610. .phy_id = 0x0181b880,
  611. .phy_id_mask = 0x0ffffff0,
  612. .name = "Davicom DM9161E",
  613. .init = dm9161_init,
  614. .config_aneg = dm9161_config_aneg,
  615. .read_status = dm9161_read_status,
  616. .close = dm9161_close,
  617. };
  618. static struct phy_info phy_info_dm9161a = {
  619. .phy_id = 0x0181b8a0,
  620. .phy_id_mask = 0x0ffffff0,
  621. .name = "Davicom DM9161A",
  622. .features = MII_BASIC_FEATURES,
  623. .init = dm9161_init,
  624. .config_aneg = dm9161_config_aneg,
  625. .read_status = dm9161_read_status,
  626. .ack_interrupt = dm9161_ack_interrupt,
  627. .config_intr = dm9161_config_intr,
  628. .close = dm9161_close,
  629. };
  630. static struct phy_info phy_info_marvell = {
  631. .phy_id = 0x01410c00,
  632. .phy_id_mask = 0xffffff00,
  633. .name = "Marvell 88E11x1",
  634. .features = MII_GBIT_FEATURES,
  635. .init = &marvell_init,
  636. .config_aneg = &marvell_config_aneg,
  637. .read_status = &marvell_read_status,
  638. .ack_interrupt = &marvell_ack_interrupt,
  639. .config_intr = &marvell_config_intr,
  640. };
  641. static struct phy_info phy_info_bcm5481 = {
  642. .phy_id = 0x0143bca0,
  643. .phy_id_mask = 0xffffff0,
  644. .name = "Broadcom 5481",
  645. .features = MII_GBIT_FEATURES,
  646. .read_status = genmii_read_status,
  647. .init = bcm_init,
  648. };
  649. static struct phy_info phy_info_fixedphy = {
  650. .phy_id = CONFIG_FIXED_PHY,
  651. .phy_id_mask = CONFIG_FIXED_PHY,
  652. .name = "Fixed PHY",
  653. .config_aneg = fixed_phy_aneg,
  654. .read_status = fixed_phy_read_status,
  655. };
  656. static struct phy_info phy_info_smsclan8700 = {
  657. .phy_id = 0x0007c0c0,
  658. .phy_id_mask = 0xfffffff0,
  659. .name = "SMSC LAN8700",
  660. .features = MII_BASIC_FEATURES,
  661. .config_aneg = smsc_config_aneg,
  662. .read_status = smsc_read_status,
  663. };
  664. static struct phy_info phy_info_genmii = {
  665. .phy_id = 0x00000000,
  666. .phy_id_mask = 0x00000000,
  667. .name = "Generic MII",
  668. .features = MII_BASIC_FEATURES,
  669. .config_aneg = genmii_config_aneg,
  670. .read_status = genmii_read_status,
  671. };
  672. static struct phy_info *phy_info[] = {
  673. &phy_info_dm9161,
  674. &phy_info_dm9161a,
  675. &phy_info_marvell,
  676. &phy_info_bcm5481,
  677. &phy_info_smsclan8700,
  678. &phy_info_fixedphy,
  679. &phy_info_genmii,
  680. NULL
  681. };
  682. u16 phy_read (struct uec_mii_info *mii_info, u16 regnum)
  683. {
  684. return mii_info->mdio_read (mii_info->dev, mii_info->mii_id, regnum);
  685. }
  686. void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val)
  687. {
  688. mii_info->mdio_write (mii_info->dev, mii_info->mii_id, regnum, val);
  689. }
  690. /* Use the PHY ID registers to determine what type of PHY is attached
  691. * to device dev. return a struct phy_info structure describing that PHY
  692. */
  693. struct phy_info *uec_get_phy_info (struct uec_mii_info *mii_info)
  694. {
  695. u16 phy_reg;
  696. u32 phy_ID;
  697. int i;
  698. struct phy_info *theInfo = NULL;
  699. /* Grab the bits from PHYIR1, and put them in the upper half */
  700. phy_reg = phy_read (mii_info, PHY_PHYIDR1);
  701. phy_ID = (phy_reg & 0xffff) << 16;
  702. /* Grab the bits from PHYIR2, and put them in the lower half */
  703. phy_reg = phy_read (mii_info, PHY_PHYIDR2);
  704. phy_ID |= (phy_reg & 0xffff);
  705. /* loop through all the known PHY types, and find one that */
  706. /* matches the ID we read from the PHY. */
  707. for (i = 0; phy_info[i]; i++)
  708. if (phy_info[i]->phy_id ==
  709. (phy_ID & phy_info[i]->phy_id_mask)) {
  710. theInfo = phy_info[i];
  711. break;
  712. }
  713. /* This shouldn't happen, as we have generic PHY support */
  714. if (theInfo == NULL) {
  715. ugphy_info ("UEC: PHY id %x is not supported!", phy_ID);
  716. return NULL;
  717. } else {
  718. ugphy_info ("UEC: PHY is %s (%x)", theInfo->name, phy_ID);
  719. }
  720. return theInfo;
  721. }
  722. void marvell_phy_interface_mode (struct eth_device *dev,
  723. enet_interface_type_e type,
  724. int speed
  725. )
  726. {
  727. uec_private_t *uec = (uec_private_t *) dev->priv;
  728. struct uec_mii_info *mii_info;
  729. u16 status;
  730. if (!uec->mii_info) {
  731. printf ("%s: the PHY not initialized\n", __FUNCTION__);
  732. return;
  733. }
  734. mii_info = uec->mii_info;
  735. if (type == RGMII) {
  736. if (speed == 100) {
  737. phy_write (mii_info, 0x00, 0x9140);
  738. phy_write (mii_info, 0x1d, 0x001f);
  739. phy_write (mii_info, 0x1e, 0x200c);
  740. phy_write (mii_info, 0x1d, 0x0005);
  741. phy_write (mii_info, 0x1e, 0x0000);
  742. phy_write (mii_info, 0x1e, 0x0100);
  743. phy_write (mii_info, 0x09, 0x0e00);
  744. phy_write (mii_info, 0x04, 0x01e1);
  745. phy_write (mii_info, 0x00, 0x9140);
  746. phy_write (mii_info, 0x00, 0x1000);
  747. udelay (100000);
  748. phy_write (mii_info, 0x00, 0x2900);
  749. phy_write (mii_info, 0x14, 0x0cd2);
  750. phy_write (mii_info, 0x00, 0xa100);
  751. phy_write (mii_info, 0x09, 0x0000);
  752. phy_write (mii_info, 0x1b, 0x800b);
  753. phy_write (mii_info, 0x04, 0x05e1);
  754. phy_write (mii_info, 0x00, 0xa100);
  755. phy_write (mii_info, 0x00, 0x2100);
  756. udelay (1000000);
  757. } else if (speed == 10) {
  758. phy_write (mii_info, 0x14, 0x8e40);
  759. phy_write (mii_info, 0x1b, 0x800b);
  760. phy_write (mii_info, 0x14, 0x0c82);
  761. phy_write (mii_info, 0x00, 0x8100);
  762. udelay (1000000);
  763. }
  764. }
  765. /* handle 88e1111 rev.B2 erratum 5.6 */
  766. if (mii_info->autoneg) {
  767. status = phy_read (mii_info, PHY_BMCR);
  768. phy_write (mii_info, PHY_BMCR, status | PHY_BMCR_AUTON);
  769. }
  770. /* now the B2 will correctly report autoneg completion status */
  771. }
  772. void change_phy_interface_mode (struct eth_device *dev,
  773. enet_interface_type_e type, int speed)
  774. {
  775. #ifdef CONFIG_PHY_MODE_NEED_CHANGE
  776. marvell_phy_interface_mode (dev, type, speed);
  777. #endif
  778. }