Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Broadcom SATA3 AHCI Controller PHY Driver |
| 3 | * |
Anup Patel | 037c418 | 2016-03-28 10:18:26 +0530 | [diff] [blame] | 4 | * Copyright (C) 2016 Broadcom |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 17 | #include <linux/delay.h> |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 18 | #include <linux/device.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/of.h> |
| 25 | #include <linux/phy/phy.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 28 | #define SATA_PCB_BANK_OFFSET 0x23c |
| 29 | #define SATA_PCB_REG_OFFSET(ofs) ((ofs) * 4) |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 30 | |
| 31 | #define MAX_PORTS 2 |
| 32 | |
| 33 | /* Register offset between PHYs in PCB space */ |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 34 | #define SATA_PCB_REG_28NM_SPACE_SIZE 0x1000 |
Jaedon Shin | 810c6f1 | 2015-11-26 11:56:34 +0900 | [diff] [blame] | 35 | |
Jaedon Shin | c1602a1 | 2015-11-26 11:56:35 +0900 | [diff] [blame] | 36 | /* The older SATA PHY registers duplicated per port registers within the map, |
| 37 | * rather than having a separate map per port. |
| 38 | */ |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 39 | #define SATA_PCB_REG_40NM_SPACE_SIZE 0x10 |
| 40 | |
| 41 | /* Register offset between PHYs in PHY control space */ |
| 42 | #define SATA_PHY_CTRL_REG_28NM_SPACE_SIZE 0x8 |
Jaedon Shin | c1602a1 | 2015-11-26 11:56:35 +0900 | [diff] [blame] | 43 | |
Jaedon Shin | 810c6f1 | 2015-11-26 11:56:34 +0900 | [diff] [blame] | 44 | enum brcm_sata_phy_version { |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 45 | BRCM_SATA_PHY_STB_28NM, |
| 46 | BRCM_SATA_PHY_STB_40NM, |
| 47 | BRCM_SATA_PHY_IPROC_NS2, |
Yendapally Reddy Dhananjaya Reddy | 0248128 | 2016-06-16 09:53:34 -0400 | [diff] [blame] | 48 | BRCM_SATA_PHY_IPROC_NSP, |
Srinath Mannam | 80886f7 | 2017-06-08 17:01:39 +0530 | [diff] [blame] | 49 | BRCM_SATA_PHY_IPROC_SR, |
Jaedon Shin | 810c6f1 | 2015-11-26 11:56:34 +0900 | [diff] [blame] | 50 | }; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 51 | |
| 52 | struct brcm_sata_port { |
| 53 | int portnum; |
| 54 | struct phy *phy; |
| 55 | struct brcm_sata_phy *phy_priv; |
| 56 | bool ssc_en; |
| 57 | }; |
| 58 | |
| 59 | struct brcm_sata_phy { |
| 60 | struct device *dev; |
| 61 | void __iomem *phy_base; |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 62 | void __iomem *ctrl_base; |
Jaedon Shin | 810c6f1 | 2015-11-26 11:56:34 +0900 | [diff] [blame] | 63 | enum brcm_sata_phy_version version; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 64 | |
| 65 | struct brcm_sata_port phys[MAX_PORTS]; |
| 66 | }; |
| 67 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 68 | enum sata_phy_regs { |
| 69 | BLOCK0_REG_BANK = 0x000, |
| 70 | BLOCK0_XGXSSTATUS = 0x81, |
| 71 | BLOCK0_XGXSSTATUS_PLL_LOCK = BIT(12), |
| 72 | BLOCK0_SPARE = 0x8d, |
| 73 | BLOCK0_SPARE_OOB_CLK_SEL_MASK = 0x3, |
| 74 | BLOCK0_SPARE_OOB_CLK_SEL_REFBY2 = 0x1, |
| 75 | |
| 76 | PLL_REG_BANK_0 = 0x050, |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 77 | PLL_REG_BANK_0_PLLCONTROL_0 = 0x81, |
Yendapally Reddy Dhananjaya Reddy | 0248128 | 2016-06-16 09:53:34 -0400 | [diff] [blame] | 78 | PLLCONTROL_0_FREQ_DET_RESTART = BIT(13), |
| 79 | PLLCONTROL_0_FREQ_MONITOR = BIT(12), |
| 80 | PLLCONTROL_0_SEQ_START = BIT(15), |
| 81 | PLL_CAP_CONTROL = 0x85, |
| 82 | PLL_ACTRL2 = 0x8b, |
| 83 | PLL_ACTRL2_SELDIV_MASK = 0x1f, |
| 84 | PLL_ACTRL2_SELDIV_SHIFT = 9, |
Srinath Mannam | 80886f7 | 2017-06-08 17:01:39 +0530 | [diff] [blame] | 85 | PLL_ACTRL6 = 0x86, |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 86 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 87 | PLL1_REG_BANK = 0x060, |
| 88 | PLL1_ACTRL2 = 0x82, |
| 89 | PLL1_ACTRL3 = 0x83, |
| 90 | PLL1_ACTRL4 = 0x84, |
| 91 | |
Srinath Mannam | 80886f7 | 2017-06-08 17:01:39 +0530 | [diff] [blame] | 92 | TX_REG_BANK = 0x070, |
| 93 | TX_ACTRL0 = 0x80, |
| 94 | TX_ACTRL0_TXPOL_FLIP = BIT(6), |
| 95 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 96 | OOB_REG_BANK = 0x150, |
Yendapally Reddy Dhananjaya Reddy | 0248128 | 2016-06-16 09:53:34 -0400 | [diff] [blame] | 97 | OOB1_REG_BANK = 0x160, |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 98 | OOB_CTRL1 = 0x80, |
| 99 | OOB_CTRL1_BURST_MAX_MASK = 0xf, |
| 100 | OOB_CTRL1_BURST_MAX_SHIFT = 12, |
| 101 | OOB_CTRL1_BURST_MIN_MASK = 0xf, |
| 102 | OOB_CTRL1_BURST_MIN_SHIFT = 8, |
| 103 | OOB_CTRL1_WAKE_IDLE_MAX_MASK = 0xf, |
| 104 | OOB_CTRL1_WAKE_IDLE_MAX_SHIFT = 4, |
| 105 | OOB_CTRL1_WAKE_IDLE_MIN_MASK = 0xf, |
| 106 | OOB_CTRL1_WAKE_IDLE_MIN_SHIFT = 0, |
| 107 | OOB_CTRL2 = 0x81, |
| 108 | OOB_CTRL2_SEL_ENA_SHIFT = 15, |
| 109 | OOB_CTRL2_SEL_ENA_RC_SHIFT = 14, |
| 110 | OOB_CTRL2_RESET_IDLE_MAX_MASK = 0x3f, |
| 111 | OOB_CTRL2_RESET_IDLE_MAX_SHIFT = 8, |
| 112 | OOB_CTRL2_BURST_CNT_MASK = 0x3, |
| 113 | OOB_CTRL2_BURST_CNT_SHIFT = 6, |
| 114 | OOB_CTRL2_RESET_IDLE_MIN_MASK = 0x3f, |
| 115 | OOB_CTRL2_RESET_IDLE_MIN_SHIFT = 0, |
| 116 | |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 117 | TXPMD_REG_BANK = 0x1a0, |
| 118 | TXPMD_CONTROL1 = 0x81, |
| 119 | TXPMD_CONTROL1_TX_SSC_EN_FRC = BIT(0), |
| 120 | TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL = BIT(1), |
| 121 | TXPMD_TX_FREQ_CTRL_CONTROL1 = 0x82, |
| 122 | TXPMD_TX_FREQ_CTRL_CONTROL2 = 0x83, |
| 123 | TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK = 0x3ff, |
| 124 | TXPMD_TX_FREQ_CTRL_CONTROL3 = 0x84, |
| 125 | TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK = 0x3ff, |
| 126 | }; |
| 127 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 128 | enum sata_phy_ctrl_regs { |
| 129 | PHY_CTRL_1 = 0x0, |
| 130 | PHY_CTRL_1_RESET = BIT(0), |
| 131 | }; |
| 132 | |
| 133 | static inline void __iomem *brcm_sata_pcb_base(struct brcm_sata_port *port) |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 134 | { |
| 135 | struct brcm_sata_phy *priv = port->phy_priv; |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 136 | u32 size = 0; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 137 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 138 | switch (priv->version) { |
| 139 | case BRCM_SATA_PHY_STB_28NM: |
| 140 | case BRCM_SATA_PHY_IPROC_NS2: |
| 141 | size = SATA_PCB_REG_28NM_SPACE_SIZE; |
| 142 | break; |
| 143 | case BRCM_SATA_PHY_STB_40NM: |
| 144 | size = SATA_PCB_REG_40NM_SPACE_SIZE; |
| 145 | break; |
| 146 | default: |
Jaedon Shin | c1602a1 | 2015-11-26 11:56:35 +0900 | [diff] [blame] | 147 | dev_err(priv->dev, "invalid phy version\n"); |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 148 | break; |
Vivek Gautam | 0e65ba2 | 2016-10-20 12:23:38 +0530 | [diff] [blame] | 149 | } |
Jaedon Shin | 810c6f1 | 2015-11-26 11:56:34 +0900 | [diff] [blame] | 150 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 151 | return priv->phy_base + (port->portnum * size); |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 154 | static inline void __iomem *brcm_sata_ctrl_base(struct brcm_sata_port *port) |
| 155 | { |
| 156 | struct brcm_sata_phy *priv = port->phy_priv; |
| 157 | u32 size = 0; |
| 158 | |
| 159 | switch (priv->version) { |
| 160 | case BRCM_SATA_PHY_IPROC_NS2: |
| 161 | size = SATA_PHY_CTRL_REG_28NM_SPACE_SIZE; |
| 162 | break; |
| 163 | default: |
| 164 | dev_err(priv->dev, "invalid phy version\n"); |
| 165 | break; |
Vivek Gautam | 0e65ba2 | 2016-10-20 12:23:38 +0530 | [diff] [blame] | 166 | } |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 167 | |
| 168 | return priv->ctrl_base + (port->portnum * size); |
| 169 | } |
| 170 | |
| 171 | static void brcm_sata_phy_wr(void __iomem *pcb_base, u32 bank, |
| 172 | u32 ofs, u32 msk, u32 value) |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 173 | { |
| 174 | u32 tmp; |
| 175 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 176 | writel(bank, pcb_base + SATA_PCB_BANK_OFFSET); |
| 177 | tmp = readl(pcb_base + SATA_PCB_REG_OFFSET(ofs)); |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 178 | tmp = (tmp & msk) | value; |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 179 | writel(tmp, pcb_base + SATA_PCB_REG_OFFSET(ofs)); |
| 180 | } |
| 181 | |
| 182 | static u32 brcm_sata_phy_rd(void __iomem *pcb_base, u32 bank, u32 ofs) |
| 183 | { |
| 184 | writel(bank, pcb_base + SATA_PCB_BANK_OFFSET); |
| 185 | return readl(pcb_base + SATA_PCB_REG_OFFSET(ofs)); |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* These defaults were characterized by H/W group */ |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 189 | #define STB_FMIN_VAL_DEFAULT 0x3df |
| 190 | #define STB_FMAX_VAL_DEFAULT 0x3df |
| 191 | #define STB_FMAX_VAL_SSC 0x83 |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 192 | |
Florian Fainelli | 6ec248f | 2017-10-11 17:53:11 -0700 | [diff] [blame^] | 193 | static void brcm_stb_sata_ssc_init(struct brcm_sata_port *port) |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 194 | { |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 195 | void __iomem *base = brcm_sata_pcb_base(port); |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 196 | struct brcm_sata_phy *priv = port->phy_priv; |
| 197 | u32 tmp; |
| 198 | |
| 199 | /* override the TX spread spectrum setting */ |
| 200 | tmp = TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL | TXPMD_CONTROL1_TX_SSC_EN_FRC; |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 201 | brcm_sata_phy_wr(base, TXPMD_REG_BANK, TXPMD_CONTROL1, ~tmp, tmp); |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 202 | |
| 203 | /* set fixed min freq */ |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 204 | brcm_sata_phy_wr(base, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL2, |
| 205 | ~TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK, |
| 206 | STB_FMIN_VAL_DEFAULT); |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 207 | |
| 208 | /* set fixed max freq depending on SSC config */ |
| 209 | if (port->ssc_en) { |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 210 | dev_info(priv->dev, "enabling SSC on port%d\n", port->portnum); |
| 211 | tmp = STB_FMAX_VAL_SSC; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 212 | } else { |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 213 | tmp = STB_FMAX_VAL_DEFAULT; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 216 | brcm_sata_phy_wr(base, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL3, |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 217 | ~TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK, tmp); |
Florian Fainelli | 6ec248f | 2017-10-11 17:53:11 -0700 | [diff] [blame^] | 218 | } |
| 219 | |
| 220 | static int brcm_stb_sata_init(struct brcm_sata_port *port) |
| 221 | { |
| 222 | brcm_stb_sata_ssc_init(port); |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | /* NS2 SATA PLL1 defaults were characterized by H/W group */ |
| 228 | #define NS2_PLL1_ACTRL2_MAGIC 0x1df8 |
| 229 | #define NS2_PLL1_ACTRL3_MAGIC 0x2b00 |
| 230 | #define NS2_PLL1_ACTRL4_MAGIC 0x8824 |
| 231 | |
| 232 | static int brcm_ns2_sata_init(struct brcm_sata_port *port) |
| 233 | { |
| 234 | int try; |
| 235 | unsigned int val; |
| 236 | void __iomem *base = brcm_sata_pcb_base(port); |
| 237 | void __iomem *ctrl_base = brcm_sata_ctrl_base(port); |
| 238 | struct device *dev = port->phy_priv->dev; |
| 239 | |
| 240 | /* Configure OOB control */ |
| 241 | val = 0x0; |
| 242 | val |= (0xc << OOB_CTRL1_BURST_MAX_SHIFT); |
| 243 | val |= (0x4 << OOB_CTRL1_BURST_MIN_SHIFT); |
| 244 | val |= (0x9 << OOB_CTRL1_WAKE_IDLE_MAX_SHIFT); |
| 245 | val |= (0x3 << OOB_CTRL1_WAKE_IDLE_MIN_SHIFT); |
| 246 | brcm_sata_phy_wr(base, OOB_REG_BANK, OOB_CTRL1, 0x0, val); |
| 247 | val = 0x0; |
| 248 | val |= (0x1b << OOB_CTRL2_RESET_IDLE_MAX_SHIFT); |
| 249 | val |= (0x2 << OOB_CTRL2_BURST_CNT_SHIFT); |
| 250 | val |= (0x9 << OOB_CTRL2_RESET_IDLE_MIN_SHIFT); |
| 251 | brcm_sata_phy_wr(base, OOB_REG_BANK, OOB_CTRL2, 0x0, val); |
| 252 | |
| 253 | /* Configure PHY PLL register bank 1 */ |
| 254 | val = NS2_PLL1_ACTRL2_MAGIC; |
| 255 | brcm_sata_phy_wr(base, PLL1_REG_BANK, PLL1_ACTRL2, 0x0, val); |
| 256 | val = NS2_PLL1_ACTRL3_MAGIC; |
| 257 | brcm_sata_phy_wr(base, PLL1_REG_BANK, PLL1_ACTRL3, 0x0, val); |
| 258 | val = NS2_PLL1_ACTRL4_MAGIC; |
| 259 | brcm_sata_phy_wr(base, PLL1_REG_BANK, PLL1_ACTRL4, 0x0, val); |
| 260 | |
| 261 | /* Configure PHY BLOCK0 register bank */ |
| 262 | /* Set oob_clk_sel to refclk/2 */ |
| 263 | brcm_sata_phy_wr(base, BLOCK0_REG_BANK, BLOCK0_SPARE, |
| 264 | ~BLOCK0_SPARE_OOB_CLK_SEL_MASK, |
| 265 | BLOCK0_SPARE_OOB_CLK_SEL_REFBY2); |
| 266 | |
| 267 | /* Strobe PHY reset using PHY control register */ |
| 268 | writel(PHY_CTRL_1_RESET, ctrl_base + PHY_CTRL_1); |
| 269 | mdelay(1); |
| 270 | writel(0x0, ctrl_base + PHY_CTRL_1); |
| 271 | mdelay(1); |
| 272 | |
| 273 | /* Wait for PHY PLL lock by polling pll_lock bit */ |
| 274 | try = 50; |
| 275 | while (try) { |
| 276 | val = brcm_sata_phy_rd(base, BLOCK0_REG_BANK, |
| 277 | BLOCK0_XGXSSTATUS); |
| 278 | if (val & BLOCK0_XGXSSTATUS_PLL_LOCK) |
| 279 | break; |
| 280 | msleep(20); |
| 281 | try--; |
| 282 | } |
| 283 | if (!try) { |
| 284 | /* PLL did not lock; give up */ |
| 285 | dev_err(dev, "port%d PLL did not lock\n", port->portnum); |
| 286 | return -ETIMEDOUT; |
| 287 | } |
| 288 | |
| 289 | dev_dbg(dev, "port%d initialized\n", port->portnum); |
| 290 | |
| 291 | return 0; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Yendapally Reddy Dhananjaya Reddy | 0248128 | 2016-06-16 09:53:34 -0400 | [diff] [blame] | 294 | static int brcm_nsp_sata_init(struct brcm_sata_port *port) |
| 295 | { |
| 296 | struct brcm_sata_phy *priv = port->phy_priv; |
| 297 | struct device *dev = port->phy_priv->dev; |
| 298 | void __iomem *base = priv->phy_base; |
| 299 | unsigned int oob_bank; |
| 300 | unsigned int val, try; |
| 301 | |
| 302 | /* Configure OOB control */ |
| 303 | if (port->portnum == 0) |
| 304 | oob_bank = OOB_REG_BANK; |
| 305 | else if (port->portnum == 1) |
| 306 | oob_bank = OOB1_REG_BANK; |
| 307 | else |
| 308 | return -EINVAL; |
| 309 | |
| 310 | val = 0x0; |
| 311 | val |= (0x0f << OOB_CTRL1_BURST_MAX_SHIFT); |
| 312 | val |= (0x06 << OOB_CTRL1_BURST_MIN_SHIFT); |
| 313 | val |= (0x0f << OOB_CTRL1_WAKE_IDLE_MAX_SHIFT); |
| 314 | val |= (0x06 << OOB_CTRL1_WAKE_IDLE_MIN_SHIFT); |
| 315 | brcm_sata_phy_wr(base, oob_bank, OOB_CTRL1, 0x0, val); |
| 316 | |
| 317 | val = 0x0; |
| 318 | val |= (0x2e << OOB_CTRL2_RESET_IDLE_MAX_SHIFT); |
| 319 | val |= (0x02 << OOB_CTRL2_BURST_CNT_SHIFT); |
| 320 | val |= (0x16 << OOB_CTRL2_RESET_IDLE_MIN_SHIFT); |
| 321 | brcm_sata_phy_wr(base, oob_bank, OOB_CTRL2, 0x0, val); |
| 322 | |
| 323 | |
| 324 | brcm_sata_phy_wr(base, PLL_REG_BANK_0, PLL_ACTRL2, |
| 325 | ~(PLL_ACTRL2_SELDIV_MASK << PLL_ACTRL2_SELDIV_SHIFT), |
| 326 | 0x0c << PLL_ACTRL2_SELDIV_SHIFT); |
| 327 | |
| 328 | brcm_sata_phy_wr(base, PLL_REG_BANK_0, PLL_CAP_CONTROL, |
| 329 | 0xff0, 0x4f0); |
| 330 | |
| 331 | val = PLLCONTROL_0_FREQ_DET_RESTART | PLLCONTROL_0_FREQ_MONITOR; |
| 332 | brcm_sata_phy_wr(base, PLL_REG_BANK_0, PLL_REG_BANK_0_PLLCONTROL_0, |
| 333 | ~val, val); |
| 334 | val = PLLCONTROL_0_SEQ_START; |
| 335 | brcm_sata_phy_wr(base, PLL_REG_BANK_0, PLL_REG_BANK_0_PLLCONTROL_0, |
| 336 | ~val, 0); |
| 337 | mdelay(10); |
| 338 | brcm_sata_phy_wr(base, PLL_REG_BANK_0, PLL_REG_BANK_0_PLLCONTROL_0, |
| 339 | ~val, val); |
| 340 | |
| 341 | /* Wait for pll_seq_done bit */ |
| 342 | try = 50; |
Dan Carpenter | d9c51f4 | 2017-06-19 13:56:05 +0300 | [diff] [blame] | 343 | while (--try) { |
Yendapally Reddy Dhananjaya Reddy | 0248128 | 2016-06-16 09:53:34 -0400 | [diff] [blame] | 344 | val = brcm_sata_phy_rd(base, BLOCK0_REG_BANK, |
| 345 | BLOCK0_XGXSSTATUS); |
| 346 | if (val & BLOCK0_XGXSSTATUS_PLL_LOCK) |
| 347 | break; |
| 348 | msleep(20); |
| 349 | } |
| 350 | if (!try) { |
| 351 | /* PLL did not lock; give up */ |
| 352 | dev_err(dev, "port%d PLL did not lock\n", port->portnum); |
| 353 | return -ETIMEDOUT; |
| 354 | } |
| 355 | |
| 356 | dev_dbg(dev, "port%d initialized\n", port->portnum); |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
Srinath Mannam | 80886f7 | 2017-06-08 17:01:39 +0530 | [diff] [blame] | 361 | /* SR PHY PLL0 registers */ |
| 362 | #define SR_PLL0_ACTRL6_MAGIC 0xa |
| 363 | |
| 364 | /* SR PHY PLL1 registers */ |
| 365 | #define SR_PLL1_ACTRL2_MAGIC 0x32 |
| 366 | #define SR_PLL1_ACTRL3_MAGIC 0x2 |
| 367 | #define SR_PLL1_ACTRL4_MAGIC 0x3e8 |
| 368 | |
| 369 | static int brcm_sr_sata_init(struct brcm_sata_port *port) |
| 370 | { |
| 371 | struct brcm_sata_phy *priv = port->phy_priv; |
| 372 | struct device *dev = port->phy_priv->dev; |
| 373 | void __iomem *base = priv->phy_base; |
| 374 | unsigned int val, try; |
| 375 | |
| 376 | /* Configure PHY PLL register bank 1 */ |
| 377 | val = SR_PLL1_ACTRL2_MAGIC; |
| 378 | brcm_sata_phy_wr(base, PLL1_REG_BANK, PLL1_ACTRL2, 0x0, val); |
| 379 | val = SR_PLL1_ACTRL3_MAGIC; |
| 380 | brcm_sata_phy_wr(base, PLL1_REG_BANK, PLL1_ACTRL3, 0x0, val); |
| 381 | val = SR_PLL1_ACTRL4_MAGIC; |
| 382 | brcm_sata_phy_wr(base, PLL1_REG_BANK, PLL1_ACTRL4, 0x0, val); |
| 383 | |
| 384 | /* Configure PHY PLL register bank 0 */ |
| 385 | val = SR_PLL0_ACTRL6_MAGIC; |
| 386 | brcm_sata_phy_wr(base, PLL_REG_BANK_0, PLL_ACTRL6, 0x0, val); |
| 387 | |
| 388 | /* Wait for PHY PLL lock by polling pll_lock bit */ |
| 389 | try = 50; |
| 390 | do { |
| 391 | val = brcm_sata_phy_rd(base, BLOCK0_REG_BANK, |
| 392 | BLOCK0_XGXSSTATUS); |
| 393 | if (val & BLOCK0_XGXSSTATUS_PLL_LOCK) |
| 394 | break; |
| 395 | msleep(20); |
| 396 | try--; |
| 397 | } while (try); |
| 398 | |
| 399 | if ((val & BLOCK0_XGXSSTATUS_PLL_LOCK) == 0) { |
| 400 | /* PLL did not lock; give up */ |
| 401 | dev_err(dev, "port%d PLL did not lock\n", port->portnum); |
| 402 | return -ETIMEDOUT; |
| 403 | } |
| 404 | |
| 405 | /* Invert Tx polarity */ |
| 406 | brcm_sata_phy_wr(base, TX_REG_BANK, TX_ACTRL0, |
| 407 | ~TX_ACTRL0_TXPOL_FLIP, TX_ACTRL0_TXPOL_FLIP); |
| 408 | |
| 409 | /* Configure OOB control to handle 100MHz reference clock */ |
| 410 | val = ((0xc << OOB_CTRL1_BURST_MAX_SHIFT) | |
| 411 | (0x4 << OOB_CTRL1_BURST_MIN_SHIFT) | |
| 412 | (0x8 << OOB_CTRL1_WAKE_IDLE_MAX_SHIFT) | |
| 413 | (0x3 << OOB_CTRL1_WAKE_IDLE_MIN_SHIFT)); |
| 414 | brcm_sata_phy_wr(base, OOB_REG_BANK, OOB_CTRL1, 0x0, val); |
| 415 | val = ((0x1b << OOB_CTRL2_RESET_IDLE_MAX_SHIFT) | |
| 416 | (0x2 << OOB_CTRL2_BURST_CNT_SHIFT) | |
| 417 | (0x9 << OOB_CTRL2_RESET_IDLE_MIN_SHIFT)); |
| 418 | brcm_sata_phy_wr(base, OOB_REG_BANK, OOB_CTRL2, 0x0, val); |
| 419 | |
| 420 | return 0; |
| 421 | } |
| 422 | |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 423 | static int brcm_sata_phy_init(struct phy *phy) |
| 424 | { |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 425 | int rc; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 426 | struct brcm_sata_port *port = phy_get_drvdata(phy); |
| 427 | |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 428 | switch (port->phy_priv->version) { |
| 429 | case BRCM_SATA_PHY_STB_28NM: |
| 430 | case BRCM_SATA_PHY_STB_40NM: |
| 431 | rc = brcm_stb_sata_init(port); |
| 432 | break; |
| 433 | case BRCM_SATA_PHY_IPROC_NS2: |
| 434 | rc = brcm_ns2_sata_init(port); |
| 435 | break; |
Yendapally Reddy Dhananjaya Reddy | 0248128 | 2016-06-16 09:53:34 -0400 | [diff] [blame] | 436 | case BRCM_SATA_PHY_IPROC_NSP: |
| 437 | rc = brcm_nsp_sata_init(port); |
| 438 | break; |
Srinath Mannam | 80886f7 | 2017-06-08 17:01:39 +0530 | [diff] [blame] | 439 | case BRCM_SATA_PHY_IPROC_SR: |
| 440 | rc = brcm_sr_sata_init(port); |
| 441 | break; |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 442 | default: |
| 443 | rc = -ENODEV; |
Vivek Gautam | 0e65ba2 | 2016-10-20 12:23:38 +0530 | [diff] [blame] | 444 | } |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 445 | |
Axel Lin | bf8ca65 | 2016-08-10 18:04:44 +0800 | [diff] [blame] | 446 | return rc; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Jaedon Shin | c1602a1 | 2015-11-26 11:56:35 +0900 | [diff] [blame] | 449 | static const struct phy_ops phy_ops = { |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 450 | .init = brcm_sata_phy_init, |
| 451 | .owner = THIS_MODULE, |
| 452 | }; |
| 453 | |
| 454 | static const struct of_device_id brcm_sata_phy_of_match[] = { |
Jaedon Shin | 810c6f1 | 2015-11-26 11:56:34 +0900 | [diff] [blame] | 455 | { .compatible = "brcm,bcm7445-sata-phy", |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 456 | .data = (void *)BRCM_SATA_PHY_STB_28NM }, |
Jaedon Shin | c1602a1 | 2015-11-26 11:56:35 +0900 | [diff] [blame] | 457 | { .compatible = "brcm,bcm7425-sata-phy", |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 458 | .data = (void *)BRCM_SATA_PHY_STB_40NM }, |
| 459 | { .compatible = "brcm,iproc-ns2-sata-phy", |
| 460 | .data = (void *)BRCM_SATA_PHY_IPROC_NS2 }, |
Yendapally Reddy Dhananjaya Reddy | 0248128 | 2016-06-16 09:53:34 -0400 | [diff] [blame] | 461 | { .compatible = "brcm,iproc-nsp-sata-phy", |
| 462 | .data = (void *)BRCM_SATA_PHY_IPROC_NSP }, |
Srinath Mannam | 80886f7 | 2017-06-08 17:01:39 +0530 | [diff] [blame] | 463 | { .compatible = "brcm,iproc-sr-sata-phy", |
| 464 | .data = (void *)BRCM_SATA_PHY_IPROC_SR }, |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 465 | {}, |
| 466 | }; |
| 467 | MODULE_DEVICE_TABLE(of, brcm_sata_phy_of_match); |
| 468 | |
| 469 | static int brcm_sata_phy_probe(struct platform_device *pdev) |
| 470 | { |
| 471 | struct device *dev = &pdev->dev; |
| 472 | struct device_node *dn = dev->of_node, *child; |
Jaedon Shin | 810c6f1 | 2015-11-26 11:56:34 +0900 | [diff] [blame] | 473 | const struct of_device_id *of_id; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 474 | struct brcm_sata_phy *priv; |
| 475 | struct resource *res; |
| 476 | struct phy_provider *provider; |
Julia Lawall | 0b25ff8 | 2015-11-16 12:33:14 +0100 | [diff] [blame] | 477 | int ret, count = 0; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 478 | |
| 479 | if (of_get_child_count(dn) == 0) |
| 480 | return -ENODEV; |
| 481 | |
| 482 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 483 | if (!priv) |
| 484 | return -ENOMEM; |
| 485 | dev_set_drvdata(dev, priv); |
| 486 | priv->dev = dev; |
| 487 | |
| 488 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy"); |
| 489 | priv->phy_base = devm_ioremap_resource(dev, res); |
| 490 | if (IS_ERR(priv->phy_base)) |
| 491 | return PTR_ERR(priv->phy_base); |
| 492 | |
Jaedon Shin | 810c6f1 | 2015-11-26 11:56:34 +0900 | [diff] [blame] | 493 | of_id = of_match_node(brcm_sata_phy_of_match, dn); |
| 494 | if (of_id) |
| 495 | priv->version = (enum brcm_sata_phy_version)of_id->data; |
| 496 | else |
Anup Patel | 4faee9a | 2016-03-28 10:18:27 +0530 | [diff] [blame] | 497 | priv->version = BRCM_SATA_PHY_STB_28NM; |
| 498 | |
| 499 | if (priv->version == BRCM_SATA_PHY_IPROC_NS2) { |
| 500 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 501 | "phy-ctrl"); |
| 502 | priv->ctrl_base = devm_ioremap_resource(dev, res); |
| 503 | if (IS_ERR(priv->ctrl_base)) |
| 504 | return PTR_ERR(priv->ctrl_base); |
| 505 | } |
Jaedon Shin | 810c6f1 | 2015-11-26 11:56:34 +0900 | [diff] [blame] | 506 | |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 507 | for_each_available_child_of_node(dn, child) { |
| 508 | unsigned int id; |
| 509 | struct brcm_sata_port *port; |
| 510 | |
| 511 | if (of_property_read_u32(child, "reg", &id)) { |
| 512 | dev_err(dev, "missing reg property in node %s\n", |
| 513 | child->name); |
Julia Lawall | 0b25ff8 | 2015-11-16 12:33:14 +0100 | [diff] [blame] | 514 | ret = -EINVAL; |
| 515 | goto put_child; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | if (id >= MAX_PORTS) { |
| 519 | dev_err(dev, "invalid reg: %u\n", id); |
Julia Lawall | 0b25ff8 | 2015-11-16 12:33:14 +0100 | [diff] [blame] | 520 | ret = -EINVAL; |
| 521 | goto put_child; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 522 | } |
| 523 | if (priv->phys[id].phy) { |
| 524 | dev_err(dev, "already registered port %u\n", id); |
Julia Lawall | 0b25ff8 | 2015-11-16 12:33:14 +0100 | [diff] [blame] | 525 | ret = -EINVAL; |
| 526 | goto put_child; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | port = &priv->phys[id]; |
| 530 | port->portnum = id; |
| 531 | port->phy_priv = priv; |
Jaedon Shin | c1602a1 | 2015-11-26 11:56:35 +0900 | [diff] [blame] | 532 | port->phy = devm_phy_create(dev, child, &phy_ops); |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 533 | port->ssc_en = of_property_read_bool(child, "brcm,enable-ssc"); |
| 534 | if (IS_ERR(port->phy)) { |
| 535 | dev_err(dev, "failed to create PHY\n"); |
Julia Lawall | 0b25ff8 | 2015-11-16 12:33:14 +0100 | [diff] [blame] | 536 | ret = PTR_ERR(port->phy); |
| 537 | goto put_child; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | phy_set_drvdata(port->phy, port); |
| 541 | count++; |
| 542 | } |
| 543 | |
| 544 | provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); |
| 545 | if (IS_ERR(provider)) { |
| 546 | dev_err(dev, "could not register PHY provider\n"); |
| 547 | return PTR_ERR(provider); |
| 548 | } |
| 549 | |
| 550 | dev_info(dev, "registered %d port(s)\n", count); |
| 551 | |
| 552 | return 0; |
Julia Lawall | 0b25ff8 | 2015-11-16 12:33:14 +0100 | [diff] [blame] | 553 | put_child: |
| 554 | of_node_put(child); |
| 555 | return ret; |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static struct platform_driver brcm_sata_phy_driver = { |
| 559 | .probe = brcm_sata_phy_probe, |
| 560 | .driver = { |
| 561 | .of_match_table = brcm_sata_phy_of_match, |
Anup Patel | 037c418 | 2016-03-28 10:18:26 +0530 | [diff] [blame] | 562 | .name = "brcm-sata-phy", |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 563 | } |
| 564 | }; |
| 565 | module_platform_driver(brcm_sata_phy_driver); |
| 566 | |
Anup Patel | 037c418 | 2016-03-28 10:18:26 +0530 | [diff] [blame] | 567 | MODULE_DESCRIPTION("Broadcom SATA PHY driver"); |
Brian Norris | 0d48680 | 2015-05-20 17:18:40 -0700 | [diff] [blame] | 568 | MODULE_LICENSE("GPL"); |
| 569 | MODULE_AUTHOR("Marc Carino"); |
| 570 | MODULE_AUTHOR("Brian Norris"); |
Anup Patel | 037c418 | 2016-03-28 10:18:26 +0530 | [diff] [blame] | 571 | MODULE_ALIAS("platform:phy-brcm-sata"); |