ahci_dm816.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * DaVinci DM816 AHCI SATA platform driver
  3. *
  4. * Copyright (C) 2017 BayLibre SAS
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/pm.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/libata.h>
  17. #include <linux/ahci_platform.h>
  18. #include "ahci.h"
  19. #define AHCI_DM816_DRV_NAME "ahci-dm816"
  20. #define AHCI_DM816_PHY_ENPLL(x) ((x) << 0)
  21. #define AHCI_DM816_PHY_MPY(x) ((x) << 1)
  22. #define AHCI_DM816_PHY_LOS(x) ((x) << 12)
  23. #define AHCI_DM816_PHY_RXCDR(x) ((x) << 13)
  24. #define AHCI_DM816_PHY_RXEQ(x) ((x) << 16)
  25. #define AHCI_DM816_PHY_TXSWING(x) ((x) << 23)
  26. #define AHCI_DM816_P0PHYCR_REG 0x178
  27. #define AHCI_DM816_P1PHYCR_REG 0x1f8
  28. #define AHCI_DM816_PLL_OUT 1500000000LU
  29. static const unsigned long pll_mpy_table[] = {
  30. 400, 500, 600, 800, 825, 1000, 1200,
  31. 1250, 1500, 1600, 1650, 2000, 2200, 2500
  32. };
  33. static int ahci_dm816_get_mpy_bits(unsigned long refclk_rate)
  34. {
  35. unsigned long pll_multiplier;
  36. int i;
  37. /*
  38. * We need to determine the value of the multiplier (MPY) bits.
  39. * In order to include the 8.25 multiplier we need to first divide
  40. * the refclk rate by 100.
  41. */
  42. pll_multiplier = AHCI_DM816_PLL_OUT / (refclk_rate / 100);
  43. for (i = 0; i < ARRAY_SIZE(pll_mpy_table); i++) {
  44. if (pll_mpy_table[i] == pll_multiplier)
  45. return i;
  46. }
  47. /*
  48. * We should have divided evenly - if not, return an invalid
  49. * value.
  50. */
  51. return -1;
  52. }
  53. static int ahci_dm816_phy_init(struct ahci_host_priv *hpriv, struct device *dev)
  54. {
  55. unsigned long refclk_rate;
  56. int mpy;
  57. u32 val;
  58. /*
  59. * We should have been supplied two clocks: the functional and
  60. * keep-alive clock and the external reference clock. We need the
  61. * rate of the latter to calculate the correct value of MPY bits.
  62. */
  63. if (!hpriv->clks[1]) {
  64. dev_err(dev, "reference clock not supplied\n");
  65. return -EINVAL;
  66. }
  67. refclk_rate = clk_get_rate(hpriv->clks[1]);
  68. if ((refclk_rate % 100) != 0) {
  69. dev_err(dev, "reference clock rate must be divisible by 100\n");
  70. return -EINVAL;
  71. }
  72. mpy = ahci_dm816_get_mpy_bits(refclk_rate);
  73. if (mpy < 0) {
  74. dev_err(dev, "can't calculate the MPY bits value\n");
  75. return -EINVAL;
  76. }
  77. /* Enable the PHY and configure the first HBA port. */
  78. val = AHCI_DM816_PHY_MPY(mpy) | AHCI_DM816_PHY_LOS(1) |
  79. AHCI_DM816_PHY_RXCDR(4) | AHCI_DM816_PHY_RXEQ(1) |
  80. AHCI_DM816_PHY_TXSWING(3) | AHCI_DM816_PHY_ENPLL(1);
  81. writel(val, hpriv->mmio + AHCI_DM816_P0PHYCR_REG);
  82. /* Configure the second HBA port. */
  83. val = AHCI_DM816_PHY_LOS(1) | AHCI_DM816_PHY_RXCDR(4) |
  84. AHCI_DM816_PHY_RXEQ(1) | AHCI_DM816_PHY_TXSWING(3);
  85. writel(val, hpriv->mmio + AHCI_DM816_P1PHYCR_REG);
  86. return 0;
  87. }
  88. static int ahci_dm816_softreset(struct ata_link *link,
  89. unsigned int *class, unsigned long deadline)
  90. {
  91. int pmp, ret;
  92. pmp = sata_srst_pmp(link);
  93. /*
  94. * There's an issue with the SATA controller on DM816 SoC: if we
  95. * enable Port Multiplier support, but the drive is connected directly
  96. * to the board, it can't be detected. As a workaround: if PMP is
  97. * enabled, we first call ahci_do_softreset() and pass it the result of
  98. * sata_srst_pmp(). If this call fails, we retry with pmp = 0.
  99. */
  100. ret = ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
  101. if (pmp && ret == -EBUSY)
  102. return ahci_do_softreset(link, class, 0,
  103. deadline, ahci_check_ready);
  104. return ret;
  105. }
  106. static struct ata_port_operations ahci_dm816_port_ops = {
  107. .inherits = &ahci_platform_ops,
  108. .softreset = ahci_dm816_softreset,
  109. };
  110. static const struct ata_port_info ahci_dm816_port_info = {
  111. .flags = AHCI_FLAG_COMMON,
  112. .pio_mask = ATA_PIO4,
  113. .udma_mask = ATA_UDMA6,
  114. .port_ops = &ahci_dm816_port_ops,
  115. };
  116. static struct scsi_host_template ahci_dm816_platform_sht = {
  117. AHCI_SHT(AHCI_DM816_DRV_NAME),
  118. };
  119. static int ahci_dm816_probe(struct platform_device *pdev)
  120. {
  121. struct device *dev = &pdev->dev;
  122. struct ahci_host_priv *hpriv;
  123. int rc;
  124. hpriv = ahci_platform_get_resources(pdev);
  125. if (IS_ERR(hpriv))
  126. return PTR_ERR(hpriv);
  127. rc = ahci_platform_enable_resources(hpriv);
  128. if (rc)
  129. return rc;
  130. rc = ahci_dm816_phy_init(hpriv, dev);
  131. if (rc)
  132. goto disable_resources;
  133. rc = ahci_platform_init_host(pdev, hpriv,
  134. &ahci_dm816_port_info,
  135. &ahci_dm816_platform_sht);
  136. if (rc)
  137. goto disable_resources;
  138. return 0;
  139. disable_resources:
  140. ahci_platform_disable_resources(hpriv);
  141. return rc;
  142. }
  143. static SIMPLE_DEV_PM_OPS(ahci_dm816_pm_ops,
  144. ahci_platform_suspend,
  145. ahci_platform_resume);
  146. static const struct of_device_id ahci_dm816_of_match[] = {
  147. { .compatible = "ti,dm816-ahci", },
  148. { },
  149. };
  150. MODULE_DEVICE_TABLE(of, ahci_dm816_of_match);
  151. static struct platform_driver ahci_dm816_driver = {
  152. .probe = ahci_dm816_probe,
  153. .remove = ata_platform_remove_one,
  154. .driver = {
  155. .name = AHCI_DM816_DRV_NAME,
  156. .of_match_table = ahci_dm816_of_match,
  157. .pm = &ahci_dm816_pm_ops,
  158. },
  159. };
  160. module_platform_driver(ahci_dm816_driver);
  161. MODULE_DESCRIPTION("DaVinci DM816 AHCI SATA platform driver");
  162. MODULE_AUTHOR("Bartosz Golaszewski <bgolaszewski@baylibre.com>");
  163. MODULE_LICENSE("GPL");