22 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <linux/types.h>
26 #include <linux/errno.h>
28 #include <linux/slab.h>
30 #include <linux/list.h>
35 #include <linux/device.h>
36 #include <linux/usb/ch9.h>
45 #include <asm/byteorder.h>
47 #include <asm/unaligned.h>
52 #define DRIVER_DESC "Freescale High-Speed USB SOC Device Controller driver"
53 #define DRIVER_AUTHOR "Li Yang/Jiang Bo"
54 #define DRIVER_VERSION "Apr 20, 2007"
56 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
72 .bEndpointAddress = 0,
77 static void fsl_ep_fifo_flush(
struct usb_ep *_ep);
113 static u32 (*_fsl_readl)(
const unsigned __iomem *
p);
116 #define fsl_readl(p) (*_fsl_readl)((p))
117 #define fsl_writel(v, p) (*_fsl_writel)((v), (p))
122 _fsl_readl = _fsl_readl_be;
123 _fsl_writel = _fsl_writel_be;
125 _fsl_readl = _fsl_readl_le;
126 _fsl_writel = _fsl_writel_le;
132 return udc_controller->
pdata->big_endian_desc
139 return udc_controller->
pdata->big_endian_desc
146 #define fsl_readl(addr) readl(addr)
147 #define fsl_writel(val32, addr) writel(val32, addr)
148 #define cpu_to_hc32(x) cpu_to_le32(x)
149 #define hc32_to_cpu(x) le32_to_cpu(x)
169 list_del_init(&req->
queue);
175 status = req->
req.status;
189 req->
req.dma, req->
req.length,
197 req->
req.dma, req->
req.length,
203 VDBG(
"complete %s req %p stat %d len %u/%u",
204 ep->
ep.name, &req->
req, status,
205 req->
req.actual, req->
req.length);
209 spin_unlock(&ep->
udc->lock);
212 if (req->
req.complete)
213 req->
req.complete(&ep->
ep, &req->
req);
215 spin_lock(&ep->
udc->lock);
228 fsl_ep_fifo_flush(&ep->
ep);
231 while (!list_empty(&ep->
queue)) {
235 done(ep, req, status);
243 static int dr_controller_setup(
struct fsl_udc *
udc)
245 unsigned int tmp, portctrl, ep_num;
246 unsigned int max_no_of_ep;
250 #define FSL_UDC_RESET_TIMEOUT 1000
257 if (udc->
pdata->have_sysif_regs) {
258 if (udc->
pdata->controller_ver) {
272 if (udc->
pdata->have_sysif_regs) {
273 if (udc->
pdata->controller_ver) {
306 ERR(
"udc reset timeout!\n");
329 VDBG(
"vir[qh_base] is %p phy[qh_base] is 0x%8x reg is 0x%8x",
330 udc->
ep_qh, (
int)tmp,
334 for (ep_num = 1; ep_num < max_no_of_ep; ep_num++) {
342 #ifndef CONFIG_ARCH_MXC
343 if (udc->
pdata->have_sysif_regs) {
350 #if defined(CONFIG_PPC32) && !defined(CONFIG_NOT_COHERENT_CACHE)
354 if (udc->
pdata->have_sysif_regs) {
367 static void dr_controller_run(
struct fsl_udc *udc)
392 static void dr_controller_stop(
struct fsl_udc *udc)
424 static void dr_ep_setup(
unsigned char ep_num,
unsigned char dir,
425 unsigned char ep_type)
427 unsigned int tmp_epctrl = 0;
435 tmp_epctrl |= ((
unsigned int)(ep_type)
442 tmp_epctrl |= ((
unsigned int)(ep_type)
450 dr_ep_change_stall(
unsigned char ep_num,
unsigned char dir,
int value)
477 static int dr_ep_get_stall(
unsigned char ep_num,
unsigned char dir)
497 static void struct_ep_qh_setup(
struct fsl_udc *udc,
unsigned char ep_num,
498 unsigned char dir,
unsigned char ep_type,
499 unsigned int max_pkt_len,
500 unsigned int zlt,
unsigned char mult)
503 unsigned int tmp = 0;
521 VDBG(
"error ep type is %d", ep_type);
533 static void ep0_setup(
struct fsl_udc *udc)
558 static int fsl_ep_enable(
struct usb_ep *_ep,
563 unsigned short max = 0;
564 unsigned char mult = 0, zlt;
566 unsigned long flags = 0;
580 max = usb_endpoint_maxp(desc);
600 mult = (
unsigned char)(1 + ((max >> 11) & 0x03));
611 ep->
ep.maxpacket =
max;
618 struct_ep_qh_setup(udc, (
unsigned char)
ep_index(ep),
626 dr_ep_setup((
unsigned char)
ep_index(ep),
632 spin_unlock_irqrestore(&udc->
lock, flags);
635 VDBG(
"enabled %s (ep%d%s) maxpacket %d",ep->
ep.name,
636 ep->
ep.desc->bEndpointAddress & 0x0f,
638 ?
"in" :
"out", max);
647 static int fsl_ep_disable(
struct usb_ep *_ep)
651 unsigned long flags = 0;
656 if (!_ep || !ep->
ep.desc) {
657 VDBG(
"%s not enabled", _ep ? ep->
ep.name :
NULL);
681 spin_unlock_irqrestore(&udc->
lock, flags);
693 fsl_alloc_request(
struct usb_ep *_ep,
gfp_t gfp_flags)
697 req = kzalloc(
sizeof *req, gfp_flags);
702 INIT_LIST_HEAD(&req->
queue);
739 static void fsl_queue_td(
struct fsl_ep *ep,
struct fsl_req *req)
755 lastreq->
tail->next_td_ptr =
781 fsl_prime_ep(ep, req->
head);
797 *length =
min(req->
req.length - req->
req.actual,
811 swap_temp = (
u32) (req->
req.dma + req->
req.actual);
822 if (*length == 0 || (*length % req->
ep->ep.maxpacket) != 0)
826 }
else if (req->
req.length == req->
req.actual)
832 VDBG(
"multi-dtd request!");
837 if (*is_last && !req->
req.no_interrupt)
844 VDBG(
"length = %d address= 0x%x", *length, (
int)*dma);
850 static int fsl_req_to_dtd(
struct fsl_req *req,
gfp_t gfp_flags)
859 dtd = fsl_build_dtd(req, &count, &dma, &is_last, gfp_flags);
892 if (!_req || !req->
req.complete || !req->
req.buf
893 || !list_empty(&req->
queue)) {
894 VDBG(
"%s, bad params", __func__);
898 VDBG(
"%s, bad ep", __func__);
901 if (usb_endpoint_xfer_isoc(ep->
ep.desc)) {
902 if (req->
req.length > ep->
ep.maxpacket)
922 req->
req.dma, req->
req.length,
934 if (!fsl_req_to_dtd(req, gfp_flags)) {
936 fsl_queue_td(ep, req);
944 spin_unlock_irqrestore(&udc->
lock, flags);
976 if (&req->
req == _req)
979 if (&req->
req != _req) {
987 fsl_ep_fifo_flush(_ep);
997 fsl_prime_ep(ep, next_req->
head);
1004 prev_req->
tail->next_td_ptr = req->
tail->next_td_ptr;
1018 spin_unlock_irqrestore(&ep->
udc->lock, flags);
1030 static int fsl_ep_set_halt(
struct usb_ep *_ep,
int value)
1033 unsigned long flags = 0;
1035 unsigned char ep_dir = 0, ep_num = 0;
1040 if (!_ep || !ep->
ep.desc) {
1045 if (usb_endpoint_xfer_isoc(ep->
ep.desc)) {
1061 dr_ep_change_stall(ep_num, ep_dir, value);
1062 spin_unlock_irqrestore(&ep->
udc->lock, flags);
1069 VDBG(
" %s %s halt stat %d", ep->
ep.name,
1070 value ?
"set" :
"clear", status);
1075 static int fsl_ep_fifo_status(
struct usb_ep *_ep)
1084 if (!_ep || (!ep->
ep.desc &&
ep_index(ep) != 0))
1092 qh = get_qh_by_ep(ep);
1101 pr_debug(
"%s %u\n", __func__, size);
1105 static void fsl_ep_fifo_flush(
struct usb_ep *_ep)
1110 unsigned long timeout;
1111 #define FSL_UDC_FLUSH_TIMEOUT 1000
1124 bits = (1 << 16) | 1;
1126 bits = 1 << (16 + ep_num);
1137 ERR(
"ep flush timeout\n");
1147 .enable = fsl_ep_enable,
1148 .disable = fsl_ep_disable,
1150 .alloc_request = fsl_alloc_request,
1151 .free_request = fsl_free_request,
1153 .queue = fsl_ep_queue,
1154 .dequeue = fsl_ep_dequeue,
1156 .set_halt = fsl_ep_set_halt,
1157 .fifo_status = fsl_ep_fifo_status,
1158 .fifo_flush = fsl_ep_fifo_flush,
1168 static int fsl_get_frame(
struct usb_gadget *gadget)
1176 static int fsl_wakeup(
struct usb_gadget *gadget)
1195 static int can_pullup(
struct fsl_udc *udc)
1205 unsigned long flags;
1209 VDBG(
"VBUS %s", is_active ?
"on" :
"off");
1211 if (can_pullup(udc))
1217 spin_unlock_irqrestore(&udc->
lock, flags);
1228 static int fsl_vbus_draw(
struct usb_gadget *gadget,
unsigned mA)
1241 static int fsl_pullup(
struct usb_gadget *gadget,
int is_on)
1247 if (can_pullup(udc))
1262 .get_frame = fsl_get_frame,
1263 .wakeup = fsl_wakeup,
1265 .vbus_session = fsl_vbus_session,
1266 .vbus_draw = fsl_vbus_draw,
1267 .pullup = fsl_pullup,
1274 static void ep0stall(
struct fsl_udc *udc)
1302 req->
req.length = 0;
1304 req->
req.actual = 0;
1309 req->
req.buf, req->
req.length,
1314 fsl_queue_td(ep, req);
1323 static void udc_reset_ep_queue(
struct fsl_udc *udc,
u8 pipe)
1367 struct fsl_ep *target_ep;
1372 if (!target_ep->
ep.desc)
1385 req->
req.length = 2;
1387 req->
req.actual = 0;
1392 req->
req.buf, req->
req.length,
1398 fsl_queue_td(ep, req);
1412 static void setup_received_irq(
struct fsl_udc *udc,
1419 udc_reset_ep_queue(udc, 0);
1428 ch9getstatus(udc, setup->
bRequestType, wValue, wIndex, wLength);
1436 ch9setaddress(udc, wValue, wIndex, wLength);
1451 if (wValue != 0 || wLength != 0 || pipe >= udc->
max_ep)
1455 spin_unlock(&udc->
lock);
1456 rc = fsl_ep_set_halt(&ep->
ep,
1459 spin_lock(&udc->
lock);
1468 else if (gadget_is_otg(&udc->
gadget)) {
1471 udc->
gadget.b_hnp_enable = 1;
1474 udc->
gadget.a_hnp_support = 1;
1477 udc->
gadget.a_alt_hnp_support = 1;
1508 spin_unlock(&udc->
lock);
1512 spin_lock(&udc->
lock);
1526 spin_unlock(&udc->
lock);
1530 spin_lock(&udc->
lock);
1537 static void ep0_req_complete(
struct fsl_udc *udc,
struct fsl_ep *ep0,
1563 ERR(
"Unexpect ep0 packets\n");
1573 static void tripwire_handler(
struct fsl_udc *udc,
u8 ep_num,
u8 *buffer_ptr)
1610 static int process_ep_req(
struct fsl_udc *udc,
int pipe,
1614 int td_complete, actual, remaining_length,
j,
tmp;
1618 int direction = pipe % 2;
1620 curr_td = curr_req->
head;
1622 actual = curr_req->
req.length;
1624 for (j = 0; j < curr_req->
dtd_count; j++) {
1628 actual -= remaining_length;
1633 ERR(
"dTD error %08x QH=%d\n", errors, pipe);
1644 VDBG(
"Transfer overflow");
1652 ERR(
"Unknown error has occurred (0x%x)!\n",
1657 VDBG(
"Request not complete");
1660 }
else if (remaining_length) {
1662 VDBG(
"Transmit dTD remaining length not zero");
1671 VDBG(
"dTD transmitted successful");
1681 curr_req->
req.actual = actual;
1687 static void dtd_complete_irq(
struct fsl_udc *udc)
1692 struct fsl_req *curr_req, *temp_req;
1701 for (i = 0; i < udc->
max_ep; i++) {
1705 bit_mask = 1 << (ep_num + 16 *
direction);
1707 if (!(bit_pos & bit_mask))
1721 status = process_ep_req(udc, i, curr_req);
1723 VDBG(
"status of process_ep_req= %d, ep = %d",
1731 ep0_req_complete(udc, curr_ep, curr_req);
1734 done(curr_ep, curr_req, status);
1754 static void port_change_irq(
struct fsl_udc *udc)
1771 static void suspend_irq(
struct fsl_udc *udc)
1777 if (udc->
driver->suspend)
1781 static void bus_resume(
struct fsl_udc *udc)
1792 static int reset_queues(
struct fsl_udc *udc)
1796 for (pipe = 0; pipe < udc->
max_pipes; pipe++)
1797 udc_reset_ep_queue(udc, pipe);
1800 spin_unlock(&udc->
lock);
1802 spin_lock(&udc->
lock);
1808 static void reset_irq(
struct fsl_udc *udc)
1811 unsigned long timeout;
1824 udc->
gadget.b_hnp_enable = 0;
1825 udc->
gadget.a_hnp_support = 0;
1826 udc->
gadget.a_alt_hnp_support = 0;
1840 ERR(
"Timeout for reset\n");
1858 VDBG(
"Controller reset");
1861 dr_controller_setup(udc);
1869 dr_controller_run(udc);
1877 static irqreturn_t fsl_udc_irq(
int irq,
void *_udc)
1882 unsigned long flags;
1904 tripwire_handler(udc, 0,
1912 dtd_complete_irq(udc);
1924 port_change_irq(udc);
1942 VDBG(
"Error IRQ %x", irq_src);
1945 spin_unlock_irqrestore(&udc->
lock, flags);
1957 unsigned long flags = 0;
1959 if (!udc_controller)
1966 if (udc_controller->
driver)
1976 spin_unlock_irqrestore(&udc_controller->
lock, flags);
1979 retval = bind(&udc_controller->
gadget, driver);
1981 VDBG(
"bind to %s --> %d", driver->
driver.name, retval);
1987 if (!IS_ERR_OR_NULL(udc_controller->
transceiver)) {
1993 if (!IS_ERR_OR_NULL(udc_controller->
transceiver)) {
1994 retval = otg_set_peripheral(
1996 &udc_controller->
gadget);
1998 ERR(
"can't bind to transceiver\n");
2000 udc_controller->
gadget.dev.driver = 0;
2001 udc_controller->
driver = 0;
2007 dr_controller_run(udc_controller);
2026 unsigned long flags;
2028 if (!udc_controller)
2031 if (!driver || driver != udc_controller->
driver || !driver->
unbind)
2038 dr_controller_stop(udc_controller);
2052 spin_unlock_irqrestore(&udc_controller->lock, flags);
2058 driver->unbind(&udc_controller->gadget);
2059 udc_controller->gadget.
dev.driver =
NULL;
2060 udc_controller->driver = NULL;
2063 driver->driver.
name);
2070 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2074 static const char proc_filename[] =
"driver/fsl_usb2_udc";
2076 static int fsl_proc_read(
char *
page,
char **
start,
off_t off,
int count,
2077 int *eof,
void *_dev)
2081 unsigned size =
count;
2082 unsigned long flags;
2088 struct fsl_udc *udc = udc_controller;
2098 "Gadget driver: %s\n\n",
2118 "Dr Suspend: %d Reset Received: %d System Error: %s "
2119 "USB Error Interrupt: %s\n\n",
2120 (tmp_reg & USB_STS_SUSPEND) ? 1 : 0,
2121 (tmp_reg & USB_STS_RESET) ? 1 : 0,
2123 (tmp_reg &
USB_STS_ERR) ?
"Err detected" :
"No err");
2129 "USB Intrrupt Enable Reg:\n"
2130 "Sleep Enable: %d SOF Received Enable: %d "
2131 "Reset Enable: %d\n"
2132 "System Error Enable: %d "
2133 "Port Change Dectected Enable: %d\n"
2134 "USB Error Intr Enable: %d USB Intr Enable: %d\n\n",
2147 "USB Frame Index Reg: Frame Number is 0x%x\n\n",
2154 "USB Device Address Reg: Device Addr is 0x%x\n\n",
2161 "USB Endpoint List Address Reg: "
2162 "Device Addr is 0x%x\n\n",
2169 "USB Port Status&Control Reg:\n"
2170 "Port Transceiver Type : %s Port Speed: %s\n"
2171 "PHY Low Power Suspend: %s Port Reset: %s "
2172 "Port Suspend Mode: %s\n"
2173 "Over-current Change: %s "
2174 "Port Enable/Disable Change: %s\n"
2175 "Port Enabled/Disabled: %s "
2176 "Current Connect Status: %s\n\n", ( {
2183 case PORTSCX_PTS_FSLS:
2184 s =
"FS/LS Serial";
break;
2191 "Normal PHY mode" :
"Low power mode",
2202 "Attached" :
"Not-Att");
2208 "USB Mode Reg: Controller Mode is: %s\n\n", ( {
2214 s =
"Device Controller";
break;
2215 case USB_MODE_CTRL_MODE_HOST:
2216 s =
"Host Controller";
break;
2227 "Endpoint Setup Status Reg: SETUP on ep 0x%x\n\n",
2232 for (i = 0; i < udc->
max_ep / 2; i++) {
2234 t =
scnprintf(next, size,
"EP Ctrl Reg [0x%x]: = [0x%x]\n",
2240 t =
scnprintf(next, size,
"EP Prime Reg = [0x%x]\n\n", tmp_reg);
2244 #ifndef CONFIG_ARCH_MXC
2245 if (udc->
pdata->have_sysif_regs) {
2246 tmp_reg = usb_sys_regs->
snoop1;
2247 t =
scnprintf(next, size,
"Snoop1 Reg : = [0x%x]\n\n", tmp_reg);
2251 tmp_reg = usb_sys_regs->
control;
2252 t =
scnprintf(next, size,
"General Control Reg : = [0x%x]\n\n",
2261 t =
scnprintf(next, size,
"For %s Maxpkt is 0x%x index is 0x%x\n",
2266 if (list_empty(&ep->
queue)) {
2267 t =
scnprintf(next, size,
"its req queue is empty\n\n");
2273 "req %p actual 0x%x length 0x%x buf %p\n",
2274 &req->
req, req->
req.actual,
2275 req->
req.length, req->
req.buf);
2284 "\nFor %s Maxpkt is 0x%x "
2291 if (list_empty(&ep->
queue)) {
2293 "its req queue is empty\n\n");
2299 "req %p actual 0x%x length "
2301 &req->
req, req->
req.actual,
2302 req->
req.length, req->
req.buf);
2310 spin_unlock_irqrestore(&udc->
lock, flags);
2313 return count -
size;
2316 #define create_proc_file() create_proc_read_entry(proc_filename, \
2317 0, NULL, fsl_proc_read, NULL)
2319 #define remove_proc_file() remove_proc_entry(proc_filename, NULL)
2323 #define create_proc_file() do {} while (0)
2324 #define remove_proc_file() do {} while (0)
2331 static void fsl_udc_release(
struct device *
dev)
2336 kfree(udc_controller);
2352 pdata = pdev->
dev.platform_data;
2357 ERR(
"malloc fsl_ep failed\n");
2366 size += QH_ALIGNMENT + 1;
2367 size &= ~(QH_ALIGNMENT - 1);
2372 ERR(
"malloc QHs for udc failed\n");
2400 static int __init struct_ep_setup(
struct fsl_udc *udc,
unsigned char index,
2401 char *name,
int link)
2409 ep->
ep.ops = &fsl_ep_ops;
2415 ep->
ep.maxpacket = (
unsigned short) ~0;
2418 INIT_LIST_HEAD(&ep->
queue);
2442 VDBG(
"Wrong device");
2447 if (udc_controller == NULL) {
2448 ERR(
"malloc udc failed\n");
2452 pdata = pdev->
dev.platform_data;
2457 #ifdef CONFIG_USB_OTG
2460 if (IS_ERR_OR_NULL(udc_controller->
transceiver)) {
2461 ERR(
"Can't find OTG driver!\n");
2477 ERR(
"request mem region for %s failed\n", pdev->
name);
2486 goto err_release_mem_region;
2489 pdata->
regs = (
void *)dr_regs;
2494 if (pdata->
init && pdata->
init(pdev)) {
2496 goto err_iounmap_noclk;
2500 fsl_set_accessors(pdata);
2502 #ifndef CONFIG_ARCH_MXC
2510 goto err_iounmap_noclk;
2515 ERR(
"This SOC doesn't support device role\n");
2524 if (!udc_controller->
irq) {
2532 ERR(
"cannot request irq %d err %d\n",
2533 udc_controller->
irq, ret);
2538 if (struct_udc_setup(udc_controller, pdev)) {
2539 ERR(
"Can't initialize udc data structure\n");
2544 if (IS_ERR_OR_NULL(udc_controller->
transceiver)) {
2547 dr_controller_setup(udc_controller);
2553 udc_controller->
gadget.ops = &fsl_gadget_ops;
2555 udc_controller->
gadget.ep0 = &udc_controller->
eps[0].ep;
2556 INIT_LIST_HEAD(&udc_controller->
gadget.ep_list);
2562 udc_controller->
gadget.dev.release = fsl_udc_release;
2563 udc_controller->
gadget.dev.parent = &pdev->
dev;
2564 udc_controller->
gadget.dev.of_node = pdev->
dev.of_node;
2570 udc_controller->
gadget.is_otg = 1;
2573 ep0_setup(udc_controller);
2576 struct_ep_setup(udc_controller, 0,
"ep0", 0);
2580 udc_controller->
eps[0].ep.desc = &fsl_ep0_desc;
2585 for (i = 1; i < (
int)(udc_controller->
max_ep / 2); i++) {
2589 struct_ep_setup(udc_controller, i * 2, name, 1);
2591 struct_ep_setup(udc_controller, i * 2 + 1, name, 1);
2598 if (udc_controller->
td_pool == NULL) {
2600 goto err_unregister;
2622 err_release_mem_region:
2626 kfree(udc_controller);
2627 udc_controller =
NULL;
2641 if (!udc_controller)
2683 dr_controller_stop(udc_controller);
2694 if (udc_controller->
stopped) {
2695 dr_controller_setup(udc_controller);
2696 dr_controller_run(udc_controller);
2706 struct fsl_udc *udc = udc_controller;
2711 pr_debug(
"%s(): mode 0x%x stopped %d\n", __func__, mode, udc->
stopped);
2719 pr_debug(
"gadget already stopped, leaving early\n");
2725 pr_debug(
"gadget not in device mode, leaving early\n");
2735 pr_info(
"USB Gadget suspended\n");
2740 static int fsl_udc_otg_resume(
struct device *dev)
2742 pr_debug(
"%s(): stopped %d already_stopped %d\n", __func__,
2751 pr_debug(
"gadget was already stopped, leaving early\n");
2755 pr_info(
"USB Gadget resume\n");
2757 return fsl_udc_resume(NULL);
2765 .remove =
__exit_p(fsl_udc_remove),
2767 .suspend = fsl_udc_suspend,
2768 .resume = fsl_udc_resume,
2773 .
suspend = fsl_udc_otg_suspend,
2774 .
resume = fsl_udc_otg_resume,
2778 static int __init udc_init(
void)
2786 static void __exit udc_exit(
void)