isp1760-hcd.c 54 KB

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