uec_phy.c 24 KB

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