musb_core.c 68 KB

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