isp1760-hcd.c 56 KB

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