isp1760-hcd.c 56 KB

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