musb_core.c 67 KB

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