ahci_brcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. #define BUS_CTRL_ENDIAN_NSP_CONF \
  67. (0x02 << DMADATA_ENDIAN_SHIFT | 0x02 << DMADESC_ENDIAN_SHIFT)
  68. #define BUS_CTRL_ENDIAN_CONF_MASK \
  69. (0x3 << MMIO_ENDIAN_SHIFT | 0x3 << DMADESC_ENDIAN_SHIFT | \
  70. 0x3 << DMADATA_ENDIAN_SHIFT | 0x3 << PIODATA_ENDIAN_SHIFT)
  71. enum brcm_ahci_version {
  72. BRCM_SATA_BCM7425 = 1,
  73. BRCM_SATA_BCM7445,
  74. BRCM_SATA_NSP,
  75. };
  76. enum brcm_ahci_quirks {
  77. BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE = BIT(0),
  78. };
  79. struct brcm_ahci_priv {
  80. struct device *dev;
  81. void __iomem *top_ctrl;
  82. u32 port_mask;
  83. u32 quirks;
  84. enum brcm_ahci_version version;
  85. struct reset_control *rcdev;
  86. };
  87. static inline u32 brcm_sata_readreg(void __iomem *addr)
  88. {
  89. /*
  90. * MIPS endianness is configured by boot strap, which also reverses all
  91. * bus endianness (i.e., big-endian CPU + big endian bus ==> native
  92. * endian I/O).
  93. *
  94. * Other architectures (e.g., ARM) either do not support big endian, or
  95. * else leave I/O in little endian mode.
  96. */
  97. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  98. return __raw_readl(addr);
  99. else
  100. return readl_relaxed(addr);
  101. }
  102. static inline void brcm_sata_writereg(u32 val, void __iomem *addr)
  103. {
  104. /* See brcm_sata_readreg() comments */
  105. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  106. __raw_writel(val, addr);
  107. else
  108. writel_relaxed(val, addr);
  109. }
  110. static void brcm_sata_alpm_init(struct ahci_host_priv *hpriv)
  111. {
  112. struct brcm_ahci_priv *priv = hpriv->plat_data;
  113. u32 port_ctrl, host_caps;
  114. int i;
  115. /* Enable support for ALPM */
  116. host_caps = readl(hpriv->mmio + HOST_CAP);
  117. if (!(host_caps & HOST_CAP_ALPM))
  118. hpriv->flags |= AHCI_HFLAG_YES_ALPM;
  119. /*
  120. * Adjust timeout to allow PLL sufficient time to lock while waking
  121. * up from slumber mode.
  122. */
  123. for (i = 0, port_ctrl = SATA_FIRST_PORT_CTRL;
  124. i < SATA_TOP_MAX_PHYS;
  125. i++, port_ctrl += SATA_NEXT_PORT_CTRL_OFFSET) {
  126. if (priv->port_mask & BIT(i))
  127. writel(0xff1003fc,
  128. hpriv->mmio + SATA_PORT_PCTRL6(port_ctrl));
  129. }
  130. }
  131. static void brcm_sata_phy_enable(struct brcm_ahci_priv *priv, int port)
  132. {
  133. void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
  134. (port * SATA_TOP_CTRL_PHY_OFFS);
  135. void __iomem *p;
  136. u32 reg;
  137. if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
  138. return;
  139. /* clear PHY_DEFAULT_POWER_STATE */
  140. p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
  141. reg = brcm_sata_readreg(p);
  142. reg &= ~SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
  143. brcm_sata_writereg(reg, p);
  144. /* reset the PHY digital logic */
  145. p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
  146. reg = brcm_sata_readreg(p);
  147. reg &= ~(SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
  148. SATA_TOP_CTRL_2_SW_RST_RX);
  149. reg |= SATA_TOP_CTRL_2_SW_RST_TX;
  150. brcm_sata_writereg(reg, p);
  151. reg = brcm_sata_readreg(p);
  152. reg |= SATA_TOP_CTRL_2_PHY_GLOBAL_RESET;
  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. (void)brcm_sata_readreg(p);
  158. }
  159. static void brcm_sata_phy_disable(struct brcm_ahci_priv *priv, int port)
  160. {
  161. void __iomem *phyctrl = priv->top_ctrl + SATA_TOP_CTRL_PHY_CTRL +
  162. (port * SATA_TOP_CTRL_PHY_OFFS);
  163. void __iomem *p;
  164. u32 reg;
  165. if (priv->quirks & BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE)
  166. return;
  167. /* power-off the PHY digital logic */
  168. p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_2;
  169. reg = brcm_sata_readreg(p);
  170. reg |= (SATA_TOP_CTRL_2_SW_RST_MDIOREG | SATA_TOP_CTRL_2_SW_RST_OOB |
  171. SATA_TOP_CTRL_2_SW_RST_RX | SATA_TOP_CTRL_2_SW_RST_TX |
  172. SATA_TOP_CTRL_2_PHY_GLOBAL_RESET);
  173. brcm_sata_writereg(reg, p);
  174. /* set PHY_DEFAULT_POWER_STATE */
  175. p = phyctrl + SATA_TOP_CTRL_PHY_CTRL_1;
  176. reg = brcm_sata_readreg(p);
  177. reg |= SATA_TOP_CTRL_1_PHY_DEFAULT_POWER_STATE;
  178. brcm_sata_writereg(reg, p);
  179. }
  180. static void brcm_sata_phys_enable(struct brcm_ahci_priv *priv)
  181. {
  182. int i;
  183. for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
  184. if (priv->port_mask & BIT(i))
  185. brcm_sata_phy_enable(priv, i);
  186. }
  187. static void brcm_sata_phys_disable(struct brcm_ahci_priv *priv)
  188. {
  189. int i;
  190. for (i = 0; i < SATA_TOP_MAX_PHYS; i++)
  191. if (priv->port_mask & BIT(i))
  192. brcm_sata_phy_disable(priv, i);
  193. }
  194. static u32 brcm_ahci_get_portmask(struct ahci_host_priv *hpriv,
  195. struct brcm_ahci_priv *priv)
  196. {
  197. u32 impl;
  198. impl = readl(hpriv->mmio + HOST_PORTS_IMPL);
  199. if (fls(impl) > SATA_TOP_MAX_PHYS)
  200. dev_warn(priv->dev, "warning: more ports than PHYs (%#x)\n",
  201. impl);
  202. else if (!impl)
  203. dev_info(priv->dev, "no ports found\n");
  204. return impl;
  205. }
  206. static void brcm_sata_init(struct brcm_ahci_priv *priv)
  207. {
  208. void __iomem *ctrl = priv->top_ctrl + SATA_TOP_CTRL_BUS_CTRL;
  209. u32 data;
  210. /* Configure endianness */
  211. data = brcm_sata_readreg(ctrl);
  212. data &= ~BUS_CTRL_ENDIAN_CONF_MASK;
  213. if (priv->version == BRCM_SATA_NSP)
  214. data |= BUS_CTRL_ENDIAN_NSP_CONF;
  215. else
  216. data |= BUS_CTRL_ENDIAN_CONF;
  217. brcm_sata_writereg(data, ctrl);
  218. }
  219. static unsigned int brcm_ahci_read_id(struct ata_device *dev,
  220. struct ata_taskfile *tf, u16 *id)
  221. {
  222. struct ata_port *ap = dev->link->ap;
  223. struct ata_host *host = ap->host;
  224. struct ahci_host_priv *hpriv = host->private_data;
  225. struct brcm_ahci_priv *priv = hpriv->plat_data;
  226. void __iomem *mmio = hpriv->mmio;
  227. unsigned int err_mask;
  228. unsigned long flags;
  229. int i, rc;
  230. u32 ctl;
  231. /* Try to read the device ID and, if this fails, proceed with the
  232. * recovery sequence below
  233. */
  234. err_mask = ata_do_dev_read_id(dev, tf, id);
  235. if (likely(!err_mask))
  236. return err_mask;
  237. /* Disable host interrupts */
  238. spin_lock_irqsave(&host->lock, flags);
  239. ctl = readl(mmio + HOST_CTL);
  240. ctl &= ~HOST_IRQ_EN;
  241. writel(ctl, mmio + HOST_CTL);
  242. readl(mmio + HOST_CTL); /* flush */
  243. spin_unlock_irqrestore(&host->lock, flags);
  244. /* Perform the SATA PHY reset sequence */
  245. brcm_sata_phy_disable(priv, ap->port_no);
  246. /* Reset the SATA clock */
  247. ahci_platform_disable_clks(hpriv);
  248. msleep(10);
  249. ahci_platform_enable_clks(hpriv);
  250. msleep(10);
  251. /* Bring the PHY back on */
  252. brcm_sata_phy_enable(priv, ap->port_no);
  253. /* Re-initialize and calibrate the PHY */
  254. for (i = 0; i < hpriv->nports; i++) {
  255. rc = phy_init(hpriv->phys[i]);
  256. if (rc)
  257. goto disable_phys;
  258. rc = phy_calibrate(hpriv->phys[i]);
  259. if (rc) {
  260. phy_exit(hpriv->phys[i]);
  261. goto disable_phys;
  262. }
  263. }
  264. /* Re-enable host interrupts */
  265. spin_lock_irqsave(&host->lock, flags);
  266. ctl = readl(mmio + HOST_CTL);
  267. ctl |= HOST_IRQ_EN;
  268. writel(ctl, mmio + HOST_CTL);
  269. readl(mmio + HOST_CTL); /* flush */
  270. spin_unlock_irqrestore(&host->lock, flags);
  271. return ata_do_dev_read_id(dev, tf, id);
  272. disable_phys:
  273. while (--i >= 0) {
  274. phy_power_off(hpriv->phys[i]);
  275. phy_exit(hpriv->phys[i]);
  276. }
  277. return AC_ERR_OTHER;
  278. }
  279. static void brcm_ahci_host_stop(struct ata_host *host)
  280. {
  281. struct ahci_host_priv *hpriv = host->private_data;
  282. ahci_platform_disable_resources(hpriv);
  283. }
  284. static struct ata_port_operations ahci_brcm_platform_ops = {
  285. .inherits = &ahci_ops,
  286. .host_stop = brcm_ahci_host_stop,
  287. .read_id = brcm_ahci_read_id,
  288. };
  289. static const struct ata_port_info ahci_brcm_port_info = {
  290. .flags = AHCI_FLAG_COMMON | ATA_FLAG_NO_DIPM,
  291. .link_flags = ATA_LFLAG_NO_DB_DELAY,
  292. .pio_mask = ATA_PIO4,
  293. .udma_mask = ATA_UDMA6,
  294. .port_ops = &ahci_brcm_platform_ops,
  295. };
  296. #ifdef CONFIG_PM_SLEEP
  297. static int brcm_ahci_suspend(struct device *dev)
  298. {
  299. struct ata_host *host = dev_get_drvdata(dev);
  300. struct ahci_host_priv *hpriv = host->private_data;
  301. struct brcm_ahci_priv *priv = hpriv->plat_data;
  302. brcm_sata_phys_disable(priv);
  303. return ahci_platform_suspend(dev);
  304. }
  305. static int brcm_ahci_resume(struct device *dev)
  306. {
  307. struct ata_host *host = dev_get_drvdata(dev);
  308. struct ahci_host_priv *hpriv = host->private_data;
  309. struct brcm_ahci_priv *priv = hpriv->plat_data;
  310. int ret;
  311. /* Make sure clocks are turned on before re-configuration */
  312. ret = ahci_platform_enable_clks(hpriv);
  313. if (ret)
  314. return ret;
  315. brcm_sata_init(priv);
  316. brcm_sata_phys_enable(priv);
  317. brcm_sata_alpm_init(hpriv);
  318. /* Since we had to enable clocks earlier on, we cannot use
  319. * ahci_platform_resume() as-is since a second call to
  320. * ahci_platform_enable_resources() would bump up the resources
  321. * (regulators, clocks, PHYs) count artificially so we copy the part
  322. * after ahci_platform_enable_resources().
  323. */
  324. ret = ahci_platform_enable_phys(hpriv);
  325. if (ret)
  326. goto out_disable_phys;
  327. ret = ahci_platform_resume_host(dev);
  328. if (ret)
  329. goto out_disable_platform_phys;
  330. /* We resumed so update PM runtime state */
  331. pm_runtime_disable(dev);
  332. pm_runtime_set_active(dev);
  333. pm_runtime_enable(dev);
  334. return 0;
  335. out_disable_platform_phys:
  336. ahci_platform_disable_phys(hpriv);
  337. out_disable_phys:
  338. brcm_sata_phys_disable(priv);
  339. ahci_platform_disable_clks(hpriv);
  340. return ret;
  341. }
  342. #endif
  343. static struct scsi_host_template ahci_platform_sht = {
  344. AHCI_SHT(DRV_NAME),
  345. };
  346. static const struct of_device_id ahci_of_match[] = {
  347. {.compatible = "brcm,bcm7425-ahci", .data = (void *)BRCM_SATA_BCM7425},
  348. {.compatible = "brcm,bcm7445-ahci", .data = (void *)BRCM_SATA_BCM7445},
  349. {.compatible = "brcm,bcm-nsp-ahci", .data = (void *)BRCM_SATA_NSP},
  350. {},
  351. };
  352. MODULE_DEVICE_TABLE(of, ahci_of_match);
  353. static int brcm_ahci_probe(struct platform_device *pdev)
  354. {
  355. const struct of_device_id *of_id;
  356. struct device *dev = &pdev->dev;
  357. struct brcm_ahci_priv *priv;
  358. struct ahci_host_priv *hpriv;
  359. struct resource *res;
  360. int ret;
  361. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  362. if (!priv)
  363. return -ENOMEM;
  364. of_id = of_match_node(ahci_of_match, pdev->dev.of_node);
  365. if (!of_id)
  366. return -ENODEV;
  367. priv->version = (enum brcm_ahci_version)of_id->data;
  368. priv->dev = dev;
  369. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "top-ctrl");
  370. priv->top_ctrl = devm_ioremap_resource(dev, res);
  371. if (IS_ERR(priv->top_ctrl))
  372. return PTR_ERR(priv->top_ctrl);
  373. /* Reset is optional depending on platform */
  374. priv->rcdev = devm_reset_control_get(&pdev->dev, "ahci");
  375. if (!IS_ERR_OR_NULL(priv->rcdev))
  376. reset_control_deassert(priv->rcdev);
  377. hpriv = ahci_platform_get_resources(pdev, 0);
  378. if (IS_ERR(hpriv)) {
  379. ret = PTR_ERR(hpriv);
  380. goto out_reset;
  381. }
  382. hpriv->plat_data = priv;
  383. hpriv->flags = AHCI_HFLAG_WAKE_BEFORE_STOP | AHCI_HFLAG_NO_WRITE_TO_RO;
  384. switch (priv->version) {
  385. case BRCM_SATA_BCM7425:
  386. hpriv->flags |= AHCI_HFLAG_DELAY_ENGINE;
  387. /* fall through */
  388. case BRCM_SATA_NSP:
  389. hpriv->flags |= AHCI_HFLAG_NO_NCQ;
  390. priv->quirks |= BRCM_AHCI_QUIRK_SKIP_PHY_ENABLE;
  391. break;
  392. default:
  393. break;
  394. }
  395. ret = ahci_platform_enable_clks(hpriv);
  396. if (ret)
  397. goto out_reset;
  398. /* Must be first so as to configure endianness including that
  399. * of the standard AHCI register space.
  400. */
  401. brcm_sata_init(priv);
  402. /* Initializes priv->port_mask which is used below */
  403. priv->port_mask = brcm_ahci_get_portmask(hpriv, priv);
  404. if (!priv->port_mask) {
  405. ret = -ENODEV;
  406. goto out_disable_clks;
  407. }
  408. /* Must be done before ahci_platform_enable_phys() */
  409. brcm_sata_phys_enable(priv);
  410. brcm_sata_alpm_init(hpriv);
  411. ret = ahci_platform_enable_phys(hpriv);
  412. if (ret)
  413. goto out_disable_phys;
  414. ret = ahci_platform_init_host(pdev, hpriv, &ahci_brcm_port_info,
  415. &ahci_platform_sht);
  416. if (ret)
  417. goto out_disable_platform_phys;
  418. dev_info(dev, "Broadcom AHCI SATA3 registered\n");
  419. return 0;
  420. out_disable_platform_phys:
  421. ahci_platform_disable_phys(hpriv);
  422. out_disable_phys:
  423. brcm_sata_phys_disable(priv);
  424. out_disable_clks:
  425. ahci_platform_disable_clks(hpriv);
  426. out_reset:
  427. if (!IS_ERR_OR_NULL(priv->rcdev))
  428. reset_control_assert(priv->rcdev);
  429. return ret;
  430. }
  431. static int brcm_ahci_remove(struct platform_device *pdev)
  432. {
  433. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  434. struct ahci_host_priv *hpriv = host->private_data;
  435. struct brcm_ahci_priv *priv = hpriv->plat_data;
  436. int ret;
  437. brcm_sata_phys_disable(priv);
  438. ret = ata_platform_remove_one(pdev);
  439. if (ret)
  440. return ret;
  441. return 0;
  442. }
  443. static SIMPLE_DEV_PM_OPS(ahci_brcm_pm_ops, brcm_ahci_suspend, brcm_ahci_resume);
  444. static struct platform_driver brcm_ahci_driver = {
  445. .probe = brcm_ahci_probe,
  446. .remove = brcm_ahci_remove,
  447. .driver = {
  448. .name = DRV_NAME,
  449. .of_match_table = ahci_of_match,
  450. .pm = &ahci_brcm_pm_ops,
  451. },
  452. };
  453. module_platform_driver(brcm_ahci_driver);
  454. MODULE_DESCRIPTION("Broadcom SATA3 AHCI Controller Driver");
  455. MODULE_AUTHOR("Brian Norris");
  456. MODULE_LICENSE("GPL");
  457. MODULE_ALIAS("platform:sata-brcmstb");