summaryrefslogtreecommitdiff
path: root/drivers/reset/reset-apple-cio.c
blob: 5727c608f82f843f17389314dd171ce11b812e10 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/*
 * Apple SoC CIO (USB4/Thunderbolt) block reset driver
 *
 * Copyright The Asahi Linux Contributors
 *
 * The CIO blocks have a reset inside the power manager (PMGR) that
 * has to be deasserted before their co-processor can be booted. On
 * t8103 each port comes with a dedicated register page while t600x
 * uses a single register shared by all ports of a die inside the PMGR
 * MMIO region.
 */

#include <linux/bits.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/reset-controller.h>

#define APPLE_CIO_RESET_POLL_US 100
#define APPLE_CIO_RESET_TIMEOUT_US 100000

#define T8103_CIO_CTRL_STRIDE 0x4000
#define T8103_CIO_CTRL_INIT_REQ BIT(0)
#define T8103_CIO_CTRL_INIT_DONE BIT(2)

#define T6000_CIO_CTRL_INIT_REQ(port) BIT(port)
#define T6000_CIO_CTRL_INIT_BUSY(port) BIT(16 + (port))

struct apple_cio_reset;

struct apple_cio_reset_variant {
	unsigned int nr_resets;
	bool pmgr_child;
	int (*deassert)(struct apple_cio_reset *priv, unsigned long id);
};

struct apple_cio_reset {
	struct reset_controller_dev rcdev;
	const struct apple_cio_reset_variant *variant;
	struct regmap *regmap;
	u32 offset;
	struct mutex lock; /* serializes access to the shared t600x register */
};

static int t8103_cio_deassert(struct apple_cio_reset *priv, unsigned long id)
{
	u32 offset = priv->offset + id * T8103_CIO_CTRL_STRIDE;
	u32 val;
	int ret;

	/*
	 * After writing BIT(0) the reset is deasserted and the CIO block comes up.
	 * Once the sequence is done BIT(0) is cleared automatically and then BIT(2)
	 * is set. There's no way to assert the reset again other than cutting the power
	 * to the entire CIO block. Writing the bit again has no effect.
	 */
	ret = regmap_write(priv->regmap, offset, T8103_CIO_CTRL_INIT_REQ);
	if (ret)
		return ret;

	return regmap_read_poll_timeout(priv->regmap, offset, val,
					val == T8103_CIO_CTRL_INIT_DONE,
					APPLE_CIO_RESET_POLL_US,
					APPLE_CIO_RESET_TIMEOUT_US);
}

static int t6000_cio_deassert(struct apple_cio_reset *priv, unsigned long id)
{
	u32 val;
	int ret;

	/*
	 * Note that even the mutex is likely paranoid and not actually required
	 * here: The way this controller works is that setting bit N immediately
	 * results in bit 16+n being set. Once the reset is fully released both
	 * bits are cleared again but macOS specifically waits for the upper one
	 * to clear before continuing.
	 * Manually clearing bits is ignored and has no effect, the only way to
	 * put the block into reset again is shut down its power domains.
	 * Thus, there's no need to either RMW here or wait before polling.
	 */
	guard(mutex)(&priv->lock);

	ret = regmap_write(priv->regmap, priv->offset, T6000_CIO_CTRL_INIT_REQ(id));
	if (ret)
		return ret;

	return regmap_read_poll_timeout(priv->regmap, priv->offset, val,
					!(val & T6000_CIO_CTRL_INIT_BUSY(id)),
					APPLE_CIO_RESET_POLL_US,
					APPLE_CIO_RESET_TIMEOUT_US);
}

static const struct apple_cio_reset_variant apple_t8103_cio_reset = {
	.nr_resets = 2,
	.deassert = t8103_cio_deassert,
};

static const struct apple_cio_reset_variant apple_t6000_cio_reset = {
	.nr_resets = 4,
	.pmgr_child = true,
	.deassert = t6000_cio_deassert,
};

static int apple_cio_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
{
	struct apple_cio_reset *priv =
		container_of(rcdev, struct apple_cio_reset, rcdev);

	return priv->variant->deassert(priv, id);
}

static const struct reset_control_ops apple_cio_reset_ops = {
	.deassert = apple_cio_reset_deassert,
};

static const struct regmap_config apple_cio_reset_regmap_config = {
	.reg_bits = 32,
	.reg_stride = 4,
	.val_bits = 32,
};

static int apple_cio_reset_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct apple_cio_reset *priv;
	int ret;

	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->variant = of_device_get_match_data(dev);
	if (!priv->variant)
		return -ENODEV;

	ret = devm_mutex_init(dev, &priv->lock);
	if (ret)
		return ret;

	if (priv->variant->pmgr_child) {
		priv->regmap = syscon_node_to_regmap(dev->of_node->parent);
		if (IS_ERR(priv->regmap))
			return dev_err_probe(dev, PTR_ERR(priv->regmap),
					     "Failed to get parent regmap\n");

		ret = of_property_read_u32(dev->of_node, "reg", &priv->offset);
		if (ret)
			return dev_err_probe(dev, ret, "Failed to read reg offset\n");
	} else {
		void __iomem *base;

		base = devm_platform_ioremap_resource(pdev, 0);
		if (IS_ERR(base))
			return PTR_ERR(base);

		priv->regmap = devm_regmap_init_mmio(dev, base,
						     &apple_cio_reset_regmap_config);
		if (IS_ERR(priv->regmap))
			return dev_err_probe(dev, PTR_ERR(priv->regmap),
					     "Failed to init MMIO regmap\n");
	}

	priv->rcdev.owner = THIS_MODULE;
	priv->rcdev.ops = &apple_cio_reset_ops;
	priv->rcdev.of_node = dev->of_node;
	priv->rcdev.nr_resets = priv->variant->nr_resets;

	return devm_reset_controller_register(dev, &priv->rcdev);
}

static const struct of_device_id apple_cio_reset_match[] = {
	{
		.compatible = "apple,t8103-cio-reset",
		.data = &apple_t8103_cio_reset,
	},
	{
		.compatible = "apple,t6000-cio-reset",
		.data = &apple_t6000_cio_reset,
	},
	{},
};
MODULE_DEVICE_TABLE(of, apple_cio_reset_match);

static struct platform_driver apple_cio_reset_driver = {
	.driver = {
		.name = "apple-cio-reset",
		.of_match_table = apple_cio_reset_match,
	},
	.probe = apple_cio_reset_probe,
};
module_platform_driver(apple_cio_reset_driver);

MODULE_AUTHOR("Sven Peter <sven@kernel.org>");
MODULE_DESCRIPTION("Apple SoC CIO block reset driver");
MODULE_LICENSE("Dual MIT/GPL");