ahci_sunxi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Allwinner sunxi AHCI SATA platform driver
  3. * Copyright 2013 Olliver Schinagl <oliver@schinagl.nl>
  4. * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
  7. * Based on code from Allwinner Technology Co., Ltd. <www.allwinnertech.com>,
  8. * Daniel Wang <danielwang@allwinnertech.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. */
  19. #include <linux/ahci_platform.h>
  20. #include <linux/clk.h>
  21. #include <linux/errno.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regulator/consumer.h>
  27. #include "ahci.h"
  28. #define AHCI_BISTAFR 0x00a0
  29. #define AHCI_BISTCR 0x00a4
  30. #define AHCI_BISTFCTR 0x00a8
  31. #define AHCI_BISTSR 0x00ac
  32. #define AHCI_BISTDECR 0x00b0
  33. #define AHCI_DIAGNR0 0x00b4
  34. #define AHCI_DIAGNR1 0x00b8
  35. #define AHCI_OOBR 0x00bc
  36. #define AHCI_PHYCS0R 0x00c0
  37. #define AHCI_PHYCS1R 0x00c4
  38. #define AHCI_PHYCS2R 0x00c8
  39. #define AHCI_TIMER1MS 0x00e0
  40. #define AHCI_GPARAM1R 0x00e8
  41. #define AHCI_GPARAM2R 0x00ec
  42. #define AHCI_PPARAMR 0x00f0
  43. #define AHCI_TESTR 0x00f4
  44. #define AHCI_VERSIONR 0x00f8
  45. #define AHCI_IDR 0x00fc
  46. #define AHCI_RWCR 0x00fc
  47. #define AHCI_P0DMACR 0x0170
  48. #define AHCI_P0PHYCR 0x0178
  49. #define AHCI_P0PHYSR 0x017c
  50. static void sunxi_clrbits(void __iomem *reg, u32 clr_val)
  51. {
  52. u32 reg_val;
  53. reg_val = readl(reg);
  54. reg_val &= ~(clr_val);
  55. writel(reg_val, reg);
  56. }
  57. static void sunxi_setbits(void __iomem *reg, u32 set_val)
  58. {
  59. u32 reg_val;
  60. reg_val = readl(reg);
  61. reg_val |= set_val;
  62. writel(reg_val, reg);
  63. }
  64. static void sunxi_clrsetbits(void __iomem *reg, u32 clr_val, u32 set_val)
  65. {
  66. u32 reg_val;
  67. reg_val = readl(reg);
  68. reg_val &= ~(clr_val);
  69. reg_val |= set_val;
  70. writel(reg_val, reg);
  71. }
  72. static u32 sunxi_getbits(void __iomem *reg, u8 mask, u8 shift)
  73. {
  74. return (readl(reg) >> shift) & mask;
  75. }
  76. static int ahci_sunxi_phy_init(struct device *dev, void __iomem *reg_base)
  77. {
  78. u32 reg_val;
  79. int timeout;
  80. /* This magic is from the original code */
  81. writel(0, reg_base + AHCI_RWCR);
  82. msleep(5);
  83. sunxi_setbits(reg_base + AHCI_PHYCS1R, BIT(19));
  84. sunxi_clrsetbits(reg_base + AHCI_PHYCS0R,
  85. (0x7 << 24),
  86. (0x5 << 24) | BIT(23) | BIT(18));
  87. sunxi_clrsetbits(reg_base + AHCI_PHYCS1R,
  88. (0x3 << 16) | (0x1f << 8) | (0x3 << 6),
  89. (0x2 << 16) | (0x6 << 8) | (0x2 << 6));
  90. sunxi_setbits(reg_base + AHCI_PHYCS1R, BIT(28) | BIT(15));
  91. sunxi_clrbits(reg_base + AHCI_PHYCS1R, BIT(19));
  92. sunxi_clrsetbits(reg_base + AHCI_PHYCS0R,
  93. (0x7 << 20), (0x3 << 20));
  94. sunxi_clrsetbits(reg_base + AHCI_PHYCS2R,
  95. (0x1f << 5), (0x19 << 5));
  96. msleep(5);
  97. sunxi_setbits(reg_base + AHCI_PHYCS0R, (0x1 << 19));
  98. timeout = 250; /* Power up takes aprox 50 us */
  99. do {
  100. reg_val = sunxi_getbits(reg_base + AHCI_PHYCS0R, 0x7, 28);
  101. if (reg_val == 0x02)
  102. break;
  103. if (--timeout == 0) {
  104. dev_err(dev, "PHY power up failed.\n");
  105. return -EIO;
  106. }
  107. udelay(1);
  108. } while (1);
  109. sunxi_setbits(reg_base + AHCI_PHYCS2R, (0x1 << 24));
  110. timeout = 100; /* Calibration takes aprox 10 us */
  111. do {
  112. reg_val = sunxi_getbits(reg_base + AHCI_PHYCS2R, 0x1, 24);
  113. if (reg_val == 0x00)
  114. break;
  115. if (--timeout == 0) {
  116. dev_err(dev, "PHY calibration failed.\n");
  117. return -EIO;
  118. }
  119. udelay(1);
  120. } while (1);
  121. msleep(15);
  122. writel(0x7, reg_base + AHCI_RWCR);
  123. return 0;
  124. }
  125. static void ahci_sunxi_start_engine(struct ata_port *ap)
  126. {
  127. void __iomem *port_mmio = ahci_port_base(ap);
  128. struct ahci_host_priv *hpriv = ap->host->private_data;
  129. /* Setup DMA before DMA start */
  130. sunxi_clrsetbits(hpriv->mmio + AHCI_P0DMACR, 0x0000ff00, 0x00004400);
  131. /* Start DMA */
  132. sunxi_setbits(port_mmio + PORT_CMD, PORT_CMD_START);
  133. }
  134. static const struct ata_port_info ahci_sunxi_port_info = {
  135. .flags = AHCI_FLAG_COMMON | ATA_FLAG_NCQ,
  136. .pio_mask = ATA_PIO4,
  137. .udma_mask = ATA_UDMA6,
  138. .port_ops = &ahci_platform_ops,
  139. };
  140. static int ahci_sunxi_probe(struct platform_device *pdev)
  141. {
  142. struct device *dev = &pdev->dev;
  143. struct ahci_host_priv *hpriv;
  144. unsigned long hflags;
  145. int rc;
  146. hpriv = ahci_platform_get_resources(pdev);
  147. if (IS_ERR(hpriv))
  148. return PTR_ERR(hpriv);
  149. hpriv->start_engine = ahci_sunxi_start_engine;
  150. rc = ahci_platform_enable_resources(hpriv);
  151. if (rc)
  152. return rc;
  153. rc = ahci_sunxi_phy_init(dev, hpriv->mmio);
  154. if (rc)
  155. goto disable_resources;
  156. hflags = AHCI_HFLAG_32BIT_ONLY | AHCI_HFLAG_NO_MSI |
  157. AHCI_HFLAG_NO_PMP | AHCI_HFLAG_YES_NCQ;
  158. rc = ahci_platform_init_host(pdev, hpriv, &ahci_sunxi_port_info,
  159. hflags, 0, 0);
  160. if (rc)
  161. goto disable_resources;
  162. return 0;
  163. disable_resources:
  164. ahci_platform_disable_resources(hpriv);
  165. return rc;
  166. }
  167. #ifdef CONFIG_PM_SLEEP
  168. static int ahci_sunxi_resume(struct device *dev)
  169. {
  170. struct ata_host *host = dev_get_drvdata(dev);
  171. struct ahci_host_priv *hpriv = host->private_data;
  172. int rc;
  173. rc = ahci_platform_enable_resources(hpriv);
  174. if (rc)
  175. return rc;
  176. rc = ahci_sunxi_phy_init(dev, hpriv->mmio);
  177. if (rc)
  178. goto disable_resources;
  179. rc = ahci_platform_resume_host(dev);
  180. if (rc)
  181. goto disable_resources;
  182. return 0;
  183. disable_resources:
  184. ahci_platform_disable_resources(hpriv);
  185. return rc;
  186. }
  187. #endif
  188. static SIMPLE_DEV_PM_OPS(ahci_sunxi_pm_ops, ahci_platform_suspend,
  189. ahci_sunxi_resume);
  190. static const struct of_device_id ahci_sunxi_of_match[] = {
  191. { .compatible = "allwinner,sun4i-a10-ahci", },
  192. { },
  193. };
  194. MODULE_DEVICE_TABLE(of, ahci_sunxi_of_match);
  195. static struct platform_driver ahci_sunxi_driver = {
  196. .probe = ahci_sunxi_probe,
  197. .remove = ata_platform_remove_one,
  198. .driver = {
  199. .name = "ahci-sunxi",
  200. .owner = THIS_MODULE,
  201. .of_match_table = ahci_sunxi_of_match,
  202. .pm = &ahci_sunxi_pm_ops,
  203. },
  204. };
  205. module_platform_driver(ahci_sunxi_driver);
  206. MODULE_DESCRIPTION("Allwinner sunxi AHCI SATA driver");
  207. MODULE_AUTHOR("Olliver Schinagl <oliver@schinagl.nl>");
  208. MODULE_LICENSE("GPL");