dev-ahci.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* linux/arch/arm/mach-exynos4/dev-ahci.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * EXYNOS4 - AHCI support
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/dma-mapping.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/ahci_platform.h>
  17. #include <plat/cpu.h>
  18. #include <mach/irqs.h>
  19. #include <mach/map.h>
  20. #include <mach/regs-pmu.h>
  21. /* PHY Control Register */
  22. #define SATA_CTRL0 0x0
  23. /* PHY Link Control Register */
  24. #define SATA_CTRL1 0x4
  25. /* PHY Status Register */
  26. #define SATA_PHY_STATUS 0x8
  27. #define SATA_CTRL0_RX_DATA_VALID(x) (x << 27)
  28. #define SATA_CTRL0_SPEED_MODE (1 << 26)
  29. #define SATA_CTRL0_M_PHY_CAL (1 << 19)
  30. #define SATA_CTRL0_PHY_CMU_RST_N (1 << 10)
  31. #define SATA_CTRL0_M_PHY_LN_RST_N (1 << 9)
  32. #define SATA_CTRL0_PHY_POR_N (1 << 8)
  33. #define SATA_CTRL1_RST_PMALIVE_N (1 << 8)
  34. #define SATA_CTRL1_RST_RXOOB_N (1 << 7)
  35. #define SATA_CTRL1_RST_RX_N (1 << 6)
  36. #define SATA_CTRL1_RST_TX_N (1 << 5)
  37. #define SATA_PHY_STATUS_CMU_OK (1 << 18)
  38. #define SATA_PHY_STATUS_LANE_OK (1 << 16)
  39. #define LANE0 0x200
  40. #define COM_LANE 0xA00
  41. #define HOST_PORTS_IMPL 0xC
  42. #define SCLK_SATA_FREQ (67 * MHZ)
  43. static void __iomem *phy_base, *phy_ctrl;
  44. struct phy_reg {
  45. u8 reg;
  46. u8 val;
  47. };
  48. /* SATA PHY setup */
  49. static const struct phy_reg exynos4_sataphy_cmu[] = {
  50. { 0x00, 0x06 }, { 0x02, 0x80 }, { 0x22, 0xa0 }, { 0x23, 0x42 },
  51. { 0x2e, 0x04 }, { 0x2f, 0x50 }, { 0x30, 0x70 }, { 0x31, 0x02 },
  52. { 0x32, 0x25 }, { 0x33, 0x40 }, { 0x34, 0x01 }, { 0x35, 0x40 },
  53. { 0x61, 0x2e }, { 0x63, 0x5e }, { 0x65, 0x42 }, { 0x66, 0xd1 },
  54. { 0x67, 0x20 }, { 0x68, 0x28 }, { 0x69, 0x78 }, { 0x6a, 0x04 },
  55. { 0x6b, 0xc8 }, { 0x6c, 0x06 },
  56. };
  57. static const struct phy_reg exynos4_sataphy_lane[] = {
  58. { 0x00, 0x02 }, { 0x05, 0x10 }, { 0x06, 0x84 }, { 0x07, 0x04 },
  59. { 0x08, 0xe0 }, { 0x10, 0x23 }, { 0x13, 0x05 }, { 0x14, 0x30 },
  60. { 0x15, 0x00 }, { 0x17, 0x70 }, { 0x18, 0xf2 }, { 0x19, 0x1e },
  61. { 0x1a, 0x18 }, { 0x1b, 0x0d }, { 0x1c, 0x08 }, { 0x50, 0x60 },
  62. { 0x51, 0x0f },
  63. };
  64. static const struct phy_reg exynos4_sataphy_comlane[] = {
  65. { 0x01, 0x20 }, { 0x03, 0x40 }, { 0x04, 0x3c }, { 0x05, 0x7d },
  66. { 0x06, 0x1d }, { 0x07, 0xcf }, { 0x08, 0x05 }, { 0x09, 0x63 },
  67. { 0x0a, 0x29 }, { 0x0b, 0xc4 }, { 0x0c, 0x01 }, { 0x0d, 0x03 },
  68. { 0x0e, 0x28 }, { 0x0f, 0x98 }, { 0x10, 0x19 }, { 0x13, 0x80 },
  69. { 0x14, 0xf0 }, { 0x15, 0xd0 }, { 0x39, 0xa0 }, { 0x3a, 0xa0 },
  70. { 0x3b, 0xa0 }, { 0x3c, 0xa0 }, { 0x3d, 0xa0 }, { 0x3e, 0xa0 },
  71. { 0x3f, 0xa0 }, { 0x40, 0x42 }, { 0x42, 0x80 }, { 0x43, 0x58 },
  72. { 0x45, 0x44 }, { 0x46, 0x5c }, { 0x47, 0x86 }, { 0x48, 0x8d },
  73. { 0x49, 0xd0 }, { 0x4a, 0x09 }, { 0x4b, 0x90 }, { 0x4c, 0x07 },
  74. { 0x4d, 0x40 }, { 0x51, 0x20 }, { 0x52, 0x32 }, { 0x7f, 0xd8 },
  75. { 0x80, 0x1a }, { 0x81, 0xff }, { 0x82, 0x11 }, { 0x83, 0x00 },
  76. { 0x87, 0xf0 }, { 0x87, 0xff }, { 0x87, 0xff }, { 0x87, 0xff },
  77. { 0x87, 0xff }, { 0x8c, 0x1c }, { 0x8d, 0xc2 }, { 0x8e, 0xc3 },
  78. { 0x8f, 0x3f }, { 0x90, 0x0a }, { 0x96, 0xf8 },
  79. };
  80. static int wait_for_phy_ready(void __iomem *reg, unsigned long bit)
  81. {
  82. unsigned long timeout;
  83. /* wait for maximum of 3 sec */
  84. timeout = jiffies + msecs_to_jiffies(3000);
  85. while (!(__raw_readl(reg) & bit)) {
  86. if (time_after(jiffies, timeout))
  87. return -1;
  88. cpu_relax();
  89. }
  90. return 0;
  91. }
  92. static int ahci_phy_init(void __iomem *mmio)
  93. {
  94. int i, ctrl0;
  95. for (i = 0; i < ARRAY_SIZE(exynos4_sataphy_cmu); i++)
  96. __raw_writeb(exynos4_sataphy_cmu[i].val,
  97. phy_base + (exynos4_sataphy_cmu[i].reg * 4));
  98. for (i = 0; i < ARRAY_SIZE(exynos4_sataphy_lane); i++)
  99. __raw_writeb(exynos4_sataphy_lane[i].val,
  100. phy_base + (LANE0 + exynos4_sataphy_lane[i].reg) * 4);
  101. for (i = 0; i < ARRAY_SIZE(exynos4_sataphy_comlane); i++)
  102. __raw_writeb(exynos4_sataphy_comlane[i].val,
  103. phy_base + (COM_LANE + exynos4_sataphy_comlane[i].reg) * 4);
  104. __raw_writeb(0x07, phy_base);
  105. ctrl0 = __raw_readl(phy_ctrl + SATA_CTRL0);
  106. ctrl0 |= SATA_CTRL0_PHY_CMU_RST_N;
  107. __raw_writel(ctrl0, phy_ctrl + SATA_CTRL0);
  108. if (wait_for_phy_ready(phy_ctrl + SATA_PHY_STATUS,
  109. SATA_PHY_STATUS_CMU_OK) < 0) {
  110. printk(KERN_ERR "PHY CMU not ready\n");
  111. return -EBUSY;
  112. }
  113. __raw_writeb(0x03, phy_base + (COM_LANE * 4));
  114. ctrl0 = __raw_readl(phy_ctrl + SATA_CTRL0);
  115. ctrl0 |= SATA_CTRL0_M_PHY_LN_RST_N;
  116. __raw_writel(ctrl0, phy_ctrl + SATA_CTRL0);
  117. if (wait_for_phy_ready(phy_ctrl + SATA_PHY_STATUS,
  118. SATA_PHY_STATUS_LANE_OK) < 0) {
  119. printk(KERN_ERR "PHY LANE not ready\n");
  120. return -EBUSY;
  121. }
  122. ctrl0 = __raw_readl(phy_ctrl + SATA_CTRL0);
  123. ctrl0 |= SATA_CTRL0_M_PHY_CAL;
  124. __raw_writel(ctrl0, phy_ctrl + SATA_CTRL0);
  125. return 0;
  126. }
  127. static int exynos4_ahci_init(struct device *dev, void __iomem *mmio)
  128. {
  129. struct clk *clk_sata, *clk_sataphy, *clk_sclk_sata;
  130. int val, ret;
  131. phy_base = ioremap(EXYNOS4_PA_SATAPHY, SZ_64K);
  132. if (!phy_base) {
  133. dev_err(dev, "failed to allocate memory for SATA PHY\n");
  134. return -ENOMEM;
  135. }
  136. phy_ctrl = ioremap(EXYNOS4_PA_SATAPHY_CTRL, SZ_16);
  137. if (!phy_ctrl) {
  138. dev_err(dev, "failed to allocate memory for SATA PHY CTRL\n");
  139. ret = -ENOMEM;
  140. goto err1;
  141. }
  142. clk_sata = clk_get(dev, "sata");
  143. if (IS_ERR(clk_sata)) {
  144. dev_err(dev, "failed to get sata clock\n");
  145. ret = PTR_ERR(clk_sata);
  146. clk_sata = NULL;
  147. goto err2;
  148. }
  149. clk_enable(clk_sata);
  150. clk_sataphy = clk_get(dev, "sataphy");
  151. if (IS_ERR(clk_sataphy)) {
  152. dev_err(dev, "failed to get sataphy clock\n");
  153. ret = PTR_ERR(clk_sataphy);
  154. clk_sataphy = NULL;
  155. goto err3;
  156. }
  157. clk_enable(clk_sataphy);
  158. clk_sclk_sata = clk_get(dev, "sclk_sata");
  159. if (IS_ERR(clk_sclk_sata)) {
  160. dev_err(dev, "failed to get sclk_sata\n");
  161. ret = PTR_ERR(clk_sclk_sata);
  162. clk_sclk_sata = NULL;
  163. goto err4;
  164. }
  165. clk_enable(clk_sclk_sata);
  166. clk_set_rate(clk_sclk_sata, SCLK_SATA_FREQ);
  167. __raw_writel(S5P_PMU_SATA_PHY_CONTROL_EN, S5P_PMU_SATA_PHY_CONTROL);
  168. /* Enable PHY link control */
  169. val = SATA_CTRL1_RST_PMALIVE_N | SATA_CTRL1_RST_RXOOB_N |
  170. SATA_CTRL1_RST_RX_N | SATA_CTRL1_RST_TX_N;
  171. __raw_writel(val, phy_ctrl + SATA_CTRL1);
  172. /* Set communication speed as 3Gbps and enable PHY power */
  173. val = SATA_CTRL0_RX_DATA_VALID(3) | SATA_CTRL0_SPEED_MODE |
  174. SATA_CTRL0_PHY_POR_N;
  175. __raw_writel(val, phy_ctrl + SATA_CTRL0);
  176. /* Port0 is available */
  177. __raw_writel(0x1, mmio + HOST_PORTS_IMPL);
  178. return ahci_phy_init(mmio);
  179. err4:
  180. clk_disable(clk_sataphy);
  181. clk_put(clk_sataphy);
  182. err3:
  183. clk_disable(clk_sata);
  184. clk_put(clk_sata);
  185. err2:
  186. iounmap(phy_ctrl);
  187. err1:
  188. iounmap(phy_base);
  189. return ret;
  190. }
  191. static struct ahci_platform_data exynos4_ahci_pdata = {
  192. .init = exynos4_ahci_init,
  193. };
  194. static struct resource exynos4_ahci_resource[] = {
  195. [0] = {
  196. .start = EXYNOS4_PA_SATA,
  197. .end = EXYNOS4_PA_SATA + SZ_64K - 1,
  198. .flags = IORESOURCE_MEM,
  199. },
  200. [1] = {
  201. .start = EXYNOS4_IRQ_SATA,
  202. .end = EXYNOS4_IRQ_SATA,
  203. .flags = IORESOURCE_IRQ,
  204. },
  205. };
  206. static u64 exynos4_ahci_dmamask = DMA_BIT_MASK(32);
  207. struct platform_device exynos4_device_ahci = {
  208. .name = "ahci",
  209. .id = -1,
  210. .resource = exynos4_ahci_resource,
  211. .num_resources = ARRAY_SIZE(exynos4_ahci_resource),
  212. .dev = {
  213. .platform_data = &exynos4_ahci_pdata,
  214. .dma_mask = &exynos4_ahci_dmamask,
  215. .coherent_dma_mask = DMA_BIT_MASK(32),
  216. },
  217. };