1
2
3
4
5
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>
17#include <linux/io.h>
18#include <linux/pinctrl/consumer.h>
19#include "sata_gemini.h"
20
21#define DRV_NAME "gemini_sata_bridge"
22
23
24
25
26
27
28
29
30
31
32
33
34
35struct sata_gemini {
36 struct device *dev;
37 void __iomem *base;
38 enum gemini_muxmode muxmode;
39 bool ide_pins;
40 bool sata_bridge;
41 struct reset_control *sata0_reset;
42 struct reset_control *sata1_reset;
43 struct clk *sata0_pclk;
44 struct clk *sata1_pclk;
45};
46
47
48#define GEMINI_GLOBAL_MISC_CTRL 0x30
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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)
84
85
86
87
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
94
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)
98
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)
109
110
111
112
113
114
115static struct sata_gemini *sg_singleton;
116
117struct sata_gemini *gemini_sata_bridge_get(void)
118{
119 if (sg_singleton)
120 return sg_singleton;
121 return ERR_PTR(-EPROBE_DEFER);
122}
123EXPORT_SYMBOL(gemini_sata_bridge_get);
124
125bool gemini_sata_bridge_enabled(struct sata_gemini *sg, bool is_ata1)
126{
127 if (!sg->sata_bridge)
128 return false;
129
130
131
132
133 if ((sg->muxmode == GEMINI_MUXMODE_2) &&
134 !is_ata1)
135 return false;
136 if ((sg->muxmode == GEMINI_MUXMODE_3) &&
137 is_ata1)
138 return false;
139
140 return true;
141}
142EXPORT_SYMBOL(gemini_sata_bridge_enabled);
143
144enum gemini_muxmode gemini_sata_get_muxmode(struct sata_gemini *sg)
145{
146 return sg->muxmode;
147}
148EXPORT_SYMBOL(gemini_sata_get_muxmode);
149
150static int gemini_sata_setup_bridge(struct sata_gemini *sg,
151 unsigned int bridge)
152{
153 unsigned long timeout = jiffies + (HZ * 1);
154 bool bridge_online;
155 u32 val;
156
157 if (bridge == 0) {
158 val = GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN | GEMINI_SATA_CTRL_EN;
159
160 if (sg->muxmode == GEMINI_MUXMODE_2)
161 val |= GEMINI_SATA_CTRL_SLAVE_EN;
162 writel(val, sg->base + GEMINI_SATA0_CTRL);
163 } else {
164 val = GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN | GEMINI_SATA_CTRL_EN;
165
166 if (sg->muxmode == GEMINI_MUXMODE_3)
167 val |= GEMINI_SATA_CTRL_SLAVE_EN;
168 writel(val, sg->base + GEMINI_SATA1_CTRL);
169 }
170
171
172 msleep(10);
173
174
175 do {
176 msleep(100);
177
178 if (bridge == 0)
179 val = readl(sg->base + GEMINI_SATA0_STATUS);
180 else
181 val = readl(sg->base + GEMINI_SATA1_STATUS);
182 if (val & GEMINI_SATA_STATUS_PHY_READY)
183 break;
184 } while (time_before(jiffies, timeout));
185
186 bridge_online = !!(val & GEMINI_SATA_STATUS_PHY_READY);
187
188 dev_info(sg->dev, "SATA%d PHY %s\n", bridge,
189 bridge_online ? "ready" : "not ready");
190
191 return bridge_online ? 0: -ENODEV;
192}
193
194int gemini_sata_start_bridge(struct sata_gemini *sg, unsigned int bridge)
195{
196 struct clk *pclk;
197 int ret;
198
199 if (bridge == 0)
200 pclk = sg->sata0_pclk;
201 else
202 pclk = sg->sata1_pclk;
203 clk_enable(pclk);
204 msleep(10);
205
206
207 ret = gemini_sata_setup_bridge(sg, bridge);
208 if (ret)
209 clk_disable(pclk);
210
211 return ret;
212}
213EXPORT_SYMBOL(gemini_sata_start_bridge);
214
215void gemini_sata_stop_bridge(struct sata_gemini *sg, unsigned int bridge)
216{
217 if (bridge == 0)
218 clk_disable(sg->sata0_pclk);
219 else if (bridge == 1)
220 clk_disable(sg->sata1_pclk);
221}
222EXPORT_SYMBOL(gemini_sata_stop_bridge);
223
224int gemini_sata_reset_bridge(struct sata_gemini *sg,
225 unsigned int bridge)
226{
227 if (bridge == 0)
228 reset_control_reset(sg->sata0_reset);
229 else
230 reset_control_reset(sg->sata1_reset);
231 msleep(10);
232 return gemini_sata_setup_bridge(sg, bridge);
233}
234EXPORT_SYMBOL(gemini_sata_reset_bridge);
235
236static int gemini_sata_bridge_init(struct sata_gemini *sg)
237{
238 struct device *dev = sg->dev;
239 u32 sata_id, sata_phy_id;
240 int ret;
241
242 sg->sata0_pclk = devm_clk_get(dev, "SATA0_PCLK");
243 if (IS_ERR(sg->sata0_pclk)) {
244 dev_err(dev, "no SATA0 PCLK");
245 return -ENODEV;
246 }
247 sg->sata1_pclk = devm_clk_get(dev, "SATA1_PCLK");
248 if (IS_ERR(sg->sata1_pclk)) {
249 dev_err(dev, "no SATA1 PCLK");
250 return -ENODEV;
251 }
252
253 ret = clk_prepare_enable(sg->sata0_pclk);
254 if (ret) {
255 pr_err("failed to enable SATA0 PCLK\n");
256 return ret;
257 }
258 ret = clk_prepare_enable(sg->sata1_pclk);
259 if (ret) {
260 pr_err("failed to enable SATA1 PCLK\n");
261 clk_disable_unprepare(sg->sata0_pclk);
262 return ret;
263 }
264
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);
271 }
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);
278 }
279
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);
285
286 dev_info(dev, "SATA ID %08x, PHY ID: %08x\n", sata_id, sata_phy_id);
287
288 return 0;
289}
290
291static int gemini_setup_ide_pins(struct device *dev)
292{
293 struct pinctrl *p;
294 struct pinctrl_state *ide_state;
295 int ret;
296
297 p = devm_pinctrl_get(dev);
298 if (IS_ERR(p))
299 return PTR_ERR(p);
300
301 ide_state = pinctrl_lookup_state(p, "ide");
302 if (IS_ERR(ide_state))
303 return PTR_ERR(ide_state);
304
305 ret = pinctrl_select_state(p, ide_state);
306 if (ret) {
307 dev_err(dev, "could not select IDE state\n");
308 return ret;
309 }
310
311 return 0;
312}
313
314static int gemini_sata_probe(struct platform_device *pdev)
315{
316 struct device *dev = &pdev->dev;
317 struct device_node *np = dev->of_node;
318 struct sata_gemini *sg;
319 struct regmap *map;
320 struct resource *res;
321 enum gemini_muxmode muxmode;
322 u32 gmode;
323 u32 gmask;
324 int ret;
325
326 sg = devm_kzalloc(dev, sizeof(*sg), GFP_KERNEL);
327 if (!sg)
328 return -ENOMEM;
329 sg->dev = dev;
330
331 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
332 if (!res)
333 return -ENODEV;
334
335 sg->base = devm_ioremap_resource(dev, res);
336 if (IS_ERR(sg->base))
337 return PTR_ERR(sg->base);
338
339 map = syscon_regmap_lookup_by_phandle(np, "syscon");
340 if (IS_ERR(map)) {
341 dev_err(dev, "no global syscon\n");
342 return PTR_ERR(map);
343 }
344
345
346 if (of_property_read_bool(np, "cortina,gemini-enable-sata-bridge")) {
347 ret = gemini_sata_bridge_init(sg);
348 if (ret)
349 return ret;
350 }
351
352 if (of_property_read_bool(np, "cortina,gemini-enable-ide-pins"))
353 sg->ide_pins = true;
354
355 if (!sg->sata_bridge && !sg->ide_pins) {
356 dev_err(dev, "neither SATA bridge or IDE output enabled\n");
357 ret = -EINVAL;
358 goto out_unprep_clk;
359 }
360
361 ret = of_property_read_u32(np, "cortina,gemini-ata-muxmode", &muxmode);
362 if (ret) {
363 dev_err(dev, "could not parse ATA muxmode\n");
364 goto out_unprep_clk;
365 }
366 if (muxmode > GEMINI_MUXMODE_3) {
367 dev_err(dev, "illegal muxmode %d\n", muxmode);
368 ret = -EINVAL;
369 goto out_unprep_clk;
370 }
371 sg->muxmode = muxmode;
372 gmask = GEMINI_IDE_IOMUX_MASK;
373 gmode = (muxmode << GEMINI_IDE_IOMUX_SHIFT);
374
375 ret = regmap_update_bits(map, GEMINI_GLOBAL_MISC_CTRL, gmask, gmode);
376 if (ret) {
377 dev_err(dev, "unable to set up IDE muxing\n");
378 ret = -ENODEV;
379 goto out_unprep_clk;
380 }
381
382
383
384
385
386
387 if (sg->ide_pins) {
388 ret = gemini_setup_ide_pins(dev);
389 if (ret)
390 return ret;
391 }
392
393 dev_info(dev, "set up the Gemini IDE/SATA nexus\n");
394 platform_set_drvdata(pdev, sg);
395 sg_singleton = sg;
396
397 return 0;
398
399out_unprep_clk:
400 if (sg->sata_bridge) {
401 clk_unprepare(sg->sata1_pclk);
402 clk_unprepare(sg->sata0_pclk);
403 }
404 return ret;
405}
406
407static int gemini_sata_remove(struct platform_device *pdev)
408{
409 struct sata_gemini *sg = platform_get_drvdata(pdev);
410
411 if (sg->sata_bridge) {
412 clk_unprepare(sg->sata1_pclk);
413 clk_unprepare(sg->sata0_pclk);
414 }
415 sg_singleton = NULL;
416
417 return 0;
418}
419
420static const struct of_device_id gemini_sata_of_match[] = {
421 {
422 .compatible = "cortina,gemini-sata-bridge",
423 },
424 {},
425};
426
427static struct platform_driver gemini_sata_driver = {
428 .driver = {
429 .name = DRV_NAME,
430 .of_match_table = of_match_ptr(gemini_sata_of_match),
431 },
432 .probe = gemini_sata_probe,
433 .remove = gemini_sata_remove,
434};
435module_platform_driver(gemini_sata_driver);
436
437MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
438MODULE_LICENSE("GPL");
439MODULE_ALIAS("platform:" DRV_NAME);
440