musb_core.c 67 KB

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