blob: 55a0ae6f2d7430cdd4846a121efaa2620ae379cc [file] [log] [blame]
Anton Vorontsov236dd4d2009-01-10 05:03:21 +03001/*
2 * Freescale QUICC Engine USB Host Controller Driver
3 *
4 * Copyright (c) Freescale Semicondutor, Inc. 2006.
5 * Shlomi Gridish <gridish@freescale.com>
6 * Jerry Huang <Chang-Ming.Huang@freescale.com>
7 * Copyright (c) Logic Product Development, Inc. 2007
8 * Peter Barada <peterb@logicpd.com>
9 * Copyright (c) MontaVista Software, Inc. 2008.
10 * Anton Vorontsov <avorontsov@ru.mvista.com>
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 */
17
18#include <linux/module.h>
19#include <linux/types.h>
20#include <linux/spinlock.h>
21#include <linux/kernel.h>
22#include <linux/delay.h>
23#include <linux/errno.h>
24#include <linux/list.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/usb.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020028#include <linux/usb/hcd.h>
Rob Herring5af50732013-09-17 14:28:33 -050029#include <linux/of_address.h>
30#include <linux/of_irq.h>
Anton Vorontsov236dd4d2009-01-10 05:03:21 +030031#include <linux/of_platform.h>
32#include <linux/of_gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Zhao Qiang7aa1aa62015-11-30 10:48:57 +080034#include <soc/fsl/qe/qe.h>
Anton Vorontsov236dd4d2009-01-10 05:03:21 +030035#include <asm/fsl_gtm.h>
Anton Vorontsov236dd4d2009-01-10 05:03:21 +030036#include "fhci.h"
37
38void fhci_start_sof_timer(struct fhci_hcd *fhci)
39{
40 fhci_dbg(fhci, "-> %s\n", __func__);
41
42 /* clear frame_n */
43 out_be16(&fhci->pram->frame_num, 0);
44
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -030045 out_be16(&fhci->regs->usb_ussft, 0);
46 setbits8(&fhci->regs->usb_usmod, USB_MODE_SFTE);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +030047
48 fhci_dbg(fhci, "<- %s\n", __func__);
49}
50
51void fhci_stop_sof_timer(struct fhci_hcd *fhci)
52{
53 fhci_dbg(fhci, "-> %s\n", __func__);
54
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -030055 clrbits8(&fhci->regs->usb_usmod, USB_MODE_SFTE);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +030056 gtm_stop_timer16(fhci->timer);
57
58 fhci_dbg(fhci, "<- %s\n", __func__);
59}
60
61u16 fhci_get_sof_timer_count(struct fhci_usb *usb)
62{
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -030063 return be16_to_cpu(in_be16(&usb->fhci->regs->usb_ussft) / 12);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +030064}
65
66/* initialize the endpoint zero */
67static u32 endpoint_zero_init(struct fhci_usb *usb,
68 enum fhci_mem_alloc data_mem,
69 u32 ring_len)
70{
71 u32 rc;
72
73 rc = fhci_create_ep(usb, data_mem, ring_len);
74 if (rc)
75 return rc;
76
77 /* inilialize endpoint registers */
78 fhci_init_ep_registers(usb, usb->ep0, data_mem);
79
80 return 0;
81}
82
83/* enable the USB interrupts */
84void fhci_usb_enable_interrupt(struct fhci_usb *usb)
85{
86 struct fhci_hcd *fhci = usb->fhci;
87
88 if (usb->intr_nesting_cnt == 1) {
89 /* initialize the USB interrupt */
90 enable_irq(fhci_to_hcd(fhci)->irq);
91
92 /* initialize the event register and mask register */
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -030093 out_be16(&usb->fhci->regs->usb_usber, 0xffff);
94 out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +030095
96 /* enable the timer interrupts */
97 enable_irq(fhci->timer->irq);
98 } else if (usb->intr_nesting_cnt > 1)
99 fhci_info(fhci, "unbalanced USB interrupts nesting\n");
100 usb->intr_nesting_cnt--;
101}
102
Justin P. Mattock48e34d02010-12-30 15:07:58 -0800103/* disable the usb interrupt */
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300104void fhci_usb_disable_interrupt(struct fhci_usb *usb)
105{
106 struct fhci_hcd *fhci = usb->fhci;
107
108 if (usb->intr_nesting_cnt == 0) {
Justin P. Mattock48e34d02010-12-30 15:07:58 -0800109 /* disable the timer interrupt */
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300110 disable_irq_nosync(fhci->timer->irq);
111
112 /* disable the usb interrupt */
113 disable_irq_nosync(fhci_to_hcd(fhci)->irq);
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -0300114 out_be16(&usb->fhci->regs->usb_usbmr, 0);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300115 }
116 usb->intr_nesting_cnt++;
117}
118
119/* enable the USB controller */
120static u32 fhci_usb_enable(struct fhci_hcd *fhci)
121{
122 struct fhci_usb *usb = fhci->usb_lld;
123
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -0300124 out_be16(&usb->fhci->regs->usb_usber, 0xffff);
125 out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
126 setbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300127
128 mdelay(100);
129
130 return 0;
131}
132
133/* disable the USB controller */
134static u32 fhci_usb_disable(struct fhci_hcd *fhci)
135{
136 struct fhci_usb *usb = fhci->usb_lld;
137
138 fhci_usb_disable_interrupt(usb);
139 fhci_port_disable(fhci);
140
141 /* disable the usb controller */
142 if (usb->port_status == FHCI_PORT_FULL ||
143 usb->port_status == FHCI_PORT_LOW)
144 fhci_device_disconnected_interrupt(fhci);
145
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -0300146 clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300147
148 return 0;
149}
150
151/* check the bus state by polling the QE bit on the IO ports */
152int fhci_ioports_check_bus_state(struct fhci_hcd *fhci)
153{
154 u8 bits = 0;
155
156 /* check USBOE,if transmitting,exit */
157 if (!gpio_get_value(fhci->gpios[GPIO_USBOE]))
158 return -1;
159
160 /* check USBRP */
161 if (gpio_get_value(fhci->gpios[GPIO_USBRP]))
162 bits |= 0x2;
163
164 /* check USBRN */
165 if (gpio_get_value(fhci->gpios[GPIO_USBRN]))
166 bits |= 0x1;
167
168 return bits;
169}
170
171static void fhci_mem_free(struct fhci_hcd *fhci)
172{
173 struct ed *ed;
174 struct ed *next_ed;
175 struct td *td;
176 struct td *next_td;
177
178 list_for_each_entry_safe(ed, next_ed, &fhci->empty_eds, node) {
179 list_del(&ed->node);
180 kfree(ed);
181 }
182
183 list_for_each_entry_safe(td, next_td, &fhci->empty_tds, node) {
184 list_del(&td->node);
185 kfree(td);
186 }
187
188 kfree(fhci->vroot_hub);
189 fhci->vroot_hub = NULL;
190
191 kfree(fhci->hc_list);
192 fhci->hc_list = NULL;
193}
194
195static int fhci_mem_init(struct fhci_hcd *fhci)
196{
197 int i;
198
199 fhci->hc_list = kzalloc(sizeof(*fhci->hc_list), GFP_KERNEL);
200 if (!fhci->hc_list)
201 goto err;
202
203 INIT_LIST_HEAD(&fhci->hc_list->ctrl_list);
204 INIT_LIST_HEAD(&fhci->hc_list->bulk_list);
205 INIT_LIST_HEAD(&fhci->hc_list->iso_list);
206 INIT_LIST_HEAD(&fhci->hc_list->intr_list);
207 INIT_LIST_HEAD(&fhci->hc_list->done_list);
208
209 fhci->vroot_hub = kzalloc(sizeof(*fhci->vroot_hub), GFP_KERNEL);
210 if (!fhci->vroot_hub)
211 goto err;
212
213 INIT_LIST_HEAD(&fhci->empty_eds);
214 INIT_LIST_HEAD(&fhci->empty_tds);
215
216 /* initialize work queue to handle done list */
217 fhci_tasklet.data = (unsigned long)fhci;
218 fhci->process_done_task = &fhci_tasklet;
219
220 for (i = 0; i < MAX_TDS; i++) {
221 struct td *td;
222
223 td = kmalloc(sizeof(*td), GFP_KERNEL);
224 if (!td)
225 goto err;
226 fhci_recycle_empty_td(fhci, td);
227 }
228 for (i = 0; i < MAX_EDS; i++) {
229 struct ed *ed;
230
231 ed = kmalloc(sizeof(*ed), GFP_KERNEL);
232 if (!ed)
233 goto err;
234 fhci_recycle_empty_ed(fhci, ed);
235 }
236
237 fhci->active_urbs = 0;
238 return 0;
239err:
240 fhci_mem_free(fhci);
241 return -ENOMEM;
242}
243
244/* destroy the fhci_usb structure */
245static void fhci_usb_free(void *lld)
246{
247 struct fhci_usb *usb = lld;
Alexander Beregalovae35fe92010-01-07 04:17:32 +0300248 struct fhci_hcd *fhci;
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300249
250 if (usb) {
Alexander Beregalovae35fe92010-01-07 04:17:32 +0300251 fhci = usb->fhci;
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300252 fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
253 fhci_ep0_free(usb);
254 kfree(usb->actual_frame);
255 kfree(usb);
256 }
257}
258
259/* initialize the USB */
260static int fhci_usb_init(struct fhci_hcd *fhci)
261{
262 struct fhci_usb *usb = fhci->usb_lld;
263
264 memset_io(usb->fhci->pram, 0, FHCI_PRAM_SIZE);
265
266 usb->port_status = FHCI_PORT_DISABLED;
267 usb->max_frame_usage = FRAME_TIME_USAGE;
268 usb->sw_transaction_time = SW_FIX_TIME_BETWEEN_TRANSACTION;
269
270 usb->actual_frame = kzalloc(sizeof(*usb->actual_frame), GFP_KERNEL);
271 if (!usb->actual_frame) {
272 fhci_usb_free(usb);
273 return -ENOMEM;
274 }
275
276 INIT_LIST_HEAD(&usb->actual_frame->tds_list);
277
278 /* initializing registers on chip, clear frame number */
279 out_be16(&fhci->pram->frame_num, 0);
280
281 /* clear rx state */
282 out_be32(&fhci->pram->rx_state, 0);
283
284 /* set mask register */
285 usb->saved_msk = (USB_E_TXB_MASK |
286 USB_E_TXE1_MASK |
287 USB_E_IDLE_MASK |
288 USB_E_RESET_MASK | USB_E_SFT_MASK | USB_E_MSF_MASK);
289
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -0300290 out_8(&usb->fhci->regs->usb_usmod, USB_MODE_HOST | USB_MODE_EN);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300291
292 /* clearing the mask register */
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -0300293 out_be16(&usb->fhci->regs->usb_usbmr, 0);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300294
295 /* initialing the event register */
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -0300296 out_be16(&usb->fhci->regs->usb_usber, 0xffff);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300297
298 if (endpoint_zero_init(usb, DEFAULT_DATA_MEM, DEFAULT_RING_LEN) != 0) {
299 fhci_usb_free(usb);
300 return -EINVAL;
301 }
302
303 return 0;
304}
305
306/* initialize the fhci_usb struct and the corresponding data staruct */
307static struct fhci_usb *fhci_create_lld(struct fhci_hcd *fhci)
308{
309 struct fhci_usb *usb;
310
311 /* allocate memory for SCC data structure */
312 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
Wolfram Sang2652de72016-08-25 19:39:06 +0200313 if (!usb)
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300314 return NULL;
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300315
316 usb->fhci = fhci;
317 usb->hc_list = fhci->hc_list;
318 usb->vroot_hub = fhci->vroot_hub;
319
320 usb->transfer_confirm = fhci_transfer_confirm_callback;
321
322 return usb;
323}
324
325static int fhci_start(struct usb_hcd *hcd)
326{
327 int ret;
328 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
329
330 ret = fhci_mem_init(fhci);
331 if (ret) {
332 fhci_err(fhci, "failed to allocate memory\n");
333 goto err;
334 }
335
336 fhci->usb_lld = fhci_create_lld(fhci);
337 if (!fhci->usb_lld) {
338 fhci_err(fhci, "low level driver config failed\n");
339 ret = -ENOMEM;
340 goto err;
341 }
342
343 ret = fhci_usb_init(fhci);
344 if (ret) {
345 fhci_err(fhci, "low level driver initialize failed\n");
346 goto err;
347 }
348
349 spin_lock_init(&fhci->lock);
350
351 /* connect the virtual root hub */
352 fhci->vroot_hub->dev_num = 1; /* this field may be needed to fix */
353 fhci->vroot_hub->hub.wHubStatus = 0;
354 fhci->vroot_hub->hub.wHubChange = 0;
355 fhci->vroot_hub->port.wPortStatus = 0;
356 fhci->vroot_hub->port.wPortChange = 0;
357
358 hcd->state = HC_STATE_RUNNING;
359
360 /*
Petr Mladek37ebb542014-09-19 17:32:23 +0200361 * From here on, hub_wq concurrently accesses the root
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300362 * hub; drivers will be talking to enumerated devices.
Petr Mladek37ebb542014-09-19 17:32:23 +0200363 * (On restart paths, hub_wq already knows about the root
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300364 * hub and could find work as soon as we wrote FLAG_CF.)
365 *
Petr Mladek37ebb542014-09-19 17:32:23 +0200366 * Before this point the HC was idle/ready. After, hub_wq
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300367 * and device drivers may start it running.
368 */
369 fhci_usb_enable(fhci);
370 return 0;
371err:
372 fhci_mem_free(fhci);
373 return ret;
374}
375
376static void fhci_stop(struct usb_hcd *hcd)
377{
378 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
379
380 fhci_usb_disable_interrupt(fhci->usb_lld);
381 fhci_usb_disable(fhci);
382
383 fhci_usb_free(fhci->usb_lld);
384 fhci->usb_lld = NULL;
385 fhci_mem_free(fhci);
386}
387
388static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
389 gfp_t mem_flags)
390{
391 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
392 u32 pipe = urb->pipe;
393 int ret;
394 int i;
395 int size = 0;
396 struct urb_priv *urb_priv;
397 unsigned long flags;
398
399 switch (usb_pipetype(pipe)) {
400 case PIPE_CONTROL:
401 /* 1 td fro setup,1 for ack */
402 size = 2;
403 case PIPE_BULK:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300404 /* one td for every 4096 bytes(can be up to 8k) */
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300405 size += urb->transfer_buffer_length / 4096;
406 /* ...add for any remaining bytes... */
407 if ((urb->transfer_buffer_length % 4096) != 0)
408 size++;
409 /* ..and maybe a zero length packet to wrap it up */
410 if (size == 0)
411 size++;
412 else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
413 && (urb->transfer_buffer_length
414 % usb_maxpacket(urb->dev, pipe,
415 usb_pipeout(pipe))) != 0)
416 size++;
417 break;
418 case PIPE_ISOCHRONOUS:
419 size = urb->number_of_packets;
420 if (size <= 0)
421 return -EINVAL;
422 for (i = 0; i < urb->number_of_packets; i++) {
423 urb->iso_frame_desc[i].actual_length = 0;
424 urb->iso_frame_desc[i].status = (u32) (-EXDEV);
425 }
426 break;
427 case PIPE_INTERRUPT:
428 size = 1;
429 }
430
431 /* allocate the private part of the URB */
432 urb_priv = kzalloc(sizeof(*urb_priv), mem_flags);
433 if (!urb_priv)
434 return -ENOMEM;
435
436 /* allocate the private part of the URB */
Julia Lawall4585ef12009-12-30 15:34:37 +0100437 urb_priv->tds = kcalloc(size, sizeof(*urb_priv->tds), mem_flags);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300438 if (!urb_priv->tds) {
439 kfree(urb_priv);
440 return -ENOMEM;
441 }
442
443 spin_lock_irqsave(&fhci->lock, flags);
444
445 ret = usb_hcd_link_urb_to_ep(hcd, urb);
446 if (ret)
447 goto err;
448
449 /* fill the private part of the URB */
450 urb_priv->num_of_tds = size;
451
452 urb->status = -EINPROGRESS;
453 urb->actual_length = 0;
454 urb->error_count = 0;
455 urb->hcpriv = urb_priv;
456
457 fhci_queue_urb(fhci, urb);
458err:
459 if (ret) {
460 kfree(urb_priv->tds);
461 kfree(urb_priv);
462 }
463 spin_unlock_irqrestore(&fhci->lock, flags);
464 return ret;
465}
466
467/* dequeue FHCI URB */
468static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
469{
470 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
471 struct fhci_usb *usb = fhci->usb_lld;
472 int ret = -EINVAL;
473 unsigned long flags;
474
475 if (!urb || !urb->dev || !urb->dev->bus)
476 goto out;
477
478 spin_lock_irqsave(&fhci->lock, flags);
479
480 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
481 if (ret)
482 goto out2;
483
484 if (usb->port_status != FHCI_PORT_DISABLED) {
485 struct urb_priv *urb_priv;
486
487 /*
488 * flag the urb's data for deletion in some upcoming
489 * SF interrupt's delete list processing
490 */
491 urb_priv = urb->hcpriv;
492
493 if (!urb_priv || (urb_priv->state == URB_DEL))
494 goto out2;
495
496 urb_priv->state = URB_DEL;
497
498 /* already pending? */
499 urb_priv->ed->state = FHCI_ED_URB_DEL;
500 } else {
501 fhci_urb_complete_free(fhci, urb);
502 }
503
504out2:
505 spin_unlock_irqrestore(&fhci->lock, flags);
506out:
507 return ret;
508}
509
510static void fhci_endpoint_disable(struct usb_hcd *hcd,
511 struct usb_host_endpoint *ep)
512{
513 struct fhci_hcd *fhci;
514 struct ed *ed;
515 unsigned long flags;
516
517 fhci = hcd_to_fhci(hcd);
518 spin_lock_irqsave(&fhci->lock, flags);
519 ed = ep->hcpriv;
520 if (ed) {
521 while (ed->td_head != NULL) {
522 struct td *td = fhci_remove_td_from_ed(ed);
523 fhci_urb_complete_free(fhci, td->urb);
524 }
525 fhci_recycle_empty_ed(fhci, ed);
526 ep->hcpriv = NULL;
527 }
528 spin_unlock_irqrestore(&fhci->lock, flags);
529}
530
531static int fhci_get_frame_number(struct usb_hcd *hcd)
532{
533 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
534
535 return get_frame_num(fhci);
536}
537
538static const struct hc_driver fhci_driver = {
539 .description = "fsl,usb-fhci",
540 .product_desc = "FHCI HOST Controller",
541 .hcd_priv_size = sizeof(struct fhci_hcd),
542
543 /* generic hardware linkage */
544 .irq = fhci_irq,
545 .flags = HCD_USB11 | HCD_MEMORY,
546
547 /* basic lifecycle operation */
548 .start = fhci_start,
549 .stop = fhci_stop,
550
551 /* managing i/o requests and associated device resources */
552 .urb_enqueue = fhci_urb_enqueue,
553 .urb_dequeue = fhci_urb_dequeue,
554 .endpoint_disable = fhci_endpoint_disable,
555
556 /* scheduling support */
557 .get_frame_number = fhci_get_frame_number,
558
559 /* root hub support */
560 .hub_status_data = fhci_hub_status_data,
561 .hub_control = fhci_hub_control,
562};
563
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500564static int of_fhci_probe(struct platform_device *ofdev)
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300565{
566 struct device *dev = &ofdev->dev;
Grant Likely61c7a082010-04-13 16:12:29 -0700567 struct device_node *node = dev->of_node;
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300568 struct usb_hcd *hcd;
569 struct fhci_hcd *fhci;
570 struct resource usb_regs;
571 unsigned long pram_addr;
572 unsigned int usb_irq;
573 const char *sprop;
574 const u32 *iprop;
575 int size;
576 int ret;
577 int i;
578 int j;
579
580 if (usb_disabled())
581 return -ENODEV;
582
583 sprop = of_get_property(node, "mode", NULL);
584 if (sprop && strcmp(sprop, "host"))
585 return -ENODEV;
586
Sachin Sant6866ac92009-02-16 19:10:11 +0530587 hcd = usb_create_hcd(&fhci_driver, dev, dev_name(dev));
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300588 if (!hcd) {
589 dev_err(dev, "could not create hcd\n");
590 return -ENOMEM;
591 }
592
593 fhci = hcd_to_fhci(hcd);
594 hcd->self.controller = dev;
595 dev_set_drvdata(dev, hcd);
596
597 iprop = of_get_property(node, "hub-power-budget", &size);
598 if (iprop && size == sizeof(*iprop))
599 hcd->power_budget = *iprop;
600
601 /* FHCI registers. */
602 ret = of_address_to_resource(node, 0, &usb_regs);
603 if (ret) {
604 dev_err(dev, "could not get regs\n");
605 goto err_regs;
606 }
607
Joe Perches28f65c112011-06-09 09:13:32 -0700608 hcd->regs = ioremap(usb_regs.start, resource_size(&usb_regs));
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300609 if (!hcd->regs) {
610 dev_err(dev, "could not ioremap regs\n");
611 ret = -ENOMEM;
612 goto err_regs;
613 }
614 fhci->regs = hcd->regs;
615
616 /* Parameter RAM. */
617 iprop = of_get_property(node, "reg", &size);
618 if (!iprop || size < sizeof(*iprop) * 4) {
619 dev_err(dev, "can't get pram offset\n");
620 ret = -EINVAL;
621 goto err_pram;
622 }
623
Joakim Tjernlund39eb4ed2011-08-23 15:23:46 +0200624 pram_addr = cpm_muram_alloc(FHCI_PRAM_SIZE, 64);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300625 if (IS_ERR_VALUE(pram_addr)) {
626 dev_err(dev, "failed to allocate usb pram\n");
627 ret = -ENOMEM;
628 goto err_pram;
629 }
Joakim Tjernlund39eb4ed2011-08-23 15:23:46 +0200630
631 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, QE_CR_SUBBLOCK_USB,
632 QE_CR_PROTOCOL_UNSPECIFIED, pram_addr);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300633 fhci->pram = cpm_muram_addr(pram_addr);
634
635 /* GPIOs and pins */
636 for (i = 0; i < NUM_GPIOS; i++) {
637 int gpio;
638 enum of_gpio_flags flags;
639
640 gpio = of_get_gpio_flags(node, i, &flags);
641 fhci->gpios[i] = gpio;
642 fhci->alow_gpios[i] = flags & OF_GPIO_ACTIVE_LOW;
643
644 if (!gpio_is_valid(gpio)) {
645 if (i < GPIO_SPEED) {
646 dev_err(dev, "incorrect GPIO%d: %d\n",
647 i, gpio);
648 goto err_gpios;
649 } else {
650 dev_info(dev, "assuming board doesn't have "
651 "%s gpio\n", i == GPIO_SPEED ?
652 "speed" : "power");
653 continue;
654 }
655 }
656
Sachin Sant6866ac92009-02-16 19:10:11 +0530657 ret = gpio_request(gpio, dev_name(dev));
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300658 if (ret) {
659 dev_err(dev, "failed to request gpio %d", i);
660 goto err_gpios;
661 }
662
663 if (i >= GPIO_SPEED) {
664 ret = gpio_direction_output(gpio, 0);
665 if (ret) {
666 dev_err(dev, "failed to set gpio %d as "
667 "an output\n", i);
668 i++;
669 goto err_gpios;
670 }
671 }
672 }
673
674 for (j = 0; j < NUM_PINS; j++) {
Grant Likely61c7a082010-04-13 16:12:29 -0700675 fhci->pins[j] = qe_pin_request(node, j);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300676 if (IS_ERR(fhci->pins[j])) {
677 ret = PTR_ERR(fhci->pins[j]);
678 dev_err(dev, "can't get pin %d: %d\n", j, ret);
679 goto err_pins;
680 }
681 }
682
683 /* Frame limit timer and its interrupt. */
684 fhci->timer = gtm_get_timer16();
685 if (IS_ERR(fhci->timer)) {
686 ret = PTR_ERR(fhci->timer);
687 dev_err(dev, "failed to request qe timer: %i", ret);
688 goto err_get_timer;
689 }
690
691 ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800692 0, "qe timer (usb)", hcd);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300693 if (ret) {
694 dev_err(dev, "failed to request timer irq");
695 goto err_timer_irq;
696 }
697
698 /* USB Host interrupt. */
699 usb_irq = irq_of_parse_and_map(node, 0);
700 if (usb_irq == NO_IRQ) {
701 dev_err(dev, "could not get usb irq\n");
702 ret = -EINVAL;
703 goto err_usb_irq;
704 }
705
706 /* Clocks. */
707 sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
708 if (sprop) {
709 fhci->fullspeed_clk = qe_clock_source(sprop);
710 if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
711 dev_err(dev, "wrong fullspeed-clock\n");
712 ret = -EINVAL;
713 goto err_clocks;
714 }
715 }
716
717 sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
718 if (sprop) {
719 fhci->lowspeed_clk = qe_clock_source(sprop);
720 if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
721 dev_err(dev, "wrong lowspeed-clock\n");
722 ret = -EINVAL;
723 goto err_clocks;
724 }
725 }
726
727 if (fhci->fullspeed_clk == QE_CLK_NONE &&
728 fhci->lowspeed_clk == QE_CLK_NONE) {
729 dev_err(dev, "no clocks specified\n");
730 ret = -EINVAL;
731 goto err_clocks;
732 }
733
734 dev_info(dev, "at 0x%p, irq %d\n", hcd->regs, usb_irq);
735
736 fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
737
738 /* Start with full-speed, if possible. */
739 if (fhci->fullspeed_clk != QE_CLK_NONE) {
740 fhci_config_transceiver(fhci, FHCI_PORT_FULL);
741 qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
742 } else {
743 fhci_config_transceiver(fhci, FHCI_PORT_LOW);
744 qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
745 }
746
747 /* Clear and disable any pending interrupts. */
Guilherme Maciel Ferreiracf61fdb2012-06-23 21:24:30 -0300748 out_be16(&fhci->regs->usb_usber, 0xffff);
749 out_be16(&fhci->regs->usb_usbmr, 0);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300750
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800751 ret = usb_add_hcd(hcd, usb_irq, 0);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300752 if (ret < 0)
753 goto err_add_hcd;
754
Peter Chen3c9740a2013-11-05 10:46:02 +0800755 device_wakeup_enable(hcd->self.controller);
756
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300757 fhci_dfs_create(fhci);
758
759 return 0;
760
761err_add_hcd:
762err_clocks:
763 irq_dispose_mapping(usb_irq);
764err_usb_irq:
765 free_irq(fhci->timer->irq, hcd);
766err_timer_irq:
767 gtm_put_timer16(fhci->timer);
768err_get_timer:
769err_pins:
770 while (--j >= 0)
771 qe_pin_free(fhci->pins[j]);
772err_gpios:
773 while (--i >= 0) {
774 if (gpio_is_valid(fhci->gpios[i]))
775 gpio_free(fhci->gpios[i]);
776 }
777 cpm_muram_free(pram_addr);
778err_pram:
779 iounmap(hcd->regs);
780err_regs:
781 usb_put_hcd(hcd);
782 return ret;
783}
784
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500785static int fhci_remove(struct device *dev)
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300786{
787 struct usb_hcd *hcd = dev_get_drvdata(dev);
788 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
789 int i;
790 int j;
791
792 usb_remove_hcd(hcd);
793 free_irq(fhci->timer->irq, hcd);
794 gtm_put_timer16(fhci->timer);
795 cpm_muram_free(cpm_muram_offset(fhci->pram));
796 for (i = 0; i < NUM_GPIOS; i++) {
797 if (!gpio_is_valid(fhci->gpios[i]))
798 continue;
799 gpio_free(fhci->gpios[i]);
800 }
801 for (j = 0; j < NUM_PINS; j++)
802 qe_pin_free(fhci->pins[j]);
803 fhci_dfs_destroy(fhci);
804 usb_put_hcd(hcd);
805 return 0;
806}
807
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500808static int of_fhci_remove(struct platform_device *ofdev)
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300809{
810 return fhci_remove(&ofdev->dev);
811}
812
Németh Mártonc4386ad2010-01-10 15:35:03 +0100813static const struct of_device_id of_fhci_match[] = {
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300814 { .compatible = "fsl,mpc8323-qe-usb", },
815 {},
816};
817MODULE_DEVICE_TABLE(of, of_fhci_match);
818
Grant Likelyd35fb642011-02-22 21:08:34 -0700819static struct platform_driver of_fhci_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700820 .driver = {
821 .name = "fsl,usb-fhci",
Grant Likely40182942010-04-13 16:13:02 -0700822 .of_match_table = of_fhci_match,
823 },
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300824 .probe = of_fhci_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500825 .remove = of_fhci_remove,
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300826};
827
Axel Lincc27c962011-11-27 20:16:27 +0800828module_platform_driver(of_fhci_driver);
Anton Vorontsov236dd4d2009-01-10 05:03:21 +0300829
830MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
831MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
832 "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
833 "Anton Vorontsov <avorontsov@ru.mvista.com>");
834MODULE_LICENSE("GPL");