ahci_sunxi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 DRV_NAME "ahci-sunxi"
  29. /* Insmod parameters */
  30. static bool enable_pmp;
  31. module_param(enable_pmp, bool, 0);
  32. MODULE_PARM_DESC(enable_pmp,
  33. "Enable support for sata port multipliers, only use if you use a pmp!");
  34. #define AHCI_BISTAFR 0x00a0
  35. #define AHCI_BISTCR 0x00a4
  36. #define AHCI_BISTFCTR 0x00a8
  37. #define AHCI_BISTSR 0x00ac
  38. #define AHCI_BISTDECR 0x00b0
  39. #define AHCI_DIAGNR0 0x00b4
  40. #define AHCI_DIAGNR1 0x00b8
  41. #define AHCI_OOBR 0x00bc
  42. #define AHCI_PHYCS0R 0x00c0
  43. #define AHCI_PHYCS1R 0x00c4
  44. #define AHCI_PHYCS2R 0x00c8
  45. #define AHCI_TIMER1MS 0x00e0
  46. #define AHCI_GPARAM1R 0x00e8
  47. #define AHCI_GPARAM2R 0x00ec
  48. #define AHCI_PPARAMR 0x00f0
  49. #define AHCI_TESTR 0x00f4
  50. #define AHCI_VERSIONR 0x00f8
  51. #define AHCI_IDR 0x00fc
  52. #define AHCI_RWCR 0x00fc
  53. #define AHCI_P0DMACR 0x0170
  54. #define AHCI_P0PHYCR 0x0178
  55. #define AHCI_P0PHYSR 0x017c
  56. static void sunxi_clrbits(void __iomem *reg, u32 clr_val)
  57. {
  58. u32 reg_val;
  59. reg_val = readl(reg);
  60. reg_val &= ~(clr_val);
  61. writel(reg_val, reg);
  62. }
  63. static void sunxi_setbits(void __iomem *reg, u32 set_val)
  64. {
  65. u32 reg_val;
  66. reg_val = readl(reg);
  67. reg_val |= set_val;
  68. writel(reg_val, reg);
  69. }
  70. static void sunxi_clrsetbits(void __iomem *reg, u32 clr_val, u32 set_val)
  71. {
  72. u32 reg_val;
  73. reg_val = readl(reg);
  74. reg_val &= ~(clr_val);
  75. reg_val |= set_val;
  76. writel(reg_val, reg);
  77. }
  78. static u32 sunxi_getbits(void __iomem *reg, u8 mask, u8 shift)
  79. {
  80. return (readl(reg) >> shift) & mask;
  81. }
  82. static int ahci_sunxi_phy_init(struct device *dev, void __iomem *reg_base)
  83. {
  84. u32 reg_val;
  85. int timeout;
  86. /* This magic is from the original code */
  87. writel(0, reg_base + AHCI_RWCR);
  88. msleep(5);
  89. sunxi_setbits(reg_base + AHCI_PHYCS1R, BIT(19));
  90. sunxi_clrsetbits(reg_base + AHCI_PHYCS0R,
  91. (0x7 << 24),
  92. (0x5 << 24) | BIT(23) | BIT(18));
  93. sunxi_clrsetbits(reg_base + AHCI_PHYCS1R,
  94. (0x3 << 16) | (0x1f << 8) | (0x3 << 6),
  95. (0x2 << 16) | (0x6 << 8) | (0x2 << 6));
  96. sunxi_setbits(reg_base + AHCI_PHYCS1R, BIT(28) | BIT(15));
  97. sunxi_clrbits(reg_base + AHCI_PHYCS1R, BIT(19));
  98. sunxi_clrsetbits(reg_base + AHCI_PHYCS0R,
  99. (0x7 << 20), (0x3 << 20));
  100. sunxi_clrsetbits(reg_base + AHCI_PHYCS2R,
  101. (0x1f << 5), (0x19 << 5));
  102. msleep(5);
  103. sunxi_setbits(reg_base + AHCI_PHYCS0R, (0x1 << 19));
  104. timeout = 250; /* Power up takes aprox 50 us */
  105. do {
  106. reg_val = sunxi_getbits(reg_base + AHCI_PHYCS0R, 0x7, 28);
  107. if (reg_val == 0x02)
  108. break;
  109. if (--timeout == 0) {
  110. dev_err(dev, "PHY power up failed.\n");
  111. return -EIO;
  112. }
  113. udelay(1);
  114. } while (1);
  115. sunxi_setbits(reg_base + AHCI_PHYCS2R, (0x1 << 24));
  116. timeout = 100; /* Calibration takes aprox 10 us */
  117. do {
  118. reg_val = sunxi_getbits(reg_base + AHCI_PHYCS2R, 0x1, 24);
  119. if (reg_val == 0x00)
  120. break;
  121. if (--timeout == 0) {
  122. dev_err(dev, "PHY calibration failed.\n");
  123. return -EIO;
  124. }
  125. udelay(1);
  126. } while (1);
  127. msleep(15);
  128. writel(0x7, reg_base + AHCI_RWCR);
  129. return 0;
  130. }
  131. static void ahci_sunxi_start_engine(struct ata_port *ap)
  132. {
  133. void __iomem *port_mmio = ahci_port_base(ap);
  134. struct ahci_host_priv *hpriv = ap->host->private_data;
  135. /* Setup DMA before DMA start */
  136. sunxi_clrsetbits(hpriv->mmio + AHCI_P0DMACR, 0x0000ff00, 0x00004400);
  137. /* Start DMA */
  138. sunxi_setbits(port_mmio + PORT_CMD, PORT_CMD_START);
  139. }
  140. static const struct ata_port_info ahci_sunxi_port_info = {
  141. .flags = AHCI_FLAG_COMMON | ATA_FLAG_NCQ,
  142. .pio_mask = ATA_PIO4,
  143. .udma_mask = ATA_UDMA6,
  144. .port_ops = &ahci_platform_ops,
  145. };
  146. static struct scsi_host_template ahci_platform_sht = {
  147. AHCI_SHT(DRV_NAME),
  148. };
  149. static int ahci_sunxi_probe(struct platform_device *pdev)
  150. {
  151. struct device *dev = &pdev->dev;
  152. struct ahci_host_priv *hpriv;
  153. int rc;
  154. hpriv = ahci_platform_get_resources(pdev);
  155. if (IS_ERR(hpriv))
  156. return PTR_ERR(hpriv);
  157. hpriv->start_engine = ahci_sunxi_start_engine;
  158. rc = ahci_platform_enable_resources(hpriv);
  159. if (rc)
  160. return rc;
  161. rc = ahci_sunxi_phy_init(dev, hpriv->mmio);
  162. if (rc)
  163. goto disable_resources;
  164. hpriv->flags = AHCI_HFLAG_32BIT_ONLY | AHCI_HFLAG_NO_MSI |
  165. AHCI_HFLAG_YES_NCQ;
  166. /*
  167. * The sunxi sata controller seems to be unable to successfully do a
  168. * soft reset if no pmp is attached, so disable pmp use unless
  169. * requested, otherwise directly attached disks do not work.
  170. */
  171. if (!enable_pmp)
  172. hpriv->flags |= AHCI_HFLAG_NO_PMP;
  173. rc = ahci_platform_init_host(pdev, hpriv, &ahci_sunxi_port_info,
  174. &ahci_platform_sht);
  175. if (rc)
  176. goto disable_resources;
  177. return 0;
  178. disable_resources:
  179. ahci_platform_disable_resources(hpriv);
  180. return rc;
  181. }
  182. #ifdef CONFIG_PM_SLEEP
  183. static int ahci_sunxi_resume(struct device *dev)
  184. {
  185. struct ata_host *host = dev_get_drvdata(dev);
  186. struct ahci_host_priv *hpriv = host->private_data;
  187. int rc;
  188. rc = ahci_platform_enable_resources(hpriv);
  189. if (rc)
  190. return rc;
  191. rc = ahci_sunxi_phy_init(dev, hpriv->mmio);
  192. if (rc)
  193. goto disable_resources;
  194. rc = ahci_platform_resume_host(dev);
  195. if (rc)
  196. goto disable_resources;
  197. return 0;
  198. disable_resources:
  199. ahci_platform_disable_resources(hpriv);
  200. return rc;
  201. }
  202. #endif
  203. static SIMPLE_DEV_PM_OPS(ahci_sunxi_pm_ops, ahci_platform_suspend,
  204. ahci_sunxi_resume);
  205. static const struct of_device_id ahci_sunxi_of_match[] = {
  206. { .compatible = "allwinner,sun4i-a10-ahci", },
  207. { },
  208. };
  209. MODULE_DEVICE_TABLE(of, ahci_sunxi_of_match);
  210. static struct platform_driver ahci_sunxi_driver = {
  211. .probe = ahci_sunxi_probe,
  212. .remove = ata_platform_remove_one,
  213. .driver = {
  214. .name = DRV_NAME,
  215. .of_match_table = ahci_sunxi_of_match,
  216. .pm = &ahci_sunxi_pm_ops,
  217. },
  218. };
  219. module_platform_driver(ahci_sunxi_driver);
  220. MODULE_DESCRIPTION("Allwinner sunxi AHCI SATA driver");
  221. MODULE_AUTHOR("Olliver Schinagl <oliver@schinagl.nl>");
  222. MODULE_LICENSE("GPL");