isp1760-hcd.c 56 KB

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