musb_core.c 67 KB

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