musb_core.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980
  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. /* Sanity CR check in */
  81. /*
  82. * This gets many kinds of configuration information:
  83. * - Kconfig for everything user-configurable
  84. * - platform_device for addressing, irq, and platform_data
  85. * - platform_data is mostly for board-specific informarion
  86. * (plus recentrly, SOC or family details)
  87. *
  88. * Most of the conditional compilation will (someday) vanish.
  89. */
  90. #include <linux/module.h>
  91. #include <linux/kernel.h>
  92. #include <linux/sched.h>
  93. #include <linux/slab.h>
  94. #include <linux/init.h>
  95. #include <linux/list.h>
  96. #include <linux/kobject.h>
  97. #include <linux/prefetch.h>
  98. #include <linux/platform_device.h>
  99. #include <linux/io.h>
  100. #ifdef CONFIG_USB_C_SWITCH
  101. #include <typec.h>
  102. #endif
  103. #ifdef CONFIG_USBIF_COMPLIANCE
  104. #include <linux/proc_fs.h>
  105. #include <asm/uaccess.h>
  106. #include <linux/seq_file.h>
  107. #include <mach/system.h>
  108. #endif
  109. #include <mt-plat/mt_chip.h>
  110. #include "musb_core.h"
  111. #include "mu3d_hal_osal.h"
  112. #include "mu3d_hal_usb_drv.h"
  113. #include "mu3d_hal_hw.h"
  114. #include "ssusb_qmu.h"
  115. #ifdef CONFIG_MTK_UART_USB_SWITCH
  116. #include <linux/of.h>
  117. #include <linux/of_irq.h>
  118. #include <linux/of_address.h>
  119. #define AP_UART0_COMPATIBLE_NAME "mediatek,AP_UART0"
  120. #endif
  121. #define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
  122. #define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
  123. #define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
  124. #define MUSB_VERSION "6.0"
  125. #define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
  126. const char musb_driver_name[] = MUSB_DRIVER_NAME;
  127. struct musb *_mu3d_musb = NULL;
  128. u32 debug_level = K_ALET | K_CRIT | K_ERR | K_WARNIN | K_NOTICE | K_INFO;
  129. u32 fake_CDP = 0;
  130. module_param(debug_level, int, 0644);
  131. MODULE_PARM_DESC(debug_level, "Debug Print Log Lvl");
  132. module_param(fake_CDP, int, 0644);
  133. #ifdef EP_PROFILING
  134. u32 is_prof = 1;
  135. module_param(is_prof, int, 0644);
  136. MODULE_PARM_DESC(is_prof, "profiling each EP");
  137. #endif
  138. MODULE_DESCRIPTION(DRIVER_INFO);
  139. MODULE_AUTHOR(DRIVER_AUTHOR);
  140. MODULE_LICENSE("GPL");
  141. MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
  142. #define U3D_FIFO_START_ADDRESS 0
  143. void __iomem *u3_base;
  144. void __iomem *u3_sif_base;
  145. void __iomem *u3_sif2_base;
  146. void __iomem *ap_uart0_base;
  147. #ifdef CONFIG_MTK_FPGA
  148. void __iomem *i2c1_base;
  149. #endif
  150. /*-------------------------------------------------------------------------*/
  151. static inline struct musb *dev_to_musb(struct device *dev)
  152. {
  153. return dev_get_drvdata(dev);
  154. }
  155. /*-------------------------------------------------------------------------*/
  156. #if 0
  157. /* #ifndef CONFIG_BLACKFIN */
  158. static int musb_ulpi_read(struct otg_transceiver *otg, u32 offset)
  159. {
  160. void __iomem *addr = otg->io_priv;
  161. int i = 0;
  162. u8 r;
  163. u8 power;
  164. /* Make sure the transceiver is not in low power mode */
  165. power = musb_readb(addr, MUSB_POWER);
  166. power &= ~MUSB_POWER_SUSPENDM;
  167. musb_writeb(addr, MUSB_POWER, power);
  168. /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
  169. * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
  170. */
  171. musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8) offset);
  172. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
  173. while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
  174. & MUSB_ULPI_REG_CMPLT)) {
  175. i++;
  176. if (i == 10000)
  177. return -ETIMEDOUT;
  178. }
  179. r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
  180. r &= ~MUSB_ULPI_REG_CMPLT;
  181. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
  182. return musb_readb(addr, MUSB_ULPI_REG_DATA);
  183. }
  184. static int musb_ulpi_write(struct otg_transceiver *otg, u32 offset, u32 data)
  185. {
  186. void __iomem *addr = otg->io_priv;
  187. int i = 0;
  188. u8 r = 0;
  189. u8 power;
  190. /* Make sure the transceiver is not in low power mode */
  191. power = musb_readb(addr, MUSB_POWER);
  192. power &= ~MUSB_POWER_SUSPENDM;
  193. musb_writeb(addr, MUSB_POWER, power);
  194. musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8) offset);
  195. musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8) data);
  196. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
  197. while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
  198. & MUSB_ULPI_REG_CMPLT)) {
  199. i++;
  200. if (i == 10000)
  201. return -ETIMEDOUT;
  202. }
  203. r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
  204. r &= ~MUSB_ULPI_REG_CMPLT;
  205. musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
  206. return 0;
  207. }
  208. #else
  209. #define musb_ulpi_read NULL
  210. #define musb_ulpi_write NULL
  211. #endif
  212. /*
  213. * Load an endpoint's FIFO
  214. */
  215. void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
  216. {
  217. unsigned int residue;
  218. unsigned int temp;
  219. /* QMU GPD address --> CPU DMA address */
  220. void __iomem *fifo = (void __iomem *)(uintptr_t) USB_FIFO(hw_ep->epnum);
  221. os_printk(K_DEBUG, "%s epnum=%d, len=%d, buf=%p\n", __func__, hw_ep->epnum, len, src);
  222. residue = len;
  223. while (residue > 0) {
  224. if (residue == 1) {
  225. temp = ((*src) & 0xFF);
  226. /* os_writeb(fifo, temp); */
  227. writeb(temp, fifo);
  228. src += 1;
  229. residue -= 1;
  230. } else if (residue == 2) {
  231. temp = ((*src) & 0xFF) + (((*(src + 1)) << 8) & 0xFF00);
  232. /* os_writew(fifo, temp); */
  233. writew(temp, fifo);
  234. src += 2;
  235. residue -= 2;
  236. } else if (residue == 3) {
  237. temp = ((*src) & 0xFF) + (((*(src + 1)) << 8) & 0xFF00);
  238. /* os_writew(fifo, temp); */
  239. writew(temp, fifo);
  240. src += 2;
  241. temp = ((*src) & 0xFF);
  242. /* os_writeb(fifo, temp); */
  243. writeb(temp, fifo);
  244. src += 1;
  245. residue -= 3;
  246. } else {
  247. temp = ((*src) & 0xFF) + (((*(src + 1)) << 8) & 0xFF00) +
  248. (((*(src + 2)) << 16) & 0xFF0000) + (((*(src + 3)) << 24) & 0xFF000000);
  249. /* os_writel(fifo, temp); */
  250. writel(temp, fifo);
  251. src += 4;
  252. residue -= 4;
  253. }
  254. }
  255. }
  256. /*
  257. * Unload an endpoint's FIFO
  258. */
  259. void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
  260. {
  261. u16 residue;
  262. unsigned int temp;
  263. /* QMU GPD address --> CPU DMA address */
  264. void __iomem *fifo = (void __iomem *)(uintptr_t) USB_FIFO(hw_ep->epnum);
  265. os_printk(K_DEBUG, "%s %cX ep%d fifo %p count %d buf %p\n",
  266. __func__, 'R', hw_ep->epnum, fifo, len, dst);
  267. residue = len;
  268. while (residue > 0) {
  269. temp = os_readl(fifo);
  270. /*Store the first byte */
  271. *dst = temp & 0xFF;
  272. /*Store the 2nd byte, If have */
  273. if (residue > 1)
  274. *(dst + 1) = (temp >> 8) & 0xFF;
  275. /*Store the 3rd byte, If have */
  276. if (residue > 2)
  277. *(dst + 2) = (temp >> 16) & 0xFF;
  278. /*Store the 4th byte, If have */
  279. if (residue > 3)
  280. *(dst + 3) = (temp >> 24) & 0xFF;
  281. if (residue > 4) {
  282. dst = dst + 4;
  283. residue = residue - 4;
  284. } else {
  285. residue = 0;
  286. }
  287. }
  288. }
  289. /*-------------------------------------------------------------------------*/
  290. /* for high speed test mode; see USB 2.0 spec 7.1.20 */
  291. static const u8 musb_test_packet[53] = {
  292. /* implicit SYNC then DATA0 to start */
  293. /* JKJKJKJK x9 */
  294. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  295. /* JJKKJJKK x8 */
  296. 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
  297. /* JJJJKKKK x8 */
  298. 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
  299. /* JJJJJJJKKKKKKK x8 */
  300. 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  301. /* JJJJJJJK x8 */
  302. 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
  303. /* JKKKKKKK x10, JK */
  304. 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
  305. /* implicit CRC16 then EOP to end */
  306. };
  307. void musb_load_testpacket(struct musb *musb)
  308. {
  309. u32 maxp;
  310. maxp = musb->endpoints->max_packet_sz_tx;
  311. mu3d_hal_write_fifo(0, sizeof(musb_test_packet), (u8 *) musb_test_packet, maxp);
  312. }
  313. /*-------------------------------------------------------------------------*/
  314. /*
  315. * Handles OTG hnp timeouts, such as b_ase0_brst
  316. */
  317. void musb_otg_timer_func(unsigned long data)
  318. {
  319. struct musb *musb = (struct musb *)data;
  320. unsigned long flags;
  321. spin_lock_irqsave(&musb->lock, flags);
  322. switch (musb->xceiv->state) {
  323. case OTG_STATE_B_WAIT_ACON:
  324. dev_dbg(musb->controller, "HNP: b_wait_acon timeout; back to b_peripheral\n");
  325. musb_g_disconnect(musb);
  326. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  327. musb->is_active = 0;
  328. break;
  329. case OTG_STATE_A_SUSPEND:
  330. case OTG_STATE_A_WAIT_BCON:
  331. dev_dbg(musb->controller, "HNP: %s timeout\n",
  332. usb_otg_state_string(musb->xceiv->state));
  333. musb_platform_set_vbus(musb, 0);
  334. musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
  335. break;
  336. default:
  337. dev_dbg(musb->controller, "HNP: Unhandled mode %s\n",
  338. usb_otg_state_string(musb->xceiv->state));
  339. }
  340. musb->ignore_disconnect = 0;
  341. spin_unlock_irqrestore(&musb->lock, flags);
  342. }
  343. /*
  344. * Stops the HNP transition. Caller must take care of locking.
  345. */
  346. void musb_hnp_stop(struct musb *musb)
  347. {
  348. struct usb_hcd *hcd = musb_to_hcd(musb);
  349. u32 reg;
  350. dev_dbg(musb->controller, "HNP: stop from %s\n", usb_otg_state_string(musb->xceiv->state));
  351. switch (musb->xceiv->state) {
  352. case OTG_STATE_A_PERIPHERAL:
  353. musb_g_disconnect(musb);
  354. dev_dbg(musb->controller, "HNP: back to %s\n",
  355. usb_otg_state_string(musb->xceiv->state));
  356. break;
  357. case OTG_STATE_B_HOST:
  358. dev_dbg(musb->controller, "HNP: Disabling HR\n");
  359. hcd->self.is_b_host = 0;
  360. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  361. MUSB_DEV_MODE(musb);
  362. /* reg = musb_readb(mbase, MUSB_POWER); */
  363. reg = os_readl(U3D_POWER_MANAGEMENT);
  364. reg |= SUSPENDM_ENABLE;
  365. os_writel(U3D_POWER_MANAGEMENT, reg);
  366. /* REVISIT: Start SESSION_REQUEST here? */
  367. break;
  368. default:
  369. dev_dbg(musb->controller, "HNP: Stopping in unknown state %s\n",
  370. usb_otg_state_string(musb->xceiv->state));
  371. }
  372. /*
  373. * When returning to A state after HNP, avoid hub_port_rebounce(),
  374. * which cause occasional OPT A "Did not receive reset after connect"
  375. * errors.
  376. */
  377. musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
  378. }
  379. /*
  380. * Interrupt Service Routine to record USB "global" interrupts.
  381. * Since these do not happen often and signify things of
  382. * paramount importance, it seems OK to check them individually;
  383. * the order of the tests is specified in the manual
  384. *
  385. * @param musb instance pointer
  386. * @param int_usb register contents
  387. * @param devctl
  388. * @param power
  389. */
  390. static irqreturn_t musb_stage0_irq(struct musb *musb, u32 int_usb, u8 devctl, u8 power)
  391. {
  392. struct usb_otg *otg = musb->xceiv->otg;
  393. irqreturn_t handled = IRQ_NONE;
  394. dev_notice(musb->controller, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
  395. int_usb);
  396. /* in host mode, the peripheral may issue remote wakeup.
  397. * in peripheral mode, the host may resume the link.
  398. * spurious RESUME irqs happen too, paired with SUSPEND.
  399. */
  400. if (int_usb & RESUME_INTR) {
  401. handled = IRQ_HANDLED;
  402. dev_notice(musb->controller, "RESUME (%s)\n",
  403. usb_otg_state_string(musb->xceiv->state));
  404. /* We implement device mode only. */
  405. switch (musb->xceiv->state) {
  406. case OTG_STATE_A_SUSPEND:
  407. /* possibly DISCONNECT is upcoming */
  408. musb->xceiv->state = OTG_STATE_A_HOST;
  409. usb_hcd_resume_root_hub(musb_to_hcd(musb));
  410. break;
  411. case OTG_STATE_B_WAIT_ACON:
  412. case OTG_STATE_B_PERIPHERAL:
  413. /* disconnect while suspended? we may
  414. * not get a disconnect irq...
  415. */
  416. if ((devctl & USB_DEVCTL_VBUSVALID)
  417. != (3 << USB_DEVCTL_VBUS_OFFSET)
  418. ) {
  419. musb->int_usb |= DISCONN_INTR;
  420. musb->int_usb &= ~SUSPEND_INTR;
  421. break;
  422. }
  423. musb_g_resume(musb);
  424. break;
  425. case OTG_STATE_B_IDLE:
  426. musb->int_usb &= ~SUSPEND_INTR;
  427. break;
  428. default:
  429. WARNING("bogus %s RESUME (%s)\n",
  430. "peripheral", usb_otg_state_string(musb->xceiv->state));
  431. }
  432. }
  433. /* see manual for the order of the tests */
  434. if (int_usb & SESSION_REQ_INTR) {
  435. if ((devctl & USB_DEVCTL_VBUSMASK) == USB_DEVCTL_VBUSVALID
  436. && (devctl & USB_DEVCTL_BDEVICE)) {
  437. dev_dbg(musb->controller, "SessReq while on B state\n");
  438. return IRQ_HANDLED;
  439. }
  440. dev_notice(musb->controller, "SESSION_REQUEST (%s)\n",
  441. usb_otg_state_string(musb->xceiv->state));
  442. /* IRQ arrives from ID pin sense or (later, if VBUS power
  443. * is removed) SRP. responses are time critical:
  444. * - turn on VBUS (with silicon-specific mechanism)
  445. * - go through A_WAIT_VRISE
  446. * - ... to A_WAIT_BCON.
  447. * a_wait_vrise_tmout triggers VBUS_ERROR transitions
  448. */
  449. /* os_writel(mregs + MAC_DEVICE_CONTROL, devctl & USB_DEVCTL_SESSION); */
  450. musb->ep0_stage = MUSB_EP0_START;
  451. musb->xceiv->state = OTG_STATE_A_IDLE;
  452. MUSB_HST_MODE(musb);
  453. musb_platform_set_vbus(musb, 1);
  454. handled = IRQ_HANDLED;
  455. }
  456. if (int_usb & VBUSERR_INTR) {
  457. int ignore = 0;
  458. /* During connection as an A-Device, we may see a short
  459. * current spikes causing voltage drop, because of cable
  460. * and peripheral capacitance combined with vbus draw.
  461. * (So: less common with truly self-powered devices, where
  462. * vbus doesn't act like a power supply.)
  463. *
  464. * Such spikes are short; usually less than ~500 usec, max
  465. * of ~2 msec. That is, they're not sustained overcurrent
  466. * errors, though they're reported using VBUSERROR irqs.
  467. *
  468. * Workarounds: (a) hardware: use self powered devices.
  469. * (b) software: ignore non-repeated VBUS errors.
  470. *
  471. * REVISIT: do delays from lots of DEBUG_KERNEL checks
  472. * make trouble here, keeping VBUS < 4.4V ?
  473. */
  474. switch (musb->xceiv->state) {
  475. case OTG_STATE_A_HOST:
  476. /* recovery is dicey once we've gotten past the
  477. * initial stages of enumeration, but if VBUS
  478. * stayed ok at the other end of the link, and
  479. * another reset is due (at least for high speed,
  480. * to redo the chirp etc), it might work OK...
  481. */
  482. case OTG_STATE_A_WAIT_BCON:
  483. case OTG_STATE_A_WAIT_VRISE:
  484. if (musb->vbuserr_retry) {
  485. musb->vbuserr_retry--;
  486. ignore = 1;
  487. devctl |= USB_DEVCTL_SESSION;
  488. /* os_writel(mregs + MAC_DEVICE_CONTROL, devctl & USB_DEVCTL_SESSION); */
  489. } else {
  490. musb->port1_status |=
  491. USB_PORT_STAT_OVERCURRENT | (USB_PORT_STAT_C_OVERCURRENT << 16);
  492. }
  493. break;
  494. default:
  495. break;
  496. }
  497. dev_notice(musb->controller, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
  498. usb_otg_state_string(musb->xceiv->state), devctl, ({
  499. char *s;
  500. switch (devctl &
  501. USB_DEVCTL_VBUSMASK) {
  502. case 0 << USB_DEVCTL_VBUS_OFFSET:
  503. s = "<SessEnd"; break; case 1 << USB_DEVCTL_VBUS_OFFSET:
  504. s = "<AValid"; break; case 2 << USB_DEVCTL_VBUS_OFFSET:
  505. s = "<VBusValid";
  506. break;
  507. /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
  508. default:
  509. s = "VALID"; break; };
  510. s; }
  511. ), VBUSERR_RETRY_COUNT - musb->vbuserr_retry, musb->port1_status);
  512. /* go through A_WAIT_VFALL then start a new session */
  513. if (!ignore)
  514. musb_platform_set_vbus(musb, 0);
  515. handled = IRQ_HANDLED;
  516. }
  517. if (int_usb & SUSPEND_INTR) {
  518. dev_notice(musb->controller, "SUSPEND (%s) devctl %02x power %02x\n",
  519. usb_otg_state_string(musb->xceiv->state), devctl, power);
  520. handled = IRQ_HANDLED;
  521. switch (musb->xceiv->state) {
  522. case OTG_STATE_A_PERIPHERAL:
  523. /* We also come here if the cable is removed, since
  524. * this silicon doesn't report ID-no-longer-grounded.
  525. *
  526. * We depend on T(a_wait_bcon) to shut us down, and
  527. * hope users don't do anything dicey during this
  528. * undesired detour through A_WAIT_BCON.
  529. */
  530. musb_hnp_stop(musb);
  531. usb_hcd_resume_root_hub(musb_to_hcd(musb));
  532. /* musb_root_disconnect(musb); //I don't port virthub now. */
  533. musb_platform_try_idle(musb, jiffies
  534. + msecs_to_jiffies(musb->a_wait_bcon
  535. ? : OTG_TIME_A_WAIT_BCON));
  536. break;
  537. case OTG_STATE_B_IDLE:
  538. if (!musb->is_active)
  539. break;
  540. case OTG_STATE_B_PERIPHERAL:
  541. musb_g_suspend(musb);
  542. musb->is_active = is_otg_enabled(musb)
  543. && otg->gadget->b_hnp_enable;
  544. if (musb->is_active) {
  545. musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
  546. dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n");
  547. mod_timer(&musb->otg_timer, jiffies
  548. + msecs_to_jiffies(OTG_TIME_B_ASE0_BRST));
  549. }
  550. break;
  551. case OTG_STATE_A_WAIT_BCON:
  552. if (musb->a_wait_bcon != 0)
  553. musb_platform_try_idle(musb, jiffies
  554. + msecs_to_jiffies(musb->a_wait_bcon));
  555. break;
  556. case OTG_STATE_A_HOST:
  557. musb->xceiv->state = OTG_STATE_A_SUSPEND;
  558. musb->is_active = is_otg_enabled(musb)
  559. && otg->host->b_hnp_enable;
  560. break;
  561. case OTG_STATE_B_HOST:
  562. /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
  563. dev_dbg(musb->controller, "REVISIT: SUSPEND as B_HOST\n");
  564. break;
  565. default:
  566. /* "should not happen" */
  567. musb->is_active = 0;
  568. break;
  569. }
  570. }
  571. if (int_usb & CONN_INTR) {
  572. struct usb_hcd *hcd = musb_to_hcd(musb);
  573. u32 int_en = 0;
  574. handled = IRQ_HANDLED;
  575. musb->is_active = 1;
  576. musb->ep0_stage = MUSB_EP0_START;
  577. os_printk(K_DEBUG, "----- ep0 state: MUSB_EP0_START\n");
  578. /* flush endpoints when transitioning from Device Mode */
  579. if (is_peripheral_active(musb)) {
  580. /* REVISIT HNP; */
  581. /* just force disconnect */
  582. }
  583. /* musb_writew(musb->mregs, MUSB_INTRTXE, musb->epmask); */
  584. /* musb_writew(musb->mregs, MUSB_INTRRXE, musb->epmask & 0xfffe); */
  585. #ifdef USE_SSUSB_QMU
  586. /*Only Enable EP0 Tx interrupt */
  587. os_writel(U3D_EPIESR, os_readl(U3D_EPIESR) | EP0ISR);
  588. #else
  589. /*Enable EP0 Tx and EPn Tx/Rx interrupt */
  590. os_printk(K_DEBUG, "Enable EP0 & EPn interrupt =%x\n",
  591. musb->epmask | ((musb->epmask << 16) & EPRISR));
  592. os_writel(U3D_EPIESR, musb->epmask | ((musb->epmask << 16) & EPRISR));
  593. #endif
  594. int_en =
  595. SUSPEND_INTR_EN | RESUME_INTR_EN | RESET_INTR_EN | CONN_INTR_EN |
  596. DISCONN_INTR_EN;
  597. os_writel(U3D_COMMON_USB_INTR_ENABLE, int_en);
  598. /* musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7); */
  599. musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
  600. | USB_PORT_STAT_HIGH_SPEED | USB_PORT_STAT_ENABLE);
  601. musb->port1_status |= USB_PORT_STAT_CONNECTION | (USB_PORT_STAT_C_CONNECTION << 16);
  602. /* high vs full speed is just a guess until after reset */
  603. if (devctl & USB_DEVCTL_LS_DEV)
  604. musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
  605. /* indicate new connection to OTG machine */
  606. switch (musb->xceiv->state) {
  607. case OTG_STATE_B_PERIPHERAL:
  608. if (int_usb & SUSPEND_INTR) {
  609. dev_dbg(musb->controller, "HNP: SUSPEND+CONNECT, now b_host\n");
  610. int_usb &= ~SUSPEND_INTR;
  611. goto b_host;
  612. } else
  613. dev_dbg(musb->controller, "CONNECT as b_peripheral???\n");
  614. break;
  615. case OTG_STATE_B_WAIT_ACON:
  616. dev_dbg(musb->controller, "HNP: CONNECT, now b_host\n");
  617. b_host:
  618. musb->xceiv->state = OTG_STATE_B_HOST;
  619. hcd->self.is_b_host = 1;
  620. musb->ignore_disconnect = 0;
  621. del_timer(&musb->otg_timer);
  622. break;
  623. default:
  624. if ((devctl & USB_DEVCTL_VBUSVALID)
  625. == (3 << USB_DEVCTL_VBUS_OFFSET)) {
  626. musb->xceiv->state = OTG_STATE_A_HOST;
  627. hcd->self.is_b_host = 0;
  628. }
  629. break;
  630. }
  631. /* poke the root hub */
  632. MUSB_HST_MODE(musb);
  633. if (hcd->status_urb)
  634. usb_hcd_poll_rh_status(hcd);
  635. else
  636. usb_hcd_resume_root_hub(hcd);
  637. dev_notice(musb->controller, "CONNECT (%s) devctl %02x\n",
  638. usb_otg_state_string(musb->xceiv->state), devctl);
  639. }
  640. if ((int_usb & DISCONN_INTR) && !musb->ignore_disconnect) {
  641. dev_notice(musb->controller, "DISCONNECT (%s) as %s, devctl %02x\n",
  642. usb_otg_state_string(musb->xceiv->state), MUSB_MODE(musb), devctl);
  643. handled = IRQ_HANDLED;
  644. switch (musb->xceiv->state) {
  645. case OTG_STATE_A_HOST:
  646. case OTG_STATE_A_SUSPEND:
  647. usb_hcd_resume_root_hub(musb_to_hcd(musb));
  648. /* musb_root_disconnect(musb); */
  649. if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
  650. musb_platform_try_idle(musb, jiffies
  651. + msecs_to_jiffies(musb->a_wait_bcon));
  652. break;
  653. case OTG_STATE_B_HOST:
  654. /* REVISIT this behaves for "real disconnect"
  655. * cases; make sure the other transitions from
  656. * from B_HOST act right too. The B_HOST code
  657. * in hnp_stop() is currently not used...
  658. */
  659. /* musb_root_disconnect(musb); //I don't port virthub now. */
  660. musb_to_hcd(musb)->self.is_b_host = 0;
  661. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  662. MUSB_DEV_MODE(musb);
  663. musb_g_disconnect(musb);
  664. break;
  665. case OTG_STATE_A_PERIPHERAL:
  666. musb_hnp_stop(musb);
  667. /* musb_root_disconnect(musb); //I don't port virthub now. */
  668. /* FALLTHROUGH */
  669. case OTG_STATE_B_WAIT_ACON:
  670. /* FALLTHROUGH */
  671. case OTG_STATE_B_PERIPHERAL:
  672. case OTG_STATE_B_IDLE:
  673. musb_g_disconnect(musb);
  674. break;
  675. default:
  676. WARNING("unhandled DISCONNECT transition (%s)\n",
  677. usb_otg_state_string(musb->xceiv->state));
  678. break;
  679. }
  680. }
  681. /* mentor saves a bit: bus reset and babble share the same irq.
  682. * only host sees babble; only peripheral sees bus reset.
  683. */
  684. if (int_usb & RESET_INTR) {
  685. handled = IRQ_HANDLED;
  686. mu3d_hal_pdn_ip_port(1, 0, 1, 1);
  687. if (1) { /* device mode */
  688. dev_notice(musb->controller, "BUS RESET as %s\n",
  689. usb_otg_state_string(musb->xceiv->state));
  690. os_printk(K_DEBUG, "BUS RESET\n");
  691. switch (musb->xceiv->state) {
  692. case OTG_STATE_A_SUSPEND:
  693. /* We need to ignore disconnect on suspend
  694. * otherwise tusb 2.0 won't reconnect after a
  695. * power cycle, which breaks otg compliance.
  696. */
  697. musb->ignore_disconnect = 1;
  698. musb_g_reset(musb);
  699. /* FALLTHROUGH */
  700. case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
  701. /* never use invalid T(a_wait_bcon) */
  702. dev_dbg(musb->controller, "HNP: in %s, %d msec timeout\n",
  703. usb_otg_state_string(musb->xceiv->state),
  704. TA_WAIT_BCON(musb));
  705. mod_timer(&musb->otg_timer, jiffies
  706. + msecs_to_jiffies(TA_WAIT_BCON(musb)));
  707. break;
  708. case OTG_STATE_A_PERIPHERAL:
  709. musb->ignore_disconnect = 0;
  710. del_timer(&musb->otg_timer);
  711. musb_g_reset(musb);
  712. break;
  713. case OTG_STATE_B_WAIT_ACON:
  714. dev_dbg(musb->controller, "HNP: RESET (%s), to b_peripheral\n",
  715. usb_otg_state_string(musb->xceiv->state));
  716. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  717. musb_g_reset(musb);
  718. break;
  719. case OTG_STATE_B_IDLE:
  720. musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
  721. /* FALLTHROUGH */
  722. case OTG_STATE_B_PERIPHERAL:
  723. musb_g_reset(musb);
  724. break;
  725. default:
  726. dev_dbg(musb->controller, "Unhandled BUS RESET as %s\n",
  727. usb_otg_state_string(musb->xceiv->state));
  728. }
  729. }
  730. }
  731. schedule_work(&musb->irq_work);
  732. return handled;
  733. }
  734. #ifdef EP_PROFILING
  735. #define POLL_INTERVAL 10
  736. unsigned int ep_prof[8][2];
  737. static void ep_prof_work(struct work_struct *data)
  738. {
  739. struct musb *musb = container_of(to_delayed_work(data), struct musb, ep_prof_work);
  740. int i;
  741. int tx = 0;
  742. int rx = 0;
  743. bool is_print = false;
  744. for (i = 1; i < 9; i++) {
  745. if ((ep_prof[i - 1][0] != 0) || (ep_prof[i - 1][1] != 0)) {
  746. os_printk(K_INFO, "[%d]T%d,R%d", i, ep_prof[i - 1][0], ep_prof[i - 1][1]);
  747. tx += ep_prof[i - 1][0];
  748. rx += ep_prof[i - 1][1];
  749. is_print = true;
  750. }
  751. ep_prof[i - 1][0] = ep_prof[i - 1][1] = 0;
  752. }
  753. if (is_print)
  754. os_printk(K_INFO, "T%d,R%d\n", tx, rx);
  755. schedule_delayed_work(&musb->ep_prof_work, msecs_to_jiffies(POLL_INTERVAL * 1000));
  756. }
  757. #endif
  758. static void musb_restore_context(struct musb *musb);
  759. static void musb_save_context(struct musb *musb);
  760. /*-------------------------------------------------------------------------*/
  761. /*
  762. * Program the HDRC to start (enable interrupts, dma, etc.).
  763. */
  764. void musb_start(struct musb *musb)
  765. {
  766. u8 devctl = (u8) os_readl(U3D_DEVICE_CONTROL);
  767. dev_dbg(musb->controller, "<== devctl %02x\n", devctl);
  768. os_printk(K_INFO, "%s\n", __func__);
  769. if (musb->is_clk_on == 0) {
  770. #ifndef CONFIG_MTK_FPGA
  771. /* Recovert PHY. And turn on CLK. */
  772. usb_phy_recover(musb->is_clk_on);
  773. musb->is_clk_on = 1;
  774. /* USB 2.0 slew rate calibration */
  775. u3phy_ops->u2_slew_rate_calibration(u3phy);
  776. #endif
  777. /* disable IP reset and power down, disable U2/U3 ip power down */
  778. _ex_mu3d_hal_ssusb_en();
  779. /* USB PLL Force settings */
  780. #ifdef CONFIG_PROJECT_PHY
  781. usb20_pll_settings(false, false);
  782. #endif
  783. /* reset U3D all dev module. */
  784. mu3d_hal_rst_dev();
  785. /*
  786. * SW workaround of SSUSB device mode fake disable interrupt
  787. * 1. Clear SSUSB_U3_PORT_DIS @ _ex_mu3d_hal_ssusb_en()
  788. * 2. Wait SSUSB_U3_MAC_RST_B_STS change to 1. @ mu3d_hal_check_clk_sts()
  789. * 3. Delay 50us
  790. * 4. Clear U3 interrupt @ mu3d_hal_check_clk_sts()
  791. * Recommended value : 50us
  792. */
  793. udelay(20);
  794. musb_restore_context(musb);
  795. }
  796. /*Enable Level 1 interrupt (BMU, QMU, MAC3, DMA, MAC2, EPCTL) */
  797. os_writel(U3D_LV1IESR, 0xFFFFFFFF);
  798. /* Initialize the default interrupts */
  799. _ex_mu3d_hal_system_intr_en();
  800. #ifdef USB_GADGET_SUPERSPEED
  801. /* HS/FS detected by HW */
  802. /* USB2.0 controller will negotiate for HS mode when the device is reset by the host */
  803. os_writel(U3D_POWER_MANAGEMENT, (os_readl(U3D_POWER_MANAGEMENT) | HS_ENABLE));
  804. /* set LPM remote wake up enable by HW */
  805. os_writel(U3D_POWER_MANAGEMENT, (os_readl(U3D_POWER_MANAGEMENT) | LPM_HRWE));
  806. os_writel(U3D_USB2_EPCTL_LPM, (L1_EXIT_EP0_CHK | L1_EXIT_EP_IN_CHK | L1_EXIT_EP_OUT_CHK));
  807. os_writel(U3D_USB2_EPCTL_LPM_FC_CHK,
  808. (L1_EXIT_EP0_FC_CHK | L1_EXIT_EP_IN_FC_CHK | L1_EXIT_EP_OUT_FC_CHK));
  809. #ifdef CONFIG_USBIF_COMPLIANCE
  810. /* Accept LGO_U1/U2 at beginning */
  811. os_writel(U3D_LINK_POWER_CONTROL,
  812. os_readl(U3D_LINK_POWER_CONTROL) | SW_U1_ACCEPT_ENABLE | SW_U2_ACCEPT_ENABLE);
  813. /* 3us timeout for PENDING HP */
  814. os_writel(U3D_LINK_HP_TIMER, (os_readl(U3D_LINK_HP_TIMER) & ~(PHP_TIMEOUT_VALUE)) | 0x6);
  815. /* set vbus force enable */
  816. os_setmsk(U3D_MISC_CTRL, (VBUS_FRC_EN | VBUS_ON));
  817. #endif
  818. /* device responses to u3_exit from host automatically */
  819. os_writel(U3D_LTSSM_CTRL, os_readl(U3D_LTSSM_CTRL) & ~SOFT_U3_EXIT_EN);
  820. #else
  821. #ifdef USB_GADGET_DUALSPEED
  822. /* HS/FS detected by HW */
  823. os_writel(U3D_POWER_MANAGEMENT, os_readl(U3D_POWER_MANAGEMENT) | HS_ENABLE);
  824. #else
  825. /* FS only */
  826. os_writel(U3D_POWER_MANAGEMENT, os_readl(U3D_POWER_MANAGEMENT) & ~HS_ENABLE);
  827. #endif
  828. /* disable U3 port */
  829. mu3d_hal_u3dev_dis();
  830. #endif
  831. #ifndef CONFIG_MTK_FPGA
  832. /*if (mt_get_chip_hw_code() == 0x6595) */
  833. {
  834. os_printk(K_INFO, "%s Set Clock to 62.4MHz+\n", __func__);
  835. /* sys_ck = OSC 124.8MHz/2 = 62.4MHz */
  836. os_setmsk(U3D_SSUSB_SYS_CK_CTRL, SSUSB_SYS_CK_DIV2_EN);
  837. /* U2 MAC sys_ck = ceil(62.4) = 63 */
  838. os_writelmsk(U3D_USB20_TIMING_PARAMETER, 63, TIME_VALUE_1US);
  839. #ifdef SUPPORT_U3
  840. /* U3 MAC sys_ck = ceil(62.4) = 63 */
  841. os_writelmsk(U3D_TIMING_PULSE_CTRL, 63, CNT_1US_VALUE);
  842. #endif
  843. os_printk(K_INFO, "%s Set Clock to 62.4MHz-\n", __func__);
  844. }
  845. #endif
  846. os_writel(U3D_LINK_RESET_INFO, os_readl(U3D_LINK_RESET_INFO) & ~WTCHRP);
  847. /* U2/U3 detected by HW */
  848. os_writel(U3D_DEVICE_CONF, 0);
  849. musb->is_active = 1;
  850. musb_platform_enable(musb);
  851. #ifdef EP_PROFILING
  852. if (is_prof != 0)
  853. schedule_delayed_work(&musb->ep_prof_work, msecs_to_jiffies(POLL_INTERVAL * 1000));
  854. #endif
  855. if (musb->softconnect)
  856. mu3d_hal_u3dev_en();
  857. }
  858. static void musb_generic_disable(void)
  859. {
  860. /*Disable interrupts */
  861. mu3d_hal_initr_dis();
  862. /*Clear all interrupt status */
  863. mu3d_hal_clear_intr();
  864. }
  865. /*
  866. * 1. Disable U2 & U3 function
  867. * 2. Notify disconnect event to upper
  868. */
  869. static void gadget_stop(struct musb *musb)
  870. {
  871. /* Disable U2 detect */
  872. mu3d_hal_u3dev_dis();
  873. mu3d_hal_u2dev_disconn();
  874. /* notify gadget driver */
  875. if (musb->g.speed != USB_SPEED_UNKNOWN) {
  876. if (musb->gadget_driver && musb->gadget_driver->disconnect)
  877. musb->gadget_driver->disconnect(&musb->g);
  878. musb->g.speed = USB_SPEED_UNKNOWN;
  879. }
  880. }
  881. /*
  882. * Make the HDRC stop (disable interrupts, etc.);
  883. * reversible by musb_start
  884. * called on gadget driver unregister
  885. * with controller locked, irqs blocked
  886. * acts as a NOP unless some role activated the hardware
  887. */
  888. void musb_stop(struct musb *musb)
  889. {
  890. os_printk(K_INFO, "musb_stop\n");
  891. /* stop IRQs, timers, ... */
  892. musb_platform_disable(musb);
  893. musb_generic_disable();
  894. /*Added by M */
  895. gadget_stop(musb);
  896. musb->is_active = 0;
  897. /*Added by M */
  898. #ifndef CONFIG_USBIF_COMPLIANCE
  899. cancel_delayed_work_sync(&musb->check_ltssm_work);
  900. #endif
  901. dev_dbg(musb->controller, "HDRC disabled\n");
  902. if (musb->active_ep == 0)
  903. schedule_work(&musb->suspend_work);
  904. /* Move to suspend work queue */
  905. #ifdef NEVER
  906. /*
  907. * Note: When reset the SSUSB IP, All MAC regs can _NOT_ be accessed and be reset to the default value.
  908. * So save the MUST-SAVED reg in the context structure before set SSUSB_IP_SW_RST.
  909. */
  910. musb_save_context(musb);
  911. /* Set SSUSB_IP_SW_RST to avoid power leakage */
  912. #ifdef CONFIG_MTK_UART_USB_SWITCH
  913. if (!in_uart_mode)
  914. os_setmsk(U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
  915. #else
  916. os_setmsk(U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
  917. #endif
  918. #ifndef CONFIG_MTK_FPGA
  919. /* Let PHY enter savecurrent mode. And turn off CLK. */
  920. usb_phy_savecurrent(musb->is_clk_on);
  921. musb->is_clk_on = 0;
  922. #endif
  923. #endif /* NEVER */
  924. /* FIXME
  925. * - mark host and/or peripheral drivers unusable/inactive
  926. * - disable DMA (and enable it in HdrcStart)
  927. * - make sure we can musb_start() after musb_stop(); with
  928. * OTG mode, gadget driver module rmmod/modprobe cycles that
  929. * - ...
  930. */
  931. musb_platform_try_idle(musb, 0);
  932. }
  933. static void musb_shutdown(struct platform_device *pdev)
  934. {
  935. struct musb *musb = dev_to_musb(&pdev->dev);
  936. unsigned long flags;
  937. pm_runtime_get_sync(musb->controller);
  938. spin_lock_irqsave(&musb->lock, flags);
  939. musb_platform_disable(musb);
  940. musb_generic_disable();
  941. spin_unlock_irqrestore(&musb->lock, flags);
  942. #ifndef CONFIG_USBIF_COMPLIANCE
  943. if (!is_otg_enabled(musb) && is_host_enabled(musb))
  944. usb_remove_hcd(musb_to_hcd(musb));
  945. #endif
  946. os_writel(U3D_DEVICE_CONTROL, 0);
  947. musb_platform_exit(musb);
  948. pm_runtime_put(musb->controller);
  949. /* FIXME power down */
  950. }
  951. /*-------------------------------------------------------------------------*/
  952. /*
  953. * The silicon either has hard-wired endpoint configurations, or else
  954. * "dynamic fifo" sizing. The driver has support for both, though at this
  955. * writing only the dynamic sizing is very well tested. Since we switched
  956. * away from compile-time hardware parameters, we can no longer rely on
  957. * dead code elimination to leave only the relevant one in the object file.
  958. *
  959. * We don't currently use dynamic fifo setup capability to do anything
  960. * more than selecting one of a bunch of predefined configurations.
  961. */
  962. #if defined(CONFIG_USB_MUSB_TUSB6010) \
  963. || defined(CONFIG_USB_MUSB_TUSB6010_MODULE) \
  964. || defined(CONFIG_USB_MUSB_OMAP2PLUS) \
  965. || defined(CONFIG_USB_MUSB_OMAP2PLUS_MODULE) \
  966. || defined(CONFIG_USB_MUSB_AM35X) \
  967. || defined(CONFIG_USB_MUSB_AM35X_MODULE)
  968. #ifdef CONFIG_USBIF_COMPLIANCE
  969. static ushort fifo_mode = 4;
  970. #else
  971. static ushort __initdata fifo_mode = 4;
  972. #endif
  973. #elif defined(CONFIG_USB_MUSB_UX500) \
  974. || defined(CONFIG_USB_MUSB_UX500_MODULE)
  975. #ifdef CONFIG_USBIF_COMPLIANCE
  976. static ushort fifo_mode = 5;
  977. #else
  978. static ushort __initdata fifo_mode = 5;
  979. #endif
  980. #else
  981. #ifdef CONFIG_USBIF_COMPLIANCE
  982. static ushort fifo_mode = 2;
  983. #else
  984. static ushort __initdata fifo_mode = 2;
  985. #endif
  986. #endif
  987. /* "modprobe ... fifo_mode=1" etc */
  988. module_param(fifo_mode, ushort, 0);
  989. MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
  990. /*
  991. * tables defining fifo_mode values. define more if you like.
  992. * for host side, make sure both halves of ep1 are set up.
  993. */
  994. /* mode 0 - fits in 2KB */
  995. #ifdef CONFIG_USBIF_COMPLIANCE
  996. static struct musb_fifo_cfg mode_0_cfg[] = {
  997. #else
  998. static struct musb_fifo_cfg mode_0_cfg[] __initdata = {
  999. #endif
  1000. {.hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512,},
  1001. {.hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512,},
  1002. {.hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512,},
  1003. {.hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256,},
  1004. {.hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256,},
  1005. };
  1006. /* mode 1 - fits in 4KB */
  1007. #ifdef CONFIG_USBIF_COMPLIANCE
  1008. static struct musb_fifo_cfg mode_1_cfg[] = {
  1009. #else
  1010. static struct musb_fifo_cfg mode_1_cfg[] __initdata = {
  1011. #endif
  1012. {.hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE,},
  1013. {.hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE,},
  1014. {.hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE,},
  1015. {.hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256,},
  1016. {.hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256,},
  1017. };
  1018. /* mode 2 - fits in 4KB */
  1019. #ifdef CONFIG_USBIF_COMPLIANCE
  1020. static struct musb_fifo_cfg mode_2_cfg[] = {
  1021. #else
  1022. static struct musb_fifo_cfg mode_2_cfg[] __initdata = {
  1023. #endif
  1024. {.hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512,},
  1025. {.hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512,},
  1026. {.hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512,},
  1027. {.hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512,},
  1028. {.hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256,},
  1029. {.hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256,},
  1030. };
  1031. /* mode 3 - fits in 4KB */
  1032. #ifdef CONFIG_USBIF_COMPLIANCE
  1033. static struct musb_fifo_cfg mode_3_cfg[] = {
  1034. #else
  1035. static struct musb_fifo_cfg mode_3_cfg[] __initdata = {
  1036. #endif
  1037. {.hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE,},
  1038. {.hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE,},
  1039. {.hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512,},
  1040. {.hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512,},
  1041. {.hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256,},
  1042. {.hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256,},
  1043. };
  1044. /* mode 4 - fits in 16KB */
  1045. #ifdef CONFIG_USBIF_COMPLIANCE
  1046. static struct musb_fifo_cfg mode_4_cfg[] = {
  1047. #else
  1048. static struct musb_fifo_cfg mode_4_cfg[] __initdata = {
  1049. #endif
  1050. {.hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512,},
  1051. {.hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512,},
  1052. {.hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512,},
  1053. {.hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512,},
  1054. {.hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512,},
  1055. {.hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512,},
  1056. {.hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512,},
  1057. {.hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512,},
  1058. {.hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512,},
  1059. {.hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512,},
  1060. {.hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512,},
  1061. {.hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512,},
  1062. {.hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512,},
  1063. {.hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512,},
  1064. {.hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512,},
  1065. {.hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512,},
  1066. {.hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512,},
  1067. {.hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512,},
  1068. {.hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256,},
  1069. {.hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64,},
  1070. {.hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256,},
  1071. {.hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64,},
  1072. {.hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256,},
  1073. {.hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64,},
  1074. {.hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096,},
  1075. {.hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024,},
  1076. {.hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024,},
  1077. };
  1078. /* mode 5 - fits in 8KB */
  1079. #ifdef CONFIG_USBIF_COMPLIANCE
  1080. static struct musb_fifo_cfg mode_5_cfg[] = {
  1081. #else
  1082. static struct musb_fifo_cfg mode_5_cfg[] __initdata = {
  1083. #endif
  1084. {.hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512,},
  1085. {.hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512,},
  1086. {.hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512,},
  1087. {.hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512,},
  1088. {.hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512,},
  1089. {.hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512,},
  1090. {.hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512,},
  1091. {.hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512,},
  1092. {.hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512,},
  1093. {.hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512,},
  1094. {.hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32,},
  1095. {.hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32,},
  1096. {.hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32,},
  1097. {.hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32,},
  1098. {.hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32,},
  1099. {.hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32,},
  1100. {.hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32,},
  1101. {.hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32,},
  1102. {.hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32,},
  1103. {.hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32,},
  1104. {.hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32,},
  1105. {.hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32,},
  1106. {.hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32,},
  1107. {.hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32,},
  1108. {.hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512,},
  1109. {.hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024,},
  1110. {.hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024,},
  1111. };
  1112. void ep0_setup(struct musb *musb, struct musb_hw_ep *hw_ep0, const struct musb_fifo_cfg *cfg)
  1113. {
  1114. os_printk(K_INFO, "ep0_setup maxpacket: %d\n", cfg->maxpacket);
  1115. hw_ep0->fifoaddr_rx = 0;
  1116. hw_ep0->fifoaddr_tx = 0;
  1117. hw_ep0->is_shared_fifo = true;
  1118. hw_ep0->fifo = (void __iomem *)(uintptr_t) MUSB_FIFO_OFFSET(0); /* QMU GPD address --> CPU DMA address */
  1119. /* for U2 */
  1120. hw_ep0->max_packet_sz_tx = cfg->maxpacket;
  1121. hw_ep0->max_packet_sz_rx = cfg->maxpacket;
  1122. /* Defines the maximum amount of data that can be transferred through EP0 in a single operation. */
  1123. os_writelmskumsk(U3D_EP0CSR, hw_ep0->max_packet_sz_tx, EP0_MAXPKTSZ0, EP0_W1C_BITS);
  1124. /* Enable EP0 interrupt */
  1125. os_writel(U3D_EPIESR, os_readl(U3D_EPIESR) | EP0ISR);
  1126. }
  1127. /*
  1128. * configure a fifo; for non-shared endpoints, this may be called
  1129. * once for a tx fifo and once for an rx fifo.
  1130. *
  1131. * returns negative errno or offset for next fifo.
  1132. */
  1133. #ifdef CONFIG_USBIF_COMPLIANCE
  1134. static int fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep, const struct musb_fifo_cfg *cfg,
  1135. u16 offset)
  1136. #else
  1137. static int __init
  1138. fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep, const struct musb_fifo_cfg *cfg, u16 offset)
  1139. #endif
  1140. {
  1141. u16 maxpacket = cfg->maxpacket;
  1142. /* u16 c_off = offset >> 3; */
  1143. u16 ret_offset = 0;
  1144. u32 maxpreg = 0;
  1145. u8 mult = 0;
  1146. /* calculate mult. added for ssusb. */
  1147. if (maxpacket > 1024) {
  1148. maxpreg = 1024;
  1149. mult = (maxpacket / 1024) - 1;
  1150. } else {
  1151. maxpreg = maxpacket;
  1152. /* set EP0 TX/RX slot to 3 by default */
  1153. /* REVISIT-J: WHY? CHECK! */
  1154. /* if (hw_ep->epnum == 1) */
  1155. /* mult = 3; */
  1156. }
  1157. /*REVISIT-J: WHY? CHECK! EP1 as BULK EP */
  1158. /* EP0 reserved endpoint for control, bidirectional;
  1159. * EP1 reserved for bulk, two unidirection halves.
  1160. */
  1161. if (hw_ep->epnum == 1)
  1162. musb->bulk_ep = hw_ep;
  1163. /* REVISIT error check: be sure ep0 can both rx and tx ... */
  1164. if ((cfg->style == FIFO_TX) || (cfg->style == FIFO_RXTX)) {
  1165. hw_ep->max_packet_sz_tx = maxpreg;
  1166. hw_ep->mult_tx = mult;
  1167. hw_ep->fifoaddr_tx = musb->txfifoadd_offset;
  1168. if (maxpacket == 1023)
  1169. musb->txfifoadd_offset += (1024 * (hw_ep->mult_tx + 1));
  1170. else
  1171. musb->txfifoadd_offset += (maxpacket * (hw_ep->mult_tx + 1));
  1172. ret_offset = musb->txfifoadd_offset;
  1173. }
  1174. if ((cfg->style == FIFO_RX) || (cfg->style == FIFO_RXTX)) {
  1175. hw_ep->max_packet_sz_rx = maxpreg;
  1176. hw_ep->mult_rx = mult;
  1177. hw_ep->fifoaddr_rx = musb->rxfifoadd_offset;
  1178. if (maxpacket == 1023)
  1179. musb->rxfifoadd_offset += (1024 * (hw_ep->mult_rx + 1));
  1180. else
  1181. musb->rxfifoadd_offset += (maxpacket * (hw_ep->mult_rx + 1));
  1182. ret_offset = musb->rxfifoadd_offset;
  1183. }
  1184. /* NOTE rx and tx endpoint irqs aren't managed separately,
  1185. * which happens to be ok
  1186. */
  1187. musb->epmask |= (1 << hw_ep->epnum);
  1188. return ret_offset;
  1189. }
  1190. struct musb_fifo_cfg ep0_cfg_u3 = {
  1191. .style = FIFO_RXTX, .maxpacket = 512,
  1192. };
  1193. struct musb_fifo_cfg ep0_cfg_u2 = {
  1194. .style = FIFO_RXTX, .maxpacket = 64,
  1195. };
  1196. #ifdef CONFIG_USBIF_COMPLIANCE
  1197. static int ep_config_from_table(struct musb *musb)
  1198. #else
  1199. static int __init ep_config_from_table(struct musb *musb)
  1200. #endif
  1201. {
  1202. const struct musb_fifo_cfg *cfg;
  1203. unsigned i, n;
  1204. int offset = 0;
  1205. struct musb_hw_ep *hw_ep = musb->endpoints;
  1206. if (musb->config->fifo_cfg) {
  1207. cfg = musb->config->fifo_cfg;
  1208. n = musb->config->fifo_cfg_size;
  1209. os_printk(K_DEBUG, "%s: usb pre-cfg fifo_mode cfg=%p sz=%d\n", musb_driver_name,
  1210. cfg, n);
  1211. goto done;
  1212. }
  1213. switch (fifo_mode) {
  1214. default:
  1215. fifo_mode = 0;
  1216. /* FALLTHROUGH */
  1217. case 0:
  1218. cfg = mode_0_cfg;
  1219. n = ARRAY_SIZE(mode_0_cfg);
  1220. break;
  1221. case 1:
  1222. cfg = mode_1_cfg;
  1223. n = ARRAY_SIZE(mode_1_cfg);
  1224. break;
  1225. case 2:
  1226. cfg = mode_2_cfg;
  1227. n = ARRAY_SIZE(mode_2_cfg);
  1228. break;
  1229. case 3:
  1230. cfg = mode_3_cfg;
  1231. n = ARRAY_SIZE(mode_3_cfg);
  1232. break;
  1233. case 4:
  1234. cfg = mode_4_cfg;
  1235. n = ARRAY_SIZE(mode_4_cfg);
  1236. break;
  1237. case 5:
  1238. cfg = mode_5_cfg;
  1239. n = ARRAY_SIZE(mode_5_cfg);
  1240. break;
  1241. }
  1242. os_printk(K_INFO, "%s: setup fifo_mode %d\n", musb_driver_name, fifo_mode);
  1243. done:
  1244. #ifdef USB_GADGET_SUPERSPEED /* SS */
  1245. /* use SS EP0 as default; it may be changed later */
  1246. os_printk(K_INFO, "%s ep_config_from_table ep0_cfg_u3\n", __func__);
  1247. ep0_setup(musb, hw_ep, &ep0_cfg_u3);
  1248. #else /* HS, FS */
  1249. os_printk(K_INFO, "%s ep_config_from_table ep0_cfg_u2\n", __func__);
  1250. ep0_setup(musb, hw_ep, &ep0_cfg_u2);
  1251. #endif
  1252. /* assert(offset > 0) */
  1253. /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
  1254. * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
  1255. */
  1256. for (i = 0; i < n; i++) {
  1257. u8 epn = cfg->hw_ep_num;
  1258. if (epn >= musb->config->num_eps) {
  1259. os_printk(K_ERR, "%s: invalid ep %d\n", musb_driver_name, epn);
  1260. return -EINVAL;
  1261. }
  1262. offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
  1263. if (offset < 0) {
  1264. os_printk(K_ERR, "%s: mem overrun, ep %d\n", musb_driver_name, epn);
  1265. return -EINVAL;
  1266. }
  1267. epn++;
  1268. musb->nr_endpoints = max(epn, musb->nr_endpoints);
  1269. }
  1270. os_printk(K_INFO, "%s: %d/%d max ep, %d/%d memory\n",
  1271. musb_driver_name,
  1272. n + 1, musb->config->num_eps * 2 - 1,
  1273. offset, (1 << (musb->config->ram_bits + 2)));
  1274. if (!musb->bulk_ep) {
  1275. pr_debug("%s: missing bulk\n", musb_driver_name);
  1276. return -EINVAL;
  1277. }
  1278. return 0;
  1279. }
  1280. /*
  1281. * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
  1282. * @param musb the controller
  1283. */
  1284. static int ep_config_from_hw(struct musb *musb)
  1285. {
  1286. u8 epnum = 0;
  1287. struct musb_hw_ep *hw_ep;
  1288. void __iomem *mbase = musb->mregs;
  1289. int ret = 0;
  1290. dev_dbg(musb->controller, "<== static silicon ep config\n");
  1291. /* FIXME pick up ep0 maxpacket size */
  1292. for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
  1293. musb_ep_select(mbase, epnum);
  1294. hw_ep = musb->endpoints + epnum;
  1295. ret = musb_read_fifosize(musb, hw_ep, epnum);
  1296. if (ret < 0)
  1297. break;
  1298. /* FIXME set up hw_ep->{rx,tx}_double_buffered */
  1299. /* pick an RX/TX endpoint for bulk */
  1300. if (hw_ep->max_packet_sz_tx < 512 || hw_ep->max_packet_sz_rx < 512)
  1301. continue;
  1302. /* REVISIT: this algorithm is lazy, we should at least
  1303. * try to pick a double buffered endpoint.
  1304. */
  1305. if (musb->bulk_ep)
  1306. continue;
  1307. musb->bulk_ep = hw_ep;
  1308. }
  1309. if (!musb->bulk_ep) {
  1310. pr_debug("%s: missing bulk\n", musb_driver_name);
  1311. return -EINVAL;
  1312. }
  1313. return 0;
  1314. }
  1315. enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
  1316. /* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
  1317. * configure endpoints, or take their config from silicon
  1318. */
  1319. #ifdef CONFIG_USBIF_COMPLIANCE
  1320. static int musb_core_init(u16 musb_type, struct musb *musb)
  1321. #else
  1322. static int __init musb_core_init(u16 musb_type, struct musb *musb)
  1323. #endif
  1324. {
  1325. /* u8 reg; */
  1326. /* char *type; */
  1327. /* char aInfo[90], aRevision[32], aDate[12]; */
  1328. void __iomem *mbase = musb->mregs;
  1329. int status = 0;
  1330. int i;
  1331. /*TODO: We can change musb_read_hwvers() api to mtu3d version! */
  1332. #ifdef NEVER
  1333. /* log core options (read using indexed model) */
  1334. reg = musb_read_configdata(mbase);
  1335. strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
  1336. if (reg & MUSB_CONFIGDATA_DYNFIFO) {
  1337. strcat(aInfo, ", dyn FIFOs");
  1338. musb->dyn_fifo = true;
  1339. }
  1340. if (reg & MUSB_CONFIGDATA_MPRXE) {
  1341. strcat(aInfo, ", bulk combine");
  1342. musb->bulk_combine = true;
  1343. }
  1344. if (reg & MUSB_CONFIGDATA_MPTXE) {
  1345. strcat(aInfo, ", bulk split");
  1346. musb->bulk_split = true;
  1347. }
  1348. if (reg & MUSB_CONFIGDATA_HBRXE) {
  1349. strcat(aInfo, ", HB-ISO Rx");
  1350. musb->hb_iso_rx = true;
  1351. }
  1352. if (reg & MUSB_CONFIGDATA_HBTXE) {
  1353. strcat(aInfo, ", HB-ISO Tx");
  1354. musb->hb_iso_tx = true;
  1355. }
  1356. if (reg & MUSB_CONFIGDATA_SOFTCONE)
  1357. strcat(aInfo, ", SoftConn");
  1358. pr_debug("%s: ConfigData=0x%02x (%s)\n", musb_driver_name, reg, aInfo);
  1359. aDate[0] = 0;
  1360. if (MUSB_CONTROLLER_MHDRC == musb_type) {
  1361. musb->is_multipoint = 1;
  1362. type = "M";
  1363. } else {
  1364. musb->is_multipoint = 0;
  1365. type = "";
  1366. #ifndef CONFIG_USB_OTG_BLACKLIST_HUB
  1367. pr_err("%s: kernel must blacklist external hubs\n", musb_driver_name);
  1368. #endif
  1369. }
  1370. /* log release info */
  1371. musb->hwvers = musb_read_hwvers(mbase);
  1372. snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
  1373. MUSB_HWVERS_MINOR(musb->hwvers), (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
  1374. pr_debug("%s: %sHDRC RTL version %s %s\n", musb_driver_name, type, aRevision, aDate);
  1375. #endif /* NEVER */
  1376. musb->hwvers = os_readl(U3D_SSUSB_HW_ID);
  1377. os_printk(K_INFO, "%s: HDC version %d\n", musb_driver_name, musb->hwvers);
  1378. /* add for U3D */
  1379. musb->txfifoadd_offset = U3D_FIFO_START_ADDRESS;
  1380. musb->rxfifoadd_offset = U3D_FIFO_START_ADDRESS;
  1381. os_printk(K_INFO, "%s EPnFIFOSz Tx=%x, Rx=%x\n", __func__, os_readl(U3D_CAP_EPNTXFFSZ),
  1382. os_readl(U3D_CAP_EPNRXFFSZ));
  1383. os_printk(K_INFO, "%s EPnNum Tx=%x, Rx=%d\n", __func__, os_readl(U3D_CAP_EPINFO) & 0x1F,
  1384. (os_readl(U3D_CAP_EPINFO) >> 8) & 0x1F);
  1385. if (os_readl(U3D_CAP_EPNTXFFSZ) && os_readl(U3D_CAP_EPNRXFFSZ))
  1386. musb->dyn_fifo = true;
  1387. else
  1388. #ifdef CONFIG_MTK_UART_USB_SWITCH
  1389. musb->dyn_fifo = true;
  1390. #else
  1391. musb->dyn_fifo = false;
  1392. #endif
  1393. /* discover endpoint configuration */
  1394. musb->nr_endpoints = 1;
  1395. musb->epmask = 1;
  1396. /* status = ep_config_from_table(musb); */
  1397. if (musb->dyn_fifo)
  1398. status = ep_config_from_table(musb);
  1399. else
  1400. status = ep_config_from_hw(musb);
  1401. if (status < 0)
  1402. return status;
  1403. /* finish init, and print endpoint config */
  1404. for (i = 0; i < musb->nr_endpoints; i++) {
  1405. struct musb_hw_ep *hw_ep = musb->endpoints + i;
  1406. hw_ep->fifo = (void __iomem *)(uintptr_t) MUSB_FIFO_OFFSET(i);
  1407. #ifdef CONFIG_USB_MUSB_TUSB6010
  1408. hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
  1409. hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
  1410. hw_ep->fifo_sync_va = musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
  1411. if (i == 0)
  1412. hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
  1413. else
  1414. hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
  1415. #endif
  1416. /* change data structure for ssusb */
  1417. hw_ep->addr_txcsr0 = (void __iomem *)(uintptr_t) SSUSB_EP_TXCR0_OFFSET(i, 0);
  1418. hw_ep->addr_txcsr1 = (void __iomem *)(uintptr_t) SSUSB_EP_TXCR1_OFFSET(i, 0);
  1419. hw_ep->addr_txcsr2 = (void __iomem *)(uintptr_t) SSUSB_EP_TXCR2_OFFSET(i, 0);
  1420. hw_ep->addr_rxcsr0 = (void __iomem *)(uintptr_t) SSUSB_EP_RXCR0_OFFSET(i, 0);
  1421. hw_ep->addr_rxcsr1 = (void __iomem *)(uintptr_t) SSUSB_EP_RXCR1_OFFSET(i, 0);
  1422. hw_ep->addr_rxcsr2 = (void __iomem *)(uintptr_t) SSUSB_EP_RXCR2_OFFSET(i, 0);
  1423. hw_ep->addr_rxcsr3 = (void __iomem *)(uintptr_t) SSUSB_EP_RXCR3_OFFSET(i, 0);
  1424. hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
  1425. hw_ep->rx_reinit = 1;
  1426. hw_ep->tx_reinit = 1;
  1427. if (hw_ep->max_packet_sz_tx) {
  1428. dev_dbg(musb->controller,
  1429. "%s: hw_ep %d%s, %smax %d\n",
  1430. musb_driver_name, i,
  1431. hw_ep->is_shared_fifo ? "shared" : "tx",
  1432. "", hw_ep->max_packet_sz_tx);
  1433. }
  1434. if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
  1435. dev_dbg(musb->controller,
  1436. "%s: hw_ep %d%s, %smax %d\n",
  1437. musb_driver_name, i, "rx", "", hw_ep->max_packet_sz_rx);
  1438. }
  1439. if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
  1440. dev_dbg(musb->controller, "hw_ep %d not configured\n", i);
  1441. }
  1442. #ifdef USE_SSUSB_QMU
  1443. /* Allocate GBD and BD */
  1444. _ex_mu3d_hal_alloc_qmu_mem(musb->controller);
  1445. /* Iniital QMU */
  1446. _ex_mu3d_hal_init_qmu();
  1447. musb_save_context(musb);
  1448. #endif
  1449. return 0;
  1450. }
  1451. /*
  1452. * handle all the irqs defined by the HDRC core. for now we expect: other
  1453. * irq sources (phy, dma, etc) will be handled first, musb->int_* values
  1454. * will be assigned, and the irq will already have been acked.
  1455. *
  1456. * called in irq context with spinlock held, irqs blocked
  1457. */
  1458. irqreturn_t musb_interrupt(struct musb *musb)
  1459. {
  1460. irqreturn_t retval = IRQ_NONE;
  1461. u8 devctl, power = 0;
  1462. #ifndef USE_SSUSB_QMU
  1463. u32 reg = 0, ep_num = 0;
  1464. #endif
  1465. #ifdef POWER_SAVING_MODE
  1466. if (!(os_readl(U3D_SSUSB_U2_CTRL_0P) & SSUSB_U2_PORT_PDN)) {
  1467. devctl = (u8) os_readl(U3D_DEVICE_CONTROL);
  1468. power = (u8) os_readl(U3D_POWER_MANAGEMENT);
  1469. } else {
  1470. devctl = 0;
  1471. power = 0;
  1472. musb->int_usb = 0;
  1473. }
  1474. #else
  1475. devctl = (u8) os_readl(U3D_DEVICE_CONTROL);
  1476. power = (u8) os_readl(U3D_POWER_MANAGEMENT);
  1477. #endif
  1478. /* dev_dbg(musb->controller, "** IRQ %s usb%04x tx%04x rx%04x\n", */
  1479. os_printk(K_DEBUG, "IRQ %s usb%04x tx%04x rx%04x\n",
  1480. (devctl & USB_DEVCTL_HOSTMODE) ? "host" : "peripheral",
  1481. musb->int_usb, musb->int_tx, musb->int_rx);
  1482. /* the core can interrupt us for multiple reasons; docs have
  1483. * a generic interrupt flowchart to follow
  1484. */
  1485. if (musb->int_usb)
  1486. retval |= musb_stage0_irq(musb, musb->int_usb, devctl, power);
  1487. /* "stage 1" is handling endpoint irqs */
  1488. /* handle endpoint 0 first */
  1489. if (musb->int_tx & 1)
  1490. retval |= musb_g_ep0_irq(musb);
  1491. #ifndef USE_SSUSB_QMU
  1492. /* RX on endpoints 1-15 */
  1493. reg = musb->int_rx >> 1;
  1494. ep_num = 1;
  1495. while (reg) {
  1496. if (reg & 1) {
  1497. /* musb_ep_select(musb->mregs, ep_num); */
  1498. /* REVISIT just retval = ep->rx_irq(...) */
  1499. retval = IRQ_HANDLED;
  1500. musb_g_rx(musb, ep_num);
  1501. }
  1502. reg >>= 1;
  1503. ep_num++;
  1504. }
  1505. /* TX on endpoints 1-15 */
  1506. reg = musb->int_tx >> 1;
  1507. ep_num = 1;
  1508. while (reg) {
  1509. if (reg & 1) {
  1510. /* musb_ep_select(musb->mregs, ep_num); */
  1511. /* REVISIT just retval |= ep->tx_irq(...) */
  1512. retval = IRQ_HANDLED;
  1513. musb_g_tx(musb, ep_num);
  1514. }
  1515. reg >>= 1;
  1516. ep_num++;
  1517. }
  1518. #endif
  1519. return retval;
  1520. }
  1521. EXPORT_SYMBOL_GPL(musb_interrupt);
  1522. #ifndef CONFIG_USB_MU3D_PIO_ONLY
  1523. static bool use_dma = 1;
  1524. /* "modprobe ... use_dma=0" etc */
  1525. module_param(use_dma, bool, 0);
  1526. MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
  1527. void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
  1528. {
  1529. u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  1530. /* called with controller lock already held */
  1531. if (!epnum) {
  1532. #ifndef CONFIG_USB_TUSB_OMAP_DMA
  1533. if (!is_cppi_enabled()) {
  1534. /* endpoint 0 */
  1535. if (devctl & MUSB_DEVCTL_HM)
  1536. /* Do nothing, MUSB does _NOT_ support host */
  1537. /* musb_h_ep0_irq(musb); */
  1538. os_printk(K_DEBUG, "Call musb_h_ep0_irq(), AYKM???!!!\n");
  1539. else
  1540. musb_g_ep0_irq(musb);
  1541. }
  1542. #endif
  1543. } else {
  1544. /* endpoints 1..15 */
  1545. if (transmit) {
  1546. if (devctl & MUSB_DEVCTL_HM) {
  1547. if (is_host_capable())
  1548. /* Do nothing, MUSB does _NOT_ support host */
  1549. /* musb_host_tx(musb, epnum); */
  1550. os_printk(K_DEBUG, "Call musb_host_tx(), AYKM???!!!\n");
  1551. } else {
  1552. if (is_peripheral_capable())
  1553. musb_g_tx(musb, epnum);
  1554. }
  1555. } else {
  1556. /* receive */
  1557. if (devctl & MUSB_DEVCTL_HM) {
  1558. if (is_host_capable())
  1559. /* Do nothing, MUSB does _NOT_ support host */
  1560. /* musb_host_tx(musb, epnum); */
  1561. os_printk(K_DEBUG, "Call musb_host_tx(), AYKM???!!!\n");
  1562. } else {
  1563. if (is_peripheral_capable())
  1564. musb_g_rx(musb, epnum);
  1565. }
  1566. }
  1567. }
  1568. }
  1569. #else
  1570. #define use_dma 0
  1571. #endif
  1572. /*-------------------------------------------------------------------------*/
  1573. #ifdef CONFIG_SYSFS
  1574. static ssize_t musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  1575. {
  1576. struct musb *musb = dev_to_musb(dev);
  1577. unsigned long flags;
  1578. int ret = -EINVAL;
  1579. spin_lock_irqsave(&musb->lock, flags);
  1580. ret = sprintf(buf, "%s\n", usb_otg_state_string(musb->xceiv->state));
  1581. spin_unlock_irqrestore(&musb->lock, flags);
  1582. return ret;
  1583. }
  1584. static ssize_t
  1585. musb_mode_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t n)
  1586. {
  1587. struct musb *musb = dev_to_musb(dev);
  1588. unsigned long flags;
  1589. int status;
  1590. spin_lock_irqsave(&musb->lock, flags);
  1591. if (sysfs_streq(buf, "host"))
  1592. status = musb_platform_set_mode(musb, MUSB_HOST);
  1593. else if (sysfs_streq(buf, "peripheral"))
  1594. status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
  1595. else if (sysfs_streq(buf, "otg"))
  1596. status = musb_platform_set_mode(musb, MUSB_OTG);
  1597. else
  1598. status = -EINVAL;
  1599. spin_unlock_irqrestore(&musb->lock, flags);
  1600. return (status == 0) ? n : status;
  1601. }
  1602. static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
  1603. static ssize_t
  1604. musb_vbus_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t n)
  1605. {
  1606. struct musb *musb = dev_to_musb(dev);
  1607. unsigned long flags;
  1608. unsigned long val;
  1609. /*if (sscanf(buf, "%lu", &val) < 1) {*/
  1610. if (kstrtol(buf, 10, &val) < 1) {
  1611. dev_err(dev, "Invalid VBUS timeout ms value\n");
  1612. return -EINVAL;
  1613. }
  1614. spin_lock_irqsave(&musb->lock, flags);
  1615. /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
  1616. musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0;
  1617. if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
  1618. musb->is_active = 0;
  1619. musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
  1620. spin_unlock_irqrestore(&musb->lock, flags);
  1621. return n;
  1622. }
  1623. static ssize_t musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
  1624. {
  1625. struct musb *musb = dev_to_musb(dev);
  1626. unsigned long flags;
  1627. unsigned long val;
  1628. int vbus;
  1629. spin_lock_irqsave(&musb->lock, flags);
  1630. val = musb->a_wait_bcon;
  1631. /* FIXME get_vbus_status() is normally #defined as false...
  1632. * and is effectively TUSB-specific.
  1633. */
  1634. vbus = musb_platform_get_vbus_status(musb);
  1635. spin_unlock_irqrestore(&musb->lock, flags);
  1636. return sprintf(buf, "Vbus %s, timeout %lu msec\n", vbus ? "on" : "off", val);
  1637. }
  1638. static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
  1639. /* Gadget drivers can't know that a host is connected so they might want
  1640. * to start SRP, but users can. This allows userspace to trigger SRP.
  1641. */
  1642. static ssize_t
  1643. musb_srp_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t n)
  1644. {
  1645. struct musb *musb = dev_to_musb(dev);
  1646. unsigned short srp;
  1647. /*if (sscanf(buf, "%hu", &srp) != 1 || (srp != 1)) {*/
  1648. if (kstrtol(buf, 10, (long *)&srp) != 1 || (srp != 1)) {
  1649. dev_err(dev, "SRP: Value must be 1\n");
  1650. return -EINVAL;
  1651. }
  1652. if (srp == 1)
  1653. musb_g_wakeup(musb);
  1654. return n;
  1655. }
  1656. static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
  1657. DEVICE_ATTR(cmode, 0664, musb_cmode_show, musb_cmode_store);
  1658. #ifdef CONFIG_MTK_UART_USB_SWITCH
  1659. DEVICE_ATTR(portmode, 0664, musb_portmode_show, musb_portmode_store);
  1660. DEVICE_ATTR(tx, 0664, musb_tx_show, musb_tx_store);
  1661. DEVICE_ATTR(rx, 0444, musb_rx_show, NULL);
  1662. DEVICE_ATTR(uartpath, 0444, musb_uart_path_show, NULL);
  1663. #endif
  1664. static struct attribute *musb_attributes[] = {
  1665. &dev_attr_mode.attr,
  1666. &dev_attr_vbus.attr,
  1667. &dev_attr_srp.attr,
  1668. &dev_attr_cmode.attr,
  1669. #ifdef CONFIG_MTK_UART_USB_SWITCH
  1670. &dev_attr_portmode.attr,
  1671. &dev_attr_tx.attr,
  1672. &dev_attr_rx.attr,
  1673. &dev_attr_uartpath.attr,
  1674. #endif
  1675. NULL
  1676. };
  1677. static const struct attribute_group musb_attr_group = {
  1678. .attrs = musb_attributes,
  1679. };
  1680. #endif /* sysfs */
  1681. static void musb_save_context(struct musb *musb)
  1682. {
  1683. int i;
  1684. for (i = 0; i < musb->config->num_eps; ++i) {
  1685. os_printk(K_DEBUG, "%s EP%d\n", __func__, i);
  1686. #ifdef USE_SSUSB_QMU
  1687. /* Save TXQ/RXQ starting address. Those would be reset to 0 after reset SSUSB IP. */
  1688. musb->context.index_regs[i].txqmuaddr = os_readl(USB_QMU_TQSAR(i + 1));
  1689. os_printk(K_DEBUG, "%s TQSAR[%d]=%x\n", __func__, i,
  1690. musb->context.index_regs[i].txqmuaddr);
  1691. musb->context.index_regs[i].rxqmuaddr = os_readl(USB_QMU_RQSAR(i + 1));
  1692. os_printk(K_DEBUG, "%s RQSAR[%d]=%x\n", __func__, i,
  1693. musb->context.index_regs[i].rxqmuaddr);
  1694. #endif
  1695. }
  1696. }
  1697. static void musb_restore_context(struct musb *musb)
  1698. {
  1699. int i;
  1700. for (i = 0; i < musb->config->num_eps; ++i) {
  1701. #ifdef USE_SSUSB_QMU
  1702. os_writel(USB_QMU_TQSAR(i + 1), musb->context.index_regs[i].txqmuaddr);
  1703. os_writel(USB_QMU_RQSAR(i + 1), musb->context.index_regs[i].rxqmuaddr);
  1704. os_printk(K_DEBUG, "%s TQSAR[%d]=%x\n", __func__, i,
  1705. os_readl(USB_QMU_TQSAR(i + 1)));
  1706. os_printk(K_DEBUG, "%s TQSAR[%d]=%x\n", __func__, i,
  1707. os_readl(USB_QMU_RQSAR(i + 1)));
  1708. #endif
  1709. }
  1710. }
  1711. static void musb_suspend_work(struct work_struct *data)
  1712. {
  1713. struct musb *musb = container_of(data, struct musb, suspend_work);
  1714. os_printk(K_INFO, "%s active_ep=%d, clk_on=%d\n", __func__, musb->active_ep,
  1715. musb->is_clk_on);
  1716. if (musb->is_clk_on == 1
  1717. && (!usb_cable_connected() || (musb->usb_mode != CABLE_MODE_NORMAL))) {
  1718. #ifdef EP_PROFILING
  1719. cancel_delayed_work_sync(&musb->ep_prof_work);
  1720. #endif
  1721. /*
  1722. * Note: musb_save_context() _MUST_ be called _BEFORE_ setting SSUSB_IP_SW_RST.
  1723. * Because when setting SSUSB_IP_SW_RST to reset the SSUSB IP,
  1724. * All MAC regs can _NOT_ be read and be reset to the default value.
  1725. * So save the MUST-SAVED reg in the context structure.
  1726. */
  1727. musb_save_context(musb);
  1728. /* Set SSUSB_IP_SW_RST to avoid power leakage */
  1729. os_setmsk(U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
  1730. #ifndef CONFIG_MTK_FPGA
  1731. /* Let PHY enter savecurrent mode. And turn off CLK. */
  1732. usb_phy_savecurrent(musb->is_clk_on);
  1733. musb->is_clk_on = 0;
  1734. #endif
  1735. }
  1736. }
  1737. /* Only used to provide driver mode change events */
  1738. static void musb_irq_work(struct work_struct *data)
  1739. {
  1740. struct musb *musb = container_of(data, struct musb, irq_work);
  1741. static int old_state;
  1742. os_printk(K_INFO, "%s [%d]=[%d]\n", __func__, musb->xceiv->state, old_state);
  1743. if (musb->xceiv->state != old_state) {
  1744. old_state = musb->xceiv->state;
  1745. sysfs_notify(&musb->controller->kobj, NULL, "mode");
  1746. }
  1747. }
  1748. const struct hc_driver musb_hc_driver = {
  1749. .description = "musb-hcd",
  1750. .product_desc = "MUSB HDRC host driver",
  1751. .hcd_priv_size = sizeof(struct musb),
  1752. .flags = HCD_USB2 | HCD_MEMORY,
  1753. };
  1754. #ifdef CONFIG_USB_C_SWITCH
  1755. static struct typec_switch_data switch_driver = {
  1756. .name = (char *)musb_driver_name,
  1757. .type = DEVICE_TYPE,
  1758. .enable = typec_switch_usb_connect,
  1759. .disable = typec_switch_usb_disconnect,
  1760. };
  1761. #endif
  1762. /* --------------------------------------------------------------------------
  1763. * Init support
  1764. */
  1765. #ifdef CONFIG_USBIF_COMPLIANCE
  1766. static struct musb *allocate_instance(struct device *dev,
  1767. struct musb_hdrc_config *config, void __iomem *mbase)
  1768. #else
  1769. static struct musb *__init
  1770. allocate_instance(struct device *dev, struct musb_hdrc_config *config, void __iomem *mbase)
  1771. #endif
  1772. {
  1773. struct musb *musb;
  1774. struct musb_hw_ep *ep;
  1775. int epnum;
  1776. struct usb_hcd *hcd;
  1777. hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
  1778. if (!hcd)
  1779. return NULL;
  1780. /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
  1781. musb = hcd_to_musb(hcd);
  1782. INIT_LIST_HEAD(&musb->control);
  1783. INIT_LIST_HEAD(&musb->in_bulk);
  1784. INIT_LIST_HEAD(&musb->out_bulk);
  1785. hcd->uses_new_polling = 1;
  1786. hcd->has_tt = 1;
  1787. musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
  1788. musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
  1789. dev_set_drvdata(dev, musb);
  1790. musb->mregs = mbase;
  1791. musb->ctrl_base = mbase;
  1792. musb->nIrq = -ENODEV;
  1793. musb->config = config;
  1794. BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
  1795. for (epnum = 0, ep = musb->endpoints; epnum < musb->config->num_eps; epnum++, ep++) {
  1796. ep->musb = musb;
  1797. ep->epnum = epnum;
  1798. }
  1799. musb->in_ipo_off = false;
  1800. musb->controller = dev;
  1801. /* added for ssusb: */
  1802. /* musb->xceiv = kzalloc(sizeof(struct otg_transceiver), GFP_KERNEL); */
  1803. /* memset(musb->xceiv, 0, sizeof(struct otg_transceiver)); */
  1804. /* musb->xceiv->state = OTG_STATE_B_IDLE; //initial its value */
  1805. #ifdef CONFIG_DEBUG_FS
  1806. if (usb20_phy_init_debugfs())
  1807. os_printk(K_ERR, "usb20_phy_init_debugfs fail!\n");
  1808. #endif
  1809. return musb;
  1810. }
  1811. static void musb_free(struct musb *musb)
  1812. {
  1813. /* this has multiple entry modes. it handles fault cleanup after
  1814. * probe(), where things may be partially set up, as well as rmmod
  1815. * cleanup after everything's been de-activated.
  1816. */
  1817. #ifdef CONFIG_SYSFS
  1818. sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
  1819. #endif
  1820. musb_gadget_cleanup(musb);
  1821. if (musb->nIrq >= 0) {
  1822. if (musb->irq_wake)
  1823. disable_irq_wake(musb->nIrq);
  1824. free_irq(musb->nIrq, musb);
  1825. }
  1826. #ifdef USE_SSUSB_QMU
  1827. tasklet_kill(&musb->qmu_done);
  1828. tasklet_kill(&musb->error_recovery);
  1829. #endif
  1830. cancel_work_sync(&musb->irq_work);
  1831. cancel_delayed_work_sync(&musb->connection_work);
  1832. /* cancel_delayed_work_sync(&musb->check_ltssm_work); */
  1833. cancel_work_sync(&musb->suspend_work);
  1834. #ifdef USE_SSUSB_QMU
  1835. _ex_mu3d_hal_free_qmu_mem(musb->controller);
  1836. #endif
  1837. /*
  1838. if (is_dma_capable() && musb->dma_controller) {
  1839. struct dma_controller *c = musb->dma_controller;
  1840. (void) c->stop(c);
  1841. dma_controller_destroy(c);
  1842. }
  1843. */
  1844. wake_lock_destroy(&musb->usb_wakelock);
  1845. /* added for ssusb: */
  1846. #ifdef CONFIG_USBIF_COMPLIANCE
  1847. /* kfree(musb->xceiv); //free the instance allocated in allocate_instance */
  1848. /* musb->xceiv = NULL; */
  1849. /* kfree(musb); */
  1850. #else
  1851. /*i add these, need to test */
  1852. usb_put_hcd(musb_to_hcd(musb));
  1853. kfree(musb->xceiv); /* free the instance allocated in allocate_instance */
  1854. musb->xceiv = NULL;
  1855. kfree(musb);
  1856. #endif
  1857. }
  1858. /*
  1859. * Perform generic per-controller initialization.
  1860. *
  1861. * @pDevice: the controller (already clocked, etc)
  1862. * @nIrq: irq
  1863. * @mregs: virtual address of controller registers,
  1864. * not yet corrected for platform-specific offsets
  1865. */
  1866. #ifdef CONFIG_USBIF_COMPLIANCE
  1867. static int musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
  1868. #else
  1869. static int __init musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
  1870. #endif
  1871. {
  1872. int status;
  1873. struct musb *musb;
  1874. struct musb_hdrc_platform_data *plat = dev->platform_data;
  1875. struct usb_hcd *hcd;
  1876. /* The driver might handle more features than the board; OK.
  1877. * Fail when the board needs a feature that's not enabled.
  1878. */
  1879. os_printk(K_INFO, "[MU3D]%s\n", __func__);
  1880. if (!plat) {
  1881. dev_err(dev, "no platform_data?\n");
  1882. status = -ENODEV;
  1883. goto fail0;
  1884. }
  1885. /* allocate */
  1886. musb = allocate_instance(dev, plat->config, ctrl);
  1887. if (!musb) {
  1888. status = -ENOMEM;
  1889. goto fail0;
  1890. }
  1891. /* pm_runtime_use_autosuspend(musb->controller); */
  1892. /* pm_runtime_set_autosuspend_delay(musb->controller, 200); */
  1893. /* pm_runtime_enable(musb->controller); */
  1894. spin_lock_init(&musb->lock);
  1895. sema_init(&musb->musb_lock, 1);
  1896. musb->board_mode = plat->mode;
  1897. musb->board_set_power = plat->set_power;
  1898. musb->min_power = plat->min_power;
  1899. musb->ops = plat->platform_ops;
  1900. musb->usb_mode = CABLE_MODE_NORMAL;
  1901. _mu3d_musb = musb;
  1902. wake_lock_init(&musb->usb_wakelock, WAKE_LOCK_SUSPEND, "USB.lock");
  1903. INIT_DELAYED_WORK(&musb->connection_work, connection_work);
  1904. INIT_DELAYED_WORK(&musb->check_ltssm_work, check_ltssm_work);
  1905. #ifndef CONFIG_USBIF_COMPLIANCE
  1906. INIT_DELAYED_WORK(&musb->reconnect_work, reconnect_work);
  1907. #endif
  1908. #ifdef EP_PROFILING
  1909. INIT_DELAYED_WORK(&musb->ep_prof_work, ep_prof_work);
  1910. #endif
  1911. /* The musb_platform_init() call:
  1912. * - adjusts musb->mregs and musb->isr if needed,
  1913. * - may initialize an integrated tranceiver
  1914. * - initializes musb->xceiv, usually by otg_get_transceiver()
  1915. * - stops powering VBUS
  1916. *
  1917. * There are various transceiver configurations. Blackfin,
  1918. * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
  1919. * external/discrete ones in various flavors (twl4030 family,
  1920. * isp1504, non-OTG, etc) mostly hooking up through ULPI.
  1921. */
  1922. /*move to musb_init.c */
  1923. /*musb->isr = generic_interrupt; */
  1924. status = musb_platform_init(musb);
  1925. if (status < 0)
  1926. goto fail1;
  1927. if (!musb->isr) {
  1928. status = -ENODEV;
  1929. goto fail3;
  1930. }
  1931. /* pm_runtime_get_sync(musb->controller); */
  1932. /* ideally this would be abstracted in platform setup */
  1933. #ifdef USE_SSUSB_QMU
  1934. if (!is_dma_capable())
  1935. #else
  1936. if (!is_dma_capable() || !musb->dma_controller)
  1937. #endif
  1938. dev->dma_mask = NULL;
  1939. /* be sure interrupts are disabled before connecting ISR */
  1940. musb_platform_disable(musb);
  1941. musb_generic_disable();
  1942. /* setup musb parts of the core (especially endpoints) */
  1943. status = musb_core_init(plat->config->multipoint
  1944. ? MUSB_CONTROLLER_MHDRC : MUSB_CONTROLLER_HDRC, musb);
  1945. if (status < 0)
  1946. goto fail3;
  1947. #ifdef CONFIG_USB_C_SWITCH
  1948. switch_driver.priv_data = musb;
  1949. status = register_typec_switch_callback(&switch_driver);
  1950. if (status < 0)
  1951. goto fail3;
  1952. #endif
  1953. /* REVISIT-J: Do _NOT_ support OTG functionality */
  1954. /* setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb); */
  1955. /* Init IRQ workqueue before request_irq */
  1956. INIT_WORK(&musb->irq_work, musb_irq_work);
  1957. INIT_WORK(&musb->suspend_work, musb_suspend_work);
  1958. #ifdef USE_SSUSB_QMU
  1959. tasklet_init(&musb->qmu_done, qmu_done_tasklet, (unsigned long)musb);
  1960. tasklet_init(&musb->error_recovery, qmu_error_recovery, (unsigned long)musb);
  1961. #endif
  1962. /* attach to the IRQ */
  1963. if (request_irq(nIrq, musb->isr, IRQF_TRIGGER_LOW, dev_name(dev), musb)) {
  1964. dev_err(dev, "request_irq %d failed!\n", nIrq);
  1965. status = -ENODEV;
  1966. goto fail3;
  1967. }
  1968. musb->nIrq = nIrq;
  1969. /* FIXME this handles wakeup irqs wrong */
  1970. if (enable_irq_wake(nIrq) == 0) {
  1971. musb->irq_wake = 1;
  1972. device_init_wakeup(dev, 1);
  1973. } else {
  1974. musb->irq_wake = 0;
  1975. }
  1976. /* host side needs more setup */
  1977. #ifndef CONFIG_USBIF_COMPLIANCE
  1978. if (is_host_enabled(musb)) {
  1979. hcd = musb_to_hcd(musb);
  1980. otg_set_host(musb->xceiv->otg, &hcd->self);
  1981. if (is_otg_enabled(musb))
  1982. hcd->self.otg_port = 1;
  1983. musb->xceiv->otg->host = &hcd->self;
  1984. hcd->power_budget = 2 * (plat->power ? : 250);
  1985. /* program PHY to use external vBus if required */
  1986. if (plat->extvbus) {
  1987. u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
  1988. busctl |= MUSB_ULPI_USE_EXTVBUS;
  1989. musb_write_ulpi_buscontrol(musb->mregs, busctl);
  1990. }
  1991. }
  1992. #endif
  1993. MUSB_DEV_MODE(musb);
  1994. musb->xceiv->otg->default_a = 0;
  1995. musb->xceiv->state = OTG_STATE_B_IDLE;
  1996. status = musb_gadget_setup(musb);
  1997. if (status < 0)
  1998. goto fail3;
  1999. status = musb_init_debugfs(musb);
  2000. if (status < 0)
  2001. goto fail4;
  2002. #ifdef CONFIG_SYSFS
  2003. status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
  2004. if (status)
  2005. goto fail5;
  2006. #endif
  2007. pm_runtime_put(musb->controller);
  2008. dev_info(dev, "USB %s mode controller at %p using %s, IRQ %d\n", ({
  2009. char *s;
  2010. switch (musb->board_mode) {
  2011. case MUSB_HOST:
  2012. s = "Host"; break; case MUSB_PERIPHERAL:
  2013. s = "Peripheral"; break; default:
  2014. s = "OTG"; break; }; s; }
  2015. ), ctrl, (is_dma_capable() && musb->dma_controller)
  2016. ? "DMA" : "PIO", musb->nIrq);
  2017. return 0;
  2018. fail5:
  2019. musb_exit_debugfs(musb);
  2020. fail4:
  2021. if (!is_otg_enabled(musb) && is_host_enabled(musb))
  2022. usb_remove_hcd(musb_to_hcd(musb));
  2023. else
  2024. musb_gadget_cleanup(musb);
  2025. fail3:
  2026. if (musb->irq_wake)
  2027. device_init_wakeup(dev, 0);
  2028. musb_platform_exit(musb);
  2029. fail1:
  2030. dev_err(musb->controller, "musb_init_controller failed with status %d\n", status);
  2031. musb_free(musb);
  2032. fail0:
  2033. return status;
  2034. }
  2035. #define USB3_BASE_REGS_ADDR_RES_NAME "ssusb_base"
  2036. #define USB3_SIF_REGS_ADDR_RES_NAME "ssusb_sif"
  2037. #define USB3_SIF2_REGS_ADDR_RES_NAME "ssusb_sif2"
  2038. static void __iomem *acquire_reg_base(struct platform_device *pdev, const char *res_name)
  2039. {
  2040. struct resource *iomem;
  2041. void __iomem *base = NULL;
  2042. iomem = platform_get_resource_byname(pdev, IORESOURCE_MEM, res_name);
  2043. if (!iomem) {
  2044. pr_err("Can't get resource for %s\n", res_name);
  2045. goto end;
  2046. }
  2047. base = ioremap(iomem->start, resource_size(iomem));
  2048. if (!(uintptr_t) base) {
  2049. pr_err("Can't remap %s\n", res_name);
  2050. goto end;
  2051. }
  2052. os_printk(K_INFO, "%s=0x%lx\n", res_name, (uintptr_t) (base));
  2053. end:
  2054. return base;
  2055. }
  2056. /*-------------------------------------------------------------------------*/
  2057. /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
  2058. * bridge to a platform device; this driver then suffices.
  2059. */
  2060. #ifdef CONFIG_USBIF_COMPLIANCE
  2061. static int mu3d_normal_driver_on;
  2062. static int musb_probe(struct platform_device *pdev)
  2063. #else
  2064. static int __init musb_probe(struct platform_device *pdev)
  2065. #endif
  2066. {
  2067. struct device *dev = &pdev->dev;
  2068. int irq = platform_get_irq_byname(pdev, MUSB_DRIVER_NAME);
  2069. int status = 0;
  2070. #ifdef CONFIG_MTK_UART_USB_SWITCH
  2071. struct device_node *ap_uart0_node = NULL;
  2072. #endif
  2073. if (irq <= 0)
  2074. return -ENODEV;
  2075. os_printk(K_INFO, "[MU3D]musb_probe irq=%d\n", irq);
  2076. u3_base = acquire_reg_base(pdev, USB3_BASE_REGS_ADDR_RES_NAME);
  2077. if (!u3_base)
  2078. goto exit_regs;
  2079. u3_sif_base = acquire_reg_base(pdev, USB3_SIF_REGS_ADDR_RES_NAME);
  2080. if (!u3_sif_base)
  2081. goto exit_regs;
  2082. u3_sif2_base = acquire_reg_base(pdev, USB3_SIF2_REGS_ADDR_RES_NAME);
  2083. if (!u3_sif2_base)
  2084. goto exit_regs;
  2085. #ifdef CONFIG_MTK_UART_USB_SWITCH
  2086. ap_uart0_node = of_find_compatible_node(NULL, NULL, AP_UART0_COMPATIBLE_NAME);
  2087. if (ap_uart0_node == NULL) {
  2088. os_printk(K_ERR, "USB get ap_uart0_node failed\n");
  2089. if (ap_uart0_base)
  2090. iounmap(ap_uart0_base);
  2091. ap_uart0_base = 0;
  2092. } else {
  2093. ap_uart0_base = of_iomap(ap_uart0_node, 0);
  2094. }
  2095. #endif
  2096. #ifdef CONFIG_MTK_FPGA
  2097. /*i2c1_base = ioremap(0x11008000, 0x1000); */
  2098. i2c1_base = ioremap(0x11009000, 0x1000);
  2099. if (!(i2c1_base)) {
  2100. pr_err("Can't remap I2C1 BASE\n");
  2101. status = -ENOMEM;
  2102. }
  2103. os_printk(K_INFO, "I2C1 BASE=0x%lx\n", (uintptr_t) (i2c1_base));
  2104. #endif
  2105. status = musb_init_controller(dev, irq, u3_base);
  2106. if (status < 0)
  2107. goto exit_regs;
  2108. return status;
  2109. exit_regs:
  2110. if (u3_base)
  2111. iounmap(u3_base);
  2112. if (u3_sif_base)
  2113. iounmap(u3_sif_base);
  2114. if (u3_sif2_base)
  2115. iounmap(u3_sif2_base);
  2116. u3_base = 0;
  2117. u3_sif_base = 0;
  2118. u3_sif2_base = 0;
  2119. return status;
  2120. }
  2121. static int musb_remove(struct platform_device *pdev)
  2122. {
  2123. struct musb *musb = dev_to_musb(&pdev->dev);
  2124. void __iomem *ctrl_base = musb->ctrl_base;
  2125. /* this gets called on rmmod.
  2126. * - Host mode: host may still be active
  2127. * - Peripheral mode: peripheral is deactivated (or never-activated)
  2128. * - OTG mode: both roles are deactivated (or never-activated)
  2129. */
  2130. #ifdef CONFIG_SYSFS /* USBIF */
  2131. sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
  2132. #endif
  2133. pm_runtime_get_sync(musb->controller);
  2134. musb_exit_debugfs(musb);
  2135. musb_shutdown(pdev);
  2136. pm_runtime_put(musb->controller);
  2137. musb_free(musb);
  2138. _mu3d_musb = NULL;
  2139. #ifndef CONFIG_USBIF_COMPLIANCE
  2140. /* USB IF share resource with mu3d nor drv, so do not unmap it in IF case */
  2141. iounmap(ctrl_base);
  2142. #endif
  2143. device_init_wakeup(&pdev->dev, 0);
  2144. return 0;
  2145. }
  2146. /*
  2147. * MU3D driver does _NOT_ use PM to control USB power state.
  2148. * When cable disconnected and all EPs are disabled, turn off all the clock and powers.
  2149. * Turn on all the clock and powers until the cable exists.
  2150. */
  2151. #ifdef NEVER /* CONFIG_PM */
  2152. /*Do _NOT_ use the original save and restore context functions*/
  2153. #ifdef NEVER
  2154. static void musb_save_context(struct musb *musb)
  2155. {
  2156. int i;
  2157. void __iomem *musb_base = musb->mregs;
  2158. void __iomem *epio;
  2159. if (is_host_enabled(musb)) {
  2160. musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
  2161. musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
  2162. musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
  2163. }
  2164. musb->context.power = musb_readb(musb_base, MUSB_POWER);
  2165. musb->context.intrtxe = musb_readw(musb_base, MUSB_INTRTXE);
  2166. musb->context.intrrxe = musb_readw(musb_base, MUSB_INTRRXE);
  2167. musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
  2168. musb->context.index = musb_readb(musb_base, MUSB_INDEX);
  2169. musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
  2170. for (i = 0; i < musb->config->num_eps; ++i) {
  2171. struct musb_hw_ep *hw_ep;
  2172. hw_ep = &musb->endpoints[i];
  2173. if (!hw_ep)
  2174. continue;
  2175. epio = hw_ep->regs;
  2176. if (!epio)
  2177. continue;
  2178. musb->context.index_regs[i].txmaxp = musb_readw(epio, MUSB_TXMAXP);
  2179. musb->context.index_regs[i].txcsr = musb_readw(epio, MUSB_TXCSR);
  2180. musb->context.index_regs[i].rxmaxp = musb_readw(epio, MUSB_RXMAXP);
  2181. musb->context.index_regs[i].rxcsr = musb_readw(epio, MUSB_RXCSR);
  2182. if (musb->dyn_fifo) {
  2183. musb->context.index_regs[i].txfifoadd = musb_read_txfifoadd(musb_base);
  2184. musb->context.index_regs[i].rxfifoadd = musb_read_rxfifoadd(musb_base);
  2185. musb->context.index_regs[i].txfifosz = musb_read_txfifosz(musb_base);
  2186. musb->context.index_regs[i].rxfifosz = musb_read_rxfifosz(musb_base);
  2187. }
  2188. if (is_host_enabled(musb)) {
  2189. musb->context.index_regs[i].txtype = musb_readb(epio, MUSB_TXTYPE);
  2190. musb->context.index_regs[i].txinterval = musb_readb(epio, MUSB_TXINTERVAL);
  2191. musb->context.index_regs[i].rxtype = musb_readb(epio, MUSB_RXTYPE);
  2192. musb->context.index_regs[i].rxinterval = musb_readb(epio, MUSB_RXINTERVAL);
  2193. musb->context.index_regs[i].txfunaddr = musb_read_txfunaddr(musb_base, i);
  2194. musb->context.index_regs[i].txhubaddr = musb_read_txhubaddr(musb_base, i);
  2195. musb->context.index_regs[i].txhubport = musb_read_txhubport(musb_base, i);
  2196. musb->context.index_regs[i].rxfunaddr = musb_read_rxfunaddr(musb_base, i);
  2197. musb->context.index_regs[i].rxhubaddr = musb_read_rxhubaddr(musb_base, i);
  2198. musb->context.index_regs[i].rxhubport = musb_read_rxhubport(musb_base, i);
  2199. }
  2200. }
  2201. }
  2202. static void musb_restore_context(struct musb *musb)
  2203. {
  2204. int i;
  2205. void __iomem *musb_base = musb->mregs;
  2206. void __iomem *ep_target_regs;
  2207. void __iomem *epio;
  2208. if (is_host_enabled(musb)) {
  2209. musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
  2210. musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
  2211. musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
  2212. }
  2213. musb_writeb(musb_base, MUSB_POWER, musb->context.power);
  2214. musb_writew(musb_base, MUSB_INTRTXE, musb->context.intrtxe);
  2215. musb_writew(musb_base, MUSB_INTRRXE, musb->context.intrrxe);
  2216. musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
  2217. musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
  2218. for (i = 0; i < musb->config->num_eps; ++i) {
  2219. struct musb_hw_ep *hw_ep;
  2220. hw_ep = &musb->endpoints[i];
  2221. if (!hw_ep)
  2222. continue;
  2223. epio = hw_ep->regs;
  2224. if (!epio)
  2225. continue;
  2226. musb_writew(epio, MUSB_TXMAXP, musb->context.index_regs[i].txmaxp);
  2227. musb_writew(epio, MUSB_TXCSR, musb->context.index_regs[i].txcsr);
  2228. musb_writew(epio, MUSB_RXMAXP, musb->context.index_regs[i].rxmaxp);
  2229. musb_writew(epio, MUSB_RXCSR, musb->context.index_regs[i].rxcsr);
  2230. if (musb->dyn_fifo) {
  2231. musb_write_txfifosz(musb_base, musb->context.index_regs[i].txfifosz);
  2232. musb_write_rxfifosz(musb_base, musb->context.index_regs[i].rxfifosz);
  2233. musb_write_txfifoadd(musb_base, musb->context.index_regs[i].txfifoadd);
  2234. musb_write_rxfifoadd(musb_base, musb->context.index_regs[i].rxfifoadd);
  2235. }
  2236. if (is_host_enabled(musb)) {
  2237. musb_writeb(epio, MUSB_TXTYPE, musb->context.index_regs[i].txtype);
  2238. musb_writeb(epio, MUSB_TXINTERVAL, musb->context.index_regs[i].txinterval);
  2239. musb_writeb(epio, MUSB_RXTYPE, musb->context.index_regs[i].rxtype);
  2240. musb_writeb(epio, MUSB_RXINTERVAL, musb->context.index_regs[i].rxinterval);
  2241. musb_write_txfunaddr(musb_base, i, musb->context.index_regs[i].txfunaddr);
  2242. musb_write_txhubaddr(musb_base, i, musb->context.index_regs[i].txhubaddr);
  2243. musb_write_txhubport(musb_base, i, musb->context.index_regs[i].txhubport);
  2244. ep_target_regs = musb_read_target_reg_base(i, musb_base);
  2245. musb_write_rxfunaddr(ep_target_regs, musb->context.index_regs[i].rxfunaddr);
  2246. musb_write_rxhubaddr(ep_target_regs, musb->context.index_regs[i].rxhubaddr);
  2247. musb_write_rxhubport(ep_target_regs, musb->context.index_regs[i].rxhubport);
  2248. }
  2249. }
  2250. musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
  2251. }
  2252. #endif /* NEVER */
  2253. static void musb_save_context(struct musb *musb)
  2254. {
  2255. int i;
  2256. #ifdef CONFIG_USB_MU3D_DRV
  2257. /*
  2258. * U3D_EPIER(Endpoint 0 interrupt enable.) and U3D_EP0CSR(EP0 MaxP Size)
  2259. * would be configured at
  2260. * #U2: RESET Signal -> musb_g_reset() -> musb_conifg_ep0() -> ep0_setup()
  2261. * #U3: ENTER_U0_INTR -> musb_conifg_ep0() -> ep0_setup()
  2262. * U3D_EPIER(Endpoint N interrupt enable.) at PIO mode
  2263. * would be configured when PC sends USB_REQ_SET_CONFIGURATION.
  2264. */
  2265. /*
  2266. musb->context.intr_ep = os_readl(U3D_EPIER);
  2267. musb->context.ep0_csr = os_readl(U3D_EP0CSR);
  2268. */
  2269. #ifdef USE_SSUSB_QMU
  2270. /*
  2271. * QGCSR(RXQ/TXQ Enable) and QIER0(QMU Done Interrupt Enable)
  2272. * would be configured at mu3d_hal_ep_enable() when PC sends USB_REQ_SET_CONFIGURATION
  2273. * USB_REQ_SET_CONFIGURATION -> set_config() -> f->set_alt() -> usb_ep_enable() -> musb_gadget_enable()
  2274. * So do _NOT_ have to save those value.
  2275. */
  2276. /*
  2277. musb->context.qmu_crs = os_readl(U3D_QGCSR);
  2278. musb->context.intr_qmu_done = os_readl(U3D_QIER0);
  2279. */
  2280. #endif
  2281. #endif /* CONFIG_USB_MU3D_DRV */
  2282. for (i = 0; i < musb->config->num_eps; ++i) {
  2283. os_printk(K_DEBUG, "%s EP%d\n", __func__, i);
  2284. #ifdef CONFIG_USB_MU3D_DRV
  2285. /*
  2286. * Each TX/RX EP CSR would be configured at mu3d_hal_ep_enable() when PC sends USB_REQ_SET_CONFIGURATION
  2287. */
  2288. /*
  2289. musb->context.index_regs[i].txcsr0 = USB_ReadCsr32(U3D_TX1CSR0, i+1);
  2290. musb->context.index_regs[i].txcsr1 = USB_ReadCsr32(U3D_TX1CSR1, i+1);
  2291. musb->context.index_regs[i].txcsr2 = USB_ReadCsr32(U3D_TX1CSR2, i+1);
  2292. musb->context.index_regs[i].rxcsr0 = USB_ReadCsr32(U3D_RX1CSR0, i+1);
  2293. musb->context.index_regs[i].rxcsr1 = USB_ReadCsr32(U3D_RX1CSR1, i+1);
  2294. musb->context.index_regs[i].rxcsr2 = USB_ReadCsr32(U3D_RX1CSR2, i+1);
  2295. */
  2296. #ifdef USE_SSUSB_QMU
  2297. /* Save TXQ/RXQ starting address. Those would be reset to 0 after reset SSUSB IP. */
  2298. musb->context.index_regs[i].txqmuaddr = os_readl(USB_QMU_TQSAR(i + 1));
  2299. os_printk(K_DEBUG, "%s TQSAR[%d]=%x\n", __func__, i,
  2300. musb->context.index_regs[i].txqmuaddr);
  2301. musb->context.index_regs[i].rxqmuaddr = os_readl(USB_QMU_RQSAR(i + 1));
  2302. os_printk(K_DEBUG, "%s RQSAR[%d]=%x\n", __func__, i,
  2303. musb->context.index_regs[i].rxqmuaddr);
  2304. #endif
  2305. #endif /* CONFIG_USB_MU3D_DRV */
  2306. }
  2307. }
  2308. static void musb_restore_context(struct musb *musb)
  2309. {
  2310. int i;
  2311. #ifdef CONFIG_USB_MU3D_DRV
  2312. /*
  2313. os_writel(U3D_EPIESR, musb->context.intr_ep);
  2314. os_writel(U3D_EP0CSR, musb->context.ep0_csr);
  2315. */
  2316. #ifdef USE_SSUSB_QMU
  2317. /*
  2318. os_writel(U3D_QGCSR, musb->context.qmu_crs);
  2319. os_writel(U3D_QIESR0, musb->context.intr_qmu_done);
  2320. */
  2321. #endif
  2322. #endif /* CONFIG_USB_MU3D_DRV */
  2323. for (i = 0; i < musb->config->num_eps; ++i) {
  2324. #ifdef CONFIG_USB_MU3D_DRV
  2325. /*
  2326. USB_WriteCsr32(U3D_TX1CSR0, i+1, musb->context.index_regs[i].txcsr0);
  2327. USB_WriteCsr32(U3D_TX1CSR1, i+1, musb->context.index_regs[i].txcsr1);
  2328. USB_WriteCsr32(U3D_TX1CSR2, i+1, musb->context.index_regs[i].txcsr2);
  2329. USB_WriteCsr32(U3D_RX1CSR0, i+1, musb->context.index_regs[i].rxcsr0);
  2330. USB_WriteCsr32(U3D_RX1CSR1, i+1, musb->context.index_regs[i].rxcsr1);
  2331. USB_WriteCsr32(U3D_RX1CSR2, i+1, musb->context.index_regs[i].rxcsr2);
  2332. */
  2333. #ifdef USE_SSUSB_QMU
  2334. os_writel(USB_QMU_TQSAR(i + 1), musb->context.index_regs[i].txqmuaddr);
  2335. os_writel(USB_QMU_RQSAR(i + 1), musb->context.index_regs[i].rxqmuaddr);
  2336. os_printk(K_INFO, "%s TQSAR[%d]=%x\n", __func__, i, os_readl(USB_QMU_TQSAR(i + 1)));
  2337. os_printk(K_INFO, "%s TQSAR[%d]=%x\n", __func__, i, os_readl(USB_QMU_RQSAR(i + 1)));
  2338. #endif
  2339. #endif /* CONFIG_USB_MU3D_DRV */
  2340. }
  2341. }
  2342. static int musb_suspend_noirq(struct device *dev)
  2343. {
  2344. struct musb *musb = dev_to_musb(dev);
  2345. os_printk(K_INFO, "%s\n", __func__);
  2346. /*
  2347. * Note: musb_save_context() _MUST_ be called _BEFORE_ mtu3d_suspend_noirq().
  2348. * Because when mtu3d_suspend_noirq() resets the SSUSB IP, All MAC regs can _NOT_ be read and be reset to
  2349. * the default value. So save the MUST-SAVED reg in the context structure.
  2350. */
  2351. musb_save_context(musb);
  2352. /* Set SSUSB_IP_SW_RST to avoid power leakage */
  2353. os_setmsk(U3D_SSUSB_IP_PW_CTRL0, SSUSB_IP_SW_RST);
  2354. #ifndef CONFIG_MTK_FPGA
  2355. /* Let PHY enter savecurrent mode. And turn off CLK. */
  2356. usb_phy_savecurrent(musb->is_clk_on);
  2357. musb->is_clk_on = 0;
  2358. #endif
  2359. return 0;
  2360. }
  2361. static int musb_resume_noirq(struct device *dev)
  2362. {
  2363. struct musb *musb = dev_to_musb(dev);
  2364. os_printk(K_INFO, "%s\n", __func__);
  2365. #ifndef CONFIG_MTK_FPGA
  2366. /* Recovert PHY. And turn on CLK. */
  2367. usb_phy_recover(musb->is_clk_on);
  2368. musb->is_clk_on = 1;
  2369. /* USB 2.0 slew rate calibration */
  2370. u3phy_ops->u2_slew_rate_calibration(u3phy);
  2371. #endif
  2372. /* disable IP reset and power down, disable U2/U3 ip power down */
  2373. _ex_mu3d_hal_ssusb_en();
  2374. /* reset U3D all dev module. */
  2375. mu3d_hal_rst_dev();
  2376. musb_restore_context(musb);
  2377. return 0;
  2378. }
  2379. static const struct dev_pm_ops musb_dev_pm_ops = {
  2380. .suspend_noirq = musb_suspend_noirq,
  2381. .resume_noirq = musb_resume_noirq,
  2382. };
  2383. #define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
  2384. #else /* NEVER */
  2385. /* These suspend/Resume function deal with UART switch related recover only */
  2386. #ifdef CONFIG_MTK_UART_USB_SWITCH
  2387. #define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
  2388. static int musb_suspend_noirq(struct device *dev)
  2389. {
  2390. os_printk(K_INFO, "%s: for CONFIG_MTK_UART_USB_SWITCH: in_uart_mode: %d\n", __func__,
  2391. in_uart_mode);
  2392. return 0;
  2393. }
  2394. static int musb_resume_noirq(struct device *dev)
  2395. {
  2396. os_printk(K_INFO, "%s: for CONFIG_MTK_UART_USB_SWITCH: in_uart_mode: %d\n", __func__,
  2397. in_uart_mode);
  2398. if (in_uart_mode == true)
  2399. usb_phy_switch_to_uart();
  2400. return 0;
  2401. }
  2402. static const struct dev_pm_ops musb_dev_pm_ops = {
  2403. .suspend_noirq = musb_suspend_noirq,
  2404. .resume_noirq = musb_resume_noirq,
  2405. };
  2406. #else
  2407. #define MUSB_DEV_PM_OPS NULL
  2408. #endif /* CONFIG_MTK_UART_USB_SWITCH */
  2409. #endif /* NEVER */
  2410. static struct platform_driver musb_driver = {
  2411. .driver = {
  2412. .name = (char *)musb_driver_name,
  2413. .bus = &platform_bus_type,
  2414. .owner = THIS_MODULE,
  2415. .pm = MUSB_DEV_PM_OPS,
  2416. },
  2417. .probe = musb_probe,
  2418. .remove = musb_remove,
  2419. .shutdown = musb_shutdown,
  2420. };
  2421. /*-------------------------------------------------------------------------*/
  2422. #ifdef CONFIG_USBIF_COMPLIANCE
  2423. static int musb_mu3d_proc_show(struct seq_file *seq, void *v)
  2424. {
  2425. seq_printf(seq, "musb_mu3d_proc_show, mu3d is %d (on:1, off:0)\n", mu3d_normal_driver_on);
  2426. return 0;
  2427. }
  2428. static int musb_mu3d_proc_open(struct inode *inode, struct file *file)
  2429. {
  2430. return single_open(file, musb_mu3d_proc_show, inode->i_private);
  2431. }
  2432. static ssize_t musb_mu3d_proc_write(struct file *file, const char __user *buf, size_t length,
  2433. loff_t *ppos)
  2434. {
  2435. int ret;
  2436. char msg[32];
  2437. int result;
  2438. int status;
  2439. struct device *dev;
  2440. int irq;
  2441. struct resource *iomem;
  2442. void __iomem *base;
  2443. struct musb *musb;
  2444. void __iomem *ctrl_base;
  2445. if (length >= sizeof(msg)) {
  2446. os_printk(K_ERR, "musb_mu3d_proc_write length error, the error len is %d\n",
  2447. (unsigned int)length);
  2448. return -EINVAL;
  2449. }
  2450. if (copy_from_user(msg, buf, length))
  2451. return -EFAULT;
  2452. msg[length] = 0;
  2453. os_printk(K_DEBUG, "musb_mu3d_proc_write: %s, current driver on/off: %d\n", msg,
  2454. mu3d_normal_driver_on);
  2455. if ((msg[0] == '1') && (mu3d_normal_driver_on == 0)) {
  2456. os_printk(K_DEBUG, "registe mu3d driver ===>\n");
  2457. init_connection_work();
  2458. init_check_ltssm_work();
  2459. platform_driver_register(&musb_driver);
  2460. mu3d_normal_driver_on = 1;
  2461. Charger_Detect_En(true);
  2462. os_printk(K_DEBUG, "registe mu3d driver <===\n");
  2463. } else if ((msg[0] == '0') && (mu3d_normal_driver_on == 1)) {
  2464. os_printk(K_DEBUG, "unregiste mu3d driver ===>\n");
  2465. mu3d_normal_driver_on = 0;
  2466. Charger_Detect_En(false);
  2467. platform_driver_unregister(&musb_driver);
  2468. os_printk(K_DEBUG, "unregiste mu3d driver <===\n");
  2469. } else {
  2470. /* kernel_restart(NULL); */
  2471. /* arch_reset(0, NULL); */
  2472. os_printk(K_ERR, "musb_mu3d_proc_write , set reboot !\n");
  2473. /* os_printk(K_ERR, "musb_mu3d_proc_write write faile !\n"); */
  2474. }
  2475. return length;
  2476. }
  2477. static const struct file_operations mu3d_proc_fops = {
  2478. .owner = THIS_MODULE,
  2479. .open = musb_mu3d_proc_open,
  2480. .write = musb_mu3d_proc_write,
  2481. .read = seq_read,
  2482. .llseek = seq_lseek,
  2483. };
  2484. static int __init musb_init(void)
  2485. {
  2486. struct proc_dir_entry *prEntry;
  2487. int ret = 0;
  2488. if (usb_disabled())
  2489. return 0;
  2490. pr_info("%s: version " MUSB_VERSION ", ?dma?, otg (peripheral+host)\n", musb_driver_name);
  2491. /* USBIF */
  2492. prEntry = proc_create("mu3d_driver_init", 0666, NULL, &mu3d_proc_fops);
  2493. if (prEntry)
  2494. os_printk(K_ERR, "create the mu3d init proc OK!\n");
  2495. else
  2496. os_printk(K_ERR, "[ERROR] create the mu3d init proc FAIL\n");
  2497. /* set MU3D up at boot up */
  2498. ret = platform_driver_register(&musb_driver);
  2499. mu3d_normal_driver_on = 1;
  2500. Charger_Detect_En(true);
  2501. return ret;
  2502. }
  2503. module_init(musb_init);
  2504. static void __exit musb_cleanup(void)
  2505. {
  2506. os_printk(K_ERR, "musb_cleanup\n");
  2507. if (mu3d_normal_driver_on == 1)
  2508. platform_driver_unregister(&musb_driver);
  2509. return 0;
  2510. }
  2511. module_exit(musb_cleanup);
  2512. #else
  2513. static int __init musb_init(void)
  2514. {
  2515. if (usb_disabled())
  2516. return 0;
  2517. pr_info("%s: version " MUSB_VERSION ", ?dma?, otg (peripheral+host)\n", musb_driver_name);
  2518. return platform_driver_register(&musb_driver);
  2519. }
  2520. module_init(musb_init);
  2521. static void __exit musb_cleanup(void)
  2522. {
  2523. platform_driver_unregister(&musb_driver);
  2524. }
  2525. module_exit(musb_cleanup);
  2526. #endif