Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Freescale QorIQ AHCI SATA platform driver |
| 3 | * |
| 4 | * Copyright 2015 Freescale, Inc. |
| 5 | * Tang Yuantian <Yuantian.Tang@freescale.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2, or (at your option) |
| 10 | * any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/pm.h> |
| 16 | #include <linux/ahci_platform.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/of_address.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_device.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/libata.h> |
| 23 | #include "ahci.h" |
| 24 | |
| 25 | #define DRV_NAME "ahci-qoriq" |
| 26 | |
| 27 | /* port register definition */ |
| 28 | #define PORT_PHY1 0xA8 |
| 29 | #define PORT_PHY2 0xAC |
| 30 | #define PORT_PHY3 0xB0 |
| 31 | #define PORT_PHY4 0xB4 |
| 32 | #define PORT_PHY5 0xB8 |
Tang Yuantian | 16af080 | 2016-08-09 09:51:22 +0800 | [diff] [blame] | 33 | #define PORT_AXICC 0xBC |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 34 | #define PORT_TRANS 0xC8 |
| 35 | |
| 36 | /* port register default value */ |
| 37 | #define AHCI_PORT_PHY_1_CFG 0xa003fffe |
Tang Yuantian | e3a6dad | 2015-12-16 13:43:50 +0800 | [diff] [blame] | 38 | #define AHCI_PORT_TRANS_CFG 0x08000029 |
Tang Yuantian | 16af080 | 2016-08-09 09:51:22 +0800 | [diff] [blame] | 39 | #define AHCI_PORT_AXICC_CFG 0x3fffffff |
Tang Yuantian | dfcdc5f | 2015-12-16 14:00:35 +0800 | [diff] [blame] | 40 | |
| 41 | /* for ls1021a */ |
| 42 | #define LS1021A_PORT_PHY2 0x28183414 |
| 43 | #define LS1021A_PORT_PHY3 0x0e080e06 |
| 44 | #define LS1021A_PORT_PHY4 0x064a080b |
| 45 | #define LS1021A_PORT_PHY5 0x2aa86470 |
Tang Yuantian | 16af080 | 2016-08-09 09:51:22 +0800 | [diff] [blame] | 46 | #define LS1021A_AXICC_ADDR 0xC0 |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 47 | |
| 48 | #define SATA_ECC_DISABLE 0x00020000 |
Tang Yuantian | 2facc6d | 2016-10-09 16:51:04 +0800 | [diff] [blame] | 49 | #define LS1046A_SATA_ECC_DIS 0x80000000 |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 50 | |
| 51 | enum ahci_qoriq_type { |
| 52 | AHCI_LS1021A, |
| 53 | AHCI_LS1043A, |
Tang Yuantian | d19f9aa | 2015-10-29 14:22:15 +0800 | [diff] [blame] | 54 | AHCI_LS2080A, |
Tang Yuantian | 2facc6d | 2016-10-09 16:51:04 +0800 | [diff] [blame] | 55 | AHCI_LS1046A, |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct ahci_qoriq_priv { |
| 59 | struct ccsr_ahci *reg_base; |
| 60 | enum ahci_qoriq_type type; |
| 61 | void __iomem *ecc_addr; |
Tang Yuantian | 386dc3b | 2017-01-20 14:59:34 +0800 | [diff] [blame^] | 62 | bool is_dmacoherent; |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static const struct of_device_id ahci_qoriq_of_match[] = { |
| 66 | { .compatible = "fsl,ls1021a-ahci", .data = (void *)AHCI_LS1021A}, |
| 67 | { .compatible = "fsl,ls1043a-ahci", .data = (void *)AHCI_LS1043A}, |
Tang Yuantian | d19f9aa | 2015-10-29 14:22:15 +0800 | [diff] [blame] | 68 | { .compatible = "fsl,ls2080a-ahci", .data = (void *)AHCI_LS2080A}, |
Tang Yuantian | 2facc6d | 2016-10-09 16:51:04 +0800 | [diff] [blame] | 69 | { .compatible = "fsl,ls1046a-ahci", .data = (void *)AHCI_LS1046A}, |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 70 | {}, |
| 71 | }; |
| 72 | MODULE_DEVICE_TABLE(of, ahci_qoriq_of_match); |
| 73 | |
| 74 | static int ahci_qoriq_hardreset(struct ata_link *link, unsigned int *class, |
| 75 | unsigned long deadline) |
| 76 | { |
| 77 | const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context); |
| 78 | void __iomem *port_mmio = ahci_port_base(link->ap); |
| 79 | u32 px_cmd, px_is, px_val; |
| 80 | struct ata_port *ap = link->ap; |
| 81 | struct ahci_port_priv *pp = ap->private_data; |
| 82 | struct ahci_host_priv *hpriv = ap->host->private_data; |
| 83 | struct ahci_qoriq_priv *qoriq_priv = hpriv->plat_data; |
| 84 | u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG; |
| 85 | struct ata_taskfile tf; |
| 86 | bool online; |
| 87 | int rc; |
Arnd Bergmann | eb35103 | 2015-10-14 16:46:52 +0800 | [diff] [blame] | 88 | bool ls1021a_workaround = (qoriq_priv->type == AHCI_LS1021A); |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 89 | |
| 90 | DPRINTK("ENTER\n"); |
| 91 | |
| 92 | ahci_stop_engine(ap); |
| 93 | |
| 94 | /* |
| 95 | * There is a errata on ls1021a Rev1.0 and Rev2.0 which is: |
| 96 | * A-009042: The device detection initialization sequence |
| 97 | * mistakenly resets some registers. |
| 98 | * |
| 99 | * Workaround for this is: |
| 100 | * The software should read and store PxCMD and PxIS values |
| 101 | * before issuing the device detection initialization sequence. |
| 102 | * After the sequence is complete, software should restore the |
| 103 | * PxCMD and PxIS with the stored values. |
| 104 | */ |
Arnd Bergmann | eb35103 | 2015-10-14 16:46:52 +0800 | [diff] [blame] | 105 | if (ls1021a_workaround) { |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 106 | px_cmd = readl(port_mmio + PORT_CMD); |
| 107 | px_is = readl(port_mmio + PORT_IRQ_STAT); |
| 108 | } |
| 109 | |
| 110 | /* clear D2H reception area to properly wait for D2H FIS */ |
| 111 | ata_tf_init(link->device, &tf); |
| 112 | tf.command = ATA_BUSY; |
| 113 | ata_tf_to_fis(&tf, 0, 0, d2h_fis); |
| 114 | |
| 115 | rc = sata_link_hardreset(link, timing, deadline, &online, |
| 116 | ahci_check_ready); |
| 117 | |
| 118 | /* restore the PxCMD and PxIS on ls1021 */ |
Arnd Bergmann | eb35103 | 2015-10-14 16:46:52 +0800 | [diff] [blame] | 119 | if (ls1021a_workaround) { |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 120 | px_val = readl(port_mmio + PORT_CMD); |
| 121 | if (px_val != px_cmd) |
| 122 | writel(px_cmd, port_mmio + PORT_CMD); |
| 123 | |
| 124 | px_val = readl(port_mmio + PORT_IRQ_STAT); |
| 125 | if (px_val != px_is) |
| 126 | writel(px_is, port_mmio + PORT_IRQ_STAT); |
| 127 | } |
| 128 | |
| 129 | hpriv->start_engine(ap); |
| 130 | |
| 131 | if (online) |
| 132 | *class = ahci_dev_classify(ap); |
| 133 | |
| 134 | DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class); |
| 135 | return rc; |
| 136 | } |
| 137 | |
| 138 | static struct ata_port_operations ahci_qoriq_ops = { |
| 139 | .inherits = &ahci_ops, |
| 140 | .hardreset = ahci_qoriq_hardreset, |
| 141 | }; |
| 142 | |
Tang Yuantian | 1ce788d | 2016-09-30 14:13:13 +0800 | [diff] [blame] | 143 | static const struct ata_port_info ahci_qoriq_port_info = { |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 144 | .flags = AHCI_FLAG_COMMON | ATA_FLAG_NCQ, |
| 145 | .pio_mask = ATA_PIO4, |
| 146 | .udma_mask = ATA_UDMA6, |
| 147 | .port_ops = &ahci_qoriq_ops, |
| 148 | }; |
| 149 | |
| 150 | static struct scsi_host_template ahci_qoriq_sht = { |
| 151 | AHCI_SHT(DRV_NAME), |
| 152 | }; |
| 153 | |
| 154 | static int ahci_qoriq_phy_init(struct ahci_host_priv *hpriv) |
| 155 | { |
| 156 | struct ahci_qoriq_priv *qpriv = hpriv->plat_data; |
| 157 | void __iomem *reg_base = hpriv->mmio; |
| 158 | |
| 159 | switch (qpriv->type) { |
| 160 | case AHCI_LS1021A: |
| 161 | writel(SATA_ECC_DISABLE, qpriv->ecc_addr); |
| 162 | writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1); |
Tang Yuantian | dfcdc5f | 2015-12-16 14:00:35 +0800 | [diff] [blame] | 163 | writel(LS1021A_PORT_PHY2, reg_base + PORT_PHY2); |
| 164 | writel(LS1021A_PORT_PHY3, reg_base + PORT_PHY3); |
| 165 | writel(LS1021A_PORT_PHY4, reg_base + PORT_PHY4); |
| 166 | writel(LS1021A_PORT_PHY5, reg_base + PORT_PHY5); |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 167 | writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS); |
Tang Yuantian | 386dc3b | 2017-01-20 14:59:34 +0800 | [diff] [blame^] | 168 | if (qpriv->is_dmacoherent) |
| 169 | writel(AHCI_PORT_AXICC_CFG, |
| 170 | reg_base + LS1021A_AXICC_ADDR); |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 171 | break; |
| 172 | |
| 173 | case AHCI_LS1043A: |
Tang Yuantian | ef0cc7f | 2015-12-16 13:43:49 +0800 | [diff] [blame] | 174 | writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1); |
Tang Yuantian | ef0cc7f | 2015-12-16 13:43:49 +0800 | [diff] [blame] | 175 | writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS); |
Tang Yuantian | 386dc3b | 2017-01-20 14:59:34 +0800 | [diff] [blame^] | 176 | if (qpriv->is_dmacoherent) |
| 177 | writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC); |
Tang Yuantian | ef0cc7f | 2015-12-16 13:43:49 +0800 | [diff] [blame] | 178 | break; |
| 179 | |
Tang Yuantian | d19f9aa | 2015-10-29 14:22:15 +0800 | [diff] [blame] | 180 | case AHCI_LS2080A: |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 181 | writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1); |
Tang Yuantian | e3a6dad | 2015-12-16 13:43:50 +0800 | [diff] [blame] | 182 | writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS); |
Tang Yuantian | 386dc3b | 2017-01-20 14:59:34 +0800 | [diff] [blame^] | 183 | if (qpriv->is_dmacoherent) |
| 184 | writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC); |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 185 | break; |
Tang Yuantian | 2facc6d | 2016-10-09 16:51:04 +0800 | [diff] [blame] | 186 | |
| 187 | case AHCI_LS1046A: |
| 188 | writel(LS1046A_SATA_ECC_DIS, qpriv->ecc_addr); |
| 189 | writel(AHCI_PORT_PHY_1_CFG, reg_base + PORT_PHY1); |
| 190 | writel(AHCI_PORT_TRANS_CFG, reg_base + PORT_TRANS); |
Tang Yuantian | 386dc3b | 2017-01-20 14:59:34 +0800 | [diff] [blame^] | 191 | if (qpriv->is_dmacoherent) |
| 192 | writel(AHCI_PORT_AXICC_CFG, reg_base + PORT_AXICC); |
Tang Yuantian | 2facc6d | 2016-10-09 16:51:04 +0800 | [diff] [blame] | 193 | break; |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int ahci_qoriq_probe(struct platform_device *pdev) |
| 200 | { |
| 201 | struct device_node *np = pdev->dev.of_node; |
| 202 | struct device *dev = &pdev->dev; |
| 203 | struct ahci_host_priv *hpriv; |
| 204 | struct ahci_qoriq_priv *qoriq_priv; |
| 205 | const struct of_device_id *of_id; |
| 206 | struct resource *res; |
| 207 | int rc; |
| 208 | |
| 209 | hpriv = ahci_platform_get_resources(pdev); |
| 210 | if (IS_ERR(hpriv)) |
| 211 | return PTR_ERR(hpriv); |
| 212 | |
| 213 | of_id = of_match_node(ahci_qoriq_of_match, np); |
| 214 | if (!of_id) |
| 215 | return -ENODEV; |
| 216 | |
| 217 | qoriq_priv = devm_kzalloc(dev, sizeof(*qoriq_priv), GFP_KERNEL); |
| 218 | if (!qoriq_priv) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | qoriq_priv->type = (enum ahci_qoriq_type)of_id->data; |
| 222 | |
Tang Yuantian | 2facc6d | 2016-10-09 16:51:04 +0800 | [diff] [blame] | 223 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 224 | "sata-ecc"); |
| 225 | if (res) { |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 226 | qoriq_priv->ecc_addr = devm_ioremap_resource(dev, res); |
| 227 | if (IS_ERR(qoriq_priv->ecc_addr)) |
| 228 | return PTR_ERR(qoriq_priv->ecc_addr); |
| 229 | } |
Tang Yuantian | 386dc3b | 2017-01-20 14:59:34 +0800 | [diff] [blame^] | 230 | qoriq_priv->is_dmacoherent = of_dma_is_coherent(np); |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 231 | |
| 232 | rc = ahci_platform_enable_resources(hpriv); |
| 233 | if (rc) |
| 234 | return rc; |
| 235 | |
| 236 | hpriv->plat_data = qoriq_priv; |
| 237 | rc = ahci_qoriq_phy_init(hpriv); |
| 238 | if (rc) |
| 239 | goto disable_resources; |
| 240 | |
Tang Yuantian | ecfb459 | 2015-09-07 16:23:16 +0800 | [diff] [blame] | 241 | rc = ahci_platform_init_host(pdev, hpriv, &ahci_qoriq_port_info, |
| 242 | &ahci_qoriq_sht); |
| 243 | if (rc) |
| 244 | goto disable_resources; |
| 245 | |
| 246 | return 0; |
| 247 | |
| 248 | disable_resources: |
| 249 | ahci_platform_disable_resources(hpriv); |
| 250 | |
| 251 | return rc; |
| 252 | } |
| 253 | |
| 254 | #ifdef CONFIG_PM_SLEEP |
| 255 | static int ahci_qoriq_resume(struct device *dev) |
| 256 | { |
| 257 | struct ata_host *host = dev_get_drvdata(dev); |
| 258 | struct ahci_host_priv *hpriv = host->private_data; |
| 259 | int rc; |
| 260 | |
| 261 | rc = ahci_platform_enable_resources(hpriv); |
| 262 | if (rc) |
| 263 | return rc; |
| 264 | |
| 265 | rc = ahci_qoriq_phy_init(hpriv); |
| 266 | if (rc) |
| 267 | goto disable_resources; |
| 268 | |
| 269 | rc = ahci_platform_resume_host(dev); |
| 270 | if (rc) |
| 271 | goto disable_resources; |
| 272 | |
| 273 | /* We resumed so update PM runtime state */ |
| 274 | pm_runtime_disable(dev); |
| 275 | pm_runtime_set_active(dev); |
| 276 | pm_runtime_enable(dev); |
| 277 | |
| 278 | return 0; |
| 279 | |
| 280 | disable_resources: |
| 281 | ahci_platform_disable_resources(hpriv); |
| 282 | |
| 283 | return rc; |
| 284 | } |
| 285 | #endif |
| 286 | |
| 287 | static SIMPLE_DEV_PM_OPS(ahci_qoriq_pm_ops, ahci_platform_suspend, |
| 288 | ahci_qoriq_resume); |
| 289 | |
| 290 | static struct platform_driver ahci_qoriq_driver = { |
| 291 | .probe = ahci_qoriq_probe, |
| 292 | .remove = ata_platform_remove_one, |
| 293 | .driver = { |
| 294 | .name = DRV_NAME, |
| 295 | .of_match_table = ahci_qoriq_of_match, |
| 296 | .pm = &ahci_qoriq_pm_ops, |
| 297 | }, |
| 298 | }; |
| 299 | module_platform_driver(ahci_qoriq_driver); |
| 300 | |
| 301 | MODULE_DESCRIPTION("Freescale QorIQ AHCI SATA platform driver"); |
| 302 | MODULE_AUTHOR("Tang Yuantian <Yuantian.Tang@freescale.com>"); |
| 303 | MODULE_LICENSE("GPL"); |