xhci-tegra.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVIDIA Tegra xHCI host controller driver
  4. *
  5. * Copyright (c) 2014-2020, NVIDIA CORPORATION. All rights reserved.
  6. * Copyright (C) 2014 Google, Inc.
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/firmware.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/phy/tegra/xusb.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/usb/ch9.h>
  22. #include <linux/pm.h>
  23. #include <linux/pm_domain.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/reset.h>
  27. #include <linux/slab.h>
  28. #include <linux/usb/otg.h>
  29. #include <linux/usb/phy.h>
  30. #include <linux/usb/role.h>
  31. #include <soc/tegra/pmc.h>
  32. #include "xhci.h"
  33. #define TEGRA_XHCI_SS_HIGH_SPEED 120000000
  34. #define TEGRA_XHCI_SS_LOW_SPEED 12000000
  35. /* FPCI CFG registers */
  36. #define XUSB_CFG_1 0x004
  37. #define XUSB_IO_SPACE_EN BIT(0)
  38. #define XUSB_MEM_SPACE_EN BIT(1)
  39. #define XUSB_BUS_MASTER_EN BIT(2)
  40. #define XUSB_CFG_4 0x010
  41. #define XUSB_BASE_ADDR_SHIFT 15
  42. #define XUSB_BASE_ADDR_MASK 0x1ffff
  43. #define XUSB_CFG_7 0x01c
  44. #define XUSB_BASE2_ADDR_SHIFT 16
  45. #define XUSB_BASE2_ADDR_MASK 0xffff
  46. #define XUSB_CFG_16 0x040
  47. #define XUSB_CFG_24 0x060
  48. #define XUSB_CFG_AXI_CFG 0x0f8
  49. #define XUSB_CFG_ARU_C11_CSBRANGE 0x41c
  50. #define XUSB_CFG_ARU_CONTEXT 0x43c
  51. #define XUSB_CFG_ARU_CONTEXT_HS_PLS 0x478
  52. #define XUSB_CFG_ARU_CONTEXT_FS_PLS 0x47c
  53. #define XUSB_CFG_ARU_CONTEXT_HSFS_SPEED 0x480
  54. #define XUSB_CFG_ARU_CONTEXT_HSFS_PP 0x484
  55. #define XUSB_CFG_CSB_BASE_ADDR 0x800
  56. /* FPCI mailbox registers */
  57. /* XUSB_CFG_ARU_MBOX_CMD */
  58. #define MBOX_DEST_FALC BIT(27)
  59. #define MBOX_DEST_PME BIT(28)
  60. #define MBOX_DEST_SMI BIT(29)
  61. #define MBOX_DEST_XHCI BIT(30)
  62. #define MBOX_INT_EN BIT(31)
  63. /* XUSB_CFG_ARU_MBOX_DATA_IN and XUSB_CFG_ARU_MBOX_DATA_OUT */
  64. #define CMD_DATA_SHIFT 0
  65. #define CMD_DATA_MASK 0xffffff
  66. #define CMD_TYPE_SHIFT 24
  67. #define CMD_TYPE_MASK 0xff
  68. /* XUSB_CFG_ARU_MBOX_OWNER */
  69. #define MBOX_OWNER_NONE 0
  70. #define MBOX_OWNER_FW 1
  71. #define MBOX_OWNER_SW 2
  72. #define XUSB_CFG_ARU_SMI_INTR 0x428
  73. #define MBOX_SMI_INTR_FW_HANG BIT(1)
  74. #define MBOX_SMI_INTR_EN BIT(3)
  75. /* BAR2 registers */
  76. #define XUSB_BAR2_ARU_MBOX_CMD 0x004
  77. #define XUSB_BAR2_ARU_MBOX_DATA_IN 0x008
  78. #define XUSB_BAR2_ARU_MBOX_DATA_OUT 0x00c
  79. #define XUSB_BAR2_ARU_MBOX_OWNER 0x010
  80. #define XUSB_BAR2_ARU_SMI_INTR 0x014
  81. #define XUSB_BAR2_ARU_SMI_ARU_FW_SCRATCH_DATA0 0x01c
  82. #define XUSB_BAR2_ARU_IFRDMA_CFG0 0x0e0
  83. #define XUSB_BAR2_ARU_IFRDMA_CFG1 0x0e4
  84. #define XUSB_BAR2_ARU_IFRDMA_STREAMID_FIELD 0x0e8
  85. #define XUSB_BAR2_ARU_C11_CSBRANGE 0x9c
  86. #define XUSB_BAR2_ARU_FW_SCRATCH 0x1000
  87. #define XUSB_BAR2_CSB_BASE_ADDR 0x2000
  88. /* IPFS registers */
  89. #define IPFS_XUSB_HOST_MSI_BAR_SZ_0 0x0c0
  90. #define IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0 0x0c4
  91. #define IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0 0x0c8
  92. #define IPFS_XUSB_HOST_MSI_VEC0_0 0x100
  93. #define IPFS_XUSB_HOST_MSI_EN_VEC0_0 0x140
  94. #define IPFS_XUSB_HOST_CONFIGURATION_0 0x180
  95. #define IPFS_EN_FPCI BIT(0)
  96. #define IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0 0x184
  97. #define IPFS_XUSB_HOST_INTR_MASK_0 0x188
  98. #define IPFS_IP_INT_MASK BIT(16)
  99. #define IPFS_XUSB_HOST_INTR_ENABLE_0 0x198
  100. #define IPFS_XUSB_HOST_UFPCI_CONFIG_0 0x19c
  101. #define IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0 0x1bc
  102. #define IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0 0x1dc
  103. #define CSB_PAGE_SELECT_MASK 0x7fffff
  104. #define CSB_PAGE_SELECT_SHIFT 9
  105. #define CSB_PAGE_OFFSET_MASK 0x1ff
  106. #define CSB_PAGE_SELECT(addr) ((addr) >> (CSB_PAGE_SELECT_SHIFT) & \
  107. CSB_PAGE_SELECT_MASK)
  108. #define CSB_PAGE_OFFSET(addr) ((addr) & CSB_PAGE_OFFSET_MASK)
  109. /* Falcon CSB registers */
  110. #define XUSB_FALC_CPUCTL 0x100
  111. #define CPUCTL_STARTCPU BIT(1)
  112. #define CPUCTL_STATE_HALTED BIT(4)
  113. #define CPUCTL_STATE_STOPPED BIT(5)
  114. #define XUSB_FALC_BOOTVEC 0x104
  115. #define XUSB_FALC_DMACTL 0x10c
  116. #define XUSB_FALC_IMFILLRNG1 0x154
  117. #define IMFILLRNG1_TAG_MASK 0xffff
  118. #define IMFILLRNG1_TAG_LO_SHIFT 0
  119. #define IMFILLRNG1_TAG_HI_SHIFT 16
  120. #define XUSB_FALC_IMFILLCTL 0x158
  121. /* CSB ARU registers */
  122. #define XUSB_CSB_ARU_SCRATCH0 0x100100
  123. /* MP CSB registers */
  124. #define XUSB_CSB_MP_ILOAD_ATTR 0x101a00
  125. #define XUSB_CSB_MP_ILOAD_BASE_LO 0x101a04
  126. #define XUSB_CSB_MP_ILOAD_BASE_HI 0x101a08
  127. #define XUSB_CSB_MP_L2IMEMOP_SIZE 0x101a10
  128. #define L2IMEMOP_SIZE_SRC_OFFSET_SHIFT 8
  129. #define L2IMEMOP_SIZE_SRC_OFFSET_MASK 0x3ff
  130. #define L2IMEMOP_SIZE_SRC_COUNT_SHIFT 24
  131. #define L2IMEMOP_SIZE_SRC_COUNT_MASK 0xff
  132. #define XUSB_CSB_MP_L2IMEMOP_TRIG 0x101a14
  133. #define L2IMEMOP_ACTION_SHIFT 24
  134. #define L2IMEMOP_INVALIDATE_ALL (0x40 << L2IMEMOP_ACTION_SHIFT)
  135. #define L2IMEMOP_LOAD_LOCKED_RESULT (0x11 << L2IMEMOP_ACTION_SHIFT)
  136. #define XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT 0x101a18
  137. #define L2IMEMOP_RESULT_VLD BIT(31)
  138. #define XUSB_CSB_MP_APMAP 0x10181c
  139. #define APMAP_BOOTPATH BIT(31)
  140. #define IMEM_BLOCK_SIZE 256
  141. #define FW_IOCTL_TYPE_SHIFT 24
  142. #define FW_IOCTL_CFGTBL_READ 17
  143. struct tegra_xusb_fw_header {
  144. __le32 boot_loadaddr_in_imem;
  145. __le32 boot_codedfi_offset;
  146. __le32 boot_codetag;
  147. __le32 boot_codesize;
  148. __le32 phys_memaddr;
  149. __le16 reqphys_memsize;
  150. __le16 alloc_phys_memsize;
  151. __le32 rodata_img_offset;
  152. __le32 rodata_section_start;
  153. __le32 rodata_section_end;
  154. __le32 main_fnaddr;
  155. __le32 fwimg_cksum;
  156. __le32 fwimg_created_time;
  157. __le32 imem_resident_start;
  158. __le32 imem_resident_end;
  159. __le32 idirect_start;
  160. __le32 idirect_end;
  161. __le32 l2_imem_start;
  162. __le32 l2_imem_end;
  163. __le32 version_id;
  164. u8 init_ddirect;
  165. u8 reserved[3];
  166. __le32 phys_addr_log_buffer;
  167. __le32 total_log_entries;
  168. __le32 dequeue_ptr;
  169. __le32 dummy_var[2];
  170. __le32 fwimg_len;
  171. u8 magic[8];
  172. __le32 ss_low_power_entry_timeout;
  173. u8 num_hsic_port;
  174. u8 padding[139]; /* Pad to 256 bytes */
  175. };
  176. struct tegra_xusb_phy_type {
  177. const char *name;
  178. unsigned int num;
  179. };
  180. struct tegra_xusb_mbox_regs {
  181. u16 cmd;
  182. u16 data_in;
  183. u16 data_out;
  184. u16 owner;
  185. u16 smi_intr;
  186. };
  187. struct tegra_xusb_context_soc {
  188. struct {
  189. const unsigned int *offsets;
  190. unsigned int num_offsets;
  191. } ipfs;
  192. struct {
  193. const unsigned int *offsets;
  194. unsigned int num_offsets;
  195. } fpci;
  196. };
  197. struct tegra_xusb;
  198. struct tegra_xusb_soc_ops {
  199. u32 (*mbox_reg_readl)(struct tegra_xusb *tegra, unsigned int offset);
  200. void (*mbox_reg_writel)(struct tegra_xusb *tegra, u32 value, unsigned int offset);
  201. u32 (*csb_reg_readl)(struct tegra_xusb *tegra, unsigned int offset);
  202. void (*csb_reg_writel)(struct tegra_xusb *tegra, u32 value, unsigned int offset);
  203. };
  204. struct tegra_xusb_soc {
  205. const char *firmware;
  206. const char * const *supply_names;
  207. unsigned int num_supplies;
  208. const struct tegra_xusb_phy_type *phy_types;
  209. unsigned int num_types;
  210. const struct tegra_xusb_context_soc *context;
  211. struct {
  212. struct {
  213. unsigned int offset;
  214. unsigned int count;
  215. } usb2, ulpi, hsic, usb3;
  216. } ports;
  217. struct tegra_xusb_mbox_regs mbox;
  218. const struct tegra_xusb_soc_ops *ops;
  219. bool scale_ss_clock;
  220. bool has_ipfs;
  221. bool lpm_support;
  222. bool otg_reset_sspi;
  223. bool has_bar2;
  224. };
  225. struct tegra_xusb_context {
  226. u32 *ipfs;
  227. u32 *fpci;
  228. };
  229. struct tegra_xusb {
  230. struct device *dev;
  231. void __iomem *regs;
  232. struct usb_hcd *hcd;
  233. struct mutex lock;
  234. int xhci_irq;
  235. int mbox_irq;
  236. int padctl_irq;
  237. void __iomem *ipfs_base;
  238. void __iomem *fpci_base;
  239. void __iomem *bar2_base;
  240. struct resource *bar2;
  241. const struct tegra_xusb_soc *soc;
  242. struct regulator_bulk_data *supplies;
  243. struct tegra_xusb_padctl *padctl;
  244. struct clk *host_clk;
  245. struct clk *falcon_clk;
  246. struct clk *ss_clk;
  247. struct clk *ss_src_clk;
  248. struct clk *hs_src_clk;
  249. struct clk *fs_src_clk;
  250. struct clk *pll_u_480m;
  251. struct clk *clk_m;
  252. struct clk *pll_e;
  253. struct reset_control *host_rst;
  254. struct reset_control *ss_rst;
  255. struct device *genpd_dev_host;
  256. struct device *genpd_dev_ss;
  257. bool use_genpd;
  258. struct phy **phys;
  259. unsigned int num_phys;
  260. struct usb_phy **usbphy;
  261. unsigned int num_usb_phys;
  262. int otg_usb2_port;
  263. int otg_usb3_port;
  264. bool host_mode;
  265. struct notifier_block id_nb;
  266. struct work_struct id_work;
  267. /* Firmware loading related */
  268. struct {
  269. size_t size;
  270. void *virt;
  271. dma_addr_t phys;
  272. } fw;
  273. bool suspended;
  274. struct tegra_xusb_context context;
  275. u8 lp0_utmi_pad_mask;
  276. };
  277. static struct hc_driver __read_mostly tegra_xhci_hc_driver;
  278. static inline u32 fpci_readl(struct tegra_xusb *tegra, unsigned int offset)
  279. {
  280. return readl(tegra->fpci_base + offset);
  281. }
  282. static inline void fpci_writel(struct tegra_xusb *tegra, u32 value,
  283. unsigned int offset)
  284. {
  285. writel(value, tegra->fpci_base + offset);
  286. }
  287. static inline u32 ipfs_readl(struct tegra_xusb *tegra, unsigned int offset)
  288. {
  289. return readl(tegra->ipfs_base + offset);
  290. }
  291. static inline void ipfs_writel(struct tegra_xusb *tegra, u32 value,
  292. unsigned int offset)
  293. {
  294. writel(value, tegra->ipfs_base + offset);
  295. }
  296. static inline u32 bar2_readl(struct tegra_xusb *tegra, unsigned int offset)
  297. {
  298. return readl(tegra->bar2_base + offset);
  299. }
  300. static inline void bar2_writel(struct tegra_xusb *tegra, u32 value,
  301. unsigned int offset)
  302. {
  303. writel(value, tegra->bar2_base + offset);
  304. }
  305. static u32 csb_readl(struct tegra_xusb *tegra, unsigned int offset)
  306. {
  307. const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
  308. return ops->csb_reg_readl(tegra, offset);
  309. }
  310. static void csb_writel(struct tegra_xusb *tegra, u32 value,
  311. unsigned int offset)
  312. {
  313. const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
  314. ops->csb_reg_writel(tegra, value, offset);
  315. }
  316. static u32 fpci_csb_readl(struct tegra_xusb *tegra, unsigned int offset)
  317. {
  318. u32 page = CSB_PAGE_SELECT(offset);
  319. u32 ofs = CSB_PAGE_OFFSET(offset);
  320. fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
  321. return fpci_readl(tegra, XUSB_CFG_CSB_BASE_ADDR + ofs);
  322. }
  323. static void fpci_csb_writel(struct tegra_xusb *tegra, u32 value,
  324. unsigned int offset)
  325. {
  326. u32 page = CSB_PAGE_SELECT(offset);
  327. u32 ofs = CSB_PAGE_OFFSET(offset);
  328. fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
  329. fpci_writel(tegra, value, XUSB_CFG_CSB_BASE_ADDR + ofs);
  330. }
  331. static u32 bar2_csb_readl(struct tegra_xusb *tegra, unsigned int offset)
  332. {
  333. u32 page = CSB_PAGE_SELECT(offset);
  334. u32 ofs = CSB_PAGE_OFFSET(offset);
  335. bar2_writel(tegra, page, XUSB_BAR2_ARU_C11_CSBRANGE);
  336. return bar2_readl(tegra, XUSB_BAR2_CSB_BASE_ADDR + ofs);
  337. }
  338. static void bar2_csb_writel(struct tegra_xusb *tegra, u32 value,
  339. unsigned int offset)
  340. {
  341. u32 page = CSB_PAGE_SELECT(offset);
  342. u32 ofs = CSB_PAGE_OFFSET(offset);
  343. bar2_writel(tegra, page, XUSB_BAR2_ARU_C11_CSBRANGE);
  344. bar2_writel(tegra, value, XUSB_BAR2_CSB_BASE_ADDR + ofs);
  345. }
  346. static int tegra_xusb_set_ss_clk(struct tegra_xusb *tegra,
  347. unsigned long rate)
  348. {
  349. unsigned long new_parent_rate, old_parent_rate;
  350. struct clk *clk = tegra->ss_src_clk;
  351. unsigned int div;
  352. int err;
  353. if (clk_get_rate(clk) == rate)
  354. return 0;
  355. switch (rate) {
  356. case TEGRA_XHCI_SS_HIGH_SPEED:
  357. /*
  358. * Reparent to PLLU_480M. Set divider first to avoid
  359. * overclocking.
  360. */
  361. old_parent_rate = clk_get_rate(clk_get_parent(clk));
  362. new_parent_rate = clk_get_rate(tegra->pll_u_480m);
  363. div = new_parent_rate / rate;
  364. err = clk_set_rate(clk, old_parent_rate / div);
  365. if (err)
  366. return err;
  367. err = clk_set_parent(clk, tegra->pll_u_480m);
  368. if (err)
  369. return err;
  370. /*
  371. * The rate should already be correct, but set it again just
  372. * to be sure.
  373. */
  374. err = clk_set_rate(clk, rate);
  375. if (err)
  376. return err;
  377. break;
  378. case TEGRA_XHCI_SS_LOW_SPEED:
  379. /* Reparent to CLK_M */
  380. err = clk_set_parent(clk, tegra->clk_m);
  381. if (err)
  382. return err;
  383. err = clk_set_rate(clk, rate);
  384. if (err)
  385. return err;
  386. break;
  387. default:
  388. dev_err(tegra->dev, "Invalid SS rate: %lu Hz\n", rate);
  389. return -EINVAL;
  390. }
  391. if (clk_get_rate(clk) != rate) {
  392. dev_err(tegra->dev, "SS clock doesn't match requested rate\n");
  393. return -EINVAL;
  394. }
  395. return 0;
  396. }
  397. static unsigned long extract_field(u32 value, unsigned int start,
  398. unsigned int count)
  399. {
  400. return (value >> start) & ((1 << count) - 1);
  401. }
  402. /* Command requests from the firmware */
  403. enum tegra_xusb_mbox_cmd {
  404. MBOX_CMD_MSG_ENABLED = 1,
  405. MBOX_CMD_INC_FALC_CLOCK,
  406. MBOX_CMD_DEC_FALC_CLOCK,
  407. MBOX_CMD_INC_SSPI_CLOCK,
  408. MBOX_CMD_DEC_SSPI_CLOCK,
  409. MBOX_CMD_SET_BW, /* no ACK/NAK required */
  410. MBOX_CMD_SET_SS_PWR_GATING,
  411. MBOX_CMD_SET_SS_PWR_UNGATING,
  412. MBOX_CMD_SAVE_DFE_CTLE_CTX,
  413. MBOX_CMD_AIRPLANE_MODE_ENABLED, /* unused */
  414. MBOX_CMD_AIRPLANE_MODE_DISABLED, /* unused */
  415. MBOX_CMD_START_HSIC_IDLE,
  416. MBOX_CMD_STOP_HSIC_IDLE,
  417. MBOX_CMD_DBC_WAKE_STACK, /* unused */
  418. MBOX_CMD_HSIC_PRETEND_CONNECT,
  419. MBOX_CMD_RESET_SSPI,
  420. MBOX_CMD_DISABLE_SS_LFPS_DETECTION,
  421. MBOX_CMD_ENABLE_SS_LFPS_DETECTION,
  422. MBOX_CMD_MAX,
  423. /* Response message to above commands */
  424. MBOX_CMD_ACK = 128,
  425. MBOX_CMD_NAK
  426. };
  427. struct tegra_xusb_mbox_msg {
  428. u32 cmd;
  429. u32 data;
  430. };
  431. static inline u32 tegra_xusb_mbox_pack(const struct tegra_xusb_mbox_msg *msg)
  432. {
  433. return (msg->cmd & CMD_TYPE_MASK) << CMD_TYPE_SHIFT |
  434. (msg->data & CMD_DATA_MASK) << CMD_DATA_SHIFT;
  435. }
  436. static inline void tegra_xusb_mbox_unpack(struct tegra_xusb_mbox_msg *msg,
  437. u32 value)
  438. {
  439. msg->cmd = (value >> CMD_TYPE_SHIFT) & CMD_TYPE_MASK;
  440. msg->data = (value >> CMD_DATA_SHIFT) & CMD_DATA_MASK;
  441. }
  442. static bool tegra_xusb_mbox_cmd_requires_ack(enum tegra_xusb_mbox_cmd cmd)
  443. {
  444. switch (cmd) {
  445. case MBOX_CMD_SET_BW:
  446. case MBOX_CMD_ACK:
  447. case MBOX_CMD_NAK:
  448. return false;
  449. default:
  450. return true;
  451. }
  452. }
  453. static int tegra_xusb_mbox_send(struct tegra_xusb *tegra,
  454. const struct tegra_xusb_mbox_msg *msg)
  455. {
  456. const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
  457. bool wait_for_idle = false;
  458. u32 value;
  459. /*
  460. * Acquire the mailbox. The firmware still owns the mailbox for
  461. * ACK/NAK messages.
  462. */
  463. if (!(msg->cmd == MBOX_CMD_ACK || msg->cmd == MBOX_CMD_NAK)) {
  464. value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner);
  465. if (value != MBOX_OWNER_NONE) {
  466. dev_err(tegra->dev, "mailbox is busy\n");
  467. return -EBUSY;
  468. }
  469. ops->mbox_reg_writel(tegra, MBOX_OWNER_SW, tegra->soc->mbox.owner);
  470. value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner);
  471. if (value != MBOX_OWNER_SW) {
  472. dev_err(tegra->dev, "failed to acquire mailbox\n");
  473. return -EBUSY;
  474. }
  475. wait_for_idle = true;
  476. }
  477. value = tegra_xusb_mbox_pack(msg);
  478. ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.data_in);
  479. value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.cmd);
  480. value |= MBOX_INT_EN | MBOX_DEST_FALC;
  481. ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.cmd);
  482. if (wait_for_idle) {
  483. unsigned long timeout = jiffies + msecs_to_jiffies(250);
  484. while (time_before(jiffies, timeout)) {
  485. value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner);
  486. if (value == MBOX_OWNER_NONE)
  487. break;
  488. usleep_range(10, 20);
  489. }
  490. if (time_after(jiffies, timeout))
  491. value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner);
  492. if (value != MBOX_OWNER_NONE)
  493. return -ETIMEDOUT;
  494. }
  495. return 0;
  496. }
  497. static irqreturn_t tegra_xusb_mbox_irq(int irq, void *data)
  498. {
  499. struct tegra_xusb *tegra = data;
  500. const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
  501. u32 value;
  502. /* clear mailbox interrupts */
  503. value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.smi_intr);
  504. ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.smi_intr);
  505. if (value & MBOX_SMI_INTR_FW_HANG)
  506. dev_err(tegra->dev, "controller firmware hang\n");
  507. return IRQ_WAKE_THREAD;
  508. }
  509. static void tegra_xusb_mbox_handle(struct tegra_xusb *tegra,
  510. const struct tegra_xusb_mbox_msg *msg)
  511. {
  512. struct tegra_xusb_padctl *padctl = tegra->padctl;
  513. const struct tegra_xusb_soc *soc = tegra->soc;
  514. struct device *dev = tegra->dev;
  515. struct tegra_xusb_mbox_msg rsp;
  516. unsigned long mask;
  517. unsigned int port;
  518. bool idle, enable;
  519. int err = 0;
  520. memset(&rsp, 0, sizeof(rsp));
  521. switch (msg->cmd) {
  522. case MBOX_CMD_INC_FALC_CLOCK:
  523. case MBOX_CMD_DEC_FALC_CLOCK:
  524. rsp.data = clk_get_rate(tegra->falcon_clk) / 1000;
  525. if (rsp.data != msg->data)
  526. rsp.cmd = MBOX_CMD_NAK;
  527. else
  528. rsp.cmd = MBOX_CMD_ACK;
  529. break;
  530. case MBOX_CMD_INC_SSPI_CLOCK:
  531. case MBOX_CMD_DEC_SSPI_CLOCK:
  532. if (tegra->soc->scale_ss_clock) {
  533. err = tegra_xusb_set_ss_clk(tegra, msg->data * 1000);
  534. if (err < 0)
  535. rsp.cmd = MBOX_CMD_NAK;
  536. else
  537. rsp.cmd = MBOX_CMD_ACK;
  538. rsp.data = clk_get_rate(tegra->ss_src_clk) / 1000;
  539. } else {
  540. rsp.cmd = MBOX_CMD_ACK;
  541. rsp.data = msg->data;
  542. }
  543. break;
  544. case MBOX_CMD_SET_BW:
  545. /*
  546. * TODO: Request bandwidth once EMC scaling is supported.
  547. * Ignore for now since ACK/NAK is not required for SET_BW
  548. * messages.
  549. */
  550. break;
  551. case MBOX_CMD_SAVE_DFE_CTLE_CTX:
  552. err = tegra_xusb_padctl_usb3_save_context(padctl, msg->data);
  553. if (err < 0) {
  554. dev_err(dev, "failed to save context for USB3#%u: %d\n",
  555. msg->data, err);
  556. rsp.cmd = MBOX_CMD_NAK;
  557. } else {
  558. rsp.cmd = MBOX_CMD_ACK;
  559. }
  560. rsp.data = msg->data;
  561. break;
  562. case MBOX_CMD_START_HSIC_IDLE:
  563. case MBOX_CMD_STOP_HSIC_IDLE:
  564. if (msg->cmd == MBOX_CMD_STOP_HSIC_IDLE)
  565. idle = false;
  566. else
  567. idle = true;
  568. mask = extract_field(msg->data, 1 + soc->ports.hsic.offset,
  569. soc->ports.hsic.count);
  570. for_each_set_bit(port, &mask, 32) {
  571. err = tegra_xusb_padctl_hsic_set_idle(padctl, port,
  572. idle);
  573. if (err < 0)
  574. break;
  575. }
  576. if (err < 0) {
  577. dev_err(dev, "failed to set HSIC#%u %s: %d\n", port,
  578. idle ? "idle" : "busy", err);
  579. rsp.cmd = MBOX_CMD_NAK;
  580. } else {
  581. rsp.cmd = MBOX_CMD_ACK;
  582. }
  583. rsp.data = msg->data;
  584. break;
  585. case MBOX_CMD_DISABLE_SS_LFPS_DETECTION:
  586. case MBOX_CMD_ENABLE_SS_LFPS_DETECTION:
  587. if (msg->cmd == MBOX_CMD_DISABLE_SS_LFPS_DETECTION)
  588. enable = false;
  589. else
  590. enable = true;
  591. mask = extract_field(msg->data, 1 + soc->ports.usb3.offset,
  592. soc->ports.usb3.count);
  593. for_each_set_bit(port, &mask, soc->ports.usb3.count) {
  594. err = tegra_xusb_padctl_usb3_set_lfps_detect(padctl,
  595. port,
  596. enable);
  597. if (err < 0)
  598. break;
  599. /*
  600. * wait 500us for LFPS detector to be disabled before
  601. * sending ACK
  602. */
  603. if (!enable)
  604. usleep_range(500, 1000);
  605. }
  606. if (err < 0) {
  607. dev_err(dev,
  608. "failed to %s LFPS detection on USB3#%u: %d\n",
  609. enable ? "enable" : "disable", port, err);
  610. rsp.cmd = MBOX_CMD_NAK;
  611. } else {
  612. rsp.cmd = MBOX_CMD_ACK;
  613. }
  614. rsp.data = msg->data;
  615. break;
  616. default:
  617. dev_warn(dev, "unknown message: %#x\n", msg->cmd);
  618. break;
  619. }
  620. if (rsp.cmd) {
  621. const char *cmd = (rsp.cmd == MBOX_CMD_ACK) ? "ACK" : "NAK";
  622. err = tegra_xusb_mbox_send(tegra, &rsp);
  623. if (err < 0)
  624. dev_err(dev, "failed to send %s: %d\n", cmd, err);
  625. }
  626. }
  627. static irqreturn_t tegra_xusb_mbox_thread(int irq, void *data)
  628. {
  629. struct tegra_xusb *tegra = data;
  630. const struct tegra_xusb_soc_ops *ops = tegra->soc->ops;
  631. struct tegra_xusb_mbox_msg msg;
  632. u32 value;
  633. mutex_lock(&tegra->lock);
  634. if (pm_runtime_suspended(tegra->dev) || tegra->suspended)
  635. goto out;
  636. value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.data_out);
  637. tegra_xusb_mbox_unpack(&msg, value);
  638. value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.cmd);
  639. value &= ~MBOX_DEST_SMI;
  640. ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.cmd);
  641. /* clear mailbox owner if no ACK/NAK is required */
  642. if (!tegra_xusb_mbox_cmd_requires_ack(msg.cmd))
  643. ops->mbox_reg_writel(tegra, MBOX_OWNER_NONE, tegra->soc->mbox.owner);
  644. tegra_xusb_mbox_handle(tegra, &msg);
  645. out:
  646. mutex_unlock(&tegra->lock);
  647. return IRQ_HANDLED;
  648. }
  649. static void tegra_xusb_config(struct tegra_xusb *tegra)
  650. {
  651. u32 regs = tegra->hcd->rsrc_start;
  652. u32 value;
  653. if (tegra->soc->has_ipfs) {
  654. value = ipfs_readl(tegra, IPFS_XUSB_HOST_CONFIGURATION_0);
  655. value |= IPFS_EN_FPCI;
  656. ipfs_writel(tegra, value, IPFS_XUSB_HOST_CONFIGURATION_0);
  657. usleep_range(10, 20);
  658. }
  659. /* Program BAR0 space */
  660. value = fpci_readl(tegra, XUSB_CFG_4);
  661. value &= ~(XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
  662. value |= regs & (XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
  663. fpci_writel(tegra, value, XUSB_CFG_4);
  664. /* Program BAR2 space */
  665. if (tegra->bar2) {
  666. value = fpci_readl(tegra, XUSB_CFG_7);
  667. value &= ~(XUSB_BASE2_ADDR_MASK << XUSB_BASE2_ADDR_SHIFT);
  668. value |= tegra->bar2->start &
  669. (XUSB_BASE2_ADDR_MASK << XUSB_BASE2_ADDR_SHIFT);
  670. fpci_writel(tegra, value, XUSB_CFG_7);
  671. }
  672. usleep_range(100, 200);
  673. /* Enable bus master */
  674. value = fpci_readl(tegra, XUSB_CFG_1);
  675. value |= XUSB_IO_SPACE_EN | XUSB_MEM_SPACE_EN | XUSB_BUS_MASTER_EN;
  676. fpci_writel(tegra, value, XUSB_CFG_1);
  677. if (tegra->soc->has_ipfs) {
  678. /* Enable interrupt assertion */
  679. value = ipfs_readl(tegra, IPFS_XUSB_HOST_INTR_MASK_0);
  680. value |= IPFS_IP_INT_MASK;
  681. ipfs_writel(tegra, value, IPFS_XUSB_HOST_INTR_MASK_0);
  682. /* Set hysteresis */
  683. ipfs_writel(tegra, 0x80, IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
  684. }
  685. }
  686. static int tegra_xusb_clk_enable(struct tegra_xusb *tegra)
  687. {
  688. int err;
  689. err = clk_prepare_enable(tegra->pll_e);
  690. if (err < 0)
  691. return err;
  692. err = clk_prepare_enable(tegra->host_clk);
  693. if (err < 0)
  694. goto disable_plle;
  695. err = clk_prepare_enable(tegra->ss_clk);
  696. if (err < 0)
  697. goto disable_host;
  698. err = clk_prepare_enable(tegra->falcon_clk);
  699. if (err < 0)
  700. goto disable_ss;
  701. err = clk_prepare_enable(tegra->fs_src_clk);
  702. if (err < 0)
  703. goto disable_falc;
  704. err = clk_prepare_enable(tegra->hs_src_clk);
  705. if (err < 0)
  706. goto disable_fs_src;
  707. if (tegra->soc->scale_ss_clock) {
  708. err = tegra_xusb_set_ss_clk(tegra, TEGRA_XHCI_SS_HIGH_SPEED);
  709. if (err < 0)
  710. goto disable_hs_src;
  711. }
  712. return 0;
  713. disable_hs_src:
  714. clk_disable_unprepare(tegra->hs_src_clk);
  715. disable_fs_src:
  716. clk_disable_unprepare(tegra->fs_src_clk);
  717. disable_falc:
  718. clk_disable_unprepare(tegra->falcon_clk);
  719. disable_ss:
  720. clk_disable_unprepare(tegra->ss_clk);
  721. disable_host:
  722. clk_disable_unprepare(tegra->host_clk);
  723. disable_plle:
  724. clk_disable_unprepare(tegra->pll_e);
  725. return err;
  726. }
  727. static void tegra_xusb_clk_disable(struct tegra_xusb *tegra)
  728. {
  729. clk_disable_unprepare(tegra->pll_e);
  730. clk_disable_unprepare(tegra->host_clk);
  731. clk_disable_unprepare(tegra->ss_clk);
  732. clk_disable_unprepare(tegra->falcon_clk);
  733. clk_disable_unprepare(tegra->fs_src_clk);
  734. clk_disable_unprepare(tegra->hs_src_clk);
  735. }
  736. static int tegra_xusb_phy_enable(struct tegra_xusb *tegra)
  737. {
  738. unsigned int i;
  739. int err;
  740. for (i = 0; i < tegra->num_phys; i++) {
  741. err = phy_init(tegra->phys[i]);
  742. if (err)
  743. goto disable_phy;
  744. err = phy_power_on(tegra->phys[i]);
  745. if (err) {
  746. phy_exit(tegra->phys[i]);
  747. goto disable_phy;
  748. }
  749. }
  750. return 0;
  751. disable_phy:
  752. while (i--) {
  753. phy_power_off(tegra->phys[i]);
  754. phy_exit(tegra->phys[i]);
  755. }
  756. return err;
  757. }
  758. static void tegra_xusb_phy_disable(struct tegra_xusb *tegra)
  759. {
  760. unsigned int i;
  761. for (i = 0; i < tegra->num_phys; i++) {
  762. phy_power_off(tegra->phys[i]);
  763. phy_exit(tegra->phys[i]);
  764. }
  765. }
  766. #ifdef CONFIG_PM_SLEEP
  767. static int tegra_xusb_init_context(struct tegra_xusb *tegra)
  768. {
  769. const struct tegra_xusb_context_soc *soc = tegra->soc->context;
  770. tegra->context.ipfs = devm_kcalloc(tegra->dev, soc->ipfs.num_offsets,
  771. sizeof(u32), GFP_KERNEL);
  772. if (!tegra->context.ipfs)
  773. return -ENOMEM;
  774. tegra->context.fpci = devm_kcalloc(tegra->dev, soc->fpci.num_offsets,
  775. sizeof(u32), GFP_KERNEL);
  776. if (!tegra->context.fpci)
  777. return -ENOMEM;
  778. return 0;
  779. }
  780. #else
  781. static inline int tegra_xusb_init_context(struct tegra_xusb *tegra)
  782. {
  783. return 0;
  784. }
  785. #endif
  786. static int tegra_xusb_request_firmware(struct tegra_xusb *tegra)
  787. {
  788. struct tegra_xusb_fw_header *header;
  789. const struct firmware *fw;
  790. int err;
  791. err = request_firmware(&fw, tegra->soc->firmware, tegra->dev);
  792. if (err < 0) {
  793. dev_err(tegra->dev, "failed to request firmware: %d\n", err);
  794. return err;
  795. }
  796. /* Load Falcon controller with its firmware. */
  797. header = (struct tegra_xusb_fw_header *)fw->data;
  798. tegra->fw.size = le32_to_cpu(header->fwimg_len);
  799. tegra->fw.virt = dma_alloc_coherent(tegra->dev, tegra->fw.size,
  800. &tegra->fw.phys, GFP_KERNEL);
  801. if (!tegra->fw.virt) {
  802. dev_err(tegra->dev, "failed to allocate memory for firmware\n");
  803. release_firmware(fw);
  804. return -ENOMEM;
  805. }
  806. header = (struct tegra_xusb_fw_header *)tegra->fw.virt;
  807. memcpy(tegra->fw.virt, fw->data, tegra->fw.size);
  808. release_firmware(fw);
  809. return 0;
  810. }
  811. static int tegra_xusb_wait_for_falcon(struct tegra_xusb *tegra)
  812. {
  813. struct xhci_cap_regs __iomem *cap_regs;
  814. struct xhci_op_regs __iomem *op_regs;
  815. int ret;
  816. u32 value;
  817. cap_regs = tegra->regs;
  818. op_regs = tegra->regs + HC_LENGTH(readl(&cap_regs->hc_capbase));
  819. ret = readl_poll_timeout(&op_regs->status, value, !(value & STS_CNR), 1000, 200000);
  820. if (ret)
  821. dev_err(tegra->dev, "XHCI Controller not ready. Falcon state: 0x%x\n",
  822. csb_readl(tegra, XUSB_FALC_CPUCTL));
  823. return ret;
  824. }
  825. static int tegra_xusb_load_firmware_rom(struct tegra_xusb *tegra)
  826. {
  827. unsigned int code_tag_blocks, code_size_blocks, code_blocks;
  828. struct tegra_xusb_fw_header *header;
  829. struct device *dev = tegra->dev;
  830. time64_t timestamp;
  831. u64 address;
  832. u32 value;
  833. int err;
  834. header = (struct tegra_xusb_fw_header *)tegra->fw.virt;
  835. if (csb_readl(tegra, XUSB_CSB_MP_ILOAD_BASE_LO) != 0) {
  836. dev_info(dev, "Firmware already loaded, Falcon state %#x\n",
  837. csb_readl(tegra, XUSB_FALC_CPUCTL));
  838. return 0;
  839. }
  840. /* Program the size of DFI into ILOAD_ATTR. */
  841. csb_writel(tegra, tegra->fw.size, XUSB_CSB_MP_ILOAD_ATTR);
  842. /*
  843. * Boot code of the firmware reads the ILOAD_BASE registers
  844. * to get to the start of the DFI in system memory.
  845. */
  846. address = tegra->fw.phys + sizeof(*header);
  847. csb_writel(tegra, address >> 32, XUSB_CSB_MP_ILOAD_BASE_HI);
  848. csb_writel(tegra, address, XUSB_CSB_MP_ILOAD_BASE_LO);
  849. /* Set BOOTPATH to 1 in APMAP. */
  850. csb_writel(tegra, APMAP_BOOTPATH, XUSB_CSB_MP_APMAP);
  851. /* Invalidate L2IMEM. */
  852. csb_writel(tegra, L2IMEMOP_INVALIDATE_ALL, XUSB_CSB_MP_L2IMEMOP_TRIG);
  853. /*
  854. * Initiate fetch of bootcode from system memory into L2IMEM.
  855. * Program bootcode location and size in system memory.
  856. */
  857. code_tag_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codetag),
  858. IMEM_BLOCK_SIZE);
  859. code_size_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codesize),
  860. IMEM_BLOCK_SIZE);
  861. code_blocks = code_tag_blocks + code_size_blocks;
  862. value = ((code_tag_blocks & L2IMEMOP_SIZE_SRC_OFFSET_MASK) <<
  863. L2IMEMOP_SIZE_SRC_OFFSET_SHIFT) |
  864. ((code_size_blocks & L2IMEMOP_SIZE_SRC_COUNT_MASK) <<
  865. L2IMEMOP_SIZE_SRC_COUNT_SHIFT);
  866. csb_writel(tegra, value, XUSB_CSB_MP_L2IMEMOP_SIZE);
  867. /* Trigger L2IMEM load operation. */
  868. csb_writel(tegra, L2IMEMOP_LOAD_LOCKED_RESULT,
  869. XUSB_CSB_MP_L2IMEMOP_TRIG);
  870. /* Setup Falcon auto-fill. */
  871. csb_writel(tegra, code_size_blocks, XUSB_FALC_IMFILLCTL);
  872. value = ((code_tag_blocks & IMFILLRNG1_TAG_MASK) <<
  873. IMFILLRNG1_TAG_LO_SHIFT) |
  874. ((code_blocks & IMFILLRNG1_TAG_MASK) <<
  875. IMFILLRNG1_TAG_HI_SHIFT);
  876. csb_writel(tegra, value, XUSB_FALC_IMFILLRNG1);
  877. csb_writel(tegra, 0, XUSB_FALC_DMACTL);
  878. /* wait for RESULT_VLD to get set */
  879. #define tegra_csb_readl(offset) csb_readl(tegra, offset)
  880. err = readx_poll_timeout(tegra_csb_readl,
  881. XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT, value,
  882. value & L2IMEMOP_RESULT_VLD, 100, 10000);
  883. if (err < 0) {
  884. dev_err(dev, "DMA controller not ready %#010x\n", value);
  885. return err;
  886. }
  887. #undef tegra_csb_readl
  888. csb_writel(tegra, le32_to_cpu(header->boot_codetag),
  889. XUSB_FALC_BOOTVEC);
  890. /* Boot Falcon CPU and wait for USBSTS_CNR to get cleared. */
  891. csb_writel(tegra, CPUCTL_STARTCPU, XUSB_FALC_CPUCTL);
  892. if (tegra_xusb_wait_for_falcon(tegra))
  893. return -EIO;
  894. timestamp = le32_to_cpu(header->fwimg_created_time);
  895. dev_info(dev, "Firmware timestamp: %ptTs UTC\n", &timestamp);
  896. return 0;
  897. }
  898. static u32 tegra_xusb_read_firmware_header(struct tegra_xusb *tegra, u32 offset)
  899. {
  900. /*
  901. * We only accept reading the firmware config table
  902. * The offset should not exceed the fw header structure
  903. */
  904. if (offset >= sizeof(struct tegra_xusb_fw_header))
  905. return 0;
  906. bar2_writel(tegra, (FW_IOCTL_CFGTBL_READ << FW_IOCTL_TYPE_SHIFT) | offset,
  907. XUSB_BAR2_ARU_FW_SCRATCH);
  908. return bar2_readl(tegra, XUSB_BAR2_ARU_SMI_ARU_FW_SCRATCH_DATA0);
  909. }
  910. static int tegra_xusb_init_ifr_firmware(struct tegra_xusb *tegra)
  911. {
  912. time64_t timestamp;
  913. if (tegra_xusb_wait_for_falcon(tegra))
  914. return -EIO;
  915. #define offsetof_32(X, Y) ((u8)(offsetof(X, Y) / sizeof(__le32)))
  916. timestamp = tegra_xusb_read_firmware_header(tegra, offsetof_32(struct tegra_xusb_fw_header,
  917. fwimg_created_time) << 2);
  918. dev_info(tegra->dev, "Firmware timestamp: %ptTs UTC\n", &timestamp);
  919. return 0;
  920. }
  921. static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
  922. {
  923. if (!tegra->soc->firmware)
  924. return tegra_xusb_init_ifr_firmware(tegra);
  925. else
  926. return tegra_xusb_load_firmware_rom(tegra);
  927. }
  928. static void tegra_xusb_powerdomain_remove(struct device *dev,
  929. struct tegra_xusb *tegra)
  930. {
  931. if (!tegra->use_genpd)
  932. return;
  933. if (!IS_ERR_OR_NULL(tegra->genpd_dev_ss))
  934. dev_pm_domain_detach(tegra->genpd_dev_ss, true);
  935. if (!IS_ERR_OR_NULL(tegra->genpd_dev_host))
  936. dev_pm_domain_detach(tegra->genpd_dev_host, true);
  937. }
  938. static int tegra_xusb_powerdomain_init(struct device *dev,
  939. struct tegra_xusb *tegra)
  940. {
  941. int err;
  942. tegra->genpd_dev_host = dev_pm_domain_attach_by_name(dev, "xusb_host");
  943. if (IS_ERR(tegra->genpd_dev_host)) {
  944. err = PTR_ERR(tegra->genpd_dev_host);
  945. dev_err(dev, "failed to get host pm-domain: %d\n", err);
  946. return err;
  947. }
  948. tegra->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "xusb_ss");
  949. if (IS_ERR(tegra->genpd_dev_ss)) {
  950. err = PTR_ERR(tegra->genpd_dev_ss);
  951. dev_err(dev, "failed to get superspeed pm-domain: %d\n", err);
  952. return err;
  953. }
  954. tegra->use_genpd = true;
  955. return 0;
  956. }
  957. static int tegra_xusb_unpowergate_partitions(struct tegra_xusb *tegra)
  958. {
  959. struct device *dev = tegra->dev;
  960. int rc;
  961. if (tegra->use_genpd) {
  962. rc = pm_runtime_resume_and_get(tegra->genpd_dev_ss);
  963. if (rc < 0) {
  964. dev_err(dev, "failed to enable XUSB SS partition\n");
  965. return rc;
  966. }
  967. rc = pm_runtime_resume_and_get(tegra->genpd_dev_host);
  968. if (rc < 0) {
  969. dev_err(dev, "failed to enable XUSB Host partition\n");
  970. pm_runtime_put_sync(tegra->genpd_dev_ss);
  971. return rc;
  972. }
  973. } else {
  974. rc = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBA,
  975. tegra->ss_clk,
  976. tegra->ss_rst);
  977. if (rc < 0) {
  978. dev_err(dev, "failed to enable XUSB SS partition\n");
  979. return rc;
  980. }
  981. rc = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC,
  982. tegra->host_clk,
  983. tegra->host_rst);
  984. if (rc < 0) {
  985. dev_err(dev, "failed to enable XUSB Host partition\n");
  986. tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
  987. return rc;
  988. }
  989. }
  990. return 0;
  991. }
  992. static int tegra_xusb_powergate_partitions(struct tegra_xusb *tegra)
  993. {
  994. struct device *dev = tegra->dev;
  995. int rc;
  996. if (tegra->use_genpd) {
  997. rc = pm_runtime_put_sync(tegra->genpd_dev_host);
  998. if (rc < 0) {
  999. dev_err(dev, "failed to disable XUSB Host partition\n");
  1000. return rc;
  1001. }
  1002. rc = pm_runtime_put_sync(tegra->genpd_dev_ss);
  1003. if (rc < 0) {
  1004. dev_err(dev, "failed to disable XUSB SS partition\n");
  1005. pm_runtime_get_sync(tegra->genpd_dev_host);
  1006. return rc;
  1007. }
  1008. } else {
  1009. rc = tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC);
  1010. if (rc < 0) {
  1011. dev_err(dev, "failed to disable XUSB Host partition\n");
  1012. return rc;
  1013. }
  1014. rc = tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
  1015. if (rc < 0) {
  1016. dev_err(dev, "failed to disable XUSB SS partition\n");
  1017. tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC,
  1018. tegra->host_clk,
  1019. tegra->host_rst);
  1020. return rc;
  1021. }
  1022. }
  1023. return 0;
  1024. }
  1025. static int __tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra)
  1026. {
  1027. struct tegra_xusb_mbox_msg msg;
  1028. int err;
  1029. /* Enable firmware messages from controller. */
  1030. msg.cmd = MBOX_CMD_MSG_ENABLED;
  1031. msg.data = 0;
  1032. err = tegra_xusb_mbox_send(tegra, &msg);
  1033. if (err < 0)
  1034. dev_err(tegra->dev, "failed to enable messages: %d\n", err);
  1035. return err;
  1036. }
  1037. static irqreturn_t tegra_xusb_padctl_irq(int irq, void *data)
  1038. {
  1039. struct tegra_xusb *tegra = data;
  1040. mutex_lock(&tegra->lock);
  1041. if (tegra->suspended) {
  1042. mutex_unlock(&tegra->lock);
  1043. return IRQ_HANDLED;
  1044. }
  1045. mutex_unlock(&tegra->lock);
  1046. pm_runtime_resume(tegra->dev);
  1047. return IRQ_HANDLED;
  1048. }
  1049. static int tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra)
  1050. {
  1051. int err;
  1052. mutex_lock(&tegra->lock);
  1053. err = __tegra_xusb_enable_firmware_messages(tegra);
  1054. mutex_unlock(&tegra->lock);
  1055. return err;
  1056. }
  1057. static void tegra_xhci_set_port_power(struct tegra_xusb *tegra, bool main,
  1058. bool set)
  1059. {
  1060. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1061. struct usb_hcd *hcd = main ? xhci->main_hcd : xhci->shared_hcd;
  1062. unsigned int wait = (!main && !set) ? 1000 : 10;
  1063. u16 typeReq = set ? SetPortFeature : ClearPortFeature;
  1064. u16 wIndex = main ? tegra->otg_usb2_port + 1 : tegra->otg_usb3_port + 1;
  1065. u32 status;
  1066. u32 stat_power = main ? USB_PORT_STAT_POWER : USB_SS_PORT_STAT_POWER;
  1067. u32 status_val = set ? stat_power : 0;
  1068. dev_dbg(tegra->dev, "%s():%s %s port power\n", __func__,
  1069. set ? "set" : "clear", main ? "HS" : "SS");
  1070. hcd->driver->hub_control(hcd, typeReq, USB_PORT_FEAT_POWER, wIndex,
  1071. NULL, 0);
  1072. do {
  1073. tegra_xhci_hc_driver.hub_control(hcd, GetPortStatus, 0, wIndex,
  1074. (char *) &status, sizeof(status));
  1075. if (status_val == (status & stat_power))
  1076. break;
  1077. if (!main && !set)
  1078. usleep_range(600, 700);
  1079. else
  1080. usleep_range(10, 20);
  1081. } while (--wait > 0);
  1082. if (status_val != (status & stat_power))
  1083. dev_info(tegra->dev, "failed to %s %s PP %d\n",
  1084. set ? "set" : "clear",
  1085. main ? "HS" : "SS", status);
  1086. }
  1087. static struct phy *tegra_xusb_get_phy(struct tegra_xusb *tegra, char *name,
  1088. int port)
  1089. {
  1090. unsigned int i, phy_count = 0;
  1091. for (i = 0; i < tegra->soc->num_types; i++) {
  1092. if (!strncmp(tegra->soc->phy_types[i].name, name,
  1093. strlen(name)))
  1094. return tegra->phys[phy_count+port];
  1095. phy_count += tegra->soc->phy_types[i].num;
  1096. }
  1097. return NULL;
  1098. }
  1099. static void tegra_xhci_id_work(struct work_struct *work)
  1100. {
  1101. struct tegra_xusb *tegra = container_of(work, struct tegra_xusb,
  1102. id_work);
  1103. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1104. struct tegra_xusb_mbox_msg msg;
  1105. struct phy *phy = tegra_xusb_get_phy(tegra, "usb2",
  1106. tegra->otg_usb2_port);
  1107. u32 status;
  1108. int ret;
  1109. dev_dbg(tegra->dev, "host mode %s\n", tegra->host_mode ? "on" : "off");
  1110. mutex_lock(&tegra->lock);
  1111. if (tegra->host_mode)
  1112. phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_HOST);
  1113. else
  1114. phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_NONE);
  1115. mutex_unlock(&tegra->lock);
  1116. tegra->otg_usb3_port = tegra_xusb_padctl_get_usb3_companion(tegra->padctl,
  1117. tegra->otg_usb2_port);
  1118. if (tegra->host_mode) {
  1119. /* switch to host mode */
  1120. if (tegra->otg_usb3_port >= 0) {
  1121. if (tegra->soc->otg_reset_sspi) {
  1122. /* set PP=0 */
  1123. tegra_xhci_hc_driver.hub_control(
  1124. xhci->shared_hcd, GetPortStatus,
  1125. 0, tegra->otg_usb3_port+1,
  1126. (char *) &status, sizeof(status));
  1127. if (status & USB_SS_PORT_STAT_POWER)
  1128. tegra_xhci_set_port_power(tegra, false,
  1129. false);
  1130. /* reset OTG port SSPI */
  1131. msg.cmd = MBOX_CMD_RESET_SSPI;
  1132. msg.data = tegra->otg_usb3_port+1;
  1133. ret = tegra_xusb_mbox_send(tegra, &msg);
  1134. if (ret < 0) {
  1135. dev_info(tegra->dev,
  1136. "failed to RESET_SSPI %d\n",
  1137. ret);
  1138. }
  1139. }
  1140. tegra_xhci_set_port_power(tegra, false, true);
  1141. }
  1142. tegra_xhci_set_port_power(tegra, true, true);
  1143. } else {
  1144. if (tegra->otg_usb3_port >= 0)
  1145. tegra_xhci_set_port_power(tegra, false, false);
  1146. tegra_xhci_set_port_power(tegra, true, false);
  1147. }
  1148. }
  1149. #if IS_ENABLED(CONFIG_PM) || IS_ENABLED(CONFIG_PM_SLEEP)
  1150. static bool is_usb2_otg_phy(struct tegra_xusb *tegra, unsigned int index)
  1151. {
  1152. return (tegra->usbphy[index] != NULL);
  1153. }
  1154. static bool is_usb3_otg_phy(struct tegra_xusb *tegra, unsigned int index)
  1155. {
  1156. struct tegra_xusb_padctl *padctl = tegra->padctl;
  1157. unsigned int i;
  1158. int port;
  1159. for (i = 0; i < tegra->num_usb_phys; i++) {
  1160. if (is_usb2_otg_phy(tegra, i)) {
  1161. port = tegra_xusb_padctl_get_usb3_companion(padctl, i);
  1162. if ((port >= 0) && (index == (unsigned int)port))
  1163. return true;
  1164. }
  1165. }
  1166. return false;
  1167. }
  1168. static bool is_host_mode_phy(struct tegra_xusb *tegra, unsigned int phy_type, unsigned int index)
  1169. {
  1170. if (strcmp(tegra->soc->phy_types[phy_type].name, "hsic") == 0)
  1171. return true;
  1172. if (strcmp(tegra->soc->phy_types[phy_type].name, "usb2") == 0) {
  1173. if (is_usb2_otg_phy(tegra, index))
  1174. return ((index == tegra->otg_usb2_port) && tegra->host_mode);
  1175. else
  1176. return true;
  1177. }
  1178. if (strcmp(tegra->soc->phy_types[phy_type].name, "usb3") == 0) {
  1179. if (is_usb3_otg_phy(tegra, index))
  1180. return ((index == tegra->otg_usb3_port) && tegra->host_mode);
  1181. else
  1182. return true;
  1183. }
  1184. return false;
  1185. }
  1186. #endif
  1187. static int tegra_xusb_get_usb2_port(struct tegra_xusb *tegra,
  1188. struct usb_phy *usbphy)
  1189. {
  1190. unsigned int i;
  1191. for (i = 0; i < tegra->num_usb_phys; i++) {
  1192. if (tegra->usbphy[i] && usbphy == tegra->usbphy[i])
  1193. return i;
  1194. }
  1195. return -1;
  1196. }
  1197. static int tegra_xhci_id_notify(struct notifier_block *nb,
  1198. unsigned long action, void *data)
  1199. {
  1200. struct tegra_xusb *tegra = container_of(nb, struct tegra_xusb,
  1201. id_nb);
  1202. struct usb_phy *usbphy = (struct usb_phy *)data;
  1203. dev_dbg(tegra->dev, "%s(): action is %d", __func__, usbphy->last_event);
  1204. if ((tegra->host_mode && usbphy->last_event == USB_EVENT_ID) ||
  1205. (!tegra->host_mode && usbphy->last_event != USB_EVENT_ID)) {
  1206. dev_dbg(tegra->dev, "Same role(%d) received. Ignore",
  1207. tegra->host_mode);
  1208. return NOTIFY_OK;
  1209. }
  1210. tegra->otg_usb2_port = tegra_xusb_get_usb2_port(tegra, usbphy);
  1211. tegra->host_mode = (usbphy->last_event == USB_EVENT_ID) ? true : false;
  1212. schedule_work(&tegra->id_work);
  1213. return NOTIFY_OK;
  1214. }
  1215. static int tegra_xusb_init_usb_phy(struct tegra_xusb *tegra)
  1216. {
  1217. unsigned int i;
  1218. tegra->usbphy = devm_kcalloc(tegra->dev, tegra->num_usb_phys,
  1219. sizeof(*tegra->usbphy), GFP_KERNEL);
  1220. if (!tegra->usbphy)
  1221. return -ENOMEM;
  1222. INIT_WORK(&tegra->id_work, tegra_xhci_id_work);
  1223. tegra->id_nb.notifier_call = tegra_xhci_id_notify;
  1224. tegra->otg_usb2_port = -EINVAL;
  1225. tegra->otg_usb3_port = -EINVAL;
  1226. for (i = 0; i < tegra->num_usb_phys; i++) {
  1227. struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", i);
  1228. if (!phy)
  1229. continue;
  1230. tegra->usbphy[i] = devm_usb_get_phy_by_node(tegra->dev,
  1231. phy->dev.of_node,
  1232. &tegra->id_nb);
  1233. if (!IS_ERR(tegra->usbphy[i])) {
  1234. dev_dbg(tegra->dev, "usbphy-%d registered", i);
  1235. otg_set_host(tegra->usbphy[i]->otg, &tegra->hcd->self);
  1236. } else {
  1237. /*
  1238. * usb-phy is optional, continue if its not available.
  1239. */
  1240. tegra->usbphy[i] = NULL;
  1241. }
  1242. }
  1243. return 0;
  1244. }
  1245. static void tegra_xusb_deinit_usb_phy(struct tegra_xusb *tegra)
  1246. {
  1247. unsigned int i;
  1248. cancel_work_sync(&tegra->id_work);
  1249. for (i = 0; i < tegra->num_usb_phys; i++)
  1250. if (tegra->usbphy[i])
  1251. otg_set_host(tegra->usbphy[i]->otg, NULL);
  1252. }
  1253. static int tegra_xusb_probe(struct platform_device *pdev)
  1254. {
  1255. struct tegra_xusb *tegra;
  1256. struct device_node *np;
  1257. struct resource *regs;
  1258. struct xhci_hcd *xhci;
  1259. unsigned int i, j, k;
  1260. struct phy *phy;
  1261. int err;
  1262. BUILD_BUG_ON(sizeof(struct tegra_xusb_fw_header) != 256);
  1263. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  1264. if (!tegra)
  1265. return -ENOMEM;
  1266. tegra->soc = of_device_get_match_data(&pdev->dev);
  1267. mutex_init(&tegra->lock);
  1268. tegra->dev = &pdev->dev;
  1269. err = tegra_xusb_init_context(tegra);
  1270. if (err < 0)
  1271. return err;
  1272. tegra->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &regs);
  1273. if (IS_ERR(tegra->regs))
  1274. return PTR_ERR(tegra->regs);
  1275. tegra->fpci_base = devm_platform_ioremap_resource(pdev, 1);
  1276. if (IS_ERR(tegra->fpci_base))
  1277. return PTR_ERR(tegra->fpci_base);
  1278. if (tegra->soc->has_ipfs) {
  1279. tegra->ipfs_base = devm_platform_ioremap_resource(pdev, 2);
  1280. if (IS_ERR(tegra->ipfs_base))
  1281. return PTR_ERR(tegra->ipfs_base);
  1282. } else if (tegra->soc->has_bar2) {
  1283. tegra->bar2_base = devm_platform_get_and_ioremap_resource(pdev, 2, &tegra->bar2);
  1284. if (IS_ERR(tegra->bar2_base))
  1285. return PTR_ERR(tegra->bar2_base);
  1286. }
  1287. tegra->xhci_irq = platform_get_irq(pdev, 0);
  1288. if (tegra->xhci_irq < 0)
  1289. return tegra->xhci_irq;
  1290. tegra->mbox_irq = platform_get_irq(pdev, 1);
  1291. if (tegra->mbox_irq < 0)
  1292. return tegra->mbox_irq;
  1293. tegra->padctl = tegra_xusb_padctl_get(&pdev->dev);
  1294. if (IS_ERR(tegra->padctl))
  1295. return PTR_ERR(tegra->padctl);
  1296. np = of_parse_phandle(pdev->dev.of_node, "nvidia,xusb-padctl", 0);
  1297. if (!np) {
  1298. err = -ENODEV;
  1299. goto put_padctl;
  1300. }
  1301. tegra->padctl_irq = of_irq_get(np, 0);
  1302. if (tegra->padctl_irq == -EPROBE_DEFER) {
  1303. err = tegra->padctl_irq;
  1304. goto put_padctl;
  1305. } else if (tegra->padctl_irq <= 0) {
  1306. /* Older device-trees don't have padctrl interrupt */
  1307. tegra->padctl_irq = 0;
  1308. dev_dbg(&pdev->dev,
  1309. "%pOF is missing an interrupt, disabling PM support\n", np);
  1310. }
  1311. tegra->host_clk = devm_clk_get(&pdev->dev, "xusb_host");
  1312. if (IS_ERR(tegra->host_clk)) {
  1313. err = PTR_ERR(tegra->host_clk);
  1314. dev_err(&pdev->dev, "failed to get xusb_host: %d\n", err);
  1315. goto put_padctl;
  1316. }
  1317. tegra->falcon_clk = devm_clk_get(&pdev->dev, "xusb_falcon_src");
  1318. if (IS_ERR(tegra->falcon_clk)) {
  1319. err = PTR_ERR(tegra->falcon_clk);
  1320. dev_err(&pdev->dev, "failed to get xusb_falcon_src: %d\n", err);
  1321. goto put_padctl;
  1322. }
  1323. tegra->ss_clk = devm_clk_get(&pdev->dev, "xusb_ss");
  1324. if (IS_ERR(tegra->ss_clk)) {
  1325. err = PTR_ERR(tegra->ss_clk);
  1326. dev_err(&pdev->dev, "failed to get xusb_ss: %d\n", err);
  1327. goto put_padctl;
  1328. }
  1329. tegra->ss_src_clk = devm_clk_get(&pdev->dev, "xusb_ss_src");
  1330. if (IS_ERR(tegra->ss_src_clk)) {
  1331. err = PTR_ERR(tegra->ss_src_clk);
  1332. dev_err(&pdev->dev, "failed to get xusb_ss_src: %d\n", err);
  1333. goto put_padctl;
  1334. }
  1335. tegra->hs_src_clk = devm_clk_get(&pdev->dev, "xusb_hs_src");
  1336. if (IS_ERR(tegra->hs_src_clk)) {
  1337. err = PTR_ERR(tegra->hs_src_clk);
  1338. dev_err(&pdev->dev, "failed to get xusb_hs_src: %d\n", err);
  1339. goto put_padctl;
  1340. }
  1341. tegra->fs_src_clk = devm_clk_get(&pdev->dev, "xusb_fs_src");
  1342. if (IS_ERR(tegra->fs_src_clk)) {
  1343. err = PTR_ERR(tegra->fs_src_clk);
  1344. dev_err(&pdev->dev, "failed to get xusb_fs_src: %d\n", err);
  1345. goto put_padctl;
  1346. }
  1347. tegra->pll_u_480m = devm_clk_get(&pdev->dev, "pll_u_480m");
  1348. if (IS_ERR(tegra->pll_u_480m)) {
  1349. err = PTR_ERR(tegra->pll_u_480m);
  1350. dev_err(&pdev->dev, "failed to get pll_u_480m: %d\n", err);
  1351. goto put_padctl;
  1352. }
  1353. tegra->clk_m = devm_clk_get(&pdev->dev, "clk_m");
  1354. if (IS_ERR(tegra->clk_m)) {
  1355. err = PTR_ERR(tegra->clk_m);
  1356. dev_err(&pdev->dev, "failed to get clk_m: %d\n", err);
  1357. goto put_padctl;
  1358. }
  1359. tegra->pll_e = devm_clk_get(&pdev->dev, "pll_e");
  1360. if (IS_ERR(tegra->pll_e)) {
  1361. err = PTR_ERR(tegra->pll_e);
  1362. dev_err(&pdev->dev, "failed to get pll_e: %d\n", err);
  1363. goto put_padctl;
  1364. }
  1365. if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) {
  1366. tegra->host_rst = devm_reset_control_get(&pdev->dev,
  1367. "xusb_host");
  1368. if (IS_ERR(tegra->host_rst)) {
  1369. err = PTR_ERR(tegra->host_rst);
  1370. dev_err(&pdev->dev,
  1371. "failed to get xusb_host reset: %d\n", err);
  1372. goto put_padctl;
  1373. }
  1374. tegra->ss_rst = devm_reset_control_get(&pdev->dev, "xusb_ss");
  1375. if (IS_ERR(tegra->ss_rst)) {
  1376. err = PTR_ERR(tegra->ss_rst);
  1377. dev_err(&pdev->dev, "failed to get xusb_ss reset: %d\n",
  1378. err);
  1379. goto put_padctl;
  1380. }
  1381. } else {
  1382. err = tegra_xusb_powerdomain_init(&pdev->dev, tegra);
  1383. if (err)
  1384. goto put_powerdomains;
  1385. }
  1386. tegra->supplies = devm_kcalloc(&pdev->dev, tegra->soc->num_supplies,
  1387. sizeof(*tegra->supplies), GFP_KERNEL);
  1388. if (!tegra->supplies) {
  1389. err = -ENOMEM;
  1390. goto put_powerdomains;
  1391. }
  1392. regulator_bulk_set_supply_names(tegra->supplies,
  1393. tegra->soc->supply_names,
  1394. tegra->soc->num_supplies);
  1395. err = devm_regulator_bulk_get(&pdev->dev, tegra->soc->num_supplies,
  1396. tegra->supplies);
  1397. if (err) {
  1398. dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
  1399. goto put_powerdomains;
  1400. }
  1401. for (i = 0; i < tegra->soc->num_types; i++) {
  1402. if (!strncmp(tegra->soc->phy_types[i].name, "usb2", 4))
  1403. tegra->num_usb_phys = tegra->soc->phy_types[i].num;
  1404. tegra->num_phys += tegra->soc->phy_types[i].num;
  1405. }
  1406. tegra->phys = devm_kcalloc(&pdev->dev, tegra->num_phys,
  1407. sizeof(*tegra->phys), GFP_KERNEL);
  1408. if (!tegra->phys) {
  1409. err = -ENOMEM;
  1410. goto put_powerdomains;
  1411. }
  1412. for (i = 0, k = 0; i < tegra->soc->num_types; i++) {
  1413. char prop[8];
  1414. for (j = 0; j < tegra->soc->phy_types[i].num; j++) {
  1415. snprintf(prop, sizeof(prop), "%s-%d",
  1416. tegra->soc->phy_types[i].name, j);
  1417. phy = devm_phy_optional_get(&pdev->dev, prop);
  1418. if (IS_ERR(phy)) {
  1419. dev_err(&pdev->dev,
  1420. "failed to get PHY %s: %ld\n", prop,
  1421. PTR_ERR(phy));
  1422. err = PTR_ERR(phy);
  1423. goto put_powerdomains;
  1424. }
  1425. tegra->phys[k++] = phy;
  1426. }
  1427. }
  1428. tegra->hcd = usb_create_hcd(&tegra_xhci_hc_driver, &pdev->dev,
  1429. dev_name(&pdev->dev));
  1430. if (!tegra->hcd) {
  1431. err = -ENOMEM;
  1432. goto put_powerdomains;
  1433. }
  1434. tegra->hcd->skip_phy_initialization = 1;
  1435. tegra->hcd->regs = tegra->regs;
  1436. tegra->hcd->rsrc_start = regs->start;
  1437. tegra->hcd->rsrc_len = resource_size(regs);
  1438. /*
  1439. * This must happen after usb_create_hcd(), because usb_create_hcd()
  1440. * will overwrite the drvdata of the device with the hcd it creates.
  1441. */
  1442. platform_set_drvdata(pdev, tegra);
  1443. err = tegra_xusb_clk_enable(tegra);
  1444. if (err) {
  1445. dev_err(tegra->dev, "failed to enable clocks: %d\n", err);
  1446. goto put_hcd;
  1447. }
  1448. err = regulator_bulk_enable(tegra->soc->num_supplies, tegra->supplies);
  1449. if (err) {
  1450. dev_err(tegra->dev, "failed to enable regulators: %d\n", err);
  1451. goto disable_clk;
  1452. }
  1453. err = tegra_xusb_phy_enable(tegra);
  1454. if (err < 0) {
  1455. dev_err(&pdev->dev, "failed to enable PHYs: %d\n", err);
  1456. goto disable_regulator;
  1457. }
  1458. /*
  1459. * The XUSB Falcon microcontroller can only address 40 bits, so set
  1460. * the DMA mask accordingly.
  1461. */
  1462. err = dma_set_mask_and_coherent(tegra->dev, DMA_BIT_MASK(40));
  1463. if (err < 0) {
  1464. dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err);
  1465. goto disable_phy;
  1466. }
  1467. if (tegra->soc->firmware) {
  1468. err = tegra_xusb_request_firmware(tegra);
  1469. if (err < 0) {
  1470. dev_err(&pdev->dev,
  1471. "failed to request firmware: %d\n", err);
  1472. goto disable_phy;
  1473. }
  1474. }
  1475. err = tegra_xusb_unpowergate_partitions(tegra);
  1476. if (err)
  1477. goto free_firmware;
  1478. tegra_xusb_config(tegra);
  1479. err = tegra_xusb_load_firmware(tegra);
  1480. if (err < 0) {
  1481. dev_err(&pdev->dev, "failed to load firmware: %d\n", err);
  1482. goto powergate;
  1483. }
  1484. err = usb_add_hcd(tegra->hcd, tegra->xhci_irq, IRQF_SHARED);
  1485. if (err < 0) {
  1486. dev_err(&pdev->dev, "failed to add USB HCD: %d\n", err);
  1487. goto powergate;
  1488. }
  1489. device_wakeup_enable(tegra->hcd->self.controller);
  1490. xhci = hcd_to_xhci(tegra->hcd);
  1491. xhci->shared_hcd = usb_create_shared_hcd(&tegra_xhci_hc_driver,
  1492. &pdev->dev,
  1493. dev_name(&pdev->dev),
  1494. tegra->hcd);
  1495. if (!xhci->shared_hcd) {
  1496. dev_err(&pdev->dev, "failed to create shared HCD\n");
  1497. err = -ENOMEM;
  1498. goto remove_usb2;
  1499. }
  1500. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  1501. xhci->shared_hcd->can_do_streams = 1;
  1502. err = usb_add_hcd(xhci->shared_hcd, tegra->xhci_irq, IRQF_SHARED);
  1503. if (err < 0) {
  1504. dev_err(&pdev->dev, "failed to add shared HCD: %d\n", err);
  1505. goto put_usb3;
  1506. }
  1507. err = devm_request_threaded_irq(&pdev->dev, tegra->mbox_irq,
  1508. tegra_xusb_mbox_irq,
  1509. tegra_xusb_mbox_thread, 0,
  1510. dev_name(&pdev->dev), tegra);
  1511. if (err < 0) {
  1512. dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
  1513. goto remove_usb3;
  1514. }
  1515. if (tegra->padctl_irq) {
  1516. err = devm_request_threaded_irq(&pdev->dev, tegra->padctl_irq,
  1517. NULL, tegra_xusb_padctl_irq,
  1518. IRQF_ONESHOT, dev_name(&pdev->dev),
  1519. tegra);
  1520. if (err < 0) {
  1521. dev_err(&pdev->dev, "failed to request padctl IRQ: %d\n", err);
  1522. goto remove_usb3;
  1523. }
  1524. }
  1525. err = tegra_xusb_enable_firmware_messages(tegra);
  1526. if (err < 0) {
  1527. dev_err(&pdev->dev, "failed to enable messages: %d\n", err);
  1528. goto remove_usb3;
  1529. }
  1530. err = tegra_xusb_init_usb_phy(tegra);
  1531. if (err < 0) {
  1532. dev_err(&pdev->dev, "failed to init USB PHY: %d\n", err);
  1533. goto remove_usb3;
  1534. }
  1535. /* Enable wake for both USB 2.0 and USB 3.0 roothubs */
  1536. device_init_wakeup(&tegra->hcd->self.root_hub->dev, true);
  1537. device_init_wakeup(&xhci->shared_hcd->self.root_hub->dev, true);
  1538. pm_runtime_use_autosuspend(tegra->dev);
  1539. pm_runtime_set_autosuspend_delay(tegra->dev, 2000);
  1540. pm_runtime_mark_last_busy(tegra->dev);
  1541. pm_runtime_set_active(tegra->dev);
  1542. if (tegra->padctl_irq) {
  1543. device_init_wakeup(tegra->dev, true);
  1544. pm_runtime_enable(tegra->dev);
  1545. }
  1546. return 0;
  1547. remove_usb3:
  1548. usb_remove_hcd(xhci->shared_hcd);
  1549. put_usb3:
  1550. usb_put_hcd(xhci->shared_hcd);
  1551. remove_usb2:
  1552. usb_remove_hcd(tegra->hcd);
  1553. powergate:
  1554. tegra_xusb_powergate_partitions(tegra);
  1555. free_firmware:
  1556. dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt,
  1557. tegra->fw.phys);
  1558. disable_phy:
  1559. tegra_xusb_phy_disable(tegra);
  1560. disable_regulator:
  1561. regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies);
  1562. disable_clk:
  1563. tegra_xusb_clk_disable(tegra);
  1564. put_hcd:
  1565. usb_put_hcd(tegra->hcd);
  1566. put_powerdomains:
  1567. tegra_xusb_powerdomain_remove(&pdev->dev, tegra);
  1568. put_padctl:
  1569. of_node_put(np);
  1570. tegra_xusb_padctl_put(tegra->padctl);
  1571. return err;
  1572. }
  1573. static void tegra_xusb_disable(struct tegra_xusb *tegra)
  1574. {
  1575. tegra_xusb_powergate_partitions(tegra);
  1576. tegra_xusb_powerdomain_remove(tegra->dev, tegra);
  1577. tegra_xusb_phy_disable(tegra);
  1578. tegra_xusb_clk_disable(tegra);
  1579. regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies);
  1580. }
  1581. static void tegra_xusb_remove(struct platform_device *pdev)
  1582. {
  1583. struct tegra_xusb *tegra = platform_get_drvdata(pdev);
  1584. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1585. tegra_xusb_deinit_usb_phy(tegra);
  1586. pm_runtime_get_sync(&pdev->dev);
  1587. usb_remove_hcd(xhci->shared_hcd);
  1588. usb_put_hcd(xhci->shared_hcd);
  1589. xhci->shared_hcd = NULL;
  1590. usb_remove_hcd(tegra->hcd);
  1591. usb_put_hcd(tegra->hcd);
  1592. dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt,
  1593. tegra->fw.phys);
  1594. if (tegra->padctl_irq)
  1595. pm_runtime_disable(&pdev->dev);
  1596. pm_runtime_put(&pdev->dev);
  1597. tegra_xusb_disable(tegra);
  1598. tegra_xusb_padctl_put(tegra->padctl);
  1599. }
  1600. static void tegra_xusb_shutdown(struct platform_device *pdev)
  1601. {
  1602. struct tegra_xusb *tegra = platform_get_drvdata(pdev);
  1603. pm_runtime_get_sync(&pdev->dev);
  1604. disable_irq(tegra->xhci_irq);
  1605. xhci_shutdown(tegra->hcd);
  1606. tegra_xusb_disable(tegra);
  1607. }
  1608. static bool xhci_hub_ports_suspended(struct xhci_hub *hub)
  1609. {
  1610. struct device *dev = hub->hcd->self.controller;
  1611. bool status = true;
  1612. unsigned int i;
  1613. u32 value;
  1614. for (i = 0; i < hub->num_ports; i++) {
  1615. value = readl(hub->ports[i]->addr);
  1616. if ((value & PORT_PE) == 0)
  1617. continue;
  1618. if ((value & PORT_PLS_MASK) != XDEV_U3) {
  1619. dev_info(dev, "%u-%u isn't suspended: %#010x\n",
  1620. hub->hcd->self.busnum, i + 1, value);
  1621. status = false;
  1622. }
  1623. }
  1624. return status;
  1625. }
  1626. static int tegra_xusb_check_ports(struct tegra_xusb *tegra)
  1627. {
  1628. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1629. struct xhci_bus_state *bus_state = &xhci->usb2_rhub.bus_state;
  1630. unsigned long flags;
  1631. int err = 0;
  1632. if (bus_state->bus_suspended) {
  1633. /* xusb_hub_suspend() has just directed one or more USB2 port(s)
  1634. * to U3 state, it takes 3ms to enter U3.
  1635. */
  1636. usleep_range(3000, 4000);
  1637. }
  1638. spin_lock_irqsave(&xhci->lock, flags);
  1639. if (!xhci_hub_ports_suspended(&xhci->usb2_rhub) ||
  1640. !xhci_hub_ports_suspended(&xhci->usb3_rhub))
  1641. err = -EBUSY;
  1642. spin_unlock_irqrestore(&xhci->lock, flags);
  1643. return err;
  1644. }
  1645. static void tegra_xusb_save_context(struct tegra_xusb *tegra)
  1646. {
  1647. const struct tegra_xusb_context_soc *soc = tegra->soc->context;
  1648. struct tegra_xusb_context *ctx = &tegra->context;
  1649. unsigned int i;
  1650. if (soc->ipfs.num_offsets > 0) {
  1651. for (i = 0; i < soc->ipfs.num_offsets; i++)
  1652. ctx->ipfs[i] = ipfs_readl(tegra, soc->ipfs.offsets[i]);
  1653. }
  1654. if (soc->fpci.num_offsets > 0) {
  1655. for (i = 0; i < soc->fpci.num_offsets; i++)
  1656. ctx->fpci[i] = fpci_readl(tegra, soc->fpci.offsets[i]);
  1657. }
  1658. }
  1659. static void tegra_xusb_restore_context(struct tegra_xusb *tegra)
  1660. {
  1661. const struct tegra_xusb_context_soc *soc = tegra->soc->context;
  1662. struct tegra_xusb_context *ctx = &tegra->context;
  1663. unsigned int i;
  1664. if (soc->fpci.num_offsets > 0) {
  1665. for (i = 0; i < soc->fpci.num_offsets; i++)
  1666. fpci_writel(tegra, ctx->fpci[i], soc->fpci.offsets[i]);
  1667. }
  1668. if (soc->ipfs.num_offsets > 0) {
  1669. for (i = 0; i < soc->ipfs.num_offsets; i++)
  1670. ipfs_writel(tegra, ctx->ipfs[i], soc->ipfs.offsets[i]);
  1671. }
  1672. }
  1673. static enum usb_device_speed tegra_xhci_portsc_to_speed(struct tegra_xusb *tegra, u32 portsc)
  1674. {
  1675. if (DEV_LOWSPEED(portsc))
  1676. return USB_SPEED_LOW;
  1677. if (DEV_HIGHSPEED(portsc))
  1678. return USB_SPEED_HIGH;
  1679. if (DEV_FULLSPEED(portsc))
  1680. return USB_SPEED_FULL;
  1681. if (DEV_SUPERSPEED_ANY(portsc))
  1682. return USB_SPEED_SUPER;
  1683. return USB_SPEED_UNKNOWN;
  1684. }
  1685. static void tegra_xhci_enable_phy_sleepwalk_wake(struct tegra_xusb *tegra)
  1686. {
  1687. struct tegra_xusb_padctl *padctl = tegra->padctl;
  1688. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1689. enum usb_device_speed speed;
  1690. struct phy *phy;
  1691. unsigned int index, offset;
  1692. unsigned int i, j, k;
  1693. struct xhci_hub *rhub;
  1694. u32 portsc;
  1695. for (i = 0, k = 0; i < tegra->soc->num_types; i++) {
  1696. if (strcmp(tegra->soc->phy_types[i].name, "usb3") == 0)
  1697. rhub = &xhci->usb3_rhub;
  1698. else
  1699. rhub = &xhci->usb2_rhub;
  1700. if (strcmp(tegra->soc->phy_types[i].name, "hsic") == 0)
  1701. offset = tegra->soc->ports.usb2.count;
  1702. else
  1703. offset = 0;
  1704. for (j = 0; j < tegra->soc->phy_types[i].num; j++) {
  1705. phy = tegra->phys[k++];
  1706. if (!phy)
  1707. continue;
  1708. index = j + offset;
  1709. if (index >= rhub->num_ports)
  1710. continue;
  1711. if (!is_host_mode_phy(tegra, i, j))
  1712. continue;
  1713. portsc = readl(rhub->ports[index]->addr);
  1714. speed = tegra_xhci_portsc_to_speed(tegra, portsc);
  1715. tegra_xusb_padctl_enable_phy_sleepwalk(padctl, phy, speed);
  1716. tegra_xusb_padctl_enable_phy_wake(padctl, phy);
  1717. }
  1718. }
  1719. }
  1720. static void tegra_xhci_disable_phy_wake(struct tegra_xusb *tegra)
  1721. {
  1722. struct tegra_xusb_padctl *padctl = tegra->padctl;
  1723. unsigned int i;
  1724. for (i = 0; i < tegra->num_usb_phys; i++) {
  1725. struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", i);
  1726. if (!phy)
  1727. continue;
  1728. if (tegra_xusb_padctl_remote_wake_detected(padctl, phy))
  1729. tegra_phy_xusb_utmi_pad_power_on(phy);
  1730. }
  1731. for (i = 0; i < tegra->num_phys; i++) {
  1732. if (!tegra->phys[i])
  1733. continue;
  1734. if (tegra_xusb_padctl_remote_wake_detected(padctl, tegra->phys[i]))
  1735. dev_dbg(tegra->dev, "%pOF remote wake detected\n",
  1736. tegra->phys[i]->dev.of_node);
  1737. tegra_xusb_padctl_disable_phy_wake(padctl, tegra->phys[i]);
  1738. }
  1739. }
  1740. static void tegra_xhci_disable_phy_sleepwalk(struct tegra_xusb *tegra)
  1741. {
  1742. struct tegra_xusb_padctl *padctl = tegra->padctl;
  1743. unsigned int i;
  1744. for (i = 0; i < tegra->num_phys; i++) {
  1745. if (!tegra->phys[i])
  1746. continue;
  1747. tegra_xusb_padctl_disable_phy_sleepwalk(padctl, tegra->phys[i]);
  1748. }
  1749. }
  1750. static void tegra_xhci_program_utmi_power_lp0_exit(struct tegra_xusb *tegra)
  1751. {
  1752. unsigned int i, index_to_usb2;
  1753. struct phy *phy;
  1754. for (i = 0; i < tegra->soc->num_types; i++) {
  1755. if (strcmp(tegra->soc->phy_types[i].name, "usb2") == 0)
  1756. index_to_usb2 = i;
  1757. }
  1758. for (i = 0; i < tegra->num_usb_phys; i++) {
  1759. if (!is_host_mode_phy(tegra, index_to_usb2, i))
  1760. continue;
  1761. phy = tegra_xusb_get_phy(tegra, "usb2", i);
  1762. if (tegra->lp0_utmi_pad_mask & BIT(i))
  1763. tegra_phy_xusb_utmi_pad_power_on(phy);
  1764. else
  1765. tegra_phy_xusb_utmi_pad_power_down(phy);
  1766. }
  1767. }
  1768. static int tegra_xusb_enter_elpg(struct tegra_xusb *tegra, bool runtime)
  1769. {
  1770. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1771. struct device *dev = tegra->dev;
  1772. bool wakeup = runtime ? true : device_may_wakeup(dev);
  1773. unsigned int i;
  1774. int err;
  1775. u32 usbcmd;
  1776. u32 portsc;
  1777. dev_dbg(dev, "entering ELPG\n");
  1778. usbcmd = readl(&xhci->op_regs->command);
  1779. usbcmd &= ~CMD_EIE;
  1780. writel(usbcmd, &xhci->op_regs->command);
  1781. err = tegra_xusb_check_ports(tegra);
  1782. if (err < 0) {
  1783. dev_err(tegra->dev, "not all ports suspended: %d\n", err);
  1784. goto out;
  1785. }
  1786. for (i = 0; i < xhci->usb2_rhub.num_ports; i++) {
  1787. if (!xhci->usb2_rhub.ports[i])
  1788. continue;
  1789. portsc = readl(xhci->usb2_rhub.ports[i]->addr);
  1790. tegra->lp0_utmi_pad_mask &= ~BIT(i);
  1791. if (((portsc & PORT_PLS_MASK) == XDEV_U3) || ((portsc & DEV_SPEED_MASK) == XDEV_FS))
  1792. tegra->lp0_utmi_pad_mask |= BIT(i);
  1793. }
  1794. err = xhci_suspend(xhci, wakeup);
  1795. if (err < 0) {
  1796. dev_err(tegra->dev, "failed to suspend XHCI: %d\n", err);
  1797. goto out;
  1798. }
  1799. tegra_xusb_save_context(tegra);
  1800. if (wakeup)
  1801. tegra_xhci_enable_phy_sleepwalk_wake(tegra);
  1802. tegra_xusb_powergate_partitions(tegra);
  1803. for (i = 0; i < tegra->num_phys; i++) {
  1804. if (!tegra->phys[i])
  1805. continue;
  1806. phy_power_off(tegra->phys[i]);
  1807. if (!wakeup)
  1808. phy_exit(tegra->phys[i]);
  1809. }
  1810. tegra_xusb_clk_disable(tegra);
  1811. out:
  1812. if (!err)
  1813. dev_dbg(tegra->dev, "entering ELPG done\n");
  1814. else {
  1815. usbcmd = readl(&xhci->op_regs->command);
  1816. usbcmd |= CMD_EIE;
  1817. writel(usbcmd, &xhci->op_regs->command);
  1818. dev_dbg(tegra->dev, "entering ELPG failed\n");
  1819. pm_runtime_mark_last_busy(tegra->dev);
  1820. }
  1821. return err;
  1822. }
  1823. static int tegra_xusb_exit_elpg(struct tegra_xusb *tegra, bool runtime)
  1824. {
  1825. struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
  1826. struct device *dev = tegra->dev;
  1827. bool wakeup = runtime ? true : device_may_wakeup(dev);
  1828. unsigned int i;
  1829. u32 usbcmd;
  1830. int err;
  1831. dev_dbg(dev, "exiting ELPG\n");
  1832. pm_runtime_mark_last_busy(tegra->dev);
  1833. err = tegra_xusb_clk_enable(tegra);
  1834. if (err < 0) {
  1835. dev_err(tegra->dev, "failed to enable clocks: %d\n", err);
  1836. goto out;
  1837. }
  1838. err = tegra_xusb_unpowergate_partitions(tegra);
  1839. if (err)
  1840. goto disable_clks;
  1841. if (wakeup)
  1842. tegra_xhci_disable_phy_wake(tegra);
  1843. for (i = 0; i < tegra->num_phys; i++) {
  1844. if (!tegra->phys[i])
  1845. continue;
  1846. if (!wakeup)
  1847. phy_init(tegra->phys[i]);
  1848. phy_power_on(tegra->phys[i]);
  1849. }
  1850. if (tegra->suspended)
  1851. tegra_xhci_program_utmi_power_lp0_exit(tegra);
  1852. tegra_xusb_config(tegra);
  1853. tegra_xusb_restore_context(tegra);
  1854. err = tegra_xusb_load_firmware(tegra);
  1855. if (err < 0) {
  1856. dev_err(tegra->dev, "failed to load firmware: %d\n", err);
  1857. goto disable_phy;
  1858. }
  1859. err = __tegra_xusb_enable_firmware_messages(tegra);
  1860. if (err < 0) {
  1861. dev_err(tegra->dev, "failed to enable messages: %d\n", err);
  1862. goto disable_phy;
  1863. }
  1864. if (wakeup)
  1865. tegra_xhci_disable_phy_sleepwalk(tegra);
  1866. err = xhci_resume(xhci, runtime ? PMSG_AUTO_RESUME : PMSG_RESUME);
  1867. if (err < 0) {
  1868. dev_err(tegra->dev, "failed to resume XHCI: %d\n", err);
  1869. goto disable_phy;
  1870. }
  1871. usbcmd = readl(&xhci->op_regs->command);
  1872. usbcmd |= CMD_EIE;
  1873. writel(usbcmd, &xhci->op_regs->command);
  1874. goto out;
  1875. disable_phy:
  1876. for (i = 0; i < tegra->num_phys; i++) {
  1877. if (!tegra->phys[i])
  1878. continue;
  1879. phy_power_off(tegra->phys[i]);
  1880. if (!wakeup)
  1881. phy_exit(tegra->phys[i]);
  1882. }
  1883. tegra_xusb_powergate_partitions(tegra);
  1884. disable_clks:
  1885. tegra_xusb_clk_disable(tegra);
  1886. out:
  1887. if (!err)
  1888. dev_dbg(dev, "exiting ELPG done\n");
  1889. else
  1890. dev_dbg(dev, "exiting ELPG failed\n");
  1891. return err;
  1892. }
  1893. static __maybe_unused int tegra_xusb_suspend(struct device *dev)
  1894. {
  1895. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1896. int err;
  1897. synchronize_irq(tegra->mbox_irq);
  1898. mutex_lock(&tegra->lock);
  1899. if (pm_runtime_suspended(dev)) {
  1900. err = tegra_xusb_exit_elpg(tegra, true);
  1901. if (err < 0)
  1902. goto out;
  1903. }
  1904. err = tegra_xusb_enter_elpg(tegra, false);
  1905. if (err < 0) {
  1906. if (pm_runtime_suspended(dev)) {
  1907. pm_runtime_disable(dev);
  1908. pm_runtime_set_active(dev);
  1909. pm_runtime_enable(dev);
  1910. }
  1911. goto out;
  1912. }
  1913. out:
  1914. if (!err) {
  1915. tegra->suspended = true;
  1916. pm_runtime_disable(dev);
  1917. if (device_may_wakeup(dev)) {
  1918. if (enable_irq_wake(tegra->padctl_irq))
  1919. dev_err(dev, "failed to enable padctl wakes\n");
  1920. }
  1921. }
  1922. mutex_unlock(&tegra->lock);
  1923. return err;
  1924. }
  1925. static __maybe_unused int tegra_xusb_resume(struct device *dev)
  1926. {
  1927. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1928. int err;
  1929. mutex_lock(&tegra->lock);
  1930. if (!tegra->suspended) {
  1931. mutex_unlock(&tegra->lock);
  1932. return 0;
  1933. }
  1934. err = tegra_xusb_exit_elpg(tegra, false);
  1935. if (err < 0) {
  1936. mutex_unlock(&tegra->lock);
  1937. return err;
  1938. }
  1939. if (device_may_wakeup(dev)) {
  1940. if (disable_irq_wake(tegra->padctl_irq))
  1941. dev_err(dev, "failed to disable padctl wakes\n");
  1942. }
  1943. tegra->suspended = false;
  1944. mutex_unlock(&tegra->lock);
  1945. pm_runtime_set_active(dev);
  1946. pm_runtime_enable(dev);
  1947. return 0;
  1948. }
  1949. static __maybe_unused int tegra_xusb_runtime_suspend(struct device *dev)
  1950. {
  1951. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1952. int ret;
  1953. synchronize_irq(tegra->mbox_irq);
  1954. mutex_lock(&tegra->lock);
  1955. ret = tegra_xusb_enter_elpg(tegra, true);
  1956. mutex_unlock(&tegra->lock);
  1957. return ret;
  1958. }
  1959. static __maybe_unused int tegra_xusb_runtime_resume(struct device *dev)
  1960. {
  1961. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  1962. int err;
  1963. mutex_lock(&tegra->lock);
  1964. err = tegra_xusb_exit_elpg(tegra, true);
  1965. mutex_unlock(&tegra->lock);
  1966. return err;
  1967. }
  1968. static const struct dev_pm_ops tegra_xusb_pm_ops = {
  1969. SET_RUNTIME_PM_OPS(tegra_xusb_runtime_suspend,
  1970. tegra_xusb_runtime_resume, NULL)
  1971. SET_SYSTEM_SLEEP_PM_OPS(tegra_xusb_suspend, tegra_xusb_resume)
  1972. };
  1973. static const char * const tegra124_supply_names[] = {
  1974. "avddio-pex",
  1975. "dvddio-pex",
  1976. "avdd-usb",
  1977. "hvdd-usb-ss",
  1978. };
  1979. static const struct tegra_xusb_phy_type tegra124_phy_types[] = {
  1980. { .name = "usb3", .num = 2, },
  1981. { .name = "usb2", .num = 3, },
  1982. { .name = "hsic", .num = 2, },
  1983. };
  1984. static const unsigned int tegra124_xusb_context_ipfs[] = {
  1985. IPFS_XUSB_HOST_MSI_BAR_SZ_0,
  1986. IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0,
  1987. IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0,
  1988. IPFS_XUSB_HOST_MSI_VEC0_0,
  1989. IPFS_XUSB_HOST_MSI_EN_VEC0_0,
  1990. IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0,
  1991. IPFS_XUSB_HOST_INTR_MASK_0,
  1992. IPFS_XUSB_HOST_INTR_ENABLE_0,
  1993. IPFS_XUSB_HOST_UFPCI_CONFIG_0,
  1994. IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0,
  1995. IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0,
  1996. };
  1997. static const unsigned int tegra124_xusb_context_fpci[] = {
  1998. XUSB_CFG_ARU_CONTEXT_HS_PLS,
  1999. XUSB_CFG_ARU_CONTEXT_FS_PLS,
  2000. XUSB_CFG_ARU_CONTEXT_HSFS_SPEED,
  2001. XUSB_CFG_ARU_CONTEXT_HSFS_PP,
  2002. XUSB_CFG_ARU_CONTEXT,
  2003. XUSB_CFG_AXI_CFG,
  2004. XUSB_CFG_24,
  2005. XUSB_CFG_16,
  2006. };
  2007. static const struct tegra_xusb_context_soc tegra124_xusb_context = {
  2008. .ipfs = {
  2009. .num_offsets = ARRAY_SIZE(tegra124_xusb_context_ipfs),
  2010. .offsets = tegra124_xusb_context_ipfs,
  2011. },
  2012. .fpci = {
  2013. .num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci),
  2014. .offsets = tegra124_xusb_context_fpci,
  2015. },
  2016. };
  2017. static const struct tegra_xusb_soc_ops tegra124_ops = {
  2018. .mbox_reg_readl = &fpci_readl,
  2019. .mbox_reg_writel = &fpci_writel,
  2020. .csb_reg_readl = &fpci_csb_readl,
  2021. .csb_reg_writel = &fpci_csb_writel,
  2022. };
  2023. static const struct tegra_xusb_soc tegra124_soc = {
  2024. .firmware = "nvidia/tegra124/xusb.bin",
  2025. .supply_names = tegra124_supply_names,
  2026. .num_supplies = ARRAY_SIZE(tegra124_supply_names),
  2027. .phy_types = tegra124_phy_types,
  2028. .num_types = ARRAY_SIZE(tegra124_phy_types),
  2029. .context = &tegra124_xusb_context,
  2030. .ports = {
  2031. .usb2 = { .offset = 4, .count = 4, },
  2032. .hsic = { .offset = 6, .count = 2, },
  2033. .usb3 = { .offset = 0, .count = 2, },
  2034. },
  2035. .scale_ss_clock = true,
  2036. .has_ipfs = true,
  2037. .otg_reset_sspi = false,
  2038. .ops = &tegra124_ops,
  2039. .mbox = {
  2040. .cmd = 0xe4,
  2041. .data_in = 0xe8,
  2042. .data_out = 0xec,
  2043. .owner = 0xf0,
  2044. .smi_intr = XUSB_CFG_ARU_SMI_INTR,
  2045. },
  2046. };
  2047. MODULE_FIRMWARE("nvidia/tegra124/xusb.bin");
  2048. static const char * const tegra210_supply_names[] = {
  2049. "dvddio-pex",
  2050. "hvddio-pex",
  2051. "avdd-usb",
  2052. };
  2053. static const struct tegra_xusb_phy_type tegra210_phy_types[] = {
  2054. { .name = "usb3", .num = 4, },
  2055. { .name = "usb2", .num = 4, },
  2056. { .name = "hsic", .num = 1, },
  2057. };
  2058. static const struct tegra_xusb_soc tegra210_soc = {
  2059. .firmware = "nvidia/tegra210/xusb.bin",
  2060. .supply_names = tegra210_supply_names,
  2061. .num_supplies = ARRAY_SIZE(tegra210_supply_names),
  2062. .phy_types = tegra210_phy_types,
  2063. .num_types = ARRAY_SIZE(tegra210_phy_types),
  2064. .context = &tegra124_xusb_context,
  2065. .ports = {
  2066. .usb2 = { .offset = 4, .count = 4, },
  2067. .hsic = { .offset = 8, .count = 1, },
  2068. .usb3 = { .offset = 0, .count = 4, },
  2069. },
  2070. .scale_ss_clock = false,
  2071. .has_ipfs = true,
  2072. .otg_reset_sspi = true,
  2073. .ops = &tegra124_ops,
  2074. .mbox = {
  2075. .cmd = 0xe4,
  2076. .data_in = 0xe8,
  2077. .data_out = 0xec,
  2078. .owner = 0xf0,
  2079. .smi_intr = XUSB_CFG_ARU_SMI_INTR,
  2080. },
  2081. };
  2082. MODULE_FIRMWARE("nvidia/tegra210/xusb.bin");
  2083. static const char * const tegra186_supply_names[] = {
  2084. };
  2085. MODULE_FIRMWARE("nvidia/tegra186/xusb.bin");
  2086. static const struct tegra_xusb_phy_type tegra186_phy_types[] = {
  2087. { .name = "usb3", .num = 3, },
  2088. { .name = "usb2", .num = 3, },
  2089. { .name = "hsic", .num = 1, },
  2090. };
  2091. static const struct tegra_xusb_context_soc tegra186_xusb_context = {
  2092. .fpci = {
  2093. .num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci),
  2094. .offsets = tegra124_xusb_context_fpci,
  2095. },
  2096. };
  2097. static const struct tegra_xusb_soc tegra186_soc = {
  2098. .firmware = "nvidia/tegra186/xusb.bin",
  2099. .supply_names = tegra186_supply_names,
  2100. .num_supplies = ARRAY_SIZE(tegra186_supply_names),
  2101. .phy_types = tegra186_phy_types,
  2102. .num_types = ARRAY_SIZE(tegra186_phy_types),
  2103. .context = &tegra186_xusb_context,
  2104. .ports = {
  2105. .usb3 = { .offset = 0, .count = 3, },
  2106. .usb2 = { .offset = 3, .count = 3, },
  2107. .hsic = { .offset = 6, .count = 1, },
  2108. },
  2109. .scale_ss_clock = false,
  2110. .has_ipfs = false,
  2111. .otg_reset_sspi = false,
  2112. .ops = &tegra124_ops,
  2113. .mbox = {
  2114. .cmd = 0xe4,
  2115. .data_in = 0xe8,
  2116. .data_out = 0xec,
  2117. .owner = 0xf0,
  2118. .smi_intr = XUSB_CFG_ARU_SMI_INTR,
  2119. },
  2120. .lpm_support = true,
  2121. };
  2122. static const char * const tegra194_supply_names[] = {
  2123. };
  2124. static const struct tegra_xusb_phy_type tegra194_phy_types[] = {
  2125. { .name = "usb3", .num = 4, },
  2126. { .name = "usb2", .num = 4, },
  2127. };
  2128. static const struct tegra_xusb_soc tegra194_soc = {
  2129. .firmware = "nvidia/tegra194/xusb.bin",
  2130. .supply_names = tegra194_supply_names,
  2131. .num_supplies = ARRAY_SIZE(tegra194_supply_names),
  2132. .phy_types = tegra194_phy_types,
  2133. .num_types = ARRAY_SIZE(tegra194_phy_types),
  2134. .context = &tegra186_xusb_context,
  2135. .ports = {
  2136. .usb3 = { .offset = 0, .count = 4, },
  2137. .usb2 = { .offset = 4, .count = 4, },
  2138. },
  2139. .scale_ss_clock = false,
  2140. .has_ipfs = false,
  2141. .otg_reset_sspi = false,
  2142. .ops = &tegra124_ops,
  2143. .mbox = {
  2144. .cmd = 0x68,
  2145. .data_in = 0x6c,
  2146. .data_out = 0x70,
  2147. .owner = 0x74,
  2148. .smi_intr = XUSB_CFG_ARU_SMI_INTR,
  2149. },
  2150. .lpm_support = true,
  2151. };
  2152. MODULE_FIRMWARE("nvidia/tegra194/xusb.bin");
  2153. static const struct tegra_xusb_soc_ops tegra234_ops = {
  2154. .mbox_reg_readl = &bar2_readl,
  2155. .mbox_reg_writel = &bar2_writel,
  2156. .csb_reg_readl = &bar2_csb_readl,
  2157. .csb_reg_writel = &bar2_csb_writel,
  2158. };
  2159. static const struct tegra_xusb_soc tegra234_soc = {
  2160. .supply_names = tegra194_supply_names,
  2161. .num_supplies = ARRAY_SIZE(tegra194_supply_names),
  2162. .phy_types = tegra194_phy_types,
  2163. .num_types = ARRAY_SIZE(tegra194_phy_types),
  2164. .context = &tegra186_xusb_context,
  2165. .ports = {
  2166. .usb3 = { .offset = 0, .count = 4, },
  2167. .usb2 = { .offset = 4, .count = 4, },
  2168. },
  2169. .scale_ss_clock = false,
  2170. .has_ipfs = false,
  2171. .otg_reset_sspi = false,
  2172. .ops = &tegra234_ops,
  2173. .mbox = {
  2174. .cmd = XUSB_BAR2_ARU_MBOX_CMD,
  2175. .data_in = XUSB_BAR2_ARU_MBOX_DATA_IN,
  2176. .data_out = XUSB_BAR2_ARU_MBOX_DATA_OUT,
  2177. .owner = XUSB_BAR2_ARU_MBOX_OWNER,
  2178. .smi_intr = XUSB_BAR2_ARU_SMI_INTR,
  2179. },
  2180. .lpm_support = true,
  2181. .has_bar2 = true,
  2182. };
  2183. static const struct of_device_id tegra_xusb_of_match[] = {
  2184. { .compatible = "nvidia,tegra124-xusb", .data = &tegra124_soc },
  2185. { .compatible = "nvidia,tegra210-xusb", .data = &tegra210_soc },
  2186. { .compatible = "nvidia,tegra186-xusb", .data = &tegra186_soc },
  2187. { .compatible = "nvidia,tegra194-xusb", .data = &tegra194_soc },
  2188. { .compatible = "nvidia,tegra234-xusb", .data = &tegra234_soc },
  2189. { },
  2190. };
  2191. MODULE_DEVICE_TABLE(of, tegra_xusb_of_match);
  2192. static struct platform_driver tegra_xusb_driver = {
  2193. .probe = tegra_xusb_probe,
  2194. .remove_new = tegra_xusb_remove,
  2195. .shutdown = tegra_xusb_shutdown,
  2196. .driver = {
  2197. .name = "tegra-xusb",
  2198. .pm = &tegra_xusb_pm_ops,
  2199. .of_match_table = tegra_xusb_of_match,
  2200. },
  2201. };
  2202. static void tegra_xhci_quirks(struct device *dev, struct xhci_hcd *xhci)
  2203. {
  2204. struct tegra_xusb *tegra = dev_get_drvdata(dev);
  2205. if (tegra && tegra->soc->lpm_support)
  2206. xhci->quirks |= XHCI_LPM_SUPPORT;
  2207. }
  2208. static int tegra_xhci_setup(struct usb_hcd *hcd)
  2209. {
  2210. return xhci_gen_setup(hcd, tegra_xhci_quirks);
  2211. }
  2212. static int tegra_xhci_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index,
  2213. char *buf, u16 length)
  2214. {
  2215. struct tegra_xusb *tegra = dev_get_drvdata(hcd->self.controller);
  2216. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  2217. struct xhci_hub *rhub;
  2218. struct xhci_bus_state *bus_state;
  2219. int port = (index & 0xff) - 1;
  2220. unsigned int i;
  2221. struct xhci_port **ports;
  2222. u32 portsc;
  2223. int ret;
  2224. struct phy *phy;
  2225. rhub = &xhci->usb2_rhub;
  2226. bus_state = &rhub->bus_state;
  2227. if (bus_state->resuming_ports && hcd->speed == HCD_USB2) {
  2228. ports = rhub->ports;
  2229. i = rhub->num_ports;
  2230. while (i--) {
  2231. if (!test_bit(i, &bus_state->resuming_ports))
  2232. continue;
  2233. portsc = readl(ports[i]->addr);
  2234. if ((portsc & PORT_PLS_MASK) == XDEV_RESUME)
  2235. tegra_phy_xusb_utmi_pad_power_on(
  2236. tegra_xusb_get_phy(tegra, "usb2", (int) i));
  2237. }
  2238. }
  2239. if (hcd->speed == HCD_USB2) {
  2240. phy = tegra_xusb_get_phy(tegra, "usb2", port);
  2241. if ((type_req == ClearPortFeature) && (value == USB_PORT_FEAT_SUSPEND)) {
  2242. if (!index || index > rhub->num_ports)
  2243. return -EPIPE;
  2244. tegra_phy_xusb_utmi_pad_power_on(phy);
  2245. }
  2246. if ((type_req == SetPortFeature) && (value == USB_PORT_FEAT_RESET)) {
  2247. if (!index || index > rhub->num_ports)
  2248. return -EPIPE;
  2249. ports = rhub->ports;
  2250. portsc = readl(ports[port]->addr);
  2251. if (portsc & PORT_CONNECT)
  2252. tegra_phy_xusb_utmi_pad_power_on(phy);
  2253. }
  2254. }
  2255. ret = xhci_hub_control(hcd, type_req, value, index, buf, length);
  2256. if (ret < 0)
  2257. return ret;
  2258. if (hcd->speed == HCD_USB2) {
  2259. /* Use phy where we set previously */
  2260. if ((type_req == SetPortFeature) && (value == USB_PORT_FEAT_SUSPEND))
  2261. /* We don't suspend the PAD while HNP role swap happens on the OTG port */
  2262. if (!((hcd->self.otg_port == (port + 1)) && hcd->self.b_hnp_enable))
  2263. tegra_phy_xusb_utmi_pad_power_down(phy);
  2264. if ((type_req == ClearPortFeature) && (value == USB_PORT_FEAT_C_CONNECTION)) {
  2265. ports = rhub->ports;
  2266. portsc = readl(ports[port]->addr);
  2267. if (!(portsc & PORT_CONNECT)) {
  2268. /* We don't suspend the PAD while HNP role swap happens on the OTG
  2269. * port
  2270. */
  2271. if (!((hcd->self.otg_port == (port + 1)) && hcd->self.b_hnp_enable))
  2272. tegra_phy_xusb_utmi_pad_power_down(phy);
  2273. }
  2274. }
  2275. if ((type_req == SetPortFeature) && (value == USB_PORT_FEAT_TEST))
  2276. tegra_phy_xusb_utmi_pad_power_on(phy);
  2277. }
  2278. return ret;
  2279. }
  2280. static const struct xhci_driver_overrides tegra_xhci_overrides __initconst = {
  2281. .reset = tegra_xhci_setup,
  2282. .hub_control = tegra_xhci_hub_control,
  2283. };
  2284. static int __init tegra_xusb_init(void)
  2285. {
  2286. xhci_init_driver(&tegra_xhci_hc_driver, &tegra_xhci_overrides);
  2287. return platform_driver_register(&tegra_xusb_driver);
  2288. }
  2289. module_init(tegra_xusb_init);
  2290. static void __exit tegra_xusb_exit(void)
  2291. {
  2292. platform_driver_unregister(&tegra_xusb_driver);
  2293. }
  2294. module_exit(tegra_xusb_exit);
  2295. MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
  2296. MODULE_DESCRIPTION("NVIDIA Tegra XUSB xHCI host-controller driver");
  2297. MODULE_LICENSE("GPL v2");