musb_core.c 64 KB

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