isp1760-hcd.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  1. /*
  2. * Driver for the NXP ISP1760 chip
  3. *
  4. * However, the code might contain some bugs. What doesn't work for sure is:
  5. * - ISO
  6. * - OTG
  7. e The interrupt line is configured as active low, level.
  8. *
  9. * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de>
  10. *
  11. * (c) 2011 Arvid Brodin <arvid.brodin@enea.com>
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/usb.h>
  19. #include <linux/usb/hcd.h>
  20. #include <linux/debugfs.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/io.h>
  23. #include <linux/mm.h>
  24. #include <linux/timer.h>
  25. #include <asm/unaligned.h>
  26. #include <asm/cacheflush.h>
  27. #include <linux/gpio.h>
  28. #include "isp1760-hcd.h"
  29. static struct kmem_cache *qtd_cachep;
  30. static struct kmem_cache *qh_cachep;
  31. static struct kmem_cache *urb_listitem_cachep;
  32. struct isp1760_hcd {
  33. u32 hcs_params;
  34. spinlock_t lock;
  35. struct slotinfo atl_slots[32];
  36. int atl_done_map;
  37. struct slotinfo int_slots[32];
  38. int int_done_map;
  39. struct memory_chunk memory_pool[BLOCKS];
  40. struct list_head controlqhs, bulkqhs, interruptqhs;
  41. /* periodic schedule support */
  42. #define DEFAULT_I_TDPS 1024
  43. unsigned periodic_size;
  44. unsigned i_thresh;
  45. unsigned long reset_done;
  46. unsigned long next_statechange;
  47. unsigned int devflags;
  48. int rst_gpio;
  49. };
  50. static inline struct isp1760_hcd *hcd_to_priv(struct usb_hcd *hcd)
  51. {
  52. return (struct isp1760_hcd *) (hcd->hcd_priv);
  53. }
  54. /* Section 2.2 Host Controller Capability Registers */
  55. #define HC_LENGTH(p) (((p)>>00)&0x00ff) /* bits 7:0 */
  56. #define HC_VERSION(p) (((p)>>16)&0xffff) /* bits 31:16 */
  57. #define HCS_INDICATOR(p) ((p)&(1 << 16)) /* true: has port indicators */
  58. #define HCS_PPC(p) ((p)&(1 << 4)) /* true: port power control */
  59. #define HCS_N_PORTS(p) (((p)>>0)&0xf) /* bits 3:0, ports on HC */
  60. #define HCC_ISOC_CACHE(p) ((p)&(1 << 7)) /* true: can cache isoc frame */
  61. #define HCC_ISOC_THRES(p) (((p)>>4)&0x7) /* bits 6:4, uframes cached */
  62. /* Section 2.3 Host Controller Operational Registers */
  63. #define CMD_LRESET (1<<7) /* partial reset (no ports, etc) */
  64. #define CMD_RESET (1<<1) /* reset HC not bus */
  65. #define CMD_RUN (1<<0) /* start/stop HC */
  66. #define STS_PCD (1<<2) /* port change detect */
  67. #define FLAG_CF (1<<0) /* true: we'll support "high speed" */
  68. #define PORT_OWNER (1<<13) /* true: companion hc owns this port */
  69. #define PORT_POWER (1<<12) /* true: has power (see PPC) */
  70. #define PORT_USB11(x) (((x) & (3 << 10)) == (1 << 10)) /* USB 1.1 device */
  71. #define PORT_RESET (1<<8) /* reset port */
  72. #define PORT_SUSPEND (1<<7) /* suspend port */
  73. #define PORT_RESUME (1<<6) /* resume it */
  74. #define PORT_PE (1<<2) /* port enable */
  75. #define PORT_CSC (1<<1) /* connect status change */
  76. #define PORT_CONNECT (1<<0) /* device connected */
  77. #define PORT_RWC_BITS (PORT_CSC)
  78. struct isp1760_qtd {
  79. u8 packet_type;
  80. void *data_buffer;
  81. u32 payload_addr;
  82. /* the rest is HCD-private */
  83. struct list_head qtd_list;
  84. struct urb *urb;
  85. size_t length;
  86. size_t actual_length;
  87. /* QTD_ENQUEUED: waiting for transfer (inactive) */
  88. /* QTD_PAYLOAD_ALLOC: chip mem has been allocated for payload */
  89. /* QTD_XFER_STARTED: valid ptd has been written to isp176x - only
  90. interrupt handler may touch this qtd! */
  91. /* QTD_XFER_COMPLETE: payload has been transferred successfully */
  92. /* QTD_RETIRE: transfer error/abort qtd */
  93. #define QTD_ENQUEUED 0
  94. #define QTD_PAYLOAD_ALLOC 1
  95. #define QTD_XFER_STARTED 2
  96. #define QTD_XFER_COMPLETE 3
  97. #define QTD_RETIRE 4
  98. u32 status;
  99. };
  100. /* Queue head, one for each active endpoint */
  101. struct isp1760_qh {
  102. struct list_head qh_list;
  103. struct list_head qtd_list;
  104. u32 toggle;
  105. u32 ping;
  106. int slot;
  107. int tt_buffer_dirty; /* See USB2.0 spec section 11.17.5 */
  108. };
  109. struct urb_listitem {
  110. struct list_head urb_list;
  111. struct urb *urb;
  112. };
  113. /*
  114. * Access functions for isp176x registers (addresses 0..0x03FF).
  115. */
  116. static u32 reg_read32(void __iomem *base, u32 reg)
  117. {
  118. return readl(base + reg);
  119. }
  120. static void reg_write32(void __iomem *base, u32 reg, u32 val)
  121. {
  122. writel(val, base + reg);
  123. }
  124. /*
  125. * Access functions for isp176x memory (offset >= 0x0400).
  126. *
  127. * bank_reads8() reads memory locations prefetched by an earlier write to
  128. * HC_MEMORY_REG (see isp176x datasheet). Unless you want to do fancy multi-
  129. * bank optimizations, you should use the more generic mem_reads8() below.
  130. *
  131. * For access to ptd memory, use the specialized ptd_read() and ptd_write()
  132. * below.
  133. *
  134. * These functions copy via MMIO data to/from the device. memcpy_{to|from}io()
  135. * doesn't quite work because some people have to enforce 32-bit access
  136. */
  137. static void bank_reads8(void __iomem *src_base, u32 src_offset, u32 bank_addr,
  138. __u32 *dst, u32 bytes)
  139. {
  140. __u32 __iomem *src;
  141. u32 val;
  142. __u8 *src_byteptr;
  143. __u8 *dst_byteptr;
  144. src = src_base + (bank_addr | src_offset);
  145. if (src_offset < PAYLOAD_OFFSET) {
  146. while (bytes >= 4) {
  147. *dst = le32_to_cpu(__raw_readl(src));
  148. bytes -= 4;
  149. src++;
  150. dst++;
  151. }
  152. } else {
  153. while (bytes >= 4) {
  154. *dst = __raw_readl(src);
  155. bytes -= 4;
  156. src++;
  157. dst++;
  158. }
  159. }
  160. if (!bytes)
  161. return;
  162. /* in case we have 3, 2 or 1 by left. The dst buffer may not be fully
  163. * allocated.
  164. */
  165. if (src_offset < PAYLOAD_OFFSET)
  166. val = le32_to_cpu(__raw_readl(src));
  167. else
  168. val = __raw_readl(src);
  169. dst_byteptr = (void *) dst;
  170. src_byteptr = (void *) &val;
  171. while (bytes > 0) {
  172. *dst_byteptr = *src_byteptr;
  173. dst_byteptr++;
  174. src_byteptr++;
  175. bytes--;
  176. }
  177. }
  178. static void mem_reads8(void __iomem *src_base, u32 src_offset, void *dst,
  179. u32 bytes)
  180. {
  181. reg_write32(src_base, HC_MEMORY_REG, src_offset + ISP_BANK(0));
  182. ndelay(90);
  183. bank_reads8(src_base, src_offset, ISP_BANK(0), dst, bytes);
  184. }
  185. static void mem_writes8(void __iomem *dst_base, u32 dst_offset,
  186. __u32 const *src, u32 bytes)
  187. {
  188. __u32 __iomem *dst;
  189. dst = dst_base + dst_offset;
  190. if (dst_offset < PAYLOAD_OFFSET) {
  191. while (bytes >= 4) {
  192. __raw_writel(cpu_to_le32(*src), dst);
  193. bytes -= 4;
  194. src++;
  195. dst++;
  196. }
  197. } else {
  198. while (bytes >= 4) {
  199. __raw_writel(*src, dst);
  200. bytes -= 4;
  201. src++;
  202. dst++;
  203. }
  204. }
  205. if (!bytes)
  206. return;
  207. /* in case we have 3, 2 or 1 bytes left. The buffer is allocated and the
  208. * extra bytes should not be read by the HW.
  209. */
  210. if (dst_offset < PAYLOAD_OFFSET)
  211. __raw_writel(cpu_to_le32(*src), dst);
  212. else
  213. __raw_writel(*src, dst);
  214. }
  215. /*
  216. * Read and write ptds. 'ptd_offset' should be one of ISO_PTD_OFFSET,
  217. * INT_PTD_OFFSET, and ATL_PTD_OFFSET. 'slot' should be less than 32.
  218. */
  219. static void ptd_read(void __iomem *base, u32 ptd_offset, u32 slot,
  220. struct ptd *ptd)
  221. {
  222. reg_write32(base, HC_MEMORY_REG,
  223. ISP_BANK(0) + ptd_offset + slot*sizeof(*ptd));
  224. ndelay(90);
  225. bank_reads8(base, ptd_offset + slot*sizeof(*ptd), ISP_BANK(0),
  226. (void *) ptd, sizeof(*ptd));
  227. }
  228. static void ptd_write(void __iomem *base, u32 ptd_offset, u32 slot,
  229. struct ptd *ptd)
  230. {
  231. mem_writes8(base, ptd_offset + slot*sizeof(*ptd) + sizeof(ptd->dw0),
  232. &ptd->dw1, 7*sizeof(ptd->dw1));
  233. /* Make sure dw0 gets written last (after other dw's and after payload)
  234. since it contains the enable bit */
  235. wmb();
  236. mem_writes8(base, ptd_offset + slot*sizeof(*ptd), &ptd->dw0,
  237. sizeof(ptd->dw0));
  238. }
  239. /* memory management of the 60kb on the chip from 0x1000 to 0xffff */
  240. static void init_memory(struct isp1760_hcd *priv)
  241. {
  242. int i, curr;
  243. u32 payload_addr;
  244. payload_addr = PAYLOAD_OFFSET;
  245. for (i = 0; i < BLOCK_1_NUM; i++) {
  246. priv->memory_pool[i].start = payload_addr;
  247. priv->memory_pool[i].size = BLOCK_1_SIZE;
  248. priv->memory_pool[i].free = 1;
  249. payload_addr += priv->memory_pool[i].size;
  250. }
  251. curr = i;
  252. for (i = 0; i < BLOCK_2_NUM; i++) {
  253. priv->memory_pool[curr + i].start = payload_addr;
  254. priv->memory_pool[curr + i].size = BLOCK_2_SIZE;
  255. priv->memory_pool[curr + i].free = 1;
  256. payload_addr += priv->memory_pool[curr + i].size;
  257. }
  258. curr = i;
  259. for (i = 0; i < BLOCK_3_NUM; i++) {
  260. priv->memory_pool[curr + i].start = payload_addr;
  261. priv->memory_pool[curr + i].size = BLOCK_3_SIZE;
  262. priv->memory_pool[curr + i].free = 1;
  263. payload_addr += priv->memory_pool[curr + i].size;
  264. }
  265. WARN_ON(payload_addr - priv->memory_pool[0].start > PAYLOAD_AREA_SIZE);
  266. }
  267. static void alloc_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
  268. {
  269. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  270. int i;
  271. WARN_ON(qtd->payload_addr);
  272. if (!qtd->length)
  273. return;
  274. for (i = 0; i < BLOCKS; i++) {
  275. if (priv->memory_pool[i].size >= qtd->length &&
  276. priv->memory_pool[i].free) {
  277. priv->memory_pool[i].free = 0;
  278. qtd->payload_addr = priv->memory_pool[i].start;
  279. return;
  280. }
  281. }
  282. }
  283. static void free_mem(struct usb_hcd *hcd, struct isp1760_qtd *qtd)
  284. {
  285. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  286. int i;
  287. if (!qtd->payload_addr)
  288. return;
  289. for (i = 0; i < BLOCKS; i++) {
  290. if (priv->memory_pool[i].start == qtd->payload_addr) {
  291. WARN_ON(priv->memory_pool[i].free);
  292. priv->memory_pool[i].free = 1;
  293. qtd->payload_addr = 0;
  294. return;
  295. }
  296. }
  297. dev_err(hcd->self.controller, "%s: Invalid pointer: %08x\n",
  298. __func__, qtd->payload_addr);
  299. WARN_ON(1);
  300. qtd->payload_addr = 0;
  301. }
  302. static int handshake(struct usb_hcd *hcd, u32 reg,
  303. u32 mask, u32 done, int usec)
  304. {
  305. u32 result;
  306. do {
  307. result = reg_read32(hcd->regs, reg);
  308. if (result == ~0)
  309. return -ENODEV;
  310. result &= mask;
  311. if (result == done)
  312. return 0;
  313. udelay(1);
  314. usec--;
  315. } while (usec > 0);
  316. return -ETIMEDOUT;
  317. }
  318. /* reset a non-running (STS_HALT == 1) controller */
  319. static int ehci_reset(struct usb_hcd *hcd)
  320. {
  321. int retval;
  322. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  323. u32 command = reg_read32(hcd->regs, HC_USBCMD);
  324. command |= CMD_RESET;
  325. reg_write32(hcd->regs, HC_USBCMD, command);
  326. hcd->state = HC_STATE_HALT;
  327. priv->next_statechange = jiffies;
  328. retval = handshake(hcd, HC_USBCMD,
  329. CMD_RESET, 0, 250 * 1000);
  330. return retval;
  331. }
  332. static struct isp1760_qh *qh_alloc(gfp_t flags)
  333. {
  334. struct isp1760_qh *qh;
  335. qh = kmem_cache_zalloc(qh_cachep, flags);
  336. if (!qh)
  337. return NULL;
  338. INIT_LIST_HEAD(&qh->qh_list);
  339. INIT_LIST_HEAD(&qh->qtd_list);
  340. qh->slot = -1;
  341. return qh;
  342. }
  343. static void qh_free(struct isp1760_qh *qh)
  344. {
  345. WARN_ON(!list_empty(&qh->qtd_list));
  346. WARN_ON(qh->slot > -1);
  347. kmem_cache_free(qh_cachep, qh);
  348. }
  349. /* one-time init, only for memory state */
  350. static int priv_init(struct usb_hcd *hcd)
  351. {
  352. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  353. u32 hcc_params;
  354. spin_lock_init(&priv->lock);
  355. INIT_LIST_HEAD(&priv->interruptqhs);
  356. INIT_LIST_HEAD(&priv->controlqhs);
  357. INIT_LIST_HEAD(&priv->bulkqhs);
  358. /*
  359. * hw default: 1K periodic list heads, one per frame.
  360. * periodic_size can shrink by USBCMD update if hcc_params allows.
  361. */
  362. priv->periodic_size = DEFAULT_I_TDPS;
  363. /* controllers may cache some of the periodic schedule ... */
  364. hcc_params = reg_read32(hcd->regs, HC_HCCPARAMS);
  365. /* full frame cache */
  366. if (HCC_ISOC_CACHE(hcc_params))
  367. priv->i_thresh = 8;
  368. else /* N microframes cached */
  369. priv->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  370. return 0;
  371. }
  372. static int isp1760_hc_setup(struct usb_hcd *hcd)
  373. {
  374. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  375. int result;
  376. u32 scratch, hwmode;
  377. /* low-level chip reset */
  378. if (gpio_is_valid(priv->rst_gpio)) {
  379. unsigned int rst_lvl;
  380. rst_lvl = (priv->devflags &
  381. ISP1760_FLAG_RESET_ACTIVE_HIGH) ? 1 : 0;
  382. gpio_set_value(priv->rst_gpio, rst_lvl);
  383. mdelay(50);
  384. gpio_set_value(priv->rst_gpio, !rst_lvl);
  385. }
  386. /* Setup HW Mode Control: This assumes a level active-low interrupt */
  387. hwmode = HW_DATA_BUS_32BIT;
  388. if (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16)
  389. hwmode &= ~HW_DATA_BUS_32BIT;
  390. if (priv->devflags & ISP1760_FLAG_ANALOG_OC)
  391. hwmode |= HW_ANA_DIGI_OC;
  392. if (priv->devflags & ISP1760_FLAG_DACK_POL_HIGH)
  393. hwmode |= HW_DACK_POL_HIGH;
  394. if (priv->devflags & ISP1760_FLAG_DREQ_POL_HIGH)
  395. hwmode |= HW_DREQ_POL_HIGH;
  396. if (priv->devflags & ISP1760_FLAG_INTR_POL_HIGH)
  397. hwmode |= HW_INTR_HIGH_ACT;
  398. if (priv->devflags & ISP1760_FLAG_INTR_EDGE_TRIG)
  399. hwmode |= HW_INTR_EDGE_TRIG;
  400. /*
  401. * We have to set this first in case we're in 16-bit mode.
  402. * Write it twice to ensure correct upper bits if switching
  403. * to 16-bit mode.
  404. */
  405. reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
  406. reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
  407. reg_write32(hcd->regs, HC_SCRATCH_REG, 0xdeadbabe);
  408. /* Change bus pattern */
  409. scratch = reg_read32(hcd->regs, HC_CHIP_ID_REG);
  410. scratch = reg_read32(hcd->regs, HC_SCRATCH_REG);
  411. if (scratch != 0xdeadbabe) {
  412. dev_err(hcd->self.controller, "Scratch test failed.\n");
  413. return -ENODEV;
  414. }
  415. /* pre reset */
  416. reg_write32(hcd->regs, HC_BUFFER_STATUS_REG, 0);
  417. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
  418. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
  419. reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, NO_TRANSFER_ACTIVE);
  420. /* reset */
  421. reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_ALL);
  422. mdelay(100);
  423. reg_write32(hcd->regs, HC_RESET_REG, SW_RESET_RESET_HC);
  424. mdelay(100);
  425. result = ehci_reset(hcd);
  426. if (result)
  427. return result;
  428. /* Step 11 passed */
  429. dev_info(hcd->self.controller, "bus width: %d, oc: %s\n",
  430. (priv->devflags & ISP1760_FLAG_BUS_WIDTH_16) ?
  431. 16 : 32, (priv->devflags & ISP1760_FLAG_ANALOG_OC) ?
  432. "analog" : "digital");
  433. /* ATL reset */
  434. reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
  435. mdelay(10);
  436. reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
  437. reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
  438. /*
  439. * PORT 1 Control register of the ISP1760 is the OTG control
  440. * register on ISP1761. Since there is no OTG or device controller
  441. * support in this driver, we use port 1 as a "normal" USB host port on
  442. * both chips.
  443. */
  444. reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
  445. mdelay(10);
  446. priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
  447. return priv_init(hcd);
  448. }
  449. static u32 base_to_chip(u32 base)
  450. {
  451. return ((base - 0x400) >> 3);
  452. }
  453. static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
  454. {
  455. struct urb *urb;
  456. if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
  457. return 1;
  458. urb = qtd->urb;
  459. qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
  460. return (qtd->urb != urb);
  461. }
  462. /* magic numbers that can affect system performance */
  463. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  464. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  465. #define EHCI_TUNE_RL_TT 0
  466. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  467. #define EHCI_TUNE_MULT_TT 1
  468. #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
  469. static void create_ptd_atl(struct isp1760_qh *qh,
  470. struct isp1760_qtd *qtd, struct ptd *ptd)
  471. {
  472. u32 maxpacket;
  473. u32 multi;
  474. u32 rl = RL_COUNTER;
  475. u32 nak = NAK_COUNTER;
  476. memset(ptd, 0, sizeof(*ptd));
  477. /* according to 3.6.2, max packet len can not be > 0x400 */
  478. maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
  479. usb_pipeout(qtd->urb->pipe));
  480. multi = 1 + ((maxpacket >> 11) & 0x3);
  481. maxpacket &= 0x7ff;
  482. /* DW0 */
  483. ptd->dw0 = DW0_VALID_BIT;
  484. ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
  485. ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
  486. ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
  487. /* DW1 */
  488. ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
  489. ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
  490. ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
  491. if (usb_pipebulk(qtd->urb->pipe))
  492. ptd->dw1 |= DW1_TRANS_BULK;
  493. else if (usb_pipeint(qtd->urb->pipe))
  494. ptd->dw1 |= DW1_TRANS_INT;
  495. if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
  496. /* split transaction */
  497. ptd->dw1 |= DW1_TRANS_SPLIT;
  498. if (qtd->urb->dev->speed == USB_SPEED_LOW)
  499. ptd->dw1 |= DW1_SE_USB_LOSPEED;
  500. ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
  501. ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
  502. /* SE bit for Split INT transfers */
  503. if (usb_pipeint(qtd->urb->pipe) &&
  504. (qtd->urb->dev->speed == USB_SPEED_LOW))
  505. ptd->dw1 |= 2 << 16;
  506. rl = 0;
  507. nak = 0;
  508. } else {
  509. ptd->dw0 |= TO_DW0_MULTI(multi);
  510. if (usb_pipecontrol(qtd->urb->pipe) ||
  511. usb_pipebulk(qtd->urb->pipe))
  512. ptd->dw3 |= TO_DW3_PING(qh->ping);
  513. }
  514. /* DW2 */
  515. ptd->dw2 = 0;
  516. ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
  517. ptd->dw2 |= TO_DW2_RL(rl);
  518. /* DW3 */
  519. ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
  520. ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
  521. if (usb_pipecontrol(qtd->urb->pipe)) {
  522. if (qtd->data_buffer == qtd->urb->setup_packet)
  523. ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
  524. else if (last_qtd_of_urb(qtd, qh))
  525. ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
  526. }
  527. ptd->dw3 |= DW3_ACTIVE_BIT;
  528. /* Cerr */
  529. ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
  530. }
  531. static void transform_add_int(struct isp1760_qh *qh,
  532. struct isp1760_qtd *qtd, struct ptd *ptd)
  533. {
  534. u32 usof;
  535. u32 period;
  536. /*
  537. * Most of this is guessing. ISP1761 datasheet is quite unclear, and
  538. * the algorithm from the original Philips driver code, which was
  539. * pretty much used in this driver before as well, is quite horrendous
  540. * and, i believe, incorrect. The code below follows the datasheet and
  541. * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
  542. * more reliable this way (fingers crossed...).
  543. */
  544. if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
  545. /* urb->interval is in units of microframes (1/8 ms) */
  546. period = qtd->urb->interval >> 3;
  547. if (qtd->urb->interval > 4)
  548. usof = 0x01; /* One bit set =>
  549. interval 1 ms * uFrame-match */
  550. else if (qtd->urb->interval > 2)
  551. usof = 0x22; /* Two bits set => interval 1/2 ms */
  552. else if (qtd->urb->interval > 1)
  553. usof = 0x55; /* Four bits set => interval 1/4 ms */
  554. else
  555. usof = 0xff; /* All bits set => interval 1/8 ms */
  556. } else {
  557. /* urb->interval is in units of frames (1 ms) */
  558. period = qtd->urb->interval;
  559. usof = 0x0f; /* Execute Start Split on any of the
  560. four first uFrames */
  561. /*
  562. * First 8 bits in dw5 is uSCS and "specifies which uSOF the
  563. * complete split needs to be sent. Valid only for IN." Also,
  564. * "All bits can be set to one for every transfer." (p 82,
  565. * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
  566. * that number come from? 0xff seems to work fine...
  567. */
  568. /* ptd->dw5 = 0x1c; */
  569. ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
  570. }
  571. period = period >> 1;/* Ensure equal or shorter period than requested */
  572. period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
  573. ptd->dw2 |= period;
  574. ptd->dw4 = usof;
  575. }
  576. static void create_ptd_int(struct isp1760_qh *qh,
  577. struct isp1760_qtd *qtd, struct ptd *ptd)
  578. {
  579. create_ptd_atl(qh, qtd, ptd);
  580. transform_add_int(qh, qtd, ptd);
  581. }
  582. static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
  583. __releases(priv->lock)
  584. __acquires(priv->lock)
  585. {
  586. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  587. if (!urb->unlinked) {
  588. if (urb->status == -EINPROGRESS)
  589. urb->status = 0;
  590. }
  591. if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
  592. void *ptr;
  593. for (ptr = urb->transfer_buffer;
  594. ptr < urb->transfer_buffer + urb->transfer_buffer_length;
  595. ptr += PAGE_SIZE)
  596. flush_dcache_page(virt_to_page(ptr));
  597. }
  598. /* complete() can reenter this HCD */
  599. usb_hcd_unlink_urb_from_ep(hcd, urb);
  600. spin_unlock(&priv->lock);
  601. usb_hcd_giveback_urb(hcd, urb, urb->status);
  602. spin_lock(&priv->lock);
  603. }
  604. static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
  605. u8 packet_type)
  606. {
  607. struct isp1760_qtd *qtd;
  608. qtd = kmem_cache_zalloc(qtd_cachep, flags);
  609. if (!qtd)
  610. return NULL;
  611. INIT_LIST_HEAD(&qtd->qtd_list);
  612. qtd->urb = urb;
  613. qtd->packet_type = packet_type;
  614. qtd->status = QTD_ENQUEUED;
  615. qtd->actual_length = 0;
  616. return qtd;
  617. }
  618. static void qtd_free(struct isp1760_qtd *qtd)
  619. {
  620. WARN_ON(qtd->payload_addr);
  621. kmem_cache_free(qtd_cachep, qtd);
  622. }
  623. static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
  624. struct slotinfo *slots, struct isp1760_qtd *qtd,
  625. struct isp1760_qh *qh, struct ptd *ptd)
  626. {
  627. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  628. int skip_map;
  629. WARN_ON((slot < 0) || (slot > 31));
  630. WARN_ON(qtd->length && !qtd->payload_addr);
  631. WARN_ON(slots[slot].qtd);
  632. WARN_ON(slots[slot].qh);
  633. WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
  634. /* Make sure done map has not triggered from some unlinked transfer */
  635. if (ptd_offset == ATL_PTD_OFFSET) {
  636. priv->atl_done_map |= reg_read32(hcd->regs,
  637. HC_ATL_PTD_DONEMAP_REG);
  638. priv->atl_done_map &= ~(1 << slot);
  639. } else {
  640. priv->int_done_map |= reg_read32(hcd->regs,
  641. HC_INT_PTD_DONEMAP_REG);
  642. priv->int_done_map &= ~(1 << slot);
  643. }
  644. qh->slot = slot;
  645. qtd->status = QTD_XFER_STARTED;
  646. slots[slot].timestamp = jiffies;
  647. slots[slot].qtd = qtd;
  648. slots[slot].qh = qh;
  649. ptd_write(hcd->regs, ptd_offset, slot, ptd);
  650. if (ptd_offset == ATL_PTD_OFFSET) {
  651. skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  652. skip_map &= ~(1 << qh->slot);
  653. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
  654. } else {
  655. skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  656. skip_map &= ~(1 << qh->slot);
  657. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
  658. }
  659. }
  660. static int is_short_bulk(struct isp1760_qtd *qtd)
  661. {
  662. return (usb_pipebulk(qtd->urb->pipe) &&
  663. (qtd->actual_length < qtd->length));
  664. }
  665. static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
  666. struct list_head *urb_list)
  667. {
  668. int last_qtd;
  669. struct isp1760_qtd *qtd, *qtd_next;
  670. struct urb_listitem *urb_listitem;
  671. list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
  672. if (qtd->status < QTD_XFER_COMPLETE)
  673. break;
  674. last_qtd = last_qtd_of_urb(qtd, qh);
  675. if ((!last_qtd) && (qtd->status == QTD_RETIRE))
  676. qtd_next->status = QTD_RETIRE;
  677. if (qtd->status == QTD_XFER_COMPLETE) {
  678. if (qtd->actual_length) {
  679. switch (qtd->packet_type) {
  680. case IN_PID:
  681. mem_reads8(hcd->regs, qtd->payload_addr,
  682. qtd->data_buffer,
  683. qtd->actual_length);
  684. /* Fall through (?) */
  685. case OUT_PID:
  686. qtd->urb->actual_length +=
  687. qtd->actual_length;
  688. /* Fall through ... */
  689. case SETUP_PID:
  690. break;
  691. }
  692. }
  693. if (is_short_bulk(qtd)) {
  694. if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
  695. qtd->urb->status = -EREMOTEIO;
  696. if (!last_qtd)
  697. qtd_next->status = QTD_RETIRE;
  698. }
  699. }
  700. if (qtd->payload_addr)
  701. free_mem(hcd, qtd);
  702. if (last_qtd) {
  703. if ((qtd->status == QTD_RETIRE) &&
  704. (qtd->urb->status == -EINPROGRESS))
  705. qtd->urb->status = -EPIPE;
  706. /* Defer calling of urb_done() since it releases lock */
  707. urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
  708. GFP_ATOMIC);
  709. if (unlikely(!urb_listitem))
  710. break; /* Try again on next call */
  711. urb_listitem->urb = qtd->urb;
  712. list_add_tail(&urb_listitem->urb_list, urb_list);
  713. }
  714. list_del(&qtd->qtd_list);
  715. qtd_free(qtd);
  716. }
  717. }
  718. #define ENQUEUE_DEPTH 2
  719. static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
  720. {
  721. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  722. int ptd_offset;
  723. struct slotinfo *slots;
  724. int curr_slot, free_slot;
  725. int n;
  726. struct ptd ptd;
  727. struct isp1760_qtd *qtd;
  728. if (unlikely(list_empty(&qh->qtd_list))) {
  729. WARN_ON(1);
  730. return;
  731. }
  732. /* Make sure this endpoint's TT buffer is clean before queueing ptds */
  733. if (qh->tt_buffer_dirty)
  734. return;
  735. if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
  736. qtd_list)->urb->pipe)) {
  737. ptd_offset = INT_PTD_OFFSET;
  738. slots = priv->int_slots;
  739. } else {
  740. ptd_offset = ATL_PTD_OFFSET;
  741. slots = priv->atl_slots;
  742. }
  743. free_slot = -1;
  744. for (curr_slot = 0; curr_slot < 32; curr_slot++) {
  745. if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
  746. free_slot = curr_slot;
  747. if (slots[curr_slot].qh == qh)
  748. break;
  749. }
  750. n = 0;
  751. list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  752. if (qtd->status == QTD_ENQUEUED) {
  753. WARN_ON(qtd->payload_addr);
  754. alloc_mem(hcd, qtd);
  755. if ((qtd->length) && (!qtd->payload_addr))
  756. break;
  757. if ((qtd->length) &&
  758. ((qtd->packet_type == SETUP_PID) ||
  759. (qtd->packet_type == OUT_PID))) {
  760. mem_writes8(hcd->regs, qtd->payload_addr,
  761. qtd->data_buffer, qtd->length);
  762. }
  763. qtd->status = QTD_PAYLOAD_ALLOC;
  764. }
  765. if (qtd->status == QTD_PAYLOAD_ALLOC) {
  766. /*
  767. if ((curr_slot > 31) && (free_slot == -1))
  768. dev_dbg(hcd->self.controller, "%s: No slot "
  769. "available for transfer\n", __func__);
  770. */
  771. /* Start xfer for this endpoint if not already done */
  772. if ((curr_slot > 31) && (free_slot > -1)) {
  773. if (usb_pipeint(qtd->urb->pipe))
  774. create_ptd_int(qh, qtd, &ptd);
  775. else
  776. create_ptd_atl(qh, qtd, &ptd);
  777. start_bus_transfer(hcd, ptd_offset, free_slot,
  778. slots, qtd, qh, &ptd);
  779. curr_slot = free_slot;
  780. }
  781. n++;
  782. if (n >= ENQUEUE_DEPTH)
  783. break;
  784. }
  785. }
  786. }
  787. void schedule_ptds(struct usb_hcd *hcd)
  788. {
  789. struct isp1760_hcd *priv;
  790. struct isp1760_qh *qh, *qh_next;
  791. struct list_head *ep_queue;
  792. struct usb_host_endpoint *ep;
  793. LIST_HEAD(urb_list);
  794. struct urb_listitem *urb_listitem, *urb_listitem_next;
  795. if (!hcd) {
  796. WARN_ON(1);
  797. return;
  798. }
  799. priv = hcd_to_priv(hcd);
  800. /*
  801. * check finished/retired xfers, transfer payloads, call urb_done()
  802. */
  803. ep_queue = &priv->interruptqhs;
  804. while (ep_queue) {
  805. list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
  806. ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
  807. qtd_list)->urb->ep;
  808. collect_qtds(hcd, qh, &urb_list);
  809. if (list_empty(&qh->qtd_list)) {
  810. list_del(&qh->qh_list);
  811. if (ep->hcpriv == NULL) {
  812. /* Endpoint has been disabled, so we
  813. can free the associated queue head. */
  814. qh_free(qh);
  815. }
  816. }
  817. }
  818. if (ep_queue == &priv->interruptqhs)
  819. ep_queue = &priv->controlqhs;
  820. else if (ep_queue == &priv->controlqhs)
  821. ep_queue = &priv->bulkqhs;
  822. else
  823. ep_queue = NULL;
  824. }
  825. list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
  826. urb_list) {
  827. isp1760_urb_done(hcd, urb_listitem->urb);
  828. kmem_cache_free(urb_listitem_cachep, urb_listitem);
  829. }
  830. /*
  831. * Schedule packets for transfer.
  832. *
  833. * According to USB2.0 specification:
  834. *
  835. * 1st prio: interrupt xfers, up to 80 % of bandwidth
  836. * 2nd prio: control xfers
  837. * 3rd prio: bulk xfers
  838. *
  839. * ... but let's use a simpler scheme here (mostly because ISP1761 doc
  840. * is very unclear on how to prioritize traffic):
  841. *
  842. * 1) Enqueue any queued control transfers, as long as payload chip mem
  843. * and PTD ATL slots are available.
  844. * 2) Enqueue any queued INT transfers, as long as payload chip mem
  845. * and PTD INT slots are available.
  846. * 3) Enqueue any queued bulk transfers, as long as payload chip mem
  847. * and PTD ATL slots are available.
  848. *
  849. * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
  850. * conservation of chip mem and performance.
  851. *
  852. * I'm sure this scheme could be improved upon!
  853. */
  854. ep_queue = &priv->controlqhs;
  855. while (ep_queue) {
  856. list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
  857. enqueue_qtds(hcd, qh);
  858. if (ep_queue == &priv->controlqhs)
  859. ep_queue = &priv->interruptqhs;
  860. else if (ep_queue == &priv->interruptqhs)
  861. ep_queue = &priv->bulkqhs;
  862. else
  863. ep_queue = NULL;
  864. }
  865. }
  866. #define PTD_STATE_QTD_DONE 1
  867. #define PTD_STATE_QTD_RELOAD 2
  868. #define PTD_STATE_URB_RETIRE 3
  869. static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
  870. struct urb *urb)
  871. {
  872. __dw dw4;
  873. int i;
  874. dw4 = ptd->dw4;
  875. dw4 >>= 8;
  876. /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
  877. need to handle these errors? Is it done in hardware? */
  878. if (ptd->dw3 & DW3_HALT_BIT) {
  879. urb->status = -EPROTO; /* Default unknown error */
  880. for (i = 0; i < 8; i++) {
  881. switch (dw4 & 0x7) {
  882. case INT_UNDERRUN:
  883. dev_dbg(hcd->self.controller, "%s: underrun "
  884. "during uFrame %d\n",
  885. __func__, i);
  886. urb->status = -ECOMM; /* Could not write data */
  887. break;
  888. case INT_EXACT:
  889. dev_dbg(hcd->self.controller, "%s: transaction "
  890. "error during uFrame %d\n",
  891. __func__, i);
  892. urb->status = -EPROTO; /* timeout, bad CRC, PID
  893. error etc. */
  894. break;
  895. case INT_BABBLE:
  896. dev_dbg(hcd->self.controller, "%s: babble "
  897. "error during uFrame %d\n",
  898. __func__, i);
  899. urb->status = -EOVERFLOW;
  900. break;
  901. }
  902. dw4 >>= 3;
  903. }
  904. return PTD_STATE_URB_RETIRE;
  905. }
  906. return PTD_STATE_QTD_DONE;
  907. }
  908. static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
  909. struct urb *urb)
  910. {
  911. WARN_ON(!ptd);
  912. if (ptd->dw3 & DW3_HALT_BIT) {
  913. if (ptd->dw3 & DW3_BABBLE_BIT)
  914. urb->status = -EOVERFLOW;
  915. else if (FROM_DW3_CERR(ptd->dw3))
  916. urb->status = -EPIPE; /* Stall */
  917. else if (ptd->dw3 & DW3_ERROR_BIT)
  918. urb->status = -EPROTO; /* XactErr */
  919. else
  920. urb->status = -EPROTO; /* Unknown */
  921. /*
  922. dev_dbg(hcd->self.controller, "%s: ptd error:\n"
  923. " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
  924. " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
  925. __func__,
  926. ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
  927. ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
  928. */
  929. return PTD_STATE_URB_RETIRE;
  930. }
  931. if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
  932. /* Transfer Error, *but* active and no HALT -> reload */
  933. dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
  934. return PTD_STATE_QTD_RELOAD;
  935. }
  936. if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
  937. /*
  938. * NAKs are handled in HW by the chip. Usually if the
  939. * device is not able to send data fast enough.
  940. * This happens mostly on slower hardware.
  941. */
  942. return PTD_STATE_QTD_RELOAD;
  943. }
  944. return PTD_STATE_QTD_DONE;
  945. }
  946. static void handle_done_ptds(struct usb_hcd *hcd)
  947. {
  948. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  949. struct ptd ptd;
  950. struct isp1760_qh *qh;
  951. int slot;
  952. int state;
  953. struct slotinfo *slots;
  954. u32 ptd_offset;
  955. struct isp1760_qtd *qtd;
  956. int modified;
  957. int skip_map;
  958. skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  959. priv->int_done_map &= ~skip_map;
  960. skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  961. priv->atl_done_map &= ~skip_map;
  962. modified = priv->int_done_map || priv->atl_done_map;
  963. while (priv->int_done_map || priv->atl_done_map) {
  964. if (priv->int_done_map) {
  965. /* INT ptd */
  966. slot = __ffs(priv->int_done_map);
  967. priv->int_done_map &= ~(1 << slot);
  968. slots = priv->int_slots;
  969. /* This should not trigger, and could be removed if
  970. noone have any problems with it triggering: */
  971. if (!slots[slot].qh) {
  972. WARN_ON(1);
  973. continue;
  974. }
  975. ptd_offset = INT_PTD_OFFSET;
  976. ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
  977. state = check_int_transfer(hcd, &ptd,
  978. slots[slot].qtd->urb);
  979. } else {
  980. /* ATL ptd */
  981. slot = __ffs(priv->atl_done_map);
  982. priv->atl_done_map &= ~(1 << slot);
  983. slots = priv->atl_slots;
  984. /* This should not trigger, and could be removed if
  985. noone have any problems with it triggering: */
  986. if (!slots[slot].qh) {
  987. WARN_ON(1);
  988. continue;
  989. }
  990. ptd_offset = ATL_PTD_OFFSET;
  991. ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
  992. state = check_atl_transfer(hcd, &ptd,
  993. slots[slot].qtd->urb);
  994. }
  995. qtd = slots[slot].qtd;
  996. slots[slot].qtd = NULL;
  997. qh = slots[slot].qh;
  998. slots[slot].qh = NULL;
  999. qh->slot = -1;
  1000. WARN_ON(qtd->status != QTD_XFER_STARTED);
  1001. switch (state) {
  1002. case PTD_STATE_QTD_DONE:
  1003. if ((usb_pipeint(qtd->urb->pipe)) &&
  1004. (qtd->urb->dev->speed != USB_SPEED_HIGH))
  1005. qtd->actual_length =
  1006. FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
  1007. else
  1008. qtd->actual_length =
  1009. FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
  1010. qtd->status = QTD_XFER_COMPLETE;
  1011. if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
  1012. is_short_bulk(qtd))
  1013. qtd = NULL;
  1014. else
  1015. qtd = list_entry(qtd->qtd_list.next,
  1016. typeof(*qtd), qtd_list);
  1017. qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
  1018. qh->ping = FROM_DW3_PING(ptd.dw3);
  1019. break;
  1020. case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
  1021. qtd->status = QTD_PAYLOAD_ALLOC;
  1022. ptd.dw0 |= DW0_VALID_BIT;
  1023. /* RL counter = ERR counter */
  1024. ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
  1025. ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
  1026. ptd.dw3 &= ~TO_DW3_CERR(3);
  1027. ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
  1028. qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
  1029. qh->ping = FROM_DW3_PING(ptd.dw3);
  1030. break;
  1031. case PTD_STATE_URB_RETIRE:
  1032. qtd->status = QTD_RETIRE;
  1033. if ((qtd->urb->dev->speed != USB_SPEED_HIGH) &&
  1034. (qtd->urb->status != -EPIPE) &&
  1035. (qtd->urb->status != -EREMOTEIO)) {
  1036. qh->tt_buffer_dirty = 1;
  1037. if (usb_hub_clear_tt_buffer(qtd->urb))
  1038. /* Clear failed; let's hope things work
  1039. anyway */
  1040. qh->tt_buffer_dirty = 0;
  1041. }
  1042. qtd = NULL;
  1043. qh->toggle = 0;
  1044. qh->ping = 0;
  1045. break;
  1046. default:
  1047. WARN_ON(1);
  1048. continue;
  1049. }
  1050. if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
  1051. if (slots == priv->int_slots) {
  1052. if (state == PTD_STATE_QTD_RELOAD)
  1053. dev_err(hcd->self.controller,
  1054. "%s: PTD_STATE_QTD_RELOAD on "
  1055. "interrupt packet\n", __func__);
  1056. if (state != PTD_STATE_QTD_RELOAD)
  1057. create_ptd_int(qh, qtd, &ptd);
  1058. } else {
  1059. if (state != PTD_STATE_QTD_RELOAD)
  1060. create_ptd_atl(qh, qtd, &ptd);
  1061. }
  1062. start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
  1063. qh, &ptd);
  1064. }
  1065. }
  1066. if (modified)
  1067. schedule_ptds(hcd);
  1068. }
  1069. static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
  1070. {
  1071. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1072. u32 imask;
  1073. irqreturn_t irqret = IRQ_NONE;
  1074. spin_lock(&priv->lock);
  1075. if (!(hcd->state & HC_STATE_RUNNING))
  1076. goto leave;
  1077. imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
  1078. if (unlikely(!imask))
  1079. goto leave;
  1080. reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
  1081. priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
  1082. priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
  1083. handle_done_ptds(hcd);
  1084. irqret = IRQ_HANDLED;
  1085. leave:
  1086. spin_unlock(&priv->lock);
  1087. return irqret;
  1088. }
  1089. /*
  1090. * Workaround for problem described in chip errata 2:
  1091. *
  1092. * Sometimes interrupts are not generated when ATL (not INT?) completion occurs.
  1093. * One solution suggested in the errata is to use SOF interrupts _instead_of_
  1094. * ATL done interrupts (the "instead of" might be important since it seems
  1095. * enabling ATL interrupts also causes the chip to sometimes - rarely - "forget"
  1096. * to set the PTD's done bit in addition to not generating an interrupt!).
  1097. *
  1098. * So if we use SOF + ATL interrupts, we sometimes get stale PTDs since their
  1099. * done bit is not being set. This is bad - it blocks the endpoint until reboot.
  1100. *
  1101. * If we use SOF interrupts only, we get latency between ptd completion and the
  1102. * actual handling. This is very noticeable in testusb runs which takes several
  1103. * minutes longer without ATL interrupts.
  1104. *
  1105. * A better solution is to run the code below every SLOT_CHECK_PERIOD ms. If it
  1106. * finds active ATL slots which are older than SLOT_TIMEOUT ms, it checks the
  1107. * slot's ACTIVE and VALID bits. If these are not set, the ptd is considered
  1108. * completed and its done map bit is set.
  1109. *
  1110. * The values of SLOT_TIMEOUT and SLOT_CHECK_PERIOD have been arbitrarily chosen
  1111. * not to cause too much lag when this HW bug occurs, while still hopefully
  1112. * ensuring that the check does not falsely trigger.
  1113. */
  1114. #define SLOT_TIMEOUT 300
  1115. #define SLOT_CHECK_PERIOD 200
  1116. static struct timer_list errata2_timer;
  1117. void errata2_function(unsigned long data)
  1118. {
  1119. struct usb_hcd *hcd = (struct usb_hcd *) data;
  1120. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1121. int slot;
  1122. struct ptd ptd;
  1123. unsigned long spinflags;
  1124. spin_lock_irqsave(&priv->lock, spinflags);
  1125. for (slot = 0; slot < 32; slot++)
  1126. if (priv->atl_slots[slot].qh && time_after(jiffies,
  1127. priv->atl_slots[slot].timestamp +
  1128. SLOT_TIMEOUT * HZ / 1000)) {
  1129. ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
  1130. if (!FROM_DW0_VALID(ptd.dw0) &&
  1131. !FROM_DW3_ACTIVE(ptd.dw3))
  1132. priv->atl_done_map |= 1 << slot;
  1133. }
  1134. if (priv->atl_done_map)
  1135. handle_done_ptds(hcd);
  1136. spin_unlock_irqrestore(&priv->lock, spinflags);
  1137. errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
  1138. add_timer(&errata2_timer);
  1139. }
  1140. static int isp1760_run(struct usb_hcd *hcd)
  1141. {
  1142. int retval;
  1143. u32 temp;
  1144. u32 command;
  1145. u32 chipid;
  1146. hcd->uses_new_polling = 1;
  1147. hcd->state = HC_STATE_RUNNING;
  1148. /* Set PTD interrupt AND & OR maps */
  1149. reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
  1150. reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
  1151. reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
  1152. reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
  1153. reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
  1154. reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
  1155. /* step 23 passed */
  1156. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  1157. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
  1158. command = reg_read32(hcd->regs, HC_USBCMD);
  1159. command &= ~(CMD_LRESET|CMD_RESET);
  1160. command |= CMD_RUN;
  1161. reg_write32(hcd->regs, HC_USBCMD, command);
  1162. retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
  1163. if (retval)
  1164. return retval;
  1165. /*
  1166. * XXX
  1167. * Spec says to write FLAG_CF as last config action, priv code grabs
  1168. * the semaphore while doing so.
  1169. */
  1170. down_write(&ehci_cf_port_reset_rwsem);
  1171. reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
  1172. retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
  1173. up_write(&ehci_cf_port_reset_rwsem);
  1174. if (retval)
  1175. return retval;
  1176. init_timer(&errata2_timer);
  1177. errata2_timer.function = errata2_function;
  1178. errata2_timer.data = (unsigned long) hcd;
  1179. errata2_timer.expires = jiffies + SLOT_CHECK_PERIOD * HZ / 1000;
  1180. add_timer(&errata2_timer);
  1181. chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
  1182. dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
  1183. chipid & 0xffff, chipid >> 16);
  1184. /* PTD Register Init Part 2, Step 28 */
  1185. /* Setup registers controlling PTD checking */
  1186. reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
  1187. reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
  1188. reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
  1189. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
  1190. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
  1191. reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
  1192. reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
  1193. ATL_BUF_FILL | INT_BUF_FILL);
  1194. /* GRR this is run-once init(), being done every time the HC starts.
  1195. * So long as they're part of class devices, we can't do it init()
  1196. * since the class device isn't created that early.
  1197. */
  1198. return 0;
  1199. }
  1200. static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
  1201. {
  1202. qtd->data_buffer = databuffer;
  1203. if (len > MAX_PAYLOAD_SIZE)
  1204. len = MAX_PAYLOAD_SIZE;
  1205. qtd->length = len;
  1206. return qtd->length;
  1207. }
  1208. static void qtd_list_free(struct list_head *qtd_list)
  1209. {
  1210. struct isp1760_qtd *qtd, *qtd_next;
  1211. list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
  1212. list_del(&qtd->qtd_list);
  1213. qtd_free(qtd);
  1214. }
  1215. }
  1216. /*
  1217. * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
  1218. * Also calculate the PID type (SETUP/IN/OUT) for each packet.
  1219. */
  1220. #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  1221. static void packetize_urb(struct usb_hcd *hcd,
  1222. struct urb *urb, struct list_head *head, gfp_t flags)
  1223. {
  1224. struct isp1760_qtd *qtd;
  1225. void *buf;
  1226. int len, maxpacketsize;
  1227. u8 packet_type;
  1228. /*
  1229. * URBs map to sequences of QTDs: one logical transaction
  1230. */
  1231. if (!urb->transfer_buffer && urb->transfer_buffer_length) {
  1232. /* XXX This looks like usb storage / SCSI bug */
  1233. dev_err(hcd->self.controller,
  1234. "buf is null, dma is %08lx len is %d\n",
  1235. (long unsigned)urb->transfer_dma,
  1236. urb->transfer_buffer_length);
  1237. WARN_ON(1);
  1238. }
  1239. if (usb_pipein(urb->pipe))
  1240. packet_type = IN_PID;
  1241. else
  1242. packet_type = OUT_PID;
  1243. if (usb_pipecontrol(urb->pipe)) {
  1244. qtd = qtd_alloc(flags, urb, SETUP_PID);
  1245. if (!qtd)
  1246. goto cleanup;
  1247. qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
  1248. list_add_tail(&qtd->qtd_list, head);
  1249. /* for zero length DATA stages, STATUS is always IN */
  1250. if (urb->transfer_buffer_length == 0)
  1251. packet_type = IN_PID;
  1252. }
  1253. maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
  1254. usb_pipeout(urb->pipe)));
  1255. /*
  1256. * buffer gets wrapped in one or more qtds;
  1257. * last one may be "short" (including zero len)
  1258. * and may serve as a control status ack
  1259. */
  1260. buf = urb->transfer_buffer;
  1261. len = urb->transfer_buffer_length;
  1262. for (;;) {
  1263. int this_qtd_len;
  1264. qtd = qtd_alloc(flags, urb, packet_type);
  1265. if (!qtd)
  1266. goto cleanup;
  1267. this_qtd_len = qtd_fill(qtd, buf, len);
  1268. list_add_tail(&qtd->qtd_list, head);
  1269. len -= this_qtd_len;
  1270. buf += this_qtd_len;
  1271. if (len <= 0)
  1272. break;
  1273. }
  1274. /*
  1275. * control requests may need a terminating data "status" ack;
  1276. * bulk ones may need a terminating short packet (zero length).
  1277. */
  1278. if (urb->transfer_buffer_length != 0) {
  1279. int one_more = 0;
  1280. if (usb_pipecontrol(urb->pipe)) {
  1281. one_more = 1;
  1282. if (packet_type == IN_PID)
  1283. packet_type = OUT_PID;
  1284. else
  1285. packet_type = IN_PID;
  1286. } else if (usb_pipebulk(urb->pipe)
  1287. && (urb->transfer_flags & URB_ZERO_PACKET)
  1288. && !(urb->transfer_buffer_length %
  1289. maxpacketsize)) {
  1290. one_more = 1;
  1291. }
  1292. if (one_more) {
  1293. qtd = qtd_alloc(flags, urb, packet_type);
  1294. if (!qtd)
  1295. goto cleanup;
  1296. /* never any data in such packets */
  1297. qtd_fill(qtd, NULL, 0);
  1298. list_add_tail(&qtd->qtd_list, head);
  1299. }
  1300. }
  1301. return;
  1302. cleanup:
  1303. qtd_list_free(head);
  1304. }
  1305. static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  1306. gfp_t mem_flags)
  1307. {
  1308. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1309. struct list_head *ep_queue;
  1310. struct isp1760_qh *qh, *qhit;
  1311. unsigned long spinflags;
  1312. LIST_HEAD(new_qtds);
  1313. int retval;
  1314. int qh_in_queue;
  1315. switch (usb_pipetype(urb->pipe)) {
  1316. case PIPE_CONTROL:
  1317. ep_queue = &priv->controlqhs;
  1318. break;
  1319. case PIPE_BULK:
  1320. ep_queue = &priv->bulkqhs;
  1321. break;
  1322. case PIPE_INTERRUPT:
  1323. if (urb->interval < 0)
  1324. return -EINVAL;
  1325. /* FIXME: Check bandwidth */
  1326. ep_queue = &priv->interruptqhs;
  1327. break;
  1328. case PIPE_ISOCHRONOUS:
  1329. dev_err(hcd->self.controller, "%s: isochronous USB packets "
  1330. "not yet supported\n",
  1331. __func__);
  1332. return -EPIPE;
  1333. default:
  1334. dev_err(hcd->self.controller, "%s: unknown pipe type\n",
  1335. __func__);
  1336. return -EPIPE;
  1337. }
  1338. if (usb_pipein(urb->pipe))
  1339. urb->actual_length = 0;
  1340. packetize_urb(hcd, urb, &new_qtds, mem_flags);
  1341. if (list_empty(&new_qtds))
  1342. return -ENOMEM;
  1343. retval = 0;
  1344. spin_lock_irqsave(&priv->lock, spinflags);
  1345. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  1346. retval = -ESHUTDOWN;
  1347. goto out;
  1348. }
  1349. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  1350. if (retval)
  1351. goto out;
  1352. qh = urb->ep->hcpriv;
  1353. if (qh) {
  1354. qh_in_queue = 0;
  1355. list_for_each_entry(qhit, ep_queue, qh_list) {
  1356. if (qhit == qh) {
  1357. qh_in_queue = 1;
  1358. break;
  1359. }
  1360. }
  1361. if (!qh_in_queue)
  1362. list_add_tail(&qh->qh_list, ep_queue);
  1363. } else {
  1364. qh = qh_alloc(GFP_ATOMIC);
  1365. if (!qh) {
  1366. retval = -ENOMEM;
  1367. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1368. goto out;
  1369. }
  1370. list_add_tail(&qh->qh_list, ep_queue);
  1371. urb->ep->hcpriv = qh;
  1372. }
  1373. list_splice_tail(&new_qtds, &qh->qtd_list);
  1374. schedule_ptds(hcd);
  1375. out:
  1376. spin_unlock_irqrestore(&priv->lock, spinflags);
  1377. return retval;
  1378. }
  1379. static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
  1380. struct isp1760_qh *qh)
  1381. {
  1382. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1383. int skip_map;
  1384. WARN_ON(qh->slot == -1);
  1385. /* We need to forcefully reclaim the slot since some transfers never
  1386. return, e.g. interrupt transfers and NAKed bulk transfers. */
  1387. if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
  1388. skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  1389. skip_map |= (1 << qh->slot);
  1390. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
  1391. priv->atl_slots[qh->slot].qh = NULL;
  1392. priv->atl_slots[qh->slot].qtd = NULL;
  1393. } else {
  1394. skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  1395. skip_map |= (1 << qh->slot);
  1396. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
  1397. priv->int_slots[qh->slot].qh = NULL;
  1398. priv->int_slots[qh->slot].qtd = NULL;
  1399. }
  1400. qh->slot = -1;
  1401. }
  1402. /*
  1403. * Retire the qtds beginning at 'qtd' and belonging all to the same urb, killing
  1404. * any active transfer belonging to the urb in the process.
  1405. */
  1406. static void dequeue_urb_from_qtd(struct usb_hcd *hcd, struct isp1760_qh *qh,
  1407. struct isp1760_qtd *qtd)
  1408. {
  1409. struct urb *urb;
  1410. int urb_was_running;
  1411. urb = qtd->urb;
  1412. urb_was_running = 0;
  1413. list_for_each_entry_from(qtd, &qh->qtd_list, qtd_list) {
  1414. if (qtd->urb != urb)
  1415. break;
  1416. if (qtd->status >= QTD_XFER_STARTED)
  1417. urb_was_running = 1;
  1418. if (last_qtd_of_urb(qtd, qh) &&
  1419. (qtd->status >= QTD_XFER_COMPLETE))
  1420. urb_was_running = 0;
  1421. if (qtd->status == QTD_XFER_STARTED)
  1422. kill_transfer(hcd, urb, qh);
  1423. qtd->status = QTD_RETIRE;
  1424. }
  1425. if ((urb->dev->speed != USB_SPEED_HIGH) && urb_was_running) {
  1426. qh->tt_buffer_dirty = 1;
  1427. if (usb_hub_clear_tt_buffer(urb))
  1428. /* Clear failed; let's hope things work anyway */
  1429. qh->tt_buffer_dirty = 0;
  1430. }
  1431. }
  1432. static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  1433. int status)
  1434. {
  1435. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1436. unsigned long spinflags;
  1437. struct isp1760_qh *qh;
  1438. struct isp1760_qtd *qtd;
  1439. int retval = 0;
  1440. spin_lock_irqsave(&priv->lock, spinflags);
  1441. retval = usb_hcd_check_unlink_urb(hcd, urb, status);
  1442. if (retval)
  1443. goto out;
  1444. qh = urb->ep->hcpriv;
  1445. if (!qh) {
  1446. retval = -EINVAL;
  1447. goto out;
  1448. }
  1449. list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
  1450. if (qtd->urb == urb) {
  1451. dequeue_urb_from_qtd(hcd, qh, qtd);
  1452. break;
  1453. }
  1454. urb->status = status;
  1455. schedule_ptds(hcd);
  1456. out:
  1457. spin_unlock_irqrestore(&priv->lock, spinflags);
  1458. return retval;
  1459. }
  1460. static void isp1760_endpoint_disable(struct usb_hcd *hcd,
  1461. struct usb_host_endpoint *ep)
  1462. {
  1463. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1464. unsigned long spinflags;
  1465. struct isp1760_qh *qh;
  1466. struct isp1760_qtd *qtd;
  1467. spin_lock_irqsave(&priv->lock, spinflags);
  1468. qh = ep->hcpriv;
  1469. if (!qh)
  1470. goto out;
  1471. list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
  1472. if (qtd->status != QTD_RETIRE) {
  1473. dequeue_urb_from_qtd(hcd, qh, qtd);
  1474. qtd->urb->status = -ECONNRESET;
  1475. }
  1476. ep->hcpriv = NULL;
  1477. /* Cannot free qh here since it will be parsed by schedule_ptds() */
  1478. schedule_ptds(hcd);
  1479. out:
  1480. spin_unlock_irqrestore(&priv->lock, spinflags);
  1481. }
  1482. static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
  1483. {
  1484. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1485. u32 temp, status = 0;
  1486. u32 mask;
  1487. int retval = 1;
  1488. unsigned long flags;
  1489. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  1490. if (!HC_IS_RUNNING(hcd->state))
  1491. return 0;
  1492. /* init status to no-changes */
  1493. buf[0] = 0;
  1494. mask = PORT_CSC;
  1495. spin_lock_irqsave(&priv->lock, flags);
  1496. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1497. if (temp & PORT_OWNER) {
  1498. if (temp & PORT_CSC) {
  1499. temp &= ~PORT_CSC;
  1500. reg_write32(hcd->regs, HC_PORTSC1, temp);
  1501. goto done;
  1502. }
  1503. }
  1504. /*
  1505. * Return status information even for ports with OWNER set.
  1506. * Otherwise khubd wouldn't see the disconnect event when a
  1507. * high-speed device is switched over to the companion
  1508. * controller by the user.
  1509. */
  1510. if ((temp & mask) != 0
  1511. || ((temp & PORT_RESUME) != 0
  1512. && time_after_eq(jiffies,
  1513. priv->reset_done))) {
  1514. buf [0] |= 1 << (0 + 1);
  1515. status = STS_PCD;
  1516. }
  1517. /* FIXME autosuspend idle root hubs */
  1518. done:
  1519. spin_unlock_irqrestore(&priv->lock, flags);
  1520. return status ? retval : 0;
  1521. }
  1522. static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
  1523. struct usb_hub_descriptor *desc)
  1524. {
  1525. int ports = HCS_N_PORTS(priv->hcs_params);
  1526. u16 temp;
  1527. desc->bDescriptorType = 0x29;
  1528. /* priv 1.0, 2.3.9 says 20ms max */
  1529. desc->bPwrOn2PwrGood = 10;
  1530. desc->bHubContrCurrent = 0;
  1531. desc->bNbrPorts = ports;
  1532. temp = 1 + (ports / 8);
  1533. desc->bDescLength = 7 + 2 * temp;
  1534. /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  1535. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  1536. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  1537. /* per-port overcurrent reporting */
  1538. temp = 0x0008;
  1539. if (HCS_PPC(priv->hcs_params))
  1540. /* per-port power control */
  1541. temp |= 0x0001;
  1542. else
  1543. /* no power switching */
  1544. temp |= 0x0002;
  1545. desc->wHubCharacteristics = cpu_to_le16(temp);
  1546. }
  1547. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  1548. static int check_reset_complete(struct usb_hcd *hcd, int index,
  1549. int port_status)
  1550. {
  1551. if (!(port_status & PORT_CONNECT))
  1552. return port_status;
  1553. /* if reset finished and it's still not enabled -- handoff */
  1554. if (!(port_status & PORT_PE)) {
  1555. dev_info(hcd->self.controller,
  1556. "port %d full speed --> companion\n",
  1557. index + 1);
  1558. port_status |= PORT_OWNER;
  1559. port_status &= ~PORT_RWC_BITS;
  1560. reg_write32(hcd->regs, HC_PORTSC1, port_status);
  1561. } else
  1562. dev_info(hcd->self.controller, "port %d high speed\n",
  1563. index + 1);
  1564. return port_status;
  1565. }
  1566. static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
  1567. u16 wValue, u16 wIndex, char *buf, u16 wLength)
  1568. {
  1569. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1570. int ports = HCS_N_PORTS(priv->hcs_params);
  1571. u32 temp, status;
  1572. unsigned long flags;
  1573. int retval = 0;
  1574. unsigned selector;
  1575. /*
  1576. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  1577. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  1578. * (track current state ourselves) ... blink for diagnostics,
  1579. * power, "this is the one", etc. EHCI spec supports this.
  1580. */
  1581. spin_lock_irqsave(&priv->lock, flags);
  1582. switch (typeReq) {
  1583. case ClearHubFeature:
  1584. switch (wValue) {
  1585. case C_HUB_LOCAL_POWER:
  1586. case C_HUB_OVER_CURRENT:
  1587. /* no hub-wide feature/status flags */
  1588. break;
  1589. default:
  1590. goto error;
  1591. }
  1592. break;
  1593. case ClearPortFeature:
  1594. if (!wIndex || wIndex > ports)
  1595. goto error;
  1596. wIndex--;
  1597. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1598. /*
  1599. * Even if OWNER is set, so the port is owned by the
  1600. * companion controller, khubd needs to be able to clear
  1601. * the port-change status bits (especially
  1602. * USB_PORT_STAT_C_CONNECTION).
  1603. */
  1604. switch (wValue) {
  1605. case USB_PORT_FEAT_ENABLE:
  1606. reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
  1607. break;
  1608. case USB_PORT_FEAT_C_ENABLE:
  1609. /* XXX error? */
  1610. break;
  1611. case USB_PORT_FEAT_SUSPEND:
  1612. if (temp & PORT_RESET)
  1613. goto error;
  1614. if (temp & PORT_SUSPEND) {
  1615. if ((temp & PORT_PE) == 0)
  1616. goto error;
  1617. /* resume signaling for 20 msec */
  1618. temp &= ~(PORT_RWC_BITS);
  1619. reg_write32(hcd->regs, HC_PORTSC1,
  1620. temp | PORT_RESUME);
  1621. priv->reset_done = jiffies +
  1622. msecs_to_jiffies(20);
  1623. }
  1624. break;
  1625. case USB_PORT_FEAT_C_SUSPEND:
  1626. /* we auto-clear this feature */
  1627. break;
  1628. case USB_PORT_FEAT_POWER:
  1629. if (HCS_PPC(priv->hcs_params))
  1630. reg_write32(hcd->regs, HC_PORTSC1,
  1631. temp & ~PORT_POWER);
  1632. break;
  1633. case USB_PORT_FEAT_C_CONNECTION:
  1634. reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
  1635. break;
  1636. case USB_PORT_FEAT_C_OVER_CURRENT:
  1637. /* XXX error ?*/
  1638. break;
  1639. case USB_PORT_FEAT_C_RESET:
  1640. /* GetPortStatus clears reset */
  1641. break;
  1642. default:
  1643. goto error;
  1644. }
  1645. reg_read32(hcd->regs, HC_USBCMD);
  1646. break;
  1647. case GetHubDescriptor:
  1648. isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
  1649. buf);
  1650. break;
  1651. case GetHubStatus:
  1652. /* no hub-wide feature/status flags */
  1653. memset(buf, 0, 4);
  1654. break;
  1655. case GetPortStatus:
  1656. if (!wIndex || wIndex > ports)
  1657. goto error;
  1658. wIndex--;
  1659. status = 0;
  1660. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1661. /* wPortChange bits */
  1662. if (temp & PORT_CSC)
  1663. status |= USB_PORT_STAT_C_CONNECTION << 16;
  1664. /* whoever resumes must GetPortStatus to complete it!! */
  1665. if (temp & PORT_RESUME) {
  1666. dev_err(hcd->self.controller, "Port resume should be skipped.\n");
  1667. /* Remote Wakeup received? */
  1668. if (!priv->reset_done) {
  1669. /* resume signaling for 20 msec */
  1670. priv->reset_done = jiffies
  1671. + msecs_to_jiffies(20);
  1672. /* check the port again */
  1673. mod_timer(&hcd->rh_timer, priv->reset_done);
  1674. }
  1675. /* resume completed? */
  1676. else if (time_after_eq(jiffies,
  1677. priv->reset_done)) {
  1678. status |= USB_PORT_STAT_C_SUSPEND << 16;
  1679. priv->reset_done = 0;
  1680. /* stop resume signaling */
  1681. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1682. reg_write32(hcd->regs, HC_PORTSC1,
  1683. temp & ~(PORT_RWC_BITS | PORT_RESUME));
  1684. retval = handshake(hcd, HC_PORTSC1,
  1685. PORT_RESUME, 0, 2000 /* 2msec */);
  1686. if (retval != 0) {
  1687. dev_err(hcd->self.controller,
  1688. "port %d resume error %d\n",
  1689. wIndex + 1, retval);
  1690. goto error;
  1691. }
  1692. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  1693. }
  1694. }
  1695. /* whoever resets must GetPortStatus to complete it!! */
  1696. if ((temp & PORT_RESET)
  1697. && time_after_eq(jiffies,
  1698. priv->reset_done)) {
  1699. status |= USB_PORT_STAT_C_RESET << 16;
  1700. priv->reset_done = 0;
  1701. /* force reset to complete */
  1702. reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
  1703. /* REVISIT: some hardware needs 550+ usec to clear
  1704. * this bit; seems too long to spin routinely...
  1705. */
  1706. retval = handshake(hcd, HC_PORTSC1,
  1707. PORT_RESET, 0, 750);
  1708. if (retval != 0) {
  1709. dev_err(hcd->self.controller, "port %d reset error %d\n",
  1710. wIndex + 1, retval);
  1711. goto error;
  1712. }
  1713. /* see what we found out */
  1714. temp = check_reset_complete(hcd, wIndex,
  1715. reg_read32(hcd->regs, HC_PORTSC1));
  1716. }
  1717. /*
  1718. * Even if OWNER is set, there's no harm letting khubd
  1719. * see the wPortStatus values (they should all be 0 except
  1720. * for PORT_POWER anyway).
  1721. */
  1722. if (temp & PORT_OWNER)
  1723. dev_err(hcd->self.controller, "PORT_OWNER is set\n");
  1724. if (temp & PORT_CONNECT) {
  1725. status |= USB_PORT_STAT_CONNECTION;
  1726. /* status may be from integrated TT */
  1727. status |= USB_PORT_STAT_HIGH_SPEED;
  1728. }
  1729. if (temp & PORT_PE)
  1730. status |= USB_PORT_STAT_ENABLE;
  1731. if (temp & (PORT_SUSPEND|PORT_RESUME))
  1732. status |= USB_PORT_STAT_SUSPEND;
  1733. if (temp & PORT_RESET)
  1734. status |= USB_PORT_STAT_RESET;
  1735. if (temp & PORT_POWER)
  1736. status |= USB_PORT_STAT_POWER;
  1737. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  1738. break;
  1739. case SetHubFeature:
  1740. switch (wValue) {
  1741. case C_HUB_LOCAL_POWER:
  1742. case C_HUB_OVER_CURRENT:
  1743. /* no hub-wide feature/status flags */
  1744. break;
  1745. default:
  1746. goto error;
  1747. }
  1748. break;
  1749. case SetPortFeature:
  1750. selector = wIndex >> 8;
  1751. wIndex &= 0xff;
  1752. if (!wIndex || wIndex > ports)
  1753. goto error;
  1754. wIndex--;
  1755. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1756. if (temp & PORT_OWNER)
  1757. break;
  1758. /* temp &= ~PORT_RWC_BITS; */
  1759. switch (wValue) {
  1760. case USB_PORT_FEAT_ENABLE:
  1761. reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
  1762. break;
  1763. case USB_PORT_FEAT_SUSPEND:
  1764. if ((temp & PORT_PE) == 0
  1765. || (temp & PORT_RESET) != 0)
  1766. goto error;
  1767. reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
  1768. break;
  1769. case USB_PORT_FEAT_POWER:
  1770. if (HCS_PPC(priv->hcs_params))
  1771. reg_write32(hcd->regs, HC_PORTSC1,
  1772. temp | PORT_POWER);
  1773. break;
  1774. case USB_PORT_FEAT_RESET:
  1775. if (temp & PORT_RESUME)
  1776. goto error;
  1777. /* line status bits may report this as low speed,
  1778. * which can be fine if this root hub has a
  1779. * transaction translator built in.
  1780. */
  1781. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  1782. && PORT_USB11(temp)) {
  1783. temp |= PORT_OWNER;
  1784. } else {
  1785. temp |= PORT_RESET;
  1786. temp &= ~PORT_PE;
  1787. /*
  1788. * caller must wait, then call GetPortStatus
  1789. * usb 2.0 spec says 50 ms resets on root
  1790. */
  1791. priv->reset_done = jiffies +
  1792. msecs_to_jiffies(50);
  1793. }
  1794. reg_write32(hcd->regs, HC_PORTSC1, temp);
  1795. break;
  1796. default:
  1797. goto error;
  1798. }
  1799. reg_read32(hcd->regs, HC_USBCMD);
  1800. break;
  1801. default:
  1802. error:
  1803. /* "stall" on error */
  1804. retval = -EPIPE;
  1805. }
  1806. spin_unlock_irqrestore(&priv->lock, flags);
  1807. return retval;
  1808. }
  1809. static int isp1760_get_frame(struct usb_hcd *hcd)
  1810. {
  1811. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1812. u32 fr;
  1813. fr = reg_read32(hcd->regs, HC_FRINDEX);
  1814. return (fr >> 3) % priv->periodic_size;
  1815. }
  1816. static void isp1760_stop(struct usb_hcd *hcd)
  1817. {
  1818. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1819. u32 temp;
  1820. del_timer(&errata2_timer);
  1821. isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
  1822. NULL, 0);
  1823. mdelay(20);
  1824. spin_lock_irq(&priv->lock);
  1825. ehci_reset(hcd);
  1826. /* Disable IRQ */
  1827. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  1828. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
  1829. spin_unlock_irq(&priv->lock);
  1830. reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
  1831. }
  1832. static void isp1760_shutdown(struct usb_hcd *hcd)
  1833. {
  1834. u32 command, temp;
  1835. isp1760_stop(hcd);
  1836. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  1837. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
  1838. command = reg_read32(hcd->regs, HC_USBCMD);
  1839. command &= ~CMD_RUN;
  1840. reg_write32(hcd->regs, HC_USBCMD, command);
  1841. }
  1842. static void isp1760_clear_tt_buffer_complete(struct usb_hcd *hcd,
  1843. struct usb_host_endpoint *ep)
  1844. {
  1845. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1846. struct isp1760_qh *qh = ep->hcpriv;
  1847. unsigned long spinflags;
  1848. if (!qh)
  1849. return;
  1850. spin_lock_irqsave(&priv->lock, spinflags);
  1851. qh->tt_buffer_dirty = 0;
  1852. schedule_ptds(hcd);
  1853. spin_unlock_irqrestore(&priv->lock, spinflags);
  1854. }
  1855. static const struct hc_driver isp1760_hc_driver = {
  1856. .description = "isp1760-hcd",
  1857. .product_desc = "NXP ISP1760 USB Host Controller",
  1858. .hcd_priv_size = sizeof(struct isp1760_hcd),
  1859. .irq = isp1760_irq,
  1860. .flags = HCD_MEMORY | HCD_USB2,
  1861. .reset = isp1760_hc_setup,
  1862. .start = isp1760_run,
  1863. .stop = isp1760_stop,
  1864. .shutdown = isp1760_shutdown,
  1865. .urb_enqueue = isp1760_urb_enqueue,
  1866. .urb_dequeue = isp1760_urb_dequeue,
  1867. .endpoint_disable = isp1760_endpoint_disable,
  1868. .get_frame_number = isp1760_get_frame,
  1869. .hub_status_data = isp1760_hub_status_data,
  1870. .hub_control = isp1760_hub_control,
  1871. .clear_tt_buffer_complete = isp1760_clear_tt_buffer_complete,
  1872. };
  1873. int __init init_kmem_once(void)
  1874. {
  1875. urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem",
  1876. sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
  1877. SLAB_MEM_SPREAD, NULL);
  1878. if (!urb_listitem_cachep)
  1879. return -ENOMEM;
  1880. qtd_cachep = kmem_cache_create("isp1760_qtd",
  1881. sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
  1882. SLAB_MEM_SPREAD, NULL);
  1883. if (!qtd_cachep)
  1884. return -ENOMEM;
  1885. qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
  1886. 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
  1887. if (!qh_cachep) {
  1888. kmem_cache_destroy(qtd_cachep);
  1889. return -ENOMEM;
  1890. }
  1891. return 0;
  1892. }
  1893. void deinit_kmem_cache(void)
  1894. {
  1895. kmem_cache_destroy(qtd_cachep);
  1896. kmem_cache_destroy(qh_cachep);
  1897. kmem_cache_destroy(urb_listitem_cachep);
  1898. }
  1899. struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
  1900. int irq, unsigned long irqflags,
  1901. int rst_gpio,
  1902. struct device *dev, const char *busname,
  1903. unsigned int devflags)
  1904. {
  1905. struct usb_hcd *hcd;
  1906. struct isp1760_hcd *priv;
  1907. int ret;
  1908. if (usb_disabled())
  1909. return ERR_PTR(-ENODEV);
  1910. /* prevent usb-core allocating DMA pages */
  1911. dev->dma_mask = NULL;
  1912. hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
  1913. if (!hcd)
  1914. return ERR_PTR(-ENOMEM);
  1915. priv = hcd_to_priv(hcd);
  1916. priv->devflags = devflags;
  1917. priv->rst_gpio = rst_gpio;
  1918. init_memory(priv);
  1919. hcd->regs = ioremap(res_start, res_len);
  1920. if (!hcd->regs) {
  1921. ret = -EIO;
  1922. goto err_put;
  1923. }
  1924. hcd->irq = irq;
  1925. hcd->rsrc_start = res_start;
  1926. hcd->rsrc_len = res_len;
  1927. ret = usb_add_hcd(hcd, irq, irqflags);
  1928. if (ret)
  1929. goto err_unmap;
  1930. return hcd;
  1931. err_unmap:
  1932. iounmap(hcd->regs);
  1933. err_put:
  1934. usb_put_hcd(hcd);
  1935. return ERR_PTR(ret);
  1936. }
  1937. MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
  1938. MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
  1939. MODULE_LICENSE("GPL v2");