isp1760-hcd.c 56 KB

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