uec_phy.c 24 KB

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