uec_phy.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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 == ENET_1000_RGMII_RXID) {
  343. u16 val;
  344. int cnt = 50;
  345. /* Wait for aneg to complete. */
  346. do
  347. val = phy_read(mii_info, PHY_BMSR);
  348. while (--cnt && !(val & PHY_BMSR_AUTN_COMP));
  349. /* Set RDX clk delay. */
  350. phy_write(mii_info, 0x18, 0x7 | (7 << 12));
  351. val = phy_read(mii_info, 0x18);
  352. /* Set RDX-RXC skew. */
  353. val |= (1 << 8);
  354. val |= (7 | (7 << 12));
  355. /* Write bits 14:0. */
  356. val |= (1 << 15);
  357. phy_write(mii_info, 0x18, val);
  358. }
  359. return 0;
  360. }
  361. static int marvell_init(struct uec_mii_info *mii_info)
  362. {
  363. struct eth_device *edev = mii_info->dev;
  364. uec_private_t *uec = edev->priv;
  365. if (uec->uec_info->enet_interface == ENET_1000_RGMII_ID) {
  366. int temp;
  367. temp = phy_read(mii_info, MII_M1111_PHY_EXT_CR);
  368. temp |= (MII_M1111_RX_DELAY | MII_M1111_TX_DELAY);
  369. phy_write(mii_info, MII_M1111_PHY_EXT_CR, temp);
  370. temp = phy_read(mii_info, MII_M1111_PHY_EXT_SR);
  371. temp &= ~MII_M1111_HWCFG_MODE_MASK;
  372. temp |= MII_M1111_HWCFG_MODE_RGMII;
  373. phy_write(mii_info, MII_M1111_PHY_EXT_SR, temp);
  374. phy_write(mii_info, PHY_BMCR, PHY_BMCR_RESET);
  375. }
  376. return 0;
  377. }
  378. static int marvell_read_status (struct uec_mii_info *mii_info)
  379. {
  380. u16 status;
  381. int err;
  382. /* Update the link, but return if there
  383. * was an error */
  384. err = genmii_update_link (mii_info);
  385. if (err)
  386. return err;
  387. /* If the link is up, read the speed and duplex */
  388. /* If we aren't autonegotiating, assume speeds
  389. * are as set */
  390. if (mii_info->autoneg && mii_info->link) {
  391. int speed;
  392. status = phy_read (mii_info, MII_M1011_PHY_SPEC_STATUS);
  393. /* Get the duplexity */
  394. if (status & MII_M1011_PHY_SPEC_STATUS_FULLDUPLEX)
  395. mii_info->duplex = DUPLEX_FULL;
  396. else
  397. mii_info->duplex = DUPLEX_HALF;
  398. /* Get the speed */
  399. speed = status & MII_M1011_PHY_SPEC_STATUS_SPD_MASK;
  400. switch (speed) {
  401. case MII_M1011_PHY_SPEC_STATUS_1000:
  402. mii_info->speed = SPEED_1000;
  403. break;
  404. case MII_M1011_PHY_SPEC_STATUS_100:
  405. mii_info->speed = SPEED_100;
  406. break;
  407. default:
  408. mii_info->speed = SPEED_10;
  409. break;
  410. }
  411. mii_info->pause = 0;
  412. }
  413. return 0;
  414. }
  415. static int marvell_ack_interrupt (struct uec_mii_info *mii_info)
  416. {
  417. /* Clear the interrupts by reading the reg */
  418. phy_read (mii_info, MII_M1011_IEVENT);
  419. return 0;
  420. }
  421. static int marvell_config_intr (struct uec_mii_info *mii_info)
  422. {
  423. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  424. phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
  425. else
  426. phy_write (mii_info, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
  427. return 0;
  428. }
  429. static int dm9161_init (struct uec_mii_info *mii_info)
  430. {
  431. /* Reset the PHY */
  432. phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) |
  433. PHY_BMCR_RESET);
  434. /* PHY and MAC connect */
  435. phy_write (mii_info, PHY_BMCR, phy_read (mii_info, PHY_BMCR) &
  436. ~PHY_BMCR_ISO);
  437. phy_write (mii_info, MII_DM9161_SCR, MII_DM9161_SCR_INIT);
  438. config_genmii_advert (mii_info);
  439. /* Start/restart aneg */
  440. genmii_config_aneg (mii_info);
  441. return 0;
  442. }
  443. static int dm9161_config_aneg (struct uec_mii_info *mii_info)
  444. {
  445. return 0;
  446. }
  447. static int dm9161_read_status (struct uec_mii_info *mii_info)
  448. {
  449. u16 status;
  450. int err;
  451. /* Update the link, but return if there was an error */
  452. err = genmii_update_link (mii_info);
  453. if (err)
  454. return err;
  455. /* If the link is up, read the speed and duplex
  456. If we aren't autonegotiating assume speeds are as set */
  457. if (mii_info->autoneg && mii_info->link) {
  458. status = phy_read (mii_info, MII_DM9161_SCSR);
  459. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_100H))
  460. mii_info->speed = SPEED_100;
  461. else
  462. mii_info->speed = SPEED_10;
  463. if (status & (MII_DM9161_SCSR_100F | MII_DM9161_SCSR_10F))
  464. mii_info->duplex = DUPLEX_FULL;
  465. else
  466. mii_info->duplex = DUPLEX_HALF;
  467. }
  468. return 0;
  469. }
  470. static int dm9161_ack_interrupt (struct uec_mii_info *mii_info)
  471. {
  472. /* Clear the interrupt by reading the reg */
  473. phy_read (mii_info, MII_DM9161_INTR);
  474. return 0;
  475. }
  476. static int dm9161_config_intr (struct uec_mii_info *mii_info)
  477. {
  478. if (mii_info->interrupts == MII_INTERRUPT_ENABLED)
  479. phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_INIT);
  480. else
  481. phy_write (mii_info, MII_DM9161_INTR, MII_DM9161_INTR_STOP);
  482. return 0;
  483. }
  484. static void dm9161_close (struct uec_mii_info *mii_info)
  485. {
  486. }
  487. static int fixed_phy_aneg (struct uec_mii_info *mii_info)
  488. {
  489. mii_info->autoneg = 0; /* Turn off auto negotiation for fixed phy */
  490. return 0;
  491. }
  492. static int fixed_phy_read_status (struct uec_mii_info *mii_info)
  493. {
  494. int i = 0;
  495. for (i = 0; i < ARRAY_SIZE(fixed_phy_port); i++) {
  496. if (strncmp(mii_info->dev->name, fixed_phy_port[i].name,
  497. strlen(mii_info->dev->name)) == 0) {
  498. mii_info->speed = fixed_phy_port[i].speed;
  499. mii_info->duplex = fixed_phy_port[i].duplex;
  500. mii_info->link = 1; /* Link is always UP */
  501. mii_info->pause = 0;
  502. break;
  503. }
  504. }
  505. return 0;
  506. }
  507. static int smsc_config_aneg (struct uec_mii_info *mii_info)
  508. {
  509. return 0;
  510. }
  511. static int smsc_read_status (struct uec_mii_info *mii_info)
  512. {
  513. u16 status;
  514. int err;
  515. /* Update the link, but return if there
  516. * was an error */
  517. err = genmii_update_link (mii_info);
  518. if (err)
  519. return err;
  520. /* If the link is up, read the speed and duplex */
  521. /* If we aren't autonegotiating, assume speeds
  522. * are as set */
  523. if (mii_info->autoneg && mii_info->link) {
  524. int val;
  525. status = phy_read (mii_info, 0x1f);
  526. val = (status & 0x1c) >> 2;
  527. switch (val) {
  528. case 1:
  529. mii_info->duplex = DUPLEX_HALF;
  530. mii_info->speed = SPEED_10;
  531. break;
  532. case 5:
  533. mii_info->duplex = DUPLEX_FULL;
  534. mii_info->speed = SPEED_10;
  535. break;
  536. case 2:
  537. mii_info->duplex = DUPLEX_HALF;
  538. mii_info->speed = SPEED_100;
  539. break;
  540. case 6:
  541. mii_info->duplex = DUPLEX_FULL;
  542. mii_info->speed = SPEED_100;
  543. break;
  544. }
  545. mii_info->pause = 0;
  546. }
  547. return 0;
  548. }
  549. static struct phy_info phy_info_dm9161 = {
  550. .phy_id = 0x0181b880,
  551. .phy_id_mask = 0x0ffffff0,
  552. .name = "Davicom DM9161E",
  553. .init = dm9161_init,
  554. .config_aneg = dm9161_config_aneg,
  555. .read_status = dm9161_read_status,
  556. .close = dm9161_close,
  557. };
  558. static struct phy_info phy_info_dm9161a = {
  559. .phy_id = 0x0181b8a0,
  560. .phy_id_mask = 0x0ffffff0,
  561. .name = "Davicom DM9161A",
  562. .features = MII_BASIC_FEATURES,
  563. .init = dm9161_init,
  564. .config_aneg = dm9161_config_aneg,
  565. .read_status = dm9161_read_status,
  566. .ack_interrupt = dm9161_ack_interrupt,
  567. .config_intr = dm9161_config_intr,
  568. .close = dm9161_close,
  569. };
  570. static struct phy_info phy_info_marvell = {
  571. .phy_id = 0x01410c00,
  572. .phy_id_mask = 0xffffff00,
  573. .name = "Marvell 88E11x1",
  574. .features = MII_GBIT_FEATURES,
  575. .init = &marvell_init,
  576. .config_aneg = &marvell_config_aneg,
  577. .read_status = &marvell_read_status,
  578. .ack_interrupt = &marvell_ack_interrupt,
  579. .config_intr = &marvell_config_intr,
  580. };
  581. static struct phy_info phy_info_bcm5481 = {
  582. .phy_id = 0x0143bca0,
  583. .phy_id_mask = 0xffffff0,
  584. .name = "Broadcom 5481",
  585. .features = MII_GBIT_FEATURES,
  586. .read_status = genmii_read_status,
  587. .init = bcm_init,
  588. };
  589. static struct phy_info phy_info_fixedphy = {
  590. .phy_id = CONFIG_FIXED_PHY,
  591. .phy_id_mask = CONFIG_FIXED_PHY,
  592. .name = "Fixed PHY",
  593. .config_aneg = fixed_phy_aneg,
  594. .read_status = fixed_phy_read_status,
  595. };
  596. static struct phy_info phy_info_smsclan8700 = {
  597. .phy_id = 0x0007c0c0,
  598. .phy_id_mask = 0xfffffff0,
  599. .name = "SMSC LAN8700",
  600. .features = MII_BASIC_FEATURES,
  601. .config_aneg = smsc_config_aneg,
  602. .read_status = smsc_read_status,
  603. };
  604. static struct phy_info phy_info_genmii = {
  605. .phy_id = 0x00000000,
  606. .phy_id_mask = 0x00000000,
  607. .name = "Generic MII",
  608. .features = MII_BASIC_FEATURES,
  609. .config_aneg = genmii_config_aneg,
  610. .read_status = genmii_read_status,
  611. };
  612. static struct phy_info *phy_info[] = {
  613. &phy_info_dm9161,
  614. &phy_info_dm9161a,
  615. &phy_info_marvell,
  616. &phy_info_bcm5481,
  617. &phy_info_smsclan8700,
  618. &phy_info_fixedphy,
  619. &phy_info_genmii,
  620. NULL
  621. };
  622. u16 phy_read (struct uec_mii_info *mii_info, u16 regnum)
  623. {
  624. return mii_info->mdio_read (mii_info->dev, mii_info->mii_id, regnum);
  625. }
  626. void phy_write (struct uec_mii_info *mii_info, u16 regnum, u16 val)
  627. {
  628. mii_info->mdio_write (mii_info->dev, mii_info->mii_id, regnum, val);
  629. }
  630. /* Use the PHY ID registers to determine what type of PHY is attached
  631. * to device dev. return a struct phy_info structure describing that PHY
  632. */
  633. struct phy_info *uec_get_phy_info (struct uec_mii_info *mii_info)
  634. {
  635. u16 phy_reg;
  636. u32 phy_ID;
  637. int i;
  638. struct phy_info *theInfo = NULL;
  639. /* Grab the bits from PHYIR1, and put them in the upper half */
  640. phy_reg = phy_read (mii_info, PHY_PHYIDR1);
  641. phy_ID = (phy_reg & 0xffff) << 16;
  642. /* Grab the bits from PHYIR2, and put them in the lower half */
  643. phy_reg = phy_read (mii_info, PHY_PHYIDR2);
  644. phy_ID |= (phy_reg & 0xffff);
  645. /* loop through all the known PHY types, and find one that */
  646. /* matches the ID we read from the PHY. */
  647. for (i = 0; phy_info[i]; i++)
  648. if (phy_info[i]->phy_id ==
  649. (phy_ID & phy_info[i]->phy_id_mask)) {
  650. theInfo = phy_info[i];
  651. break;
  652. }
  653. /* This shouldn't happen, as we have generic PHY support */
  654. if (theInfo == NULL) {
  655. ugphy_info ("UEC: PHY id %x is not supported!", phy_ID);
  656. return NULL;
  657. } else {
  658. ugphy_info ("UEC: PHY is %s (%x)", theInfo->name, phy_ID);
  659. }
  660. return theInfo;
  661. }
  662. void marvell_phy_interface_mode (struct eth_device *dev,
  663. enet_interface_e mode)
  664. {
  665. uec_private_t *uec = (uec_private_t *) dev->priv;
  666. struct uec_mii_info *mii_info;
  667. u16 status;
  668. if (!uec->mii_info) {
  669. printf ("%s: the PHY not initialized\n", __FUNCTION__);
  670. return;
  671. }
  672. mii_info = uec->mii_info;
  673. if (mode == ENET_100_RGMII) {
  674. phy_write (mii_info, 0x00, 0x9140);
  675. phy_write (mii_info, 0x1d, 0x001f);
  676. phy_write (mii_info, 0x1e, 0x200c);
  677. phy_write (mii_info, 0x1d, 0x0005);
  678. phy_write (mii_info, 0x1e, 0x0000);
  679. phy_write (mii_info, 0x1e, 0x0100);
  680. phy_write (mii_info, 0x09, 0x0e00);
  681. phy_write (mii_info, 0x04, 0x01e1);
  682. phy_write (mii_info, 0x00, 0x9140);
  683. phy_write (mii_info, 0x00, 0x1000);
  684. udelay (100000);
  685. phy_write (mii_info, 0x00, 0x2900);
  686. phy_write (mii_info, 0x14, 0x0cd2);
  687. phy_write (mii_info, 0x00, 0xa100);
  688. phy_write (mii_info, 0x09, 0x0000);
  689. phy_write (mii_info, 0x1b, 0x800b);
  690. phy_write (mii_info, 0x04, 0x05e1);
  691. phy_write (mii_info, 0x00, 0xa100);
  692. phy_write (mii_info, 0x00, 0x2100);
  693. udelay (1000000);
  694. } else if (mode == ENET_10_RGMII) {
  695. phy_write (mii_info, 0x14, 0x8e40);
  696. phy_write (mii_info, 0x1b, 0x800b);
  697. phy_write (mii_info, 0x14, 0x0c82);
  698. phy_write (mii_info, 0x00, 0x8100);
  699. udelay (1000000);
  700. }
  701. /* handle 88e1111 rev.B2 erratum 5.6 */
  702. if (mii_info->autoneg) {
  703. status = phy_read (mii_info, PHY_BMCR);
  704. phy_write (mii_info, PHY_BMCR, status | PHY_BMCR_AUTON);
  705. }
  706. /* now the B2 will correctly report autoneg completion status */
  707. }
  708. void change_phy_interface_mode (struct eth_device *dev, enet_interface_e mode)
  709. {
  710. #ifdef CONFIG_PHY_MODE_NEED_CHANGE
  711. marvell_phy_interface_mode (dev, mode);
  712. #endif
  713. }