48 #define USE_PLATFORM_DELAY
58 #include <linux/module.h>
62 #include <linux/errno.h>
64 #include <linux/list.h>
65 #include <linux/slab.h>
73 #include <asm/byteorder.h>
77 #define DRIVER_VERSION "03 Nov 2005"
78 #define DRIVER_DESC "ISP116x USB Host Controller Driver"
83 static const char hcd_name[] =
"isp116x-hcd";
101 if ((
unsigned long)dp2 & 1) {
103 for (; len > 1; len -= 2) {
106 isp116x_raw_write_data16(isp116x, w);
109 isp116x_write_data16(isp116x, (
u16) * dp);
112 for (; len > 1; len -= 2) {
114 isp116x_raw_write_data16(isp116x,
cpu_to_le16(*dp2++));
118 isp116x_write_data16(isp116x, 0xff & *((
u8 *) dp2));
120 if (quot == 1 || quot == 2)
121 isp116x_raw_write_data16(isp116x, 0);
127 static void read_ptddata_from_fifo(
struct isp116x *isp116x,
void *buf,
int len)
138 if ((
unsigned long)dp2 & 1) {
140 for (; len > 1; len -= 2) {
141 w = isp116x_raw_read_data16(isp116x);
143 *dp++ = (w >> 8) & 0xff;
147 *dp = 0xff & isp116x_read_data16(isp116x);
150 for (; len > 1; len -= 2) {
152 *dp2++ =
le16_to_cpu(isp116x_raw_read_data16(isp116x));
156 *(
u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
158 if (quot == 1 || quot == 2)
159 isp116x_raw_read_data16(isp116x);
166 static void pack_fifo(
struct isp116x *isp116x)
174 isp116x_write_reg16(isp116x,
HCXFERCTR, buflen);
180 isp116x_write_data16(isp116x, ptd->
count);
181 isp116x_write_data16(isp116x, ptd->
mps);
182 isp116x_write_data16(isp116x, ptd->
len);
183 isp116x_write_data16(isp116x, ptd->
faddr);
184 buflen -=
sizeof(
struct ptd);
187 write_ptddata_to_fifo(isp116x, ep->
data, ep->
length);
198 static void unpack_fifo(
struct isp116x *isp116x)
206 isp116x_write_reg16(isp116x,
HCXFERCTR, buflen);
210 ptd->
count = isp116x_read_data16(isp116x);
211 ptd->
mps = isp116x_read_data16(isp116x);
212 ptd->
len = isp116x_read_data16(isp116x);
213 ptd->
faddr = isp116x_read_data16(isp116x);
214 buflen -=
sizeof(
struct ptd);
217 read_ptddata_from_fifo(isp116x, ep->
data, ep->
length);
231 static void preproc_atl_queue(
struct isp116x *isp116x)
246 ep->
data = (
unsigned char *)urb->transfer_buffer
247 + urb->actual_length;
251 toggle = usb_gettoggle(urb->dev, ep->
epnum, 0);
255 toggle = usb_gettoggle(urb->dev, ep->
epnum, 1);
260 ep->
data = urb->setup_packet;
265 dir = (urb->transfer_buffer_length
266 && usb_pipein(urb->pipe))
270 ERR(
"%s %d: ep->nextpid %d\n", __func__, __LINE__,
285 isp116x->
atl_bufshrt =
sizeof(
struct ptd) + isp116x->atl_buflen;
294 static void finish_request(
struct isp116x *isp116x,
struct isp116x_ep *ep,
295 struct urb *urb,
int status)
302 if (usb_pipecontrol(urb->pipe))
308 spin_unlock(&isp116x->
lock);
310 spin_lock(&isp116x->
lock);
313 if (!list_empty(&ep->
hep->urb_list))
324 for (i = ep->
branch; i < PERIODIC_SIZE; i += ep->
period) {
328 while (*prev && ((temp = *prev) != ep))
335 isp116x_to_hcd(isp116x)->self.bandwidth_allocated -=
348 static void postproc_atl_queue(
struct isp116x *isp116x)
352 struct usb_device *
udev;
374 if (!(urb->transfer_flags & URB_SHORT_NOT_OK) ||
375 usb_pipecontrol(urb->pipe)) {
376 DBG(
"Allowed or control data underrun\n");
381 usb_settoggle(udev, ep->
epnum,
393 status = cc_to_error[
cc];
428 if (urb->transfer_buffer_length != urb->actual_length) {
432 if (urb->transfer_flags & URB_ZERO_PACKET
435 DBG(
"Zero packet requested\n");
440 if (usb_pipecontrol(urb->pipe))
449 if (urb->transfer_buffer_length == urb->actual_length)
451 else if (usb_pipeout(urb->pipe)) {
452 usb_settoggle(udev, 0, 1, 1);
455 usb_settoggle(udev, 0, 0, 1);
472 finish_request(isp116x, ep, urb, status);
480 static void start_atl_transfers(
struct isp116x *isp116x)
490 if (!HC_IS_RUNNING(isp116x_to_hcd(isp116x)->
state))
504 if ((load = isp116x->
load[index])) {
509 while (last_ep->
next)
519 speed = urb->dev->speed;
539 (urb->transfer_buffer_length -
540 urb->actual_length)) {
541 len -= len % ep->maxpacket;
545 len = urb->transfer_buffer_length -
550 load += len * byte_time;
564 if ((&isp116x->
async)->next != (&isp116x->
async)->prev)
565 list_move(&isp116x->
async, (&isp116x->
async)->next);
568 preproc_atl_queue(isp116x);
576 static void finish_atl_transfers(
struct isp116x *isp116x)
585 unpack_fifo(isp116x);
586 postproc_atl_queue(isp116x);
590 static irqreturn_t isp116x_irq(
struct usb_hcd *hcd)
592 struct isp116x *isp116x = hcd_to_isp116x(hcd);
596 spin_lock(&isp116x->
lock);
598 irqstat = isp116x_read_reg16(isp116x,
HCuPINT);
599 isp116x_write_reg16(isp116x,
HCuPINT, irqstat);
603 finish_atl_transfers(isp116x);
608 isp116x_write_reg32(isp116x,
HCINTSTAT, intstat);
610 ERR(
"Unrecoverable error, HC is dead!\n");
613 hcd->state = HC_STATE_HALT;
625 DBG(
"---- remote wakeup\n");
626 usb_hcd_resume_root_hub(hcd);
628 irqstat &= ~HCuPINT_OPR;
633 start_atl_transfers(isp116x);
638 spin_unlock(&isp116x->
lock);
650 #define MAX_PERIODIC_LOAD 600
651 static int balance(
struct isp116x *isp116x,
u16 period,
u16 load)
657 for (i = 0; i <
period; i++) {
662 if ((isp116x->
load[j] + load)
666 if (j < PERIODIC_SIZE)
680 static int isp116x_urb_enqueue(
struct usb_hcd *hcd,
684 struct isp116x *isp116x = hcd_to_isp116x(hcd);
685 struct usb_device *udev = urb->dev;
686 unsigned int pipe = urb->pipe;
687 int is_out = !usb_pipein(pipe);
688 int type = usb_pipetype(pipe);
689 int epnum = usb_pipeendpoint(pipe);
690 struct usb_host_endpoint *
hep = urb->ep;
698 if (type == PIPE_ISOCHRONOUS) {
699 ERR(
"Isochronous transfers not supported\n");
700 urb_dbg(urb,
"Refused to enqueue");
705 ep = kzalloc(
sizeof *ep, mem_flags);
711 if (!HC_IS_RUNNING(hcd->state)) {
714 goto fail_not_linked;
719 goto fail_not_linked;
728 ep->
maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
729 usb_settoggle(udev, epnum, is_out, 0);
731 if (type == PIPE_CONTROL) {
750 if (urb->interval < 2)
754 ep->
period = urb->interval >> 1;
758 (type == PIPE_ISOCHRONOUS),
759 usb_maxpacket(udev, pipe,
775 urb->interval = ep->
period;
777 urb->transfer_buffer_length);
795 for (i = ep->
branch; i < PERIODIC_SIZE; i += ep->period) {
799 while (here && ep != here) {
811 hcd->self.bandwidth_allocated += ep->
load / ep->
period;
823 start_atl_transfers(isp116x);
829 spin_unlock_irqrestore(&isp116x->
lock, flags);
836 static int isp116x_urb_dequeue(
struct usb_hcd *hcd,
struct urb *urb,
839 struct isp116x *isp116x = hcd_to_isp116x(hcd);
840 struct usb_host_endpoint *
hep;
855 if (ep->
hep->urb_list.next == &urb->urb_list)
860 VDBG(
"dequeue, urb %p active; wait for irq\n",
867 finish_request(isp116x, ep, urb, status);
869 spin_unlock_irqrestore(&isp116x->
lock, flags);
873 static void isp116x_endpoint_disable(
struct usb_hcd *hcd,
874 struct usb_host_endpoint *hep)
883 for (i = 0; i < 100 && !list_empty(&hep->urb_list); i++)
885 if (!list_empty(&hep->urb_list))
886 WARNING(
"ep %p not empty?\n", ep);
892 static int isp116x_get_frame(
struct usb_hcd *hcd)
894 struct isp116x *isp116x = hcd_to_isp116x(hcd);
899 fmnum = isp116x_read_reg32(isp116x,
HCFMNUM);
900 spin_unlock_irqrestore(&isp116x->
lock, flags);
907 static int isp116x_hub_status_data(
struct usb_hcd *hcd,
char *buf)
909 struct isp116x *isp116x = hcd_to_isp116x(hcd);
913 if (!HC_IS_RUNNING(hcd->state))
918 if (timer_pending(&hcd->rh_timer))
925 buf[0] = changed = 1;
929 for (i = 0; i < ports; i++) {
935 buf[0] |= 1 << (i + 1);
938 spin_unlock_irqrestore(&isp116x->
lock, flags);
942 static void isp116x_hub_descriptor(
struct isp116x *isp116x,
966 static inline void root_port_reset(
struct isp116x *isp116x,
unsigned port)
979 tmp = isp116x_read_reg32(isp116x, port ?
987 spin_unlock_irqrestore(&isp116x->
lock, flags);
991 isp116x_write_reg32(isp116x, port ?
HCRHPORT2 :
993 spin_unlock_irqrestore(&isp116x->
lock, flags);
999 static int isp116x_hub_control(
struct usb_hcd *hcd,
1003 struct isp116x *isp116x = hcd_to_isp116x(hcd);
1005 unsigned long flags;
1010 case ClearHubFeature:
1011 DBG(
"ClearHubFeature: ");
1014 DBG(
"C_HUB_OVER_CURRENT\n");
1017 spin_unlock_irqrestore(&isp116x->
lock, flags);
1019 DBG(
"C_HUB_LOCAL_POWER\n");
1026 DBG(
"SetHubFeature: ");
1030 DBG(
"C_HUB_OVER_CURRENT or C_HUB_LOCAL_POWER\n");
1036 case GetHubDescriptor:
1037 DBG(
"GetHubDescriptor\n");
1038 isp116x_hub_descriptor(isp116x,
1042 DBG(
"GetHubStatus\n");
1046 DBG(
"GetPortStatus\n");
1047 if (!wIndex || wIndex > ports)
1051 spin_unlock_irqrestore(&isp116x->
lock, flags);
1053 DBG(
"GetPortStatus: port[%d] %08x\n", wIndex + 1, tmp);
1055 case ClearPortFeature:
1056 DBG(
"ClearPortFeature: ");
1057 if (!wIndex || wIndex > ports)
1063 DBG(
"USB_PORT_FEAT_ENABLE\n");
1067 DBG(
"USB_PORT_FEAT_C_ENABLE\n");
1071 DBG(
"USB_PORT_FEAT_SUSPEND\n");
1075 DBG(
"USB_PORT_FEAT_C_SUSPEND\n");
1079 DBG(
"USB_PORT_FEAT_POWER\n");
1083 DBG(
"USB_PORT_FEAT_C_CONNECTION\n");
1087 DBG(
"USB_PORT_FEAT_C_OVER_CURRENT\n");
1091 DBG(
"USB_PORT_FEAT_C_RESET\n");
1098 isp116x_write_reg32(isp116x, wIndex
1100 spin_unlock_irqrestore(&isp116x->
lock, flags);
1102 case SetPortFeature:
1103 DBG(
"SetPortFeature: ");
1104 if (!wIndex || wIndex > ports)
1109 DBG(
"USB_PORT_FEAT_SUSPEND\n");
1111 isp116x_write_reg32(isp116x, wIndex
1113 spin_unlock_irqrestore(&isp116x->
lock, flags);
1116 DBG(
"USB_PORT_FEAT_POWER\n");
1118 isp116x_write_reg32(isp116x, wIndex
1120 spin_unlock_irqrestore(&isp116x->
lock, flags);
1123 DBG(
"USB_PORT_FEAT_RESET\n");
1124 root_port_reset(isp116x, wIndex);
1134 DBG(
"PROTOCOL STALL\n");
1142 #ifdef CONFIG_DEBUG_FS
1146 seq_printf(s,
"%s %04x%s%s%s%s%s%s\n", label, mask,
1149 mask & HCuPINT_OPR ?
" opr" :
"",
1157 seq_printf(s,
"%s %08x%s%s%s%s%s%s%s\n", label, mask,
1159 mask & HCINT_RHSC ?
" rhsc" :
"",
1161 mask & HCINT_UE ?
" ue" :
"",
1168 struct isp116x *isp116x = s->
private;
1171 isp116x_to_hcd(isp116x)->product_desc, hcd_name,
1174 if (HC_IS_SUSPENDED(isp116x_to_hcd(isp116x)->
state)) {
1178 if (!HC_IS_RUNNING(isp116x_to_hcd(isp116x)->
state)) {
1183 spin_lock_irq(&isp116x->
lock);
1184 dump_irq(s,
"hc_irq_enable", isp116x_read_reg16(isp116x,
HCuPINTENB));
1185 dump_irq(s,
"hc_irq_status", isp116x_read_reg16(isp116x,
HCuPINT));
1186 dump_int(s,
"hc_int_enable", isp116x_read_reg32(isp116x,
HCINTENB));
1187 dump_int(s,
"hc_int_status", isp116x_read_reg32(isp116x,
HCINTSTAT));
1188 isp116x_show_regs_seq(isp116x, s);
1189 spin_unlock_irq(&isp116x->
lock);
1201 .
open = isp116x_open_seq,
1211 &isp116x_debug_fops);
1224 #define create_debug_file(d) 0
1225 #define remove_debug_file(d) do{}while(0)
1234 static int isp116x_sw_reset(
struct isp116x *isp116x)
1237 unsigned long flags;
1250 ERR(
"Software reset timeout\n");
1253 spin_unlock_irqrestore(&isp116x->
lock, flags);
1257 static int isp116x_reset(
struct usb_hcd *hcd)
1259 struct isp116x *isp116x = hcd_to_isp116x(hcd);
1264 ret = isp116x_sw_reset(isp116x);
1271 spin_lock_irq(&isp116x->
lock);
1273 spin_unlock_irq(&isp116x->
lock);
1278 ERR(
"Clock not ready after %dms\n", timeout);
1281 ERR(
"Please make sure that the H_WAKEUP pin is pulled low!\n");
1287 static void isp116x_stop(
struct usb_hcd *hcd)
1289 struct isp116x *isp116x = hcd_to_isp116x(hcd);
1290 unsigned long flags;
1298 val = isp116x_read_reg32(isp116x,
HCRHDESCA);
1300 isp116x_write_reg32(isp116x,
HCRHDESCA, val);
1302 spin_unlock_irqrestore(&isp116x->
lock, flags);
1304 isp116x_sw_reset(isp116x);
1310 static int isp116x_start(
struct usb_hcd *hcd)
1312 struct isp116x *isp116x = hcd_to_isp116x(hcd);
1315 unsigned long flags;
1320 isp116x_write_reg16(isp116x,
HCuPINT, 0xff);
1323 val = isp116x_read_reg16(isp116x,
HCCHIPID);
1325 ERR(
"Invalid chip ID %04x\n", val);
1326 spin_unlock_irqrestore(&isp116x->
lock, flags);
1331 hcd->uses_new_polling = 1;
1349 isp116x_write_reg16(isp116x,
HCHWCFG, val);
1359 isp116x_write_reg32(isp116x,
HCRHDESCA, val);
1363 isp116x_write_reg32(isp116x,
HCRHDESCB, val);
1368 if (!device_can_wakeup(hcd->self.controller))
1372 isp116x_write_reg32(isp116x,
HCRHSTATUS, val);
1375 isp116x_write_reg32(isp116x,
HCFMINTVL, 0x27782edf);
1377 hcd->state = HC_STATE_RUNNING;
1391 isp116x_write_reg32(isp116x,
HCCONTROL, val);
1397 isp116x_show_regs_log(isp116x);
1398 spin_unlock_irqrestore(&isp116x->
lock, flags);
1406 struct isp116x *isp116x = hcd_to_isp116x(hcd);
1407 unsigned long flags;
1412 val = isp116x_read_reg32(isp116x,
HCCONTROL);
1416 spin_unlock_irqrestore(&isp116x->
lock, flags);
1419 if (hcd->self.root_hub->do_remote_wakeup)
1424 isp116x_write_reg32(isp116x,
HCCONTROL, val);
1425 spin_unlock_irqrestore(&isp116x->
lock, flags);
1431 (val & ~HCCONTROL_HCFS) |
1436 spin_unlock_irqrestore(&isp116x->
lock, flags);
1445 struct isp116x *isp116x = hcd_to_isp116x(hcd);
1449 spin_lock_irq(&isp116x->
lock);
1451 val = isp116x_read_reg32(isp116x,
HCCONTROL);
1452 switch (val & HCCONTROL_HCFS) {
1454 val &= ~HCCONTROL_HCFS;
1456 isp116x_write_reg32(isp116x,
HCCONTROL, val);
1460 spin_unlock_irq(&isp116x->
lock);
1465 spin_unlock_irq(&isp116x->
lock);
1466 DBG(
"Chip has been reset while suspended. Reinit from scratch.\n");
1469 isp116x_hub_control(hcd, SetPortFeature,
1472 isp116x_hub_control(hcd, SetPortFeature,
1484 DBG(
"%s: Resuming port %d\n", __func__, val);
1488 spin_unlock_irq(&isp116x->
lock);
1490 hcd->state = HC_STATE_RESUMING;
1494 spin_lock_irq(&isp116x->
lock);
1495 val = isp116x_read_reg32(isp116x,
HCCONTROL);
1498 spin_unlock_irq(&isp116x->
lock);
1499 hcd->state = HC_STATE_RUNNING;
1506 #define isp116x_bus_suspend NULL
1507 #define isp116x_bus_resume NULL
1511 static struct hc_driver isp116x_hc_driver = {
1512 .description = hcd_name,
1513 .product_desc =
"ISP116x Host Controller",
1514 .hcd_priv_size =
sizeof(
struct isp116x),
1519 .reset = isp116x_reset,
1520 .start = isp116x_start,
1521 .stop = isp116x_stop,
1523 .urb_enqueue = isp116x_urb_enqueue,
1524 .urb_dequeue = isp116x_urb_dequeue,
1525 .endpoint_disable = isp116x_endpoint_disable,
1527 .get_frame_number = isp116x_get_frame,
1529 .hub_status_data = isp116x_hub_status_data,
1530 .hub_control = isp116x_hub_control,
1539 struct usb_hcd *hcd = platform_get_drvdata(pdev);
1540 struct isp116x *isp116x;
1545 isp116x = hcd_to_isp116x(hcd);
1562 struct usb_hcd *hcd;
1563 struct isp116x *isp116x;
1569 unsigned long irqflags;
1583 if (!addr || !data || !ires) {
1591 if (pdev->
dev.dma_mask) {
1592 DBG(
"DMA not supported\n");
1602 if (addr_reg ==
NULL) {
1611 if (data_reg ==
NULL) {
1623 hcd->rsrc_start = addr->
start;
1624 isp116x = hcd_to_isp116x(hcd);
1628 INIT_LIST_HEAD(&isp116x->
async);
1629 isp116x->
board = pdev->
dev.platform_data;
1631 if (!isp116x->
board) {
1632 ERR(
"Platform data structure not initialized\n");
1637 ERR(
"USE_PLATFORM_DELAY defined, but delay function not "
1639 ERR(
"See comments in drivers/usb/host/isp116x-hcd.c\n");
1650 ERR(
"Couldn't create debugfs entry\n");
1669 ERR(
"init error, %d\n", ret);
1679 VDBG(
"%s: state %x\n", __func__, state.
event);
1688 VDBG(
"%s\n", __func__);
1694 #define isp116x_suspend NULL
1695 #define isp116x_resume NULL
1703 .probe = isp116x_probe,
1704 .remove = isp116x_remove,
1708 .name = (
char *)hcd_name,