musb_core.c 68 KB

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