ibm_emac_phy.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * drivers/net/ibm_emac/ibm_emac_phy.c
  3. *
  4. * Driver for PowerPC 4xx on-chip ethernet controller, PHY support.
  5. * Borrowed from sungem_phy.c, though I only kept the generic MII
  6. * driver for now.
  7. *
  8. * This file should be shared with other drivers or eventually
  9. * merged as the "low level" part of miilib
  10. *
  11. * (c) 2003, Benjamin Herrenscmidt (benh@kernel.crashing.org)
  12. * (c) 2004-2005, Eugene Surovegin <ebs@ebshome.net>
  13. *
  14. */
  15. #include <linux/config.h>
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/mii.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/delay.h>
  23. #include <asm/ocp.h>
  24. #include "ibm_emac_phy.h"
  25. static inline int phy_read(struct mii_phy *phy, int reg)
  26. {
  27. return phy->mdio_read(phy->dev, phy->address, reg);
  28. }
  29. static inline void phy_write(struct mii_phy *phy, int reg, int val)
  30. {
  31. phy->mdio_write(phy->dev, phy->address, reg, val);
  32. }
  33. int mii_reset_phy(struct mii_phy *phy)
  34. {
  35. int val;
  36. int limit = 10000;
  37. val = phy_read(phy, MII_BMCR);
  38. val &= ~BMCR_ISOLATE;
  39. val |= BMCR_RESET;
  40. phy_write(phy, MII_BMCR, val);
  41. udelay(300);
  42. while (limit--) {
  43. val = phy_read(phy, MII_BMCR);
  44. if (val >= 0 && (val & BMCR_RESET) == 0)
  45. break;
  46. udelay(10);
  47. }
  48. if ((val & BMCR_ISOLATE) && limit > 0)
  49. phy_write(phy, MII_BMCR, val & ~BMCR_ISOLATE);
  50. return limit <= 0;
  51. }
  52. static int genmii_setup_aneg(struct mii_phy *phy, u32 advertise)
  53. {
  54. int ctl, adv;
  55. phy->autoneg = AUTONEG_ENABLE;
  56. phy->speed = SPEED_10;
  57. phy->duplex = DUPLEX_HALF;
  58. phy->pause = phy->asym_pause = 0;
  59. phy->advertising = advertise;
  60. /* Setup standard advertise */
  61. adv = phy_read(phy, MII_ADVERTISE);
  62. if (adv < 0)
  63. return adv;
  64. adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
  65. ADVERTISE_PAUSE_ASYM);
  66. if (advertise & ADVERTISED_10baseT_Half)
  67. adv |= ADVERTISE_10HALF;
  68. if (advertise & ADVERTISED_10baseT_Full)
  69. adv |= ADVERTISE_10FULL;
  70. if (advertise & ADVERTISED_100baseT_Half)
  71. adv |= ADVERTISE_100HALF;
  72. if (advertise & ADVERTISED_100baseT_Full)
  73. adv |= ADVERTISE_100FULL;
  74. if (advertise & ADVERTISED_Pause)
  75. adv |= ADVERTISE_PAUSE_CAP;
  76. if (advertise & ADVERTISED_Asym_Pause)
  77. adv |= ADVERTISE_PAUSE_ASYM;
  78. phy_write(phy, MII_ADVERTISE, adv);
  79. if (phy->features &
  80. (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseT_Half)) {
  81. adv = phy_read(phy, MII_CTRL1000);
  82. if (adv < 0)
  83. return adv;
  84. adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  85. if (advertise & ADVERTISED_1000baseT_Full)
  86. adv |= ADVERTISE_1000FULL;
  87. if (advertise & ADVERTISED_1000baseT_Half)
  88. adv |= ADVERTISE_1000HALF;
  89. phy_write(phy, MII_CTRL1000, adv);
  90. }
  91. /* Start/Restart aneg */
  92. ctl = phy_read(phy, MII_BMCR);
  93. ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
  94. phy_write(phy, MII_BMCR, ctl);
  95. return 0;
  96. }
  97. static int genmii_setup_forced(struct mii_phy *phy, int speed, int fd)
  98. {
  99. int ctl;
  100. phy->autoneg = AUTONEG_DISABLE;
  101. phy->speed = speed;
  102. phy->duplex = fd;
  103. phy->pause = phy->asym_pause = 0;
  104. ctl = phy_read(phy, MII_BMCR);
  105. if (ctl < 0)
  106. return ctl;
  107. ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 | BMCR_ANENABLE);
  108. /* First reset the PHY */
  109. phy_write(phy, MII_BMCR, ctl | BMCR_RESET);
  110. /* Select speed & duplex */
  111. switch (speed) {
  112. case SPEED_10:
  113. break;
  114. case SPEED_100:
  115. ctl |= BMCR_SPEED100;
  116. break;
  117. case SPEED_1000:
  118. ctl |= BMCR_SPEED1000;
  119. break;
  120. default:
  121. return -EINVAL;
  122. }
  123. if (fd == DUPLEX_FULL)
  124. ctl |= BMCR_FULLDPLX;
  125. phy_write(phy, MII_BMCR, ctl);
  126. return 0;
  127. }
  128. static int genmii_poll_link(struct mii_phy *phy)
  129. {
  130. int status;
  131. /* Clear latched value with dummy read */
  132. phy_read(phy, MII_BMSR);
  133. status = phy_read(phy, MII_BMSR);
  134. if (status < 0 || (status & BMSR_LSTATUS) == 0)
  135. return 0;
  136. if (phy->autoneg == AUTONEG_ENABLE && !(status & BMSR_ANEGCOMPLETE))
  137. return 0;
  138. return 1;
  139. }
  140. static int genmii_read_link(struct mii_phy *phy)
  141. {
  142. if (phy->autoneg == AUTONEG_ENABLE) {
  143. int glpa = 0;
  144. int lpa = phy_read(phy, MII_LPA) & phy_read(phy, MII_ADVERTISE);
  145. if (lpa < 0)
  146. return lpa;
  147. if (phy->features &
  148. (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseT_Half)) {
  149. int adv = phy_read(phy, MII_CTRL1000);
  150. glpa = phy_read(phy, MII_STAT1000);
  151. if (glpa < 0 || adv < 0)
  152. return adv;
  153. glpa &= adv << 2;
  154. }
  155. phy->speed = SPEED_10;
  156. phy->duplex = DUPLEX_HALF;
  157. phy->pause = phy->asym_pause = 0;
  158. if (glpa & (LPA_1000FULL | LPA_1000HALF)) {
  159. phy->speed = SPEED_1000;
  160. if (glpa & LPA_1000FULL)
  161. phy->duplex = DUPLEX_FULL;
  162. } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
  163. phy->speed = SPEED_100;
  164. if (lpa & LPA_100FULL)
  165. phy->duplex = DUPLEX_FULL;
  166. } else if (lpa & LPA_10FULL)
  167. phy->duplex = DUPLEX_FULL;
  168. if (phy->duplex == DUPLEX_FULL) {
  169. phy->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  170. phy->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  171. }
  172. } else {
  173. int bmcr = phy_read(phy, MII_BMCR);
  174. if (bmcr < 0)
  175. return bmcr;
  176. if (bmcr & BMCR_FULLDPLX)
  177. phy->duplex = DUPLEX_FULL;
  178. else
  179. phy->duplex = DUPLEX_HALF;
  180. if (bmcr & BMCR_SPEED1000)
  181. phy->speed = SPEED_1000;
  182. else if (bmcr & BMCR_SPEED100)
  183. phy->speed = SPEED_100;
  184. else
  185. phy->speed = SPEED_10;
  186. phy->pause = phy->asym_pause = 0;
  187. }
  188. return 0;
  189. }
  190. /* Generic implementation for most 10/100/1000 PHYs */
  191. static struct mii_phy_ops generic_phy_ops = {
  192. .setup_aneg = genmii_setup_aneg,
  193. .setup_forced = genmii_setup_forced,
  194. .poll_link = genmii_poll_link,
  195. .read_link = genmii_read_link
  196. };
  197. static struct mii_phy_def genmii_phy_def = {
  198. .phy_id = 0x00000000,
  199. .phy_id_mask = 0x00000000,
  200. .name = "Generic MII",
  201. .ops = &generic_phy_ops
  202. };
  203. /* CIS8201 */
  204. #define MII_CIS8201_10BTCSR 0x16
  205. #define TENBTCSR_ECHO_DISABLE 0x2000
  206. #define MII_CIS8201_EPCR 0x17
  207. #define EPCR_MODE_MASK 0x3000
  208. #define EPCR_GMII_MODE 0x0000
  209. #define EPCR_RGMII_MODE 0x1000
  210. #define EPCR_TBI_MODE 0x2000
  211. #define EPCR_RTBI_MODE 0x3000
  212. #define MII_CIS8201_ACSR 0x1c
  213. #define ACSR_PIN_PRIO_SELECT 0x0004
  214. static int cis8201_init(struct mii_phy *phy)
  215. {
  216. int epcr;
  217. epcr = phy_read(phy, MII_CIS8201_EPCR);
  218. if (epcr < 0)
  219. return epcr;
  220. epcr &= ~EPCR_MODE_MASK;
  221. switch (phy->mode) {
  222. case PHY_MODE_TBI:
  223. epcr |= EPCR_TBI_MODE;
  224. break;
  225. case PHY_MODE_RTBI:
  226. epcr |= EPCR_RTBI_MODE;
  227. break;
  228. case PHY_MODE_GMII:
  229. epcr |= EPCR_GMII_MODE;
  230. break;
  231. case PHY_MODE_RGMII:
  232. default:
  233. epcr |= EPCR_RGMII_MODE;
  234. }
  235. phy_write(phy, MII_CIS8201_EPCR, epcr);
  236. /* MII regs override strap pins */
  237. phy_write(phy, MII_CIS8201_ACSR,
  238. phy_read(phy, MII_CIS8201_ACSR) | ACSR_PIN_PRIO_SELECT);
  239. /* Disable TX_EN -> CRS echo mode, otherwise 10/HDX doesn't work */
  240. phy_write(phy, MII_CIS8201_10BTCSR,
  241. phy_read(phy, MII_CIS8201_10BTCSR) | TENBTCSR_ECHO_DISABLE);
  242. return 0;
  243. }
  244. static struct mii_phy_ops cis8201_phy_ops = {
  245. .init = cis8201_init,
  246. .setup_aneg = genmii_setup_aneg,
  247. .setup_forced = genmii_setup_forced,
  248. .poll_link = genmii_poll_link,
  249. .read_link = genmii_read_link
  250. };
  251. static struct mii_phy_def cis8201_phy_def = {
  252. .phy_id = 0x000fc410,
  253. .phy_id_mask = 0x000ffff0,
  254. .name = "CIS8201 Gigabit Ethernet",
  255. .ops = &cis8201_phy_ops
  256. };
  257. static struct mii_phy_def *mii_phy_table[] = {
  258. &cis8201_phy_def,
  259. &genmii_phy_def,
  260. NULL
  261. };
  262. int mii_phy_probe(struct mii_phy *phy, int address)
  263. {
  264. struct mii_phy_def *def;
  265. int i;
  266. u32 id;
  267. phy->autoneg = AUTONEG_DISABLE;
  268. phy->advertising = 0;
  269. phy->address = address;
  270. phy->speed = SPEED_10;
  271. phy->duplex = DUPLEX_HALF;
  272. phy->pause = phy->asym_pause = 0;
  273. /* Take PHY out of isolate mode and reset it. */
  274. if (mii_reset_phy(phy))
  275. return -ENODEV;
  276. /* Read ID and find matching entry */
  277. id = (phy_read(phy, MII_PHYSID1) << 16) | phy_read(phy, MII_PHYSID2);
  278. for (i = 0; (def = mii_phy_table[i]) != NULL; i++)
  279. if ((id & def->phy_id_mask) == def->phy_id)
  280. break;
  281. /* Should never be NULL (we have a generic entry), but... */
  282. if (!def)
  283. return -ENODEV;
  284. phy->def = def;
  285. /* Determine PHY features if needed */
  286. phy->features = def->features;
  287. if (!phy->features) {
  288. u16 bmsr = phy_read(phy, MII_BMSR);
  289. if (bmsr & BMSR_ANEGCAPABLE)
  290. phy->features |= SUPPORTED_Autoneg;
  291. if (bmsr & BMSR_10HALF)
  292. phy->features |= SUPPORTED_10baseT_Half;
  293. if (bmsr & BMSR_10FULL)
  294. phy->features |= SUPPORTED_10baseT_Full;
  295. if (bmsr & BMSR_100HALF)
  296. phy->features |= SUPPORTED_100baseT_Half;
  297. if (bmsr & BMSR_100FULL)
  298. phy->features |= SUPPORTED_100baseT_Full;
  299. if (bmsr & BMSR_ESTATEN) {
  300. u16 esr = phy_read(phy, MII_ESTATUS);
  301. if (esr & ESTATUS_1000_TFULL)
  302. phy->features |= SUPPORTED_1000baseT_Full;
  303. if (esr & ESTATUS_1000_THALF)
  304. phy->features |= SUPPORTED_1000baseT_Half;
  305. }
  306. phy->features |= SUPPORTED_MII;
  307. }
  308. /* Setup default advertising */
  309. phy->advertising = phy->features;
  310. return 0;
  311. }
  312. MODULE_LICENSE("GPL");