1 // SPDX-License-Identifier: GPL-2.0-only
3 * Cortina Systems Gemini SATA bridge add-on to Faraday FTIDE010
4 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/bitops.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/regmap.h>
13 #include <linux/delay.h>
14 #include <linux/reset.h>
15 #include <linux/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/clk.h>
19 #include <linux/pinctrl/consumer.h>
20 #include "sata_gemini.h"
22 #define DRV_NAME "gemini_sata_bridge"
25 * struct sata_gemini - a state container for a Gemini SATA bridge
26 * @dev: the containing device
27 * @base: remapped I/O memory base
28 * @muxmode: the current muxing mode
29 * @ide_pins: if the device is using the plain IDE interface pins
30 * @sata_bridge: if the device enables the SATA bridge
31 * @sata0_reset: SATA0 reset handler
32 * @sata1_reset: SATA1 reset handler
33 * @sata0_pclk: SATA0 PCLK handler
34 * @sata1_pclk: SATA1 PCLK handler
39 enum gemini_muxmode muxmode;
42 struct reset_control *sata0_reset;
43 struct reset_control *sata1_reset;
44 struct clk *sata0_pclk;
45 struct clk *sata1_pclk;
48 /* Miscellaneous Control Register */
49 #define GEMINI_GLOBAL_MISC_CTRL 0x30
51 * Values of IDE IOMUX bits in the misc control register
53 * Bits 26:24 are "IDE IO Select", which decides what SATA
54 * adapters are connected to which of the two IDE/ATA
55 * controllers in the Gemini. We can connect the two IDE blocks
56 * to one SATA adapter each, both acting as master, or one IDE
57 * blocks to two SATA adapters so the IDE block can act in a
58 * master/slave configuration.
60 * We also bring out different blocks on the actual IDE
61 * pins (not SATA pins) if (and only if) these are muxed in.
64 * Mode 0: 000 - ata0 master <-> sata0
65 * ata1 master <-> sata1
66 * ata0 slave interface brought out on IDE pads
67 * Mode 1: 001 - ata0 master <-> sata0
68 * ata1 master <-> sata1
69 * ata1 slave interface brought out on IDE pads
70 * Mode 2: 010 - ata1 master <-> sata1
71 * ata1 slave <-> sata0
72 * ata0 master and slave interfaces brought out
74 * Mode 3: 011 - ata0 master <-> sata0
75 * ata1 slave <-> sata1
76 * ata1 master and slave interfaces brought out
79 #define GEMINI_IDE_IOMUX_MASK (7 << 24)
80 #define GEMINI_IDE_IOMUX_MODE0 (0 << 24)
81 #define GEMINI_IDE_IOMUX_MODE1 (1 << 24)
82 #define GEMINI_IDE_IOMUX_MODE2 (2 << 24)
83 #define GEMINI_IDE_IOMUX_MODE3 (3 << 24)
84 #define GEMINI_IDE_IOMUX_SHIFT (24)
87 * Registers directly controlling the PATA<->SATA adapters
89 #define GEMINI_SATA_ID 0x00
90 #define GEMINI_SATA_PHY_ID 0x04
91 #define GEMINI_SATA0_STATUS 0x08
92 #define GEMINI_SATA1_STATUS 0x0c
93 #define GEMINI_SATA0_CTRL 0x18
94 #define GEMINI_SATA1_CTRL 0x1c
96 #define GEMINI_SATA_STATUS_BIST_DONE BIT(5)
97 #define GEMINI_SATA_STATUS_BIST_OK BIT(4)
98 #define GEMINI_SATA_STATUS_PHY_READY BIT(0)
100 #define GEMINI_SATA_CTRL_PHY_BIST_EN BIT(14)
101 #define GEMINI_SATA_CTRL_PHY_FORCE_IDLE BIT(13)
102 #define GEMINI_SATA_CTRL_PHY_FORCE_READY BIT(12)
103 #define GEMINI_SATA_CTRL_PHY_AFE_LOOP_EN BIT(10)
104 #define GEMINI_SATA_CTRL_PHY_DIG_LOOP_EN BIT(9)
105 #define GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN BIT(4)
106 #define GEMINI_SATA_CTRL_ATAPI_EN BIT(3)
107 #define GEMINI_SATA_CTRL_BUS_WITH_20 BIT(2)
108 #define GEMINI_SATA_CTRL_SLAVE_EN BIT(1)
109 #define GEMINI_SATA_CTRL_EN BIT(0)
112 * There is only ever one instance of this bridge on a system,
113 * so create a singleton so that the FTIDE010 instances can grab
116 static struct sata_gemini *sg_singleton;
118 struct sata_gemini *gemini_sata_bridge_get(void)
122 return ERR_PTR(-EPROBE_DEFER);
124 EXPORT_SYMBOL(gemini_sata_bridge_get);
126 bool gemini_sata_bridge_enabled(struct sata_gemini *sg, bool is_ata1)
128 if (!sg->sata_bridge)
131 * In muxmode 2 and 3 one of the ATA controllers is
132 * actually not connected to any SATA bridge.
134 if ((sg->muxmode == GEMINI_MUXMODE_2) &&
137 if ((sg->muxmode == GEMINI_MUXMODE_3) &&
143 EXPORT_SYMBOL(gemini_sata_bridge_enabled);
145 enum gemini_muxmode gemini_sata_get_muxmode(struct sata_gemini *sg)
149 EXPORT_SYMBOL(gemini_sata_get_muxmode);
151 static int gemini_sata_setup_bridge(struct sata_gemini *sg,
154 unsigned long timeout = jiffies + (HZ * 1);
159 val = GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN | GEMINI_SATA_CTRL_EN;
160 /* SATA0 slave mode is only used in muxmode 2 */
161 if (sg->muxmode == GEMINI_MUXMODE_2)
162 val |= GEMINI_SATA_CTRL_SLAVE_EN;
163 writel(val, sg->base + GEMINI_SATA0_CTRL);
165 val = GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN | GEMINI_SATA_CTRL_EN;
166 /* SATA1 slave mode is only used in muxmode 3 */
167 if (sg->muxmode == GEMINI_MUXMODE_3)
168 val |= GEMINI_SATA_CTRL_SLAVE_EN;
169 writel(val, sg->base + GEMINI_SATA1_CTRL);
172 /* Vendor code waits 10 ms here */
175 /* Wait for PHY to become ready */
180 val = readl(sg->base + GEMINI_SATA0_STATUS);
182 val = readl(sg->base + GEMINI_SATA1_STATUS);
183 if (val & GEMINI_SATA_STATUS_PHY_READY)
185 } while (time_before(jiffies, timeout));
187 bridge_online = !!(val & GEMINI_SATA_STATUS_PHY_READY);
189 dev_info(sg->dev, "SATA%d PHY %s\n", bridge,
190 bridge_online ? "ready" : "not ready");
192 return bridge_online ? 0: -ENODEV;
195 int gemini_sata_start_bridge(struct sata_gemini *sg, unsigned int bridge)
201 pclk = sg->sata0_pclk;
203 pclk = sg->sata1_pclk;
204 ret = clk_enable(pclk);
210 /* Do not keep clocking a bridge that is not online */
211 ret = gemini_sata_setup_bridge(sg, bridge);
217 EXPORT_SYMBOL(gemini_sata_start_bridge);
219 void gemini_sata_stop_bridge(struct sata_gemini *sg, unsigned int bridge)
222 clk_disable(sg->sata0_pclk);
223 else if (bridge == 1)
224 clk_disable(sg->sata1_pclk);
226 EXPORT_SYMBOL(gemini_sata_stop_bridge);
228 int gemini_sata_reset_bridge(struct sata_gemini *sg,
232 reset_control_reset(sg->sata0_reset);
234 reset_control_reset(sg->sata1_reset);
236 return gemini_sata_setup_bridge(sg, bridge);
238 EXPORT_SYMBOL(gemini_sata_reset_bridge);
240 static int gemini_sata_bridge_init(struct sata_gemini *sg)
242 struct device *dev = sg->dev;
243 u32 sata_id, sata_phy_id;
246 sg->sata0_pclk = devm_clk_get(dev, "SATA0_PCLK");
247 if (IS_ERR(sg->sata0_pclk)) {
248 dev_err(dev, "no SATA0 PCLK");
251 sg->sata1_pclk = devm_clk_get(dev, "SATA1_PCLK");
252 if (IS_ERR(sg->sata1_pclk)) {
253 dev_err(dev, "no SATA1 PCLK");
257 ret = clk_prepare_enable(sg->sata0_pclk);
259 pr_err("failed to enable SATA0 PCLK\n");
262 ret = clk_prepare_enable(sg->sata1_pclk);
264 pr_err("failed to enable SATA1 PCLK\n");
265 clk_disable_unprepare(sg->sata0_pclk);
269 sg->sata0_reset = devm_reset_control_get_exclusive(dev, "sata0");
270 if (IS_ERR(sg->sata0_reset)) {
271 dev_err(dev, "no SATA0 reset controller\n");
272 clk_disable_unprepare(sg->sata1_pclk);
273 clk_disable_unprepare(sg->sata0_pclk);
274 return PTR_ERR(sg->sata0_reset);
276 sg->sata1_reset = devm_reset_control_get_exclusive(dev, "sata1");
277 if (IS_ERR(sg->sata1_reset)) {
278 dev_err(dev, "no SATA1 reset controller\n");
279 clk_disable_unprepare(sg->sata1_pclk);
280 clk_disable_unprepare(sg->sata0_pclk);
281 return PTR_ERR(sg->sata1_reset);
284 sata_id = readl(sg->base + GEMINI_SATA_ID);
285 sata_phy_id = readl(sg->base + GEMINI_SATA_PHY_ID);
286 sg->sata_bridge = true;
287 clk_disable(sg->sata0_pclk);
288 clk_disable(sg->sata1_pclk);
290 dev_info(dev, "SATA ID %08x, PHY ID: %08x\n", sata_id, sata_phy_id);
295 static int gemini_setup_ide_pins(struct device *dev)
298 struct pinctrl_state *ide_state;
301 p = devm_pinctrl_get(dev);
305 ide_state = pinctrl_lookup_state(p, "ide");
306 if (IS_ERR(ide_state))
307 return PTR_ERR(ide_state);
309 ret = pinctrl_select_state(p, ide_state);
311 dev_err(dev, "could not select IDE state\n");
318 static int gemini_sata_probe(struct platform_device *pdev)
320 struct device *dev = &pdev->dev;
321 struct device_node *np = dev->of_node;
322 struct sata_gemini *sg;
324 struct resource *res;
325 enum gemini_muxmode muxmode;
330 sg = devm_kzalloc(dev, sizeof(*sg), GFP_KERNEL);
335 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
339 sg->base = devm_ioremap_resource(dev, res);
340 if (IS_ERR(sg->base))
341 return PTR_ERR(sg->base);
343 map = syscon_regmap_lookup_by_phandle(np, "syscon");
345 dev_err(dev, "no global syscon\n");
349 /* Set up the SATA bridge if need be */
350 if (of_property_read_bool(np, "cortina,gemini-enable-sata-bridge")) {
351 ret = gemini_sata_bridge_init(sg);
356 if (of_property_read_bool(np, "cortina,gemini-enable-ide-pins"))
359 if (!sg->sata_bridge && !sg->ide_pins) {
360 dev_err(dev, "neither SATA bridge or IDE output enabled\n");
365 ret = of_property_read_u32(np, "cortina,gemini-ata-muxmode", &muxmode);
367 dev_err(dev, "could not parse ATA muxmode\n");
370 if (muxmode > GEMINI_MUXMODE_3) {
371 dev_err(dev, "illegal muxmode %d\n", muxmode);
375 sg->muxmode = muxmode;
376 gmask = GEMINI_IDE_IOMUX_MASK;
377 gmode = (muxmode << GEMINI_IDE_IOMUX_SHIFT);
379 ret = regmap_update_bits(map, GEMINI_GLOBAL_MISC_CTRL, gmask, gmode);
381 dev_err(dev, "unable to set up IDE muxing\n");
387 * Route out the IDE pins if desired.
388 * This is done by looking up a special pin control state called
389 * "ide" that will route out the IDE pins.
392 ret = gemini_setup_ide_pins(dev);
397 dev_info(dev, "set up the Gemini IDE/SATA nexus\n");
398 platform_set_drvdata(pdev, sg);
404 if (sg->sata_bridge) {
405 clk_unprepare(sg->sata1_pclk);
406 clk_unprepare(sg->sata0_pclk);
411 static int gemini_sata_remove(struct platform_device *pdev)
413 struct sata_gemini *sg = platform_get_drvdata(pdev);
415 if (sg->sata_bridge) {
416 clk_unprepare(sg->sata1_pclk);
417 clk_unprepare(sg->sata0_pclk);
424 static const struct of_device_id gemini_sata_of_match[] = {
426 .compatible = "cortina,gemini-sata-bridge",
431 static struct platform_driver gemini_sata_driver = {
434 .of_match_table = of_match_ptr(gemini_sata_of_match),
436 .probe = gemini_sata_probe,
437 .remove = gemini_sata_remove,
439 module_platform_driver(gemini_sata_driver);
441 MODULE_DESCRIPTION("low level driver for Cortina Systems Gemini SATA bridge");
442 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
443 MODULE_LICENSE("GPL");
444 MODULE_ALIAS("platform:" DRV_NAME);