2 * Cortina Systems Gemini SATA bridge add-on to Faraday FTIDE010
3 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/bitops.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/regmap.h>
12 #include <linux/delay.h>
13 #include <linux/reset.h>
14 #include <linux/of_address.h>
15 #include <linux/of_device.h>
16 #include <linux/clk.h>
18 #include <linux/pinctrl/consumer.h>
19 #include "sata_gemini.h"
21 #define DRV_NAME "gemini_sata_bridge"
24 * struct sata_gemini - a state container for a Gemini SATA bridge
25 * @dev: the containing device
26 * @base: remapped I/O memory base
27 * @muxmode: the current muxing mode
28 * @ide_pins: if the device is using the plain IDE interface pins
29 * @sata_bridge: if the device enables the SATA bridge
30 * @sata0_reset: SATA0 reset handler
31 * @sata1_reset: SATA1 reset handler
32 * @sata0_pclk: SATA0 PCLK handler
33 * @sata1_pclk: SATA1 PCLK handler
38 enum gemini_muxmode muxmode
;
41 struct reset_control
*sata0_reset
;
42 struct reset_control
*sata1_reset
;
43 struct clk
*sata0_pclk
;
44 struct clk
*sata1_pclk
;
47 /* Miscellaneous Control Register */
48 #define GEMINI_GLOBAL_MISC_CTRL 0x30
50 * Values of IDE IOMUX bits in the misc control register
52 * Bits 26:24 are "IDE IO Select", which decides what SATA
53 * adapters are connected to which of the two IDE/ATA
54 * controllers in the Gemini. We can connect the two IDE blocks
55 * to one SATA adapter each, both acting as master, or one IDE
56 * blocks to two SATA adapters so the IDE block can act in a
57 * master/slave configuration.
59 * We also bring out different blocks on the actual IDE
60 * pins (not SATA pins) if (and only if) these are muxed in.
63 * Mode 0: 000 - ata0 master <-> sata0
64 * ata1 master <-> sata1
65 * ata0 slave interface brought out on IDE pads
66 * Mode 1: 001 - ata0 master <-> sata0
67 * ata1 master <-> sata1
68 * ata1 slave interface brought out on IDE pads
69 * Mode 2: 010 - ata1 master <-> sata1
70 * ata1 slave <-> sata0
71 * ata0 master and slave interfaces brought out
73 * Mode 3: 011 - ata0 master <-> sata0
74 * ata1 slave <-> sata1
75 * ata1 master and slave interfaces brought out
78 #define GEMINI_IDE_IOMUX_MASK (7 << 24)
79 #define GEMINI_IDE_IOMUX_MODE0 (0 << 24)
80 #define GEMINI_IDE_IOMUX_MODE1 (1 << 24)
81 #define GEMINI_IDE_IOMUX_MODE2 (2 << 24)
82 #define GEMINI_IDE_IOMUX_MODE3 (3 << 24)
83 #define GEMINI_IDE_IOMUX_SHIFT (24)
86 * Registers directly controlling the PATA<->SATA adapters
88 #define GEMINI_SATA_ID 0x00
89 #define GEMINI_SATA_PHY_ID 0x04
90 #define GEMINI_SATA0_STATUS 0x08
91 #define GEMINI_SATA1_STATUS 0x0c
92 #define GEMINI_SATA0_CTRL 0x18
93 #define GEMINI_SATA1_CTRL 0x1c
95 #define GEMINI_SATA_STATUS_BIST_DONE BIT(5)
96 #define GEMINI_SATA_STATUS_BIST_OK BIT(4)
97 #define GEMINI_SATA_STATUS_PHY_READY BIT(0)
99 #define GEMINI_SATA_CTRL_PHY_BIST_EN BIT(14)
100 #define GEMINI_SATA_CTRL_PHY_FORCE_IDLE BIT(13)
101 #define GEMINI_SATA_CTRL_PHY_FORCE_READY BIT(12)
102 #define GEMINI_SATA_CTRL_PHY_AFE_LOOP_EN BIT(10)
103 #define GEMINI_SATA_CTRL_PHY_DIG_LOOP_EN BIT(9)
104 #define GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN BIT(4)
105 #define GEMINI_SATA_CTRL_ATAPI_EN BIT(3)
106 #define GEMINI_SATA_CTRL_BUS_WITH_20 BIT(2)
107 #define GEMINI_SATA_CTRL_SLAVE_EN BIT(1)
108 #define GEMINI_SATA_CTRL_EN BIT(0)
111 * There is only ever one instance of this bridge on a system,
112 * so create a singleton so that the FTIDE010 instances can grab
115 static struct sata_gemini
*sg_singleton
;
117 struct sata_gemini
*gemini_sata_bridge_get(void)
121 return ERR_PTR(-EPROBE_DEFER
);
123 EXPORT_SYMBOL(gemini_sata_bridge_get
);
125 bool gemini_sata_bridge_enabled(struct sata_gemini
*sg
, bool is_ata1
)
127 if (!sg
->sata_bridge
)
130 * In muxmode 2 and 3 one of the ATA controllers is
131 * actually not connected to any SATA bridge.
133 if ((sg
->muxmode
== GEMINI_MUXMODE_2
) &&
136 if ((sg
->muxmode
== GEMINI_MUXMODE_3
) &&
142 EXPORT_SYMBOL(gemini_sata_bridge_enabled
);
144 enum gemini_muxmode
gemini_sata_get_muxmode(struct sata_gemini
*sg
)
148 EXPORT_SYMBOL(gemini_sata_get_muxmode
);
150 static int gemini_sata_setup_bridge(struct sata_gemini
*sg
,
153 unsigned long timeout
= jiffies
+ (HZ
* 1);
158 val
= GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN
| GEMINI_SATA_CTRL_EN
;
159 /* SATA0 slave mode is only used in muxmode 2 */
160 if (sg
->muxmode
== GEMINI_MUXMODE_2
)
161 val
|= GEMINI_SATA_CTRL_SLAVE_EN
;
162 writel(val
, sg
->base
+ GEMINI_SATA0_CTRL
);
164 val
= GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN
| GEMINI_SATA_CTRL_EN
;
165 /* SATA1 slave mode is only used in muxmode 3 */
166 if (sg
->muxmode
== GEMINI_MUXMODE_3
)
167 val
|= GEMINI_SATA_CTRL_SLAVE_EN
;
168 writel(val
, sg
->base
+ GEMINI_SATA1_CTRL
);
171 /* Vendor code waits 10 ms here */
174 /* Wait for PHY to become ready */
179 val
= readl(sg
->base
+ GEMINI_SATA0_STATUS
);
181 val
= readl(sg
->base
+ GEMINI_SATA1_STATUS
);
182 if (val
& GEMINI_SATA_STATUS_PHY_READY
)
184 } while (time_before(jiffies
, timeout
));
186 bridge_online
= !!(val
& GEMINI_SATA_STATUS_PHY_READY
);
188 dev_info(sg
->dev
, "SATA%d PHY %s\n", bridge
,
189 bridge_online
? "ready" : "not ready");
191 return bridge_online
? 0: -ENODEV
;
194 int gemini_sata_start_bridge(struct sata_gemini
*sg
, unsigned int bridge
)
200 pclk
= sg
->sata0_pclk
;
202 pclk
= sg
->sata1_pclk
;
206 /* Do not keep clocking a bridge that is not online */
207 ret
= gemini_sata_setup_bridge(sg
, bridge
);
213 EXPORT_SYMBOL(gemini_sata_start_bridge
);
215 void gemini_sata_stop_bridge(struct sata_gemini
*sg
, unsigned int bridge
)
218 clk_disable(sg
->sata0_pclk
);
219 else if (bridge
== 1)
220 clk_disable(sg
->sata1_pclk
);
222 EXPORT_SYMBOL(gemini_sata_stop_bridge
);
224 int gemini_sata_reset_bridge(struct sata_gemini
*sg
,
228 reset_control_reset(sg
->sata0_reset
);
230 reset_control_reset(sg
->sata1_reset
);
232 return gemini_sata_setup_bridge(sg
, bridge
);
234 EXPORT_SYMBOL(gemini_sata_reset_bridge
);
236 static int gemini_sata_bridge_init(struct sata_gemini
*sg
)
238 struct device
*dev
= sg
->dev
;
239 u32 sata_id
, sata_phy_id
;
242 sg
->sata0_pclk
= devm_clk_get(dev
, "SATA0_PCLK");
243 if (IS_ERR(sg
->sata0_pclk
)) {
244 dev_err(dev
, "no SATA0 PCLK");
247 sg
->sata1_pclk
= devm_clk_get(dev
, "SATA1_PCLK");
248 if (IS_ERR(sg
->sata1_pclk
)) {
249 dev_err(dev
, "no SATA1 PCLK");
253 ret
= clk_prepare_enable(sg
->sata0_pclk
);
255 pr_err("failed to enable SATA0 PCLK\n");
258 ret
= clk_prepare_enable(sg
->sata1_pclk
);
260 pr_err("failed to enable SATA1 PCLK\n");
261 clk_disable_unprepare(sg
->sata0_pclk
);
265 sg
->sata0_reset
= devm_reset_control_get_exclusive(dev
, "sata0");
266 if (IS_ERR(sg
->sata0_reset
)) {
267 dev_err(dev
, "no SATA0 reset controller\n");
268 clk_disable_unprepare(sg
->sata1_pclk
);
269 clk_disable_unprepare(sg
->sata0_pclk
);
270 return PTR_ERR(sg
->sata0_reset
);
272 sg
->sata1_reset
= devm_reset_control_get_exclusive(dev
, "sata1");
273 if (IS_ERR(sg
->sata1_reset
)) {
274 dev_err(dev
, "no SATA1 reset controller\n");
275 clk_disable_unprepare(sg
->sata1_pclk
);
276 clk_disable_unprepare(sg
->sata0_pclk
);
277 return PTR_ERR(sg
->sata1_reset
);
280 sata_id
= readl(sg
->base
+ GEMINI_SATA_ID
);
281 sata_phy_id
= readl(sg
->base
+ GEMINI_SATA_PHY_ID
);
282 sg
->sata_bridge
= true;
283 clk_disable(sg
->sata0_pclk
);
284 clk_disable(sg
->sata1_pclk
);
286 dev_info(dev
, "SATA ID %08x, PHY ID: %08x\n", sata_id
, sata_phy_id
);
291 static int gemini_setup_ide_pins(struct device
*dev
)
294 struct pinctrl_state
*ide_state
;
297 p
= devm_pinctrl_get(dev
);
301 ide_state
= pinctrl_lookup_state(p
, "ide");
302 if (IS_ERR(ide_state
))
303 return PTR_ERR(ide_state
);
305 ret
= pinctrl_select_state(p
, ide_state
);
307 dev_err(dev
, "could not select IDE state\n");
314 static int gemini_sata_probe(struct platform_device
*pdev
)
316 struct device
*dev
= &pdev
->dev
;
317 struct device_node
*np
= dev
->of_node
;
318 struct sata_gemini
*sg
;
320 struct resource
*res
;
321 enum gemini_muxmode muxmode
;
326 sg
= devm_kzalloc(dev
, sizeof(*sg
), GFP_KERNEL
);
331 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
335 sg
->base
= devm_ioremap_resource(dev
, res
);
336 if (IS_ERR(sg
->base
))
337 return PTR_ERR(sg
->base
);
339 map
= syscon_regmap_lookup_by_phandle(np
, "syscon");
341 dev_err(dev
, "no global syscon\n");
345 /* Set up the SATA bridge if need be */
346 if (of_property_read_bool(np
, "cortina,gemini-enable-sata-bridge")) {
347 ret
= gemini_sata_bridge_init(sg
);
352 if (of_property_read_bool(np
, "cortina,gemini-enable-ide-pins"))
355 if (!sg
->sata_bridge
&& !sg
->ide_pins
) {
356 dev_err(dev
, "neither SATA bridge or IDE output enabled\n");
361 ret
= of_property_read_u32(np
, "cortina,gemini-ata-muxmode", &muxmode
);
363 dev_err(dev
, "could not parse ATA muxmode\n");
366 if (muxmode
> GEMINI_MUXMODE_3
) {
367 dev_err(dev
, "illegal muxmode %d\n", muxmode
);
371 sg
->muxmode
= muxmode
;
372 gmask
= GEMINI_IDE_IOMUX_MASK
;
373 gmode
= (muxmode
<< GEMINI_IDE_IOMUX_SHIFT
);
375 ret
= regmap_update_bits(map
, GEMINI_GLOBAL_MISC_CTRL
, gmask
, gmode
);
377 dev_err(dev
, "unable to set up IDE muxing\n");
383 * Route out the IDE pins if desired.
384 * This is done by looking up a special pin control state called
385 * "ide" that will route out the IDE pins.
388 ret
= gemini_setup_ide_pins(dev
);
393 dev_info(dev
, "set up the Gemini IDE/SATA nexus\n");
394 platform_set_drvdata(pdev
, sg
);
400 if (sg
->sata_bridge
) {
401 clk_unprepare(sg
->sata1_pclk
);
402 clk_unprepare(sg
->sata0_pclk
);
407 static int gemini_sata_remove(struct platform_device
*pdev
)
409 struct sata_gemini
*sg
= platform_get_drvdata(pdev
);
411 if (sg
->sata_bridge
) {
412 clk_unprepare(sg
->sata1_pclk
);
413 clk_unprepare(sg
->sata0_pclk
);
420 static const struct of_device_id gemini_sata_of_match
[] = {
422 .compatible
= "cortina,gemini-sata-bridge",
427 static struct platform_driver gemini_sata_driver
= {
430 .of_match_table
= of_match_ptr(gemini_sata_of_match
),
432 .probe
= gemini_sata_probe
,
433 .remove
= gemini_sata_remove
,
435 module_platform_driver(gemini_sata_driver
);
437 MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
438 MODULE_LICENSE("GPL");
439 MODULE_ALIAS("platform:" DRV_NAME
);