15 #define pr_fmt(fmt) "s3c2410_udc: " fmt
17 #include <linux/module.h>
18 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/errno.h>
26 #include <linux/list.h>
31 #include <linux/prefetch.h>
40 #include <asm/byteorder.h>
42 #include <asm/unaligned.h>
43 #include <mach/irqs.h>
45 #include <mach/hardware.h>
53 #define DRIVER_DESC "S3C2410 USB Device Controller Gadget"
54 #define DRIVER_VERSION "29 Apr 2007"
58 static const char gadget_name[] =
"s3c2410_udc";
62 static struct clk *udc_clock;
63 static struct clk *usb_bus_clock;
65 static u64 rsrc_start;
67 static struct dentry *s3c2410_udc_debugfs_root;
71 return readb(base_addr + reg);
76 writeb(value, base_addr + reg);
87 #define DEBUG_NORMAL 1
88 #define DEBUG_VERBOSE 2
90 #ifdef CONFIG_USB_S3C2410_DEBUG
91 #define USB_S3C2410_DEBUG_LEVEL 0
97 static char printk_buf[1024];
98 static long prevticks;
99 static int invocation;
103 if (level > USB_S3C2410_DEBUG_LEVEL)
106 if (s3c2410_ticks != prevticks) {
107 prevticks = s3c2410_ticks;
112 sizeof(printk_buf),
"%1lu.%02d USB: ",
113 prevticks, invocation++);
117 sizeof(printk_buf)-len, fmt, args);
128 static int s3c2410_udc_debugfs_seq_show(
struct seq_file *
m,
void *
p)
130 u32 addr_reg, pwr_reg, ep_int_reg, usb_int_reg;
131 u32 ep_int_en_reg, usb_int_en_reg, ep0_csr;
132 u32 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2;
133 u32 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2;
156 "EP_INT_REG : 0x%04X\n"
157 "USB_INT_REG : 0x%04X\n"
158 "EP_INT_EN_REG : 0x%04X\n"
159 "USB_INT_EN_REG : 0x%04X\n"
161 "EP1_I_CSR1 : 0x%04X\n"
162 "EP1_I_CSR2 : 0x%04X\n"
163 "EP1_O_CSR1 : 0x%04X\n"
164 "EP1_O_CSR2 : 0x%04X\n"
165 "EP2_I_CSR1 : 0x%04X\n"
166 "EP2_I_CSR2 : 0x%04X\n"
167 "EP2_O_CSR1 : 0x%04X\n"
168 "EP2_O_CSR2 : 0x%04X\n",
169 addr_reg, pwr_reg, ep_int_reg, usb_int_reg,
170 ep_int_en_reg, usb_int_en_reg, ep0_csr,
171 ep1_i_csr1, ep1_i_csr2, ep1_o_csr1, ep1_o_csr2,
172 ep2_i_csr1, ep2_i_csr2, ep2_o_csr1, ep2_o_csr2
178 static int s3c2410_udc_debugfs_fops_open(
struct inode *
inode,
185 .open = s3c2410_udc_debugfs_fops_open,
194 static inline void s3c2410_udc_clear_ep0_opr(
void __iomem *base)
201 static inline void s3c2410_udc_clear_ep0_sst(
void __iomem *base)
207 static inline void s3c2410_udc_clear_ep0_se(
void __iomem *base)
213 static inline void s3c2410_udc_set_ep0_ipr(
void __iomem *base)
219 static inline void s3c2410_udc_set_ep0_de(
void __iomem *base)
231 static inline void s3c2410_udc_set_ep0_de_out(
void __iomem *base)
240 static inline void s3c2410_udc_set_ep0_sse_out(
void __iomem *base)
248 static inline void s3c2410_udc_set_ep0_de_in(
void __iomem *base)
261 static void s3c2410_udc_done(
struct s3c2410_ep *ep,
264 unsigned halted = ep->
halted;
266 list_del_init(&req->
queue);
271 status = req->
req.status;
274 req->
req.complete(&ep->
ep, &req->
req);
285 while (!list_empty(&ep->
queue)) {
289 s3c2410_udc_done(ep, req, status);
293 static inline void s3c2410_udc_clear_ep_state(
struct s3c2410_udc *
dev)
305 static inline int s3c2410_udc_fifo_count_out(
void)
317 static inline int s3c2410_udc_write_packet(
int fifo,
321 unsigned len =
min(req->
req.length - req->
req.actual, max);
327 req->
req.actual, req->
req.length, len, req->
req.actual + len);
329 req->
req.actual += len;
332 writesb(base_addr + fifo, buf, len);
341 static int s3c2410_udc_write_fifo(
struct s3c2410_ep *ep,
371 count = s3c2410_udc_write_packet(fifo_reg, req, ep->
ep.maxpacket);
374 if (count != ep->
ep.maxpacket)
376 else if (req->
req.length != req->
req.actual || req->
req.zero)
384 "Written ep%d %d.%d of %d b [last %d,z %d]\n",
385 idx, count, req->
req.actual, req->
req.length,
386 is_last, req->
req.zero);
396 s3c2410_udc_set_ep0_de_in(base_addr);
406 s3c2410_udc_done(ep, req, 0);
413 s3c2410_udc_set_ep0_ipr(base_addr);
426 static inline int s3c2410_udc_read_packet(
int fifo,
u8 *
buf,
431 len =
min(req->
req.length - req->
req.actual, avail);
432 req->
req.actual += len;
434 readsb(fifo + base_addr, buf, len);
441 static int s3c2410_udc_read_fifo(
struct s3c2410_ep *ep,
446 unsigned bufferspace;
475 if (!req->
req.length)
478 buf = req->
req.buf + req->
req.actual;
479 bufferspace = req->
req.length - req->
req.actual;
487 fifo_count = s3c2410_udc_fifo_count_out();
490 if (fifo_count > ep->
ep.maxpacket)
491 avail = ep->
ep.maxpacket;
495 fifo_count = s3c2410_udc_read_packet(fifo_reg, buf, req, avail);
500 if (idx != 0 && fifo_count < ep->ep.maxpacket) {
503 if (fifo_count != avail)
506 is_last = (req->
req.length <= req->
req.actual) ? 1 : 0;
510 fifo_count = s3c2410_udc_fifo_count_out();
515 __func__, fifo_count, is_last);
519 s3c2410_udc_set_ep0_de_out(base_addr);
529 s3c2410_udc_done(ep, req, 0);
532 s3c2410_udc_clear_ep0_opr(base_addr);
547 unsigned char *outbuf = (
unsigned char *)crq;
552 bytes_read = s3c2410_udc_fifo_count_out();
568 static int s3c2410_udc_get_status(
struct s3c2410_udc *dev,
584 if (ep_num > 4 || crq->
wLength > 2)
602 status = status ? 1 : 0;
613 s3c2410_udc_set_ep0_de_in(base_addr);
618 static int s3c2410_udc_set_halt(
struct usb_ep *_ep,
int value);
620 static void s3c2410_udc_handle_ep0_idle(
struct s3c2410_udc *dev,
631 s3c2410_udc_nuke(dev, ep, -
EPROTO);
633 len = s3c2410_udc_read_fifo_crq(crq);
634 if (len !=
sizeof(*crq)) {
636 " wanted %d bytes got %d. Stalling out...\n",
657 s3c2410_udc_set_ep0_de_out(base_addr);
666 s3c2410_udc_set_ep0_de_out(base_addr);
678 s3c2410_udc_set_ep0_de_out(base_addr);
685 s3c2410_udc_clear_ep0_opr(base_addr);
688 if (!s3c2410_udc_get_status(dev, crq))
694 s3c2410_udc_clear_ep0_opr(base_addr);
702 s3c2410_udc_set_halt(&dev->
ep[crq->
wIndex & 0x7f].ep, 0);
703 s3c2410_udc_set_ep0_de_out(base_addr);
707 s3c2410_udc_clear_ep0_opr(base_addr);
715 s3c2410_udc_set_halt(&dev->
ep[crq->
wIndex & 0x7f].ep, 1);
716 s3c2410_udc_set_ep0_de_out(base_addr);
720 s3c2410_udc_clear_ep0_opr(base_addr);
745 "dev->driver->setup failed. (%d)\n", ret);
749 s3c2410_udc_set_ep0_de_out(base_addr);
760 static void s3c2410_udc_handle_ep0(
struct s3c2410_udc *dev)
767 if (list_empty(&ep->
queue))
783 s3c2410_udc_nuke(dev, ep, -
EPIPE);
785 s3c2410_udc_clear_ep0_sst(base_addr);
793 s3c2410_udc_nuke(dev, ep, 0);
794 s3c2410_udc_clear_ep0_se(base_addr);
800 s3c2410_udc_handle_ep0_idle(dev, ep, &crq, ep0csr);
806 s3c2410_udc_write_fifo(ep, req);
811 if ((ep0csr & S3C2410_UDC_EP0_CSR_OPKRDY) && req)
812 s3c2410_udc_read_fifo(ep, req);
831 static void s3c2410_udc_handle_ep(
struct s3c2410_ep *ep)
850 idx, ep_csr1, req ? 1 : 0);
855 udc_write(ep_csr1 & ~S3C2410_UDC_ICSR1_SENTSTL,
861 s3c2410_udc_write_fifo(ep, req);
869 udc_write(ep_csr1 & ~S3C2410_UDC_OCSR1_SENTSTL,
875 s3c2410_udc_read_fifo(ep, req);
879 #include <mach/regs-irq.h>
918 usb_status, usbd_status, pwr_reg, ep0csr);
937 udc_write((dev->
ep[0].ep.maxpacket & 0x7ff) >> 3,
945 udc_write(S3C2410_UDC_USBINT_RESET,
949 spin_unlock_irqrestore(&dev->
lock, flags);
958 udc_write(S3C2410_UDC_USBINT_RESUME,
972 udc_write(S3C2410_UDC_USBINT_SUSPEND,
992 s3c2410_udc_handle_ep0(dev);
998 if (usbd_status & tmp) {
1003 s3c2410_udc_handle_ep(&dev->
ep[i]);
1008 if (!usb_status && !usbd_status && !pwr_reg && !ep0csr) {
1014 s3c2410_udc_handle_ep(&dev->
ep[i]);
1026 spin_unlock_irqrestore(&dev->
lock, flags);
1050 static int s3c2410_udc_ep_enable(
struct usb_ep *_ep,
1056 unsigned long flags;
1060 ep = to_s3c2410_ep(_ep);
1063 || _ep->
name == ep0name
1071 max = usb_endpoint_maxp(desc) & 0x1fff;
1122 s3c2410_udc_set_halt(_ep, 0);
1130 static int s3c2410_udc_ep_disable(
struct usb_ep *_ep)
1133 unsigned long flags;
1136 if (!_ep || !ep->
ep.desc) {
1138 _ep ? ep->
ep.name :
NULL);
1166 s3c2410_udc_alloc_request(
struct usb_ep *_ep,
gfp_t mem_flags)
1179 INIT_LIST_HEAD(&req->
queue);
1194 if (!ep || !_req || (!ep->
ep.desc && _ep->
name != ep0name))
1212 unsigned long flags;
1214 if (
unlikely(!_ep || (!ep->
ep.desc && ep->
ep.name != ep0name))) {
1228 || !_req->
buf || !list_empty(&req->
queue))) {
1234 !list_empty(&req->
queue));
1253 fifo_count = s3c2410_udc_fifo_count_out();
1257 fifo_count = s3c2410_udc_fifo_count_out();
1265 if (!(ep_csr&S3C2410_UDC_EP0_CSR_IPKRDY)
1266 && s3c2410_udc_write_fifo(ep,
1276 && s3c2410_udc_read_fifo(ep,
1289 && s3c2410_udc_write_fifo(ep, req)) {
1291 }
else if ((ep_csr & S3C2410_UDC_OCSR1_PKTRDY)
1293 && s3c2410_udc_read_fifo(ep, req)) {
1316 unsigned long flags;
1321 if (!the_controller->
driver)
1327 udc = to_s3c2410_udc(ep->
gadget);
1332 if (&req->
req == _req) {
1333 list_del_init(&req->
queue);
1342 "dequeued req %p from %s, len %d buf %p\n",
1355 static int s3c2410_udc_set_halt(
struct usb_ep *_ep,
int value)
1359 unsigned long flags;
1362 if (
unlikely(!_ep || (!ep->
ep.desc && ep->
ep.name != ep0name))) {
1373 s3c2410_udc_set_ep0_de_out(base_addr);
1403 ep->
halted = value ? 1 : 0;
1409 static const struct usb_ep_ops s3c2410_ep_ops = {
1410 .enable = s3c2410_udc_ep_enable,
1411 .disable = s3c2410_udc_ep_disable,
1413 .alloc_request = s3c2410_udc_alloc_request,
1414 .free_request = s3c2410_udc_free_request,
1416 .queue = s3c2410_udc_queue,
1417 .dequeue = s3c2410_udc_dequeue,
1419 .set_halt = s3c2410_udc_set_halt,
1427 static int s3c2410_udc_get_frame(
struct usb_gadget *_gadget)
1441 static int s3c2410_udc_wakeup(
struct usb_gadget *_gadget)
1450 static int s3c2410_udc_set_selfpowered(
struct usb_gadget *gadget,
int value)
1464 static void s3c2410_udc_disable(
struct s3c2410_udc *dev);
1465 static void s3c2410_udc_enable(
struct s3c2410_udc *dev);
1467 static int s3c2410_udc_set_pullup(
struct s3c2410_udc *udc,
int is_on)
1475 s3c2410_udc_enable(udc);
1482 s3c2410_udc_disable(udc);
1497 udc->
vbus = (is_active != 0);
1498 s3c2410_udc_set_pullup(udc, is_active);
1502 static int s3c2410_udc_pullup(
struct usb_gadget *gadget,
int is_on)
1508 s3c2410_udc_set_pullup(udc, is_on ? 0 : 1);
1512 static irqreturn_t s3c2410_udc_vbus_irq(
int irq,
void *_dev)
1523 if (value != dev->
vbus)
1524 s3c2410_udc_vbus_session(&dev->
gadget, value);
1529 static int s3c2410_vbus_draw(
struct usb_gadget *_gadget,
unsigned ma)
1546 .get_frame = s3c2410_udc_get_frame,
1547 .wakeup = s3c2410_udc_wakeup,
1548 .set_selfpowered = s3c2410_udc_set_selfpowered,
1549 .pullup = s3c2410_udc_pullup,
1550 .vbus_session = s3c2410_udc_vbus_session,
1551 .vbus_draw = s3c2410_vbus_draw,
1552 .start = s3c2410_udc_start,
1553 .stop = s3c2410_udc_stop,
1563 }
else if (gpio_is_valid(udc_info->
pullup_pin)) {
1586 static void s3c2410_udc_disable(
struct s3c2410_udc *dev)
1595 udc_write(S3C2410_UDC_USBINT_RESET
1596 | S3C2410_UDC_USBINT_RESUME
1597 | S3C2410_UDC_USBINT_SUSPEND,
1612 static void s3c2410_udc_reinit(
struct s3c2410_udc *dev)
1617 INIT_LIST_HEAD(&dev->
gadget.ep_list);
1618 INIT_LIST_HEAD(&dev->
gadget.ep0->ep_list);
1630 INIT_LIST_HEAD(&ep->
queue);
1637 static void s3c2410_udc_enable(
struct s3c2410_udc *dev)
1649 udc_write((dev->
ep[i].ep.maxpacket & 0x7ff) >> 3,
1657 udc_write(S3C2410_UDC_USBINT_RESET | S3C2410_UDC_USBINT_SUSPEND,
1683 dev_err(&udc->
gadget.dev,
"Invalid driver: bind %p setup %p speed %d\n",
1689 dev_err(&udc->
gadget.dev,
"Invalid driver: no unbind method\n");
1701 dev_err(&udc->
gadget.dev,
"Error in device_add() : %d\n", retval);
1702 goto register_error;
1708 retval = bind(&udc->
gadget, driver);
1711 goto register_error;
1715 s3c2410_udc_enable(udc);
1732 if (!driver || driver != udc->
driver || !driver->
unbind)
1748 s3c2410_udc_disable(udc);
1756 .ops = &s3c2410_ops,
1757 .ep0 = &memory.
ep[0].ep,
1758 .name = gadget_name,
1760 .init_name =
"gadget",
1769 .ops = &s3c2410_ep_ops,
1780 .ops = &s3c2410_ep_ops,
1785 .bEndpointAddress = 1,
1792 .ops = &s3c2410_ep_ops,
1797 .bEndpointAddress = 2,
1804 .ops = &s3c2410_ep_ops,
1809 .bEndpointAddress = 3,
1816 .ops = &s3c2410_ep_ops,
1821 .bEndpointAddress = 4,
1837 dev_dbg(dev,
"%s()\n", __func__);
1840 if (IS_ERR(usb_bus_clock)) {
1841 dev_err(dev,
"failed to get usb bus clock source\n");
1842 return PTR_ERR(usb_bus_clock);
1848 if (IS_ERR(udc_clock)) {
1849 dev_err(dev,
"failed to get udc clock source\n");
1850 return PTR_ERR(udc_clock);
1857 dev_dbg(dev,
"got and enabled clocks\n");
1860 dev_info(dev,
"S3C2440: increasing FIFO to 128 bytes\n");
1868 udc_info = pdev->
dev.platform_data;
1876 base_addr =
ioremap(rsrc_start, rsrc_len);
1884 udc->
gadget.dev.dma_mask = pdev->
dev.dma_mask;
1886 the_controller = udc;
1887 platform_set_drvdata(pdev, udc);
1889 s3c2410_udc_disable(udc);
1890 s3c2410_udc_reinit(udc);
1894 0, gadget_name, udc);
1904 if (udc_info && udc_info->
vbus_pin > 0) {
1907 dev_err(dev,
"cannot claim vbus pin\n");
1913 dev_err(dev,
"no irq for gpio vbus pin\n");
1914 goto err_gpio_claim;
1923 dev_err(dev,
"can't get vbus irq %d, err %d\n",
1926 goto err_gpio_claim;
1929 dev_dbg(dev,
"got irq %i\n", irq);
1949 if (s3c2410_udc_debugfs_root) {
1951 s3c2410_udc_debugfs_root,
1952 udc, &s3c2410_udc_debugfs_fops);
1954 dev_warn(dev,
"debugfs file creation failed\n");
1966 if (udc_info && udc_info->
vbus_pin > 0)
1969 if (udc_info && udc_info->
vbus_pin > 0)
1986 struct s3c2410_udc *udc = platform_get_drvdata(pdev);
2001 if (udc_info && udc_info->
vbus_pin > 0) {
2011 platform_set_drvdata(pdev,
NULL);
2013 if (!IS_ERR(udc_clock) && udc_clock !=
NULL) {
2019 if (!IS_ERR(usb_bus_clock) && usb_bus_clock !=
NULL) {
2022 usb_bus_clock =
NULL;
2025 dev_dbg(&pdev->
dev,
"%s: remove ok\n", __func__);
2045 #define s3c2410_udc_suspend NULL
2046 #define s3c2410_udc_resume NULL
2050 {
"s3c2410-usbgadget", },
2051 {
"s3c2440-usbgadget", },
2058 .name =
"s3c24x0-usbgadget",
2061 .probe = s3c2410_udc_probe,
2062 .remove = s3c2410_udc_remove,
2065 .id_table = s3c_udc_ids,
2068 static int __init udc_init(
void)
2075 if (IS_ERR(s3c2410_udc_debugfs_root)) {
2076 pr_err(
"%s: debugfs dir creation failed %ld\n",
2077 gadget_name, PTR_ERR(s3c2410_udc_debugfs_root));
2078 s3c2410_udc_debugfs_root =
NULL;
2092 static void __exit udc_exit(
void)