isp1760-hcd.c 55 KB

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