isp1760-hcd.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  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 void isp1760_init_maps(struct usb_hcd *hcd)
  441. {
  442. /*set last maps, for iso its only 1, else 32 tds bitmap*/
  443. reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
  444. reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
  445. reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
  446. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
  447. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
  448. reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
  449. reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
  450. ATL_BUF_FILL | INT_BUF_FILL);
  451. }
  452. static void isp1760_enable_interrupts(struct usb_hcd *hcd)
  453. {
  454. reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
  455. reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
  456. reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
  457. reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
  458. reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
  459. reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
  460. /* step 23 passed */
  461. }
  462. static int isp1760_run(struct usb_hcd *hcd)
  463. {
  464. int retval;
  465. u32 temp;
  466. u32 command;
  467. u32 chipid;
  468. hcd->uses_new_polling = 1;
  469. hcd->state = HC_STATE_RUNNING;
  470. isp1760_enable_interrupts(hcd);
  471. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  472. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
  473. command = reg_read32(hcd->regs, HC_USBCMD);
  474. command &= ~(CMD_LRESET|CMD_RESET);
  475. command |= CMD_RUN;
  476. reg_write32(hcd->regs, HC_USBCMD, command);
  477. retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
  478. if (retval)
  479. return retval;
  480. /*
  481. * XXX
  482. * Spec says to write FLAG_CF as last config action, priv code grabs
  483. * the semaphore while doing so.
  484. */
  485. down_write(&ehci_cf_port_reset_rwsem);
  486. reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
  487. retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
  488. up_write(&ehci_cf_port_reset_rwsem);
  489. if (retval)
  490. return retval;
  491. chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
  492. dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
  493. chipid & 0xffff, chipid >> 16);
  494. /* PTD Register Init Part 2, Step 28 */
  495. /* enable INTs */
  496. isp1760_init_maps(hcd);
  497. /* GRR this is run-once init(), being done every time the HC starts.
  498. * So long as they're part of class devices, we can't do it init()
  499. * since the class device isn't created that early.
  500. */
  501. return 0;
  502. }
  503. static u32 base_to_chip(u32 base)
  504. {
  505. return ((base - 0x400) >> 3);
  506. }
  507. static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
  508. {
  509. struct urb *urb;
  510. if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
  511. return 1;
  512. urb = qtd->urb;
  513. qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
  514. return (qtd->urb != urb);
  515. }
  516. /* magic numbers that can affect system performance */
  517. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  518. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  519. #define EHCI_TUNE_RL_TT 0
  520. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  521. #define EHCI_TUNE_MULT_TT 1
  522. #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
  523. static void create_ptd_atl(struct isp1760_qh *qh,
  524. struct isp1760_qtd *qtd, struct ptd *ptd)
  525. {
  526. u32 maxpacket;
  527. u32 multi;
  528. u32 rl = RL_COUNTER;
  529. u32 nak = NAK_COUNTER;
  530. memset(ptd, 0, sizeof(*ptd));
  531. /* according to 3.6.2, max packet len can not be > 0x400 */
  532. maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
  533. usb_pipeout(qtd->urb->pipe));
  534. multi = 1 + ((maxpacket >> 11) & 0x3);
  535. maxpacket &= 0x7ff;
  536. /* DW0 */
  537. ptd->dw0 = DW0_VALID_BIT;
  538. ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
  539. ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
  540. ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
  541. /* DW1 */
  542. ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
  543. ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
  544. ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
  545. if (usb_pipebulk(qtd->urb->pipe))
  546. ptd->dw1 |= DW1_TRANS_BULK;
  547. else if (usb_pipeint(qtd->urb->pipe))
  548. ptd->dw1 |= DW1_TRANS_INT;
  549. if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
  550. /* split transaction */
  551. ptd->dw1 |= DW1_TRANS_SPLIT;
  552. if (qtd->urb->dev->speed == USB_SPEED_LOW)
  553. ptd->dw1 |= DW1_SE_USB_LOSPEED;
  554. ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
  555. ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
  556. /* SE bit for Split INT transfers */
  557. if (usb_pipeint(qtd->urb->pipe) &&
  558. (qtd->urb->dev->speed == USB_SPEED_LOW))
  559. ptd->dw1 |= 2 << 16;
  560. rl = 0;
  561. nak = 0;
  562. } else {
  563. ptd->dw0 |= TO_DW0_MULTI(multi);
  564. if (usb_pipecontrol(qtd->urb->pipe) ||
  565. usb_pipebulk(qtd->urb->pipe))
  566. ptd->dw3 |= TO_DW3_PING(qh->ping);
  567. }
  568. /* DW2 */
  569. ptd->dw2 = 0;
  570. ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
  571. ptd->dw2 |= TO_DW2_RL(rl);
  572. /* DW3 */
  573. ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
  574. ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
  575. if (usb_pipecontrol(qtd->urb->pipe)) {
  576. if (qtd->data_buffer == qtd->urb->setup_packet)
  577. ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
  578. else if (last_qtd_of_urb(qtd, qh))
  579. ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
  580. }
  581. ptd->dw3 |= DW3_ACTIVE_BIT;
  582. /* Cerr */
  583. ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
  584. }
  585. static void transform_add_int(struct isp1760_qh *qh,
  586. struct isp1760_qtd *qtd, struct ptd *ptd)
  587. {
  588. u32 usof;
  589. u32 period;
  590. /*
  591. * Most of this is guessing. ISP1761 datasheet is quite unclear, and
  592. * the algorithm from the original Philips driver code, which was
  593. * pretty much used in this driver before as well, is quite horrendous
  594. * and, i believe, incorrect. The code below follows the datasheet and
  595. * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
  596. * more reliable this way (fingers crossed...).
  597. */
  598. if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
  599. /* urb->interval is in units of microframes (1/8 ms) */
  600. period = qtd->urb->interval >> 3;
  601. if (qtd->urb->interval > 4)
  602. usof = 0x01; /* One bit set =>
  603. interval 1 ms * uFrame-match */
  604. else if (qtd->urb->interval > 2)
  605. usof = 0x22; /* Two bits set => interval 1/2 ms */
  606. else if (qtd->urb->interval > 1)
  607. usof = 0x55; /* Four bits set => interval 1/4 ms */
  608. else
  609. usof = 0xff; /* All bits set => interval 1/8 ms */
  610. } else {
  611. /* urb->interval is in units of frames (1 ms) */
  612. period = qtd->urb->interval;
  613. usof = 0x0f; /* Execute Start Split on any of the
  614. four first uFrames */
  615. /*
  616. * First 8 bits in dw5 is uSCS and "specifies which uSOF the
  617. * complete split needs to be sent. Valid only for IN." Also,
  618. * "All bits can be set to one for every transfer." (p 82,
  619. * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
  620. * that number come from? 0xff seems to work fine...
  621. */
  622. /* ptd->dw5 = 0x1c; */
  623. ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
  624. }
  625. period = period >> 1;/* Ensure equal or shorter period than requested */
  626. period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
  627. ptd->dw2 |= period;
  628. ptd->dw4 = usof;
  629. }
  630. static void create_ptd_int(struct isp1760_qh *qh,
  631. struct isp1760_qtd *qtd, struct ptd *ptd)
  632. {
  633. create_ptd_atl(qh, qtd, ptd);
  634. transform_add_int(qh, qtd, ptd);
  635. }
  636. static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
  637. __releases(priv->lock)
  638. __acquires(priv->lock)
  639. {
  640. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  641. if (!urb->unlinked) {
  642. if (urb->status == -EINPROGRESS)
  643. urb->status = 0;
  644. }
  645. if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
  646. void *ptr;
  647. for (ptr = urb->transfer_buffer;
  648. ptr < urb->transfer_buffer + urb->transfer_buffer_length;
  649. ptr += PAGE_SIZE)
  650. flush_dcache_page(virt_to_page(ptr));
  651. }
  652. /* complete() can reenter this HCD */
  653. usb_hcd_unlink_urb_from_ep(hcd, urb);
  654. spin_unlock(&priv->lock);
  655. usb_hcd_giveback_urb(hcd, urb, urb->status);
  656. spin_lock(&priv->lock);
  657. }
  658. static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
  659. u8 packet_type)
  660. {
  661. struct isp1760_qtd *qtd;
  662. qtd = kmem_cache_zalloc(qtd_cachep, flags);
  663. if (!qtd)
  664. return NULL;
  665. INIT_LIST_HEAD(&qtd->qtd_list);
  666. qtd->urb = urb;
  667. qtd->packet_type = packet_type;
  668. qtd->status = QTD_ENQUEUED;
  669. qtd->actual_length = 0;
  670. return qtd;
  671. }
  672. static void qtd_free(struct isp1760_qtd *qtd)
  673. {
  674. WARN_ON(qtd->payload_addr);
  675. kmem_cache_free(qtd_cachep, qtd);
  676. }
  677. static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
  678. struct slotinfo *slots, struct isp1760_qtd *qtd,
  679. struct isp1760_qh *qh, struct ptd *ptd)
  680. {
  681. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  682. int skip_map;
  683. WARN_ON((slot < 0) || (slot > 31));
  684. WARN_ON(qtd->length && !qtd->payload_addr);
  685. WARN_ON(slots[slot].qtd);
  686. WARN_ON(slots[slot].qh);
  687. WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
  688. slots[slot].qtd = qtd;
  689. slots[slot].qh = qh;
  690. qh->slot = slot;
  691. qtd->status = QTD_XFER_STARTED; /* Set this before writing ptd, since
  692. interrupt routine may preempt and expects this value. */
  693. ptd_write(hcd->regs, ptd_offset, slot, ptd);
  694. priv->active_ptds++;
  695. /* Make sure done map has not triggered from some unlinked transfer */
  696. if (ptd_offset == ATL_PTD_OFFSET) {
  697. priv->atl_done_map |= reg_read32(hcd->regs,
  698. HC_ATL_PTD_DONEMAP_REG);
  699. priv->atl_done_map &= ~(1 << qh->slot);
  700. skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  701. skip_map &= ~(1 << qh->slot);
  702. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
  703. } else {
  704. priv->int_done_map |= reg_read32(hcd->regs,
  705. HC_INT_PTD_DONEMAP_REG);
  706. priv->int_done_map &= ~(1 << qh->slot);
  707. skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  708. skip_map &= ~(1 << qh->slot);
  709. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
  710. }
  711. }
  712. static int is_short_bulk(struct isp1760_qtd *qtd)
  713. {
  714. return (usb_pipebulk(qtd->urb->pipe) &&
  715. (qtd->actual_length < qtd->length));
  716. }
  717. static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
  718. struct list_head *urb_list)
  719. {
  720. int last_qtd;
  721. struct isp1760_qtd *qtd, *qtd_next;
  722. struct urb_listitem *urb_listitem;
  723. list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
  724. if (qtd->status < QTD_XFER_COMPLETE)
  725. break;
  726. if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
  727. last_qtd = 1;
  728. else
  729. last_qtd = qtd->urb != qtd_next->urb;
  730. if ((!last_qtd) && (qtd->status == QTD_RETIRE))
  731. qtd_next->status = QTD_RETIRE;
  732. if (qtd->status == QTD_XFER_COMPLETE) {
  733. if (qtd->actual_length) {
  734. switch (qtd->packet_type) {
  735. case IN_PID:
  736. mem_reads8(hcd->regs, qtd->payload_addr,
  737. qtd->data_buffer,
  738. qtd->actual_length);
  739. /* Fall through (?) */
  740. case OUT_PID:
  741. qtd->urb->actual_length +=
  742. qtd->actual_length;
  743. /* Fall through ... */
  744. case SETUP_PID:
  745. break;
  746. }
  747. }
  748. if (is_short_bulk(qtd)) {
  749. if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
  750. qtd->urb->status = -EREMOTEIO;
  751. if (!last_qtd)
  752. qtd_next->status = QTD_RETIRE;
  753. }
  754. }
  755. if (qtd->payload_addr)
  756. free_mem(hcd, qtd);
  757. if (last_qtd) {
  758. if ((qtd->status == QTD_RETIRE) &&
  759. (qtd->urb->status == -EINPROGRESS))
  760. qtd->urb->status = -EPIPE;
  761. /* Defer calling of urb_done() since it releases lock */
  762. urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
  763. GFP_ATOMIC);
  764. if (unlikely(!urb_listitem))
  765. break;
  766. urb_listitem->urb = qtd->urb;
  767. list_add_tail(&urb_listitem->urb_list, urb_list);
  768. }
  769. list_del(&qtd->qtd_list);
  770. qtd_free(qtd);
  771. }
  772. }
  773. #define ENQUEUE_DEPTH 2
  774. static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
  775. {
  776. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  777. int ptd_offset;
  778. struct slotinfo *slots;
  779. int curr_slot, free_slot;
  780. int n;
  781. struct ptd ptd;
  782. struct isp1760_qtd *qtd;
  783. if (unlikely(list_empty(&qh->qtd_list))) {
  784. WARN_ON(1);
  785. return;
  786. }
  787. if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
  788. qtd_list)->urb->pipe)) {
  789. ptd_offset = INT_PTD_OFFSET;
  790. slots = priv->int_slots;
  791. } else {
  792. ptd_offset = ATL_PTD_OFFSET;
  793. slots = priv->atl_slots;
  794. }
  795. free_slot = -1;
  796. for (curr_slot = 0; curr_slot < 32; curr_slot++) {
  797. if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
  798. free_slot = curr_slot;
  799. if (slots[curr_slot].qh == qh)
  800. break;
  801. }
  802. n = 0;
  803. list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  804. if (qtd->status == QTD_ENQUEUED) {
  805. WARN_ON(qtd->payload_addr);
  806. alloc_mem(hcd, qtd);
  807. if ((qtd->length) && (!qtd->payload_addr))
  808. break;
  809. if ((qtd->length) &&
  810. ((qtd->packet_type == SETUP_PID) ||
  811. (qtd->packet_type == OUT_PID))) {
  812. mem_writes8(hcd->regs, qtd->payload_addr,
  813. qtd->data_buffer, qtd->length);
  814. }
  815. qtd->status = QTD_PAYLOAD_ALLOC;
  816. }
  817. if (qtd->status == QTD_PAYLOAD_ALLOC) {
  818. /*
  819. if ((curr_slot > 31) && (free_slot == -1))
  820. dev_dbg(hcd->self.controller, "%s: No slot "
  821. "available for transfer\n", __func__);
  822. */
  823. /* Start xfer for this endpoint if not already done */
  824. if ((curr_slot > 31) && (free_slot > -1)) {
  825. if (usb_pipeint(qtd->urb->pipe))
  826. create_ptd_int(qh, qtd, &ptd);
  827. else
  828. create_ptd_atl(qh, qtd, &ptd);
  829. start_bus_transfer(hcd, ptd_offset, free_slot,
  830. slots, qtd, qh, &ptd);
  831. curr_slot = free_slot;
  832. }
  833. n++;
  834. if (n >= ENQUEUE_DEPTH)
  835. break;
  836. }
  837. }
  838. }
  839. void schedule_ptds(struct usb_hcd *hcd)
  840. {
  841. struct isp1760_hcd *priv;
  842. struct isp1760_qh *qh, *qh_next;
  843. struct list_head *ep_queue;
  844. struct usb_host_endpoint *ep;
  845. LIST_HEAD(urb_list);
  846. struct urb_listitem *urb_listitem, *urb_listitem_next;
  847. if (!hcd) {
  848. WARN_ON(1);
  849. return;
  850. }
  851. priv = hcd_to_priv(hcd);
  852. /*
  853. * check finished/retired xfers, transfer payloads, call urb_done()
  854. */
  855. ep_queue = &priv->interruptqhs;
  856. while (ep_queue) {
  857. list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
  858. ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
  859. qtd_list)->urb->ep;
  860. collect_qtds(hcd, qh, &urb_list);
  861. if (list_empty(&qh->qtd_list)) {
  862. list_del(&qh->qh_list);
  863. if (ep->hcpriv == NULL) {
  864. /* Endpoint has been disabled, so we
  865. can free the associated queue head. */
  866. qh_free(qh);
  867. }
  868. }
  869. }
  870. if (ep_queue == &priv->interruptqhs)
  871. ep_queue = &priv->controlqhs;
  872. else if (ep_queue == &priv->controlqhs)
  873. ep_queue = &priv->bulkqhs;
  874. else
  875. ep_queue = NULL;
  876. }
  877. list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
  878. urb_list) {
  879. isp1760_urb_done(hcd, urb_listitem->urb);
  880. kmem_cache_free(urb_listitem_cachep, urb_listitem);
  881. }
  882. /*
  883. * Schedule packets for transfer.
  884. *
  885. * According to USB2.0 specification:
  886. *
  887. * 1st prio: interrupt xfers, up to 80 % of bandwidth
  888. * 2nd prio: control xfers
  889. * 3rd prio: bulk xfers
  890. *
  891. * ... but let's use a simpler scheme here (mostly because ISP1761 doc
  892. * is very unclear on how to prioritize traffic):
  893. *
  894. * 1) Enqueue any queued control transfers, as long as payload chip mem
  895. * and PTD ATL slots are available.
  896. * 2) Enqueue any queued INT transfers, as long as payload chip mem
  897. * and PTD INT slots are available.
  898. * 3) Enqueue any queued bulk transfers, as long as payload chip mem
  899. * and PTD ATL slots are available.
  900. *
  901. * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
  902. * conservation of chip mem and performance.
  903. *
  904. * I'm sure this scheme could be improved upon!
  905. */
  906. ep_queue = &priv->controlqhs;
  907. while (ep_queue) {
  908. list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
  909. enqueue_qtds(hcd, qh);
  910. if (ep_queue == &priv->controlqhs)
  911. ep_queue = &priv->interruptqhs;
  912. else if (ep_queue == &priv->interruptqhs)
  913. ep_queue = &priv->bulkqhs;
  914. else
  915. ep_queue = NULL;
  916. }
  917. }
  918. #define PTD_STATE_QTD_DONE 1
  919. #define PTD_STATE_QTD_RELOAD 2
  920. #define PTD_STATE_URB_RETIRE 3
  921. static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
  922. struct urb *urb)
  923. {
  924. __dw dw4;
  925. int i;
  926. dw4 = ptd->dw4;
  927. dw4 >>= 8;
  928. /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
  929. need to handle these errors? Is it done in hardware? */
  930. if (ptd->dw3 & DW3_HALT_BIT) {
  931. urb->status = -EPROTO; /* Default unknown error */
  932. for (i = 0; i < 8; i++) {
  933. switch (dw4 & 0x7) {
  934. case INT_UNDERRUN:
  935. dev_dbg(hcd->self.controller, "%s: underrun "
  936. "during uFrame %d\n",
  937. __func__, i);
  938. urb->status = -ECOMM; /* Could not write data */
  939. break;
  940. case INT_EXACT:
  941. dev_dbg(hcd->self.controller, "%s: transaction "
  942. "error during uFrame %d\n",
  943. __func__, i);
  944. urb->status = -EPROTO; /* timeout, bad CRC, PID
  945. error etc. */
  946. break;
  947. case INT_BABBLE:
  948. dev_dbg(hcd->self.controller, "%s: babble "
  949. "error during uFrame %d\n",
  950. __func__, i);
  951. urb->status = -EOVERFLOW;
  952. break;
  953. }
  954. dw4 >>= 3;
  955. }
  956. return PTD_STATE_URB_RETIRE;
  957. }
  958. return PTD_STATE_QTD_DONE;
  959. }
  960. static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
  961. struct urb *urb)
  962. {
  963. WARN_ON(!ptd);
  964. if (ptd->dw3 & DW3_HALT_BIT) {
  965. if (ptd->dw3 & DW3_BABBLE_BIT)
  966. urb->status = -EOVERFLOW;
  967. else if (FROM_DW3_CERR(ptd->dw3))
  968. urb->status = -EPIPE; /* Stall */
  969. else if (ptd->dw3 & DW3_ERROR_BIT)
  970. urb->status = -EPROTO; /* XactErr */
  971. else
  972. urb->status = -EPROTO; /* Unknown */
  973. /*
  974. dev_dbg(hcd->self.controller, "%s: ptd error:\n"
  975. " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
  976. " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
  977. __func__,
  978. ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
  979. ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
  980. */
  981. return PTD_STATE_URB_RETIRE;
  982. }
  983. if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
  984. /* Transfer Error, *but* active and no HALT -> reload */
  985. dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
  986. return PTD_STATE_QTD_RELOAD;
  987. }
  988. if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
  989. /*
  990. * NAKs are handled in HW by the chip. Usually if the
  991. * device is not able to send data fast enough.
  992. * This happens mostly on slower hardware.
  993. */
  994. return PTD_STATE_QTD_RELOAD;
  995. }
  996. return PTD_STATE_QTD_DONE;
  997. }
  998. static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
  999. {
  1000. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1001. u32 imask;
  1002. irqreturn_t irqret = IRQ_NONE;
  1003. struct ptd ptd;
  1004. struct isp1760_qh *qh;
  1005. int slot;
  1006. int state;
  1007. struct slotinfo *slots;
  1008. u32 ptd_offset;
  1009. struct isp1760_qtd *qtd;
  1010. int modified;
  1011. static int last_active_ptds;
  1012. int int_skip_map, atl_skip_map;
  1013. spin_lock(&priv->lock);
  1014. if (!(hcd->state & HC_STATE_RUNNING))
  1015. goto leave;
  1016. imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
  1017. if (unlikely(!imask))
  1018. goto leave;
  1019. reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
  1020. int_skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  1021. atl_skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  1022. priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
  1023. priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
  1024. priv->int_done_map &= ~int_skip_map;
  1025. priv->atl_done_map &= ~atl_skip_map;
  1026. modified = priv->int_done_map | priv->atl_done_map;
  1027. while (priv->int_done_map || priv->atl_done_map) {
  1028. if (priv->int_done_map) {
  1029. /* INT ptd */
  1030. slot = __ffs(priv->int_done_map);
  1031. priv->int_done_map &= ~(1 << slot);
  1032. slots = priv->int_slots;
  1033. /* This should not trigger, and could be removed if
  1034. noone have any problems with it triggering: */
  1035. if (!slots[slot].qh) {
  1036. WARN_ON(1);
  1037. continue;
  1038. }
  1039. ptd_offset = INT_PTD_OFFSET;
  1040. ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
  1041. state = check_int_transfer(hcd, &ptd,
  1042. slots[slot].qtd->urb);
  1043. } else {
  1044. /* ATL ptd */
  1045. slot = __ffs(priv->atl_done_map);
  1046. priv->atl_done_map &= ~(1 << slot);
  1047. slots = priv->atl_slots;
  1048. /* This should not trigger, and could be removed if
  1049. noone have any problems with it triggering: */
  1050. if (!slots[slot].qh) {
  1051. WARN_ON(1);
  1052. continue;
  1053. }
  1054. ptd_offset = ATL_PTD_OFFSET;
  1055. ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
  1056. state = check_atl_transfer(hcd, &ptd,
  1057. slots[slot].qtd->urb);
  1058. }
  1059. qtd = slots[slot].qtd;
  1060. slots[slot].qtd = NULL;
  1061. qh = slots[slot].qh;
  1062. slots[slot].qh = NULL;
  1063. priv->active_ptds--;
  1064. qh->slot = -1;
  1065. WARN_ON(qtd->status != QTD_XFER_STARTED);
  1066. switch (state) {
  1067. case PTD_STATE_QTD_DONE:
  1068. if ((usb_pipeint(qtd->urb->pipe)) &&
  1069. (qtd->urb->dev->speed != USB_SPEED_HIGH))
  1070. qtd->actual_length =
  1071. FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
  1072. else
  1073. qtd->actual_length =
  1074. FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
  1075. qtd->status = QTD_XFER_COMPLETE;
  1076. if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
  1077. is_short_bulk(qtd))
  1078. qtd = NULL;
  1079. else
  1080. qtd = list_entry(qtd->qtd_list.next,
  1081. typeof(*qtd), qtd_list);
  1082. qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
  1083. qh->ping = FROM_DW3_PING(ptd.dw3);
  1084. break;
  1085. case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
  1086. qtd->status = QTD_PAYLOAD_ALLOC;
  1087. ptd.dw0 |= DW0_VALID_BIT;
  1088. /* RL counter = ERR counter */
  1089. ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
  1090. ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
  1091. ptd.dw3 &= ~TO_DW3_CERR(3);
  1092. ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
  1093. qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
  1094. qh->ping = FROM_DW3_PING(ptd.dw3);
  1095. break;
  1096. case PTD_STATE_URB_RETIRE:
  1097. qtd->status = QTD_RETIRE;
  1098. qtd = NULL;
  1099. qh->toggle = 0;
  1100. qh->ping = 0;
  1101. break;
  1102. default:
  1103. WARN_ON(1);
  1104. continue;
  1105. }
  1106. if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
  1107. if (slots == priv->int_slots) {
  1108. if (state == PTD_STATE_QTD_RELOAD)
  1109. dev_err(hcd->self.controller,
  1110. "%s: PTD_STATE_QTD_RELOAD on "
  1111. "interrupt packet\n", __func__);
  1112. if (state != PTD_STATE_QTD_RELOAD)
  1113. create_ptd_int(qh, qtd, &ptd);
  1114. } else {
  1115. if (state != PTD_STATE_QTD_RELOAD)
  1116. create_ptd_atl(qh, qtd, &ptd);
  1117. }
  1118. start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
  1119. qh, &ptd);
  1120. }
  1121. }
  1122. if (modified)
  1123. schedule_ptds(hcd);
  1124. /* ISP1760 Errata 2 explains that interrupts may be missed (or not
  1125. happen?) if two USB devices are running simultaneously. Perhaps
  1126. this happens when a PTD is finished during interrupt handling;
  1127. enable SOF interrupts if PTDs are still scheduled when exiting this
  1128. interrupt handler, just to be safe. */
  1129. if (priv->active_ptds != last_active_ptds) {
  1130. if (priv->active_ptds > 0)
  1131. reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
  1132. INTERRUPT_ENABLE_SOT_MASK);
  1133. else
  1134. reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
  1135. INTERRUPT_ENABLE_MASK);
  1136. last_active_ptds = priv->active_ptds;
  1137. }
  1138. irqret = IRQ_HANDLED;
  1139. leave:
  1140. spin_unlock(&priv->lock);
  1141. return irqret;
  1142. }
  1143. static int qtd_fill(struct isp1760_qtd *qtd, void *databuffer, size_t len)
  1144. {
  1145. qtd->data_buffer = databuffer;
  1146. if (len > MAX_PAYLOAD_SIZE)
  1147. len = MAX_PAYLOAD_SIZE;
  1148. qtd->length = len;
  1149. return qtd->length;
  1150. }
  1151. static void qtd_list_free(struct list_head *qtd_list)
  1152. {
  1153. struct isp1760_qtd *qtd, *qtd_next;
  1154. list_for_each_entry_safe(qtd, qtd_next, qtd_list, qtd_list) {
  1155. list_del(&qtd->qtd_list);
  1156. qtd_free(qtd);
  1157. }
  1158. }
  1159. /*
  1160. * Packetize urb->transfer_buffer into list of packets of size wMaxPacketSize.
  1161. * Also calculate the PID type (SETUP/IN/OUT) for each packet.
  1162. */
  1163. #define max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  1164. static void packetize_urb(struct usb_hcd *hcd,
  1165. struct urb *urb, struct list_head *head, gfp_t flags)
  1166. {
  1167. struct isp1760_qtd *qtd;
  1168. void *buf;
  1169. int len, maxpacketsize;
  1170. u8 packet_type;
  1171. /*
  1172. * URBs map to sequences of QTDs: one logical transaction
  1173. */
  1174. if (!urb->transfer_buffer && urb->transfer_buffer_length) {
  1175. /* XXX This looks like usb storage / SCSI bug */
  1176. dev_err(hcd->self.controller,
  1177. "buf is null, dma is %08lx len is %d\n",
  1178. (long unsigned)urb->transfer_dma,
  1179. urb->transfer_buffer_length);
  1180. WARN_ON(1);
  1181. }
  1182. if (usb_pipein(urb->pipe))
  1183. packet_type = IN_PID;
  1184. else
  1185. packet_type = OUT_PID;
  1186. if (usb_pipecontrol(urb->pipe)) {
  1187. qtd = qtd_alloc(flags, urb, SETUP_PID);
  1188. if (!qtd)
  1189. goto cleanup;
  1190. qtd_fill(qtd, urb->setup_packet, sizeof(struct usb_ctrlrequest));
  1191. list_add_tail(&qtd->qtd_list, head);
  1192. /* for zero length DATA stages, STATUS is always IN */
  1193. if (urb->transfer_buffer_length == 0)
  1194. packet_type = IN_PID;
  1195. }
  1196. maxpacketsize = max_packet(usb_maxpacket(urb->dev, urb->pipe,
  1197. usb_pipeout(urb->pipe)));
  1198. /*
  1199. * buffer gets wrapped in one or more qtds;
  1200. * last one may be "short" (including zero len)
  1201. * and may serve as a control status ack
  1202. */
  1203. buf = urb->transfer_buffer;
  1204. len = urb->transfer_buffer_length;
  1205. for (;;) {
  1206. int this_qtd_len;
  1207. qtd = qtd_alloc(flags, urb, packet_type);
  1208. if (!qtd)
  1209. goto cleanup;
  1210. this_qtd_len = qtd_fill(qtd, buf, len);
  1211. list_add_tail(&qtd->qtd_list, head);
  1212. len -= this_qtd_len;
  1213. buf += this_qtd_len;
  1214. if (len <= 0)
  1215. break;
  1216. }
  1217. /*
  1218. * control requests may need a terminating data "status" ack;
  1219. * bulk ones may need a terminating short packet (zero length).
  1220. */
  1221. if (urb->transfer_buffer_length != 0) {
  1222. int one_more = 0;
  1223. if (usb_pipecontrol(urb->pipe)) {
  1224. one_more = 1;
  1225. if (packet_type == IN_PID)
  1226. packet_type = OUT_PID;
  1227. else
  1228. packet_type = IN_PID;
  1229. } else if (usb_pipebulk(urb->pipe)
  1230. && (urb->transfer_flags & URB_ZERO_PACKET)
  1231. && !(urb->transfer_buffer_length %
  1232. maxpacketsize)) {
  1233. one_more = 1;
  1234. }
  1235. if (one_more) {
  1236. qtd = qtd_alloc(flags, urb, packet_type);
  1237. if (!qtd)
  1238. goto cleanup;
  1239. /* never any data in such packets */
  1240. qtd_fill(qtd, NULL, 0);
  1241. list_add_tail(&qtd->qtd_list, head);
  1242. }
  1243. }
  1244. return;
  1245. cleanup:
  1246. qtd_list_free(head);
  1247. }
  1248. static int isp1760_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  1249. gfp_t mem_flags)
  1250. {
  1251. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1252. struct list_head *ep_queue;
  1253. struct isp1760_qh *qh, *qhit;
  1254. unsigned long spinflags;
  1255. LIST_HEAD(new_qtds);
  1256. int retval;
  1257. int qh_in_queue;
  1258. switch (usb_pipetype(urb->pipe)) {
  1259. case PIPE_CONTROL:
  1260. ep_queue = &priv->controlqhs;
  1261. break;
  1262. case PIPE_BULK:
  1263. ep_queue = &priv->bulkqhs;
  1264. break;
  1265. case PIPE_INTERRUPT:
  1266. if (urb->interval < 0)
  1267. return -EINVAL;
  1268. /* FIXME: Check bandwidth */
  1269. ep_queue = &priv->interruptqhs;
  1270. break;
  1271. case PIPE_ISOCHRONOUS:
  1272. dev_err(hcd->self.controller, "%s: isochronous USB packets "
  1273. "not yet supported\n",
  1274. __func__);
  1275. return -EPIPE;
  1276. default:
  1277. dev_err(hcd->self.controller, "%s: unknown pipe type\n",
  1278. __func__);
  1279. return -EPIPE;
  1280. }
  1281. if (usb_pipein(urb->pipe))
  1282. urb->actual_length = 0;
  1283. packetize_urb(hcd, urb, &new_qtds, mem_flags);
  1284. if (list_empty(&new_qtds))
  1285. return -ENOMEM;
  1286. urb->hcpriv = NULL; /* Used to signal unlink to interrupt handler */
  1287. retval = 0;
  1288. spin_lock_irqsave(&priv->lock, spinflags);
  1289. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  1290. retval = -ESHUTDOWN;
  1291. goto out;
  1292. }
  1293. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  1294. if (retval)
  1295. goto out;
  1296. qh = urb->ep->hcpriv;
  1297. if (qh) {
  1298. qh_in_queue = 0;
  1299. list_for_each_entry(qhit, ep_queue, qh_list) {
  1300. if (qhit == qh) {
  1301. qh_in_queue = 1;
  1302. break;
  1303. }
  1304. }
  1305. if (!qh_in_queue)
  1306. list_add_tail(&qh->qh_list, ep_queue);
  1307. } else {
  1308. qh = qh_alloc(GFP_ATOMIC);
  1309. if (!qh) {
  1310. retval = -ENOMEM;
  1311. goto out;
  1312. }
  1313. list_add_tail(&qh->qh_list, ep_queue);
  1314. urb->ep->hcpriv = qh;
  1315. }
  1316. list_splice_tail(&new_qtds, &qh->qtd_list);
  1317. schedule_ptds(hcd);
  1318. out:
  1319. spin_unlock_irqrestore(&priv->lock, spinflags);
  1320. return retval;
  1321. }
  1322. static void kill_transfer(struct usb_hcd *hcd, struct urb *urb,
  1323. struct isp1760_qh *qh)
  1324. {
  1325. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1326. int skip_map;
  1327. WARN_ON(qh->slot == -1);
  1328. /* We need to forcefully reclaim the slot since some transfers never
  1329. return, e.g. interrupt transfers and NAKed bulk transfers. */
  1330. if (usb_pipecontrol(urb->pipe) || usb_pipebulk(urb->pipe)) {
  1331. skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  1332. skip_map |= (1 << qh->slot);
  1333. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
  1334. priv->atl_slots[qh->slot].qh = NULL;
  1335. priv->atl_slots[qh->slot].qtd = NULL;
  1336. } else {
  1337. skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  1338. skip_map |= (1 << qh->slot);
  1339. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
  1340. priv->int_slots[qh->slot].qh = NULL;
  1341. priv->int_slots[qh->slot].qtd = NULL;
  1342. }
  1343. qh->slot = -1;
  1344. priv->active_ptds--;
  1345. }
  1346. static int isp1760_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  1347. int status)
  1348. {
  1349. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1350. unsigned long spinflags;
  1351. struct isp1760_qh *qh;
  1352. struct isp1760_qtd *qtd;
  1353. int retval = 0;
  1354. spin_lock_irqsave(&priv->lock, spinflags);
  1355. qh = urb->ep->hcpriv;
  1356. if (!qh) {
  1357. retval = -EINVAL;
  1358. goto out;
  1359. }
  1360. list_for_each_entry(qtd, &qh->qtd_list, qtd_list)
  1361. if (qtd->urb == urb) {
  1362. if (qtd->status == QTD_XFER_STARTED)
  1363. kill_transfer(hcd, urb, qh);
  1364. qtd->status = QTD_RETIRE;
  1365. }
  1366. urb->status = status;
  1367. schedule_ptds(hcd);
  1368. out:
  1369. spin_unlock_irqrestore(&priv->lock, spinflags);
  1370. return retval;
  1371. }
  1372. static void isp1760_endpoint_disable(struct usb_hcd *hcd,
  1373. struct usb_host_endpoint *ep)
  1374. {
  1375. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1376. unsigned long spinflags;
  1377. struct isp1760_qh *qh;
  1378. struct isp1760_qtd *qtd;
  1379. spin_lock_irqsave(&priv->lock, spinflags);
  1380. qh = ep->hcpriv;
  1381. if (!qh)
  1382. goto out;
  1383. list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  1384. if (qtd->status == QTD_XFER_STARTED)
  1385. kill_transfer(hcd, qtd->urb, qh);
  1386. qtd->status = QTD_RETIRE;
  1387. qtd->urb->status = -ECONNRESET;
  1388. }
  1389. ep->hcpriv = NULL;
  1390. /* Cannot free qh here since it will be parsed by schedule_ptds() */
  1391. schedule_ptds(hcd);
  1392. out:
  1393. spin_unlock_irqrestore(&priv->lock, spinflags);
  1394. }
  1395. static int isp1760_hub_status_data(struct usb_hcd *hcd, char *buf)
  1396. {
  1397. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1398. u32 temp, status = 0;
  1399. u32 mask;
  1400. int retval = 1;
  1401. unsigned long flags;
  1402. /* if !USB_SUSPEND, root hub timers won't get shut down ... */
  1403. if (!HC_IS_RUNNING(hcd->state))
  1404. return 0;
  1405. /* init status to no-changes */
  1406. buf[0] = 0;
  1407. mask = PORT_CSC;
  1408. spin_lock_irqsave(&priv->lock, flags);
  1409. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1410. if (temp & PORT_OWNER) {
  1411. if (temp & PORT_CSC) {
  1412. temp &= ~PORT_CSC;
  1413. reg_write32(hcd->regs, HC_PORTSC1, temp);
  1414. goto done;
  1415. }
  1416. }
  1417. /*
  1418. * Return status information even for ports with OWNER set.
  1419. * Otherwise khubd wouldn't see the disconnect event when a
  1420. * high-speed device is switched over to the companion
  1421. * controller by the user.
  1422. */
  1423. if ((temp & mask) != 0
  1424. || ((temp & PORT_RESUME) != 0
  1425. && time_after_eq(jiffies,
  1426. priv->reset_done))) {
  1427. buf [0] |= 1 << (0 + 1);
  1428. status = STS_PCD;
  1429. }
  1430. /* FIXME autosuspend idle root hubs */
  1431. done:
  1432. spin_unlock_irqrestore(&priv->lock, flags);
  1433. return status ? retval : 0;
  1434. }
  1435. static void isp1760_hub_descriptor(struct isp1760_hcd *priv,
  1436. struct usb_hub_descriptor *desc)
  1437. {
  1438. int ports = HCS_N_PORTS(priv->hcs_params);
  1439. u16 temp;
  1440. desc->bDescriptorType = 0x29;
  1441. /* priv 1.0, 2.3.9 says 20ms max */
  1442. desc->bPwrOn2PwrGood = 10;
  1443. desc->bHubContrCurrent = 0;
  1444. desc->bNbrPorts = ports;
  1445. temp = 1 + (ports / 8);
  1446. desc->bDescLength = 7 + 2 * temp;
  1447. /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  1448. memset(&desc->u.hs.DeviceRemovable[0], 0, temp);
  1449. memset(&desc->u.hs.DeviceRemovable[temp], 0xff, temp);
  1450. /* per-port overcurrent reporting */
  1451. temp = 0x0008;
  1452. if (HCS_PPC(priv->hcs_params))
  1453. /* per-port power control */
  1454. temp |= 0x0001;
  1455. else
  1456. /* no power switching */
  1457. temp |= 0x0002;
  1458. desc->wHubCharacteristics = cpu_to_le16(temp);
  1459. }
  1460. #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
  1461. static int check_reset_complete(struct usb_hcd *hcd, int index,
  1462. int port_status)
  1463. {
  1464. if (!(port_status & PORT_CONNECT))
  1465. return port_status;
  1466. /* if reset finished and it's still not enabled -- handoff */
  1467. if (!(port_status & PORT_PE)) {
  1468. dev_info(hcd->self.controller,
  1469. "port %d full speed --> companion\n",
  1470. index + 1);
  1471. port_status |= PORT_OWNER;
  1472. port_status &= ~PORT_RWC_BITS;
  1473. reg_write32(hcd->regs, HC_PORTSC1, port_status);
  1474. } else
  1475. dev_info(hcd->self.controller, "port %d high speed\n",
  1476. index + 1);
  1477. return port_status;
  1478. }
  1479. static int isp1760_hub_control(struct usb_hcd *hcd, u16 typeReq,
  1480. u16 wValue, u16 wIndex, char *buf, u16 wLength)
  1481. {
  1482. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1483. int ports = HCS_N_PORTS(priv->hcs_params);
  1484. u32 temp, status;
  1485. unsigned long flags;
  1486. int retval = 0;
  1487. unsigned selector;
  1488. /*
  1489. * FIXME: support SetPortFeatures USB_PORT_FEAT_INDICATOR.
  1490. * HCS_INDICATOR may say we can change LEDs to off/amber/green.
  1491. * (track current state ourselves) ... blink for diagnostics,
  1492. * power, "this is the one", etc. EHCI spec supports this.
  1493. */
  1494. spin_lock_irqsave(&priv->lock, flags);
  1495. switch (typeReq) {
  1496. case ClearHubFeature:
  1497. switch (wValue) {
  1498. case C_HUB_LOCAL_POWER:
  1499. case C_HUB_OVER_CURRENT:
  1500. /* no hub-wide feature/status flags */
  1501. break;
  1502. default:
  1503. goto error;
  1504. }
  1505. break;
  1506. case ClearPortFeature:
  1507. if (!wIndex || wIndex > ports)
  1508. goto error;
  1509. wIndex--;
  1510. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1511. /*
  1512. * Even if OWNER is set, so the port is owned by the
  1513. * companion controller, khubd needs to be able to clear
  1514. * the port-change status bits (especially
  1515. * USB_PORT_STAT_C_CONNECTION).
  1516. */
  1517. switch (wValue) {
  1518. case USB_PORT_FEAT_ENABLE:
  1519. reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_PE);
  1520. break;
  1521. case USB_PORT_FEAT_C_ENABLE:
  1522. /* XXX error? */
  1523. break;
  1524. case USB_PORT_FEAT_SUSPEND:
  1525. if (temp & PORT_RESET)
  1526. goto error;
  1527. if (temp & PORT_SUSPEND) {
  1528. if ((temp & PORT_PE) == 0)
  1529. goto error;
  1530. /* resume signaling for 20 msec */
  1531. temp &= ~(PORT_RWC_BITS);
  1532. reg_write32(hcd->regs, HC_PORTSC1,
  1533. temp | PORT_RESUME);
  1534. priv->reset_done = jiffies +
  1535. msecs_to_jiffies(20);
  1536. }
  1537. break;
  1538. case USB_PORT_FEAT_C_SUSPEND:
  1539. /* we auto-clear this feature */
  1540. break;
  1541. case USB_PORT_FEAT_POWER:
  1542. if (HCS_PPC(priv->hcs_params))
  1543. reg_write32(hcd->regs, HC_PORTSC1,
  1544. temp & ~PORT_POWER);
  1545. break;
  1546. case USB_PORT_FEAT_C_CONNECTION:
  1547. reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_CSC);
  1548. break;
  1549. case USB_PORT_FEAT_C_OVER_CURRENT:
  1550. /* XXX error ?*/
  1551. break;
  1552. case USB_PORT_FEAT_C_RESET:
  1553. /* GetPortStatus clears reset */
  1554. break;
  1555. default:
  1556. goto error;
  1557. }
  1558. reg_read32(hcd->regs, HC_USBCMD);
  1559. break;
  1560. case GetHubDescriptor:
  1561. isp1760_hub_descriptor(priv, (struct usb_hub_descriptor *)
  1562. buf);
  1563. break;
  1564. case GetHubStatus:
  1565. /* no hub-wide feature/status flags */
  1566. memset(buf, 0, 4);
  1567. break;
  1568. case GetPortStatus:
  1569. if (!wIndex || wIndex > ports)
  1570. goto error;
  1571. wIndex--;
  1572. status = 0;
  1573. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1574. /* wPortChange bits */
  1575. if (temp & PORT_CSC)
  1576. status |= USB_PORT_STAT_C_CONNECTION << 16;
  1577. /* whoever resumes must GetPortStatus to complete it!! */
  1578. if (temp & PORT_RESUME) {
  1579. dev_err(hcd->self.controller, "Port resume should be skipped.\n");
  1580. /* Remote Wakeup received? */
  1581. if (!priv->reset_done) {
  1582. /* resume signaling for 20 msec */
  1583. priv->reset_done = jiffies
  1584. + msecs_to_jiffies(20);
  1585. /* check the port again */
  1586. mod_timer(&hcd->rh_timer, priv->reset_done);
  1587. }
  1588. /* resume completed? */
  1589. else if (time_after_eq(jiffies,
  1590. priv->reset_done)) {
  1591. status |= USB_PORT_STAT_C_SUSPEND << 16;
  1592. priv->reset_done = 0;
  1593. /* stop resume signaling */
  1594. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1595. reg_write32(hcd->regs, HC_PORTSC1,
  1596. temp & ~(PORT_RWC_BITS | PORT_RESUME));
  1597. retval = handshake(hcd, HC_PORTSC1,
  1598. PORT_RESUME, 0, 2000 /* 2msec */);
  1599. if (retval != 0) {
  1600. dev_err(hcd->self.controller,
  1601. "port %d resume error %d\n",
  1602. wIndex + 1, retval);
  1603. goto error;
  1604. }
  1605. temp &= ~(PORT_SUSPEND|PORT_RESUME|(3<<10));
  1606. }
  1607. }
  1608. /* whoever resets must GetPortStatus to complete it!! */
  1609. if ((temp & PORT_RESET)
  1610. && time_after_eq(jiffies,
  1611. priv->reset_done)) {
  1612. status |= USB_PORT_STAT_C_RESET << 16;
  1613. priv->reset_done = 0;
  1614. /* force reset to complete */
  1615. reg_write32(hcd->regs, HC_PORTSC1, temp & ~PORT_RESET);
  1616. /* REVISIT: some hardware needs 550+ usec to clear
  1617. * this bit; seems too long to spin routinely...
  1618. */
  1619. retval = handshake(hcd, HC_PORTSC1,
  1620. PORT_RESET, 0, 750);
  1621. if (retval != 0) {
  1622. dev_err(hcd->self.controller, "port %d reset error %d\n",
  1623. wIndex + 1, retval);
  1624. goto error;
  1625. }
  1626. /* see what we found out */
  1627. temp = check_reset_complete(hcd, wIndex,
  1628. reg_read32(hcd->regs, HC_PORTSC1));
  1629. }
  1630. /*
  1631. * Even if OWNER is set, there's no harm letting khubd
  1632. * see the wPortStatus values (they should all be 0 except
  1633. * for PORT_POWER anyway).
  1634. */
  1635. if (temp & PORT_OWNER)
  1636. dev_err(hcd->self.controller, "PORT_OWNER is set\n");
  1637. if (temp & PORT_CONNECT) {
  1638. status |= USB_PORT_STAT_CONNECTION;
  1639. /* status may be from integrated TT */
  1640. status |= USB_PORT_STAT_HIGH_SPEED;
  1641. }
  1642. if (temp & PORT_PE)
  1643. status |= USB_PORT_STAT_ENABLE;
  1644. if (temp & (PORT_SUSPEND|PORT_RESUME))
  1645. status |= USB_PORT_STAT_SUSPEND;
  1646. if (temp & PORT_RESET)
  1647. status |= USB_PORT_STAT_RESET;
  1648. if (temp & PORT_POWER)
  1649. status |= USB_PORT_STAT_POWER;
  1650. put_unaligned(cpu_to_le32(status), (__le32 *) buf);
  1651. break;
  1652. case SetHubFeature:
  1653. switch (wValue) {
  1654. case C_HUB_LOCAL_POWER:
  1655. case C_HUB_OVER_CURRENT:
  1656. /* no hub-wide feature/status flags */
  1657. break;
  1658. default:
  1659. goto error;
  1660. }
  1661. break;
  1662. case SetPortFeature:
  1663. selector = wIndex >> 8;
  1664. wIndex &= 0xff;
  1665. if (!wIndex || wIndex > ports)
  1666. goto error;
  1667. wIndex--;
  1668. temp = reg_read32(hcd->regs, HC_PORTSC1);
  1669. if (temp & PORT_OWNER)
  1670. break;
  1671. /* temp &= ~PORT_RWC_BITS; */
  1672. switch (wValue) {
  1673. case USB_PORT_FEAT_ENABLE:
  1674. reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_PE);
  1675. break;
  1676. case USB_PORT_FEAT_SUSPEND:
  1677. if ((temp & PORT_PE) == 0
  1678. || (temp & PORT_RESET) != 0)
  1679. goto error;
  1680. reg_write32(hcd->regs, HC_PORTSC1, temp | PORT_SUSPEND);
  1681. break;
  1682. case USB_PORT_FEAT_POWER:
  1683. if (HCS_PPC(priv->hcs_params))
  1684. reg_write32(hcd->regs, HC_PORTSC1,
  1685. temp | PORT_POWER);
  1686. break;
  1687. case USB_PORT_FEAT_RESET:
  1688. if (temp & PORT_RESUME)
  1689. goto error;
  1690. /* line status bits may report this as low speed,
  1691. * which can be fine if this root hub has a
  1692. * transaction translator built in.
  1693. */
  1694. if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
  1695. && PORT_USB11(temp)) {
  1696. temp |= PORT_OWNER;
  1697. } else {
  1698. temp |= PORT_RESET;
  1699. temp &= ~PORT_PE;
  1700. /*
  1701. * caller must wait, then call GetPortStatus
  1702. * usb 2.0 spec says 50 ms resets on root
  1703. */
  1704. priv->reset_done = jiffies +
  1705. msecs_to_jiffies(50);
  1706. }
  1707. reg_write32(hcd->regs, HC_PORTSC1, temp);
  1708. break;
  1709. default:
  1710. goto error;
  1711. }
  1712. reg_read32(hcd->regs, HC_USBCMD);
  1713. break;
  1714. default:
  1715. error:
  1716. /* "stall" on error */
  1717. retval = -EPIPE;
  1718. }
  1719. spin_unlock_irqrestore(&priv->lock, flags);
  1720. return retval;
  1721. }
  1722. static int isp1760_get_frame(struct usb_hcd *hcd)
  1723. {
  1724. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1725. u32 fr;
  1726. fr = reg_read32(hcd->regs, HC_FRINDEX);
  1727. return (fr >> 3) % priv->periodic_size;
  1728. }
  1729. static void isp1760_stop(struct usb_hcd *hcd)
  1730. {
  1731. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  1732. u32 temp;
  1733. isp1760_hub_control(hcd, ClearPortFeature, USB_PORT_FEAT_POWER, 1,
  1734. NULL, 0);
  1735. mdelay(20);
  1736. spin_lock_irq(&priv->lock);
  1737. ehci_reset(hcd);
  1738. /* Disable IRQ */
  1739. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  1740. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
  1741. spin_unlock_irq(&priv->lock);
  1742. reg_write32(hcd->regs, HC_CONFIGFLAG, 0);
  1743. }
  1744. static void isp1760_shutdown(struct usb_hcd *hcd)
  1745. {
  1746. u32 command, temp;
  1747. isp1760_stop(hcd);
  1748. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  1749. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp &= ~HW_GLOBAL_INTR_EN);
  1750. command = reg_read32(hcd->regs, HC_USBCMD);
  1751. command &= ~CMD_RUN;
  1752. reg_write32(hcd->regs, HC_USBCMD, command);
  1753. }
  1754. static const struct hc_driver isp1760_hc_driver = {
  1755. .description = "isp1760-hcd",
  1756. .product_desc = "NXP ISP1760 USB Host Controller",
  1757. .hcd_priv_size = sizeof(struct isp1760_hcd),
  1758. .irq = isp1760_irq,
  1759. .flags = HCD_MEMORY | HCD_USB2,
  1760. .reset = isp1760_hc_setup,
  1761. .start = isp1760_run,
  1762. .stop = isp1760_stop,
  1763. .shutdown = isp1760_shutdown,
  1764. .urb_enqueue = isp1760_urb_enqueue,
  1765. .urb_dequeue = isp1760_urb_dequeue,
  1766. .endpoint_disable = isp1760_endpoint_disable,
  1767. .get_frame_number = isp1760_get_frame,
  1768. .hub_status_data = isp1760_hub_status_data,
  1769. .hub_control = isp1760_hub_control,
  1770. };
  1771. int __init init_kmem_once(void)
  1772. {
  1773. urb_listitem_cachep = kmem_cache_create("isp1760 urb_listitem",
  1774. sizeof(struct urb_listitem), 0, SLAB_TEMPORARY |
  1775. SLAB_MEM_SPREAD, NULL);
  1776. if (!urb_listitem_cachep)
  1777. return -ENOMEM;
  1778. qtd_cachep = kmem_cache_create("isp1760_qtd",
  1779. sizeof(struct isp1760_qtd), 0, SLAB_TEMPORARY |
  1780. SLAB_MEM_SPREAD, NULL);
  1781. if (!qtd_cachep)
  1782. return -ENOMEM;
  1783. qh_cachep = kmem_cache_create("isp1760_qh", sizeof(struct isp1760_qh),
  1784. 0, SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
  1785. if (!qh_cachep) {
  1786. kmem_cache_destroy(qtd_cachep);
  1787. return -ENOMEM;
  1788. }
  1789. return 0;
  1790. }
  1791. void deinit_kmem_cache(void)
  1792. {
  1793. kmem_cache_destroy(qtd_cachep);
  1794. kmem_cache_destroy(qh_cachep);
  1795. kmem_cache_destroy(urb_listitem_cachep);
  1796. }
  1797. struct usb_hcd *isp1760_register(phys_addr_t res_start, resource_size_t res_len,
  1798. int irq, unsigned long irqflags,
  1799. struct device *dev, const char *busname,
  1800. unsigned int devflags)
  1801. {
  1802. struct usb_hcd *hcd;
  1803. struct isp1760_hcd *priv;
  1804. int ret;
  1805. if (usb_disabled())
  1806. return ERR_PTR(-ENODEV);
  1807. /* prevent usb-core allocating DMA pages */
  1808. dev->dma_mask = NULL;
  1809. hcd = usb_create_hcd(&isp1760_hc_driver, dev, dev_name(dev));
  1810. if (!hcd)
  1811. return ERR_PTR(-ENOMEM);
  1812. priv = hcd_to_priv(hcd);
  1813. priv->devflags = devflags;
  1814. init_memory(priv);
  1815. hcd->regs = ioremap(res_start, res_len);
  1816. if (!hcd->regs) {
  1817. ret = -EIO;
  1818. goto err_put;
  1819. }
  1820. hcd->irq = irq;
  1821. hcd->rsrc_start = res_start;
  1822. hcd->rsrc_len = res_len;
  1823. ret = usb_add_hcd(hcd, irq, irqflags);
  1824. if (ret)
  1825. goto err_unmap;
  1826. return hcd;
  1827. err_unmap:
  1828. iounmap(hcd->regs);
  1829. err_put:
  1830. usb_put_hcd(hcd);
  1831. return ERR_PTR(ret);
  1832. }
  1833. MODULE_DESCRIPTION("Driver for the ISP1760 USB-controller from NXP");
  1834. MODULE_AUTHOR("Sebastian Siewior <bigeasy@linuxtronix.de>");
  1835. MODULE_LICENSE("GPL v2");