blob: baa3f78d62bc3f6d639529298ffda06ccbd9125b [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>
31
32#define PLL_STATUS 0x00000004
33#define PLL_GO 0x00000008
34#define PLL_CONFIGURATION1 0x0000000C
35#define PLL_CONFIGURATION2 0x00000010
36#define PLL_CONFIGURATION3 0x00000014
37#define PLL_CONFIGURATION4 0x00000020
38
39#define PLL_REGM_MASK 0x001FFE00
40#define PLL_REGM_SHIFT 0x9
41#define PLL_REGM_F_MASK 0x0003FFFF
42#define PLL_REGM_F_SHIFT 0x0
43#define PLL_REGN_MASK 0x000001FE
44#define PLL_REGN_SHIFT 0x1
45#define PLL_SELFREQDCO_MASK 0x0000000E
46#define PLL_SELFREQDCO_SHIFT 0x1
47#define PLL_SD_MASK 0x0003FC00
Roger Quadros1562864f2014-03-07 11:27:09 +053048#define PLL_SD_SHIFT 10
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053049#define SET_PLL_GO 0x1
50#define PLL_TICOPWDN 0x10000
51#define PLL_LOCK 0x2
52#define PLL_IDLE 0x1
53
54/*
55 * This is an Empirical value that works, need to confirm the actual
56 * value required for the PIPE3PHY_PLL_CONFIGURATION2.PLL_IDLE status
57 * to be correctly reflected in the PIPE3PHY_PLL_STATUS register.
58 */
59# define PLL_IDLE_TIME 100;
60
61struct pipe3_dpll_params {
62 u16 m;
63 u8 n;
64 u8 freq:3;
65 u8 sd;
66 u32 mf;
67};
68
Roger Quadros61f54672014-03-07 11:43:39 +053069struct pipe3_dpll_map {
70 unsigned long rate;
71 struct pipe3_dpll_params params;
72};
73
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053074struct ti_pipe3 {
75 void __iomem *pll_ctrl_base;
76 struct device *dev;
77 struct device *control_dev;
78 struct clk *wkupclk;
79 struct clk *sys_clk;
Roger Quadros1562864f2014-03-07 11:27:09 +053080 struct clk *refclk;
Roger Quadros61f54672014-03-07 11:43:39 +053081 struct pipe3_dpll_map *dpll_map;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053082};
83
Roger Quadros61f54672014-03-07 11:43:39 +053084static struct pipe3_dpll_map dpll_map_usb[] = {
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +053085 {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
86 {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
87 {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
88 {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
89 {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
90 {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
Roger Quadros61f54672014-03-07 11:43:39 +053091 { }, /* Terminator */
92};
93
94static struct pipe3_dpll_map dpll_map_sata[] = {
95 {12000000, {1000, 7, 4, 6, 0} }, /* 12 MHz */
96 {16800000, {714, 7, 4, 6, 0} }, /* 16.8 MHz */
97 {19200000, {625, 7, 4, 6, 0} }, /* 19.2 MHz */
98 {20000000, {600, 7, 4, 6, 0} }, /* 20 MHz */
99 {26000000, {461, 7, 4, 6, 0} }, /* 26 MHz */
100 {38400000, {312, 7, 4, 6, 0} }, /* 38.4 MHz */
101 { }, /* Terminator */
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530102};
103
104static inline u32 ti_pipe3_readl(void __iomem *addr, unsigned offset)
105{
106 return __raw_readl(addr + offset);
107}
108
109static inline void ti_pipe3_writel(void __iomem *addr, unsigned offset,
110 u32 data)
111{
112 __raw_writel(data, addr + offset);
113}
114
Roger Quadros61f54672014-03-07 11:43:39 +0530115static struct pipe3_dpll_params *ti_pipe3_get_dpll_params(struct ti_pipe3 *phy)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530116{
Roger Quadros61f54672014-03-07 11:43:39 +0530117 unsigned long rate;
118 struct pipe3_dpll_map *dpll_map = phy->dpll_map;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530119
Roger Quadros61f54672014-03-07 11:43:39 +0530120 rate = clk_get_rate(phy->sys_clk);
121
122 for (; dpll_map->rate; dpll_map++) {
123 if (rate == dpll_map->rate)
124 return &dpll_map->params;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530125 }
126
Roger Quadros61f54672014-03-07 11:43:39 +0530127 dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
128
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530129 return NULL;
130}
131
132static int ti_pipe3_power_off(struct phy *x)
133{
134 struct ti_pipe3 *phy = phy_get_drvdata(x);
135 int val;
136 int timeout = PLL_IDLE_TIME;
137
138 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
139 val |= PLL_IDLE;
140 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
141
142 do {
143 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
144 if (val & PLL_TICOPWDN)
145 break;
146 udelay(5);
147 } while (--timeout);
148
149 if (!timeout) {
150 dev_err(phy->dev, "power off failed\n");
151 return -EBUSY;
152 }
153
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200154 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530155
156 return 0;
157}
158
159static int ti_pipe3_power_on(struct phy *x)
160{
161 struct ti_pipe3 *phy = phy_get_drvdata(x);
162 int val;
163 int timeout = PLL_IDLE_TIME;
164
165 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
166 val &= ~PLL_IDLE;
167 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
168
169 do {
170 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
171 if (!(val & PLL_TICOPWDN))
172 break;
173 udelay(5);
174 } while (--timeout);
175
176 if (!timeout) {
177 dev_err(phy->dev, "power on failed\n");
178 return -EBUSY;
179 }
180
181 return 0;
182}
183
184static void ti_pipe3_dpll_relock(struct ti_pipe3 *phy)
185{
186 u32 val;
187 unsigned long timeout;
188
189 ti_pipe3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
190
191 timeout = jiffies + msecs_to_jiffies(20);
192 do {
193 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
194 if (val & PLL_LOCK)
195 break;
196 } while (!WARN_ON(time_after(jiffies, timeout)));
197}
198
199static int ti_pipe3_dpll_lock(struct ti_pipe3 *phy)
200{
201 u32 val;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530202 struct pipe3_dpll_params *dpll_params;
203
Roger Quadros61f54672014-03-07 11:43:39 +0530204 dpll_params = ti_pipe3_get_dpll_params(phy);
205 if (!dpll_params)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530206 return -EINVAL;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530207
208 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
209 val &= ~PLL_REGN_MASK;
210 val |= dpll_params->n << PLL_REGN_SHIFT;
211 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
212
213 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
214 val &= ~PLL_SELFREQDCO_MASK;
215 val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
216 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
217
218 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
219 val &= ~PLL_REGM_MASK;
220 val |= dpll_params->m << PLL_REGM_SHIFT;
221 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
222
223 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
224 val &= ~PLL_REGM_F_MASK;
225 val |= dpll_params->mf << PLL_REGM_F_SHIFT;
226 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
227
228 val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
229 val &= ~PLL_SD_MASK;
230 val |= dpll_params->sd << PLL_SD_SHIFT;
231 ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
232
233 ti_pipe3_dpll_relock(phy);
234
235 return 0;
236}
237
238static int ti_pipe3_init(struct phy *x)
239{
240 struct ti_pipe3 *phy = phy_get_drvdata(x);
241 int ret;
242
243 ret = ti_pipe3_dpll_lock(phy);
244 if (ret)
245 return ret;
246
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200247 omap_control_phy_power(phy->control_dev, 1);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530248
249 return 0;
250}
251
252static struct phy_ops ops = {
253 .init = ti_pipe3_init,
254 .power_on = ti_pipe3_power_on,
255 .power_off = ti_pipe3_power_off,
256 .owner = THIS_MODULE,
257};
258
Roger Quadros61f54672014-03-07 11:43:39 +0530259#ifdef CONFIG_OF
260static const struct of_device_id ti_pipe3_id_table[];
261#endif
262
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530263static int ti_pipe3_probe(struct platform_device *pdev)
264{
265 struct ti_pipe3 *phy;
266 struct phy *generic_phy;
267 struct phy_provider *phy_provider;
268 struct resource *res;
269 struct device_node *node = pdev->dev.of_node;
270 struct device_node *control_node;
271 struct platform_device *control_pdev;
Roger Quadros61f54672014-03-07 11:43:39 +0530272 const struct of_device_id *match;
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530273
Roger Quadros61f54672014-03-07 11:43:39 +0530274 match = of_match_device(of_match_ptr(ti_pipe3_id_table), &pdev->dev);
275 if (!match)
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530276 return -EINVAL;
277
278 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
279 if (!phy) {
280 dev_err(&pdev->dev, "unable to alloc mem for TI PIPE3 PHY\n");
281 return -ENOMEM;
282 }
283
Roger Quadros61f54672014-03-07 11:43:39 +0530284 phy->dpll_map = (struct pipe3_dpll_map *)match->data;
285 if (!phy->dpll_map) {
286 dev_err(&pdev->dev, "no DPLL data\n");
287 return -EINVAL;
288 }
289
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530290 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pll_ctrl");
291 phy->pll_ctrl_base = devm_ioremap_resource(&pdev->dev, res);
292 if (IS_ERR(phy->pll_ctrl_base))
293 return PTR_ERR(phy->pll_ctrl_base);
294
295 phy->dev = &pdev->dev;
296
Roger Quadros9c7f0442014-03-06 16:38:42 +0200297 if (!of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530298
Roger Quadros9c7f0442014-03-06 16:38:42 +0200299 phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
300 if (IS_ERR(phy->wkupclk)) {
301 dev_err(&pdev->dev, "unable to get wkupclk\n");
302 return PTR_ERR(phy->wkupclk);
303 }
304
305 phy->refclk = devm_clk_get(phy->dev, "refclk");
306 if (IS_ERR(phy->refclk)) {
307 dev_err(&pdev->dev, "unable to get refclk\n");
308 return PTR_ERR(phy->refclk);
309 }
310 } else {
311 phy->wkupclk = ERR_PTR(-ENODEV);
312 phy->refclk = ERR_PTR(-ENODEV);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530313 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530314
Roger Quadros1562864f2014-03-07 11:27:09 +0530315 phy->sys_clk = devm_clk_get(phy->dev, "sysclk");
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530316 if (IS_ERR(phy->sys_clk)) {
Roger Quadros1562864f2014-03-07 11:27:09 +0530317 dev_err(&pdev->dev, "unable to get sysclk\n");
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530318 return -EINVAL;
319 }
320
321 control_node = of_parse_phandle(node, "ctrl-module", 0);
322 if (!control_node) {
323 dev_err(&pdev->dev, "Failed to get control device phandle\n");
324 return -EINVAL;
325 }
326
327 control_pdev = of_find_device_by_node(control_node);
328 if (!control_pdev) {
329 dev_err(&pdev->dev, "Failed to get control device\n");
330 return -EINVAL;
331 }
332
333 phy->control_dev = &control_pdev->dev;
334
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +0200335 omap_control_phy_power(phy->control_dev, 0);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530336
337 platform_set_drvdata(pdev, phy);
338 pm_runtime_enable(phy->dev);
339
340 generic_phy = devm_phy_create(phy->dev, &ops, NULL);
341 if (IS_ERR(generic_phy))
342 return PTR_ERR(generic_phy);
343
344 phy_set_drvdata(generic_phy, phy);
345 phy_provider = devm_of_phy_provider_register(phy->dev,
346 of_phy_simple_xlate);
347 if (IS_ERR(phy_provider))
348 return PTR_ERR(phy_provider);
349
350 pm_runtime_get(&pdev->dev);
351
352 return 0;
353}
354
355static int ti_pipe3_remove(struct platform_device *pdev)
356{
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530357 if (!pm_runtime_suspended(&pdev->dev))
358 pm_runtime_put(&pdev->dev);
359 pm_runtime_disable(&pdev->dev);
360
361 return 0;
362}
363
364#ifdef CONFIG_PM_RUNTIME
365
366static int ti_pipe3_runtime_suspend(struct device *dev)
367{
368 struct ti_pipe3 *phy = dev_get_drvdata(dev);
369
Roger Quadros1562864f2014-03-07 11:27:09 +0530370 if (!IS_ERR(phy->wkupclk))
371 clk_disable_unprepare(phy->wkupclk);
372 if (!IS_ERR(phy->refclk))
373 clk_disable_unprepare(phy->refclk);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530374
375 return 0;
376}
377
378static int ti_pipe3_runtime_resume(struct device *dev)
379{
380 u32 ret = 0;
381 struct ti_pipe3 *phy = dev_get_drvdata(dev);
382
Roger Quadros1562864f2014-03-07 11:27:09 +0530383 if (!IS_ERR(phy->refclk)) {
384 ret = clk_prepare_enable(phy->refclk);
385 if (ret) {
386 dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
387 goto err1;
388 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530389 }
390
Roger Quadros1562864f2014-03-07 11:27:09 +0530391 if (!IS_ERR(phy->wkupclk)) {
392 ret = clk_prepare_enable(phy->wkupclk);
393 if (ret) {
394 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
395 goto err2;
396 }
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530397 }
398
399 return 0;
400
401err2:
Roger Quadros1562864f2014-03-07 11:27:09 +0530402 if (!IS_ERR(phy->refclk))
403 clk_disable_unprepare(phy->refclk);
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530404
405err1:
406 return ret;
407}
408
409static const struct dev_pm_ops ti_pipe3_pm_ops = {
410 SET_RUNTIME_PM_OPS(ti_pipe3_runtime_suspend,
411 ti_pipe3_runtime_resume, NULL)
412};
413
414#define DEV_PM_OPS (&ti_pipe3_pm_ops)
415#else
416#define DEV_PM_OPS NULL
417#endif
418
419#ifdef CONFIG_OF
420static const struct of_device_id ti_pipe3_id_table[] = {
Roger Quadros61f54672014-03-07 11:43:39 +0530421 {
422 .compatible = "ti,phy-usb3",
423 .data = dpll_map_usb,
424 },
425 {
426 .compatible = "ti,omap-usb3",
427 .data = dpll_map_usb,
428 },
429 {
430 .compatible = "ti,phy-pipe3-sata",
431 .data = dpll_map_sata,
432 },
Kishon Vijay Abraham Ia70143b2014-03-03 17:08:12 +0530433 {}
434};
435MODULE_DEVICE_TABLE(of, ti_pipe3_id_table);
436#endif
437
438static struct platform_driver ti_pipe3_driver = {
439 .probe = ti_pipe3_probe,
440 .remove = ti_pipe3_remove,
441 .driver = {
442 .name = "ti-pipe3",
443 .owner = THIS_MODULE,
444 .pm = DEV_PM_OPS,
445 .of_match_table = of_match_ptr(ti_pipe3_id_table),
446 },
447};
448
449module_platform_driver(ti_pipe3_driver);
450
451MODULE_ALIAS("platform: ti_pipe3");
452MODULE_AUTHOR("Texas Instruments Inc.");
453MODULE_DESCRIPTION("TI PIPE3 phy driver");
454MODULE_LICENSE("GPL v2");