ahci_imx.c 34 KB

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