musb_core.c 67 KB

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