usb: gadget: renesas_usbhs: move usbhsp_type() to usbhs_pipe_type()
[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
75587f52
KM
162int usbhs_bus_get_speed(struct usbhs_priv *priv)
163{
164 u16 dvstctr = usbhs_read(priv, DVSTCTR);
165
166 switch (RHST & dvstctr) {
167 case RHST_LOW_SPEED:
168 return USB_SPEED_LOW;
169 case RHST_FULL_SPEED:
170 return USB_SPEED_FULL;
171 case RHST_HIGH_SPEED:
172 return USB_SPEED_HIGH;
173 }
174
175 return USB_SPEED_UNKNOWN;
176}
177
258485d9
KM
178int usbhs_vbus_ctrl(struct usbhs_priv *priv, int enable)
179{
180 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
181
182 return usbhs_platform_call(priv, set_vbus, pdev, enable);
183}
184
185static void usbhsc_bus_init(struct usbhs_priv *priv)
186{
187 usbhs_write(priv, DVSTCTR, 0);
188
189 usbhs_vbus_ctrl(priv, 0);
190}
191
f1407d5c
KM
192/*
193 * local functions
194 */
11935de5 195static void usbhsc_set_buswait(struct usbhs_priv *priv)
f1407d5c
KM
196{
197 int wait = usbhs_get_dparam(priv, buswait_bwait);
f1407d5c 198
11935de5
KM
199 /* set bus wait if platform have */
200 if (wait)
201 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
f1407d5c
KM
202}
203
204/*
205 * platform default param
206 */
207static u32 usbhsc_default_pipe_type[] = {
208 USB_ENDPOINT_XFER_CONTROL,
209 USB_ENDPOINT_XFER_ISOC,
210 USB_ENDPOINT_XFER_ISOC,
211 USB_ENDPOINT_XFER_BULK,
212 USB_ENDPOINT_XFER_BULK,
213 USB_ENDPOINT_XFER_BULK,
214 USB_ENDPOINT_XFER_INT,
215 USB_ENDPOINT_XFER_INT,
216 USB_ENDPOINT_XFER_INT,
217 USB_ENDPOINT_XFER_INT,
218};
219
220/*
6e267da8
KM
221 * power control
222 */
223static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
224{
225 struct device *dev = usbhs_priv_to_dev(priv);
226
227 if (enable) {
228 /* enable PM */
229 pm_runtime_get_sync(dev);
230
231 /* USB on */
232 usbhs_sys_clock_ctrl(priv, enable);
6e267da8
KM
233 } else {
234 /* USB off */
6e267da8
KM
235 usbhs_sys_clock_ctrl(priv, enable);
236
237 /* disable PM */
238 pm_runtime_put_sync(dev);
239 }
240}
241
242/*
ca8a282a 243 * hotplug
f1407d5c 244 */
ca8a282a 245static void usbhsc_hotplug(struct usbhs_priv *priv)
f1407d5c 246{
f1407d5c
KM
247 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
248 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
249 int id;
250 int enable;
251 int ret;
252
253 /*
254 * get vbus status from platform
255 */
256 enable = usbhs_platform_call(priv, get_vbus, pdev);
257
258 /*
259 * get id from platform
260 */
261 id = usbhs_platform_call(priv, get_id, pdev);
262
263 if (enable && !mod) {
264 ret = usbhs_mod_change(priv, id);
265 if (ret < 0)
266 return;
267
268 dev_dbg(&pdev->dev, "%s enable\n", __func__);
269
6e267da8 270 /* power on */
b002ff6e
KM
271 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
272 usbhsc_power_ctrl(priv, enable);
f1407d5c 273
258485d9
KM
274 /* bus init */
275 usbhsc_set_buswait(priv);
276 usbhsc_bus_init(priv);
277
f1407d5c
KM
278 /* module start */
279 usbhs_mod_call(priv, start, priv);
280
281 } else if (!enable && mod) {
282 dev_dbg(&pdev->dev, "%s disable\n", __func__);
283
284 /* module stop */
285 usbhs_mod_call(priv, stop, priv);
286
258485d9
KM
287 /* bus init */
288 usbhsc_bus_init(priv);
289
6e267da8 290 /* power off */
b002ff6e
KM
291 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
292 usbhsc_power_ctrl(priv, enable);
f1407d5c
KM
293
294 usbhs_mod_change(priv, -1);
295
296 /* reset phy for next connection */
297 usbhs_platform_call(priv, phy_reset, pdev);
298 }
299}
300
ca8a282a
KM
301/*
302 * notify hotplug
303 */
304static void usbhsc_notify_hotplug(struct work_struct *work)
305{
306 struct usbhs_priv *priv = container_of(work,
307 struct usbhs_priv,
308 notify_hotplug_work.work);
309 usbhsc_hotplug(priv);
310}
311
bc57381e 312int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
f1407d5c 313{
206dcc2c 314 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
bc57381e 315 int delay = usbhs_get_dparam(priv, detection_delay);
f1407d5c
KM
316
317 /*
318 * This functions will be called in interrupt.
319 * To make sure safety context,
320 * use workqueue for usbhs_notify_hotplug
321 */
bc57381e 322 schedule_delayed_work(&priv->notify_hotplug_work, delay);
f1407d5c
KM
323 return 0;
324}
325
326/*
327 * platform functions
328 */
329static int __devinit usbhs_probe(struct platform_device *pdev)
330{
331 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
332 struct renesas_usbhs_driver_callback *dfunc;
333 struct usbhs_priv *priv;
334 struct resource *res;
335 unsigned int irq;
336 int ret;
337
338 /* check platform information */
339 if (!info ||
b002ff6e 340 !info->platform_callback.get_id) {
f1407d5c
KM
341 dev_err(&pdev->dev, "no platform information\n");
342 return -EINVAL;
343 }
344
345 /* platform data */
346 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
347 irq = platform_get_irq(pdev, 0);
348 if (!res || (int)irq <= 0) {
349 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
350 return -ENODEV;
351 }
352
353 /* usb private data */
354 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
355 if (!priv) {
356 dev_err(&pdev->dev, "Could not allocate priv\n");
357 return -ENOMEM;
358 }
359
360 priv->base = ioremap_nocache(res->start, resource_size(res));
361 if (!priv->base) {
362 dev_err(&pdev->dev, "ioremap error.\n");
363 ret = -ENOMEM;
364 goto probe_end_kfree;
365 }
366
367 /*
368 * care platform info
369 */
370 priv->pfunc = &info->platform_callback;
371 priv->dparam = &info->driver_param;
372
373 /* set driver callback functions for platform */
374 dfunc = &info->driver_callback;
375 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
376
377 /* set default param if platform doesn't have */
378 if (!priv->dparam->pipe_type) {
379 priv->dparam->pipe_type = usbhsc_default_pipe_type;
380 priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
381 }
e73a9891
KM
382 if (!priv->dparam->pio_dma_border)
383 priv->dparam->pio_dma_border = 64; /* 64byte */
f1407d5c 384
b002ff6e
KM
385 /* FIXME */
386 /* runtime power control ? */
387 if (priv->pfunc->get_vbus)
388 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
389
f1407d5c
KM
390 /*
391 * priv settings
392 */
393 priv->irq = irq;
394 priv->pdev = pdev;
bc57381e 395 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
f1407d5c
KM
396 spin_lock_init(usbhs_priv_to_lock(priv));
397
398 /* call pipe and module init */
399 ret = usbhs_pipe_probe(priv);
400 if (ret < 0)
97f93227 401 goto probe_end_iounmap;
f1407d5c 402
d3af90a5 403 ret = usbhs_fifo_probe(priv);
f1407d5c 404 if (ret < 0)
97f93227 405 goto probe_end_pipe_exit;
f1407d5c 406
d3af90a5
KM
407 ret = usbhs_mod_probe(priv);
408 if (ret < 0)
409 goto probe_end_fifo_exit;
410
f1407d5c
KM
411 /* dev_set_drvdata should be called after usbhs_mod_init */
412 dev_set_drvdata(&pdev->dev, priv);
413
414 /*
415 * deviece reset here because
416 * USB device might be used in boot loader.
417 */
418 usbhs_sys_clock_ctrl(priv, 0);
419
420 /*
421 * platform call
422 *
423 * USB phy setup might depend on CPU/Board.
424 * If platform has its callback functions,
425 * call it here.
426 */
427 ret = usbhs_platform_call(priv, hardware_init, pdev);
428 if (ret < 0) {
429 dev_err(&pdev->dev, "platform prove failed.\n");
97f93227 430 goto probe_end_mod_exit;
f1407d5c
KM
431 }
432
433 /* reset phy for connection */
434 usbhs_platform_call(priv, phy_reset, pdev);
435
b002ff6e
KM
436 /* power control */
437 pm_runtime_enable(&pdev->dev);
438 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
439 usbhsc_power_ctrl(priv, 1);
440 usbhs_mod_autonomy_mode(priv);
441 }
442
f1407d5c
KM
443 /*
444 * manual call notify_hotplug for cold plug
445 */
f1407d5c
KM
446 ret = usbhsc_drvcllbck_notify_hotplug(pdev);
447 if (ret < 0)
448 goto probe_end_call_remove;
449
450 dev_info(&pdev->dev, "probed\n");
451
452 return ret;
453
454probe_end_call_remove:
455 usbhs_platform_call(priv, hardware_exit, pdev);
f1407d5c
KM
456probe_end_mod_exit:
457 usbhs_mod_remove(priv);
d3af90a5
KM
458probe_end_fifo_exit:
459 usbhs_fifo_remove(priv);
97f93227
KM
460probe_end_pipe_exit:
461 usbhs_pipe_remove(priv);
f1407d5c
KM
462probe_end_iounmap:
463 iounmap(priv->base);
464probe_end_kfree:
465 kfree(priv);
466
467 dev_info(&pdev->dev, "probe failed\n");
468
469 return ret;
470}
471
472static int __devexit usbhs_remove(struct platform_device *pdev)
473{
206dcc2c 474 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
af32fe51
KM
475 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
476 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
f1407d5c
KM
477
478 dev_dbg(&pdev->dev, "usb remove\n");
479
af32fe51
KM
480 dfunc->notify_hotplug = NULL;
481
b002ff6e
KM
482 /* power off */
483 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
484 usbhsc_power_ctrl(priv, 0);
f1407d5c 485
b002ff6e 486 pm_runtime_disable(&pdev->dev);
f1407d5c
KM
487
488 usbhs_platform_call(priv, hardware_exit, pdev);
f1407d5c 489 usbhs_mod_remove(priv);
d3af90a5 490 usbhs_fifo_remove(priv);
97f93227 491 usbhs_pipe_remove(priv);
f1407d5c
KM
492 iounmap(priv->base);
493 kfree(priv);
494
495 return 0;
496}
497
ca8a282a
KM
498static int usbhsc_suspend(struct device *dev)
499{
500 struct usbhs_priv *priv = dev_get_drvdata(dev);
501 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
502
503 if (mod) {
504 usbhs_mod_call(priv, stop, priv);
505 usbhs_mod_change(priv, -1);
506 }
507
508 if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
509 usbhsc_power_ctrl(priv, 0);
510
511 return 0;
512}
513
514static int usbhsc_resume(struct device *dev)
515{
516 struct usbhs_priv *priv = dev_get_drvdata(dev);
517 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
518
519 usbhs_platform_call(priv, phy_reset, pdev);
520
521 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
522 usbhsc_power_ctrl(priv, 1);
523
524 usbhsc_hotplug(priv);
525
526 return 0;
527}
528
529static int usbhsc_runtime_nop(struct device *dev)
530{
531 /* Runtime PM callback shared between ->runtime_suspend()
532 * and ->runtime_resume(). Simply returns success.
533 *
534 * This driver re-initializes all registers after
535 * pm_runtime_get_sync() anyway so there is no need
536 * to save and restore registers here.
537 */
538 return 0;
539}
540
541static const struct dev_pm_ops usbhsc_pm_ops = {
542 .suspend = usbhsc_suspend,
543 .resume = usbhsc_resume,
544 .runtime_suspend = usbhsc_runtime_nop,
545 .runtime_resume = usbhsc_runtime_nop,
546};
547
f1407d5c
KM
548static struct platform_driver renesas_usbhs_driver = {
549 .driver = {
550 .name = "renesas_usbhs",
ca8a282a 551 .pm = &usbhsc_pm_ops,
f1407d5c
KM
552 },
553 .probe = usbhs_probe,
554 .remove = __devexit_p(usbhs_remove),
555};
556
557static int __init usbhs_init(void)
558{
559 return platform_driver_register(&renesas_usbhs_driver);
560}
561
562static void __exit usbhs_exit(void)
563{
564 platform_driver_unregister(&renesas_usbhs_driver);
565}
566
567module_init(usbhs_init);
568module_exit(usbhs_exit);
569
570MODULE_LICENSE("GPL");
571MODULE_DESCRIPTION("Renesas USB driver");
572MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
This page took 0.07474 seconds and 5 git commands to generate.