ahci_tegra.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * drivers/ata/ahci_tegra.c
  3. *
  4. * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * Author:
  7. * Mikko Perttunen <mperttunen@nvidia.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/ahci_platform.h>
  20. #include <linux/errno.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/reset.h>
  27. #include <soc/tegra/fuse.h>
  28. #include <soc/tegra/pmc.h>
  29. #include "ahci.h"
  30. #define DRV_NAME "tegra-ahci"
  31. #define SATA_CONFIGURATION_0 0x180
  32. #define SATA_CONFIGURATION_EN_FPCI BIT(0)
  33. #define SCFG_OFFSET 0x1000
  34. #define T_SATA0_CFG_1 0x04
  35. #define T_SATA0_CFG_1_IO_SPACE BIT(0)
  36. #define T_SATA0_CFG_1_MEMORY_SPACE BIT(1)
  37. #define T_SATA0_CFG_1_BUS_MASTER BIT(2)
  38. #define T_SATA0_CFG_1_SERR BIT(8)
  39. #define T_SATA0_CFG_9 0x24
  40. #define T_SATA0_CFG_9_BASE_ADDRESS_SHIFT 13
  41. #define SATA_FPCI_BAR5 0x94
  42. #define SATA_FPCI_BAR5_START_SHIFT 4
  43. #define SATA_INTR_MASK 0x188
  44. #define SATA_INTR_MASK_IP_INT_MASK BIT(16)
  45. #define T_SATA0_AHCI_HBA_CAP_BKDR 0x300
  46. #define T_SATA0_BKDOOR_CC 0x4a4
  47. #define T_SATA0_CFG_SATA 0x54c
  48. #define T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN BIT(12)
  49. #define T_SATA0_CFG_MISC 0x550
  50. #define T_SATA0_INDEX 0x680
  51. #define T_SATA0_CHX_PHY_CTRL1_GEN1 0x690
  52. #define T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP_MASK 0xff
  53. #define T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP_SHIFT 0
  54. #define T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK_MASK (0xff << 8)
  55. #define T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK_SHIFT 8
  56. #define T_SATA0_CHX_PHY_CTRL1_GEN2 0x694
  57. #define T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP_MASK 0xff
  58. #define T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP_SHIFT 0
  59. #define T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK_MASK (0xff << 12)
  60. #define T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK_SHIFT 12
  61. #define T_SATA0_CHX_PHY_CTRL2 0x69c
  62. #define T_SATA0_CHX_PHY_CTRL2_CDR_CNTL_GEN1 0x23
  63. #define T_SATA0_CHX_PHY_CTRL11 0x6d0
  64. #define T_SATA0_CHX_PHY_CTRL11_GEN2_RX_EQ (0x2800 << 16)
  65. #define FUSE_SATA_CALIB 0x124
  66. #define FUSE_SATA_CALIB_MASK 0x3
  67. struct sata_pad_calibration {
  68. u8 gen1_tx_amp;
  69. u8 gen1_tx_peak;
  70. u8 gen2_tx_amp;
  71. u8 gen2_tx_peak;
  72. };
  73. static const struct sata_pad_calibration tegra124_pad_calibration[] = {
  74. {0x18, 0x04, 0x18, 0x0a},
  75. {0x0e, 0x04, 0x14, 0x0a},
  76. {0x0e, 0x07, 0x1a, 0x0e},
  77. {0x14, 0x0e, 0x1a, 0x0e},
  78. };
  79. struct tegra_ahci_priv {
  80. struct platform_device *pdev;
  81. void __iomem *sata_regs;
  82. struct reset_control *sata_rst;
  83. struct reset_control *sata_oob_rst;
  84. struct reset_control *sata_cold_rst;
  85. /* Needs special handling, cannot use ahci_platform */
  86. struct clk *sata_clk;
  87. struct regulator_bulk_data supplies[5];
  88. };
  89. static int tegra_ahci_power_on(struct ahci_host_priv *hpriv)
  90. {
  91. struct tegra_ahci_priv *tegra = hpriv->plat_data;
  92. int ret;
  93. ret = regulator_bulk_enable(ARRAY_SIZE(tegra->supplies),
  94. tegra->supplies);
  95. if (ret)
  96. return ret;
  97. ret = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_SATA,
  98. tegra->sata_clk,
  99. tegra->sata_rst);
  100. if (ret)
  101. goto disable_regulators;
  102. reset_control_assert(tegra->sata_oob_rst);
  103. reset_control_assert(tegra->sata_cold_rst);
  104. ret = ahci_platform_enable_resources(hpriv);
  105. if (ret)
  106. goto disable_power;
  107. reset_control_deassert(tegra->sata_cold_rst);
  108. reset_control_deassert(tegra->sata_oob_rst);
  109. return 0;
  110. disable_power:
  111. clk_disable_unprepare(tegra->sata_clk);
  112. tegra_powergate_power_off(TEGRA_POWERGATE_SATA);
  113. disable_regulators:
  114. regulator_bulk_disable(ARRAY_SIZE(tegra->supplies), tegra->supplies);
  115. return ret;
  116. }
  117. static void tegra_ahci_power_off(struct ahci_host_priv *hpriv)
  118. {
  119. struct tegra_ahci_priv *tegra = hpriv->plat_data;
  120. ahci_platform_disable_resources(hpriv);
  121. reset_control_assert(tegra->sata_rst);
  122. reset_control_assert(tegra->sata_oob_rst);
  123. reset_control_assert(tegra->sata_cold_rst);
  124. clk_disable_unprepare(tegra->sata_clk);
  125. tegra_powergate_power_off(TEGRA_POWERGATE_SATA);
  126. regulator_bulk_disable(ARRAY_SIZE(tegra->supplies), tegra->supplies);
  127. }
  128. static int tegra_ahci_controller_init(struct ahci_host_priv *hpriv)
  129. {
  130. struct tegra_ahci_priv *tegra = hpriv->plat_data;
  131. int ret;
  132. unsigned int val;
  133. struct sata_pad_calibration calib;
  134. ret = tegra_ahci_power_on(hpriv);
  135. if (ret) {
  136. dev_err(&tegra->pdev->dev,
  137. "failed to power on AHCI controller: %d\n", ret);
  138. return ret;
  139. }
  140. val = readl(tegra->sata_regs + SATA_CONFIGURATION_0);
  141. val |= SATA_CONFIGURATION_EN_FPCI;
  142. writel(val, tegra->sata_regs + SATA_CONFIGURATION_0);
  143. /* Pad calibration */
  144. ret = tegra_fuse_readl(FUSE_SATA_CALIB, &val);
  145. if (ret) {
  146. dev_err(&tegra->pdev->dev,
  147. "failed to read calibration fuse: %d\n", ret);
  148. return ret;
  149. }
  150. calib = tegra124_pad_calibration[val & FUSE_SATA_CALIB_MASK];
  151. writel(BIT(0), tegra->sata_regs + SCFG_OFFSET + T_SATA0_INDEX);
  152. val = readl(tegra->sata_regs +
  153. SCFG_OFFSET + T_SATA0_CHX_PHY_CTRL1_GEN1);
  154. val &= ~T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP_MASK;
  155. val &= ~T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK_MASK;
  156. val |= calib.gen1_tx_amp <<
  157. T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP_SHIFT;
  158. val |= calib.gen1_tx_peak <<
  159. T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK_SHIFT;
  160. writel(val, tegra->sata_regs + SCFG_OFFSET +
  161. T_SATA0_CHX_PHY_CTRL1_GEN1);
  162. val = readl(tegra->sata_regs +
  163. SCFG_OFFSET + T_SATA0_CHX_PHY_CTRL1_GEN2);
  164. val &= ~T_SATA0_CHX_PHY_CTRL1_GEN2_TX_AMP_MASK;
  165. val &= ~T_SATA0_CHX_PHY_CTRL1_GEN2_TX_PEAK_MASK;
  166. val |= calib.gen2_tx_amp <<
  167. T_SATA0_CHX_PHY_CTRL1_GEN1_TX_AMP_SHIFT;
  168. val |= calib.gen2_tx_peak <<
  169. T_SATA0_CHX_PHY_CTRL1_GEN1_TX_PEAK_SHIFT;
  170. writel(val, tegra->sata_regs + SCFG_OFFSET +
  171. T_SATA0_CHX_PHY_CTRL1_GEN2);
  172. writel(T_SATA0_CHX_PHY_CTRL11_GEN2_RX_EQ,
  173. tegra->sata_regs + SCFG_OFFSET + T_SATA0_CHX_PHY_CTRL11);
  174. writel(T_SATA0_CHX_PHY_CTRL2_CDR_CNTL_GEN1,
  175. tegra->sata_regs + SCFG_OFFSET + T_SATA0_CHX_PHY_CTRL2);
  176. writel(0, tegra->sata_regs + SCFG_OFFSET + T_SATA0_INDEX);
  177. /* Program controller device ID */
  178. val = readl(tegra->sata_regs + SCFG_OFFSET + T_SATA0_CFG_SATA);
  179. val |= T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN;
  180. writel(val, tegra->sata_regs + SCFG_OFFSET + T_SATA0_CFG_SATA);
  181. writel(0x01060100, tegra->sata_regs + SCFG_OFFSET + T_SATA0_BKDOOR_CC);
  182. val = readl(tegra->sata_regs + SCFG_OFFSET + T_SATA0_CFG_SATA);
  183. val &= ~T_SATA0_CFG_SATA_BACKDOOR_PROG_IF_EN;
  184. writel(val, tegra->sata_regs + SCFG_OFFSET + T_SATA0_CFG_SATA);
  185. /* Enable IO & memory access, bus master mode */
  186. val = readl(tegra->sata_regs + SCFG_OFFSET + T_SATA0_CFG_1);
  187. val |= T_SATA0_CFG_1_IO_SPACE | T_SATA0_CFG_1_MEMORY_SPACE |
  188. T_SATA0_CFG_1_BUS_MASTER | T_SATA0_CFG_1_SERR;
  189. writel(val, tegra->sata_regs + SCFG_OFFSET + T_SATA0_CFG_1);
  190. /* Program SATA MMIO */
  191. writel(0x10000 << SATA_FPCI_BAR5_START_SHIFT,
  192. tegra->sata_regs + SATA_FPCI_BAR5);
  193. writel(0x08000 << T_SATA0_CFG_9_BASE_ADDRESS_SHIFT,
  194. tegra->sata_regs + SCFG_OFFSET + T_SATA0_CFG_9);
  195. /* Unmask SATA interrupts */
  196. val = readl(tegra->sata_regs + SATA_INTR_MASK);
  197. val |= SATA_INTR_MASK_IP_INT_MASK;
  198. writel(val, tegra->sata_regs + SATA_INTR_MASK);
  199. return 0;
  200. }
  201. static void tegra_ahci_controller_deinit(struct ahci_host_priv *hpriv)
  202. {
  203. tegra_ahci_power_off(hpriv);
  204. }
  205. static void tegra_ahci_host_stop(struct ata_host *host)
  206. {
  207. struct ahci_host_priv *hpriv = host->private_data;
  208. tegra_ahci_controller_deinit(hpriv);
  209. }
  210. static struct ata_port_operations ahci_tegra_port_ops = {
  211. .inherits = &ahci_ops,
  212. .host_stop = tegra_ahci_host_stop,
  213. };
  214. static const struct ata_port_info ahci_tegra_port_info = {
  215. .flags = AHCI_FLAG_COMMON,
  216. .pio_mask = ATA_PIO4,
  217. .udma_mask = ATA_UDMA6,
  218. .port_ops = &ahci_tegra_port_ops,
  219. };
  220. static const struct of_device_id tegra_ahci_of_match[] = {
  221. { .compatible = "nvidia,tegra124-ahci" },
  222. {}
  223. };
  224. MODULE_DEVICE_TABLE(of, tegra_ahci_of_match);
  225. static struct scsi_host_template ahci_platform_sht = {
  226. AHCI_SHT(DRV_NAME),
  227. };
  228. static int tegra_ahci_probe(struct platform_device *pdev)
  229. {
  230. struct ahci_host_priv *hpriv;
  231. struct tegra_ahci_priv *tegra;
  232. struct resource *res;
  233. int ret;
  234. hpriv = ahci_platform_get_resources(pdev);
  235. if (IS_ERR(hpriv))
  236. return PTR_ERR(hpriv);
  237. tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
  238. if (!tegra)
  239. return -ENOMEM;
  240. hpriv->plat_data = tegra;
  241. tegra->pdev = pdev;
  242. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  243. tegra->sata_regs = devm_ioremap_resource(&pdev->dev, res);
  244. if (IS_ERR(tegra->sata_regs))
  245. return PTR_ERR(tegra->sata_regs);
  246. tegra->sata_rst = devm_reset_control_get(&pdev->dev, "sata");
  247. if (IS_ERR(tegra->sata_rst)) {
  248. dev_err(&pdev->dev, "Failed to get sata reset\n");
  249. return PTR_ERR(tegra->sata_rst);
  250. }
  251. tegra->sata_oob_rst = devm_reset_control_get(&pdev->dev, "sata-oob");
  252. if (IS_ERR(tegra->sata_oob_rst)) {
  253. dev_err(&pdev->dev, "Failed to get sata-oob reset\n");
  254. return PTR_ERR(tegra->sata_oob_rst);
  255. }
  256. tegra->sata_cold_rst = devm_reset_control_get(&pdev->dev, "sata-cold");
  257. if (IS_ERR(tegra->sata_cold_rst)) {
  258. dev_err(&pdev->dev, "Failed to get sata-cold reset\n");
  259. return PTR_ERR(tegra->sata_cold_rst);
  260. }
  261. tegra->sata_clk = devm_clk_get(&pdev->dev, "sata");
  262. if (IS_ERR(tegra->sata_clk)) {
  263. dev_err(&pdev->dev, "Failed to get sata clock\n");
  264. return PTR_ERR(tegra->sata_clk);
  265. }
  266. tegra->supplies[0].supply = "avdd";
  267. tegra->supplies[1].supply = "hvdd";
  268. tegra->supplies[2].supply = "vddio";
  269. tegra->supplies[3].supply = "target-5v";
  270. tegra->supplies[4].supply = "target-12v";
  271. ret = devm_regulator_bulk_get(&pdev->dev, ARRAY_SIZE(tegra->supplies),
  272. tegra->supplies);
  273. if (ret) {
  274. dev_err(&pdev->dev, "Failed to get regulators\n");
  275. return ret;
  276. }
  277. ret = tegra_ahci_controller_init(hpriv);
  278. if (ret)
  279. return ret;
  280. ret = ahci_platform_init_host(pdev, hpriv, &ahci_tegra_port_info,
  281. &ahci_platform_sht);
  282. if (ret)
  283. goto deinit_controller;
  284. return 0;
  285. deinit_controller:
  286. tegra_ahci_controller_deinit(hpriv);
  287. return ret;
  288. };
  289. static struct platform_driver tegra_ahci_driver = {
  290. .probe = tegra_ahci_probe,
  291. .remove = ata_platform_remove_one,
  292. .driver = {
  293. .name = DRV_NAME,
  294. .of_match_table = tegra_ahci_of_match,
  295. },
  296. /* LP0 suspend support not implemented */
  297. };
  298. module_platform_driver(tegra_ahci_driver);
  299. MODULE_AUTHOR("Mikko Perttunen <mperttunen@nvidia.com>");
  300. MODULE_DESCRIPTION("Tegra124 AHCI SATA driver");
  301. MODULE_LICENSE("GPL v2");