marvell.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  1. /*
  2. * drivers/net/phy/marvell.c
  3. *
  4. * Driver for Marvell PHYs
  5. *
  6. * Author: Andy Fleming
  7. *
  8. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  9. *
  10. * Copyright (c) 2013 Michael Stapelberg <michael@stapelberg.de>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/unistd.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/mm.h>
  30. #include <linux/module.h>
  31. #include <linux/mii.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/phy.h>
  34. #include <linux/marvell_phy.h>
  35. #include <linux/of.h>
  36. #include <linux/io.h>
  37. #include <asm/irq.h>
  38. #include <linux/uaccess.h>
  39. #define MII_MARVELL_PHY_PAGE 22
  40. #define MII_M1011_IEVENT 0x13
  41. #define MII_M1011_IEVENT_CLEAR 0x0000
  42. #define MII_M1011_IMASK 0x12
  43. #define MII_M1011_IMASK_INIT 0x6400
  44. #define MII_M1011_IMASK_CLEAR 0x0000
  45. #define MII_M1011_PHY_SCR 0x10
  46. #define MII_M1011_PHY_SCR_MDI 0x0000
  47. #define MII_M1011_PHY_SCR_MDI_X 0x0020
  48. #define MII_M1011_PHY_SCR_AUTO_CROSS 0x0060
  49. #define MII_M1145_PHY_EXT_ADDR_PAGE 0x16
  50. #define MII_M1145_PHY_EXT_SR 0x1b
  51. #define MII_M1145_PHY_EXT_CR 0x14
  52. #define MII_M1145_RGMII_RX_DELAY 0x0080
  53. #define MII_M1145_RGMII_TX_DELAY 0x0002
  54. #define MII_M1145_HWCFG_MODE_SGMII_NO_CLK 0x4
  55. #define MII_M1145_HWCFG_MODE_MASK 0xf
  56. #define MII_M1145_HWCFG_FIBER_COPPER_AUTO 0x8000
  57. #define MII_M1145_HWCFG_MODE_SGMII_NO_CLK 0x4
  58. #define MII_M1145_HWCFG_MODE_MASK 0xf
  59. #define MII_M1145_HWCFG_FIBER_COPPER_AUTO 0x8000
  60. #define MII_M1111_PHY_LED_CONTROL 0x18
  61. #define MII_M1111_PHY_LED_DIRECT 0x4100
  62. #define MII_M1111_PHY_LED_COMBINE 0x411c
  63. #define MII_M1111_PHY_EXT_CR 0x14
  64. #define MII_M1111_RX_DELAY 0x80
  65. #define MII_M1111_TX_DELAY 0x2
  66. #define MII_M1111_PHY_EXT_SR 0x1b
  67. #define MII_M1111_HWCFG_MODE_MASK 0xf
  68. #define MII_M1111_HWCFG_MODE_COPPER_RGMII 0xb
  69. #define MII_M1111_HWCFG_MODE_FIBER_RGMII 0x3
  70. #define MII_M1111_HWCFG_MODE_SGMII_NO_CLK 0x4
  71. #define MII_M1111_HWCFG_MODE_COPPER_RTBI 0x9
  72. #define MII_M1111_HWCFG_FIBER_COPPER_AUTO 0x8000
  73. #define MII_M1111_HWCFG_FIBER_COPPER_RES 0x2000
  74. #define MII_M1111_COPPER 0
  75. #define MII_M1111_FIBER 1
  76. #define MII_88E1121_PHY_MSCR_PAGE 2
  77. #define MII_88E1121_PHY_MSCR_REG 21
  78. #define MII_88E1121_PHY_MSCR_RX_DELAY BIT(5)
  79. #define MII_88E1121_PHY_MSCR_TX_DELAY BIT(4)
  80. #define MII_88E1121_PHY_MSCR_DELAY_MASK (~(0x3 << 4))
  81. #define MII_88E1318S_PHY_MSCR1_REG 16
  82. #define MII_88E1318S_PHY_MSCR1_PAD_ODD BIT(6)
  83. /* Copper Specific Interrupt Enable Register */
  84. #define MII_88E1318S_PHY_CSIER 0x12
  85. /* WOL Event Interrupt Enable */
  86. #define MII_88E1318S_PHY_CSIER_WOL_EIE BIT(7)
  87. /* LED Timer Control Register */
  88. #define MII_88E1318S_PHY_LED_PAGE 0x03
  89. #define MII_88E1318S_PHY_LED_TCR 0x12
  90. #define MII_88E1318S_PHY_LED_TCR_FORCE_INT BIT(15)
  91. #define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE BIT(7)
  92. #define MII_88E1318S_PHY_LED_TCR_INT_ACTIVE_LOW BIT(11)
  93. /* Magic Packet MAC address registers */
  94. #define MII_88E1318S_PHY_MAGIC_PACKET_WORD2 0x17
  95. #define MII_88E1318S_PHY_MAGIC_PACKET_WORD1 0x18
  96. #define MII_88E1318S_PHY_MAGIC_PACKET_WORD0 0x19
  97. #define MII_88E1318S_PHY_WOL_PAGE 0x11
  98. #define MII_88E1318S_PHY_WOL_CTRL 0x10
  99. #define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS BIT(12)
  100. #define MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE BIT(14)
  101. #define MII_88E1121_PHY_LED_CTRL 16
  102. #define MII_88E1121_PHY_LED_PAGE 3
  103. #define MII_88E1121_PHY_LED_DEF 0x0030
  104. #define MII_M1011_PHY_STATUS 0x11
  105. #define MII_M1011_PHY_STATUS_1000 0x8000
  106. #define MII_M1011_PHY_STATUS_100 0x4000
  107. #define MII_M1011_PHY_STATUS_SPD_MASK 0xc000
  108. #define MII_M1011_PHY_STATUS_FULLDUPLEX 0x2000
  109. #define MII_M1011_PHY_STATUS_RESOLVED 0x0800
  110. #define MII_M1011_PHY_STATUS_LINK 0x0400
  111. #define MII_M1116R_CONTROL_REG_MAC 21
  112. #define MII_88E3016_PHY_SPEC_CTRL 0x10
  113. #define MII_88E3016_DISABLE_SCRAMBLER 0x0200
  114. #define MII_88E3016_AUTO_MDIX_CROSSOVER 0x0030
  115. #define MII_88E1510_GEN_CTRL_REG_1 0x14
  116. #define MII_88E1510_GEN_CTRL_REG_1_MODE_MASK 0x7
  117. #define MII_88E1510_GEN_CTRL_REG_1_MODE_SGMII 0x1 /* SGMII to copper */
  118. #define MII_88E1510_GEN_CTRL_REG_1_RESET 0x8000 /* Soft reset */
  119. #define LPA_FIBER_1000HALF 0x40
  120. #define LPA_FIBER_1000FULL 0x20
  121. #define LPA_PAUSE_FIBER 0x180
  122. #define LPA_PAUSE_ASYM_FIBER 0x100
  123. #define ADVERTISE_FIBER_1000HALF 0x40
  124. #define ADVERTISE_FIBER_1000FULL 0x20
  125. #define ADVERTISE_PAUSE_FIBER 0x180
  126. #define ADVERTISE_PAUSE_ASYM_FIBER 0x100
  127. #define REGISTER_LINK_STATUS 0x400
  128. #define NB_FIBER_STATS 1
  129. MODULE_DESCRIPTION("Marvell PHY driver");
  130. MODULE_AUTHOR("Andy Fleming");
  131. MODULE_LICENSE("GPL");
  132. struct marvell_hw_stat {
  133. const char *string;
  134. u8 page;
  135. u8 reg;
  136. u8 bits;
  137. };
  138. static struct marvell_hw_stat marvell_hw_stats[] = {
  139. { "phy_receive_errors_copper", 0, 21, 16},
  140. { "phy_idle_errors", 0, 10, 8 },
  141. { "phy_receive_errors_fiber", 1, 21, 16},
  142. };
  143. struct marvell_priv {
  144. u64 stats[ARRAY_SIZE(marvell_hw_stats)];
  145. };
  146. static int marvell_ack_interrupt(struct phy_device *phydev)
  147. {
  148. int err;
  149. /* Clear the interrupts by reading the reg */
  150. err = phy_read(phydev, MII_M1011_IEVENT);
  151. if (err < 0)
  152. return err;
  153. return 0;
  154. }
  155. static int marvell_config_intr(struct phy_device *phydev)
  156. {
  157. int err;
  158. if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
  159. err = phy_write(phydev, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
  160. else
  161. err = phy_write(phydev, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
  162. return err;
  163. }
  164. static int marvell_set_polarity(struct phy_device *phydev, int polarity)
  165. {
  166. int reg;
  167. int err;
  168. int val;
  169. /* get the current settings */
  170. reg = phy_read(phydev, MII_M1011_PHY_SCR);
  171. if (reg < 0)
  172. return reg;
  173. val = reg;
  174. val &= ~MII_M1011_PHY_SCR_AUTO_CROSS;
  175. switch (polarity) {
  176. case ETH_TP_MDI:
  177. val |= MII_M1011_PHY_SCR_MDI;
  178. break;
  179. case ETH_TP_MDI_X:
  180. val |= MII_M1011_PHY_SCR_MDI_X;
  181. break;
  182. case ETH_TP_MDI_AUTO:
  183. case ETH_TP_MDI_INVALID:
  184. default:
  185. val |= MII_M1011_PHY_SCR_AUTO_CROSS;
  186. break;
  187. }
  188. if (val != reg) {
  189. /* Set the new polarity value in the register */
  190. err = phy_write(phydev, MII_M1011_PHY_SCR, val);
  191. if (err)
  192. return err;
  193. }
  194. return 0;
  195. }
  196. static int marvell_config_aneg(struct phy_device *phydev)
  197. {
  198. int err;
  199. err = marvell_set_polarity(phydev, phydev->mdix);
  200. if (err < 0)
  201. return err;
  202. err = phy_write(phydev, MII_M1111_PHY_LED_CONTROL,
  203. MII_M1111_PHY_LED_DIRECT);
  204. if (err < 0)
  205. return err;
  206. err = genphy_config_aneg(phydev);
  207. if (err < 0)
  208. return err;
  209. if (phydev->autoneg != AUTONEG_ENABLE) {
  210. int bmcr;
  211. /*
  212. * A write to speed/duplex bits (that is performed by
  213. * genphy_config_aneg() call above) must be followed by
  214. * a software reset. Otherwise, the write has no effect.
  215. */
  216. bmcr = phy_read(phydev, MII_BMCR);
  217. if (bmcr < 0)
  218. return bmcr;
  219. err = phy_write(phydev, MII_BMCR, bmcr | BMCR_RESET);
  220. if (err < 0)
  221. return err;
  222. }
  223. return 0;
  224. }
  225. static int m88e1101_config_aneg(struct phy_device *phydev)
  226. {
  227. int err;
  228. /* This Marvell PHY has an errata which requires
  229. * that certain registers get written in order
  230. * to restart autonegotiation
  231. */
  232. err = phy_write(phydev, MII_BMCR, BMCR_RESET);
  233. if (err < 0)
  234. return err;
  235. err = phy_write(phydev, 0x1d, 0x1f);
  236. if (err < 0)
  237. return err;
  238. err = phy_write(phydev, 0x1e, 0x200c);
  239. if (err < 0)
  240. return err;
  241. err = phy_write(phydev, 0x1d, 0x5);
  242. if (err < 0)
  243. return err;
  244. err = phy_write(phydev, 0x1e, 0);
  245. if (err < 0)
  246. return err;
  247. err = phy_write(phydev, 0x1e, 0x100);
  248. if (err < 0)
  249. return err;
  250. return marvell_config_aneg(phydev);
  251. }
  252. static int m88e1111_config_aneg(struct phy_device *phydev)
  253. {
  254. int err;
  255. /* The Marvell PHY has an errata which requires
  256. * that certain registers get written in order
  257. * to restart autonegotiation
  258. */
  259. err = phy_write(phydev, MII_BMCR, BMCR_RESET);
  260. err = marvell_set_polarity(phydev, phydev->mdix);
  261. if (err < 0)
  262. return err;
  263. err = phy_write(phydev, MII_M1111_PHY_LED_CONTROL,
  264. MII_M1111_PHY_LED_DIRECT);
  265. if (err < 0)
  266. return err;
  267. err = genphy_config_aneg(phydev);
  268. if (err < 0)
  269. return err;
  270. if (phydev->autoneg != AUTONEG_ENABLE) {
  271. int bmcr;
  272. /* A write to speed/duplex bits (that is performed by
  273. * genphy_config_aneg() call above) must be followed by
  274. * a software reset. Otherwise, the write has no effect.
  275. */
  276. bmcr = phy_read(phydev, MII_BMCR);
  277. if (bmcr < 0)
  278. return bmcr;
  279. err = phy_write(phydev, MII_BMCR, bmcr | BMCR_RESET);
  280. if (err < 0)
  281. return err;
  282. }
  283. return 0;
  284. }
  285. #ifdef CONFIG_OF_MDIO
  286. /*
  287. * Set and/or override some configuration registers based on the
  288. * marvell,reg-init property stored in the of_node for the phydev.
  289. *
  290. * marvell,reg-init = <reg-page reg mask value>,...;
  291. *
  292. * There may be one or more sets of <reg-page reg mask value>:
  293. *
  294. * reg-page: which register bank to use.
  295. * reg: the register.
  296. * mask: if non-zero, ANDed with existing register value.
  297. * value: ORed with the masked value and written to the regiser.
  298. *
  299. */
  300. static int marvell_of_reg_init(struct phy_device *phydev)
  301. {
  302. const __be32 *paddr;
  303. int len, i, saved_page, current_page, page_changed, ret;
  304. if (!phydev->mdio.dev.of_node)
  305. return 0;
  306. paddr = of_get_property(phydev->mdio.dev.of_node,
  307. "marvell,reg-init", &len);
  308. if (!paddr || len < (4 * sizeof(*paddr)))
  309. return 0;
  310. saved_page = phy_read(phydev, MII_MARVELL_PHY_PAGE);
  311. if (saved_page < 0)
  312. return saved_page;
  313. page_changed = 0;
  314. current_page = saved_page;
  315. ret = 0;
  316. len /= sizeof(*paddr);
  317. for (i = 0; i < len - 3; i += 4) {
  318. u16 reg_page = be32_to_cpup(paddr + i);
  319. u16 reg = be32_to_cpup(paddr + i + 1);
  320. u16 mask = be32_to_cpup(paddr + i + 2);
  321. u16 val_bits = be32_to_cpup(paddr + i + 3);
  322. int val;
  323. if (reg_page != current_page) {
  324. current_page = reg_page;
  325. page_changed = 1;
  326. ret = phy_write(phydev, MII_MARVELL_PHY_PAGE, reg_page);
  327. if (ret < 0)
  328. goto err;
  329. }
  330. val = 0;
  331. if (mask) {
  332. val = phy_read(phydev, reg);
  333. if (val < 0) {
  334. ret = val;
  335. goto err;
  336. }
  337. val &= mask;
  338. }
  339. val |= val_bits;
  340. ret = phy_write(phydev, reg, val);
  341. if (ret < 0)
  342. goto err;
  343. }
  344. err:
  345. if (page_changed) {
  346. i = phy_write(phydev, MII_MARVELL_PHY_PAGE, saved_page);
  347. if (ret == 0)
  348. ret = i;
  349. }
  350. return ret;
  351. }
  352. #else
  353. static int marvell_of_reg_init(struct phy_device *phydev)
  354. {
  355. return 0;
  356. }
  357. #endif /* CONFIG_OF_MDIO */
  358. static int m88e1121_config_aneg(struct phy_device *phydev)
  359. {
  360. int err, oldpage, mscr;
  361. oldpage = phy_read(phydev, MII_MARVELL_PHY_PAGE);
  362. err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
  363. MII_88E1121_PHY_MSCR_PAGE);
  364. if (err < 0)
  365. return err;
  366. if (phy_interface_is_rgmii(phydev)) {
  367. mscr = phy_read(phydev, MII_88E1121_PHY_MSCR_REG) &
  368. MII_88E1121_PHY_MSCR_DELAY_MASK;
  369. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
  370. mscr |= (MII_88E1121_PHY_MSCR_RX_DELAY |
  371. MII_88E1121_PHY_MSCR_TX_DELAY);
  372. else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
  373. mscr |= MII_88E1121_PHY_MSCR_RX_DELAY;
  374. else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
  375. mscr |= MII_88E1121_PHY_MSCR_TX_DELAY;
  376. err = phy_write(phydev, MII_88E1121_PHY_MSCR_REG, mscr);
  377. if (err < 0)
  378. return err;
  379. }
  380. phy_write(phydev, MII_MARVELL_PHY_PAGE, oldpage);
  381. err = phy_write(phydev, MII_BMCR, BMCR_RESET);
  382. if (err < 0)
  383. return err;
  384. err = phy_write(phydev, MII_M1011_PHY_SCR,
  385. MII_M1011_PHY_SCR_AUTO_CROSS);
  386. if (err < 0)
  387. return err;
  388. return genphy_config_aneg(phydev);
  389. }
  390. static int m88e1318_config_aneg(struct phy_device *phydev)
  391. {
  392. int err, oldpage, mscr;
  393. oldpage = phy_read(phydev, MII_MARVELL_PHY_PAGE);
  394. err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
  395. MII_88E1121_PHY_MSCR_PAGE);
  396. if (err < 0)
  397. return err;
  398. mscr = phy_read(phydev, MII_88E1318S_PHY_MSCR1_REG);
  399. mscr |= MII_88E1318S_PHY_MSCR1_PAD_ODD;
  400. err = phy_write(phydev, MII_88E1318S_PHY_MSCR1_REG, mscr);
  401. if (err < 0)
  402. return err;
  403. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, oldpage);
  404. if (err < 0)
  405. return err;
  406. return m88e1121_config_aneg(phydev);
  407. }
  408. /**
  409. * ethtool_adv_to_fiber_adv_t
  410. * @ethadv: the ethtool advertisement settings
  411. *
  412. * A small helper function that translates ethtool advertisement
  413. * settings to phy autonegotiation advertisements for the
  414. * MII_ADV register for fiber link.
  415. */
  416. static inline u32 ethtool_adv_to_fiber_adv_t(u32 ethadv)
  417. {
  418. u32 result = 0;
  419. if (ethadv & ADVERTISED_1000baseT_Half)
  420. result |= ADVERTISE_FIBER_1000HALF;
  421. if (ethadv & ADVERTISED_1000baseT_Full)
  422. result |= ADVERTISE_FIBER_1000FULL;
  423. if ((ethadv & ADVERTISE_PAUSE_ASYM) && (ethadv & ADVERTISE_PAUSE_CAP))
  424. result |= LPA_PAUSE_ASYM_FIBER;
  425. else if (ethadv & ADVERTISE_PAUSE_CAP)
  426. result |= (ADVERTISE_PAUSE_FIBER
  427. & (~ADVERTISE_PAUSE_ASYM_FIBER));
  428. return result;
  429. }
  430. /**
  431. * marvell_config_aneg_fiber - restart auto-negotiation or write BMCR
  432. * @phydev: target phy_device struct
  433. *
  434. * Description: If auto-negotiation is enabled, we configure the
  435. * advertising, and then restart auto-negotiation. If it is not
  436. * enabled, then we write the BMCR. Adapted for fiber link in
  437. * some Marvell's devices.
  438. */
  439. static int marvell_config_aneg_fiber(struct phy_device *phydev)
  440. {
  441. int changed = 0;
  442. int err;
  443. int adv, oldadv;
  444. u32 advertise;
  445. if (phydev->autoneg != AUTONEG_ENABLE)
  446. return genphy_setup_forced(phydev);
  447. /* Only allow advertising what this PHY supports */
  448. phydev->advertising &= phydev->supported;
  449. advertise = phydev->advertising;
  450. /* Setup fiber advertisement */
  451. adv = phy_read(phydev, MII_ADVERTISE);
  452. if (adv < 0)
  453. return adv;
  454. oldadv = adv;
  455. adv &= ~(ADVERTISE_FIBER_1000HALF | ADVERTISE_FIBER_1000FULL
  456. | LPA_PAUSE_FIBER);
  457. adv |= ethtool_adv_to_fiber_adv_t(advertise);
  458. if (adv != oldadv) {
  459. err = phy_write(phydev, MII_ADVERTISE, adv);
  460. if (err < 0)
  461. return err;
  462. changed = 1;
  463. }
  464. if (changed == 0) {
  465. /* Advertisement hasn't changed, but maybe aneg was never on to
  466. * begin with? Or maybe phy was isolated?
  467. */
  468. int ctl = phy_read(phydev, MII_BMCR);
  469. if (ctl < 0)
  470. return ctl;
  471. if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
  472. changed = 1; /* do restart aneg */
  473. }
  474. /* Only restart aneg if we are advertising something different
  475. * than we were before.
  476. */
  477. if (changed > 0)
  478. changed = genphy_restart_aneg(phydev);
  479. return changed;
  480. }
  481. static int m88e1510_config_aneg(struct phy_device *phydev)
  482. {
  483. int err;
  484. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_COPPER);
  485. if (err < 0)
  486. goto error;
  487. /* Configure the copper link first */
  488. err = m88e1318_config_aneg(phydev);
  489. if (err < 0)
  490. goto error;
  491. /* Then the fiber link */
  492. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_FIBER);
  493. if (err < 0)
  494. goto error;
  495. err = marvell_config_aneg_fiber(phydev);
  496. if (err < 0)
  497. goto error;
  498. return phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_COPPER);
  499. error:
  500. phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_COPPER);
  501. return err;
  502. }
  503. static int marvell_config_init(struct phy_device *phydev)
  504. {
  505. /* Set registers from marvell,reg-init DT property */
  506. return marvell_of_reg_init(phydev);
  507. }
  508. static int m88e1116r_config_init(struct phy_device *phydev)
  509. {
  510. int temp;
  511. int err;
  512. temp = phy_read(phydev, MII_BMCR);
  513. temp |= BMCR_RESET;
  514. err = phy_write(phydev, MII_BMCR, temp);
  515. if (err < 0)
  516. return err;
  517. mdelay(500);
  518. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0);
  519. if (err < 0)
  520. return err;
  521. temp = phy_read(phydev, MII_M1011_PHY_SCR);
  522. temp |= (7 << 12); /* max number of gigabit attempts */
  523. temp |= (1 << 11); /* enable downshift */
  524. temp |= MII_M1011_PHY_SCR_AUTO_CROSS;
  525. err = phy_write(phydev, MII_M1011_PHY_SCR, temp);
  526. if (err < 0)
  527. return err;
  528. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 2);
  529. if (err < 0)
  530. return err;
  531. temp = phy_read(phydev, MII_M1116R_CONTROL_REG_MAC);
  532. temp |= (1 << 5);
  533. temp |= (1 << 4);
  534. err = phy_write(phydev, MII_M1116R_CONTROL_REG_MAC, temp);
  535. if (err < 0)
  536. return err;
  537. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0);
  538. if (err < 0)
  539. return err;
  540. temp = phy_read(phydev, MII_BMCR);
  541. temp |= BMCR_RESET;
  542. err = phy_write(phydev, MII_BMCR, temp);
  543. if (err < 0)
  544. return err;
  545. mdelay(500);
  546. return marvell_config_init(phydev);
  547. }
  548. static int m88e3016_config_init(struct phy_device *phydev)
  549. {
  550. int reg;
  551. /* Enable Scrambler and Auto-Crossover */
  552. reg = phy_read(phydev, MII_88E3016_PHY_SPEC_CTRL);
  553. if (reg < 0)
  554. return reg;
  555. reg &= ~MII_88E3016_DISABLE_SCRAMBLER;
  556. reg |= MII_88E3016_AUTO_MDIX_CROSSOVER;
  557. reg = phy_write(phydev, MII_88E3016_PHY_SPEC_CTRL, reg);
  558. if (reg < 0)
  559. return reg;
  560. return marvell_config_init(phydev);
  561. }
  562. static int m88e1111_config_init(struct phy_device *phydev)
  563. {
  564. int err;
  565. int temp;
  566. if (phy_interface_is_rgmii(phydev)) {
  567. temp = phy_read(phydev, MII_M1111_PHY_EXT_CR);
  568. if (temp < 0)
  569. return temp;
  570. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
  571. temp |= (MII_M1111_RX_DELAY | MII_M1111_TX_DELAY);
  572. } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
  573. temp &= ~MII_M1111_TX_DELAY;
  574. temp |= MII_M1111_RX_DELAY;
  575. } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
  576. temp &= ~MII_M1111_RX_DELAY;
  577. temp |= MII_M1111_TX_DELAY;
  578. }
  579. err = phy_write(phydev, MII_M1111_PHY_EXT_CR, temp);
  580. if (err < 0)
  581. return err;
  582. temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
  583. if (temp < 0)
  584. return temp;
  585. temp &= ~(MII_M1111_HWCFG_MODE_MASK);
  586. if (temp & MII_M1111_HWCFG_FIBER_COPPER_RES)
  587. temp |= MII_M1111_HWCFG_MODE_FIBER_RGMII;
  588. else
  589. temp |= MII_M1111_HWCFG_MODE_COPPER_RGMII;
  590. err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
  591. if (err < 0)
  592. return err;
  593. }
  594. if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
  595. temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
  596. if (temp < 0)
  597. return temp;
  598. temp &= ~(MII_M1111_HWCFG_MODE_MASK);
  599. temp |= MII_M1111_HWCFG_MODE_SGMII_NO_CLK;
  600. temp |= MII_M1111_HWCFG_FIBER_COPPER_AUTO;
  601. err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
  602. if (err < 0)
  603. return err;
  604. /* make sure copper is selected */
  605. err = phy_read(phydev, MII_M1145_PHY_EXT_ADDR_PAGE);
  606. if (err < 0)
  607. return err;
  608. err = phy_write(phydev, MII_M1145_PHY_EXT_ADDR_PAGE,
  609. err & (~0xff));
  610. if (err < 0)
  611. return err;
  612. }
  613. if (phydev->interface == PHY_INTERFACE_MODE_RTBI) {
  614. temp = phy_read(phydev, MII_M1111_PHY_EXT_CR);
  615. if (temp < 0)
  616. return temp;
  617. temp |= (MII_M1111_RX_DELAY | MII_M1111_TX_DELAY);
  618. err = phy_write(phydev, MII_M1111_PHY_EXT_CR, temp);
  619. if (err < 0)
  620. return err;
  621. temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
  622. if (temp < 0)
  623. return temp;
  624. temp &= ~(MII_M1111_HWCFG_MODE_MASK | MII_M1111_HWCFG_FIBER_COPPER_RES);
  625. temp |= 0x7 | MII_M1111_HWCFG_FIBER_COPPER_AUTO;
  626. err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
  627. if (err < 0)
  628. return err;
  629. /* soft reset */
  630. err = phy_write(phydev, MII_BMCR, BMCR_RESET);
  631. if (err < 0)
  632. return err;
  633. do
  634. temp = phy_read(phydev, MII_BMCR);
  635. while (temp & BMCR_RESET);
  636. temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
  637. if (temp < 0)
  638. return temp;
  639. temp &= ~(MII_M1111_HWCFG_MODE_MASK | MII_M1111_HWCFG_FIBER_COPPER_RES);
  640. temp |= MII_M1111_HWCFG_MODE_COPPER_RTBI | MII_M1111_HWCFG_FIBER_COPPER_AUTO;
  641. err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
  642. if (err < 0)
  643. return err;
  644. }
  645. err = marvell_of_reg_init(phydev);
  646. if (err < 0)
  647. return err;
  648. return phy_write(phydev, MII_BMCR, BMCR_RESET);
  649. }
  650. static int m88e1121_config_init(struct phy_device *phydev)
  651. {
  652. int err, oldpage;
  653. oldpage = phy_read(phydev, MII_MARVELL_PHY_PAGE);
  654. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_88E1121_PHY_LED_PAGE);
  655. if (err < 0)
  656. return err;
  657. /* Default PHY LED config: LED[0] .. Link, LED[1] .. Activity */
  658. err = phy_write(phydev, MII_88E1121_PHY_LED_CTRL,
  659. MII_88E1121_PHY_LED_DEF);
  660. if (err < 0)
  661. return err;
  662. phy_write(phydev, MII_MARVELL_PHY_PAGE, oldpage);
  663. /* Set marvell,reg-init configuration from device tree */
  664. return marvell_config_init(phydev);
  665. }
  666. static int m88e1510_config_init(struct phy_device *phydev)
  667. {
  668. int err;
  669. int temp;
  670. /* SGMII-to-Copper mode initialization */
  671. if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
  672. /* Select page 18 */
  673. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 18);
  674. if (err < 0)
  675. return err;
  676. /* In reg 20, write MODE[2:0] = 0x1 (SGMII to Copper) */
  677. temp = phy_read(phydev, MII_88E1510_GEN_CTRL_REG_1);
  678. temp &= ~MII_88E1510_GEN_CTRL_REG_1_MODE_MASK;
  679. temp |= MII_88E1510_GEN_CTRL_REG_1_MODE_SGMII;
  680. err = phy_write(phydev, MII_88E1510_GEN_CTRL_REG_1, temp);
  681. if (err < 0)
  682. return err;
  683. /* PHY reset is necessary after changing MODE[2:0] */
  684. temp |= MII_88E1510_GEN_CTRL_REG_1_RESET;
  685. err = phy_write(phydev, MII_88E1510_GEN_CTRL_REG_1, temp);
  686. if (err < 0)
  687. return err;
  688. /* Reset page selection */
  689. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0);
  690. if (err < 0)
  691. return err;
  692. }
  693. return m88e1121_config_init(phydev);
  694. }
  695. static int m88e1118_config_aneg(struct phy_device *phydev)
  696. {
  697. int err;
  698. err = phy_write(phydev, MII_BMCR, BMCR_RESET);
  699. if (err < 0)
  700. return err;
  701. err = phy_write(phydev, MII_M1011_PHY_SCR,
  702. MII_M1011_PHY_SCR_AUTO_CROSS);
  703. if (err < 0)
  704. return err;
  705. err = genphy_config_aneg(phydev);
  706. return 0;
  707. }
  708. static int m88e1118_config_init(struct phy_device *phydev)
  709. {
  710. int err;
  711. /* Change address */
  712. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0002);
  713. if (err < 0)
  714. return err;
  715. /* Enable 1000 Mbit */
  716. err = phy_write(phydev, 0x15, 0x1070);
  717. if (err < 0)
  718. return err;
  719. /* Change address */
  720. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0003);
  721. if (err < 0)
  722. return err;
  723. /* Adjust LED Control */
  724. if (phydev->dev_flags & MARVELL_PHY_M1118_DNS323_LEDS)
  725. err = phy_write(phydev, 0x10, 0x1100);
  726. else
  727. err = phy_write(phydev, 0x10, 0x021e);
  728. if (err < 0)
  729. return err;
  730. err = marvell_of_reg_init(phydev);
  731. if (err < 0)
  732. return err;
  733. /* Reset address */
  734. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0);
  735. if (err < 0)
  736. return err;
  737. return phy_write(phydev, MII_BMCR, BMCR_RESET);
  738. }
  739. static int m88e1149_config_init(struct phy_device *phydev)
  740. {
  741. int err;
  742. /* Change address */
  743. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0002);
  744. if (err < 0)
  745. return err;
  746. /* Enable 1000 Mbit */
  747. err = phy_write(phydev, 0x15, 0x1048);
  748. if (err < 0)
  749. return err;
  750. err = marvell_of_reg_init(phydev);
  751. if (err < 0)
  752. return err;
  753. /* Reset address */
  754. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x0);
  755. if (err < 0)
  756. return err;
  757. return phy_write(phydev, MII_BMCR, BMCR_RESET);
  758. }
  759. static int m88e1145_config_init(struct phy_device *phydev)
  760. {
  761. int err;
  762. int temp;
  763. /* Take care of errata E0 & E1 */
  764. err = phy_write(phydev, 0x1d, 0x001b);
  765. if (err < 0)
  766. return err;
  767. err = phy_write(phydev, 0x1e, 0x418f);
  768. if (err < 0)
  769. return err;
  770. err = phy_write(phydev, 0x1d, 0x0016);
  771. if (err < 0)
  772. return err;
  773. err = phy_write(phydev, 0x1e, 0xa2da);
  774. if (err < 0)
  775. return err;
  776. if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
  777. int temp = phy_read(phydev, MII_M1145_PHY_EXT_CR);
  778. if (temp < 0)
  779. return temp;
  780. temp |= (MII_M1145_RGMII_RX_DELAY | MII_M1145_RGMII_TX_DELAY);
  781. err = phy_write(phydev, MII_M1145_PHY_EXT_CR, temp);
  782. if (err < 0)
  783. return err;
  784. if (phydev->dev_flags & MARVELL_PHY_M1145_FLAGS_RESISTANCE) {
  785. err = phy_write(phydev, 0x1d, 0x0012);
  786. if (err < 0)
  787. return err;
  788. temp = phy_read(phydev, 0x1e);
  789. if (temp < 0)
  790. return temp;
  791. temp &= 0xf03f;
  792. temp |= 2 << 9; /* 36 ohm */
  793. temp |= 2 << 6; /* 39 ohm */
  794. err = phy_write(phydev, 0x1e, temp);
  795. if (err < 0)
  796. return err;
  797. err = phy_write(phydev, 0x1d, 0x3);
  798. if (err < 0)
  799. return err;
  800. err = phy_write(phydev, 0x1e, 0x8000);
  801. if (err < 0)
  802. return err;
  803. }
  804. }
  805. if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
  806. temp = phy_read(phydev, MII_M1145_PHY_EXT_SR);
  807. if (temp < 0)
  808. return temp;
  809. temp &= ~MII_M1145_HWCFG_MODE_MASK;
  810. temp |= MII_M1145_HWCFG_MODE_SGMII_NO_CLK;
  811. temp |= MII_M1145_HWCFG_FIBER_COPPER_AUTO;
  812. err = phy_write(phydev, MII_M1145_PHY_EXT_SR, temp);
  813. if (err < 0)
  814. return err;
  815. }
  816. err = marvell_of_reg_init(phydev);
  817. if (err < 0)
  818. return err;
  819. return 0;
  820. }
  821. /**
  822. * fiber_lpa_to_ethtool_lpa_t
  823. * @lpa: value of the MII_LPA register for fiber link
  824. *
  825. * A small helper function that translates MII_LPA
  826. * bits to ethtool LP advertisement settings.
  827. */
  828. static u32 fiber_lpa_to_ethtool_lpa_t(u32 lpa)
  829. {
  830. u32 result = 0;
  831. if (lpa & LPA_FIBER_1000HALF)
  832. result |= ADVERTISED_1000baseT_Half;
  833. if (lpa & LPA_FIBER_1000FULL)
  834. result |= ADVERTISED_1000baseT_Full;
  835. return result;
  836. }
  837. /**
  838. * marvell_update_link - update link status in real time in @phydev
  839. * @phydev: target phy_device struct
  840. *
  841. * Description: Update the value in phydev->link to reflect the
  842. * current link value.
  843. */
  844. static int marvell_update_link(struct phy_device *phydev, int fiber)
  845. {
  846. int status;
  847. /* Use the generic register for copper link, or specific
  848. * register for fiber case */
  849. if (fiber) {
  850. status = phy_read(phydev, MII_M1011_PHY_STATUS);
  851. if (status < 0)
  852. return status;
  853. if ((status & REGISTER_LINK_STATUS) == 0)
  854. phydev->link = 0;
  855. else
  856. phydev->link = 1;
  857. } else {
  858. return genphy_update_link(phydev);
  859. }
  860. return 0;
  861. }
  862. /* marvell_read_status_page
  863. *
  864. * Description:
  865. * Check the link, then figure out the current state
  866. * by comparing what we advertise with what the link partner
  867. * advertises. Start by checking the gigabit possibilities,
  868. * then move on to 10/100.
  869. */
  870. static int marvell_read_status_page(struct phy_device *phydev, int page)
  871. {
  872. int adv;
  873. int err;
  874. int lpa;
  875. int lpagb;
  876. int status = 0;
  877. int fiber;
  878. /* Detect and update the link, but return if there
  879. * was an error */
  880. if (page == MII_M1111_FIBER)
  881. fiber = 1;
  882. else
  883. fiber = 0;
  884. err = marvell_update_link(phydev, fiber);
  885. if (err)
  886. return err;
  887. if (AUTONEG_ENABLE == phydev->autoneg) {
  888. status = phy_read(phydev, MII_M1011_PHY_STATUS);
  889. if (status < 0)
  890. return status;
  891. lpa = phy_read(phydev, MII_LPA);
  892. if (lpa < 0)
  893. return lpa;
  894. lpagb = phy_read(phydev, MII_STAT1000);
  895. if (lpagb < 0)
  896. return lpagb;
  897. adv = phy_read(phydev, MII_ADVERTISE);
  898. if (adv < 0)
  899. return adv;
  900. if (status & MII_M1011_PHY_STATUS_FULLDUPLEX)
  901. phydev->duplex = DUPLEX_FULL;
  902. else
  903. phydev->duplex = DUPLEX_HALF;
  904. status = status & MII_M1011_PHY_STATUS_SPD_MASK;
  905. phydev->pause = phydev->asym_pause = 0;
  906. switch (status) {
  907. case MII_M1011_PHY_STATUS_1000:
  908. phydev->speed = SPEED_1000;
  909. break;
  910. case MII_M1011_PHY_STATUS_100:
  911. phydev->speed = SPEED_100;
  912. break;
  913. default:
  914. phydev->speed = SPEED_10;
  915. break;
  916. }
  917. if (!fiber) {
  918. phydev->lp_advertising = mii_stat1000_to_ethtool_lpa_t(lpagb) |
  919. mii_lpa_to_ethtool_lpa_t(lpa);
  920. if (phydev->duplex == DUPLEX_FULL) {
  921. phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
  922. phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
  923. }
  924. } else {
  925. /* The fiber link is only 1000M capable */
  926. phydev->lp_advertising = fiber_lpa_to_ethtool_lpa_t(lpa);
  927. if (phydev->duplex == DUPLEX_FULL) {
  928. if (!(lpa & LPA_PAUSE_FIBER)) {
  929. phydev->pause = 0;
  930. phydev->asym_pause = 0;
  931. } else if ((lpa & LPA_PAUSE_ASYM_FIBER)) {
  932. phydev->pause = 1;
  933. phydev->asym_pause = 1;
  934. } else {
  935. phydev->pause = 1;
  936. phydev->asym_pause = 0;
  937. }
  938. }
  939. }
  940. } else {
  941. int bmcr = phy_read(phydev, MII_BMCR);
  942. if (bmcr < 0)
  943. return bmcr;
  944. if (bmcr & BMCR_FULLDPLX)
  945. phydev->duplex = DUPLEX_FULL;
  946. else
  947. phydev->duplex = DUPLEX_HALF;
  948. if (bmcr & BMCR_SPEED1000)
  949. phydev->speed = SPEED_1000;
  950. else if (bmcr & BMCR_SPEED100)
  951. phydev->speed = SPEED_100;
  952. else
  953. phydev->speed = SPEED_10;
  954. phydev->pause = phydev->asym_pause = 0;
  955. phydev->lp_advertising = 0;
  956. }
  957. return 0;
  958. }
  959. /* marvell_read_status
  960. *
  961. * Some Marvell's phys have two modes: fiber and copper.
  962. * Both need status checked.
  963. * Description:
  964. * First, check the fiber link and status.
  965. * If the fiber link is down, check the copper link and status which
  966. * will be the default value if both link are down.
  967. */
  968. static int marvell_read_status(struct phy_device *phydev)
  969. {
  970. int err;
  971. /* Check the fiber mode first */
  972. if (phydev->supported & SUPPORTED_FIBRE &&
  973. phydev->interface != PHY_INTERFACE_MODE_SGMII) {
  974. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_FIBER);
  975. if (err < 0)
  976. goto error;
  977. err = marvell_read_status_page(phydev, MII_M1111_FIBER);
  978. if (err < 0)
  979. goto error;
  980. /* If the fiber link is up, it is the selected and used link.
  981. * In this case, we need to stay in the fiber page.
  982. * Please to be careful about that, avoid to restore Copper page
  983. * in other functions which could break the behaviour
  984. * for some fiber phy like 88E1512.
  985. * */
  986. if (phydev->link)
  987. return 0;
  988. /* If fiber link is down, check and save copper mode state */
  989. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_COPPER);
  990. if (err < 0)
  991. goto error;
  992. }
  993. return marvell_read_status_page(phydev, MII_M1111_COPPER);
  994. error:
  995. phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_COPPER);
  996. return err;
  997. }
  998. /* marvell_suspend
  999. *
  1000. * Some Marvell's phys have two modes: fiber and copper.
  1001. * Both need to be suspended
  1002. */
  1003. static int marvell_suspend(struct phy_device *phydev)
  1004. {
  1005. int err;
  1006. /* Suspend the fiber mode first */
  1007. if (!(phydev->supported & SUPPORTED_FIBRE)) {
  1008. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_FIBER);
  1009. if (err < 0)
  1010. goto error;
  1011. /* With the page set, use the generic suspend */
  1012. err = genphy_suspend(phydev);
  1013. if (err < 0)
  1014. goto error;
  1015. /* Then, the copper link */
  1016. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_COPPER);
  1017. if (err < 0)
  1018. goto error;
  1019. }
  1020. /* With the page set, use the generic suspend */
  1021. return genphy_suspend(phydev);
  1022. error:
  1023. phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_COPPER);
  1024. return err;
  1025. }
  1026. /* marvell_resume
  1027. *
  1028. * Some Marvell's phys have two modes: fiber and copper.
  1029. * Both need to be resumed
  1030. */
  1031. static int marvell_resume(struct phy_device *phydev)
  1032. {
  1033. int err;
  1034. /* Resume the fiber mode first */
  1035. if (!(phydev->supported & SUPPORTED_FIBRE)) {
  1036. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_FIBER);
  1037. if (err < 0)
  1038. goto error;
  1039. /* With the page set, use the generic resume */
  1040. err = genphy_resume(phydev);
  1041. if (err < 0)
  1042. goto error;
  1043. /* Then, the copper link */
  1044. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_COPPER);
  1045. if (err < 0)
  1046. goto error;
  1047. }
  1048. /* With the page set, use the generic resume */
  1049. return genphy_resume(phydev);
  1050. error:
  1051. phy_write(phydev, MII_MARVELL_PHY_PAGE, MII_M1111_COPPER);
  1052. return err;
  1053. }
  1054. static int marvell_aneg_done(struct phy_device *phydev)
  1055. {
  1056. int retval = phy_read(phydev, MII_M1011_PHY_STATUS);
  1057. return (retval < 0) ? retval : (retval & MII_M1011_PHY_STATUS_RESOLVED);
  1058. }
  1059. static int m88e1121_did_interrupt(struct phy_device *phydev)
  1060. {
  1061. int imask;
  1062. imask = phy_read(phydev, MII_M1011_IEVENT);
  1063. if (imask & MII_M1011_IMASK_INIT)
  1064. return 1;
  1065. return 0;
  1066. }
  1067. static void m88e1318_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1068. {
  1069. wol->supported = WAKE_MAGIC;
  1070. wol->wolopts = 0;
  1071. if (phy_write(phydev, MII_MARVELL_PHY_PAGE,
  1072. MII_88E1318S_PHY_WOL_PAGE) < 0)
  1073. return;
  1074. if (phy_read(phydev, MII_88E1318S_PHY_WOL_CTRL) &
  1075. MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE)
  1076. wol->wolopts |= WAKE_MAGIC;
  1077. if (phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x00) < 0)
  1078. return;
  1079. }
  1080. static int m88e1318_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
  1081. {
  1082. int err, oldpage, temp;
  1083. oldpage = phy_read(phydev, MII_MARVELL_PHY_PAGE);
  1084. if (wol->wolopts & WAKE_MAGIC) {
  1085. /* Explicitly switch to page 0x00, just to be sure */
  1086. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, 0x00);
  1087. if (err < 0)
  1088. return err;
  1089. /* Enable the WOL interrupt */
  1090. temp = phy_read(phydev, MII_88E1318S_PHY_CSIER);
  1091. temp |= MII_88E1318S_PHY_CSIER_WOL_EIE;
  1092. err = phy_write(phydev, MII_88E1318S_PHY_CSIER, temp);
  1093. if (err < 0)
  1094. return err;
  1095. err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
  1096. MII_88E1318S_PHY_LED_PAGE);
  1097. if (err < 0)
  1098. return err;
  1099. /* Setup LED[2] as interrupt pin (active low) */
  1100. temp = phy_read(phydev, MII_88E1318S_PHY_LED_TCR);
  1101. temp &= ~MII_88E1318S_PHY_LED_TCR_FORCE_INT;
  1102. temp |= MII_88E1318S_PHY_LED_TCR_INTn_ENABLE;
  1103. temp |= MII_88E1318S_PHY_LED_TCR_INT_ACTIVE_LOW;
  1104. err = phy_write(phydev, MII_88E1318S_PHY_LED_TCR, temp);
  1105. if (err < 0)
  1106. return err;
  1107. err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
  1108. MII_88E1318S_PHY_WOL_PAGE);
  1109. if (err < 0)
  1110. return err;
  1111. /* Store the device address for the magic packet */
  1112. err = phy_write(phydev, MII_88E1318S_PHY_MAGIC_PACKET_WORD2,
  1113. ((phydev->attached_dev->dev_addr[5] << 8) |
  1114. phydev->attached_dev->dev_addr[4]));
  1115. if (err < 0)
  1116. return err;
  1117. err = phy_write(phydev, MII_88E1318S_PHY_MAGIC_PACKET_WORD1,
  1118. ((phydev->attached_dev->dev_addr[3] << 8) |
  1119. phydev->attached_dev->dev_addr[2]));
  1120. if (err < 0)
  1121. return err;
  1122. err = phy_write(phydev, MII_88E1318S_PHY_MAGIC_PACKET_WORD0,
  1123. ((phydev->attached_dev->dev_addr[1] << 8) |
  1124. phydev->attached_dev->dev_addr[0]));
  1125. if (err < 0)
  1126. return err;
  1127. /* Clear WOL status and enable magic packet matching */
  1128. temp = phy_read(phydev, MII_88E1318S_PHY_WOL_CTRL);
  1129. temp |= MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS;
  1130. temp |= MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE;
  1131. err = phy_write(phydev, MII_88E1318S_PHY_WOL_CTRL, temp);
  1132. if (err < 0)
  1133. return err;
  1134. } else {
  1135. err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
  1136. MII_88E1318S_PHY_WOL_PAGE);
  1137. if (err < 0)
  1138. return err;
  1139. /* Clear WOL status and disable magic packet matching */
  1140. temp = phy_read(phydev, MII_88E1318S_PHY_WOL_CTRL);
  1141. temp |= MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS;
  1142. temp &= ~MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE;
  1143. err = phy_write(phydev, MII_88E1318S_PHY_WOL_CTRL, temp);
  1144. if (err < 0)
  1145. return err;
  1146. }
  1147. err = phy_write(phydev, MII_MARVELL_PHY_PAGE, oldpage);
  1148. if (err < 0)
  1149. return err;
  1150. return 0;
  1151. }
  1152. static int marvell_get_sset_count(struct phy_device *phydev)
  1153. {
  1154. if (phydev->supported & SUPPORTED_FIBRE)
  1155. return ARRAY_SIZE(marvell_hw_stats);
  1156. else
  1157. return ARRAY_SIZE(marvell_hw_stats) - NB_FIBER_STATS;
  1158. }
  1159. static void marvell_get_strings(struct phy_device *phydev, u8 *data)
  1160. {
  1161. int i;
  1162. for (i = 0; i < ARRAY_SIZE(marvell_hw_stats); i++) {
  1163. memcpy(data + i * ETH_GSTRING_LEN,
  1164. marvell_hw_stats[i].string, ETH_GSTRING_LEN);
  1165. }
  1166. }
  1167. #ifndef UINT64_MAX
  1168. #define UINT64_MAX (u64)(~((u64)0))
  1169. #endif
  1170. static u64 marvell_get_stat(struct phy_device *phydev, int i)
  1171. {
  1172. struct marvell_hw_stat stat = marvell_hw_stats[i];
  1173. struct marvell_priv *priv = phydev->priv;
  1174. int err, oldpage, val;
  1175. u64 ret;
  1176. oldpage = phy_read(phydev, MII_MARVELL_PHY_PAGE);
  1177. err = phy_write(phydev, MII_MARVELL_PHY_PAGE,
  1178. stat.page);
  1179. if (err < 0)
  1180. return UINT64_MAX;
  1181. val = phy_read(phydev, stat.reg);
  1182. if (val < 0) {
  1183. ret = UINT64_MAX;
  1184. } else {
  1185. val = val & ((1 << stat.bits) - 1);
  1186. priv->stats[i] += val;
  1187. ret = priv->stats[i];
  1188. }
  1189. phy_write(phydev, MII_MARVELL_PHY_PAGE, oldpage);
  1190. return ret;
  1191. }
  1192. static void marvell_get_stats(struct phy_device *phydev,
  1193. struct ethtool_stats *stats, u64 *data)
  1194. {
  1195. int i;
  1196. for (i = 0; i < ARRAY_SIZE(marvell_hw_stats); i++)
  1197. data[i] = marvell_get_stat(phydev, i);
  1198. }
  1199. static int marvell_probe(struct phy_device *phydev)
  1200. {
  1201. struct marvell_priv *priv;
  1202. priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
  1203. if (!priv)
  1204. return -ENOMEM;
  1205. phydev->priv = priv;
  1206. return 0;
  1207. }
  1208. static struct phy_driver marvell_drivers[] = {
  1209. {
  1210. .phy_id = MARVELL_PHY_ID_88E1101,
  1211. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1212. .name = "Marvell 88E1101",
  1213. .features = PHY_GBIT_FEATURES,
  1214. .probe = marvell_probe,
  1215. .flags = PHY_HAS_INTERRUPT,
  1216. .config_init = &marvell_config_init,
  1217. .config_aneg = &m88e1101_config_aneg,
  1218. .read_status = &genphy_read_status,
  1219. .ack_interrupt = &marvell_ack_interrupt,
  1220. .config_intr = &marvell_config_intr,
  1221. .resume = &genphy_resume,
  1222. .suspend = &genphy_suspend,
  1223. .get_sset_count = marvell_get_sset_count,
  1224. .get_strings = marvell_get_strings,
  1225. .get_stats = marvell_get_stats,
  1226. },
  1227. {
  1228. .phy_id = MARVELL_PHY_ID_88E1112,
  1229. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1230. .name = "Marvell 88E1112",
  1231. .features = PHY_GBIT_FEATURES,
  1232. .flags = PHY_HAS_INTERRUPT,
  1233. .probe = marvell_probe,
  1234. .config_init = &m88e1111_config_init,
  1235. .config_aneg = &marvell_config_aneg,
  1236. .read_status = &genphy_read_status,
  1237. .ack_interrupt = &marvell_ack_interrupt,
  1238. .config_intr = &marvell_config_intr,
  1239. .resume = &genphy_resume,
  1240. .suspend = &genphy_suspend,
  1241. .get_sset_count = marvell_get_sset_count,
  1242. .get_strings = marvell_get_strings,
  1243. .get_stats = marvell_get_stats,
  1244. },
  1245. {
  1246. .phy_id = MARVELL_PHY_ID_88E1111,
  1247. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1248. .name = "Marvell 88E1111",
  1249. .features = PHY_GBIT_FEATURES,
  1250. .flags = PHY_HAS_INTERRUPT,
  1251. .probe = marvell_probe,
  1252. .config_init = &m88e1111_config_init,
  1253. .config_aneg = &m88e1111_config_aneg,
  1254. .read_status = &marvell_read_status,
  1255. .ack_interrupt = &marvell_ack_interrupt,
  1256. .config_intr = &marvell_config_intr,
  1257. .resume = &genphy_resume,
  1258. .suspend = &genphy_suspend,
  1259. .get_sset_count = marvell_get_sset_count,
  1260. .get_strings = marvell_get_strings,
  1261. .get_stats = marvell_get_stats,
  1262. },
  1263. {
  1264. .phy_id = MARVELL_PHY_ID_88E1118,
  1265. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1266. .name = "Marvell 88E1118",
  1267. .features = PHY_GBIT_FEATURES,
  1268. .flags = PHY_HAS_INTERRUPT,
  1269. .probe = marvell_probe,
  1270. .config_init = &m88e1118_config_init,
  1271. .config_aneg = &m88e1118_config_aneg,
  1272. .read_status = &genphy_read_status,
  1273. .ack_interrupt = &marvell_ack_interrupt,
  1274. .config_intr = &marvell_config_intr,
  1275. .resume = &genphy_resume,
  1276. .suspend = &genphy_suspend,
  1277. .get_sset_count = marvell_get_sset_count,
  1278. .get_strings = marvell_get_strings,
  1279. .get_stats = marvell_get_stats,
  1280. },
  1281. {
  1282. .phy_id = MARVELL_PHY_ID_88E1121R,
  1283. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1284. .name = "Marvell 88E1121R",
  1285. .features = PHY_GBIT_FEATURES,
  1286. .flags = PHY_HAS_INTERRUPT,
  1287. .probe = marvell_probe,
  1288. .config_init = &m88e1121_config_init,
  1289. .config_aneg = &m88e1121_config_aneg,
  1290. .read_status = &marvell_read_status,
  1291. .ack_interrupt = &marvell_ack_interrupt,
  1292. .config_intr = &marvell_config_intr,
  1293. .did_interrupt = &m88e1121_did_interrupt,
  1294. .resume = &genphy_resume,
  1295. .suspend = &genphy_suspend,
  1296. .get_sset_count = marvell_get_sset_count,
  1297. .get_strings = marvell_get_strings,
  1298. .get_stats = marvell_get_stats,
  1299. },
  1300. {
  1301. .phy_id = MARVELL_PHY_ID_88E1318S,
  1302. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1303. .name = "Marvell 88E1318S",
  1304. .features = PHY_GBIT_FEATURES,
  1305. .flags = PHY_HAS_INTERRUPT,
  1306. .probe = marvell_probe,
  1307. .config_init = &m88e1121_config_init,
  1308. .config_aneg = &m88e1318_config_aneg,
  1309. .read_status = &marvell_read_status,
  1310. .ack_interrupt = &marvell_ack_interrupt,
  1311. .config_intr = &marvell_config_intr,
  1312. .did_interrupt = &m88e1121_did_interrupt,
  1313. .get_wol = &m88e1318_get_wol,
  1314. .set_wol = &m88e1318_set_wol,
  1315. .resume = &genphy_resume,
  1316. .suspend = &genphy_suspend,
  1317. .get_sset_count = marvell_get_sset_count,
  1318. .get_strings = marvell_get_strings,
  1319. .get_stats = marvell_get_stats,
  1320. },
  1321. {
  1322. .phy_id = MARVELL_PHY_ID_88E1145,
  1323. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1324. .name = "Marvell 88E1145",
  1325. .features = PHY_GBIT_FEATURES,
  1326. .flags = PHY_HAS_INTERRUPT,
  1327. .probe = marvell_probe,
  1328. .config_init = &m88e1145_config_init,
  1329. .config_aneg = &marvell_config_aneg,
  1330. .read_status = &genphy_read_status,
  1331. .ack_interrupt = &marvell_ack_interrupt,
  1332. .config_intr = &marvell_config_intr,
  1333. .resume = &genphy_resume,
  1334. .suspend = &genphy_suspend,
  1335. .get_sset_count = marvell_get_sset_count,
  1336. .get_strings = marvell_get_strings,
  1337. .get_stats = marvell_get_stats,
  1338. },
  1339. {
  1340. .phy_id = MARVELL_PHY_ID_88E1149R,
  1341. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1342. .name = "Marvell 88E1149R",
  1343. .features = PHY_GBIT_FEATURES,
  1344. .flags = PHY_HAS_INTERRUPT,
  1345. .probe = marvell_probe,
  1346. .config_init = &m88e1149_config_init,
  1347. .config_aneg = &m88e1118_config_aneg,
  1348. .read_status = &genphy_read_status,
  1349. .ack_interrupt = &marvell_ack_interrupt,
  1350. .config_intr = &marvell_config_intr,
  1351. .resume = &genphy_resume,
  1352. .suspend = &genphy_suspend,
  1353. .get_sset_count = marvell_get_sset_count,
  1354. .get_strings = marvell_get_strings,
  1355. .get_stats = marvell_get_stats,
  1356. },
  1357. {
  1358. .phy_id = MARVELL_PHY_ID_88E1240,
  1359. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1360. .name = "Marvell 88E1240",
  1361. .features = PHY_GBIT_FEATURES,
  1362. .flags = PHY_HAS_INTERRUPT,
  1363. .probe = marvell_probe,
  1364. .config_init = &m88e1111_config_init,
  1365. .config_aneg = &marvell_config_aneg,
  1366. .read_status = &genphy_read_status,
  1367. .ack_interrupt = &marvell_ack_interrupt,
  1368. .config_intr = &marvell_config_intr,
  1369. .resume = &genphy_resume,
  1370. .suspend = &genphy_suspend,
  1371. .get_sset_count = marvell_get_sset_count,
  1372. .get_strings = marvell_get_strings,
  1373. .get_stats = marvell_get_stats,
  1374. },
  1375. {
  1376. .phy_id = MARVELL_PHY_ID_88E1116R,
  1377. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1378. .name = "Marvell 88E1116R",
  1379. .features = PHY_GBIT_FEATURES,
  1380. .flags = PHY_HAS_INTERRUPT,
  1381. .probe = marvell_probe,
  1382. .config_init = &m88e1116r_config_init,
  1383. .config_aneg = &genphy_config_aneg,
  1384. .read_status = &genphy_read_status,
  1385. .ack_interrupt = &marvell_ack_interrupt,
  1386. .config_intr = &marvell_config_intr,
  1387. .resume = &genphy_resume,
  1388. .suspend = &genphy_suspend,
  1389. .get_sset_count = marvell_get_sset_count,
  1390. .get_strings = marvell_get_strings,
  1391. .get_stats = marvell_get_stats,
  1392. },
  1393. {
  1394. .phy_id = MARVELL_PHY_ID_88E1510,
  1395. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1396. .name = "Marvell 88E1510",
  1397. .features = PHY_GBIT_FEATURES | SUPPORTED_FIBRE,
  1398. .flags = PHY_HAS_INTERRUPT,
  1399. .probe = marvell_probe,
  1400. .config_init = &m88e1510_config_init,
  1401. .config_aneg = &m88e1510_config_aneg,
  1402. .read_status = &marvell_read_status,
  1403. .ack_interrupt = &marvell_ack_interrupt,
  1404. .config_intr = &marvell_config_intr,
  1405. .did_interrupt = &m88e1121_did_interrupt,
  1406. .resume = &marvell_resume,
  1407. .suspend = &marvell_suspend,
  1408. .get_sset_count = marvell_get_sset_count,
  1409. .get_strings = marvell_get_strings,
  1410. .get_stats = marvell_get_stats,
  1411. },
  1412. {
  1413. .phy_id = MARVELL_PHY_ID_88E1540,
  1414. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1415. .name = "Marvell 88E1540",
  1416. .features = PHY_GBIT_FEATURES,
  1417. .flags = PHY_HAS_INTERRUPT,
  1418. .probe = marvell_probe,
  1419. .config_init = &marvell_config_init,
  1420. .config_aneg = &m88e1510_config_aneg,
  1421. .read_status = &marvell_read_status,
  1422. .ack_interrupt = &marvell_ack_interrupt,
  1423. .config_intr = &marvell_config_intr,
  1424. .did_interrupt = &m88e1121_did_interrupt,
  1425. .resume = &genphy_resume,
  1426. .suspend = &genphy_suspend,
  1427. .get_sset_count = marvell_get_sset_count,
  1428. .get_strings = marvell_get_strings,
  1429. .get_stats = marvell_get_stats,
  1430. },
  1431. {
  1432. .phy_id = MARVELL_PHY_ID_88E3016,
  1433. .phy_id_mask = MARVELL_PHY_ID_MASK,
  1434. .name = "Marvell 88E3016",
  1435. .features = PHY_BASIC_FEATURES,
  1436. .flags = PHY_HAS_INTERRUPT,
  1437. .probe = marvell_probe,
  1438. .config_aneg = &genphy_config_aneg,
  1439. .config_init = &m88e3016_config_init,
  1440. .aneg_done = &marvell_aneg_done,
  1441. .read_status = &marvell_read_status,
  1442. .ack_interrupt = &marvell_ack_interrupt,
  1443. .config_intr = &marvell_config_intr,
  1444. .did_interrupt = &m88e1121_did_interrupt,
  1445. .resume = &genphy_resume,
  1446. .suspend = &genphy_suspend,
  1447. .get_sset_count = marvell_get_sset_count,
  1448. .get_strings = marvell_get_strings,
  1449. .get_stats = marvell_get_stats,
  1450. },
  1451. };
  1452. module_phy_driver(marvell_drivers);
  1453. static struct mdio_device_id __maybe_unused marvell_tbl[] = {
  1454. { MARVELL_PHY_ID_88E1101, MARVELL_PHY_ID_MASK },
  1455. { MARVELL_PHY_ID_88E1112, MARVELL_PHY_ID_MASK },
  1456. { MARVELL_PHY_ID_88E1111, MARVELL_PHY_ID_MASK },
  1457. { MARVELL_PHY_ID_88E1118, MARVELL_PHY_ID_MASK },
  1458. { MARVELL_PHY_ID_88E1121R, MARVELL_PHY_ID_MASK },
  1459. { MARVELL_PHY_ID_88E1145, MARVELL_PHY_ID_MASK },
  1460. { MARVELL_PHY_ID_88E1149R, MARVELL_PHY_ID_MASK },
  1461. { MARVELL_PHY_ID_88E1240, MARVELL_PHY_ID_MASK },
  1462. { MARVELL_PHY_ID_88E1318S, MARVELL_PHY_ID_MASK },
  1463. { MARVELL_PHY_ID_88E1116R, MARVELL_PHY_ID_MASK },
  1464. { MARVELL_PHY_ID_88E1510, MARVELL_PHY_ID_MASK },
  1465. { MARVELL_PHY_ID_88E1540, MARVELL_PHY_ID_MASK },
  1466. { MARVELL_PHY_ID_88E3016, MARVELL_PHY_ID_MASK },
  1467. { }
  1468. };
  1469. MODULE_DEVICE_TABLE(mdio, marvell_tbl);