blob: 319ed47153d7e39b38190872e70beff4df7f3a0c [file] [log] [blame]
Kuninori Morimotof1407d52011-04-04 13:44:59 +09001/*
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
Kuninori Morimoto233f5192011-07-07 00:23:24 -070024/*
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
Kuninori Morimotob002ff62011-04-28 16:41:20 +090047#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
Kuninori Morimotof1407d52011-04-04 13:44:59 +090055/*
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
Kuninori Morimoto206dcc22011-04-28 16:40:54 +090090struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev)
91{
92 return dev_get_drvdata(&pdev->dev);
93}
94
Kuninori Morimotof1407d52011-04-04 13:44:59 +090095/*
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
149/*
150 * local functions
151 */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900152static void usbhsc_bus_ctrl(struct usbhs_priv *priv, int enable)
153{
154 int wait = usbhs_get_dparam(priv, buswait_bwait);
155 u16 data = 0;
156
157 if (enable) {
158 /* set bus wait if platform have */
159 if (wait)
160 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
161 }
162 usbhs_write(priv, DVSTCTR, data);
163}
164
165/*
166 * platform default param
167 */
168static u32 usbhsc_default_pipe_type[] = {
169 USB_ENDPOINT_XFER_CONTROL,
170 USB_ENDPOINT_XFER_ISOC,
171 USB_ENDPOINT_XFER_ISOC,
172 USB_ENDPOINT_XFER_BULK,
173 USB_ENDPOINT_XFER_BULK,
174 USB_ENDPOINT_XFER_BULK,
175 USB_ENDPOINT_XFER_INT,
176 USB_ENDPOINT_XFER_INT,
177 USB_ENDPOINT_XFER_INT,
178 USB_ENDPOINT_XFER_INT,
179};
180
181/*
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900182 * power control
183 */
184static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable)
185{
186 struct device *dev = usbhs_priv_to_dev(priv);
187
188 if (enable) {
189 /* enable PM */
190 pm_runtime_get_sync(dev);
191
192 /* USB on */
193 usbhs_sys_clock_ctrl(priv, enable);
194 usbhsc_bus_ctrl(priv, enable);
195 } else {
196 /* USB off */
197 usbhsc_bus_ctrl(priv, enable);
198 usbhs_sys_clock_ctrl(priv, enable);
199
200 /* disable PM */
201 pm_runtime_put_sync(dev);
202 }
203}
204
205/*
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700206 * hotplug
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900207 */
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700208static void usbhsc_hotplug(struct usbhs_priv *priv)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900209{
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900210 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
211 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
212 int id;
213 int enable;
214 int ret;
215
216 /*
217 * get vbus status from platform
218 */
219 enable = usbhs_platform_call(priv, get_vbus, pdev);
220
221 /*
222 * get id from platform
223 */
224 id = usbhs_platform_call(priv, get_id, pdev);
225
226 if (enable && !mod) {
227 ret = usbhs_mod_change(priv, id);
228 if (ret < 0)
229 return;
230
231 dev_dbg(&pdev->dev, "%s enable\n", __func__);
232
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900233 /* power on */
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900234 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
235 usbhsc_power_ctrl(priv, enable);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900236
237 /* module start */
238 usbhs_mod_call(priv, start, priv);
239
240 } else if (!enable && mod) {
241 dev_dbg(&pdev->dev, "%s disable\n", __func__);
242
243 /* module stop */
244 usbhs_mod_call(priv, stop, priv);
245
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900246 /* power off */
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900247 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
248 usbhsc_power_ctrl(priv, enable);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900249
250 usbhs_mod_change(priv, -1);
251
252 /* reset phy for next connection */
253 usbhs_platform_call(priv, phy_reset, pdev);
254 }
255}
256
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700257/*
258 * notify hotplug
259 */
260static void usbhsc_notify_hotplug(struct work_struct *work)
261{
262 struct usbhs_priv *priv = container_of(work,
263 struct usbhs_priv,
264 notify_hotplug_work.work);
265 usbhsc_hotplug(priv);
266}
267
Kuninori Morimotobc573812011-04-28 16:41:14 +0900268int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900269{
Kuninori Morimoto206dcc22011-04-28 16:40:54 +0900270 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
Kuninori Morimotobc573812011-04-28 16:41:14 +0900271 int delay = usbhs_get_dparam(priv, detection_delay);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900272
273 /*
274 * This functions will be called in interrupt.
275 * To make sure safety context,
276 * use workqueue for usbhs_notify_hotplug
277 */
Kuninori Morimotobc573812011-04-28 16:41:14 +0900278 schedule_delayed_work(&priv->notify_hotplug_work, delay);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900279 return 0;
280}
281
282/*
283 * platform functions
284 */
285static int __devinit usbhs_probe(struct platform_device *pdev)
286{
287 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
288 struct renesas_usbhs_driver_callback *dfunc;
289 struct usbhs_priv *priv;
290 struct resource *res;
291 unsigned int irq;
292 int ret;
293
294 /* check platform information */
295 if (!info ||
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900296 !info->platform_callback.get_id) {
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900297 dev_err(&pdev->dev, "no platform information\n");
298 return -EINVAL;
299 }
300
301 /* platform data */
302 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
303 irq = platform_get_irq(pdev, 0);
304 if (!res || (int)irq <= 0) {
305 dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n");
306 return -ENODEV;
307 }
308
309 /* usb private data */
310 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
311 if (!priv) {
312 dev_err(&pdev->dev, "Could not allocate priv\n");
313 return -ENOMEM;
314 }
315
316 priv->base = ioremap_nocache(res->start, resource_size(res));
317 if (!priv->base) {
318 dev_err(&pdev->dev, "ioremap error.\n");
319 ret = -ENOMEM;
320 goto probe_end_kfree;
321 }
322
323 /*
324 * care platform info
325 */
326 priv->pfunc = &info->platform_callback;
327 priv->dparam = &info->driver_param;
328
329 /* set driver callback functions for platform */
330 dfunc = &info->driver_callback;
331 dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug;
332
333 /* set default param if platform doesn't have */
334 if (!priv->dparam->pipe_type) {
335 priv->dparam->pipe_type = usbhsc_default_pipe_type;
336 priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type);
337 }
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900338 if (!priv->dparam->pio_dma_border)
339 priv->dparam->pio_dma_border = 64; /* 64byte */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900340
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900341 /* FIXME */
342 /* runtime power control ? */
343 if (priv->pfunc->get_vbus)
344 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
345
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900346 /*
347 * priv settings
348 */
349 priv->irq = irq;
350 priv->pdev = pdev;
Kuninori Morimotobc573812011-04-28 16:41:14 +0900351 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900352 spin_lock_init(usbhs_priv_to_lock(priv));
353
354 /* call pipe and module init */
355 ret = usbhs_pipe_probe(priv);
356 if (ret < 0)
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900357 goto probe_end_iounmap;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900358
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900359 ret = usbhs_fifo_probe(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900360 if (ret < 0)
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900361 goto probe_end_pipe_exit;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900362
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900363 ret = usbhs_mod_probe(priv);
364 if (ret < 0)
365 goto probe_end_fifo_exit;
366
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900367 /* dev_set_drvdata should be called after usbhs_mod_init */
368 dev_set_drvdata(&pdev->dev, priv);
369
370 /*
371 * deviece reset here because
372 * USB device might be used in boot loader.
373 */
374 usbhs_sys_clock_ctrl(priv, 0);
375
376 /*
377 * platform call
378 *
379 * USB phy setup might depend on CPU/Board.
380 * If platform has its callback functions,
381 * call it here.
382 */
383 ret = usbhs_platform_call(priv, hardware_init, pdev);
384 if (ret < 0) {
385 dev_err(&pdev->dev, "platform prove failed.\n");
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900386 goto probe_end_mod_exit;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900387 }
388
389 /* reset phy for connection */
390 usbhs_platform_call(priv, phy_reset, pdev);
391
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900392 /* power control */
393 pm_runtime_enable(&pdev->dev);
394 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL)) {
395 usbhsc_power_ctrl(priv, 1);
396 usbhs_mod_autonomy_mode(priv);
397 }
398
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900399 /*
400 * manual call notify_hotplug for cold plug
401 */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900402 ret = usbhsc_drvcllbck_notify_hotplug(pdev);
403 if (ret < 0)
404 goto probe_end_call_remove;
405
406 dev_info(&pdev->dev, "probed\n");
407
408 return ret;
409
410probe_end_call_remove:
411 usbhs_platform_call(priv, hardware_exit, pdev);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900412probe_end_mod_exit:
413 usbhs_mod_remove(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900414probe_end_fifo_exit:
415 usbhs_fifo_remove(priv);
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900416probe_end_pipe_exit:
417 usbhs_pipe_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900418probe_end_iounmap:
419 iounmap(priv->base);
420probe_end_kfree:
421 kfree(priv);
422
423 dev_info(&pdev->dev, "probe failed\n");
424
425 return ret;
426}
427
428static int __devexit usbhs_remove(struct platform_device *pdev)
429{
Kuninori Morimoto206dcc22011-04-28 16:40:54 +0900430 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
Kuninori Morimotoaf32fe52011-04-21 14:10:16 +0900431 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
432 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900433
434 dev_dbg(&pdev->dev, "usb remove\n");
435
Kuninori Morimotoaf32fe52011-04-21 14:10:16 +0900436 dfunc->notify_hotplug = NULL;
437
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900438 /* power off */
439 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
440 usbhsc_power_ctrl(priv, 0);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900441
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900442 pm_runtime_disable(&pdev->dev);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900443
444 usbhs_platform_call(priv, hardware_exit, pdev);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900445 usbhs_mod_remove(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900446 usbhs_fifo_remove(priv);
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900447 usbhs_pipe_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900448 iounmap(priv->base);
449 kfree(priv);
450
451 return 0;
452}
453
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700454static int usbhsc_suspend(struct device *dev)
455{
456 struct usbhs_priv *priv = dev_get_drvdata(dev);
457 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
458
459 if (mod) {
460 usbhs_mod_call(priv, stop, priv);
461 usbhs_mod_change(priv, -1);
462 }
463
464 if (mod || !usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
465 usbhsc_power_ctrl(priv, 0);
466
467 return 0;
468}
469
470static int usbhsc_resume(struct device *dev)
471{
472 struct usbhs_priv *priv = dev_get_drvdata(dev);
473 struct platform_device *pdev = usbhs_priv_to_pdev(priv);
474
475 usbhs_platform_call(priv, phy_reset, pdev);
476
477 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
478 usbhsc_power_ctrl(priv, 1);
479
480 usbhsc_hotplug(priv);
481
482 return 0;
483}
484
485static int usbhsc_runtime_nop(struct device *dev)
486{
487 /* Runtime PM callback shared between ->runtime_suspend()
488 * and ->runtime_resume(). Simply returns success.
489 *
490 * This driver re-initializes all registers after
491 * pm_runtime_get_sync() anyway so there is no need
492 * to save and restore registers here.
493 */
494 return 0;
495}
496
497static const struct dev_pm_ops usbhsc_pm_ops = {
498 .suspend = usbhsc_suspend,
499 .resume = usbhsc_resume,
500 .runtime_suspend = usbhsc_runtime_nop,
501 .runtime_resume = usbhsc_runtime_nop,
502};
503
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900504static struct platform_driver renesas_usbhs_driver = {
505 .driver = {
506 .name = "renesas_usbhs",
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700507 .pm = &usbhsc_pm_ops,
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900508 },
509 .probe = usbhs_probe,
510 .remove = __devexit_p(usbhs_remove),
511};
512
513static int __init usbhs_init(void)
514{
515 return platform_driver_register(&renesas_usbhs_driver);
516}
517
518static void __exit usbhs_exit(void)
519{
520 platform_driver_unregister(&renesas_usbhs_driver);
521}
522
523module_init(usbhs_init);
524module_exit(usbhs_exit);
525
526MODULE_LICENSE("GPL");
527MODULE_DESCRIPTION("Renesas USB driver");
528MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");