isp1760-hcd.c 56 KB

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