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