musb_core.c 78 KB

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