ahci_imx.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * copyright (c) 2013 Freescale Semiconductor, Inc.
  4. * Freescale IMX AHCI SATA platform driver
  5. *
  6. * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/regmap.h>
  12. #include <linux/ahci_platform.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/of_device.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
  17. #include <linux/libata.h>
  18. #include <linux/hwmon.h>
  19. #include <linux/hwmon-sysfs.h>
  20. #include <linux/thermal.h>
  21. #include "ahci.h"
  22. #define DRV_NAME "ahci-imx"
  23. enum {
  24. /* Timer 1-ms Register */
  25. IMX_TIMER1MS = 0x00e0,
  26. /* Port0 PHY Control Register */
  27. IMX_P0PHYCR = 0x0178,
  28. IMX_P0PHYCR_TEST_PDDQ = 1 << 20,
  29. IMX_P0PHYCR_CR_READ = 1 << 19,
  30. IMX_P0PHYCR_CR_WRITE = 1 << 18,
  31. IMX_P0PHYCR_CR_CAP_DATA = 1 << 17,
  32. IMX_P0PHYCR_CR_CAP_ADDR = 1 << 16,
  33. /* Port0 PHY Status Register */
  34. IMX_P0PHYSR = 0x017c,
  35. IMX_P0PHYSR_CR_ACK = 1 << 18,
  36. IMX_P0PHYSR_CR_DATA_OUT = 0xffff << 0,
  37. /* Lane0 Output Status Register */
  38. IMX_LANE0_OUT_STAT = 0x2003,
  39. IMX_LANE0_OUT_STAT_RX_PLL_STATE = 1 << 1,
  40. /* Clock Reset Register */
  41. IMX_CLOCK_RESET = 0x7f3f,
  42. IMX_CLOCK_RESET_RESET = 1 << 0,
  43. /* IMX8QM HSIO AHCI definitions */
  44. IMX8QM_SATA_PHY_RX_IMPED_RATIO_OFFSET = 0x03,
  45. IMX8QM_SATA_PHY_TX_IMPED_RATIO_OFFSET = 0x09,
  46. IMX8QM_SATA_PHY_IMPED_RATIO_85OHM = 0x6c,
  47. IMX8QM_LPCG_PHYX2_OFFSET = 0x00000,
  48. IMX8QM_CSR_PHYX2_OFFSET = 0x90000,
  49. IMX8QM_CSR_PHYX1_OFFSET = 0xa0000,
  50. IMX8QM_CSR_PHYX_STTS0_OFFSET = 0x4,
  51. IMX8QM_CSR_PCIEA_OFFSET = 0xb0000,
  52. IMX8QM_CSR_PCIEB_OFFSET = 0xc0000,
  53. IMX8QM_CSR_SATA_OFFSET = 0xd0000,
  54. IMX8QM_CSR_PCIE_CTRL2_OFFSET = 0x8,
  55. IMX8QM_CSR_MISC_OFFSET = 0xe0000,
  56. IMX8QM_LPCG_PHYX2_PCLK0_MASK = (0x3 << 16),
  57. IMX8QM_LPCG_PHYX2_PCLK1_MASK = (0x3 << 20),
  58. IMX8QM_PHY_APB_RSTN_0 = BIT(0),
  59. IMX8QM_PHY_MODE_SATA = BIT(19),
  60. IMX8QM_PHY_MODE_MASK = (0xf << 17),
  61. IMX8QM_PHY_PIPE_RSTN_0 = BIT(24),
  62. IMX8QM_PHY_PIPE_RSTN_OVERRIDE_0 = BIT(25),
  63. IMX8QM_PHY_PIPE_RSTN_1 = BIT(26),
  64. IMX8QM_PHY_PIPE_RSTN_OVERRIDE_1 = BIT(27),
  65. IMX8QM_STTS0_LANE0_TX_PLL_LOCK = BIT(4),
  66. IMX8QM_MISC_IOB_RXENA = BIT(0),
  67. IMX8QM_MISC_IOB_TXENA = BIT(1),
  68. IMX8QM_MISC_PHYX1_EPCS_SEL = BIT(12),
  69. IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_1 = BIT(24),
  70. IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_0 = BIT(25),
  71. IMX8QM_MISC_CLKREQN_IN_OVERRIDE_1 = BIT(28),
  72. IMX8QM_MISC_CLKREQN_IN_OVERRIDE_0 = BIT(29),
  73. IMX8QM_SATA_CTRL_RESET_N = BIT(12),
  74. IMX8QM_SATA_CTRL_EPCS_PHYRESET_N = BIT(7),
  75. IMX8QM_CTRL_BUTTON_RST_N = BIT(21),
  76. IMX8QM_CTRL_POWER_UP_RST_N = BIT(23),
  77. IMX8QM_CTRL_LTSSM_ENABLE = BIT(4),
  78. };
  79. enum ahci_imx_type {
  80. AHCI_IMX53,
  81. AHCI_IMX6Q,
  82. AHCI_IMX6QP,
  83. AHCI_IMX8QM,
  84. };
  85. struct imx_ahci_priv {
  86. struct platform_device *ahci_pdev;
  87. enum ahci_imx_type type;
  88. struct clk *sata_clk;
  89. struct clk *sata_ref_clk;
  90. struct clk *ahb_clk;
  91. struct clk *epcs_tx_clk;
  92. struct clk *epcs_rx_clk;
  93. struct clk *phy_apbclk;
  94. struct clk *phy_pclk0;
  95. struct clk *phy_pclk1;
  96. void __iomem *phy_base;
  97. struct gpio_desc *clkreq_gpiod;
  98. struct regmap *gpr;
  99. bool no_device;
  100. bool first_time;
  101. u32 phy_params;
  102. u32 imped_ratio;
  103. };
  104. static int ahci_imx_hotplug;
  105. module_param_named(hotplug, ahci_imx_hotplug, int, 0644);
  106. MODULE_PARM_DESC(hotplug, "AHCI IMX hot-plug support (0=Don't support, 1=support)");
  107. static void ahci_imx_host_stop(struct ata_host *host);
  108. static int imx_phy_crbit_assert(void __iomem *mmio, u32 bit, bool assert)
  109. {
  110. int timeout = 10;
  111. u32 crval;
  112. u32 srval;
  113. /* Assert or deassert the bit */
  114. crval = readl(mmio + IMX_P0PHYCR);
  115. if (assert)
  116. crval |= bit;
  117. else
  118. crval &= ~bit;
  119. writel(crval, mmio + IMX_P0PHYCR);
  120. /* Wait for the cr_ack signal */
  121. do {
  122. srval = readl(mmio + IMX_P0PHYSR);
  123. if ((assert ? srval : ~srval) & IMX_P0PHYSR_CR_ACK)
  124. break;
  125. usleep_range(100, 200);
  126. } while (--timeout);
  127. return timeout ? 0 : -ETIMEDOUT;
  128. }
  129. static int imx_phy_reg_addressing(u16 addr, void __iomem *mmio)
  130. {
  131. u32 crval = addr;
  132. int ret;
  133. /* Supply the address on cr_data_in */
  134. writel(crval, mmio + IMX_P0PHYCR);
  135. /* Assert the cr_cap_addr signal */
  136. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, true);
  137. if (ret)
  138. return ret;
  139. /* Deassert cr_cap_addr */
  140. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_ADDR, false);
  141. if (ret)
  142. return ret;
  143. return 0;
  144. }
  145. static int imx_phy_reg_write(u16 val, void __iomem *mmio)
  146. {
  147. u32 crval = val;
  148. int ret;
  149. /* Supply the data on cr_data_in */
  150. writel(crval, mmio + IMX_P0PHYCR);
  151. /* Assert the cr_cap_data signal */
  152. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, true);
  153. if (ret)
  154. return ret;
  155. /* Deassert cr_cap_data */
  156. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_CAP_DATA, false);
  157. if (ret)
  158. return ret;
  159. if (val & IMX_CLOCK_RESET_RESET) {
  160. /*
  161. * In case we're resetting the phy, it's unable to acknowledge,
  162. * so we return immediately here.
  163. */
  164. crval |= IMX_P0PHYCR_CR_WRITE;
  165. writel(crval, mmio + IMX_P0PHYCR);
  166. goto out;
  167. }
  168. /* Assert the cr_write signal */
  169. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, true);
  170. if (ret)
  171. return ret;
  172. /* Deassert cr_write */
  173. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_WRITE, false);
  174. if (ret)
  175. return ret;
  176. out:
  177. return 0;
  178. }
  179. static int imx_phy_reg_read(u16 *val, void __iomem *mmio)
  180. {
  181. int ret;
  182. /* Assert the cr_read signal */
  183. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, true);
  184. if (ret)
  185. return ret;
  186. /* Capture the data from cr_data_out[] */
  187. *val = readl(mmio + IMX_P0PHYSR) & IMX_P0PHYSR_CR_DATA_OUT;
  188. /* Deassert cr_read */
  189. ret = imx_phy_crbit_assert(mmio, IMX_P0PHYCR_CR_READ, false);
  190. if (ret)
  191. return ret;
  192. return 0;
  193. }
  194. static int imx_sata_phy_reset(struct ahci_host_priv *hpriv)
  195. {
  196. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  197. void __iomem *mmio = hpriv->mmio;
  198. int timeout = 10;
  199. u16 val;
  200. int ret;
  201. if (imxpriv->type == AHCI_IMX6QP) {
  202. /* 6qp adds the sata reset mechanism, use it for 6qp sata */
  203. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
  204. IMX6Q_GPR5_SATA_SW_PD, 0);
  205. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
  206. IMX6Q_GPR5_SATA_SW_RST, 0);
  207. udelay(50);
  208. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
  209. IMX6Q_GPR5_SATA_SW_RST,
  210. IMX6Q_GPR5_SATA_SW_RST);
  211. return 0;
  212. }
  213. /* Reset SATA PHY by setting RESET bit of PHY register CLOCK_RESET */
  214. ret = imx_phy_reg_addressing(IMX_CLOCK_RESET, mmio);
  215. if (ret)
  216. return ret;
  217. ret = imx_phy_reg_write(IMX_CLOCK_RESET_RESET, mmio);
  218. if (ret)
  219. return ret;
  220. /* Wait for PHY RX_PLL to be stable */
  221. do {
  222. usleep_range(100, 200);
  223. ret = imx_phy_reg_addressing(IMX_LANE0_OUT_STAT, mmio);
  224. if (ret)
  225. return ret;
  226. ret = imx_phy_reg_read(&val, mmio);
  227. if (ret)
  228. return ret;
  229. if (val & IMX_LANE0_OUT_STAT_RX_PLL_STATE)
  230. break;
  231. } while (--timeout);
  232. return timeout ? 0 : -ETIMEDOUT;
  233. }
  234. enum {
  235. /* SATA PHY Register */
  236. SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT = 0x0001,
  237. SATA_PHY_CR_CLOCK_DAC_CTL = 0x0008,
  238. SATA_PHY_CR_CLOCK_RTUNE_CTL = 0x0009,
  239. SATA_PHY_CR_CLOCK_ADC_OUT = 0x000A,
  240. SATA_PHY_CR_CLOCK_MPLL_TST = 0x0017,
  241. };
  242. static int read_adc_sum(void *dev, u16 rtune_ctl_reg, void __iomem * mmio)
  243. {
  244. u16 adc_out_reg, read_sum;
  245. u32 index, read_attempt;
  246. const u32 attempt_limit = 200;
  247. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
  248. imx_phy_reg_write(rtune_ctl_reg, mmio);
  249. /* two dummy read */
  250. index = 0;
  251. read_attempt = 0;
  252. adc_out_reg = 0;
  253. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_ADC_OUT, mmio);
  254. while (index < 2) {
  255. imx_phy_reg_read(&adc_out_reg, mmio);
  256. /* check if valid */
  257. if (adc_out_reg & 0x400)
  258. index++;
  259. read_attempt++;
  260. if (read_attempt > attempt_limit) {
  261. dev_err(dev, "Read REG more than %d times!\n",
  262. attempt_limit);
  263. break;
  264. }
  265. }
  266. index = 0;
  267. read_attempt = 0;
  268. read_sum = 0;
  269. while (index < 80) {
  270. imx_phy_reg_read(&adc_out_reg, mmio);
  271. if (adc_out_reg & 0x400) {
  272. read_sum = read_sum + (adc_out_reg & 0x3FF);
  273. index++;
  274. }
  275. read_attempt++;
  276. if (read_attempt > attempt_limit) {
  277. dev_err(dev, "Read REG more than %d times!\n",
  278. attempt_limit);
  279. break;
  280. }
  281. }
  282. /* Use the U32 to make 1000 precision */
  283. return (read_sum * 1000) / 80;
  284. }
  285. /* SATA AHCI temperature monitor */
  286. static int __sata_ahci_read_temperature(void *dev, int *temp)
  287. {
  288. u16 mpll_test_reg, rtune_ctl_reg, dac_ctl_reg, read_sum;
  289. u32 str1, str2, str3, str4;
  290. int m1, m2, a;
  291. struct ahci_host_priv *hpriv = dev_get_drvdata(dev);
  292. void __iomem *mmio = hpriv->mmio;
  293. /* check rd-wr to reg */
  294. read_sum = 0;
  295. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_CRCMP_LT_LIMIT, mmio);
  296. imx_phy_reg_write(read_sum, mmio);
  297. imx_phy_reg_read(&read_sum, mmio);
  298. if ((read_sum & 0xffff) != 0)
  299. dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
  300. imx_phy_reg_write(0x5A5A, mmio);
  301. imx_phy_reg_read(&read_sum, mmio);
  302. if ((read_sum & 0xffff) != 0x5A5A)
  303. dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
  304. imx_phy_reg_write(0x1234, mmio);
  305. imx_phy_reg_read(&read_sum, mmio);
  306. if ((read_sum & 0xffff) != 0x1234)
  307. dev_err(dev, "Read/Write REG error, 0x%x!\n", read_sum);
  308. /* start temperature test */
  309. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
  310. imx_phy_reg_read(&mpll_test_reg, mmio);
  311. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
  312. imx_phy_reg_read(&rtune_ctl_reg, mmio);
  313. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
  314. imx_phy_reg_read(&dac_ctl_reg, mmio);
  315. /* mpll_tst.meas_iv ([12:2]) */
  316. str1 = (mpll_test_reg >> 2) & 0x7FF;
  317. /* rtune_ctl.mode ([1:0]) */
  318. str2 = (rtune_ctl_reg) & 0x3;
  319. /* dac_ctl.dac_mode ([14:12]) */
  320. str3 = (dac_ctl_reg >> 12) & 0x7;
  321. /* rtune_ctl.sel_atbp ([4]) */
  322. str4 = (rtune_ctl_reg >> 4);
  323. /* Calculate the m1 */
  324. /* mpll_tst.meas_iv */
  325. mpll_test_reg = (mpll_test_reg & 0xE03) | (512) << 2;
  326. /* rtune_ctl.mode */
  327. rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (1);
  328. /* dac_ctl.dac_mode */
  329. dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (4) << 12;
  330. /* rtune_ctl.sel_atbp */
  331. rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (0) << 4;
  332. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
  333. imx_phy_reg_write(mpll_test_reg, mmio);
  334. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
  335. imx_phy_reg_write(dac_ctl_reg, mmio);
  336. m1 = read_adc_sum(dev, rtune_ctl_reg, mmio);
  337. /* Calculate the m2 */
  338. /* rtune_ctl.sel_atbp */
  339. rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (1) << 4;
  340. m2 = read_adc_sum(dev, rtune_ctl_reg, mmio);
  341. /* restore the status */
  342. /* mpll_tst.meas_iv */
  343. mpll_test_reg = (mpll_test_reg & 0xE03) | (str1) << 2;
  344. /* rtune_ctl.mode */
  345. rtune_ctl_reg = (rtune_ctl_reg & 0xFFC) | (str2);
  346. /* dac_ctl.dac_mode */
  347. dac_ctl_reg = (dac_ctl_reg & 0x8FF) | (str3) << 12;
  348. /* rtune_ctl.sel_atbp */
  349. rtune_ctl_reg = (rtune_ctl_reg & 0xFEF) | (str4) << 4;
  350. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_MPLL_TST, mmio);
  351. imx_phy_reg_write(mpll_test_reg, mmio);
  352. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_DAC_CTL, mmio);
  353. imx_phy_reg_write(dac_ctl_reg, mmio);
  354. imx_phy_reg_addressing(SATA_PHY_CR_CLOCK_RTUNE_CTL, mmio);
  355. imx_phy_reg_write(rtune_ctl_reg, mmio);
  356. /* Compute temperature */
  357. if (!(m2 / 1000))
  358. m2 = 1000;
  359. a = (m2 - m1) / (m2/1000);
  360. *temp = ((-559) * a * a) / 1000 + (1379) * a + (-458000);
  361. return 0;
  362. }
  363. static int sata_ahci_read_temperature(struct thermal_zone_device *tz, int *temp)
  364. {
  365. return __sata_ahci_read_temperature(tz->devdata, temp);
  366. }
  367. static ssize_t sata_ahci_show_temp(struct device *dev,
  368. struct device_attribute *da,
  369. char *buf)
  370. {
  371. unsigned int temp = 0;
  372. int err;
  373. err = __sata_ahci_read_temperature(dev, &temp);
  374. if (err < 0)
  375. return err;
  376. return sprintf(buf, "%u\n", temp);
  377. }
  378. static const struct thermal_zone_device_ops fsl_sata_ahci_of_thermal_ops = {
  379. .get_temp = sata_ahci_read_temperature,
  380. };
  381. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, sata_ahci_show_temp, NULL, 0);
  382. static struct attribute *fsl_sata_ahci_attrs[] = {
  383. &sensor_dev_attr_temp1_input.dev_attr.attr,
  384. NULL
  385. };
  386. ATTRIBUTE_GROUPS(fsl_sata_ahci);
  387. static int imx8_sata_enable(struct ahci_host_priv *hpriv)
  388. {
  389. u32 val, reg;
  390. int i, ret;
  391. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  392. struct device *dev = &imxpriv->ahci_pdev->dev;
  393. /* configure the hsio for sata */
  394. ret = clk_prepare_enable(imxpriv->phy_pclk0);
  395. if (ret < 0) {
  396. dev_err(dev, "can't enable phy_pclk0.\n");
  397. return ret;
  398. }
  399. ret = clk_prepare_enable(imxpriv->phy_pclk1);
  400. if (ret < 0) {
  401. dev_err(dev, "can't enable phy_pclk1.\n");
  402. goto disable_phy_pclk0;
  403. }
  404. ret = clk_prepare_enable(imxpriv->epcs_tx_clk);
  405. if (ret < 0) {
  406. dev_err(dev, "can't enable epcs_tx_clk.\n");
  407. goto disable_phy_pclk1;
  408. }
  409. ret = clk_prepare_enable(imxpriv->epcs_rx_clk);
  410. if (ret < 0) {
  411. dev_err(dev, "can't enable epcs_rx_clk.\n");
  412. goto disable_epcs_tx_clk;
  413. }
  414. ret = clk_prepare_enable(imxpriv->phy_apbclk);
  415. if (ret < 0) {
  416. dev_err(dev, "can't enable phy_apbclk.\n");
  417. goto disable_epcs_rx_clk;
  418. }
  419. /* Configure PHYx2 PIPE_RSTN */
  420. regmap_read(imxpriv->gpr, IMX8QM_CSR_PCIEA_OFFSET +
  421. IMX8QM_CSR_PCIE_CTRL2_OFFSET, &val);
  422. if ((val & IMX8QM_CTRL_LTSSM_ENABLE) == 0) {
  423. /* The link of the PCIEA of HSIO is down */
  424. regmap_update_bits(imxpriv->gpr,
  425. IMX8QM_CSR_PHYX2_OFFSET,
  426. IMX8QM_PHY_PIPE_RSTN_0 |
  427. IMX8QM_PHY_PIPE_RSTN_OVERRIDE_0,
  428. IMX8QM_PHY_PIPE_RSTN_0 |
  429. IMX8QM_PHY_PIPE_RSTN_OVERRIDE_0);
  430. }
  431. regmap_read(imxpriv->gpr, IMX8QM_CSR_PCIEB_OFFSET +
  432. IMX8QM_CSR_PCIE_CTRL2_OFFSET, &reg);
  433. if ((reg & IMX8QM_CTRL_LTSSM_ENABLE) == 0) {
  434. /* The link of the PCIEB of HSIO is down */
  435. regmap_update_bits(imxpriv->gpr,
  436. IMX8QM_CSR_PHYX2_OFFSET,
  437. IMX8QM_PHY_PIPE_RSTN_1 |
  438. IMX8QM_PHY_PIPE_RSTN_OVERRIDE_1,
  439. IMX8QM_PHY_PIPE_RSTN_1 |
  440. IMX8QM_PHY_PIPE_RSTN_OVERRIDE_1);
  441. }
  442. if (((reg | val) & IMX8QM_CTRL_LTSSM_ENABLE) == 0) {
  443. /* The links of both PCIA and PCIEB of HSIO are down */
  444. regmap_update_bits(imxpriv->gpr,
  445. IMX8QM_LPCG_PHYX2_OFFSET,
  446. IMX8QM_LPCG_PHYX2_PCLK0_MASK |
  447. IMX8QM_LPCG_PHYX2_PCLK1_MASK,
  448. 0);
  449. }
  450. /* set PWR_RST and BT_RST of csr_pciea */
  451. val = IMX8QM_CSR_PCIEA_OFFSET + IMX8QM_CSR_PCIE_CTRL2_OFFSET;
  452. regmap_update_bits(imxpriv->gpr,
  453. val,
  454. IMX8QM_CTRL_BUTTON_RST_N,
  455. IMX8QM_CTRL_BUTTON_RST_N);
  456. regmap_update_bits(imxpriv->gpr,
  457. val,
  458. IMX8QM_CTRL_POWER_UP_RST_N,
  459. IMX8QM_CTRL_POWER_UP_RST_N);
  460. /* PHYX1_MODE to SATA */
  461. regmap_update_bits(imxpriv->gpr,
  462. IMX8QM_CSR_PHYX1_OFFSET,
  463. IMX8QM_PHY_MODE_MASK,
  464. IMX8QM_PHY_MODE_SATA);
  465. /*
  466. * BIT0 RXENA 1, BIT1 TXENA 0
  467. * BIT12 PHY_X1_EPCS_SEL 1.
  468. */
  469. regmap_update_bits(imxpriv->gpr,
  470. IMX8QM_CSR_MISC_OFFSET,
  471. IMX8QM_MISC_IOB_RXENA,
  472. IMX8QM_MISC_IOB_RXENA);
  473. regmap_update_bits(imxpriv->gpr,
  474. IMX8QM_CSR_MISC_OFFSET,
  475. IMX8QM_MISC_IOB_TXENA,
  476. 0);
  477. regmap_update_bits(imxpriv->gpr,
  478. IMX8QM_CSR_MISC_OFFSET,
  479. IMX8QM_MISC_PHYX1_EPCS_SEL,
  480. IMX8QM_MISC_PHYX1_EPCS_SEL);
  481. /*
  482. * It is possible, for PCIe and SATA are sharing
  483. * the same clock source, HPLL or external oscillator.
  484. * When PCIe is in low power modes (L1.X or L2 etc),
  485. * the clock source can be turned off. In this case,
  486. * if this clock source is required to be toggling by
  487. * SATA, then SATA functions will be abnormal.
  488. * Set the override here to avoid it.
  489. */
  490. regmap_update_bits(imxpriv->gpr,
  491. IMX8QM_CSR_MISC_OFFSET,
  492. IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_1 |
  493. IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_0 |
  494. IMX8QM_MISC_CLKREQN_IN_OVERRIDE_1 |
  495. IMX8QM_MISC_CLKREQN_IN_OVERRIDE_0,
  496. IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_1 |
  497. IMX8QM_MISC_CLKREQN_OUT_OVERRIDE_0 |
  498. IMX8QM_MISC_CLKREQN_IN_OVERRIDE_1 |
  499. IMX8QM_MISC_CLKREQN_IN_OVERRIDE_0);
  500. /* clear PHY RST, then set it */
  501. regmap_update_bits(imxpriv->gpr,
  502. IMX8QM_CSR_SATA_OFFSET,
  503. IMX8QM_SATA_CTRL_EPCS_PHYRESET_N,
  504. 0);
  505. regmap_update_bits(imxpriv->gpr,
  506. IMX8QM_CSR_SATA_OFFSET,
  507. IMX8QM_SATA_CTRL_EPCS_PHYRESET_N,
  508. IMX8QM_SATA_CTRL_EPCS_PHYRESET_N);
  509. /* CTRL RST: SET -> delay 1 us -> CLEAR -> SET */
  510. regmap_update_bits(imxpriv->gpr,
  511. IMX8QM_CSR_SATA_OFFSET,
  512. IMX8QM_SATA_CTRL_RESET_N,
  513. IMX8QM_SATA_CTRL_RESET_N);
  514. udelay(1);
  515. regmap_update_bits(imxpriv->gpr,
  516. IMX8QM_CSR_SATA_OFFSET,
  517. IMX8QM_SATA_CTRL_RESET_N,
  518. 0);
  519. regmap_update_bits(imxpriv->gpr,
  520. IMX8QM_CSR_SATA_OFFSET,
  521. IMX8QM_SATA_CTRL_RESET_N,
  522. IMX8QM_SATA_CTRL_RESET_N);
  523. /* APB reset */
  524. regmap_update_bits(imxpriv->gpr,
  525. IMX8QM_CSR_PHYX1_OFFSET,
  526. IMX8QM_PHY_APB_RSTN_0,
  527. IMX8QM_PHY_APB_RSTN_0);
  528. for (i = 0; i < 100; i++) {
  529. reg = IMX8QM_CSR_PHYX1_OFFSET +
  530. IMX8QM_CSR_PHYX_STTS0_OFFSET;
  531. regmap_read(imxpriv->gpr, reg, &val);
  532. val &= IMX8QM_STTS0_LANE0_TX_PLL_LOCK;
  533. if (val == IMX8QM_STTS0_LANE0_TX_PLL_LOCK)
  534. break;
  535. udelay(1);
  536. }
  537. if (val != IMX8QM_STTS0_LANE0_TX_PLL_LOCK) {
  538. dev_err(dev, "TX PLL of the PHY is not locked\n");
  539. ret = -ENODEV;
  540. } else {
  541. writeb(imxpriv->imped_ratio, imxpriv->phy_base +
  542. IMX8QM_SATA_PHY_RX_IMPED_RATIO_OFFSET);
  543. writeb(imxpriv->imped_ratio, imxpriv->phy_base +
  544. IMX8QM_SATA_PHY_TX_IMPED_RATIO_OFFSET);
  545. reg = readb(imxpriv->phy_base +
  546. IMX8QM_SATA_PHY_RX_IMPED_RATIO_OFFSET);
  547. if (unlikely(reg != imxpriv->imped_ratio))
  548. dev_info(dev, "Can't set PHY RX impedance ratio.\n");
  549. reg = readb(imxpriv->phy_base +
  550. IMX8QM_SATA_PHY_TX_IMPED_RATIO_OFFSET);
  551. if (unlikely(reg != imxpriv->imped_ratio))
  552. dev_info(dev, "Can't set PHY TX impedance ratio.\n");
  553. usleep_range(50, 100);
  554. /*
  555. * To reduce the power consumption, gate off
  556. * the PHY clks
  557. */
  558. clk_disable_unprepare(imxpriv->phy_apbclk);
  559. clk_disable_unprepare(imxpriv->phy_pclk1);
  560. clk_disable_unprepare(imxpriv->phy_pclk0);
  561. return ret;
  562. }
  563. clk_disable_unprepare(imxpriv->phy_apbclk);
  564. disable_epcs_rx_clk:
  565. clk_disable_unprepare(imxpriv->epcs_rx_clk);
  566. disable_epcs_tx_clk:
  567. clk_disable_unprepare(imxpriv->epcs_tx_clk);
  568. disable_phy_pclk1:
  569. clk_disable_unprepare(imxpriv->phy_pclk1);
  570. disable_phy_pclk0:
  571. clk_disable_unprepare(imxpriv->phy_pclk0);
  572. return ret;
  573. }
  574. static int imx_sata_enable(struct ahci_host_priv *hpriv)
  575. {
  576. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  577. struct device *dev = &imxpriv->ahci_pdev->dev;
  578. int ret;
  579. if (imxpriv->no_device)
  580. return 0;
  581. ret = ahci_platform_enable_regulators(hpriv);
  582. if (ret)
  583. return ret;
  584. ret = clk_prepare_enable(imxpriv->sata_ref_clk);
  585. if (ret < 0)
  586. goto disable_regulator;
  587. if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
  588. /*
  589. * set PHY Paremeters, two steps to configure the GPR13,
  590. * one write for rest of parameters, mask of first write
  591. * is 0x07ffffff, and the other one write for setting
  592. * the mpll_clk_en.
  593. */
  594. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  595. IMX6Q_GPR13_SATA_RX_EQ_VAL_MASK |
  596. IMX6Q_GPR13_SATA_RX_LOS_LVL_MASK |
  597. IMX6Q_GPR13_SATA_RX_DPLL_MODE_MASK |
  598. IMX6Q_GPR13_SATA_SPD_MODE_MASK |
  599. IMX6Q_GPR13_SATA_MPLL_SS_EN |
  600. IMX6Q_GPR13_SATA_TX_ATTEN_MASK |
  601. IMX6Q_GPR13_SATA_TX_BOOST_MASK |
  602. IMX6Q_GPR13_SATA_TX_LVL_MASK |
  603. IMX6Q_GPR13_SATA_MPLL_CLK_EN |
  604. IMX6Q_GPR13_SATA_TX_EDGE_RATE,
  605. imxpriv->phy_params);
  606. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  607. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  608. IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  609. usleep_range(100, 200);
  610. ret = imx_sata_phy_reset(hpriv);
  611. if (ret) {
  612. dev_err(dev, "failed to reset phy: %d\n", ret);
  613. goto disable_clk;
  614. }
  615. } else if (imxpriv->type == AHCI_IMX8QM) {
  616. ret = imx8_sata_enable(hpriv);
  617. }
  618. usleep_range(1000, 2000);
  619. return 0;
  620. disable_clk:
  621. clk_disable_unprepare(imxpriv->sata_ref_clk);
  622. disable_regulator:
  623. ahci_platform_disable_regulators(hpriv);
  624. return ret;
  625. }
  626. static void imx_sata_disable(struct ahci_host_priv *hpriv)
  627. {
  628. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  629. if (imxpriv->no_device)
  630. return;
  631. switch (imxpriv->type) {
  632. case AHCI_IMX6QP:
  633. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR5,
  634. IMX6Q_GPR5_SATA_SW_PD,
  635. IMX6Q_GPR5_SATA_SW_PD);
  636. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  637. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  638. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  639. break;
  640. case AHCI_IMX6Q:
  641. regmap_update_bits(imxpriv->gpr, IOMUXC_GPR13,
  642. IMX6Q_GPR13_SATA_MPLL_CLK_EN,
  643. !IMX6Q_GPR13_SATA_MPLL_CLK_EN);
  644. break;
  645. case AHCI_IMX8QM:
  646. clk_disable_unprepare(imxpriv->epcs_rx_clk);
  647. clk_disable_unprepare(imxpriv->epcs_tx_clk);
  648. break;
  649. default:
  650. break;
  651. }
  652. clk_disable_unprepare(imxpriv->sata_ref_clk);
  653. ahci_platform_disable_regulators(hpriv);
  654. }
  655. static void ahci_imx_error_handler(struct ata_port *ap)
  656. {
  657. u32 reg_val;
  658. struct ata_device *dev;
  659. struct ata_host *host = dev_get_drvdata(ap->dev);
  660. struct ahci_host_priv *hpriv = host->private_data;
  661. void __iomem *mmio = hpriv->mmio;
  662. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  663. ahci_error_handler(ap);
  664. if (!(imxpriv->first_time) || ahci_imx_hotplug)
  665. return;
  666. imxpriv->first_time = false;
  667. ata_for_each_dev(dev, &ap->link, ENABLED)
  668. return;
  669. /*
  670. * Disable link to save power. An imx ahci port can't be recovered
  671. * without full reset once the pddq mode is enabled making it
  672. * impossible to use as part of libata LPM.
  673. */
  674. reg_val = readl(mmio + IMX_P0PHYCR);
  675. writel(reg_val | IMX_P0PHYCR_TEST_PDDQ, mmio + IMX_P0PHYCR);
  676. imx_sata_disable(hpriv);
  677. imxpriv->no_device = true;
  678. dev_info(ap->dev, "no device found, disabling link.\n");
  679. dev_info(ap->dev, "pass " MODULE_PARAM_PREFIX ".hotplug=1 to enable hotplug\n");
  680. }
  681. static int ahci_imx_softreset(struct ata_link *link, unsigned int *class,
  682. unsigned long deadline)
  683. {
  684. struct ata_port *ap = link->ap;
  685. struct ata_host *host = dev_get_drvdata(ap->dev);
  686. struct ahci_host_priv *hpriv = host->private_data;
  687. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  688. int ret;
  689. if (imxpriv->type == AHCI_IMX53)
  690. ret = ahci_pmp_retry_srst_ops.softreset(link, class, deadline);
  691. else
  692. ret = ahci_ops.softreset(link, class, deadline);
  693. return ret;
  694. }
  695. static struct ata_port_operations ahci_imx_ops = {
  696. .inherits = &ahci_ops,
  697. .host_stop = ahci_imx_host_stop,
  698. .error_handler = ahci_imx_error_handler,
  699. .softreset = ahci_imx_softreset,
  700. };
  701. static const struct ata_port_info ahci_imx_port_info = {
  702. .flags = AHCI_FLAG_COMMON,
  703. .pio_mask = ATA_PIO4,
  704. .udma_mask = ATA_UDMA6,
  705. .port_ops = &ahci_imx_ops,
  706. };
  707. static const struct of_device_id imx_ahci_of_match[] = {
  708. { .compatible = "fsl,imx53-ahci", .data = (void *)AHCI_IMX53 },
  709. { .compatible = "fsl,imx6q-ahci", .data = (void *)AHCI_IMX6Q },
  710. { .compatible = "fsl,imx6qp-ahci", .data = (void *)AHCI_IMX6QP },
  711. { .compatible = "fsl,imx8qm-ahci", .data = (void *)AHCI_IMX8QM },
  712. { /* sentinel */ }
  713. };
  714. MODULE_DEVICE_TABLE(of, imx_ahci_of_match);
  715. struct reg_value {
  716. u32 of_value;
  717. u32 reg_value;
  718. };
  719. struct reg_property {
  720. const char *name;
  721. const struct reg_value *values;
  722. size_t num_values;
  723. u32 def_value;
  724. u32 set_value;
  725. };
  726. static const struct reg_value gpr13_tx_level[] = {
  727. { 937, IMX6Q_GPR13_SATA_TX_LVL_0_937_V },
  728. { 947, IMX6Q_GPR13_SATA_TX_LVL_0_947_V },
  729. { 957, IMX6Q_GPR13_SATA_TX_LVL_0_957_V },
  730. { 966, IMX6Q_GPR13_SATA_TX_LVL_0_966_V },
  731. { 976, IMX6Q_GPR13_SATA_TX_LVL_0_976_V },
  732. { 986, IMX6Q_GPR13_SATA_TX_LVL_0_986_V },
  733. { 996, IMX6Q_GPR13_SATA_TX_LVL_0_996_V },
  734. { 1005, IMX6Q_GPR13_SATA_TX_LVL_1_005_V },
  735. { 1015, IMX6Q_GPR13_SATA_TX_LVL_1_015_V },
  736. { 1025, IMX6Q_GPR13_SATA_TX_LVL_1_025_V },
  737. { 1035, IMX6Q_GPR13_SATA_TX_LVL_1_035_V },
  738. { 1045, IMX6Q_GPR13_SATA_TX_LVL_1_045_V },
  739. { 1054, IMX6Q_GPR13_SATA_TX_LVL_1_054_V },
  740. { 1064, IMX6Q_GPR13_SATA_TX_LVL_1_064_V },
  741. { 1074, IMX6Q_GPR13_SATA_TX_LVL_1_074_V },
  742. { 1084, IMX6Q_GPR13_SATA_TX_LVL_1_084_V },
  743. { 1094, IMX6Q_GPR13_SATA_TX_LVL_1_094_V },
  744. { 1104, IMX6Q_GPR13_SATA_TX_LVL_1_104_V },
  745. { 1113, IMX6Q_GPR13_SATA_TX_LVL_1_113_V },
  746. { 1123, IMX6Q_GPR13_SATA_TX_LVL_1_123_V },
  747. { 1133, IMX6Q_GPR13_SATA_TX_LVL_1_133_V },
  748. { 1143, IMX6Q_GPR13_SATA_TX_LVL_1_143_V },
  749. { 1152, IMX6Q_GPR13_SATA_TX_LVL_1_152_V },
  750. { 1162, IMX6Q_GPR13_SATA_TX_LVL_1_162_V },
  751. { 1172, IMX6Q_GPR13_SATA_TX_LVL_1_172_V },
  752. { 1182, IMX6Q_GPR13_SATA_TX_LVL_1_182_V },
  753. { 1191, IMX6Q_GPR13_SATA_TX_LVL_1_191_V },
  754. { 1201, IMX6Q_GPR13_SATA_TX_LVL_1_201_V },
  755. { 1211, IMX6Q_GPR13_SATA_TX_LVL_1_211_V },
  756. { 1221, IMX6Q_GPR13_SATA_TX_LVL_1_221_V },
  757. { 1230, IMX6Q_GPR13_SATA_TX_LVL_1_230_V },
  758. { 1240, IMX6Q_GPR13_SATA_TX_LVL_1_240_V }
  759. };
  760. static const struct reg_value gpr13_tx_boost[] = {
  761. { 0, IMX6Q_GPR13_SATA_TX_BOOST_0_00_DB },
  762. { 370, IMX6Q_GPR13_SATA_TX_BOOST_0_37_DB },
  763. { 740, IMX6Q_GPR13_SATA_TX_BOOST_0_74_DB },
  764. { 1110, IMX6Q_GPR13_SATA_TX_BOOST_1_11_DB },
  765. { 1480, IMX6Q_GPR13_SATA_TX_BOOST_1_48_DB },
  766. { 1850, IMX6Q_GPR13_SATA_TX_BOOST_1_85_DB },
  767. { 2220, IMX6Q_GPR13_SATA_TX_BOOST_2_22_DB },
  768. { 2590, IMX6Q_GPR13_SATA_TX_BOOST_2_59_DB },
  769. { 2960, IMX6Q_GPR13_SATA_TX_BOOST_2_96_DB },
  770. { 3330, IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB },
  771. { 3700, IMX6Q_GPR13_SATA_TX_BOOST_3_70_DB },
  772. { 4070, IMX6Q_GPR13_SATA_TX_BOOST_4_07_DB },
  773. { 4440, IMX6Q_GPR13_SATA_TX_BOOST_4_44_DB },
  774. { 4810, IMX6Q_GPR13_SATA_TX_BOOST_4_81_DB },
  775. { 5280, IMX6Q_GPR13_SATA_TX_BOOST_5_28_DB },
  776. { 5750, IMX6Q_GPR13_SATA_TX_BOOST_5_75_DB }
  777. };
  778. static const struct reg_value gpr13_tx_atten[] = {
  779. { 8, IMX6Q_GPR13_SATA_TX_ATTEN_8_16 },
  780. { 9, IMX6Q_GPR13_SATA_TX_ATTEN_9_16 },
  781. { 10, IMX6Q_GPR13_SATA_TX_ATTEN_10_16 },
  782. { 12, IMX6Q_GPR13_SATA_TX_ATTEN_12_16 },
  783. { 14, IMX6Q_GPR13_SATA_TX_ATTEN_14_16 },
  784. { 16, IMX6Q_GPR13_SATA_TX_ATTEN_16_16 },
  785. };
  786. static const struct reg_value gpr13_rx_eq[] = {
  787. { 500, IMX6Q_GPR13_SATA_RX_EQ_VAL_0_5_DB },
  788. { 1000, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_0_DB },
  789. { 1500, IMX6Q_GPR13_SATA_RX_EQ_VAL_1_5_DB },
  790. { 2000, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_0_DB },
  791. { 2500, IMX6Q_GPR13_SATA_RX_EQ_VAL_2_5_DB },
  792. { 3000, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB },
  793. { 3500, IMX6Q_GPR13_SATA_RX_EQ_VAL_3_5_DB },
  794. { 4000, IMX6Q_GPR13_SATA_RX_EQ_VAL_4_0_DB },
  795. };
  796. static const struct reg_property gpr13_props[] = {
  797. {
  798. .name = "fsl,transmit-level-mV",
  799. .values = gpr13_tx_level,
  800. .num_values = ARRAY_SIZE(gpr13_tx_level),
  801. .def_value = IMX6Q_GPR13_SATA_TX_LVL_1_025_V,
  802. }, {
  803. .name = "fsl,transmit-boost-mdB",
  804. .values = gpr13_tx_boost,
  805. .num_values = ARRAY_SIZE(gpr13_tx_boost),
  806. .def_value = IMX6Q_GPR13_SATA_TX_BOOST_3_33_DB,
  807. }, {
  808. .name = "fsl,transmit-atten-16ths",
  809. .values = gpr13_tx_atten,
  810. .num_values = ARRAY_SIZE(gpr13_tx_atten),
  811. .def_value = IMX6Q_GPR13_SATA_TX_ATTEN_9_16,
  812. }, {
  813. .name = "fsl,receive-eq-mdB",
  814. .values = gpr13_rx_eq,
  815. .num_values = ARRAY_SIZE(gpr13_rx_eq),
  816. .def_value = IMX6Q_GPR13_SATA_RX_EQ_VAL_3_0_DB,
  817. }, {
  818. .name = "fsl,no-spread-spectrum",
  819. .def_value = IMX6Q_GPR13_SATA_MPLL_SS_EN,
  820. .set_value = 0,
  821. },
  822. };
  823. static u32 imx_ahci_parse_props(struct device *dev,
  824. const struct reg_property *prop, size_t num)
  825. {
  826. struct device_node *np = dev->of_node;
  827. u32 reg_value = 0;
  828. int i, j;
  829. for (i = 0; i < num; i++, prop++) {
  830. u32 of_val;
  831. if (prop->num_values == 0) {
  832. if (of_property_read_bool(np, prop->name))
  833. reg_value |= prop->set_value;
  834. else
  835. reg_value |= prop->def_value;
  836. continue;
  837. }
  838. if (of_property_read_u32(np, prop->name, &of_val)) {
  839. dev_info(dev, "%s not specified, using %08x\n",
  840. prop->name, prop->def_value);
  841. reg_value |= prop->def_value;
  842. continue;
  843. }
  844. for (j = 0; j < prop->num_values; j++) {
  845. if (prop->values[j].of_value == of_val) {
  846. dev_info(dev, "%s value %u, using %08x\n",
  847. prop->name, of_val, prop->values[j].reg_value);
  848. reg_value |= prop->values[j].reg_value;
  849. break;
  850. }
  851. }
  852. if (j == prop->num_values) {
  853. dev_err(dev, "DT property %s is not a valid value\n",
  854. prop->name);
  855. reg_value |= prop->def_value;
  856. }
  857. }
  858. return reg_value;
  859. }
  860. static struct scsi_host_template ahci_platform_sht = {
  861. AHCI_SHT(DRV_NAME),
  862. };
  863. static int imx8_sata_probe(struct device *dev, struct imx_ahci_priv *imxpriv)
  864. {
  865. struct resource *phy_res;
  866. struct platform_device *pdev = imxpriv->ahci_pdev;
  867. struct device_node *np = dev->of_node;
  868. if (of_property_read_u32(np, "fsl,phy-imp", &imxpriv->imped_ratio))
  869. imxpriv->imped_ratio = IMX8QM_SATA_PHY_IMPED_RATIO_85OHM;
  870. phy_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
  871. if (phy_res) {
  872. imxpriv->phy_base = devm_ioremap(dev, phy_res->start,
  873. resource_size(phy_res));
  874. if (!imxpriv->phy_base) {
  875. dev_err(dev, "error with ioremap\n");
  876. return -ENOMEM;
  877. }
  878. } else {
  879. dev_err(dev, "missing *phy* reg region.\n");
  880. return -ENOMEM;
  881. }
  882. imxpriv->gpr =
  883. syscon_regmap_lookup_by_phandle(np, "hsio");
  884. if (IS_ERR(imxpriv->gpr)) {
  885. dev_err(dev, "unable to find gpr registers\n");
  886. return PTR_ERR(imxpriv->gpr);
  887. }
  888. imxpriv->epcs_tx_clk = devm_clk_get(dev, "epcs_tx");
  889. if (IS_ERR(imxpriv->epcs_tx_clk)) {
  890. dev_err(dev, "can't get epcs_tx_clk clock.\n");
  891. return PTR_ERR(imxpriv->epcs_tx_clk);
  892. }
  893. imxpriv->epcs_rx_clk = devm_clk_get(dev, "epcs_rx");
  894. if (IS_ERR(imxpriv->epcs_rx_clk)) {
  895. dev_err(dev, "can't get epcs_rx_clk clock.\n");
  896. return PTR_ERR(imxpriv->epcs_rx_clk);
  897. }
  898. imxpriv->phy_pclk0 = devm_clk_get(dev, "phy_pclk0");
  899. if (IS_ERR(imxpriv->phy_pclk0)) {
  900. dev_err(dev, "can't get phy_pclk0 clock.\n");
  901. return PTR_ERR(imxpriv->phy_pclk0);
  902. }
  903. imxpriv->phy_pclk1 = devm_clk_get(dev, "phy_pclk1");
  904. if (IS_ERR(imxpriv->phy_pclk1)) {
  905. dev_err(dev, "can't get phy_pclk1 clock.\n");
  906. return PTR_ERR(imxpriv->phy_pclk1);
  907. }
  908. imxpriv->phy_apbclk = devm_clk_get(dev, "phy_apbclk");
  909. if (IS_ERR(imxpriv->phy_apbclk)) {
  910. dev_err(dev, "can't get phy_apbclk clock.\n");
  911. return PTR_ERR(imxpriv->phy_apbclk);
  912. }
  913. /* Fetch GPIO, then enable the external OSC */
  914. imxpriv->clkreq_gpiod = devm_gpiod_get_optional(dev, "clkreq",
  915. GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE);
  916. if (IS_ERR(imxpriv->clkreq_gpiod))
  917. return PTR_ERR(imxpriv->clkreq_gpiod);
  918. if (imxpriv->clkreq_gpiod)
  919. gpiod_set_consumer_name(imxpriv->clkreq_gpiod, "SATA CLKREQ");
  920. return 0;
  921. }
  922. static int imx_ahci_probe(struct platform_device *pdev)
  923. {
  924. struct device *dev = &pdev->dev;
  925. const struct of_device_id *of_id;
  926. struct ahci_host_priv *hpriv;
  927. struct imx_ahci_priv *imxpriv;
  928. unsigned int reg_val;
  929. int ret;
  930. of_id = of_match_device(imx_ahci_of_match, dev);
  931. if (!of_id)
  932. return -EINVAL;
  933. imxpriv = devm_kzalloc(dev, sizeof(*imxpriv), GFP_KERNEL);
  934. if (!imxpriv)
  935. return -ENOMEM;
  936. imxpriv->ahci_pdev = pdev;
  937. imxpriv->no_device = false;
  938. imxpriv->first_time = true;
  939. imxpriv->type = (unsigned long)of_id->data;
  940. imxpriv->sata_clk = devm_clk_get(dev, "sata");
  941. if (IS_ERR(imxpriv->sata_clk)) {
  942. dev_err(dev, "can't get sata clock.\n");
  943. return PTR_ERR(imxpriv->sata_clk);
  944. }
  945. imxpriv->sata_ref_clk = devm_clk_get(dev, "sata_ref");
  946. if (IS_ERR(imxpriv->sata_ref_clk)) {
  947. dev_err(dev, "can't get sata_ref clock.\n");
  948. return PTR_ERR(imxpriv->sata_ref_clk);
  949. }
  950. imxpriv->ahb_clk = devm_clk_get(dev, "ahb");
  951. if (IS_ERR(imxpriv->ahb_clk)) {
  952. dev_err(dev, "can't get ahb clock.\n");
  953. return PTR_ERR(imxpriv->ahb_clk);
  954. }
  955. if (imxpriv->type == AHCI_IMX6Q || imxpriv->type == AHCI_IMX6QP) {
  956. u32 reg_value;
  957. imxpriv->gpr = syscon_regmap_lookup_by_compatible(
  958. "fsl,imx6q-iomuxc-gpr");
  959. if (IS_ERR(imxpriv->gpr)) {
  960. dev_err(dev,
  961. "failed to find fsl,imx6q-iomux-gpr regmap\n");
  962. return PTR_ERR(imxpriv->gpr);
  963. }
  964. reg_value = imx_ahci_parse_props(dev, gpr13_props,
  965. ARRAY_SIZE(gpr13_props));
  966. imxpriv->phy_params =
  967. IMX6Q_GPR13_SATA_RX_LOS_LVL_SATA2M |
  968. IMX6Q_GPR13_SATA_RX_DPLL_MODE_2P_4F |
  969. IMX6Q_GPR13_SATA_SPD_MODE_3P0G |
  970. reg_value;
  971. } else if (imxpriv->type == AHCI_IMX8QM) {
  972. ret = imx8_sata_probe(dev, imxpriv);
  973. if (ret)
  974. return ret;
  975. }
  976. hpriv = ahci_platform_get_resources(pdev, 0);
  977. if (IS_ERR(hpriv))
  978. return PTR_ERR(hpriv);
  979. hpriv->plat_data = imxpriv;
  980. ret = clk_prepare_enable(imxpriv->sata_clk);
  981. if (ret)
  982. return ret;
  983. if (imxpriv->type == AHCI_IMX53 &&
  984. IS_ENABLED(CONFIG_HWMON)) {
  985. /* Add the temperature monitor */
  986. struct device *hwmon_dev;
  987. hwmon_dev =
  988. devm_hwmon_device_register_with_groups(dev,
  989. "sata_ahci",
  990. hpriv,
  991. fsl_sata_ahci_groups);
  992. if (IS_ERR(hwmon_dev)) {
  993. ret = PTR_ERR(hwmon_dev);
  994. goto disable_clk;
  995. }
  996. devm_thermal_of_zone_register(hwmon_dev, 0, hwmon_dev,
  997. &fsl_sata_ahci_of_thermal_ops);
  998. dev_info(dev, "%s: sensor 'sata_ahci'\n", dev_name(hwmon_dev));
  999. }
  1000. ret = imx_sata_enable(hpriv);
  1001. if (ret)
  1002. goto disable_clk;
  1003. /*
  1004. * Configure the HWINIT bits of the HOST_CAP and HOST_PORTS_IMPL,
  1005. * and IP vendor specific register IMX_TIMER1MS.
  1006. * Configure CAP_SSS (support stagered spin up).
  1007. * Implement the port0.
  1008. * Get the ahb clock rate, and configure the TIMER1MS register.
  1009. */
  1010. reg_val = readl(hpriv->mmio + HOST_CAP);
  1011. if (!(reg_val & HOST_CAP_SSS)) {
  1012. reg_val |= HOST_CAP_SSS;
  1013. writel(reg_val, hpriv->mmio + HOST_CAP);
  1014. }
  1015. reg_val = readl(hpriv->mmio + HOST_PORTS_IMPL);
  1016. if (!(reg_val & 0x1)) {
  1017. reg_val |= 0x1;
  1018. writel(reg_val, hpriv->mmio + HOST_PORTS_IMPL);
  1019. }
  1020. reg_val = clk_get_rate(imxpriv->ahb_clk) / 1000;
  1021. writel(reg_val, hpriv->mmio + IMX_TIMER1MS);
  1022. ret = ahci_platform_init_host(pdev, hpriv, &ahci_imx_port_info,
  1023. &ahci_platform_sht);
  1024. if (ret)
  1025. goto disable_sata;
  1026. return 0;
  1027. disable_sata:
  1028. imx_sata_disable(hpriv);
  1029. disable_clk:
  1030. clk_disable_unprepare(imxpriv->sata_clk);
  1031. return ret;
  1032. }
  1033. static void ahci_imx_host_stop(struct ata_host *host)
  1034. {
  1035. struct ahci_host_priv *hpriv = host->private_data;
  1036. struct imx_ahci_priv *imxpriv = hpriv->plat_data;
  1037. imx_sata_disable(hpriv);
  1038. clk_disable_unprepare(imxpriv->sata_clk);
  1039. }
  1040. #ifdef CONFIG_PM_SLEEP
  1041. static int imx_ahci_suspend(struct device *dev)
  1042. {
  1043. struct ata_host *host = dev_get_drvdata(dev);
  1044. struct ahci_host_priv *hpriv = host->private_data;
  1045. int ret;
  1046. ret = ahci_platform_suspend_host(dev);
  1047. if (ret)
  1048. return ret;
  1049. imx_sata_disable(hpriv);
  1050. return 0;
  1051. }
  1052. static int imx_ahci_resume(struct device *dev)
  1053. {
  1054. struct ata_host *host = dev_get_drvdata(dev);
  1055. struct ahci_host_priv *hpriv = host->private_data;
  1056. int ret;
  1057. ret = imx_sata_enable(hpriv);
  1058. if (ret)
  1059. return ret;
  1060. return ahci_platform_resume_host(dev);
  1061. }
  1062. #endif
  1063. static SIMPLE_DEV_PM_OPS(ahci_imx_pm_ops, imx_ahci_suspend, imx_ahci_resume);
  1064. static struct platform_driver imx_ahci_driver = {
  1065. .probe = imx_ahci_probe,
  1066. .remove = ata_platform_remove_one,
  1067. .driver = {
  1068. .name = DRV_NAME,
  1069. .of_match_table = imx_ahci_of_match,
  1070. .pm = &ahci_imx_pm_ops,
  1071. },
  1072. };
  1073. module_platform_driver(imx_ahci_driver);
  1074. MODULE_DESCRIPTION("Freescale i.MX AHCI SATA platform driver");
  1075. MODULE_AUTHOR("Richard Zhu <[email protected]>");
  1076. MODULE_LICENSE("GPL");
  1077. MODULE_ALIAS("platform:" DRV_NAME);