1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <linux/platform_device.h>
33#include <linux/mfd/core.h>
34#include <linux/mfd/tmio.h>
35#include <linux/dma-mapping.h>
36
37
38
39
40
41
42#define CCR_REVID 0x08
43#define CCR_BASE 0x10
44#define CCR_ILME 0x40
45#define CCR_PM 0x4c
46#define CCR_INTC 0x50
47#define CCR_LMW1L 0x54
48#define CCR_LMW1H 0x56
49#define CCR_LMW1BL 0x58
50#define CCR_LMW1BH 0x5A
51#define CCR_LMW2L 0x5C
52#define CCR_LMW2H 0x5E
53#define CCR_LMW2BL 0x60
54#define CCR_LMW2BH 0x62
55#define CCR_MISC 0xFC
56
57#define CCR_PM_GKEN 0x0001
58#define CCR_PM_CKRNEN 0x0002
59#define CCR_PM_USBPW1 0x0004
60#define CCR_PM_USBPW2 0x0008
61#define CCR_PM_USBPW3 0x0008
62#define CCR_PM_PMEE 0x0100
63#define CCR_PM_PMES 0x8000
64
65
66
67struct tmio_hcd {
68 void __iomem *ccr;
69 spinlock_t lock;
70};
71
72#define hcd_to_tmio(hcd) ((struct tmio_hcd *)(hcd_to_ohci(hcd) + 1))
73
74
75
76static void tmio_write_pm(struct platform_device *dev)
77{
78 struct usb_hcd *hcd = platform_get_drvdata(dev);
79 struct tmio_hcd *tmio = hcd_to_tmio(hcd);
80 u16 pm;
81 unsigned long flags;
82
83 spin_lock_irqsave(&tmio->lock, flags);
84
85 pm = CCR_PM_GKEN | CCR_PM_CKRNEN |
86 CCR_PM_PMEE | CCR_PM_PMES;
87
88 tmio_iowrite16(pm, tmio->ccr + CCR_PM);
89 spin_unlock_irqrestore(&tmio->lock, flags);
90}
91
92static void tmio_stop_hc(struct platform_device *dev)
93{
94 struct usb_hcd *hcd = platform_get_drvdata(dev);
95 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
96 struct tmio_hcd *tmio = hcd_to_tmio(hcd);
97 u16 pm;
98
99 pm = CCR_PM_GKEN | CCR_PM_CKRNEN;
100 switch (ohci->num_ports) {
101 default:
102 dev_err(&dev->dev, "Unsupported amount of ports: %d\n", ohci->num_ports);
103 case 3:
104 pm |= CCR_PM_USBPW3;
105 case 2:
106 pm |= CCR_PM_USBPW2;
107 case 1:
108 pm |= CCR_PM_USBPW1;
109 }
110 tmio_iowrite8(0, tmio->ccr + CCR_INTC);
111 tmio_iowrite8(0, tmio->ccr + CCR_ILME);
112 tmio_iowrite16(0, tmio->ccr + CCR_BASE);
113 tmio_iowrite16(0, tmio->ccr + CCR_BASE + 2);
114 tmio_iowrite16(pm, tmio->ccr + CCR_PM);
115}
116
117static void tmio_start_hc(struct platform_device *dev)
118{
119 struct usb_hcd *hcd = platform_get_drvdata(dev);
120 struct tmio_hcd *tmio = hcd_to_tmio(hcd);
121 unsigned long base = hcd->rsrc_start;
122
123 tmio_write_pm(dev);
124 tmio_iowrite16(base, tmio->ccr + CCR_BASE);
125 tmio_iowrite16(base >> 16, tmio->ccr + CCR_BASE + 2);
126 tmio_iowrite8(1, tmio->ccr + CCR_ILME);
127 tmio_iowrite8(2, tmio->ccr + CCR_INTC);
128
129 dev_info(&dev->dev, "revision %d @ 0x%08llx, irq %d\n",
130 tmio_ioread8(tmio->ccr + CCR_REVID),
131 (u64) hcd->rsrc_start, hcd->irq);
132}
133
134static int ohci_tmio_start(struct usb_hcd *hcd)
135{
136 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
137 int ret;
138
139 if ((ret = ohci_init(ohci)) < 0)
140 return ret;
141
142 if ((ret = ohci_run(ohci)) < 0) {
143 dev_err(hcd->self.controller, "can't start %s\n",
144 hcd->self.bus_name);
145 ohci_stop(hcd);
146 return ret;
147 }
148
149 return 0;
150}
151
152static const struct hc_driver ohci_tmio_hc_driver = {
153 .description = hcd_name,
154 .product_desc = "TMIO OHCI USB Host Controller",
155 .hcd_priv_size = sizeof(struct ohci_hcd) + sizeof (struct tmio_hcd),
156
157
158 .irq = ohci_irq,
159 .flags = HCD_USB11 | HCD_MEMORY | HCD_LOCAL_MEM,
160
161
162 .start = ohci_tmio_start,
163 .stop = ohci_stop,
164 .shutdown = ohci_shutdown,
165
166
167 .urb_enqueue = ohci_urb_enqueue,
168 .urb_dequeue = ohci_urb_dequeue,
169 .endpoint_disable = ohci_endpoint_disable,
170
171
172 .get_frame_number = ohci_get_frame,
173
174
175 .hub_status_data = ohci_hub_status_data,
176 .hub_control = ohci_hub_control,
177#ifdef CONFIG_PM
178 .bus_suspend = ohci_bus_suspend,
179 .bus_resume = ohci_bus_resume,
180#endif
181 .start_port_reset = ohci_start_port_reset,
182};
183
184
185static struct platform_driver ohci_hcd_tmio_driver;
186
187static int ohci_hcd_tmio_drv_probe(struct platform_device *dev)
188{
189 const struct mfd_cell *cell = mfd_get_cell(dev);
190 struct resource *regs = platform_get_resource(dev, IORESOURCE_MEM, 0);
191 struct resource *config = platform_get_resource(dev, IORESOURCE_MEM, 1);
192 struct resource *sram = platform_get_resource(dev, IORESOURCE_MEM, 2);
193 int irq = platform_get_irq(dev, 0);
194 struct tmio_hcd *tmio;
195 struct ohci_hcd *ohci;
196 struct usb_hcd *hcd;
197 int ret;
198
199 if (usb_disabled())
200 return -ENODEV;
201
202 if (!cell)
203 return -EINVAL;
204
205 hcd = usb_create_hcd(&ohci_tmio_hc_driver, &dev->dev, dev_name(&dev->dev));
206 if (!hcd) {
207 ret = -ENOMEM;
208 goto err_usb_create_hcd;
209 }
210
211 hcd->rsrc_start = regs->start;
212 hcd->rsrc_len = resource_size(regs);
213
214 tmio = hcd_to_tmio(hcd);
215
216 spin_lock_init(&tmio->lock);
217
218 tmio->ccr = ioremap(config->start, resource_size(config));
219 if (!tmio->ccr) {
220 ret = -ENOMEM;
221 goto err_ioremap_ccr;
222 }
223
224 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
225 if (!hcd->regs) {
226 ret = -ENOMEM;
227 goto err_ioremap_regs;
228 }
229
230 if (!dma_declare_coherent_memory(&dev->dev, sram->start,
231 sram->start,
232 resource_size(sram),
233 DMA_MEMORY_MAP | DMA_MEMORY_EXCLUSIVE)) {
234 ret = -EBUSY;
235 goto err_dma_declare;
236 }
237
238 if (cell->enable) {
239 ret = cell->enable(dev);
240 if (ret)
241 goto err_enable;
242 }
243
244 tmio_start_hc(dev);
245 ohci = hcd_to_ohci(hcd);
246 ohci_hcd_init(ohci);
247
248 ret = usb_add_hcd(hcd, irq, 0);
249 if (ret)
250 goto err_add_hcd;
251
252 if (ret == 0)
253 return ret;
254
255 usb_remove_hcd(hcd);
256
257err_add_hcd:
258 tmio_stop_hc(dev);
259 if (cell->disable)
260 cell->disable(dev);
261err_enable:
262 dma_release_declared_memory(&dev->dev);
263err_dma_declare:
264 iounmap(hcd->regs);
265err_ioremap_regs:
266 iounmap(tmio->ccr);
267err_ioremap_ccr:
268 usb_put_hcd(hcd);
269err_usb_create_hcd:
270
271 return ret;
272}
273
274static int ohci_hcd_tmio_drv_remove(struct platform_device *dev)
275{
276 struct usb_hcd *hcd = platform_get_drvdata(dev);
277 struct tmio_hcd *tmio = hcd_to_tmio(hcd);
278 const struct mfd_cell *cell = mfd_get_cell(dev);
279
280 usb_remove_hcd(hcd);
281 tmio_stop_hc(dev);
282 if (cell->disable)
283 cell->disable(dev);
284 dma_release_declared_memory(&dev->dev);
285 iounmap(hcd->regs);
286 iounmap(tmio->ccr);
287 usb_put_hcd(hcd);
288
289 platform_set_drvdata(dev, NULL);
290
291 return 0;
292}
293
294#ifdef CONFIG_PM
295static int ohci_hcd_tmio_drv_suspend(struct platform_device *dev, pm_message_t state)
296{
297 const struct mfd_cell *cell = mfd_get_cell(dev);
298 struct usb_hcd *hcd = platform_get_drvdata(dev);
299 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
300 struct tmio_hcd *tmio = hcd_to_tmio(hcd);
301 unsigned long flags;
302 u8 misc;
303 int ret;
304
305 if (time_before(jiffies, ohci->next_statechange))
306 msleep(5);
307 ohci->next_statechange = jiffies;
308
309 spin_lock_irqsave(&tmio->lock, flags);
310
311 misc = tmio_ioread8(tmio->ccr + CCR_MISC);
312 misc |= 1 << 3;
313 tmio_iowrite8(misc, tmio->ccr + CCR_MISC);
314
315 spin_unlock_irqrestore(&tmio->lock, flags);
316
317 if (cell->suspend) {
318 ret = cell->suspend(dev);
319 if (ret)
320 return ret;
321 }
322 return 0;
323}
324
325static int ohci_hcd_tmio_drv_resume(struct platform_device *dev)
326{
327 const struct mfd_cell *cell = mfd_get_cell(dev);
328 struct usb_hcd *hcd = platform_get_drvdata(dev);
329 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
330 struct tmio_hcd *tmio = hcd_to_tmio(hcd);
331 unsigned long flags;
332 u8 misc;
333 int ret;
334
335 if (time_before(jiffies, ohci->next_statechange))
336 msleep(5);
337 ohci->next_statechange = jiffies;
338
339 if (cell->resume) {
340 ret = cell->resume(dev);
341 if (ret)
342 return ret;
343 }
344
345 tmio_start_hc(dev);
346
347 spin_lock_irqsave(&tmio->lock, flags);
348
349 misc = tmio_ioread8(tmio->ccr + CCR_MISC);
350 misc &= ~(1 << 3);
351 tmio_iowrite8(misc, tmio->ccr + CCR_MISC);
352
353 spin_unlock_irqrestore(&tmio->lock, flags);
354
355 ohci_resume(hcd, false);
356
357 return 0;
358}
359#else
360#define ohci_hcd_tmio_drv_suspend NULL
361#define ohci_hcd_tmio_drv_resume NULL
362#endif
363
364static struct platform_driver ohci_hcd_tmio_driver = {
365 .probe = ohci_hcd_tmio_drv_probe,
366 .remove = ohci_hcd_tmio_drv_remove,
367 .shutdown = usb_hcd_platform_shutdown,
368 .suspend = ohci_hcd_tmio_drv_suspend,
369 .resume = ohci_hcd_tmio_drv_resume,
370 .driver = {
371 .name = "tmio-ohci",
372 .owner = THIS_MODULE,
373 },
374};
375