123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323 |
- /*
- * ISP116x HCD (Host Controller Driver) for u-boot.
- *
- * Copyright (C) 2006-2007 Rodolfo Giometti <giometti@linux.it>
- * Copyright (C) 2006-2007 Eurotech S.p.A. <info@eurotech.it>
- *
- * SPDX-License-Identifier: GPL-2.0+
- *
- * Derived in part from the SL811 HCD driver "u-boot/drivers/usb/sl811_usb.c"
- * (original copyright message follows):
- *
- * (C) Copyright 2004
- * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
- *
- * This code is based on linux driver for sl811hs chip, source at
- * drivers/usb/host/sl811.c:
- *
- * SL811 Host Controller Interface driver for USB.
- *
- * Copyright (c) 2003/06, Courage Co., Ltd.
- *
- * Based on:
- * 1.uhci.c by Linus Torvalds, Johannes Erdfelt, Randy Dunlap,
- * Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber,
- * Adam Richter, Gregory P. Smith;
- * 2.Original SL811 driver (hc_sl811.o) by Pei Liu <pbl@cypress.com>
- * 3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
- *
- * [[GNU/GPL disclaimer]]
- *
- * and in part from AU1x00 OHCI HCD driver "u-boot/arch/mips/cpu/au1x00_usb_ohci.c"
- * (original copyright message follows):
- *
- * URB OHCI HCD (Host Controller Driver) for USB on the AU1x00.
- *
- * (C) Copyright 2003
- * Gary Jennejohn, DENX Software Engineering <garyj@denx.de>
- *
- * [[GNU/GPL disclaimer]]
- *
- * Note: Part of this code has been derived from linux
- */
- #include <common.h>
- #include <asm/io.h>
- #include <usb.h>
- #include <malloc.h>
- #include <linux/list.h>
- /*
- * ISP116x chips require certain delays between accesses to its
- * registers. The following timing options exist.
- *
- * 1. Configure your memory controller (the best)
- * 2. Use ndelay (easiest, poorest). For that, enable the following macro.
- *
- * Value is in microseconds.
- */
- #ifdef ISP116X_HCD_USE_UDELAY
- #define UDELAY 1
- #endif
- /*
- * On some (slowly?) machines an extra delay after data packing into
- * controller's FIFOs is required, * otherwise you may get the following
- * error:
- *
- * uboot> usb start
- * (Re)start USB...
- * USB: scanning bus for devices... isp116x: isp116x_submit_job: CTL:TIMEOUT
- * isp116x: isp116x_submit_job: ****** FIFO not ready! ******
- *
- * USB device not responding, giving up (status=4)
- * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
- * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
- * isp116x: isp116x_submit_job: ****** FIFO not empty! ******
- * 3 USB Device(s) found
- * scanning bus for storage devices... 0 Storage Device(s) found
- *
- * Value is in milliseconds.
- */
- #ifdef ISP116X_HCD_USE_EXTRA_DELAY
- #define EXTRA_DELAY 2
- #endif
- /*
- * Enable the following defines if you wish enable debugging messages.
- */
- #undef DEBUG /* enable debugging messages */
- #undef TRACE /* enable tracing code */
- #undef VERBOSE /* verbose debugging messages */
- #include "isp116x.h"
- #define DRIVER_VERSION "08 Jan 2007"
- static const char hcd_name[] = "isp116x-hcd";
- struct isp116x isp116x_dev;
- struct isp116x_platform_data isp116x_board;
- static int got_rhsc; /* root hub status change */
- struct usb_device *devgone; /* device which was disconnected */
- static int rh_devnum; /* address of Root Hub endpoint */
- /* ------------------------------------------------------------------------- */
- static int isp116x_reset(struct isp116x *isp116x);
- /* --- Debugging functions ------------------------------------------------- */
- #define isp116x_show_reg(d, r) { \
- if ((r) < 0x20) { \
- DBG("%-12s[%02x]: %08x", #r, \
- r, isp116x_read_reg32(d, r)); \
- } else { \
- DBG("%-12s[%02x]: %04x", #r, \
- r, isp116x_read_reg16(d, r)); \
- } \
- }
- #define isp116x_show_regs(d) { \
- isp116x_show_reg(d, HCREVISION); \
- isp116x_show_reg(d, HCCONTROL); \
- isp116x_show_reg(d, HCCMDSTAT); \
- isp116x_show_reg(d, HCINTSTAT); \
- isp116x_show_reg(d, HCINTENB); \
- isp116x_show_reg(d, HCFMINTVL); \
- isp116x_show_reg(d, HCFMREM); \
- isp116x_show_reg(d, HCFMNUM); \
- isp116x_show_reg(d, HCLSTHRESH); \
- isp116x_show_reg(d, HCRHDESCA); \
- isp116x_show_reg(d, HCRHDESCB); \
- isp116x_show_reg(d, HCRHSTATUS); \
- isp116x_show_reg(d, HCRHPORT1); \
- isp116x_show_reg(d, HCRHPORT2); \
- isp116x_show_reg(d, HCHWCFG); \
- isp116x_show_reg(d, HCDMACFG); \
- isp116x_show_reg(d, HCXFERCTR); \
- isp116x_show_reg(d, HCuPINT); \
- isp116x_show_reg(d, HCuPINTENB); \
- isp116x_show_reg(d, HCCHIPID); \
- isp116x_show_reg(d, HCSCRATCH); \
- isp116x_show_reg(d, HCITLBUFLEN); \
- isp116x_show_reg(d, HCATLBUFLEN); \
- isp116x_show_reg(d, HCBUFSTAT); \
- isp116x_show_reg(d, HCRDITL0LEN); \
- isp116x_show_reg(d, HCRDITL1LEN); \
- }
- #if defined(TRACE)
- static int isp116x_get_current_frame_number(struct usb_device *usb_dev)
- {
- struct isp116x *isp116x = &isp116x_dev;
- return isp116x_read_reg32(isp116x, HCFMNUM);
- }
- static void dump_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
- int len, char *str)
- {
- #if defined(VERBOSE)
- int i;
- #endif
- DBG("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d stat:%#lx",
- str,
- isp116x_get_current_frame_number(dev),
- usb_pipedevice(pipe),
- usb_pipeendpoint(pipe),
- usb_pipeout(pipe) ? 'O' : 'I',
- usb_pipetype(pipe) < 2 ?
- (usb_pipeint(pipe) ?
- "INTR" : "ISOC") :
- (usb_pipecontrol(pipe) ? "CTRL" : "BULK"), len, dev->status);
- #if defined(VERBOSE)
- if (len > 0 && buffer) {
- printf(__FILE__ ": data(%d):", len);
- for (i = 0; i < 16 && i < len; i++)
- printf(" %02x", ((__u8 *) buffer)[i]);
- printf("%s\n", i < len ? "..." : "");
- }
- #endif
- }
- #define PTD_DIR_STR(ptd) ({char __c; \
- switch(PTD_GET_DIR(ptd)){ \
- case 0: __c = 's'; break; \
- case 1: __c = 'o'; break; \
- default: __c = 'i'; break; \
- }; __c;})
- /*
- Dump PTD info. The code documents the format
- perfectly, right :)
- */
- static inline void dump_ptd(struct ptd *ptd)
- {
- #if defined(VERBOSE)
- int k;
- #endif
- DBG("PTD(ext) : cc:%x %d%c%d %d,%d,%d t:%x %x%x%x",
- PTD_GET_CC(ptd),
- PTD_GET_FA(ptd), PTD_DIR_STR(ptd), PTD_GET_EP(ptd),
- PTD_GET_COUNT(ptd), PTD_GET_LEN(ptd), PTD_GET_MPS(ptd),
- PTD_GET_TOGGLE(ptd),
- PTD_GET_ACTIVE(ptd), PTD_GET_SPD(ptd), PTD_GET_LAST(ptd));
- #if defined(VERBOSE)
- printf("isp116x: %s: PTD(byte): ", __FUNCTION__);
- for (k = 0; k < sizeof(struct ptd); ++k)
- printf("%02x ", ((u8 *) ptd)[k]);
- printf("\n");
- #endif
- }
- static inline void dump_ptd_data(struct ptd *ptd, u8 * buf, int type)
- {
- #if defined(VERBOSE)
- int k;
- if (type == 0 /* 0ut data */ ) {
- printf("isp116x: %s: out data: ", __FUNCTION__);
- for (k = 0; k < PTD_GET_LEN(ptd); ++k)
- printf("%02x ", ((u8 *) buf)[k]);
- printf("\n");
- }
- if (type == 1 /* 1n data */ ) {
- printf("isp116x: %s: in data: ", __FUNCTION__);
- for (k = 0; k < PTD_GET_COUNT(ptd); ++k)
- printf("%02x ", ((u8 *) buf)[k]);
- printf("\n");
- }
- if (PTD_GET_LAST(ptd))
- DBG("--- last PTD ---");
- #endif
- }
- #else
- #define dump_msg(dev, pipe, buffer, len, str) do { } while (0)
- #define dump_pkt(dev, pipe, buffer, len, setup, str, small) do {} while (0)
- #define dump_ptd(ptd) do {} while (0)
- #define dump_ptd_data(ptd, buf, type) do {} while (0)
- #endif
- /* --- Virtual Root Hub ---------------------------------------------------- */
- #include <usbroothubdes.h>
- /*
- * Hub class-specific descriptor is constructed dynamically
- */
- /* --- Virtual root hub management functions ------------------------------- */
- static int rh_check_port_status(struct isp116x *isp116x)
- {
- u32 temp, ndp, i;
- int res;
- res = -1;
- temp = isp116x_read_reg32(isp116x, HCRHSTATUS);
- ndp = (temp & RH_A_NDP);
- for (i = 0; i < ndp; i++) {
- temp = isp116x_read_reg32(isp116x, HCRHPORT1 + i);
- /* check for a device disconnect */
- if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
- (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
- res = i;
- break;
- }
- }
- return res;
- }
- /* --- HC management functions --------------------------------------------- */
- /* Write len bytes to fifo, pad till 32-bit boundary
- */
- static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
- {
- u8 *dp = (u8 *) buf;
- u16 *dp2 = (u16 *) buf;
- u16 w;
- int quot = len % 4;
- if ((unsigned long)dp2 & 1) {
- /* not aligned */
- for (; len > 1; len -= 2) {
- w = *dp++;
- w |= *dp++ << 8;
- isp116x_raw_write_data16(isp116x, w);
- }
- if (len)
- isp116x_write_data16(isp116x, (u16) * dp);
- } else {
- /* aligned */
- for (; len > 1; len -= 2)
- isp116x_raw_write_data16(isp116x, *dp2++);
- if (len)
- isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
- }
- if (quot == 1 || quot == 2)
- isp116x_raw_write_data16(isp116x, 0);
- }
- /* Read len bytes from fifo and then read till 32-bit boundary
- */
- static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
- {
- u8 *dp = (u8 *) buf;
- u16 *dp2 = (u16 *) buf;
- u16 w;
- int quot = len % 4;
- if ((unsigned long)dp2 & 1) {
- /* not aligned */
- for (; len > 1; len -= 2) {
- w = isp116x_raw_read_data16(isp116x);
- *dp++ = w & 0xff;
- *dp++ = (w >> 8) & 0xff;
- }
- if (len)
- *dp = 0xff & isp116x_read_data16(isp116x);
- } else {
- /* aligned */
- for (; len > 1; len -= 2)
- *dp2++ = isp116x_raw_read_data16(isp116x);
- if (len)
- *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
- }
- if (quot == 1 || quot == 2)
- isp116x_raw_read_data16(isp116x);
- }
- /* Write PTD's and data for scheduled transfers into the fifo ram.
- * Fifo must be empty and ready */
- static void pack_fifo(struct isp116x *isp116x, struct usb_device *dev,
- unsigned long pipe, struct ptd *ptd, int n, void *data,
- int len)
- {
- int buflen = n * sizeof(struct ptd) + len;
- int i, done;
- DBG("--- pack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
- isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
- isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
- isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
- done = 0;
- for (i = 0; i < n; i++) {
- DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
- dump_ptd(&ptd[i]);
- isp116x_write_data16(isp116x, ptd[i].count);
- isp116x_write_data16(isp116x, ptd[i].mps);
- isp116x_write_data16(isp116x, ptd[i].len);
- isp116x_write_data16(isp116x, ptd[i].faddr);
- dump_ptd_data(&ptd[i], (__u8 *) data + done, 0);
- write_ptddata_to_fifo(isp116x,
- (__u8 *) data + done,
- PTD_GET_LEN(&ptd[i]));
- done += PTD_GET_LEN(&ptd[i]);
- }
- }
- /* Read the processed PTD's and data from fifo ram back to URBs' buffers.
- * Fifo must be full and done */
- static int unpack_fifo(struct isp116x *isp116x, struct usb_device *dev,
- unsigned long pipe, struct ptd *ptd, int n, void *data,
- int len)
- {
- int buflen = n * sizeof(struct ptd) + len;
- int i, done, cc, ret;
- isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
- isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
- isp116x_write_addr(isp116x, HCATLPORT);
- ret = TD_CC_NOERROR;
- done = 0;
- for (i = 0; i < n; i++) {
- DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
- ptd[i].count = isp116x_read_data16(isp116x);
- ptd[i].mps = isp116x_read_data16(isp116x);
- ptd[i].len = isp116x_read_data16(isp116x);
- ptd[i].faddr = isp116x_read_data16(isp116x);
- dump_ptd(&ptd[i]);
- read_ptddata_from_fifo(isp116x,
- (__u8 *) data + done,
- PTD_GET_LEN(&ptd[i]));
- dump_ptd_data(&ptd[i], (__u8 *) data + done, 1);
- done += PTD_GET_LEN(&ptd[i]);
- cc = PTD_GET_CC(&ptd[i]);
- /* Data underrun means basically that we had more buffer space than
- * the function had data. It is perfectly normal but upper levels have
- * to know how much we actually transferred.
- */
- if (cc == TD_NOTACCESSED ||
- (cc != TD_CC_NOERROR && (ret == TD_CC_NOERROR || ret == TD_DATAUNDERRUN)))
- ret = cc;
- }
- DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
- return ret;
- }
- /* Interrupt handling
- */
- static int isp116x_interrupt(struct isp116x *isp116x)
- {
- u16 irqstat;
- u32 intstat;
- int ret = 0;
- isp116x_write_reg16(isp116x, HCuPINTENB, 0);
- irqstat = isp116x_read_reg16(isp116x, HCuPINT);
- isp116x_write_reg16(isp116x, HCuPINT, irqstat);
- DBG("------ irqstat %x ------", irqstat);
- if (irqstat & HCuPINT_ATL) {
- DBG("------ HCuPINT_ATL ------");
- udelay(500);
- ret = 1;
- }
- if (irqstat & HCuPINT_OPR) {
- intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
- isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
- DBG("------ HCuPINT_OPR %x ------", intstat);
- if (intstat & HCINT_UE) {
- ERR("unrecoverable error, controller disabled");
- /* FIXME: be optimistic, hope that bug won't repeat
- * often. Make some non-interrupt context restart the
- * controller. Count and limit the retries though;
- * either hardware or software errors can go forever...
- */
- isp116x_reset(isp116x);
- ret = -1;
- return -1;
- }
- if (intstat & HCINT_RHSC) {
- got_rhsc = 1;
- ret = 1;
- /* When root hub or any of its ports is going
- to come out of suspend, it may take more
- than 10ms for status bits to stabilize. */
- mdelay(20);
- }
- if (intstat & HCINT_SO) {
- ERR("schedule overrun");
- ret = -1;
- }
- irqstat &= ~HCuPINT_OPR;
- }
- return ret;
- }
- /* With one PTD we can transfer almost 1K in one go;
- * HC does the splitting into endpoint digestible transactions
- */
- struct ptd ptd[1];
- static inline int max_transfer_len(struct usb_device *dev, unsigned long pipe)
- {
- unsigned mpck = usb_maxpacket(dev, pipe);
- /* One PTD can transfer 1023 bytes but try to always
- * transfer multiples of endpoint buffer size
- */
- return 1023 / mpck * mpck;
- }
- /* Do an USB transfer
- */
- static int isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
- int dir, void *buffer, int len)
- {
- struct isp116x *isp116x = &isp116x_dev;
- int type = usb_pipetype(pipe);
- int epnum = usb_pipeendpoint(pipe);
- int max = usb_maxpacket(dev, pipe);
- int dir_out = usb_pipeout(pipe);
- int speed_low = (dev->speed == USB_SPEED_LOW);
- int i, done = 0, stat, timeout, cc;
- /* 500 frames or 0.5s timeout when function is busy and NAKs transactions for a while */
- int retries = 500;
- DBG("------------------------------------------------");
- dump_msg(dev, pipe, buffer, len, "SUBMIT");
- DBG("------------------------------------------------");
- if (len >= 1024) {
- ERR("Too big job");
- dev->status = USB_ST_CRC_ERR;
- return -1;
- }
- if (isp116x->disabled) {
- ERR("EPIPE");
- dev->status = USB_ST_CRC_ERR;
- return -1;
- }
- /* device pulled? Shortcut the action. */
- if (devgone == dev) {
- ERR("ENODEV");
- dev->status = USB_ST_CRC_ERR;
- return USB_ST_CRC_ERR;
- }
- if (!max) {
- ERR("pipesize for pipe %lx is zero", pipe);
- dev->status = USB_ST_CRC_ERR;
- return -1;
- }
- if (type == PIPE_ISOCHRONOUS) {
- ERR("isochronous transfers not supported");
- dev->status = USB_ST_CRC_ERR;
- return -1;
- }
- /* FIFO not empty? */
- if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL) {
- ERR("****** FIFO not empty! ******");
- dev->status = USB_ST_BUF_ERR;
- return -1;
- }
- retry:
- isp116x_write_reg32(isp116x, HCINTSTAT, 0xff);
- /* Prepare the PTD data */
- ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK |
- PTD_TOGGLE(usb_gettoggle(dev, epnum, dir_out));
- ptd->mps = PTD_MPS(max) | PTD_SPD(speed_low) | PTD_EP(epnum) | PTD_LAST_MSK;
- ptd->len = PTD_LEN(len) | PTD_DIR(dir);
- ptd->faddr = PTD_FA(usb_pipedevice(pipe));
- retry_same:
- /* Pack data into FIFO ram */
- pack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
- #ifdef EXTRA_DELAY
- mdelay(EXTRA_DELAY);
- #endif
- /* Start the data transfer */
- /* Allow more time for a BULK device to react - some are slow */
- if (usb_pipebulk(pipe))
- timeout = 5000;
- else
- timeout = 100;
- /* Wait for it to complete */
- for (;;) {
- /* Check whether the controller is done */
- stat = isp116x_interrupt(isp116x);
- if (stat < 0) {
- dev->status = USB_ST_CRC_ERR;
- break;
- }
- if (stat > 0)
- break;
- /* Check the timeout */
- if (--timeout)
- udelay(1);
- else {
- ERR("CTL:TIMEOUT ");
- stat = USB_ST_CRC_ERR;
- break;
- }
- }
- /* We got an Root Hub Status Change interrupt */
- if (got_rhsc) {
- isp116x_show_regs(isp116x);
- got_rhsc = 0;
- /* Abuse timeout */
- timeout = rh_check_port_status(isp116x);
- if (timeout >= 0) {
- /*
- * FIXME! NOTE! AAAARGH!
- * This is potentially dangerous because it assumes
- * that only one device is ever plugged in!
- */
- devgone = dev;
- }
- }
- /* Ok, now we can read transfer status */
- /* FIFO not ready? */
- if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE)) {
- ERR("****** FIFO not ready! ******");
- dev->status = USB_ST_BUF_ERR;
- return -1;
- }
- /* Unpack data from FIFO ram */
- cc = unpack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
- i = PTD_GET_COUNT(ptd);
- done += i;
- buffer += i;
- len -= i;
- /* There was some kind of real problem; Prepare the PTD again
- * and retry from the failed transaction on
- */
- if (cc && cc != TD_NOTACCESSED && cc != TD_DATAUNDERRUN) {
- if (retries >= 100) {
- retries -= 100;
- /* The chip will have toggled the toggle bit for the failed
- * transaction too. We have to toggle it back.
- */
- usb_settoggle(dev, epnum, dir_out, !PTD_GET_TOGGLE(ptd));
- goto retry;
- }
- }
- /* "Normal" errors; TD_NOTACCESSED would mean in effect that the function have NAKed
- * the transactions from the first on for the whole frame. It may be busy and we retry
- * with the same PTD. PTD_ACTIVE (and not TD_NOTACCESSED) would mean that some of the
- * PTD didn't make it because the function was busy or the frame ended before the PTD
- * finished. We prepare the rest of the data and try again.
- */
- else if (cc == TD_NOTACCESSED || PTD_GET_ACTIVE(ptd) || (cc != TD_DATAUNDERRUN && PTD_GET_COUNT(ptd) < PTD_GET_LEN(ptd))) {
- if (retries) {
- --retries;
- if (cc == TD_NOTACCESSED && PTD_GET_ACTIVE(ptd) && !PTD_GET_COUNT(ptd)) goto retry_same;
- usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
- goto retry;
- }
- }
- if (cc != TD_CC_NOERROR && cc != TD_DATAUNDERRUN) {
- DBG("****** completition code error %x ******", cc);
- switch (cc) {
- case TD_CC_BITSTUFFING:
- dev->status = USB_ST_BIT_ERR;
- break;
- case TD_CC_STALL:
- dev->status = USB_ST_STALLED;
- break;
- case TD_BUFFEROVERRUN:
- case TD_BUFFERUNDERRUN:
- dev->status = USB_ST_BUF_ERR;
- break;
- default:
- dev->status = USB_ST_CRC_ERR;
- }
- return -cc;
- }
- else usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
- dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");
- dev->status = 0;
- return done;
- }
- /* Adapted from au1x00_usb_ohci.c
- */
- static int isp116x_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
- void *buffer, int transfer_len,
- struct devrequest *cmd)
- {
- struct isp116x *isp116x = &isp116x_dev;
- u32 tmp = 0;
- int leni = transfer_len;
- int len = 0;
- int stat = 0;
- u32 datab[4];
- u8 *data_buf = (u8 *) datab;
- u16 bmRType_bReq;
- u16 wValue;
- u16 wIndex;
- u16 wLength;
- if (usb_pipeint(pipe)) {
- INFO("Root-Hub submit IRQ: NOT implemented");
- return 0;
- }
- bmRType_bReq = cmd->requesttype | (cmd->request << 8);
- wValue = swap_16(cmd->value);
- wIndex = swap_16(cmd->index);
- wLength = swap_16(cmd->length);
- DBG("--- HUB ----------------------------------------");
- DBG("submit rh urb, req=%x val=%#x index=%#x len=%d",
- bmRType_bReq, wValue, wIndex, wLength);
- dump_msg(dev, pipe, buffer, transfer_len, "RH");
- DBG("------------------------------------------------");
- switch (bmRType_bReq) {
- case RH_GET_STATUS:
- DBG("RH_GET_STATUS");
- *(__u16 *) data_buf = swap_16(1);
- len = 2;
- break;
- case RH_GET_STATUS | RH_INTERFACE:
- DBG("RH_GET_STATUS | RH_INTERFACE");
- *(__u16 *) data_buf = swap_16(0);
- len = 2;
- break;
- case RH_GET_STATUS | RH_ENDPOINT:
- DBG("RH_GET_STATUS | RH_ENDPOINT");
- *(__u16 *) data_buf = swap_16(0);
- len = 2;
- break;
- case RH_GET_STATUS | RH_CLASS:
- DBG("RH_GET_STATUS | RH_CLASS");
- tmp = isp116x_read_reg32(isp116x, HCRHSTATUS);
- *(__u32 *) data_buf = swap_32(tmp & ~(RH_HS_CRWE | RH_HS_DRWE));
- len = 4;
- break;
- case RH_GET_STATUS | RH_OTHER | RH_CLASS:
- DBG("RH_GET_STATUS | RH_OTHER | RH_CLASS");
- tmp = isp116x_read_reg32(isp116x, HCRHPORT1 + wIndex - 1);
- *(__u32 *) data_buf = swap_32(tmp);
- isp116x_show_regs(isp116x);
- len = 4;
- break;
- case RH_CLEAR_FEATURE | RH_ENDPOINT:
- DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");
- switch (wValue) {
- case RH_ENDPOINT_STALL:
- DBG("C_HUB_ENDPOINT_STALL");
- len = 0;
- break;
- }
- break;
- case RH_CLEAR_FEATURE | RH_CLASS:
- DBG("RH_CLEAR_FEATURE | RH_CLASS");
- switch (wValue) {
- case RH_C_HUB_LOCAL_POWER:
- DBG("C_HUB_LOCAL_POWER");
- len = 0;
- break;
- case RH_C_HUB_OVER_CURRENT:
- DBG("C_HUB_OVER_CURRENT");
- isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
- len = 0;
- break;
- }
- break;
- case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
- DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");
- switch (wValue) {
- case RH_PORT_ENABLE:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_CCS);
- len = 0;
- break;
- case RH_PORT_SUSPEND:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_POCI);
- len = 0;
- break;
- case RH_PORT_POWER:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_LSDA);
- len = 0;
- break;
- case RH_C_PORT_CONNECTION:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_CSC);
- len = 0;
- break;
- case RH_C_PORT_ENABLE:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_PESC);
- len = 0;
- break;
- case RH_C_PORT_SUSPEND:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_PSSC);
- len = 0;
- break;
- case RH_C_PORT_OVER_CURRENT:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_POCI);
- len = 0;
- break;
- case RH_C_PORT_RESET:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_PRSC);
- len = 0;
- break;
- default:
- ERR("invalid wValue");
- stat = USB_ST_STALLED;
- }
- isp116x_show_regs(isp116x);
- break;
- case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
- DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");
- switch (wValue) {
- case RH_PORT_SUSPEND:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_PSS);
- len = 0;
- break;
- case RH_PORT_RESET:
- /* Spin until any current reset finishes */
- while (1) {
- tmp =
- isp116x_read_reg32(isp116x,
- HCRHPORT1 + wIndex - 1);
- if (!(tmp & RH_PS_PRS))
- break;
- mdelay(1);
- }
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_PRS);
- mdelay(10);
- len = 0;
- break;
- case RH_PORT_POWER:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_PPS);
- len = 0;
- break;
- case RH_PORT_ENABLE:
- isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
- RH_PS_PES);
- len = 0;
- break;
- default:
- ERR("invalid wValue");
- stat = USB_ST_STALLED;
- }
- isp116x_show_regs(isp116x);
- break;
- case RH_SET_ADDRESS:
- DBG("RH_SET_ADDRESS");
- rh_devnum = wValue;
- len = 0;
- break;
- case RH_GET_DESCRIPTOR:
- DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);
- switch (wValue) {
- case (USB_DT_DEVICE << 8): /* device descriptor */
- len = min_t(unsigned int,
- leni, min_t(unsigned int,
- sizeof(root_hub_dev_des),
- wLength));
- data_buf = root_hub_dev_des;
- break;
- case (USB_DT_CONFIG << 8): /* configuration descriptor */
- len = min_t(unsigned int,
- leni, min_t(unsigned int,
- sizeof(root_hub_config_des),
- wLength));
- data_buf = root_hub_config_des;
- break;
- case ((USB_DT_STRING << 8) | 0x00): /* string 0 descriptors */
- len = min_t(unsigned int,
- leni, min_t(unsigned int,
- sizeof(root_hub_str_index0),
- wLength));
- data_buf = root_hub_str_index0;
- break;
- case ((USB_DT_STRING << 8) | 0x01): /* string 1 descriptors */
- len = min_t(unsigned int,
- leni, min_t(unsigned int,
- sizeof(root_hub_str_index1),
- wLength));
- data_buf = root_hub_str_index1;
- break;
- default:
- ERR("invalid wValue");
- stat = USB_ST_STALLED;
- }
- break;
- case RH_GET_DESCRIPTOR | RH_CLASS:
- DBG("RH_GET_DESCRIPTOR | RH_CLASS");
- tmp = isp116x_read_reg32(isp116x, HCRHDESCA);
- data_buf[0] = 0x09; /* min length; */
- data_buf[1] = 0x29;
- data_buf[2] = tmp & RH_A_NDP;
- data_buf[3] = 0;
- if (tmp & RH_A_PSM) /* per-port power switching? */
- data_buf[3] |= 0x01;
- if (tmp & RH_A_NOCP) /* no overcurrent reporting? */
- data_buf[3] |= 0x10;
- else if (tmp & RH_A_OCPM) /* per-port overcurrent rep? */
- data_buf[3] |= 0x08;
- /* Corresponds to data_buf[4-7] */
- datab[1] = 0;
- data_buf[5] = (tmp & RH_A_POTPGT) >> 24;
- tmp = isp116x_read_reg32(isp116x, HCRHDESCB);
- data_buf[7] = tmp & RH_B_DR;
- if (data_buf[2] < 7)
- data_buf[8] = 0xff;
- else {
- data_buf[0] += 2;
- data_buf[8] = (tmp & RH_B_DR) >> 8;
- data_buf[10] = data_buf[9] = 0xff;
- }
- len = min_t(unsigned int, leni,
- min_t(unsigned int, data_buf[0], wLength));
- break;
- case RH_GET_CONFIGURATION:
- DBG("RH_GET_CONFIGURATION");
- *(__u8 *) data_buf = 0x01;
- len = 1;
- break;
- case RH_SET_CONFIGURATION:
- DBG("RH_SET_CONFIGURATION");
- isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
- len = 0;
- break;
- default:
- ERR("*** *** *** unsupported root hub command *** *** ***");
- stat = USB_ST_STALLED;
- }
- len = min_t(int, len, leni);
- if (buffer != data_buf)
- memcpy(buffer, data_buf, len);
- dev->act_len = len;
- dev->status = stat;
- DBG("dev act_len %d, status %d", dev->act_len, dev->status);
- dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");
- return stat;
- }
- /* --- Transfer functions -------------------------------------------------- */
- int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
- int len, int interval)
- {
- DBG("dev=%p pipe=%#lx buf=%p size=%d int=%d",
- dev, pipe, buffer, len, interval);
- return -1;
- }
- int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
- int len, struct devrequest *setup)
- {
- int devnum = usb_pipedevice(pipe);
- int epnum = usb_pipeendpoint(pipe);
- int max = max_transfer_len(dev, pipe);
- int dir_in = usb_pipein(pipe);
- int done, ret;
- /* Control message is for the HUB? */
- if (devnum == rh_devnum)
- return isp116x_submit_rh_msg(dev, pipe, buffer, len, setup);
- /* Ok, no HUB message so send the message to the device */
- /* Setup phase */
- DBG("--- SETUP PHASE --------------------------------");
- usb_settoggle(dev, epnum, 1, 0);
- ret = isp116x_submit_job(dev, pipe,
- PTD_DIR_SETUP,
- setup, sizeof(struct devrequest));
- if (ret < 0) {
- DBG("control setup phase error (ret = %d", ret);
- return -1;
- }
- /* Data phase */
- DBG("--- DATA PHASE ---------------------------------");
- done = 0;
- usb_settoggle(dev, epnum, !dir_in, 1);
- while (done < len) {
- ret = isp116x_submit_job(dev, pipe,
- dir_in ? PTD_DIR_IN : PTD_DIR_OUT,
- (__u8 *) buffer + done,
- max > len - done ? len - done : max);
- if (ret < 0) {
- DBG("control data phase error (ret = %d)", ret);
- return -1;
- }
- done += ret;
- if (dir_in && ret < max) /* short packet */
- break;
- }
- /* Status phase */
- DBG("--- STATUS PHASE -------------------------------");
- usb_settoggle(dev, epnum, !dir_in, 1);
- ret = isp116x_submit_job(dev, pipe,
- !dir_in ? PTD_DIR_IN : PTD_DIR_OUT, NULL, 0);
- if (ret < 0) {
- DBG("control status phase error (ret = %d", ret);
- return -1;
- }
- dev->act_len = done;
- dump_msg(dev, pipe, buffer, len, "DEV(ret)");
- return done;
- }
- int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
- int len)
- {
- int dir_out = usb_pipeout(pipe);
- int max = max_transfer_len(dev, pipe);
- int done, ret;
- DBG("--- BULK ---------------------------------------");
- DBG("dev=%ld pipe=%ld buf=%p size=%d dir_out=%d",
- usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
- done = 0;
- while (done < len) {
- ret = isp116x_submit_job(dev, pipe,
- !dir_out ? PTD_DIR_IN : PTD_DIR_OUT,
- (__u8 *) buffer + done,
- max > len - done ? len - done : max);
- if (ret < 0) {
- DBG("error on bulk message (ret = %d)", ret);
- return -1;
- }
- done += ret;
- if (!dir_out && ret < max) /* short packet */
- break;
- }
- dev->act_len = done;
- return 0;
- }
- /* --- Basic functions ----------------------------------------------------- */
- static int isp116x_sw_reset(struct isp116x *isp116x)
- {
- int retries = 15;
- int ret = 0;
- DBG("");
- isp116x->disabled = 1;
- isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
- isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
- while (--retries) {
- /* It usually resets within 1 ms */
- mdelay(1);
- if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
- break;
- }
- if (!retries) {
- ERR("software reset timeout");
- ret = -1;
- }
- return ret;
- }
- static int isp116x_reset(struct isp116x *isp116x)
- {
- unsigned long t;
- u16 clkrdy = 0;
- int ret, timeout = 15 /* ms */ ;
- DBG("");
- ret = isp116x_sw_reset(isp116x);
- if (ret)
- return ret;
- for (t = 0; t < timeout; t++) {
- clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
- if (clkrdy)
- break;
- mdelay(1);
- }
- if (!clkrdy) {
- ERR("clock not ready after %dms", timeout);
- /* After sw_reset the clock won't report to be ready, if
- H_WAKEUP pin is high. */
- ERR("please make sure that the H_WAKEUP pin is pulled low!");
- ret = -1;
- }
- return ret;
- }
- static void isp116x_stop(struct isp116x *isp116x)
- {
- u32 val;
- DBG("");
- isp116x_write_reg16(isp116x, HCuPINTENB, 0);
- /* Switch off ports' power, some devices don't come up
- after next 'start' without this */
- val = isp116x_read_reg32(isp116x, HCRHDESCA);
- val &= ~(RH_A_NPS | RH_A_PSM);
- isp116x_write_reg32(isp116x, HCRHDESCA, val);
- isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
- isp116x_sw_reset(isp116x);
- }
- /*
- * Configure the chip. The chip must be successfully reset by now.
- */
- static int isp116x_start(struct isp116x *isp116x)
- {
- struct isp116x_platform_data *board = isp116x->board;
- u32 val;
- DBG("");
- /* Clear interrupt status and disable all interrupt sources */
- isp116x_write_reg16(isp116x, HCuPINT, 0xff);
- isp116x_write_reg16(isp116x, HCuPINTENB, 0);
- isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
- isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
- /* Hardware configuration */
- val = HCHWCFG_DBWIDTH(1);
- if (board->sel15Kres)
- val |= HCHWCFG_15KRSEL;
- /* Remote wakeup won't work without working clock */
- if (board->remote_wakeup_enable)
- val |= HCHWCFG_CLKNOTSTOP;
- if (board->oc_enable)
- val |= HCHWCFG_ANALOG_OC;
- isp116x_write_reg16(isp116x, HCHWCFG, val);
- /* --- Root hub configuration */
- val = (25 << 24) & RH_A_POTPGT;
- /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
- be always set. Yet, instead, we request individual port
- power switching. */
- val |= RH_A_PSM;
- /* Report overcurrent per port */
- val |= RH_A_OCPM;
- isp116x_write_reg32(isp116x, HCRHDESCA, val);
- isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
- val = RH_B_PPCM;
- isp116x_write_reg32(isp116x, HCRHDESCB, val);
- isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
- val = 0;
- if (board->remote_wakeup_enable)
- val |= RH_HS_DRWE;
- isp116x_write_reg32(isp116x, HCRHSTATUS, val);
- isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
- isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
- /* Go operational */
- val = HCCONTROL_USB_OPER;
- if (board->remote_wakeup_enable)
- val |= HCCONTROL_RWE;
- isp116x_write_reg32(isp116x, HCCONTROL, val);
- /* Disable ports to avoid race in device enumeration */
- isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
- isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
- isp116x_show_regs(isp116x);
- isp116x->disabled = 0;
- return 0;
- }
- /* --- Init functions ------------------------------------------------------ */
- int isp116x_check_id(struct isp116x *isp116x)
- {
- int val;
- val = isp116x_read_reg16(isp116x, HCCHIPID);
- if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
- ERR("invalid chip ID %04x", val);
- return -1;
- }
- return 0;
- }
- int usb_lowlevel_init(int index, enum usb_init_type init, void **controller))
- {
- struct isp116x *isp116x = &isp116x_dev;
- DBG("");
- got_rhsc = rh_devnum = 0;
- /* Init device registers addr */
- isp116x->addr_reg = (u16 *) ISP116X_HCD_ADDR;
- isp116x->data_reg = (u16 *) ISP116X_HCD_DATA;
- /* Setup specific board settings */
- #ifdef ISP116X_HCD_SEL15kRES
- isp116x_board.sel15Kres = 1;
- #endif
- #ifdef ISP116X_HCD_OC_ENABLE
- isp116x_board.oc_enable = 1;
- #endif
- #ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
- isp116x_board.remote_wakeup_enable = 1;
- #endif
- isp116x->board = &isp116x_board;
- /* Try to get ISP116x silicon chip ID */
- if (isp116x_check_id(isp116x) < 0)
- return -1;
- isp116x->disabled = 1;
- isp116x->sleeping = 0;
- isp116x_reset(isp116x);
- isp116x_start(isp116x);
- return 0;
- }
- int usb_lowlevel_stop(int index)
- {
- struct isp116x *isp116x = &isp116x_dev;
- DBG("");
- if (!isp116x->disabled)
- isp116x_stop(isp116x);
- return 0;
- }
|