ahci_brcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Broadcom SATA3 AHCI Controller Driver
  3. *
  4. * Copyright © 2009-2015 Broadcom Corporation
  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. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/ahci_platform.h>
  17. #include <linux/compiler.h>
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/kernel.h>
  23. #include <linux/libata.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/reset.h>
  28. #include <linux/string.h>
  29. #include "ahci.h"
  30. #define DRV_NAME "brcm-ahci"
  31. #define SATA_TOP_CTRL_VERSION 0x0
  32. #define SATA_TOP_CTRL_BUS_CTRL 0x4
  33. #define MMIO_ENDIAN_SHIFT 0 /* CPU->AHCI */
  34. #define DMADESC_ENDIAN_SHIFT 2 /* AHCI->DDR */
  35. #define DMADATA_ENDIAN_SHIFT 4 /* AHCI->DDR */
  36. #define PIODATA_ENDIAN_SHIFT 6
  37. #define ENDIAN_SWAP_NONE 0
  38. #define ENDIAN_SWAP_FULL 2
  39. #define SATA_TOP_CTRL_TP_CTRL 0x8
  40. #define SATA_TOP_CTRL_PHY_CTRL 0xc
  41. #define SATA_TOP_CTRL_PHY_CTRL_1 0x0
  42. #define SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE BIT(14)
  43. #define SATA_TOP_CTRL_PHY_CTRL_2 0x4
  44. #define SATA_TOP_CTRL_2_SW_RST_MDIOREG BIT(0)
  45. #define SATA_TOP_CTRL_2_SW_RST_OOB BIT(1)
  46. #define SATA_TOP_CTRL_2_SW_RST_RX BIT(2)
  47. #define SATA_TOP_CTRL_2_SW_RST_TX BIT(3)
  48. #define SATA_TOP_CTRL_2_PHY_GLOBAL_RESET BIT(14)
  49. #define SATA_TOP_CTRL_PHY_OFFS 0x8
  50. #define SATA_TOP_MAX_PHYS 2
  51. #define SATA_FIRST_PORT_CTRL 0x700
  52. #define SATA_NEXT_PORT_CTRL_OFFSET 0x80
  53. #define SATA_PORT_PCTRL6(reg_base) (reg_base + 0x18)
  54. /* On big-endian MIPS, buses are reversed to big endian, so switch them back */
  55. #if defined(CONFIG_MIPS) && defined(__BIG_ENDIAN)
  56. #define DATA_ENDIAN 2 /* AHCI->DDR inbound accesses */
  57. #define MMIO_ENDIAN 2 /* CPU->AHCI outbound accesses */
  58. #else
  59. #define DATA_ENDIAN 0
  60. #define MMIO_ENDIAN 0
  61. #endif
  62. #define BUS_CTRL_ENDIAN_CONF \
  63. ((DATA_ENDIAN << DMADATA_ENDIAN_SHIFT) | \
  64. (DATA_ENDIAN << DMADESC_ENDIAN_SHIFT) | \
  65. (MMIO_ENDIAN << MMIO_ENDIAN_SHIFT))
  66. enum brcm_ahci_version {
  67. BRCM_SATA_BCM7425 = 1,
  68. BRCM_SATA_BCM7445,
  69. BRCM_SATA_NSP,
  70. };
  71. enum brcm_ahci_quirks {
  72. BRCM_AHCI_QUIRK_NO_NCQ = BIT(0),
  73. BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE = BIT(1),
  74. };
  75. struct brcm_ahci_priv {
  76. struct device *dev;
  77. void __iomem *top_ctrl;
  78. u32 port_mask;
  79. u32 quirks;
  80. enum brcm_ahci_version version;
  81. struct reset_control *rcdev;
  82. };
  83. static const struct ata_port_info ahci_brcm_port_info = {
  84. .flags = AHCI_FLAG_COMMON | ATA_FLAG_NO_DIPM,
  85. .link_flags = ATA_LFLAG_NO_DB_DELAY,
  86. .pio_mask = ATA_PIO4,
  87. .udma_mask = ATA_UDMA6,
  88. .port_ops = &ahci_platform_ops,
  89. };
  90. static inline u32 brcm_sata_readreg(void __iomem *addr)
  91. {
  92. /*
  93. * MIPS endianness is configured by boot strap, which also reverses all
  94. * bus endianness (i.e., big-endian CPU + big endian bus ==> native
  95. * endian I/O).
  96. *
  97. * Other architectures (e.g., ARM) either do not support big endian, or
  98. * else leave I/O in little endian mode.
  99. */
  100. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  101. return __raw_readl(addr);
  102. else
  103. return readl_relaxed(addr);
  104. }
  105. static inline void brcm_sata_writereg(u32 val, void __iomem *addr)
  106. {
  107. /* See brcm_sata_readreg() comments */
  108. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  109. __raw_writel(val, addr);
  110. else
  111. writel_relaxed(val, addr);
  112. }
  113. static void brcm_sata_alpm_init(struct ahci_host_priv *hpriv)
  114. {
  115. struct brcm_ahci_priv *priv = hpriv->plat_data;
  116. u32 port_ctrl, host_caps;
  117. int i;
  118. /* Enable support for ALPM */
  119. host_caps = readl(hpriv->mmio + HOST_CAP);
  120. if (!(host_caps & HOST_CAP_ALPM))
  121. hpriv->flags |= AHCI_HFLAG_YES_ALPM;
  122. /*
  123. * Adjust timeout to allow PLL sufficient time to lock while waking
  124. * up from slumber mode.
  125. */
  126. for (i = 0, port_ctrl = SATA_FIRST_PORT_CTRL;
  127. i < SATA_TOP_MAX_PHYS;
  128. i++, port_ctrl += SATA_NEXT_PORT_CTRL_OFFSET) {
  129. if (priv->port_mask & BIT(i))
  130. writel(0xff1003fc,
  131. hpriv->mmio + SATA_PORT_PCTRL6(port_ctrl));
  132. }
  133. }
  134. static void brcm_sata_phy_enable(struct brcm_ahci_priv *priv, int port)
  135. {
  136. void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
  137. (port * SATA_TOP_CTRL_PHY_OFFS);
  138. void __iomem *p;
  139. u32 reg;
  140. if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
  141. return;
  142. /* clear PHY_DEFAULT_POWER_STATE */
  143. p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
  144. reg = brcm_sata_readreg(p);
  145. reg &= ~SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
  146. brcm_sata_writereg(reg, p);
  147. /* reset the PHY digital logic */
  148. p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
  149. reg = brcm_sata_readreg(p);
  150. reg &= ~(SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
  151. SATA_TOP_CTRL_2_SW_RST_RX);
  152. reg |= SATA_TOP_CTRL_2_SW_RST_TX;
  153. brcm_sata_writereg(reg, p);
  154. reg = brcm_sata_readreg(p);
  155. reg |= SATA_TOP_CTRL_2_PHY_GLOBAL_RESET;
  156. brcm_sata_writereg(reg, p);
  157. reg = brcm_sata_readreg(p);
  158. reg &= ~SATA_TOP_CTRL_2_PHY_GLOBAL_RESET;
  159. brcm_sata_writereg(reg, p);
  160. (void)brcm_sata_readreg(p);
  161. }
  162. static void brcm_sata_phy_disable(struct brcm_ahci_priv *priv, int port)
  163. {
  164. void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
  165. (port * SATA_TOP_CTRL_PHY_OFFS);
  166. void __iomem *p;
  167. u32 reg;
  168. if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
  169. return;
  170. /* power-off the PHY digital logic */
  171. p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
  172. reg = brcm_sata_readreg(p);
  173. reg |= (SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
  174. SATA_TOP_CTRL_2_SW_RST_RX | SATA_TOP_CTRL_2_SW_RST_TX |
  175. SATA_TOP_CTRL_2_PHY_GLOBAL_RESET);
  176. brcm_sata_writereg(reg, p);
  177. /* set PHY_DEFAULT_POWER_STATE */
  178. p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
  179. reg = brcm_sata_readreg(p);
  180. reg |= SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
  181. brcm_sata_writereg(reg, p);
  182. }
  183. static void brcm_sata_phys_enable(struct brcm_ahci_priv *priv)
  184. {
  185. int i;
  186. for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
  187. if (priv->port_mask & BIT(i))
  188. brcm_sata_phy_enable(priv, i);
  189. }
  190. static void brcm_sata_phys_disable(struct brcm_ahci_priv *priv)
  191. {
  192. int i;
  193. for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
  194. if (priv->port_mask & BIT(i))
  195. brcm_sata_phy_disable(priv, i);
  196. }
  197. static u32 brcm_ahci_get_portmask(struct ahci_host_priv *hpriv,
  198. struct brcm_ahci_priv *priv)
  199. {
  200. u32 impl;
  201. impl = readl(hpriv->mmio + HOST_PORTS_IMPL);
  202. if (fls(impl) > SATA_TOP_MAX_PHYS)
  203. dev_warn(priv->dev, "warning: more ports than PHYs (%#x)\n",
  204. impl);
  205. else if (!impl)
  206. dev_info(priv->dev, "no ports found\n");
  207. return impl;
  208. }
  209. static void brcm_sata_init(struct brcm_ahci_priv *priv)
  210. {
  211. void __iomem *ctrl = priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL;
  212. /* Configure endianness */
  213. if (priv->version == BRCM_SATA_NSP) {
  214. u32 data = brcm_sata_readreg(ctrl);
  215. data &= ~((0x03 << DMADATA_ENDIAN_SHIFT) |
  216. (0x03 << DMADESC_ENDIAN_SHIFT));
  217. data |= (0x02 << DMADATA_ENDIAN_SHIFT) |
  218. (0x02 << DMADESC_ENDIAN_SHIFT);
  219. brcm_sata_writereg(data, ctrl);
  220. } else
  221. brcm_sata_writereg(BUS_CTRL_ENDIAN_CONF, ctrl);
  222. }
  223. #ifdef CONFIG_PM_SLEEP
  224. static int brcm_ahci_suspend(struct device *dev)
  225. {
  226. struct ata_host *host = dev_get_drvdata(dev);
  227. struct ahci_host_priv *hpriv = host->private_data;
  228. struct brcm_ahci_priv *priv = hpriv->plat_data;
  229. brcm_sata_phys_disable(priv);
  230. return ahci_platform_suspend(dev);
  231. }
  232. static int brcm_ahci_resume(struct device *dev)
  233. {
  234. struct ata_host *host = dev_get_drvdata(dev);
  235. struct ahci_host_priv *hpriv = host->private_data;
  236. struct brcm_ahci_priv *priv = hpriv->plat_data;
  237. int ret;
  238. /* Make sure clocks are turned on before re-configuration */
  239. ret = ahci_platform_enable_clks(hpriv);
  240. if (ret)
  241. return ret;
  242. ret = ahci_platform_enable_regulators(hpriv);
  243. if (ret)
  244. goto out_disable_clks;
  245. brcm_sata_init(priv);
  246. brcm_sata_phys_enable(priv);
  247. brcm_sata_alpm_init(hpriv);
  248. /* Since we had to enable clocks earlier on, we cannot use
  249. * ahci_platform_resume() as-is since a second call to
  250. * ahci_platform_enable_resources() would bump up the resources
  251. * (regulators, clocks, PHYs) count artificially so we copy the part
  252. * after ahci_platform_enable_resources().
  253. */
  254. ret = ahci_platform_enable_phys(hpriv);
  255. if (ret)
  256. goto out_disable_phys;
  257. ret = ahci_platform_resume_host(dev);
  258. if (ret)
  259. goto out_disable_platform_phys;
  260. /* We resumed so update PM runtime state */
  261. pm_runtime_disable(dev);
  262. pm_runtime_set_active(dev);
  263. pm_runtime_enable(dev);
  264. return 0;
  265. out_disable_platform_phys:
  266. ahci_platform_disable_phys(hpriv);
  267. out_disable_phys:
  268. brcm_sata_phys_disable(priv);
  269. ahci_platform_disable_regulators(hpriv);
  270. out_disable_clks:
  271. ahci_platform_disable_clks(hpriv);
  272. return ret;
  273. }
  274. #endif
  275. static struct scsi_host_template ahci_platform_sht = {
  276. AHCI_SHT(DRV_NAME),
  277. };
  278. static const struct of_device_id ahci_of_match[] = {
  279. {.compatible = "brcm,bcm7425-ahci", .data = (void *)BRCM_SATA_BCM7425},
  280. {.compatible = "brcm,bcm7445-ahci", .data = (void *)BRCM_SATA_BCM7445},
  281. {.compatible = "brcm,bcm-nsp-ahci", .data = (void *)BRCM_SATA_NSP},
  282. {},
  283. };
  284. MODULE_DEVICE_TABLE(of, ahci_of_match);
  285. static int brcm_ahci_probe(struct platform_device *pdev)
  286. {
  287. const struct of_device_id *of_id;
  288. struct device *dev = &pdev->dev;
  289. struct brcm_ahci_priv *priv;
  290. struct ahci_host_priv *hpriv;
  291. struct resource *res;
  292. int ret;
  293. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  294. if (!priv)
  295. return -ENOMEM;
  296. of_id = of_match_node(ahci_of_match, pdev->dev.of_node);
  297. if (!of_id)
  298. return -ENODEV;
  299. priv->version = (enum brcm_ahci_version)of_id->data;
  300. priv->dev = dev;
  301. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "top-ctrl");
  302. priv->top_ctrl = devm_ioremap_resource(dev, res);
  303. if (IS_ERR(priv->top_ctrl))
  304. return PTR_ERR(priv->top_ctrl);
  305. /* Reset is optional depending on platform */
  306. priv->rcdev = devm_reset_control_get(&pdev->dev, "ahci");
  307. if (!IS_ERR_OR_NULL(priv->rcdev))
  308. reset_control_deassert(priv->rcdev);
  309. if ((priv->version == BRCM_SATA_BCM7425) ||
  310. (priv->version == BRCM_SATA_NSP)) {
  311. priv->quirks |= BRCM_AHCI_QUIRK_NO_NCQ;
  312. priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE;
  313. }
  314. hpriv = ahci_platform_get_resources(pdev);
  315. if (IS_ERR(hpriv)) {
  316. ret = PTR_ERR(hpriv);
  317. goto out_reset;
  318. }
  319. ret = ahci_platform_enable_clks(hpriv);
  320. if (ret)
  321. goto out_reset;
  322. ret = ahci_platform_enable_regulators(hpriv);
  323. if (ret)
  324. goto out_disable_clks;
  325. /* Must be first so as to configure endianness including that
  326. * of the standard AHCI register space.
  327. */
  328. brcm_sata_init(priv);
  329. /* Initializes priv->port_mask which is used below */
  330. priv->port_mask = brcm_ahci_get_portmask(hpriv, priv);
  331. if (!priv->port_mask) {
  332. ret = -ENODEV;
  333. goto out_disable_regulators;
  334. }
  335. /* Must be done before ahci_platform_enable_phys() */
  336. brcm_sata_phys_enable(priv);
  337. hpriv->plat_data = priv;
  338. hpriv->flags = AHCI_HFLAG_WAKE_BEFORE_STOP;
  339. brcm_sata_alpm_init(hpriv);
  340. if (priv->quirks & BRCM_AHCI_QUIRK_NO_NCQ)
  341. hpriv->flags |= AHCI_HFLAG_NO_NCQ;
  342. hpriv->flags |= AHCI_HFLAG_NO_WRITE_TO_RO;
  343. ret = ahci_platform_enable_phys(hpriv);
  344. if (ret)
  345. goto out_disable_phys;
  346. ret = ahci_platform_init_host(pdev, hpriv, &ahci_brcm_port_info,
  347. &ahci_platform_sht);
  348. if (ret)
  349. goto out_disable_platform_phys;
  350. dev_info(dev, "Broadcom AHCI SATA3 registered\n");
  351. return 0;
  352. out_disable_platform_phys:
  353. ahci_platform_disable_phys(hpriv);
  354. out_disable_phys:
  355. brcm_sata_phys_disable(priv);
  356. out_disable_regulators:
  357. ahci_platform_disable_regulators(hpriv);
  358. out_disable_clks:
  359. ahci_platform_disable_clks(hpriv);
  360. out_reset:
  361. if (!IS_ERR_OR_NULL(priv->rcdev))
  362. reset_control_assert(priv->rcdev);
  363. return ret;
  364. }
  365. static int brcm_ahci_remove(struct platform_device *pdev)
  366. {
  367. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  368. struct ahci_host_priv *hpriv = host->private_data;
  369. struct brcm_ahci_priv *priv = hpriv->plat_data;
  370. int ret;
  371. brcm_sata_phys_disable(priv);
  372. ret = ata_platform_remove_one(pdev);
  373. if (ret)
  374. return ret;
  375. return 0;
  376. }
  377. static SIMPLE_DEV_PM_OPS(ahci_brcm_pm_ops, brcm_ahci_suspend, brcm_ahci_resume);
  378. static struct platform_driver brcm_ahci_driver = {
  379. .probe = brcm_ahci_probe,
  380. .remove = brcm_ahci_remove,
  381. .driver = {
  382. .name = DRV_NAME,
  383. .of_match_table = ahci_of_match,
  384. .pm = &ahci_brcm_pm_ops,
  385. },
  386. };
  387. module_platform_driver(brcm_ahci_driver);
  388. MODULE_DESCRIPTION("Broadcom SATA3 AHCI Controller Driver");
  389. MODULE_AUTHOR("Brian Norris");
  390. MODULE_LICENSE("GPL");
  391. MODULE_ALIAS("platform:sata-brcmstb");