blob: 0152d0b32daeea66ff0271102a012b59c929608a [file] [log] [blame]
Brian Norris0d486802015-05-20 17:18:40 -07001/*
2 * Broadcom SATA3 AHCI Controller PHY Driver
3 *
Anup Patel037c4182016-03-28 10:18:26 +05304 * Copyright (C) 2016 Broadcom
Brian Norris0d486802015-05-20 17:18:40 -07005 *
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 Patel4faee9a2016-03-28 10:18:27 +053017#include <linux/delay.h>
Brian Norris0d486802015-05-20 17:18:40 -070018#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 Patel4faee9a2016-03-28 10:18:27 +053028#define SATA_PCB_BANK_OFFSET 0x23c
29#define SATA_PCB_REG_OFFSET(ofs) ((ofs) * 4)
Brian Norris0d486802015-05-20 17:18:40 -070030
31#define MAX_PORTS 2
32
33/* Register offset between PHYs in PCB space */
Anup Patel4faee9a2016-03-28 10:18:27 +053034#define SATA_PCB_REG_28NM_SPACE_SIZE 0x1000
Jaedon Shin810c6f12015-11-26 11:56:34 +090035
Jaedon Shinc1602a12015-11-26 11:56:35 +090036/* The older SATA PHY registers duplicated per port registers within the map,
37 * rather than having a separate map per port.
38 */
Anup Patel4faee9a2016-03-28 10:18:27 +053039#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 Shinc1602a12015-11-26 11:56:35 +090043
Jaedon Shin810c6f12015-11-26 11:56:34 +090044enum brcm_sata_phy_version {
Anup Patel4faee9a2016-03-28 10:18:27 +053045 BRCM_SATA_PHY_STB_28NM,
46 BRCM_SATA_PHY_STB_40NM,
47 BRCM_SATA_PHY_IPROC_NS2,
Yendapally Reddy Dhananjaya Reddy02481282016-06-16 09:53:34 -040048 BRCM_SATA_PHY_IPROC_NSP,
Srinath Mannam80886f72017-06-08 17:01:39 +053049 BRCM_SATA_PHY_IPROC_SR,
Jaedon Shin810c6f12015-11-26 11:56:34 +090050};
Brian Norris0d486802015-05-20 17:18:40 -070051
52struct brcm_sata_port {
53 int portnum;
54 struct phy *phy;
55 struct brcm_sata_phy *phy_priv;
56 bool ssc_en;
57};
58
59struct brcm_sata_phy {
60 struct device *dev;
61 void __iomem *phy_base;
Anup Patel4faee9a2016-03-28 10:18:27 +053062 void __iomem *ctrl_base;
Jaedon Shin810c6f12015-11-26 11:56:34 +090063 enum brcm_sata_phy_version version;
Brian Norris0d486802015-05-20 17:18:40 -070064
65 struct brcm_sata_port phys[MAX_PORTS];
66};
67
Anup Patel4faee9a2016-03-28 10:18:27 +053068enum 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 Norris0d486802015-05-20 17:18:40 -070077 PLL_REG_BANK_0_PLLCONTROL_0 = 0x81,
Yendapally Reddy Dhananjaya Reddy02481282016-06-16 09:53:34 -040078 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 Mannam80886f72017-06-08 17:01:39 +053085 PLL_ACTRL6 = 0x86,
Brian Norris0d486802015-05-20 17:18:40 -070086
Anup Patel4faee9a2016-03-28 10:18:27 +053087 PLL1_REG_BANK = 0x060,
88 PLL1_ACTRL2 = 0x82,
89 PLL1_ACTRL3 = 0x83,
90 PLL1_ACTRL4 = 0x84,
91
Srinath Mannam80886f72017-06-08 17:01:39 +053092 TX_REG_BANK = 0x070,
93 TX_ACTRL0 = 0x80,
94 TX_ACTRL0_TXPOL_FLIP = BIT(6),
95
Anup Patel4faee9a2016-03-28 10:18:27 +053096 OOB_REG_BANK = 0x150,
Yendapally Reddy Dhananjaya Reddy02481282016-06-16 09:53:34 -040097 OOB1_REG_BANK = 0x160,
Anup Patel4faee9a2016-03-28 10:18:27 +053098 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 Norris0d486802015-05-20 17:18:40 -0700117 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 Patel4faee9a2016-03-28 10:18:27 +0530128enum sata_phy_ctrl_regs {
129 PHY_CTRL_1 = 0x0,
130 PHY_CTRL_1_RESET = BIT(0),
131};
132
133static inline void __iomem *brcm_sata_pcb_base(struct brcm_sata_port *port)
Brian Norris0d486802015-05-20 17:18:40 -0700134{
135 struct brcm_sata_phy *priv = port->phy_priv;
Anup Patel4faee9a2016-03-28 10:18:27 +0530136 u32 size = 0;
Brian Norris0d486802015-05-20 17:18:40 -0700137
Anup Patel4faee9a2016-03-28 10:18:27 +0530138 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 Shinc1602a12015-11-26 11:56:35 +0900147 dev_err(priv->dev, "invalid phy version\n");
Anup Patel4faee9a2016-03-28 10:18:27 +0530148 break;
Vivek Gautam0e65ba22016-10-20 12:23:38 +0530149 }
Jaedon Shin810c6f12015-11-26 11:56:34 +0900150
Anup Patel4faee9a2016-03-28 10:18:27 +0530151 return priv->phy_base + (port->portnum * size);
Brian Norris0d486802015-05-20 17:18:40 -0700152}
153
Anup Patel4faee9a2016-03-28 10:18:27 +0530154static 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 Gautam0e65ba22016-10-20 12:23:38 +0530166 }
Anup Patel4faee9a2016-03-28 10:18:27 +0530167
168 return priv->ctrl_base + (port->portnum * size);
169}
170
171static void brcm_sata_phy_wr(void __iomem *pcb_base, u32 bank,
172 u32 ofs, u32 msk, u32 value)
Brian Norris0d486802015-05-20 17:18:40 -0700173{
174 u32 tmp;
175
Anup Patel4faee9a2016-03-28 10:18:27 +0530176 writel(bank, pcb_base + SATA_PCB_BANK_OFFSET);
177 tmp = readl(pcb_base + SATA_PCB_REG_OFFSET(ofs));
Brian Norris0d486802015-05-20 17:18:40 -0700178 tmp = (tmp & msk) | value;
Anup Patel4faee9a2016-03-28 10:18:27 +0530179 writel(tmp, pcb_base + SATA_PCB_REG_OFFSET(ofs));
180}
181
182static 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 Norris0d486802015-05-20 17:18:40 -0700186}
187
188/* These defaults were characterized by H/W group */
Anup Patel4faee9a2016-03-28 10:18:27 +0530189#define STB_FMIN_VAL_DEFAULT 0x3df
190#define STB_FMAX_VAL_DEFAULT 0x3df
191#define STB_FMAX_VAL_SSC 0x83
Brian Norris0d486802015-05-20 17:18:40 -0700192
Florian Fainelli6ec248f2017-10-11 17:53:11 -0700193static void brcm_stb_sata_ssc_init(struct brcm_sata_port *port)
Brian Norris0d486802015-05-20 17:18:40 -0700194{
Anup Patel4faee9a2016-03-28 10:18:27 +0530195 void __iomem *base = brcm_sata_pcb_base(port);
Brian Norris0d486802015-05-20 17:18:40 -0700196 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 Patel4faee9a2016-03-28 10:18:27 +0530201 brcm_sata_phy_wr(base, TXPMD_REG_BANK, TXPMD_CONTROL1, ~tmp, tmp);
Brian Norris0d486802015-05-20 17:18:40 -0700202
203 /* set fixed min freq */
Anup Patel4faee9a2016-03-28 10:18:27 +0530204 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 Norris0d486802015-05-20 17:18:40 -0700207
208 /* set fixed max freq depending on SSC config */
209 if (port->ssc_en) {
Anup Patel4faee9a2016-03-28 10:18:27 +0530210 dev_info(priv->dev, "enabling SSC on port%d\n", port->portnum);
211 tmp = STB_FMAX_VAL_SSC;
Brian Norris0d486802015-05-20 17:18:40 -0700212 } else {
Anup Patel4faee9a2016-03-28 10:18:27 +0530213 tmp = STB_FMAX_VAL_DEFAULT;
Brian Norris0d486802015-05-20 17:18:40 -0700214 }
215
Anup Patel4faee9a2016-03-28 10:18:27 +0530216 brcm_sata_phy_wr(base, TXPMD_REG_BANK, TXPMD_TX_FREQ_CTRL_CONTROL3,
Brian Norris0d486802015-05-20 17:18:40 -0700217 ~TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK, tmp);
Florian Fainelli6ec248f2017-10-11 17:53:11 -0700218}
219
220static int brcm_stb_sata_init(struct brcm_sata_port *port)
221{
222 brcm_stb_sata_ssc_init(port);
Anup Patel4faee9a2016-03-28 10:18:27 +0530223
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
232static 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 Norris0d486802015-05-20 17:18:40 -0700292}
293
Yendapally Reddy Dhananjaya Reddy02481282016-06-16 09:53:34 -0400294static 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 Carpenterd9c51f42017-06-19 13:56:05 +0300343 while (--try) {
Yendapally Reddy Dhananjaya Reddy02481282016-06-16 09:53:34 -0400344 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 Mannam80886f72017-06-08 17:01:39 +0530361/* 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
369static 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 Norris0d486802015-05-20 17:18:40 -0700423static int brcm_sata_phy_init(struct phy *phy)
424{
Anup Patel4faee9a2016-03-28 10:18:27 +0530425 int rc;
Brian Norris0d486802015-05-20 17:18:40 -0700426 struct brcm_sata_port *port = phy_get_drvdata(phy);
427
Anup Patel4faee9a2016-03-28 10:18:27 +0530428 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 Reddy02481282016-06-16 09:53:34 -0400436 case BRCM_SATA_PHY_IPROC_NSP:
437 rc = brcm_nsp_sata_init(port);
438 break;
Srinath Mannam80886f72017-06-08 17:01:39 +0530439 case BRCM_SATA_PHY_IPROC_SR:
440 rc = brcm_sr_sata_init(port);
441 break;
Anup Patel4faee9a2016-03-28 10:18:27 +0530442 default:
443 rc = -ENODEV;
Vivek Gautam0e65ba22016-10-20 12:23:38 +0530444 }
Brian Norris0d486802015-05-20 17:18:40 -0700445
Axel Linbf8ca652016-08-10 18:04:44 +0800446 return rc;
Brian Norris0d486802015-05-20 17:18:40 -0700447}
448
Jaedon Shinc1602a12015-11-26 11:56:35 +0900449static const struct phy_ops phy_ops = {
Brian Norris0d486802015-05-20 17:18:40 -0700450 .init = brcm_sata_phy_init,
451 .owner = THIS_MODULE,
452};
453
454static const struct of_device_id brcm_sata_phy_of_match[] = {
Jaedon Shin810c6f12015-11-26 11:56:34 +0900455 { .compatible = "brcm,bcm7445-sata-phy",
Anup Patel4faee9a2016-03-28 10:18:27 +0530456 .data = (void *)BRCM_SATA_PHY_STB_28NM },
Jaedon Shinc1602a12015-11-26 11:56:35 +0900457 { .compatible = "brcm,bcm7425-sata-phy",
Anup Patel4faee9a2016-03-28 10:18:27 +0530458 .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 Reddy02481282016-06-16 09:53:34 -0400461 { .compatible = "brcm,iproc-nsp-sata-phy",
462 .data = (void *)BRCM_SATA_PHY_IPROC_NSP },
Srinath Mannam80886f72017-06-08 17:01:39 +0530463 { .compatible = "brcm,iproc-sr-sata-phy",
464 .data = (void *)BRCM_SATA_PHY_IPROC_SR },
Brian Norris0d486802015-05-20 17:18:40 -0700465 {},
466};
467MODULE_DEVICE_TABLE(of, brcm_sata_phy_of_match);
468
469static 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 Shin810c6f12015-11-26 11:56:34 +0900473 const struct of_device_id *of_id;
Brian Norris0d486802015-05-20 17:18:40 -0700474 struct brcm_sata_phy *priv;
475 struct resource *res;
476 struct phy_provider *provider;
Julia Lawall0b25ff82015-11-16 12:33:14 +0100477 int ret, count = 0;
Brian Norris0d486802015-05-20 17:18:40 -0700478
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 Shin810c6f12015-11-26 11:56:34 +0900493 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 Patel4faee9a2016-03-28 10:18:27 +0530497 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 Shin810c6f12015-11-26 11:56:34 +0900506
Brian Norris0d486802015-05-20 17:18:40 -0700507 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 Lawall0b25ff82015-11-16 12:33:14 +0100514 ret = -EINVAL;
515 goto put_child;
Brian Norris0d486802015-05-20 17:18:40 -0700516 }
517
518 if (id >= MAX_PORTS) {
519 dev_err(dev, "invalid reg: %u\n", id);
Julia Lawall0b25ff82015-11-16 12:33:14 +0100520 ret = -EINVAL;
521 goto put_child;
Brian Norris0d486802015-05-20 17:18:40 -0700522 }
523 if (priv->phys[id].phy) {
524 dev_err(dev, "already registered port %u\n", id);
Julia Lawall0b25ff82015-11-16 12:33:14 +0100525 ret = -EINVAL;
526 goto put_child;
Brian Norris0d486802015-05-20 17:18:40 -0700527 }
528
529 port = &priv->phys[id];
530 port->portnum = id;
531 port->phy_priv = priv;
Jaedon Shinc1602a12015-11-26 11:56:35 +0900532 port->phy = devm_phy_create(dev, child, &phy_ops);
Brian Norris0d486802015-05-20 17:18:40 -0700533 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 Lawall0b25ff82015-11-16 12:33:14 +0100536 ret = PTR_ERR(port->phy);
537 goto put_child;
Brian Norris0d486802015-05-20 17:18:40 -0700538 }
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 Lawall0b25ff82015-11-16 12:33:14 +0100553put_child:
554 of_node_put(child);
555 return ret;
Brian Norris0d486802015-05-20 17:18:40 -0700556}
557
558static 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 Patel037c4182016-03-28 10:18:26 +0530562 .name = "brcm-sata-phy",
Brian Norris0d486802015-05-20 17:18:40 -0700563 }
564};
565module_platform_driver(brcm_sata_phy_driver);
566
Anup Patel037c4182016-03-28 10:18:26 +0530567MODULE_DESCRIPTION("Broadcom SATA PHY driver");
Brian Norris0d486802015-05-20 17:18:40 -0700568MODULE_LICENSE("GPL");
569MODULE_AUTHOR("Marc Carino");
570MODULE_AUTHOR("Brian Norris");
Anup Patel037c4182016-03-28 10:18:26 +0530571MODULE_ALIAS("platform:phy-brcm-sata");