musb_core.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683
  1. /*
  2. * MUSB OTG driver core code
  3. *
  4. * Copyright 2005 Mentor Graphics Corporation
  5. * Copyright (C) 2005-2006 by Texas Instruments
  6. * Copyright (C) 2006-2007 Nokia Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
  23. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  25. * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  28. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  29. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. /*
  35. * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
  36. *
  37. * This consists of a Host Controller Driver (HCD) and a peripheral
  38. * controller driver implementing the "Gadget" API; OTG support is
  39. * in the works. These are normal Linux-USB controller drivers which
  40. * use IRQs and have no dedicated thread.
  41. *
  42. * This version of the driver has only been used with products from
  43. * Texas Instruments. Those products integrate the Inventra logic
  44. * with other DMA, IRQ, and bus modules, as well as other logic that
  45. * needs to be reflected in this driver.
  46. *
  47. *
  48. * NOTE: the original Mentor code here was pretty much a collection
  49. * of mechanisms that don't seem to have been fully integrated/working
  50. * for *any* Linux kernel version. This version aims at Linux 2.6.now,
  51. * Key open issues include:
  52. *
  53. * - Lack of host-side transaction scheduling, for all transfer types.
  54. * The hardware doesn't do it; instead, software must.
  55. *
  56. * This is not an issue for OTG devices that don't support external
  57. * hubs, but for more "normal" USB hosts it's a user issue that the
  58. * "multipoint" support doesn't scale in the expected ways. That
  59. * includes DaVinci EVM in a common non-OTG mode.
  60. *
  61. * * Control and bulk use dedicated endpoints, and there's as
  62. * yet no mechanism to either (a) reclaim the hardware when
  63. * peripherals are NAKing, which gets complicated with bulk
  64. * endpoints, or (b) use more than a single bulk endpoint in
  65. * each direction.
  66. *
  67. * RESULT: one device may be perceived as blocking another one.
  68. *
  69. * * Interrupt and isochronous will dynamically allocate endpoint
  70. * hardware, but (a) there's no record keeping for bandwidth;
  71. * (b) in the common case that few endpoints are available, there
  72. * is no mechanism to reuse endpoints to talk to multiple devices.
  73. *
  74. * RESULT: At one extreme, bandwidth can be overcommitted in
  75. * some hardware configurations, no faults will be reported.
  76. * At the other extreme, the bandwidth capabilities which do
  77. * exist tend to be severely undercommitted. You can't yet hook
  78. * up both a keyboard and a mouse to an external USB hub.
  79. */
  80. /*
  81. * This gets many kinds of configuration information:
  82. * - Kconfig for everything user-configurable
  83. * - platform_device for addressing, irq, and platform_data
  84. * - platform_data is mostly for board-specific information
  85. * (plus recentrly, SOC or family details)
  86. *
  87. * Most of the conditional compilation will (someday) vanish.
  88. */
  89. #include <linux/module.h>
  90. #include <linux/kernel.h>
  91. #include <linux/sched.h>
  92. #include <linux/slab.h>
  93. #include <linux/list.h>
  94. #include <linux/kobject.h>
  95. #include <linux/prefetch.h>
  96. #include <linux/platform_device.h>
  97. #include <linux/io.h>
  98. #include <linux/dma-mapping.h>
  99. #include <linux/usb.h>
  100. #include "musb_core.h"
  101. #include "musb_trace.h"
  102. #define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
  103. #define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
  104. #define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
  105. #define MUSB_VERSION "6.0"
  106. #define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
  107. #define MUSB_DRIVER_NAME "musb-hdrc"
  108. const char musb_driver_name[] = MUSB_DRIVER_NAME;
  109. MODULE_DESCRIPTION(DRIVER_INFO);
  110. MODULE_AUTHOR(DRIVER_AUTHOR);
  111. MODULE_LICENSE("GPL");
  112. MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
  113. /*-------------------------------------------------------------------------*/
  114. static inline struct musb *dev_to_musb(struct device *dev)
  115. {
  116. return dev_get_drvdata(dev);
  117. }
  118. /*-------------------------------------------------------------------------*/
  119. #ifndef CONFIG_BLACKFIN
  120. static int musb_ulpi_read(struct usb_phy *phy, u32 reg)
  121. {
  122. void __iomem *addr = phy->io_priv;
  123. int i = 0;
  124. u8 r;
  125. u8 power;
  126. int ret;
  127. pm_runtime_get_sync(phy->io_dev);
  128. /* Make sure the transceiver is not in low power mode */
  129. power = musb_readb(addr, MUSB_POWER);
  130. power &= ~MUSB_POWER_SUSPENDM;
  131. musb_writeb(addr, MUSB_POWER, power);
  132. /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
  133. * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
  134. */
  135. musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)reg);
  136. musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
  137. MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
  138. while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
  139. & MUSB_ULPI_REG_CMPLT)) {
  140. i++;
  141. if (i == 10000) {
  142. ret = -ETIMEDOUT;
  143. goto out;
  144. }
  145. }
  146. r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
  147. r &= ~MUSB_ULPI_REG_CMPLT;
  148. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
  149. ret = musb_readb(addr, MUSB_ULPI_REG_DATA);
  150. out:
  151. pm_runtime_put(phy->io_dev);
  152. return ret;
  153. }
  154. static int musb_ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
  155. {
  156. void __iomem *addr = phy->io_priv;
  157. int i = 0;
  158. u8 r = 0;
  159. u8 power;
  160. int ret = 0;
  161. pm_runtime_get_sync(phy->io_dev);
  162. /* Make sure the transceiver is not in low power mode */
  163. power = musb_readb(addr, MUSB_POWER);
  164. power &= ~MUSB_POWER_SUSPENDM;
  165. musb_writeb(addr, MUSB_POWER, power);
  166. musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)reg);
  167. musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)val);
  168. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
  169. while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
  170. & MUSB_ULPI_REG_CMPLT)) {
  171. i++;
  172. if (i == 10000) {
  173. ret = -ETIMEDOUT;
  174. goto out;
  175. }
  176. }
  177. r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
  178. r &= ~MUSB_ULPI_REG_CMPLT;
  179. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
  180. out:
  181. pm_runtime_put(phy->io_dev);
  182. return ret;
  183. }
  184. #else
  185. #define musb_ulpi_read NULL
  186. #define musb_ulpi_write NULL
  187. #endif
  188. static struct usb_phy_io_ops musb_ulpi_access = {
  189. .read = musb_ulpi_read,
  190. .write = musb_ulpi_write,
  191. };
  192. /*-------------------------------------------------------------------------*/
  193. static u32 musb_default_fifo_offset(u8 epnum)
  194. {
  195. return 0x20 + (epnum * 4);
  196. }
  197. /* "flat" mapping: each endpoint has its own i/o address */
  198. static void musb_flat_ep_select(void __iomem *mbase, u8 epnum)
  199. {
  200. }
  201. static u32 musb_flat_ep_offset(u8 epnum, u16 offset)
  202. {
  203. return 0x100 + (0x10 * epnum) + offset;
  204. }
  205. /* "indexed" mapping: INDEX register controls register bank select */
  206. static void musb_indexed_ep_select(void __iomem *mbase, u8 epnum)
  207. {
  208. musb_writeb(mbase, MUSB_INDEX, epnum);
  209. }
  210. static u32 musb_indexed_ep_offset(u8 epnum, u16 offset)
  211. {
  212. return 0x10 + offset;
  213. }
  214. static u32 musb_default_busctl_offset(u8 epnum, u16 offset)
  215. {
  216. return 0x80 + (0x08 * epnum) + offset;
  217. }
  218. static u8 musb_default_readb(const void __iomem *addr, unsigned offset)
  219. {
  220. u8 data = __raw_readb(addr + offset);
  221. trace_musb_readb(__builtin_return_address(0), addr, offset, data);
  222. return data;
  223. }
  224. static void musb_default_writeb(void __iomem *addr, unsigned offset, u8 data)
  225. {
  226. trace_musb_writeb(__builtin_return_address(0), addr, offset, data);
  227. __raw_writeb(data, addr + offset);
  228. }
  229. static u16 musb_default_readw(const void __iomem *addr, unsigned offset)
  230. {
  231. u16 data = __raw_readw(addr + offset);
  232. trace_musb_readw(__builtin_return_address(0), addr, offset, data);
  233. return data;
  234. }
  235. static void musb_default_writew(void __iomem *addr, unsigned offset, u16 data)
  236. {
  237. trace_musb_writew(__builtin_return_address(0), addr, offset, data);
  238. __raw_writew(data, addr + offset);
  239. }
  240. static u32 musb_default_readl(const void __iomem *addr, unsigned offset)
  241. {
  242. u32 data = __raw_readl(addr + offset);
  243. trace_musb_readl(__builtin_return_address(0), addr, offset, data);
  244. return data;
  245. }
  246. static void musb_default_writel(void __iomem *addr, unsigned offset, u32 data)
  247. {
  248. trace_musb_writel(__builtin_return_address(0), addr, offset, data);
  249. __raw_writel(data, addr + offset);
  250. }
  251. /*
  252. * Load an endpoint's FIFO
  253. */
  254. static void musb_default_write_fifo(struct musb_hw_ep *hw_ep, u16 len,
  255. const u8 *src)
  256. {
  257. struct musb *musb = hw_ep->musb;
  258. void __iomem *fifo = hw_ep->fifo;
  259. if (unlikely(len == 0))
  260. return;
  261. prefetch((u8 *)src);
  262. dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
  263. 'T', hw_ep->epnum, fifo, len, src);
  264. /* we can't assume unaligned reads work */
  265. if (likely((0x01 & (unsigned long) src) == 0)) {
  266. u16 index = 0;
  267. /* best case is 32bit-aligned source address */
  268. if ((0x02 & (unsigned long) src) == 0) {
  269. if (len >= 4) {
  270. iowrite32_rep(fifo, src + index, len >> 2);
  271. index += len & ~0x03;
  272. }
  273. if (len & 0x02) {
  274. __raw_writew(*(u16 *)&src[index], fifo);
  275. index += 2;
  276. }
  277. } else {
  278. if (len >= 2) {
  279. iowrite16_rep(fifo, src + index, len >> 1);
  280. index += len & ~0x01;
  281. }
  282. }
  283. if (len & 0x01)
  284. __raw_writeb(src[index], fifo);
  285. } else {
  286. /* byte aligned */
  287. iowrite8_rep(fifo, src, len);
  288. }
  289. }
  290. /*
  291. * Unload an endpoint's FIFO
  292. */
  293. static void musb_default_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  294. {
  295. struct musb *musb = hw_ep->musb;
  296. void __iomem *fifo = hw_ep->fifo;
  297. if (unlikely(len == 0))
  298. return;
  299. dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
  300. 'R', hw_ep->epnum, fifo, len, dst);
  301. /* we can't assume unaligned writes work */
  302. if (likely((0x01 & (unsigned long) dst) == 0)) {
  303. u16 index = 0;
  304. /* best case is 32bit-aligned destination address */
  305. if ((0x02 & (unsigned long) dst) == 0) {
  306. if (len >= 4) {
  307. ioread32_rep(fifo, dst, len >> 2);
  308. index = len & ~0x03;
  309. }
  310. if (len & 0x02) {
  311. *(u16 *)&dst[index] = __raw_readw(fifo);
  312. index += 2;
  313. }
  314. } else {
  315. if (len >= 2) {
  316. ioread16_rep(fifo, dst, len >> 1);
  317. index = len & ~0x01;
  318. }
  319. }
  320. if (len & 0x01)
  321. dst[index] = __raw_readb(fifo);
  322. } else {
  323. /* byte aligned */
  324. ioread8_rep(fifo, dst, len);
  325. }
  326. }
  327. /*
  328. * Old style IO functions
  329. */
  330. u8 (*musb_readb)(const void __iomem *addr, unsigned offset);
  331. EXPORT_SYMBOL_GPL(musb_readb);
  332. void (*musb_writeb)(void __iomem *addr, unsigned offset, u8 data);
  333. EXPORT_SYMBOL_GPL(musb_writeb);
  334. u16 (*musb_readw)(const void __iomem *addr, unsigned offset);
  335. EXPORT_SYMBOL_GPL(musb_readw);
  336. void (*musb_writew)(void __iomem *addr, unsigned offset, u16 data);
  337. EXPORT_SYMBOL_GPL(musb_writew);
  338. u32 (*musb_readl)(const void __iomem *addr, unsigned offset);
  339. EXPORT_SYMBOL_GPL(musb_readl);
  340. void (*musb_writel)(void __iomem *addr, unsigned offset, u32 data);
  341. EXPORT_SYMBOL_GPL(musb_writel);
  342. #ifndef CONFIG_MUSB_PIO_ONLY
  343. struct dma_controller *
  344. (*musb_dma_controller_create)(struct musb *musb, void __iomem *base);
  345. EXPORT_SYMBOL(musb_dma_controller_create);
  346. void (*musb_dma_controller_destroy)(struct dma_controller *c);
  347. EXPORT_SYMBOL(musb_dma_controller_destroy);
  348. #endif
  349. /*
  350. * New style IO functions
  351. */
  352. void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  353. {
  354. return hw_ep->musb->io.read_fifo(hw_ep, len, dst);
  355. }
  356. void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
  357. {
  358. return hw_ep->musb->io.write_fifo(hw_ep, len, src);
  359. }
  360. /*-------------------------------------------------------------------------*/
  361. /* for high speed test mode; see USB 2.0 spec 7.1.20 */
  362. static const u8 musb_test_packet[53] = {
  363. /* implicit SYNC then DATA0 to start */
  364. /* JKJKJKJK x9 */
  365. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  366. /* JJKKJJKK x8 */
  367. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  368. /* JJJJKKKK x8 */
  369. 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
  370. /* JJJJJJJKKKKKKK x8 */
  371. 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  372. /* JJJJJJJK x8 */
  373. 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
  374. /* JKKKKKKK x10, JK */
  375. 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
  376. /* implicit CRC16 then EOP to end */
  377. };
  378. void musb_load_testpacket(struct musb *musb)
  379. {
  380. void __iomem *regs = musb->endpoints[0].regs;
  381. musb_ep_select(musb->mregs, 0);
  382. musb_write_fifo(musb->control_ep,
  383. sizeof(musb_test_packet), musb_test_packet);
  384. musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
  385. }
  386. /*-------------------------------------------------------------------------*/
  387. /*
  388. * Handles OTG hnp timeouts, such as b_ase0_brst
  389. */
  390. static void musb_otg_timer_func(unsigned long data)
  391. {
  392. struct musb *musb = (struct musb *)data;
  393. unsigned long flags;
  394. spin_lock_irqsave(&musb->lock, flags);
  395. switch (musb->xceiv->otg->state) {
  396. case OTG_STATE_B_WAIT_ACON:
  397. musb_dbg(musb,
  398. "HNP: b_wait_acon timeout; back to b_peripheral");
  399. musb_g_disconnect(musb);
  400. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  401. musb->is_active = 0;
  402. break;
  403. case OTG_STATE_A_SUSPEND:
  404. case OTG_STATE_A_WAIT_BCON:
  405. musb_dbg(musb, "HNP: %s timeout",
  406. usb_otg_state_string(musb->xceiv->otg->state));
  407. musb_platform_set_vbus(musb, 0);
  408. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL;
  409. break;
  410. default:
  411. musb_dbg(musb, "HNP: Unhandled mode %s",
  412. usb_otg_state_string(musb->xceiv->otg->state));
  413. }
  414. spin_unlock_irqrestore(&musb->lock, flags);
  415. }
  416. /*
  417. * Stops the HNP transition. Caller must take care of locking.
  418. */
  419. void musb_hnp_stop(struct musb *musb)
  420. {
  421. struct usb_hcd *hcd = musb->hcd;
  422. void __iomem *mbase = musb->mregs;
  423. u8 reg;
  424. musb_dbg(musb, "HNP: stop from %s",
  425. usb_otg_state_string(musb->xceiv->otg->state));
  426. switch (musb->xceiv->otg->state) {
  427. case OTG_STATE_A_PERIPHERAL:
  428. musb_g_disconnect(musb);
  429. musb_dbg(musb, "HNP: back to %s",
  430. usb_otg_state_string(musb->xceiv->otg->state));
  431. break;
  432. case OTG_STATE_B_HOST:
  433. musb_dbg(musb, "HNP: Disabling HR");
  434. if (hcd)
  435. hcd->self.is_b_host = 0;
  436. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  437. MUSB_DEV_MODE(musb);
  438. reg = musb_readb(mbase, MUSB_POWER);
  439. reg |= MUSB_POWER_SUSPENDM;
  440. musb_writeb(mbase, MUSB_POWER, reg);
  441. /* REVISIT: Start SESSION_REQUEST here? */
  442. break;
  443. default:
  444. musb_dbg(musb, "HNP: Stopping in unknown state %s",
  445. usb_otg_state_string(musb->xceiv->otg->state));
  446. }
  447. /*
  448. * When returning to A state after HNP, avoid hub_port_rebounce(),
  449. * which cause occasional OPT A "Did not receive reset after connect"
  450. * errors.
  451. */
  452. musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
  453. }
  454. static void musb_recover_from_babble(struct musb *musb);
  455. /*
  456. * Interrupt Service Routine to record USB "global" interrupts.
  457. * Since these do not happen often and signify things of
  458. * paramount importance, it seems OK to check them individually;
  459. * the order of the tests is specified in the manual
  460. *
  461. * @param musb instance pointer
  462. * @param int_usb register contents
  463. * @param devctl
  464. * @param power
  465. */
  466. static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
  467. u8 devctl)
  468. {
  469. irqreturn_t handled = IRQ_NONE;
  470. musb_dbg(musb, "<== DevCtl=%02x, int_usb=0x%x", devctl, int_usb);
  471. /* in host mode, the peripheral may issue remote wakeup.
  472. * in peripheral mode, the host may resume the link.
  473. * spurious RESUME irqs happen too, paired with SUSPEND.
  474. */
  475. if (int_usb & MUSB_INTR_RESUME) {
  476. handled = IRQ_HANDLED;
  477. musb_dbg(musb, "RESUME (%s)",
  478. usb_otg_state_string(musb->xceiv->otg->state));
  479. if (devctl & MUSB_DEVCTL_HM) {
  480. switch (musb->xceiv->otg->state) {
  481. case OTG_STATE_A_SUSPEND:
  482. /* remote wakeup? later, GetPortStatus
  483. * will stop RESUME signaling
  484. */
  485. musb->port1_status |=
  486. (USB_PORT_STAT_C_SUSPEND << 16)
  487. | MUSB_PORT_STAT_RESUME;
  488. musb->rh_timer = jiffies
  489. + msecs_to_jiffies(USB_RESUME_TIMEOUT);
  490. musb->need_finish_resume = 1;
  491. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  492. musb->is_active = 1;
  493. musb_host_resume_root_hub(musb);
  494. break;
  495. case OTG_STATE_B_WAIT_ACON:
  496. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  497. musb->is_active = 1;
  498. MUSB_DEV_MODE(musb);
  499. break;
  500. default:
  501. WARNING("bogus %s RESUME (%s)\n",
  502. "host",
  503. usb_otg_state_string(musb->xceiv->otg->state));
  504. }
  505. } else {
  506. switch (musb->xceiv->otg->state) {
  507. case OTG_STATE_A_SUSPEND:
  508. /* possibly DISCONNECT is upcoming */
  509. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  510. musb_host_resume_root_hub(musb);
  511. break;
  512. case OTG_STATE_B_WAIT_ACON:
  513. case OTG_STATE_B_PERIPHERAL:
  514. /* disconnect while suspended? we may
  515. * not get a disconnect irq...
  516. */
  517. if ((devctl & MUSB_DEVCTL_VBUS)
  518. != (3 << MUSB_DEVCTL_VBUS_SHIFT)
  519. ) {
  520. musb->int_usb |= MUSB_INTR_DISCONNECT;
  521. musb->int_usb &= ~MUSB_INTR_SUSPEND;
  522. break;
  523. }
  524. musb_g_resume(musb);
  525. break;
  526. case OTG_STATE_B_IDLE:
  527. musb->int_usb &= ~MUSB_INTR_SUSPEND;
  528. break;
  529. default:
  530. WARNING("bogus %s RESUME (%s)\n",
  531. "peripheral",
  532. usb_otg_state_string(musb->xceiv->otg->state));
  533. }
  534. }
  535. }
  536. /* see manual for the order of the tests */
  537. if (int_usb & MUSB_INTR_SESSREQ) {
  538. void __iomem *mbase = musb->mregs;
  539. if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
  540. && (devctl & MUSB_DEVCTL_BDEVICE)) {
  541. musb_dbg(musb, "SessReq while on B state");
  542. return IRQ_HANDLED;
  543. }
  544. musb_dbg(musb, "SESSION_REQUEST (%s)",
  545. usb_otg_state_string(musb->xceiv->otg->state));
  546. /* IRQ arrives from ID pin sense or (later, if VBUS power
  547. * is removed) SRP. responses are time critical:
  548. * - turn on VBUS (with silicon-specific mechanism)
  549. * - go through A_WAIT_VRISE
  550. * - ... to A_WAIT_BCON.
  551. * a_wait_vrise_tmout triggers VBUS_ERROR transitions
  552. */
  553. musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
  554. musb->ep0_stage = MUSB_EP0_START;
  555. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  556. MUSB_HST_MODE(musb);
  557. musb_platform_set_vbus(musb, 1);
  558. handled = IRQ_HANDLED;
  559. }
  560. if (int_usb & MUSB_INTR_VBUSERROR) {
  561. int ignore = 0;
  562. /* During connection as an A-Device, we may see a short
  563. * current spikes causing voltage drop, because of cable
  564. * and peripheral capacitance combined with vbus draw.
  565. * (So: less common with truly self-powered devices, where
  566. * vbus doesn't act like a power supply.)
  567. *
  568. * Such spikes are short; usually less than ~500 usec, max
  569. * of ~2 msec. That is, they're not sustained overcurrent
  570. * errors, though they're reported using VBUSERROR irqs.
  571. *
  572. * Workarounds: (a) hardware: use self powered devices.
  573. * (b) software: ignore non-repeated VBUS errors.
  574. *
  575. * REVISIT: do delays from lots of DEBUG_KERNEL checks
  576. * make trouble here, keeping VBUS < 4.4V ?
  577. */
  578. switch (musb->xceiv->otg->state) {
  579. case OTG_STATE_A_HOST:
  580. /* recovery is dicey once we've gotten past the
  581. * initial stages of enumeration, but if VBUS
  582. * stayed ok at the other end of the link, and
  583. * another reset is due (at least for high speed,
  584. * to redo the chirp etc), it might work OK...
  585. */
  586. case OTG_STATE_A_WAIT_BCON:
  587. case OTG_STATE_A_WAIT_VRISE:
  588. if (musb->vbuserr_retry) {
  589. void __iomem *mbase = musb->mregs;
  590. musb->vbuserr_retry--;
  591. ignore = 1;
  592. devctl |= MUSB_DEVCTL_SESSION;
  593. musb_writeb(mbase, MUSB_DEVCTL, devctl);
  594. } else {
  595. musb->port1_status |=
  596. USB_PORT_STAT_OVERCURRENT
  597. | (USB_PORT_STAT_C_OVERCURRENT << 16);
  598. }
  599. break;
  600. default:
  601. break;
  602. }
  603. dev_printk(ignore ? KERN_DEBUG : KERN_ERR, musb->controller,
  604. "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
  605. usb_otg_state_string(musb->xceiv->otg->state),
  606. devctl,
  607. ({ char *s;
  608. switch (devctl & MUSB_DEVCTL_VBUS) {
  609. case 0 << MUSB_DEVCTL_VBUS_SHIFT:
  610. s = "<SessEnd"; break;
  611. case 1 << MUSB_DEVCTL_VBUS_SHIFT:
  612. s = "<AValid"; break;
  613. case 2 << MUSB_DEVCTL_VBUS_SHIFT:
  614. s = "<VBusValid"; break;
  615. /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
  616. default:
  617. s = "VALID"; break;
  618. } s; }),
  619. VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
  620. musb->port1_status);
  621. /* go through A_WAIT_VFALL then start a new session */
  622. if (!ignore)
  623. musb_platform_set_vbus(musb, 0);
  624. handled = IRQ_HANDLED;
  625. }
  626. if (int_usb & MUSB_INTR_SUSPEND) {
  627. musb_dbg(musb, "SUSPEND (%s) devctl %02x",
  628. usb_otg_state_string(musb->xceiv->otg->state), devctl);
  629. handled = IRQ_HANDLED;
  630. switch (musb->xceiv->otg->state) {
  631. case OTG_STATE_A_PERIPHERAL:
  632. /* We also come here if the cable is removed, since
  633. * this silicon doesn't report ID-no-longer-grounded.
  634. *
  635. * We depend on T(a_wait_bcon) to shut us down, and
  636. * hope users don't do anything dicey during this
  637. * undesired detour through A_WAIT_BCON.
  638. */
  639. musb_hnp_stop(musb);
  640. musb_host_resume_root_hub(musb);
  641. musb_root_disconnect(musb);
  642. musb_platform_try_idle(musb, jiffies
  643. + msecs_to_jiffies(musb->a_wait_bcon
  644. ? : OTG_TIME_A_WAIT_BCON));
  645. break;
  646. case OTG_STATE_B_IDLE:
  647. if (!musb->is_active)
  648. break;
  649. case OTG_STATE_B_PERIPHERAL:
  650. musb_g_suspend(musb);
  651. musb->is_active = musb->g.b_hnp_enable;
  652. if (musb->is_active) {
  653. musb->xceiv->otg->state = OTG_STATE_B_WAIT_ACON;
  654. musb_dbg(musb, "HNP: Setting timer for b_ase0_brst");
  655. mod_timer(&musb->otg_timer, jiffies
  656. + msecs_to_jiffies(
  657. OTG_TIME_B_ASE0_BRST));
  658. }
  659. break;
  660. case OTG_STATE_A_WAIT_BCON:
  661. if (musb->a_wait_bcon != 0)
  662. musb_platform_try_idle(musb, jiffies
  663. + msecs_to_jiffies(musb->a_wait_bcon));
  664. break;
  665. case OTG_STATE_A_HOST:
  666. musb->xceiv->otg->state = OTG_STATE_A_SUSPEND;
  667. musb->is_active = musb->hcd->self.b_hnp_enable;
  668. break;
  669. case OTG_STATE_B_HOST:
  670. /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
  671. musb_dbg(musb, "REVISIT: SUSPEND as B_HOST");
  672. break;
  673. default:
  674. /* "should not happen" */
  675. musb->is_active = 0;
  676. break;
  677. }
  678. }
  679. if (int_usb & MUSB_INTR_CONNECT) {
  680. struct usb_hcd *hcd = musb->hcd;
  681. handled = IRQ_HANDLED;
  682. musb->is_active = 1;
  683. musb->ep0_stage = MUSB_EP0_START;
  684. musb->intrtxe = musb->epmask;
  685. musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
  686. musb->intrrxe = musb->epmask & 0xfffe;
  687. musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
  688. musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
  689. musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
  690. |USB_PORT_STAT_HIGH_SPEED
  691. |USB_PORT_STAT_ENABLE
  692. );
  693. musb->port1_status |= USB_PORT_STAT_CONNECTION
  694. |(USB_PORT_STAT_C_CONNECTION << 16);
  695. /* high vs full speed is just a guess until after reset */
  696. if (devctl & MUSB_DEVCTL_LSDEV)
  697. musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
  698. /* indicate new connection to OTG machine */
  699. switch (musb->xceiv->otg->state) {
  700. case OTG_STATE_B_PERIPHERAL:
  701. if (int_usb & MUSB_INTR_SUSPEND) {
  702. musb_dbg(musb, "HNP: SUSPEND+CONNECT, now b_host");
  703. int_usb &= ~MUSB_INTR_SUSPEND;
  704. goto b_host;
  705. } else
  706. musb_dbg(musb, "CONNECT as b_peripheral???");
  707. break;
  708. case OTG_STATE_B_WAIT_ACON:
  709. musb_dbg(musb, "HNP: CONNECT, now b_host");
  710. b_host:
  711. musb->xceiv->otg->state = OTG_STATE_B_HOST;
  712. if (musb->hcd)
  713. musb->hcd->self.is_b_host = 1;
  714. del_timer(&musb->otg_timer);
  715. break;
  716. default:
  717. if ((devctl & MUSB_DEVCTL_VBUS)
  718. == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
  719. musb->xceiv->otg->state = OTG_STATE_A_HOST;
  720. if (hcd)
  721. hcd->self.is_b_host = 0;
  722. }
  723. break;
  724. }
  725. musb_host_poke_root_hub(musb);
  726. musb_dbg(musb, "CONNECT (%s) devctl %02x",
  727. usb_otg_state_string(musb->xceiv->otg->state), devctl);
  728. }
  729. if (int_usb & MUSB_INTR_DISCONNECT) {
  730. musb_dbg(musb, "DISCONNECT (%s) as %s, devctl %02x",
  731. usb_otg_state_string(musb->xceiv->otg->state),
  732. MUSB_MODE(musb), devctl);
  733. handled = IRQ_HANDLED;
  734. switch (musb->xceiv->otg->state) {
  735. case OTG_STATE_A_HOST:
  736. case OTG_STATE_A_SUSPEND:
  737. musb_host_resume_root_hub(musb);
  738. musb_root_disconnect(musb);
  739. if (musb->a_wait_bcon != 0)
  740. musb_platform_try_idle(musb, jiffies
  741. + msecs_to_jiffies(musb->a_wait_bcon));
  742. break;
  743. case OTG_STATE_B_HOST:
  744. /* REVISIT this behaves for "real disconnect"
  745. * cases; make sure the other transitions from
  746. * from B_HOST act right too. The B_HOST code
  747. * in hnp_stop() is currently not used...
  748. */
  749. musb_root_disconnect(musb);
  750. if (musb->hcd)
  751. musb->hcd->self.is_b_host = 0;
  752. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  753. MUSB_DEV_MODE(musb);
  754. musb_g_disconnect(musb);
  755. break;
  756. case OTG_STATE_A_PERIPHERAL:
  757. musb_hnp_stop(musb);
  758. musb_root_disconnect(musb);
  759. /* FALLTHROUGH */
  760. case OTG_STATE_B_WAIT_ACON:
  761. /* FALLTHROUGH */
  762. case OTG_STATE_B_PERIPHERAL:
  763. case OTG_STATE_B_IDLE:
  764. musb_g_disconnect(musb);
  765. break;
  766. default:
  767. WARNING("unhandled DISCONNECT transition (%s)\n",
  768. usb_otg_state_string(musb->xceiv->otg->state));
  769. break;
  770. }
  771. }
  772. /* mentor saves a bit: bus reset and babble share the same irq.
  773. * only host sees babble; only peripheral sees bus reset.
  774. */
  775. if (int_usb & MUSB_INTR_RESET) {
  776. handled = IRQ_HANDLED;
  777. if (devctl & MUSB_DEVCTL_HM) {
  778. /*
  779. * When BABBLE happens what we can depends on which
  780. * platform MUSB is running, because some platforms
  781. * implemented proprietary means for 'recovering' from
  782. * Babble conditions. One such platform is AM335x. In
  783. * most cases, however, the only thing we can do is
  784. * drop the session.
  785. */
  786. dev_err(musb->controller, "Babble\n");
  787. if (is_host_active(musb))
  788. musb_recover_from_babble(musb);
  789. } else {
  790. musb_dbg(musb, "BUS RESET as %s",
  791. usb_otg_state_string(musb->xceiv->otg->state));
  792. switch (musb->xceiv->otg->state) {
  793. case OTG_STATE_A_SUSPEND:
  794. musb_g_reset(musb);
  795. /* FALLTHROUGH */
  796. case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
  797. /* never use invalid T(a_wait_bcon) */
  798. musb_dbg(musb, "HNP: in %s, %d msec timeout",
  799. usb_otg_state_string(musb->xceiv->otg->state),
  800. TA_WAIT_BCON(musb));
  801. mod_timer(&musb->otg_timer, jiffies
  802. + msecs_to_jiffies(TA_WAIT_BCON(musb)));
  803. break;
  804. case OTG_STATE_A_PERIPHERAL:
  805. del_timer(&musb->otg_timer);
  806. musb_g_reset(musb);
  807. break;
  808. case OTG_STATE_B_WAIT_ACON:
  809. musb_dbg(musb, "HNP: RESET (%s), to b_peripheral",
  810. usb_otg_state_string(musb->xceiv->otg->state));
  811. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  812. musb_g_reset(musb);
  813. break;
  814. case OTG_STATE_B_IDLE:
  815. musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
  816. /* FALLTHROUGH */
  817. case OTG_STATE_B_PERIPHERAL:
  818. musb_g_reset(musb);
  819. break;
  820. default:
  821. musb_dbg(musb, "Unhandled BUS RESET as %s",
  822. usb_otg_state_string(musb->xceiv->otg->state));
  823. }
  824. }
  825. }
  826. #if 0
  827. /* REVISIT ... this would be for multiplexing periodic endpoints, or
  828. * supporting transfer phasing to prevent exceeding ISO bandwidth
  829. * limits of a given frame or microframe.
  830. *
  831. * It's not needed for peripheral side, which dedicates endpoints;
  832. * though it _might_ use SOF irqs for other purposes.
  833. *
  834. * And it's not currently needed for host side, which also dedicates
  835. * endpoints, relies on TX/RX interval registers, and isn't claimed
  836. * to support ISO transfers yet.
  837. */
  838. if (int_usb & MUSB_INTR_SOF) {
  839. void __iomem *mbase = musb->mregs;
  840. struct musb_hw_ep *ep;
  841. u8 epnum;
  842. u16 frame;
  843. dev_dbg(musb->controller, "START_OF_FRAME\n");
  844. handled = IRQ_HANDLED;
  845. /* start any periodic Tx transfers waiting for current frame */
  846. frame = musb_readw(mbase, MUSB_FRAME);
  847. ep = musb->endpoints;
  848. for (epnum = 1; (epnum < musb->nr_endpoints)
  849. && (musb->epmask >= (1 << epnum));
  850. epnum++, ep++) {
  851. /*
  852. * FIXME handle framecounter wraps (12 bits)
  853. * eliminate duplicated StartUrb logic
  854. */
  855. if (ep->dwWaitFrame >= frame) {
  856. ep->dwWaitFrame = 0;
  857. pr_debug("SOF --> periodic TX%s on %d\n",
  858. ep->tx_channel ? " DMA" : "",
  859. epnum);
  860. if (!ep->tx_channel)
  861. musb_h_tx_start(musb, epnum);
  862. else
  863. cppi_hostdma_start(musb, epnum);
  864. }
  865. } /* end of for loop */
  866. }
  867. #endif
  868. schedule_work(&musb->irq_work);
  869. return handled;
  870. }
  871. /*-------------------------------------------------------------------------*/
  872. static void musb_disable_interrupts(struct musb *musb)
  873. {
  874. void __iomem *mbase = musb->mregs;
  875. u16 temp;
  876. /* disable interrupts */
  877. musb_writeb(mbase, MUSB_INTRUSBE, 0);
  878. musb->intrtxe = 0;
  879. musb_writew(mbase, MUSB_INTRTXE, 0);
  880. musb->intrrxe = 0;
  881. musb_writew(mbase, MUSB_INTRRXE, 0);
  882. /* flush pending interrupts */
  883. temp = musb_readb(mbase, MUSB_INTRUSB);
  884. temp = musb_readw(mbase, MUSB_INTRTX);
  885. temp = musb_readw(mbase, MUSB_INTRRX);
  886. }
  887. static void musb_enable_interrupts(struct musb *musb)
  888. {
  889. void __iomem *regs = musb->mregs;
  890. /* Set INT enable registers, enable interrupts */
  891. musb->intrtxe = musb->epmask;
  892. musb_writew(regs, MUSB_INTRTXE, musb->intrtxe);
  893. musb->intrrxe = musb->epmask & 0xfffe;
  894. musb_writew(regs, MUSB_INTRRXE, musb->intrrxe);
  895. musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
  896. }
  897. static void musb_generic_disable(struct musb *musb)
  898. {
  899. void __iomem *mbase = musb->mregs;
  900. musb_disable_interrupts(musb);
  901. /* off */
  902. musb_writeb(mbase, MUSB_DEVCTL, 0);
  903. }
  904. /*
  905. * Program the HDRC to start (enable interrupts, dma, etc.).
  906. */
  907. void musb_start(struct musb *musb)
  908. {
  909. void __iomem *regs = musb->mregs;
  910. u8 devctl = musb_readb(regs, MUSB_DEVCTL);
  911. u8 power;
  912. musb_dbg(musb, "<== devctl %02x", devctl);
  913. musb_enable_interrupts(musb);
  914. musb_writeb(regs, MUSB_TESTMODE, 0);
  915. power = MUSB_POWER_ISOUPDATE;
  916. /*
  917. * treating UNKNOWN as unspecified maximum speed, in which case
  918. * we will default to high-speed.
  919. */
  920. if (musb->config->maximum_speed == USB_SPEED_HIGH ||
  921. musb->config->maximum_speed == USB_SPEED_UNKNOWN)
  922. power |= MUSB_POWER_HSENAB;
  923. musb_writeb(regs, MUSB_POWER, power);
  924. musb->is_active = 0;
  925. devctl = musb_readb(regs, MUSB_DEVCTL);
  926. devctl &= ~MUSB_DEVCTL_SESSION;
  927. /* session started after:
  928. * (a) ID-grounded irq, host mode;
  929. * (b) vbus present/connect IRQ, peripheral mode;
  930. * (c) peripheral initiates, using SRP
  931. */
  932. if (musb->port_mode != MUSB_PORT_MODE_HOST &&
  933. musb->xceiv->otg->state != OTG_STATE_A_WAIT_BCON &&
  934. (devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) {
  935. musb->is_active = 1;
  936. } else {
  937. devctl |= MUSB_DEVCTL_SESSION;
  938. }
  939. musb_platform_enable(musb);
  940. musb_writeb(regs, MUSB_DEVCTL, devctl);
  941. }
  942. /*
  943. * Make the HDRC stop (disable interrupts, etc.);
  944. * reversible by musb_start
  945. * called on gadget driver unregister
  946. * with controller locked, irqs blocked
  947. * acts as a NOP unless some role activated the hardware
  948. */
  949. void musb_stop(struct musb *musb)
  950. {
  951. /* stop IRQs, timers, ... */
  952. musb_platform_disable(musb);
  953. musb_generic_disable(musb);
  954. musb_dbg(musb, "HDRC disabled");
  955. /* FIXME
  956. * - mark host and/or peripheral drivers unusable/inactive
  957. * - disable DMA (and enable it in HdrcStart)
  958. * - make sure we can musb_start() after musb_stop(); with
  959. * OTG mode, gadget driver module rmmod/modprobe cycles that
  960. * - ...
  961. */
  962. musb_platform_try_idle(musb, 0);
  963. }
  964. /*-------------------------------------------------------------------------*/
  965. /*
  966. * The silicon either has hard-wired endpoint configurations, or else
  967. * "dynamic fifo" sizing. The driver has support for both, though at this
  968. * writing only the dynamic sizing is very well tested. Since we switched
  969. * away from compile-time hardware parameters, we can no longer rely on
  970. * dead code elimination to leave only the relevant one in the object file.
  971. *
  972. * We don't currently use dynamic fifo setup capability to do anything
  973. * more than selecting one of a bunch of predefined configurations.
  974. */
  975. static ushort fifo_mode;
  976. /* "modprobe ... fifo_mode=1" etc */
  977. module_param(fifo_mode, ushort, 0);
  978. MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
  979. /*
  980. * tables defining fifo_mode values. define more if you like.
  981. * for host side, make sure both halves of ep1 are set up.
  982. */
  983. /* mode 0 - fits in 2KB */
  984. static struct musb_fifo_cfg mode_0_cfg[] = {
  985. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  986. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  987. { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
  988. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  989. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  990. };
  991. /* mode 1 - fits in 4KB */
  992. static struct musb_fifo_cfg mode_1_cfg[] = {
  993. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  994. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  995. { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  996. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  997. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  998. };
  999. /* mode 2 - fits in 4KB */
  1000. static struct musb_fifo_cfg mode_2_cfg[] = {
  1001. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1002. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1003. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1004. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1005. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  1006. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  1007. };
  1008. /* mode 3 - fits in 4KB */
  1009. static struct musb_fifo_cfg mode_3_cfg[] = {
  1010. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  1011. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
  1012. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1013. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1014. { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
  1015. { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
  1016. };
  1017. /* mode 4 - fits in 16KB */
  1018. static struct musb_fifo_cfg mode_4_cfg[] = {
  1019. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1020. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1021. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1022. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1023. { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  1024. { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  1025. { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
  1026. { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
  1027. { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
  1028. { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
  1029. { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
  1030. { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
  1031. { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
  1032. { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
  1033. { .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
  1034. { .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
  1035. { .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
  1036. { .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
  1037. { .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
  1038. { .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
  1039. { .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
  1040. { .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
  1041. { .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
  1042. { .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
  1043. { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
  1044. { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
  1045. { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
  1046. };
  1047. /* mode 5 - fits in 8KB */
  1048. static struct musb_fifo_cfg mode_5_cfg[] = {
  1049. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  1050. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  1051. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  1052. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  1053. { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  1054. { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  1055. { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
  1056. { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
  1057. { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
  1058. { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
  1059. { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
  1060. { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
  1061. { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
  1062. { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
  1063. { .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
  1064. { .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
  1065. { .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
  1066. { .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
  1067. { .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
  1068. { .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
  1069. { .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
  1070. { .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
  1071. { .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
  1072. { .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
  1073. { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
  1074. { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
  1075. { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
  1076. };
  1077. /*
  1078. * configure a fifo; for non-shared endpoints, this may be called
  1079. * once for a tx fifo and once for an rx fifo.
  1080. *
  1081. * returns negative errno or offset for next fifo.
  1082. */
  1083. static int
  1084. fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
  1085. const struct musb_fifo_cfg *cfg, u16 offset)
  1086. {
  1087. void __iomem *mbase = musb->mregs;
  1088. int size = 0;
  1089. u16 maxpacket = cfg->maxpacket;
  1090. u16 c_off = offset >> 3;
  1091. u8 c_size;
  1092. /* expect hw_ep has already been zero-initialized */
  1093. size = ffs(max(maxpacket, (u16) 8)) - 1;
  1094. maxpacket = 1 << size;
  1095. c_size = size - 3;
  1096. if (cfg->mode == BUF_DOUBLE) {
  1097. if ((offset + (maxpacket << 1)) >
  1098. (1 << (musb->config->ram_bits + 2)))
  1099. return -EMSGSIZE;
  1100. c_size |= MUSB_FIFOSZ_DPB;
  1101. } else {
  1102. if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
  1103. return -EMSGSIZE;
  1104. }
  1105. /* configure the FIFO */
  1106. musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
  1107. /* EP0 reserved endpoint for control, bidirectional;
  1108. * EP1 reserved for bulk, two unidirectional halves.
  1109. */
  1110. if (hw_ep->epnum == 1)
  1111. musb->bulk_ep = hw_ep;
  1112. /* REVISIT error check: be sure ep0 can both rx and tx ... */
  1113. switch (cfg->style) {
  1114. case FIFO_TX:
  1115. musb_write_txfifosz(mbase, c_size);
  1116. musb_write_txfifoadd(mbase, c_off);
  1117. hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
  1118. hw_ep->max_packet_sz_tx = maxpacket;
  1119. break;
  1120. case FIFO_RX:
  1121. musb_write_rxfifosz(mbase, c_size);
  1122. musb_write_rxfifoadd(mbase, c_off);
  1123. hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
  1124. hw_ep->max_packet_sz_rx = maxpacket;
  1125. break;
  1126. case FIFO_RXTX:
  1127. musb_write_txfifosz(mbase, c_size);
  1128. musb_write_txfifoadd(mbase, c_off);
  1129. hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
  1130. hw_ep->max_packet_sz_rx = maxpacket;
  1131. musb_write_rxfifosz(mbase, c_size);
  1132. musb_write_rxfifoadd(mbase, c_off);
  1133. hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
  1134. hw_ep->max_packet_sz_tx = maxpacket;
  1135. hw_ep->is_shared_fifo = true;
  1136. break;
  1137. }
  1138. /* NOTE rx and tx endpoint irqs aren't managed separately,
  1139. * which happens to be ok
  1140. */
  1141. musb->epmask |= (1 << hw_ep->epnum);
  1142. return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
  1143. }
  1144. static struct musb_fifo_cfg ep0_cfg = {
  1145. .style = FIFO_RXTX, .maxpacket = 64,
  1146. };
  1147. static int ep_config_from_table(struct musb *musb)
  1148. {
  1149. const struct musb_fifo_cfg *cfg;
  1150. unsigned i, n;
  1151. int offset;
  1152. struct musb_hw_ep *hw_ep = musb->endpoints;
  1153. if (musb->config->fifo_cfg) {
  1154. cfg = musb->config->fifo_cfg;
  1155. n = musb->config->fifo_cfg_size;
  1156. goto done;
  1157. }
  1158. switch (fifo_mode) {
  1159. default:
  1160. fifo_mode = 0;
  1161. /* FALLTHROUGH */
  1162. case 0:
  1163. cfg = mode_0_cfg;
  1164. n = ARRAY_SIZE(mode_0_cfg);
  1165. break;
  1166. case 1:
  1167. cfg = mode_1_cfg;
  1168. n = ARRAY_SIZE(mode_1_cfg);
  1169. break;
  1170. case 2:
  1171. cfg = mode_2_cfg;
  1172. n = ARRAY_SIZE(mode_2_cfg);
  1173. break;
  1174. case 3:
  1175. cfg = mode_3_cfg;
  1176. n = ARRAY_SIZE(mode_3_cfg);
  1177. break;
  1178. case 4:
  1179. cfg = mode_4_cfg;
  1180. n = ARRAY_SIZE(mode_4_cfg);
  1181. break;
  1182. case 5:
  1183. cfg = mode_5_cfg;
  1184. n = ARRAY_SIZE(mode_5_cfg);
  1185. break;
  1186. }
  1187. pr_debug("%s: setup fifo_mode %d\n", musb_driver_name, fifo_mode);
  1188. done:
  1189. offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
  1190. /* assert(offset > 0) */
  1191. /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
  1192. * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
  1193. */
  1194. for (i = 0; i < n; i++) {
  1195. u8 epn = cfg->hw_ep_num;
  1196. if (epn >= musb->config->num_eps) {
  1197. pr_debug("%s: invalid ep %d\n",
  1198. musb_driver_name, epn);
  1199. return -EINVAL;
  1200. }
  1201. offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
  1202. if (offset < 0) {
  1203. pr_debug("%s: mem overrun, ep %d\n",
  1204. musb_driver_name, epn);
  1205. return offset;
  1206. }
  1207. epn++;
  1208. musb->nr_endpoints = max(epn, musb->nr_endpoints);
  1209. }
  1210. pr_debug("%s: %d/%d max ep, %d/%d memory\n",
  1211. musb_driver_name,
  1212. n + 1, musb->config->num_eps * 2 - 1,
  1213. offset, (1 << (musb->config->ram_bits + 2)));
  1214. if (!musb->bulk_ep) {
  1215. pr_debug("%s: missing bulk\n", musb_driver_name);
  1216. return -EINVAL;
  1217. }
  1218. return 0;
  1219. }
  1220. /*
  1221. * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
  1222. * @param musb the controller
  1223. */
  1224. static int ep_config_from_hw(struct musb *musb)
  1225. {
  1226. u8 epnum = 0;
  1227. struct musb_hw_ep *hw_ep;
  1228. void __iomem *mbase = musb->mregs;
  1229. int ret = 0;
  1230. musb_dbg(musb, "<== static silicon ep config");
  1231. /* FIXME pick up ep0 maxpacket size */
  1232. for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
  1233. musb_ep_select(mbase, epnum);
  1234. hw_ep = musb->endpoints + epnum;
  1235. ret = musb_read_fifosize(musb, hw_ep, epnum);
  1236. if (ret < 0)
  1237. break;
  1238. /* FIXME set up hw_ep->{rx,tx}_double_buffered */
  1239. /* pick an RX/TX endpoint for bulk */
  1240. if (hw_ep->max_packet_sz_tx < 512
  1241. || hw_ep->max_packet_sz_rx < 512)
  1242. continue;
  1243. /* REVISIT: this algorithm is lazy, we should at least
  1244. * try to pick a double buffered endpoint.
  1245. */
  1246. if (musb->bulk_ep)
  1247. continue;
  1248. musb->bulk_ep = hw_ep;
  1249. }
  1250. if (!musb->bulk_ep) {
  1251. pr_debug("%s: missing bulk\n", musb_driver_name);
  1252. return -EINVAL;
  1253. }
  1254. return 0;
  1255. }
  1256. enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
  1257. /* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
  1258. * configure endpoints, or take their config from silicon
  1259. */
  1260. static int musb_core_init(u16 musb_type, struct musb *musb)
  1261. {
  1262. u8 reg;
  1263. char *type;
  1264. char aInfo[90];
  1265. void __iomem *mbase = musb->mregs;
  1266. int status = 0;
  1267. int i;
  1268. /* log core options (read using indexed model) */
  1269. reg = musb_read_configdata(mbase);
  1270. strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
  1271. if (reg & MUSB_CONFIGDATA_DYNFIFO) {
  1272. strcat(aInfo, ", dyn FIFOs");
  1273. musb->dyn_fifo = true;
  1274. }
  1275. if (reg & MUSB_CONFIGDATA_MPRXE) {
  1276. strcat(aInfo, ", bulk combine");
  1277. musb->bulk_combine = true;
  1278. }
  1279. if (reg & MUSB_CONFIGDATA_MPTXE) {
  1280. strcat(aInfo, ", bulk split");
  1281. musb->bulk_split = true;
  1282. }
  1283. if (reg & MUSB_CONFIGDATA_HBRXE) {
  1284. strcat(aInfo, ", HB-ISO Rx");
  1285. musb->hb_iso_rx = true;
  1286. }
  1287. if (reg & MUSB_CONFIGDATA_HBTXE) {
  1288. strcat(aInfo, ", HB-ISO Tx");
  1289. musb->hb_iso_tx = true;
  1290. }
  1291. if (reg & MUSB_CONFIGDATA_SOFTCONE)
  1292. strcat(aInfo, ", SoftConn");
  1293. pr_debug("%s: ConfigData=0x%02x (%s)\n", musb_driver_name, reg, aInfo);
  1294. if (MUSB_CONTROLLER_MHDRC == musb_type) {
  1295. musb->is_multipoint = 1;
  1296. type = "M";
  1297. } else {
  1298. musb->is_multipoint = 0;
  1299. type = "";
  1300. #ifndef CONFIG_USB_OTG_BLACKLIST_HUB
  1301. pr_err("%s: kernel must blacklist external hubs\n",
  1302. musb_driver_name);
  1303. #endif
  1304. }
  1305. /* log release info */
  1306. musb->hwvers = musb_read_hwvers(mbase);
  1307. pr_debug("%s: %sHDRC RTL version %d.%d%s\n",
  1308. musb_driver_name, type, MUSB_HWVERS_MAJOR(musb->hwvers),
  1309. MUSB_HWVERS_MINOR(musb->hwvers),
  1310. (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
  1311. /* configure ep0 */
  1312. musb_configure_ep0(musb);
  1313. /* discover endpoint configuration */
  1314. musb->nr_endpoints = 1;
  1315. musb->epmask = 1;
  1316. if (musb->dyn_fifo)
  1317. status = ep_config_from_table(musb);
  1318. else
  1319. status = ep_config_from_hw(musb);
  1320. if (status < 0)
  1321. return status;
  1322. /* finish init, and print endpoint config */
  1323. for (i = 0; i < musb->nr_endpoints; i++) {
  1324. struct musb_hw_ep *hw_ep = musb->endpoints + i;
  1325. hw_ep->fifo = musb->io.fifo_offset(i) + mbase;
  1326. #if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
  1327. if (musb->io.quirks & MUSB_IN_TUSB) {
  1328. hw_ep->fifo_async = musb->async + 0x400 +
  1329. musb->io.fifo_offset(i);
  1330. hw_ep->fifo_sync = musb->sync + 0x400 +
  1331. musb->io.fifo_offset(i);
  1332. hw_ep->fifo_sync_va =
  1333. musb->sync_va + 0x400 + musb->io.fifo_offset(i);
  1334. if (i == 0)
  1335. hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
  1336. else
  1337. hw_ep->conf = mbase + 0x400 +
  1338. (((i - 1) & 0xf) << 2);
  1339. }
  1340. #endif
  1341. hw_ep->regs = musb->io.ep_offset(i, 0) + mbase;
  1342. hw_ep->rx_reinit = 1;
  1343. hw_ep->tx_reinit = 1;
  1344. if (hw_ep->max_packet_sz_tx) {
  1345. musb_dbg(musb, "%s: hw_ep %d%s, %smax %d",
  1346. musb_driver_name, i,
  1347. hw_ep->is_shared_fifo ? "shared" : "tx",
  1348. hw_ep->tx_double_buffered
  1349. ? "doublebuffer, " : "",
  1350. hw_ep->max_packet_sz_tx);
  1351. }
  1352. if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
  1353. musb_dbg(musb, "%s: hw_ep %d%s, %smax %d",
  1354. musb_driver_name, i,
  1355. "rx",
  1356. hw_ep->rx_double_buffered
  1357. ? "doublebuffer, " : "",
  1358. hw_ep->max_packet_sz_rx);
  1359. }
  1360. if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
  1361. musb_dbg(musb, "hw_ep %d not configured", i);
  1362. }
  1363. return 0;
  1364. }
  1365. /*-------------------------------------------------------------------------*/
  1366. /*
  1367. * handle all the irqs defined by the HDRC core. for now we expect: other
  1368. * irq sources (phy, dma, etc) will be handled first, musb->int_* values
  1369. * will be assigned, and the irq will already have been acked.
  1370. *
  1371. * called in irq context with spinlock held, irqs blocked
  1372. */
  1373. irqreturn_t musb_interrupt(struct musb *musb)
  1374. {
  1375. irqreturn_t retval = IRQ_NONE;
  1376. unsigned long status;
  1377. unsigned long epnum;
  1378. u8 devctl;
  1379. if (!musb->int_usb && !musb->int_tx && !musb->int_rx)
  1380. return IRQ_NONE;
  1381. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1382. trace_musb_isr(musb);
  1383. /**
  1384. * According to Mentor Graphics' documentation, flowchart on page 98,
  1385. * IRQ should be handled as follows:
  1386. *
  1387. * . Resume IRQ
  1388. * . Session Request IRQ
  1389. * . VBUS Error IRQ
  1390. * . Suspend IRQ
  1391. * . Connect IRQ
  1392. * . Disconnect IRQ
  1393. * . Reset/Babble IRQ
  1394. * . SOF IRQ (we're not using this one)
  1395. * . Endpoint 0 IRQ
  1396. * . TX Endpoints
  1397. * . RX Endpoints
  1398. *
  1399. * We will be following that flowchart in order to avoid any problems
  1400. * that might arise with internal Finite State Machine.
  1401. */
  1402. if (musb->int_usb)
  1403. retval |= musb_stage0_irq(musb, musb->int_usb, devctl);
  1404. if (musb->int_tx & 1) {
  1405. if (is_host_active(musb))
  1406. retval |= musb_h_ep0_irq(musb);
  1407. else
  1408. retval |= musb_g_ep0_irq(musb);
  1409. /* we have just handled endpoint 0 IRQ, clear it */
  1410. musb->int_tx &= ~BIT(0);
  1411. }
  1412. status = musb->int_tx;
  1413. for_each_set_bit(epnum, &status, 16) {
  1414. retval = IRQ_HANDLED;
  1415. if (is_host_active(musb))
  1416. musb_host_tx(musb, epnum);
  1417. else
  1418. musb_g_tx(musb, epnum);
  1419. }
  1420. status = musb->int_rx;
  1421. for_each_set_bit(epnum, &status, 16) {
  1422. retval = IRQ_HANDLED;
  1423. if (is_host_active(musb))
  1424. musb_host_rx(musb, epnum);
  1425. else
  1426. musb_g_rx(musb, epnum);
  1427. }
  1428. return retval;
  1429. }
  1430. EXPORT_SYMBOL_GPL(musb_interrupt);
  1431. #ifndef CONFIG_MUSB_PIO_ONLY
  1432. static bool use_dma = 1;
  1433. /* "modprobe ... use_dma=0" etc */
  1434. module_param(use_dma, bool, 0644);
  1435. MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
  1436. void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
  1437. {
  1438. /* called with controller lock already held */
  1439. if (!epnum) {
  1440. if (!is_cppi_enabled(musb)) {
  1441. /* endpoint 0 */
  1442. if (is_host_active(musb))
  1443. musb_h_ep0_irq(musb);
  1444. else
  1445. musb_g_ep0_irq(musb);
  1446. }
  1447. } else {
  1448. /* endpoints 1..15 */
  1449. if (transmit) {
  1450. if (is_host_active(musb))
  1451. musb_host_tx(musb, epnum);
  1452. else
  1453. musb_g_tx(musb, epnum);
  1454. } else {
  1455. /* receive */
  1456. if (is_host_active(musb))
  1457. musb_host_rx(musb, epnum);
  1458. else
  1459. musb_g_rx(musb, epnum);
  1460. }
  1461. }
  1462. }
  1463. EXPORT_SYMBOL_GPL(musb_dma_completion);
  1464. #else
  1465. #define use_dma 0
  1466. #endif
  1467. static int (*musb_phy_callback)(enum musb_vbus_id_status status);
  1468. /*
  1469. * musb_mailbox - optional phy notifier function
  1470. * @status phy state change
  1471. *
  1472. * Optionally gets called from the USB PHY. Note that the USB PHY must be
  1473. * disabled at the point the phy_callback is registered or unregistered.
  1474. */
  1475. int musb_mailbox(enum musb_vbus_id_status status)
  1476. {
  1477. if (musb_phy_callback)
  1478. return musb_phy_callback(status);
  1479. return -ENODEV;
  1480. };
  1481. EXPORT_SYMBOL_GPL(musb_mailbox);
  1482. /*-------------------------------------------------------------------------*/
  1483. static ssize_t
  1484. musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  1485. {
  1486. struct musb *musb = dev_to_musb(dev);
  1487. unsigned long flags;
  1488. int ret = -EINVAL;
  1489. spin_lock_irqsave(&musb->lock, flags);
  1490. ret = sprintf(buf, "%s\n", usb_otg_state_string(musb->xceiv->otg->state));
  1491. spin_unlock_irqrestore(&musb->lock, flags);
  1492. return ret;
  1493. }
  1494. static ssize_t
  1495. musb_mode_store(struct device *dev, struct device_attribute *attr,
  1496. const char *buf, size_t n)
  1497. {
  1498. struct musb *musb = dev_to_musb(dev);
  1499. unsigned long flags;
  1500. int status;
  1501. spin_lock_irqsave(&musb->lock, flags);
  1502. if (sysfs_streq(buf, "host"))
  1503. status = musb_platform_set_mode(musb, MUSB_HOST);
  1504. else if (sysfs_streq(buf, "peripheral"))
  1505. status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
  1506. else if (sysfs_streq(buf, "otg"))
  1507. status = musb_platform_set_mode(musb, MUSB_OTG);
  1508. else
  1509. status = -EINVAL;
  1510. spin_unlock_irqrestore(&musb->lock, flags);
  1511. return (status == 0) ? n : status;
  1512. }
  1513. static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
  1514. static ssize_t
  1515. musb_vbus_store(struct device *dev, struct device_attribute *attr,
  1516. const char *buf, size_t n)
  1517. {
  1518. struct musb *musb = dev_to_musb(dev);
  1519. unsigned long flags;
  1520. unsigned long val;
  1521. if (sscanf(buf, "%lu", &val) < 1) {
  1522. dev_err(dev, "Invalid VBUS timeout ms value\n");
  1523. return -EINVAL;
  1524. }
  1525. spin_lock_irqsave(&musb->lock, flags);
  1526. /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
  1527. musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
  1528. if (musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON)
  1529. musb->is_active = 0;
  1530. musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
  1531. spin_unlock_irqrestore(&musb->lock, flags);
  1532. return n;
  1533. }
  1534. static ssize_t
  1535. musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
  1536. {
  1537. struct musb *musb = dev_to_musb(dev);
  1538. unsigned long flags;
  1539. unsigned long val;
  1540. int vbus;
  1541. u8 devctl;
  1542. spin_lock_irqsave(&musb->lock, flags);
  1543. val = musb->a_wait_bcon;
  1544. vbus = musb_platform_get_vbus_status(musb);
  1545. if (vbus < 0) {
  1546. /* Use default MUSB method by means of DEVCTL register */
  1547. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1548. if ((devctl & MUSB_DEVCTL_VBUS)
  1549. == (3 << MUSB_DEVCTL_VBUS_SHIFT))
  1550. vbus = 1;
  1551. else
  1552. vbus = 0;
  1553. }
  1554. spin_unlock_irqrestore(&musb->lock, flags);
  1555. return sprintf(buf, "Vbus %s, timeout %lu msec\n",
  1556. vbus ? "on" : "off", val);
  1557. }
  1558. static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
  1559. /* Gadget drivers can't know that a host is connected so they might want
  1560. * to start SRP, but users can. This allows userspace to trigger SRP.
  1561. */
  1562. static ssize_t
  1563. musb_srp_store(struct device *dev, struct device_attribute *attr,
  1564. const char *buf, size_t n)
  1565. {
  1566. struct musb *musb = dev_to_musb(dev);
  1567. unsigned short srp;
  1568. if (sscanf(buf, "%hu", &srp) != 1
  1569. || (srp != 1)) {
  1570. dev_err(dev, "SRP: Value must be 1\n");
  1571. return -EINVAL;
  1572. }
  1573. if (srp == 1)
  1574. musb_g_wakeup(musb);
  1575. return n;
  1576. }
  1577. static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
  1578. static struct attribute *musb_attributes[] = {
  1579. &dev_attr_mode.attr,
  1580. &dev_attr_vbus.attr,
  1581. &dev_attr_srp.attr,
  1582. NULL
  1583. };
  1584. static const struct attribute_group musb_attr_group = {
  1585. .attrs = musb_attributes,
  1586. };
  1587. #define MUSB_QUIRK_B_INVALID_VBUS_91 (MUSB_DEVCTL_BDEVICE | \
  1588. (2 << MUSB_DEVCTL_VBUS_SHIFT) | \
  1589. MUSB_DEVCTL_SESSION)
  1590. #define MUSB_QUIRK_A_DISCONNECT_19 ((3 << MUSB_DEVCTL_VBUS_SHIFT) | \
  1591. MUSB_DEVCTL_SESSION)
  1592. /*
  1593. * Check the musb devctl session bit to determine if we want to
  1594. * allow PM runtime for the device. In general, we want to keep things
  1595. * active when the session bit is set except after host disconnect.
  1596. *
  1597. * Only called from musb_irq_work. If this ever needs to get called
  1598. * elsewhere, proper locking must be implemented for musb->session.
  1599. */
  1600. static void musb_pm_runtime_check_session(struct musb *musb)
  1601. {
  1602. u8 devctl, s;
  1603. int error;
  1604. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1605. /* Handle session status quirks first */
  1606. s = MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV |
  1607. MUSB_DEVCTL_HR;
  1608. switch (devctl & ~s) {
  1609. case MUSB_QUIRK_B_INVALID_VBUS_91:
  1610. if (!musb->session && !musb->quirk_invalid_vbus) {
  1611. musb->quirk_invalid_vbus = true;
  1612. musb_dbg(musb,
  1613. "First invalid vbus, assume no session");
  1614. return;
  1615. }
  1616. break;
  1617. case MUSB_QUIRK_A_DISCONNECT_19:
  1618. if (!musb->session)
  1619. break;
  1620. musb_dbg(musb, "Allow PM on possible host mode disconnect");
  1621. pm_runtime_mark_last_busy(musb->controller);
  1622. pm_runtime_put_autosuspend(musb->controller);
  1623. musb->session = false;
  1624. return;
  1625. default:
  1626. break;
  1627. }
  1628. /* No need to do anything if session has not changed */
  1629. s = devctl & MUSB_DEVCTL_SESSION;
  1630. if (s == musb->session)
  1631. return;
  1632. /* Block PM or allow PM? */
  1633. if (s) {
  1634. musb_dbg(musb, "Block PM on active session: %02x", devctl);
  1635. error = pm_runtime_get_sync(musb->controller);
  1636. if (error < 0)
  1637. dev_err(musb->controller, "Could not enable: %i\n",
  1638. error);
  1639. } else {
  1640. musb_dbg(musb, "Allow PM with no session: %02x", devctl);
  1641. musb->quirk_invalid_vbus = false;
  1642. pm_runtime_mark_last_busy(musb->controller);
  1643. pm_runtime_put_autosuspend(musb->controller);
  1644. }
  1645. musb->session = s;
  1646. }
  1647. /* Only used to provide driver mode change events */
  1648. static void musb_irq_work(struct work_struct *data)
  1649. {
  1650. struct musb *musb = container_of(data, struct musb, irq_work);
  1651. musb_pm_runtime_check_session(musb);
  1652. if (musb->xceiv->otg->state != musb->xceiv_old_state) {
  1653. musb->xceiv_old_state = musb->xceiv->otg->state;
  1654. sysfs_notify(&musb->controller->kobj, NULL, "mode");
  1655. }
  1656. }
  1657. static void musb_recover_from_babble(struct musb *musb)
  1658. {
  1659. int ret;
  1660. u8 devctl;
  1661. musb_disable_interrupts(musb);
  1662. /*
  1663. * wait at least 320 cycles of 60MHz clock. That's 5.3us, we will give
  1664. * it some slack and wait for 10us.
  1665. */
  1666. udelay(10);
  1667. ret = musb_platform_recover(musb);
  1668. if (ret) {
  1669. musb_enable_interrupts(musb);
  1670. return;
  1671. }
  1672. /* drop session bit */
  1673. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1674. devctl &= ~MUSB_DEVCTL_SESSION;
  1675. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  1676. /* tell usbcore about it */
  1677. musb_root_disconnect(musb);
  1678. /*
  1679. * When a babble condition occurs, the musb controller
  1680. * removes the session bit and the endpoint config is lost.
  1681. */
  1682. if (musb->dyn_fifo)
  1683. ret = ep_config_from_table(musb);
  1684. else
  1685. ret = ep_config_from_hw(musb);
  1686. /* restart session */
  1687. if (ret == 0)
  1688. musb_start(musb);
  1689. }
  1690. /* --------------------------------------------------------------------------
  1691. * Init support
  1692. */
  1693. static struct musb *allocate_instance(struct device *dev,
  1694. const struct musb_hdrc_config *config, void __iomem *mbase)
  1695. {
  1696. struct musb *musb;
  1697. struct musb_hw_ep *ep;
  1698. int epnum;
  1699. int ret;
  1700. musb = devm_kzalloc(dev, sizeof(*musb), GFP_KERNEL);
  1701. if (!musb)
  1702. return NULL;
  1703. INIT_LIST_HEAD(&musb->control);
  1704. INIT_LIST_HEAD(&musb->in_bulk);
  1705. INIT_LIST_HEAD(&musb->out_bulk);
  1706. musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
  1707. musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
  1708. musb->mregs = mbase;
  1709. musb->ctrl_base = mbase;
  1710. musb->nIrq = -ENODEV;
  1711. musb->config = config;
  1712. BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
  1713. for (epnum = 0, ep = musb->endpoints;
  1714. epnum < musb->config->num_eps;
  1715. epnum++, ep++) {
  1716. ep->musb = musb;
  1717. ep->epnum = epnum;
  1718. }
  1719. musb->controller = dev;
  1720. ret = musb_host_alloc(musb);
  1721. if (ret < 0)
  1722. goto err_free;
  1723. dev_set_drvdata(dev, musb);
  1724. return musb;
  1725. err_free:
  1726. return NULL;
  1727. }
  1728. static void musb_free(struct musb *musb)
  1729. {
  1730. /* this has multiple entry modes. it handles fault cleanup after
  1731. * probe(), where things may be partially set up, as well as rmmod
  1732. * cleanup after everything's been de-activated.
  1733. */
  1734. #ifdef CONFIG_SYSFS
  1735. sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
  1736. #endif
  1737. if (musb->nIrq >= 0) {
  1738. if (musb->irq_wake)
  1739. disable_irq_wake(musb->nIrq);
  1740. free_irq(musb->nIrq, musb);
  1741. }
  1742. musb_host_free(musb);
  1743. }
  1744. static void musb_deassert_reset(struct work_struct *work)
  1745. {
  1746. struct musb *musb;
  1747. unsigned long flags;
  1748. musb = container_of(work, struct musb, deassert_reset_work.work);
  1749. spin_lock_irqsave(&musb->lock, flags);
  1750. if (musb->port1_status & USB_PORT_STAT_RESET)
  1751. musb_port_reset(musb, false);
  1752. spin_unlock_irqrestore(&musb->lock, flags);
  1753. }
  1754. /*
  1755. * Perform generic per-controller initialization.
  1756. *
  1757. * @dev: the controller (already clocked, etc)
  1758. * @nIrq: IRQ number
  1759. * @ctrl: virtual address of controller registers,
  1760. * not yet corrected for platform-specific offsets
  1761. */
  1762. static int
  1763. musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
  1764. {
  1765. int status;
  1766. struct musb *musb;
  1767. struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
  1768. /* The driver might handle more features than the board; OK.
  1769. * Fail when the board needs a feature that's not enabled.
  1770. */
  1771. if (!plat) {
  1772. dev_err(dev, "no platform_data?\n");
  1773. status = -ENODEV;
  1774. goto fail0;
  1775. }
  1776. /* allocate */
  1777. musb = allocate_instance(dev, plat->config, ctrl);
  1778. if (!musb) {
  1779. status = -ENOMEM;
  1780. goto fail0;
  1781. }
  1782. spin_lock_init(&musb->lock);
  1783. musb->board_set_power = plat->set_power;
  1784. musb->min_power = plat->min_power;
  1785. musb->ops = plat->platform_ops;
  1786. musb->port_mode = plat->mode;
  1787. /*
  1788. * Initialize the default IO functions. At least omap2430 needs
  1789. * these early. We initialize the platform specific IO functions
  1790. * later on.
  1791. */
  1792. musb_readb = musb_default_readb;
  1793. musb_writeb = musb_default_writeb;
  1794. musb_readw = musb_default_readw;
  1795. musb_writew = musb_default_writew;
  1796. musb_readl = musb_default_readl;
  1797. musb_writel = musb_default_writel;
  1798. /* The musb_platform_init() call:
  1799. * - adjusts musb->mregs
  1800. * - sets the musb->isr
  1801. * - may initialize an integrated transceiver
  1802. * - initializes musb->xceiv, usually by otg_get_phy()
  1803. * - stops powering VBUS
  1804. *
  1805. * There are various transceiver configurations. Blackfin,
  1806. * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
  1807. * external/discrete ones in various flavors (twl4030 family,
  1808. * isp1504, non-OTG, etc) mostly hooking up through ULPI.
  1809. */
  1810. status = musb_platform_init(musb);
  1811. if (status < 0)
  1812. goto fail1;
  1813. if (!musb->isr) {
  1814. status = -ENODEV;
  1815. goto fail2;
  1816. }
  1817. if (musb->ops->quirks)
  1818. musb->io.quirks = musb->ops->quirks;
  1819. /* Most devices use indexed offset or flat offset */
  1820. if (musb->io.quirks & MUSB_INDEXED_EP) {
  1821. musb->io.ep_offset = musb_indexed_ep_offset;
  1822. musb->io.ep_select = musb_indexed_ep_select;
  1823. } else {
  1824. musb->io.ep_offset = musb_flat_ep_offset;
  1825. musb->io.ep_select = musb_flat_ep_select;
  1826. }
  1827. /* And override them with platform specific ops if specified. */
  1828. if (musb->ops->ep_offset)
  1829. musb->io.ep_offset = musb->ops->ep_offset;
  1830. if (musb->ops->ep_select)
  1831. musb->io.ep_select = musb->ops->ep_select;
  1832. /* At least tusb6010 has its own offsets */
  1833. if (musb->ops->ep_offset)
  1834. musb->io.ep_offset = musb->ops->ep_offset;
  1835. if (musb->ops->ep_select)
  1836. musb->io.ep_select = musb->ops->ep_select;
  1837. if (musb->ops->fifo_mode)
  1838. fifo_mode = musb->ops->fifo_mode;
  1839. else
  1840. fifo_mode = 4;
  1841. if (musb->ops->fifo_offset)
  1842. musb->io.fifo_offset = musb->ops->fifo_offset;
  1843. else
  1844. musb->io.fifo_offset = musb_default_fifo_offset;
  1845. if (musb->ops->busctl_offset)
  1846. musb->io.busctl_offset = musb->ops->busctl_offset;
  1847. else
  1848. musb->io.busctl_offset = musb_default_busctl_offset;
  1849. if (musb->ops->readb)
  1850. musb_readb = musb->ops->readb;
  1851. if (musb->ops->writeb)
  1852. musb_writeb = musb->ops->writeb;
  1853. if (musb->ops->readw)
  1854. musb_readw = musb->ops->readw;
  1855. if (musb->ops->writew)
  1856. musb_writew = musb->ops->writew;
  1857. if (musb->ops->readl)
  1858. musb_readl = musb->ops->readl;
  1859. if (musb->ops->writel)
  1860. musb_writel = musb->ops->writel;
  1861. #ifndef CONFIG_MUSB_PIO_ONLY
  1862. if (!musb->ops->dma_init || !musb->ops->dma_exit) {
  1863. dev_err(dev, "DMA controller not set\n");
  1864. status = -ENODEV;
  1865. goto fail2;
  1866. }
  1867. musb_dma_controller_create = musb->ops->dma_init;
  1868. musb_dma_controller_destroy = musb->ops->dma_exit;
  1869. #endif
  1870. if (musb->ops->read_fifo)
  1871. musb->io.read_fifo = musb->ops->read_fifo;
  1872. else
  1873. musb->io.read_fifo = musb_default_read_fifo;
  1874. if (musb->ops->write_fifo)
  1875. musb->io.write_fifo = musb->ops->write_fifo;
  1876. else
  1877. musb->io.write_fifo = musb_default_write_fifo;
  1878. if (!musb->xceiv->io_ops) {
  1879. musb->xceiv->io_dev = musb->controller;
  1880. musb->xceiv->io_priv = musb->mregs;
  1881. musb->xceiv->io_ops = &musb_ulpi_access;
  1882. }
  1883. if (musb->ops->phy_callback)
  1884. musb_phy_callback = musb->ops->phy_callback;
  1885. /*
  1886. * We need musb_read/write functions initialized for PM.
  1887. * Note that at least 2430 glue needs autosuspend delay
  1888. * somewhere above 300 ms for the hardware to idle properly
  1889. * after disconnecting the cable in host mode. Let's use
  1890. * 500 ms for some margin.
  1891. */
  1892. pm_runtime_use_autosuspend(musb->controller);
  1893. pm_runtime_set_autosuspend_delay(musb->controller, 500);
  1894. pm_runtime_enable(musb->controller);
  1895. pm_runtime_get_sync(musb->controller);
  1896. status = usb_phy_init(musb->xceiv);
  1897. if (status < 0)
  1898. goto err_usb_phy_init;
  1899. if (use_dma && dev->dma_mask) {
  1900. musb->dma_controller =
  1901. musb_dma_controller_create(musb, musb->mregs);
  1902. if (IS_ERR(musb->dma_controller)) {
  1903. status = PTR_ERR(musb->dma_controller);
  1904. goto fail2_5;
  1905. }
  1906. }
  1907. /* be sure interrupts are disabled before connecting ISR */
  1908. musb_platform_disable(musb);
  1909. musb_generic_disable(musb);
  1910. /* Init IRQ workqueue before request_irq */
  1911. INIT_WORK(&musb->irq_work, musb_irq_work);
  1912. INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset);
  1913. INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume);
  1914. /* setup musb parts of the core (especially endpoints) */
  1915. status = musb_core_init(plat->config->multipoint
  1916. ? MUSB_CONTROLLER_MHDRC
  1917. : MUSB_CONTROLLER_HDRC, musb);
  1918. if (status < 0)
  1919. goto fail3;
  1920. setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
  1921. /* attach to the IRQ */
  1922. if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
  1923. dev_err(dev, "request_irq %d failed!\n", nIrq);
  1924. status = -ENODEV;
  1925. goto fail3;
  1926. }
  1927. musb->nIrq = nIrq;
  1928. /* FIXME this handles wakeup irqs wrong */
  1929. if (enable_irq_wake(nIrq) == 0) {
  1930. musb->irq_wake = 1;
  1931. device_init_wakeup(dev, 1);
  1932. } else {
  1933. musb->irq_wake = 0;
  1934. }
  1935. /* program PHY to use external vBus if required */
  1936. if (plat->extvbus) {
  1937. u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
  1938. busctl |= MUSB_ULPI_USE_EXTVBUS;
  1939. musb_write_ulpi_buscontrol(musb->mregs, busctl);
  1940. }
  1941. if (musb->xceiv->otg->default_a) {
  1942. MUSB_HST_MODE(musb);
  1943. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  1944. } else {
  1945. MUSB_DEV_MODE(musb);
  1946. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  1947. }
  1948. switch (musb->port_mode) {
  1949. case MUSB_PORT_MODE_HOST:
  1950. status = musb_host_setup(musb, plat->power);
  1951. if (status < 0)
  1952. goto fail3;
  1953. status = musb_platform_set_mode(musb, MUSB_HOST);
  1954. break;
  1955. case MUSB_PORT_MODE_GADGET:
  1956. status = musb_gadget_setup(musb);
  1957. if (status < 0)
  1958. goto fail3;
  1959. status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
  1960. break;
  1961. case MUSB_PORT_MODE_DUAL_ROLE:
  1962. status = musb_host_setup(musb, plat->power);
  1963. if (status < 0)
  1964. goto fail3;
  1965. status = musb_gadget_setup(musb);
  1966. if (status) {
  1967. musb_host_cleanup(musb);
  1968. goto fail3;
  1969. }
  1970. status = musb_platform_set_mode(musb, MUSB_OTG);
  1971. break;
  1972. default:
  1973. dev_err(dev, "unsupported port mode %d\n", musb->port_mode);
  1974. break;
  1975. }
  1976. if (status < 0)
  1977. goto fail3;
  1978. status = musb_init_debugfs(musb);
  1979. if (status < 0)
  1980. goto fail4;
  1981. status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
  1982. if (status)
  1983. goto fail5;
  1984. pm_runtime_mark_last_busy(musb->controller);
  1985. pm_runtime_put_autosuspend(musb->controller);
  1986. return 0;
  1987. fail5:
  1988. musb_exit_debugfs(musb);
  1989. fail4:
  1990. musb_gadget_cleanup(musb);
  1991. musb_host_cleanup(musb);
  1992. fail3:
  1993. cancel_work_sync(&musb->irq_work);
  1994. cancel_delayed_work_sync(&musb->finish_resume_work);
  1995. cancel_delayed_work_sync(&musb->deassert_reset_work);
  1996. if (musb->dma_controller)
  1997. musb_dma_controller_destroy(musb->dma_controller);
  1998. fail2_5:
  1999. usb_phy_shutdown(musb->xceiv);
  2000. err_usb_phy_init:
  2001. pm_runtime_dont_use_autosuspend(musb->controller);
  2002. pm_runtime_put_sync(musb->controller);
  2003. pm_runtime_disable(musb->controller);
  2004. fail2:
  2005. if (musb->irq_wake)
  2006. device_init_wakeup(dev, 0);
  2007. musb_platform_exit(musb);
  2008. fail1:
  2009. dev_err(musb->controller,
  2010. "musb_init_controller failed with status %d\n", status);
  2011. musb_free(musb);
  2012. fail0:
  2013. return status;
  2014. }
  2015. /*-------------------------------------------------------------------------*/
  2016. /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
  2017. * bridge to a platform device; this driver then suffices.
  2018. */
  2019. static int musb_probe(struct platform_device *pdev)
  2020. {
  2021. struct device *dev = &pdev->dev;
  2022. int irq = platform_get_irq_byname(pdev, "mc");
  2023. struct resource *iomem;
  2024. void __iomem *base;
  2025. if (irq <= 0)
  2026. return -ENODEV;
  2027. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2028. base = devm_ioremap_resource(dev, iomem);
  2029. if (IS_ERR(base))
  2030. return PTR_ERR(base);
  2031. return musb_init_controller(dev, irq, base);
  2032. }
  2033. static int musb_remove(struct platform_device *pdev)
  2034. {
  2035. struct device *dev = &pdev->dev;
  2036. struct musb *musb = dev_to_musb(dev);
  2037. unsigned long flags;
  2038. /* this gets called on rmmod.
  2039. * - Host mode: host may still be active
  2040. * - Peripheral mode: peripheral is deactivated (or never-activated)
  2041. * - OTG mode: both roles are deactivated (or never-activated)
  2042. */
  2043. musb_exit_debugfs(musb);
  2044. cancel_work_sync(&musb->irq_work);
  2045. cancel_delayed_work_sync(&musb->finish_resume_work);
  2046. cancel_delayed_work_sync(&musb->deassert_reset_work);
  2047. pm_runtime_get_sync(musb->controller);
  2048. musb_host_cleanup(musb);
  2049. musb_gadget_cleanup(musb);
  2050. spin_lock_irqsave(&musb->lock, flags);
  2051. musb_platform_disable(musb);
  2052. musb_generic_disable(musb);
  2053. spin_unlock_irqrestore(&musb->lock, flags);
  2054. musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
  2055. pm_runtime_dont_use_autosuspend(musb->controller);
  2056. pm_runtime_put_sync(musb->controller);
  2057. pm_runtime_disable(musb->controller);
  2058. musb_platform_exit(musb);
  2059. musb_phy_callback = NULL;
  2060. if (musb->dma_controller)
  2061. musb_dma_controller_destroy(musb->dma_controller);
  2062. usb_phy_shutdown(musb->xceiv);
  2063. musb_free(musb);
  2064. device_init_wakeup(dev, 0);
  2065. return 0;
  2066. }
  2067. #ifdef CONFIG_PM
  2068. static void musb_save_context(struct musb *musb)
  2069. {
  2070. int i;
  2071. void __iomem *musb_base = musb->mregs;
  2072. void __iomem *epio;
  2073. musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
  2074. musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
  2075. musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
  2076. musb->context.power = musb_readb(musb_base, MUSB_POWER);
  2077. musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
  2078. musb->context.index = musb_readb(musb_base, MUSB_INDEX);
  2079. musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
  2080. for (i = 0; i < musb->config->num_eps; ++i) {
  2081. struct musb_hw_ep *hw_ep;
  2082. hw_ep = &musb->endpoints[i];
  2083. if (!hw_ep)
  2084. continue;
  2085. epio = hw_ep->regs;
  2086. if (!epio)
  2087. continue;
  2088. musb_writeb(musb_base, MUSB_INDEX, i);
  2089. musb->context.index_regs[i].txmaxp =
  2090. musb_readw(epio, MUSB_TXMAXP);
  2091. musb->context.index_regs[i].txcsr =
  2092. musb_readw(epio, MUSB_TXCSR);
  2093. musb->context.index_regs[i].rxmaxp =
  2094. musb_readw(epio, MUSB_RXMAXP);
  2095. musb->context.index_regs[i].rxcsr =
  2096. musb_readw(epio, MUSB_RXCSR);
  2097. if (musb->dyn_fifo) {
  2098. musb->context.index_regs[i].txfifoadd =
  2099. musb_read_txfifoadd(musb_base);
  2100. musb->context.index_regs[i].rxfifoadd =
  2101. musb_read_rxfifoadd(musb_base);
  2102. musb->context.index_regs[i].txfifosz =
  2103. musb_read_txfifosz(musb_base);
  2104. musb->context.index_regs[i].rxfifosz =
  2105. musb_read_rxfifosz(musb_base);
  2106. }
  2107. musb->context.index_regs[i].txtype =
  2108. musb_readb(epio, MUSB_TXTYPE);
  2109. musb->context.index_regs[i].txinterval =
  2110. musb_readb(epio, MUSB_TXINTERVAL);
  2111. musb->context.index_regs[i].rxtype =
  2112. musb_readb(epio, MUSB_RXTYPE);
  2113. musb->context.index_regs[i].rxinterval =
  2114. musb_readb(epio, MUSB_RXINTERVAL);
  2115. musb->context.index_regs[i].txfunaddr =
  2116. musb_read_txfunaddr(musb, i);
  2117. musb->context.index_regs[i].txhubaddr =
  2118. musb_read_txhubaddr(musb, i);
  2119. musb->context.index_regs[i].txhubport =
  2120. musb_read_txhubport(musb, i);
  2121. musb->context.index_regs[i].rxfunaddr =
  2122. musb_read_rxfunaddr(musb, i);
  2123. musb->context.index_regs[i].rxhubaddr =
  2124. musb_read_rxhubaddr(musb, i);
  2125. musb->context.index_regs[i].rxhubport =
  2126. musb_read_rxhubport(musb, i);
  2127. }
  2128. }
  2129. static void musb_restore_context(struct musb *musb)
  2130. {
  2131. int i;
  2132. void __iomem *musb_base = musb->mregs;
  2133. void __iomem *epio;
  2134. u8 power;
  2135. musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
  2136. musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
  2137. musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
  2138. /* Don't affect SUSPENDM/RESUME bits in POWER reg */
  2139. power = musb_readb(musb_base, MUSB_POWER);
  2140. power &= MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME;
  2141. musb->context.power &= ~(MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME);
  2142. power |= musb->context.power;
  2143. musb_writeb(musb_base, MUSB_POWER, power);
  2144. musb_writew(musb_base, MUSB_INTRTXE, musb->intrtxe);
  2145. musb_writew(musb_base, MUSB_INTRRXE, musb->intrrxe);
  2146. musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
  2147. if (musb->context.devctl & MUSB_DEVCTL_SESSION)
  2148. musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
  2149. for (i = 0; i < musb->config->num_eps; ++i) {
  2150. struct musb_hw_ep *hw_ep;
  2151. hw_ep = &musb->endpoints[i];
  2152. if (!hw_ep)
  2153. continue;
  2154. epio = hw_ep->regs;
  2155. if (!epio)
  2156. continue;
  2157. musb_writeb(musb_base, MUSB_INDEX, i);
  2158. musb_writew(epio, MUSB_TXMAXP,
  2159. musb->context.index_regs[i].txmaxp);
  2160. musb_writew(epio, MUSB_TXCSR,
  2161. musb->context.index_regs[i].txcsr);
  2162. musb_writew(epio, MUSB_RXMAXP,
  2163. musb->context.index_regs[i].rxmaxp);
  2164. musb_writew(epio, MUSB_RXCSR,
  2165. musb->context.index_regs[i].rxcsr);
  2166. if (musb->dyn_fifo) {
  2167. musb_write_txfifosz(musb_base,
  2168. musb->context.index_regs[i].txfifosz);
  2169. musb_write_rxfifosz(musb_base,
  2170. musb->context.index_regs[i].rxfifosz);
  2171. musb_write_txfifoadd(musb_base,
  2172. musb->context.index_regs[i].txfifoadd);
  2173. musb_write_rxfifoadd(musb_base,
  2174. musb->context.index_regs[i].rxfifoadd);
  2175. }
  2176. musb_writeb(epio, MUSB_TXTYPE,
  2177. musb->context.index_regs[i].txtype);
  2178. musb_writeb(epio, MUSB_TXINTERVAL,
  2179. musb->context.index_regs[i].txinterval);
  2180. musb_writeb(epio, MUSB_RXTYPE,
  2181. musb->context.index_regs[i].rxtype);
  2182. musb_writeb(epio, MUSB_RXINTERVAL,
  2183. musb->context.index_regs[i].rxinterval);
  2184. musb_write_txfunaddr(musb, i,
  2185. musb->context.index_regs[i].txfunaddr);
  2186. musb_write_txhubaddr(musb, i,
  2187. musb->context.index_regs[i].txhubaddr);
  2188. musb_write_txhubport(musb, i,
  2189. musb->context.index_regs[i].txhubport);
  2190. musb_write_rxfunaddr(musb, i,
  2191. musb->context.index_regs[i].rxfunaddr);
  2192. musb_write_rxhubaddr(musb, i,
  2193. musb->context.index_regs[i].rxhubaddr);
  2194. musb_write_rxhubport(musb, i,
  2195. musb->context.index_regs[i].rxhubport);
  2196. }
  2197. musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
  2198. }
  2199. static int musb_suspend(struct device *dev)
  2200. {
  2201. struct musb *musb = dev_to_musb(dev);
  2202. unsigned long flags;
  2203. musb_platform_disable(musb);
  2204. musb_generic_disable(musb);
  2205. spin_lock_irqsave(&musb->lock, flags);
  2206. if (is_peripheral_active(musb)) {
  2207. /* FIXME force disconnect unless we know USB will wake
  2208. * the system up quickly enough to respond ...
  2209. */
  2210. } else if (is_host_active(musb)) {
  2211. /* we know all the children are suspended; sometimes
  2212. * they will even be wakeup-enabled.
  2213. */
  2214. }
  2215. musb_save_context(musb);
  2216. spin_unlock_irqrestore(&musb->lock, flags);
  2217. return 0;
  2218. }
  2219. static int musb_resume(struct device *dev)
  2220. {
  2221. struct musb *musb = dev_to_musb(dev);
  2222. u8 devctl;
  2223. u8 mask;
  2224. /*
  2225. * For static cmos like DaVinci, register values were preserved
  2226. * unless for some reason the whole soc powered down or the USB
  2227. * module got reset through the PSC (vs just being disabled).
  2228. *
  2229. * For the DSPS glue layer though, a full register restore has to
  2230. * be done. As it shouldn't harm other platforms, we do it
  2231. * unconditionally.
  2232. */
  2233. musb_restore_context(musb);
  2234. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  2235. mask = MUSB_DEVCTL_BDEVICE | MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV;
  2236. if ((devctl & mask) != (musb->context.devctl & mask))
  2237. musb->port1_status = 0;
  2238. if (musb->need_finish_resume) {
  2239. musb->need_finish_resume = 0;
  2240. schedule_delayed_work(&musb->finish_resume_work,
  2241. msecs_to_jiffies(USB_RESUME_TIMEOUT));
  2242. }
  2243. /*
  2244. * The USB HUB code expects the device to be in RPM_ACTIVE once it came
  2245. * out of suspend
  2246. */
  2247. pm_runtime_disable(dev);
  2248. pm_runtime_set_active(dev);
  2249. pm_runtime_enable(dev);
  2250. musb_start(musb);
  2251. return 0;
  2252. }
  2253. static int musb_runtime_suspend(struct device *dev)
  2254. {
  2255. struct musb *musb = dev_to_musb(dev);
  2256. musb_save_context(musb);
  2257. return 0;
  2258. }
  2259. static int musb_runtime_resume(struct device *dev)
  2260. {
  2261. struct musb *musb = dev_to_musb(dev);
  2262. static int first = 1;
  2263. /*
  2264. * When pm_runtime_get_sync called for the first time in driver
  2265. * init, some of the structure is still not initialized which is
  2266. * used in restore function. But clock needs to be
  2267. * enabled before any register access, so
  2268. * pm_runtime_get_sync has to be called.
  2269. * Also context restore without save does not make
  2270. * any sense
  2271. */
  2272. if (!first)
  2273. musb_restore_context(musb);
  2274. first = 0;
  2275. if (musb->need_finish_resume) {
  2276. musb->need_finish_resume = 0;
  2277. schedule_delayed_work(&musb->finish_resume_work,
  2278. msecs_to_jiffies(USB_RESUME_TIMEOUT));
  2279. }
  2280. return 0;
  2281. }
  2282. static const struct dev_pm_ops musb_dev_pm_ops = {
  2283. .suspend = musb_suspend,
  2284. .resume = musb_resume,
  2285. .runtime_suspend = musb_runtime_suspend,
  2286. .runtime_resume = musb_runtime_resume,
  2287. };
  2288. #define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
  2289. #else
  2290. #define MUSB_DEV_PM_OPS NULL
  2291. #endif
  2292. static struct platform_driver musb_driver = {
  2293. .driver = {
  2294. .name = (char *)musb_driver_name,
  2295. .bus = &platform_bus_type,
  2296. .pm = MUSB_DEV_PM_OPS,
  2297. },
  2298. .probe = musb_probe,
  2299. .remove = musb_remove,
  2300. };
  2301. module_platform_driver(musb_driver);