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 | |
46 | * - Isochronous & Interrupt Traffic | |
47 | * - Handle requests which spawns into several TDs | |
48 | * - GET_STATUS(device) - always reports 0 | |
49 | * - Gadget API (majority of optional features) | |
50 | * - Suspend & Remote Wakeup | |
51 | */ | |
52 | #include <linux/delay.h> | |
53 | #include <linux/device.h> | |
54 | #include <linux/dmapool.h> | |
55 | #include <linux/dma-mapping.h> | |
56 | #include <linux/init.h> | |
57 | #include <linux/platform_device.h> | |
58 | #include <linux/module.h> | |
59 | #include <linux/interrupt.h> | |
60 | #include <linux/io.h> | |
61 | #include <linux/irq.h> | |
62 | #include <linux/kernel.h> | |
63 | #include <linux/slab.h> | |
64 | #include <linux/pm_runtime.h> | |
65 | #include <linux/usb/ch9.h> | |
66 | #include <linux/usb/gadget.h> | |
67 | #include <linux/usb/otg.h> | |
68 | #include <linux/usb/chipidea.h> | |
69 | ||
70 | #include "ci.h" | |
71 | #include "udc.h" | |
72 | #include "bits.h" | |
73 | #include "debug.h" | |
74 | ||
75 | /* MSM specific */ | |
76 | #define ABS_AHBBURST (0x0090UL) | |
77 | #define ABS_AHBMODE (0x0098UL) | |
5f36e231 | 78 | /* Controller register map */ |
e443b333 AS |
79 | static uintptr_t ci_regs_nolpm[] = { |
80 | [CAP_CAPLENGTH] = 0x000UL, | |
81 | [CAP_HCCPARAMS] = 0x008UL, | |
82 | [CAP_DCCPARAMS] = 0x024UL, | |
83 | [CAP_TESTMODE] = 0x038UL, | |
84 | [OP_USBCMD] = 0x000UL, | |
85 | [OP_USBSTS] = 0x004UL, | |
86 | [OP_USBINTR] = 0x008UL, | |
87 | [OP_DEVICEADDR] = 0x014UL, | |
88 | [OP_ENDPTLISTADDR] = 0x018UL, | |
89 | [OP_PORTSC] = 0x044UL, | |
90 | [OP_DEVLC] = 0x084UL, | |
5f36e231 | 91 | [OP_OTGSC] = 0x064UL, |
e443b333 AS |
92 | [OP_USBMODE] = 0x068UL, |
93 | [OP_ENDPTSETUPSTAT] = 0x06CUL, | |
94 | [OP_ENDPTPRIME] = 0x070UL, | |
95 | [OP_ENDPTFLUSH] = 0x074UL, | |
96 | [OP_ENDPTSTAT] = 0x078UL, | |
97 | [OP_ENDPTCOMPLETE] = 0x07CUL, | |
98 | [OP_ENDPTCTRL] = 0x080UL, | |
99 | }; | |
100 | ||
101 | static uintptr_t ci_regs_lpm[] = { | |
102 | [CAP_CAPLENGTH] = 0x000UL, | |
103 | [CAP_HCCPARAMS] = 0x008UL, | |
104 | [CAP_DCCPARAMS] = 0x024UL, | |
105 | [CAP_TESTMODE] = 0x0FCUL, | |
106 | [OP_USBCMD] = 0x000UL, | |
107 | [OP_USBSTS] = 0x004UL, | |
108 | [OP_USBINTR] = 0x008UL, | |
109 | [OP_DEVICEADDR] = 0x014UL, | |
110 | [OP_ENDPTLISTADDR] = 0x018UL, | |
111 | [OP_PORTSC] = 0x044UL, | |
112 | [OP_DEVLC] = 0x084UL, | |
5f36e231 | 113 | [OP_OTGSC] = 0x0C4UL, |
e443b333 AS |
114 | [OP_USBMODE] = 0x0C8UL, |
115 | [OP_ENDPTSETUPSTAT] = 0x0D8UL, | |
116 | [OP_ENDPTPRIME] = 0x0DCUL, | |
117 | [OP_ENDPTFLUSH] = 0x0E0UL, | |
118 | [OP_ENDPTSTAT] = 0x0E4UL, | |
119 | [OP_ENDPTCOMPLETE] = 0x0E8UL, | |
120 | [OP_ENDPTCTRL] = 0x0ECUL, | |
121 | }; | |
122 | ||
5f36e231 | 123 | static int hw_alloc_regmap(struct ci13xxx *ci, bool is_lpm) |
e443b333 AS |
124 | { |
125 | int i; | |
126 | ||
5f36e231 | 127 | kfree(ci->hw_bank.regmap); |
e443b333 | 128 | |
5f36e231 AS |
129 | ci->hw_bank.regmap = kzalloc((OP_LAST + 1) * sizeof(void *), |
130 | GFP_KERNEL); | |
131 | if (!ci->hw_bank.regmap) | |
e443b333 AS |
132 | return -ENOMEM; |
133 | ||
134 | for (i = 0; i < OP_ENDPTCTRL; i++) | |
5f36e231 AS |
135 | ci->hw_bank.regmap[i] = |
136 | (i <= CAP_LAST ? ci->hw_bank.cap : ci->hw_bank.op) + | |
e443b333 AS |
137 | (is_lpm ? ci_regs_lpm[i] : ci_regs_nolpm[i]); |
138 | ||
139 | for (; i <= OP_LAST; i++) | |
5f36e231 | 140 | ci->hw_bank.regmap[i] = ci->hw_bank.op + |
e443b333 AS |
141 | 4 * (i - OP_ENDPTCTRL) + |
142 | (is_lpm | |
143 | ? ci_regs_lpm[OP_ENDPTCTRL] | |
144 | : ci_regs_nolpm[OP_ENDPTCTRL]); | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | /** | |
150 | * hw_port_test_set: writes port test mode (execute without interruption) | |
151 | * @mode: new value | |
152 | * | |
153 | * This function returns an error code | |
154 | */ | |
155 | int hw_port_test_set(struct ci13xxx *ci, u8 mode) | |
156 | { | |
157 | const u8 TEST_MODE_MAX = 7; | |
158 | ||
159 | if (mode > TEST_MODE_MAX) | |
160 | return -EINVAL; | |
161 | ||
162 | hw_write(ci, OP_PORTSC, PORTSC_PTC, mode << ffs_nr(PORTSC_PTC)); | |
163 | return 0; | |
164 | } | |
165 | ||
166 | /** | |
167 | * hw_port_test_get: reads port test mode value | |
168 | * | |
169 | * This function returns port test mode value | |
170 | */ | |
171 | u8 hw_port_test_get(struct ci13xxx *ci) | |
172 | { | |
173 | return hw_read(ci, OP_PORTSC, PORTSC_PTC) >> ffs_nr(PORTSC_PTC); | |
174 | } | |
175 | ||
5f36e231 | 176 | static int hw_device_init(struct ci13xxx *ci, void __iomem *base) |
e443b333 AS |
177 | { |
178 | u32 reg; | |
179 | ||
180 | /* bank is a module variable */ | |
5f36e231 | 181 | ci->hw_bank.abs = base; |
e443b333 | 182 | |
5f36e231 AS |
183 | ci->hw_bank.cap = ci->hw_bank.abs; |
184 | ci->hw_bank.cap += ci->udc_driver->capoffset; | |
185 | ci->hw_bank.op = ci->hw_bank.cap + ioread8(ci->hw_bank.cap); | |
e443b333 | 186 | |
5f36e231 AS |
187 | hw_alloc_regmap(ci, false); |
188 | reg = hw_read(ci, CAP_HCCPARAMS, HCCPARAMS_LEN) >> | |
e443b333 | 189 | ffs_nr(HCCPARAMS_LEN); |
5f36e231 AS |
190 | ci->hw_bank.lpm = reg; |
191 | hw_alloc_regmap(ci, !!reg); | |
192 | ci->hw_bank.size = ci->hw_bank.op - ci->hw_bank.abs; | |
193 | ci->hw_bank.size += OP_LAST; | |
194 | ci->hw_bank.size /= sizeof(u32); | |
e443b333 | 195 | |
5f36e231 | 196 | reg = hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_DEN) >> |
e443b333 | 197 | ffs_nr(DCCPARAMS_DEN); |
5f36e231 | 198 | ci->hw_ep_max = reg * 2; /* cache hw ENDPT_MAX */ |
e443b333 | 199 | |
5f36e231 | 200 | if (ci->hw_ep_max == 0 || ci->hw_ep_max > ENDPT_MAX) |
e443b333 AS |
201 | return -ENODEV; |
202 | ||
5f36e231 AS |
203 | dev_dbg(ci->dev, "ChipIdea HDRC found, lpm: %d; cap: %p op: %p\n", |
204 | ci->hw_bank.lpm, ci->hw_bank.cap, ci->hw_bank.op); | |
e443b333 AS |
205 | |
206 | /* setup lock mode ? */ | |
207 | ||
208 | /* ENDPTSETUPSTAT is '0' by default */ | |
209 | ||
210 | /* HCSPARAMS.bf.ppc SHOULD BE zero for device */ | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | /** | |
216 | * hw_device_reset: resets chip (execute without interruption) | |
217 | * @ci: the controller | |
218 | * | |
219 | * This function returns an error code | |
220 | */ | |
221 | int hw_device_reset(struct ci13xxx *ci) | |
222 | { | |
223 | /* should flush & stop before reset */ | |
224 | hw_write(ci, OP_ENDPTFLUSH, ~0, ~0); | |
225 | hw_write(ci, OP_USBCMD, USBCMD_RS, 0); | |
226 | ||
227 | hw_write(ci, OP_USBCMD, USBCMD_RST, USBCMD_RST); | |
228 | while (hw_read(ci, OP_USBCMD, USBCMD_RST)) | |
229 | udelay(10); /* not RTOS friendly */ | |
230 | ||
231 | ||
232 | if (ci->udc_driver->notify_event) | |
233 | ci->udc_driver->notify_event(ci, | |
234 | CI13XXX_CONTROLLER_RESET_EVENT); | |
235 | ||
236 | if (ci->udc_driver->flags & CI13XXX_DISABLE_STREAMING) | |
237 | hw_write(ci, OP_USBMODE, USBMODE_SDIS, USBMODE_SDIS); | |
238 | ||
239 | /* USBMODE should be configured step by step */ | |
240 | hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_IDLE); | |
241 | hw_write(ci, OP_USBMODE, USBMODE_CM, USBMODE_CM_DEVICE); | |
242 | /* HW >= 2.3 */ | |
243 | hw_write(ci, OP_USBMODE, USBMODE_SLOM, USBMODE_SLOM); | |
244 | ||
245 | if (hw_read(ci, OP_USBMODE, USBMODE_CM) != USBMODE_CM_DEVICE) { | |
246 | pr_err("cannot enter in device mode"); | |
247 | pr_err("lpm = %i", ci->hw_bank.lpm); | |
248 | return -ENODEV; | |
249 | } | |
250 | ||
251 | return 0; | |
252 | } | |
253 | ||
5f36e231 AS |
254 | /** |
255 | * ci_otg_role - pick role based on ID pin state | |
256 | * @ci: the controller | |
257 | */ | |
258 | static enum ci_role ci_otg_role(struct ci13xxx *ci) | |
259 | { | |
260 | u32 sts = hw_read(ci, OP_OTGSC, ~0); | |
261 | enum ci_role role = sts & OTGSC_ID | |
262 | ? CI_ROLE_GADGET | |
263 | : CI_ROLE_HOST; | |
264 | ||
265 | return role; | |
266 | } | |
267 | ||
268 | /** | |
269 | * ci_role_work - perform role changing based on ID pin | |
270 | * @work: work struct | |
271 | */ | |
272 | static void ci_role_work(struct work_struct *work) | |
273 | { | |
274 | struct ci13xxx *ci = container_of(work, struct ci13xxx, work); | |
275 | enum ci_role role = ci_otg_role(ci); | |
276 | ||
277 | hw_write(ci, OP_OTGSC, OTGSC_IDIS, OTGSC_IDIS); | |
278 | ||
279 | if (role != ci->role) { | |
280 | dev_dbg(ci->dev, "switching from %s to %s\n", | |
281 | ci_role(ci)->name, ci->roles[role]->name); | |
282 | ||
283 | ci_role_stop(ci); | |
284 | ci_role_start(ci, role); | |
285 | } | |
286 | } | |
287 | ||
288 | static ssize_t show_role(struct device *dev, struct device_attribute *attr, | |
289 | char *buf) | |
290 | { | |
291 | struct ci13xxx *ci = dev_get_drvdata(dev); | |
292 | ||
293 | return sprintf(buf, "%s\n", ci_role(ci)->name); | |
294 | } | |
295 | ||
296 | static ssize_t store_role(struct device *dev, struct device_attribute *attr, | |
297 | const char *buf, size_t count) | |
298 | { | |
299 | struct ci13xxx *ci = dev_get_drvdata(dev); | |
300 | enum ci_role role; | |
301 | int ret; | |
302 | ||
303 | for (role = CI_ROLE_HOST; role < CI_ROLE_END; role++) | |
304 | if (ci->roles[role] && !strcmp(buf, ci->roles[role]->name)) | |
305 | break; | |
306 | ||
307 | if (role == CI_ROLE_END || role == ci->role) | |
308 | return -EINVAL; | |
309 | ||
310 | ci_role_stop(ci); | |
311 | ret = ci_role_start(ci, role); | |
312 | if (ret) | |
313 | return ret; | |
314 | ||
315 | return count; | |
316 | } | |
317 | ||
318 | static DEVICE_ATTR(role, S_IRUSR | S_IWUSR, show_role, store_role); | |
319 | ||
320 | static irqreturn_t ci_irq(int irq, void *data) | |
321 | { | |
322 | struct ci13xxx *ci = data; | |
323 | irqreturn_t ret = IRQ_NONE; | |
324 | ||
325 | if (ci->is_otg) { | |
326 | u32 sts = hw_read(ci, OP_OTGSC, ~0); | |
327 | ||
328 | if (sts & OTGSC_IDIS) { | |
329 | queue_work(ci->wq, &ci->work); | |
330 | ret = IRQ_HANDLED; | |
331 | } | |
332 | } | |
333 | ||
334 | return ci->role == CI_ROLE_END ? ret : ci_role(ci)->irq(ci); | |
335 | } | |
336 | ||
337 | static int __devinit ci_hdrc_probe(struct platform_device *pdev) | |
e443b333 AS |
338 | { |
339 | struct device *dev = &pdev->dev; | |
5f36e231 | 340 | struct ci13xxx *ci; |
e443b333 AS |
341 | struct resource *res; |
342 | void __iomem *base; | |
343 | int ret; | |
344 | ||
5f36e231 | 345 | if (!dev->platform_data) { |
e443b333 AS |
346 | dev_err(dev, "platform data missing\n"); |
347 | return -ENODEV; | |
348 | } | |
349 | ||
350 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
351 | if (!res) { | |
352 | dev_err(dev, "missing resource\n"); | |
353 | return -ENODEV; | |
354 | } | |
355 | ||
356 | base = devm_request_and_ioremap(dev, res); | |
357 | if (!res) { | |
358 | dev_err(dev, "can't request and ioremap resource\n"); | |
359 | return -ENOMEM; | |
360 | } | |
361 | ||
5f36e231 AS |
362 | ci = devm_kzalloc(dev, sizeof(*ci), GFP_KERNEL); |
363 | if (!ci) { | |
364 | dev_err(dev, "can't allocate device\n"); | |
365 | return -ENOMEM; | |
366 | } | |
367 | ||
368 | ci->dev = dev; | |
369 | ci->udc_driver = dev->platform_data; | |
370 | ||
371 | ret = hw_device_init(ci, base); | |
372 | if (ret < 0) { | |
373 | dev_err(dev, "can't initialize hardware\n"); | |
374 | return -ENODEV; | |
375 | } | |
e443b333 | 376 | |
5f36e231 AS |
377 | ci->irq = platform_get_irq(pdev, 0); |
378 | if (ci->irq < 0) { | |
e443b333 | 379 | dev_err(dev, "missing IRQ\n"); |
5f36e231 AS |
380 | return -ENODEV; |
381 | } | |
382 | ||
383 | INIT_WORK(&ci->work, ci_role_work); | |
384 | ci->wq = create_singlethread_workqueue("ci_otg"); | |
385 | if (!ci->wq) { | |
386 | dev_err(dev, "can't create workqueue\n"); | |
387 | return -ENODEV; | |
388 | } | |
389 | ||
390 | /* initialize role(s) before the interrupt is requested */ | |
391 | ret = ci_hdrc_gadget_init(ci); | |
392 | if (ret) | |
393 | dev_info(dev, "doesn't support gadget\n"); | |
394 | ||
395 | if (!ci->roles[CI_ROLE_HOST] && !ci->roles[CI_ROLE_GADGET]) { | |
396 | dev_err(dev, "no supported roles\n"); | |
397 | ret = -ENODEV; | |
398 | goto rm_wq; | |
399 | } | |
400 | ||
401 | if (ci->roles[CI_ROLE_HOST] && ci->roles[CI_ROLE_GADGET]) { | |
402 | ci->is_otg = true; | |
403 | ci->role = ci_otg_role(ci); | |
404 | } else { | |
405 | ci->role = ci->roles[CI_ROLE_HOST] | |
406 | ? CI_ROLE_HOST | |
407 | : CI_ROLE_GADGET; | |
408 | } | |
409 | ||
410 | ret = ci_role_start(ci, ci->role); | |
411 | if (ret) { | |
412 | dev_err(dev, "can't start %s role\n", ci_role(ci)->name); | |
e443b333 | 413 | ret = -ENODEV; |
5f36e231 | 414 | goto rm_wq; |
e443b333 AS |
415 | } |
416 | ||
5f36e231 AS |
417 | platform_set_drvdata(pdev, ci); |
418 | ret = request_irq(ci->irq, ci_irq, IRQF_SHARED, ci->udc_driver->name, | |
419 | ci); | |
420 | if (ret) | |
421 | goto stop; | |
e443b333 | 422 | |
5f36e231 | 423 | ret = device_create_file(dev, &dev_attr_role); |
e443b333 | 424 | if (ret) |
5f36e231 AS |
425 | goto rm_attr; |
426 | ||
427 | if (ci->is_otg) | |
428 | hw_write(ci, OP_OTGSC, OTGSC_IDIE, OTGSC_IDIE); | |
429 | ||
430 | return ret; | |
431 | ||
432 | rm_attr: | |
433 | device_remove_file(dev, &dev_attr_role); | |
434 | stop: | |
435 | ci_role_stop(ci); | |
436 | rm_wq: | |
437 | flush_workqueue(ci->wq); | |
438 | destroy_workqueue(ci->wq); | |
e443b333 AS |
439 | |
440 | return ret; | |
441 | } | |
442 | ||
5f36e231 | 443 | static int __devexit ci_hdrc_remove(struct platform_device *pdev) |
e443b333 | 444 | { |
5f36e231 | 445 | struct ci13xxx *ci = platform_get_drvdata(pdev); |
e443b333 | 446 | |
5f36e231 AS |
447 | flush_workqueue(ci->wq); |
448 | destroy_workqueue(ci->wq); | |
449 | device_remove_file(ci->dev, &dev_attr_role); | |
450 | free_irq(ci->irq, ci); | |
451 | ci_role_stop(ci); | |
e443b333 AS |
452 | |
453 | return 0; | |
454 | } | |
455 | ||
5f36e231 AS |
456 | static struct platform_driver ci_hdrc_driver = { |
457 | .probe = ci_hdrc_probe, | |
458 | .remove = __devexit_p(ci_hdrc_remove), | |
e443b333 | 459 | .driver = { |
5f36e231 | 460 | .name = "ci_hdrc", |
e443b333 AS |
461 | }, |
462 | }; | |
463 | ||
5f36e231 | 464 | module_platform_driver(ci_hdrc_driver); |
e443b333 | 465 | |
5f36e231 | 466 | MODULE_ALIAS("platform:ci_hdrc"); |
e443b333 AS |
467 | MODULE_ALIAS("platform:ci13xxx"); |
468 | MODULE_LICENSE("GPL v2"); | |
469 | MODULE_AUTHOR("David Lopo <dlopo@chipidea.mips.com>"); | |
5f36e231 | 470 | MODULE_DESCRIPTION("ChipIdea HDRC Driver"); |