isp1760-hcd.c 56 KB

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