ufs-exynos.c 64 KB

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