musb_core.c 65 KB

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