Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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