18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cortina Systems Gemini SATA bridge add-on to Faraday FTIDE010
48c2ecf20Sopenharmony_ci * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
108c2ecf20Sopenharmony_ci#include <linux/bitops.h>
118c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
128c2ecf20Sopenharmony_ci#include <linux/regmap.h>
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/reset.h>
158c2ecf20Sopenharmony_ci#include <linux/of_address.h>
168c2ecf20Sopenharmony_ci#include <linux/of_device.h>
178c2ecf20Sopenharmony_ci#include <linux/clk.h>
188c2ecf20Sopenharmony_ci#include <linux/io.h>
198c2ecf20Sopenharmony_ci#include <linux/pinctrl/consumer.h>
208c2ecf20Sopenharmony_ci#include "sata_gemini.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define DRV_NAME "gemini_sata_bridge"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/**
258c2ecf20Sopenharmony_ci * struct sata_gemini - a state container for a Gemini SATA bridge
268c2ecf20Sopenharmony_ci * @dev: the containing device
278c2ecf20Sopenharmony_ci * @base: remapped I/O memory base
288c2ecf20Sopenharmony_ci * @muxmode: the current muxing mode
298c2ecf20Sopenharmony_ci * @ide_pins: if the device is using the plain IDE interface pins
308c2ecf20Sopenharmony_ci * @sata_bridge: if the device enables the SATA bridge
318c2ecf20Sopenharmony_ci * @sata0_reset: SATA0 reset handler
328c2ecf20Sopenharmony_ci * @sata1_reset: SATA1 reset handler
338c2ecf20Sopenharmony_ci * @sata0_pclk: SATA0 PCLK handler
348c2ecf20Sopenharmony_ci * @sata1_pclk: SATA1 PCLK handler
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_cistruct sata_gemini {
378c2ecf20Sopenharmony_ci	struct device *dev;
388c2ecf20Sopenharmony_ci	void __iomem *base;
398c2ecf20Sopenharmony_ci	enum gemini_muxmode muxmode;
408c2ecf20Sopenharmony_ci	bool ide_pins;
418c2ecf20Sopenharmony_ci	bool sata_bridge;
428c2ecf20Sopenharmony_ci	struct reset_control *sata0_reset;
438c2ecf20Sopenharmony_ci	struct reset_control *sata1_reset;
448c2ecf20Sopenharmony_ci	struct clk *sata0_pclk;
458c2ecf20Sopenharmony_ci	struct clk *sata1_pclk;
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/* Miscellaneous Control Register */
498c2ecf20Sopenharmony_ci#define GEMINI_GLOBAL_MISC_CTRL		0x30
508c2ecf20Sopenharmony_ci/*
518c2ecf20Sopenharmony_ci * Values of IDE IOMUX bits in the misc control register
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * Bits 26:24 are "IDE IO Select", which decides what SATA
548c2ecf20Sopenharmony_ci * adapters are connected to which of the two IDE/ATA
558c2ecf20Sopenharmony_ci * controllers in the Gemini. We can connect the two IDE blocks
568c2ecf20Sopenharmony_ci * to one SATA adapter each, both acting as master, or one IDE
578c2ecf20Sopenharmony_ci * blocks to two SATA adapters so the IDE block can act in a
588c2ecf20Sopenharmony_ci * master/slave configuration.
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci * We also bring out different blocks on the actual IDE
618c2ecf20Sopenharmony_ci * pins (not SATA pins) if (and only if) these are muxed in.
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * 111-100 - Reserved
648c2ecf20Sopenharmony_ci * Mode 0: 000 - ata0 master <-> sata0
658c2ecf20Sopenharmony_ci *               ata1 master <-> sata1
668c2ecf20Sopenharmony_ci *               ata0 slave interface brought out on IDE pads
678c2ecf20Sopenharmony_ci * Mode 1: 001 - ata0 master <-> sata0
688c2ecf20Sopenharmony_ci *               ata1 master <-> sata1
698c2ecf20Sopenharmony_ci *               ata1 slave interface brought out on IDE pads
708c2ecf20Sopenharmony_ci * Mode 2: 010 - ata1 master <-> sata1
718c2ecf20Sopenharmony_ci *               ata1 slave  <-> sata0
728c2ecf20Sopenharmony_ci *               ata0 master and slave interfaces brought out
738c2ecf20Sopenharmony_ci *                    on IDE pads
748c2ecf20Sopenharmony_ci * Mode 3: 011 - ata0 master <-> sata0
758c2ecf20Sopenharmony_ci *               ata1 slave  <-> sata1
768c2ecf20Sopenharmony_ci *               ata1 master and slave interfaces brought out
778c2ecf20Sopenharmony_ci *                    on IDE pads
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_ci#define GEMINI_IDE_IOMUX_MASK			(7 << 24)
808c2ecf20Sopenharmony_ci#define GEMINI_IDE_IOMUX_MODE0			(0 << 24)
818c2ecf20Sopenharmony_ci#define GEMINI_IDE_IOMUX_MODE1			(1 << 24)
828c2ecf20Sopenharmony_ci#define GEMINI_IDE_IOMUX_MODE2			(2 << 24)
838c2ecf20Sopenharmony_ci#define GEMINI_IDE_IOMUX_MODE3			(3 << 24)
848c2ecf20Sopenharmony_ci#define GEMINI_IDE_IOMUX_SHIFT			(24)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/*
878c2ecf20Sopenharmony_ci * Registers directly controlling the PATA<->SATA adapters
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_ci#define GEMINI_SATA_ID				0x00
908c2ecf20Sopenharmony_ci#define GEMINI_SATA_PHY_ID			0x04
918c2ecf20Sopenharmony_ci#define GEMINI_SATA0_STATUS			0x08
928c2ecf20Sopenharmony_ci#define GEMINI_SATA1_STATUS			0x0c
938c2ecf20Sopenharmony_ci#define GEMINI_SATA0_CTRL			0x18
948c2ecf20Sopenharmony_ci#define GEMINI_SATA1_CTRL			0x1c
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define GEMINI_SATA_STATUS_BIST_DONE		BIT(5)
978c2ecf20Sopenharmony_ci#define GEMINI_SATA_STATUS_BIST_OK		BIT(4)
988c2ecf20Sopenharmony_ci#define GEMINI_SATA_STATUS_PHY_READY		BIT(0)
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_PHY_BIST_EN		BIT(14)
1018c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_PHY_FORCE_IDLE		BIT(13)
1028c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_PHY_FORCE_READY	BIT(12)
1038c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_PHY_AFE_LOOP_EN	BIT(10)
1048c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_PHY_DIG_LOOP_EN	BIT(9)
1058c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN	BIT(4)
1068c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_ATAPI_EN		BIT(3)
1078c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_BUS_WITH_20		BIT(2)
1088c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_SLAVE_EN		BIT(1)
1098c2ecf20Sopenharmony_ci#define GEMINI_SATA_CTRL_EN			BIT(0)
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci/*
1128c2ecf20Sopenharmony_ci * There is only ever one instance of this bridge on a system,
1138c2ecf20Sopenharmony_ci * so create a singleton so that the FTIDE010 instances can grab
1148c2ecf20Sopenharmony_ci * a reference to it.
1158c2ecf20Sopenharmony_ci */
1168c2ecf20Sopenharmony_cistatic struct sata_gemini *sg_singleton;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistruct sata_gemini *gemini_sata_bridge_get(void)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	if (sg_singleton)
1218c2ecf20Sopenharmony_ci		return sg_singleton;
1228c2ecf20Sopenharmony_ci	return ERR_PTR(-EPROBE_DEFER);
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gemini_sata_bridge_get);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cibool gemini_sata_bridge_enabled(struct sata_gemini *sg, bool is_ata1)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	if (!sg->sata_bridge)
1298c2ecf20Sopenharmony_ci		return false;
1308c2ecf20Sopenharmony_ci	/*
1318c2ecf20Sopenharmony_ci	 * In muxmode 2 and 3 one of the ATA controllers is
1328c2ecf20Sopenharmony_ci	 * actually not connected to any SATA bridge.
1338c2ecf20Sopenharmony_ci	 */
1348c2ecf20Sopenharmony_ci	if ((sg->muxmode == GEMINI_MUXMODE_2) &&
1358c2ecf20Sopenharmony_ci	    !is_ata1)
1368c2ecf20Sopenharmony_ci		return false;
1378c2ecf20Sopenharmony_ci	if ((sg->muxmode == GEMINI_MUXMODE_3) &&
1388c2ecf20Sopenharmony_ci	    is_ata1)
1398c2ecf20Sopenharmony_ci		return false;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	return true;
1428c2ecf20Sopenharmony_ci}
1438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gemini_sata_bridge_enabled);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cienum gemini_muxmode gemini_sata_get_muxmode(struct sata_gemini *sg)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	return sg->muxmode;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gemini_sata_get_muxmode);
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic int gemini_sata_setup_bridge(struct sata_gemini *sg,
1528c2ecf20Sopenharmony_ci				    unsigned int bridge)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	unsigned long timeout = jiffies + (HZ * 1);
1558c2ecf20Sopenharmony_ci	bool bridge_online;
1568c2ecf20Sopenharmony_ci	u32 val;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	if (bridge == 0) {
1598c2ecf20Sopenharmony_ci		val = GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN | GEMINI_SATA_CTRL_EN;
1608c2ecf20Sopenharmony_ci		/* SATA0 slave mode is only used in muxmode 2 */
1618c2ecf20Sopenharmony_ci		if (sg->muxmode == GEMINI_MUXMODE_2)
1628c2ecf20Sopenharmony_ci			val |= GEMINI_SATA_CTRL_SLAVE_EN;
1638c2ecf20Sopenharmony_ci		writel(val, sg->base + GEMINI_SATA0_CTRL);
1648c2ecf20Sopenharmony_ci	} else {
1658c2ecf20Sopenharmony_ci		val = GEMINI_SATA_CTRL_HOTPLUG_DETECT_EN | GEMINI_SATA_CTRL_EN;
1668c2ecf20Sopenharmony_ci		/* SATA1 slave mode is only used in muxmode 3 */
1678c2ecf20Sopenharmony_ci		if (sg->muxmode == GEMINI_MUXMODE_3)
1688c2ecf20Sopenharmony_ci			val |= GEMINI_SATA_CTRL_SLAVE_EN;
1698c2ecf20Sopenharmony_ci		writel(val, sg->base + GEMINI_SATA1_CTRL);
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/* Vendor code waits 10 ms here */
1738c2ecf20Sopenharmony_ci	msleep(10);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* Wait for PHY to become ready */
1768c2ecf20Sopenharmony_ci	do {
1778c2ecf20Sopenharmony_ci		msleep(100);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci		if (bridge == 0)
1808c2ecf20Sopenharmony_ci			val = readl(sg->base + GEMINI_SATA0_STATUS);
1818c2ecf20Sopenharmony_ci		else
1828c2ecf20Sopenharmony_ci			val = readl(sg->base + GEMINI_SATA1_STATUS);
1838c2ecf20Sopenharmony_ci		if (val & GEMINI_SATA_STATUS_PHY_READY)
1848c2ecf20Sopenharmony_ci			break;
1858c2ecf20Sopenharmony_ci	} while (time_before(jiffies, timeout));
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	bridge_online = !!(val & GEMINI_SATA_STATUS_PHY_READY);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	dev_info(sg->dev, "SATA%d PHY %s\n", bridge,
1908c2ecf20Sopenharmony_ci		 bridge_online ? "ready" : "not ready");
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	return bridge_online ? 0: -ENODEV;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ciint gemini_sata_start_bridge(struct sata_gemini *sg, unsigned int bridge)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct clk *pclk;
1988c2ecf20Sopenharmony_ci	int ret;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	if (bridge == 0)
2018c2ecf20Sopenharmony_ci		pclk = sg->sata0_pclk;
2028c2ecf20Sopenharmony_ci	else
2038c2ecf20Sopenharmony_ci		pclk = sg->sata1_pclk;
2048c2ecf20Sopenharmony_ci	clk_enable(pclk);
2058c2ecf20Sopenharmony_ci	msleep(10);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/* Do not keep clocking a bridge that is not online */
2088c2ecf20Sopenharmony_ci	ret = gemini_sata_setup_bridge(sg, bridge);
2098c2ecf20Sopenharmony_ci	if (ret)
2108c2ecf20Sopenharmony_ci		clk_disable(pclk);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return ret;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gemini_sata_start_bridge);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_civoid gemini_sata_stop_bridge(struct sata_gemini *sg, unsigned int bridge)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	if (bridge == 0)
2198c2ecf20Sopenharmony_ci		clk_disable(sg->sata0_pclk);
2208c2ecf20Sopenharmony_ci	else if (bridge == 1)
2218c2ecf20Sopenharmony_ci		clk_disable(sg->sata1_pclk);
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gemini_sata_stop_bridge);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ciint gemini_sata_reset_bridge(struct sata_gemini *sg,
2268c2ecf20Sopenharmony_ci			     unsigned int bridge)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	if (bridge == 0)
2298c2ecf20Sopenharmony_ci		reset_control_reset(sg->sata0_reset);
2308c2ecf20Sopenharmony_ci	else
2318c2ecf20Sopenharmony_ci		reset_control_reset(sg->sata1_reset);
2328c2ecf20Sopenharmony_ci	msleep(10);
2338c2ecf20Sopenharmony_ci	return gemini_sata_setup_bridge(sg, bridge);
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ciEXPORT_SYMBOL(gemini_sata_reset_bridge);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic int gemini_sata_bridge_init(struct sata_gemini *sg)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	struct device *dev = sg->dev;
2408c2ecf20Sopenharmony_ci	u32 sata_id, sata_phy_id;
2418c2ecf20Sopenharmony_ci	int ret;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	sg->sata0_pclk = devm_clk_get(dev, "SATA0_PCLK");
2448c2ecf20Sopenharmony_ci	if (IS_ERR(sg->sata0_pclk)) {
2458c2ecf20Sopenharmony_ci		dev_err(dev, "no SATA0 PCLK");
2468c2ecf20Sopenharmony_ci		return -ENODEV;
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci	sg->sata1_pclk = devm_clk_get(dev, "SATA1_PCLK");
2498c2ecf20Sopenharmony_ci	if (IS_ERR(sg->sata1_pclk)) {
2508c2ecf20Sopenharmony_ci		dev_err(dev, "no SATA1 PCLK");
2518c2ecf20Sopenharmony_ci		return -ENODEV;
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(sg->sata0_pclk);
2558c2ecf20Sopenharmony_ci	if (ret) {
2568c2ecf20Sopenharmony_ci		pr_err("failed to enable SATA0 PCLK\n");
2578c2ecf20Sopenharmony_ci		return ret;
2588c2ecf20Sopenharmony_ci	}
2598c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(sg->sata1_pclk);
2608c2ecf20Sopenharmony_ci	if (ret) {
2618c2ecf20Sopenharmony_ci		pr_err("failed to enable SATA1 PCLK\n");
2628c2ecf20Sopenharmony_ci		clk_disable_unprepare(sg->sata0_pclk);
2638c2ecf20Sopenharmony_ci		return ret;
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	sg->sata0_reset = devm_reset_control_get_exclusive(dev, "sata0");
2678c2ecf20Sopenharmony_ci	if (IS_ERR(sg->sata0_reset)) {
2688c2ecf20Sopenharmony_ci		dev_err(dev, "no SATA0 reset controller\n");
2698c2ecf20Sopenharmony_ci		clk_disable_unprepare(sg->sata1_pclk);
2708c2ecf20Sopenharmony_ci		clk_disable_unprepare(sg->sata0_pclk);
2718c2ecf20Sopenharmony_ci		return PTR_ERR(sg->sata0_reset);
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci	sg->sata1_reset = devm_reset_control_get_exclusive(dev, "sata1");
2748c2ecf20Sopenharmony_ci	if (IS_ERR(sg->sata1_reset)) {
2758c2ecf20Sopenharmony_ci		dev_err(dev, "no SATA1 reset controller\n");
2768c2ecf20Sopenharmony_ci		clk_disable_unprepare(sg->sata1_pclk);
2778c2ecf20Sopenharmony_ci		clk_disable_unprepare(sg->sata0_pclk);
2788c2ecf20Sopenharmony_ci		return PTR_ERR(sg->sata1_reset);
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	sata_id = readl(sg->base + GEMINI_SATA_ID);
2828c2ecf20Sopenharmony_ci	sata_phy_id = readl(sg->base + GEMINI_SATA_PHY_ID);
2838c2ecf20Sopenharmony_ci	sg->sata_bridge = true;
2848c2ecf20Sopenharmony_ci	clk_disable(sg->sata0_pclk);
2858c2ecf20Sopenharmony_ci	clk_disable(sg->sata1_pclk);
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	dev_info(dev, "SATA ID %08x, PHY ID: %08x\n", sata_id, sata_phy_id);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	return 0;
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic int gemini_setup_ide_pins(struct device *dev)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct pinctrl *p;
2958c2ecf20Sopenharmony_ci	struct pinctrl_state *ide_state;
2968c2ecf20Sopenharmony_ci	int ret;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	p = devm_pinctrl_get(dev);
2998c2ecf20Sopenharmony_ci	if (IS_ERR(p))
3008c2ecf20Sopenharmony_ci		return PTR_ERR(p);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	ide_state = pinctrl_lookup_state(p, "ide");
3038c2ecf20Sopenharmony_ci	if (IS_ERR(ide_state))
3048c2ecf20Sopenharmony_ci		return PTR_ERR(ide_state);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	ret = pinctrl_select_state(p, ide_state);
3078c2ecf20Sopenharmony_ci	if (ret) {
3088c2ecf20Sopenharmony_ci		dev_err(dev, "could not select IDE state\n");
3098c2ecf20Sopenharmony_ci		return ret;
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	return 0;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistatic int gemini_sata_probe(struct platform_device *pdev)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
3188c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
3198c2ecf20Sopenharmony_ci	struct sata_gemini *sg;
3208c2ecf20Sopenharmony_ci	struct regmap *map;
3218c2ecf20Sopenharmony_ci	struct resource *res;
3228c2ecf20Sopenharmony_ci	enum gemini_muxmode muxmode;
3238c2ecf20Sopenharmony_ci	u32 gmode;
3248c2ecf20Sopenharmony_ci	u32 gmask;
3258c2ecf20Sopenharmony_ci	int ret;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	sg = devm_kzalloc(dev, sizeof(*sg), GFP_KERNEL);
3288c2ecf20Sopenharmony_ci	if (!sg)
3298c2ecf20Sopenharmony_ci		return -ENOMEM;
3308c2ecf20Sopenharmony_ci	sg->dev = dev;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3338c2ecf20Sopenharmony_ci	if (!res)
3348c2ecf20Sopenharmony_ci		return -ENODEV;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	sg->base = devm_ioremap_resource(dev, res);
3378c2ecf20Sopenharmony_ci	if (IS_ERR(sg->base))
3388c2ecf20Sopenharmony_ci		return PTR_ERR(sg->base);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	map = syscon_regmap_lookup_by_phandle(np, "syscon");
3418c2ecf20Sopenharmony_ci	if (IS_ERR(map)) {
3428c2ecf20Sopenharmony_ci		dev_err(dev, "no global syscon\n");
3438c2ecf20Sopenharmony_ci		return PTR_ERR(map);
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	/* Set up the SATA bridge if need be */
3478c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "cortina,gemini-enable-sata-bridge")) {
3488c2ecf20Sopenharmony_ci		ret = gemini_sata_bridge_init(sg);
3498c2ecf20Sopenharmony_ci		if (ret)
3508c2ecf20Sopenharmony_ci			return ret;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	if (of_property_read_bool(np, "cortina,gemini-enable-ide-pins"))
3548c2ecf20Sopenharmony_ci		sg->ide_pins = true;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	if (!sg->sata_bridge && !sg->ide_pins) {
3578c2ecf20Sopenharmony_ci		dev_err(dev, "neither SATA bridge or IDE output enabled\n");
3588c2ecf20Sopenharmony_ci		ret = -EINVAL;
3598c2ecf20Sopenharmony_ci		goto out_unprep_clk;
3608c2ecf20Sopenharmony_ci	}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	ret = of_property_read_u32(np, "cortina,gemini-ata-muxmode", &muxmode);
3638c2ecf20Sopenharmony_ci	if (ret) {
3648c2ecf20Sopenharmony_ci		dev_err(dev, "could not parse ATA muxmode\n");
3658c2ecf20Sopenharmony_ci		goto out_unprep_clk;
3668c2ecf20Sopenharmony_ci	}
3678c2ecf20Sopenharmony_ci	if (muxmode > GEMINI_MUXMODE_3) {
3688c2ecf20Sopenharmony_ci		dev_err(dev, "illegal muxmode %d\n", muxmode);
3698c2ecf20Sopenharmony_ci		ret = -EINVAL;
3708c2ecf20Sopenharmony_ci		goto out_unprep_clk;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci	sg->muxmode = muxmode;
3738c2ecf20Sopenharmony_ci	gmask = GEMINI_IDE_IOMUX_MASK;
3748c2ecf20Sopenharmony_ci	gmode = (muxmode << GEMINI_IDE_IOMUX_SHIFT);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	ret = regmap_update_bits(map, GEMINI_GLOBAL_MISC_CTRL, gmask, gmode);
3778c2ecf20Sopenharmony_ci	if (ret) {
3788c2ecf20Sopenharmony_ci		dev_err(dev, "unable to set up IDE muxing\n");
3798c2ecf20Sopenharmony_ci		ret = -ENODEV;
3808c2ecf20Sopenharmony_ci		goto out_unprep_clk;
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	/*
3848c2ecf20Sopenharmony_ci	 * Route out the IDE pins if desired.
3858c2ecf20Sopenharmony_ci	 * This is done by looking up a special pin control state called
3868c2ecf20Sopenharmony_ci	 * "ide" that will route out the IDE pins.
3878c2ecf20Sopenharmony_ci	 */
3888c2ecf20Sopenharmony_ci	if (sg->ide_pins) {
3898c2ecf20Sopenharmony_ci		ret = gemini_setup_ide_pins(dev);
3908c2ecf20Sopenharmony_ci		if (ret)
3918c2ecf20Sopenharmony_ci			return ret;
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	dev_info(dev, "set up the Gemini IDE/SATA nexus\n");
3958c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, sg);
3968c2ecf20Sopenharmony_ci	sg_singleton = sg;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return 0;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ciout_unprep_clk:
4018c2ecf20Sopenharmony_ci	if (sg->sata_bridge) {
4028c2ecf20Sopenharmony_ci		clk_unprepare(sg->sata1_pclk);
4038c2ecf20Sopenharmony_ci		clk_unprepare(sg->sata0_pclk);
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	return ret;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic int gemini_sata_remove(struct platform_device *pdev)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	struct sata_gemini *sg = platform_get_drvdata(pdev);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	if (sg->sata_bridge) {
4138c2ecf20Sopenharmony_ci		clk_unprepare(sg->sata1_pclk);
4148c2ecf20Sopenharmony_ci		clk_unprepare(sg->sata0_pclk);
4158c2ecf20Sopenharmony_ci	}
4168c2ecf20Sopenharmony_ci	sg_singleton = NULL;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	return 0;
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic const struct of_device_id gemini_sata_of_match[] = {
4228c2ecf20Sopenharmony_ci	{
4238c2ecf20Sopenharmony_ci		.compatible = "cortina,gemini-sata-bridge",
4248c2ecf20Sopenharmony_ci	},
4258c2ecf20Sopenharmony_ci	{},
4268c2ecf20Sopenharmony_ci};
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic struct platform_driver gemini_sata_driver = {
4298c2ecf20Sopenharmony_ci	.driver = {
4308c2ecf20Sopenharmony_ci		.name = DRV_NAME,
4318c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(gemini_sata_of_match),
4328c2ecf20Sopenharmony_ci	},
4338c2ecf20Sopenharmony_ci	.probe = gemini_sata_probe,
4348c2ecf20Sopenharmony_ci	.remove = gemini_sata_remove,
4358c2ecf20Sopenharmony_ci};
4368c2ecf20Sopenharmony_cimodule_platform_driver(gemini_sata_driver);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("low level driver for Cortina Systems Gemini SATA bridge");
4398c2ecf20Sopenharmony_ciMODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>");
4408c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4418c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DRV_NAME);
442