ahci_da850.c 6.5 KB

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