uec_phy.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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 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("FSL UEC0",SPEED_100,DUPLEX_FULL) \
  69. * CONFIG_SYS_FIXED_PHY_PORT("FSL 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. static void config_genmii_advert (struct uec_mii_info *mii_info);
  86. static void genmii_setup_forced (struct uec_mii_info *mii_info);
  87. static void genmii_restart_aneg (struct uec_mii_info *mii_info);
  88. static int gbit_config_aneg (struct uec_mii_info *mii_info);
  89. static int genmii_config_aneg (struct uec_mii_info *mii_info);
  90. static int genmii_update_link (struct uec_mii_info *mii_info);
  91. static int genmii_read_status (struct uec_mii_info *mii_info);
  92. u16 phy_read (struct uec_mii_info *mii_info, u16 regnum);
  93. void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val);
  94. /* Write value to the PHY for this device to the register at regnum, */
  95. /* waiting until the write is done before it returns. All PHY */
  96. /* configuration has to be done through the TSEC1 MIIM regs */
  97. void uec_write_phy_reg (struct eth_device *dev, int mii_id, int regnum, int value)
  98. {
  99. uec_private_t *ugeth = (uec_private_t *) dev->priv;
  100. uec_mii_t *ug_regs;
  101. enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
  102. u32 tmp_reg;
  103. ug_regs = ugeth->uec_mii_regs;
  104. /* Stop the MII management read cycle */
  105. out_be32 (&ug_regs->miimcom, 0);
  106. /* Setting up the MII Mangement Address Register */
  107. tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
  108. out_be32 (&ug_regs->miimadd, tmp_reg);
  109. /* Setting up the MII Mangement Control Register with the value */
  110. out_be32 (&ug_regs->miimcon, (u32) value);
  111. sync();
  112. /* Wait till MII management write is complete */
  113. while ((in_be32 (&ug_regs->miimind)) & MIIMIND_BUSY);
  114. }
  115. /* Reads from register regnum in the PHY for device dev, */
  116. /* returning the value. Clears miimcom first. All PHY */
  117. /* configuration has to be done through the TSEC1 MIIM regs */
  118. int uec_read_phy_reg (struct eth_device *dev, int mii_id, int regnum)
  119. {
  120. uec_private_t *ugeth = (uec_private_t *) dev->priv;
  121. uec_mii_t *ug_regs;
  122. enet_tbi_mii_reg_e mii_reg = (enet_tbi_mii_reg_e) regnum;
  123. u32 tmp_reg;
  124. u16 value;
  125. ug_regs = ugeth->uec_mii_regs;
  126. /* Setting up the MII Mangement Address Register */
  127. tmp_reg = ((u32) mii_id << MIIMADD_PHY_ADDRESS_SHIFT) | mii_reg;
  128. out_be32 (&ug_regs->miimadd, tmp_reg);
  129. /* clear MII management command cycle */
  130. out_be32 (&ug_regs->miimcom, 0);
  131. sync();
  132. /* Perform an MII management read cycle */
  133. out_be32 (&ug_regs->miimcom, MIIMCOM_READ_CYCLE);
  134. /* Wait till MII management write is complete */
  135. while ((in_be32 (&ug_regs->miimind)) &
  136. (MIIMIND_NOT_VALID | MIIMIND_BUSY));
  137. /* Read MII management status */
  138. value = (u16) in_be32 (&ug_regs->miimstat);
  139. if (value == 0xffff)
  140. ugphy_vdbg
  141. ("read wrong value : mii_id %d,mii_reg %d, base %08x",
  142. mii_id, mii_reg, (u32) & (ug_regs->miimcfg));
  143. return (value);
  144. }
  145. void mii_clear_phy_interrupt (struct uec_mii_info *mii_info)
  146. {
  147. if (mii_info->phyinfo->ack_interrupt)
  148. mii_info->phyinfo->ack_interrupt (mii_info);
  149. }
  150. void mii_configure_phy_interrupt (struct uec_mii_info *mii_info,
  151. u32 interrupts)
  152. {
  153. mii_info->interrupts = interrupts;
  154. if (mii_info->phyinfo->config_intr)
  155. mii_info->phyinfo->config_intr (mii_info);
  156. }
  157. /* Writes MII_ADVERTISE with the appropriate values, after
  158. * sanitizing advertise to make sure only supported features
  159. * are advertised
  160. */
  161. static void config_genmii_advert (struct uec_mii_info *mii_info)
  162. {
  163. u32 advertise;
  164. u16 adv;
  165. /* Only allow advertising what this PHY supports */
  166. mii_info->advertising &= mii_info->phyinfo->features;
  167. advertise = mii_info->advertising;
  168. /* Setup standard advertisement */
  169. adv = phy_read (mii_info, PHY_ANAR);
  170. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
  171. if (advertise & ADVERTISED_10baseT_Half)
  172. adv |= ADVERTISE_10HALF;
  173. if (advertise & ADVERTISED_10baseT_Full)
  174. adv |= ADVERTISE_10FULL;
  175. if (advertise & ADVERTISED_100baseT_Half)
  176. adv |= ADVERTISE_100HALF;
  177. if (advertise & ADVERTISED_100baseT_Full)
  178. adv |= ADVERTISE_100FULL;
  179. phy_write (mii_info, PHY_ANAR, adv);
  180. }
  181. static void genmii_setup_forced (struct uec_mii_info *mii_info)
  182. {
  183. u16 ctrl;
  184. u32 features = mii_info->phyinfo->features;
  185. ctrl = phy_read (mii_info, PHY_BMCR);
  186. ctrl &= ~(PHY_BMCR_DPLX | PHY_BMCR_100_MBPS |
  187. PHY_BMCR_1000_MBPS | PHY_BMCR_AUTON);
  188. ctrl |= PHY_BMCR_RESET;
  189. switch (mii_info->speed) {
  190. case SPEED_1000:
  191. if (features & (SUPPORTED_1000baseT_Half
  192. | SUPPORTED_1000baseT_Full)) {
  193. ctrl |= PHY_BMCR_1000_MBPS;
  194. break;
  195. }
  196. mii_info->speed = SPEED_100;
  197. case SPEED_100:
  198. if (features & (SUPPORTED_100baseT_Half
  199. | SUPPORTED_100baseT_Full)) {
  200. ctrl |= PHY_BMCR_100_MBPS;
  201. break;
  202. }
  203. mii_info->speed = SPEED_10;
  204. case SPEED_10:
  205. if (features & (SUPPORTED_10baseT_Half
  206. | SUPPORTED_10baseT_Full))
  207. break;
  208. default: /* Unsupported speed! */
  209. ugphy_err ("%s: Bad speed!", mii_info->dev->name);
  210. break;
  211. }
  212. phy_write (mii_info, PHY_BMCR, ctrl);
  213. }
  214. /* Enable and Restart Autonegotiation */
  215. static void genmii_restart_aneg (struct uec_mii_info *mii_info)
  216. {
  217. u16 ctl;
  218. ctl = phy_read (mii_info, PHY_BMCR);
  219. ctl |= (PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
  220. phy_write (mii_info, PHY_BMCR, ctl);
  221. }
  222. static int gbit_config_aneg (struct uec_mii_info *mii_info)
  223. {
  224. u16 adv;
  225. u32 advertise;
  226. if (mii_info->autoneg) {
  227. /* Configure the ADVERTISE register */
  228. config_genmii_advert (mii_info);
  229. advertise = mii_info->advertising;
  230. adv = phy_read (mii_info, MII_1000BASETCONTROL);
  231. adv &= ~(MII_1000BASETCONTROL_FULLDUPLEXCAP |
  232. MII_1000BASETCONTROL_HALFDUPLEXCAP);
  233. if (advertise & SUPPORTED_1000baseT_Half)
  234. adv |= MII_1000BASETCONTROL_HALFDUPLEXCAP;
  235. if (advertise & SUPPORTED_1000baseT_Full)
  236. adv |= MII_1000BASETCONTROL_FULLDUPLEXCAP;
  237. phy_write (mii_info, MII_1000BASETCONTROL, adv);
  238. /* Start/Restart aneg */
  239. genmii_restart_aneg (mii_info);
  240. } else
  241. genmii_setup_forced (mii_info);
  242. return 0;
  243. }
  244. static int marvell_config_aneg (struct uec_mii_info *mii_info)
  245. {
  246. /* The Marvell PHY has an errata which requires
  247. * that certain registers get written in order
  248. * to restart autonegotiation */
  249. phy_write (mii_info, PHY_BMCR, PHY_BMCR_RESET);
  250. phy_write (mii_info, 0x1d, 0x1f);
  251. phy_write (mii_info, 0x1e, 0x200c);
  252. phy_write (mii_info, 0x1d, 0x5);
  253. phy_write (mii_info, 0x1e, 0);
  254. phy_write (mii_info, 0x1e, 0x100);
  255. gbit_config_aneg (mii_info);
  256. return 0;
  257. }
  258. static int genmii_config_aneg (struct uec_mii_info *mii_info)
  259. {
  260. if (mii_info->autoneg) {
  261. config_genmii_advert (mii_info);
  262. genmii_restart_aneg (mii_info);
  263. } else
  264. genmii_setup_forced (mii_info);
  265. return 0;
  266. }
  267. static int genmii_update_link (struct uec_mii_info *mii_info)
  268. {
  269. u16 status;
  270. /* Status is read once to clear old link state */
  271. phy_read (mii_info, PHY_BMSR);
  272. /*
  273. * Wait if the link is up, and autonegotiation is in progress
  274. * (ie - we're capable and it's not done)
  275. */
  276. status = phy_read(mii_info, PHY_BMSR);
  277. if ((status & PHY_BMSR_LS) && (status & PHY_BMSR_AUTN_ABLE)
  278. && !(status & PHY_BMSR_AUTN_COMP)) {
  279. int i = 0;
  280. while (!(status & PHY_BMSR_AUTN_COMP)) {
  281. /*
  282. * Timeout reached ?
  283. */
  284. if (i > UGETH_AN_TIMEOUT) {
  285. mii_info->link = 0;
  286. return 0;
  287. }
  288. i++;
  289. udelay(1000); /* 1 ms */
  290. status = phy_read(mii_info, PHY_BMSR);
  291. }
  292. mii_info->link = 1;
  293. udelay(500000); /* another 500 ms (results in faster booting) */
  294. } else {
  295. if (status & PHY_BMSR_LS)
  296. mii_info->link = 1;
  297. else
  298. mii_info->link = 0;
  299. }
  300. return 0;
  301. }
  302. static int genmii_read_status (struct uec_mii_info *mii_info)
  303. {
  304. u16 status;
  305. int err;
  306. /* Update the link, but return if there
  307. * was an error */
  308. err = genmii_update_link (mii_info);
  309. if (err)
  310. return err;
  311. if (mii_info->autoneg) {
  312. status = phy_read(mii_info, MII_1000BASETSTATUS);
  313. if (status & (LPA_1000FULL | LPA_1000HALF)) {
  314. mii_info->speed = SPEED_1000;
  315. if (status & LPA_1000FULL)
  316. mii_info->duplex = DUPLEX_FULL;
  317. else
  318. mii_info->duplex = DUPLEX_HALF;
  319. } else {
  320. status = phy_read(mii_info, PHY_ANLPAR);
  321. if (status & (PHY_ANLPAR_10FD | PHY_ANLPAR_TXFD))
  322. mii_info->duplex = DUPLEX_FULL;
  323. else
  324. mii_info->duplex = DUPLEX_HALF;
  325. if (status & (PHY_ANLPAR_TXFD | PHY_ANLPAR_TX))
  326. mii_info->speed = SPEED_100;
  327. else
  328. mii_info->speed = SPEED_10;
  329. }
  330. mii_info->pause = 0;
  331. }
  332. /* On non-aneg, we assume what we put in BMCR is the speed,
  333. * though magic-aneg shouldn't prevent this case from occurring
  334. */
  335. return 0;
  336. }
  337. static int bcm_init(struct uec_mii_info *mii_info)
  338. {
  339. struct eth_device *edev = mii_info->dev;
  340. uec_private_t *uec = edev->priv;
  341. gbit_config_aneg(mii_info);
  342. if ((uec->uec_info->enet_interface_type == RGMII_RXID) &&
  343. (uec->uec_info->speed == 1000)) {
  344. u16 val;
  345. int cnt = 50;
  346. /* Wait for aneg to complete. */
  347. do
  348. val = phy_read(mii_info, PHY_BMSR);
  349. while (--cnt && !(val & PHY_BMSR_AUTN_COMP));
  350. /* Set RDX clk delay. */
  351. phy_write(mii_info, 0x18, 0x7 | (7 << 12));
  352. val = phy_read(mii_info, 0x18);
  353. /* Set RDX-RXC skew. */
  354. val |= (1 << 8);
  355. val |= (7 | (7 << 12));
  356. /* Write bits 14:0. */
  357. val |= (1 << 15);
  358. phy_write(mii_info, 0x18, val);
  359. }
  360. return 0;
  361. }
  362. static int marvell_init(struct uec_mii_info *mii_info)
  363. {
  364. struct eth_device *edev = mii_info->dev;
  365. uec_private_t *uec = edev->priv;
  366. enum enet_interface_type iface = uec->uec_info->enet_interface_type;
  367. int speed = uec->uec_info->speed;
  368. if ((speed == 1000) &&
  369. (iface == RGMII_ID ||
  370. iface == RGMII_RXID ||
  371. iface == RGMII_TXID)) {
  372. int temp;
  373. temp = phy_read(mii_info, MII_M1111_PHY_EXT_CR);
  374. if (iface == RGMII_ID) {
  375. temp |= MII_M1111_RX_DELAY | MII_M1111_TX_DELAY;
  376. } else if (iface == RGMII_RXID) {
  377. temp &= ~MII_M1111_TX_DELAY;
  378. temp |= MII_M1111_RX_DELAY;
  379. } else if (iface == RGMII_TXID) {
  380. temp &= ~MII_M1111_RX_DELAY;
  381. temp |= MII_M1111_TX_DELAY;
  382. }
  383. phy_write(mii_info, MII_M1111_PHY_EXT_CR, temp);
  384. temp = phy_read(mii_info, MII_M1111_PHY_EXT_SR);
  385. temp &= ~MII_M1111_HWCFG_MODE_MASK;
  386. temp |= MII_M1111_HWCFG_MODE_RGMII;
  387. phy_write(mii_info, MII_M1111_PHY_EXT_SR, temp);
  388. phy_write(mii_info, PHY_BMCR, PHY_BMCR_RESET);
  389. }
  390. return 0;
  391. }
  392. static int marvell_read_status (struct uec_mii_info *mii_info)
  393. {
  394. u16 status;
  395. int err;
  396. /* Update the link, but return if there
  397. * was an error */
  398. err = genmii_update_link (mii_info);
  399. if (err)
  400. return err;
  401. /* If the link is up, read the speed and duplex */
  402. /* If we aren't autonegotiating, assume speeds
  403. * are as set */
  404. if (mii_info->autoneg && mii_info->link) {
  405. int speed;
  406. status = phy_read (mii_info, MII_M1011_PHY_SPEC_STATUS);
  407. /* Get the duplexity */
  408. if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
  409. mii_info->duplex = DUPLEX_FULL;
  410. else
  411. mii_info->duplex = DUPLEX_HALF;
  412. /* Get the speed */
  413. speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
  414. switch (speed) {
  415. case MII_M1011_PHY_SPEC_STATUS_1000:
  416. mii_info->speed = SPEED_1000;
  417. break;
  418. case MII_M1011_PHY_SPEC_STATUS_100:
  419. mii_info->speed = SPEED_100;
  420. break;
  421. default:
  422. mii_info->speed = SPEED_10;
  423. break;
  424. }
  425. mii_info->pause = 0;
  426. }
  427. return 0;
  428. }
  429. static int marvell_ack_interrupt (struct uec_mii_info *mii_info)
  430. {
  431. /* Clear the interrupts by reading the reg */
  432. phy_read (mii_info, MII_M1011_IEVENT);
  433. return 0;
  434. }
  435. static int marvell_config_intr (struct uec_mii_info *mii_info)
  436. {
  437. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  438. phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
  439. else
  440. phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
  441. return 0;
  442. }
  443. static int dm9161_init (struct uec_mii_info *mii_info)
  444. {
  445. /* Reset the PHY */
  446. phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) |
  447. PHY_BMCR_RESET);
  448. /* PHY and MAC connect */
  449. phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) &
  450. ~PHY_BMCR_ISO);
  451. phy_write (mii_info, MII_DM9161_SCR, MII_DM9161_SCR_INIT);
  452. config_genmii_advert (mii_info);
  453. /* Start/restart aneg */
  454. genmii_config_aneg (mii_info);
  455. return 0;
  456. }
  457. static int dm9161_config_aneg (struct uec_mii_info *mii_info)
  458. {
  459. return 0;
  460. }
  461. static int dm9161_read_status (struct uec_mii_info *mii_info)
  462. {
  463. u16 status;
  464. int err;
  465. /* Update the link, but return if there was an error */
  466. err = genmii_update_link (mii_info);
  467. if (err)
  468. return err;
  469. /* If the link is up, read the speed and duplex
  470. If we aren't autonegotiating assume speeds are as set */
  471. if (mii_info->autoneg && mii_info->link) {
  472. status = phy_read (mii_info, MII_DM9161_SCSR);
  473. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
  474. mii_info->speed = SPEED_100;
  475. else
  476. mii_info->speed = SPEED_10;
  477. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
  478. mii_info->duplex = DUPLEX_FULL;
  479. else
  480. mii_info->duplex = DUPLEX_HALF;
  481. }
  482. return 0;
  483. }
  484. static int dm9161_ack_interrupt (struct uec_mii_info *mii_info)
  485. {
  486. /* Clear the interrupt by reading the reg */
  487. phy_read (mii_info, MII_DM9161_INTR);
  488. return 0;
  489. }
  490. static int dm9161_config_intr (struct uec_mii_info *mii_info)
  491. {
  492. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  493. phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
  494. else
  495. phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
  496. return 0;
  497. }
  498. static void dm9161_close (struct uec_mii_info *mii_info)
  499. {
  500. }
  501. static int fixed_phy_aneg (struct uec_mii_info *mii_info)
  502. {
  503. mii_info->autoneg = 0; /* Turn off auto negotiation for fixed phy */
  504. return 0;
  505. }
  506. static int fixed_phy_read_status (struct uec_mii_info *mii_info)
  507. {
  508. int i = 0;
  509. for (i = 0; i < ARRAY_SIZE(fixed_phy_port); i++) {
  510. if (strncmp(mii_info->dev->name, fixed_phy_port[i].name,
  511. strlen(mii_info->dev->name)) == 0) {
  512. mii_info->speed = fixed_phy_port[i].speed;
  513. mii_info->duplex = fixed_phy_port[i].duplex;
  514. mii_info->link = 1; /* Link is always UP */
  515. mii_info->pause = 0;
  516. break;
  517. }
  518. }
  519. return 0;
  520. }
  521. static int smsc_config_aneg (struct uec_mii_info *mii_info)
  522. {
  523. return 0;
  524. }
  525. static int smsc_read_status (struct uec_mii_info *mii_info)
  526. {
  527. u16 status;
  528. int err;
  529. /* Update the link, but return if there
  530. * was an error */
  531. err = genmii_update_link (mii_info);
  532. if (err)
  533. return err;
  534. /* If the link is up, read the speed and duplex */
  535. /* If we aren't autonegotiating, assume speeds
  536. * are as set */
  537. if (mii_info->autoneg && mii_info->link) {
  538. int val;
  539. status = phy_read (mii_info, 0x1f);
  540. val = (status & 0x1c) >> 2;
  541. switch (val) {
  542. case 1:
  543. mii_info->duplex = DUPLEX_HALF;
  544. mii_info->speed = SPEED_10;
  545. break;
  546. case 5:
  547. mii_info->duplex = DUPLEX_FULL;
  548. mii_info->speed = SPEED_10;
  549. break;
  550. case 2:
  551. mii_info->duplex = DUPLEX_HALF;
  552. mii_info->speed = SPEED_100;
  553. break;
  554. case 6:
  555. mii_info->duplex = DUPLEX_FULL;
  556. mii_info->speed = SPEED_100;
  557. break;
  558. }
  559. mii_info->pause = 0;
  560. }
  561. return 0;
  562. }
  563. static struct phy_info phy_info_dm9161 = {
  564. .phy_id = 0x0181b880,
  565. .phy_id_mask = 0x0ffffff0,
  566. .name = "Davicom DM9161E",
  567. .init = dm9161_init,
  568. .config_aneg = dm9161_config_aneg,
  569. .read_status = dm9161_read_status,
  570. .close = dm9161_close,
  571. };
  572. static struct phy_info phy_info_dm9161a = {
  573. .phy_id = 0x0181b8a0,
  574. .phy_id_mask = 0x0ffffff0,
  575. .name = "Davicom DM9161A",
  576. .features = MII_BASIC_FEATURES,
  577. .init = dm9161_init,
  578. .config_aneg = dm9161_config_aneg,
  579. .read_status = dm9161_read_status,
  580. .ack_interrupt = dm9161_ack_interrupt,
  581. .config_intr = dm9161_config_intr,
  582. .close = dm9161_close,
  583. };
  584. static struct phy_info phy_info_marvell = {
  585. .phy_id = 0x01410c00,
  586. .phy_id_mask = 0xffffff00,
  587. .name = "Marvell 88E11x1",
  588. .features = MII_GBIT_FEATURES,
  589. .init = &marvell_init,
  590. .config_aneg = &marvell_config_aneg,
  591. .read_status = &marvell_read_status,
  592. .ack_interrupt = &marvell_ack_interrupt,
  593. .config_intr = &marvell_config_intr,
  594. };
  595. static struct phy_info phy_info_bcm5481 = {
  596. .phy_id = 0x0143bca0,
  597. .phy_id_mask = 0xffffff0,
  598. .name = "Broadcom 5481",
  599. .features = MII_GBIT_FEATURES,
  600. .read_status = genmii_read_status,
  601. .init = bcm_init,
  602. };
  603. static struct phy_info phy_info_fixedphy = {
  604. .phy_id = CONFIG_FIXED_PHY,
  605. .phy_id_mask = CONFIG_FIXED_PHY,
  606. .name = "Fixed PHY",
  607. .config_aneg = fixed_phy_aneg,
  608. .read_status = fixed_phy_read_status,
  609. };
  610. static struct phy_info phy_info_smsclan8700 = {
  611. .phy_id = 0x0007c0c0,
  612. .phy_id_mask = 0xfffffff0,
  613. .name = "SMSC LAN8700",
  614. .features = MII_BASIC_FEATURES,
  615. .config_aneg = smsc_config_aneg,
  616. .read_status = smsc_read_status,
  617. };
  618. static struct phy_info phy_info_genmii = {
  619. .phy_id = 0x00000000,
  620. .phy_id_mask = 0x00000000,
  621. .name = "Generic MII",
  622. .features = MII_BASIC_FEATURES,
  623. .config_aneg = genmii_config_aneg,
  624. .read_status = genmii_read_status,
  625. };
  626. static struct phy_info *phy_info[] = {
  627. &phy_info_dm9161,
  628. &phy_info_dm9161a,
  629. &phy_info_marvell,
  630. &phy_info_bcm5481,
  631. &phy_info_smsclan8700,
  632. &phy_info_fixedphy,
  633. &phy_info_genmii,
  634. NULL
  635. };
  636. u16 phy_read (struct uec_mii_info *mii_info, u16 regnum)
  637. {
  638. return mii_info->mdio_read (mii_info->dev, mii_info->mii_id, regnum);
  639. }
  640. void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val)
  641. {
  642. mii_info->mdio_write (mii_info->dev, mii_info->mii_id, regnum, val);
  643. }
  644. /* Use the PHY ID registers to determine what type of PHY is attached
  645. * to device dev. return a struct phy_info structure describing that PHY
  646. */
  647. struct phy_info *uec_get_phy_info (struct uec_mii_info *mii_info)
  648. {
  649. u16 phy_reg;
  650. u32 phy_ID;
  651. int i;
  652. struct phy_info *theInfo = NULL;
  653. /* Grab the bits from PHYIR1, and put them in the upper half */
  654. phy_reg = phy_read (mii_info, PHY_PHYIDR1);
  655. phy_ID = (phy_reg & 0xffff) << 16;
  656. /* Grab the bits from PHYIR2, and put them in the lower half */
  657. phy_reg = phy_read (mii_info, PHY_PHYIDR2);
  658. phy_ID |= (phy_reg & 0xffff);
  659. /* loop through all the known PHY types, and find one that */
  660. /* matches the ID we read from the PHY. */
  661. for (i = 0; phy_info[i]; i++)
  662. if (phy_info[i]->phy_id ==
  663. (phy_ID & phy_info[i]->phy_id_mask)) {
  664. theInfo = phy_info[i];
  665. break;
  666. }
  667. /* This shouldn't happen, as we have generic PHY support */
  668. if (theInfo == NULL) {
  669. ugphy_info ("UEC: PHY id %x is not supported!", phy_ID);
  670. return NULL;
  671. } else {
  672. ugphy_info ("UEC: PHY is %s (%x)", theInfo->name, phy_ID);
  673. }
  674. return theInfo;
  675. }
  676. void marvell_phy_interface_mode (struct eth_device *dev,
  677. enet_interface_type_e type,
  678. int speed
  679. )
  680. {
  681. uec_private_t *uec = (uec_private_t *) dev->priv;
  682. struct uec_mii_info *mii_info;
  683. u16 status;
  684. if (!uec->mii_info) {
  685. printf ("%s: the PHY not initialized\n", __FUNCTION__);
  686. return;
  687. }
  688. mii_info = uec->mii_info;
  689. if (type == RGMII) {
  690. if (speed == 100) {
  691. phy_write (mii_info, 0x00, 0x9140);
  692. phy_write (mii_info, 0x1d, 0x001f);
  693. phy_write (mii_info, 0x1e, 0x200c);
  694. phy_write (mii_info, 0x1d, 0x0005);
  695. phy_write (mii_info, 0x1e, 0x0000);
  696. phy_write (mii_info, 0x1e, 0x0100);
  697. phy_write (mii_info, 0x09, 0x0e00);
  698. phy_write (mii_info, 0x04, 0x01e1);
  699. phy_write (mii_info, 0x00, 0x9140);
  700. phy_write (mii_info, 0x00, 0x1000);
  701. udelay (100000);
  702. phy_write (mii_info, 0x00, 0x2900);
  703. phy_write (mii_info, 0x14, 0x0cd2);
  704. phy_write (mii_info, 0x00, 0xa100);
  705. phy_write (mii_info, 0x09, 0x0000);
  706. phy_write (mii_info, 0x1b, 0x800b);
  707. phy_write (mii_info, 0x04, 0x05e1);
  708. phy_write (mii_info, 0x00, 0xa100);
  709. phy_write (mii_info, 0x00, 0x2100);
  710. udelay (1000000);
  711. } else if (speed == 10) {
  712. phy_write (mii_info, 0x14, 0x8e40);
  713. phy_write (mii_info, 0x1b, 0x800b);
  714. phy_write (mii_info, 0x14, 0x0c82);
  715. phy_write (mii_info, 0x00, 0x8100);
  716. udelay (1000000);
  717. }
  718. }
  719. /* handle 88e1111 rev.B2 erratum 5.6 */
  720. if (mii_info->autoneg) {
  721. status = phy_read (mii_info, PHY_BMCR);
  722. phy_write (mii_info, PHY_BMCR, status | PHY_BMCR_AUTON);
  723. }
  724. /* now the B2 will correctly report autoneg completion status */
  725. }
  726. void change_phy_interface_mode (struct eth_device *dev,
  727. enet_interface_type_e type, int speed)
  728. {
  729. #ifdef CONFIG_PHY_MODE_NEED_CHANGE
  730. marvell_phy_interface_mode (dev, type, speed);
  731. #endif
  732. }