isp1760-hcd.c 54 KB

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