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>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/platform_device.h>
29 #include <linux/slab.h>
30 #include <linux/gpio/consumer.h>
31 #include <soc/fsl/qe/qe.h>
32 #include <asm/fsl_gtm.h>
35 void fhci_start_sof_timer(struct fhci_hcd
*fhci
)
37 fhci_dbg(fhci
, "-> %s\n", __func__
);
40 out_be16(&fhci
->pram
->frame_num
, 0);
42 out_be16(&fhci
->regs
->usb_ussft
, 0);
43 setbits8(&fhci
->regs
->usb_usmod
, USB_MODE_SFTE
);
45 fhci_dbg(fhci
, "<- %s\n", __func__
);
48 void fhci_stop_sof_timer(struct fhci_hcd
*fhci
)
50 fhci_dbg(fhci
, "-> %s\n", __func__
);
52 clrbits8(&fhci
->regs
->usb_usmod
, USB_MODE_SFTE
);
53 gtm_stop_timer16(fhci
->timer
);
55 fhci_dbg(fhci
, "<- %s\n", __func__
);
58 u16
fhci_get_sof_timer_count(struct fhci_usb
*usb
)
60 return be16_to_cpu(in_be16(&usb
->fhci
->regs
->usb_ussft
) / 12);
63 /* initialize the endpoint zero */
64 static u32
endpoint_zero_init(struct fhci_usb
*usb
,
65 enum fhci_mem_alloc data_mem
,
70 rc
= fhci_create_ep(usb
, data_mem
, ring_len
);
74 /* inilialize endpoint registers */
75 fhci_init_ep_registers(usb
, usb
->ep0
, data_mem
);
80 /* enable the USB interrupts */
81 void fhci_usb_enable_interrupt(struct fhci_usb
*usb
)
83 struct fhci_hcd
*fhci
= usb
->fhci
;
85 if (usb
->intr_nesting_cnt
== 1) {
86 /* initialize the USB interrupt */
87 enable_irq(fhci_to_hcd(fhci
)->irq
);
89 /* initialize the event register and mask register */
90 out_be16(&usb
->fhci
->regs
->usb_usber
, 0xffff);
91 out_be16(&usb
->fhci
->regs
->usb_usbmr
, usb
->saved_msk
);
93 /* enable the timer interrupts */
94 enable_irq(fhci
->timer
->irq
);
95 } else if (usb
->intr_nesting_cnt
> 1)
96 fhci_info(fhci
, "unbalanced USB interrupts nesting\n");
97 usb
->intr_nesting_cnt
--;
100 /* disable the usb interrupt */
101 void fhci_usb_disable_interrupt(struct fhci_usb
*usb
)
103 struct fhci_hcd
*fhci
= usb
->fhci
;
105 if (usb
->intr_nesting_cnt
== 0) {
106 /* disable the timer interrupt */
107 disable_irq_nosync(fhci
->timer
->irq
);
109 /* disable the usb interrupt */
110 disable_irq_nosync(fhci_to_hcd(fhci
)->irq
);
111 out_be16(&usb
->fhci
->regs
->usb_usbmr
, 0);
113 usb
->intr_nesting_cnt
++;
116 /* enable the USB controller */
117 static u32
fhci_usb_enable(struct fhci_hcd
*fhci
)
119 struct fhci_usb
*usb
= fhci
->usb_lld
;
121 out_be16(&usb
->fhci
->regs
->usb_usber
, 0xffff);
122 out_be16(&usb
->fhci
->regs
->usb_usbmr
, usb
->saved_msk
);
123 setbits8(&usb
->fhci
->regs
->usb_usmod
, USB_MODE_EN
);
130 /* disable the USB controller */
131 static u32
fhci_usb_disable(struct fhci_hcd
*fhci
)
133 struct fhci_usb
*usb
= fhci
->usb_lld
;
135 fhci_usb_disable_interrupt(usb
);
136 fhci_port_disable(fhci
);
138 /* disable the usb controller */
139 if (usb
->port_status
== FHCI_PORT_FULL
||
140 usb
->port_status
== FHCI_PORT_LOW
)
141 fhci_device_disconnected_interrupt(fhci
);
143 clrbits8(&usb
->fhci
->regs
->usb_usmod
, USB_MODE_EN
);
148 /* check the bus state by polling the QE bit on the IO ports */
149 int fhci_ioports_check_bus_state(struct fhci_hcd
*fhci
)
153 /* check USBOE,if transmitting,exit */
154 if (!gpiod_get_value(fhci
->gpiods
[GPIO_USBOE
]))
158 if (gpiod_get_value(fhci
->gpiods
[GPIO_USBRP
]))
162 if (gpiod_get_value(fhci
->gpiods
[GPIO_USBRN
]))
168 static void fhci_mem_free(struct fhci_hcd
*fhci
)
175 list_for_each_entry_safe(ed
, next_ed
, &fhci
->empty_eds
, node
) {
180 list_for_each_entry_safe(td
, next_td
, &fhci
->empty_tds
, node
) {
185 kfree(fhci
->vroot_hub
);
186 fhci
->vroot_hub
= NULL
;
188 kfree(fhci
->hc_list
);
189 fhci
->hc_list
= NULL
;
192 static int fhci_mem_init(struct fhci_hcd
*fhci
)
196 fhci
->hc_list
= kzalloc(sizeof(*fhci
->hc_list
), GFP_KERNEL
);
200 INIT_LIST_HEAD(&fhci
->hc_list
->ctrl_list
);
201 INIT_LIST_HEAD(&fhci
->hc_list
->bulk_list
);
202 INIT_LIST_HEAD(&fhci
->hc_list
->iso_list
);
203 INIT_LIST_HEAD(&fhci
->hc_list
->intr_list
);
204 INIT_LIST_HEAD(&fhci
->hc_list
->done_list
);
206 fhci
->vroot_hub
= kzalloc(sizeof(*fhci
->vroot_hub
), GFP_KERNEL
);
207 if (!fhci
->vroot_hub
)
210 INIT_LIST_HEAD(&fhci
->empty_eds
);
211 INIT_LIST_HEAD(&fhci
->empty_tds
);
213 /* initialize work queue to handle done list */
214 fhci_tasklet
.data
= (unsigned long)fhci
;
215 fhci
->process_done_task
= &fhci_tasklet
;
217 for (i
= 0; i
< MAX_TDS
; i
++) {
220 td
= kmalloc(sizeof(*td
), GFP_KERNEL
);
223 fhci_recycle_empty_td(fhci
, td
);
225 for (i
= 0; i
< MAX_EDS
; i
++) {
228 ed
= kmalloc(sizeof(*ed
), GFP_KERNEL
);
231 fhci_recycle_empty_ed(fhci
, ed
);
234 fhci
->active_urbs
= 0;
241 /* destroy the fhci_usb structure */
242 static void fhci_usb_free(void *lld
)
244 struct fhci_usb
*usb
= lld
;
245 struct fhci_hcd
*fhci
;
249 fhci_config_transceiver(fhci
, FHCI_PORT_POWER_OFF
);
251 kfree(usb
->actual_frame
);
256 /* initialize the USB */
257 static int fhci_usb_init(struct fhci_hcd
*fhci
)
259 struct fhci_usb
*usb
= fhci
->usb_lld
;
261 memset_io(usb
->fhci
->pram
, 0, FHCI_PRAM_SIZE
);
263 usb
->port_status
= FHCI_PORT_DISABLED
;
264 usb
->max_frame_usage
= FRAME_TIME_USAGE
;
265 usb
->sw_transaction_time
= SW_FIX_TIME_BETWEEN_TRANSACTION
;
267 usb
->actual_frame
= kzalloc(sizeof(*usb
->actual_frame
), GFP_KERNEL
);
268 if (!usb
->actual_frame
) {
273 INIT_LIST_HEAD(&usb
->actual_frame
->tds_list
);
275 /* initializing registers on chip, clear frame number */
276 out_be16(&fhci
->pram
->frame_num
, 0);
279 out_be32(&fhci
->pram
->rx_state
, 0);
281 /* set mask register */
282 usb
->saved_msk
= (USB_E_TXB_MASK
|
285 USB_E_RESET_MASK
| USB_E_SFT_MASK
| USB_E_MSF_MASK
);
287 out_8(&usb
->fhci
->regs
->usb_usmod
, USB_MODE_HOST
| USB_MODE_EN
);
289 /* clearing the mask register */
290 out_be16(&usb
->fhci
->regs
->usb_usbmr
, 0);
292 /* initialing the event register */
293 out_be16(&usb
->fhci
->regs
->usb_usber
, 0xffff);
295 if (endpoint_zero_init(usb
, DEFAULT_DATA_MEM
, DEFAULT_RING_LEN
) != 0) {
303 /* initialize the fhci_usb struct and the corresponding data staruct */
304 static struct fhci_usb
*fhci_create_lld(struct fhci_hcd
*fhci
)
306 struct fhci_usb
*usb
;
308 /* allocate memory for SCC data structure */
309 usb
= kzalloc(sizeof(*usb
), GFP_KERNEL
);
314 usb
->hc_list
= fhci
->hc_list
;
315 usb
->vroot_hub
= fhci
->vroot_hub
;
317 usb
->transfer_confirm
= fhci_transfer_confirm_callback
;
322 static int fhci_start(struct usb_hcd
*hcd
)
325 struct fhci_hcd
*fhci
= hcd_to_fhci(hcd
);
327 ret
= fhci_mem_init(fhci
);
329 fhci_err(fhci
, "failed to allocate memory\n");
333 fhci
->usb_lld
= fhci_create_lld(fhci
);
334 if (!fhci
->usb_lld
) {
335 fhci_err(fhci
, "low level driver config failed\n");
340 ret
= fhci_usb_init(fhci
);
342 fhci_err(fhci
, "low level driver initialize failed\n");
346 spin_lock_init(&fhci
->lock
);
348 /* connect the virtual root hub */
349 fhci
->vroot_hub
->dev_num
= 1; /* this field may be needed to fix */
350 fhci
->vroot_hub
->hub
.wHubStatus
= 0;
351 fhci
->vroot_hub
->hub
.wHubChange
= 0;
352 fhci
->vroot_hub
->port
.wPortStatus
= 0;
353 fhci
->vroot_hub
->port
.wPortChange
= 0;
355 hcd
->state
= HC_STATE_RUNNING
;
358 * From here on, hub_wq concurrently accesses the root
359 * hub; drivers will be talking to enumerated devices.
360 * (On restart paths, hub_wq already knows about the root
361 * hub and could find work as soon as we wrote FLAG_CF.)
363 * Before this point the HC was idle/ready. After, hub_wq
364 * and device drivers may start it running.
366 fhci_usb_enable(fhci
);
373 static void fhci_stop(struct usb_hcd
*hcd
)
375 struct fhci_hcd
*fhci
= hcd_to_fhci(hcd
);
377 fhci_usb_disable_interrupt(fhci
->usb_lld
);
378 fhci_usb_disable(fhci
);
380 fhci_usb_free(fhci
->usb_lld
);
381 fhci
->usb_lld
= NULL
;
385 static int fhci_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
388 struct fhci_hcd
*fhci
= hcd_to_fhci(hcd
);
389 u32 pipe
= urb
->pipe
;
393 struct urb_priv
*urb_priv
;
396 switch (usb_pipetype(pipe
)) {
398 /* 1 td fro setup,1 for ack */
402 /* one td for every 4096 bytes(can be up to 8k) */
403 size
+= urb
->transfer_buffer_length
/ 4096;
404 /* ...add for any remaining bytes... */
405 if ((urb
->transfer_buffer_length
% 4096) != 0)
407 /* ..and maybe a zero length packet to wrap it up */
410 else if ((urb
->transfer_flags
& URB_ZERO_PACKET
) != 0
411 && (urb
->transfer_buffer_length
412 % usb_maxpacket(urb
->dev
, pipe
)) != 0)
415 case PIPE_ISOCHRONOUS
:
416 size
= urb
->number_of_packets
;
419 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
420 urb
->iso_frame_desc
[i
].actual_length
= 0;
421 urb
->iso_frame_desc
[i
].status
= (u32
) (-EXDEV
);
428 /* allocate the private part of the URB */
429 urb_priv
= kzalloc(sizeof(*urb_priv
), mem_flags
);
433 /* allocate the private part of the URB */
434 urb_priv
->tds
= kcalloc(size
, sizeof(*urb_priv
->tds
), mem_flags
);
435 if (!urb_priv
->tds
) {
440 spin_lock_irqsave(&fhci
->lock
, flags
);
442 ret
= usb_hcd_link_urb_to_ep(hcd
, urb
);
446 /* fill the private part of the URB */
447 urb_priv
->num_of_tds
= size
;
449 urb
->status
= -EINPROGRESS
;
450 urb
->actual_length
= 0;
451 urb
->error_count
= 0;
452 urb
->hcpriv
= urb_priv
;
454 fhci_queue_urb(fhci
, urb
);
457 kfree(urb_priv
->tds
);
460 spin_unlock_irqrestore(&fhci
->lock
, flags
);
464 /* dequeue FHCI URB */
465 static int fhci_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
467 struct fhci_hcd
*fhci
= hcd_to_fhci(hcd
);
468 struct fhci_usb
*usb
= fhci
->usb_lld
;
472 if (!urb
|| !urb
->dev
|| !urb
->dev
->bus
)
475 spin_lock_irqsave(&fhci
->lock
, flags
);
477 ret
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
481 if (usb
->port_status
!= FHCI_PORT_DISABLED
) {
482 struct urb_priv
*urb_priv
;
485 * flag the urb's data for deletion in some upcoming
486 * SF interrupt's delete list processing
488 urb_priv
= urb
->hcpriv
;
490 if (!urb_priv
|| (urb_priv
->state
== URB_DEL
))
493 urb_priv
->state
= URB_DEL
;
495 /* already pending? */
496 urb_priv
->ed
->state
= FHCI_ED_URB_DEL
;
498 fhci_urb_complete_free(fhci
, urb
);
502 spin_unlock_irqrestore(&fhci
->lock
, flags
);
507 static void fhci_endpoint_disable(struct usb_hcd
*hcd
,
508 struct usb_host_endpoint
*ep
)
510 struct fhci_hcd
*fhci
;
514 fhci
= hcd_to_fhci(hcd
);
515 spin_lock_irqsave(&fhci
->lock
, flags
);
518 while (ed
->td_head
!= NULL
) {
519 struct td
*td
= fhci_remove_td_from_ed(ed
);
520 fhci_urb_complete_free(fhci
, td
->urb
);
522 fhci_recycle_empty_ed(fhci
, ed
);
525 spin_unlock_irqrestore(&fhci
->lock
, flags
);
528 static int fhci_get_frame_number(struct usb_hcd
*hcd
)
530 struct fhci_hcd
*fhci
= hcd_to_fhci(hcd
);
532 return get_frame_num(fhci
);
535 static const struct hc_driver fhci_driver
= {
536 .description
= "fsl,usb-fhci",
537 .product_desc
= "FHCI HOST Controller",
538 .hcd_priv_size
= sizeof(struct fhci_hcd
),
540 /* generic hardware linkage */
542 .flags
= HCD_DMA
| HCD_USB11
| HCD_MEMORY
,
544 /* basic lifecycle operation */
548 /* managing i/o requests and associated device resources */
549 .urb_enqueue
= fhci_urb_enqueue
,
550 .urb_dequeue
= fhci_urb_dequeue
,
551 .endpoint_disable
= fhci_endpoint_disable
,
553 /* scheduling support */
554 .get_frame_number
= fhci_get_frame_number
,
556 /* root hub support */
557 .hub_status_data
= fhci_hub_status_data
,
558 .hub_control
= fhci_hub_control
,
561 static int of_fhci_probe(struct platform_device
*ofdev
)
563 struct device
*dev
= &ofdev
->dev
;
564 struct device_node
*node
= dev
->of_node
;
566 struct fhci_hcd
*fhci
;
567 struct resource usb_regs
;
568 unsigned long pram_addr
;
569 unsigned int usb_irq
;
580 sprop
= of_get_property(node
, "mode", NULL
);
581 if (sprop
&& strcmp(sprop
, "host"))
584 hcd
= usb_create_hcd(&fhci_driver
, dev
, dev_name(dev
));
586 dev_err(dev
, "could not create hcd\n");
590 fhci
= hcd_to_fhci(hcd
);
591 hcd
->self
.controller
= dev
;
592 dev_set_drvdata(dev
, hcd
);
594 iprop
= of_get_property(node
, "hub-power-budget", &size
);
595 if (iprop
&& size
== sizeof(*iprop
))
596 hcd
->power_budget
= *iprop
;
598 /* FHCI registers. */
599 ret
= of_address_to_resource(node
, 0, &usb_regs
);
601 dev_err(dev
, "could not get regs\n");
605 hcd
->regs
= ioremap(usb_regs
.start
, resource_size(&usb_regs
));
607 dev_err(dev
, "could not ioremap regs\n");
611 fhci
->regs
= hcd
->regs
;
614 iprop
= of_get_property(node
, "reg", &size
);
615 if (!iprop
|| size
< sizeof(*iprop
) * 4) {
616 dev_err(dev
, "can't get pram offset\n");
621 pram_addr
= cpm_muram_alloc(FHCI_PRAM_SIZE
, 64);
622 if (IS_ERR_VALUE(pram_addr
)) {
623 dev_err(dev
, "failed to allocate usb pram\n");
628 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE
, QE_CR_SUBBLOCK_USB
,
629 QE_CR_PROTOCOL_UNSPECIFIED
, pram_addr
);
630 fhci
->pram
= cpm_muram_addr(pram_addr
);
633 for (i
= 0; i
< NUM_GPIOS
; i
++) {
635 fhci
->gpiods
[i
] = devm_gpiod_get_index(dev
,
639 fhci
->gpiods
[i
] = devm_gpiod_get_index_optional(dev
,
640 NULL
, i
, GPIOD_OUT_LOW
);
642 if (IS_ERR(fhci
->gpiods
[i
])) {
643 dev_err(dev
, "incorrect GPIO%d: %ld\n",
644 i
, PTR_ERR(fhci
->gpiods
[i
]));
647 if (!fhci
->gpiods
[i
]) {
648 dev_info(dev
, "assuming board doesn't have "
649 "%s gpio\n", i
== GPIO_SPEED
?
654 for (j
= 0; j
< NUM_PINS
; j
++) {
655 fhci
->pins
[j
] = qe_pin_request(dev
, j
);
656 if (IS_ERR(fhci
->pins
[j
])) {
657 ret
= PTR_ERR(fhci
->pins
[j
]);
658 dev_err(dev
, "can't get pin %d: %d\n", j
, ret
);
663 /* Frame limit timer and its interrupt. */
664 fhci
->timer
= gtm_get_timer16();
665 if (IS_ERR(fhci
->timer
)) {
666 ret
= PTR_ERR(fhci
->timer
);
667 dev_err(dev
, "failed to request qe timer: %i", ret
);
671 ret
= request_irq(fhci
->timer
->irq
, fhci_frame_limit_timer_irq
,
672 0, "qe timer (usb)", hcd
);
674 dev_err(dev
, "failed to request timer irq");
678 /* USB Host interrupt. */
679 usb_irq
= irq_of_parse_and_map(node
, 0);
681 dev_err(dev
, "could not get usb irq\n");
687 sprop
= of_get_property(node
, "fsl,fullspeed-clock", NULL
);
689 fhci
->fullspeed_clk
= qe_clock_source(sprop
);
690 if (fhci
->fullspeed_clk
== QE_CLK_DUMMY
) {
691 dev_err(dev
, "wrong fullspeed-clock\n");
697 sprop
= of_get_property(node
, "fsl,lowspeed-clock", NULL
);
699 fhci
->lowspeed_clk
= qe_clock_source(sprop
);
700 if (fhci
->lowspeed_clk
== QE_CLK_DUMMY
) {
701 dev_err(dev
, "wrong lowspeed-clock\n");
707 if (fhci
->fullspeed_clk
== QE_CLK_NONE
&&
708 fhci
->lowspeed_clk
== QE_CLK_NONE
) {
709 dev_err(dev
, "no clocks specified\n");
714 dev_info(dev
, "at 0x%p, irq %d\n", hcd
->regs
, usb_irq
);
716 fhci_config_transceiver(fhci
, FHCI_PORT_POWER_OFF
);
718 /* Start with full-speed, if possible. */
719 if (fhci
->fullspeed_clk
!= QE_CLK_NONE
) {
720 fhci_config_transceiver(fhci
, FHCI_PORT_FULL
);
721 qe_usb_clock_set(fhci
->fullspeed_clk
, USB_CLOCK
);
723 fhci_config_transceiver(fhci
, FHCI_PORT_LOW
);
724 qe_usb_clock_set(fhci
->lowspeed_clk
, USB_CLOCK
>> 3);
727 /* Clear and disable any pending interrupts. */
728 out_be16(&fhci
->regs
->usb_usber
, 0xffff);
729 out_be16(&fhci
->regs
->usb_usbmr
, 0);
731 ret
= usb_add_hcd(hcd
, usb_irq
, 0);
735 device_wakeup_enable(hcd
->self
.controller
);
737 fhci_dfs_create(fhci
);
743 irq_dispose_mapping(usb_irq
);
745 free_irq(fhci
->timer
->irq
, hcd
);
747 gtm_put_timer16(fhci
->timer
);
751 qe_pin_free(fhci
->pins
[j
]);
753 cpm_muram_free(pram_addr
);
761 static void fhci_remove(struct device
*dev
)
763 struct usb_hcd
*hcd
= dev_get_drvdata(dev
);
764 struct fhci_hcd
*fhci
= hcd_to_fhci(hcd
);
768 free_irq(fhci
->timer
->irq
, hcd
);
769 gtm_put_timer16(fhci
->timer
);
770 cpm_muram_free(cpm_muram_offset(fhci
->pram
));
771 for (j
= 0; j
< NUM_PINS
; j
++)
772 qe_pin_free(fhci
->pins
[j
]);
773 fhci_dfs_destroy(fhci
);
777 static void of_fhci_remove(struct platform_device
*ofdev
)
779 fhci_remove(&ofdev
->dev
);
782 static const struct of_device_id of_fhci_match
[] = {
783 { .compatible
= "fsl,mpc8323-qe-usb", },
786 MODULE_DEVICE_TABLE(of
, of_fhci_match
);
788 static struct platform_driver of_fhci_driver
= {
790 .name
= "fsl,usb-fhci",
791 .of_match_table
= of_fhci_match
,
793 .probe
= of_fhci_probe
,
794 .remove
= of_fhci_remove
,
797 module_platform_driver(of_fhci_driver
);
799 MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
800 MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
801 "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
802 "Anton Vorontsov <avorontsov@ru.mvista.com>");
803 MODULE_LICENSE("GPL");