isp1760-hcd.c 54 KB

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