musb_core.c 67 KB

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