blob: 3154da08ff373de8faa91fe6d2fbdc92fa92755f [file] [log] [blame]
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +05301/*
2 * phy-ti-pipe3 - PIPE3 PHY driver.
3 *
4 * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * Author: Kishon Vijay Abraham I <kishon@ti.com>
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22#include <linux/phy/phy.h>
23#include <linux/of.h>
24#include <linux/clk.h>
25#include <linux/err.h>
26#include <linux/io.h>
27#include <linux/pm_runtime.h>
28#include <linux/delay.h>
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +020029#include <linux/phy/omap_control_phy.h>
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053030#include <linux/of_platform.h>
Roger Quadrosc934b362015-07-17 16:47:22 +030031#include <linux/mfd/syscon.h>
32#include <linux/regmap.h>
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053033
34#define PLL_STATUS 0x00000004
35#define PLL_GO 0x00000008
36#define PLL_CONFIGURATION1 0x0000000C
37#define PLL_CONFIGURATION2 0x00000010
38#define PLL_CONFIGURATION3 0x00000014
39#define PLL_CONFIGURATION4 0x00000020
40
41#define PLL_REGM_MASK 0x001FFE00
42#define PLL_REGM_SHIFT 0x9
43#define PLL_REGM_F_MASK 0x0003FFFF
44#define PLL_REGM_F_SHIFT 0x0
45#define PLL_REGN_MASK 0x000001FE
46#define PLL_REGN_SHIFT 0x1
47#define PLL_SELFREQDCO_MASK 0x0000000E
48#define PLL_SELFREQDCO_SHIFT 0x1
49#define PLL_SD_MASK 0x0003FC00
Roger Quadros1562864f2014-03-07 11:27:09 +053050#define PLL_SD_SHIFT 10
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053051#define SET_PLL_GO 0x1
Roger Quadros629138d2014-03-06 16:38:43 +020052#define PLL_LDOPWDN BIT(15)
53#define PLL_TICOPWDN BIT(16)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053054#define PLL_LOCK 0x2
55#define PLL_IDLE 0x1
56
Roger Quadrosc934b362015-07-17 16:47:22 +030057#define SATA_PLL_SOFT_RESET BIT(18)
58
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053059/*
60 * This is an Empirical value that works, need to confirm the actual
61 * value required for the PIPE3PHY_PLL_CONFIGURATION2.PLL_IDLE status
62 * to be correctly reflected in the PIPE3PHY_PLL_STATUS register.
63 */
Roger Quadros629138d2014-03-06 16:38:43 +020064#define PLL_IDLE_TIME 100 /* in milliseconds */
65#define PLL_LOCK_TIME 100 /* in milliseconds */
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053066
67struct pipe3_dpll_params {
68 u16 m;
69 u8 n;
70 u8 freq:3;
71 u8 sd;
72 u32 mf;
73};
74
Roger Quadros61f54672014-03-07 11:43:39 +053075struct pipe3_dpll_map {
76 unsigned long rate;
77 struct pipe3_dpll_params params;
78};
79
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053080struct ti_pipe3 {
81 void __iomem *pll_ctrl_base;
82 struct device *dev;
83 struct device *control_dev;
84 struct clk *wkupclk;
85 struct clk *sys_clk;
Roger Quadros1562864f2014-03-07 11:27:09 +053086 struct clk *refclk;
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +053087 struct clk *div_clk;
Roger Quadros61f54672014-03-07 11:43:39 +053088 struct pipe3_dpll_map *dpll_map;
Roger Quadrosc934b362015-07-17 16:47:22 +030089 struct regmap *dpll_reset_syscon; /* ctrl. reg. acces */
90 unsigned int dpll_reset_reg; /* reg. index within syscon */
91 bool sata_refclk_enabled;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053092};
93
Roger Quadros61f54672014-03-07 11:43:39 +053094static struct pipe3_dpll_map dpll_map_usb[] = {
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053095 {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
96 {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
97 {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
98 {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
99 {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
100 {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
Roger Quadros61f54672014-03-07 11:43:39 +0530101 { }, /* Terminator */
102};
103
104static struct pipe3_dpll_map dpll_map_sata[] = {
105 {12000000, {1000, 7, 4, 6, 0} }, /* 12 MHz */
106 {16800000, {714, 7, 4, 6, 0} }, /* 16.8 MHz */
107 {19200000, {625, 7, 4, 6, 0} }, /* 19.2 MHz */
108 {20000000, {600, 7, 4, 6, 0} }, /* 20 MHz */
109 {26000000, {461, 7, 4, 6, 0} }, /* 26 MHz */
110 {38400000, {312, 7, 4, 6, 0} }, /* 38.4 MHz */
111 { }, /* Terminator */
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530112};
113
114static inline u32 ti_pipe3_readl(void __iomem *addr, unsigned offset)
115{
116 return __raw_readl(addr + offset);
117}
118
119static inline void ti_pipe3_writel(void __iomem *addr, unsigned offset,
120 u32 data)
121{
122 __raw_writel(data, addr + offset);
123}
124
Roger Quadros61f54672014-03-07 11:43:39 +0530125static struct pipe3_dpll_params *ti_pipe3_get_dpll_params(struct ti_pipe3 *phy)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530126{
Roger Quadros61f54672014-03-07 11:43:39 +0530127 unsigned long rate;
128 struct pipe3_dpll_map *dpll_map = phy->dpll_map;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530129
Roger Quadros61f54672014-03-07 11:43:39 +0530130 rate = clk_get_rate(phy->sys_clk);
131
132 for (; dpll_map->rate; dpll_map++) {
133 if (rate == dpll_map->rate)
134 return &dpll_map->params;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530135 }
136
Roger Quadros61f54672014-03-07 11:43:39 +0530137 dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
138
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530139 return NULL;
140}
141
Roger Quadros0a0830f2015-06-02 12:10:40 +0300142static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy);
143static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy);
144
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530145static int ti_pipe3_power_off(struct phy *x)
146{
147 struct ti_pipe3 *phy = phy_get_drvdata(x);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530148
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200149 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530150
151 return 0;
152}
153
154static int ti_pipe3_power_on(struct phy *x)
155{
156 struct ti_pipe3 *phy = phy_get_drvdata(x);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530157
Roger Quadros629138d2014-03-06 16:38:43 +0200158 omap_control_phy_power(phy->control_dev, 1);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530159
Roger Quadros629138d2014-03-06 16:38:43 +0200160 return 0;
161}
162
163static int ti_pipe3_dpll_wait_lock(struct ti_pipe3 *phy)
164{
165 u32 val;
166 unsigned long timeout;
167
168 timeout = jiffies + msecs_to_jiffies(PLL_LOCK_TIME);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530169 do {
Roger Quadros629138d2014-03-06 16:38:43 +0200170 cpu_relax();
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530171 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
Roger Quadros629138d2014-03-06 16:38:43 +0200172 if (val & PLL_LOCK)
Axel Lina5e5d3c2015-03-03 20:04:55 +0800173 return 0;
Roger Quadros629138d2014-03-06 16:38:43 +0200174 } while (!time_after(jiffies, timeout));
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530175
Axel Lina5e5d3c2015-03-03 20:04:55 +0800176 dev_err(phy->dev, "DPLL failed to lock\n");
177 return -EBUSY;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530178}
179
Roger Quadros629138d2014-03-06 16:38:43 +0200180static int ti_pipe3_dpll_program(struct ti_pipe3 *phy)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530181{
182 u32 val;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530183 struct pipe3_dpll_params *dpll_params;
184
Roger Quadros61f54672014-03-07 11:43:39 +0530185 dpll_params = ti_pipe3_get_dpll_params(phy);
186 if (!dpll_params)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530187 return -EINVAL;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530188
189 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
190 val &= ~PLL_REGN_MASK;
191 val |= dpll_params->n << PLL_REGN_SHIFT;
192 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
193
194 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
195 val &= ~PLL_SELFREQDCO_MASK;
196 val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
197 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
198
199 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
200 val &= ~PLL_REGM_MASK;
201 val |= dpll_params->m << PLL_REGM_SHIFT;
202 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
203
204 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
205 val &= ~PLL_REGM_F_MASK;
206 val |= dpll_params->mf << PLL_REGM_F_SHIFT;
207 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
208
209 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
210 val &= ~PLL_SD_MASK;
211 val |= dpll_params->sd << PLL_SD_SHIFT;
212 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
213
Roger Quadros629138d2014-03-06 16:38:43 +0200214 ti_pipe3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530215
Roger Quadros629138d2014-03-06 16:38:43 +0200216 return ti_pipe3_dpll_wait_lock(phy);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530217}
218
219static int ti_pipe3_init(struct phy *x)
220{
221 struct ti_pipe3 *phy = phy_get_drvdata(x);
Roger Quadros629138d2014-03-06 16:38:43 +0200222 u32 val;
223 int ret = 0;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530224
Roger Quadros0a0830f2015-06-02 12:10:40 +0300225 ti_pipe3_enable_clocks(phy);
Vignesh R0bc09f92014-12-16 14:52:50 +0530226 /*
227 * Set pcie_pcs register to 0x96 for proper functioning of phy
228 * as recommended in AM572x TRM SPRUHZ6, section 18.5.2.2, table
229 * 18-1804.
230 */
Kishon Vijay Abraham If0e2cf72014-06-25 23:22:57 +0530231 if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) {
Vignesh R0bc09f92014-12-16 14:52:50 +0530232 omap_control_pcie_pcs(phy->control_dev, 0x96);
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530233 return 0;
Kishon Vijay Abraham If0e2cf72014-06-25 23:22:57 +0530234 }
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530235
Roger Quadros629138d2014-03-06 16:38:43 +0200236 /* Bring it out of IDLE if it is IDLE */
237 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
238 if (val & PLL_IDLE) {
239 val &= ~PLL_IDLE;
240 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
241 ret = ti_pipe3_dpll_wait_lock(phy);
242 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530243
Roger Quadros629138d2014-03-06 16:38:43 +0200244 /* Program the DPLL only if not locked */
245 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
246 if (!(val & PLL_LOCK))
247 if (ti_pipe3_dpll_program(phy))
248 return -EINVAL;
249
250 return ret;
251}
252
253static int ti_pipe3_exit(struct phy *x)
254{
255 struct ti_pipe3 *phy = phy_get_drvdata(x);
256 u32 val;
257 unsigned long timeout;
258
Roger Quadrosc934b362015-07-17 16:47:22 +0300259 /* If dpll_reset_syscon is not present we wont power down SATA DPLL
260 * due to Errata i783
261 */
262 if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata") &&
263 !phy->dpll_reset_syscon)
Roger Quadros56042e42014-03-06 16:38:44 +0200264 return 0;
265
Roger Quadros0a0830f2015-06-02 12:10:40 +0300266 /* PCIe doesn't have internal DPLL */
267 if (!of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) {
268 /* Put DPLL in IDLE mode */
269 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
270 val |= PLL_IDLE;
271 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
Roger Quadros629138d2014-03-06 16:38:43 +0200272
Roger Quadros0a0830f2015-06-02 12:10:40 +0300273 /* wait for LDO and Oscillator to power down */
274 timeout = jiffies + msecs_to_jiffies(PLL_IDLE_TIME);
275 do {
276 cpu_relax();
277 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
278 if ((val & PLL_TICOPWDN) && (val & PLL_LDOPWDN))
279 break;
280 } while (!time_after(jiffies, timeout));
Roger Quadros629138d2014-03-06 16:38:43 +0200281
Roger Quadros0a0830f2015-06-02 12:10:40 +0300282 if (!(val & PLL_TICOPWDN) || !(val & PLL_LDOPWDN)) {
283 dev_err(phy->dev, "Failed to power down: PLL_STATUS 0x%x\n",
284 val);
285 return -EBUSY;
286 }
Roger Quadros629138d2014-03-06 16:38:43 +0200287 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530288
Roger Quadrosc934b362015-07-17 16:47:22 +0300289 /* i783: SATA needs control bit toggle after PLL unlock */
290 if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata")) {
291 regmap_update_bits(phy->dpll_reset_syscon, phy->dpll_reset_reg,
292 SATA_PLL_SOFT_RESET, SATA_PLL_SOFT_RESET);
293 regmap_update_bits(phy->dpll_reset_syscon, phy->dpll_reset_reg,
294 SATA_PLL_SOFT_RESET, 0);
295 }
296
Roger Quadros0a0830f2015-06-02 12:10:40 +0300297 ti_pipe3_disable_clocks(phy);
298
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530299 return 0;
300}
Axel Lin4a9e5ca2015-07-15 15:33:51 +0800301static const struct phy_ops ops = {
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530302 .init = ti_pipe3_init,
Roger Quadros629138d2014-03-06 16:38:43 +0200303 .exit = ti_pipe3_exit,
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530304 .power_on = ti_pipe3_power_on,
305 .power_off = ti_pipe3_power_off,
306 .owner = THIS_MODULE,
307};
308
Roger Quadros61f54672014-03-07 11:43:39 +0530309static const struct of_device_id ti_pipe3_id_table[];
Roger Quadros61f54672014-03-07 11:43:39 +0530310
Kishon Vijay Abraham I234738e2015-12-21 14:24:06 +0530311static int ti_pipe3_get_clk(struct ti_pipe3 *phy)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530312{
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530313 struct clk *clk;
Kishon Vijay Abraham I234738e2015-12-21 14:24:06 +0530314 struct device *dev = phy->dev;
315 struct device_node *node = dev->of_node;
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530316
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530317 phy->refclk = devm_clk_get(dev, "refclk");
Roger Quadros7f339122015-01-13 14:23:20 +0200318 if (IS_ERR(phy->refclk)) {
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530319 dev_err(dev, "unable to get refclk\n");
Roger Quadros7f339122015-01-13 14:23:20 +0200320 /* older DTBs have missing refclk in SATA PHY
321 * so don't bail out in case of SATA PHY.
322 */
323 if (!of_device_is_compatible(node, "ti,phy-pipe3-sata"))
324 return PTR_ERR(phy->refclk);
325 }
326
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530327 if (!of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530328 phy->wkupclk = devm_clk_get(dev, "wkupclk");
Roger Quadros9c7f0442014-03-06 16:38:42 +0200329 if (IS_ERR(phy->wkupclk)) {
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530330 dev_err(dev, "unable to get wkupclk\n");
Roger Quadros9c7f0442014-03-06 16:38:42 +0200331 return PTR_ERR(phy->wkupclk);
332 }
Roger Quadros9c7f0442014-03-06 16:38:42 +0200333 } else {
334 phy->wkupclk = ERR_PTR(-ENODEV);
Kishon Vijay Abraham I234738e2015-12-21 14:24:06 +0530335 }
336
337 if (!of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
338 phy->sys_clk = devm_clk_get(dev, "sysclk");
339 if (IS_ERR(phy->sys_clk)) {
340 dev_err(dev, "unable to get sysclk\n");
341 return -EINVAL;
Roger Quadrosc934b362015-07-17 16:47:22 +0300342 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530343 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530344
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530345 if (of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530346 clk = devm_clk_get(dev, "dpll_ref");
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530347 if (IS_ERR(clk)) {
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530348 dev_err(dev, "unable to get dpll ref clk\n");
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530349 return PTR_ERR(clk);
350 }
351 clk_set_rate(clk, 1500000000);
352
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530353 clk = devm_clk_get(dev, "dpll_ref_m2");
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530354 if (IS_ERR(clk)) {
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530355 dev_err(dev, "unable to get dpll ref m2 clk\n");
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530356 return PTR_ERR(clk);
357 }
358 clk_set_rate(clk, 100000000);
359
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530360 clk = devm_clk_get(dev, "phy-div");
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530361 if (IS_ERR(clk)) {
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530362 dev_err(dev, "unable to get phy-div clk\n");
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530363 return PTR_ERR(clk);
364 }
365 clk_set_rate(clk, 100000000);
366
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530367 phy->div_clk = devm_clk_get(dev, "div-clk");
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530368 if (IS_ERR(phy->div_clk)) {
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530369 dev_err(dev, "unable to get div-clk\n");
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530370 return PTR_ERR(phy->div_clk);
371 }
372 } else {
373 phy->div_clk = ERR_PTR(-ENODEV);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530374 }
375
Kishon Vijay Abraham I234738e2015-12-21 14:24:06 +0530376 return 0;
377}
378
Kishon Vijay Abraham I73bbc782015-12-21 14:24:07 +0530379static int ti_pipe3_get_sysctrl(struct ti_pipe3 *phy)
380{
381 struct device *dev = phy->dev;
382 struct device_node *node = dev->of_node;
383 struct device_node *control_node;
384 struct platform_device *control_pdev;
385
386 control_node = of_parse_phandle(node, "ctrl-module", 0);
387 if (!control_node) {
388 dev_err(dev, "Failed to get control device phandle\n");
389 return -EINVAL;
390 }
391
392 control_pdev = of_find_device_by_node(control_node);
393 if (!control_pdev) {
394 dev_err(dev, "Failed to get control device\n");
395 return -EINVAL;
396 }
397
398 phy->control_dev = &control_pdev->dev;
399
400 if (of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
401 phy->dpll_reset_syscon = syscon_regmap_lookup_by_phandle(node,
402 "syscon-pllreset");
403 if (IS_ERR(phy->dpll_reset_syscon)) {
404 dev_info(dev,
405 "can't get syscon-pllreset, sata dpll won't idle\n");
406 phy->dpll_reset_syscon = NULL;
407 } else {
408 if (of_property_read_u32_index(node,
409 "syscon-pllreset", 1,
410 &phy->dpll_reset_reg)) {
411 dev_err(dev,
412 "couldn't get pllreset reg. offset\n");
413 return -EINVAL;
414 }
415 }
416 }
417
418 return 0;
419}
420
Kishon Vijay Abraham I234738e2015-12-21 14:24:06 +0530421static int ti_pipe3_probe(struct platform_device *pdev)
422{
423 struct ti_pipe3 *phy;
424 struct phy *generic_phy;
425 struct phy_provider *phy_provider;
426 struct resource *res;
427 struct device_node *node = pdev->dev.of_node;
Kishon Vijay Abraham I234738e2015-12-21 14:24:06 +0530428 const struct of_device_id *match;
429 struct device *dev = &pdev->dev;
430 int ret;
431
432 phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
433 if (!phy)
434 return -ENOMEM;
435
436 phy->dev = dev;
437
438 if (!of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
439 match = of_match_device(ti_pipe3_id_table, dev);
440 if (!match)
441 return -EINVAL;
442
443 phy->dpll_map = (struct pipe3_dpll_map *)match->data;
444 if (!phy->dpll_map) {
445 dev_err(dev, "no DPLL data\n");
446 return -EINVAL;
447 }
448
449 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
450 "pll_ctrl");
451 phy->pll_ctrl_base = devm_ioremap_resource(dev, res);
452 if (IS_ERR(phy->pll_ctrl_base))
453 return PTR_ERR(phy->pll_ctrl_base);
454 }
455
Kishon Vijay Abraham I73bbc782015-12-21 14:24:07 +0530456 ret = ti_pipe3_get_sysctrl(phy);
457 if (ret)
458 return ret;
Kishon Vijay Abraham I234738e2015-12-21 14:24:06 +0530459
460 ret = ti_pipe3_get_clk(phy);
461 if (ret)
462 return ret;
463
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200464 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530465
466 platform_set_drvdata(pdev, phy);
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530467 pm_runtime_enable(dev);
Roger Quadrosc934b362015-07-17 16:47:22 +0300468
469 /*
470 * Prevent auto-disable of refclk for SATA PHY due to Errata i783
471 */
472 if (of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
473 if (!IS_ERR(phy->refclk)) {
Roger Quadros0a0830f2015-06-02 12:10:40 +0300474 clk_prepare_enable(phy->refclk);
Roger Quadrosc934b362015-07-17 16:47:22 +0300475 phy->sata_refclk_enabled = true;
476 }
477 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530478
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530479 generic_phy = devm_phy_create(dev, NULL, &ops);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530480 if (IS_ERR(generic_phy))
481 return PTR_ERR(generic_phy);
482
483 phy_set_drvdata(generic_phy, phy);
Kishon Vijay Abraham Id65ff522015-12-21 14:24:05 +0530484 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530485 if (IS_ERR(phy_provider))
486 return PTR_ERR(phy_provider);
487
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530488 return 0;
489}
490
491static int ti_pipe3_remove(struct platform_device *pdev)
492{
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530493 pm_runtime_disable(&pdev->dev);
494
495 return 0;
496}
497
Roger Quadros0a0830f2015-06-02 12:10:40 +0300498static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy)
Roger Quadros7f339122015-01-13 14:23:20 +0200499{
Roger Quadros0a0830f2015-06-02 12:10:40 +0300500 int ret = 0;
Roger Quadros7f339122015-01-13 14:23:20 +0200501
Roger Quadros0a0830f2015-06-02 12:10:40 +0300502 if (!IS_ERR(phy->refclk)) {
Roger Quadros7f339122015-01-13 14:23:20 +0200503 ret = clk_prepare_enable(phy->refclk);
504 if (ret) {
505 dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
506 return ret;
507 }
Roger Quadros7f339122015-01-13 14:23:20 +0200508 }
509
Roger Quadros1562864f2014-03-07 11:27:09 +0530510 if (!IS_ERR(phy->wkupclk)) {
511 ret = clk_prepare_enable(phy->wkupclk);
512 if (ret) {
513 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
Roger Quadros0a0830f2015-06-02 12:10:40 +0300514 goto disable_refclk;
Roger Quadros1562864f2014-03-07 11:27:09 +0530515 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530516 }
517
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530518 if (!IS_ERR(phy->div_clk)) {
519 ret = clk_prepare_enable(phy->div_clk);
520 if (ret) {
521 dev_err(phy->dev, "Failed to enable div_clk %d\n", ret);
Roger Quadros0a0830f2015-06-02 12:10:40 +0300522 goto disable_wkupclk;
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530523 }
524 }
Roger Quadros6e738432015-01-13 14:23:19 +0200525
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530526 return 0;
527
Roger Quadros0a0830f2015-06-02 12:10:40 +0300528disable_wkupclk:
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530529 if (!IS_ERR(phy->wkupclk))
530 clk_disable_unprepare(phy->wkupclk);
531
Roger Quadros0a0830f2015-06-02 12:10:40 +0300532disable_refclk:
Roger Quadros1562864f2014-03-07 11:27:09 +0530533 if (!IS_ERR(phy->refclk))
534 clk_disable_unprepare(phy->refclk);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530535
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530536 return ret;
537}
538
Roger Quadros6e738432015-01-13 14:23:19 +0200539static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy)
540{
Roger Quadros6e738432015-01-13 14:23:19 +0200541 if (!IS_ERR(phy->wkupclk))
542 clk_disable_unprepare(phy->wkupclk);
Roger Quadrosc934b362015-07-17 16:47:22 +0300543 if (!IS_ERR(phy->refclk)) {
Roger Quadros0a0830f2015-06-02 12:10:40 +0300544 clk_disable_unprepare(phy->refclk);
Roger Quadrosc934b362015-07-17 16:47:22 +0300545 /*
546 * SATA refclk needs an additional disable as we left it
547 * on in probe to avoid Errata i783
548 */
549 if (phy->sata_refclk_enabled) {
550 clk_disable_unprepare(phy->refclk);
551 phy->sata_refclk_enabled = false;
552 }
553 }
554
Roger Quadros6e738432015-01-13 14:23:19 +0200555 if (!IS_ERR(phy->div_clk))
556 clk_disable_unprepare(phy->div_clk);
Roger Quadros6e738432015-01-13 14:23:19 +0200557}
558
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530559static const struct of_device_id ti_pipe3_id_table[] = {
Roger Quadros61f54672014-03-07 11:43:39 +0530560 {
561 .compatible = "ti,phy-usb3",
562 .data = dpll_map_usb,
563 },
564 {
565 .compatible = "ti,omap-usb3",
566 .data = dpll_map_usb,
567 },
568 {
569 .compatible = "ti,phy-pipe3-sata",
570 .data = dpll_map_sata,
571 },
Kishon Vijay Abraham I99bbd482014-06-25 23:22:56 +0530572 {
573 .compatible = "ti,phy-pipe3-pcie",
574 },
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530575 {}
576};
577MODULE_DEVICE_TABLE(of, ti_pipe3_id_table);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530578
579static struct platform_driver ti_pipe3_driver = {
580 .probe = ti_pipe3_probe,
581 .remove = ti_pipe3_remove,
582 .driver = {
583 .name = "ti-pipe3",
Axel Lin298fe562015-03-05 18:20:53 +0800584 .of_match_table = ti_pipe3_id_table,
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530585 },
586};
587
588module_platform_driver(ti_pipe3_driver);
589
Axel Lindd64ad382015-03-07 00:01:21 +0800590MODULE_ALIAS("platform:ti_pipe3");
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530591MODULE_AUTHOR("Texas Instruments Inc.");
592MODULE_DESCRIPTION("TI PIPE3 phy driver");
593MODULE_LICENSE("GPL v2");