isp1760-hcd.c 55 KB

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