Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1 | /* |
| 2 | * AMD 10Gb Ethernet PHY 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 | * |
| 25 | * License 2: Modified BSD |
| 26 | * |
| 27 | * Copyright (c) 2014 Advanced Micro Devices, Inc. |
| 28 | * All rights reserved. |
| 29 | * |
| 30 | * Redistribution and use in source and binary forms, with or without |
| 31 | * modification, are permitted provided that the following conditions are met: |
| 32 | * * Redistributions of source code must retain the above copyright |
| 33 | * notice, this list of conditions and the following disclaimer. |
| 34 | * * Redistributions in binary form must reproduce the above copyright |
| 35 | * notice, this list of conditions and the following disclaimer in the |
| 36 | * documentation and/or other materials provided with the distribution. |
| 37 | * * Neither the name of Advanced Micro Devices, Inc. nor the |
| 38 | * names of its contributors may be used to endorse or promote products |
| 39 | * derived from this software without specific prior written permission. |
| 40 | * |
| 41 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 42 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY |
| 45 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 46 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 48 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 49 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 50 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | */ |
| 52 | |
| 53 | #include <linux/kernel.h> |
| 54 | #include <linux/device.h> |
| 55 | #include <linux/platform_device.h> |
| 56 | #include <linux/string.h> |
| 57 | #include <linux/errno.h> |
| 58 | #include <linux/unistd.h> |
| 59 | #include <linux/slab.h> |
| 60 | #include <linux/interrupt.h> |
| 61 | #include <linux/init.h> |
| 62 | #include <linux/delay.h> |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 63 | #include <linux/workqueue.h> |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 64 | #include <linux/netdevice.h> |
| 65 | #include <linux/etherdevice.h> |
| 66 | #include <linux/skbuff.h> |
| 67 | #include <linux/mm.h> |
| 68 | #include <linux/module.h> |
| 69 | #include <linux/mii.h> |
| 70 | #include <linux/ethtool.h> |
| 71 | #include <linux/phy.h> |
| 72 | #include <linux/mdio.h> |
| 73 | #include <linux/io.h> |
| 74 | #include <linux/of.h> |
| 75 | #include <linux/of_platform.h> |
| 76 | #include <linux/of_device.h> |
| 77 | #include <linux/uaccess.h> |
Lendacky, Thomas | cb69cb0 | 2015-01-16 12:46:29 -0600 | [diff] [blame] | 78 | #include <linux/bitops.h> |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 79 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 80 | MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>"); |
| 81 | MODULE_LICENSE("Dual BSD/GPL"); |
| 82 | MODULE_VERSION("1.0.0-a"); |
| 83 | MODULE_DESCRIPTION("AMD 10GbE (amd-xgbe) PHY driver"); |
| 84 | |
| 85 | #define XGBE_PHY_ID 0x000162d0 |
| 86 | #define XGBE_PHY_MASK 0xfffffff0 |
| 87 | |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 88 | #define XGBE_PHY_SPEEDSET_PROPERTY "amd,speed-set" |
| 89 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 90 | #define XGBE_AN_INT_CMPLT 0x01 |
| 91 | #define XGBE_AN_INC_LINK 0x02 |
| 92 | #define XGBE_AN_PG_RCV 0x04 |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 93 | #define XGBE_AN_INT_MASK 0x07 |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 94 | |
| 95 | #define XNP_MCF_NULL_MESSAGE 0x001 |
Lendacky, Thomas | cb69cb0 | 2015-01-16 12:46:29 -0600 | [diff] [blame] | 96 | #define XNP_ACK_PROCESSED BIT(12) |
| 97 | #define XNP_MP_FORMATTED BIT(13) |
| 98 | #define XNP_NP_EXCHANGE BIT(15) |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 99 | |
Lendacky, Thomas | 1fa1f2e | 2014-08-01 11:56:36 -0500 | [diff] [blame] | 100 | #define XGBE_PHY_RATECHANGE_COUNT 500 |
Lendacky, Thomas | 169a630 | 2014-07-29 08:57:37 -0500 | [diff] [blame] | 101 | |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 102 | #define XGBE_PHY_KR_TRAINING_START 0x01 |
| 103 | #define XGBE_PHY_KR_TRAINING_ENABLE 0x02 |
| 104 | |
| 105 | #define XGBE_PHY_FEC_ENABLE 0x01 |
| 106 | #define XGBE_PHY_FEC_FORWARD 0x02 |
| 107 | #define XGBE_PHY_FEC_MASK 0x03 |
| 108 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 109 | #ifndef MDIO_PMA_10GBR_PMD_CTRL |
| 110 | #define MDIO_PMA_10GBR_PMD_CTRL 0x0096 |
| 111 | #endif |
Lendacky, Thomas | e6f0562 | 2014-09-03 12:14:22 -0500 | [diff] [blame] | 112 | |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 113 | #ifndef MDIO_PMA_10GBR_FEC_ABILITY |
| 114 | #define MDIO_PMA_10GBR_FEC_ABILITY 0x00aa |
| 115 | #endif |
| 116 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 117 | #ifndef MDIO_PMA_10GBR_FEC_CTRL |
| 118 | #define MDIO_PMA_10GBR_FEC_CTRL 0x00ab |
| 119 | #endif |
Lendacky, Thomas | e6f0562 | 2014-09-03 12:14:22 -0500 | [diff] [blame] | 120 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 121 | #ifndef MDIO_AN_XNP |
| 122 | #define MDIO_AN_XNP 0x0016 |
| 123 | #endif |
| 124 | |
| 125 | #ifndef MDIO_AN_INTMASK |
| 126 | #define MDIO_AN_INTMASK 0x8001 |
| 127 | #endif |
Lendacky, Thomas | e6f0562 | 2014-09-03 12:14:22 -0500 | [diff] [blame] | 128 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 129 | #ifndef MDIO_AN_INT |
| 130 | #define MDIO_AN_INT 0x8002 |
| 131 | #endif |
| 132 | |
| 133 | #ifndef MDIO_CTRL1_SPEED1G |
| 134 | #define MDIO_CTRL1_SPEED1G (MDIO_CTRL1_SPEED10G & ~BMCR_SPEED100) |
| 135 | #endif |
| 136 | |
| 137 | /* SerDes integration register offsets */ |
Lendacky, Thomas | 5c10e5c | 2014-07-29 08:57:43 -0500 | [diff] [blame] | 138 | #define SIR0_KR_RT_1 0x002c |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 139 | #define SIR0_STATUS 0x0040 |
| 140 | #define SIR1_SPEED 0x0000 |
| 141 | |
| 142 | /* SerDes integration register entry bit positions and sizes */ |
Lendacky, Thomas | 5c10e5c | 2014-07-29 08:57:43 -0500 | [diff] [blame] | 143 | #define SIR0_KR_RT_1_RESET_INDEX 11 |
| 144 | #define SIR0_KR_RT_1_RESET_WIDTH 1 |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 145 | #define SIR0_STATUS_RX_READY_INDEX 0 |
| 146 | #define SIR0_STATUS_RX_READY_WIDTH 1 |
| 147 | #define SIR0_STATUS_TX_READY_INDEX 8 |
| 148 | #define SIR0_STATUS_TX_READY_WIDTH 1 |
| 149 | #define SIR1_SPEED_DATARATE_INDEX 4 |
| 150 | #define SIR1_SPEED_DATARATE_WIDTH 2 |
| 151 | #define SIR1_SPEED_PI_SPD_SEL_INDEX 12 |
| 152 | #define SIR1_SPEED_PI_SPD_SEL_WIDTH 4 |
| 153 | #define SIR1_SPEED_PLLSEL_INDEX 3 |
| 154 | #define SIR1_SPEED_PLLSEL_WIDTH 1 |
| 155 | #define SIR1_SPEED_RATECHANGE_INDEX 6 |
| 156 | #define SIR1_SPEED_RATECHANGE_WIDTH 1 |
| 157 | #define SIR1_SPEED_TXAMP_INDEX 8 |
| 158 | #define SIR1_SPEED_TXAMP_WIDTH 4 |
| 159 | #define SIR1_SPEED_WORDMODE_INDEX 0 |
| 160 | #define SIR1_SPEED_WORDMODE_WIDTH 3 |
| 161 | |
| 162 | #define SPEED_10000_CDR 0x7 |
| 163 | #define SPEED_10000_PLL 0x1 |
| 164 | #define SPEED_10000_RATE 0x0 |
| 165 | #define SPEED_10000_TXAMP 0xa |
| 166 | #define SPEED_10000_WORD 0x7 |
| 167 | |
| 168 | #define SPEED_2500_CDR 0x2 |
| 169 | #define SPEED_2500_PLL 0x0 |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 170 | #define SPEED_2500_RATE 0x1 |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 171 | #define SPEED_2500_TXAMP 0xf |
| 172 | #define SPEED_2500_WORD 0x1 |
| 173 | |
| 174 | #define SPEED_1000_CDR 0x2 |
| 175 | #define SPEED_1000_PLL 0x0 |
| 176 | #define SPEED_1000_RATE 0x3 |
| 177 | #define SPEED_1000_TXAMP 0xf |
| 178 | #define SPEED_1000_WORD 0x1 |
| 179 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 180 | /* SerDes RxTx register offsets */ |
| 181 | #define RXTX_REG20 0x0050 |
| 182 | #define RXTX_REG114 0x01c8 |
| 183 | |
| 184 | /* SerDes RxTx register entry bit positions and sizes */ |
| 185 | #define RXTX_REG20_BLWC_ENA_INDEX 2 |
| 186 | #define RXTX_REG20_BLWC_ENA_WIDTH 1 |
| 187 | #define RXTX_REG114_PQ_REG_INDEX 9 |
| 188 | #define RXTX_REG114_PQ_REG_WIDTH 7 |
| 189 | |
| 190 | #define RXTX_10000_BLWC 0 |
| 191 | #define RXTX_10000_PQ 0x1e |
| 192 | |
| 193 | #define RXTX_2500_BLWC 1 |
| 194 | #define RXTX_2500_PQ 0xa |
| 195 | |
| 196 | #define RXTX_1000_BLWC 1 |
| 197 | #define RXTX_1000_PQ 0xa |
| 198 | |
| 199 | /* Bit setting and getting macros |
| 200 | * The get macro will extract the current bit field value from within |
| 201 | * the variable |
| 202 | * |
| 203 | * The set macro will clear the current bit field value within the |
| 204 | * variable and then set the bit field of the variable to the |
| 205 | * specified value |
| 206 | */ |
| 207 | #define GET_BITS(_var, _index, _width) \ |
| 208 | (((_var) >> (_index)) & ((0x1 << (_width)) - 1)) |
| 209 | |
| 210 | #define SET_BITS(_var, _index, _width, _val) \ |
| 211 | do { \ |
| 212 | (_var) &= ~(((0x1 << (_width)) - 1) << (_index)); \ |
| 213 | (_var) |= (((_val) & ((0x1 << (_width)) - 1)) << (_index)); \ |
| 214 | } while (0) |
| 215 | |
Lendacky, Thomas | 169a630 | 2014-07-29 08:57:37 -0500 | [diff] [blame] | 216 | #define XSIR_GET_BITS(_var, _prefix, _field) \ |
| 217 | GET_BITS((_var), \ |
| 218 | _prefix##_##_field##_INDEX, \ |
| 219 | _prefix##_##_field##_WIDTH) |
| 220 | |
| 221 | #define XSIR_SET_BITS(_var, _prefix, _field, _val) \ |
| 222 | SET_BITS((_var), \ |
| 223 | _prefix##_##_field##_INDEX, \ |
| 224 | _prefix##_##_field##_WIDTH, (_val)) |
| 225 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 226 | /* Macros for reading or writing SerDes integration registers |
| 227 | * The ioread macros will get bit fields or full values using the |
| 228 | * register definitions formed using the input names |
| 229 | * |
| 230 | * The iowrite macros will set bit fields or full values using the |
| 231 | * register definitions formed using the input names |
| 232 | */ |
| 233 | #define XSIR0_IOREAD(_priv, _reg) \ |
| 234 | ioread16((_priv)->sir0_regs + _reg) |
| 235 | |
| 236 | #define XSIR0_IOREAD_BITS(_priv, _reg, _field) \ |
| 237 | GET_BITS(XSIR0_IOREAD((_priv), _reg), \ |
| 238 | _reg##_##_field##_INDEX, \ |
| 239 | _reg##_##_field##_WIDTH) |
| 240 | |
| 241 | #define XSIR0_IOWRITE(_priv, _reg, _val) \ |
| 242 | iowrite16((_val), (_priv)->sir0_regs + _reg) |
| 243 | |
| 244 | #define XSIR0_IOWRITE_BITS(_priv, _reg, _field, _val) \ |
| 245 | do { \ |
| 246 | u16 reg_val = XSIR0_IOREAD((_priv), _reg); \ |
| 247 | SET_BITS(reg_val, \ |
| 248 | _reg##_##_field##_INDEX, \ |
| 249 | _reg##_##_field##_WIDTH, (_val)); \ |
| 250 | XSIR0_IOWRITE((_priv), _reg, reg_val); \ |
| 251 | } while (0) |
| 252 | |
| 253 | #define XSIR1_IOREAD(_priv, _reg) \ |
| 254 | ioread16((_priv)->sir1_regs + _reg) |
| 255 | |
| 256 | #define XSIR1_IOREAD_BITS(_priv, _reg, _field) \ |
| 257 | GET_BITS(XSIR1_IOREAD((_priv), _reg), \ |
| 258 | _reg##_##_field##_INDEX, \ |
| 259 | _reg##_##_field##_WIDTH) |
| 260 | |
| 261 | #define XSIR1_IOWRITE(_priv, _reg, _val) \ |
| 262 | iowrite16((_val), (_priv)->sir1_regs + _reg) |
| 263 | |
| 264 | #define XSIR1_IOWRITE_BITS(_priv, _reg, _field, _val) \ |
| 265 | do { \ |
| 266 | u16 reg_val = XSIR1_IOREAD((_priv), _reg); \ |
| 267 | SET_BITS(reg_val, \ |
| 268 | _reg##_##_field##_INDEX, \ |
| 269 | _reg##_##_field##_WIDTH, (_val)); \ |
| 270 | XSIR1_IOWRITE((_priv), _reg, reg_val); \ |
| 271 | } while (0) |
| 272 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 273 | /* Macros for reading or writing SerDes RxTx registers |
| 274 | * The ioread macros will get bit fields or full values using the |
| 275 | * register definitions formed using the input names |
| 276 | * |
| 277 | * The iowrite macros will set bit fields or full values using the |
| 278 | * register definitions formed using the input names |
| 279 | */ |
| 280 | #define XRXTX_IOREAD(_priv, _reg) \ |
| 281 | ioread16((_priv)->rxtx_regs + _reg) |
| 282 | |
| 283 | #define XRXTX_IOREAD_BITS(_priv, _reg, _field) \ |
| 284 | GET_BITS(XRXTX_IOREAD((_priv), _reg), \ |
| 285 | _reg##_##_field##_INDEX, \ |
| 286 | _reg##_##_field##_WIDTH) |
| 287 | |
| 288 | #define XRXTX_IOWRITE(_priv, _reg, _val) \ |
| 289 | iowrite16((_val), (_priv)->rxtx_regs + _reg) |
| 290 | |
| 291 | #define XRXTX_IOWRITE_BITS(_priv, _reg, _field, _val) \ |
| 292 | do { \ |
| 293 | u16 reg_val = XRXTX_IOREAD((_priv), _reg); \ |
| 294 | SET_BITS(reg_val, \ |
| 295 | _reg##_##_field##_INDEX, \ |
| 296 | _reg##_##_field##_WIDTH, (_val)); \ |
| 297 | XRXTX_IOWRITE((_priv), _reg, reg_val); \ |
| 298 | } while (0) |
| 299 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 300 | enum amd_xgbe_phy_an { |
| 301 | AMD_XGBE_AN_READY = 0, |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 302 | AMD_XGBE_AN_PAGE_RECEIVED, |
| 303 | AMD_XGBE_AN_INCOMPAT_LINK, |
| 304 | AMD_XGBE_AN_COMPLETE, |
| 305 | AMD_XGBE_AN_NO_LINK, |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 306 | AMD_XGBE_AN_ERROR, |
| 307 | }; |
| 308 | |
| 309 | enum amd_xgbe_phy_rx { |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 310 | AMD_XGBE_RX_BPA = 0, |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 311 | AMD_XGBE_RX_XNP, |
| 312 | AMD_XGBE_RX_COMPLETE, |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 313 | AMD_XGBE_RX_ERROR, |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | enum amd_xgbe_phy_mode { |
| 317 | AMD_XGBE_MODE_KR, |
| 318 | AMD_XGBE_MODE_KX, |
| 319 | }; |
| 320 | |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 321 | enum amd_xgbe_phy_speedset { |
| 322 | AMD_XGBE_PHY_SPEEDSET_1000_10000, |
| 323 | AMD_XGBE_PHY_SPEEDSET_2500_10000, |
| 324 | }; |
| 325 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 326 | struct amd_xgbe_phy_priv { |
| 327 | struct platform_device *pdev; |
| 328 | struct device *dev; |
| 329 | |
| 330 | struct phy_device *phydev; |
| 331 | |
| 332 | /* SerDes related mmio resources */ |
| 333 | struct resource *rxtx_res; |
| 334 | struct resource *sir0_res; |
| 335 | struct resource *sir1_res; |
| 336 | |
| 337 | /* SerDes related mmio registers */ |
| 338 | void __iomem *rxtx_regs; /* SerDes Rx/Tx CSRs */ |
| 339 | void __iomem *sir0_regs; /* SerDes integration registers (1/2) */ |
| 340 | void __iomem *sir1_regs; /* SerDes integration registers (2/2) */ |
| 341 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 342 | int an_irq; |
| 343 | char an_irq_name[IFNAMSIZ + 32]; |
| 344 | struct work_struct an_irq_work; |
| 345 | unsigned int an_irq_allocated; |
| 346 | |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 347 | unsigned int speed_set; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 348 | |
| 349 | /* Auto-negotiation state machine support */ |
| 350 | struct mutex an_mutex; |
| 351 | enum amd_xgbe_phy_an an_result; |
| 352 | enum amd_xgbe_phy_an an_state; |
| 353 | enum amd_xgbe_phy_rx kr_state; |
| 354 | enum amd_xgbe_phy_rx kx_state; |
| 355 | struct work_struct an_work; |
| 356 | struct workqueue_struct *an_workqueue; |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 357 | unsigned int an_supported; |
Lendacky, Thomas | e6f0562 | 2014-09-03 12:14:22 -0500 | [diff] [blame] | 358 | unsigned int parallel_detect; |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 359 | unsigned int fec_ability; |
Lendacky, Thomas | 03e50fd | 2015-01-16 12:46:39 -0600 | [diff] [blame] | 360 | |
| 361 | unsigned int lpm_ctrl; /* CTRL1 for resume */ |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 362 | }; |
| 363 | |
| 364 | static int amd_xgbe_an_enable_kr_training(struct phy_device *phydev) |
| 365 | { |
| 366 | int ret; |
| 367 | |
| 368 | ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL); |
| 369 | if (ret < 0) |
| 370 | return ret; |
| 371 | |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 372 | ret |= XGBE_PHY_KR_TRAINING_ENABLE; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 373 | phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret); |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | static int amd_xgbe_an_disable_kr_training(struct phy_device *phydev) |
| 379 | { |
| 380 | int ret; |
| 381 | |
| 382 | ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL); |
| 383 | if (ret < 0) |
| 384 | return ret; |
| 385 | |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 386 | ret &= ~XGBE_PHY_KR_TRAINING_ENABLE; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 387 | phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret); |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static int amd_xgbe_phy_pcs_power_cycle(struct phy_device *phydev) |
| 393 | { |
| 394 | int ret; |
| 395 | |
| 396 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); |
| 397 | if (ret < 0) |
| 398 | return ret; |
| 399 | |
| 400 | ret |= MDIO_CTRL1_LPOWER; |
| 401 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret); |
| 402 | |
| 403 | usleep_range(75, 100); |
| 404 | |
| 405 | ret &= ~MDIO_CTRL1_LPOWER; |
| 406 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static void amd_xgbe_phy_serdes_start_ratechange(struct phy_device *phydev) |
| 412 | { |
| 413 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
| 414 | |
| 415 | /* Assert Rx and Tx ratechange */ |
| 416 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 1); |
| 417 | } |
| 418 | |
| 419 | static void amd_xgbe_phy_serdes_complete_ratechange(struct phy_device *phydev) |
| 420 | { |
| 421 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
Lendacky, Thomas | 169a630 | 2014-07-29 08:57:37 -0500 | [diff] [blame] | 422 | unsigned int wait; |
| 423 | u16 status; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 424 | |
| 425 | /* Release Rx and Tx ratechange */ |
| 426 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 0); |
| 427 | |
| 428 | /* Wait for Rx and Tx ready */ |
Lendacky, Thomas | 169a630 | 2014-07-29 08:57:37 -0500 | [diff] [blame] | 429 | wait = XGBE_PHY_RATECHANGE_COUNT; |
| 430 | while (wait--) { |
Lendacky, Thomas | 1fa1f2e | 2014-08-01 11:56:36 -0500 | [diff] [blame] | 431 | usleep_range(50, 75); |
Lendacky, Thomas | 169a630 | 2014-07-29 08:57:37 -0500 | [diff] [blame] | 432 | |
| 433 | status = XSIR0_IOREAD(priv, SIR0_STATUS); |
| 434 | if (XSIR_GET_BITS(status, SIR0_STATUS, RX_READY) && |
| 435 | XSIR_GET_BITS(status, SIR0_STATUS, TX_READY)) |
| 436 | return; |
| 437 | } |
| 438 | |
Lendacky, Thomas | 1fa1f2e | 2014-08-01 11:56:36 -0500 | [diff] [blame] | 439 | netdev_dbg(phydev->attached_dev, "SerDes rx/tx not ready (%#hx)\n", |
Lendacky, Thomas | 169a630 | 2014-07-29 08:57:37 -0500 | [diff] [blame] | 440 | status); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static int amd_xgbe_phy_xgmii_mode(struct phy_device *phydev) |
| 444 | { |
| 445 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
| 446 | int ret; |
| 447 | |
| 448 | /* Enable KR training */ |
| 449 | ret = amd_xgbe_an_enable_kr_training(phydev); |
| 450 | if (ret < 0) |
| 451 | return ret; |
| 452 | |
| 453 | /* Set PCS to KR/10G speed */ |
| 454 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2); |
| 455 | if (ret < 0) |
| 456 | return ret; |
| 457 | |
| 458 | ret &= ~MDIO_PCS_CTRL2_TYPE; |
| 459 | ret |= MDIO_PCS_CTRL2_10GBR; |
| 460 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret); |
| 461 | |
| 462 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); |
| 463 | if (ret < 0) |
| 464 | return ret; |
| 465 | |
| 466 | ret &= ~MDIO_CTRL1_SPEEDSEL; |
| 467 | ret |= MDIO_CTRL1_SPEED10G; |
| 468 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret); |
| 469 | |
| 470 | ret = amd_xgbe_phy_pcs_power_cycle(phydev); |
| 471 | if (ret < 0) |
| 472 | return ret; |
| 473 | |
| 474 | /* Set SerDes to 10G speed */ |
| 475 | amd_xgbe_phy_serdes_start_ratechange(phydev); |
| 476 | |
| 477 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_10000_RATE); |
| 478 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_10000_WORD); |
| 479 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_10000_TXAMP); |
| 480 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_10000_PLL); |
| 481 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_10000_CDR); |
| 482 | |
| 483 | XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_10000_BLWC); |
| 484 | XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_10000_PQ); |
| 485 | |
| 486 | amd_xgbe_phy_serdes_complete_ratechange(phydev); |
| 487 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static int amd_xgbe_phy_gmii_2500_mode(struct phy_device *phydev) |
| 492 | { |
| 493 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
| 494 | int ret; |
| 495 | |
| 496 | /* Disable KR training */ |
| 497 | ret = amd_xgbe_an_disable_kr_training(phydev); |
| 498 | if (ret < 0) |
| 499 | return ret; |
| 500 | |
| 501 | /* Set PCS to KX/1G speed */ |
| 502 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2); |
| 503 | if (ret < 0) |
| 504 | return ret; |
| 505 | |
| 506 | ret &= ~MDIO_PCS_CTRL2_TYPE; |
| 507 | ret |= MDIO_PCS_CTRL2_10GBX; |
| 508 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret); |
| 509 | |
| 510 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); |
| 511 | if (ret < 0) |
| 512 | return ret; |
| 513 | |
| 514 | ret &= ~MDIO_CTRL1_SPEEDSEL; |
| 515 | ret |= MDIO_CTRL1_SPEED1G; |
| 516 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret); |
| 517 | |
| 518 | ret = amd_xgbe_phy_pcs_power_cycle(phydev); |
| 519 | if (ret < 0) |
| 520 | return ret; |
| 521 | |
| 522 | /* Set SerDes to 2.5G speed */ |
| 523 | amd_xgbe_phy_serdes_start_ratechange(phydev); |
| 524 | |
| 525 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_2500_RATE); |
| 526 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_2500_WORD); |
| 527 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_2500_TXAMP); |
| 528 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_2500_PLL); |
| 529 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_2500_CDR); |
| 530 | |
| 531 | XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_2500_BLWC); |
| 532 | XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_2500_PQ); |
| 533 | |
| 534 | amd_xgbe_phy_serdes_complete_ratechange(phydev); |
| 535 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static int amd_xgbe_phy_gmii_mode(struct phy_device *phydev) |
| 540 | { |
| 541 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
| 542 | int ret; |
| 543 | |
| 544 | /* Disable KR training */ |
| 545 | ret = amd_xgbe_an_disable_kr_training(phydev); |
| 546 | if (ret < 0) |
| 547 | return ret; |
| 548 | |
| 549 | /* Set PCS to KX/1G speed */ |
| 550 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2); |
| 551 | if (ret < 0) |
| 552 | return ret; |
| 553 | |
| 554 | ret &= ~MDIO_PCS_CTRL2_TYPE; |
| 555 | ret |= MDIO_PCS_CTRL2_10GBX; |
| 556 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret); |
| 557 | |
| 558 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); |
| 559 | if (ret < 0) |
| 560 | return ret; |
| 561 | |
| 562 | ret &= ~MDIO_CTRL1_SPEEDSEL; |
| 563 | ret |= MDIO_CTRL1_SPEED1G; |
| 564 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret); |
| 565 | |
| 566 | ret = amd_xgbe_phy_pcs_power_cycle(phydev); |
| 567 | if (ret < 0) |
| 568 | return ret; |
| 569 | |
| 570 | /* Set SerDes to 1G speed */ |
| 571 | amd_xgbe_phy_serdes_start_ratechange(phydev); |
| 572 | |
| 573 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_1000_RATE); |
| 574 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_1000_WORD); |
| 575 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_1000_TXAMP); |
| 576 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_1000_PLL); |
| 577 | XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_1000_CDR); |
| 578 | |
| 579 | XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_1000_BLWC); |
| 580 | XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_1000_PQ); |
| 581 | |
| 582 | amd_xgbe_phy_serdes_complete_ratechange(phydev); |
| 583 | |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | static int amd_xgbe_phy_cur_mode(struct phy_device *phydev, |
| 588 | enum amd_xgbe_phy_mode *mode) |
| 589 | { |
| 590 | int ret; |
| 591 | |
| 592 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2); |
| 593 | if (ret < 0) |
| 594 | return ret; |
| 595 | |
| 596 | if ((ret & MDIO_PCS_CTRL2_TYPE) == MDIO_PCS_CTRL2_10GBR) |
| 597 | *mode = AMD_XGBE_MODE_KR; |
| 598 | else |
| 599 | *mode = AMD_XGBE_MODE_KX; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 604 | static bool amd_xgbe_phy_in_kr_mode(struct phy_device *phydev) |
| 605 | { |
| 606 | enum amd_xgbe_phy_mode mode; |
| 607 | |
| 608 | if (amd_xgbe_phy_cur_mode(phydev, &mode)) |
| 609 | return false; |
| 610 | |
| 611 | return (mode == AMD_XGBE_MODE_KR); |
| 612 | } |
| 613 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 614 | static int amd_xgbe_phy_switch_mode(struct phy_device *phydev) |
| 615 | { |
| 616 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
| 617 | int ret; |
| 618 | |
| 619 | /* If we are in KR switch to KX, and vice-versa */ |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 620 | if (amd_xgbe_phy_in_kr_mode(phydev)) { |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 621 | if (priv->speed_set == AMD_XGBE_PHY_SPEEDSET_1000_10000) |
| 622 | ret = amd_xgbe_phy_gmii_mode(phydev); |
| 623 | else |
| 624 | ret = amd_xgbe_phy_gmii_2500_mode(phydev); |
| 625 | } else { |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 626 | ret = amd_xgbe_phy_xgmii_mode(phydev); |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 627 | } |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 632 | static int amd_xgbe_phy_set_mode(struct phy_device *phydev, |
| 633 | enum amd_xgbe_phy_mode mode) |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 634 | { |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 635 | enum amd_xgbe_phy_mode cur_mode; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 636 | int ret; |
| 637 | |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 638 | ret = amd_xgbe_phy_cur_mode(phydev, &cur_mode); |
| 639 | if (ret) |
| 640 | return ret; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 641 | |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 642 | if (mode != cur_mode) |
| 643 | ret = amd_xgbe_phy_switch_mode(phydev); |
| 644 | |
| 645 | return ret; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 646 | } |
| 647 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 648 | static int amd_xgbe_phy_set_an(struct phy_device *phydev, bool enable, |
| 649 | bool restart) |
| 650 | { |
| 651 | int ret; |
| 652 | |
| 653 | ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1); |
| 654 | if (ret < 0) |
| 655 | return ret; |
| 656 | |
| 657 | ret &= ~MDIO_AN_CTRL1_ENABLE; |
| 658 | |
| 659 | if (enable) |
| 660 | ret |= MDIO_AN_CTRL1_ENABLE; |
| 661 | |
| 662 | if (restart) |
| 663 | ret |= MDIO_AN_CTRL1_RESTART; |
| 664 | |
| 665 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, ret); |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int amd_xgbe_phy_restart_an(struct phy_device *phydev) |
| 671 | { |
| 672 | return amd_xgbe_phy_set_an(phydev, true, true); |
| 673 | } |
| 674 | |
| 675 | static int amd_xgbe_phy_disable_an(struct phy_device *phydev) |
| 676 | { |
| 677 | return amd_xgbe_phy_set_an(phydev, false, false); |
| 678 | } |
| 679 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 680 | static enum amd_xgbe_phy_an amd_xgbe_an_tx_training(struct phy_device *phydev, |
| 681 | enum amd_xgbe_phy_rx *state) |
| 682 | { |
Tom Lendacky | a42f5c1 | 2014-09-07 09:54:41 -0500 | [diff] [blame] | 683 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 684 | int ad_reg, lp_reg, ret; |
| 685 | |
| 686 | *state = AMD_XGBE_RX_COMPLETE; |
| 687 | |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 688 | /* If we're not in KR mode then we're done */ |
| 689 | if (!amd_xgbe_phy_in_kr_mode(phydev)) |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 690 | return AMD_XGBE_AN_PAGE_RECEIVED; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 691 | |
| 692 | /* Enable/Disable FEC */ |
| 693 | ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2); |
| 694 | if (ad_reg < 0) |
| 695 | return AMD_XGBE_AN_ERROR; |
| 696 | |
| 697 | lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 2); |
| 698 | if (lp_reg < 0) |
| 699 | return AMD_XGBE_AN_ERROR; |
| 700 | |
| 701 | ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL); |
| 702 | if (ret < 0) |
| 703 | return AMD_XGBE_AN_ERROR; |
| 704 | |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 705 | ret &= ~XGBE_PHY_FEC_MASK; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 706 | if ((ad_reg & 0xc000) && (lp_reg & 0xc000)) |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 707 | ret |= priv->fec_ability; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 708 | |
| 709 | phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL, ret); |
| 710 | |
| 711 | /* Start KR training */ |
| 712 | ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL); |
| 713 | if (ret < 0) |
| 714 | return AMD_XGBE_AN_ERROR; |
| 715 | |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 716 | if (ret & XGBE_PHY_KR_TRAINING_ENABLE) { |
| 717 | XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 1); |
Lendacky, Thomas | 5c10e5c | 2014-07-29 08:57:43 -0500 | [diff] [blame] | 718 | |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 719 | ret |= XGBE_PHY_KR_TRAINING_START; |
| 720 | phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, |
| 721 | ret); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 722 | |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 723 | XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 0); |
| 724 | } |
Lendacky, Thomas | 5c10e5c | 2014-07-29 08:57:43 -0500 | [diff] [blame] | 725 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 726 | return AMD_XGBE_AN_PAGE_RECEIVED; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static enum amd_xgbe_phy_an amd_xgbe_an_tx_xnp(struct phy_device *phydev, |
| 730 | enum amd_xgbe_phy_rx *state) |
| 731 | { |
| 732 | u16 msg; |
| 733 | |
| 734 | *state = AMD_XGBE_RX_XNP; |
| 735 | |
| 736 | msg = XNP_MCF_NULL_MESSAGE; |
| 737 | msg |= XNP_MP_FORMATTED; |
| 738 | |
| 739 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 2, 0); |
| 740 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 1, 0); |
| 741 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP, msg); |
| 742 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 743 | return AMD_XGBE_AN_PAGE_RECEIVED; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | static enum amd_xgbe_phy_an amd_xgbe_an_rx_bpa(struct phy_device *phydev, |
| 747 | enum amd_xgbe_phy_rx *state) |
| 748 | { |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 749 | unsigned int link_support; |
| 750 | int ret, ad_reg, lp_reg; |
| 751 | |
| 752 | /* Read Base Ability register 2 first */ |
| 753 | ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1); |
| 754 | if (ret < 0) |
| 755 | return AMD_XGBE_AN_ERROR; |
| 756 | |
| 757 | /* Check for a supported mode, otherwise restart in a different one */ |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 758 | link_support = amd_xgbe_phy_in_kr_mode(phydev) ? 0x80 : 0x20; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 759 | if (!(ret & link_support)) |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 760 | return AMD_XGBE_AN_INCOMPAT_LINK; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 761 | |
| 762 | /* Check Extended Next Page support */ |
| 763 | ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE); |
| 764 | if (ad_reg < 0) |
| 765 | return AMD_XGBE_AN_ERROR; |
| 766 | |
| 767 | lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA); |
| 768 | if (lp_reg < 0) |
| 769 | return AMD_XGBE_AN_ERROR; |
| 770 | |
| 771 | return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ? |
| 772 | amd_xgbe_an_tx_xnp(phydev, state) : |
| 773 | amd_xgbe_an_tx_training(phydev, state); |
| 774 | } |
| 775 | |
| 776 | static enum amd_xgbe_phy_an amd_xgbe_an_rx_xnp(struct phy_device *phydev, |
| 777 | enum amd_xgbe_phy_rx *state) |
| 778 | { |
| 779 | int ad_reg, lp_reg; |
| 780 | |
| 781 | /* Check Extended Next Page support */ |
| 782 | ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE); |
| 783 | if (ad_reg < 0) |
| 784 | return AMD_XGBE_AN_ERROR; |
| 785 | |
| 786 | lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA); |
| 787 | if (lp_reg < 0) |
| 788 | return AMD_XGBE_AN_ERROR; |
| 789 | |
| 790 | return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ? |
| 791 | amd_xgbe_an_tx_xnp(phydev, state) : |
| 792 | amd_xgbe_an_tx_training(phydev, state); |
| 793 | } |
| 794 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 795 | static enum amd_xgbe_phy_an amd_xgbe_an_page_received(struct phy_device *phydev) |
| 796 | { |
| 797 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
| 798 | enum amd_xgbe_phy_rx *state; |
| 799 | int ret; |
| 800 | |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 801 | state = amd_xgbe_phy_in_kr_mode(phydev) ? &priv->kr_state |
| 802 | : &priv->kx_state; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 803 | |
| 804 | switch (*state) { |
| 805 | case AMD_XGBE_RX_BPA: |
| 806 | ret = amd_xgbe_an_rx_bpa(phydev, state); |
| 807 | break; |
| 808 | |
| 809 | case AMD_XGBE_RX_XNP: |
| 810 | ret = amd_xgbe_an_rx_xnp(phydev, state); |
| 811 | break; |
| 812 | |
| 813 | default: |
| 814 | ret = AMD_XGBE_AN_ERROR; |
| 815 | } |
| 816 | |
| 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | static enum amd_xgbe_phy_an amd_xgbe_an_incompat_link(struct phy_device *phydev) |
| 821 | { |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 822 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 823 | int ret; |
| 824 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 825 | /* Be sure we aren't looping trying to negotiate */ |
| 826 | if (amd_xgbe_phy_in_kr_mode(phydev)) { |
| 827 | priv->kr_state = AMD_XGBE_RX_ERROR; |
| 828 | |
| 829 | if (!(phydev->supported & SUPPORTED_1000baseKX_Full) && |
| 830 | !(phydev->supported & SUPPORTED_2500baseX_Full)) |
| 831 | return AMD_XGBE_AN_NO_LINK; |
| 832 | |
| 833 | if (priv->kx_state != AMD_XGBE_RX_BPA) |
| 834 | return AMD_XGBE_AN_NO_LINK; |
| 835 | } else { |
| 836 | priv->kx_state = AMD_XGBE_RX_ERROR; |
| 837 | |
| 838 | if (!(phydev->supported & SUPPORTED_10000baseKR_Full)) |
| 839 | return AMD_XGBE_AN_NO_LINK; |
| 840 | |
| 841 | if (priv->kr_state != AMD_XGBE_RX_BPA) |
| 842 | return AMD_XGBE_AN_NO_LINK; |
| 843 | } |
| 844 | |
| 845 | ret = amd_xgbe_phy_disable_an(phydev); |
| 846 | if (ret) |
| 847 | return AMD_XGBE_AN_ERROR; |
| 848 | |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 849 | ret = amd_xgbe_phy_switch_mode(phydev); |
| 850 | if (ret) |
| 851 | return AMD_XGBE_AN_ERROR; |
| 852 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 853 | ret = amd_xgbe_phy_restart_an(phydev); |
| 854 | if (ret) |
| 855 | return AMD_XGBE_AN_ERROR; |
| 856 | |
| 857 | return AMD_XGBE_AN_INCOMPAT_LINK; |
| 858 | } |
| 859 | |
| 860 | static irqreturn_t amd_xgbe_an_isr(int irq, void *data) |
| 861 | { |
| 862 | struct amd_xgbe_phy_priv *priv = (struct amd_xgbe_phy_priv *)data; |
| 863 | |
| 864 | /* Interrupt reason must be read and cleared outside of IRQ context */ |
| 865 | disable_irq_nosync(priv->an_irq); |
| 866 | |
| 867 | queue_work(priv->an_workqueue, &priv->an_irq_work); |
| 868 | |
| 869 | return IRQ_HANDLED; |
| 870 | } |
| 871 | |
| 872 | static void amd_xgbe_an_irq_work(struct work_struct *work) |
| 873 | { |
| 874 | struct amd_xgbe_phy_priv *priv = container_of(work, |
| 875 | struct amd_xgbe_phy_priv, |
| 876 | an_irq_work); |
| 877 | |
| 878 | /* Avoid a race between enabling the IRQ and exiting the work by |
| 879 | * waiting for the work to finish and then queueing it |
| 880 | */ |
| 881 | flush_work(&priv->an_work); |
| 882 | queue_work(priv->an_workqueue, &priv->an_work); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | static void amd_xgbe_an_state_machine(struct work_struct *work) |
| 886 | { |
| 887 | struct amd_xgbe_phy_priv *priv = container_of(work, |
| 888 | struct amd_xgbe_phy_priv, |
| 889 | an_work); |
| 890 | struct phy_device *phydev = priv->phydev; |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 891 | enum amd_xgbe_phy_an cur_state = priv->an_state; |
| 892 | int int_reg, int_mask; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 893 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 894 | mutex_lock(&priv->an_mutex); |
| 895 | |
| 896 | /* Read the interrupt */ |
| 897 | int_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT); |
| 898 | if (!int_reg) |
| 899 | goto out; |
| 900 | |
| 901 | next_int: |
| 902 | if (int_reg < 0) { |
Lendacky, Thomas | e6f0562 | 2014-09-03 12:14:22 -0500 | [diff] [blame] | 903 | priv->an_state = AMD_XGBE_AN_ERROR; |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 904 | int_mask = XGBE_AN_INT_MASK; |
| 905 | } else if (int_reg & XGBE_AN_PG_RCV) { |
| 906 | priv->an_state = AMD_XGBE_AN_PAGE_RECEIVED; |
| 907 | int_mask = XGBE_AN_PG_RCV; |
| 908 | } else if (int_reg & XGBE_AN_INC_LINK) { |
| 909 | priv->an_state = AMD_XGBE_AN_INCOMPAT_LINK; |
| 910 | int_mask = XGBE_AN_INC_LINK; |
| 911 | } else if (int_reg & XGBE_AN_INT_CMPLT) { |
| 912 | priv->an_state = AMD_XGBE_AN_COMPLETE; |
| 913 | int_mask = XGBE_AN_INT_CMPLT; |
| 914 | } else { |
| 915 | priv->an_state = AMD_XGBE_AN_ERROR; |
| 916 | int_mask = 0; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 917 | } |
| 918 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 919 | /* Clear the interrupt to be processed */ |
| 920 | int_reg &= ~int_mask; |
| 921 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, int_reg); |
| 922 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 923 | priv->an_result = priv->an_state; |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 924 | |
| 925 | again: |
| 926 | cur_state = priv->an_state; |
| 927 | |
| 928 | switch (priv->an_state) { |
| 929 | case AMD_XGBE_AN_READY: |
| 930 | priv->an_supported = 0; |
| 931 | break; |
| 932 | |
| 933 | case AMD_XGBE_AN_PAGE_RECEIVED: |
| 934 | priv->an_state = amd_xgbe_an_page_received(phydev); |
| 935 | priv->an_supported++; |
| 936 | break; |
| 937 | |
| 938 | case AMD_XGBE_AN_INCOMPAT_LINK: |
| 939 | priv->an_supported = 0; |
| 940 | priv->parallel_detect = 0; |
| 941 | priv->an_state = amd_xgbe_an_incompat_link(phydev); |
| 942 | break; |
| 943 | |
| 944 | case AMD_XGBE_AN_COMPLETE: |
| 945 | priv->parallel_detect = priv->an_supported ? 0 : 1; |
| 946 | netdev_dbg(phydev->attached_dev, "%s successful\n", |
| 947 | priv->an_supported ? "Auto negotiation" |
| 948 | : "Parallel detection"); |
| 949 | break; |
| 950 | |
| 951 | case AMD_XGBE_AN_NO_LINK: |
| 952 | break; |
| 953 | |
| 954 | default: |
| 955 | priv->an_state = AMD_XGBE_AN_ERROR; |
| 956 | } |
| 957 | |
| 958 | if (priv->an_state == AMD_XGBE_AN_NO_LINK) { |
| 959 | int_reg = 0; |
| 960 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0); |
| 961 | } else if (priv->an_state == AMD_XGBE_AN_ERROR) { |
| 962 | netdev_err(phydev->attached_dev, |
| 963 | "error during auto-negotiation, state=%u\n", |
| 964 | cur_state); |
| 965 | |
| 966 | int_reg = 0; |
| 967 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0); |
| 968 | } |
| 969 | |
| 970 | if (priv->an_state >= AMD_XGBE_AN_COMPLETE) { |
| 971 | priv->an_result = priv->an_state; |
| 972 | priv->an_state = AMD_XGBE_AN_READY; |
| 973 | priv->kr_state = AMD_XGBE_RX_BPA; |
| 974 | priv->kx_state = AMD_XGBE_RX_BPA; |
| 975 | } |
| 976 | |
| 977 | if (cur_state != priv->an_state) |
| 978 | goto again; |
| 979 | |
| 980 | if (int_reg) |
| 981 | goto next_int; |
| 982 | |
| 983 | out: |
| 984 | enable_irq(priv->an_irq); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 985 | |
| 986 | mutex_unlock(&priv->an_mutex); |
| 987 | } |
| 988 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 989 | static int amd_xgbe_an_init(struct phy_device *phydev) |
| 990 | { |
| 991 | int ret; |
| 992 | |
| 993 | /* Set up Advertisement register 3 first */ |
| 994 | ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2); |
| 995 | if (ret < 0) |
| 996 | return ret; |
| 997 | |
| 998 | if (phydev->supported & SUPPORTED_10000baseR_FEC) |
| 999 | ret |= 0xc000; |
| 1000 | else |
| 1001 | ret &= ~0xc000; |
| 1002 | |
| 1003 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2, ret); |
| 1004 | |
| 1005 | /* Set up Advertisement register 2 next */ |
| 1006 | ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1); |
| 1007 | if (ret < 0) |
| 1008 | return ret; |
| 1009 | |
| 1010 | if (phydev->supported & SUPPORTED_10000baseKR_Full) |
| 1011 | ret |= 0x80; |
| 1012 | else |
| 1013 | ret &= ~0x80; |
| 1014 | |
| 1015 | if ((phydev->supported & SUPPORTED_1000baseKX_Full) || |
| 1016 | (phydev->supported & SUPPORTED_2500baseX_Full)) |
| 1017 | ret |= 0x20; |
| 1018 | else |
| 1019 | ret &= ~0x20; |
| 1020 | |
| 1021 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1, ret); |
| 1022 | |
| 1023 | /* Set up Advertisement register 1 last */ |
| 1024 | ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE); |
| 1025 | if (ret < 0) |
| 1026 | return ret; |
| 1027 | |
| 1028 | if (phydev->supported & SUPPORTED_Pause) |
| 1029 | ret |= 0x400; |
| 1030 | else |
| 1031 | ret &= ~0x400; |
| 1032 | |
| 1033 | if (phydev->supported & SUPPORTED_Asym_Pause) |
| 1034 | ret |= 0x800; |
| 1035 | else |
| 1036 | ret &= ~0x800; |
| 1037 | |
| 1038 | /* We don't intend to perform XNP */ |
| 1039 | ret &= ~XNP_NP_EXCHANGE; |
| 1040 | |
| 1041 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE, ret); |
| 1042 | |
| 1043 | return 0; |
| 1044 | } |
| 1045 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1046 | static int amd_xgbe_phy_soft_reset(struct phy_device *phydev) |
| 1047 | { |
| 1048 | int count, ret; |
| 1049 | |
| 1050 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); |
| 1051 | if (ret < 0) |
| 1052 | return ret; |
| 1053 | |
| 1054 | ret |= MDIO_CTRL1_RESET; |
| 1055 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret); |
| 1056 | |
| 1057 | count = 50; |
| 1058 | do { |
| 1059 | msleep(20); |
| 1060 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); |
| 1061 | if (ret < 0) |
| 1062 | return ret; |
| 1063 | } while ((ret & MDIO_CTRL1_RESET) && --count); |
| 1064 | |
| 1065 | if (ret & MDIO_CTRL1_RESET) |
| 1066 | return -ETIMEDOUT; |
| 1067 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1068 | /* Disable auto-negotiation for now */ |
| 1069 | ret = amd_xgbe_phy_disable_an(phydev); |
| 1070 | if (ret < 0) |
| 1071 | return ret; |
| 1072 | |
| 1073 | /* Clear auto-negotiation interrupts */ |
| 1074 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0); |
| 1075 | |
| 1076 | return 0; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | static int amd_xgbe_phy_config_init(struct phy_device *phydev) |
| 1080 | { |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1081 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1082 | struct net_device *netdev = phydev->attached_dev; |
| 1083 | int ret; |
| 1084 | |
| 1085 | if (!priv->an_irq_allocated) { |
| 1086 | /* Allocate the auto-negotiation workqueue and interrupt */ |
| 1087 | snprintf(priv->an_irq_name, sizeof(priv->an_irq_name) - 1, |
| 1088 | "%s-pcs", netdev_name(netdev)); |
| 1089 | |
| 1090 | priv->an_workqueue = |
| 1091 | create_singlethread_workqueue(priv->an_irq_name); |
| 1092 | if (!priv->an_workqueue) { |
| 1093 | netdev_err(netdev, "phy workqueue creation failed\n"); |
| 1094 | return -ENOMEM; |
| 1095 | } |
| 1096 | |
| 1097 | ret = devm_request_irq(priv->dev, priv->an_irq, |
| 1098 | amd_xgbe_an_isr, 0, priv->an_irq_name, |
| 1099 | priv); |
| 1100 | if (ret) { |
| 1101 | netdev_err(netdev, "phy irq request failed\n"); |
| 1102 | destroy_workqueue(priv->an_workqueue); |
| 1103 | return ret; |
| 1104 | } |
| 1105 | |
| 1106 | priv->an_irq_allocated = 1; |
| 1107 | } |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1108 | |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 1109 | ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_ABILITY); |
| 1110 | if (ret < 0) |
| 1111 | return ret; |
| 1112 | priv->fec_ability = ret & XGBE_PHY_FEC_MASK; |
| 1113 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1114 | /* Initialize supported features */ |
| 1115 | phydev->supported = SUPPORTED_Autoneg; |
| 1116 | phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause; |
| 1117 | phydev->supported |= SUPPORTED_Backplane; |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 1118 | phydev->supported |= SUPPORTED_10000baseKR_Full; |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1119 | switch (priv->speed_set) { |
| 1120 | case AMD_XGBE_PHY_SPEEDSET_1000_10000: |
| 1121 | phydev->supported |= SUPPORTED_1000baseKX_Full; |
| 1122 | break; |
| 1123 | case AMD_XGBE_PHY_SPEEDSET_2500_10000: |
| 1124 | phydev->supported |= SUPPORTED_2500baseX_Full; |
| 1125 | break; |
| 1126 | } |
Lendacky, Thomas | cf26252 | 2015-01-16 12:47:05 -0600 | [diff] [blame^] | 1127 | |
| 1128 | if (priv->fec_ability & XGBE_PHY_FEC_ENABLE) |
| 1129 | phydev->supported |= SUPPORTED_10000baseR_FEC; |
| 1130 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1131 | phydev->advertising = phydev->supported; |
| 1132 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1133 | /* Set initial mode - call the mode setting routines |
| 1134 | * directly to insure we are properly configured |
| 1135 | */ |
| 1136 | if (phydev->supported & SUPPORTED_10000baseKR_Full) |
| 1137 | ret = amd_xgbe_phy_xgmii_mode(phydev); |
| 1138 | else if (phydev->supported & SUPPORTED_1000baseKX_Full) |
| 1139 | ret = amd_xgbe_phy_gmii_mode(phydev); |
| 1140 | else if (phydev->supported & SUPPORTED_2500baseX_Full) |
| 1141 | ret = amd_xgbe_phy_gmii_2500_mode(phydev); |
| 1142 | else |
| 1143 | ret = -EINVAL; |
| 1144 | if (ret < 0) |
| 1145 | return ret; |
| 1146 | |
| 1147 | /* Set up advertisement registers based on current settings */ |
| 1148 | ret = amd_xgbe_an_init(phydev); |
| 1149 | if (ret) |
| 1150 | return ret; |
| 1151 | |
| 1152 | /* Enable auto-negotiation interrupts */ |
| 1153 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INTMASK, 0x07); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1154 | |
| 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | static int amd_xgbe_phy_setup_forced(struct phy_device *phydev) |
| 1159 | { |
| 1160 | int ret; |
| 1161 | |
| 1162 | /* Disable auto-negotiation */ |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1163 | ret = amd_xgbe_phy_disable_an(phydev); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1164 | if (ret < 0) |
| 1165 | return ret; |
| 1166 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1167 | /* Validate/Set specified speed */ |
| 1168 | switch (phydev->speed) { |
| 1169 | case SPEED_10000: |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1170 | ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1171 | break; |
| 1172 | |
| 1173 | case SPEED_2500: |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1174 | case SPEED_1000: |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1175 | ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1176 | break; |
| 1177 | |
| 1178 | default: |
| 1179 | ret = -EINVAL; |
| 1180 | } |
| 1181 | |
| 1182 | if (ret < 0) |
| 1183 | return ret; |
| 1184 | |
| 1185 | /* Validate duplex mode */ |
| 1186 | if (phydev->duplex != DUPLEX_FULL) |
| 1187 | return -EINVAL; |
| 1188 | |
| 1189 | phydev->pause = 0; |
| 1190 | phydev->asym_pause = 0; |
| 1191 | |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1195 | static int __amd_xgbe_phy_config_aneg(struct phy_device *phydev) |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1196 | { |
| 1197 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
| 1198 | u32 mmd_mask = phydev->c45_ids.devices_in_package; |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1199 | int ret; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1200 | |
| 1201 | if (phydev->autoneg != AUTONEG_ENABLE) |
| 1202 | return amd_xgbe_phy_setup_forced(phydev); |
| 1203 | |
| 1204 | /* Make sure we have the AN MMD present */ |
| 1205 | if (!(mmd_mask & MDIO_DEVS_AN)) |
| 1206 | return -EINVAL; |
| 1207 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1208 | /* Disable auto-negotiation interrupt */ |
| 1209 | disable_irq(priv->an_irq); |
| 1210 | |
| 1211 | /* Start auto-negotiation in a supported mode */ |
| 1212 | if (phydev->supported & SUPPORTED_10000baseKR_Full) |
| 1213 | ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR); |
| 1214 | else if ((phydev->supported & SUPPORTED_1000baseKX_Full) || |
| 1215 | (phydev->supported & SUPPORTED_2500baseX_Full)) |
| 1216 | ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX); |
| 1217 | else |
| 1218 | ret = -EINVAL; |
| 1219 | if (ret < 0) { |
| 1220 | enable_irq(priv->an_irq); |
| 1221 | return ret; |
| 1222 | } |
| 1223 | |
| 1224 | /* Disable and stop any in progress auto-negotiation */ |
| 1225 | ret = amd_xgbe_phy_disable_an(phydev); |
| 1226 | if (ret < 0) |
| 1227 | return ret; |
| 1228 | |
| 1229 | /* Clear any auto-negotitation interrupts */ |
| 1230 | phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0); |
| 1231 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1232 | priv->an_result = AMD_XGBE_AN_READY; |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1233 | priv->an_state = AMD_XGBE_AN_READY; |
| 1234 | priv->kr_state = AMD_XGBE_RX_BPA; |
| 1235 | priv->kx_state = AMD_XGBE_RX_BPA; |
| 1236 | |
| 1237 | /* Re-enable auto-negotiation interrupt */ |
| 1238 | enable_irq(priv->an_irq); |
| 1239 | |
| 1240 | /* Set up advertisement registers based on current settings */ |
| 1241 | ret = amd_xgbe_an_init(phydev); |
| 1242 | if (ret) |
| 1243 | return ret; |
| 1244 | |
| 1245 | /* Enable and start auto-negotiation */ |
| 1246 | return amd_xgbe_phy_restart_an(phydev); |
| 1247 | } |
| 1248 | |
| 1249 | static int amd_xgbe_phy_config_aneg(struct phy_device *phydev) |
| 1250 | { |
| 1251 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
| 1252 | int ret; |
| 1253 | |
| 1254 | mutex_lock(&priv->an_mutex); |
| 1255 | |
| 1256 | ret = __amd_xgbe_phy_config_aneg(phydev); |
| 1257 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1258 | mutex_unlock(&priv->an_mutex); |
| 1259 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1260 | return ret; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | static int amd_xgbe_phy_aneg_done(struct phy_device *phydev) |
| 1264 | { |
| 1265 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1266 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1267 | return (priv->an_result == AMD_XGBE_AN_COMPLETE); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | static int amd_xgbe_phy_update_link(struct phy_device *phydev) |
| 1271 | { |
| 1272 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1273 | int ret; |
| 1274 | |
| 1275 | /* If we're doing auto-negotiation don't report link down */ |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1276 | if (priv->an_state != AMD_XGBE_AN_READY) { |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1277 | phydev->link = 1; |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1281 | /* Link status is latched low, so read once to clear |
| 1282 | * and then read again to get current state |
| 1283 | */ |
| 1284 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1); |
| 1285 | if (ret < 0) |
| 1286 | return ret; |
| 1287 | |
| 1288 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1); |
| 1289 | if (ret < 0) |
| 1290 | return ret; |
| 1291 | |
| 1292 | phydev->link = (ret & MDIO_STAT1_LSTATUS) ? 1 : 0; |
| 1293 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1294 | return 0; |
| 1295 | } |
| 1296 | |
| 1297 | static int amd_xgbe_phy_read_status(struct phy_device *phydev) |
| 1298 | { |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1299 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1300 | u32 mmd_mask = phydev->c45_ids.devices_in_package; |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1301 | int ret, ad_ret, lp_ret; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1302 | |
| 1303 | ret = amd_xgbe_phy_update_link(phydev); |
| 1304 | if (ret) |
| 1305 | return ret; |
| 1306 | |
Lendacky, Thomas | e6f0562 | 2014-09-03 12:14:22 -0500 | [diff] [blame] | 1307 | if ((phydev->autoneg == AUTONEG_ENABLE) && |
| 1308 | !priv->parallel_detect) { |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1309 | if (!(mmd_mask & MDIO_DEVS_AN)) |
| 1310 | return -EINVAL; |
| 1311 | |
| 1312 | if (!amd_xgbe_phy_aneg_done(phydev)) |
| 1313 | return 0; |
| 1314 | |
| 1315 | /* Compare Advertisement and Link Partner register 1 */ |
| 1316 | ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE); |
| 1317 | if (ad_ret < 0) |
| 1318 | return ad_ret; |
| 1319 | lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA); |
| 1320 | if (lp_ret < 0) |
| 1321 | return lp_ret; |
| 1322 | |
| 1323 | ad_ret &= lp_ret; |
| 1324 | phydev->pause = (ad_ret & 0x400) ? 1 : 0; |
| 1325 | phydev->asym_pause = (ad_ret & 0x800) ? 1 : 0; |
| 1326 | |
| 1327 | /* Compare Advertisement and Link Partner register 2 */ |
| 1328 | ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN, |
| 1329 | MDIO_AN_ADVERTISE + 1); |
| 1330 | if (ad_ret < 0) |
| 1331 | return ad_ret; |
| 1332 | lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1); |
| 1333 | if (lp_ret < 0) |
| 1334 | return lp_ret; |
| 1335 | |
| 1336 | ad_ret &= lp_ret; |
| 1337 | if (ad_ret & 0x80) { |
| 1338 | phydev->speed = SPEED_10000; |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1339 | ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR); |
| 1340 | if (ret) |
| 1341 | return ret; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1342 | } else { |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1343 | switch (priv->speed_set) { |
| 1344 | case AMD_XGBE_PHY_SPEEDSET_1000_10000: |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1345 | phydev->speed = SPEED_1000; |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1346 | break; |
| 1347 | |
| 1348 | case AMD_XGBE_PHY_SPEEDSET_2500_10000: |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1349 | phydev->speed = SPEED_2500; |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1350 | break; |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1351 | } |
| 1352 | |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1353 | ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX); |
| 1354 | if (ret) |
| 1355 | return ret; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | phydev->duplex = DUPLEX_FULL; |
| 1359 | } else { |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1360 | if (amd_xgbe_phy_in_kr_mode(phydev)) { |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1361 | phydev->speed = SPEED_10000; |
| 1362 | } else { |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1363 | switch (priv->speed_set) { |
| 1364 | case AMD_XGBE_PHY_SPEEDSET_1000_10000: |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1365 | phydev->speed = SPEED_1000; |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1366 | break; |
| 1367 | |
| 1368 | case AMD_XGBE_PHY_SPEEDSET_2500_10000: |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1369 | phydev->speed = SPEED_2500; |
Lendacky, Thomas | e3eec4e | 2014-09-03 12:14:16 -0500 | [diff] [blame] | 1370 | break; |
| 1371 | } |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1372 | } |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1373 | phydev->duplex = DUPLEX_FULL; |
| 1374 | phydev->pause = 0; |
| 1375 | phydev->asym_pause = 0; |
| 1376 | } |
| 1377 | |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | static int amd_xgbe_phy_suspend(struct phy_device *phydev) |
| 1382 | { |
Lendacky, Thomas | 03e50fd | 2015-01-16 12:46:39 -0600 | [diff] [blame] | 1383 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1384 | int ret; |
| 1385 | |
| 1386 | mutex_lock(&phydev->lock); |
| 1387 | |
| 1388 | ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1); |
| 1389 | if (ret < 0) |
| 1390 | goto unlock; |
| 1391 | |
Lendacky, Thomas | 03e50fd | 2015-01-16 12:46:39 -0600 | [diff] [blame] | 1392 | priv->lpm_ctrl = ret; |
| 1393 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1394 | ret |= MDIO_CTRL1_LPOWER; |
| 1395 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret); |
| 1396 | |
| 1397 | ret = 0; |
| 1398 | |
| 1399 | unlock: |
| 1400 | mutex_unlock(&phydev->lock); |
| 1401 | |
| 1402 | return ret; |
| 1403 | } |
| 1404 | |
| 1405 | static int amd_xgbe_phy_resume(struct phy_device *phydev) |
| 1406 | { |
Lendacky, Thomas | 03e50fd | 2015-01-16 12:46:39 -0600 | [diff] [blame] | 1407 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1408 | |
| 1409 | mutex_lock(&phydev->lock); |
| 1410 | |
Lendacky, Thomas | 03e50fd | 2015-01-16 12:46:39 -0600 | [diff] [blame] | 1411 | priv->lpm_ctrl &= ~MDIO_CTRL1_LPOWER; |
| 1412 | phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, priv->lpm_ctrl); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1413 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1414 | mutex_unlock(&phydev->lock); |
| 1415 | |
Lendacky, Thomas | 03e50fd | 2015-01-16 12:46:39 -0600 | [diff] [blame] | 1416 | return 0; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | static int amd_xgbe_phy_probe(struct phy_device *phydev) |
| 1420 | { |
| 1421 | struct amd_xgbe_phy_priv *priv; |
| 1422 | struct platform_device *pdev; |
| 1423 | struct device *dev; |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1424 | const __be32 *property; |
| 1425 | unsigned int speed_set; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1426 | int ret; |
| 1427 | |
| 1428 | if (!phydev->dev.of_node) |
| 1429 | return -EINVAL; |
| 1430 | |
| 1431 | pdev = of_find_device_by_node(phydev->dev.of_node); |
| 1432 | if (!pdev) |
| 1433 | return -EINVAL; |
| 1434 | dev = &pdev->dev; |
| 1435 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1436 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 1437 | if (!priv) { |
| 1438 | ret = -ENOMEM; |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1439 | goto err_pdev; |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | priv->pdev = pdev; |
| 1443 | priv->dev = dev; |
| 1444 | priv->phydev = phydev; |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1445 | mutex_init(&priv->an_mutex); |
| 1446 | INIT_WORK(&priv->an_irq_work, amd_xgbe_an_irq_work); |
| 1447 | INIT_WORK(&priv->an_work, amd_xgbe_an_state_machine); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1448 | |
| 1449 | /* Get the device mmio areas */ |
| 1450 | priv->rxtx_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1451 | priv->rxtx_regs = devm_ioremap_resource(dev, priv->rxtx_res); |
| 1452 | if (IS_ERR(priv->rxtx_regs)) { |
| 1453 | dev_err(dev, "rxtx ioremap failed\n"); |
| 1454 | ret = PTR_ERR(priv->rxtx_regs); |
| 1455 | goto err_priv; |
| 1456 | } |
| 1457 | |
| 1458 | priv->sir0_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1459 | priv->sir0_regs = devm_ioremap_resource(dev, priv->sir0_res); |
| 1460 | if (IS_ERR(priv->sir0_regs)) { |
| 1461 | dev_err(dev, "sir0 ioremap failed\n"); |
| 1462 | ret = PTR_ERR(priv->sir0_regs); |
| 1463 | goto err_rxtx; |
| 1464 | } |
| 1465 | |
| 1466 | priv->sir1_res = platform_get_resource(pdev, IORESOURCE_MEM, 2); |
| 1467 | priv->sir1_regs = devm_ioremap_resource(dev, priv->sir1_res); |
| 1468 | if (IS_ERR(priv->sir1_regs)) { |
| 1469 | dev_err(dev, "sir1 ioremap failed\n"); |
| 1470 | ret = PTR_ERR(priv->sir1_regs); |
| 1471 | goto err_sir0; |
| 1472 | } |
| 1473 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1474 | /* Get the auto-negotiation interrupt */ |
| 1475 | ret = platform_get_irq(pdev, 0); |
| 1476 | if (ret < 0) { |
| 1477 | dev_err(dev, "platform_get_irq failed\n"); |
| 1478 | goto err_sir1; |
| 1479 | } |
| 1480 | priv->an_irq = ret; |
| 1481 | |
Lendacky, Thomas | f047604 | 2014-07-29 08:57:25 -0500 | [diff] [blame] | 1482 | /* Get the device speed set property */ |
| 1483 | speed_set = 0; |
| 1484 | property = of_get_property(dev->of_node, XGBE_PHY_SPEEDSET_PROPERTY, |
| 1485 | NULL); |
| 1486 | if (property) |
| 1487 | speed_set = be32_to_cpu(*property); |
| 1488 | |
| 1489 | switch (speed_set) { |
| 1490 | case 0: |
| 1491 | priv->speed_set = AMD_XGBE_PHY_SPEEDSET_1000_10000; |
| 1492 | break; |
| 1493 | case 1: |
| 1494 | priv->speed_set = AMD_XGBE_PHY_SPEEDSET_2500_10000; |
| 1495 | break; |
| 1496 | default: |
| 1497 | dev_err(dev, "invalid amd,speed-set property\n"); |
| 1498 | ret = -EINVAL; |
| 1499 | goto err_sir1; |
| 1500 | } |
| 1501 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1502 | phydev->priv = priv; |
| 1503 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1504 | of_dev_put(pdev); |
| 1505 | |
| 1506 | return 0; |
| 1507 | |
| 1508 | err_sir1: |
| 1509 | devm_iounmap(dev, priv->sir1_regs); |
| 1510 | devm_release_mem_region(dev, priv->sir1_res->start, |
| 1511 | resource_size(priv->sir1_res)); |
| 1512 | |
| 1513 | err_sir0: |
| 1514 | devm_iounmap(dev, priv->sir0_regs); |
| 1515 | devm_release_mem_region(dev, priv->sir0_res->start, |
| 1516 | resource_size(priv->sir0_res)); |
| 1517 | |
| 1518 | err_rxtx: |
| 1519 | devm_iounmap(dev, priv->rxtx_regs); |
| 1520 | devm_release_mem_region(dev, priv->rxtx_res->start, |
| 1521 | resource_size(priv->rxtx_res)); |
| 1522 | |
| 1523 | err_priv: |
| 1524 | devm_kfree(dev, priv); |
| 1525 | |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1526 | err_pdev: |
| 1527 | of_dev_put(pdev); |
| 1528 | |
| 1529 | return ret; |
| 1530 | } |
| 1531 | |
| 1532 | static void amd_xgbe_phy_remove(struct phy_device *phydev) |
| 1533 | { |
| 1534 | struct amd_xgbe_phy_priv *priv = phydev->priv; |
| 1535 | struct device *dev = priv->dev; |
| 1536 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1537 | if (priv->an_irq_allocated) { |
| 1538 | devm_free_irq(dev, priv->an_irq, priv); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1539 | |
Lendacky, Thomas | c3152d4 | 2015-01-16 12:47:00 -0600 | [diff] [blame] | 1540 | flush_workqueue(priv->an_workqueue); |
| 1541 | destroy_workqueue(priv->an_workqueue); |
| 1542 | } |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1543 | |
| 1544 | /* Release resources */ |
| 1545 | devm_iounmap(dev, priv->sir1_regs); |
| 1546 | devm_release_mem_region(dev, priv->sir1_res->start, |
| 1547 | resource_size(priv->sir1_res)); |
| 1548 | |
| 1549 | devm_iounmap(dev, priv->sir0_regs); |
| 1550 | devm_release_mem_region(dev, priv->sir0_res->start, |
| 1551 | resource_size(priv->sir0_res)); |
| 1552 | |
| 1553 | devm_iounmap(dev, priv->rxtx_regs); |
| 1554 | devm_release_mem_region(dev, priv->rxtx_res->start, |
| 1555 | resource_size(priv->rxtx_res)); |
| 1556 | |
| 1557 | devm_kfree(dev, priv); |
| 1558 | } |
| 1559 | |
| 1560 | static int amd_xgbe_match_phy_device(struct phy_device *phydev) |
| 1561 | { |
| 1562 | return phydev->c45_ids.device_ids[MDIO_MMD_PCS] == XGBE_PHY_ID; |
| 1563 | } |
| 1564 | |
| 1565 | static struct phy_driver amd_xgbe_phy_driver[] = { |
| 1566 | { |
| 1567 | .phy_id = XGBE_PHY_ID, |
| 1568 | .phy_id_mask = XGBE_PHY_MASK, |
| 1569 | .name = "AMD XGBE PHY", |
| 1570 | .features = 0, |
| 1571 | .probe = amd_xgbe_phy_probe, |
| 1572 | .remove = amd_xgbe_phy_remove, |
| 1573 | .soft_reset = amd_xgbe_phy_soft_reset, |
| 1574 | .config_init = amd_xgbe_phy_config_init, |
| 1575 | .suspend = amd_xgbe_phy_suspend, |
| 1576 | .resume = amd_xgbe_phy_resume, |
| 1577 | .config_aneg = amd_xgbe_phy_config_aneg, |
| 1578 | .aneg_done = amd_xgbe_phy_aneg_done, |
| 1579 | .read_status = amd_xgbe_phy_read_status, |
| 1580 | .match_phy_device = amd_xgbe_match_phy_device, |
| 1581 | .driver = { |
| 1582 | .owner = THIS_MODULE, |
| 1583 | }, |
| 1584 | }, |
| 1585 | }; |
| 1586 | |
Johan Hovold | 50fd715 | 2014-11-11 19:45:59 +0100 | [diff] [blame] | 1587 | module_phy_driver(amd_xgbe_phy_driver); |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1588 | |
françois romieu | a25aafa | 2014-06-07 11:07:48 +0200 | [diff] [blame] | 1589 | static struct mdio_device_id __maybe_unused amd_xgbe_phy_ids[] = { |
Lendacky, Thomas | 4d874b3 | 2014-06-05 09:15:12 -0500 | [diff] [blame] | 1590 | { XGBE_PHY_ID, XGBE_PHY_MASK }, |
| 1591 | { } |
| 1592 | }; |
| 1593 | MODULE_DEVICE_TABLE(mdio, amd_xgbe_phy_ids); |