Commit | Line | Data |
---|---|---|
e443b333 AS |
1 | /* |
2 | * core.c - ChipIdea USB IP core family device controller | |
3 | * | |
4 | * Copyright (C) 2008 Chipidea - MIPS Technologies, Inc. All rights reserved. | |
5 | * | |
6 | * Author: David Lopo | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
11 | */ | |
12 | ||
13 | /* | |
14 | * Description: ChipIdea USB IP core family device controller | |
15 | * | |
16 | * This driver is composed of several blocks: | |
17 | * - HW: hardware interface | |
18 | * - DBG: debug facilities (optional) | |
19 | * - UTIL: utilities | |
20 | * - ISR: interrupts handling | |
21 | * - ENDPT: endpoint operations (Gadget API) | |
22 | * - GADGET: gadget operations (Gadget API) | |
23 | * - BUS: bus glue code, bus abstraction layer | |
24 | * | |
25 | * Compile Options | |
26 | * - CONFIG_USB_GADGET_DEBUG_FILES: enable debug facilities | |
27 | * - STALL_IN: non-empty bulk-in pipes cannot be halted | |
28 | * if defined mass storage compliance succeeds but with warnings | |
29 | * => case 4: Hi > Dn | |
30 | * => case 5: Hi > Di | |
31 | * => case 8: Hi <> Do | |
32 | * if undefined usbtest 13 fails | |
33 | * - TRACE: enable function tracing (depends on DEBUG) | |
34 | * | |
35 | * Main Features | |
36 | * - Chapter 9 & Mass Storage Compliance with Gadget File Storage | |
37 | * - Chapter 9 Compliance with Gadget Zero (STALL_IN undefined) | |
38 | * - Normal & LPM support | |
39 | * | |
40 | * USBTEST Report | |
41 | * - OK: 0-12, 13 (STALL_IN defined) & 14 | |
42 | * - Not Supported: 15 & 16 (ISO) | |
43 | * | |
44 | * TODO List | |
45 | * - OTG | |
e4ce4ecd | 46 | * - Interrupt Traffic |
e443b333 AS |
47 | * - GET_STATUS(device) - always reports 0 |
48 | * - Gadget API (majority of optional features) | |
49 | * - Suspend & Remote Wakeup | |
50 | */ | |
51 | #include <linux/delay.h> | |
52 | #include <linux/device.h> | |
e443b333 | 53 | #include <linux/dma-mapping.h> |
e443b333 AS |
54 | #include <linux/platform_device.h> |
55 | #include <linux/module.h> | |
fe6e125e | 56 | #include <linux/idr.h> |
e443b333 AS |
57 | #include <linux/interrupt.h> |
58 | #include <linux/io.h> | |
e443b333 AS |
59 | #include <linux/kernel.h> |
60 | #include <linux/slab.h> | |
61 | #include <linux/pm_runtime.h> | |
62 | #include <linux/usb/ch9.h> | |
63 | #include <linux/usb/gadget.h> | |
64 | #include <linux/usb/otg.h> | |
65 | #include <linux/usb/chipidea.h> | |
40dcd0e8 MG |
66 | #include <linux/usb/of.h> |
67 | #include <linux/phy.h> | |
1542d9c3 | 68 | #include <linux/regulator/consumer.h> |
e443b333 AS |
69 | |
70 | #include "ci.h" | |
71 | #include "udc.h" | |
72 | #include "bits.h" | |
eb70e5ab | 73 | #include "host.h" |
e443b333 AS |
74 | #include "debug.h" |
75 | ||
5f36e231 | 76 | /* Controller register map */ |
e443b333 AS |
77 | static uintptr_t ci_regs_nolpm[] = { |
78 | [CAP_CAPLENGTH] = 0x000UL, | |
79 | [CAP_HCCPARAMS] = 0x008UL, | |
80 | [CAP_DCCPARAMS] = 0x024UL, | |
81 | [CAP_TESTMODE] = 0x038UL, | |
82 | [OP_USBCMD] = 0x000UL, | |
83 | [OP_USBSTS] = 0x004UL, | |
84 | [OP_USBINTR] = 0x008UL, | |
85 | [OP_DEVICEADDR] = 0x014UL, | |
86 | [OP_ENDPTLISTADDR] = 0x018UL, | |
87 | [OP_PORTSC] = 0x044UL, | |
88 | [OP_DEVLC] = 0x084UL, | |
5f36e231 | 89 | [OP_OTGSC] = 0x064UL, |
e443b333 AS |
90 | [OP_USBMODE] = 0x068UL, |
91 | [OP_ENDPTSETUPSTAT] = 0x06CUL, | |
92 | [OP_ENDPTPRIME] = 0x070UL, | |
93 | [OP_ENDPTFLUSH] = 0x074UL, | |
94 | [OP_ENDPTSTAT] = 0x078UL, | |
95 | [OP_ENDPTCOMPLETE] = 0x07CUL, | |
96 | [OP_ENDPTCTRL] = 0x080UL, | |
97 | }; | |
98 | ||
99 | static uintptr_t ci_regs_lpm[] = { | |
100 | [CAP_CAPLENGTH] = 0x000UL, | |
101 | [CAP_HCCPARAMS] = 0x008UL, | |
102 | [CAP_DCCPARAMS] = 0x024UL, | |
103 | [CAP_TESTMODE] = 0x0FCUL, | |
104 | [OP_USBCMD] = 0x000UL, | |
105 | [OP_USBSTS] = 0x004UL, | |
106 | [OP_USBINTR] = 0x008UL, | |
107 | [OP_DEVICEADDR] = 0x014UL, | |
108 | [OP_ENDPTLISTADDR] = 0x018UL, | |
109 | [OP_PORTSC] = 0x044UL, | |
110 | [OP_DEVLC] = 0x084UL, | |
5f36e231 | 111 | [OP_OTGSC] = 0x0C4UL, |
e443b333 AS |
112 | [OP_USBMODE] = 0x0C8UL, |
113 | [OP_ENDPTSETUPSTAT] = 0x0D8UL, | |
114 | [OP_ENDPTPRIME] = 0x0DCUL, | |
115 | [OP_ENDPTFLUSH] = 0x0E0UL, | |
116 | [OP_ENDPTSTAT] = 0x0E4UL, | |
117 | [OP_ENDPTCOMPLETE] = 0x0E8UL, | |
118 | [OP_ENDPTCTRL] = 0x0ECUL, | |
119 | }; | |
120 | ||
8e22978c | 121 | static int hw_alloc_regmap(struct ci_hdrc *ci, bool is_lpm) |
e443b333 AS |
122 | { |
123 | int i; | |
124 | ||
5f36e231 | 125 | kfree(ci->hw_bank.regmap); |
e443b333 | 126 | |
5f36e231 AS |
127 | ci->hw_bank.regmap = kzalloc((OP_LAST + 1) * sizeof(void *), |
128 | GFP_KERNEL); | |
129 | if (!ci->hw_bank.regmap) | |
e443b333 AS |
130 | return -ENOMEM; |
131 | ||
132 | for (i = 0; i < OP_ENDPTCTRL; i++) | |
5f36e231 AS |
133 | ci->hw_bank.regmap[i] = |
134 | (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) + | |
e443b333 AS |
135 | (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]); |
136 | ||
137 | for (; i <= OP_LAST; i++) | |
5f36e231 | 138 | ci->hw_bank.regmap[i] = ci->hw_bank.op + |
e443b333 AS |
139 | 4 * (i - OP_ENDPTCTRL) + |
140 | (is_lpm | |
141 | ? ci_regs_lpm[OP_ENDPTCTRL] | |
142 | : ci_regs_nolpm[OP_ENDPTCTRL]); | |
143 | ||
144 | return 0; | |
145 | } | |
146 | ||
147 | /** | |
148 | * hw_port_test_set: writes port test mode (execute without interruption) | |
149 | * @mode: new value | |
150 | * | |
151 | * This function returns an error code | |
152 | */ | |
8e22978c | 153 | int hw_port_test_set(struct ci_hdrc *ci, u8 mode) |
e443b333 AS |
154 | { |
155 | const u8 TEST_MODE_MAX = 7; | |
156 | ||
157 | if (mode > TEST_MODE_MAX) | |
158 | return -EINVAL; | |
159 | ||
727b4ddb | 160 | hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << __ffs(PORTSC_PTC)); |
e443b333 AS |
161 | return 0; |
162 | } | |
163 | ||
164 | /** | |
165 | * hw_port_test_get: reads port test mode value | |
166 | * | |
167 | * This function returns port test mode value | |
168 | */ | |
8e22978c | 169 | u8 hw_port_test_get(struct ci_hdrc *ci) |
e443b333 | 170 | { |
727b4ddb | 171 | return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> __ffs(PORTSC_PTC); |
e443b333 AS |
172 | } |
173 | ||
8e22978c | 174 | static int hw_device_init(struct ci_hdrc *ci, void __iomem *base) |
e443b333 AS |
175 | { |
176 | u32 reg; | |
177 | ||
178 | /* bank is a module variable */ | |
5f36e231 | 179 | ci->hw_bank.abs = base; |
e443b333 | 180 | |
5f36e231 | 181 | ci->hw_bank.cap = ci->hw_bank.abs; |
77c4400f | 182 | ci->hw_bank.cap += ci->platdata->capoffset; |
938d323f | 183 | ci->hw_bank.op = ci->hw_bank.cap + (ioread32(ci->hw_bank.cap) & 0xff); |
e443b333 | 184 | |
5f36e231 AS |
185 | hw_alloc_regmap(ci, false); |
186 | reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >> | |
727b4ddb | 187 | __ffs(HCCPARAMS_LEN); |
5f36e231 AS |
188 | ci->hw_bank.lpm = reg; |
189 | hw_alloc_regmap(ci, !!reg); | |
190 | ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs; | |
191 | ci->hw_bank.size += OP_LAST; | |
192 | ci->hw_bank.size /= sizeof(u32); | |
e443b333 | 193 | |
5f36e231 | 194 | reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >> |
727b4ddb | 195 | __ffs(DCCPARAMS_DEN); |
5f36e231 | 196 | ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */ |
e443b333 | 197 | |
09c94e62 | 198 | if (ci->hw_ep_max > ENDPT_MAX) |
e443b333 AS |
199 | return -ENODEV; |
200 | ||
5f36e231 AS |
201 | dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n", |
202 | ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op); | |
e443b333 AS |
203 | |
204 | /* setup lock mode ? */ | |
205 | ||
206 | /* ENDPTSETUPSTAT is '0' by default */ | |
207 | ||
208 | /* HCSPARAMS.bf.ppc SHOULD BE zero for device */ | |
209 | ||
210 | return 0; | |
211 | } | |
212 | ||
8e22978c | 213 | static void hw_phymode_configure(struct ci_hdrc *ci) |
40dcd0e8 MG |
214 | { |
215 | u32 portsc, lpm, sts; | |
216 | ||
217 | switch (ci->platdata->phy_mode) { | |
218 | case USBPHY_INTERFACE_MODE_UTMI: | |
219 | portsc = PORTSC_PTS(PTS_UTMI); | |
220 | lpm = DEVLC_PTS(PTS_UTMI); | |
221 | break; | |
222 | case USBPHY_INTERFACE_MODE_UTMIW: | |
223 | portsc = PORTSC_PTS(PTS_UTMI) | PORTSC_PTW; | |
224 | lpm = DEVLC_PTS(PTS_UTMI) | DEVLC_PTW; | |
225 | break; | |
226 | case USBPHY_INTERFACE_MODE_ULPI: | |
227 | portsc = PORTSC_PTS(PTS_ULPI); | |
228 | lpm = DEVLC_PTS(PTS_ULPI); | |
229 | break; | |
230 | case USBPHY_INTERFACE_MODE_SERIAL: | |
231 | portsc = PORTSC_PTS(PTS_SERIAL); | |
232 | lpm = DEVLC_PTS(PTS_SERIAL); | |
233 | sts = 1; | |
234 | break; | |
235 | case USBPHY_INTERFACE_MODE_HSIC: | |
236 | portsc = PORTSC_PTS(PTS_HSIC); | |
237 | lpm = DEVLC_PTS(PTS_HSIC); | |
238 | break; | |
239 | default: | |
240 | return; | |
241 | } | |
242 | ||
243 | if (ci->hw_bank.lpm) { | |
244 | hw_write(ci, OP_DEVLC, DEVLC_PTS(7) | DEVLC_PTW, lpm); | |
245 | hw_write(ci, OP_DEVLC, DEVLC_STS, sts); | |
246 | } else { | |
247 | hw_write(ci, OP_PORTSC, PORTSC_PTS(7) | PORTSC_PTW, portsc); | |
248 | hw_write(ci, OP_PORTSC, PORTSC_STS, sts); | |
249 | } | |
250 | } | |
251 | ||
e443b333 AS |
252 | /** |
253 | * hw_device_reset: resets chip (execute without interruption) | |
254 | * @ci: the controller | |
255 | * | |
256 | * This function returns an error code | |
257 | */ | |
8e22978c | 258 | int hw_device_reset(struct ci_hdrc *ci, u32 mode) |
e443b333 AS |
259 | { |
260 | /* should flush & stop before reset */ | |
261 | hw_write(ci, OP_ENDPTFLUSH, ~0, ~0); | |
262 | hw_write(ci, OP_USBCMD, USBCMD_RS, 0); | |
263 | ||
264 | hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST); | |
265 | while (hw_read(ci, OP_USBCMD, USBCMD_RST)) | |
266 | udelay(10); /* not RTOS friendly */ | |
267 | ||
77c4400f RZ |
268 | if (ci->platdata->notify_event) |
269 | ci->platdata->notify_event(ci, | |
8e22978c | 270 | CI_HDRC_CONTROLLER_RESET_EVENT); |
e443b333 | 271 | |
8e22978c | 272 | if (ci->platdata->flags & CI_HDRC_DISABLE_STREAMING) |
758fc986 | 273 | hw_write(ci, OP_USBMODE, USBMODE_CI_SDIS, USBMODE_CI_SDIS); |
e443b333 AS |
274 | |
275 | /* USBMODE should be configured step by step */ | |
276 | hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE); | |
eb70e5ab | 277 | hw_write(ci, OP_USBMODE, USBMODE_CM, mode); |
e443b333 AS |
278 | /* HW >= 2.3 */ |
279 | hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); | |
280 | ||
eb70e5ab AS |
281 | if (hw_read(ci, OP_USBMODE, USBMODE_CM) != mode) { |
282 | pr_err("cannot enter in %s mode", ci_role(ci)->name); | |
e443b333 AS |
283 | pr_err("lpm = %i", ci->hw_bank.lpm); |
284 | return -ENODEV; | |
285 | } | |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
5f36e231 AS |
290 | /** |
291 | * ci_otg_role - pick role based on ID pin state | |
292 | * @ci: the controller | |
293 | */ | |
8e22978c | 294 | static enum ci_role ci_otg_role(struct ci_hdrc *ci) |
5f36e231 AS |
295 | { |
296 | u32 sts = hw_read(ci, OP_OTGSC, ~0); | |
297 | enum ci_role role = sts & OTGSC_ID | |
298 | ? CI_ROLE_GADGET | |
299 | : CI_ROLE_HOST; | |
300 | ||
301 | return role; | |
302 | } | |
303 | ||
304 | /** | |
305 | * ci_role_work - perform role changing based on ID pin | |
306 | * @work: work struct | |
307 | */ | |
308 | static void ci_role_work(struct work_struct *work) | |
309 | { | |
8e22978c | 310 | struct ci_hdrc *ci = container_of(work, struct ci_hdrc, work); |
5f36e231 AS |
311 | enum ci_role role = ci_otg_role(ci); |
312 | ||
5f36e231 AS |
313 | if (role != ci->role) { |
314 | dev_dbg(ci->dev, "switching from %s to %s\n", | |
315 | ci_role(ci)->name, ci->roles[role]->name); | |
316 | ||
317 | ci_role_stop(ci); | |
318 | ci_role_start(ci, role); | |
319 | } | |
0c3f3dc6 AS |
320 | |
321 | enable_irq(ci->irq); | |
5f36e231 AS |
322 | } |
323 | ||
5f36e231 AS |
324 | static irqreturn_t ci_irq(int irq, void *data) |
325 | { | |
8e22978c | 326 | struct ci_hdrc *ci = data; |
5f36e231 | 327 | irqreturn_t ret = IRQ_NONE; |
b183c19f | 328 | u32 otgsc = 0; |
5f36e231 | 329 | |
b183c19f RZ |
330 | if (ci->is_otg) |
331 | otgsc = hw_read(ci, OP_OTGSC, ~0); | |
5f36e231 | 332 | |
b183c19f RZ |
333 | if (ci->role != CI_ROLE_END) |
334 | ret = ci_role(ci)->irq(ci); | |
335 | ||
336 | if (ci->is_otg && (otgsc & OTGSC_IDIS)) { | |
337 | hw_write(ci, OP_OTGSC, OTGSC_IDIS, OTGSC_IDIS); | |
338 | disable_irq_nosync(ci->irq); | |
339 | queue_work(ci->wq, &ci->work); | |
340 | ret = IRQ_HANDLED; | |
5f36e231 AS |
341 | } |
342 | ||
b183c19f | 343 | return ret; |
5f36e231 AS |
344 | } |
345 | ||
1542d9c3 PC |
346 | static int ci_get_platdata(struct device *dev, |
347 | struct ci_hdrc_platform_data *platdata) | |
348 | { | |
349 | /* Get the vbus regulator */ | |
350 | platdata->reg_vbus = devm_regulator_get(dev, "vbus"); | |
351 | if (PTR_ERR(platdata->reg_vbus) == -EPROBE_DEFER) { | |
352 | return -EPROBE_DEFER; | |
353 | } else if (PTR_ERR(platdata->reg_vbus) == -ENODEV) { | |
354 | platdata->reg_vbus = NULL; /* no vbus regualator is needed */ | |
355 | } else if (IS_ERR(platdata->reg_vbus)) { | |
356 | dev_err(dev, "Getting regulator error: %ld\n", | |
357 | PTR_ERR(platdata->reg_vbus)); | |
358 | return PTR_ERR(platdata->reg_vbus); | |
359 | } | |
360 | ||
361 | return 0; | |
362 | } | |
363 | ||
fe6e125e RZ |
364 | static DEFINE_IDA(ci_ida); |
365 | ||
8e22978c | 366 | struct platform_device *ci_hdrc_add_device(struct device *dev, |
cbc6dc2a | 367 | struct resource *res, int nres, |
8e22978c | 368 | struct ci_hdrc_platform_data *platdata) |
cbc6dc2a RZ |
369 | { |
370 | struct platform_device *pdev; | |
fe6e125e | 371 | int id, ret; |
cbc6dc2a | 372 | |
1542d9c3 PC |
373 | ret = ci_get_platdata(dev, platdata); |
374 | if (ret) | |
375 | return ERR_PTR(ret); | |
376 | ||
fe6e125e RZ |
377 | id = ida_simple_get(&ci_ida, 0, 0, GFP_KERNEL); |
378 | if (id < 0) | |
379 | return ERR_PTR(id); | |
380 | ||
381 | pdev = platform_device_alloc("ci_hdrc", id); | |
382 | if (!pdev) { | |
383 | ret = -ENOMEM; | |
384 | goto put_id; | |
385 | } | |
cbc6dc2a RZ |
386 | |
387 | pdev->dev.parent = dev; | |
388 | pdev->dev.dma_mask = dev->dma_mask; | |
389 | pdev->dev.dma_parms = dev->dma_parms; | |
390 | dma_set_coherent_mask(&pdev->dev, dev->coherent_dma_mask); | |
391 | ||
392 | ret = platform_device_add_resources(pdev, res, nres); | |
393 | if (ret) | |
394 | goto err; | |
395 | ||
396 | ret = platform_device_add_data(pdev, platdata, sizeof(*platdata)); | |
397 | if (ret) | |
398 | goto err; | |
399 | ||
400 | ret = platform_device_add(pdev); | |
401 | if (ret) | |
402 | goto err; | |
403 | ||
404 | return pdev; | |
405 | ||
406 | err: | |
407 | platform_device_put(pdev); | |
fe6e125e RZ |
408 | put_id: |
409 | ida_simple_remove(&ci_ida, id); | |
cbc6dc2a RZ |
410 | return ERR_PTR(ret); |
411 | } | |
8e22978c | 412 | EXPORT_SYMBOL_GPL(ci_hdrc_add_device); |
cbc6dc2a | 413 | |
8e22978c | 414 | void ci_hdrc_remove_device(struct platform_device *pdev) |
cbc6dc2a | 415 | { |
98c35534 | 416 | int id = pdev->id; |
cbc6dc2a | 417 | platform_device_unregister(pdev); |
98c35534 | 418 | ida_simple_remove(&ci_ida, id); |
cbc6dc2a | 419 | } |
8e22978c | 420 | EXPORT_SYMBOL_GPL(ci_hdrc_remove_device); |
cbc6dc2a | 421 | |
41ac7b3a | 422 | static int ci_hdrc_probe(struct platform_device *pdev) |
e443b333 AS |
423 | { |
424 | struct device *dev = &pdev->dev; | |
8e22978c | 425 | struct ci_hdrc *ci; |
e443b333 AS |
426 | struct resource *res; |
427 | void __iomem *base; | |
428 | int ret; | |
691962d1 | 429 | enum usb_dr_mode dr_mode; |
e98b44e9 | 430 | struct device_node *of_node = dev->of_node ?: dev->parent->of_node; |
e443b333 | 431 | |
5f36e231 | 432 | if (!dev->platform_data) { |
e443b333 AS |
433 | dev_err(dev, "platform data missing\n"); |
434 | return -ENODEV; | |
435 | } | |
436 | ||
437 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
19290816 FB |
438 | base = devm_ioremap_resource(dev, res); |
439 | if (IS_ERR(base)) | |
440 | return PTR_ERR(base); | |
e443b333 | 441 | |
5f36e231 AS |
442 | ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL); |
443 | if (!ci) { | |
444 | dev_err(dev, "can't allocate device\n"); | |
445 | return -ENOMEM; | |
446 | } | |
447 | ||
448 | ci->dev = dev; | |
77c4400f | 449 | ci->platdata = dev->platform_data; |
a2c3d690 RZ |
450 | if (ci->platdata->phy) |
451 | ci->transceiver = ci->platdata->phy; | |
452 | else | |
453 | ci->global_phy = true; | |
5f36e231 AS |
454 | |
455 | ret = hw_device_init(ci, base); | |
456 | if (ret < 0) { | |
457 | dev_err(dev, "can't initialize hardware\n"); | |
458 | return -ENODEV; | |
459 | } | |
e443b333 | 460 | |
eb70e5ab AS |
461 | ci->hw_bank.phys = res->start; |
462 | ||
5f36e231 AS |
463 | ci->irq = platform_get_irq(pdev, 0); |
464 | if (ci->irq < 0) { | |
e443b333 | 465 | dev_err(dev, "missing IRQ\n"); |
5f36e231 AS |
466 | return -ENODEV; |
467 | } | |
468 | ||
469 | INIT_WORK(&ci->work, ci_role_work); | |
470 | ci->wq = create_singlethread_workqueue("ci_otg"); | |
471 | if (!ci->wq) { | |
472 | dev_err(dev, "can't create workqueue\n"); | |
473 | return -ENODEV; | |
474 | } | |
475 | ||
40dcd0e8 | 476 | if (!ci->platdata->phy_mode) |
e98b44e9 | 477 | ci->platdata->phy_mode = of_usb_get_phy_mode(of_node); |
40dcd0e8 | 478 | |
03779f05 FE |
479 | hw_phymode_configure(ci); |
480 | ||
691962d1 | 481 | if (!ci->platdata->dr_mode) |
e98b44e9 | 482 | ci->platdata->dr_mode = of_usb_get_dr_mode(of_node); |
691962d1 SH |
483 | |
484 | if (ci->platdata->dr_mode == USB_DR_MODE_UNKNOWN) | |
485 | ci->platdata->dr_mode = USB_DR_MODE_OTG; | |
486 | ||
487 | dr_mode = ci->platdata->dr_mode; | |
5f36e231 | 488 | /* initialize role(s) before the interrupt is requested */ |
691962d1 SH |
489 | if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_HOST) { |
490 | ret = ci_hdrc_host_init(ci); | |
491 | if (ret) | |
492 | dev_info(dev, "doesn't support host\n"); | |
493 | } | |
eb70e5ab | 494 | |
691962d1 SH |
495 | if (dr_mode == USB_DR_MODE_OTG || dr_mode == USB_DR_MODE_PERIPHERAL) { |
496 | ret = ci_hdrc_gadget_init(ci); | |
497 | if (ret) | |
498 | dev_info(dev, "doesn't support gadget\n"); | |
499 | } | |
5f36e231 AS |
500 | |
501 | if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) { | |
502 | dev_err(dev, "no supported roles\n"); | |
503 | ret = -ENODEV; | |
504 | goto rm_wq; | |
505 | } | |
506 | ||
507 | if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) { | |
508 | ci->is_otg = true; | |
86ad01a9 RZ |
509 | /* ID pin needs 1ms debouce time, we delay 2ms for safe */ |
510 | mdelay(2); | |
5f36e231 AS |
511 | ci->role = ci_otg_role(ci); |
512 | } else { | |
513 | ci->role = ci->roles[CI_ROLE_HOST] | |
514 | ? CI_ROLE_HOST | |
515 | : CI_ROLE_GADGET; | |
516 | } | |
517 | ||
518 | ret = ci_role_start(ci, ci->role); | |
519 | if (ret) { | |
520 | dev_err(dev, "can't start %s role\n", ci_role(ci)->name); | |
5f36e231 | 521 | goto rm_wq; |
e443b333 AS |
522 | } |
523 | ||
5f36e231 | 524 | platform_set_drvdata(pdev, ci); |
77c4400f | 525 | ret = request_irq(ci->irq, ci_irq, IRQF_SHARED, ci->platdata->name, |
5f36e231 AS |
526 | ci); |
527 | if (ret) | |
528 | goto stop; | |
e443b333 | 529 | |
5f36e231 AS |
530 | if (ci->is_otg) |
531 | hw_write(ci, OP_OTGSC, OTGSC_IDIE, OTGSC_IDIE); | |
532 | ||
adf0f735 AS |
533 | ret = dbg_create_files(ci); |
534 | if (!ret) | |
535 | return 0; | |
5f36e231 | 536 | |
adf0f735 | 537 | free_irq(ci->irq, ci); |
5f36e231 AS |
538 | stop: |
539 | ci_role_stop(ci); | |
540 | rm_wq: | |
541 | flush_workqueue(ci->wq); | |
542 | destroy_workqueue(ci->wq); | |
e443b333 AS |
543 | |
544 | return ret; | |
545 | } | |
546 | ||
fb4e98ab | 547 | static int ci_hdrc_remove(struct platform_device *pdev) |
e443b333 | 548 | { |
8e22978c | 549 | struct ci_hdrc *ci = platform_get_drvdata(pdev); |
e443b333 | 550 | |
adf0f735 | 551 | dbg_remove_files(ci); |
5f36e231 AS |
552 | flush_workqueue(ci->wq); |
553 | destroy_workqueue(ci->wq); | |
5f36e231 AS |
554 | free_irq(ci->irq, ci); |
555 | ci_role_stop(ci); | |
e443b333 AS |
556 | |
557 | return 0; | |
558 | } | |
559 | ||
5f36e231 AS |
560 | static struct platform_driver ci_hdrc_driver = { |
561 | .probe = ci_hdrc_probe, | |
7690417d | 562 | .remove = ci_hdrc_remove, |
e443b333 | 563 | .driver = { |
5f36e231 | 564 | .name = "ci_hdrc", |
e443b333 AS |
565 | }, |
566 | }; | |
567 | ||
5f36e231 | 568 | module_platform_driver(ci_hdrc_driver); |
e443b333 | 569 | |
5f36e231 | 570 | MODULE_ALIAS("platform:ci_hdrc"); |
e443b333 AS |
571 | MODULE_LICENSE("GPL v2"); |
572 | MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>"); | |
5f36e231 | 573 | MODULE_DESCRIPTION("ChipIdea HDRC Driver"); |