1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
4 * Written by Jean-Jacques Hiblot <jjhiblot@ti.com>
10 #include <generic-phy.h>
12 #include <asm/arch/sys_proto.h>
16 /* PLLCTRL Registers */
17 #define PLL_STATUS 0x00000004
18 #define PLL_GO 0x00000008
19 #define PLL_CONFIGURATION1 0x0000000C
20 #define PLL_CONFIGURATION2 0x00000010
21 #define PLL_CONFIGURATION3 0x00000014
22 #define PLL_CONFIGURATION4 0x00000020
24 #define PLL_REGM_MASK 0x001FFE00
25 #define PLL_REGM_SHIFT 9
26 #define PLL_REGM_F_MASK 0x0003FFFF
27 #define PLL_REGM_F_SHIFT 0
28 #define PLL_REGN_MASK 0x000001FE
29 #define PLL_REGN_SHIFT 1
30 #define PLL_SELFREQDCO_MASK 0x0000000E
31 #define PLL_SELFREQDCO_SHIFT 1
32 #define PLL_SD_MASK 0x0003FC00
33 #define PLL_SD_SHIFT 10
34 #define SET_PLL_GO 0x1
35 #define PLL_TICOPWDN BIT(16)
36 #define PLL_LDOPWDN BIT(15)
40 /* Software rest for the SATA PLL (in CTRL_CORE_SMA_SW_0 register)*/
41 #define SATA_PLL_SOFT_RESET (1<<18)
43 /* PHY POWER CONTROL Register */
44 #define OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK 0x003FC000
45 #define OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT 0xE
47 #define OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_MASK 0xFFC00000
48 #define OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_SHIFT 0x16
50 #define OMAP_CTRL_PIPE3_PHY_TX_RX_POWERON 0x3
51 #define OMAP_CTRL_PIPE3_PHY_TX_RX_POWEROFF 0x0
54 #define PLL_IDLE_TIME 100 /* in milliseconds */
55 #define PLL_LOCK_TIME 100 /* in milliseconds */
58 void __iomem *pll_ctrl_base;
59 void __iomem *power_reg;
60 void __iomem *pll_reset_reg;
61 struct pipe3_dpll_map *dpll_map;
65 struct pipe3_dpll_params {
73 struct pipe3_dpll_map {
75 struct pipe3_dpll_params params;
78 static inline u32 omap_pipe3_readl(void __iomem *addr, unsigned offset)
80 return readl(addr + offset);
83 static inline void omap_pipe3_writel(void __iomem *addr, unsigned offset,
86 writel(data, addr + offset);
89 static struct pipe3_dpll_params *omap_pipe3_get_dpll_params(struct omap_pipe3
93 struct pipe3_dpll_map *dpll_map = pipe3->dpll_map;
95 rate = get_sys_clk_freq();
97 for (; dpll_map->rate; dpll_map++) {
98 if (rate == dpll_map->rate)
99 return &dpll_map->params;
102 printf("%s: No DPLL configuration for %u Hz SYS CLK\n",
107 static int omap_pipe3_wait_lock(struct omap_pipe3 *pipe3)
110 int timeout = PLL_LOCK_TIME;
114 val = omap_pipe3_readl(pipe3->pll_ctrl_base, PLL_STATUS);
119 if (!(val & PLL_LOCK)) {
120 printf("%s: DPLL failed to lock\n", __func__);
127 static int omap_pipe3_dpll_program(struct omap_pipe3 *pipe3)
130 struct pipe3_dpll_params *dpll_params;
132 dpll_params = omap_pipe3_get_dpll_params(pipe3);
134 printf("%s: Invalid DPLL parameters\n", __func__);
138 val = omap_pipe3_readl(pipe3->pll_ctrl_base, PLL_CONFIGURATION1);
139 val &= ~PLL_REGN_MASK;
140 val |= dpll_params->n << PLL_REGN_SHIFT;
141 omap_pipe3_writel(pipe3->pll_ctrl_base, PLL_CONFIGURATION1, val);
143 val = omap_pipe3_readl(pipe3->pll_ctrl_base, PLL_CONFIGURATION2);
144 val &= ~PLL_SELFREQDCO_MASK;
145 val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
146 omap_pipe3_writel(pipe3->pll_ctrl_base, PLL_CONFIGURATION2, val);
148 val = omap_pipe3_readl(pipe3->pll_ctrl_base, PLL_CONFIGURATION1);
149 val &= ~PLL_REGM_MASK;
150 val |= dpll_params->m << PLL_REGM_SHIFT;
151 omap_pipe3_writel(pipe3->pll_ctrl_base, PLL_CONFIGURATION1, val);
153 val = omap_pipe3_readl(pipe3->pll_ctrl_base, PLL_CONFIGURATION4);
154 val &= ~PLL_REGM_F_MASK;
155 val |= dpll_params->mf << PLL_REGM_F_SHIFT;
156 omap_pipe3_writel(pipe3->pll_ctrl_base, PLL_CONFIGURATION4, val);
158 val = omap_pipe3_readl(pipe3->pll_ctrl_base, PLL_CONFIGURATION3);
160 val |= dpll_params->sd << PLL_SD_SHIFT;
161 omap_pipe3_writel(pipe3->pll_ctrl_base, PLL_CONFIGURATION3, val);
163 omap_pipe3_writel(pipe3->pll_ctrl_base, PLL_GO, SET_PLL_GO);
165 return omap_pipe3_wait_lock(pipe3);
168 static void omap_control_pipe3_power(struct omap_pipe3 *pipe3, int on)
172 val = readl(pipe3->power_reg);
174 rate = get_sys_clk_freq();
178 val &= ~(OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK |
179 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_MASK);
180 val |= OMAP_CTRL_PIPE3_PHY_TX_RX_POWERON <<
181 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT;
183 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_SHIFT;
185 val &= ~OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK;
186 val |= OMAP_CTRL_PIPE3_PHY_TX_RX_POWEROFF <<
187 OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT;
190 writel(val, pipe3->power_reg);
193 static int pipe3_init(struct phy *phy)
197 struct omap_pipe3 *pipe3 = dev_get_priv(phy->dev);
199 /* Program the DPLL only if not locked */
200 val = omap_pipe3_readl(pipe3->pll_ctrl_base, PLL_STATUS);
201 if (!(val & PLL_LOCK)) {
202 ret = omap_pipe3_dpll_program(pipe3);
206 /* else just bring it out of IDLE mode */
207 val = omap_pipe3_readl(pipe3->pll_ctrl_base,
209 if (val & PLL_IDLE) {
211 omap_pipe3_writel(pipe3->pll_ctrl_base,
212 PLL_CONFIGURATION2, val);
213 ret = omap_pipe3_wait_lock(pipe3);
221 static int pipe3_power_on(struct phy *phy)
223 struct omap_pipe3 *pipe3 = dev_get_priv(phy->dev);
225 /* Power up the PHY */
226 omap_control_pipe3_power(pipe3, 1);
231 static int pipe3_power_off(struct phy *phy)
233 struct omap_pipe3 *pipe3 = dev_get_priv(phy->dev);
235 /* Power down the PHY */
236 omap_control_pipe3_power(pipe3, 0);
241 static int pipe3_exit(struct phy *phy)
244 int timeout = PLL_IDLE_TIME;
245 struct omap_pipe3 *pipe3 = dev_get_priv(phy->dev);
247 pipe3_power_off(phy);
249 /* Put DPLL in IDLE mode */
250 val = omap_pipe3_readl(pipe3->pll_ctrl_base, PLL_CONFIGURATION2);
252 omap_pipe3_writel(pipe3->pll_ctrl_base, PLL_CONFIGURATION2, val);
254 /* wait for LDO and Oscillator to power down */
257 val = omap_pipe3_readl(pipe3->pll_ctrl_base, PLL_STATUS);
258 if ((val & PLL_TICOPWDN) && (val & PLL_LDOPWDN))
262 if (!(val & PLL_TICOPWDN) || !(val & PLL_LDOPWDN)) {
263 pr_err("%s: Failed to power down DPLL: PLL_STATUS 0x%x\n",
268 val = readl(pipe3->pll_reset_reg);
269 writel(val | SATA_PLL_SOFT_RESET, pipe3->pll_reset_reg);
271 writel(val & ~SATA_PLL_SOFT_RESET, pipe3->pll_reset_reg);
275 static void *get_reg(struct udevice *dev, const char *name)
277 struct udevice *syscon;
278 struct regmap *regmap;
283 err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
286 pr_err("unable to find syscon device for %s (%d)\n",
291 regmap = syscon_get_regmap(syscon);
292 if (IS_ERR(regmap)) {
293 pr_err("unable to find regmap for %s (%ld)\n",
294 name, PTR_ERR(regmap));
298 cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), name,
300 if (len < 2*sizeof(fdt32_t)) {
301 pr_err("offset not available for %s\n", name);
305 base = regmap_get_range(regmap, 0);
309 return fdtdec_get_number(cell + 1, 1) + base;
312 static int pipe3_phy_probe(struct udevice *dev)
316 struct omap_pipe3 *pipe3 = dev_get_priv(dev);
318 addr = devfdt_get_addr_size_index(dev, 2, &sz);
319 if (addr == FDT_ADDR_T_NONE) {
320 pr_err("missing pll ctrl address\n");
324 pipe3->pll_ctrl_base = map_physmem(addr, sz, MAP_NOCACHE);
325 if (!pipe3->pll_ctrl_base) {
326 pr_err("unable to remap pll ctrl\n");
330 pipe3->power_reg = get_reg(dev, "syscon-phy-power");
331 if (!pipe3->power_reg)
334 pipe3->pll_reset_reg = get_reg(dev, "syscon-pllreset");
335 if (!pipe3->pll_reset_reg)
338 pipe3->dpll_map = (struct pipe3_dpll_map *)dev_get_driver_data(dev);
343 static struct pipe3_dpll_map dpll_map_sata[] = {
344 {12000000, {1000, 7, 4, 6, 0} }, /* 12 MHz */
345 {16800000, {714, 7, 4, 6, 0} }, /* 16.8 MHz */
346 {19200000, {625, 7, 4, 6, 0} }, /* 19.2 MHz */
347 {20000000, {600, 7, 4, 6, 0} }, /* 20 MHz */
348 {26000000, {461, 7, 4, 6, 0} }, /* 26 MHz */
349 {38400000, {312, 7, 4, 6, 0} }, /* 38.4 MHz */
350 { }, /* Terminator */
353 static const struct udevice_id pipe3_phy_ids[] = {
354 { .compatible = "ti,phy-pipe3-sata", .data = (ulong)&dpll_map_sata },
358 static struct phy_ops pipe3_phy_ops = {
360 .power_on = pipe3_power_on,
361 .power_off = pipe3_power_off,
365 U_BOOT_DRIVER(pipe3_phy) = {
368 .of_match = pipe3_phy_ids,
369 .ops = &pipe3_phy_ops,
370 .probe = pipe3_phy_probe,
371 .priv_auto_alloc_size = sizeof(struct omap_pipe3),