162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Samsung SoC USB 1.1/2.0 PHY driver - Exynos 4210 support
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2013 Samsung Electronics Co., Ltd.
662306a36Sopenharmony_ci * Author: Kamil Debski <k.debski@samsung.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/delay.h>
1062306a36Sopenharmony_ci#include <linux/io.h>
1162306a36Sopenharmony_ci#include <linux/phy/phy.h>
1262306a36Sopenharmony_ci#include <linux/regmap.h>
1362306a36Sopenharmony_ci#include "phy-samsung-usb2.h"
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/* Exynos USB PHY registers */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/* PHY power control */
1862306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR			0x0
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_PHY0_SUSPEND	BIT(0)
2162306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_PHY0_PWR		BIT(3)
2262306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_PHY0_OTG_PWR	BIT(4)
2362306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_PHY0_SLEEP		BIT(5)
2462306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_PHY0	( \
2562306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_PHY0_SUSPEND | \
2662306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_PHY0_PWR | \
2762306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_PHY0_OTG_PWR | \
2862306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_PHY0_SLEEP)
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_PHY1_SUSPEND	BIT(6)
3162306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_PHY1_PWR		BIT(7)
3262306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_PHY1_SLEEP		BIT(8)
3362306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_PHY1 ( \
3462306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_PHY1_SUSPEND | \
3562306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_PHY1_PWR | \
3662306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_PHY1_SLEEP)
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_HSIC0_SUSPEND	BIT(9)
3962306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_HSIC0_SLEEP		BIT(10)
4062306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_HSIC0 ( \
4162306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_HSIC0_SUSPEND | \
4262306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_HSIC0_SLEEP)
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_HSIC1_SUSPEND	BIT(11)
4562306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_HSIC1_SLEEP		BIT(12)
4662306a36Sopenharmony_ci#define EXYNOS_4210_UPHYPWR_HSIC1 ( \
4762306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_HSIC1_SUSPEND | \
4862306a36Sopenharmony_ci	EXYNOS_4210_UPHYPWR_HSIC1_SLEEP)
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/* PHY clock control */
5162306a36Sopenharmony_ci#define EXYNOS_4210_UPHYCLK			0x4
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#define EXYNOS_4210_UPHYCLK_PHYFSEL_MASK	(0x3 << 0)
5462306a36Sopenharmony_ci#define EXYNOS_4210_UPHYCLK_PHYFSEL_OFFSET	0
5562306a36Sopenharmony_ci#define EXYNOS_4210_UPHYCLK_PHYFSEL_48MHZ	(0x0 << 0)
5662306a36Sopenharmony_ci#define EXYNOS_4210_UPHYCLK_PHYFSEL_24MHZ	(0x3 << 0)
5762306a36Sopenharmony_ci#define EXYNOS_4210_UPHYCLK_PHYFSEL_12MHZ	(0x2 << 0)
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci#define EXYNOS_4210_UPHYCLK_PHY0_ID_PULLUP	BIT(2)
6062306a36Sopenharmony_ci#define EXYNOS_4210_UPHYCLK_PHY0_COMMON_ON	BIT(4)
6162306a36Sopenharmony_ci#define EXYNOS_4210_UPHYCLK_PHY1_COMMON_ON	BIT(7)
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* PHY reset control */
6462306a36Sopenharmony_ci#define EXYNOS_4210_UPHYRST			0x8
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_PHY0		BIT(0)
6762306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_OTG_HLINK		BIT(1)
6862306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_OTG_PHYLINK		BIT(2)
6962306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_PHY1_ALL		BIT(3)
7062306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_PHY1_P0		BIT(4)
7162306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_PHY1_P1P2		BIT(5)
7262306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_HOST_LINK_ALL	BIT(6)
7362306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_HOST_LINK_P0	BIT(7)
7462306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_HOST_LINK_P1	BIT(8)
7562306a36Sopenharmony_ci#define EXYNOS_4210_URSTCON_HOST_LINK_P2	BIT(9)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/* Isolation, configured in the power management unit */
7862306a36Sopenharmony_ci#define EXYNOS_4210_USB_ISOL_DEVICE_OFFSET	0x704
7962306a36Sopenharmony_ci#define EXYNOS_4210_USB_ISOL_DEVICE		BIT(0)
8062306a36Sopenharmony_ci#define EXYNOS_4210_USB_ISOL_HOST_OFFSET	0x708
8162306a36Sopenharmony_ci#define EXYNOS_4210_USB_ISOL_HOST		BIT(0)
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/* USBYPHY1 Floating prevention */
8462306a36Sopenharmony_ci#define EXYNOS_4210_UPHY1CON			0x34
8562306a36Sopenharmony_ci#define EXYNOS_4210_UPHY1CON_FLOAT_PREVENTION	0x1
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci/* Mode switching SUB Device <-> Host */
8862306a36Sopenharmony_ci#define EXYNOS_4210_MODE_SWITCH_OFFSET		0x21c
8962306a36Sopenharmony_ci#define EXYNOS_4210_MODE_SWITCH_MASK		1
9062306a36Sopenharmony_ci#define EXYNOS_4210_MODE_SWITCH_DEVICE		0
9162306a36Sopenharmony_ci#define EXYNOS_4210_MODE_SWITCH_HOST		1
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_cienum exynos4210_phy_id {
9462306a36Sopenharmony_ci	EXYNOS4210_DEVICE,
9562306a36Sopenharmony_ci	EXYNOS4210_HOST,
9662306a36Sopenharmony_ci	EXYNOS4210_HSIC0,
9762306a36Sopenharmony_ci	EXYNOS4210_HSIC1,
9862306a36Sopenharmony_ci	EXYNOS4210_NUM_PHYS,
9962306a36Sopenharmony_ci};
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/*
10262306a36Sopenharmony_ci * exynos4210_rate_to_clk() converts the supplied clock rate to the value that
10362306a36Sopenharmony_ci * can be written to the phy register.
10462306a36Sopenharmony_ci */
10562306a36Sopenharmony_cistatic int exynos4210_rate_to_clk(unsigned long rate, u32 *reg)
10662306a36Sopenharmony_ci{
10762306a36Sopenharmony_ci	switch (rate) {
10862306a36Sopenharmony_ci	case 12 * MHZ:
10962306a36Sopenharmony_ci		*reg = EXYNOS_4210_UPHYCLK_PHYFSEL_12MHZ;
11062306a36Sopenharmony_ci		break;
11162306a36Sopenharmony_ci	case 24 * MHZ:
11262306a36Sopenharmony_ci		*reg = EXYNOS_4210_UPHYCLK_PHYFSEL_24MHZ;
11362306a36Sopenharmony_ci		break;
11462306a36Sopenharmony_ci	case 48 * MHZ:
11562306a36Sopenharmony_ci		*reg = EXYNOS_4210_UPHYCLK_PHYFSEL_48MHZ;
11662306a36Sopenharmony_ci		break;
11762306a36Sopenharmony_ci	default:
11862306a36Sopenharmony_ci		return -EINVAL;
11962306a36Sopenharmony_ci	}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	return 0;
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistatic void exynos4210_isol(struct samsung_usb2_phy_instance *inst, bool on)
12562306a36Sopenharmony_ci{
12662306a36Sopenharmony_ci	struct samsung_usb2_phy_driver *drv = inst->drv;
12762306a36Sopenharmony_ci	u32 offset;
12862306a36Sopenharmony_ci	u32 mask;
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	switch (inst->cfg->id) {
13162306a36Sopenharmony_ci	case EXYNOS4210_DEVICE:
13262306a36Sopenharmony_ci		offset = EXYNOS_4210_USB_ISOL_DEVICE_OFFSET;
13362306a36Sopenharmony_ci		mask = EXYNOS_4210_USB_ISOL_DEVICE;
13462306a36Sopenharmony_ci		break;
13562306a36Sopenharmony_ci	case EXYNOS4210_HOST:
13662306a36Sopenharmony_ci		offset = EXYNOS_4210_USB_ISOL_HOST_OFFSET;
13762306a36Sopenharmony_ci		mask = EXYNOS_4210_USB_ISOL_HOST;
13862306a36Sopenharmony_ci		break;
13962306a36Sopenharmony_ci	default:
14062306a36Sopenharmony_ci		return;
14162306a36Sopenharmony_ci	}
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	regmap_update_bits(drv->reg_pmu, offset, mask, on ? 0 : mask);
14462306a36Sopenharmony_ci}
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_cistatic void exynos4210_phy_pwr(struct samsung_usb2_phy_instance *inst, bool on)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	struct samsung_usb2_phy_driver *drv = inst->drv;
14962306a36Sopenharmony_ci	u32 rstbits = 0;
15062306a36Sopenharmony_ci	u32 phypwr = 0;
15162306a36Sopenharmony_ci	u32 rst;
15262306a36Sopenharmony_ci	u32 pwr;
15362306a36Sopenharmony_ci	u32 clk;
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	switch (inst->cfg->id) {
15662306a36Sopenharmony_ci	case EXYNOS4210_DEVICE:
15762306a36Sopenharmony_ci		phypwr =	EXYNOS_4210_UPHYPWR_PHY0;
15862306a36Sopenharmony_ci		rstbits =	EXYNOS_4210_URSTCON_PHY0;
15962306a36Sopenharmony_ci		break;
16062306a36Sopenharmony_ci	case EXYNOS4210_HOST:
16162306a36Sopenharmony_ci		phypwr =	EXYNOS_4210_UPHYPWR_PHY1;
16262306a36Sopenharmony_ci		rstbits =	EXYNOS_4210_URSTCON_PHY1_ALL |
16362306a36Sopenharmony_ci				EXYNOS_4210_URSTCON_PHY1_P0 |
16462306a36Sopenharmony_ci				EXYNOS_4210_URSTCON_PHY1_P1P2 |
16562306a36Sopenharmony_ci				EXYNOS_4210_URSTCON_HOST_LINK_ALL |
16662306a36Sopenharmony_ci				EXYNOS_4210_URSTCON_HOST_LINK_P0;
16762306a36Sopenharmony_ci		writel(on, drv->reg_phy + EXYNOS_4210_UPHY1CON);
16862306a36Sopenharmony_ci		break;
16962306a36Sopenharmony_ci	case EXYNOS4210_HSIC0:
17062306a36Sopenharmony_ci		phypwr =	EXYNOS_4210_UPHYPWR_HSIC0;
17162306a36Sopenharmony_ci		rstbits =	EXYNOS_4210_URSTCON_PHY1_P1P2 |
17262306a36Sopenharmony_ci				EXYNOS_4210_URSTCON_HOST_LINK_P1;
17362306a36Sopenharmony_ci		break;
17462306a36Sopenharmony_ci	case EXYNOS4210_HSIC1:
17562306a36Sopenharmony_ci		phypwr =	EXYNOS_4210_UPHYPWR_HSIC1;
17662306a36Sopenharmony_ci		rstbits =	EXYNOS_4210_URSTCON_PHY1_P1P2 |
17762306a36Sopenharmony_ci				EXYNOS_4210_URSTCON_HOST_LINK_P2;
17862306a36Sopenharmony_ci		break;
17962306a36Sopenharmony_ci	}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	if (on) {
18262306a36Sopenharmony_ci		clk = readl(drv->reg_phy + EXYNOS_4210_UPHYCLK);
18362306a36Sopenharmony_ci		clk &= ~EXYNOS_4210_UPHYCLK_PHYFSEL_MASK;
18462306a36Sopenharmony_ci		clk |= drv->ref_reg_val << EXYNOS_4210_UPHYCLK_PHYFSEL_OFFSET;
18562306a36Sopenharmony_ci		writel(clk, drv->reg_phy + EXYNOS_4210_UPHYCLK);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci		pwr = readl(drv->reg_phy + EXYNOS_4210_UPHYPWR);
18862306a36Sopenharmony_ci		pwr &= ~phypwr;
18962306a36Sopenharmony_ci		writel(pwr, drv->reg_phy + EXYNOS_4210_UPHYPWR);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci		rst = readl(drv->reg_phy + EXYNOS_4210_UPHYRST);
19262306a36Sopenharmony_ci		rst |= rstbits;
19362306a36Sopenharmony_ci		writel(rst, drv->reg_phy + EXYNOS_4210_UPHYRST);
19462306a36Sopenharmony_ci		udelay(10);
19562306a36Sopenharmony_ci		rst &= ~rstbits;
19662306a36Sopenharmony_ci		writel(rst, drv->reg_phy + EXYNOS_4210_UPHYRST);
19762306a36Sopenharmony_ci		/* The following delay is necessary for the reset sequence to be
19862306a36Sopenharmony_ci		 * completed */
19962306a36Sopenharmony_ci		udelay(80);
20062306a36Sopenharmony_ci	} else {
20162306a36Sopenharmony_ci		pwr = readl(drv->reg_phy + EXYNOS_4210_UPHYPWR);
20262306a36Sopenharmony_ci		pwr |= phypwr;
20362306a36Sopenharmony_ci		writel(pwr, drv->reg_phy + EXYNOS_4210_UPHYPWR);
20462306a36Sopenharmony_ci	}
20562306a36Sopenharmony_ci}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_cistatic int exynos4210_power_on(struct samsung_usb2_phy_instance *inst)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	/* Order of initialisation is important - first power then isolation */
21062306a36Sopenharmony_ci	exynos4210_phy_pwr(inst, 1);
21162306a36Sopenharmony_ci	exynos4210_isol(inst, 0);
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	return 0;
21462306a36Sopenharmony_ci}
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_cistatic int exynos4210_power_off(struct samsung_usb2_phy_instance *inst)
21762306a36Sopenharmony_ci{
21862306a36Sopenharmony_ci	exynos4210_isol(inst, 1);
21962306a36Sopenharmony_ci	exynos4210_phy_pwr(inst, 0);
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	return 0;
22262306a36Sopenharmony_ci}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cistatic const struct samsung_usb2_common_phy exynos4210_phys[] = {
22662306a36Sopenharmony_ci	{
22762306a36Sopenharmony_ci		.label		= "device",
22862306a36Sopenharmony_ci		.id		= EXYNOS4210_DEVICE,
22962306a36Sopenharmony_ci		.power_on	= exynos4210_power_on,
23062306a36Sopenharmony_ci		.power_off	= exynos4210_power_off,
23162306a36Sopenharmony_ci	},
23262306a36Sopenharmony_ci	{
23362306a36Sopenharmony_ci		.label		= "host",
23462306a36Sopenharmony_ci		.id		= EXYNOS4210_HOST,
23562306a36Sopenharmony_ci		.power_on	= exynos4210_power_on,
23662306a36Sopenharmony_ci		.power_off	= exynos4210_power_off,
23762306a36Sopenharmony_ci	},
23862306a36Sopenharmony_ci	{
23962306a36Sopenharmony_ci		.label		= "hsic0",
24062306a36Sopenharmony_ci		.id		= EXYNOS4210_HSIC0,
24162306a36Sopenharmony_ci		.power_on	= exynos4210_power_on,
24262306a36Sopenharmony_ci		.power_off	= exynos4210_power_off,
24362306a36Sopenharmony_ci	},
24462306a36Sopenharmony_ci	{
24562306a36Sopenharmony_ci		.label		= "hsic1",
24662306a36Sopenharmony_ci		.id		= EXYNOS4210_HSIC1,
24762306a36Sopenharmony_ci		.power_on	= exynos4210_power_on,
24862306a36Sopenharmony_ci		.power_off	= exynos4210_power_off,
24962306a36Sopenharmony_ci	},
25062306a36Sopenharmony_ci};
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ciconst struct samsung_usb2_phy_config exynos4210_usb2_phy_config = {
25362306a36Sopenharmony_ci	.has_mode_switch	= 0,
25462306a36Sopenharmony_ci	.num_phys		= EXYNOS4210_NUM_PHYS,
25562306a36Sopenharmony_ci	.phys			= exynos4210_phys,
25662306a36Sopenharmony_ci	.rate_to_clk		= exynos4210_rate_to_clk,
25762306a36Sopenharmony_ci};
258