musb_core.c 68 KB

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