isp1760-hcd.c 56 KB

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