isp1760-hcd.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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 <asm/unaligned.h>
  25. #include <asm/cacheflush.h>
  26. #include "isp1760-hcd.h"
  27. static struct kmem_cache *qtd_cachep;
  28. static struct kmem_cache *qh_cachep;
  29. static struct kmem_cache *urb_listitem_cachep;
  30. struct isp1760_hcd {
  31. u32 hcs_params;
  32. spinlock_t lock;
  33. struct slotinfo atl_slots[32];
  34. int atl_done_map;
  35. struct slotinfo int_slots[32];
  36. int int_done_map;
  37. struct memory_chunk memory_pool[BLOCKS];
  38. struct list_head controlqhs, bulkqhs, interruptqhs;
  39. int active_ptds;
  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. /* This is weird: at the first plug-in of a device there seems to be
  422. one packet queued that never gets returned? */
  423. priv->active_ptds = -1;
  424. /* ATL reset */
  425. reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode | ALL_ATX_RESET);
  426. mdelay(10);
  427. reg_write32(hcd->regs, HC_HW_MODE_CTRL, hwmode);
  428. reg_write32(hcd->regs, HC_INTERRUPT_ENABLE, INTERRUPT_ENABLE_MASK);
  429. /*
  430. * PORT 1 Control register of the ISP1760 is the OTG control
  431. * register on ISP1761. Since there is no OTG or device controller
  432. * support in this driver, we use port 1 as a "normal" USB host port on
  433. * both chips.
  434. */
  435. reg_write32(hcd->regs, HC_PORT1_CTRL, PORT1_POWER | PORT1_INIT2);
  436. mdelay(10);
  437. priv->hcs_params = reg_read32(hcd->regs, HC_HCSPARAMS);
  438. return priv_init(hcd);
  439. }
  440. static void isp1760_init_maps(struct usb_hcd *hcd)
  441. {
  442. /*set last maps, for iso its only 1, else 32 tds bitmap*/
  443. reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
  444. reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
  445. reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
  446. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
  447. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
  448. reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
  449. reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
  450. ATL_BUF_FILL | INT_BUF_FILL);
  451. }
  452. static void isp1760_enable_interrupts(struct usb_hcd *hcd)
  453. {
  454. reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
  455. reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
  456. reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
  457. reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
  458. reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
  459. reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
  460. /* step 23 passed */
  461. }
  462. static int isp1760_run(struct usb_hcd *hcd)
  463. {
  464. int retval;
  465. u32 temp;
  466. u32 command;
  467. u32 chipid;
  468. hcd->uses_new_polling = 1;
  469. hcd->state = HC_STATE_RUNNING;
  470. isp1760_enable_interrupts(hcd);
  471. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  472. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
  473. command = reg_read32(hcd->regs, HC_USBCMD);
  474. command &= ~(CMD_LRESET|CMD_RESET);
  475. command |= CMD_RUN;
  476. reg_write32(hcd->regs, HC_USBCMD, command);
  477. retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
  478. if (retval)
  479. return retval;
  480. /*
  481. * XXX
  482. * Spec says to write FLAG_CF as last config action, priv code grabs
  483. * the semaphore while doing so.
  484. */
  485. down_write(&ehci_cf_port_reset_rwsem);
  486. reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
  487. retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
  488. up_write(&ehci_cf_port_reset_rwsem);
  489. if (retval)
  490. return retval;
  491. chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
  492. dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
  493. chipid & 0xffff, chipid >> 16);
  494. /* PTD Register Init Part 2, Step 28 */
  495. /* enable INTs */
  496. isp1760_init_maps(hcd);
  497. /* GRR this is run-once init(), being done every time the HC starts.
  498. * So long as they're part of class devices, we can't do it init()
  499. * since the class device isn't created that early.
  500. */
  501. return 0;
  502. }
  503. static u32 base_to_chip(u32 base)
  504. {
  505. return ((base - 0x400) >> 3);
  506. }
  507. static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
  508. {
  509. struct urb *urb;
  510. if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
  511. return 1;
  512. urb = qtd->urb;
  513. qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
  514. return (qtd->urb != urb);
  515. }
  516. /* magic numbers that can affect system performance */
  517. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  518. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  519. #define EHCI_TUNE_RL_TT 0
  520. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  521. #define EHCI_TUNE_MULT_TT 1
  522. #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
  523. static void create_ptd_atl(struct isp1760_qh *qh,
  524. struct isp1760_qtd *qtd, struct ptd *ptd)
  525. {
  526. u32 maxpacket;
  527. u32 multi;
  528. u32 rl = RL_COUNTER;
  529. u32 nak = NAK_COUNTER;
  530. memset(ptd, 0, sizeof(*ptd));
  531. /* according to 3.6.2, max packet len can not be > 0x400 */
  532. maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
  533. usb_pipeout(qtd->urb->pipe));
  534. multi = 1 + ((maxpacket >> 11) & 0x3);
  535. maxpacket &= 0x7ff;
  536. /* DW0 */
  537. ptd->dw0 = DW0_VALID_BIT;
  538. ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
  539. ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
  540. ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
  541. /* DW1 */
  542. ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
  543. ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
  544. ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
  545. if (usb_pipebulk(qtd->urb->pipe))
  546. ptd->dw1 |= DW1_TRANS_BULK;
  547. else if (usb_pipeint(qtd->urb->pipe))
  548. ptd->dw1 |= DW1_TRANS_INT;
  549. if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
  550. /* split transaction */
  551. ptd->dw1 |= DW1_TRANS_SPLIT;
  552. if (qtd->urb->dev->speed == USB_SPEED_LOW)
  553. ptd->dw1 |= DW1_SE_USB_LOSPEED;
  554. ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
  555. ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
  556. /* SE bit for Split INT transfers */
  557. if (usb_pipeint(qtd->urb->pipe) &&
  558. (qtd->urb->dev->speed == USB_SPEED_LOW))
  559. ptd->dw1 |= 2 << 16;
  560. rl = 0;
  561. nak = 0;
  562. } else {
  563. ptd->dw0 |= TO_DW0_MULTI(multi);
  564. if (usb_pipecontrol(qtd->urb->pipe) ||
  565. usb_pipebulk(qtd->urb->pipe))
  566. ptd->dw3 |= TO_DW3_PING(qh->ping);
  567. }
  568. /* DW2 */
  569. ptd->dw2 = 0;
  570. ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
  571. ptd->dw2 |= TO_DW2_RL(rl);
  572. /* DW3 */
  573. ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
  574. ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
  575. if (usb_pipecontrol(qtd->urb->pipe)) {
  576. if (qtd->data_buffer == qtd->urb->setup_packet)
  577. ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
  578. else if (last_qtd_of_urb(qtd, qh))
  579. ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
  580. }
  581. ptd->dw3 |= DW3_ACTIVE_BIT;
  582. /* Cerr */
  583. ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
  584. }
  585. static void transform_add_int(struct isp1760_qh *qh,
  586. struct isp1760_qtd *qtd, struct ptd *ptd)
  587. {
  588. u32 usof;
  589. u32 period;
  590. /*
  591. * Most of this is guessing. ISP1761 datasheet is quite unclear, and
  592. * the algorithm from the original Philips driver code, which was
  593. * pretty much used in this driver before as well, is quite horrendous
  594. * and, i believe, incorrect. The code below follows the datasheet and
  595. * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
  596. * more reliable this way (fingers crossed...).
  597. */
  598. if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
  599. /* urb->interval is in units of microframes (1/8 ms) */
  600. period = qtd->urb->interval >> 3;
  601. if (qtd->urb->interval > 4)
  602. usof = 0x01; /* One bit set =>
  603. interval 1 ms * uFrame-match */
  604. else if (qtd->urb->interval > 2)
  605. usof = 0x22; /* Two bits set => interval 1/2 ms */
  606. else if (qtd->urb->interval > 1)
  607. usof = 0x55; /* Four bits set => interval 1/4 ms */
  608. else
  609. usof = 0xff; /* All bits set => interval 1/8 ms */
  610. } else {
  611. /* urb->interval is in units of frames (1 ms) */
  612. period = qtd->urb->interval;
  613. usof = 0x0f; /* Execute Start Split on any of the
  614. four first uFrames */
  615. /*
  616. * First 8 bits in dw5 is uSCS and "specifies which uSOF the
  617. * complete split needs to be sent. Valid only for IN." Also,
  618. * "All bits can be set to one for every transfer." (p 82,
  619. * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
  620. * that number come from? 0xff seems to work fine...
  621. */
  622. /* ptd->dw5 = 0x1c; */
  623. ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
  624. }
  625. period = period >> 1;/* Ensure equal or shorter period than requested */
  626. period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
  627. ptd->dw2 |= period;
  628. ptd->dw4 = usof;
  629. }
  630. static void create_ptd_int(struct isp1760_qh *qh,
  631. struct isp1760_qtd *qtd, struct ptd *ptd)
  632. {
  633. create_ptd_atl(qh, qtd, ptd);
  634. transform_add_int(qh, qtd, ptd);
  635. }
  636. static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
  637. __releases(priv->lock)
  638. __acquires(priv->lock)
  639. {
  640. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  641. if (!urb->unlinked) {
  642. if (urb->status == -EINPROGRESS)
  643. urb->status = 0;
  644. }
  645. if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
  646. void *ptr;
  647. for (ptr = urb->transfer_buffer;
  648. ptr < urb->transfer_buffer + urb->transfer_buffer_length;
  649. ptr += PAGE_SIZE)
  650. flush_dcache_page(virt_to_page(ptr));
  651. }
  652. /* complete() can reenter this HCD */
  653. usb_hcd_unlink_urb_from_ep(hcd, urb);
  654. spin_unlock(&priv->lock);
  655. usb_hcd_giveback_urb(hcd, urb, urb->status);
  656. spin_lock(&priv->lock);
  657. }
  658. static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
  659. u8 packet_type)
  660. {
  661. struct isp1760_qtd *qtd;
  662. qtd = kmem_cache_zalloc(qtd_cachep, flags);
  663. if (!qtd)
  664. return NULL;
  665. INIT_LIST_HEAD(&qtd->qtd_list);
  666. qtd->urb = urb;
  667. qtd->packet_type = packet_type;
  668. qtd->status = QTD_ENQUEUED;
  669. qtd->actual_length = 0;
  670. return qtd;
  671. }
  672. static void qtd_free(struct isp1760_qtd *qtd)
  673. {
  674. WARN_ON(qtd->payload_addr);
  675. kmem_cache_free(qtd_cachep, qtd);
  676. }
  677. static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
  678. struct slotinfo *slots, struct isp1760_qtd *qtd,
  679. struct isp1760_qh *qh, struct ptd *ptd)
  680. {
  681. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  682. int skip_map;
  683. WARN_ON((slot < 0) || (slot > 31));
  684. WARN_ON(qtd->length && !qtd->payload_addr);
  685. WARN_ON(slots[slot].qtd);
  686. WARN_ON(slots[slot].qh);
  687. WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
  688. slots[slot].qtd = qtd;
  689. slots[slot].qh = qh;
  690. qh->slot = slot;
  691. qtd->status = QTD_XFER_STARTED; /* Set this before writing ptd, since
  692. interrupt routine may preempt and expects this value. */
  693. ptd_write(hcd->regs, ptd_offset, slot, ptd);
  694. priv->active_ptds++;
  695. /* Make sure done map has not triggered from some unlinked transfer */
  696. if (ptd_offset == ATL_PTD_OFFSET) {
  697. priv->atl_done_map |= reg_read32(hcd->regs,
  698. HC_ATL_PTD_DONEMAP_REG);
  699. priv->atl_done_map &= ~(1 << qh->slot);
  700. skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  701. skip_map &= ~(1 << qh->slot);
  702. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
  703. } else {
  704. priv->int_done_map |= reg_read32(hcd->regs,
  705. HC_INT_PTD_DONEMAP_REG);
  706. priv->int_done_map &= ~(1 << qh->slot);
  707. skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  708. skip_map &= ~(1 << qh->slot);
  709. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
  710. }
  711. }
  712. static int is_short_bulk(struct isp1760_qtd *qtd)
  713. {
  714. return (usb_pipebulk(qtd->urb->pipe) &&
  715. (qtd->actual_length < qtd->length));
  716. }
  717. static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
  718. struct list_head *urb_list)
  719. {
  720. int last_qtd;
  721. struct isp1760_qtd *qtd, *qtd_next;
  722. struct urb_listitem *urb_listitem;
  723. list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
  724. if (qtd->status < QTD_XFER_COMPLETE)
  725. break;
  726. if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
  727. last_qtd = 1;
  728. else
  729. last_qtd = qtd->urb != qtd_next->urb;
  730. if ((!last_qtd) && (qtd->status == QTD_RETIRE))
  731. qtd_next->status = QTD_RETIRE;
  732. if (qtd->status == QTD_XFER_COMPLETE) {
  733. if (qtd->actual_length) {
  734. switch (qtd->packet_type) {
  735. case IN_PID:
  736. mem_reads8(hcd->regs, qtd->payload_addr,
  737. qtd->data_buffer,
  738. qtd->actual_length);
  739. /* Fall through (?) */
  740. case OUT_PID:
  741. qtd->urb->actual_length +=
  742. qtd->actual_length;
  743. /* Fall through ... */
  744. case SETUP_PID:
  745. break;
  746. }
  747. }
  748. if (is_short_bulk(qtd)) {
  749. if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
  750. qtd->urb->status = -EREMOTEIO;
  751. if (!last_qtd)
  752. qtd_next->status = QTD_RETIRE;
  753. }
  754. }
  755. if (qtd->payload_addr)
  756. free_mem(hcd, qtd);
  757. if (last_qtd) {
  758. if ((qtd->status == QTD_RETIRE) &&
  759. (qtd->urb->status == -EINPROGRESS))
  760. qtd->urb->status = -EPIPE;
  761. /* Defer calling of urb_done() since it releases lock */
  762. urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
  763. GFP_ATOMIC);
  764. if (unlikely(!urb_listitem))
  765. break;
  766. urb_listitem->urb = qtd->urb;
  767. list_add_tail(&urb_listitem->urb_list, urb_list);
  768. }
  769. list_del(&qtd->qtd_list);
  770. qtd_free(qtd);
  771. }
  772. }
  773. #define ENQUEUE_DEPTH 2
  774. static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
  775. {
  776. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  777. int ptd_offset;
  778. struct slotinfo *slots;
  779. int curr_slot, free_slot;
  780. int n;
  781. struct ptd ptd;
  782. struct isp1760_qtd *qtd;
  783. if (unlikely(list_empty(&qh->qtd_list))) {
  784. WARN_ON(1);
  785. return;
  786. }
  787. if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
  788. qtd_list)->urb->pipe)) {
  789. ptd_offset = INT_PTD_OFFSET;
  790. slots = priv->int_slots;
  791. } else {
  792. ptd_offset = ATL_PTD_OFFSET;
  793. slots = priv->atl_slots;
  794. }
  795. free_slot = -1;
  796. for (curr_slot = 0; curr_slot < 32; curr_slot++) {
  797. if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
  798. free_slot = curr_slot;
  799. if (slots[curr_slot].qh == qh)
  800. break;
  801. }
  802. n = 0;
  803. list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  804. if (qtd->status == QTD_ENQUEUED) {
  805. WARN_ON(qtd->payload_addr);
  806. alloc_mem(hcd, qtd);
  807. if ((qtd->length) && (!qtd->payload_addr))
  808. break;
  809. if ((qtd->length) &&
  810. ((qtd->packet_type == SETUP_PID) ||
  811. (qtd->packet_type == OUT_PID))) {
  812. mem_writes8(hcd->regs, qtd->payload_addr,
  813. qtd->data_buffer, qtd->length);
  814. }
  815. qtd->status = QTD_PAYLOAD_ALLOC;
  816. }
  817. if (qtd->status == QTD_PAYLOAD_ALLOC) {
  818. /*
  819. if ((curr_slot > 31) && (free_slot == -1))
  820. dev_dbg(hcd->self.controller, "%s: No slot "
  821. "available for transfer\n", __func__);
  822. */
  823. /* Start xfer for this endpoint if not already done */
  824. if ((curr_slot > 31) && (free_slot > -1)) {
  825. if (usb_pipeint(qtd->urb->pipe))
  826. create_ptd_int(qh, qtd, &ptd);
  827. else
  828. create_ptd_atl(qh, qtd, &ptd);
  829. start_bus_transfer(hcd, ptd_offset, free_slot,
  830. slots, qtd, qh, &ptd);
  831. curr_slot = free_slot;
  832. }
  833. n++;
  834. if (n >= ENQUEUE_DEPTH)
  835. break;
  836. }
  837. }
  838. }
  839. void schedule_ptds(struct usb_hcd *hcd)
  840. {
  841. struct isp1760_hcd *priv;
  842. struct isp1760_qh *qh, *qh_next;
  843. struct list_head *ep_queue;
  844. struct usb_host_endpoint *ep;
  845. LIST_HEAD(urb_list);
  846. struct urb_listitem *urb_listitem, *urb_listitem_next;
  847. if (!hcd) {
  848. WARN_ON(1);
  849. return;
  850. }
  851. priv = hcd_to_priv(hcd);
  852. /*
  853. * check finished/retired xfers, transfer payloads, call urb_done()
  854. */
  855. ep_queue = &priv->interruptqhs;
  856. while (ep_queue) {
  857. list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
  858. ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
  859. qtd_list)->urb->ep;
  860. collect_qtds(hcd, qh, &urb_list);
  861. if (list_empty(&qh->qtd_list)) {
  862. list_del(&qh->qh_list);
  863. if (ep->hcpriv == NULL) {
  864. /* Endpoint has been disabled, so we
  865. can free the associated queue head. */
  866. qh_free(qh);
  867. }
  868. }
  869. }
  870. if (ep_queue == &priv->interruptqhs)
  871. ep_queue = &priv->controlqhs;
  872. else if (ep_queue == &priv->controlqhs)
  873. ep_queue = &priv->bulkqhs;
  874. else
  875. ep_queue = NULL;
  876. }
  877. list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
  878. urb_list) {
  879. isp1760_urb_done(hcd, urb_listitem->urb);
  880. kmem_cache_free(urb_listitem_cachep, urb_listitem);
  881. }
  882. /*
  883. * Schedule packets for transfer.
  884. *
  885. * According to USB2.0 specification:
  886. *
  887. * 1st prio: interrupt xfers, up to 80 % of bandwidth
  888. * 2nd prio: control xfers
  889. * 3rd prio: bulk xfers
  890. *
  891. * ... but let's use a simpler scheme here (mostly because ISP1761 doc
  892. * is very unclear on how to prioritize traffic):
  893. *
  894. * 1) Enqueue any queued control transfers, as long as payload chip mem
  895. * and PTD ATL slots are available.
  896. * 2) Enqueue any queued INT transfers, as long as payload chip mem
  897. * and PTD INT slots are available.
  898. * 3) Enqueue any queued bulk transfers, as long as payload chip mem
  899. * and PTD ATL slots are available.
  900. *
  901. * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
  902. * conservation of chip mem and performance.
  903. *
  904. * I'm sure this scheme could be improved upon!
  905. */
  906. ep_queue = &priv->controlqhs;
  907. while (ep_queue) {
  908. list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
  909. enqueue_qtds(hcd, qh);
  910. if (ep_queue == &priv->controlqhs)
  911. ep_queue = &priv->interruptqhs;
  912. else if (ep_queue == &priv->interruptqhs)
  913. ep_queue = &priv->bulkqhs;
  914. else
  915. ep_queue = NULL;
  916. }
  917. }
  918. #define PTD_STATE_QTD_DONE 1
  919. #define PTD_STATE_QTD_RELOAD 2
  920. #define PTD_STATE_URB_RETIRE 3
  921. static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
  922. struct urb *urb)
  923. {
  924. __dw dw4;
  925. int i;
  926. dw4 = ptd->dw4;
  927. dw4 >>= 8;
  928. /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
  929. need to handle these errors? Is it done in hardware? */
  930. if (ptd->dw3 & DW3_HALT_BIT) {
  931. urb->status = -EPROTO; /* Default unknown error */
  932. for (i = 0; i < 8; i++) {
  933. switch (dw4 & 0x7) {
  934. case INT_UNDERRUN:
  935. dev_dbg(hcd->self.controller, "%s: underrun "
  936. "during uFrame %d\n",
  937. __func__, i);
  938. urb->status = -ECOMM; /* Could not write data */
  939. break;
  940. case INT_EXACT:
  941. dev_dbg(hcd->self.controller, "%s: transaction "
  942. "error during uFrame %d\n",
  943. __func__, i);
  944. urb->status = -EPROTO; /* timeout, bad CRC, PID
  945. error etc. */
  946. break;
  947. case INT_BABBLE:
  948. dev_dbg(hcd->self.controller, "%s: babble "
  949. "error during uFrame %d\n",
  950. __func__, i);
  951. urb->status = -EOVERFLOW;
  952. break;
  953. }
  954. dw4 >>= 3;
  955. }
  956. return PTD_STATE_URB_RETIRE;
  957. }
  958. return PTD_STATE_QTD_DONE;
  959. }
  960. static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
  961. struct urb *urb)
  962. {
  963. WARN_ON(!ptd);
  964. if (ptd->dw3 & DW3_HALT_BIT) {
  965. if (ptd->dw3 & DW3_BABBLE_BIT)
  966. urb->status = -EOVERFLOW;
  967. else if (FROM_DW3_CERR(ptd->dw3))
  968. urb->status = -EPIPE; /* Stall */
  969. else if (ptd->dw3 & DW3_ERROR_BIT)
  970. urb->status = -EPROTO; /* XactErr */
  971. else
  972. urb->status = -EPROTO; /* Unknown */
  973. /*
  974. dev_dbg(hcd->self.controller, "%s: ptd error:\n"
  975. " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
  976. " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
  977. __func__,
  978. ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
  979. ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
  980. */
  981. return PTD_STATE_URB_RETIRE;
  982. }
  983. if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
  984. /* Transfer Error, *but* active and no HALT -> reload */
  985. dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
  986. return PTD_STATE_QTD_RELOAD;
  987. }
  988. if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
  989. /*
  990. * NAKs are handled in HW by the chip. Usually if the
  991. * device is not able to send data fast enough.
  992. * This happens mostly on slower hardware.
  993. */
  994. return PTD_STATE_QTD_RELOAD;
  995. }
  996. return PTD_STATE_QTD_DONE;
  997. }
  998. static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
  999. {
  1000. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1001. u32 imask;
  1002. irqreturn_t irqret = IRQ_NONE;
  1003. struct ptd ptd;
  1004. struct isp1760_qh *qh;
  1005. int slot;
  1006. int state;
  1007. struct slotinfo *slots;
  1008. u32 ptd_offset;
  1009. struct isp1760_qtd *qtd;
  1010. int modified;
  1011. static int last_active_ptds;
  1012. int int_skip_map, atl_skip_map;
  1013. spin_lock(&priv->lock);
  1014. if (!(hcd->state & HC_STATE_RUNNING))
  1015. goto leave;
  1016. imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
  1017. if (unlikely(!imask))
  1018. goto leave;
  1019. reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
  1020. int_skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  1021. atl_skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  1022. priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
  1023. priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
  1024. priv->int_done_map &= ~int_skip_map;
  1025. priv->atl_done_map &= ~atl_skip_map;
  1026. modified = priv->int_done_map | priv->atl_done_map;
  1027. while (priv->int_done_map || priv->atl_done_map) {
  1028. if (priv->int_done_map) {
  1029. /* INT ptd */
  1030. slot = __ffs(priv->int_done_map);
  1031. priv->int_done_map &= ~(1 << slot);
  1032. slots = priv->int_slots;
  1033. /* This should not trigger, and could be removed if
  1034. noone have any problems with it triggering: */
  1035. if (!slots[slot].qh) {
  1036. WARN_ON(1);
  1037. continue;
  1038. }
  1039. ptd_offset = INT_PTD_OFFSET;
  1040. ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
  1041. state = check_int_transfer(hcd, &ptd,
  1042. slots[slot].qtd->urb);
  1043. } else {
  1044. /* ATL ptd */
  1045. slot = __ffs(priv->atl_done_map);
  1046. priv->atl_done_map &= ~(1 << slot);
  1047. slots = priv->atl_slots;
  1048. /* This should not trigger, and could be removed if
  1049. noone have any problems with it triggering: */
  1050. if (!slots[slot].qh) {
  1051. WARN_ON(1);
  1052. continue;
  1053. }
  1054. ptd_offset = ATL_PTD_OFFSET;
  1055. ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
  1056. state = check_atl_transfer(hcd, &ptd,
  1057. slots[slot].qtd->urb);
  1058. }
  1059. qtd = slots[slot].qtd;
  1060. slots[slot].qtd = NULL;
  1061. qh = slots[slot].qh;
  1062. slots[slot].qh = NULL;
  1063. priv->active_ptds--;
  1064. qh->slot = -1;
  1065. WARN_ON(qtd->status != QTD_XFER_STARTED);
  1066. switch (state) {
  1067. case PTD_STATE_QTD_DONE:
  1068. if ((usb_pipeint(qtd->urb->pipe)) &&
  1069. (qtd->urb->dev->speed != USB_SPEED_HIGH))
  1070. qtd->actual_length =
  1071. FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
  1072. else
  1073. qtd->actual_length =
  1074. FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
  1075. qtd->status = QTD_XFER_COMPLETE;
  1076. if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
  1077. is_short_bulk(qtd))
  1078. qtd = NULL;
  1079. else
  1080. qtd = list_entry(qtd->qtd_list.next,
  1081. typeof(*qtd), qtd_list);
  1082. qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
  1083. qh->ping = FROM_DW3_PING(ptd.dw3);
  1084. break;
  1085. case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
  1086. qtd->status = QTD_PAYLOAD_ALLOC;
  1087. ptd.dw0 |= DW0_VALID_BIT;
  1088. /* RL counter = ERR counter */
  1089. ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
  1090. ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
  1091. ptd.dw3 &= ~TO_DW3_CERR(3);
  1092. ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
  1093. qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
  1094. qh->ping = FROM_DW3_PING(ptd.dw3);
  1095. break;
  1096. case PTD_STATE_URB_RETIRE:
  1097. qtd->status = QTD_RETIRE;
  1098. qtd = NULL;
  1099. qh->toggle = 0;
  1100. qh->ping = 0;
  1101. break;
  1102. default:
  1103. WARN_ON(1);
  1104. continue;
  1105. }
  1106. if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
  1107. if (slots == priv->int_slots) {
  1108. if (state == PTD_STATE_QTD_RELOAD)
  1109. dev_err(hcd->self.controller,
  1110. "%s: PTD_STATE_QTD_RELOAD on "
  1111. "interrupt packet\n", __func__);
  1112. if (state != PTD_STATE_QTD_RELOAD)
  1113. create_ptd_int(qh, qtd, &ptd);
  1114. } else {
  1115. if (state != PTD_STATE_QTD_RELOAD)
  1116. create_ptd_atl(qh, qtd, &ptd);
  1117. }
  1118. start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
  1119. qh, &ptd);
  1120. }
  1121. }
  1122. if (modified)
  1123. schedule_ptds(hcd);
  1124. /* ISP1760 Errata 2 explains that interrupts may be missed (or not
  1125. happen?) if two USB devices are running simultaneously. Perhaps
  1126. this happens when a PTD is finished during interrupt handling;
  1127. enable SOF interrupts if PTDs are still scheduled when exiting this
  1128. interrupt handler, just to be safe. */
  1129. if (priv->active_ptds != last_active_ptds) {
  1130. if (priv->active_ptds > 0)
  1131. reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
  1132. INTERRUPT_ENABLE_SOT_MASK);
  1133. else
  1134. reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
  1135. INTERRUPT_ENABLE_MASK);
  1136. last_active_ptds = priv->active_ptds;
  1137. }
  1138. irqret = IRQ_HANDLED;
  1139. leave:
  1140. spin_unlock(&priv->lock);
  1141. return irqret;
  1142. }
  1143. static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
  1144. {
  1145. qtd->data_buffer = databuffer;
  1146. if (len > MAX_PAYLOAD_SIZE)
  1147. len = MAX_PAYLOAD_SIZE;
  1148. qtd->length = len;
  1149. return qtd->length;
  1150. }
  1151. static void qtd_list_free(struct list_head *qtd_list)
  1152. {
  1153. struct isp1760_qtd *qtd, *qtd_next;
  1154. list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
  1155. list_del(&qtd->qtd_list);
  1156. qtd_free(qtd);
  1157. }
  1158. }
  1159. /*
  1160. * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
  1161. * Also calculate the PID type (SETUP/IN/OUT) for each packet.
  1162. */
  1163. #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  1164. static void packetize_urb(struct usb_hcd *hcd,
  1165. struct urb *urb, struct list_head *head, gfp_t flags)
  1166. {
  1167. struct isp1760_qtd *qtd;
  1168. void *buf;
  1169. int len, maxpacketsize;
  1170. u8 packet_type;
  1171. /*
  1172. * URBs map to sequences of QTDs: one logical transaction
  1173. */
  1174. if (!urb->transfer_buffer && urb->transfer_buffer_length) {
  1175. /* XXX This looks like usb storage / SCSI bug */
  1176. dev_err(hcd->self.controller,
  1177. "buf is null, dma is %08lx len is %d\n",
  1178. (long unsigned)urb->transfer_dma,
  1179. urb->transfer_buffer_length);
  1180. WARN_ON(1);
  1181. }
  1182. if (usb_pipein(urb->pipe))
  1183. packet_type = IN_PID;
  1184. else
  1185. packet_type = OUT_PID;
  1186. if (usb_pipecontrol(urb->pipe)) {
  1187. qtd = qtd_alloc(flags, urb, SETUP_PID);
  1188. if (!qtd)
  1189. goto cleanup;
  1190. qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
  1191. list_add_tail(&qtd->qtd_list, head);
  1192. /* for zero length DATA stages, STATUS is always IN */
  1193. if (urb->transfer_buffer_length == 0)
  1194. packet_type = IN_PID;
  1195. }
  1196. maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
  1197. usb_pipeout(urb->pipe)));
  1198. /*
  1199. * buffer gets wrapped in one or more qtds;
  1200. * last one may be "short" (including zero len)
  1201. * and may serve as a control status ack
  1202. */
  1203. buf = urb->transfer_buffer;
  1204. len = urb->transfer_buffer_length;
  1205. for (;;) {
  1206. int this_qtd_len;
  1207. qtd = qtd_alloc(flags, urb, packet_type);
  1208. if (!qtd)
  1209. goto cleanup;
  1210. this_qtd_len = qtd_fill(qtd, buf, len);
  1211. list_add_tail(&qtd->qtd_list, head);
  1212. len -= this_qtd_len;
  1213. buf += this_qtd_len;
  1214. if (len <= 0)
  1215. break;
  1216. }
  1217. /*
  1218. * control requests may need a terminating data "status" ack;
  1219. * bulk ones may need a terminating short packet (zero length).
  1220. */
  1221. if (urb->transfer_buffer_length != 0) {
  1222. int one_more = 0;
  1223. if (usb_pipecontrol(urb->pipe)) {
  1224. one_more = 1;
  1225. if (packet_type == IN_PID)
  1226. packet_type = OUT_PID;
  1227. else
  1228. packet_type = IN_PID;
  1229. } else if (usb_pipebulk(urb->pipe)
  1230. && (urb->transfer_flags & URB_ZERO_PACKET)
  1231. && !(urb->transfer_buffer_length %
  1232. maxpacketsize)) {
  1233. one_more = 1;
  1234. }
  1235. if (one_more) {
  1236. qtd = qtd_alloc(flags, urb, packet_type);
  1237. if (!qtd)
  1238. goto cleanup;
  1239. /* never any data in such packets */
  1240. qtd_fill(qtd, NULL, 0);
  1241. list_add_tail(&qtd->qtd_list, head);
  1242. }
  1243. }
  1244. return;
  1245. cleanup:
  1246. qtd_list_free(head);
  1247. }
  1248. static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  1249. gfp_t mem_flags)
  1250. {
  1251. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1252. struct list_head *ep_queue;
  1253. struct isp1760_qh *qh, *qhit;
  1254. unsigned long spinflags;
  1255. LIST_HEAD(new_qtds);
  1256. int retval;
  1257. int qh_in_queue;
  1258. switch (usb_pipetype(urb->pipe)) {
  1259. case PIPE_CONTROL:
  1260. ep_queue = &priv->controlqhs;
  1261. break;
  1262. case PIPE_BULK:
  1263. ep_queue = &priv->bulkqhs;
  1264. break;
  1265. case PIPE_INTERRUPT:
  1266. if (urb->interval < 0)
  1267. return -EINVAL;
  1268. /* FIXME: Check bandwidth */
  1269. ep_queue = &priv->interruptqhs;
  1270. break;
  1271. case PIPE_ISOCHRONOUS:
  1272. dev_err(hcd->self.controller, "%s: isochronous USB packets "
  1273. "not yet supported\n",
  1274. __func__);
  1275. return -EPIPE;
  1276. default:
  1277. dev_err(hcd->self.controller, "%s: unknown pipe type\n",
  1278. __func__);
  1279. return -EPIPE;
  1280. }
  1281. if (usb_pipein(urb->pipe))
  1282. urb->actual_length = 0;
  1283. packetize_urb(hcd, urb, &new_qtds, mem_flags);
  1284. if (list_empty(&new_qtds))
  1285. return -ENOMEM;
  1286. urb->hcpriv = NULL; /* Used to signal unlink to interrupt handler */
  1287. retval = 0;
  1288. spin_lock_irqsave(&priv->lock, spinflags);
  1289. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  1290. retval = -ESHUTDOWN;
  1291. goto out;
  1292. }
  1293. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  1294. if (retval)
  1295. goto out;
  1296. qh = urb->ep->hcpriv;
  1297. if (qh) {
  1298. qh_in_queue = 0;
  1299. list_for_each_entry(qhit, ep_queue, qh_list) {
  1300. if (qhit == qh) {
  1301. qh_in_queue = 1;
  1302. break;
  1303. }
  1304. }
  1305. if (!qh_in_queue)
  1306. list_add_tail(&qh->qh_list, ep_queue);
  1307. } else {
  1308. qh = qh_alloc(GFP_ATOMIC);
  1309. if (!qh) {
  1310. retval = -ENOMEM;
  1311. goto out;
  1312. }
  1313. list_add_tail(&qh->qh_list, ep_queue);
  1314. urb->ep->hcpriv = qh;
  1315. }
  1316. list_splice_tail(&new_qtds, &qh->qtd_list);
  1317. schedule_ptds(hcd);
  1318. out:
  1319. spin_unlock_irqrestore(&priv->lock, spinflags);
  1320. return retval;
  1321. }
  1322. static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
  1323. struct isp1760_qh *qh)
  1324. {
  1325. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1326. int skip_map;
  1327. WARN_ON(qh->slot == -1);
  1328. /* We need to forcefully reclaim the slot since some transfers never
  1329. return, e.g. interrupt transfers and NAKed bulk transfers. */
  1330. if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
  1331. skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  1332. skip_map |= (1 << qh->slot);
  1333. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
  1334. priv->atl_slots[qh->slot].qh = NULL;
  1335. priv->atl_slots[qh->slot].qtd = NULL;
  1336. } else {
  1337. skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  1338. skip_map |= (1 << qh->slot);
  1339. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
  1340. priv->int_slots[qh->slot].qh = NULL;
  1341. priv->int_slots[qh->slot].qtd = NULL;
  1342. }
  1343. qh->slot = -1;
  1344. priv->active_ptds--;
  1345. }
  1346. static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  1347. int status)
  1348. {
  1349. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1350. unsigned long spinflags;
  1351. struct isp1760_qh *qh;
  1352. struct isp1760_qtd *qtd;
  1353. int retval = 0;
  1354. spin_lock_irqsave(&priv->lock, spinflags);
  1355. retval = usb_hcd_check_unlink_urb(hcd, urb, status);
  1356. if (retval)
  1357. goto out;
  1358. qh = urb->ep->hcpriv;
  1359. if (!qh) {
  1360. retval = -EINVAL;
  1361. goto out;
  1362. }
  1363. list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
  1364. if (qtd->urb == urb) {
  1365. if (qtd->status == QTD_XFER_STARTED)
  1366. kill_transfer(hcd, urb, qh);
  1367. qtd->status = QTD_RETIRE;
  1368. }
  1369. urb->status = status;
  1370. schedule_ptds(hcd);
  1371. out:
  1372. spin_unlock_irqrestore(&priv->lock, spinflags);
  1373. return retval;
  1374. }
  1375. static void isp1760_endpoint_disable(struct usb_hcd *hcd,
  1376. struct usb_host_endpoint *ep)
  1377. {
  1378. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1379. unsigned long spinflags;
  1380. struct isp1760_qh *qh;
  1381. struct isp1760_qtd *qtd;
  1382. spin_lock_irqsave(&priv->lock, spinflags);
  1383. qh = ep->hcpriv;
  1384. if (!qh)
  1385. goto out;
  1386. list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  1387. if (qtd->status == QTD_XFER_STARTED)
  1388. kill_transfer(hcd, qtd->urb, qh);
  1389. qtd->status = QTD_RETIRE;
  1390. qtd->urb->status = -ECONNRESET;
  1391. }
  1392. ep->hcpriv = NULL;
  1393. /* Cannot free qh here since it will be parsed by schedule_ptds() */
  1394. schedule_ptds(hcd);
  1395. out:
  1396. spin_unlock_irqrestore(&priv->lock, spinflags);
  1397. }
  1398. static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
  1399. {
  1400. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1401. u32 temp, status = 0;
  1402. u32 mask;
  1403. int retval = 1;
  1404. unsigned long flags;
  1405. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  1406. if (!HC_IS_RUNNING(hcd->state))
  1407. return 0;
  1408. /* init status to no-changes */
  1409. buf[0] = 0;
  1410. mask = PORT_CSC;
  1411. spin_lock_irqsave(&priv->lock, flags);
  1412. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1413. if (temp & PORT_OWNER) {
  1414. if (temp & PORT_CSC) {
  1415. temp &= ~PORT_CSC;
  1416. reg_write32(hcd->regs, HC_PORTSC1, temp);
  1417. goto done;
  1418. }
  1419. }
  1420. /*
  1421. * Return status information even for ports with OWNER set.
  1422. * Otherwise khubd wouldn't see the disconnect event when a
  1423. * high-speed device is switched over to the companion
  1424. * controller by the user.
  1425. */
  1426. if ((temp & mask) != 0
  1427. || ((temp & PORT_RESUME) != 0
  1428. && time_after_eq(jiffies,
  1429. priv->reset_done))) {
  1430. buf [0] |= 1 << (0 + 1);
  1431. status = STS_PCD;
  1432. }
  1433. /* FIXME autosuspend idle root hubs */
  1434. done:
  1435. spin_unlock_irqrestore(&priv->lock, flags);
  1436. return status ? retval : 0;
  1437. }
  1438. static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
  1439. struct usb_hub_descriptor *desc)
  1440. {
  1441. int ports = HCS_N_PORTS(priv->hcs_params);
  1442. u16 temp;
  1443. desc->bDescriptorType = 0x29;
  1444. /* priv 1.0, 2.3.9 says 20ms max */
  1445. desc->bPwrOn2PwrGood = 10;
  1446. desc->bHubContrCurrent = 0;
  1447. desc->bNbrPorts = ports;
  1448. temp = 1 + (ports / 8);
  1449. desc->bDescLength = 7 + 2 * temp;
  1450. /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  1451. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  1452. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  1453. /* per-port overcurrent reporting */
  1454. temp = 0x0008;
  1455. if (HCS_PPC(priv->hcs_params))
  1456. /* per-port power control */
  1457. temp |= 0x0001;
  1458. else
  1459. /* no power switching */
  1460. temp |= 0x0002;
  1461. desc->wHubCharacteristics = cpu_to_le16(temp);
  1462. }
  1463. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  1464. static int check_reset_complete(struct usb_hcd *hcd, int index,
  1465. int port_status)
  1466. {
  1467. if (!(port_status & PORT_CONNECT))
  1468. return port_status;
  1469. /* if reset finished and it's still not enabled -- handoff */
  1470. if (!(port_status & PORT_PE)) {
  1471. dev_info(hcd->self.controller,
  1472. "port %d full speed --> companion\n",
  1473. index + 1);
  1474. port_status |= PORT_OWNER;
  1475. port_status &= ~PORT_RWC_BITS;
  1476. reg_write32(hcd->regs, HC_PORTSC1, port_status);
  1477. } else
  1478. dev_info(hcd->self.controller, "port %d high speed\n",
  1479. index + 1);
  1480. return port_status;
  1481. }
  1482. static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
  1483. u16 wValue, u16 wIndex, char *buf, u16 wLength)
  1484. {
  1485. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1486. int ports = HCS_N_PORTS(priv->hcs_params);
  1487. u32 temp, status;
  1488. unsigned long flags;
  1489. int retval = 0;
  1490. unsigned selector;
  1491. /*
  1492. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  1493. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  1494. * (track current state ourselves) ... blink for diagnostics,
  1495. * power, "this is the one", etc. EHCI spec supports this.
  1496. */
  1497. spin_lock_irqsave(&priv->lock, flags);
  1498. switch (typeReq) {
  1499. case ClearHubFeature:
  1500. switch (wValue) {
  1501. case C_HUB_LOCAL_POWER:
  1502. case C_HUB_OVER_CURRENT:
  1503. /* no hub-wide feature/status flags */
  1504. break;
  1505. default:
  1506. goto error;
  1507. }
  1508. break;
  1509. case ClearPortFeature:
  1510. if (!wIndex || wIndex > ports)
  1511. goto error;
  1512. wIndex--;
  1513. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1514. /*
  1515. * Even if OWNER is set, so the port is owned by the
  1516. * companion controller, khubd needs to be able to clear
  1517. * the port-change status bits (especially
  1518. * USB_PORT_STAT_C_CONNECTION).
  1519. */
  1520. switch (wValue) {
  1521. case USB_PORT_FEAT_ENABLE:
  1522. reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
  1523. break;
  1524. case USB_PORT_FEAT_C_ENABLE:
  1525. /* XXX error? */
  1526. break;
  1527. case USB_PORT_FEAT_SUSPEND:
  1528. if (temp & PORT_RESET)
  1529. goto error;
  1530. if (temp & PORT_SUSPEND) {
  1531. if ((temp & PORT_PE) == 0)
  1532. goto error;
  1533. /* resume signaling for 20 msec */
  1534. temp &= ~(PORT_RWC_BITS);
  1535. reg_write32(hcd->regs, HC_PORTSC1,
  1536. temp | PORT_RESUME);
  1537. priv->reset_done = jiffies +
  1538. msecs_to_jiffies(20);
  1539. }
  1540. break;
  1541. case USB_PORT_FEAT_C_SUSPEND:
  1542. /* we auto-clear this feature */
  1543. break;
  1544. case USB_PORT_FEAT_POWER:
  1545. if (HCS_PPC(priv->hcs_params))
  1546. reg_write32(hcd->regs, HC_PORTSC1,
  1547. temp & ~PORT_POWER);
  1548. break;
  1549. case USB_PORT_FEAT_C_CONNECTION:
  1550. reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
  1551. break;
  1552. case USB_PORT_FEAT_C_OVER_CURRENT:
  1553. /* XXX error ?*/
  1554. break;
  1555. case USB_PORT_FEAT_C_RESET:
  1556. /* GetPortStatus clears reset */
  1557. break;
  1558. default:
  1559. goto error;
  1560. }
  1561. reg_read32(hcd->regs, HC_USBCMD);
  1562. break;
  1563. case GetHubDescriptor:
  1564. isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
  1565. buf);
  1566. break;
  1567. case GetHubStatus:
  1568. /* no hub-wide feature/status flags */
  1569. memset(buf, 0, 4);
  1570. break;
  1571. case GetPortStatus:
  1572. if (!wIndex || wIndex > ports)
  1573. goto error;
  1574. wIndex--;
  1575. status = 0;
  1576. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1577. /* wPortChange bits */
  1578. if (temp & PORT_CSC)
  1579. status |= USB_PORT_STAT_C_CONNECTION << 16;
  1580. /* whoever resumes must GetPortStatus to complete it!! */
  1581. if (temp & PORT_RESUME) {
  1582. dev_err(hcd->self.controller, "Port resume should be skipped.\n");
  1583. /* Remote Wakeup received? */
  1584. if (!priv->reset_done) {
  1585. /* resume signaling for 20 msec */
  1586. priv->reset_done = jiffies
  1587. + msecs_to_jiffies(20);
  1588. /* check the port again */
  1589. mod_timer(&hcd->rh_timer, priv->reset_done);
  1590. }
  1591. /* resume completed? */
  1592. else if (time_after_eq(jiffies,
  1593. priv->reset_done)) {
  1594. status |= USB_PORT_STAT_C_SUSPEND << 16;
  1595. priv->reset_done = 0;
  1596. /* stop resume signaling */
  1597. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1598. reg_write32(hcd->regs, HC_PORTSC1,
  1599. temp & ~(PORT_RWC_BITS | PORT_RESUME));
  1600. retval = handshake(hcd, HC_PORTSC1,
  1601. PORT_RESUME, 0, 2000 /* 2msec */);
  1602. if (retval != 0) {
  1603. dev_err(hcd->self.controller,
  1604. "port %d resume error %d\n",
  1605. wIndex + 1, retval);
  1606. goto error;
  1607. }
  1608. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  1609. }
  1610. }
  1611. /* whoever resets must GetPortStatus to complete it!! */
  1612. if ((temp & PORT_RESET)
  1613. && time_after_eq(jiffies,
  1614. priv->reset_done)) {
  1615. status |= USB_PORT_STAT_C_RESET << 16;
  1616. priv->reset_done = 0;
  1617. /* force reset to complete */
  1618. reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
  1619. /* REVISIT: some hardware needs 550+ usec to clear
  1620. * this bit; seems too long to spin routinely...
  1621. */
  1622. retval = handshake(hcd, HC_PORTSC1,
  1623. PORT_RESET, 0, 750);
  1624. if (retval != 0) {
  1625. dev_err(hcd->self.controller, "port %d reset error %d\n",
  1626. wIndex + 1, retval);
  1627. goto error;
  1628. }
  1629. /* see what we found out */
  1630. temp = check_reset_complete(hcd, wIndex,
  1631. reg_read32(hcd->regs, HC_PORTSC1));
  1632. }
  1633. /*
  1634. * Even if OWNER is set, there's no harm letting khubd
  1635. * see the wPortStatus values (they should all be 0 except
  1636. * for PORT_POWER anyway).
  1637. */
  1638. if (temp & PORT_OWNER)
  1639. dev_err(hcd->self.controller, "PORT_OWNER is set\n");
  1640. if (temp & PORT_CONNECT) {
  1641. status |= USB_PORT_STAT_CONNECTION;
  1642. /* status may be from integrated TT */
  1643. status |= USB_PORT_STAT_HIGH_SPEED;
  1644. }
  1645. if (temp & PORT_PE)
  1646. status |= USB_PORT_STAT_ENABLE;
  1647. if (temp & (PORT_SUSPEND|PORT_RESUME))
  1648. status |= USB_PORT_STAT_SUSPEND;
  1649. if (temp & PORT_RESET)
  1650. status |= USB_PORT_STAT_RESET;
  1651. if (temp & PORT_POWER)
  1652. status |= USB_PORT_STAT_POWER;
  1653. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  1654. break;
  1655. case SetHubFeature:
  1656. switch (wValue) {
  1657. case C_HUB_LOCAL_POWER:
  1658. case C_HUB_OVER_CURRENT:
  1659. /* no hub-wide feature/status flags */
  1660. break;
  1661. default:
  1662. goto error;
  1663. }
  1664. break;
  1665. case SetPortFeature:
  1666. selector = wIndex >> 8;
  1667. wIndex &= 0xff;
  1668. if (!wIndex || wIndex > ports)
  1669. goto error;
  1670. wIndex--;
  1671. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1672. if (temp & PORT_OWNER)
  1673. break;
  1674. /* temp &= ~PORT_RWC_BITS; */
  1675. switch (wValue) {
  1676. case USB_PORT_FEAT_ENABLE:
  1677. reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
  1678. break;
  1679. case USB_PORT_FEAT_SUSPEND:
  1680. if ((temp & PORT_PE) == 0
  1681. || (temp & PORT_RESET) != 0)
  1682. goto error;
  1683. reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
  1684. break;
  1685. case USB_PORT_FEAT_POWER:
  1686. if (HCS_PPC(priv->hcs_params))
  1687. reg_write32(hcd->regs, HC_PORTSC1,
  1688. temp | PORT_POWER);
  1689. break;
  1690. case USB_PORT_FEAT_RESET:
  1691. if (temp & PORT_RESUME)
  1692. goto error;
  1693. /* line status bits may report this as low speed,
  1694. * which can be fine if this root hub has a
  1695. * transaction translator built in.
  1696. */
  1697. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  1698. && PORT_USB11(temp)) {
  1699. temp |= PORT_OWNER;
  1700. } else {
  1701. temp |= PORT_RESET;
  1702. temp &= ~PORT_PE;
  1703. /*
  1704. * caller must wait, then call GetPortStatus
  1705. * usb 2.0 spec says 50 ms resets on root
  1706. */
  1707. priv->reset_done = jiffies +
  1708. msecs_to_jiffies(50);
  1709. }
  1710. reg_write32(hcd->regs, HC_PORTSC1, temp);
  1711. break;
  1712. default:
  1713. goto error;
  1714. }
  1715. reg_read32(hcd->regs, HC_USBCMD);
  1716. break;
  1717. default:
  1718. error:
  1719. /* "stall" on error */
  1720. retval = -EPIPE;
  1721. }
  1722. spin_unlock_irqrestore(&priv->lock, flags);
  1723. return retval;
  1724. }
  1725. static int isp1760_get_frame(struct usb_hcd *hcd)
  1726. {
  1727. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1728. u32 fr;
  1729. fr = reg_read32(hcd->regs, HC_FRINDEX);
  1730. return (fr >> 3) % priv->periodic_size;
  1731. }
  1732. static void isp1760_stop(struct usb_hcd *hcd)
  1733. {
  1734. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1735. u32 temp;
  1736. isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
  1737. NULL, 0);
  1738. mdelay(20);
  1739. spin_lock_irq(&priv->lock);
  1740. ehci_reset(hcd);
  1741. /* Disable IRQ */
  1742. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  1743. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
  1744. spin_unlock_irq(&priv->lock);
  1745. reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
  1746. }
  1747. static void isp1760_shutdown(struct usb_hcd *hcd)
  1748. {
  1749. u32 command, temp;
  1750. isp1760_stop(hcd);
  1751. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  1752. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
  1753. command = reg_read32(hcd->regs, HC_USBCMD);
  1754. command &= ~CMD_RUN;
  1755. reg_write32(hcd->regs, HC_USBCMD, command);
  1756. }
  1757. static const struct hc_driver isp1760_hc_driver = {
  1758. .description = "isp1760-hcd",
  1759. .product_desc = "NXP ISP1760 USB Host Controller",
  1760. .hcd_priv_size = sizeof(struct isp1760_hcd),
  1761. .irq = isp1760_irq,
  1762. .flags = HCD_MEMORY | HCD_USB2,
  1763. .reset = isp1760_hc_setup,
  1764. .start = isp1760_run,
  1765. .stop = isp1760_stop,
  1766. .shutdown = isp1760_shutdown,
  1767. .urb_enqueue = isp1760_urb_enqueue,
  1768. .urb_dequeue = isp1760_urb_dequeue,
  1769. .endpoint_disable = isp1760_endpoint_disable,
  1770. .get_frame_number = isp1760_get_frame,
  1771. .hub_status_data = isp1760_hub_status_data,
  1772. .hub_control = isp1760_hub_control,
  1773. };
  1774. int __init init_kmem_once(void)
  1775. {
  1776. urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem",
  1777. sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
  1778. SLAB_MEM_SPREAD, NULL);
  1779. if (!urb_listitem_cachep)
  1780. return -ENOMEM;
  1781. qtd_cachep = kmem_cache_create("isp1760_qtd",
  1782. sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
  1783. SLAB_MEM_SPREAD, NULL);
  1784. if (!qtd_cachep)
  1785. return -ENOMEM;
  1786. qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
  1787. 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
  1788. if (!qh_cachep) {
  1789. kmem_cache_destroy(qtd_cachep);
  1790. return -ENOMEM;
  1791. }
  1792. return 0;
  1793. }
  1794. void deinit_kmem_cache(void)
  1795. {
  1796. kmem_cache_destroy(qtd_cachep);
  1797. kmem_cache_destroy(qh_cachep);
  1798. kmem_cache_destroy(urb_listitem_cachep);
  1799. }
  1800. struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
  1801. int irq, unsigned long irqflags,
  1802. struct device *dev, const char *busname,
  1803. unsigned int devflags)
  1804. {
  1805. struct usb_hcd *hcd;
  1806. struct isp1760_hcd *priv;
  1807. int ret;
  1808. if (usb_disabled())
  1809. return ERR_PTR(-ENODEV);
  1810. /* prevent usb-core allocating DMA pages */
  1811. dev->dma_mask = NULL;
  1812. hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
  1813. if (!hcd)
  1814. return ERR_PTR(-ENOMEM);
  1815. priv = hcd_to_priv(hcd);
  1816. priv->devflags = devflags;
  1817. init_memory(priv);
  1818. hcd->regs = ioremap(res_start, res_len);
  1819. if (!hcd->regs) {
  1820. ret = -EIO;
  1821. goto err_put;
  1822. }
  1823. hcd->irq = irq;
  1824. hcd->rsrc_start = res_start;
  1825. hcd->rsrc_len = res_len;
  1826. ret = usb_add_hcd(hcd, irq, irqflags);
  1827. if (ret)
  1828. goto err_unmap;
  1829. return hcd;
  1830. err_unmap:
  1831. iounmap(hcd->regs);
  1832. err_put:
  1833. usb_put_hcd(hcd);
  1834. return ERR_PTR(ret);
  1835. }
  1836. MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
  1837. MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
  1838. MODULE_LICENSE("GPL v2");