usb: gadget: renesas_usbhs: add bus control functions
[deliverable/linux.git] / drivers / usb / renesas_usbhs / common.c
CommitLineData
f1407d5c
KM
1/*
2 * Renesas USB driver
3 *
4 * Copyright (C) 2011 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 *
16 */
17#include <linux/io.h>
18#include <linux/module.h>
19#include <linux/pm_runtime.h>
20#include <linux/slab.h>
21#include <linux/sysfs.h>
22#include "./common.h"
23
233f519d
KM
24/*
25 * image of renesas_usbhs
26 *
27 * ex) gadget case
28
29 * mod.c
30 * mod_gadget.c
31 * mod_host.c pipe.c fifo.c
32 *
33 * +-------+ +-----------+
34 * | pipe0 |------>| fifo pio |
35 * +------------+ +-------+ +-----------+
36 * | mod_gadget |=====> | pipe1 |--+
37 * +------------+ +-------+ | +-----------+
38 * | pipe2 | | +-| fifo dma0 |
39 * +------------+ +-------+ | | +-----------+
40 * | mod_host | | pipe3 |<-|--+
41 * +------------+ +-------+ | +-----------+
42 * | .... | +--->| fifo dma1 |
43 * | .... | +-----------+
44 */
45
46
b002ff6e
KM
47#define USBHSF_RUNTIME_PWCTRL (1 << 0)
48
49/* status */
50#define usbhsc_flags_init(p) do {(p)->flags = 0; } while (0)
51#define usbhsc_flags_set(p, b) ((p)->flags |= (b))
52#define usbhsc_flags_clr(p, b) ((p)->flags &= ~(b))
53#define usbhsc_flags_has(p, b) ((p)->flags & (b))
54
f1407d5c
KM
55/*
56 * platform call back
57 *
58 * renesas usb support platform callback function.
59 * Below macro call it.
60 * if platform doesn't have callback, it return 0 (no error)
61 */
62#define usbhs_platform_call(priv, func, args...)\
63 (!(priv) ? -ENODEV : \
64 !((priv)->pfunc->func) ? 0 : \
65 (priv)->pfunc->func(args))
66
67/*
68 * common functions
69 */
70u16 usbhs_read(struct usbhs_priv *priv, u32 reg)
71{
72 return ioread16(priv->base + reg);
73}
74
75void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data)
76{
77 iowrite16(data, priv->base + reg);
78}
79
80void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data)
81{
82 u16 val = usbhs_read(priv, reg);
83
84 val &= ~mask;
85 val |= data & mask;
86
87 usbhs_write(priv, reg, val);
88}
89
206dcc2c
KM
90struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
91{
92 return dev_get_drvdata(&pdev->dev);
93}
94
f1407d5c
KM
95/*
96 * syscfg functions
97 */
98void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable)
99{
100 usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0);
101}
102
103void usbhs_sys_hispeed_ctrl(struct usbhs_priv *priv, int enable)
104{
105 usbhs_bset(priv, SYSCFG, HSE, enable ? HSE : 0);
106}
107
108void usbhs_sys_usb_ctrl(struct usbhs_priv *priv, int enable)
109{
110 usbhs_bset(priv, SYSCFG, USBE, enable ? USBE : 0);
111}
112
113void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable)
114{
115 u16 mask = DCFM | DRPD | DPRPU;
116 u16 val = DCFM | DRPD;
117
118 /*
119 * if enable
120 *
121 * - select Host mode
122 * - D+ Line/D- Line Pull-down
123 */
124 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
125}
126
127void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable)
128{
129 u16 mask = DCFM | DRPD | DPRPU;
130 u16 val = DPRPU;
131
132 /*
133 * if enable
134 *
135 * - select Function mode
136 * - D+ Line Pull-up
137 */
138 usbhs_bset(priv, SYSCFG, mask, enable ? val : 0);
139}
140
141/*
142 * frame functions
143 */
144int usbhs_frame_get_num(struct usbhs_priv *priv)
145{
146 return usbhs_read(priv, FRMNUM) & FRNM_MASK;
147}
148
258485d9
KM
149/*
150 * bus/vbus functions
151 */
152void usbhs_bus_send_sof_enable(struct usbhs_priv *priv)
153{
154 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), UACT);
155}
156
157void usbhs_bus_send_reset(struct usbhs_priv *priv)
158{
159 usbhs_bset(priv, DVSTCTR, (USBRST | UACT), USBRST);
160}
161
162int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
163{
164 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
165
166 return usbhs_platform_call(priv, set_vbus, pdev, enable);
167}
168
169static void usbhsc_bus_init(struct usbhs_priv *priv)
170{
171 usbhs_write(priv, DVSTCTR, 0);
172
173 usbhs_vbus_ctrl(priv, 0);
174}
175
f1407d5c
KM
176/*
177 * local functions
178 */
11935de5 179static void usbhsc_set_buswait(struct usbhs_priv *priv)
f1407d5c
KM
180{
181 int wait = usbhs_get_dparam(priv, buswait_bwait);
f1407d5c 182
11935de5
KM
183 /* set bus wait if platform have */
184 if (wait)
185 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
f1407d5c
KM
186}
187
188/*
189 * platform default param
190 */
191static u32 usbhsc_default_pipe_type[] = {
192 USB_ENDPOINT_XFER_CONTROL,
193 USB_ENDPOINT_XFER_ISOC,
194 USB_ENDPOINT_XFER_ISOC,
195 USB_ENDPOINT_XFER_BULK,
196 USB_ENDPOINT_XFER_BULK,
197 USB_ENDPOINT_XFER_BULK,
198 USB_ENDPOINT_XFER_INT,
199 USB_ENDPOINT_XFER_INT,
200 USB_ENDPOINT_XFER_INT,
201 USB_ENDPOINT_XFER_INT,
202};
203
204/*
6e267da8
KM
205 * power control
206 */
207static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
208{
209 struct device *dev = usbhs_priv_to_dev(priv);
210
211 if (enable) {
212 /* enable PM */
213 pm_runtime_get_sync(dev);
214
215 /* USB on */
216 usbhs_sys_clock_ctrl(priv, enable);
6e267da8
KM
217 } else {
218 /* USB off */
6e267da8
KM
219 usbhs_sys_clock_ctrl(priv, enable);
220
221 /* disable PM */
222 pm_runtime_put_sync(dev);
223 }
224}
225
226/*
ca8a282a 227 * hotplug
f1407d5c 228 */
ca8a282a 229static void usbhsc_hotplug(struct usbhs_priv *priv)
f1407d5c 230{
f1407d5c
KM
231 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
232 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
233 int id;
234 int enable;
235 int ret;
236
237 /*
238 * get vbus status from platform
239 */
240 enable = usbhs_platform_call(priv, get_vbus, pdev);
241
242 /*
243 * get id from platform
244 */
245 id = usbhs_platform_call(priv, get_id, pdev);
246
247 if (enable && !mod) {
248 ret = usbhs_mod_change(priv, id);
249 if (ret < 0)
250 return;
251
252 dev_dbg(&pdev->dev, "%s enable\n", __func__);
253
6e267da8 254 /* power on */
b002ff6e
KM
255 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
256 usbhsc_power_ctrl(priv, enable);
f1407d5c 257
258485d9
KM
258 /* bus init */
259 usbhsc_set_buswait(priv);
260 usbhsc_bus_init(priv);
261
f1407d5c
KM
262 /* module start */
263 usbhs_mod_call(priv, start, priv);
264
265 } else if (!enable && mod) {
266 dev_dbg(&pdev->dev, "%s disable\n", __func__);
267
268 /* module stop */
269 usbhs_mod_call(priv, stop, priv);
270
258485d9
KM
271 /* bus init */
272 usbhsc_bus_init(priv);
273
6e267da8 274 /* power off */
b002ff6e
KM
275 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
276 usbhsc_power_ctrl(priv, enable);
f1407d5c
KM
277
278 usbhs_mod_change(priv, -1);
279
280 /* reset phy for next connection */
281 usbhs_platform_call(priv, phy_reset, pdev);
282 }
283}
284
ca8a282a
KM
285/*
286 * notify hotplug
287 */
288static void usbhsc_notify_hotplug(struct work_struct *work)
289{
290 struct usbhs_priv *priv = container_of(work,
291 struct usbhs_priv,
292 notify_hotplug_work.work);
293 usbhsc_hotplug(priv);
294}
295
bc57381e 296int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
f1407d5c 297{
206dcc2c 298 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
bc57381e 299 int delay = usbhs_get_dparam(priv, detection_delay);
f1407d5c
KM
300
301 /*
302 * This functions will be called in interrupt.
303 * To make sure safety context,
304 * use workqueue for usbhs_notify_hotplug
305 */
bc57381e 306 schedule_delayed_work(&priv->notify_hotplug_work, delay);
f1407d5c
KM
307 return 0;
308}
309
310/*
311 * platform functions
312 */
313static int __devinit usbhs_probe(struct platform_device *pdev)
314{
315 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
316 struct renesas_usbhs_driver_callback *dfunc;
317 struct usbhs_priv *priv;
318 struct resource *res;
319 unsigned int irq;
320 int ret;
321
322 /* check platform information */
323 if (!info ||
b002ff6e 324 !info->platform_callback.get_id) {
f1407d5c
KM
325 dev_err(&pdev->dev, "no platform information\n");
326 return -EINVAL;
327 }
328
329 /* platform data */
330 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
331 irq = platform_get_irq(pdev, 0);
332 if (!res || (int)irq <= 0) {
333 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
334 return -ENODEV;
335 }
336
337 /* usb private data */
338 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
339 if (!priv) {
340 dev_err(&pdev->dev, "Could not allocate priv\n");
341 return -ENOMEM;
342 }
343
344 priv->base = ioremap_nocache(res->start, resource_size(res));
345 if (!priv->base) {
346 dev_err(&pdev->dev, "ioremap error.\n");
347 ret = -ENOMEM;
348 goto probe_end_kfree;
349 }
350
351 /*
352 * care platform info
353 */
354 priv->pfunc = &info->platform_callback;
355 priv->dparam = &info->driver_param;
356
357 /* set driver callback functions for platform */
358 dfunc = &info->driver_callback;
359 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
360
361 /* set default param if platform doesn't have */
362 if (!priv->dparam->pipe_type) {
363 priv->dparam->pipe_type = usbhsc_default_pipe_type;
364 priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
365 }
e73a9891
KM
366 if (!priv->dparam->pio_dma_border)
367 priv->dparam->pio_dma_border = 64; /* 64byte */
f1407d5c 368
b002ff6e
KM
369 /* FIXME */
370 /* runtime power control ? */
371 if (priv->pfunc->get_vbus)
372 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
373
f1407d5c
KM
374 /*
375 * priv settings
376 */
377 priv->irq = irq;
378 priv->pdev = pdev;
bc57381e 379 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
f1407d5c
KM
380 spin_lock_init(usbhs_priv_to_lock(priv));
381
382 /* call pipe and module init */
383 ret = usbhs_pipe_probe(priv);
384 if (ret < 0)
97f93227 385 goto probe_end_iounmap;
f1407d5c 386
d3af90a5 387 ret = usbhs_fifo_probe(priv);
f1407d5c 388 if (ret < 0)
97f93227 389 goto probe_end_pipe_exit;
f1407d5c 390
d3af90a5
KM
391 ret = usbhs_mod_probe(priv);
392 if (ret < 0)
393 goto probe_end_fifo_exit;
394
f1407d5c
KM
395 /* dev_set_drvdata should be called after usbhs_mod_init */
396 dev_set_drvdata(&pdev->dev, priv);
397
398 /*
399 * deviece reset here because
400 * USB device might be used in boot loader.
401 */
402 usbhs_sys_clock_ctrl(priv, 0);
403
404 /*
405 * platform call
406 *
407 * USB phy setup might depend on CPU/Board.
408 * If platform has its callback functions,
409 * call it here.
410 */
411 ret = usbhs_platform_call(priv, hardware_init, pdev);
412 if (ret < 0) {
413 dev_err(&pdev->dev, "platform prove failed.\n");
97f93227 414 goto probe_end_mod_exit;
f1407d5c
KM
415 }
416
417 /* reset phy for connection */
418 usbhs_platform_call(priv, phy_reset, pdev);
419
b002ff6e
KM
420 /* power control */
421 pm_runtime_enable(&pdev->dev);
422 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
423 usbhsc_power_ctrl(priv, 1);
424 usbhs_mod_autonomy_mode(priv);
425 }
426
f1407d5c
KM
427 /*
428 * manual call notify_hotplug for cold plug
429 */
f1407d5c
KM
430 ret = usbhsc_drvcllbck_notify_hotplug(pdev);
431 if (ret < 0)
432 goto probe_end_call_remove;
433
434 dev_info(&pdev->dev, "probed\n");
435
436 return ret;
437
438probe_end_call_remove:
439 usbhs_platform_call(priv, hardware_exit, pdev);
f1407d5c
KM
440probe_end_mod_exit:
441 usbhs_mod_remove(priv);
d3af90a5
KM
442probe_end_fifo_exit:
443 usbhs_fifo_remove(priv);
97f93227
KM
444probe_end_pipe_exit:
445 usbhs_pipe_remove(priv);
f1407d5c
KM
446probe_end_iounmap:
447 iounmap(priv->base);
448probe_end_kfree:
449 kfree(priv);
450
451 dev_info(&pdev->dev, "probe failed\n");
452
453 return ret;
454}
455
456static int __devexit usbhs_remove(struct platform_device *pdev)
457{
206dcc2c 458 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
af32fe51
KM
459 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
460 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
f1407d5c
KM
461
462 dev_dbg(&pdev->dev, "usb remove\n");
463
af32fe51
KM
464 dfunc->notify_hotplug = NULL;
465
b002ff6e
KM
466 /* power off */
467 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
468 usbhsc_power_ctrl(priv, 0);
f1407d5c 469
b002ff6e 470 pm_runtime_disable(&pdev->dev);
f1407d5c
KM
471
472 usbhs_platform_call(priv, hardware_exit, pdev);
f1407d5c 473 usbhs_mod_remove(priv);
d3af90a5 474 usbhs_fifo_remove(priv);
97f93227 475 usbhs_pipe_remove(priv);
f1407d5c
KM
476 iounmap(priv->base);
477 kfree(priv);
478
479 return 0;
480}
481
ca8a282a
KM
482static int usbhsc_suspend(struct device *dev)
483{
484 struct usbhs_priv *priv = dev_get_drvdata(dev);
485 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
486
487 if (mod) {
488 usbhs_mod_call(priv, stop, priv);
489 usbhs_mod_change(priv, -1);
490 }
491
492 if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
493 usbhsc_power_ctrl(priv, 0);
494
495 return 0;
496}
497
498static int usbhsc_resume(struct device *dev)
499{
500 struct usbhs_priv *priv = dev_get_drvdata(dev);
501 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
502
503 usbhs_platform_call(priv, phy_reset, pdev);
504
505 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
506 usbhsc_power_ctrl(priv, 1);
507
508 usbhsc_hotplug(priv);
509
510 return 0;
511}
512
513static int usbhsc_runtime_nop(struct device *dev)
514{
515 /* Runtime PM callback shared between ->runtime_suspend()
516 * and ->runtime_resume(). Simply returns success.
517 *
518 * This driver re-initializes all registers after
519 * pm_runtime_get_sync() anyway so there is no need
520 * to save and restore registers here.
521 */
522 return 0;
523}
524
525static const struct dev_pm_ops usbhsc_pm_ops = {
526 .suspend = usbhsc_suspend,
527 .resume = usbhsc_resume,
528 .runtime_suspend = usbhsc_runtime_nop,
529 .runtime_resume = usbhsc_runtime_nop,
530};
531
f1407d5c
KM
532static struct platform_driver renesas_usbhs_driver = {
533 .driver = {
534 .name = "renesas_usbhs",
ca8a282a 535 .pm = &usbhsc_pm_ops,
f1407d5c
KM
536 },
537 .probe = usbhs_probe,
538 .remove = __devexit_p(usbhs_remove),
539};
540
541static int __init usbhs_init(void)
542{
543 return platform_driver_register(&renesas_usbhs_driver);
544}
545
546static void __exit usbhs_exit(void)
547{
548 platform_driver_unregister(&renesas_usbhs_driver);
549}
550
551module_init(usbhs_init);
552module_exit(usbhs_exit);
553
554MODULE_LICENSE("GPL");
555MODULE_DESCRIPTION("Renesas USB driver");
556MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
This page took 0.073991 seconds and 5 git commands to generate.