ufs-exynos.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * UFS Host Controller driver for Exynos specific extensions
  4. *
  5. * Copyright (C) 2014-2015 Samsung Electronics Co., Ltd.
  6. * Author: Seungwon Jeon <essuuj@gmail.com>
  7. * Author: Alim Akhtar <alim.akhtar@samsung.com>
  8. *
  9. */
  10. #include <linux/unaligned.h>
  11. #include <crypto/aes.h>
  12. #include <linux/arm-smccc.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regmap.h>
  22. #include <ufs/ufshcd.h>
  23. #include "ufshcd-pltfrm.h"
  24. #include <ufs/ufshci.h>
  25. #include <ufs/unipro.h>
  26. #include "ufs-exynos.h"
  27. #define DATA_UNIT_SIZE 4096
  28. /*
  29. * Exynos's Vendor specific registers for UFSHCI
  30. */
  31. #define HCI_TXPRDT_ENTRY_SIZE 0x00
  32. #define PRDT_PREFETCH_EN BIT(31)
  33. #define HCI_RXPRDT_ENTRY_SIZE 0x04
  34. #define HCI_1US_TO_CNT_VAL 0x0C
  35. #define CNT_VAL_1US_MASK 0x3FF
  36. #define HCI_UTRL_NEXUS_TYPE 0x40
  37. #define HCI_UTMRL_NEXUS_TYPE 0x44
  38. #define HCI_SW_RST 0x50
  39. #define UFS_LINK_SW_RST BIT(0)
  40. #define UFS_UNIPRO_SW_RST BIT(1)
  41. #define UFS_SW_RST_MASK (UFS_UNIPRO_SW_RST | UFS_LINK_SW_RST)
  42. #define HCI_DATA_REORDER 0x60
  43. #define HCI_UNIPRO_APB_CLK_CTRL 0x68
  44. #define UNIPRO_APB_CLK(v, x) (((v) & ~0xF) | ((x) & 0xF))
  45. #define HCI_AXIDMA_RWDATA_BURST_LEN 0x6C
  46. #define HCI_GPIO_OUT 0x70
  47. #define HCI_ERR_EN_PA_LAYER 0x78
  48. #define HCI_ERR_EN_DL_LAYER 0x7C
  49. #define HCI_ERR_EN_N_LAYER 0x80
  50. #define HCI_ERR_EN_T_LAYER 0x84
  51. #define HCI_ERR_EN_DME_LAYER 0x88
  52. #define HCI_V2P1_CTRL 0x8C
  53. #define IA_TICK_SEL BIT(16)
  54. #define HCI_CLKSTOP_CTRL 0xB0
  55. #define REFCLKOUT_STOP BIT(4)
  56. #define MPHY_APBCLK_STOP BIT(3)
  57. #define REFCLK_STOP BIT(2)
  58. #define UNIPRO_MCLK_STOP BIT(1)
  59. #define UNIPRO_PCLK_STOP BIT(0)
  60. #define CLK_STOP_MASK (REFCLKOUT_STOP | REFCLK_STOP |\
  61. UNIPRO_MCLK_STOP | MPHY_APBCLK_STOP|\
  62. UNIPRO_PCLK_STOP)
  63. /* HCI_MISC is also known as HCI_FORCE_HCS */
  64. #define HCI_MISC 0xB4
  65. #define REFCLK_CTRL_EN BIT(7)
  66. #define UNIPRO_PCLK_CTRL_EN BIT(6)
  67. #define UNIPRO_MCLK_CTRL_EN BIT(5)
  68. #define HCI_CORECLK_CTRL_EN BIT(4)
  69. #define CLK_CTRL_EN_MASK (REFCLK_CTRL_EN |\
  70. UNIPRO_PCLK_CTRL_EN |\
  71. UNIPRO_MCLK_CTRL_EN)
  72. /* Device fatal error */
  73. #define DFES_ERR_EN BIT(31)
  74. #define DFES_DEF_L2_ERRS (UIC_DATA_LINK_LAYER_ERROR_RX_BUF_OF |\
  75. UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
  76. #define DFES_DEF_L3_ERRS (UIC_NETWORK_UNSUPPORTED_HEADER_TYPE |\
  77. UIC_NETWORK_BAD_DEVICEID_ENC |\
  78. UIC_NETWORK_LHDR_TRAP_PACKET_DROPPING)
  79. #define DFES_DEF_L4_ERRS (UIC_TRANSPORT_UNSUPPORTED_HEADER_TYPE |\
  80. UIC_TRANSPORT_UNKNOWN_CPORTID |\
  81. UIC_TRANSPORT_NO_CONNECTION_RX |\
  82. UIC_TRANSPORT_BAD_TC)
  83. /* UFS Shareability */
  84. #define UFS_EXYNOSAUTO_WR_SHARABLE BIT(2)
  85. #define UFS_EXYNOSAUTO_RD_SHARABLE BIT(1)
  86. #define UFS_EXYNOSAUTO_SHARABLE (UFS_EXYNOSAUTO_WR_SHARABLE | \
  87. UFS_EXYNOSAUTO_RD_SHARABLE)
  88. #define UFS_GS101_WR_SHARABLE BIT(1)
  89. #define UFS_GS101_RD_SHARABLE BIT(0)
  90. #define UFS_GS101_SHARABLE (UFS_GS101_WR_SHARABLE | \
  91. UFS_GS101_RD_SHARABLE)
  92. #define UFS_SHAREABILITY_OFFSET 0x710
  93. /* Multi-host registers */
  94. #define MHCTRL 0xC4
  95. #define MHCTRL_EN_VH_MASK (0xE)
  96. #define MHCTRL_EN_VH(vh) (vh << 1)
  97. #define PH2VH_MBOX 0xD8
  98. #define MH_MSG_MASK (0xFF)
  99. #define MH_MSG(id, msg) ((id << 8) | (msg & 0xFF))
  100. #define MH_MSG_PH_READY 0x1
  101. #define MH_MSG_VH_READY 0x2
  102. #define ALLOW_INQUIRY BIT(25)
  103. #define ALLOW_MODE_SELECT BIT(24)
  104. #define ALLOW_MODE_SENSE BIT(23)
  105. #define ALLOW_PRE_FETCH GENMASK(22, 21)
  106. #define ALLOW_READ_CMD_ALL GENMASK(20, 18) /* read_6/10/16 */
  107. #define ALLOW_READ_BUFFER BIT(17)
  108. #define ALLOW_READ_CAPACITY GENMASK(16, 15)
  109. #define ALLOW_REPORT_LUNS BIT(14)
  110. #define ALLOW_REQUEST_SENSE BIT(13)
  111. #define ALLOW_SYNCHRONIZE_CACHE GENMASK(8, 7)
  112. #define ALLOW_TEST_UNIT_READY BIT(6)
  113. #define ALLOW_UNMAP BIT(5)
  114. #define ALLOW_VERIFY BIT(4)
  115. #define ALLOW_WRITE_CMD_ALL GENMASK(3, 1) /* write_6/10/16 */
  116. #define ALLOW_TRANS_VH_DEFAULT (ALLOW_INQUIRY | ALLOW_MODE_SELECT | \
  117. ALLOW_MODE_SENSE | ALLOW_PRE_FETCH | \
  118. ALLOW_READ_CMD_ALL | ALLOW_READ_BUFFER | \
  119. ALLOW_READ_CAPACITY | ALLOW_REPORT_LUNS | \
  120. ALLOW_REQUEST_SENSE | ALLOW_SYNCHRONIZE_CACHE | \
  121. ALLOW_TEST_UNIT_READY | ALLOW_UNMAP | \
  122. ALLOW_VERIFY | ALLOW_WRITE_CMD_ALL)
  123. #define HCI_MH_ALLOWABLE_TRAN_OF_VH 0x30C
  124. #define HCI_MH_IID_IN_TASK_TAG 0X308
  125. #define PH_READY_TIMEOUT_MS (5 * MSEC_PER_SEC)
  126. enum {
  127. UNIPRO_L1_5 = 0,/* PHY Adapter */
  128. UNIPRO_L2, /* Data Link */
  129. UNIPRO_L3, /* Network */
  130. UNIPRO_L4, /* Transport */
  131. UNIPRO_DME, /* DME */
  132. };
  133. /*
  134. * UNIPRO registers
  135. */
  136. #define UNIPRO_DME_POWERMODE_REQ_LOCALL2TIMER0 0x7888
  137. #define UNIPRO_DME_POWERMODE_REQ_LOCALL2TIMER1 0x788c
  138. #define UNIPRO_DME_POWERMODE_REQ_LOCALL2TIMER2 0x7890
  139. #define UNIPRO_DME_POWERMODE_REQ_REMOTEL2TIMER0 0x78B8
  140. #define UNIPRO_DME_POWERMODE_REQ_REMOTEL2TIMER1 0x78BC
  141. #define UNIPRO_DME_POWERMODE_REQ_REMOTEL2TIMER2 0x78C0
  142. /*
  143. * UFS Protector registers
  144. */
  145. #define UFSPRSECURITY 0x010
  146. #define NSSMU BIT(14)
  147. #define UFSPSBEGIN0 0x200
  148. #define UFSPSEND0 0x204
  149. #define UFSPSLUN0 0x208
  150. #define UFSPSCTRL0 0x20C
  151. #define CNTR_DIV_VAL 40
  152. static void exynos_ufs_auto_ctrl_hcc(struct exynos_ufs *ufs, bool en);
  153. static void exynos_ufs_ctrl_clkstop(struct exynos_ufs *ufs, bool en);
  154. static inline void exynos_ufs_enable_auto_ctrl_hcc(struct exynos_ufs *ufs)
  155. {
  156. exynos_ufs_auto_ctrl_hcc(ufs, true);
  157. }
  158. static inline void exynos_ufs_disable_auto_ctrl_hcc(struct exynos_ufs *ufs)
  159. {
  160. exynos_ufs_auto_ctrl_hcc(ufs, false);
  161. }
  162. static inline void exynos_ufs_disable_auto_ctrl_hcc_save(
  163. struct exynos_ufs *ufs, u32 *val)
  164. {
  165. *val = hci_readl(ufs, HCI_MISC);
  166. exynos_ufs_auto_ctrl_hcc(ufs, false);
  167. }
  168. static inline void exynos_ufs_auto_ctrl_hcc_restore(
  169. struct exynos_ufs *ufs, u32 *val)
  170. {
  171. hci_writel(ufs, *val, HCI_MISC);
  172. }
  173. static inline void exynos_ufs_gate_clks(struct exynos_ufs *ufs)
  174. {
  175. exynos_ufs_ctrl_clkstop(ufs, true);
  176. }
  177. static inline void exynos_ufs_ungate_clks(struct exynos_ufs *ufs)
  178. {
  179. exynos_ufs_ctrl_clkstop(ufs, false);
  180. }
  181. static int exynos_ufs_shareability(struct exynos_ufs *ufs)
  182. {
  183. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  184. /* IO Coherency setting */
  185. if (ufs->sysreg) {
  186. return regmap_update_bits(ufs->sysreg,
  187. ufs->iocc_offset,
  188. ufs->iocc_mask, ufs->iocc_val);
  189. }
  190. attr->tx_dif_p_nsec = 3200000;
  191. return 0;
  192. }
  193. static int gs101_ufs_drv_init(struct exynos_ufs *ufs)
  194. {
  195. struct ufs_hba *hba = ufs->hba;
  196. /* Enable WriteBooster */
  197. hba->caps |= UFSHCD_CAP_WB_EN;
  198. return exynos_ufs_shareability(ufs);
  199. }
  200. static int exynosauto_ufs_drv_init(struct exynos_ufs *ufs)
  201. {
  202. return exynos_ufs_shareability(ufs);
  203. }
  204. static int exynosauto_ufs_post_hce_enable(struct exynos_ufs *ufs)
  205. {
  206. struct ufs_hba *hba = ufs->hba;
  207. /* Enable Virtual Host #1 */
  208. ufshcd_rmwl(hba, MHCTRL_EN_VH_MASK, MHCTRL_EN_VH(1), MHCTRL);
  209. /* Default VH Transfer permissions */
  210. hci_writel(ufs, ALLOW_TRANS_VH_DEFAULT, HCI_MH_ALLOWABLE_TRAN_OF_VH);
  211. /* IID information is replaced in TASKTAG[7:5] instead of IID in UCD */
  212. hci_writel(ufs, 0x1, HCI_MH_IID_IN_TASK_TAG);
  213. return 0;
  214. }
  215. static int exynosauto_ufs_pre_link(struct exynos_ufs *ufs)
  216. {
  217. struct ufs_hba *hba = ufs->hba;
  218. int i;
  219. u32 tx_line_reset_period, rx_line_reset_period;
  220. rx_line_reset_period = (RX_LINE_RESET_TIME * ufs->mclk_rate) / NSEC_PER_MSEC;
  221. tx_line_reset_period = (TX_LINE_RESET_TIME * ufs->mclk_rate) / NSEC_PER_MSEC;
  222. ufshcd_dme_set(hba, UIC_ARG_MIB(0x200), 0x40);
  223. for_each_ufs_rx_lane(ufs, i) {
  224. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_CLK_PRD, i),
  225. DIV_ROUND_UP(NSEC_PER_SEC, ufs->mclk_rate));
  226. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_CLK_PRD_EN, i), 0x0);
  227. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_VALUE2, i),
  228. (rx_line_reset_period >> 16) & 0xFF);
  229. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_VALUE1, i),
  230. (rx_line_reset_period >> 8) & 0xFF);
  231. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_VALUE0, i),
  232. (rx_line_reset_period) & 0xFF);
  233. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x2f, i), 0x79);
  234. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x84, i), 0x1);
  235. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x25, i), 0xf6);
  236. }
  237. for_each_ufs_tx_lane(ufs, i) {
  238. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_CLK_PRD, i),
  239. DIV_ROUND_UP(NSEC_PER_SEC, ufs->mclk_rate));
  240. /* Not to affect VND_TX_LINERESET_PVALUE to VND_TX_CLK_PRD */
  241. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_CLK_PRD_EN, i),
  242. 0x02);
  243. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_LINERESET_PVALUE2, i),
  244. (tx_line_reset_period >> 16) & 0xFF);
  245. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_LINERESET_PVALUE1, i),
  246. (tx_line_reset_period >> 8) & 0xFF);
  247. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_LINERESET_PVALUE0, i),
  248. (tx_line_reset_period) & 0xFF);
  249. /* TX PWM Gear Capability / PWM_G1_ONLY */
  250. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x04, i), 0x1);
  251. }
  252. ufshcd_dme_set(hba, UIC_ARG_MIB(0x200), 0x0);
  253. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), 0x0);
  254. ufshcd_dme_set(hba, UIC_ARG_MIB(0xa011), 0x8000);
  255. return 0;
  256. }
  257. static int exynosauto_ufs_pre_pwr_change(struct exynos_ufs *ufs,
  258. struct ufs_pa_layer_attr *pwr)
  259. {
  260. struct ufs_hba *hba = ufs->hba;
  261. /* PACP_PWR_req and delivered to the remote DME */
  262. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), 12000);
  263. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), 32000);
  264. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), 16000);
  265. return 0;
  266. }
  267. static int exynosauto_ufs_post_pwr_change(struct exynos_ufs *ufs,
  268. struct ufs_pa_layer_attr *pwr)
  269. {
  270. struct ufs_hba *hba = ufs->hba;
  271. u32 enabled_vh;
  272. enabled_vh = ufshcd_readl(hba, MHCTRL) & MHCTRL_EN_VH_MASK;
  273. /* Send physical host ready message to virtual hosts */
  274. ufshcd_writel(hba, MH_MSG(enabled_vh, MH_MSG_PH_READY), PH2VH_MBOX);
  275. return 0;
  276. }
  277. static int exynos7_ufs_pre_link(struct exynos_ufs *ufs)
  278. {
  279. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  280. u32 val = attr->pa_dbg_opt_suite1_val;
  281. struct ufs_hba *hba = ufs->hba;
  282. int i;
  283. exynos_ufs_enable_ov_tm(hba);
  284. for_each_ufs_tx_lane(ufs, i)
  285. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x297, i), 0x17);
  286. for_each_ufs_rx_lane(ufs, i) {
  287. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x362, i), 0xff);
  288. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x363, i), 0x00);
  289. }
  290. exynos_ufs_disable_ov_tm(hba);
  291. for_each_ufs_tx_lane(ufs, i)
  292. ufshcd_dme_set(hba,
  293. UIC_ARG_MIB_SEL(TX_HIBERN8_CONTROL, i), 0x0);
  294. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_DBG_TXPHY_CFGUPDT), 0x1);
  295. udelay(1);
  296. ufshcd_dme_set(hba, UIC_ARG_MIB(attr->pa_dbg_opt_suite1_off),
  297. val | (1 << 12));
  298. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_DBG_SKIP_RESET_PHY), 0x1);
  299. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_DBG_SKIP_LINE_RESET), 0x1);
  300. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_DBG_LINE_RESET_REQ), 0x1);
  301. udelay(1600);
  302. ufshcd_dme_set(hba, UIC_ARG_MIB(attr->pa_dbg_opt_suite1_off), val);
  303. return 0;
  304. }
  305. static int exynos7_ufs_post_link(struct exynos_ufs *ufs)
  306. {
  307. struct ufs_hba *hba = ufs->hba;
  308. int i;
  309. exynos_ufs_enable_ov_tm(hba);
  310. for_each_ufs_tx_lane(ufs, i) {
  311. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x28b, i), 0x83);
  312. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x29a, i), 0x07);
  313. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x277, i),
  314. TX_LINERESET_N(exynos_ufs_calc_time_cntr(ufs, 200000)));
  315. }
  316. exynos_ufs_disable_ov_tm(hba);
  317. exynos_ufs_enable_dbg_mode(hba);
  318. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_SAVECONFIGTIME), 0xbb8);
  319. exynos_ufs_disable_dbg_mode(hba);
  320. return 0;
  321. }
  322. static int exynos7_ufs_pre_pwr_change(struct exynos_ufs *ufs,
  323. struct ufs_pa_layer_attr *pwr)
  324. {
  325. unipro_writel(ufs, 0x22, UNIPRO_DBG_FORCE_DME_CTRL_STATE);
  326. return 0;
  327. }
  328. static int exynos7_ufs_post_pwr_change(struct exynos_ufs *ufs,
  329. struct ufs_pa_layer_attr *pwr)
  330. {
  331. struct ufs_hba *hba = ufs->hba;
  332. int lanes = max_t(u32, pwr->lane_rx, pwr->lane_tx);
  333. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_DBG_RXPHY_CFGUPDT), 0x1);
  334. if (lanes == 1) {
  335. exynos_ufs_enable_dbg_mode(hba);
  336. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), 0x1);
  337. exynos_ufs_disable_dbg_mode(hba);
  338. }
  339. return 0;
  340. }
  341. /*
  342. * exynos_ufs_auto_ctrl_hcc - HCI core clock control by h/w
  343. * Control should be disabled in the below cases
  344. * - Before host controller S/W reset
  345. * - Access to UFS protector's register
  346. */
  347. static void exynos_ufs_auto_ctrl_hcc(struct exynos_ufs *ufs, bool en)
  348. {
  349. u32 misc = hci_readl(ufs, HCI_MISC);
  350. if (en)
  351. hci_writel(ufs, misc | HCI_CORECLK_CTRL_EN, HCI_MISC);
  352. else
  353. hci_writel(ufs, misc & ~HCI_CORECLK_CTRL_EN, HCI_MISC);
  354. }
  355. static void exynos_ufs_ctrl_clkstop(struct exynos_ufs *ufs, bool en)
  356. {
  357. u32 ctrl = hci_readl(ufs, HCI_CLKSTOP_CTRL);
  358. u32 misc = hci_readl(ufs, HCI_MISC);
  359. if (en) {
  360. hci_writel(ufs, misc | CLK_CTRL_EN_MASK, HCI_MISC);
  361. hci_writel(ufs, ctrl | CLK_STOP_MASK, HCI_CLKSTOP_CTRL);
  362. } else {
  363. hci_writel(ufs, ctrl & ~CLK_STOP_MASK, HCI_CLKSTOP_CTRL);
  364. hci_writel(ufs, misc & ~CLK_CTRL_EN_MASK, HCI_MISC);
  365. }
  366. }
  367. static int exynos_ufs_get_clk_info(struct exynos_ufs *ufs)
  368. {
  369. struct ufs_hba *hba = ufs->hba;
  370. struct list_head *head = &hba->clk_list_head;
  371. struct ufs_clk_info *clki;
  372. unsigned long pclk_rate;
  373. u32 f_min, f_max;
  374. u8 div = 0;
  375. int ret = 0;
  376. if (list_empty(head))
  377. goto out;
  378. list_for_each_entry(clki, head, list) {
  379. if (!IS_ERR(clki->clk)) {
  380. if (!strcmp(clki->name, "core_clk"))
  381. ufs->clk_hci_core = clki->clk;
  382. else if (!strcmp(clki->name, "sclk_unipro_main"))
  383. ufs->clk_unipro_main = clki->clk;
  384. }
  385. }
  386. if (!ufs->clk_hci_core || !ufs->clk_unipro_main) {
  387. dev_err(hba->dev, "failed to get clk info\n");
  388. ret = -EINVAL;
  389. goto out;
  390. }
  391. ufs->mclk_rate = clk_get_rate(ufs->clk_unipro_main);
  392. pclk_rate = clk_get_rate(ufs->clk_hci_core);
  393. f_min = ufs->pclk_avail_min;
  394. f_max = ufs->pclk_avail_max;
  395. if (ufs->opts & EXYNOS_UFS_OPT_HAS_APB_CLK_CTRL) {
  396. do {
  397. pclk_rate /= (div + 1);
  398. if (pclk_rate <= f_max)
  399. break;
  400. div++;
  401. } while (pclk_rate >= f_min);
  402. }
  403. if (unlikely(pclk_rate < f_min || pclk_rate > f_max)) {
  404. dev_err(hba->dev, "not available pclk range %lu\n", pclk_rate);
  405. ret = -EINVAL;
  406. goto out;
  407. }
  408. ufs->pclk_rate = pclk_rate;
  409. ufs->pclk_div = div;
  410. out:
  411. return ret;
  412. }
  413. static void exynos_ufs_set_unipro_pclk_div(struct exynos_ufs *ufs)
  414. {
  415. if (ufs->opts & EXYNOS_UFS_OPT_HAS_APB_CLK_CTRL) {
  416. u32 val;
  417. val = hci_readl(ufs, HCI_UNIPRO_APB_CLK_CTRL);
  418. hci_writel(ufs, UNIPRO_APB_CLK(val, ufs->pclk_div),
  419. HCI_UNIPRO_APB_CLK_CTRL);
  420. }
  421. }
  422. static void exynos_ufs_set_pwm_clk_div(struct exynos_ufs *ufs)
  423. {
  424. struct ufs_hba *hba = ufs->hba;
  425. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  426. ufshcd_dme_set(hba,
  427. UIC_ARG_MIB(CMN_PWM_CLK_CTRL), attr->cmn_pwm_clk_ctrl);
  428. }
  429. static void exynos_ufs_calc_pwm_clk_div(struct exynos_ufs *ufs)
  430. {
  431. struct ufs_hba *hba = ufs->hba;
  432. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  433. const unsigned int div = 30, mult = 20;
  434. const unsigned long pwm_min = 3 * 1000 * 1000;
  435. const unsigned long pwm_max = 9 * 1000 * 1000;
  436. const int divs[] = {32, 16, 8, 4};
  437. unsigned long clk = 0, _clk, clk_period;
  438. int i = 0, clk_idx = -1;
  439. clk_period = UNIPRO_PCLK_PERIOD(ufs);
  440. for (i = 0; i < ARRAY_SIZE(divs); i++) {
  441. _clk = NSEC_PER_SEC * mult / (clk_period * divs[i] * div);
  442. if (_clk >= pwm_min && _clk <= pwm_max) {
  443. if (_clk > clk) {
  444. clk_idx = i;
  445. clk = _clk;
  446. }
  447. }
  448. }
  449. if (clk_idx == -1) {
  450. ufshcd_dme_get(hba, UIC_ARG_MIB(CMN_PWM_CLK_CTRL), &clk_idx);
  451. dev_err(hba->dev,
  452. "failed to decide pwm clock divider, will not change\n");
  453. }
  454. attr->cmn_pwm_clk_ctrl = clk_idx & PWM_CLK_CTRL_MASK;
  455. }
  456. long exynos_ufs_calc_time_cntr(struct exynos_ufs *ufs, long period)
  457. {
  458. const int precise = 10;
  459. long pclk_rate = ufs->pclk_rate;
  460. long clk_period, fraction;
  461. clk_period = UNIPRO_PCLK_PERIOD(ufs);
  462. fraction = ((NSEC_PER_SEC % pclk_rate) * precise) / pclk_rate;
  463. return (period * precise) / ((clk_period * precise) + fraction);
  464. }
  465. static void exynos_ufs_specify_phy_time_attr(struct exynos_ufs *ufs)
  466. {
  467. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  468. struct ufs_phy_time_cfg *t_cfg = &ufs->t_cfg;
  469. t_cfg->tx_linereset_p =
  470. exynos_ufs_calc_time_cntr(ufs, attr->tx_dif_p_nsec);
  471. t_cfg->tx_linereset_n =
  472. exynos_ufs_calc_time_cntr(ufs, attr->tx_dif_n_nsec);
  473. t_cfg->tx_high_z_cnt =
  474. exynos_ufs_calc_time_cntr(ufs, attr->tx_high_z_cnt_nsec);
  475. t_cfg->tx_base_n_val =
  476. exynos_ufs_calc_time_cntr(ufs, attr->tx_base_unit_nsec);
  477. t_cfg->tx_gran_n_val =
  478. exynos_ufs_calc_time_cntr(ufs, attr->tx_gran_unit_nsec);
  479. t_cfg->tx_sleep_cnt =
  480. exynos_ufs_calc_time_cntr(ufs, attr->tx_sleep_cnt);
  481. t_cfg->rx_linereset =
  482. exynos_ufs_calc_time_cntr(ufs, attr->rx_dif_p_nsec);
  483. t_cfg->rx_hibern8_wait =
  484. exynos_ufs_calc_time_cntr(ufs, attr->rx_hibern8_wait_nsec);
  485. t_cfg->rx_base_n_val =
  486. exynos_ufs_calc_time_cntr(ufs, attr->rx_base_unit_nsec);
  487. t_cfg->rx_gran_n_val =
  488. exynos_ufs_calc_time_cntr(ufs, attr->rx_gran_unit_nsec);
  489. t_cfg->rx_sleep_cnt =
  490. exynos_ufs_calc_time_cntr(ufs, attr->rx_sleep_cnt);
  491. t_cfg->rx_stall_cnt =
  492. exynos_ufs_calc_time_cntr(ufs, attr->rx_stall_cnt);
  493. }
  494. static void exynos_ufs_config_phy_time_attr(struct exynos_ufs *ufs)
  495. {
  496. struct ufs_hba *hba = ufs->hba;
  497. struct ufs_phy_time_cfg *t_cfg = &ufs->t_cfg;
  498. int i;
  499. exynos_ufs_set_pwm_clk_div(ufs);
  500. exynos_ufs_enable_ov_tm(hba);
  501. for_each_ufs_rx_lane(ufs, i) {
  502. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RX_FILLER_ENABLE, i),
  503. ufs->drv_data->uic_attr->rx_filler_enable);
  504. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RX_LINERESET_VAL, i),
  505. RX_LINERESET(t_cfg->rx_linereset));
  506. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RX_BASE_NVAL_07_00, i),
  507. RX_BASE_NVAL_L(t_cfg->rx_base_n_val));
  508. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RX_BASE_NVAL_15_08, i),
  509. RX_BASE_NVAL_H(t_cfg->rx_base_n_val));
  510. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RX_GRAN_NVAL_07_00, i),
  511. RX_GRAN_NVAL_L(t_cfg->rx_gran_n_val));
  512. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RX_GRAN_NVAL_10_08, i),
  513. RX_GRAN_NVAL_H(t_cfg->rx_gran_n_val));
  514. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RX_OV_SLEEP_CNT_TIMER, i),
  515. RX_OV_SLEEP_CNT(t_cfg->rx_sleep_cnt));
  516. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(RX_OV_STALL_CNT_TIMER, i),
  517. RX_OV_STALL_CNT(t_cfg->rx_stall_cnt));
  518. }
  519. for_each_ufs_tx_lane(ufs, i) {
  520. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_LINERESET_P_VAL, i),
  521. TX_LINERESET_P(t_cfg->tx_linereset_p));
  522. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_HIGH_Z_CNT_07_00, i),
  523. TX_HIGH_Z_CNT_L(t_cfg->tx_high_z_cnt));
  524. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_HIGH_Z_CNT_11_08, i),
  525. TX_HIGH_Z_CNT_H(t_cfg->tx_high_z_cnt));
  526. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_BASE_NVAL_07_00, i),
  527. TX_BASE_NVAL_L(t_cfg->tx_base_n_val));
  528. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_BASE_NVAL_15_08, i),
  529. TX_BASE_NVAL_H(t_cfg->tx_base_n_val));
  530. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_GRAN_NVAL_07_00, i),
  531. TX_GRAN_NVAL_L(t_cfg->tx_gran_n_val));
  532. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_GRAN_NVAL_10_08, i),
  533. TX_GRAN_NVAL_H(t_cfg->tx_gran_n_val));
  534. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_OV_SLEEP_CNT_TIMER, i),
  535. TX_OV_H8_ENTER_EN |
  536. TX_OV_SLEEP_CNT(t_cfg->tx_sleep_cnt));
  537. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_MIN_ACTIVATETIME, i),
  538. ufs->drv_data->uic_attr->tx_min_activatetime);
  539. }
  540. exynos_ufs_disable_ov_tm(hba);
  541. }
  542. static void exynos_ufs_config_phy_cap_attr(struct exynos_ufs *ufs)
  543. {
  544. struct ufs_hba *hba = ufs->hba;
  545. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  546. int i;
  547. exynos_ufs_enable_ov_tm(hba);
  548. for_each_ufs_rx_lane(ufs, i) {
  549. ufshcd_dme_set(hba,
  550. UIC_ARG_MIB_SEL(RX_HS_G1_SYNC_LENGTH_CAP, i),
  551. attr->rx_hs_g1_sync_len_cap);
  552. ufshcd_dme_set(hba,
  553. UIC_ARG_MIB_SEL(RX_HS_G2_SYNC_LENGTH_CAP, i),
  554. attr->rx_hs_g2_sync_len_cap);
  555. ufshcd_dme_set(hba,
  556. UIC_ARG_MIB_SEL(RX_HS_G3_SYNC_LENGTH_CAP, i),
  557. attr->rx_hs_g3_sync_len_cap);
  558. ufshcd_dme_set(hba,
  559. UIC_ARG_MIB_SEL(RX_HS_G1_PREP_LENGTH_CAP, i),
  560. attr->rx_hs_g1_prep_sync_len_cap);
  561. ufshcd_dme_set(hba,
  562. UIC_ARG_MIB_SEL(RX_HS_G2_PREP_LENGTH_CAP, i),
  563. attr->rx_hs_g2_prep_sync_len_cap);
  564. ufshcd_dme_set(hba,
  565. UIC_ARG_MIB_SEL(RX_HS_G3_PREP_LENGTH_CAP, i),
  566. attr->rx_hs_g3_prep_sync_len_cap);
  567. }
  568. if (attr->rx_adv_fine_gran_sup_en == 0) {
  569. for_each_ufs_rx_lane(ufs, i) {
  570. ufshcd_dme_set(hba,
  571. UIC_ARG_MIB_SEL(RX_ADV_GRANULARITY_CAP, i), 0);
  572. if (attr->rx_min_actv_time_cap)
  573. ufshcd_dme_set(hba,
  574. UIC_ARG_MIB_SEL(
  575. RX_MIN_ACTIVATETIME_CAPABILITY, i),
  576. attr->rx_min_actv_time_cap);
  577. if (attr->rx_hibern8_time_cap)
  578. ufshcd_dme_set(hba,
  579. UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAP, i),
  580. attr->rx_hibern8_time_cap);
  581. }
  582. } else if (attr->rx_adv_fine_gran_sup_en == 1) {
  583. for_each_ufs_rx_lane(ufs, i) {
  584. if (attr->rx_adv_fine_gran_step)
  585. ufshcd_dme_set(hba,
  586. UIC_ARG_MIB_SEL(RX_ADV_GRANULARITY_CAP,
  587. i), RX_ADV_FINE_GRAN_STEP(
  588. attr->rx_adv_fine_gran_step));
  589. if (attr->rx_adv_min_actv_time_cap)
  590. ufshcd_dme_set(hba,
  591. UIC_ARG_MIB_SEL(
  592. RX_ADV_MIN_ACTIVATETIME_CAP, i),
  593. attr->rx_adv_min_actv_time_cap);
  594. if (attr->rx_adv_hibern8_time_cap)
  595. ufshcd_dme_set(hba,
  596. UIC_ARG_MIB_SEL(RX_ADV_HIBERN8TIME_CAP,
  597. i),
  598. attr->rx_adv_hibern8_time_cap);
  599. }
  600. }
  601. exynos_ufs_disable_ov_tm(hba);
  602. }
  603. static void exynos_ufs_establish_connt(struct exynos_ufs *ufs)
  604. {
  605. struct ufs_hba *hba = ufs->hba;
  606. enum {
  607. DEV_ID = 0x00,
  608. PEER_DEV_ID = 0x01,
  609. PEER_CPORT_ID = 0x00,
  610. TRAFFIC_CLASS = 0x00,
  611. };
  612. /* allow cport attributes to be set */
  613. ufshcd_dme_set(hba, UIC_ARG_MIB(T_CONNECTIONSTATE), CPORT_IDLE);
  614. /* local unipro attributes */
  615. ufshcd_dme_set(hba, UIC_ARG_MIB(N_DEVICEID), DEV_ID);
  616. ufshcd_dme_set(hba, UIC_ARG_MIB(N_DEVICEID_VALID), true);
  617. ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERDEVICEID), PEER_DEV_ID);
  618. ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERCPORTID), PEER_CPORT_ID);
  619. ufshcd_dme_set(hba, UIC_ARG_MIB(T_CPORTFLAGS), CPORT_DEF_FLAGS);
  620. ufshcd_dme_set(hba, UIC_ARG_MIB(T_TRAFFICCLASS), TRAFFIC_CLASS);
  621. ufshcd_dme_set(hba, UIC_ARG_MIB(T_CONNECTIONSTATE), CPORT_CONNECTED);
  622. }
  623. static void exynos_ufs_config_smu(struct exynos_ufs *ufs)
  624. {
  625. u32 reg, val;
  626. if (ufs->opts & EXYNOS_UFS_OPT_UFSPR_SECURE)
  627. return;
  628. exynos_ufs_disable_auto_ctrl_hcc_save(ufs, &val);
  629. /* make encryption disabled by default */
  630. reg = ufsp_readl(ufs, UFSPRSECURITY);
  631. ufsp_writel(ufs, reg | NSSMU, UFSPRSECURITY);
  632. ufsp_writel(ufs, 0x0, UFSPSBEGIN0);
  633. ufsp_writel(ufs, 0xffffffff, UFSPSEND0);
  634. ufsp_writel(ufs, 0xff, UFSPSLUN0);
  635. ufsp_writel(ufs, 0xf1, UFSPSCTRL0);
  636. exynos_ufs_auto_ctrl_hcc_restore(ufs, &val);
  637. }
  638. static void exynos_ufs_config_sync_pattern_mask(struct exynos_ufs *ufs,
  639. struct ufs_pa_layer_attr *pwr)
  640. {
  641. struct ufs_hba *hba = ufs->hba;
  642. u8 g = max_t(u32, pwr->gear_rx, pwr->gear_tx);
  643. u32 mask, sync_len;
  644. enum {
  645. SYNC_LEN_G1 = 80 * 1000, /* 80us */
  646. SYNC_LEN_G2 = 40 * 1000, /* 44us */
  647. SYNC_LEN_G3 = 20 * 1000, /* 20us */
  648. };
  649. int i;
  650. if (g == 1)
  651. sync_len = SYNC_LEN_G1;
  652. else if (g == 2)
  653. sync_len = SYNC_LEN_G2;
  654. else if (g == 3)
  655. sync_len = SYNC_LEN_G3;
  656. else
  657. return;
  658. mask = exynos_ufs_calc_time_cntr(ufs, sync_len);
  659. mask = (mask >> 8) & 0xff;
  660. exynos_ufs_enable_ov_tm(hba);
  661. for_each_ufs_rx_lane(ufs, i)
  662. ufshcd_dme_set(hba,
  663. UIC_ARG_MIB_SEL(RX_SYNC_MASK_LENGTH, i), mask);
  664. exynos_ufs_disable_ov_tm(hba);
  665. }
  666. static int exynos_ufs_pre_pwr_mode(struct ufs_hba *hba,
  667. struct ufs_pa_layer_attr *dev_max_params,
  668. struct ufs_pa_layer_attr *dev_req_params)
  669. {
  670. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  671. struct phy *generic_phy = ufs->phy;
  672. struct ufs_host_params host_params;
  673. int ret;
  674. if (!dev_req_params) {
  675. pr_err("%s: incoming dev_req_params is NULL\n", __func__);
  676. ret = -EINVAL;
  677. goto out;
  678. }
  679. ufshcd_init_host_params(&host_params);
  680. ret = ufshcd_negotiate_pwr_params(&host_params, dev_max_params, dev_req_params);
  681. if (ret) {
  682. pr_err("%s: failed to determine capabilities\n", __func__);
  683. goto out;
  684. }
  685. if (ufs->drv_data->pre_pwr_change)
  686. ufs->drv_data->pre_pwr_change(ufs, dev_req_params);
  687. if (ufshcd_is_hs_mode(dev_req_params)) {
  688. exynos_ufs_config_sync_pattern_mask(ufs, dev_req_params);
  689. switch (dev_req_params->hs_rate) {
  690. case PA_HS_MODE_A:
  691. case PA_HS_MODE_B:
  692. phy_calibrate(generic_phy);
  693. break;
  694. }
  695. }
  696. /* setting for three timeout values for traffic class #0 */
  697. ufshcd_dme_set(hba, UIC_ARG_MIB(DL_FC0PROTTIMEOUTVAL), 8064);
  698. ufshcd_dme_set(hba, UIC_ARG_MIB(DL_TC0REPLAYTIMEOUTVAL), 28224);
  699. ufshcd_dme_set(hba, UIC_ARG_MIB(DL_AFC0REQTIMEOUTVAL), 20160);
  700. return 0;
  701. out:
  702. return ret;
  703. }
  704. #define PWR_MODE_STR_LEN 64
  705. static int exynos_ufs_post_pwr_mode(struct ufs_hba *hba,
  706. struct ufs_pa_layer_attr *pwr_req)
  707. {
  708. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  709. struct phy *generic_phy = ufs->phy;
  710. int gear = max_t(u32, pwr_req->gear_rx, pwr_req->gear_tx);
  711. int lanes = max_t(u32, pwr_req->lane_rx, pwr_req->lane_tx);
  712. char pwr_str[PWR_MODE_STR_LEN] = "";
  713. /* let default be PWM Gear 1, Lane 1 */
  714. if (!gear)
  715. gear = 1;
  716. if (!lanes)
  717. lanes = 1;
  718. if (ufs->drv_data->post_pwr_change)
  719. ufs->drv_data->post_pwr_change(ufs, pwr_req);
  720. if ((ufshcd_is_hs_mode(pwr_req))) {
  721. switch (pwr_req->hs_rate) {
  722. case PA_HS_MODE_A:
  723. case PA_HS_MODE_B:
  724. phy_calibrate(generic_phy);
  725. break;
  726. }
  727. snprintf(pwr_str, PWR_MODE_STR_LEN, "%s series_%s G_%d L_%d",
  728. "FAST", pwr_req->hs_rate == PA_HS_MODE_A ? "A" : "B",
  729. gear, lanes);
  730. } else {
  731. snprintf(pwr_str, PWR_MODE_STR_LEN, "%s G_%d L_%d",
  732. "SLOW", gear, lanes);
  733. }
  734. dev_info(hba->dev, "Power mode changed to : %s\n", pwr_str);
  735. return 0;
  736. }
  737. static void exynos_ufs_specify_nexus_t_xfer_req(struct ufs_hba *hba,
  738. int tag, bool is_scsi_cmd)
  739. {
  740. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  741. u32 type;
  742. type = hci_readl(ufs, HCI_UTRL_NEXUS_TYPE);
  743. if (is_scsi_cmd)
  744. hci_writel(ufs, type | (1 << tag), HCI_UTRL_NEXUS_TYPE);
  745. else
  746. hci_writel(ufs, type & ~(1 << tag), HCI_UTRL_NEXUS_TYPE);
  747. }
  748. static void exynos_ufs_specify_nexus_t_tm_req(struct ufs_hba *hba,
  749. int tag, u8 func)
  750. {
  751. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  752. u32 type;
  753. type = hci_readl(ufs, HCI_UTMRL_NEXUS_TYPE);
  754. switch (func) {
  755. case UFS_ABORT_TASK:
  756. case UFS_QUERY_TASK:
  757. hci_writel(ufs, type | (1 << tag), HCI_UTMRL_NEXUS_TYPE);
  758. break;
  759. case UFS_ABORT_TASK_SET:
  760. case UFS_CLEAR_TASK_SET:
  761. case UFS_LOGICAL_RESET:
  762. case UFS_QUERY_TASK_SET:
  763. hci_writel(ufs, type & ~(1 << tag), HCI_UTMRL_NEXUS_TYPE);
  764. break;
  765. }
  766. }
  767. static int exynos_ufs_phy_init(struct exynos_ufs *ufs)
  768. {
  769. struct ufs_hba *hba = ufs->hba;
  770. struct phy *generic_phy = ufs->phy;
  771. int ret = 0;
  772. if (ufs->avail_ln_rx == 0 || ufs->avail_ln_tx == 0) {
  773. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_AVAILRXDATALANES),
  774. &ufs->avail_ln_rx);
  775. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_AVAILTXDATALANES),
  776. &ufs->avail_ln_tx);
  777. WARN(ufs->avail_ln_rx != ufs->avail_ln_tx,
  778. "available data lane is not equal(rx:%d, tx:%d)\n",
  779. ufs->avail_ln_rx, ufs->avail_ln_tx);
  780. }
  781. phy_set_bus_width(generic_phy, ufs->avail_ln_rx);
  782. if (generic_phy->power_count) {
  783. phy_power_off(generic_phy);
  784. phy_exit(generic_phy);
  785. }
  786. ret = phy_init(generic_phy);
  787. if (ret) {
  788. dev_err(hba->dev, "%s: phy init failed, ret = %d\n",
  789. __func__, ret);
  790. return ret;
  791. }
  792. ret = phy_power_on(generic_phy);
  793. if (ret)
  794. goto out_exit_phy;
  795. return 0;
  796. out_exit_phy:
  797. phy_exit(generic_phy);
  798. return ret;
  799. }
  800. static void exynos_ufs_config_unipro(struct exynos_ufs *ufs)
  801. {
  802. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  803. struct ufs_hba *hba = ufs->hba;
  804. if (attr->pa_dbg_clk_period_off)
  805. ufshcd_dme_set(hba, UIC_ARG_MIB(attr->pa_dbg_clk_period_off),
  806. DIV_ROUND_UP(NSEC_PER_SEC, ufs->mclk_rate));
  807. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTRAILINGCLOCKS),
  808. ufs->drv_data->uic_attr->tx_trailingclks);
  809. if (attr->pa_dbg_opt_suite1_off)
  810. ufshcd_dme_set(hba, UIC_ARG_MIB(attr->pa_dbg_opt_suite1_off),
  811. attr->pa_dbg_opt_suite1_val);
  812. if (attr->pa_dbg_opt_suite2_off)
  813. ufshcd_dme_set(hba, UIC_ARG_MIB(attr->pa_dbg_opt_suite2_off),
  814. attr->pa_dbg_opt_suite2_val);
  815. }
  816. static void exynos_ufs_config_intr(struct exynos_ufs *ufs, u32 errs, u8 index)
  817. {
  818. switch (index) {
  819. case UNIPRO_L1_5:
  820. hci_writel(ufs, DFES_ERR_EN | errs, HCI_ERR_EN_PA_LAYER);
  821. break;
  822. case UNIPRO_L2:
  823. hci_writel(ufs, DFES_ERR_EN | errs, HCI_ERR_EN_DL_LAYER);
  824. break;
  825. case UNIPRO_L3:
  826. hci_writel(ufs, DFES_ERR_EN | errs, HCI_ERR_EN_N_LAYER);
  827. break;
  828. case UNIPRO_L4:
  829. hci_writel(ufs, DFES_ERR_EN | errs, HCI_ERR_EN_T_LAYER);
  830. break;
  831. case UNIPRO_DME:
  832. hci_writel(ufs, DFES_ERR_EN | errs, HCI_ERR_EN_DME_LAYER);
  833. break;
  834. }
  835. }
  836. static int exynos_ufs_setup_clocks(struct ufs_hba *hba, bool on,
  837. enum ufs_notify_change_status status)
  838. {
  839. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  840. if (!ufs)
  841. return 0;
  842. if (on && status == PRE_CHANGE) {
  843. if (ufs->opts & EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL)
  844. exynos_ufs_disable_auto_ctrl_hcc(ufs);
  845. exynos_ufs_ungate_clks(ufs);
  846. } else if (!on && status == POST_CHANGE) {
  847. exynos_ufs_gate_clks(ufs);
  848. if (ufs->opts & EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL)
  849. exynos_ufs_enable_auto_ctrl_hcc(ufs);
  850. }
  851. return 0;
  852. }
  853. static int exynos_ufs_pre_link(struct ufs_hba *hba)
  854. {
  855. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  856. /* hci */
  857. exynos_ufs_config_intr(ufs, DFES_DEF_L2_ERRS, UNIPRO_L2);
  858. exynos_ufs_config_intr(ufs, DFES_DEF_L3_ERRS, UNIPRO_L3);
  859. exynos_ufs_config_intr(ufs, DFES_DEF_L4_ERRS, UNIPRO_L4);
  860. exynos_ufs_set_unipro_pclk_div(ufs);
  861. exynos_ufs_setup_clocks(hba, true, PRE_CHANGE);
  862. /* unipro */
  863. exynos_ufs_config_unipro(ufs);
  864. if (ufs->drv_data->pre_link)
  865. ufs->drv_data->pre_link(ufs);
  866. /* m-phy */
  867. exynos_ufs_phy_init(ufs);
  868. if (!(ufs->opts & EXYNOS_UFS_OPT_SKIP_CONFIG_PHY_ATTR)) {
  869. exynos_ufs_config_phy_time_attr(ufs);
  870. exynos_ufs_config_phy_cap_attr(ufs);
  871. }
  872. return 0;
  873. }
  874. static void exynos_ufs_fit_aggr_timeout(struct exynos_ufs *ufs)
  875. {
  876. u32 val;
  877. /* Select function clock (mclk) for timer tick */
  878. if (ufs->opts & EXYNOS_UFS_OPT_TIMER_TICK_SELECT) {
  879. val = hci_readl(ufs, HCI_V2P1_CTRL);
  880. val |= IA_TICK_SEL;
  881. hci_writel(ufs, val, HCI_V2P1_CTRL);
  882. }
  883. val = exynos_ufs_calc_time_cntr(ufs, IATOVAL_NSEC / CNTR_DIV_VAL);
  884. hci_writel(ufs, val & CNT_VAL_1US_MASK, HCI_1US_TO_CNT_VAL);
  885. }
  886. static int exynos_ufs_post_link(struct ufs_hba *hba)
  887. {
  888. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  889. struct phy *generic_phy = ufs->phy;
  890. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  891. u32 val = ilog2(DATA_UNIT_SIZE);
  892. exynos_ufs_establish_connt(ufs);
  893. exynos_ufs_fit_aggr_timeout(ufs);
  894. hci_writel(ufs, 0xa, HCI_DATA_REORDER);
  895. if (hba->caps & UFSHCD_CAP_CRYPTO)
  896. val |= PRDT_PREFETCH_EN;
  897. hci_writel(ufs, val, HCI_TXPRDT_ENTRY_SIZE);
  898. hci_writel(ufs, ilog2(DATA_UNIT_SIZE), HCI_RXPRDT_ENTRY_SIZE);
  899. hci_writel(ufs, BIT(hba->nutrs) - 1, HCI_UTRL_NEXUS_TYPE);
  900. hci_writel(ufs, BIT(hba->nutmrs) - 1, HCI_UTMRL_NEXUS_TYPE);
  901. hci_writel(ufs, 0xf, HCI_AXIDMA_RWDATA_BURST_LEN);
  902. if (ufs->opts & EXYNOS_UFS_OPT_SKIP_CONNECTION_ESTAB)
  903. ufshcd_dme_set(hba,
  904. UIC_ARG_MIB(T_DBG_SKIP_INIT_HIBERN8_EXIT), true);
  905. if (attr->pa_granularity) {
  906. exynos_ufs_enable_dbg_mode(hba);
  907. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_GRANULARITY),
  908. attr->pa_granularity);
  909. exynos_ufs_disable_dbg_mode(hba);
  910. if (attr->pa_tactivate)
  911. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
  912. attr->pa_tactivate);
  913. if (attr->pa_hibern8time &&
  914. !(ufs->opts & EXYNOS_UFS_OPT_USE_SW_HIBERN8_TIMER))
  915. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
  916. attr->pa_hibern8time);
  917. }
  918. if (ufs->opts & EXYNOS_UFS_OPT_USE_SW_HIBERN8_TIMER) {
  919. if (!attr->pa_granularity)
  920. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
  921. &attr->pa_granularity);
  922. if (!attr->pa_hibern8time)
  923. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
  924. &attr->pa_hibern8time);
  925. /*
  926. * not wait for HIBERN8 time to exit hibernation
  927. */
  928. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), 0);
  929. if (attr->pa_granularity < 1 || attr->pa_granularity > 6) {
  930. /* Valid range for granularity: 1 ~ 6 */
  931. dev_warn(hba->dev,
  932. "%s: pa_granularity %d is invalid, assuming backwards compatibility\n",
  933. __func__,
  934. attr->pa_granularity);
  935. attr->pa_granularity = 6;
  936. }
  937. }
  938. phy_calibrate(generic_phy);
  939. if (ufs->drv_data->post_link)
  940. ufs->drv_data->post_link(ufs);
  941. return 0;
  942. }
  943. static int exynos_ufs_parse_dt(struct device *dev, struct exynos_ufs *ufs)
  944. {
  945. struct device_node *np = dev->of_node;
  946. struct exynos_ufs_uic_attr *attr;
  947. int ret = 0;
  948. ufs->drv_data = device_get_match_data(dev);
  949. if (ufs->drv_data && ufs->drv_data->uic_attr) {
  950. attr = ufs->drv_data->uic_attr;
  951. } else {
  952. dev_err(dev, "failed to get uic attributes\n");
  953. ret = -EINVAL;
  954. goto out;
  955. }
  956. ufs->sysreg = syscon_regmap_lookup_by_phandle(np, "samsung,sysreg");
  957. if (IS_ERR(ufs->sysreg))
  958. ufs->sysreg = NULL;
  959. else {
  960. if (of_property_read_u32_index(np, "samsung,sysreg", 1,
  961. &ufs->iocc_offset)) {
  962. dev_warn(dev, "can't get an offset from sysreg. Set to default value\n");
  963. ufs->iocc_offset = UFS_SHAREABILITY_OFFSET;
  964. }
  965. }
  966. ufs->iocc_mask = ufs->drv_data->iocc_mask;
  967. /*
  968. * no 'dma-coherent' property means the descriptors are
  969. * non-cacheable so iocc shareability should be disabled.
  970. */
  971. if (of_dma_is_coherent(dev->of_node))
  972. ufs->iocc_val = ufs->iocc_mask;
  973. else
  974. ufs->iocc_val = 0;
  975. ufs->pclk_avail_min = PCLK_AVAIL_MIN;
  976. ufs->pclk_avail_max = PCLK_AVAIL_MAX;
  977. attr->rx_adv_fine_gran_sup_en = RX_ADV_FINE_GRAN_SUP_EN;
  978. attr->rx_adv_fine_gran_step = RX_ADV_FINE_GRAN_STEP_VAL;
  979. attr->rx_adv_min_actv_time_cap = RX_ADV_MIN_ACTV_TIME_CAP;
  980. attr->pa_granularity = PA_GRANULARITY_VAL;
  981. attr->pa_tactivate = PA_TACTIVATE_VAL;
  982. attr->pa_hibern8time = PA_HIBERN8TIME_VAL;
  983. out:
  984. return ret;
  985. }
  986. static inline void exynos_ufs_priv_init(struct ufs_hba *hba,
  987. struct exynos_ufs *ufs)
  988. {
  989. ufs->hba = hba;
  990. ufs->opts = ufs->drv_data->opts;
  991. ufs->rx_sel_idx = PA_MAXDATALANES;
  992. if (ufs->opts & EXYNOS_UFS_OPT_BROKEN_RX_SEL_IDX)
  993. ufs->rx_sel_idx = 0;
  994. hba->priv = (void *)ufs;
  995. hba->quirks = ufs->drv_data->quirks;
  996. }
  997. #ifdef CONFIG_SCSI_UFS_CRYPTO
  998. /*
  999. * Support for Flash Memory Protector (FMP), which is the inline encryption
  1000. * hardware on Exynos and Exynos-based SoCs. The interface to this hardware is
  1001. * not compatible with the standard UFS crypto. It requires that encryption be
  1002. * configured in the PRDT using a nonstandard extension.
  1003. */
  1004. enum fmp_crypto_algo_mode {
  1005. FMP_BYPASS_MODE = 0,
  1006. FMP_ALGO_MODE_AES_CBC = 1,
  1007. FMP_ALGO_MODE_AES_XTS = 2,
  1008. };
  1009. enum fmp_crypto_key_length {
  1010. FMP_KEYLEN_256BIT = 1,
  1011. };
  1012. /**
  1013. * struct fmp_sg_entry - nonstandard format of PRDT entries when FMP is enabled
  1014. *
  1015. * @base: The standard PRDT entry, but with nonstandard bitfields in the high
  1016. * bits of the 'size' field, i.e. the last 32-bit word. When these
  1017. * nonstandard bitfields are zero, the data segment won't be encrypted or
  1018. * decrypted. Otherwise they specify the algorithm and key length with
  1019. * which the data segment will be encrypted or decrypted.
  1020. * @file_iv: The initialization vector (IV) with all bytes reversed
  1021. * @file_enckey: The first half of the AES-XTS key with all bytes reserved
  1022. * @file_twkey: The second half of the AES-XTS key with all bytes reserved
  1023. * @disk_iv: Unused
  1024. * @reserved: Unused
  1025. */
  1026. struct fmp_sg_entry {
  1027. struct ufshcd_sg_entry base;
  1028. __be64 file_iv[2];
  1029. __be64 file_enckey[4];
  1030. __be64 file_twkey[4];
  1031. __be64 disk_iv[2];
  1032. __be64 reserved[2];
  1033. };
  1034. #define SMC_CMD_FMP_SECURITY \
  1035. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64, \
  1036. ARM_SMCCC_OWNER_SIP, 0x1810)
  1037. #define SMC_CMD_SMU \
  1038. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64, \
  1039. ARM_SMCCC_OWNER_SIP, 0x1850)
  1040. #define SMC_CMD_FMP_SMU_RESUME \
  1041. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64, \
  1042. ARM_SMCCC_OWNER_SIP, 0x1860)
  1043. #define SMU_EMBEDDED 0
  1044. #define SMU_INIT 0
  1045. #define CFG_DESCTYPE_3 3
  1046. static void exynos_ufs_fmp_init(struct ufs_hba *hba, struct exynos_ufs *ufs)
  1047. {
  1048. struct blk_crypto_profile *profile = &hba->crypto_profile;
  1049. struct arm_smccc_res res;
  1050. int err;
  1051. /*
  1052. * Check for the standard crypto support bit, since it's available even
  1053. * though the rest of the interface to FMP is nonstandard.
  1054. *
  1055. * This check should have the effect of preventing the driver from
  1056. * trying to use FMP on old Exynos SoCs that don't have FMP.
  1057. */
  1058. if (!(ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES) &
  1059. MASK_CRYPTO_SUPPORT))
  1060. return;
  1061. /*
  1062. * The below sequence of SMC calls to enable FMP can be found in the
  1063. * downstream driver source for gs101 and other Exynos-based SoCs. It
  1064. * is the only way to enable FMP that works on SoCs such as gs101 that
  1065. * don't make the FMP registers accessible to Linux. It probably works
  1066. * on other Exynos-based SoCs too, and might even still be the only way
  1067. * that works. But this hasn't been properly tested, and this code is
  1068. * mutually exclusive with exynos_ufs_config_smu(). So for now only
  1069. * enable FMP support on SoCs with EXYNOS_UFS_OPT_UFSPR_SECURE.
  1070. */
  1071. if (!(ufs->opts & EXYNOS_UFS_OPT_UFSPR_SECURE))
  1072. return;
  1073. /*
  1074. * This call (which sets DESCTYPE to 0x3 in the FMPSECURITY0 register)
  1075. * is needed to make the hardware use the larger PRDT entry size.
  1076. */
  1077. BUILD_BUG_ON(sizeof(struct fmp_sg_entry) != 128);
  1078. arm_smccc_smc(SMC_CMD_FMP_SECURITY, 0, SMU_EMBEDDED, CFG_DESCTYPE_3,
  1079. 0, 0, 0, 0, &res);
  1080. if (res.a0) {
  1081. dev_warn(hba->dev,
  1082. "SMC_CMD_FMP_SECURITY failed on init: %ld. Disabling FMP support.\n",
  1083. res.a0);
  1084. return;
  1085. }
  1086. ufshcd_set_sg_entry_size(hba, sizeof(struct fmp_sg_entry));
  1087. /*
  1088. * This is needed to initialize FMP. Without it, errors occur when
  1089. * inline encryption is used.
  1090. */
  1091. arm_smccc_smc(SMC_CMD_SMU, SMU_INIT, SMU_EMBEDDED, 0, 0, 0, 0, 0, &res);
  1092. if (res.a0) {
  1093. dev_err(hba->dev,
  1094. "SMC_CMD_SMU(SMU_INIT) failed: %ld. Disabling FMP support.\n",
  1095. res.a0);
  1096. return;
  1097. }
  1098. /* Advertise crypto capabilities to the block layer. */
  1099. err = devm_blk_crypto_profile_init(hba->dev, profile, 0);
  1100. if (err) {
  1101. /* Only ENOMEM should be possible here. */
  1102. dev_err(hba->dev, "Failed to initialize crypto profile: %d\n",
  1103. err);
  1104. return;
  1105. }
  1106. profile->max_dun_bytes_supported = AES_BLOCK_SIZE;
  1107. profile->dev = hba->dev;
  1108. profile->modes_supported[BLK_ENCRYPTION_MODE_AES_256_XTS] =
  1109. DATA_UNIT_SIZE;
  1110. /* Advertise crypto support to ufshcd-core. */
  1111. hba->caps |= UFSHCD_CAP_CRYPTO;
  1112. /* Advertise crypto quirks to ufshcd-core. */
  1113. hba->quirks |= UFSHCD_QUIRK_CUSTOM_CRYPTO_PROFILE |
  1114. UFSHCD_QUIRK_BROKEN_CRYPTO_ENABLE |
  1115. UFSHCD_QUIRK_KEYS_IN_PRDT;
  1116. }
  1117. static void exynos_ufs_fmp_resume(struct ufs_hba *hba)
  1118. {
  1119. struct arm_smccc_res res;
  1120. if (!(hba->caps & UFSHCD_CAP_CRYPTO))
  1121. return;
  1122. arm_smccc_smc(SMC_CMD_FMP_SECURITY, 0, SMU_EMBEDDED, CFG_DESCTYPE_3,
  1123. 0, 0, 0, 0, &res);
  1124. if (res.a0)
  1125. dev_err(hba->dev,
  1126. "SMC_CMD_FMP_SECURITY failed on resume: %ld\n", res.a0);
  1127. arm_smccc_smc(SMC_CMD_FMP_SMU_RESUME, 0, SMU_EMBEDDED, 0, 0, 0, 0, 0,
  1128. &res);
  1129. if (res.a0)
  1130. dev_err(hba->dev,
  1131. "SMC_CMD_FMP_SMU_RESUME failed: %ld\n", res.a0);
  1132. }
  1133. static inline __be64 fmp_key_word(const u8 *key, int j)
  1134. {
  1135. return cpu_to_be64(get_unaligned_le64(
  1136. key + AES_KEYSIZE_256 - (j + 1) * sizeof(u64)));
  1137. }
  1138. /* Fill the PRDT for a request according to the given encryption context. */
  1139. static int exynos_ufs_fmp_fill_prdt(struct ufs_hba *hba,
  1140. const struct bio_crypt_ctx *crypt_ctx,
  1141. void *prdt, unsigned int num_segments)
  1142. {
  1143. struct fmp_sg_entry *fmp_prdt = prdt;
  1144. const u8 *enckey = crypt_ctx->bc_key->raw;
  1145. const u8 *twkey = enckey + AES_KEYSIZE_256;
  1146. u64 dun_lo = crypt_ctx->bc_dun[0];
  1147. u64 dun_hi = crypt_ctx->bc_dun[1];
  1148. unsigned int i;
  1149. /* If FMP wasn't enabled, we shouldn't get any encrypted requests. */
  1150. if (WARN_ON_ONCE(!(hba->caps & UFSHCD_CAP_CRYPTO)))
  1151. return -EIO;
  1152. /* Configure FMP on each segment of the request. */
  1153. for (i = 0; i < num_segments; i++) {
  1154. struct fmp_sg_entry *prd = &fmp_prdt[i];
  1155. int j;
  1156. /* Each segment must be exactly one data unit. */
  1157. if (prd->base.size != cpu_to_le32(DATA_UNIT_SIZE - 1)) {
  1158. dev_err(hba->dev,
  1159. "data segment is misaligned for FMP\n");
  1160. return -EIO;
  1161. }
  1162. /* Set the algorithm and key length. */
  1163. prd->base.size |= cpu_to_le32((FMP_ALGO_MODE_AES_XTS << 28) |
  1164. (FMP_KEYLEN_256BIT << 26));
  1165. /* Set the IV. */
  1166. prd->file_iv[0] = cpu_to_be64(dun_hi);
  1167. prd->file_iv[1] = cpu_to_be64(dun_lo);
  1168. /* Set the key. */
  1169. for (j = 0; j < AES_KEYSIZE_256 / sizeof(u64); j++) {
  1170. prd->file_enckey[j] = fmp_key_word(enckey, j);
  1171. prd->file_twkey[j] = fmp_key_word(twkey, j);
  1172. }
  1173. /* Increment the data unit number. */
  1174. dun_lo++;
  1175. if (dun_lo == 0)
  1176. dun_hi++;
  1177. }
  1178. return 0;
  1179. }
  1180. #else /* CONFIG_SCSI_UFS_CRYPTO */
  1181. static void exynos_ufs_fmp_init(struct ufs_hba *hba, struct exynos_ufs *ufs)
  1182. {
  1183. }
  1184. static void exynos_ufs_fmp_resume(struct ufs_hba *hba)
  1185. {
  1186. }
  1187. #define exynos_ufs_fmp_fill_prdt NULL
  1188. #endif /* !CONFIG_SCSI_UFS_CRYPTO */
  1189. static int exynos_ufs_init(struct ufs_hba *hba)
  1190. {
  1191. struct device *dev = hba->dev;
  1192. struct platform_device *pdev = to_platform_device(dev);
  1193. struct exynos_ufs *ufs;
  1194. int ret;
  1195. ufs = devm_kzalloc(dev, sizeof(*ufs), GFP_KERNEL);
  1196. if (!ufs)
  1197. return -ENOMEM;
  1198. /* exynos-specific hci */
  1199. ufs->reg_hci = devm_platform_ioremap_resource_byname(pdev, "vs_hci");
  1200. if (IS_ERR(ufs->reg_hci)) {
  1201. dev_err(dev, "cannot ioremap for hci vendor register\n");
  1202. return PTR_ERR(ufs->reg_hci);
  1203. }
  1204. /* unipro */
  1205. ufs->reg_unipro = devm_platform_ioremap_resource_byname(pdev, "unipro");
  1206. if (IS_ERR(ufs->reg_unipro)) {
  1207. dev_err(dev, "cannot ioremap for unipro register\n");
  1208. return PTR_ERR(ufs->reg_unipro);
  1209. }
  1210. /* ufs protector */
  1211. ufs->reg_ufsp = devm_platform_ioremap_resource_byname(pdev, "ufsp");
  1212. if (IS_ERR(ufs->reg_ufsp)) {
  1213. dev_err(dev, "cannot ioremap for ufs protector register\n");
  1214. return PTR_ERR(ufs->reg_ufsp);
  1215. }
  1216. ret = exynos_ufs_parse_dt(dev, ufs);
  1217. if (ret) {
  1218. dev_err(dev, "failed to get dt info.\n");
  1219. goto out;
  1220. }
  1221. ufs->phy = devm_phy_get(dev, "ufs-phy");
  1222. if (IS_ERR(ufs->phy)) {
  1223. ret = PTR_ERR(ufs->phy);
  1224. dev_err(dev, "failed to get ufs-phy\n");
  1225. goto out;
  1226. }
  1227. exynos_ufs_priv_init(hba, ufs);
  1228. exynos_ufs_fmp_init(hba, ufs);
  1229. if (ufs->drv_data->drv_init) {
  1230. ret = ufs->drv_data->drv_init(ufs);
  1231. if (ret) {
  1232. dev_err(dev, "failed to init drv-data\n");
  1233. goto out;
  1234. }
  1235. }
  1236. ret = exynos_ufs_get_clk_info(ufs);
  1237. if (ret)
  1238. goto out;
  1239. exynos_ufs_specify_phy_time_attr(ufs);
  1240. exynos_ufs_config_smu(ufs);
  1241. hba->host->dma_alignment = DATA_UNIT_SIZE - 1;
  1242. return 0;
  1243. out:
  1244. hba->priv = NULL;
  1245. return ret;
  1246. }
  1247. static void exynos_ufs_exit(struct ufs_hba *hba)
  1248. {
  1249. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  1250. phy_power_off(ufs->phy);
  1251. phy_exit(ufs->phy);
  1252. }
  1253. static int exynos_ufs_host_reset(struct ufs_hba *hba)
  1254. {
  1255. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  1256. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  1257. u32 val;
  1258. int ret = 0;
  1259. exynos_ufs_disable_auto_ctrl_hcc_save(ufs, &val);
  1260. hci_writel(ufs, UFS_SW_RST_MASK, HCI_SW_RST);
  1261. do {
  1262. if (!(hci_readl(ufs, HCI_SW_RST) & UFS_SW_RST_MASK))
  1263. goto out;
  1264. } while (time_before(jiffies, timeout));
  1265. dev_err(hba->dev, "timeout host sw-reset\n");
  1266. ret = -ETIMEDOUT;
  1267. out:
  1268. exynos_ufs_auto_ctrl_hcc_restore(ufs, &val);
  1269. return ret;
  1270. }
  1271. static void exynos_ufs_dev_hw_reset(struct ufs_hba *hba)
  1272. {
  1273. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  1274. hci_writel(ufs, 0 << 0, HCI_GPIO_OUT);
  1275. udelay(5);
  1276. hci_writel(ufs, 1 << 0, HCI_GPIO_OUT);
  1277. }
  1278. static void exynos_ufs_pre_hibern8(struct ufs_hba *hba, enum uic_cmd_dme cmd)
  1279. {
  1280. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  1281. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  1282. if (cmd == UIC_CMD_DME_HIBER_EXIT) {
  1283. if (ufs->opts & EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL)
  1284. exynos_ufs_disable_auto_ctrl_hcc(ufs);
  1285. exynos_ufs_ungate_clks(ufs);
  1286. if (ufs->opts & EXYNOS_UFS_OPT_USE_SW_HIBERN8_TIMER) {
  1287. static const unsigned int granularity_tbl[] = {
  1288. 1, 4, 8, 16, 32, 100
  1289. };
  1290. int h8_time = attr->pa_hibern8time *
  1291. granularity_tbl[attr->pa_granularity - 1];
  1292. unsigned long us;
  1293. s64 delta;
  1294. do {
  1295. delta = h8_time - ktime_us_delta(ktime_get(),
  1296. ufs->entry_hibern8_t);
  1297. if (delta <= 0)
  1298. break;
  1299. us = min_t(s64, delta, USEC_PER_MSEC);
  1300. if (us >= 10)
  1301. usleep_range(us, us + 10);
  1302. } while (1);
  1303. }
  1304. }
  1305. }
  1306. static void exynos_ufs_post_hibern8(struct ufs_hba *hba, enum uic_cmd_dme cmd)
  1307. {
  1308. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  1309. if (cmd == UIC_CMD_DME_HIBER_EXIT) {
  1310. u32 cur_mode = 0;
  1311. u32 pwrmode;
  1312. if (ufshcd_is_hs_mode(&ufs->dev_req_params))
  1313. pwrmode = FAST_MODE;
  1314. else
  1315. pwrmode = SLOW_MODE;
  1316. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &cur_mode);
  1317. if (cur_mode != (pwrmode << 4 | pwrmode)) {
  1318. dev_warn(hba->dev, "%s: power mode change\n", __func__);
  1319. hba->pwr_info.pwr_rx = (cur_mode >> 4) & 0xf;
  1320. hba->pwr_info.pwr_tx = cur_mode & 0xf;
  1321. ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
  1322. }
  1323. if (!(ufs->opts & EXYNOS_UFS_OPT_SKIP_CONNECTION_ESTAB))
  1324. exynos_ufs_establish_connt(ufs);
  1325. } else if (cmd == UIC_CMD_DME_HIBER_ENTER) {
  1326. ufs->entry_hibern8_t = ktime_get();
  1327. exynos_ufs_gate_clks(ufs);
  1328. if (ufs->opts & EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL)
  1329. exynos_ufs_enable_auto_ctrl_hcc(ufs);
  1330. }
  1331. }
  1332. static int exynos_ufs_hce_enable_notify(struct ufs_hba *hba,
  1333. enum ufs_notify_change_status status)
  1334. {
  1335. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  1336. int ret = 0;
  1337. switch (status) {
  1338. case PRE_CHANGE:
  1339. /*
  1340. * The maximum segment size must be set after scsi_host_alloc()
  1341. * has been called and before LUN scanning starts
  1342. * (ufshcd_async_scan()). Note: this callback may also be called
  1343. * from other functions than ufshcd_init().
  1344. */
  1345. hba->host->max_segment_size = DATA_UNIT_SIZE;
  1346. if (ufs->drv_data->pre_hce_enable) {
  1347. ret = ufs->drv_data->pre_hce_enable(ufs);
  1348. if (ret)
  1349. return ret;
  1350. }
  1351. ret = exynos_ufs_host_reset(hba);
  1352. if (ret)
  1353. return ret;
  1354. exynos_ufs_dev_hw_reset(hba);
  1355. break;
  1356. case POST_CHANGE:
  1357. exynos_ufs_calc_pwm_clk_div(ufs);
  1358. if (!(ufs->opts & EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL))
  1359. exynos_ufs_enable_auto_ctrl_hcc(ufs);
  1360. if (ufs->drv_data->post_hce_enable)
  1361. ret = ufs->drv_data->post_hce_enable(ufs);
  1362. break;
  1363. }
  1364. return ret;
  1365. }
  1366. static int exynos_ufs_link_startup_notify(struct ufs_hba *hba,
  1367. enum ufs_notify_change_status status)
  1368. {
  1369. int ret = 0;
  1370. switch (status) {
  1371. case PRE_CHANGE:
  1372. ret = exynos_ufs_pre_link(hba);
  1373. break;
  1374. case POST_CHANGE:
  1375. ret = exynos_ufs_post_link(hba);
  1376. break;
  1377. }
  1378. return ret;
  1379. }
  1380. static int exynos_ufs_pwr_change_notify(struct ufs_hba *hba,
  1381. enum ufs_notify_change_status status,
  1382. struct ufs_pa_layer_attr *dev_max_params,
  1383. struct ufs_pa_layer_attr *dev_req_params)
  1384. {
  1385. int ret = 0;
  1386. switch (status) {
  1387. case PRE_CHANGE:
  1388. ret = exynos_ufs_pre_pwr_mode(hba, dev_max_params,
  1389. dev_req_params);
  1390. break;
  1391. case POST_CHANGE:
  1392. ret = exynos_ufs_post_pwr_mode(hba, dev_req_params);
  1393. break;
  1394. }
  1395. return ret;
  1396. }
  1397. static void exynos_ufs_hibern8_notify(struct ufs_hba *hba,
  1398. enum uic_cmd_dme cmd,
  1399. enum ufs_notify_change_status notify)
  1400. {
  1401. switch ((u8)notify) {
  1402. case PRE_CHANGE:
  1403. exynos_ufs_pre_hibern8(hba, cmd);
  1404. break;
  1405. case POST_CHANGE:
  1406. exynos_ufs_post_hibern8(hba, cmd);
  1407. break;
  1408. }
  1409. }
  1410. static int gs101_ufs_suspend(struct exynos_ufs *ufs)
  1411. {
  1412. hci_writel(ufs, 0 << 0, HCI_GPIO_OUT);
  1413. return 0;
  1414. }
  1415. static int exynos_ufs_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
  1416. enum ufs_notify_change_status status)
  1417. {
  1418. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  1419. if (status == PRE_CHANGE)
  1420. return 0;
  1421. if (ufs->drv_data->suspend)
  1422. ufs->drv_data->suspend(ufs);
  1423. if (!ufshcd_is_link_active(hba))
  1424. phy_power_off(ufs->phy);
  1425. return 0;
  1426. }
  1427. static int exynos_ufs_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
  1428. {
  1429. struct exynos_ufs *ufs = ufshcd_get_variant(hba);
  1430. if (!ufshcd_is_link_active(hba))
  1431. phy_power_on(ufs->phy);
  1432. exynos_ufs_config_smu(ufs);
  1433. exynos_ufs_fmp_resume(hba);
  1434. return 0;
  1435. }
  1436. static int exynosauto_ufs_vh_link_startup_notify(struct ufs_hba *hba,
  1437. enum ufs_notify_change_status status)
  1438. {
  1439. if (status == POST_CHANGE) {
  1440. ufshcd_set_link_active(hba);
  1441. ufshcd_set_ufs_dev_active(hba);
  1442. }
  1443. return 0;
  1444. }
  1445. static int exynosauto_ufs_vh_wait_ph_ready(struct ufs_hba *hba)
  1446. {
  1447. u32 mbox;
  1448. ktime_t start, stop;
  1449. start = ktime_get();
  1450. stop = ktime_add(start, ms_to_ktime(PH_READY_TIMEOUT_MS));
  1451. do {
  1452. mbox = ufshcd_readl(hba, PH2VH_MBOX);
  1453. /* TODO: Mailbox message protocols between the PH and VHs are
  1454. * not implemented yet. This will be supported later
  1455. */
  1456. if ((mbox & MH_MSG_MASK) == MH_MSG_PH_READY)
  1457. return 0;
  1458. usleep_range(40, 50);
  1459. } while (ktime_before(ktime_get(), stop));
  1460. return -ETIME;
  1461. }
  1462. static int exynosauto_ufs_vh_init(struct ufs_hba *hba)
  1463. {
  1464. struct device *dev = hba->dev;
  1465. struct platform_device *pdev = to_platform_device(dev);
  1466. struct exynos_ufs *ufs;
  1467. int ret;
  1468. ufs = devm_kzalloc(dev, sizeof(*ufs), GFP_KERNEL);
  1469. if (!ufs)
  1470. return -ENOMEM;
  1471. /* exynos-specific hci */
  1472. ufs->reg_hci = devm_platform_ioremap_resource_byname(pdev, "vs_hci");
  1473. if (IS_ERR(ufs->reg_hci)) {
  1474. dev_err(dev, "cannot ioremap for hci vendor register\n");
  1475. return PTR_ERR(ufs->reg_hci);
  1476. }
  1477. ret = exynosauto_ufs_vh_wait_ph_ready(hba);
  1478. if (ret)
  1479. return ret;
  1480. ufs->drv_data = device_get_match_data(dev);
  1481. if (!ufs->drv_data)
  1482. return -ENODEV;
  1483. exynos_ufs_priv_init(hba, ufs);
  1484. return 0;
  1485. }
  1486. static int fsd_ufs_pre_link(struct exynos_ufs *ufs)
  1487. {
  1488. struct exynos_ufs_uic_attr *attr = ufs->drv_data->uic_attr;
  1489. struct ufs_hba *hba = ufs->hba;
  1490. int i;
  1491. ufshcd_dme_set(hba, UIC_ARG_MIB(attr->pa_dbg_clk_period_off),
  1492. DIV_ROUND_UP(NSEC_PER_SEC, ufs->mclk_rate));
  1493. ufshcd_dme_set(hba, UIC_ARG_MIB(0x201), 0x12);
  1494. ufshcd_dme_set(hba, UIC_ARG_MIB(0x200), 0x40);
  1495. for_each_ufs_tx_lane(ufs, i) {
  1496. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0xAA, i),
  1497. DIV_ROUND_UP(NSEC_PER_SEC, ufs->mclk_rate));
  1498. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x8F, i), 0x3F);
  1499. }
  1500. for_each_ufs_rx_lane(ufs, i) {
  1501. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x12, i),
  1502. DIV_ROUND_UP(NSEC_PER_SEC, ufs->mclk_rate));
  1503. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x5C, i), 0x38);
  1504. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x0F, i), 0x0);
  1505. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x65, i), 0x1);
  1506. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x69, i), 0x1);
  1507. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x21, i), 0x0);
  1508. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x22, i), 0x0);
  1509. }
  1510. ufshcd_dme_set(hba, UIC_ARG_MIB(0x200), 0x0);
  1511. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_DBG_AUTOMODE_THLD), 0x4E20);
  1512. ufshcd_dme_set(hba, UIC_ARG_MIB(attr->pa_dbg_opt_suite1_off),
  1513. 0x2e820183);
  1514. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), 0x0);
  1515. exynos_ufs_establish_connt(ufs);
  1516. return 0;
  1517. }
  1518. static int fsd_ufs_post_link(struct exynos_ufs *ufs)
  1519. {
  1520. int i;
  1521. struct ufs_hba *hba = ufs->hba;
  1522. u32 hw_cap_min_tactivate;
  1523. u32 peer_rx_min_actv_time_cap;
  1524. u32 max_rx_hibern8_time_cap;
  1525. ufshcd_dme_get(hba, UIC_ARG_MIB_SEL(0x8F, 4),
  1526. &hw_cap_min_tactivate); /* HW Capability of MIN_TACTIVATE */
  1527. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
  1528. &peer_rx_min_actv_time_cap); /* PA_TActivate */
  1529. ufshcd_dme_get(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
  1530. &max_rx_hibern8_time_cap); /* PA_Hibern8Time */
  1531. if (peer_rx_min_actv_time_cap >= hw_cap_min_tactivate)
  1532. ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
  1533. peer_rx_min_actv_time_cap + 1);
  1534. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), max_rx_hibern8_time_cap + 1);
  1535. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_DBG_MODE), 0x01);
  1536. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_SAVECONFIGTIME), 0xFA);
  1537. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_DBG_MODE), 0x00);
  1538. ufshcd_dme_set(hba, UIC_ARG_MIB(0x200), 0x40);
  1539. for_each_ufs_rx_lane(ufs, i) {
  1540. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x35, i), 0x05);
  1541. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x73, i), 0x01);
  1542. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x41, i), 0x02);
  1543. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x42, i), 0xAC);
  1544. }
  1545. ufshcd_dme_set(hba, UIC_ARG_MIB(0x200), 0x0);
  1546. return 0;
  1547. }
  1548. static int fsd_ufs_pre_pwr_change(struct exynos_ufs *ufs,
  1549. struct ufs_pa_layer_attr *pwr)
  1550. {
  1551. struct ufs_hba *hba = ufs->hba;
  1552. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), 0x1);
  1553. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), 0x1);
  1554. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), 12000);
  1555. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), 32000);
  1556. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), 16000);
  1557. unipro_writel(ufs, 12000, UNIPRO_DME_POWERMODE_REQ_REMOTEL2TIMER0);
  1558. unipro_writel(ufs, 32000, UNIPRO_DME_POWERMODE_REQ_REMOTEL2TIMER1);
  1559. unipro_writel(ufs, 16000, UNIPRO_DME_POWERMODE_REQ_REMOTEL2TIMER2);
  1560. return 0;
  1561. }
  1562. static inline u32 get_mclk_period_unipro_18(struct exynos_ufs *ufs)
  1563. {
  1564. return (16 * 1000 * 1000000UL / ufs->mclk_rate);
  1565. }
  1566. static int gs101_ufs_pre_link(struct exynos_ufs *ufs)
  1567. {
  1568. struct ufs_hba *hba = ufs->hba;
  1569. int i;
  1570. u32 tx_line_reset_period, rx_line_reset_period;
  1571. rx_line_reset_period = (RX_LINE_RESET_TIME * ufs->mclk_rate)
  1572. / NSEC_PER_MSEC;
  1573. tx_line_reset_period = (TX_LINE_RESET_TIME * ufs->mclk_rate)
  1574. / NSEC_PER_MSEC;
  1575. unipro_writel(ufs, get_mclk_period_unipro_18(ufs), COMP_CLK_PERIOD);
  1576. ufshcd_dme_set(hba, UIC_ARG_MIB(0x200), 0x40);
  1577. for_each_ufs_rx_lane(ufs, i) {
  1578. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_CLK_PRD, i),
  1579. DIV_ROUND_UP(NSEC_PER_SEC, ufs->mclk_rate));
  1580. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_CLK_PRD_EN, i), 0x0);
  1581. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_VALUE2, i),
  1582. (rx_line_reset_period >> 16) & 0xFF);
  1583. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_VALUE1, i),
  1584. (rx_line_reset_period >> 8) & 0xFF);
  1585. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_RX_LINERESET_VALUE0, i),
  1586. (rx_line_reset_period) & 0xFF);
  1587. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x2f, i), 0x69);
  1588. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x84, i), 0x1);
  1589. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x25, i), 0xf6);
  1590. }
  1591. for_each_ufs_tx_lane(ufs, i) {
  1592. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_CLK_PRD, i),
  1593. DIV_ROUND_UP(NSEC_PER_SEC, ufs->mclk_rate));
  1594. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_CLK_PRD_EN, i),
  1595. 0x02);
  1596. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_LINERESET_PVALUE2, i),
  1597. (tx_line_reset_period >> 16) & 0xFF);
  1598. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_LINERESET_PVALUE1, i),
  1599. (tx_line_reset_period >> 8) & 0xFF);
  1600. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(VND_TX_LINERESET_PVALUE0, i),
  1601. (tx_line_reset_period) & 0xFF);
  1602. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x04, i), 1);
  1603. ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(0x7F, i), 0);
  1604. }
  1605. ufshcd_dme_set(hba, UIC_ARG_MIB(0x200), 0x0);
  1606. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), 0x0);
  1607. ufshcd_dme_set(hba, UIC_ARG_MIB(N_DEVICEID), 0x0);
  1608. ufshcd_dme_set(hba, UIC_ARG_MIB(N_DEVICEID_VALID), 0x1);
  1609. ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERDEVICEID), 0x1);
  1610. ufshcd_dme_set(hba, UIC_ARG_MIB(T_CONNECTIONSTATE), CPORT_CONNECTED);
  1611. ufshcd_dme_set(hba, UIC_ARG_MIB(0xA006), 0x8000);
  1612. return 0;
  1613. }
  1614. static int gs101_ufs_post_link(struct exynos_ufs *ufs)
  1615. {
  1616. struct ufs_hba *hba = ufs->hba;
  1617. exynos_ufs_enable_dbg_mode(hba);
  1618. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_SAVECONFIGTIME), 0x3e8);
  1619. exynos_ufs_disable_dbg_mode(hba);
  1620. return 0;
  1621. }
  1622. static int gs101_ufs_pre_pwr_change(struct exynos_ufs *ufs,
  1623. struct ufs_pa_layer_attr *pwr)
  1624. {
  1625. struct ufs_hba *hba = ufs->hba;
  1626. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), 12000);
  1627. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), 32000);
  1628. ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), 16000);
  1629. unipro_writel(ufs, 8064, UNIPRO_DME_POWERMODE_REQ_LOCALL2TIMER0);
  1630. unipro_writel(ufs, 28224, UNIPRO_DME_POWERMODE_REQ_LOCALL2TIMER1);
  1631. unipro_writel(ufs, 20160, UNIPRO_DME_POWERMODE_REQ_LOCALL2TIMER2);
  1632. unipro_writel(ufs, 12000, UNIPRO_DME_POWERMODE_REQ_REMOTEL2TIMER0);
  1633. unipro_writel(ufs, 32000, UNIPRO_DME_POWERMODE_REQ_REMOTEL2TIMER1);
  1634. unipro_writel(ufs, 16000, UNIPRO_DME_POWERMODE_REQ_REMOTEL2TIMER2);
  1635. return 0;
  1636. }
  1637. static const struct ufs_hba_variant_ops ufs_hba_exynos_ops = {
  1638. .name = "exynos_ufs",
  1639. .init = exynos_ufs_init,
  1640. .exit = exynos_ufs_exit,
  1641. .hce_enable_notify = exynos_ufs_hce_enable_notify,
  1642. .link_startup_notify = exynos_ufs_link_startup_notify,
  1643. .pwr_change_notify = exynos_ufs_pwr_change_notify,
  1644. .setup_clocks = exynos_ufs_setup_clocks,
  1645. .setup_xfer_req = exynos_ufs_specify_nexus_t_xfer_req,
  1646. .setup_task_mgmt = exynos_ufs_specify_nexus_t_tm_req,
  1647. .hibern8_notify = exynos_ufs_hibern8_notify,
  1648. .suspend = exynos_ufs_suspend,
  1649. .resume = exynos_ufs_resume,
  1650. .fill_crypto_prdt = exynos_ufs_fmp_fill_prdt,
  1651. };
  1652. static struct ufs_hba_variant_ops ufs_hba_exynosauto_vh_ops = {
  1653. .name = "exynosauto_ufs_vh",
  1654. .init = exynosauto_ufs_vh_init,
  1655. .link_startup_notify = exynosauto_ufs_vh_link_startup_notify,
  1656. };
  1657. static int exynos_ufs_probe(struct platform_device *pdev)
  1658. {
  1659. int err;
  1660. struct device *dev = &pdev->dev;
  1661. const struct ufs_hba_variant_ops *vops = &ufs_hba_exynos_ops;
  1662. const struct exynos_ufs_drv_data *drv_data =
  1663. device_get_match_data(dev);
  1664. if (drv_data && drv_data->vops)
  1665. vops = drv_data->vops;
  1666. err = ufshcd_pltfrm_init(pdev, vops);
  1667. if (err)
  1668. dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
  1669. return err;
  1670. }
  1671. static void exynos_ufs_remove(struct platform_device *pdev)
  1672. {
  1673. ufshcd_pltfrm_remove(pdev);
  1674. }
  1675. static struct exynos_ufs_uic_attr exynos7_uic_attr = {
  1676. .tx_trailingclks = 0x10,
  1677. .tx_dif_p_nsec = 3000000, /* unit: ns */
  1678. .tx_dif_n_nsec = 1000000, /* unit: ns */
  1679. .tx_high_z_cnt_nsec = 20000, /* unit: ns */
  1680. .tx_base_unit_nsec = 100000, /* unit: ns */
  1681. .tx_gran_unit_nsec = 4000, /* unit: ns */
  1682. .tx_sleep_cnt = 1000, /* unit: ns */
  1683. .tx_min_activatetime = 0xa,
  1684. .rx_filler_enable = 0x2,
  1685. .rx_dif_p_nsec = 1000000, /* unit: ns */
  1686. .rx_hibern8_wait_nsec = 4000000, /* unit: ns */
  1687. .rx_base_unit_nsec = 100000, /* unit: ns */
  1688. .rx_gran_unit_nsec = 4000, /* unit: ns */
  1689. .rx_sleep_cnt = 1280, /* unit: ns */
  1690. .rx_stall_cnt = 320, /* unit: ns */
  1691. .rx_hs_g1_sync_len_cap = SYNC_LEN_COARSE(0xf),
  1692. .rx_hs_g2_sync_len_cap = SYNC_LEN_COARSE(0xf),
  1693. .rx_hs_g3_sync_len_cap = SYNC_LEN_COARSE(0xf),
  1694. .rx_hs_g1_prep_sync_len_cap = PREP_LEN(0xf),
  1695. .rx_hs_g2_prep_sync_len_cap = PREP_LEN(0xf),
  1696. .rx_hs_g3_prep_sync_len_cap = PREP_LEN(0xf),
  1697. .pa_dbg_clk_period_off = PA_DBG_CLK_PERIOD,
  1698. .pa_dbg_opt_suite1_val = 0x30103,
  1699. .pa_dbg_opt_suite1_off = PA_DBG_OPTION_SUITE,
  1700. };
  1701. static const struct exynos_ufs_drv_data exynosauto_ufs_drvs = {
  1702. .uic_attr = &exynos7_uic_attr,
  1703. .quirks = UFSHCD_QUIRK_PRDT_BYTE_GRAN |
  1704. UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR |
  1705. UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR |
  1706. UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING,
  1707. .opts = EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL |
  1708. EXYNOS_UFS_OPT_SKIP_CONFIG_PHY_ATTR |
  1709. EXYNOS_UFS_OPT_BROKEN_RX_SEL_IDX,
  1710. .iocc_mask = UFS_EXYNOSAUTO_SHARABLE,
  1711. .drv_init = exynosauto_ufs_drv_init,
  1712. .post_hce_enable = exynosauto_ufs_post_hce_enable,
  1713. .pre_link = exynosauto_ufs_pre_link,
  1714. .pre_pwr_change = exynosauto_ufs_pre_pwr_change,
  1715. .post_pwr_change = exynosauto_ufs_post_pwr_change,
  1716. };
  1717. static const struct exynos_ufs_drv_data exynosauto_ufs_vh_drvs = {
  1718. .vops = &ufs_hba_exynosauto_vh_ops,
  1719. .quirks = UFSHCD_QUIRK_PRDT_BYTE_GRAN |
  1720. UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR |
  1721. UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR |
  1722. UFSHCI_QUIRK_BROKEN_HCE |
  1723. UFSHCD_QUIRK_BROKEN_UIC_CMD |
  1724. UFSHCD_QUIRK_SKIP_PH_CONFIGURATION |
  1725. UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING,
  1726. .opts = EXYNOS_UFS_OPT_BROKEN_RX_SEL_IDX,
  1727. };
  1728. static const struct exynos_ufs_drv_data exynos_ufs_drvs = {
  1729. .uic_attr = &exynos7_uic_attr,
  1730. .quirks = UFSHCD_QUIRK_PRDT_BYTE_GRAN |
  1731. UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR |
  1732. UFSHCI_QUIRK_BROKEN_HCE |
  1733. UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR |
  1734. UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR |
  1735. UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL |
  1736. UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING,
  1737. .opts = EXYNOS_UFS_OPT_HAS_APB_CLK_CTRL |
  1738. EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL |
  1739. EXYNOS_UFS_OPT_BROKEN_RX_SEL_IDX |
  1740. EXYNOS_UFS_OPT_SKIP_CONNECTION_ESTAB |
  1741. EXYNOS_UFS_OPT_USE_SW_HIBERN8_TIMER,
  1742. .pre_link = exynos7_ufs_pre_link,
  1743. .post_link = exynos7_ufs_post_link,
  1744. .pre_pwr_change = exynos7_ufs_pre_pwr_change,
  1745. .post_pwr_change = exynos7_ufs_post_pwr_change,
  1746. };
  1747. static struct exynos_ufs_uic_attr gs101_uic_attr = {
  1748. .tx_trailingclks = 0xff,
  1749. .tx_dif_p_nsec = 3000000, /* unit: ns */
  1750. .tx_dif_n_nsec = 1000000, /* unit: ns */
  1751. .tx_high_z_cnt_nsec = 20000, /* unit: ns */
  1752. .tx_base_unit_nsec = 100000, /* unit: ns */
  1753. .tx_gran_unit_nsec = 4000, /* unit: ns */
  1754. .tx_sleep_cnt = 1000, /* unit: ns */
  1755. .tx_min_activatetime = 0xa,
  1756. .rx_filler_enable = 0x2,
  1757. .rx_dif_p_nsec = 1000000, /* unit: ns */
  1758. .rx_hibern8_wait_nsec = 4000000, /* unit: ns */
  1759. .rx_base_unit_nsec = 100000, /* unit: ns */
  1760. .rx_gran_unit_nsec = 4000, /* unit: ns */
  1761. .rx_sleep_cnt = 1280, /* unit: ns */
  1762. .rx_stall_cnt = 320, /* unit: ns */
  1763. .rx_hs_g1_sync_len_cap = SYNC_LEN_COARSE(0xf),
  1764. .rx_hs_g2_sync_len_cap = SYNC_LEN_COARSE(0xf),
  1765. .rx_hs_g3_sync_len_cap = SYNC_LEN_COARSE(0xf),
  1766. .rx_hs_g1_prep_sync_len_cap = PREP_LEN(0xf),
  1767. .rx_hs_g2_prep_sync_len_cap = PREP_LEN(0xf),
  1768. .rx_hs_g3_prep_sync_len_cap = PREP_LEN(0xf),
  1769. .pa_dbg_opt_suite1_val = 0x90913C1C,
  1770. .pa_dbg_opt_suite1_off = PA_GS101_DBG_OPTION_SUITE1,
  1771. .pa_dbg_opt_suite2_val = 0xE01C115F,
  1772. .pa_dbg_opt_suite2_off = PA_GS101_DBG_OPTION_SUITE2,
  1773. };
  1774. static struct exynos_ufs_uic_attr fsd_uic_attr = {
  1775. .tx_trailingclks = 0x10,
  1776. .tx_dif_p_nsec = 3000000, /* unit: ns */
  1777. .tx_dif_n_nsec = 1000000, /* unit: ns */
  1778. .tx_high_z_cnt_nsec = 20000, /* unit: ns */
  1779. .tx_base_unit_nsec = 100000, /* unit: ns */
  1780. .tx_gran_unit_nsec = 4000, /* unit: ns */
  1781. .tx_sleep_cnt = 1000, /* unit: ns */
  1782. .tx_min_activatetime = 0xa,
  1783. .rx_filler_enable = 0x2,
  1784. .rx_dif_p_nsec = 1000000, /* unit: ns */
  1785. .rx_hibern8_wait_nsec = 4000000, /* unit: ns */
  1786. .rx_base_unit_nsec = 100000, /* unit: ns */
  1787. .rx_gran_unit_nsec = 4000, /* unit: ns */
  1788. .rx_sleep_cnt = 1280, /* unit: ns */
  1789. .rx_stall_cnt = 320, /* unit: ns */
  1790. .rx_hs_g1_sync_len_cap = SYNC_LEN_COARSE(0xf),
  1791. .rx_hs_g2_sync_len_cap = SYNC_LEN_COARSE(0xf),
  1792. .rx_hs_g3_sync_len_cap = SYNC_LEN_COARSE(0xf),
  1793. .rx_hs_g1_prep_sync_len_cap = PREP_LEN(0xf),
  1794. .rx_hs_g2_prep_sync_len_cap = PREP_LEN(0xf),
  1795. .rx_hs_g3_prep_sync_len_cap = PREP_LEN(0xf),
  1796. .pa_dbg_clk_period_off = PA_DBG_CLK_PERIOD,
  1797. .pa_dbg_opt_suite1_val = 0x2E820183,
  1798. .pa_dbg_opt_suite1_off = PA_DBG_OPTION_SUITE,
  1799. };
  1800. static const struct exynos_ufs_drv_data fsd_ufs_drvs = {
  1801. .uic_attr = &fsd_uic_attr,
  1802. .quirks = UFSHCD_QUIRK_PRDT_BYTE_GRAN |
  1803. UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR |
  1804. UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR |
  1805. UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING |
  1806. UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR,
  1807. .opts = EXYNOS_UFS_OPT_HAS_APB_CLK_CTRL |
  1808. EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL |
  1809. EXYNOS_UFS_OPT_SKIP_CONFIG_PHY_ATTR |
  1810. EXYNOS_UFS_OPT_BROKEN_RX_SEL_IDX,
  1811. .pre_link = fsd_ufs_pre_link,
  1812. .post_link = fsd_ufs_post_link,
  1813. .pre_pwr_change = fsd_ufs_pre_pwr_change,
  1814. };
  1815. static const struct exynos_ufs_drv_data gs101_ufs_drvs = {
  1816. .uic_attr = &gs101_uic_attr,
  1817. .quirks = UFSHCD_QUIRK_PRDT_BYTE_GRAN |
  1818. UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR |
  1819. UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR |
  1820. UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR |
  1821. UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL |
  1822. UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING,
  1823. .opts = EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL |
  1824. EXYNOS_UFS_OPT_SKIP_CONFIG_PHY_ATTR |
  1825. EXYNOS_UFS_OPT_UFSPR_SECURE |
  1826. EXYNOS_UFS_OPT_TIMER_TICK_SELECT,
  1827. .iocc_mask = UFS_GS101_SHARABLE,
  1828. .drv_init = gs101_ufs_drv_init,
  1829. .pre_link = gs101_ufs_pre_link,
  1830. .post_link = gs101_ufs_post_link,
  1831. .pre_pwr_change = gs101_ufs_pre_pwr_change,
  1832. .suspend = gs101_ufs_suspend,
  1833. };
  1834. static const struct of_device_id exynos_ufs_of_match[] = {
  1835. { .compatible = "google,gs101-ufs",
  1836. .data = &gs101_ufs_drvs },
  1837. { .compatible = "samsung,exynos7-ufs",
  1838. .data = &exynos_ufs_drvs },
  1839. { .compatible = "samsung,exynosautov9-ufs",
  1840. .data = &exynosauto_ufs_drvs },
  1841. { .compatible = "samsung,exynosautov9-ufs-vh",
  1842. .data = &exynosauto_ufs_vh_drvs },
  1843. { .compatible = "tesla,fsd-ufs",
  1844. .data = &fsd_ufs_drvs },
  1845. {},
  1846. };
  1847. MODULE_DEVICE_TABLE(of, exynos_ufs_of_match);
  1848. static const struct dev_pm_ops exynos_ufs_pm_ops = {
  1849. SET_SYSTEM_SLEEP_PM_OPS(ufshcd_system_suspend, ufshcd_system_resume)
  1850. SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
  1851. .prepare = ufshcd_suspend_prepare,
  1852. .complete = ufshcd_resume_complete,
  1853. };
  1854. static struct platform_driver exynos_ufs_pltform = {
  1855. .probe = exynos_ufs_probe,
  1856. .remove_new = exynos_ufs_remove,
  1857. .driver = {
  1858. .name = "exynos-ufshc",
  1859. .pm = &exynos_ufs_pm_ops,
  1860. .of_match_table = exynos_ufs_of_match,
  1861. },
  1862. };
  1863. module_platform_driver(exynos_ufs_pltform);
  1864. MODULE_AUTHOR("Alim Akhtar <alim.akhtar@samsung.com>");
  1865. MODULE_AUTHOR("Seungwon Jeon <essuuj@gmail.com>");
  1866. MODULE_DESCRIPTION("Exynos UFS HCI Driver");
  1867. MODULE_LICENSE("GPL v2");