1 From 22345cc059de4a6ea1dc7657dd6ad86ca16a8814 Mon Sep 17 00:00:00 2001
2 From: Oliver Schinagl <oliver@schinagl.nl>
3 Date: Tue, 3 Dec 2013 12:07:01 +0100
4 Subject: [PATCH] ARM: sunxi: Add ahci-sunxi driver for the Allwinner SUNXi
7 This patch adds support for the ahci sata controler found on Allwinner A10
10 Orignally written by Olliver Schinagl using the approach of having a platform
11 device which probe method creates a new child platform device which gets
12 driven by ahci_platform.c, as done by ahci_imx.c .
14 Given that almost all functionality already is shared through libahci /
15 ata-core, and that ahci_platform.c cannot cleanly handle somewhat more complex
16 platform specific ahci cases, such as the sunxi case, it was refactored into
17 a stand-alone platform driver by Hans de Goede.
19 Signed-off-by: Olliver Schinagl <oliver@schinagl.nl>
20 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
22 .../devicetree/bindings/ata/ahci-sunxi.txt | 24 ++
23 drivers/ata/Kconfig | 9 +
24 drivers/ata/Makefile | 1 +
25 drivers/ata/ahci_sunxi.c | 349 +++++++++++++++++++++
26 4 files changed, 383 insertions(+)
27 create mode 100644 Documentation/devicetree/bindings/ata/ahci-sunxi.txt
28 create mode 100644 drivers/ata/ahci_sunxi.c
31 +++ b/Documentation/devicetree/bindings/ata/ahci-sunxi.txt
33 +Allwinner SUNXI AHCI SATA Controller
35 +SATA nodes are defined to describe on-chip Serial ATA controllers.
36 +Each SATA controller should have its own node.
39 +- compatible : compatible list, contains "allwinner,sun4i-a10-ahci"
40 +- reg : <registers mapping>
41 +- interrupts : <interrupt mapping for AHCI IRQ>
42 +- clocks : clocks for ACHI
43 +- clock-names : clock names for AHCI
46 +- pwr-supply : regulator to control the power supply GPIO
50 + compatible = "allwinner,sun4i-a10-ahci";
51 + reg = <0x01c18000 0x1000>;
52 + interrupts = <0 56 1>;
53 + clocks = <&ahb_gates 25>, <&pll6 0>;
54 + clock-names = "ahb_sata", "pll6_sata";
55 + pwr-supply = <®_ahci_5v>;
57 --- a/drivers/ata/Kconfig
58 +++ b/drivers/ata/Kconfig
59 @@ -106,6 +106,15 @@ config AHCI_IMX
64 + tristate "Allwinner sunxi AHCI SATA support"
65 + depends on ARCH_SUNXI
67 + This option enables support for the Allwinner sunxi SoC's
73 tristate "Freescale 3.0Gbps SATA support"
75 --- a/drivers/ata/Makefile
76 +++ b/drivers/ata/Makefile
77 @@ -11,6 +11,7 @@ obj-$(CONFIG_SATA_SIL24) += sata_sil24.o
78 obj-$(CONFIG_SATA_DWC) += sata_dwc_460ex.o
79 obj-$(CONFIG_SATA_HIGHBANK) += sata_highbank.o libahci.o
80 obj-$(CONFIG_AHCI_IMX) += ahci_imx.o
81 +obj-$(CONFIG_AHCI_SUNXI) += ahci_sunxi.o libahci.o
84 obj-$(CONFIG_PDC_ADMA) += pdc_adma.o
86 +++ b/drivers/ata/ahci_sunxi.c
89 + * Allwinner sunxi AHCI SATA platform driver
90 + * Copyright 2013 Olliver Schinagl <oliver@schinagl.nl>
91 + * Copyright 2014 Hans de Goede <hdegoede@redhat.com>
93 + * based on the AHCI SATA platform driver by Jeff Garzik and Anton Vorontsov
94 + * Based on code from Allwinner Technology Co., Ltd. <www.allwinnertech.com>,
95 + * Daniel Wang <danielwang@allwinnertech.com>
97 + * This program is free software; you can redistribute it and/or modify it
98 + * under the terms and conditions of the GNU General Public License,
99 + * version 2, as published by the Free Software Foundation.
101 + * This program is distributed in the hope it will be useful, but WITHOUT
102 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
103 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
107 +#include <linux/clk.h>
108 +#include <linux/errno.h>
109 +#include <linux/kernel.h>
110 +#include <linux/module.h>
111 +#include <linux/of_device.h>
112 +#include <linux/platform_device.h>
113 +#include <linux/regulator/consumer.h>
116 +#define AHCI_BISTAFR 0x00a0
117 +#define AHCI_BISTCR 0x00a4
118 +#define AHCI_BISTFCTR 0x00a8
119 +#define AHCI_BISTSR 0x00ac
120 +#define AHCI_BISTDECR 0x00b0
121 +#define AHCI_DIAGNR0 0x00b4
122 +#define AHCI_DIAGNR1 0x00b8
123 +#define AHCI_OOBR 0x00bc
124 +#define AHCI_PHYCS0R 0x00c0
125 +#define AHCI_PHYCS1R 0x00c4
126 +#define AHCI_PHYCS2R 0x00c8
127 +#define AHCI_TIMER1MS 0x00e0
128 +#define AHCI_GPARAM1R 0x00e8
129 +#define AHCI_GPARAM2R 0x00ec
130 +#define AHCI_PPARAMR 0x00f0
131 +#define AHCI_TESTR 0x00f4
132 +#define AHCI_VERSIONR 0x00f8
133 +#define AHCI_IDR 0x00fc
134 +#define AHCI_RWCR 0x00fc
135 +#define AHCI_P0DMACR 0x0170
136 +#define AHCI_P0PHYCR 0x0178
137 +#define AHCI_P0PHYSR 0x017c
140 + struct ahci_host_priv hpriv;
141 + struct regulator *pwr;
142 + struct clk *sata_clk;
143 + struct clk *ahb_clk;
146 +static void sunxi_clrbits(void __iomem *reg, u32 clr_val)
150 + reg_val = readl(reg);
151 + reg_val &= ~(clr_val);
152 + writel(reg_val, reg);
155 +static void sunxi_setbits(void __iomem *reg, u32 set_val)
159 + reg_val = readl(reg);
160 + reg_val |= set_val;
161 + writel(reg_val, reg);
164 +static void sunxi_clrsetbits(void __iomem *reg, u32 clr_val, u32 set_val)
168 + reg_val = readl(reg);
169 + reg_val &= ~(clr_val);
170 + reg_val |= set_val;
171 + writel(reg_val, reg);
174 +static u32 sunxi_getbits(void __iomem *reg, u8 mask, u8 shift)
176 + return (readl(reg) >> shift) & mask;
179 +static int sunxi_ahci_phy_init(struct device *dev, void __iomem *reg_base)
184 + /* This magic is from the original code */
185 + writel(0, reg_base + AHCI_RWCR);
188 + sunxi_setbits(reg_base + AHCI_PHYCS1R, BIT(19));
189 + sunxi_clrsetbits(reg_base + AHCI_PHYCS0R,
191 + (0x5 << 24) | BIT(23) | BIT(18));
192 + sunxi_clrsetbits(reg_base + AHCI_PHYCS1R,
193 + (0x3 << 16) | (0x1f << 8) | (0x3 << 6),
194 + (0x2 << 16) | (0x6 << 8) | (0x2 << 6));
195 + sunxi_setbits(reg_base + AHCI_PHYCS1R, BIT(28) | BIT(15));
196 + sunxi_clrbits(reg_base + AHCI_PHYCS1R, BIT(19));
197 + sunxi_clrsetbits(reg_base + AHCI_PHYCS0R,
198 + (0x7 << 20), (0x3 << 20));
199 + sunxi_clrsetbits(reg_base + AHCI_PHYCS2R,
200 + (0x1f << 5), (0x19 << 5));
203 + sunxi_setbits(reg_base + AHCI_PHYCS0R, (0x1 << 19));
205 + timeout = 0x100000;
207 + reg_val = sunxi_getbits(reg_base + AHCI_PHYCS0R, 0x7, 28);
208 + } while (--timeout && (reg_val != 0x2));
210 + dev_err(dev, "PHY power up failed.\n");
214 + sunxi_setbits(reg_base + AHCI_PHYCS2R, (0x1 << 24));
216 + timeout = 0x100000;
218 + reg_val = sunxi_getbits(reg_base + AHCI_PHYCS2R, 0x1, 24);
219 + } while (--timeout && reg_val);
221 + dev_err(dev, "PHY calibration failed.\n");
226 + writel(0x7, reg_base + AHCI_RWCR);
231 +void sunxi_ahci_pre_start_engine(struct ata_port *ap)
233 + struct ahci_host_priv *hpriv = ap->host->private_data;
235 + /* Setup DMA before DMA start */
236 + sunxi_clrsetbits(hpriv->mmio + AHCI_P0DMACR, 0x0000ff00, 0x00004400);
239 +static int sunxi_ahci_enable_clks(struct sunxi_ahci *ahci)
243 + ret = clk_prepare_enable(ahci->sata_clk);
247 + ret = clk_prepare_enable(ahci->ahb_clk);
249 + clk_disable_unprepare(ahci->sata_clk);
254 +static void sunxi_ahci_disable_clks(struct sunxi_ahci *ahci)
256 + clk_disable_unprepare(ahci->ahb_clk);
257 + clk_disable_unprepare(ahci->sata_clk);
260 +static void sunxi_ahci_host_stop(struct ata_host *host)
262 + struct ahci_host_priv *hpriv = host->private_data;
263 + struct sunxi_ahci *ahci = hpriv->plat_data;
265 + if (!IS_ERR(ahci->pwr))
266 + regulator_disable(ahci->pwr);
268 + sunxi_ahci_disable_clks(ahci);
271 +static struct ata_port_operations sunxi_ahci_platform_ops = {
272 + .inherits = &ahci_ops,
273 + .host_stop = sunxi_ahci_host_stop,
276 +static const struct ata_port_info sunxiahci_port_info = {
277 + AHCI_HFLAGS(AHCI_HFLAG_32BIT_ONLY | AHCI_HFLAG_NO_MSI |
278 + AHCI_HFLAG_NO_PMP | AHCI_HFLAG_YES_NCQ),
279 + .flags = AHCI_FLAG_COMMON | ATA_FLAG_NCQ,
280 + .pio_mask = ATA_PIO4,
281 + .udma_mask = ATA_UDMA6,
282 + .port_ops = &sunxi_ahci_platform_ops,
285 +static struct scsi_host_template sunxi_ahci_platform_sht = {
286 + AHCI_SHT("sunxi_ahci"),
289 +static int sunxi_ahci_probe(struct platform_device *pdev)
291 + struct device *dev = &pdev->dev;
292 + const struct ata_port_info *ppi[] = { &sunxiahci_port_info, NULL };
293 + struct sunxi_ahci *ahci;
294 + struct ata_host *host;
297 + ahci = devm_kzalloc(&pdev->dev, sizeof(*ahci), GFP_KERNEL);
301 + ahci->pwr = devm_regulator_get_optional(dev, "pwr");
302 + if (IS_ERR(ahci->pwr) && PTR_ERR(ahci->pwr) == -EPROBE_DEFER)
303 + return -EPROBE_DEFER;
305 + host = ata_host_alloc_pinfo(dev, ppi, 1);
309 + host->private_data = &ahci->hpriv;
310 + host->flags |= ATA_HOST_PARALLEL_SCAN;
312 + ahci->hpriv.flags = (unsigned long)ppi[0]->private_data;
313 + ahci->hpriv.plat_data = ahci;
314 + ahci->hpriv.pre_start_engine = sunxi_ahci_pre_start_engine;
315 + ahci->hpriv.mmio = devm_ioremap_resource(dev,
316 + platform_get_resource(pdev, IORESOURCE_MEM, 0));
317 + if (IS_ERR(ahci->hpriv.mmio))
318 + return PTR_ERR(ahci->hpriv.mmio);
320 + ahci->ahb_clk = devm_clk_get(&pdev->dev, "ahb_sata");
321 + if (IS_ERR(ahci->ahb_clk))
322 + return PTR_ERR(ahci->ahb_clk);
324 + ahci->sata_clk = devm_clk_get(&pdev->dev, "pll6_sata");
325 + if (IS_ERR(ahci->sata_clk))
326 + return PTR_ERR(ahci->sata_clk);
328 + ret = sunxi_ahci_enable_clks(ahci);
332 + if (!IS_ERR(ahci->pwr)) {
333 + ret = regulator_enable(ahci->pwr);
335 + sunxi_ahci_disable_clks(ahci);
340 + ret = sunxi_ahci_phy_init(dev, ahci->hpriv.mmio);
342 + sunxi_ahci_host_stop(host);
346 + ahci_save_initial_config(dev, &ahci->hpriv, 0, 0);
348 + ret = ahci_reset_controller(host);
350 + sunxi_ahci_host_stop(host);
354 + ahci_init_controller(host);
355 + ahci_print_info(host, "sunxi");
357 + ret = ata_host_activate(host, platform_get_irq(pdev, 0),
358 + ahci_interrupt, 0, &sunxi_ahci_platform_sht);
360 + sunxi_ahci_host_stop(host);
365 +#ifdef CONFIG_PM_SLEEP
366 +static int sunxi_ahci_susp(struct device *dev)
368 + struct ata_host *host = dev_get_drvdata(dev);
369 + struct ahci_host_priv *hpriv = host->private_data;
370 + struct sunxi_ahci *ahci = hpriv->plat_data;
374 + * AHCI spec rev1.1 section 8.3.3:
375 + * Software must disable interrupts prior to requesting a
376 + * transition of the HBA to D3 state.
378 + sunxi_clrbits(hpriv->mmio + HOST_CTL, HOST_IRQ_EN);
380 + ret = ata_host_suspend(host, PMSG_SUSPEND);
384 + sunxi_ahci_disable_clks(ahci);
389 +static int sunxi_ahci_resume(struct device *dev)
391 + struct ata_host *host = dev_get_drvdata(dev);
392 + struct ahci_host_priv *hpriv = host->private_data;
393 + struct sunxi_ahci *ahci = hpriv->plat_data;
396 + ret = sunxi_ahci_enable_clks(ahci);
400 + if (dev->power.power_state.event == PM_EVENT_SUSPEND) {
401 + ret = ahci_reset_controller(host);
405 + ahci_init_controller(host);
408 + ata_host_resume(host);
414 +static SIMPLE_DEV_PM_OPS(sunxi_ahci_pmo, sunxi_ahci_susp, sunxi_ahci_resume);
416 +static const struct of_device_id sunxi_ahci_of_match[] = {
417 + { .compatible = "allwinner,sun4i-a10-ahci" },
418 + { /* sentinel */ },
420 +MODULE_DEVICE_TABLE(of, sunxi_ahci_of_match);
422 +static struct platform_driver sunxi_ahci_driver = {
423 + .probe = sunxi_ahci_probe,
424 + .remove = ata_platform_remove_one,
426 + .name = "sunxi-ahci",
427 + .owner = THIS_MODULE,
428 + .of_match_table = sunxi_ahci_of_match,
429 + .pm = &sunxi_ahci_pmo,
432 +module_platform_driver(sunxi_ahci_driver);
434 +MODULE_DESCRIPTION("Allwinner sunxi AHCI SATA platform driver");
435 +MODULE_AUTHOR("Olliver Schinagl <oliver@schinagl.nl>");
436 +MODULE_LICENSE("GPL");