14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
25 #include <asm/unaligned.h>
26 #include <asm/cacheflush.h>
53 #define DEFAULT_I_TDPS 1024
63 static inline struct isp1760_hcd *hcd_to_priv(
struct usb_hcd *hcd)
69 #define HC_LENGTH(p) (((p)>>00)&0x00ff)
70 #define HC_VERSION(p) (((p)>>16)&0xffff)
71 #define HCS_INDICATOR(p) ((p)&(1 << 16))
72 #define HCS_PPC(p) ((p)&(1 << 4))
73 #define HCS_N_PORTS(p) (((p)>>0)&0xf)
74 #define HCC_ISOC_CACHE(p) ((p)&(1 << 7))
75 #define HCC_ISOC_THRES(p) (((p)>>4)&0x7)
78 #define CMD_LRESET (1<<7)
79 #define CMD_RESET (1<<1)
80 #define CMD_RUN (1<<0)
81 #define STS_PCD (1<<2)
82 #define FLAG_CF (1<<0)
84 #define PORT_OWNER (1<<13)
85 #define PORT_POWER (1<<12)
86 #define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10))
87 #define PORT_RESET (1<<8)
88 #define PORT_SUSPEND (1<<7)
89 #define PORT_RESUME (1<<6)
90 #define PORT_PE (1<<2)
91 #define PORT_CSC (1<<1)
92 #define PORT_CONNECT (1<<0)
93 #define PORT_RWC_BITS (PORT_CSC)
112 #define QTD_ENQUEUED 0
113 #define QTD_PAYLOAD_ALLOC 1
114 #define QTD_XFER_STARTED 2
115 #define QTD_XFER_COMPLETE 3
140 return readl(base + reg);
161 static void bank_reads8(
void __iomem *src_base,
u32 src_offset,
u32 bank_addr,
169 src = src_base + (bank_addr | src_offset);
198 dst_byteptr = (
void *) dst;
199 src_byteptr = (
void *) &val;
201 *dst_byteptr = *src_byteptr;
208 static void mem_reads8(
void __iomem *src_base,
u32 src_offset,
void *dst,
213 bank_reads8(src_base, src_offset,
ISP_BANK(0), dst, bytes);
216 static void mem_writes8(
void __iomem *dst_base,
u32 dst_offset,
221 dst = dst_base + dst_offset;
259 ISP_BANK(0) + ptd_offset + slot*
sizeof(*ptd));
261 bank_reads8(base, ptd_offset + slot*
sizeof(*ptd),
ISP_BANK(0),
262 (
void *) ptd,
sizeof(*ptd));
265 static void ptd_write(
void __iomem *base,
u32 ptd_offset,
u32 slot,
268 mem_writes8(base, ptd_offset + slot*
sizeof(*ptd) +
sizeof(ptd->
dw0),
269 &ptd->
dw1, 7*
sizeof(ptd->
dw1));
273 mem_writes8(base, ptd_offset + slot*
sizeof(*ptd), &ptd->
dw0,
311 static void alloc_mem(
struct usb_hcd *hcd,
struct isp1760_qtd *
qtd)
321 for (i = 0; i <
BLOCKS; i++) {
331 static void free_mem(
struct usb_hcd *hcd,
struct isp1760_qtd *qtd)
339 for (i = 0; i <
BLOCKS; i++) {
348 dev_err(hcd->self.controller,
"%s: Invalid pointer: %08x\n",
360 result = reg_read32(hcd->regs, reg);
373 static int ehci_reset(
struct usb_hcd *hcd)
381 reg_write32(hcd->regs,
HC_USBCMD, command);
382 hcd->state = HC_STATE_HALT;
393 qh = kmem_cache_zalloc(qh_cachep, flags);
412 static int priv_init(
struct usb_hcd *hcd)
420 for (i = 0; i <
QH_END; i++)
421 INIT_LIST_HEAD(&priv->
qh_list[i]);
440 static int isp1760_hc_setup(
struct usb_hcd *hcd)
447 if (gpio_is_valid(priv->
rst_gpio)) {
448 unsigned int rst_lvl;
486 if (scratch != 0xdeadbabe) {
487 dev_err(hcd->self.controller,
"Scratch test failed.\n");
504 result = ehci_reset(hcd);
510 dev_info(hcd->self.controller,
"bus width: %d, oc: %s\n",
513 "analog" :
"digital");
533 return priv_init(hcd);
536 static u32 base_to_chip(
u32 base)
538 return ((base - 0x400) >> 3);
550 return (qtd->
urb != urb);
554 #define EHCI_TUNE_CERR 3
555 #define EHCI_TUNE_RL_HS 4
556 #define EHCI_TUNE_RL_TT 0
557 #define EHCI_TUNE_MULT_HS 1
558 #define EHCI_TUNE_MULT_TT 1
559 #define EHCI_TUNE_FLS 2
561 static void create_ptd_atl(
struct isp1760_qh *qh,
569 memset(ptd, 0,
sizeof(*ptd));
572 maxpacket = usb_maxpacket(qtd->
urb->dev, qtd->
urb->pipe,
573 usb_pipeout(qtd->
urb->pipe));
574 multi = 1 + ((maxpacket >> 11) & 0x3);
584 ptd->
dw1 = usb_pipeendpoint(qtd->
urb->pipe) >> 1;
588 if (usb_pipebulk(qtd->
urb->pipe))
590 else if (usb_pipeint(qtd->
urb->pipe))
604 if (usb_pipeint(qtd->
urb->pipe) &&
612 if (usb_pipecontrol(qtd->
urb->pipe) ||
613 usb_pipebulk(qtd->
urb->pipe))
624 if (usb_pipecontrol(qtd->
urb->pipe)) {
627 else if (last_qtd_of_urb(qtd, qh))
636 static void transform_add_int(
struct isp1760_qh *qh,
653 period = qtd->
urb->interval >> 3;
655 if (qtd->
urb->interval > 4)
658 else if (qtd->
urb->interval > 2)
660 else if (qtd->
urb->interval > 1)
666 period = qtd->
urb->interval;
681 period = period >> 1;
688 static void create_ptd_int(
struct isp1760_qh *qh,
691 create_ptd_atl(qh, qtd, ptd);
692 transform_add_int(qh, qtd, ptd);
695 static void isp1760_urb_done(
struct usb_hcd *hcd,
struct urb *urb)
701 if (!urb->unlinked) {
706 if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
708 for (ptr = urb->transfer_buffer;
716 spin_unlock(&priv->
lock);
718 spin_lock(&priv->
lock);
726 qtd = kmem_cache_zalloc(qtd_cachep, flags);
745 static void start_bus_transfer(
struct usb_hcd *hcd,
u32 ptd_offset,
int slot,
752 WARN_ON((slot < 0) || (slot > 31));
774 ptd_write(hcd->regs, ptd_offset, slot, ptd);
778 skip_map &= ~(1 << qh->
slot);
782 skip_map &= ~(1 << qh->
slot);
789 return (usb_pipebulk(qtd->
urb->pipe) &&
793 static void collect_qtds(
struct usb_hcd *hcd,
struct isp1760_qh *qh,
804 last_qtd = last_qtd_of_urb(qtd, qh);
818 qtd->
urb->actual_length +=
826 if (is_short_bulk(qtd)) {
827 if (qtd->
urb->transfer_flags & URB_SHORT_NOT_OK)
842 urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
846 urb_listitem->
urb = qtd->
urb;
855 #define ENQUEUE_DEPTH 2
856 static void enqueue_qtds(
struct usb_hcd *hcd,
struct isp1760_qh *qh)
885 for (curr_slot = 0; curr_slot < 32; curr_slot++) {
886 if ((free_slot == -1) && (slots[curr_slot].qtd ==
NULL))
887 free_slot = curr_slot;
888 if (slots[curr_slot].qh == qh)
917 if ((curr_slot > 31) && (free_slot > -1)) {
918 if (usb_pipeint(qtd->
urb->pipe))
919 create_ptd_int(qh, qtd, &ptd);
921 create_ptd_atl(qh, qtd, &ptd);
923 start_bus_transfer(hcd, ptd_offset, free_slot,
924 slots, qtd, qh, &ptd);
941 struct urb_listitem *urb_listitem, *urb_listitem_next;
949 priv = hcd_to_priv(hcd);
954 for (i = 0; i <
QH_END; i++) {
957 collect_qtds(hcd, qh, &urb_list);
965 isp1760_urb_done(hcd, urb_listitem->
urb);
993 for (i = 0; i <
QH_END; i++) {
996 enqueue_qtds(hcd, qh);
1000 #define PTD_STATE_QTD_DONE 1
1001 #define PTD_STATE_QTD_RELOAD 2
1002 #define PTD_STATE_URB_RETIRE 3
1004 static int check_int_transfer(
struct usb_hcd *hcd,
struct ptd *ptd,
1020 for (i = 0; i < 8; i++) {
1021 switch (dw4 & 0x7) {
1023 dev_dbg(hcd->self.controller,
"%s: underrun "
1024 "during uFrame %d\n",
1026 urb->status = -
ECOMM;
1029 dev_dbg(hcd->self.controller,
"%s: transaction "
1030 "error during uFrame %d\n",
1036 dev_dbg(hcd->self.controller,
"%s: babble "
1037 "error during uFrame %d\n",
1051 static int check_atl_transfer(
struct usb_hcd *hcd,
struct ptd *ptd,
1059 urb->status = -
EPIPE;
1077 dev_dbg(hcd->self.controller,
"PID error; reloading ptd\n");
1093 static void handle_done_ptds(
struct usb_hcd *hcd)
1121 if (!slots[slot].qh) {
1127 state = check_int_transfer(hcd, &ptd,
1128 slots[slot].qtd->
urb);
1136 if (!slots[slot].qh) {
1142 state = check_atl_transfer(hcd, &ptd,
1143 slots[slot].qtd->
urb);
1156 if ((usb_pipeint(qtd->
urb->pipe)) &&
1213 "%s: PTD_STATE_QTD_RELOAD on "
1214 "interrupt packet\n", __func__);
1216 create_ptd_int(qh, qtd, &ptd);
1219 create_ptd_atl(qh, qtd, &ptd);
1222 start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
1231 static irqreturn_t isp1760_irq(
struct usb_hcd *hcd)
1237 spin_lock(&priv->
lock);
1239 if (!(hcd->state & HC_STATE_RUNNING))
1250 handle_done_ptds(hcd);
1254 spin_unlock(&priv->
lock);
1284 #define SLOT_TIMEOUT 300
1285 #define SLOT_CHECK_PERIOD 200
1290 struct usb_hcd *hcd = (
struct usb_hcd *) data;
1294 unsigned long spinflags;
1298 for (slot = 0; slot < 32; slot++)
1309 handle_done_ptds(hcd);
1311 spin_unlock_irqrestore(&priv->
lock, spinflags);
1317 static int isp1760_run(
struct usb_hcd *hcd)
1324 hcd->uses_new_polling = 1;
1326 hcd->state = HC_STATE_RUNNING;
1340 command = reg_read32(hcd->regs,
HC_USBCMD);
1343 reg_write32(hcd->regs,
HC_USBCMD, command);
1358 up_write(&ehci_cf_port_reset_rwsem);
1364 errata2_timer.data = (
unsigned long) hcd;
1369 dev_info(hcd->self.controller,
"USB ISP %04x HW rev. %d started\n",
1370 chipid & 0xffff, chipid >> 16);
1391 static int qtd_fill(
struct isp1760_qtd *qtd,
void *databuffer,
size_t len)
1416 #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
1417 static void packetize_urb(
struct usb_hcd *hcd,
1422 int len, maxpacketsize;
1429 if (!urb->transfer_buffer && urb->transfer_buffer_length) {
1432 "buf is null, dma is %08lx len is %d\n",
1433 (
long unsigned)urb->transfer_dma,
1434 urb->transfer_buffer_length);
1438 if (usb_pipein(urb->pipe))
1443 if (usb_pipecontrol(urb->pipe)) {
1451 if (urb->transfer_buffer_length == 0)
1455 maxpacketsize =
max_packet(usb_maxpacket(urb->dev, urb->pipe,
1456 usb_pipeout(urb->pipe)));
1463 buf = urb->transfer_buffer;
1464 len = urb->transfer_buffer_length;
1469 qtd = qtd_alloc(flags, urb, packet_type);
1472 this_qtd_len = qtd_fill(qtd, buf, len);
1475 len -= this_qtd_len;
1476 buf += this_qtd_len;
1486 if (urb->transfer_buffer_length != 0) {
1489 if (usb_pipecontrol(urb->pipe)) {
1491 if (packet_type ==
IN_PID)
1495 }
else if (usb_pipebulk(urb->pipe)
1496 && (urb->transfer_flags & URB_ZERO_PACKET)
1497 && !(urb->transfer_buffer_length %
1502 qtd = qtd_alloc(flags, urb, packet_type);
1507 qtd_fill(qtd,
NULL, 0);
1515 qtd_list_free(head);
1518 static int isp1760_urb_enqueue(
struct usb_hcd *hcd,
struct urb *urb,
1524 unsigned long spinflags;
1529 switch (usb_pipetype(urb->pipe)) {
1536 case PIPE_INTERRUPT:
1537 if (urb->interval < 0)
1542 case PIPE_ISOCHRONOUS:
1543 dev_err(hcd->self.controller,
"%s: isochronous USB packets "
1544 "not yet supported\n",
1548 dev_err(hcd->self.controller,
"%s: unknown pipe type\n",
1553 if (usb_pipein(urb->pipe))
1554 urb->actual_length = 0;
1556 packetize_urb(hcd, urb, &new_qtds, mem_flags);
1557 if (list_empty(&new_qtds))
1563 if (!
test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1565 qtd_list_free(&new_qtds);
1570 qtd_list_free(&new_qtds);
1574 qh = urb->ep->hcpriv;
1590 qtd_list_free(&new_qtds);
1594 urb->ep->hcpriv = qh;
1597 list_splice_tail(&new_qtds, &qh->
qtd_list);
1601 spin_unlock_irqrestore(&priv->
lock, spinflags);
1605 static void kill_transfer(
struct usb_hcd *hcd,
struct urb *urb,
1615 if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
1617 skip_map |= (1 << qh->
slot);
1623 skip_map |= (1 << qh->
slot);
1636 static void dequeue_urb_from_qtd(
struct usb_hcd *hcd,
struct isp1760_qh *qh,
1640 int urb_was_running;
1643 urb_was_running = 0;
1645 if (qtd->
urb != urb)
1649 urb_was_running = 1;
1650 if (last_qtd_of_urb(qtd, qh) &&
1652 urb_was_running = 0;
1655 kill_transfer(hcd, urb, qh);
1667 static int isp1760_urb_dequeue(
struct usb_hcd *hcd,
struct urb *urb,
1671 unsigned long spinflags;
1681 qh = urb->ep->hcpriv;
1688 if (qtd->urb == urb) {
1689 dequeue_urb_from_qtd(hcd, qh, qtd);
1698 spin_unlock_irqrestore(&priv->
lock, spinflags);
1702 static void isp1760_endpoint_disable(
struct usb_hcd *hcd,
1703 struct usb_host_endpoint *ep)
1706 unsigned long spinflags;
1718 for (i = 0; i <
QH_END; i++)
1720 if (qh_iter == qh) {
1731 spin_unlock_irqrestore(&priv->
lock, spinflags);
1734 static int isp1760_hub_status_data(
struct usb_hcd *hcd,
char *
buf)
1740 unsigned long flags;
1743 if (!HC_IS_RUNNING(hcd->state))
1768 if ((temp & mask) != 0
1772 buf [0] |= 1 << (0 + 1);
1777 spin_unlock_irqrestore(&priv->
lock, flags);
1778 return status ? retval : 0;
1781 static void isp1760_hub_descriptor(
struct isp1760_hcd *priv,
1793 temp = 1 + (ports / 8);
1811 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
1813 static int check_reset_complete(
struct usb_hcd *hcd,
int index,
1820 if (!(port_status &
PORT_PE)) {
1823 "port %d full speed --> companion\n",
1828 reg_write32(hcd->regs,
HC_PORTSC1, port_status);
1831 dev_info(hcd->self.controller,
"port %d high speed\n",
1837 static int isp1760_hub_control(
struct usb_hcd *hcd,
u16 typeReq,
1843 unsigned long flags;
1856 case ClearHubFeature:
1866 case ClearPortFeature:
1867 if (!wIndex || wIndex > ports)
1896 temp | PORT_RESUME);
1923 case GetHubDescriptor:
1932 if (!wIndex || wIndex > ports)
1944 if (temp & PORT_RESUME) {
1945 dev_err(hcd->self.controller,
"Port resume should be skipped.\n");
1967 PORT_RESUME, 0, 2000 );
1970 "port %d resume error %d\n",
1971 wIndex + 1, retval);
1974 temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
1979 if ((temp & PORT_RESET)
1986 reg_write32(hcd->regs,
HC_PORTSC1, temp & ~PORT_RESET);
1991 PORT_RESET, 0, 750);
1993 dev_err(hcd->self.controller,
"port %d reset error %d\n",
1994 wIndex + 1, retval);
1999 temp = check_reset_complete(hcd, wIndex,
2008 if (temp & PORT_OWNER)
2009 dev_err(hcd->self.controller,
"PORT_OWNER is set\n");
2018 if (temp & (PORT_SUSPEND|PORT_RESUME))
2020 if (temp & PORT_RESET)
2037 case SetPortFeature:
2038 selector = wIndex >> 8;
2040 if (!wIndex || wIndex > ports)
2044 if (temp & PORT_OWNER)
2050 reg_write32(hcd->regs,
HC_PORTSC1, temp | PORT_PE);
2054 if ((temp & PORT_PE) == 0
2055 || (temp & PORT_RESET) != 0)
2058 reg_write32(hcd->regs,
HC_PORTSC1, temp | PORT_SUSPEND);
2066 if (temp & PORT_RESUME)
2072 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
2099 spin_unlock_irqrestore(&priv->
lock, flags);
2103 static int isp1760_get_frame(
struct usb_hcd *hcd)
2112 static void isp1760_stop(
struct usb_hcd *hcd)
2123 spin_lock_irq(&priv->
lock);
2128 spin_unlock_irq(&priv->
lock);
2133 static void isp1760_shutdown(
struct usb_hcd *hcd)
2141 command = reg_read32(hcd->regs,
HC_USBCMD);
2143 reg_write32(hcd->regs,
HC_USBCMD, command);
2146 static void isp1760_clear_tt_buffer_complete(
struct usb_hcd *hcd,
2147 struct usb_host_endpoint *ep)
2151 unsigned long spinflags;
2159 spin_unlock_irqrestore(&priv->
lock, spinflags);
2163 static const struct hc_driver isp1760_hc_driver = {
2164 .description =
"isp1760-hcd",
2165 .product_desc =
"NXP ISP1760 USB Host Controller",
2168 .flags = HCD_MEMORY | HCD_USB2,
2169 .reset = isp1760_hc_setup,
2170 .start = isp1760_run,
2171 .stop = isp1760_stop,
2172 .shutdown = isp1760_shutdown,
2173 .urb_enqueue = isp1760_urb_enqueue,
2174 .urb_dequeue = isp1760_urb_dequeue,
2175 .endpoint_disable = isp1760_endpoint_disable,
2176 .get_frame_number = isp1760_get_frame,
2177 .hub_status_data = isp1760_hub_status_data,
2178 .hub_control = isp1760_hub_control,
2179 .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
2188 if (!urb_listitem_cachep)
2217 int irq,
unsigned long irqflags,
2219 struct device *
dev,
const char *busname,
2220 unsigned int devflags)
2222 struct usb_hcd *hcd;
2236 priv = hcd_to_priv(hcd);
2240 hcd->regs =
ioremap(res_start, res_len);
2247 hcd->rsrc_start = res_start;
2248 hcd->rsrc_len = res_len;
2262 return ERR_PTR(ret);