ahci_da850.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * DaVinci DA850 AHCI SATA platform driver
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/pm.h>
  8. #include <linux/device.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/libata.h>
  11. #include <linux/ahci_platform.h>
  12. #include "ahci.h"
  13. #define DRV_NAME "ahci_da850"
  14. #define HARDRESET_RETRIES 5
  15. /* SATA PHY Control Register offset from AHCI base */
  16. #define SATA_P0PHYCR_REG 0x178
  17. #define SATA_PHY_MPY(x) ((x) << 0)
  18. #define SATA_PHY_LOS(x) ((x) << 6)
  19. #define SATA_PHY_RXCDR(x) ((x) << 10)
  20. #define SATA_PHY_RXEQ(x) ((x) << 13)
  21. #define SATA_PHY_TXSWING(x) ((x) << 19)
  22. #define SATA_PHY_ENPLL(x) ((x) << 31)
  23. static void da850_sata_init(struct device *dev, void __iomem *pwrdn_reg,
  24. void __iomem *ahci_base, u32 mpy)
  25. {
  26. unsigned int val;
  27. /* Enable SATA clock receiver */
  28. val = readl(pwrdn_reg);
  29. val &= ~BIT(0);
  30. writel(val, pwrdn_reg);
  31. val = SATA_PHY_MPY(mpy) | SATA_PHY_LOS(1) | SATA_PHY_RXCDR(4) |
  32. SATA_PHY_RXEQ(1) | SATA_PHY_TXSWING(3) | SATA_PHY_ENPLL(1);
  33. writel(val, ahci_base + SATA_P0PHYCR_REG);
  34. }
  35. static u32 ahci_da850_calculate_mpy(unsigned long refclk_rate)
  36. {
  37. u32 pll_output = 1500000000, needed;
  38. /*
  39. * We need to determine the value of the multiplier (MPY) bits.
  40. * In order to include the 12.5 multiplier we need to first divide
  41. * the refclk rate by ten.
  42. *
  43. * __div64_32() turned out to be unreliable, sometimes returning
  44. * false results.
  45. */
  46. WARN((refclk_rate % 10) != 0, "refclk must be divisible by 10");
  47. needed = pll_output / (refclk_rate / 10);
  48. /*
  49. * What we have now is (multiplier * 10).
  50. *
  51. * Let's determine the actual register value we need to write.
  52. */
  53. switch (needed) {
  54. case 50:
  55. return 0x1;
  56. case 60:
  57. return 0x2;
  58. case 80:
  59. return 0x4;
  60. case 100:
  61. return 0x5;
  62. case 120:
  63. return 0x6;
  64. case 125:
  65. return 0x7;
  66. case 150:
  67. return 0x8;
  68. case 200:
  69. return 0x9;
  70. case 250:
  71. return 0xa;
  72. default:
  73. /*
  74. * We should have divided evenly - if not, return an invalid
  75. * value.
  76. */
  77. return 0;
  78. }
  79. }
  80. static int ahci_da850_softreset(struct ata_link *link,
  81. unsigned int *class, unsigned long deadline)
  82. {
  83. int pmp, ret;
  84. pmp = sata_srst_pmp(link);
  85. /*
  86. * There's an issue with the SATA controller on da850 SoCs: if we
  87. * enable Port Multiplier support, but the drive is connected directly
  88. * to the board, it can't be detected. As a workaround: if PMP is
  89. * enabled, we first call ahci_do_softreset() and pass it the result of
  90. * sata_srst_pmp(). If this call fails, we retry with pmp = 0.
  91. */
  92. ret = ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
  93. if (pmp && ret == -EBUSY)
  94. return ahci_do_softreset(link, class, 0,
  95. deadline, ahci_check_ready);
  96. return ret;
  97. }
  98. static int ahci_da850_hardreset(struct ata_link *link,
  99. unsigned int *class, unsigned long deadline)
  100. {
  101. int ret, retry = HARDRESET_RETRIES;
  102. bool online;
  103. /*
  104. * In order to correctly service the LCD controller of the da850 SoC,
  105. * we increased the PLL0 frequency to 456MHz from the default 300MHz.
  106. *
  107. * This made the SATA controller unstable and the hardreset operation
  108. * does not always succeed the first time. Before really giving up to
  109. * bring up the link, retry the reset a couple times.
  110. */
  111. do {
  112. ret = ahci_do_hardreset(link, class, deadline, &online);
  113. if (online)
  114. return ret;
  115. } while (retry--);
  116. return ret;
  117. }
  118. static struct ata_port_operations ahci_da850_port_ops = {
  119. .inherits = &ahci_platform_ops,
  120. .softreset = ahci_da850_softreset,
  121. /*
  122. * No need to override .pmp_softreset - it's only used for actual
  123. * PMP-enabled ports.
  124. */
  125. .hardreset = ahci_da850_hardreset,
  126. .pmp_hardreset = ahci_da850_hardreset,
  127. };
  128. static const struct ata_port_info ahci_da850_port_info = {
  129. .flags = AHCI_FLAG_COMMON,
  130. .pio_mask = ATA_PIO4,
  131. .udma_mask = ATA_UDMA6,
  132. .port_ops = &ahci_da850_port_ops,
  133. };
  134. static struct scsi_host_template ahci_platform_sht = {
  135. AHCI_SHT(DRV_NAME),
  136. };
  137. static int ahci_da850_probe(struct platform_device *pdev)
  138. {
  139. struct device *dev = &pdev->dev;
  140. struct ahci_host_priv *hpriv;
  141. void __iomem *pwrdn_reg;
  142. struct resource *res;
  143. struct clk *clk;
  144. u32 mpy;
  145. int rc;
  146. hpriv = ahci_platform_get_resources(pdev, 0);
  147. if (IS_ERR(hpriv))
  148. return PTR_ERR(hpriv);
  149. /*
  150. * Internally ahci_platform_get_resources() calls clk_get(dev, NULL)
  151. * when trying to obtain the functional clock. This SATA controller
  152. * uses two clocks for which we specify two connection ids. If we don't
  153. * have the functional clock at this point - call clk_get() again with
  154. * con_id = "fck".
  155. */
  156. if (!hpriv->clks[0]) {
  157. clk = clk_get(dev, "fck");
  158. if (IS_ERR(clk))
  159. return PTR_ERR(clk);
  160. hpriv->clks[0] = clk;
  161. }
  162. /*
  163. * The second clock used by ahci-da850 is the external REFCLK. If we
  164. * didn't get it from ahci_platform_get_resources(), let's try to
  165. * specify the con_id in clk_get().
  166. */
  167. if (!hpriv->clks[1]) {
  168. clk = clk_get(dev, "refclk");
  169. if (IS_ERR(clk)) {
  170. dev_err(dev, "unable to obtain the reference clock");
  171. return -ENODEV;
  172. }
  173. hpriv->clks[1] = clk;
  174. }
  175. mpy = ahci_da850_calculate_mpy(clk_get_rate(hpriv->clks[1]));
  176. if (mpy == 0) {
  177. dev_err(dev, "invalid REFCLK multiplier value: 0x%x", mpy);
  178. return -EINVAL;
  179. }
  180. rc = ahci_platform_enable_resources(hpriv);
  181. if (rc)
  182. return rc;
  183. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  184. if (!res) {
  185. rc = -ENODEV;
  186. goto disable_resources;
  187. }
  188. pwrdn_reg = devm_ioremap(dev, res->start, resource_size(res));
  189. if (!pwrdn_reg) {
  190. rc = -ENOMEM;
  191. goto disable_resources;
  192. }
  193. da850_sata_init(dev, pwrdn_reg, hpriv->mmio, mpy);
  194. rc = ahci_platform_init_host(pdev, hpriv, &ahci_da850_port_info,
  195. &ahci_platform_sht);
  196. if (rc)
  197. goto disable_resources;
  198. return 0;
  199. disable_resources:
  200. ahci_platform_disable_resources(hpriv);
  201. return rc;
  202. }
  203. static SIMPLE_DEV_PM_OPS(ahci_da850_pm_ops, ahci_platform_suspend,
  204. ahci_platform_resume);
  205. static const struct of_device_id ahci_da850_of_match[] = {
  206. { .compatible = "ti,da850-ahci", },
  207. { },
  208. };
  209. MODULE_DEVICE_TABLE(of, ahci_da850_of_match);
  210. static struct platform_driver ahci_da850_driver = {
  211. .probe = ahci_da850_probe,
  212. .remove = ata_platform_remove_one,
  213. .driver = {
  214. .name = DRV_NAME,
  215. .of_match_table = ahci_da850_of_match,
  216. .pm = &ahci_da850_pm_ops,
  217. },
  218. };
  219. module_platform_driver(ahci_da850_driver);
  220. MODULE_DESCRIPTION("DaVinci DA850 AHCI SATA platform driver");
  221. MODULE_AUTHOR("Bartlomiej Zolnierkiewicz <b.zolnierkie@samsung.com>");
  222. MODULE_LICENSE("GPL");