xgbe-mdio.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /*
  2. * AMD 10Gb Ethernet driver
  3. *
  4. * This file is available to you under your choice of the following two
  5. * licenses:
  6. *
  7. * License 1: GPLv2
  8. *
  9. * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
  10. *
  11. * This file is free software; you may copy, redistribute and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * This file incorporates work covered by the following copyright and
  25. * permission notice:
  26. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  27. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  28. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  29. * and you.
  30. *
  31. * The Software IS NOT an item of Licensed Software or Licensed Product
  32. * under any End User Software License Agreement or Agreement for Licensed
  33. * Product with Synopsys or any supplement thereto. Permission is hereby
  34. * granted, free of charge, to any person obtaining a copy of this software
  35. * annotated with this license and the Software, to deal in the Software
  36. * without restriction, including without limitation the rights to use,
  37. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  38. * of the Software, and to permit persons to whom the Software is furnished
  39. * to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included
  42. * in all copies or substantial portions of the Software.
  43. *
  44. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  46. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  47. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  48. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  54. * THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. *
  57. * License 2: Modified BSD
  58. *
  59. * Copyright (c) 2014-2016 Advanced Micro Devices, Inc.
  60. * All rights reserved.
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions are met:
  64. * * Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. * * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. * * Neither the name of Advanced Micro Devices, Inc. nor the
  70. * names of its contributors may be used to endorse or promote products
  71. * derived from this software without specific prior written permission.
  72. *
  73. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  74. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  79. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  80. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  81. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  82. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  83. *
  84. * This file incorporates work covered by the following copyright and
  85. * permission notice:
  86. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  87. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  88. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  89. * and you.
  90. *
  91. * The Software IS NOT an item of Licensed Software or Licensed Product
  92. * under any End User Software License Agreement or Agreement for Licensed
  93. * Product with Synopsys or any supplement thereto. Permission is hereby
  94. * granted, free of charge, to any person obtaining a copy of this software
  95. * annotated with this license and the Software, to deal in the Software
  96. * without restriction, including without limitation the rights to use,
  97. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  98. * of the Software, and to permit persons to whom the Software is furnished
  99. * to do so, subject to the following conditions:
  100. *
  101. * The above copyright notice and this permission notice shall be included
  102. * in all copies or substantial portions of the Software.
  103. *
  104. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  105. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  106. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  107. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  108. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  109. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  110. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  111. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  112. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  114. * THE POSSIBILITY OF SUCH DAMAGE.
  115. */
  116. #include <linux/module.h>
  117. #include <linux/kmod.h>
  118. #include <linux/mdio.h>
  119. #include <linux/phy.h>
  120. #include <linux/of.h>
  121. #include <linux/bitops.h>
  122. #include <linux/jiffies.h>
  123. #include "xgbe.h"
  124. #include "xgbe-common.h"
  125. static void xgbe_an_enable_kr_training(struct xgbe_prv_data *pdata)
  126. {
  127. unsigned int reg;
  128. reg = XMDIO_READ(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
  129. reg |= XGBE_KR_TRAINING_ENABLE;
  130. XMDIO_WRITE(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, reg);
  131. }
  132. static void xgbe_an_disable_kr_training(struct xgbe_prv_data *pdata)
  133. {
  134. unsigned int reg;
  135. reg = XMDIO_READ(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
  136. reg &= ~XGBE_KR_TRAINING_ENABLE;
  137. XMDIO_WRITE(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, reg);
  138. }
  139. static void xgbe_pcs_power_cycle(struct xgbe_prv_data *pdata)
  140. {
  141. unsigned int reg;
  142. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  143. reg |= MDIO_CTRL1_LPOWER;
  144. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
  145. usleep_range(75, 100);
  146. reg &= ~MDIO_CTRL1_LPOWER;
  147. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
  148. }
  149. static void xgbe_serdes_start_ratechange(struct xgbe_prv_data *pdata)
  150. {
  151. /* Assert Rx and Tx ratechange */
  152. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, RATECHANGE, 1);
  153. }
  154. static void xgbe_serdes_complete_ratechange(struct xgbe_prv_data *pdata)
  155. {
  156. unsigned int wait;
  157. u16 status;
  158. /* Release Rx and Tx ratechange */
  159. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, RATECHANGE, 0);
  160. /* Wait for Rx and Tx ready */
  161. wait = XGBE_RATECHANGE_COUNT;
  162. while (wait--) {
  163. usleep_range(50, 75);
  164. status = XSIR0_IOREAD(pdata, SIR0_STATUS);
  165. if (XSIR_GET_BITS(status, SIR0_STATUS, RX_READY) &&
  166. XSIR_GET_BITS(status, SIR0_STATUS, TX_READY))
  167. goto rx_reset;
  168. }
  169. netif_dbg(pdata, link, pdata->netdev, "SerDes rx/tx not ready (%#hx)\n",
  170. status);
  171. rx_reset:
  172. /* Perform Rx reset for the DFE changes */
  173. XRXTX_IOWRITE_BITS(pdata, RXTX_REG6, RESETB_RXD, 0);
  174. XRXTX_IOWRITE_BITS(pdata, RXTX_REG6, RESETB_RXD, 1);
  175. }
  176. static void xgbe_xgmii_mode(struct xgbe_prv_data *pdata)
  177. {
  178. unsigned int reg;
  179. /* Enable KR training */
  180. xgbe_an_enable_kr_training(pdata);
  181. /* Set MAC to 10G speed */
  182. pdata->hw_if.set_xgmii_speed(pdata);
  183. /* Set PCS to KR/10G speed */
  184. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL2);
  185. reg &= ~MDIO_PCS_CTRL2_TYPE;
  186. reg |= MDIO_PCS_CTRL2_10GBR;
  187. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL2, reg);
  188. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  189. reg &= ~MDIO_CTRL1_SPEEDSEL;
  190. reg |= MDIO_CTRL1_SPEED10G;
  191. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
  192. xgbe_pcs_power_cycle(pdata);
  193. /* Set SerDes to 10G speed */
  194. xgbe_serdes_start_ratechange(pdata);
  195. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, DATARATE, XGBE_SPEED_10000_RATE);
  196. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, WORDMODE, XGBE_SPEED_10000_WORD);
  197. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, PLLSEL, XGBE_SPEED_10000_PLL);
  198. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, CDR_RATE,
  199. pdata->serdes_cdr_rate[XGBE_SPEED_10000]);
  200. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, TXAMP,
  201. pdata->serdes_tx_amp[XGBE_SPEED_10000]);
  202. XRXTX_IOWRITE_BITS(pdata, RXTX_REG20, BLWC_ENA,
  203. pdata->serdes_blwc[XGBE_SPEED_10000]);
  204. XRXTX_IOWRITE_BITS(pdata, RXTX_REG114, PQ_REG,
  205. pdata->serdes_pq_skew[XGBE_SPEED_10000]);
  206. XRXTX_IOWRITE_BITS(pdata, RXTX_REG129, RXDFE_CONFIG,
  207. pdata->serdes_dfe_tap_cfg[XGBE_SPEED_10000]);
  208. XRXTX_IOWRITE(pdata, RXTX_REG22,
  209. pdata->serdes_dfe_tap_ena[XGBE_SPEED_10000]);
  210. xgbe_serdes_complete_ratechange(pdata);
  211. netif_dbg(pdata, link, pdata->netdev, "10GbE KR mode set\n");
  212. }
  213. static void xgbe_gmii_2500_mode(struct xgbe_prv_data *pdata)
  214. {
  215. unsigned int reg;
  216. /* Disable KR training */
  217. xgbe_an_disable_kr_training(pdata);
  218. /* Set MAC to 2.5G speed */
  219. pdata->hw_if.set_gmii_2500_speed(pdata);
  220. /* Set PCS to KX/1G speed */
  221. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL2);
  222. reg &= ~MDIO_PCS_CTRL2_TYPE;
  223. reg |= MDIO_PCS_CTRL2_10GBX;
  224. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL2, reg);
  225. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  226. reg &= ~MDIO_CTRL1_SPEEDSEL;
  227. reg |= MDIO_CTRL1_SPEED1G;
  228. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
  229. xgbe_pcs_power_cycle(pdata);
  230. /* Set SerDes to 2.5G speed */
  231. xgbe_serdes_start_ratechange(pdata);
  232. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, DATARATE, XGBE_SPEED_2500_RATE);
  233. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, WORDMODE, XGBE_SPEED_2500_WORD);
  234. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, PLLSEL, XGBE_SPEED_2500_PLL);
  235. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, CDR_RATE,
  236. pdata->serdes_cdr_rate[XGBE_SPEED_2500]);
  237. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, TXAMP,
  238. pdata->serdes_tx_amp[XGBE_SPEED_2500]);
  239. XRXTX_IOWRITE_BITS(pdata, RXTX_REG20, BLWC_ENA,
  240. pdata->serdes_blwc[XGBE_SPEED_2500]);
  241. XRXTX_IOWRITE_BITS(pdata, RXTX_REG114, PQ_REG,
  242. pdata->serdes_pq_skew[XGBE_SPEED_2500]);
  243. XRXTX_IOWRITE_BITS(pdata, RXTX_REG129, RXDFE_CONFIG,
  244. pdata->serdes_dfe_tap_cfg[XGBE_SPEED_2500]);
  245. XRXTX_IOWRITE(pdata, RXTX_REG22,
  246. pdata->serdes_dfe_tap_ena[XGBE_SPEED_2500]);
  247. xgbe_serdes_complete_ratechange(pdata);
  248. netif_dbg(pdata, link, pdata->netdev, "2.5GbE KX mode set\n");
  249. }
  250. static void xgbe_gmii_mode(struct xgbe_prv_data *pdata)
  251. {
  252. unsigned int reg;
  253. /* Disable KR training */
  254. xgbe_an_disable_kr_training(pdata);
  255. /* Set MAC to 1G speed */
  256. pdata->hw_if.set_gmii_speed(pdata);
  257. /* Set PCS to KX/1G speed */
  258. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL2);
  259. reg &= ~MDIO_PCS_CTRL2_TYPE;
  260. reg |= MDIO_PCS_CTRL2_10GBX;
  261. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL2, reg);
  262. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  263. reg &= ~MDIO_CTRL1_SPEEDSEL;
  264. reg |= MDIO_CTRL1_SPEED1G;
  265. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
  266. xgbe_pcs_power_cycle(pdata);
  267. /* Set SerDes to 1G speed */
  268. xgbe_serdes_start_ratechange(pdata);
  269. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, DATARATE, XGBE_SPEED_1000_RATE);
  270. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, WORDMODE, XGBE_SPEED_1000_WORD);
  271. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, PLLSEL, XGBE_SPEED_1000_PLL);
  272. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, CDR_RATE,
  273. pdata->serdes_cdr_rate[XGBE_SPEED_1000]);
  274. XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, TXAMP,
  275. pdata->serdes_tx_amp[XGBE_SPEED_1000]);
  276. XRXTX_IOWRITE_BITS(pdata, RXTX_REG20, BLWC_ENA,
  277. pdata->serdes_blwc[XGBE_SPEED_1000]);
  278. XRXTX_IOWRITE_BITS(pdata, RXTX_REG114, PQ_REG,
  279. pdata->serdes_pq_skew[XGBE_SPEED_1000]);
  280. XRXTX_IOWRITE_BITS(pdata, RXTX_REG129, RXDFE_CONFIG,
  281. pdata->serdes_dfe_tap_cfg[XGBE_SPEED_1000]);
  282. XRXTX_IOWRITE(pdata, RXTX_REG22,
  283. pdata->serdes_dfe_tap_ena[XGBE_SPEED_1000]);
  284. xgbe_serdes_complete_ratechange(pdata);
  285. netif_dbg(pdata, link, pdata->netdev, "1GbE KX mode set\n");
  286. }
  287. static void xgbe_cur_mode(struct xgbe_prv_data *pdata,
  288. enum xgbe_mode *mode)
  289. {
  290. unsigned int reg;
  291. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL2);
  292. if ((reg & MDIO_PCS_CTRL2_TYPE) == MDIO_PCS_CTRL2_10GBR)
  293. *mode = XGBE_MODE_KR;
  294. else
  295. *mode = XGBE_MODE_KX;
  296. }
  297. static bool xgbe_in_kr_mode(struct xgbe_prv_data *pdata)
  298. {
  299. enum xgbe_mode mode;
  300. xgbe_cur_mode(pdata, &mode);
  301. return (mode == XGBE_MODE_KR);
  302. }
  303. static void xgbe_switch_mode(struct xgbe_prv_data *pdata)
  304. {
  305. /* If we are in KR switch to KX, and vice-versa */
  306. if (xgbe_in_kr_mode(pdata)) {
  307. if (pdata->speed_set == XGBE_SPEEDSET_1000_10000)
  308. xgbe_gmii_mode(pdata);
  309. else
  310. xgbe_gmii_2500_mode(pdata);
  311. } else {
  312. xgbe_xgmii_mode(pdata);
  313. }
  314. }
  315. static void xgbe_set_mode(struct xgbe_prv_data *pdata,
  316. enum xgbe_mode mode)
  317. {
  318. enum xgbe_mode cur_mode;
  319. xgbe_cur_mode(pdata, &cur_mode);
  320. if (mode != cur_mode)
  321. xgbe_switch_mode(pdata);
  322. }
  323. static bool xgbe_use_xgmii_mode(struct xgbe_prv_data *pdata)
  324. {
  325. if (pdata->phy.autoneg == AUTONEG_ENABLE) {
  326. if (pdata->phy.advertising & ADVERTISED_10000baseKR_Full)
  327. return true;
  328. } else {
  329. if (pdata->phy.speed == SPEED_10000)
  330. return true;
  331. }
  332. return false;
  333. }
  334. static bool xgbe_use_gmii_2500_mode(struct xgbe_prv_data *pdata)
  335. {
  336. if (pdata->phy.autoneg == AUTONEG_ENABLE) {
  337. if (pdata->phy.advertising & ADVERTISED_2500baseX_Full)
  338. return true;
  339. } else {
  340. if (pdata->phy.speed == SPEED_2500)
  341. return true;
  342. }
  343. return false;
  344. }
  345. static bool xgbe_use_gmii_mode(struct xgbe_prv_data *pdata)
  346. {
  347. if (pdata->phy.autoneg == AUTONEG_ENABLE) {
  348. if (pdata->phy.advertising & ADVERTISED_1000baseKX_Full)
  349. return true;
  350. } else {
  351. if (pdata->phy.speed == SPEED_1000)
  352. return true;
  353. }
  354. return false;
  355. }
  356. static void xgbe_set_an(struct xgbe_prv_data *pdata, bool enable, bool restart)
  357. {
  358. unsigned int reg;
  359. reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_CTRL1);
  360. reg &= ~MDIO_AN_CTRL1_ENABLE;
  361. if (enable)
  362. reg |= MDIO_AN_CTRL1_ENABLE;
  363. if (restart)
  364. reg |= MDIO_AN_CTRL1_RESTART;
  365. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_CTRL1, reg);
  366. }
  367. static void xgbe_restart_an(struct xgbe_prv_data *pdata)
  368. {
  369. xgbe_set_an(pdata, true, true);
  370. netif_dbg(pdata, link, pdata->netdev, "AN enabled/restarted\n");
  371. }
  372. static void xgbe_disable_an(struct xgbe_prv_data *pdata)
  373. {
  374. xgbe_set_an(pdata, false, false);
  375. netif_dbg(pdata, link, pdata->netdev, "AN disabled\n");
  376. }
  377. static enum xgbe_an xgbe_an_tx_training(struct xgbe_prv_data *pdata,
  378. enum xgbe_rx *state)
  379. {
  380. unsigned int ad_reg, lp_reg, reg;
  381. *state = XGBE_RX_COMPLETE;
  382. /* If we're not in KR mode then we're done */
  383. if (!xgbe_in_kr_mode(pdata))
  384. return XGBE_AN_PAGE_RECEIVED;
  385. /* Enable/Disable FEC */
  386. ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
  387. lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 2);
  388. reg = XMDIO_READ(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FECCTRL);
  389. reg &= ~(MDIO_PMA_10GBR_FECABLE_ABLE | MDIO_PMA_10GBR_FECABLE_ERRABLE);
  390. if ((ad_reg & 0xc000) && (lp_reg & 0xc000))
  391. reg |= pdata->fec_ability;
  392. XMDIO_WRITE(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FECCTRL, reg);
  393. /* Start KR training */
  394. reg = XMDIO_READ(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
  395. if (reg & XGBE_KR_TRAINING_ENABLE) {
  396. XSIR0_IOWRITE_BITS(pdata, SIR0_KR_RT_1, RESET, 1);
  397. reg |= XGBE_KR_TRAINING_START;
  398. XMDIO_WRITE(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL,
  399. reg);
  400. XSIR0_IOWRITE_BITS(pdata, SIR0_KR_RT_1, RESET, 0);
  401. netif_dbg(pdata, link, pdata->netdev,
  402. "KR training initiated\n");
  403. }
  404. return XGBE_AN_PAGE_RECEIVED;
  405. }
  406. static enum xgbe_an xgbe_an_tx_xnp(struct xgbe_prv_data *pdata,
  407. enum xgbe_rx *state)
  408. {
  409. u16 msg;
  410. *state = XGBE_RX_XNP;
  411. msg = XGBE_XNP_MCF_NULL_MESSAGE;
  412. msg |= XGBE_XNP_MP_FORMATTED;
  413. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_XNP + 2, 0);
  414. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_XNP + 1, 0);
  415. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_XNP, msg);
  416. return XGBE_AN_PAGE_RECEIVED;
  417. }
  418. static enum xgbe_an xgbe_an_rx_bpa(struct xgbe_prv_data *pdata,
  419. enum xgbe_rx *state)
  420. {
  421. unsigned int link_support;
  422. unsigned int reg, ad_reg, lp_reg;
  423. /* Read Base Ability register 2 first */
  424. reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 1);
  425. /* Check for a supported mode, otherwise restart in a different one */
  426. link_support = xgbe_in_kr_mode(pdata) ? 0x80 : 0x20;
  427. if (!(reg & link_support))
  428. return XGBE_AN_INCOMPAT_LINK;
  429. /* Check Extended Next Page support */
  430. ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
  431. lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA);
  432. return ((ad_reg & XGBE_XNP_NP_EXCHANGE) ||
  433. (lp_reg & XGBE_XNP_NP_EXCHANGE))
  434. ? xgbe_an_tx_xnp(pdata, state)
  435. : xgbe_an_tx_training(pdata, state);
  436. }
  437. static enum xgbe_an xgbe_an_rx_xnp(struct xgbe_prv_data *pdata,
  438. enum xgbe_rx *state)
  439. {
  440. unsigned int ad_reg, lp_reg;
  441. /* Check Extended Next Page support */
  442. ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_XNP);
  443. lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPX);
  444. return ((ad_reg & XGBE_XNP_NP_EXCHANGE) ||
  445. (lp_reg & XGBE_XNP_NP_EXCHANGE))
  446. ? xgbe_an_tx_xnp(pdata, state)
  447. : xgbe_an_tx_training(pdata, state);
  448. }
  449. static enum xgbe_an xgbe_an_page_received(struct xgbe_prv_data *pdata)
  450. {
  451. enum xgbe_rx *state;
  452. unsigned long an_timeout;
  453. enum xgbe_an ret;
  454. if (!pdata->an_start) {
  455. pdata->an_start = jiffies;
  456. } else {
  457. an_timeout = pdata->an_start +
  458. msecs_to_jiffies(XGBE_AN_MS_TIMEOUT);
  459. if (time_after(jiffies, an_timeout)) {
  460. /* Auto-negotiation timed out, reset state */
  461. pdata->kr_state = XGBE_RX_BPA;
  462. pdata->kx_state = XGBE_RX_BPA;
  463. pdata->an_start = jiffies;
  464. netif_dbg(pdata, link, pdata->netdev,
  465. "AN timed out, resetting state\n");
  466. }
  467. }
  468. state = xgbe_in_kr_mode(pdata) ? &pdata->kr_state
  469. : &pdata->kx_state;
  470. switch (*state) {
  471. case XGBE_RX_BPA:
  472. ret = xgbe_an_rx_bpa(pdata, state);
  473. break;
  474. case XGBE_RX_XNP:
  475. ret = xgbe_an_rx_xnp(pdata, state);
  476. break;
  477. default:
  478. ret = XGBE_AN_ERROR;
  479. }
  480. return ret;
  481. }
  482. static enum xgbe_an xgbe_an_incompat_link(struct xgbe_prv_data *pdata)
  483. {
  484. /* Be sure we aren't looping trying to negotiate */
  485. if (xgbe_in_kr_mode(pdata)) {
  486. pdata->kr_state = XGBE_RX_ERROR;
  487. if (!(pdata->phy.advertising & ADVERTISED_1000baseKX_Full) &&
  488. !(pdata->phy.advertising & ADVERTISED_2500baseX_Full))
  489. return XGBE_AN_NO_LINK;
  490. if (pdata->kx_state != XGBE_RX_BPA)
  491. return XGBE_AN_NO_LINK;
  492. } else {
  493. pdata->kx_state = XGBE_RX_ERROR;
  494. if (!(pdata->phy.advertising & ADVERTISED_10000baseKR_Full))
  495. return XGBE_AN_NO_LINK;
  496. if (pdata->kr_state != XGBE_RX_BPA)
  497. return XGBE_AN_NO_LINK;
  498. }
  499. xgbe_disable_an(pdata);
  500. xgbe_switch_mode(pdata);
  501. xgbe_restart_an(pdata);
  502. return XGBE_AN_INCOMPAT_LINK;
  503. }
  504. static irqreturn_t xgbe_an_isr(int irq, void *data)
  505. {
  506. struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;
  507. netif_dbg(pdata, intr, pdata->netdev, "AN interrupt received\n");
  508. /* Disable AN interrupts */
  509. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK, 0);
  510. /* Save the interrupt(s) that fired */
  511. pdata->an_int = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_INT);
  512. if (pdata->an_int) {
  513. /* Clear the interrupt(s) that fired and process them */
  514. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, ~pdata->an_int);
  515. queue_work(pdata->an_workqueue, &pdata->an_irq_work);
  516. } else {
  517. /* Enable AN interrupts */
  518. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK,
  519. XGBE_AN_INT_MASK);
  520. }
  521. return IRQ_HANDLED;
  522. }
  523. static void xgbe_an_irq_work(struct work_struct *work)
  524. {
  525. struct xgbe_prv_data *pdata = container_of(work,
  526. struct xgbe_prv_data,
  527. an_irq_work);
  528. /* Avoid a race between enabling the IRQ and exiting the work by
  529. * waiting for the work to finish and then queueing it
  530. */
  531. flush_work(&pdata->an_work);
  532. queue_work(pdata->an_workqueue, &pdata->an_work);
  533. }
  534. static const char *xgbe_state_as_string(enum xgbe_an state)
  535. {
  536. switch (state) {
  537. case XGBE_AN_READY:
  538. return "Ready";
  539. case XGBE_AN_PAGE_RECEIVED:
  540. return "Page-Received";
  541. case XGBE_AN_INCOMPAT_LINK:
  542. return "Incompatible-Link";
  543. case XGBE_AN_COMPLETE:
  544. return "Complete";
  545. case XGBE_AN_NO_LINK:
  546. return "No-Link";
  547. case XGBE_AN_ERROR:
  548. return "Error";
  549. default:
  550. return "Undefined";
  551. }
  552. }
  553. static void xgbe_an_state_machine(struct work_struct *work)
  554. {
  555. struct xgbe_prv_data *pdata = container_of(work,
  556. struct xgbe_prv_data,
  557. an_work);
  558. enum xgbe_an cur_state = pdata->an_state;
  559. mutex_lock(&pdata->an_mutex);
  560. if (!pdata->an_int)
  561. goto out;
  562. next_int:
  563. if (pdata->an_int & XGBE_AN_PG_RCV) {
  564. pdata->an_state = XGBE_AN_PAGE_RECEIVED;
  565. pdata->an_int &= ~XGBE_AN_PG_RCV;
  566. } else if (pdata->an_int & XGBE_AN_INC_LINK) {
  567. pdata->an_state = XGBE_AN_INCOMPAT_LINK;
  568. pdata->an_int &= ~XGBE_AN_INC_LINK;
  569. } else if (pdata->an_int & XGBE_AN_INT_CMPLT) {
  570. pdata->an_state = XGBE_AN_COMPLETE;
  571. pdata->an_int &= ~XGBE_AN_INT_CMPLT;
  572. } else {
  573. pdata->an_state = XGBE_AN_ERROR;
  574. }
  575. pdata->an_result = pdata->an_state;
  576. again:
  577. netif_dbg(pdata, link, pdata->netdev, "AN %s\n",
  578. xgbe_state_as_string(pdata->an_state));
  579. cur_state = pdata->an_state;
  580. switch (pdata->an_state) {
  581. case XGBE_AN_READY:
  582. pdata->an_supported = 0;
  583. break;
  584. case XGBE_AN_PAGE_RECEIVED:
  585. pdata->an_state = xgbe_an_page_received(pdata);
  586. pdata->an_supported++;
  587. break;
  588. case XGBE_AN_INCOMPAT_LINK:
  589. pdata->an_supported = 0;
  590. pdata->parallel_detect = 0;
  591. pdata->an_state = xgbe_an_incompat_link(pdata);
  592. break;
  593. case XGBE_AN_COMPLETE:
  594. pdata->parallel_detect = pdata->an_supported ? 0 : 1;
  595. netif_dbg(pdata, link, pdata->netdev, "%s successful\n",
  596. pdata->an_supported ? "Auto negotiation"
  597. : "Parallel detection");
  598. break;
  599. case XGBE_AN_NO_LINK:
  600. break;
  601. default:
  602. pdata->an_state = XGBE_AN_ERROR;
  603. }
  604. if (pdata->an_state == XGBE_AN_NO_LINK) {
  605. pdata->an_int = 0;
  606. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, 0);
  607. } else if (pdata->an_state == XGBE_AN_ERROR) {
  608. netdev_err(pdata->netdev,
  609. "error during auto-negotiation, state=%u\n",
  610. cur_state);
  611. pdata->an_int = 0;
  612. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, 0);
  613. }
  614. if (pdata->an_state >= XGBE_AN_COMPLETE) {
  615. pdata->an_result = pdata->an_state;
  616. pdata->an_state = XGBE_AN_READY;
  617. pdata->kr_state = XGBE_RX_BPA;
  618. pdata->kx_state = XGBE_RX_BPA;
  619. pdata->an_start = 0;
  620. netif_dbg(pdata, link, pdata->netdev, "AN result: %s\n",
  621. xgbe_state_as_string(pdata->an_result));
  622. }
  623. if (cur_state != pdata->an_state)
  624. goto again;
  625. if (pdata->an_int)
  626. goto next_int;
  627. out:
  628. /* Enable AN interrupts on the way out */
  629. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK, XGBE_AN_INT_MASK);
  630. mutex_unlock(&pdata->an_mutex);
  631. }
  632. static void xgbe_an_init(struct xgbe_prv_data *pdata)
  633. {
  634. unsigned int reg;
  635. /* Set up Advertisement register 3 first */
  636. reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
  637. if (pdata->phy.advertising & ADVERTISED_10000baseR_FEC)
  638. reg |= 0xc000;
  639. else
  640. reg &= ~0xc000;
  641. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2, reg);
  642. /* Set up Advertisement register 2 next */
  643. reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
  644. if (pdata->phy.advertising & ADVERTISED_10000baseKR_Full)
  645. reg |= 0x80;
  646. else
  647. reg &= ~0x80;
  648. if ((pdata->phy.advertising & ADVERTISED_1000baseKX_Full) ||
  649. (pdata->phy.advertising & ADVERTISED_2500baseX_Full))
  650. reg |= 0x20;
  651. else
  652. reg &= ~0x20;
  653. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1, reg);
  654. /* Set up Advertisement register 1 last */
  655. reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
  656. if (pdata->phy.advertising & ADVERTISED_Pause)
  657. reg |= 0x400;
  658. else
  659. reg &= ~0x400;
  660. if (pdata->phy.advertising & ADVERTISED_Asym_Pause)
  661. reg |= 0x800;
  662. else
  663. reg &= ~0x800;
  664. /* We don't intend to perform XNP */
  665. reg &= ~XGBE_XNP_NP_EXCHANGE;
  666. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE, reg);
  667. netif_dbg(pdata, link, pdata->netdev, "AN initialized\n");
  668. }
  669. static const char *xgbe_phy_fc_string(struct xgbe_prv_data *pdata)
  670. {
  671. if (pdata->tx_pause && pdata->rx_pause)
  672. return "rx/tx";
  673. else if (pdata->rx_pause)
  674. return "rx";
  675. else if (pdata->tx_pause)
  676. return "tx";
  677. else
  678. return "off";
  679. }
  680. static const char *xgbe_phy_speed_string(int speed)
  681. {
  682. switch (speed) {
  683. case SPEED_1000:
  684. return "1Gbps";
  685. case SPEED_2500:
  686. return "2.5Gbps";
  687. case SPEED_10000:
  688. return "10Gbps";
  689. case SPEED_UNKNOWN:
  690. return "Unknown";
  691. default:
  692. return "Unsupported";
  693. }
  694. }
  695. static void xgbe_phy_print_status(struct xgbe_prv_data *pdata)
  696. {
  697. if (pdata->phy.link)
  698. netdev_info(pdata->netdev,
  699. "Link is Up - %s/%s - flow control %s\n",
  700. xgbe_phy_speed_string(pdata->phy.speed),
  701. pdata->phy.duplex == DUPLEX_FULL ? "Full" : "Half",
  702. xgbe_phy_fc_string(pdata));
  703. else
  704. netdev_info(pdata->netdev, "Link is Down\n");
  705. }
  706. static void xgbe_phy_adjust_link(struct xgbe_prv_data *pdata)
  707. {
  708. int new_state = 0;
  709. if (pdata->phy.link) {
  710. /* Flow control support */
  711. pdata->pause_autoneg = pdata->phy.pause_autoneg;
  712. if (pdata->tx_pause != pdata->phy.tx_pause) {
  713. new_state = 1;
  714. pdata->hw_if.config_tx_flow_control(pdata);
  715. pdata->tx_pause = pdata->phy.tx_pause;
  716. }
  717. if (pdata->rx_pause != pdata->phy.rx_pause) {
  718. new_state = 1;
  719. pdata->hw_if.config_rx_flow_control(pdata);
  720. pdata->rx_pause = pdata->phy.rx_pause;
  721. }
  722. /* Speed support */
  723. if (pdata->phy_speed != pdata->phy.speed) {
  724. new_state = 1;
  725. pdata->phy_speed = pdata->phy.speed;
  726. }
  727. if (pdata->phy_link != pdata->phy.link) {
  728. new_state = 1;
  729. pdata->phy_link = pdata->phy.link;
  730. }
  731. } else if (pdata->phy_link) {
  732. new_state = 1;
  733. pdata->phy_link = 0;
  734. pdata->phy_speed = SPEED_UNKNOWN;
  735. }
  736. if (new_state && netif_msg_link(pdata))
  737. xgbe_phy_print_status(pdata);
  738. }
  739. static int xgbe_phy_config_fixed(struct xgbe_prv_data *pdata)
  740. {
  741. netif_dbg(pdata, link, pdata->netdev, "fixed PHY configuration\n");
  742. /* Disable auto-negotiation */
  743. xgbe_disable_an(pdata);
  744. /* Validate/Set specified speed */
  745. switch (pdata->phy.speed) {
  746. case SPEED_10000:
  747. xgbe_set_mode(pdata, XGBE_MODE_KR);
  748. break;
  749. case SPEED_2500:
  750. case SPEED_1000:
  751. xgbe_set_mode(pdata, XGBE_MODE_KX);
  752. break;
  753. default:
  754. return -EINVAL;
  755. }
  756. /* Validate duplex mode */
  757. if (pdata->phy.duplex != DUPLEX_FULL)
  758. return -EINVAL;
  759. return 0;
  760. }
  761. static int __xgbe_phy_config_aneg(struct xgbe_prv_data *pdata)
  762. {
  763. set_bit(XGBE_LINK_INIT, &pdata->dev_state);
  764. pdata->link_check = jiffies;
  765. if (pdata->phy.autoneg != AUTONEG_ENABLE)
  766. return xgbe_phy_config_fixed(pdata);
  767. netif_dbg(pdata, link, pdata->netdev, "AN PHY configuration\n");
  768. /* Disable auto-negotiation interrupt */
  769. disable_irq(pdata->an_irq);
  770. /* Start auto-negotiation in a supported mode */
  771. if (pdata->phy.advertising & ADVERTISED_10000baseKR_Full) {
  772. xgbe_set_mode(pdata, XGBE_MODE_KR);
  773. } else if ((pdata->phy.advertising & ADVERTISED_1000baseKX_Full) ||
  774. (pdata->phy.advertising & ADVERTISED_2500baseX_Full)) {
  775. xgbe_set_mode(pdata, XGBE_MODE_KX);
  776. } else {
  777. enable_irq(pdata->an_irq);
  778. return -EINVAL;
  779. }
  780. /* Disable and stop any in progress auto-negotiation */
  781. xgbe_disable_an(pdata);
  782. /* Clear any auto-negotitation interrupts */
  783. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, 0);
  784. pdata->an_result = XGBE_AN_READY;
  785. pdata->an_state = XGBE_AN_READY;
  786. pdata->kr_state = XGBE_RX_BPA;
  787. pdata->kx_state = XGBE_RX_BPA;
  788. /* Re-enable auto-negotiation interrupt */
  789. enable_irq(pdata->an_irq);
  790. /* Set up advertisement registers based on current settings */
  791. xgbe_an_init(pdata);
  792. /* Enable and start auto-negotiation */
  793. xgbe_restart_an(pdata);
  794. return 0;
  795. }
  796. static int xgbe_phy_config_aneg(struct xgbe_prv_data *pdata)
  797. {
  798. int ret;
  799. mutex_lock(&pdata->an_mutex);
  800. ret = __xgbe_phy_config_aneg(pdata);
  801. if (ret)
  802. set_bit(XGBE_LINK_ERR, &pdata->dev_state);
  803. else
  804. clear_bit(XGBE_LINK_ERR, &pdata->dev_state);
  805. mutex_unlock(&pdata->an_mutex);
  806. return ret;
  807. }
  808. static bool xgbe_phy_aneg_done(struct xgbe_prv_data *pdata)
  809. {
  810. return (pdata->an_result == XGBE_AN_COMPLETE);
  811. }
  812. static void xgbe_check_link_timeout(struct xgbe_prv_data *pdata)
  813. {
  814. unsigned long link_timeout;
  815. link_timeout = pdata->link_check + (XGBE_LINK_TIMEOUT * HZ);
  816. if (time_after(jiffies, link_timeout)) {
  817. netif_dbg(pdata, link, pdata->netdev, "AN link timeout\n");
  818. xgbe_phy_config_aneg(pdata);
  819. }
  820. }
  821. static void xgbe_phy_status_force(struct xgbe_prv_data *pdata)
  822. {
  823. if (xgbe_in_kr_mode(pdata)) {
  824. pdata->phy.speed = SPEED_10000;
  825. } else {
  826. switch (pdata->speed_set) {
  827. case XGBE_SPEEDSET_1000_10000:
  828. pdata->phy.speed = SPEED_1000;
  829. break;
  830. case XGBE_SPEEDSET_2500_10000:
  831. pdata->phy.speed = SPEED_2500;
  832. break;
  833. }
  834. }
  835. pdata->phy.duplex = DUPLEX_FULL;
  836. }
  837. static void xgbe_phy_status_aneg(struct xgbe_prv_data *pdata)
  838. {
  839. unsigned int ad_reg, lp_reg;
  840. pdata->phy.lp_advertising = 0;
  841. if ((pdata->phy.autoneg != AUTONEG_ENABLE) || pdata->parallel_detect)
  842. return xgbe_phy_status_force(pdata);
  843. pdata->phy.lp_advertising |= ADVERTISED_Autoneg;
  844. pdata->phy.lp_advertising |= ADVERTISED_Backplane;
  845. /* Compare Advertisement and Link Partner register 1 */
  846. ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
  847. lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA);
  848. if (lp_reg & 0x400)
  849. pdata->phy.lp_advertising |= ADVERTISED_Pause;
  850. if (lp_reg & 0x800)
  851. pdata->phy.lp_advertising |= ADVERTISED_Asym_Pause;
  852. if (pdata->phy.pause_autoneg) {
  853. /* Set flow control based on auto-negotiation result */
  854. pdata->phy.tx_pause = 0;
  855. pdata->phy.rx_pause = 0;
  856. if (ad_reg & lp_reg & 0x400) {
  857. pdata->phy.tx_pause = 1;
  858. pdata->phy.rx_pause = 1;
  859. } else if (ad_reg & lp_reg & 0x800) {
  860. if (ad_reg & 0x400)
  861. pdata->phy.rx_pause = 1;
  862. else if (lp_reg & 0x400)
  863. pdata->phy.tx_pause = 1;
  864. }
  865. }
  866. /* Compare Advertisement and Link Partner register 2 */
  867. ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
  868. lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 1);
  869. if (lp_reg & 0x80)
  870. pdata->phy.lp_advertising |= ADVERTISED_10000baseKR_Full;
  871. if (lp_reg & 0x20) {
  872. switch (pdata->speed_set) {
  873. case XGBE_SPEEDSET_1000_10000:
  874. pdata->phy.lp_advertising |= ADVERTISED_1000baseKX_Full;
  875. break;
  876. case XGBE_SPEEDSET_2500_10000:
  877. pdata->phy.lp_advertising |= ADVERTISED_2500baseX_Full;
  878. break;
  879. }
  880. }
  881. ad_reg &= lp_reg;
  882. if (ad_reg & 0x80) {
  883. pdata->phy.speed = SPEED_10000;
  884. xgbe_set_mode(pdata, XGBE_MODE_KR);
  885. } else if (ad_reg & 0x20) {
  886. switch (pdata->speed_set) {
  887. case XGBE_SPEEDSET_1000_10000:
  888. pdata->phy.speed = SPEED_1000;
  889. break;
  890. case XGBE_SPEEDSET_2500_10000:
  891. pdata->phy.speed = SPEED_2500;
  892. break;
  893. }
  894. xgbe_set_mode(pdata, XGBE_MODE_KX);
  895. } else {
  896. pdata->phy.speed = SPEED_UNKNOWN;
  897. }
  898. /* Compare Advertisement and Link Partner register 3 */
  899. ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
  900. lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 2);
  901. if (lp_reg & 0xc000)
  902. pdata->phy.lp_advertising |= ADVERTISED_10000baseR_FEC;
  903. pdata->phy.duplex = DUPLEX_FULL;
  904. }
  905. static void xgbe_phy_status(struct xgbe_prv_data *pdata)
  906. {
  907. unsigned int reg, link_aneg;
  908. if (test_bit(XGBE_LINK_ERR, &pdata->dev_state)) {
  909. netif_carrier_off(pdata->netdev);
  910. pdata->phy.link = 0;
  911. goto adjust_link;
  912. }
  913. link_aneg = (pdata->phy.autoneg == AUTONEG_ENABLE);
  914. /* Get the link status. Link status is latched low, so read
  915. * once to clear and then read again to get current state
  916. */
  917. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1);
  918. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1);
  919. pdata->phy.link = (reg & MDIO_STAT1_LSTATUS) ? 1 : 0;
  920. if (pdata->phy.link) {
  921. if (link_aneg && !xgbe_phy_aneg_done(pdata)) {
  922. xgbe_check_link_timeout(pdata);
  923. return;
  924. }
  925. xgbe_phy_status_aneg(pdata);
  926. if (test_bit(XGBE_LINK_INIT, &pdata->dev_state))
  927. clear_bit(XGBE_LINK_INIT, &pdata->dev_state);
  928. netif_carrier_on(pdata->netdev);
  929. } else {
  930. if (test_bit(XGBE_LINK_INIT, &pdata->dev_state)) {
  931. xgbe_check_link_timeout(pdata);
  932. if (link_aneg)
  933. return;
  934. }
  935. xgbe_phy_status_aneg(pdata);
  936. netif_carrier_off(pdata->netdev);
  937. }
  938. adjust_link:
  939. xgbe_phy_adjust_link(pdata);
  940. }
  941. static void xgbe_phy_stop(struct xgbe_prv_data *pdata)
  942. {
  943. netif_dbg(pdata, link, pdata->netdev, "stopping PHY\n");
  944. /* Disable auto-negotiation */
  945. xgbe_disable_an(pdata);
  946. /* Disable auto-negotiation interrupts */
  947. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK, 0);
  948. devm_free_irq(pdata->dev, pdata->an_irq, pdata);
  949. pdata->phy.link = 0;
  950. netif_carrier_off(pdata->netdev);
  951. xgbe_phy_adjust_link(pdata);
  952. }
  953. static int xgbe_phy_start(struct xgbe_prv_data *pdata)
  954. {
  955. struct net_device *netdev = pdata->netdev;
  956. int ret;
  957. netif_dbg(pdata, link, pdata->netdev, "starting PHY\n");
  958. ret = devm_request_irq(pdata->dev, pdata->an_irq,
  959. xgbe_an_isr, 0, pdata->an_name,
  960. pdata);
  961. if (ret) {
  962. netdev_err(netdev, "phy irq request failed\n");
  963. return ret;
  964. }
  965. /* Set initial mode - call the mode setting routines
  966. * directly to insure we are properly configured
  967. */
  968. if (xgbe_use_xgmii_mode(pdata)) {
  969. xgbe_xgmii_mode(pdata);
  970. } else if (xgbe_use_gmii_mode(pdata)) {
  971. xgbe_gmii_mode(pdata);
  972. } else if (xgbe_use_gmii_2500_mode(pdata)) {
  973. xgbe_gmii_2500_mode(pdata);
  974. } else {
  975. ret = -EINVAL;
  976. goto err_irq;
  977. }
  978. /* Set up advertisement registers based on current settings */
  979. xgbe_an_init(pdata);
  980. /* Enable auto-negotiation interrupts */
  981. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK, 0x07);
  982. return xgbe_phy_config_aneg(pdata);
  983. err_irq:
  984. devm_free_irq(pdata->dev, pdata->an_irq, pdata);
  985. return ret;
  986. }
  987. static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
  988. {
  989. unsigned int count, reg;
  990. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  991. reg |= MDIO_CTRL1_RESET;
  992. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
  993. count = 50;
  994. do {
  995. msleep(20);
  996. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  997. } while ((reg & MDIO_CTRL1_RESET) && --count);
  998. if (reg & MDIO_CTRL1_RESET)
  999. return -ETIMEDOUT;
  1000. /* Disable auto-negotiation for now */
  1001. xgbe_disable_an(pdata);
  1002. /* Clear auto-negotiation interrupts */
  1003. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, 0);
  1004. return 0;
  1005. }
  1006. static void xgbe_dump_phy_registers(struct xgbe_prv_data *pdata)
  1007. {
  1008. struct device *dev = pdata->dev;
  1009. dev_dbg(dev, "\n************* PHY Reg dump **********************\n");
  1010. dev_dbg(dev, "PCS Control Reg (%#04x) = %#04x\n", MDIO_CTRL1,
  1011. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1));
  1012. dev_dbg(dev, "PCS Status Reg (%#04x) = %#04x\n", MDIO_STAT1,
  1013. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1));
  1014. dev_dbg(dev, "Phy Id (PHYS ID 1 %#04x)= %#04x\n", MDIO_DEVID1,
  1015. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVID1));
  1016. dev_dbg(dev, "Phy Id (PHYS ID 2 %#04x)= %#04x\n", MDIO_DEVID2,
  1017. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVID2));
  1018. dev_dbg(dev, "Devices in Package (%#04x)= %#04x\n", MDIO_DEVS1,
  1019. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVS1));
  1020. dev_dbg(dev, "Devices in Package (%#04x)= %#04x\n", MDIO_DEVS2,
  1021. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVS2));
  1022. dev_dbg(dev, "Auto-Neg Control Reg (%#04x) = %#04x\n", MDIO_CTRL1,
  1023. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_CTRL1));
  1024. dev_dbg(dev, "Auto-Neg Status Reg (%#04x) = %#04x\n", MDIO_STAT1,
  1025. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_STAT1));
  1026. dev_dbg(dev, "Auto-Neg Ad Reg 1 (%#04x) = %#04x\n",
  1027. MDIO_AN_ADVERTISE,
  1028. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE));
  1029. dev_dbg(dev, "Auto-Neg Ad Reg 2 (%#04x) = %#04x\n",
  1030. MDIO_AN_ADVERTISE + 1,
  1031. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1));
  1032. dev_dbg(dev, "Auto-Neg Ad Reg 3 (%#04x) = %#04x\n",
  1033. MDIO_AN_ADVERTISE + 2,
  1034. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2));
  1035. dev_dbg(dev, "Auto-Neg Completion Reg (%#04x) = %#04x\n",
  1036. MDIO_AN_COMP_STAT,
  1037. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_COMP_STAT));
  1038. dev_dbg(dev, "\n*************************************************\n");
  1039. }
  1040. static void xgbe_phy_init(struct xgbe_prv_data *pdata)
  1041. {
  1042. mutex_init(&pdata->an_mutex);
  1043. INIT_WORK(&pdata->an_irq_work, xgbe_an_irq_work);
  1044. INIT_WORK(&pdata->an_work, xgbe_an_state_machine);
  1045. pdata->mdio_mmd = MDIO_MMD_PCS;
  1046. /* Initialize supported features */
  1047. pdata->phy.supported = SUPPORTED_Autoneg;
  1048. pdata->phy.supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
  1049. pdata->phy.supported |= SUPPORTED_Backplane;
  1050. pdata->phy.supported |= SUPPORTED_10000baseKR_Full;
  1051. switch (pdata->speed_set) {
  1052. case XGBE_SPEEDSET_1000_10000:
  1053. pdata->phy.supported |= SUPPORTED_1000baseKX_Full;
  1054. break;
  1055. case XGBE_SPEEDSET_2500_10000:
  1056. pdata->phy.supported |= SUPPORTED_2500baseX_Full;
  1057. break;
  1058. }
  1059. pdata->fec_ability = XMDIO_READ(pdata, MDIO_MMD_PMAPMD,
  1060. MDIO_PMA_10GBR_FECABLE);
  1061. pdata->fec_ability &= (MDIO_PMA_10GBR_FECABLE_ABLE |
  1062. MDIO_PMA_10GBR_FECABLE_ERRABLE);
  1063. if (pdata->fec_ability & MDIO_PMA_10GBR_FECABLE_ABLE)
  1064. pdata->phy.supported |= SUPPORTED_10000baseR_FEC;
  1065. pdata->phy.advertising = pdata->phy.supported;
  1066. pdata->phy.address = 0;
  1067. pdata->phy.autoneg = AUTONEG_ENABLE;
  1068. pdata->phy.speed = SPEED_UNKNOWN;
  1069. pdata->phy.duplex = DUPLEX_UNKNOWN;
  1070. pdata->phy.link = 0;
  1071. pdata->phy.pause_autoneg = pdata->pause_autoneg;
  1072. pdata->phy.tx_pause = pdata->tx_pause;
  1073. pdata->phy.rx_pause = pdata->rx_pause;
  1074. /* Fix up Flow Control advertising */
  1075. pdata->phy.advertising &= ~ADVERTISED_Pause;
  1076. pdata->phy.advertising &= ~ADVERTISED_Asym_Pause;
  1077. if (pdata->rx_pause) {
  1078. pdata->phy.advertising |= ADVERTISED_Pause;
  1079. pdata->phy.advertising |= ADVERTISED_Asym_Pause;
  1080. }
  1081. if (pdata->tx_pause)
  1082. pdata->phy.advertising ^= ADVERTISED_Asym_Pause;
  1083. if (netif_msg_drv(pdata))
  1084. xgbe_dump_phy_registers(pdata);
  1085. }
  1086. void xgbe_init_function_ptrs_phy(struct xgbe_phy_if *phy_if)
  1087. {
  1088. phy_if->phy_init = xgbe_phy_init;
  1089. phy_if->phy_reset = xgbe_phy_reset;
  1090. phy_if->phy_start = xgbe_phy_start;
  1091. phy_if->phy_stop = xgbe_phy_stop;
  1092. phy_if->phy_status = xgbe_phy_status;
  1093. phy_if->phy_config_aneg = xgbe_phy_config_aneg;
  1094. }