GNU Linux-libre 5.10.217-gnu1
[releases.git] / drivers / ata / sata_gemini.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Cortina Systems Gemini SATA bridge add-on to Faraday FTIDE010
4  * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
5  */
6
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>
18 #include <linux/io.h>
19 #include <linux/pinctrl/consumer.h>
20 #include "sata_gemini.h"
21
22 #define DRV_NAME "gemini_sata_bridge"
23
24 /**
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
35  */
36 struct sata_gemini {
37         struct device *dev;
38         void __iomem *base;
39         enum gemini_muxmode muxmode;
40         bool ide_pins;
41         bool sata_bridge;
42         struct reset_control *sata0_reset;
43         struct reset_control *sata1_reset;
44         struct clk *sata0_pclk;
45         struct clk *sata1_pclk;
46 };
47
48 /* Miscellaneous Control Register */
49 #define GEMINI_GLOBAL_MISC_CTRL         0x30
50 /*
51  * Values of IDE IOMUX bits in the misc control register
52  *
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.
59  *
60  * We also bring out different blocks on the actual IDE
61  * pins (not SATA pins) if (and only if) these are muxed in.
62  *
63  * 111-100 - Reserved
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
73  *                    on IDE pads
74  * Mode 3: 011 - ata0 master <-> sata0
75  *               ata1 slave  <-> sata1
76  *               ata1 master and slave interfaces brought out
77  *                    on IDE pads
78  */
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)
85
86 /*
87  * Registers directly controlling the PATA<->SATA adapters
88  */
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
95
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)
99
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)
110
111 /*
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
114  * a reference to it.
115  */
116 static struct sata_gemini *sg_singleton;
117
118 struct sata_gemini *gemini_sata_bridge_get(void)
119 {
120         if (sg_singleton)
121                 return sg_singleton;
122         return ERR_PTR(-EPROBE_DEFER);
123 }
124 EXPORT_SYMBOL(gemini_sata_bridge_get);
125
126 bool gemini_sata_bridge_enabled(struct sata_gemini *sg, bool is_ata1)
127 {
128         if (!sg->sata_bridge)
129                 return false;
130         /*
131          * In muxmode 2 and 3 one of the ATA controllers is
132          * actually not connected to any SATA bridge.
133          */
134         if ((sg->muxmode == GEMINI_MUXMODE_2) &&
135             !is_ata1)
136                 return false;
137         if ((sg->muxmode == GEMINI_MUXMODE_3) &&
138             is_ata1)
139                 return false;
140
141         return true;
142 }
143 EXPORT_SYMBOL(gemini_sata_bridge_enabled);
144
145 enum gemini_muxmode gemini_sata_get_muxmode(struct sata_gemini *sg)
146 {
147         return sg->muxmode;
148 }
149 EXPORT_SYMBOL(gemini_sata_get_muxmode);
150
151 static int gemini_sata_setup_bridge(struct sata_gemini *sg,
152                                     unsigned int bridge)
153 {
154         unsigned long timeout = jiffies + (HZ * 1);
155         bool bridge_online;
156         u32 val;
157
158         if (bridge == 0) {
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);
164         } else {
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);
170         }
171
172         /* Vendor code waits 10 ms here */
173         msleep(10);
174
175         /* Wait for PHY to become ready */
176         do {
177                 msleep(100);
178
179                 if (bridge == 0)
180                         val = readl(sg->base + GEMINI_SATA0_STATUS);
181                 else
182                         val = readl(sg->base + GEMINI_SATA1_STATUS);
183                 if (val & GEMINI_SATA_STATUS_PHY_READY)
184                         break;
185         } while (time_before(jiffies, timeout));
186
187         bridge_online = !!(val & GEMINI_SATA_STATUS_PHY_READY);
188
189         dev_info(sg->dev, "SATA%d PHY %s\n", bridge,
190                  bridge_online ? "ready" : "not ready");
191
192         return bridge_online ? 0: -ENODEV;
193 }
194
195 int gemini_sata_start_bridge(struct sata_gemini *sg, unsigned int bridge)
196 {
197         struct clk *pclk;
198         int ret;
199
200         if (bridge == 0)
201                 pclk = sg->sata0_pclk;
202         else
203                 pclk = sg->sata1_pclk;
204         ret = clk_enable(pclk);
205         if (ret)
206                 return ret;
207
208         msleep(10);
209
210         /* Do not keep clocking a bridge that is not online */
211         ret = gemini_sata_setup_bridge(sg, bridge);
212         if (ret)
213                 clk_disable(pclk);
214
215         return ret;
216 }
217 EXPORT_SYMBOL(gemini_sata_start_bridge);
218
219 void gemini_sata_stop_bridge(struct sata_gemini *sg, unsigned int bridge)
220 {
221         if (bridge == 0)
222                 clk_disable(sg->sata0_pclk);
223         else if (bridge == 1)
224                 clk_disable(sg->sata1_pclk);
225 }
226 EXPORT_SYMBOL(gemini_sata_stop_bridge);
227
228 int gemini_sata_reset_bridge(struct sata_gemini *sg,
229                              unsigned int bridge)
230 {
231         if (bridge == 0)
232                 reset_control_reset(sg->sata0_reset);
233         else
234                 reset_control_reset(sg->sata1_reset);
235         msleep(10);
236         return gemini_sata_setup_bridge(sg, bridge);
237 }
238 EXPORT_SYMBOL(gemini_sata_reset_bridge);
239
240 static int gemini_sata_bridge_init(struct sata_gemini *sg)
241 {
242         struct device *dev = sg->dev;
243         u32 sata_id, sata_phy_id;
244         int ret;
245
246         sg->sata0_pclk = devm_clk_get(dev, "SATA0_PCLK");
247         if (IS_ERR(sg->sata0_pclk)) {
248                 dev_err(dev, "no SATA0 PCLK");
249                 return -ENODEV;
250         }
251         sg->sata1_pclk = devm_clk_get(dev, "SATA1_PCLK");
252         if (IS_ERR(sg->sata1_pclk)) {
253                 dev_err(dev, "no SATA1 PCLK");
254                 return -ENODEV;
255         }
256
257         ret = clk_prepare_enable(sg->sata0_pclk);
258         if (ret) {
259                 pr_err("failed to enable SATA0 PCLK\n");
260                 return ret;
261         }
262         ret = clk_prepare_enable(sg->sata1_pclk);
263         if (ret) {
264                 pr_err("failed to enable SATA1 PCLK\n");
265                 clk_disable_unprepare(sg->sata0_pclk);
266                 return ret;
267         }
268
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);
275         }
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);
282         }
283
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);
289
290         dev_info(dev, "SATA ID %08x, PHY ID: %08x\n", sata_id, sata_phy_id);
291
292         return 0;
293 }
294
295 static int gemini_setup_ide_pins(struct device *dev)
296 {
297         struct pinctrl *p;
298         struct pinctrl_state *ide_state;
299         int ret;
300
301         p = devm_pinctrl_get(dev);
302         if (IS_ERR(p))
303                 return PTR_ERR(p);
304
305         ide_state = pinctrl_lookup_state(p, "ide");
306         if (IS_ERR(ide_state))
307                 return PTR_ERR(ide_state);
308
309         ret = pinctrl_select_state(p, ide_state);
310         if (ret) {
311                 dev_err(dev, "could not select IDE state\n");
312                 return ret;
313         }
314
315         return 0;
316 }
317
318 static int gemini_sata_probe(struct platform_device *pdev)
319 {
320         struct device *dev = &pdev->dev;
321         struct device_node *np = dev->of_node;
322         struct sata_gemini *sg;
323         struct regmap *map;
324         struct resource *res;
325         enum gemini_muxmode muxmode;
326         u32 gmode;
327         u32 gmask;
328         int ret;
329
330         sg = devm_kzalloc(dev, sizeof(*sg), GFP_KERNEL);
331         if (!sg)
332                 return -ENOMEM;
333         sg->dev = dev;
334
335         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
336         if (!res)
337                 return -ENODEV;
338
339         sg->base = devm_ioremap_resource(dev, res);
340         if (IS_ERR(sg->base))
341                 return PTR_ERR(sg->base);
342
343         map = syscon_regmap_lookup_by_phandle(np, "syscon");
344         if (IS_ERR(map)) {
345                 dev_err(dev, "no global syscon\n");
346                 return PTR_ERR(map);
347         }
348
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);
352                 if (ret)
353                         return ret;
354         }
355
356         if (of_property_read_bool(np, "cortina,gemini-enable-ide-pins"))
357                 sg->ide_pins = true;
358
359         if (!sg->sata_bridge && !sg->ide_pins) {
360                 dev_err(dev, "neither SATA bridge or IDE output enabled\n");
361                 ret = -EINVAL;
362                 goto out_unprep_clk;
363         }
364
365         ret = of_property_read_u32(np, "cortina,gemini-ata-muxmode", &muxmode);
366         if (ret) {
367                 dev_err(dev, "could not parse ATA muxmode\n");
368                 goto out_unprep_clk;
369         }
370         if (muxmode > GEMINI_MUXMODE_3) {
371                 dev_err(dev, "illegal muxmode %d\n", muxmode);
372                 ret = -EINVAL;
373                 goto out_unprep_clk;
374         }
375         sg->muxmode = muxmode;
376         gmask = GEMINI_IDE_IOMUX_MASK;
377         gmode = (muxmode << GEMINI_IDE_IOMUX_SHIFT);
378
379         ret = regmap_update_bits(map, GEMINI_GLOBAL_MISC_CTRL, gmask, gmode);
380         if (ret) {
381                 dev_err(dev, "unable to set up IDE muxing\n");
382                 ret = -ENODEV;
383                 goto out_unprep_clk;
384         }
385
386         /*
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.
390          */
391         if (sg->ide_pins) {
392                 ret = gemini_setup_ide_pins(dev);
393                 if (ret)
394                         return ret;
395         }
396
397         dev_info(dev, "set up the Gemini IDE/SATA nexus\n");
398         platform_set_drvdata(pdev, sg);
399         sg_singleton = sg;
400
401         return 0;
402
403 out_unprep_clk:
404         if (sg->sata_bridge) {
405                 clk_unprepare(sg->sata1_pclk);
406                 clk_unprepare(sg->sata0_pclk);
407         }
408         return ret;
409 }
410
411 static int gemini_sata_remove(struct platform_device *pdev)
412 {
413         struct sata_gemini *sg = platform_get_drvdata(pdev);
414
415         if (sg->sata_bridge) {
416                 clk_unprepare(sg->sata1_pclk);
417                 clk_unprepare(sg->sata0_pclk);
418         }
419         sg_singleton = NULL;
420
421         return 0;
422 }
423
424 static const struct of_device_id gemini_sata_of_match[] = {
425         {
426                 .compatible = "cortina,gemini-sata-bridge",
427         },
428         {},
429 };
430
431 static struct platform_driver gemini_sata_driver = {
432         .driver = {
433                 .name = DRV_NAME,
434                 .of_match_table = of_match_ptr(gemini_sata_of_match),
435         },
436         .probe = gemini_sata_probe,
437         .remove = gemini_sata_remove,
438 };
439 module_platform_driver(gemini_sata_driver);
440
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);