isp1760-hcd.c 54 KB

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