isp1760-hcd.c 57 KB

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