Commit | Line | Data |
---|---|---|
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 | ||
24 | /* | |
25 | * platform call back | |
26 | * | |
27 | * renesas usb support platform callback function. | |
28 | * Below macro call it. | |
29 | * if platform doesn't have callback, it return 0 (no error) | |
30 | */ | |
31 | #define usbhs_platform_call(priv, func, args...)\ | |
32 | (!(priv) ? -ENODEV : \ | |
33 | !((priv)->pfunc->func) ? 0 : \ | |
34 | (priv)->pfunc->func(args)) | |
35 | ||
36 | /* | |
37 | * common functions | |
38 | */ | |
39 | u16 usbhs_read(struct usbhs_priv *priv, u32 reg) | |
40 | { | |
41 | return ioread16(priv->base + reg); | |
42 | } | |
43 | ||
44 | void usbhs_write(struct usbhs_priv *priv, u32 reg, u16 data) | |
45 | { | |
46 | iowrite16(data, priv->base + reg); | |
47 | } | |
48 | ||
49 | void usbhs_bset(struct usbhs_priv *priv, u32 reg, u16 mask, u16 data) | |
50 | { | |
51 | u16 val = usbhs_read(priv, reg); | |
52 | ||
53 | val &= ~mask; | |
54 | val |= data & mask; | |
55 | ||
56 | usbhs_write(priv, reg, val); | |
57 | } | |
58 | ||
206dcc2c KM |
59 | struct usbhs_priv *usbhs_pdev_to_priv(struct platform_device *pdev) |
60 | { | |
61 | return dev_get_drvdata(&pdev->dev); | |
62 | } | |
63 | ||
f1407d5c KM |
64 | /* |
65 | * syscfg functions | |
66 | */ | |
67 | void usbhs_sys_clock_ctrl(struct usbhs_priv *priv, int enable) | |
68 | { | |
69 | usbhs_bset(priv, SYSCFG, SCKE, enable ? SCKE : 0); | |
70 | } | |
71 | ||
72 | void usbhs_sys_hispeed_ctrl(struct usbhs_priv *priv, int enable) | |
73 | { | |
74 | usbhs_bset(priv, SYSCFG, HSE, enable ? HSE : 0); | |
75 | } | |
76 | ||
77 | void usbhs_sys_usb_ctrl(struct usbhs_priv *priv, int enable) | |
78 | { | |
79 | usbhs_bset(priv, SYSCFG, USBE, enable ? USBE : 0); | |
80 | } | |
81 | ||
82 | void usbhs_sys_host_ctrl(struct usbhs_priv *priv, int enable) | |
83 | { | |
84 | u16 mask = DCFM | DRPD | DPRPU; | |
85 | u16 val = DCFM | DRPD; | |
86 | ||
87 | /* | |
88 | * if enable | |
89 | * | |
90 | * - select Host mode | |
91 | * - D+ Line/D- Line Pull-down | |
92 | */ | |
93 | usbhs_bset(priv, SYSCFG, mask, enable ? val : 0); | |
94 | } | |
95 | ||
96 | void usbhs_sys_function_ctrl(struct usbhs_priv *priv, int enable) | |
97 | { | |
98 | u16 mask = DCFM | DRPD | DPRPU; | |
99 | u16 val = DPRPU; | |
100 | ||
101 | /* | |
102 | * if enable | |
103 | * | |
104 | * - select Function mode | |
105 | * - D+ Line Pull-up | |
106 | */ | |
107 | usbhs_bset(priv, SYSCFG, mask, enable ? val : 0); | |
108 | } | |
109 | ||
110 | /* | |
111 | * frame functions | |
112 | */ | |
113 | int usbhs_frame_get_num(struct usbhs_priv *priv) | |
114 | { | |
115 | return usbhs_read(priv, FRMNUM) & FRNM_MASK; | |
116 | } | |
117 | ||
118 | /* | |
119 | * local functions | |
120 | */ | |
f1407d5c KM |
121 | static void usbhsc_bus_ctrl(struct usbhs_priv *priv, int enable) |
122 | { | |
123 | int wait = usbhs_get_dparam(priv, buswait_bwait); | |
124 | u16 data = 0; | |
125 | ||
126 | if (enable) { | |
127 | /* set bus wait if platform have */ | |
128 | if (wait) | |
129 | usbhs_bset(priv, BUSWAIT, 0x000F, wait); | |
130 | } | |
131 | usbhs_write(priv, DVSTCTR, data); | |
132 | } | |
133 | ||
134 | /* | |
135 | * platform default param | |
136 | */ | |
137 | static u32 usbhsc_default_pipe_type[] = { | |
138 | USB_ENDPOINT_XFER_CONTROL, | |
139 | USB_ENDPOINT_XFER_ISOC, | |
140 | USB_ENDPOINT_XFER_ISOC, | |
141 | USB_ENDPOINT_XFER_BULK, | |
142 | USB_ENDPOINT_XFER_BULK, | |
143 | USB_ENDPOINT_XFER_BULK, | |
144 | USB_ENDPOINT_XFER_INT, | |
145 | USB_ENDPOINT_XFER_INT, | |
146 | USB_ENDPOINT_XFER_INT, | |
147 | USB_ENDPOINT_XFER_INT, | |
148 | }; | |
149 | ||
150 | /* | |
6e267da8 KM |
151 | * power control |
152 | */ | |
153 | static void usbhsc_power_ctrl(struct usbhs_priv *priv, int enable) | |
154 | { | |
155 | struct device *dev = usbhs_priv_to_dev(priv); | |
156 | ||
157 | if (enable) { | |
158 | /* enable PM */ | |
159 | pm_runtime_get_sync(dev); | |
160 | ||
161 | /* USB on */ | |
162 | usbhs_sys_clock_ctrl(priv, enable); | |
163 | usbhsc_bus_ctrl(priv, enable); | |
164 | } else { | |
165 | /* USB off */ | |
166 | usbhsc_bus_ctrl(priv, enable); | |
167 | usbhs_sys_clock_ctrl(priv, enable); | |
168 | ||
169 | /* disable PM */ | |
170 | pm_runtime_put_sync(dev); | |
171 | } | |
172 | } | |
173 | ||
174 | /* | |
175 | * notify hotplug | |
f1407d5c KM |
176 | */ |
177 | static void usbhsc_notify_hotplug(struct work_struct *work) | |
178 | { | |
179 | struct usbhs_priv *priv = container_of(work, | |
180 | struct usbhs_priv, | |
181 | notify_hotplug_work); | |
182 | struct platform_device *pdev = usbhs_priv_to_pdev(priv); | |
183 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); | |
184 | int id; | |
185 | int enable; | |
186 | int ret; | |
187 | ||
188 | /* | |
189 | * get vbus status from platform | |
190 | */ | |
191 | enable = usbhs_platform_call(priv, get_vbus, pdev); | |
192 | ||
193 | /* | |
194 | * get id from platform | |
195 | */ | |
196 | id = usbhs_platform_call(priv, get_id, pdev); | |
197 | ||
198 | if (enable && !mod) { | |
199 | ret = usbhs_mod_change(priv, id); | |
200 | if (ret < 0) | |
201 | return; | |
202 | ||
203 | dev_dbg(&pdev->dev, "%s enable\n", __func__); | |
204 | ||
6e267da8 KM |
205 | /* power on */ |
206 | usbhsc_power_ctrl(priv, enable); | |
f1407d5c KM |
207 | |
208 | /* module start */ | |
209 | usbhs_mod_call(priv, start, priv); | |
210 | ||
211 | } else if (!enable && mod) { | |
212 | dev_dbg(&pdev->dev, "%s disable\n", __func__); | |
213 | ||
214 | /* module stop */ | |
215 | usbhs_mod_call(priv, stop, priv); | |
216 | ||
6e267da8 KM |
217 | /* power off */ |
218 | usbhsc_power_ctrl(priv, enable); | |
f1407d5c KM |
219 | |
220 | usbhs_mod_change(priv, -1); | |
221 | ||
222 | /* reset phy for next connection */ | |
223 | usbhs_platform_call(priv, phy_reset, pdev); | |
224 | } | |
225 | } | |
226 | ||
227 | static int usbhsc_drvcllbck_notify_hotplug(struct platform_device *pdev) | |
228 | { | |
206dcc2c | 229 | struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); |
f1407d5c KM |
230 | |
231 | /* | |
232 | * This functions will be called in interrupt. | |
233 | * To make sure safety context, | |
234 | * use workqueue for usbhs_notify_hotplug | |
235 | */ | |
236 | schedule_work(&priv->notify_hotplug_work); | |
237 | return 0; | |
238 | } | |
239 | ||
240 | /* | |
241 | * platform functions | |
242 | */ | |
243 | static int __devinit usbhs_probe(struct platform_device *pdev) | |
244 | { | |
245 | struct renesas_usbhs_platform_info *info = pdev->dev.platform_data; | |
246 | struct renesas_usbhs_driver_callback *dfunc; | |
247 | struct usbhs_priv *priv; | |
248 | struct resource *res; | |
249 | unsigned int irq; | |
250 | int ret; | |
251 | ||
252 | /* check platform information */ | |
253 | if (!info || | |
254 | !info->platform_callback.get_id || | |
255 | !info->platform_callback.get_vbus) { | |
256 | dev_err(&pdev->dev, "no platform information\n"); | |
257 | return -EINVAL; | |
258 | } | |
259 | ||
260 | /* platform data */ | |
261 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
262 | irq = platform_get_irq(pdev, 0); | |
263 | if (!res || (int)irq <= 0) { | |
264 | dev_err(&pdev->dev, "Not enough Renesas USB platform resources.\n"); | |
265 | return -ENODEV; | |
266 | } | |
267 | ||
268 | /* usb private data */ | |
269 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
270 | if (!priv) { | |
271 | dev_err(&pdev->dev, "Could not allocate priv\n"); | |
272 | return -ENOMEM; | |
273 | } | |
274 | ||
275 | priv->base = ioremap_nocache(res->start, resource_size(res)); | |
276 | if (!priv->base) { | |
277 | dev_err(&pdev->dev, "ioremap error.\n"); | |
278 | ret = -ENOMEM; | |
279 | goto probe_end_kfree; | |
280 | } | |
281 | ||
282 | /* | |
283 | * care platform info | |
284 | */ | |
285 | priv->pfunc = &info->platform_callback; | |
286 | priv->dparam = &info->driver_param; | |
287 | ||
288 | /* set driver callback functions for platform */ | |
289 | dfunc = &info->driver_callback; | |
290 | dfunc->notify_hotplug = usbhsc_drvcllbck_notify_hotplug; | |
291 | ||
292 | /* set default param if platform doesn't have */ | |
293 | if (!priv->dparam->pipe_type) { | |
294 | priv->dparam->pipe_type = usbhsc_default_pipe_type; | |
295 | priv->dparam->pipe_size = ARRAY_SIZE(usbhsc_default_pipe_type); | |
296 | } | |
297 | ||
298 | /* | |
299 | * priv settings | |
300 | */ | |
301 | priv->irq = irq; | |
302 | priv->pdev = pdev; | |
303 | INIT_WORK(&priv->notify_hotplug_work, usbhsc_notify_hotplug); | |
304 | spin_lock_init(usbhs_priv_to_lock(priv)); | |
305 | ||
306 | /* call pipe and module init */ | |
307 | ret = usbhs_pipe_probe(priv); | |
308 | if (ret < 0) | |
309 | goto probe_end_mod_exit; | |
310 | ||
311 | ret = usbhs_mod_probe(priv); | |
312 | if (ret < 0) | |
313 | goto probe_end_iounmap; | |
314 | ||
315 | /* dev_set_drvdata should be called after usbhs_mod_init */ | |
316 | dev_set_drvdata(&pdev->dev, priv); | |
317 | ||
318 | /* | |
319 | * deviece reset here because | |
320 | * USB device might be used in boot loader. | |
321 | */ | |
322 | usbhs_sys_clock_ctrl(priv, 0); | |
323 | ||
324 | /* | |
325 | * platform call | |
326 | * | |
327 | * USB phy setup might depend on CPU/Board. | |
328 | * If platform has its callback functions, | |
329 | * call it here. | |
330 | */ | |
331 | ret = usbhs_platform_call(priv, hardware_init, pdev); | |
332 | if (ret < 0) { | |
333 | dev_err(&pdev->dev, "platform prove failed.\n"); | |
334 | goto probe_end_pipe_exit; | |
335 | } | |
336 | ||
337 | /* reset phy for connection */ | |
338 | usbhs_platform_call(priv, phy_reset, pdev); | |
339 | ||
340 | /* | |
341 | * manual call notify_hotplug for cold plug | |
342 | */ | |
343 | pm_runtime_enable(&pdev->dev); | |
344 | ret = usbhsc_drvcllbck_notify_hotplug(pdev); | |
345 | if (ret < 0) | |
346 | goto probe_end_call_remove; | |
347 | ||
348 | dev_info(&pdev->dev, "probed\n"); | |
349 | ||
350 | return ret; | |
351 | ||
352 | probe_end_call_remove: | |
353 | usbhs_platform_call(priv, hardware_exit, pdev); | |
354 | probe_end_pipe_exit: | |
355 | usbhs_pipe_remove(priv); | |
356 | probe_end_mod_exit: | |
357 | usbhs_mod_remove(priv); | |
358 | probe_end_iounmap: | |
359 | iounmap(priv->base); | |
360 | probe_end_kfree: | |
361 | kfree(priv); | |
362 | ||
363 | dev_info(&pdev->dev, "probe failed\n"); | |
364 | ||
365 | return ret; | |
366 | } | |
367 | ||
368 | static int __devexit usbhs_remove(struct platform_device *pdev) | |
369 | { | |
206dcc2c | 370 | struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev); |
af32fe51 KM |
371 | struct renesas_usbhs_platform_info *info = pdev->dev.platform_data; |
372 | struct renesas_usbhs_driver_callback *dfunc = &info->driver_callback; | |
f1407d5c KM |
373 | |
374 | dev_dbg(&pdev->dev, "usb remove\n"); | |
375 | ||
af32fe51 KM |
376 | dfunc->notify_hotplug = NULL; |
377 | ||
f1407d5c KM |
378 | pm_runtime_disable(&pdev->dev); |
379 | ||
380 | usbhsc_bus_ctrl(priv, 0); | |
381 | ||
382 | usbhs_platform_call(priv, hardware_exit, pdev); | |
383 | usbhs_pipe_remove(priv); | |
384 | usbhs_mod_remove(priv); | |
385 | iounmap(priv->base); | |
386 | kfree(priv); | |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
391 | static struct platform_driver renesas_usbhs_driver = { | |
392 | .driver = { | |
393 | .name = "renesas_usbhs", | |
394 | }, | |
395 | .probe = usbhs_probe, | |
396 | .remove = __devexit_p(usbhs_remove), | |
397 | }; | |
398 | ||
399 | static int __init usbhs_init(void) | |
400 | { | |
401 | return platform_driver_register(&renesas_usbhs_driver); | |
402 | } | |
403 | ||
404 | static void __exit usbhs_exit(void) | |
405 | { | |
406 | platform_driver_unregister(&renesas_usbhs_driver); | |
407 | } | |
408 | ||
409 | module_init(usbhs_init); | |
410 | module_exit(usbhs_exit); | |
411 | ||
412 | MODULE_LICENSE("GPL"); | |
413 | MODULE_DESCRIPTION("Renesas USB driver"); | |
414 | MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>"); |