isp1760-hcd.c 66 KB

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