2 * Freescale QUICC Engine USB Host Controller Driver
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>
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.
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>
27 #include <linux/usb.h>
28 #include <linux/usb/hcd.h>
29 #include <linux/of_platform.h>
30 #include <linux/of_gpio.h>
31 #include <linux/slab.h>
33 #include <asm/fsl_gtm.h>
36 void fhci_start_sof_timer(struct fhci_hcd *fhci)
38 fhci_dbg(fhci, "-> %s\n", __func__);
41 out_be16(&fhci->pram->frame_num, 0);
43 out_be16(&fhci->regs->usb_sof_tmr, 0);
44 setbits8(&fhci->regs->usb_mod, USB_MODE_SFTE);
46 fhci_dbg(fhci, "<- %s\n", __func__);
49 void fhci_stop_sof_timer(struct fhci_hcd *fhci)
51 fhci_dbg(fhci, "-> %s\n", __func__);
53 clrbits8(&fhci->regs->usb_mod, USB_MODE_SFTE);
54 gtm_stop_timer16(fhci->timer);
56 fhci_dbg(fhci, "<- %s\n", __func__);
59 u16 fhci_get_sof_timer_count(struct fhci_usb *usb)
61 return be16_to_cpu(in_be16(&usb->fhci->regs->usb_sof_tmr) / 12);
64 /* initialize the endpoint zero */
65 static u32 endpoint_zero_init(struct fhci_usb *usb,
66 enum fhci_mem_alloc data_mem,
71 rc = fhci_create_ep(usb, data_mem, ring_len);
75 /* inilialize endpoint registers */
76 fhci_init_ep_registers(usb, usb->ep0, data_mem);
81 /* enable the USB interrupts */
82 void fhci_usb_enable_interrupt(struct fhci_usb *usb)
84 struct fhci_hcd *fhci = usb->fhci;
86 if (usb->intr_nesting_cnt == 1) {
87 /* initialize the USB interrupt */
88 enable_irq(fhci_to_hcd(fhci)->irq);
90 /* initialize the event register and mask register */
91 out_be16(&usb->fhci->regs->usb_event, 0xffff);
92 out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
94 /* enable the timer interrupts */
95 enable_irq(fhci->timer->irq);
96 } else if (usb->intr_nesting_cnt > 1)
97 fhci_info(fhci, "unbalanced USB interrupts nesting\n");
98 usb->intr_nesting_cnt--;
101 /* diable the usb interrupt */
102 void fhci_usb_disable_interrupt(struct fhci_usb *usb)
104 struct fhci_hcd *fhci = usb->fhci;
106 if (usb->intr_nesting_cnt == 0) {
107 /* diable the timer interrupt */
108 disable_irq_nosync(fhci->timer->irq);
110 /* disable the usb interrupt */
111 disable_irq_nosync(fhci_to_hcd(fhci)->irq);
112 out_be16(&usb->fhci->regs->usb_mask, 0);
114 usb->intr_nesting_cnt++;
117 /* enable the USB controller */
118 static u32 fhci_usb_enable(struct fhci_hcd *fhci)
120 struct fhci_usb *usb = fhci->usb_lld;
122 out_be16(&usb->fhci->regs->usb_event, 0xffff);
123 out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
124 setbits8(&usb->fhci->regs->usb_mod, USB_MODE_EN);
131 /* disable the USB controller */
132 static u32 fhci_usb_disable(struct fhci_hcd *fhci)
134 struct fhci_usb *usb = fhci->usb_lld;
136 fhci_usb_disable_interrupt(usb);
137 fhci_port_disable(fhci);
139 /* disable the usb controller */
140 if (usb->port_status == FHCI_PORT_FULL ||
141 usb->port_status == FHCI_PORT_LOW)
142 fhci_device_disconnected_interrupt(fhci);
144 clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_EN);
149 /* check the bus state by polling the QE bit on the IO ports */
150 int fhci_ioports_check_bus_state(struct fhci_hcd *fhci)
154 /* check USBOE,if transmitting,exit */
155 if (!gpio_get_value(fhci->gpios[GPIO_USBOE]))
159 if (gpio_get_value(fhci->gpios[GPIO_USBRP]))
163 if (gpio_get_value(fhci->gpios[GPIO_USBRN]))
169 static void fhci_mem_free(struct fhci_hcd *fhci)
176 list_for_each_entry_safe(ed, next_ed, &fhci->empty_eds, node) {
181 list_for_each_entry_safe(td, next_td, &fhci->empty_tds, node) {
186 kfree(fhci->vroot_hub);
187 fhci->vroot_hub = NULL;
189 kfree(fhci->hc_list);
190 fhci->hc_list = NULL;
193 static int fhci_mem_init(struct fhci_hcd *fhci)
197 fhci->hc_list = kzalloc(sizeof(*fhci->hc_list), GFP_KERNEL);
201 INIT_LIST_HEAD(&fhci->hc_list->ctrl_list);
202 INIT_LIST_HEAD(&fhci->hc_list->bulk_list);
203 INIT_LIST_HEAD(&fhci->hc_list->iso_list);
204 INIT_LIST_HEAD(&fhci->hc_list->intr_list);
205 INIT_LIST_HEAD(&fhci->hc_list->done_list);
207 fhci->vroot_hub = kzalloc(sizeof(*fhci->vroot_hub), GFP_KERNEL);
208 if (!fhci->vroot_hub)
211 INIT_LIST_HEAD(&fhci->empty_eds);
212 INIT_LIST_HEAD(&fhci->empty_tds);
214 /* initialize work queue to handle done list */
215 fhci_tasklet.data = (unsigned long)fhci;
216 fhci->process_done_task = &fhci_tasklet;
218 for (i = 0; i < MAX_TDS; i++) {
221 td = kmalloc(sizeof(*td), GFP_KERNEL);
224 fhci_recycle_empty_td(fhci, td);
226 for (i = 0; i < MAX_EDS; i++) {
229 ed = kmalloc(sizeof(*ed), GFP_KERNEL);
232 fhci_recycle_empty_ed(fhci, ed);
235 fhci->active_urbs = 0;
242 /* destroy the fhci_usb structure */
243 static void fhci_usb_free(void *lld)
245 struct fhci_usb *usb = lld;
246 struct fhci_hcd *fhci;
250 fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
252 kfree(usb->actual_frame);
257 /* initialize the USB */
258 static int fhci_usb_init(struct fhci_hcd *fhci)
260 struct fhci_usb *usb = fhci->usb_lld;
262 memset_io(usb->fhci->pram, 0, FHCI_PRAM_SIZE);
264 usb->port_status = FHCI_PORT_DISABLED;
265 usb->max_frame_usage = FRAME_TIME_USAGE;
266 usb->sw_transaction_time = SW_FIX_TIME_BETWEEN_TRANSACTION;
268 usb->actual_frame = kzalloc(sizeof(*usb->actual_frame), GFP_KERNEL);
269 if (!usb->actual_frame) {
274 INIT_LIST_HEAD(&usb->actual_frame->tds_list);
276 /* initializing registers on chip, clear frame number */
277 out_be16(&fhci->pram->frame_num, 0);
280 out_be32(&fhci->pram->rx_state, 0);
282 /* set mask register */
283 usb->saved_msk = (USB_E_TXB_MASK |
286 USB_E_RESET_MASK | USB_E_SFT_MASK | USB_E_MSF_MASK);
288 out_8(&usb->fhci->regs->usb_mod, USB_MODE_HOST | USB_MODE_EN);
290 /* clearing the mask register */
291 out_be16(&usb->fhci->regs->usb_mask, 0);
293 /* initialing the event register */
294 out_be16(&usb->fhci->regs->usb_event, 0xffff);
296 if (endpoint_zero_init(usb, DEFAULT_DATA_MEM, DEFAULT_RING_LEN) != 0) {
304 /* initialize the fhci_usb struct and the corresponding data staruct */
305 static struct fhci_usb *fhci_create_lld(struct fhci_hcd *fhci)
307 struct fhci_usb *usb;
309 /* allocate memory for SCC data structure */
310 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
312 fhci_err(fhci, "no memory for SCC data struct\n");
317 usb->hc_list = fhci->hc_list;
318 usb->vroot_hub = fhci->vroot_hub;
320 usb->transfer_confirm = fhci_transfer_confirm_callback;
325 static int fhci_start(struct usb_hcd *hcd)
328 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
330 ret = fhci_mem_init(fhci);
332 fhci_err(fhci, "failed to allocate memory\n");
336 fhci->usb_lld = fhci_create_lld(fhci);
337 if (!fhci->usb_lld) {
338 fhci_err(fhci, "low level driver config failed\n");
343 ret = fhci_usb_init(fhci);
345 fhci_err(fhci, "low level driver initialize failed\n");
349 spin_lock_init(&fhci->lock);
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;
358 hcd->state = HC_STATE_RUNNING;
361 * From here on, khubd concurrently accesses the root
362 * hub; drivers will be talking to enumerated devices.
363 * (On restart paths, khubd already knows about the root
364 * hub and could find work as soon as we wrote FLAG_CF.)
366 * Before this point the HC was idle/ready. After, khubd
367 * and device drivers may start it running.
369 fhci_usb_enable(fhci);
376 static void fhci_stop(struct usb_hcd *hcd)
378 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
380 fhci_usb_disable_interrupt(fhci->usb_lld);
381 fhci_usb_disable(fhci);
383 fhci_usb_free(fhci->usb_lld);
384 fhci->usb_lld = NULL;
388 static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
391 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
392 u32 pipe = urb->pipe;
396 struct urb_priv *urb_priv;
399 switch (usb_pipetype(pipe)) {
401 /* 1 td fro setup,1 for ack */
404 /* one td for every 4096 bytes(can be upto 8k) */
405 size += urb->transfer_buffer_length / 4096;
406 /* ...add for any remaining bytes... */
407 if ((urb->transfer_buffer_length % 4096) != 0)
409 /* ..and maybe a zero length packet to wrap it up */
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)
418 case PIPE_ISOCHRONOUS:
419 size = urb->number_of_packets;
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);
431 /* allocate the private part of the URB */
432 urb_priv = kzalloc(sizeof(*urb_priv), mem_flags);
436 /* allocate the private part of the URB */
437 urb_priv->tds = kcalloc(size, sizeof(*urb_priv->tds), mem_flags);
438 if (!urb_priv->tds) {
443 spin_lock_irqsave(&fhci->lock, flags);
445 ret = usb_hcd_link_urb_to_ep(hcd, urb);
449 /* fill the private part of the URB */
450 urb_priv->num_of_tds = size;
452 urb->status = -EINPROGRESS;
453 urb->actual_length = 0;
454 urb->error_count = 0;
455 urb->hcpriv = urb_priv;
457 fhci_queue_urb(fhci, urb);
460 kfree(urb_priv->tds);
463 spin_unlock_irqrestore(&fhci->lock, flags);
467 /* dequeue FHCI URB */
468 static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
470 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
471 struct fhci_usb *usb = fhci->usb_lld;
475 if (!urb || !urb->dev || !urb->dev->bus)
478 spin_lock_irqsave(&fhci->lock, flags);
480 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
484 if (usb->port_status != FHCI_PORT_DISABLED) {
485 struct urb_priv *urb_priv;
488 * flag the urb's data for deletion in some upcoming
489 * SF interrupt's delete list processing
491 urb_priv = urb->hcpriv;
493 if (!urb_priv || (urb_priv->state == URB_DEL))
496 urb_priv->state = URB_DEL;
498 /* already pending? */
499 urb_priv->ed->state = FHCI_ED_URB_DEL;
501 fhci_urb_complete_free(fhci, urb);
505 spin_unlock_irqrestore(&fhci->lock, flags);
510 static void fhci_endpoint_disable(struct usb_hcd *hcd,
511 struct usb_host_endpoint *ep)
513 struct fhci_hcd *fhci;
517 fhci = hcd_to_fhci(hcd);
518 spin_lock_irqsave(&fhci->lock, flags);
521 while (ed->td_head != NULL) {
522 struct td *td = fhci_remove_td_from_ed(ed);
523 fhci_urb_complete_free(fhci, td->urb);
525 fhci_recycle_empty_ed(fhci, ed);
528 spin_unlock_irqrestore(&fhci->lock, flags);
531 static int fhci_get_frame_number(struct usb_hcd *hcd)
533 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
535 return get_frame_num(fhci);
538 static 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),
543 /* generic hardware linkage */
545 .flags = HCD_USB11 | HCD_MEMORY,
547 /* basic lifecycle operation */
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,
556 /* scheduling support */
557 .get_frame_number = fhci_get_frame_number,
559 /* root hub support */
560 .hub_status_data = fhci_hub_status_data,
561 .hub_control = fhci_hub_control,
564 static int __devinit of_fhci_probe(struct platform_device *ofdev,
565 const struct of_device_id *ofid)
567 struct device *dev = &ofdev->dev;
568 struct device_node *node = dev->of_node;
570 struct fhci_hcd *fhci;
571 struct resource usb_regs;
572 unsigned long pram_addr;
573 unsigned int usb_irq;
584 sprop = of_get_property(node, "mode", NULL);
585 if (sprop && strcmp(sprop, "host"))
588 hcd = usb_create_hcd(&fhci_driver, dev, dev_name(dev));
590 dev_err(dev, "could not create hcd\n");
594 fhci = hcd_to_fhci(hcd);
595 hcd->self.controller = dev;
596 dev_set_drvdata(dev, hcd);
598 iprop = of_get_property(node, "hub-power-budget", &size);
599 if (iprop && size == sizeof(*iprop))
600 hcd->power_budget = *iprop;
602 /* FHCI registers. */
603 ret = of_address_to_resource(node, 0, &usb_regs);
605 dev_err(dev, "could not get regs\n");
609 hcd->regs = ioremap(usb_regs.start, usb_regs.end - usb_regs.start + 1);
611 dev_err(dev, "could not ioremap regs\n");
615 fhci->regs = hcd->regs;
618 iprop = of_get_property(node, "reg", &size);
619 if (!iprop || size < sizeof(*iprop) * 4) {
620 dev_err(dev, "can't get pram offset\n");
625 pram_addr = cpm_muram_alloc_fixed(iprop[2], FHCI_PRAM_SIZE);
626 if (IS_ERR_VALUE(pram_addr)) {
627 dev_err(dev, "failed to allocate usb pram\n");
631 fhci->pram = cpm_muram_addr(pram_addr);
634 for (i = 0; i < NUM_GPIOS; i++) {
636 enum of_gpio_flags flags;
638 gpio = of_get_gpio_flags(node, i, &flags);
639 fhci->gpios[i] = gpio;
640 fhci->alow_gpios[i] = flags & OF_GPIO_ACTIVE_LOW;
642 if (!gpio_is_valid(gpio)) {
643 if (i < GPIO_SPEED) {
644 dev_err(dev, "incorrect GPIO%d: %d\n",
648 dev_info(dev, "assuming board doesn't have "
649 "%s gpio\n", i == GPIO_SPEED ?
655 ret = gpio_request(gpio, dev_name(dev));
657 dev_err(dev, "failed to request gpio %d", i);
661 if (i >= GPIO_SPEED) {
662 ret = gpio_direction_output(gpio, 0);
664 dev_err(dev, "failed to set gpio %d as "
672 for (j = 0; j < NUM_PINS; j++) {
673 fhci->pins[j] = qe_pin_request(node, j);
674 if (IS_ERR(fhci->pins[j])) {
675 ret = PTR_ERR(fhci->pins[j]);
676 dev_err(dev, "can't get pin %d: %d\n", j, ret);
681 /* Frame limit timer and its interrupt. */
682 fhci->timer = gtm_get_timer16();
683 if (IS_ERR(fhci->timer)) {
684 ret = PTR_ERR(fhci->timer);
685 dev_err(dev, "failed to request qe timer: %i", ret);
689 ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
690 IRQF_DISABLED, "qe timer (usb)", hcd);
692 dev_err(dev, "failed to request timer irq");
696 /* USB Host interrupt. */
697 usb_irq = irq_of_parse_and_map(node, 0);
698 if (usb_irq == NO_IRQ) {
699 dev_err(dev, "could not get usb irq\n");
705 sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
707 fhci->fullspeed_clk = qe_clock_source(sprop);
708 if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
709 dev_err(dev, "wrong fullspeed-clock\n");
715 sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
717 fhci->lowspeed_clk = qe_clock_source(sprop);
718 if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
719 dev_err(dev, "wrong lowspeed-clock\n");
725 if (fhci->fullspeed_clk == QE_CLK_NONE &&
726 fhci->lowspeed_clk == QE_CLK_NONE) {
727 dev_err(dev, "no clocks specified\n");
732 dev_info(dev, "at 0x%p, irq %d\n", hcd->regs, usb_irq);
734 fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
736 /* Start with full-speed, if possible. */
737 if (fhci->fullspeed_clk != QE_CLK_NONE) {
738 fhci_config_transceiver(fhci, FHCI_PORT_FULL);
739 qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
741 fhci_config_transceiver(fhci, FHCI_PORT_LOW);
742 qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
745 /* Clear and disable any pending interrupts. */
746 out_be16(&fhci->regs->usb_event, 0xffff);
747 out_be16(&fhci->regs->usb_mask, 0);
749 ret = usb_add_hcd(hcd, usb_irq, IRQF_DISABLED);
753 fhci_dfs_create(fhci);
759 irq_dispose_mapping(usb_irq);
761 free_irq(fhci->timer->irq, hcd);
763 gtm_put_timer16(fhci->timer);
767 qe_pin_free(fhci->pins[j]);
770 if (gpio_is_valid(fhci->gpios[i]))
771 gpio_free(fhci->gpios[i]);
773 cpm_muram_free(pram_addr);
781 static int __devexit fhci_remove(struct device *dev)
783 struct usb_hcd *hcd = dev_get_drvdata(dev);
784 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
789 free_irq(fhci->timer->irq, hcd);
790 gtm_put_timer16(fhci->timer);
791 cpm_muram_free(cpm_muram_offset(fhci->pram));
792 for (i = 0; i < NUM_GPIOS; i++) {
793 if (!gpio_is_valid(fhci->gpios[i]))
795 gpio_free(fhci->gpios[i]);
797 for (j = 0; j < NUM_PINS; j++)
798 qe_pin_free(fhci->pins[j]);
799 fhci_dfs_destroy(fhci);
804 static int __devexit of_fhci_remove(struct platform_device *ofdev)
806 return fhci_remove(&ofdev->dev);
809 static const struct of_device_id of_fhci_match[] = {
810 { .compatible = "fsl,mpc8323-qe-usb", },
813 MODULE_DEVICE_TABLE(of, of_fhci_match);
815 static struct of_platform_driver of_fhci_driver = {
817 .name = "fsl,usb-fhci",
818 .owner = THIS_MODULE,
819 .of_match_table = of_fhci_match,
821 .probe = of_fhci_probe,
822 .remove = __devexit_p(of_fhci_remove),
825 static int __init fhci_module_init(void)
827 return of_register_platform_driver(&of_fhci_driver);
829 module_init(fhci_module_init);
831 static void __exit fhci_module_exit(void)
833 of_unregister_platform_driver(&of_fhci_driver);
835 module_exit(fhci_module_exit);
837 MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
838 MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
839 "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
840 "Anton Vorontsov <avorontsov@ru.mvista.com>");
841 MODULE_LICENSE("GPL");