blob: 17abdfe53067b49c729e7d392d7e294cb5a11616 [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/*
Kuninori Morimoto258485d2011-10-10 22:01:40 -0700150 * 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
Kuninori Morimoto75587f52011-10-10 22:01:51 -0700162int 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
Kuninori Morimoto258485d2011-10-10 22:01:40 -0700178int 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
192/*
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900193 * local functions
194 */
Kuninori Morimoto11935de2011-10-10 22:01:28 -0700195static void usbhsc_set_buswait(struct usbhs_priv *priv)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900196{
197 int wait = usbhs_get_dparam(priv, buswait_bwait);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900198
Kuninori Morimoto11935de2011-10-10 22:01:28 -0700199 /* set bus wait if platform have */
200 if (wait)
201 usbhs_bset(priv, BUSWAIT, 0x000F, wait);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900202}
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/*
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900221 * 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);
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900233 } else {
234 /* USB off */
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900235 usbhs_sys_clock_ctrl(priv, enable);
236
237 /* disable PM */
238 pm_runtime_put_sync(dev);
239 }
240}
241
242/*
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700243 * hotplug
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900244 */
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700245static void usbhsc_hotplug(struct usbhs_priv *priv)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900246{
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900247 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
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900270 /* power on */
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900271 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
272 usbhsc_power_ctrl(priv, enable);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900273
Kuninori Morimoto258485d2011-10-10 22:01:40 -0700274 /* bus init */
275 usbhsc_set_buswait(priv);
276 usbhsc_bus_init(priv);
277
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900278 /* 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
Kuninori Morimoto258485d2011-10-10 22:01:40 -0700287 /* bus init */
288 usbhsc_bus_init(priv);
289
Kuninori Morimoto6e267da2011-04-28 16:41:02 +0900290 /* power off */
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900291 if (usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
292 usbhsc_power_ctrl(priv, enable);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900293
294 usbhs_mod_change(priv, -1);
295
296 /* reset phy for next connection */
297 usbhs_platform_call(priv, phy_reset, pdev);
298 }
299}
300
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700301/*
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
Kuninori Morimotobc573812011-04-28 16:41:14 +0900312int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev)
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900313{
Kuninori Morimoto206dcc22011-04-28 16:40:54 +0900314 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
Kuninori Morimotobc573812011-04-28 16:41:14 +0900315 int delay = usbhs_get_dparam(priv, detection_delay);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900316
317 /*
318 * This functions will be called in interrupt.
319 * To make sure safety context,
320 * use workqueue for usbhs_notify_hotplug
321 */
Kuninori Morimotobc573812011-04-28 16:41:14 +0900322 schedule_delayed_work(&priv->notify_hotplug_work, delay);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900323 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 ||
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900340 !info->platform_callback.get_id) {
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900341 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 }
Kuninori Morimotoe73a9892011-06-06 14:19:03 +0900382 if (!priv->dparam->pio_dma_border)
383 priv->dparam->pio_dma_border = 64; /* 64byte */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900384
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900385 /* FIXME */
386 /* runtime power control ? */
387 if (priv->pfunc->get_vbus)
388 usbhsc_flags_set(priv, USBHSF_RUNTIME_PWCTRL);
389
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900390 /*
391 * priv settings
392 */
393 priv->irq = irq;
394 priv->pdev = pdev;
Kuninori Morimotobc573812011-04-28 16:41:14 +0900395 INIT_DELAYED_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900396 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)
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900401 goto probe_end_iounmap;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900402
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900403 ret = usbhs_fifo_probe(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900404 if (ret < 0)
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900405 goto probe_end_pipe_exit;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900406
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900407 ret = usbhs_mod_probe(priv);
408 if (ret < 0)
409 goto probe_end_fifo_exit;
410
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900411 /* 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");
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900430 goto probe_end_mod_exit;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900431 }
432
433 /* reset phy for connection */
434 usbhs_platform_call(priv, phy_reset, pdev);
435
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900436 /* 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
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900443 /*
444 * manual call notify_hotplug for cold plug
445 */
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900446 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);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900456probe_end_mod_exit:
457 usbhs_mod_remove(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900458probe_end_fifo_exit:
459 usbhs_fifo_remove(priv);
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900460probe_end_pipe_exit:
461 usbhs_pipe_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900462probe_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{
Kuninori Morimoto206dcc22011-04-28 16:40:54 +0900474 struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
Kuninori Morimotoaf32fe52011-04-21 14:10:16 +0900475 struct renesas_usbhs_platform_info *info = pdev->dev.platform_data;
476 struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900477
478 dev_dbg(&pdev->dev, "usb remove\n");
479
Kuninori Morimotoaf32fe52011-04-21 14:10:16 +0900480 dfunc->notify_hotplug = NULL;
481
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900482 /* power off */
483 if (!usbhsc_flags_has(priv, USBHSF_RUNTIME_PWCTRL))
484 usbhsc_power_ctrl(priv, 0);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900485
Kuninori Morimotob002ff62011-04-28 16:41:20 +0900486 pm_runtime_disable(&pdev->dev);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900487
488 usbhs_platform_call(priv, hardware_exit, pdev);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900489 usbhs_mod_remove(priv);
Kuninori Morimotod3af90a2011-06-06 14:18:44 +0900490 usbhs_fifo_remove(priv);
Kuninori Morimoto97f93222011-05-11 16:00:15 +0900491 usbhs_pipe_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900492 iounmap(priv->base);
493 kfree(priv);
494
495 return 0;
496}
497
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700498static 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
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900548static struct platform_driver renesas_usbhs_driver = {
549 .driver = {
550 .name = "renesas_usbhs",
Kuninori Morimotoca8a2822011-10-10 21:58:19 -0700551 .pm = &usbhsc_pm_ops,
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900552 },
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>");