1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale QUICC Engine USB Host Controller Driver
5 * Copyright (c) Freescale Semicondutor, Inc. 2006.
6 * Shlomi Gridish <gridish@freescale.com>
7 * Jerry Huang <Chang-Ming.Huang@freescale.com>
8 * Copyright (c) Logic Product Development, Inc. 2007
9 * Peter Barada <peterb@logicpd.com>
10 * Copyright (c) MontaVista Software, Inc. 2008.
11 * Anton Vorontsov <avorontsov@ru.mvista.com>
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/spinlock.h>
17 #include <linux/kernel.h>
18 #include <linux/delay.h>
19 #include <linux/errno.h>
20 #include <linux/list.h>
21 #include <linux/interrupt.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_platform.h>
28 #include <linux/of_gpio.h>
29 #include <linux/slab.h>
30 #include <soc/fsl/qe/qe.h>
31 #include <asm/fsl_gtm.h>
34 void fhci_start_sof_timer(struct fhci_hcd *fhci)
36 fhci_dbg(fhci, "-> %s\n", __func__);
39 out_be16(&fhci->pram->frame_num, 0);
41 out_be16(&fhci->regs->usb_ussft, 0);
42 setbits8(&fhci->regs->usb_usmod, USB_MODE_SFTE);
44 fhci_dbg(fhci, "<- %s\n", __func__);
47 void fhci_stop_sof_timer(struct fhci_hcd *fhci)
49 fhci_dbg(fhci, "-> %s\n", __func__);
51 clrbits8(&fhci->regs->usb_usmod, USB_MODE_SFTE);
52 gtm_stop_timer16(fhci->timer);
54 fhci_dbg(fhci, "<- %s\n", __func__);
57 u16 fhci_get_sof_timer_count(struct fhci_usb *usb)
59 return be16_to_cpu(in_be16(&usb->fhci->regs->usb_ussft) / 12);
62 /* initialize the endpoint zero */
63 static u32 endpoint_zero_init(struct fhci_usb *usb,
64 enum fhci_mem_alloc data_mem,
69 rc = fhci_create_ep(usb, data_mem, ring_len);
73 /* inilialize endpoint registers */
74 fhci_init_ep_registers(usb, usb->ep0, data_mem);
79 /* enable the USB interrupts */
80 void fhci_usb_enable_interrupt(struct fhci_usb *usb)
82 struct fhci_hcd *fhci = usb->fhci;
84 if (usb->intr_nesting_cnt == 1) {
85 /* initialize the USB interrupt */
86 enable_irq(fhci_to_hcd(fhci)->irq);
88 /* initialize the event register and mask register */
89 out_be16(&usb->fhci->regs->usb_usber, 0xffff);
90 out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
92 /* enable the timer interrupts */
93 enable_irq(fhci->timer->irq);
94 } else if (usb->intr_nesting_cnt > 1)
95 fhci_info(fhci, "unbalanced USB interrupts nesting\n");
96 usb->intr_nesting_cnt--;
99 /* disable the usb interrupt */
100 void fhci_usb_disable_interrupt(struct fhci_usb *usb)
102 struct fhci_hcd *fhci = usb->fhci;
104 if (usb->intr_nesting_cnt == 0) {
105 /* disable the timer interrupt */
106 disable_irq_nosync(fhci->timer->irq);
108 /* disable the usb interrupt */
109 disable_irq_nosync(fhci_to_hcd(fhci)->irq);
110 out_be16(&usb->fhci->regs->usb_usbmr, 0);
112 usb->intr_nesting_cnt++;
115 /* enable the USB controller */
116 static u32 fhci_usb_enable(struct fhci_hcd *fhci)
118 struct fhci_usb *usb = fhci->usb_lld;
120 out_be16(&usb->fhci->regs->usb_usber, 0xffff);
121 out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
122 setbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
129 /* disable the USB controller */
130 static u32 fhci_usb_disable(struct fhci_hcd *fhci)
132 struct fhci_usb *usb = fhci->usb_lld;
134 fhci_usb_disable_interrupt(usb);
135 fhci_port_disable(fhci);
137 /* disable the usb controller */
138 if (usb->port_status == FHCI_PORT_FULL ||
139 usb->port_status == FHCI_PORT_LOW)
140 fhci_device_disconnected_interrupt(fhci);
142 clrbits8(&usb->fhci->regs->usb_usmod, USB_MODE_EN);
147 /* check the bus state by polling the QE bit on the IO ports */
148 int fhci_ioports_check_bus_state(struct fhci_hcd *fhci)
152 /* check USBOE,if transmitting,exit */
153 if (!gpio_get_value(fhci->gpios[GPIO_USBOE]))
157 if (gpio_get_value(fhci->gpios[GPIO_USBRP]))
161 if (gpio_get_value(fhci->gpios[GPIO_USBRN]))
167 static void fhci_mem_free(struct fhci_hcd *fhci)
174 list_for_each_entry_safe(ed, next_ed, &fhci->empty_eds, node) {
179 list_for_each_entry_safe(td, next_td, &fhci->empty_tds, node) {
184 kfree(fhci->vroot_hub);
185 fhci->vroot_hub = NULL;
187 kfree(fhci->hc_list);
188 fhci->hc_list = NULL;
191 static int fhci_mem_init(struct fhci_hcd *fhci)
195 fhci->hc_list = kzalloc(sizeof(*fhci->hc_list), GFP_KERNEL);
199 INIT_LIST_HEAD(&fhci->hc_list->ctrl_list);
200 INIT_LIST_HEAD(&fhci->hc_list->bulk_list);
201 INIT_LIST_HEAD(&fhci->hc_list->iso_list);
202 INIT_LIST_HEAD(&fhci->hc_list->intr_list);
203 INIT_LIST_HEAD(&fhci->hc_list->done_list);
205 fhci->vroot_hub = kzalloc(sizeof(*fhci->vroot_hub), GFP_KERNEL);
206 if (!fhci->vroot_hub)
209 INIT_LIST_HEAD(&fhci->empty_eds);
210 INIT_LIST_HEAD(&fhci->empty_tds);
212 /* initialize work queue to handle done list */
213 fhci_tasklet.data = (unsigned long)fhci;
214 fhci->process_done_task = &fhci_tasklet;
216 for (i = 0; i < MAX_TDS; i++) {
219 td = kmalloc(sizeof(*td), GFP_KERNEL);
222 fhci_recycle_empty_td(fhci, td);
224 for (i = 0; i < MAX_EDS; i++) {
227 ed = kmalloc(sizeof(*ed), GFP_KERNEL);
230 fhci_recycle_empty_ed(fhci, ed);
233 fhci->active_urbs = 0;
240 /* destroy the fhci_usb structure */
241 static void fhci_usb_free(void *lld)
243 struct fhci_usb *usb = lld;
244 struct fhci_hcd *fhci;
248 fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
250 kfree(usb->actual_frame);
255 /* initialize the USB */
256 static int fhci_usb_init(struct fhci_hcd *fhci)
258 struct fhci_usb *usb = fhci->usb_lld;
260 memset_io(usb->fhci->pram, 0, FHCI_PRAM_SIZE);
262 usb->port_status = FHCI_PORT_DISABLED;
263 usb->max_frame_usage = FRAME_TIME_USAGE;
264 usb->sw_transaction_time = SW_FIX_TIME_BETWEEN_TRANSACTION;
266 usb->actual_frame = kzalloc(sizeof(*usb->actual_frame), GFP_KERNEL);
267 if (!usb->actual_frame) {
272 INIT_LIST_HEAD(&usb->actual_frame->tds_list);
274 /* initializing registers on chip, clear frame number */
275 out_be16(&fhci->pram->frame_num, 0);
278 out_be32(&fhci->pram->rx_state, 0);
280 /* set mask register */
281 usb->saved_msk = (USB_E_TXB_MASK |
284 USB_E_RESET_MASK | USB_E_SFT_MASK | USB_E_MSF_MASK);
286 out_8(&usb->fhci->regs->usb_usmod, USB_MODE_HOST | USB_MODE_EN);
288 /* clearing the mask register */
289 out_be16(&usb->fhci->regs->usb_usbmr, 0);
291 /* initialing the event register */
292 out_be16(&usb->fhci->regs->usb_usber, 0xffff);
294 if (endpoint_zero_init(usb, DEFAULT_DATA_MEM, DEFAULT_RING_LEN) != 0) {
302 /* initialize the fhci_usb struct and the corresponding data staruct */
303 static struct fhci_usb *fhci_create_lld(struct fhci_hcd *fhci)
305 struct fhci_usb *usb;
307 /* allocate memory for SCC data structure */
308 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
313 usb->hc_list = fhci->hc_list;
314 usb->vroot_hub = fhci->vroot_hub;
316 usb->transfer_confirm = fhci_transfer_confirm_callback;
321 static int fhci_start(struct usb_hcd *hcd)
324 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
326 ret = fhci_mem_init(fhci);
328 fhci_err(fhci, "failed to allocate memory\n");
332 fhci->usb_lld = fhci_create_lld(fhci);
333 if (!fhci->usb_lld) {
334 fhci_err(fhci, "low level driver config failed\n");
339 ret = fhci_usb_init(fhci);
341 fhci_err(fhci, "low level driver initialize failed\n");
345 spin_lock_init(&fhci->lock);
347 /* connect the virtual root hub */
348 fhci->vroot_hub->dev_num = 1; /* this field may be needed to fix */
349 fhci->vroot_hub->hub.wHubStatus = 0;
350 fhci->vroot_hub->hub.wHubChange = 0;
351 fhci->vroot_hub->port.wPortStatus = 0;
352 fhci->vroot_hub->port.wPortChange = 0;
354 hcd->state = HC_STATE_RUNNING;
357 * From here on, hub_wq concurrently accesses the root
358 * hub; drivers will be talking to enumerated devices.
359 * (On restart paths, hub_wq already knows about the root
360 * hub and could find work as soon as we wrote FLAG_CF.)
362 * Before this point the HC was idle/ready. After, hub_wq
363 * and device drivers may start it running.
365 fhci_usb_enable(fhci);
372 static void fhci_stop(struct usb_hcd *hcd)
374 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
376 fhci_usb_disable_interrupt(fhci->usb_lld);
377 fhci_usb_disable(fhci);
379 fhci_usb_free(fhci->usb_lld);
380 fhci->usb_lld = NULL;
384 static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
387 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
388 u32 pipe = urb->pipe;
392 struct urb_priv *urb_priv;
395 switch (usb_pipetype(pipe)) {
397 /* 1 td fro setup,1 for ack */
400 /* one td for every 4096 bytes(can be up to 8k) */
401 size += urb->transfer_buffer_length / 4096;
402 /* ...add for any remaining bytes... */
403 if ((urb->transfer_buffer_length % 4096) != 0)
405 /* ..and maybe a zero length packet to wrap it up */
408 else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
409 && (urb->transfer_buffer_length
410 % usb_maxpacket(urb->dev, pipe,
411 usb_pipeout(pipe))) != 0)
414 case PIPE_ISOCHRONOUS:
415 size = urb->number_of_packets;
418 for (i = 0; i < urb->number_of_packets; i++) {
419 urb->iso_frame_desc[i].actual_length = 0;
420 urb->iso_frame_desc[i].status = (u32) (-EXDEV);
427 /* allocate the private part of the URB */
428 urb_priv = kzalloc(sizeof(*urb_priv), mem_flags);
432 /* allocate the private part of the URB */
433 urb_priv->tds = kcalloc(size, sizeof(*urb_priv->tds), mem_flags);
434 if (!urb_priv->tds) {
439 spin_lock_irqsave(&fhci->lock, flags);
441 ret = usb_hcd_link_urb_to_ep(hcd, urb);
445 /* fill the private part of the URB */
446 urb_priv->num_of_tds = size;
448 urb->status = -EINPROGRESS;
449 urb->actual_length = 0;
450 urb->error_count = 0;
451 urb->hcpriv = urb_priv;
453 fhci_queue_urb(fhci, urb);
456 kfree(urb_priv->tds);
459 spin_unlock_irqrestore(&fhci->lock, flags);
463 /* dequeue FHCI URB */
464 static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
466 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
467 struct fhci_usb *usb = fhci->usb_lld;
471 if (!urb || !urb->dev || !urb->dev->bus)
474 spin_lock_irqsave(&fhci->lock, flags);
476 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
480 if (usb->port_status != FHCI_PORT_DISABLED) {
481 struct urb_priv *urb_priv;
484 * flag the urb's data for deletion in some upcoming
485 * SF interrupt's delete list processing
487 urb_priv = urb->hcpriv;
489 if (!urb_priv || (urb_priv->state == URB_DEL))
492 urb_priv->state = URB_DEL;
494 /* already pending? */
495 urb_priv->ed->state = FHCI_ED_URB_DEL;
497 fhci_urb_complete_free(fhci, urb);
501 spin_unlock_irqrestore(&fhci->lock, flags);
506 static void fhci_endpoint_disable(struct usb_hcd *hcd,
507 struct usb_host_endpoint *ep)
509 struct fhci_hcd *fhci;
513 fhci = hcd_to_fhci(hcd);
514 spin_lock_irqsave(&fhci->lock, flags);
517 while (ed->td_head != NULL) {
518 struct td *td = fhci_remove_td_from_ed(ed);
519 fhci_urb_complete_free(fhci, td->urb);
521 fhci_recycle_empty_ed(fhci, ed);
524 spin_unlock_irqrestore(&fhci->lock, flags);
527 static int fhci_get_frame_number(struct usb_hcd *hcd)
529 struct fhci_hcd *fhci = hcd_to_fhci(hcd);
531 return get_frame_num(fhci);
534 static const struct hc_driver fhci_driver = {
535 .description = "fsl,usb-fhci",
536 .product_desc = "FHCI HOST Controller",
537 .hcd_priv_size = sizeof(struct fhci_hcd),
539 /* generic hardware linkage */
541 .flags = HCD_DMA | HCD_USB11 | HCD_MEMORY,
543 /* basic lifecycle operation */
547 /* managing i/o requests and associated device resources */
548 .urb_enqueue = fhci_urb_enqueue,
549 .urb_dequeue = fhci_urb_dequeue,
550 .endpoint_disable = fhci_endpoint_disable,
552 /* scheduling support */
553 .get_frame_number = fhci_get_frame_number,
555 /* root hub support */
556 .hub_status_data = fhci_hub_status_data,
557 .hub_control = fhci_hub_control,
560 static int of_fhci_probe(struct platform_device *ofdev)
562 struct device *dev = &ofdev->dev;
563 struct device_node *node = dev->of_node;
565 struct fhci_hcd *fhci;
566 struct resource usb_regs;
567 unsigned long pram_addr;
568 unsigned int usb_irq;
579 sprop = of_get_property(node, "mode", NULL);
580 if (sprop && strcmp(sprop, "host"))
583 hcd = usb_create_hcd(&fhci_driver, dev, dev_name(dev));
585 dev_err(dev, "could not create hcd\n");
589 fhci = hcd_to_fhci(hcd);
590 hcd->self.controller = dev;
591 dev_set_drvdata(dev, hcd);
593 iprop = of_get_property(node, "hub-power-budget", &size);
594 if (iprop && size == sizeof(*iprop))
595 hcd->power_budget = *iprop;
597 /* FHCI registers. */
598 ret = of_address_to_resource(node, 0, &usb_regs);
600 dev_err(dev, "could not get regs\n");
604 hcd->regs = ioremap(usb_regs.start, resource_size(&usb_regs));
606 dev_err(dev, "could not ioremap regs\n");
610 fhci->regs = hcd->regs;
613 iprop = of_get_property(node, "reg", &size);
614 if (!iprop || size < sizeof(*iprop) * 4) {
615 dev_err(dev, "can't get pram offset\n");
620 pram_addr = cpm_muram_alloc(FHCI_PRAM_SIZE, 64);
621 if (IS_ERR_VALUE(pram_addr)) {
622 dev_err(dev, "failed to allocate usb pram\n");
627 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, QE_CR_SUBBLOCK_USB,
628 QE_CR_PROTOCOL_UNSPECIFIED, pram_addr);
629 fhci->pram = cpm_muram_addr(pram_addr);
632 for (i = 0; i < NUM_GPIOS; i++) {
634 enum of_gpio_flags flags;
636 gpio = of_get_gpio_flags(node, i, &flags);
637 fhci->gpios[i] = gpio;
638 fhci->alow_gpios[i] = flags & OF_GPIO_ACTIVE_LOW;
640 if (!gpio_is_valid(gpio)) {
641 if (i < GPIO_SPEED) {
642 dev_err(dev, "incorrect GPIO%d: %d\n",
646 dev_info(dev, "assuming board doesn't have "
647 "%s gpio\n", i == GPIO_SPEED ?
653 ret = gpio_request(gpio, dev_name(dev));
655 dev_err(dev, "failed to request gpio %d", i);
659 if (i >= GPIO_SPEED) {
660 ret = gpio_direction_output(gpio, 0);
662 dev_err(dev, "failed to set gpio %d as "
670 for (j = 0; j < NUM_PINS; j++) {
671 fhci->pins[j] = qe_pin_request(node, j);
672 if (IS_ERR(fhci->pins[j])) {
673 ret = PTR_ERR(fhci->pins[j]);
674 dev_err(dev, "can't get pin %d: %d\n", j, ret);
679 /* Frame limit timer and its interrupt. */
680 fhci->timer = gtm_get_timer16();
681 if (IS_ERR(fhci->timer)) {
682 ret = PTR_ERR(fhci->timer);
683 dev_err(dev, "failed to request qe timer: %i", ret);
687 ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
688 0, "qe timer (usb)", hcd);
690 dev_err(dev, "failed to request timer irq");
694 /* USB Host interrupt. */
695 usb_irq = irq_of_parse_and_map(node, 0);
696 if (usb_irq == NO_IRQ) {
697 dev_err(dev, "could not get usb irq\n");
703 sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
705 fhci->fullspeed_clk = qe_clock_source(sprop);
706 if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
707 dev_err(dev, "wrong fullspeed-clock\n");
713 sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
715 fhci->lowspeed_clk = qe_clock_source(sprop);
716 if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
717 dev_err(dev, "wrong lowspeed-clock\n");
723 if (fhci->fullspeed_clk == QE_CLK_NONE &&
724 fhci->lowspeed_clk == QE_CLK_NONE) {
725 dev_err(dev, "no clocks specified\n");
730 dev_info(dev, "at 0x%p, irq %d\n", hcd->regs, usb_irq);
732 fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
734 /* Start with full-speed, if possible. */
735 if (fhci->fullspeed_clk != QE_CLK_NONE) {
736 fhci_config_transceiver(fhci, FHCI_PORT_FULL);
737 qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
739 fhci_config_transceiver(fhci, FHCI_PORT_LOW);
740 qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
743 /* Clear and disable any pending interrupts. */
744 out_be16(&fhci->regs->usb_usber, 0xffff);
745 out_be16(&fhci->regs->usb_usbmr, 0);
747 ret = usb_add_hcd(hcd, usb_irq, 0);
751 device_wakeup_enable(hcd->self.controller);
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 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 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 platform_driver of_fhci_driver = {
817 .name = "fsl,usb-fhci",
818 .of_match_table = of_fhci_match,
820 .probe = of_fhci_probe,
821 .remove = of_fhci_remove,
824 module_platform_driver(of_fhci_driver);
826 MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
827 MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
828 "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
829 "Anton Vorontsov <avorontsov@ru.mvista.com>");
830 MODULE_LICENSE("GPL");