2 * Broadcom SATA3 AHCI Controller PHY Driver
4 * Copyright (C) 2016 Broadcom
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)
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.
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include <linux/phy/phy.h>
26 #include <linux/platform_device.h>
28 #define SATA_PCB_BANK_OFFSET 0x23c
29 #define SATA_PCB_REG_OFFSET(ofs) ((ofs) * 4)
33 /* Register offset between PHYs in PCB space */
34 #define SATA_PCB_REG_28NM_SPACE_SIZE 0x1000
36 /* The older SATA PHY registers duplicated per port registers within the map,
37 * rather than having a separate map per port.
39 #define SATA_PCB_REG_40NM_SPACE_SIZE 0x10
41 /* Register offset between PHYs in PHY control space */
42 #define SATA_PHY_CTRL_REG_28NM_SPACE_SIZE 0x8
44 enum brcm_sata_phy_version
{
45 BRCM_SATA_PHY_STB_28NM
,
46 BRCM_SATA_PHY_STB_40NM
,
47 BRCM_SATA_PHY_IPROC_NS2
,
50 struct brcm_sata_port
{
53 struct brcm_sata_phy
*phy_priv
;
57 struct brcm_sata_phy
{
59 void __iomem
*phy_base
;
60 void __iomem
*ctrl_base
;
61 enum brcm_sata_phy_version version
;
63 struct brcm_sata_port phys
[MAX_PORTS
];
67 BLOCK0_REG_BANK
= 0x000,
68 BLOCK0_XGXSSTATUS
= 0x81,
69 BLOCK0_XGXSSTATUS_PLL_LOCK
= BIT(12),
71 BLOCK0_SPARE_OOB_CLK_SEL_MASK
= 0x3,
72 BLOCK0_SPARE_OOB_CLK_SEL_REFBY2
= 0x1,
74 PLL_REG_BANK_0
= 0x050,
75 PLL_REG_BANK_0_PLLCONTROL_0
= 0x81,
77 PLL1_REG_BANK
= 0x060,
84 OOB_CTRL1_BURST_MAX_MASK
= 0xf,
85 OOB_CTRL1_BURST_MAX_SHIFT
= 12,
86 OOB_CTRL1_BURST_MIN_MASK
= 0xf,
87 OOB_CTRL1_BURST_MIN_SHIFT
= 8,
88 OOB_CTRL1_WAKE_IDLE_MAX_MASK
= 0xf,
89 OOB_CTRL1_WAKE_IDLE_MAX_SHIFT
= 4,
90 OOB_CTRL1_WAKE_IDLE_MIN_MASK
= 0xf,
91 OOB_CTRL1_WAKE_IDLE_MIN_SHIFT
= 0,
93 OOB_CTRL2_SEL_ENA_SHIFT
= 15,
94 OOB_CTRL2_SEL_ENA_RC_SHIFT
= 14,
95 OOB_CTRL2_RESET_IDLE_MAX_MASK
= 0x3f,
96 OOB_CTRL2_RESET_IDLE_MAX_SHIFT
= 8,
97 OOB_CTRL2_BURST_CNT_MASK
= 0x3,
98 OOB_CTRL2_BURST_CNT_SHIFT
= 6,
99 OOB_CTRL2_RESET_IDLE_MIN_MASK
= 0x3f,
100 OOB_CTRL2_RESET_IDLE_MIN_SHIFT
= 0,
102 TXPMD_REG_BANK
= 0x1a0,
103 TXPMD_CONTROL1
= 0x81,
104 TXPMD_CONTROL1_TX_SSC_EN_FRC
= BIT(0),
105 TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL
= BIT(1),
106 TXPMD_TX_FREQ_CTRL_CONTROL1
= 0x82,
107 TXPMD_TX_FREQ_CTRL_CONTROL2
= 0x83,
108 TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK
= 0x3ff,
109 TXPMD_TX_FREQ_CTRL_CONTROL3
= 0x84,
110 TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK
= 0x3ff,
113 enum sata_phy_ctrl_regs
{
115 PHY_CTRL_1_RESET
= BIT(0),
118 static inline void __iomem
*brcm_sata_pcb_base(struct brcm_sata_port
*port
)
120 struct brcm_sata_phy
*priv
= port
->phy_priv
;
123 switch (priv
->version
) {
124 case BRCM_SATA_PHY_STB_28NM
:
125 case BRCM_SATA_PHY_IPROC_NS2
:
126 size
= SATA_PCB_REG_28NM_SPACE_SIZE
;
128 case BRCM_SATA_PHY_STB_40NM
:
129 size
= SATA_PCB_REG_40NM_SPACE_SIZE
;
132 dev_err(priv
->dev
, "invalid phy version\n");
136 return priv
->phy_base
+ (port
->portnum
* size
);
139 static inline void __iomem
*brcm_sata_ctrl_base(struct brcm_sata_port
*port
)
141 struct brcm_sata_phy
*priv
= port
->phy_priv
;
144 switch (priv
->version
) {
145 case BRCM_SATA_PHY_IPROC_NS2
:
146 size
= SATA_PHY_CTRL_REG_28NM_SPACE_SIZE
;
149 dev_err(priv
->dev
, "invalid phy version\n");
153 return priv
->ctrl_base
+ (port
->portnum
* size
);
156 static void brcm_sata_phy_wr(void __iomem
*pcb_base
, u32 bank
,
157 u32 ofs
, u32 msk
, u32 value
)
161 writel(bank
, pcb_base
+ SATA_PCB_BANK_OFFSET
);
162 tmp
= readl(pcb_base
+ SATA_PCB_REG_OFFSET(ofs
));
163 tmp
= (tmp
& msk
) | value
;
164 writel(tmp
, pcb_base
+ SATA_PCB_REG_OFFSET(ofs
));
167 static u32
brcm_sata_phy_rd(void __iomem
*pcb_base
, u32 bank
, u32 ofs
)
169 writel(bank
, pcb_base
+ SATA_PCB_BANK_OFFSET
);
170 return readl(pcb_base
+ SATA_PCB_REG_OFFSET(ofs
));
173 /* These defaults were characterized by H/W group */
174 #define STB_FMIN_VAL_DEFAULT 0x3df
175 #define STB_FMAX_VAL_DEFAULT 0x3df
176 #define STB_FMAX_VAL_SSC 0x83
178 static int brcm_stb_sata_init(struct brcm_sata_port
*port
)
180 void __iomem
*base
= brcm_sata_pcb_base(port
);
181 struct brcm_sata_phy
*priv
= port
->phy_priv
;
184 /* override the TX spread spectrum setting */
185 tmp
= TXPMD_CONTROL1_TX_SSC_EN_FRC_VAL
| TXPMD_CONTROL1_TX_SSC_EN_FRC
;
186 brcm_sata_phy_wr(base
, TXPMD_REG_BANK
, TXPMD_CONTROL1
, ~tmp
, tmp
);
188 /* set fixed min freq */
189 brcm_sata_phy_wr(base
, TXPMD_REG_BANK
, TXPMD_TX_FREQ_CTRL_CONTROL2
,
190 ~TXPMD_TX_FREQ_CTRL_CONTROL2_FMIN_MASK
,
191 STB_FMIN_VAL_DEFAULT
);
193 /* set fixed max freq depending on SSC config */
195 dev_info(priv
->dev
, "enabling SSC on port%d\n", port
->portnum
);
196 tmp
= STB_FMAX_VAL_SSC
;
198 tmp
= STB_FMAX_VAL_DEFAULT
;
201 brcm_sata_phy_wr(base
, TXPMD_REG_BANK
, TXPMD_TX_FREQ_CTRL_CONTROL3
,
202 ~TXPMD_TX_FREQ_CTRL_CONTROL3_FMAX_MASK
, tmp
);
207 /* NS2 SATA PLL1 defaults were characterized by H/W group */
208 #define NS2_PLL1_ACTRL2_MAGIC 0x1df8
209 #define NS2_PLL1_ACTRL3_MAGIC 0x2b00
210 #define NS2_PLL1_ACTRL4_MAGIC 0x8824
212 static int brcm_ns2_sata_init(struct brcm_sata_port
*port
)
216 void __iomem
*base
= brcm_sata_pcb_base(port
);
217 void __iomem
*ctrl_base
= brcm_sata_ctrl_base(port
);
218 struct device
*dev
= port
->phy_priv
->dev
;
220 /* Configure OOB control */
222 val
|= (0xc << OOB_CTRL1_BURST_MAX_SHIFT
);
223 val
|= (0x4 << OOB_CTRL1_BURST_MIN_SHIFT
);
224 val
|= (0x9 << OOB_CTRL1_WAKE_IDLE_MAX_SHIFT
);
225 val
|= (0x3 << OOB_CTRL1_WAKE_IDLE_MIN_SHIFT
);
226 brcm_sata_phy_wr(base
, OOB_REG_BANK
, OOB_CTRL1
, 0x0, val
);
228 val
|= (0x1b << OOB_CTRL2_RESET_IDLE_MAX_SHIFT
);
229 val
|= (0x2 << OOB_CTRL2_BURST_CNT_SHIFT
);
230 val
|= (0x9 << OOB_CTRL2_RESET_IDLE_MIN_SHIFT
);
231 brcm_sata_phy_wr(base
, OOB_REG_BANK
, OOB_CTRL2
, 0x0, val
);
233 /* Configure PHY PLL register bank 1 */
234 val
= NS2_PLL1_ACTRL2_MAGIC
;
235 brcm_sata_phy_wr(base
, PLL1_REG_BANK
, PLL1_ACTRL2
, 0x0, val
);
236 val
= NS2_PLL1_ACTRL3_MAGIC
;
237 brcm_sata_phy_wr(base
, PLL1_REG_BANK
, PLL1_ACTRL3
, 0x0, val
);
238 val
= NS2_PLL1_ACTRL4_MAGIC
;
239 brcm_sata_phy_wr(base
, PLL1_REG_BANK
, PLL1_ACTRL4
, 0x0, val
);
241 /* Configure PHY BLOCK0 register bank */
242 /* Set oob_clk_sel to refclk/2 */
243 brcm_sata_phy_wr(base
, BLOCK0_REG_BANK
, BLOCK0_SPARE
,
244 ~BLOCK0_SPARE_OOB_CLK_SEL_MASK
,
245 BLOCK0_SPARE_OOB_CLK_SEL_REFBY2
);
247 /* Strobe PHY reset using PHY control register */
248 writel(PHY_CTRL_1_RESET
, ctrl_base
+ PHY_CTRL_1
);
250 writel(0x0, ctrl_base
+ PHY_CTRL_1
);
253 /* Wait for PHY PLL lock by polling pll_lock bit */
256 val
= brcm_sata_phy_rd(base
, BLOCK0_REG_BANK
,
258 if (val
& BLOCK0_XGXSSTATUS_PLL_LOCK
)
264 /* PLL did not lock; give up */
265 dev_err(dev
, "port%d PLL did not lock\n", port
->portnum
);
269 dev_dbg(dev
, "port%d initialized\n", port
->portnum
);
274 static int brcm_sata_phy_init(struct phy
*phy
)
277 struct brcm_sata_port
*port
= phy_get_drvdata(phy
);
279 switch (port
->phy_priv
->version
) {
280 case BRCM_SATA_PHY_STB_28NM
:
281 case BRCM_SATA_PHY_STB_40NM
:
282 rc
= brcm_stb_sata_init(port
);
284 case BRCM_SATA_PHY_IPROC_NS2
:
285 rc
= brcm_ns2_sata_init(port
);
294 static const struct phy_ops phy_ops
= {
295 .init
= brcm_sata_phy_init
,
296 .owner
= THIS_MODULE
,
299 static const struct of_device_id brcm_sata_phy_of_match
[] = {
300 { .compatible
= "brcm,bcm7445-sata-phy",
301 .data
= (void *)BRCM_SATA_PHY_STB_28NM
},
302 { .compatible
= "brcm,bcm7425-sata-phy",
303 .data
= (void *)BRCM_SATA_PHY_STB_40NM
},
304 { .compatible
= "brcm,iproc-ns2-sata-phy",
305 .data
= (void *)BRCM_SATA_PHY_IPROC_NS2
},
308 MODULE_DEVICE_TABLE(of
, brcm_sata_phy_of_match
);
310 static int brcm_sata_phy_probe(struct platform_device
*pdev
)
312 struct device
*dev
= &pdev
->dev
;
313 struct device_node
*dn
= dev
->of_node
, *child
;
314 const struct of_device_id
*of_id
;
315 struct brcm_sata_phy
*priv
;
316 struct resource
*res
;
317 struct phy_provider
*provider
;
320 if (of_get_child_count(dn
) == 0)
323 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
326 dev_set_drvdata(dev
, priv
);
329 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "phy");
330 priv
->phy_base
= devm_ioremap_resource(dev
, res
);
331 if (IS_ERR(priv
->phy_base
))
332 return PTR_ERR(priv
->phy_base
);
334 of_id
= of_match_node(brcm_sata_phy_of_match
, dn
);
336 priv
->version
= (enum brcm_sata_phy_version
)of_id
->data
;
338 priv
->version
= BRCM_SATA_PHY_STB_28NM
;
340 if (priv
->version
== BRCM_SATA_PHY_IPROC_NS2
) {
341 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
343 priv
->ctrl_base
= devm_ioremap_resource(dev
, res
);
344 if (IS_ERR(priv
->ctrl_base
))
345 return PTR_ERR(priv
->ctrl_base
);
348 for_each_available_child_of_node(dn
, child
) {
350 struct brcm_sata_port
*port
;
352 if (of_property_read_u32(child
, "reg", &id
)) {
353 dev_err(dev
, "missing reg property in node %s\n",
359 if (id
>= MAX_PORTS
) {
360 dev_err(dev
, "invalid reg: %u\n", id
);
364 if (priv
->phys
[id
].phy
) {
365 dev_err(dev
, "already registered port %u\n", id
);
370 port
= &priv
->phys
[id
];
372 port
->phy_priv
= priv
;
373 port
->phy
= devm_phy_create(dev
, child
, &phy_ops
);
374 port
->ssc_en
= of_property_read_bool(child
, "brcm,enable-ssc");
375 if (IS_ERR(port
->phy
)) {
376 dev_err(dev
, "failed to create PHY\n");
377 ret
= PTR_ERR(port
->phy
);
381 phy_set_drvdata(port
->phy
, port
);
385 provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
386 if (IS_ERR(provider
)) {
387 dev_err(dev
, "could not register PHY provider\n");
388 return PTR_ERR(provider
);
391 dev_info(dev
, "registered %d port(s)\n", count
);
399 static struct platform_driver brcm_sata_phy_driver
= {
400 .probe
= brcm_sata_phy_probe
,
402 .of_match_table
= brcm_sata_phy_of_match
,
403 .name
= "brcm-sata-phy",
406 module_platform_driver(brcm_sata_phy_driver
);
408 MODULE_DESCRIPTION("Broadcom SATA PHY driver");
409 MODULE_LICENSE("GPL");
410 MODULE_AUTHOR("Marc Carino");
411 MODULE_AUTHOR("Brian Norris");
412 MODULE_ALIAS("platform:phy-brcm-sata");