isp1760-hcd.c 51 KB

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