musb_core.c 67 KB

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