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
;
207 /* Do not keep clocking a bridge that is not online */
208 ret
= gemini_sata_setup_bridge(sg
, bridge
);
214 EXPORT_SYMBOL(gemini_sata_start_bridge
);
216 void gemini_sata_stop_bridge(struct sata_gemini
*sg
, unsigned int bridge
)
219 clk_disable(sg
->sata0_pclk
);
220 else if (bridge
== 1)
221 clk_disable(sg
->sata1_pclk
);
223 EXPORT_SYMBOL(gemini_sata_stop_bridge
);
225 int gemini_sata_reset_bridge(struct sata_gemini
*sg
,
229 reset_control_reset(sg
->sata0_reset
);
231 reset_control_reset(sg
->sata1_reset
);
233 return gemini_sata_setup_bridge(sg
, bridge
);
235 EXPORT_SYMBOL(gemini_sata_reset_bridge
);
237 static int gemini_sata_bridge_init(struct sata_gemini
*sg
)
239 struct device
*dev
= sg
->dev
;
240 u32 sata_id
, sata_phy_id
;
243 sg
->sata0_pclk
= devm_clk_get(dev
, "SATA0_PCLK");
244 if (IS_ERR(sg
->sata0_pclk
)) {
245 dev_err(dev
, "no SATA0 PCLK");
248 sg
->sata1_pclk
= devm_clk_get(dev
, "SATA1_PCLK");
249 if (IS_ERR(sg
->sata1_pclk
)) {
250 dev_err(dev
, "no SATA1 PCLK");
254 ret
= clk_prepare_enable(sg
->sata0_pclk
);
256 pr_err("failed to enable SATA0 PCLK\n");
259 ret
= clk_prepare_enable(sg
->sata1_pclk
);
261 pr_err("failed to enable SATA1 PCLK\n");
262 clk_disable_unprepare(sg
->sata0_pclk
);
266 sg
->sata0_reset
= devm_reset_control_get_exclusive(dev
, "sata0");
267 if (IS_ERR(sg
->sata0_reset
)) {
268 dev_err(dev
, "no SATA0 reset controller\n");
269 clk_disable_unprepare(sg
->sata1_pclk
);
270 clk_disable_unprepare(sg
->sata0_pclk
);
271 return PTR_ERR(sg
->sata0_reset
);
273 sg
->sata1_reset
= devm_reset_control_get_exclusive(dev
, "sata1");
274 if (IS_ERR(sg
->sata1_reset
)) {
275 dev_err(dev
, "no SATA1 reset controller\n");
276 clk_disable_unprepare(sg
->sata1_pclk
);
277 clk_disable_unprepare(sg
->sata0_pclk
);
278 return PTR_ERR(sg
->sata1_reset
);
281 sata_id
= readl(sg
->base
+ GEMINI_SATA_ID
);
282 sata_phy_id
= readl(sg
->base
+ GEMINI_SATA_PHY_ID
);
283 sg
->sata_bridge
= true;
284 clk_disable(sg
->sata0_pclk
);
285 clk_disable(sg
->sata1_pclk
);
287 dev_info(dev
, "SATA ID %08x, PHY ID: %08x\n", sata_id
, sata_phy_id
);
292 static int gemini_setup_ide_pins(struct device
*dev
)
295 struct pinctrl_state
*ide_state
;
298 p
= devm_pinctrl_get(dev
);
302 ide_state
= pinctrl_lookup_state(p
, "ide");
303 if (IS_ERR(ide_state
))
304 return PTR_ERR(ide_state
);
306 ret
= pinctrl_select_state(p
, ide_state
);
308 dev_err(dev
, "could not select IDE state\n");
315 static int gemini_sata_probe(struct platform_device
*pdev
)
317 struct device
*dev
= &pdev
->dev
;
318 struct device_node
*np
= dev
->of_node
;
319 struct sata_gemini
*sg
;
321 struct resource
*res
;
322 enum gemini_muxmode muxmode
;
327 sg
= devm_kzalloc(dev
, sizeof(*sg
), GFP_KERNEL
);
332 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
336 sg
->base
= devm_ioremap_resource(dev
, res
);
337 if (IS_ERR(sg
->base
))
338 return PTR_ERR(sg
->base
);
340 map
= syscon_regmap_lookup_by_phandle(np
, "syscon");
342 dev_err(dev
, "no global syscon\n");
346 /* Set up the SATA bridge if need be */
347 if (of_property_read_bool(np
, "cortina,gemini-enable-sata-bridge")) {
348 ret
= gemini_sata_bridge_init(sg
);
353 if (of_property_read_bool(np
, "cortina,gemini-enable-ide-pins"))
356 if (!sg
->sata_bridge
&& !sg
->ide_pins
) {
357 dev_err(dev
, "neither SATA bridge or IDE output enabled\n");
362 ret
= of_property_read_u32(np
, "cortina,gemini-ata-muxmode", &muxmode
);
364 dev_err(dev
, "could not parse ATA muxmode\n");
367 if (muxmode
> GEMINI_MUXMODE_3
) {
368 dev_err(dev
, "illegal muxmode %d\n", muxmode
);
372 sg
->muxmode
= muxmode
;
373 gmask
= GEMINI_IDE_IOMUX_MASK
;
374 gmode
= (muxmode
<< GEMINI_IDE_IOMUX_SHIFT
);
376 ret
= regmap_update_bits(map
, GEMINI_GLOBAL_MISC_CTRL
, gmask
, gmode
);
378 dev_err(dev
, "unable to set up IDE muxing\n");
384 * Route out the IDE pins if desired.
385 * This is done by looking up a special pin control state called
386 * "ide" that will route out the IDE pins.
389 ret
= gemini_setup_ide_pins(dev
);
394 dev_info(dev
, "set up the Gemini IDE/SATA nexus\n");
395 platform_set_drvdata(pdev
, sg
);
401 if (sg
->sata_bridge
) {
402 clk_unprepare(sg
->sata1_pclk
);
403 clk_unprepare(sg
->sata0_pclk
);
408 static int gemini_sata_remove(struct platform_device
*pdev
)
410 struct sata_gemini
*sg
= platform_get_drvdata(pdev
);
412 if (sg
->sata_bridge
) {
413 clk_unprepare(sg
->sata1_pclk
);
414 clk_unprepare(sg
->sata0_pclk
);
421 static const struct of_device_id gemini_sata_of_match
[] = {
423 .compatible
= "cortina,gemini-sata-bridge",
428 static struct platform_driver gemini_sata_driver
= {
431 .of_match_table
= of_match_ptr(gemini_sata_of_match
),
433 .probe
= gemini_sata_probe
,
434 .remove
= gemini_sata_remove
,
436 module_platform_driver(gemini_sata_driver
);
438 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
439 MODULE_LICENSE("GPL");
440 MODULE_ALIAS("platform:" DRV_NAME
);