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 u32 base_to_chip(u32 base)
  441. {
  442. return ((base - 0x400) >> 3);
  443. }
  444. static int last_qtd_of_urb(struct isp1760_qtd *qtd, struct isp1760_qh *qh)
  445. {
  446. struct urb *urb;
  447. if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
  448. return 1;
  449. urb = qtd->urb;
  450. qtd = list_entry(qtd->qtd_list.next, typeof(*qtd), qtd_list);
  451. return (qtd->urb != urb);
  452. }
  453. /* magic numbers that can affect system performance */
  454. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  455. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  456. #define EHCI_TUNE_RL_TT 0
  457. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  458. #define EHCI_TUNE_MULT_TT 1
  459. #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
  460. static void create_ptd_atl(struct isp1760_qh *qh,
  461. struct isp1760_qtd *qtd, struct ptd *ptd)
  462. {
  463. u32 maxpacket;
  464. u32 multi;
  465. u32 rl = RL_COUNTER;
  466. u32 nak = NAK_COUNTER;
  467. memset(ptd, 0, sizeof(*ptd));
  468. /* according to 3.6.2, max packet len can not be > 0x400 */
  469. maxpacket = usb_maxpacket(qtd->urb->dev, qtd->urb->pipe,
  470. usb_pipeout(qtd->urb->pipe));
  471. multi = 1 + ((maxpacket >> 11) & 0x3);
  472. maxpacket &= 0x7ff;
  473. /* DW0 */
  474. ptd->dw0 = DW0_VALID_BIT;
  475. ptd->dw0 |= TO_DW0_LENGTH(qtd->length);
  476. ptd->dw0 |= TO_DW0_MAXPACKET(maxpacket);
  477. ptd->dw0 |= TO_DW0_ENDPOINT(usb_pipeendpoint(qtd->urb->pipe));
  478. /* DW1 */
  479. ptd->dw1 = usb_pipeendpoint(qtd->urb->pipe) >> 1;
  480. ptd->dw1 |= TO_DW1_DEVICE_ADDR(usb_pipedevice(qtd->urb->pipe));
  481. ptd->dw1 |= TO_DW1_PID_TOKEN(qtd->packet_type);
  482. if (usb_pipebulk(qtd->urb->pipe))
  483. ptd->dw1 |= DW1_TRANS_BULK;
  484. else if (usb_pipeint(qtd->urb->pipe))
  485. ptd->dw1 |= DW1_TRANS_INT;
  486. if (qtd->urb->dev->speed != USB_SPEED_HIGH) {
  487. /* split transaction */
  488. ptd->dw1 |= DW1_TRANS_SPLIT;
  489. if (qtd->urb->dev->speed == USB_SPEED_LOW)
  490. ptd->dw1 |= DW1_SE_USB_LOSPEED;
  491. ptd->dw1 |= TO_DW1_PORT_NUM(qtd->urb->dev->ttport);
  492. ptd->dw1 |= TO_DW1_HUB_NUM(qtd->urb->dev->tt->hub->devnum);
  493. /* SE bit for Split INT transfers */
  494. if (usb_pipeint(qtd->urb->pipe) &&
  495. (qtd->urb->dev->speed == USB_SPEED_LOW))
  496. ptd->dw1 |= 2 << 16;
  497. rl = 0;
  498. nak = 0;
  499. } else {
  500. ptd->dw0 |= TO_DW0_MULTI(multi);
  501. if (usb_pipecontrol(qtd->urb->pipe) ||
  502. usb_pipebulk(qtd->urb->pipe))
  503. ptd->dw3 |= TO_DW3_PING(qh->ping);
  504. }
  505. /* DW2 */
  506. ptd->dw2 = 0;
  507. ptd->dw2 |= TO_DW2_DATA_START_ADDR(base_to_chip(qtd->payload_addr));
  508. ptd->dw2 |= TO_DW2_RL(rl);
  509. /* DW3 */
  510. ptd->dw3 |= TO_DW3_NAKCOUNT(nak);
  511. ptd->dw3 |= TO_DW3_DATA_TOGGLE(qh->toggle);
  512. if (usb_pipecontrol(qtd->urb->pipe)) {
  513. if (qtd->data_buffer == qtd->urb->setup_packet)
  514. ptd->dw3 &= ~TO_DW3_DATA_TOGGLE(1);
  515. else if (last_qtd_of_urb(qtd, qh))
  516. ptd->dw3 |= TO_DW3_DATA_TOGGLE(1);
  517. }
  518. ptd->dw3 |= DW3_ACTIVE_BIT;
  519. /* Cerr */
  520. ptd->dw3 |= TO_DW3_CERR(ERR_COUNTER);
  521. }
  522. static void transform_add_int(struct isp1760_qh *qh,
  523. struct isp1760_qtd *qtd, struct ptd *ptd)
  524. {
  525. u32 usof;
  526. u32 period;
  527. /*
  528. * Most of this is guessing. ISP1761 datasheet is quite unclear, and
  529. * the algorithm from the original Philips driver code, which was
  530. * pretty much used in this driver before as well, is quite horrendous
  531. * and, i believe, incorrect. The code below follows the datasheet and
  532. * USB2.0 spec as far as I can tell, and plug/unplug seems to be much
  533. * more reliable this way (fingers crossed...).
  534. */
  535. if (qtd->urb->dev->speed == USB_SPEED_HIGH) {
  536. /* urb->interval is in units of microframes (1/8 ms) */
  537. period = qtd->urb->interval >> 3;
  538. if (qtd->urb->interval > 4)
  539. usof = 0x01; /* One bit set =>
  540. interval 1 ms * uFrame-match */
  541. else if (qtd->urb->interval > 2)
  542. usof = 0x22; /* Two bits set => interval 1/2 ms */
  543. else if (qtd->urb->interval > 1)
  544. usof = 0x55; /* Four bits set => interval 1/4 ms */
  545. else
  546. usof = 0xff; /* All bits set => interval 1/8 ms */
  547. } else {
  548. /* urb->interval is in units of frames (1 ms) */
  549. period = qtd->urb->interval;
  550. usof = 0x0f; /* Execute Start Split on any of the
  551. four first uFrames */
  552. /*
  553. * First 8 bits in dw5 is uSCS and "specifies which uSOF the
  554. * complete split needs to be sent. Valid only for IN." Also,
  555. * "All bits can be set to one for every transfer." (p 82,
  556. * ISP1761 data sheet.) 0x1c is from Philips driver. Where did
  557. * that number come from? 0xff seems to work fine...
  558. */
  559. /* ptd->dw5 = 0x1c; */
  560. ptd->dw5 = 0xff; /* Execute Complete Split on any uFrame */
  561. }
  562. period = period >> 1;/* Ensure equal or shorter period than requested */
  563. period &= 0xf8; /* Mask off too large values and lowest unused 3 bits */
  564. ptd->dw2 |= period;
  565. ptd->dw4 = usof;
  566. }
  567. static void create_ptd_int(struct isp1760_qh *qh,
  568. struct isp1760_qtd *qtd, struct ptd *ptd)
  569. {
  570. create_ptd_atl(qh, qtd, ptd);
  571. transform_add_int(qh, qtd, ptd);
  572. }
  573. static void isp1760_urb_done(struct usb_hcd *hcd, struct urb *urb)
  574. __releases(priv->lock)
  575. __acquires(priv->lock)
  576. {
  577. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  578. if (!urb->unlinked) {
  579. if (urb->status == -EINPROGRESS)
  580. urb->status = 0;
  581. }
  582. if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
  583. void *ptr;
  584. for (ptr = urb->transfer_buffer;
  585. ptr < urb->transfer_buffer + urb->transfer_buffer_length;
  586. ptr += PAGE_SIZE)
  587. flush_dcache_page(virt_to_page(ptr));
  588. }
  589. /* complete() can reenter this HCD */
  590. usb_hcd_unlink_urb_from_ep(hcd, urb);
  591. spin_unlock(&priv->lock);
  592. usb_hcd_giveback_urb(hcd, urb, urb->status);
  593. spin_lock(&priv->lock);
  594. }
  595. static struct isp1760_qtd *qtd_alloc(gfp_t flags, struct urb *urb,
  596. u8 packet_type)
  597. {
  598. struct isp1760_qtd *qtd;
  599. qtd = kmem_cache_zalloc(qtd_cachep, flags);
  600. if (!qtd)
  601. return NULL;
  602. INIT_LIST_HEAD(&qtd->qtd_list);
  603. qtd->urb = urb;
  604. qtd->packet_type = packet_type;
  605. qtd->status = QTD_ENQUEUED;
  606. qtd->actual_length = 0;
  607. return qtd;
  608. }
  609. static void qtd_free(struct isp1760_qtd *qtd)
  610. {
  611. WARN_ON(qtd->payload_addr);
  612. kmem_cache_free(qtd_cachep, qtd);
  613. }
  614. static void start_bus_transfer(struct usb_hcd *hcd, u32 ptd_offset, int slot,
  615. struct slotinfo *slots, struct isp1760_qtd *qtd,
  616. struct isp1760_qh *qh, struct ptd *ptd)
  617. {
  618. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  619. int skip_map;
  620. WARN_ON((slot < 0) || (slot > 31));
  621. WARN_ON(qtd->length && !qtd->payload_addr);
  622. WARN_ON(slots[slot].qtd);
  623. WARN_ON(slots[slot].qh);
  624. WARN_ON(qtd->status != QTD_PAYLOAD_ALLOC);
  625. slots[slot].qtd = qtd;
  626. slots[slot].qh = qh;
  627. qh->slot = slot;
  628. qtd->status = QTD_XFER_STARTED; /* Set this before writing ptd, since
  629. interrupt routine may preempt and expects this value. */
  630. ptd_write(hcd->regs, ptd_offset, slot, ptd);
  631. priv->active_ptds++;
  632. /* Make sure done map has not triggered from some unlinked transfer */
  633. if (ptd_offset == ATL_PTD_OFFSET) {
  634. priv->atl_done_map |= reg_read32(hcd->regs,
  635. HC_ATL_PTD_DONEMAP_REG);
  636. priv->atl_done_map &= ~(1 << qh->slot);
  637. skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  638. skip_map &= ~(1 << qh->slot);
  639. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, skip_map);
  640. } else {
  641. priv->int_done_map |= reg_read32(hcd->regs,
  642. HC_INT_PTD_DONEMAP_REG);
  643. priv->int_done_map &= ~(1 << qh->slot);
  644. skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  645. skip_map &= ~(1 << qh->slot);
  646. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, skip_map);
  647. }
  648. }
  649. static int is_short_bulk(struct isp1760_qtd *qtd)
  650. {
  651. return (usb_pipebulk(qtd->urb->pipe) &&
  652. (qtd->actual_length < qtd->length));
  653. }
  654. static void collect_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh,
  655. struct list_head *urb_list)
  656. {
  657. int last_qtd;
  658. struct isp1760_qtd *qtd, *qtd_next;
  659. struct urb_listitem *urb_listitem;
  660. list_for_each_entry_safe(qtd, qtd_next, &qh->qtd_list, qtd_list) {
  661. if (qtd->status < QTD_XFER_COMPLETE)
  662. break;
  663. if (list_is_last(&qtd->qtd_list, &qh->qtd_list))
  664. last_qtd = 1;
  665. else
  666. last_qtd = qtd->urb != qtd_next->urb;
  667. if ((!last_qtd) && (qtd->status == QTD_RETIRE))
  668. qtd_next->status = QTD_RETIRE;
  669. if (qtd->status == QTD_XFER_COMPLETE) {
  670. if (qtd->actual_length) {
  671. switch (qtd->packet_type) {
  672. case IN_PID:
  673. mem_reads8(hcd->regs, qtd->payload_addr,
  674. qtd->data_buffer,
  675. qtd->actual_length);
  676. /* Fall through (?) */
  677. case OUT_PID:
  678. qtd->urb->actual_length +=
  679. qtd->actual_length;
  680. /* Fall through ... */
  681. case SETUP_PID:
  682. break;
  683. }
  684. }
  685. if (is_short_bulk(qtd)) {
  686. if (qtd->urb->transfer_flags & URB_SHORT_NOT_OK)
  687. qtd->urb->status = -EREMOTEIO;
  688. if (!last_qtd)
  689. qtd_next->status = QTD_RETIRE;
  690. }
  691. }
  692. if (qtd->payload_addr)
  693. free_mem(hcd, qtd);
  694. if (last_qtd) {
  695. if ((qtd->status == QTD_RETIRE) &&
  696. (qtd->urb->status == -EINPROGRESS))
  697. qtd->urb->status = -EPIPE;
  698. /* Defer calling of urb_done() since it releases lock */
  699. urb_listitem = kmem_cache_zalloc(urb_listitem_cachep,
  700. GFP_ATOMIC);
  701. if (unlikely(!urb_listitem))
  702. break;
  703. urb_listitem->urb = qtd->urb;
  704. list_add_tail(&urb_listitem->urb_list, urb_list);
  705. }
  706. list_del(&qtd->qtd_list);
  707. qtd_free(qtd);
  708. }
  709. }
  710. #define ENQUEUE_DEPTH 2
  711. static void enqueue_qtds(struct usb_hcd *hcd, struct isp1760_qh *qh)
  712. {
  713. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  714. int ptd_offset;
  715. struct slotinfo *slots;
  716. int curr_slot, free_slot;
  717. int n;
  718. struct ptd ptd;
  719. struct isp1760_qtd *qtd;
  720. if (unlikely(list_empty(&qh->qtd_list))) {
  721. WARN_ON(1);
  722. return;
  723. }
  724. if (usb_pipeint(list_entry(qh->qtd_list.next, struct isp1760_qtd,
  725. qtd_list)->urb->pipe)) {
  726. ptd_offset = INT_PTD_OFFSET;
  727. slots = priv->int_slots;
  728. } else {
  729. ptd_offset = ATL_PTD_OFFSET;
  730. slots = priv->atl_slots;
  731. }
  732. free_slot = -1;
  733. for (curr_slot = 0; curr_slot < 32; curr_slot++) {
  734. if ((free_slot == -1) && (slots[curr_slot].qtd == NULL))
  735. free_slot = curr_slot;
  736. if (slots[curr_slot].qh == qh)
  737. break;
  738. }
  739. n = 0;
  740. list_for_each_entry(qtd, &qh->qtd_list, qtd_list) {
  741. if (qtd->status == QTD_ENQUEUED) {
  742. WARN_ON(qtd->payload_addr);
  743. alloc_mem(hcd, qtd);
  744. if ((qtd->length) && (!qtd->payload_addr))
  745. break;
  746. if ((qtd->length) &&
  747. ((qtd->packet_type == SETUP_PID) ||
  748. (qtd->packet_type == OUT_PID))) {
  749. mem_writes8(hcd->regs, qtd->payload_addr,
  750. qtd->data_buffer, qtd->length);
  751. }
  752. qtd->status = QTD_PAYLOAD_ALLOC;
  753. }
  754. if (qtd->status == QTD_PAYLOAD_ALLOC) {
  755. /*
  756. if ((curr_slot > 31) && (free_slot == -1))
  757. dev_dbg(hcd->self.controller, "%s: No slot "
  758. "available for transfer\n", __func__);
  759. */
  760. /* Start xfer for this endpoint if not already done */
  761. if ((curr_slot > 31) && (free_slot > -1)) {
  762. if (usb_pipeint(qtd->urb->pipe))
  763. create_ptd_int(qh, qtd, &ptd);
  764. else
  765. create_ptd_atl(qh, qtd, &ptd);
  766. start_bus_transfer(hcd, ptd_offset, free_slot,
  767. slots, qtd, qh, &ptd);
  768. curr_slot = free_slot;
  769. }
  770. n++;
  771. if (n >= ENQUEUE_DEPTH)
  772. break;
  773. }
  774. }
  775. }
  776. void schedule_ptds(struct usb_hcd *hcd)
  777. {
  778. struct isp1760_hcd *priv;
  779. struct isp1760_qh *qh, *qh_next;
  780. struct list_head *ep_queue;
  781. struct usb_host_endpoint *ep;
  782. LIST_HEAD(urb_list);
  783. struct urb_listitem *urb_listitem, *urb_listitem_next;
  784. if (!hcd) {
  785. WARN_ON(1);
  786. return;
  787. }
  788. priv = hcd_to_priv(hcd);
  789. /*
  790. * check finished/retired xfers, transfer payloads, call urb_done()
  791. */
  792. ep_queue = &priv->interruptqhs;
  793. while (ep_queue) {
  794. list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list) {
  795. ep = list_entry(qh->qtd_list.next, struct isp1760_qtd,
  796. qtd_list)->urb->ep;
  797. collect_qtds(hcd, qh, &urb_list);
  798. if (list_empty(&qh->qtd_list)) {
  799. list_del(&qh->qh_list);
  800. if (ep->hcpriv == NULL) {
  801. /* Endpoint has been disabled, so we
  802. can free the associated queue head. */
  803. qh_free(qh);
  804. }
  805. }
  806. }
  807. if (ep_queue == &priv->interruptqhs)
  808. ep_queue = &priv->controlqhs;
  809. else if (ep_queue == &priv->controlqhs)
  810. ep_queue = &priv->bulkqhs;
  811. else
  812. ep_queue = NULL;
  813. }
  814. list_for_each_entry_safe(urb_listitem, urb_listitem_next, &urb_list,
  815. urb_list) {
  816. isp1760_urb_done(hcd, urb_listitem->urb);
  817. kmem_cache_free(urb_listitem_cachep, urb_listitem);
  818. }
  819. /*
  820. * Schedule packets for transfer.
  821. *
  822. * According to USB2.0 specification:
  823. *
  824. * 1st prio: interrupt xfers, up to 80 % of bandwidth
  825. * 2nd prio: control xfers
  826. * 3rd prio: bulk xfers
  827. *
  828. * ... but let's use a simpler scheme here (mostly because ISP1761 doc
  829. * is very unclear on how to prioritize traffic):
  830. *
  831. * 1) Enqueue any queued control transfers, as long as payload chip mem
  832. * and PTD ATL slots are available.
  833. * 2) Enqueue any queued INT transfers, as long as payload chip mem
  834. * and PTD INT slots are available.
  835. * 3) Enqueue any queued bulk transfers, as long as payload chip mem
  836. * and PTD ATL slots are available.
  837. *
  838. * Use double buffering (ENQUEUE_DEPTH==2) as a compromise between
  839. * conservation of chip mem and performance.
  840. *
  841. * I'm sure this scheme could be improved upon!
  842. */
  843. ep_queue = &priv->controlqhs;
  844. while (ep_queue) {
  845. list_for_each_entry_safe(qh, qh_next, ep_queue, qh_list)
  846. enqueue_qtds(hcd, qh);
  847. if (ep_queue == &priv->controlqhs)
  848. ep_queue = &priv->interruptqhs;
  849. else if (ep_queue == &priv->interruptqhs)
  850. ep_queue = &priv->bulkqhs;
  851. else
  852. ep_queue = NULL;
  853. }
  854. }
  855. #define PTD_STATE_QTD_DONE 1
  856. #define PTD_STATE_QTD_RELOAD 2
  857. #define PTD_STATE_URB_RETIRE 3
  858. static int check_int_transfer(struct usb_hcd *hcd, struct ptd *ptd,
  859. struct urb *urb)
  860. {
  861. __dw dw4;
  862. int i;
  863. dw4 = ptd->dw4;
  864. dw4 >>= 8;
  865. /* FIXME: ISP1761 datasheet does not say what to do with these. Do we
  866. need to handle these errors? Is it done in hardware? */
  867. if (ptd->dw3 & DW3_HALT_BIT) {
  868. urb->status = -EPROTO; /* Default unknown error */
  869. for (i = 0; i < 8; i++) {
  870. switch (dw4 & 0x7) {
  871. case INT_UNDERRUN:
  872. dev_dbg(hcd->self.controller, "%s: underrun "
  873. "during uFrame %d\n",
  874. __func__, i);
  875. urb->status = -ECOMM; /* Could not write data */
  876. break;
  877. case INT_EXACT:
  878. dev_dbg(hcd->self.controller, "%s: transaction "
  879. "error during uFrame %d\n",
  880. __func__, i);
  881. urb->status = -EPROTO; /* timeout, bad CRC, PID
  882. error etc. */
  883. break;
  884. case INT_BABBLE:
  885. dev_dbg(hcd->self.controller, "%s: babble "
  886. "error during uFrame %d\n",
  887. __func__, i);
  888. urb->status = -EOVERFLOW;
  889. break;
  890. }
  891. dw4 >>= 3;
  892. }
  893. return PTD_STATE_URB_RETIRE;
  894. }
  895. return PTD_STATE_QTD_DONE;
  896. }
  897. static int check_atl_transfer(struct usb_hcd *hcd, struct ptd *ptd,
  898. struct urb *urb)
  899. {
  900. WARN_ON(!ptd);
  901. if (ptd->dw3 & DW3_HALT_BIT) {
  902. if (ptd->dw3 & DW3_BABBLE_BIT)
  903. urb->status = -EOVERFLOW;
  904. else if (FROM_DW3_CERR(ptd->dw3))
  905. urb->status = -EPIPE; /* Stall */
  906. else if (ptd->dw3 & DW3_ERROR_BIT)
  907. urb->status = -EPROTO; /* XactErr */
  908. else
  909. urb->status = -EPROTO; /* Unknown */
  910. /*
  911. dev_dbg(hcd->self.controller, "%s: ptd error:\n"
  912. " dw0: %08x dw1: %08x dw2: %08x dw3: %08x\n"
  913. " dw4: %08x dw5: %08x dw6: %08x dw7: %08x\n",
  914. __func__,
  915. ptd->dw0, ptd->dw1, ptd->dw2, ptd->dw3,
  916. ptd->dw4, ptd->dw5, ptd->dw6, ptd->dw7);
  917. */
  918. return PTD_STATE_URB_RETIRE;
  919. }
  920. if ((ptd->dw3 & DW3_ERROR_BIT) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
  921. /* Transfer Error, *but* active and no HALT -> reload */
  922. dev_dbg(hcd->self.controller, "PID error; reloading ptd\n");
  923. return PTD_STATE_QTD_RELOAD;
  924. }
  925. if (!FROM_DW3_NAKCOUNT(ptd->dw3) && (ptd->dw3 & DW3_ACTIVE_BIT)) {
  926. /*
  927. * NAKs are handled in HW by the chip. Usually if the
  928. * device is not able to send data fast enough.
  929. * This happens mostly on slower hardware.
  930. */
  931. return PTD_STATE_QTD_RELOAD;
  932. }
  933. return PTD_STATE_QTD_DONE;
  934. }
  935. static irqreturn_t isp1760_irq(struct usb_hcd *hcd)
  936. {
  937. struct isp1760_hcd *priv = hcd_to_priv(hcd);
  938. u32 imask;
  939. irqreturn_t irqret = IRQ_NONE;
  940. struct ptd ptd;
  941. struct isp1760_qh *qh;
  942. int slot;
  943. int state;
  944. struct slotinfo *slots;
  945. u32 ptd_offset;
  946. struct isp1760_qtd *qtd;
  947. int modified;
  948. static int last_active_ptds;
  949. int int_skip_map, atl_skip_map;
  950. spin_lock(&priv->lock);
  951. if (!(hcd->state & HC_STATE_RUNNING))
  952. goto leave;
  953. imask = reg_read32(hcd->regs, HC_INTERRUPT_REG);
  954. if (unlikely(!imask))
  955. goto leave;
  956. reg_write32(hcd->regs, HC_INTERRUPT_REG, imask); /* Clear */
  957. int_skip_map = reg_read32(hcd->regs, HC_INT_PTD_SKIPMAP_REG);
  958. atl_skip_map = reg_read32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG);
  959. priv->int_done_map |= reg_read32(hcd->regs, HC_INT_PTD_DONEMAP_REG);
  960. priv->atl_done_map |= reg_read32(hcd->regs, HC_ATL_PTD_DONEMAP_REG);
  961. priv->int_done_map &= ~int_skip_map;
  962. priv->atl_done_map &= ~atl_skip_map;
  963. modified = priv->int_done_map | priv->atl_done_map;
  964. while (priv->int_done_map || priv->atl_done_map) {
  965. if (priv->int_done_map) {
  966. /* INT ptd */
  967. slot = __ffs(priv->int_done_map);
  968. priv->int_done_map &= ~(1 << slot);
  969. slots = priv->int_slots;
  970. /* This should not trigger, and could be removed if
  971. noone have any problems with it triggering: */
  972. if (!slots[slot].qh) {
  973. WARN_ON(1);
  974. continue;
  975. }
  976. ptd_offset = INT_PTD_OFFSET;
  977. ptd_read(hcd->regs, INT_PTD_OFFSET, slot, &ptd);
  978. state = check_int_transfer(hcd, &ptd,
  979. slots[slot].qtd->urb);
  980. } else {
  981. /* ATL ptd */
  982. slot = __ffs(priv->atl_done_map);
  983. priv->atl_done_map &= ~(1 << slot);
  984. slots = priv->atl_slots;
  985. /* This should not trigger, and could be removed if
  986. noone have any problems with it triggering: */
  987. if (!slots[slot].qh) {
  988. WARN_ON(1);
  989. continue;
  990. }
  991. ptd_offset = ATL_PTD_OFFSET;
  992. ptd_read(hcd->regs, ATL_PTD_OFFSET, slot, &ptd);
  993. state = check_atl_transfer(hcd, &ptd,
  994. slots[slot].qtd->urb);
  995. }
  996. qtd = slots[slot].qtd;
  997. slots[slot].qtd = NULL;
  998. qh = slots[slot].qh;
  999. slots[slot].qh = NULL;
  1000. priv->active_ptds--;
  1001. qh->slot = -1;
  1002. WARN_ON(qtd->status != QTD_XFER_STARTED);
  1003. switch (state) {
  1004. case PTD_STATE_QTD_DONE:
  1005. if ((usb_pipeint(qtd->urb->pipe)) &&
  1006. (qtd->urb->dev->speed != USB_SPEED_HIGH))
  1007. qtd->actual_length =
  1008. FROM_DW3_SCS_NRBYTESTRANSFERRED(ptd.dw3);
  1009. else
  1010. qtd->actual_length =
  1011. FROM_DW3_NRBYTESTRANSFERRED(ptd.dw3);
  1012. qtd->status = QTD_XFER_COMPLETE;
  1013. if (list_is_last(&qtd->qtd_list, &qh->qtd_list) ||
  1014. is_short_bulk(qtd))
  1015. qtd = NULL;
  1016. else
  1017. qtd = list_entry(qtd->qtd_list.next,
  1018. typeof(*qtd), qtd_list);
  1019. qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
  1020. qh->ping = FROM_DW3_PING(ptd.dw3);
  1021. break;
  1022. case PTD_STATE_QTD_RELOAD: /* QTD_RETRY, for atls only */
  1023. qtd->status = QTD_PAYLOAD_ALLOC;
  1024. ptd.dw0 |= DW0_VALID_BIT;
  1025. /* RL counter = ERR counter */
  1026. ptd.dw3 &= ~TO_DW3_NAKCOUNT(0xf);
  1027. ptd.dw3 |= TO_DW3_NAKCOUNT(FROM_DW2_RL(ptd.dw2));
  1028. ptd.dw3 &= ~TO_DW3_CERR(3);
  1029. ptd.dw3 |= TO_DW3_CERR(ERR_COUNTER);
  1030. qh->toggle = FROM_DW3_DATA_TOGGLE(ptd.dw3);
  1031. qh->ping = FROM_DW3_PING(ptd.dw3);
  1032. break;
  1033. case PTD_STATE_URB_RETIRE:
  1034. qtd->status = QTD_RETIRE;
  1035. qtd = NULL;
  1036. qh->toggle = 0;
  1037. qh->ping = 0;
  1038. break;
  1039. default:
  1040. WARN_ON(1);
  1041. continue;
  1042. }
  1043. if (qtd && (qtd->status == QTD_PAYLOAD_ALLOC)) {
  1044. if (slots == priv->int_slots) {
  1045. if (state == PTD_STATE_QTD_RELOAD)
  1046. dev_err(hcd->self.controller,
  1047. "%s: PTD_STATE_QTD_RELOAD on "
  1048. "interrupt packet\n", __func__);
  1049. if (state != PTD_STATE_QTD_RELOAD)
  1050. create_ptd_int(qh, qtd, &ptd);
  1051. } else {
  1052. if (state != PTD_STATE_QTD_RELOAD)
  1053. create_ptd_atl(qh, qtd, &ptd);
  1054. }
  1055. start_bus_transfer(hcd, ptd_offset, slot, slots, qtd,
  1056. qh, &ptd);
  1057. }
  1058. }
  1059. if (modified)
  1060. schedule_ptds(hcd);
  1061. /* ISP1760 Errata 2 explains that interrupts may be missed (or not
  1062. happen?) if two USB devices are running simultaneously. Perhaps
  1063. this happens when a PTD is finished during interrupt handling;
  1064. enable SOF interrupts if PTDs are still scheduled when exiting this
  1065. interrupt handler, just to be safe. */
  1066. if (priv->active_ptds != last_active_ptds) {
  1067. if (priv->active_ptds > 0)
  1068. reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
  1069. INTERRUPT_ENABLE_SOT_MASK);
  1070. else
  1071. reg_write32(hcd->regs, HC_INTERRUPT_ENABLE,
  1072. INTERRUPT_ENABLE_MASK);
  1073. last_active_ptds = priv->active_ptds;
  1074. }
  1075. irqret = IRQ_HANDLED;
  1076. leave:
  1077. spin_unlock(&priv->lock);
  1078. return irqret;
  1079. }
  1080. static int isp1760_run(struct usb_hcd *hcd)
  1081. {
  1082. int retval;
  1083. u32 temp;
  1084. u32 command;
  1085. u32 chipid;
  1086. hcd->uses_new_polling = 1;
  1087. hcd->state = HC_STATE_RUNNING;
  1088. /* Set PTD interrupt AND & OR maps */
  1089. reg_write32(hcd->regs, HC_ATL_IRQ_MASK_AND_REG, 0);
  1090. reg_write32(hcd->regs, HC_ATL_IRQ_MASK_OR_REG, 0xffffffff);
  1091. reg_write32(hcd->regs, HC_INT_IRQ_MASK_AND_REG, 0);
  1092. reg_write32(hcd->regs, HC_INT_IRQ_MASK_OR_REG, 0xffffffff);
  1093. reg_write32(hcd->regs, HC_ISO_IRQ_MASK_AND_REG, 0);
  1094. reg_write32(hcd->regs, HC_ISO_IRQ_MASK_OR_REG, 0xffffffff);
  1095. /* step 23 passed */
  1096. temp = reg_read32(hcd->regs, HC_HW_MODE_CTRL);
  1097. reg_write32(hcd->regs, HC_HW_MODE_CTRL, temp | HW_GLOBAL_INTR_EN);
  1098. command = reg_read32(hcd->regs, HC_USBCMD);
  1099. command &= ~(CMD_LRESET|CMD_RESET);
  1100. command |= CMD_RUN;
  1101. reg_write32(hcd->regs, HC_USBCMD, command);
  1102. retval = handshake(hcd, HC_USBCMD, CMD_RUN, CMD_RUN, 250 * 1000);
  1103. if (retval)
  1104. return retval;
  1105. /*
  1106. * XXX
  1107. * Spec says to write FLAG_CF as last config action, priv code grabs
  1108. * the semaphore while doing so.
  1109. */
  1110. down_write(&ehci_cf_port_reset_rwsem);
  1111. reg_write32(hcd->regs, HC_CONFIGFLAG, FLAG_CF);
  1112. retval = handshake(hcd, HC_CONFIGFLAG, FLAG_CF, FLAG_CF, 250 * 1000);
  1113. up_write(&ehci_cf_port_reset_rwsem);
  1114. if (retval)
  1115. return retval;
  1116. chipid = reg_read32(hcd->regs, HC_CHIP_ID_REG);
  1117. dev_info(hcd->self.controller, "USB ISP %04x HW rev. %d started\n",
  1118. chipid & 0xffff, chipid >> 16);
  1119. /* PTD Register Init Part 2, Step 28 */
  1120. /* Setup registers controlling PTD checking */
  1121. reg_write32(hcd->regs, HC_ATL_PTD_LASTPTD_REG, 0x80000000);
  1122. reg_write32(hcd->regs, HC_INT_PTD_LASTPTD_REG, 0x80000000);
  1123. reg_write32(hcd->regs, HC_ISO_PTD_LASTPTD_REG, 0x00000001);
  1124. reg_write32(hcd->regs, HC_ATL_PTD_SKIPMAP_REG, 0xffffffff);
  1125. reg_write32(hcd->regs, HC_INT_PTD_SKIPMAP_REG, 0xffffffff);
  1126. reg_write32(hcd->regs, HC_ISO_PTD_SKIPMAP_REG, 0xffffffff);
  1127. reg_write32(hcd->regs, HC_BUFFER_STATUS_REG,
  1128. ATL_BUF_FILL | INT_BUF_FILL);
  1129. /* GRR this is run-once init(), being done every time the HC starts.
  1130. * So long as they're part of class devices, we can't do it init()
  1131. * since the class device isn't created that early.
  1132. */
  1133. return 0;
  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");