xgbe-mdio.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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 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 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. /* Interrupt reason must be read and cleared outside of IRQ context */
  509. disable_irq_nosync(pdata->an_irq);
  510. queue_work(pdata->an_workqueue, &pdata->an_irq_work);
  511. return IRQ_HANDLED;
  512. }
  513. static void xgbe_an_irq_work(struct work_struct *work)
  514. {
  515. struct xgbe_prv_data *pdata = container_of(work,
  516. struct xgbe_prv_data,
  517. an_irq_work);
  518. /* Avoid a race between enabling the IRQ and exiting the work by
  519. * waiting for the work to finish and then queueing it
  520. */
  521. flush_work(&pdata->an_work);
  522. queue_work(pdata->an_workqueue, &pdata->an_work);
  523. }
  524. static const char *xgbe_state_as_string(enum xgbe_an state)
  525. {
  526. switch (state) {
  527. case XGBE_AN_READY:
  528. return "Ready";
  529. case XGBE_AN_PAGE_RECEIVED:
  530. return "Page-Received";
  531. case XGBE_AN_INCOMPAT_LINK:
  532. return "Incompatible-Link";
  533. case XGBE_AN_COMPLETE:
  534. return "Complete";
  535. case XGBE_AN_NO_LINK:
  536. return "No-Link";
  537. case XGBE_AN_ERROR:
  538. return "Error";
  539. default:
  540. return "Undefined";
  541. }
  542. }
  543. static void xgbe_an_state_machine(struct work_struct *work)
  544. {
  545. struct xgbe_prv_data *pdata = container_of(work,
  546. struct xgbe_prv_data,
  547. an_work);
  548. enum xgbe_an cur_state = pdata->an_state;
  549. unsigned int int_reg, int_mask;
  550. mutex_lock(&pdata->an_mutex);
  551. /* Read the interrupt */
  552. int_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_INT);
  553. if (!int_reg)
  554. goto out;
  555. next_int:
  556. if (int_reg & XGBE_AN_PG_RCV) {
  557. pdata->an_state = XGBE_AN_PAGE_RECEIVED;
  558. int_mask = XGBE_AN_PG_RCV;
  559. } else if (int_reg & XGBE_AN_INC_LINK) {
  560. pdata->an_state = XGBE_AN_INCOMPAT_LINK;
  561. int_mask = XGBE_AN_INC_LINK;
  562. } else if (int_reg & XGBE_AN_INT_CMPLT) {
  563. pdata->an_state = XGBE_AN_COMPLETE;
  564. int_mask = XGBE_AN_INT_CMPLT;
  565. } else {
  566. pdata->an_state = XGBE_AN_ERROR;
  567. int_mask = 0;
  568. }
  569. /* Clear the interrupt to be processed */
  570. int_reg &= ~int_mask;
  571. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, int_reg);
  572. pdata->an_result = pdata->an_state;
  573. again:
  574. netif_dbg(pdata, link, pdata->netdev, "AN %s\n",
  575. xgbe_state_as_string(pdata->an_state));
  576. cur_state = pdata->an_state;
  577. switch (pdata->an_state) {
  578. case XGBE_AN_READY:
  579. pdata->an_supported = 0;
  580. break;
  581. case XGBE_AN_PAGE_RECEIVED:
  582. pdata->an_state = xgbe_an_page_received(pdata);
  583. pdata->an_supported++;
  584. break;
  585. case XGBE_AN_INCOMPAT_LINK:
  586. pdata->an_supported = 0;
  587. pdata->parallel_detect = 0;
  588. pdata->an_state = xgbe_an_incompat_link(pdata);
  589. break;
  590. case XGBE_AN_COMPLETE:
  591. pdata->parallel_detect = pdata->an_supported ? 0 : 1;
  592. netif_dbg(pdata, link, pdata->netdev, "%s successful\n",
  593. pdata->an_supported ? "Auto negotiation"
  594. : "Parallel detection");
  595. break;
  596. case XGBE_AN_NO_LINK:
  597. break;
  598. default:
  599. pdata->an_state = XGBE_AN_ERROR;
  600. }
  601. if (pdata->an_state == XGBE_AN_NO_LINK) {
  602. int_reg = 0;
  603. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, 0);
  604. } else if (pdata->an_state == XGBE_AN_ERROR) {
  605. netdev_err(pdata->netdev,
  606. "error during auto-negotiation, state=%u\n",
  607. cur_state);
  608. int_reg = 0;
  609. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, 0);
  610. }
  611. if (pdata->an_state >= XGBE_AN_COMPLETE) {
  612. pdata->an_result = pdata->an_state;
  613. pdata->an_state = XGBE_AN_READY;
  614. pdata->kr_state = XGBE_RX_BPA;
  615. pdata->kx_state = XGBE_RX_BPA;
  616. pdata->an_start = 0;
  617. netif_dbg(pdata, link, pdata->netdev, "AN result: %s\n",
  618. xgbe_state_as_string(pdata->an_result));
  619. }
  620. if (cur_state != pdata->an_state)
  621. goto again;
  622. if (int_reg)
  623. goto next_int;
  624. out:
  625. enable_irq(pdata->an_irq);
  626. mutex_unlock(&pdata->an_mutex);
  627. }
  628. static void xgbe_an_init(struct xgbe_prv_data *pdata)
  629. {
  630. unsigned int reg;
  631. /* Set up Advertisement register 3 first */
  632. reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
  633. if (pdata->phy.advertising & ADVERTISED_10000baseR_FEC)
  634. reg |= 0xc000;
  635. else
  636. reg &= ~0xc000;
  637. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2, reg);
  638. /* Set up Advertisement register 2 next */
  639. reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
  640. if (pdata->phy.advertising & ADVERTISED_10000baseKR_Full)
  641. reg |= 0x80;
  642. else
  643. reg &= ~0x80;
  644. if ((pdata->phy.advertising & ADVERTISED_1000baseKX_Full) ||
  645. (pdata->phy.advertising & ADVERTISED_2500baseX_Full))
  646. reg |= 0x20;
  647. else
  648. reg &= ~0x20;
  649. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1, reg);
  650. /* Set up Advertisement register 1 last */
  651. reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
  652. if (pdata->phy.advertising & ADVERTISED_Pause)
  653. reg |= 0x400;
  654. else
  655. reg &= ~0x400;
  656. if (pdata->phy.advertising & ADVERTISED_Asym_Pause)
  657. reg |= 0x800;
  658. else
  659. reg &= ~0x800;
  660. /* We don't intend to perform XNP */
  661. reg &= ~XGBE_XNP_NP_EXCHANGE;
  662. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE, reg);
  663. netif_dbg(pdata, link, pdata->netdev, "AN initialized\n");
  664. }
  665. static const char *xgbe_phy_fc_string(struct xgbe_prv_data *pdata)
  666. {
  667. if (pdata->tx_pause && pdata->rx_pause)
  668. return "rx/tx";
  669. else if (pdata->rx_pause)
  670. return "rx";
  671. else if (pdata->tx_pause)
  672. return "tx";
  673. else
  674. return "off";
  675. }
  676. static const char *xgbe_phy_speed_string(int speed)
  677. {
  678. switch (speed) {
  679. case SPEED_1000:
  680. return "1Gbps";
  681. case SPEED_2500:
  682. return "2.5Gbps";
  683. case SPEED_10000:
  684. return "10Gbps";
  685. case SPEED_UNKNOWN:
  686. return "Unknown";
  687. default:
  688. return "Unsupported";
  689. }
  690. }
  691. static void xgbe_phy_print_status(struct xgbe_prv_data *pdata)
  692. {
  693. if (pdata->phy.link)
  694. netdev_info(pdata->netdev,
  695. "Link is Up - %s/%s - flow control %s\n",
  696. xgbe_phy_speed_string(pdata->phy.speed),
  697. pdata->phy.duplex == DUPLEX_FULL ? "Full" : "Half",
  698. xgbe_phy_fc_string(pdata));
  699. else
  700. netdev_info(pdata->netdev, "Link is Down\n");
  701. }
  702. static void xgbe_phy_adjust_link(struct xgbe_prv_data *pdata)
  703. {
  704. int new_state = 0;
  705. if (pdata->phy.link) {
  706. /* Flow control support */
  707. pdata->pause_autoneg = pdata->phy.pause_autoneg;
  708. if (pdata->tx_pause != pdata->phy.tx_pause) {
  709. new_state = 1;
  710. pdata->tx_pause = pdata->phy.tx_pause;
  711. pdata->hw_if.config_tx_flow_control(pdata);
  712. }
  713. if (pdata->rx_pause != pdata->phy.rx_pause) {
  714. new_state = 1;
  715. pdata->rx_pause = pdata->phy.rx_pause;
  716. pdata->hw_if.config_rx_flow_control(pdata);
  717. }
  718. /* Speed support */
  719. if (pdata->phy_speed != pdata->phy.speed) {
  720. new_state = 1;
  721. pdata->phy_speed = pdata->phy.speed;
  722. }
  723. if (pdata->phy_link != pdata->phy.link) {
  724. new_state = 1;
  725. pdata->phy_link = pdata->phy.link;
  726. }
  727. } else if (pdata->phy_link) {
  728. new_state = 1;
  729. pdata->phy_link = 0;
  730. pdata->phy_speed = SPEED_UNKNOWN;
  731. }
  732. if (new_state && netif_msg_link(pdata))
  733. xgbe_phy_print_status(pdata);
  734. }
  735. static int xgbe_phy_config_fixed(struct xgbe_prv_data *pdata)
  736. {
  737. netif_dbg(pdata, link, pdata->netdev, "fixed PHY configuration\n");
  738. /* Disable auto-negotiation */
  739. xgbe_disable_an(pdata);
  740. /* Validate/Set specified speed */
  741. switch (pdata->phy.speed) {
  742. case SPEED_10000:
  743. xgbe_set_mode(pdata, XGBE_MODE_KR);
  744. break;
  745. case SPEED_2500:
  746. case SPEED_1000:
  747. xgbe_set_mode(pdata, XGBE_MODE_KX);
  748. break;
  749. default:
  750. return -EINVAL;
  751. }
  752. /* Validate duplex mode */
  753. if (pdata->phy.duplex != DUPLEX_FULL)
  754. return -EINVAL;
  755. return 0;
  756. }
  757. static int __xgbe_phy_config_aneg(struct xgbe_prv_data *pdata)
  758. {
  759. set_bit(XGBE_LINK_INIT, &pdata->dev_state);
  760. pdata->link_check = jiffies;
  761. if (pdata->phy.autoneg != AUTONEG_ENABLE)
  762. return xgbe_phy_config_fixed(pdata);
  763. netif_dbg(pdata, link, pdata->netdev, "AN PHY configuration\n");
  764. /* Disable auto-negotiation interrupt */
  765. disable_irq(pdata->an_irq);
  766. /* Start auto-negotiation in a supported mode */
  767. if (pdata->phy.advertising & ADVERTISED_10000baseKR_Full) {
  768. xgbe_set_mode(pdata, XGBE_MODE_KR);
  769. } else if ((pdata->phy.advertising & ADVERTISED_1000baseKX_Full) ||
  770. (pdata->phy.advertising & ADVERTISED_2500baseX_Full)) {
  771. xgbe_set_mode(pdata, XGBE_MODE_KX);
  772. } else {
  773. enable_irq(pdata->an_irq);
  774. return -EINVAL;
  775. }
  776. /* Disable and stop any in progress auto-negotiation */
  777. xgbe_disable_an(pdata);
  778. /* Clear any auto-negotitation interrupts */
  779. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, 0);
  780. pdata->an_result = XGBE_AN_READY;
  781. pdata->an_state = XGBE_AN_READY;
  782. pdata->kr_state = XGBE_RX_BPA;
  783. pdata->kx_state = XGBE_RX_BPA;
  784. /* Re-enable auto-negotiation interrupt */
  785. enable_irq(pdata->an_irq);
  786. /* Set up advertisement registers based on current settings */
  787. xgbe_an_init(pdata);
  788. /* Enable and start auto-negotiation */
  789. xgbe_restart_an(pdata);
  790. return 0;
  791. }
  792. static int xgbe_phy_config_aneg(struct xgbe_prv_data *pdata)
  793. {
  794. int ret;
  795. mutex_lock(&pdata->an_mutex);
  796. ret = __xgbe_phy_config_aneg(pdata);
  797. if (ret)
  798. set_bit(XGBE_LINK_ERR, &pdata->dev_state);
  799. else
  800. clear_bit(XGBE_LINK_ERR, &pdata->dev_state);
  801. mutex_unlock(&pdata->an_mutex);
  802. return ret;
  803. }
  804. static bool xgbe_phy_aneg_done(struct xgbe_prv_data *pdata)
  805. {
  806. return (pdata->an_result == XGBE_AN_COMPLETE);
  807. }
  808. static void xgbe_check_link_timeout(struct xgbe_prv_data *pdata)
  809. {
  810. unsigned long link_timeout;
  811. link_timeout = pdata->link_check + (XGBE_LINK_TIMEOUT * HZ);
  812. if (time_after(jiffies, link_timeout)) {
  813. netif_dbg(pdata, link, pdata->netdev, "AN link timeout\n");
  814. xgbe_phy_config_aneg(pdata);
  815. }
  816. }
  817. static void xgbe_phy_status_force(struct xgbe_prv_data *pdata)
  818. {
  819. if (xgbe_in_kr_mode(pdata)) {
  820. pdata->phy.speed = SPEED_10000;
  821. } else {
  822. switch (pdata->speed_set) {
  823. case XGBE_SPEEDSET_1000_10000:
  824. pdata->phy.speed = SPEED_1000;
  825. break;
  826. case XGBE_SPEEDSET_2500_10000:
  827. pdata->phy.speed = SPEED_2500;
  828. break;
  829. }
  830. }
  831. pdata->phy.duplex = DUPLEX_FULL;
  832. }
  833. static void xgbe_phy_status_aneg(struct xgbe_prv_data *pdata)
  834. {
  835. unsigned int ad_reg, lp_reg;
  836. pdata->phy.lp_advertising = 0;
  837. if ((pdata->phy.autoneg != AUTONEG_ENABLE) || pdata->parallel_detect)
  838. return xgbe_phy_status_force(pdata);
  839. pdata->phy.lp_advertising |= ADVERTISED_Autoneg;
  840. pdata->phy.lp_advertising |= ADVERTISED_Backplane;
  841. /* Compare Advertisement and Link Partner register 1 */
  842. ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
  843. lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA);
  844. if (lp_reg & 0x400)
  845. pdata->phy.lp_advertising |= ADVERTISED_Pause;
  846. if (lp_reg & 0x800)
  847. pdata->phy.lp_advertising |= ADVERTISED_Asym_Pause;
  848. if (pdata->phy.pause_autoneg) {
  849. /* Set flow control based on auto-negotiation result */
  850. pdata->phy.tx_pause = 0;
  851. pdata->phy.rx_pause = 0;
  852. if (ad_reg & lp_reg & 0x400) {
  853. pdata->phy.tx_pause = 1;
  854. pdata->phy.rx_pause = 1;
  855. } else if (ad_reg & lp_reg & 0x800) {
  856. if (ad_reg & 0x400)
  857. pdata->phy.rx_pause = 1;
  858. else if (lp_reg & 0x400)
  859. pdata->phy.tx_pause = 1;
  860. }
  861. }
  862. /* Compare Advertisement and Link Partner register 2 */
  863. ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
  864. lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 1);
  865. if (lp_reg & 0x80)
  866. pdata->phy.lp_advertising |= ADVERTISED_10000baseKR_Full;
  867. if (lp_reg & 0x20) {
  868. switch (pdata->speed_set) {
  869. case XGBE_SPEEDSET_1000_10000:
  870. pdata->phy.lp_advertising |= ADVERTISED_1000baseKX_Full;
  871. break;
  872. case XGBE_SPEEDSET_2500_10000:
  873. pdata->phy.lp_advertising |= ADVERTISED_2500baseX_Full;
  874. break;
  875. }
  876. }
  877. ad_reg &= lp_reg;
  878. if (ad_reg & 0x80) {
  879. pdata->phy.speed = SPEED_10000;
  880. xgbe_set_mode(pdata, XGBE_MODE_KR);
  881. } else if (ad_reg & 0x20) {
  882. switch (pdata->speed_set) {
  883. case XGBE_SPEEDSET_1000_10000:
  884. pdata->phy.speed = SPEED_1000;
  885. break;
  886. case XGBE_SPEEDSET_2500_10000:
  887. pdata->phy.speed = SPEED_2500;
  888. break;
  889. }
  890. xgbe_set_mode(pdata, XGBE_MODE_KX);
  891. } else {
  892. pdata->phy.speed = SPEED_UNKNOWN;
  893. }
  894. /* Compare Advertisement and Link Partner register 3 */
  895. ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
  896. lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 2);
  897. if (lp_reg & 0xc000)
  898. pdata->phy.lp_advertising |= ADVERTISED_10000baseR_FEC;
  899. pdata->phy.duplex = DUPLEX_FULL;
  900. }
  901. static void xgbe_phy_status(struct xgbe_prv_data *pdata)
  902. {
  903. unsigned int reg, link_aneg;
  904. if (test_bit(XGBE_LINK_ERR, &pdata->dev_state)) {
  905. netif_carrier_off(pdata->netdev);
  906. pdata->phy.link = 0;
  907. goto adjust_link;
  908. }
  909. link_aneg = (pdata->phy.autoneg == AUTONEG_ENABLE);
  910. /* Get the link status. Link status is latched low, so read
  911. * once to clear and then read again to get current state
  912. */
  913. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1);
  914. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1);
  915. pdata->phy.link = (reg & MDIO_STAT1_LSTATUS) ? 1 : 0;
  916. if (pdata->phy.link) {
  917. if (link_aneg && !xgbe_phy_aneg_done(pdata)) {
  918. xgbe_check_link_timeout(pdata);
  919. return;
  920. }
  921. xgbe_phy_status_aneg(pdata);
  922. if (test_bit(XGBE_LINK_INIT, &pdata->dev_state))
  923. clear_bit(XGBE_LINK_INIT, &pdata->dev_state);
  924. netif_carrier_on(pdata->netdev);
  925. } else {
  926. if (test_bit(XGBE_LINK_INIT, &pdata->dev_state)) {
  927. xgbe_check_link_timeout(pdata);
  928. if (link_aneg)
  929. return;
  930. }
  931. xgbe_phy_status_aneg(pdata);
  932. netif_carrier_off(pdata->netdev);
  933. }
  934. adjust_link:
  935. xgbe_phy_adjust_link(pdata);
  936. }
  937. static void xgbe_phy_stop(struct xgbe_prv_data *pdata)
  938. {
  939. netif_dbg(pdata, link, pdata->netdev, "stopping PHY\n");
  940. /* Disable auto-negotiation */
  941. xgbe_disable_an(pdata);
  942. /* Disable auto-negotiation interrupts */
  943. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK, 0);
  944. devm_free_irq(pdata->dev, pdata->an_irq, pdata);
  945. pdata->phy.link = 0;
  946. netif_carrier_off(pdata->netdev);
  947. xgbe_phy_adjust_link(pdata);
  948. }
  949. static int xgbe_phy_start(struct xgbe_prv_data *pdata)
  950. {
  951. struct net_device *netdev = pdata->netdev;
  952. int ret;
  953. netif_dbg(pdata, link, pdata->netdev, "starting PHY\n");
  954. ret = devm_request_irq(pdata->dev, pdata->an_irq,
  955. xgbe_an_isr, 0, pdata->an_name,
  956. pdata);
  957. if (ret) {
  958. netdev_err(netdev, "phy irq request failed\n");
  959. return ret;
  960. }
  961. /* Set initial mode - call the mode setting routines
  962. * directly to insure we are properly configured
  963. */
  964. if (xgbe_use_xgmii_mode(pdata)) {
  965. xgbe_xgmii_mode(pdata);
  966. } else if (xgbe_use_gmii_mode(pdata)) {
  967. xgbe_gmii_mode(pdata);
  968. } else if (xgbe_use_gmii_2500_mode(pdata)) {
  969. xgbe_gmii_2500_mode(pdata);
  970. } else {
  971. ret = -EINVAL;
  972. goto err_irq;
  973. }
  974. /* Set up advertisement registers based on current settings */
  975. xgbe_an_init(pdata);
  976. /* Enable auto-negotiation interrupts */
  977. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK, 0x07);
  978. return xgbe_phy_config_aneg(pdata);
  979. err_irq:
  980. devm_free_irq(pdata->dev, pdata->an_irq, pdata);
  981. return ret;
  982. }
  983. static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
  984. {
  985. unsigned int count, reg;
  986. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  987. reg |= MDIO_CTRL1_RESET;
  988. XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
  989. count = 50;
  990. do {
  991. msleep(20);
  992. reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
  993. } while ((reg & MDIO_CTRL1_RESET) && --count);
  994. if (reg & MDIO_CTRL1_RESET)
  995. return -ETIMEDOUT;
  996. /* Disable auto-negotiation for now */
  997. xgbe_disable_an(pdata);
  998. /* Clear auto-negotiation interrupts */
  999. XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, 0);
  1000. return 0;
  1001. }
  1002. static void xgbe_dump_phy_registers(struct xgbe_prv_data *pdata)
  1003. {
  1004. struct device *dev = pdata->dev;
  1005. dev_dbg(dev, "\n************* PHY Reg dump **********************\n");
  1006. dev_dbg(dev, "PCS Control Reg (%#04x) = %#04x\n", MDIO_CTRL1,
  1007. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1));
  1008. dev_dbg(dev, "PCS Status Reg (%#04x) = %#04x\n", MDIO_STAT1,
  1009. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1));
  1010. dev_dbg(dev, "Phy Id (PHYS ID 1 %#04x)= %#04x\n", MDIO_DEVID1,
  1011. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVID1));
  1012. dev_dbg(dev, "Phy Id (PHYS ID 2 %#04x)= %#04x\n", MDIO_DEVID2,
  1013. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVID2));
  1014. dev_dbg(dev, "Devices in Package (%#04x)= %#04x\n", MDIO_DEVS1,
  1015. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVS1));
  1016. dev_dbg(dev, "Devices in Package (%#04x)= %#04x\n", MDIO_DEVS2,
  1017. XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVS2));
  1018. dev_dbg(dev, "Auto-Neg Control Reg (%#04x) = %#04x\n", MDIO_CTRL1,
  1019. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_CTRL1));
  1020. dev_dbg(dev, "Auto-Neg Status Reg (%#04x) = %#04x\n", MDIO_STAT1,
  1021. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_STAT1));
  1022. dev_dbg(dev, "Auto-Neg Ad Reg 1 (%#04x) = %#04x\n",
  1023. MDIO_AN_ADVERTISE,
  1024. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE));
  1025. dev_dbg(dev, "Auto-Neg Ad Reg 2 (%#04x) = %#04x\n",
  1026. MDIO_AN_ADVERTISE + 1,
  1027. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1));
  1028. dev_dbg(dev, "Auto-Neg Ad Reg 3 (%#04x) = %#04x\n",
  1029. MDIO_AN_ADVERTISE + 2,
  1030. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2));
  1031. dev_dbg(dev, "Auto-Neg Completion Reg (%#04x) = %#04x\n",
  1032. MDIO_AN_COMP_STAT,
  1033. XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_COMP_STAT));
  1034. dev_dbg(dev, "\n*************************************************\n");
  1035. }
  1036. static void xgbe_phy_init(struct xgbe_prv_data *pdata)
  1037. {
  1038. mutex_init(&pdata->an_mutex);
  1039. INIT_WORK(&pdata->an_irq_work, xgbe_an_irq_work);
  1040. INIT_WORK(&pdata->an_work, xgbe_an_state_machine);
  1041. pdata->mdio_mmd = MDIO_MMD_PCS;
  1042. /* Initialize supported features */
  1043. pdata->phy.supported = SUPPORTED_Autoneg;
  1044. pdata->phy.supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
  1045. pdata->phy.supported |= SUPPORTED_Backplane;
  1046. pdata->phy.supported |= SUPPORTED_10000baseKR_Full;
  1047. switch (pdata->speed_set) {
  1048. case XGBE_SPEEDSET_1000_10000:
  1049. pdata->phy.supported |= SUPPORTED_1000baseKX_Full;
  1050. break;
  1051. case XGBE_SPEEDSET_2500_10000:
  1052. pdata->phy.supported |= SUPPORTED_2500baseX_Full;
  1053. break;
  1054. }
  1055. pdata->fec_ability = XMDIO_READ(pdata, MDIO_MMD_PMAPMD,
  1056. MDIO_PMA_10GBR_FECABLE);
  1057. pdata->fec_ability &= (MDIO_PMA_10GBR_FECABLE_ABLE |
  1058. MDIO_PMA_10GBR_FECABLE_ERRABLE);
  1059. if (pdata->fec_ability & MDIO_PMA_10GBR_FECABLE_ABLE)
  1060. pdata->phy.supported |= SUPPORTED_10000baseR_FEC;
  1061. pdata->phy.advertising = pdata->phy.supported;
  1062. pdata->phy.address = 0;
  1063. pdata->phy.autoneg = AUTONEG_ENABLE;
  1064. pdata->phy.speed = SPEED_UNKNOWN;
  1065. pdata->phy.duplex = DUPLEX_UNKNOWN;
  1066. pdata->phy.link = 0;
  1067. pdata->phy.pause_autoneg = pdata->pause_autoneg;
  1068. pdata->phy.tx_pause = pdata->tx_pause;
  1069. pdata->phy.rx_pause = pdata->rx_pause;
  1070. /* Fix up Flow Control advertising */
  1071. pdata->phy.advertising &= ~ADVERTISED_Pause;
  1072. pdata->phy.advertising &= ~ADVERTISED_Asym_Pause;
  1073. if (pdata->rx_pause) {
  1074. pdata->phy.advertising |= ADVERTISED_Pause;
  1075. pdata->phy.advertising |= ADVERTISED_Asym_Pause;
  1076. }
  1077. if (pdata->tx_pause)
  1078. pdata->phy.advertising ^= ADVERTISED_Asym_Pause;
  1079. if (netif_msg_drv(pdata))
  1080. xgbe_dump_phy_registers(pdata);
  1081. }
  1082. void xgbe_init_function_ptrs_phy(struct xgbe_phy_if *phy_if)
  1083. {
  1084. phy_if->phy_init = xgbe_phy_init;
  1085. phy_if->phy_reset = xgbe_phy_reset;
  1086. phy_if->phy_start = xgbe_phy_start;
  1087. phy_if->phy_stop = xgbe_phy_stop;
  1088. phy_if->phy_status = xgbe_phy_status;
  1089. phy_if->phy_config_aneg = xgbe_phy_config_aneg;
  1090. }