summaryrefslogtreecommitdiffstats
path: root/drivers/i3c/master/mipi-i3c-hci/ext_caps.c
blob: 2e9b23efdc45da0a4ad1bc102300f7db024f778d (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// SPDX-License-Identifier: BSD-3-Clause
/*
 * Copyright (c) 2020, MIPI Alliance, Inc.
 *
 * Author: Nicolas Pitre <npitre@baylibre.com>
 */

#include <linux/bitfield.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/i3c/master.h>
#include <linux/kernel.h>
#include <linux/io.h>

#include "hci.h"
#include "ext_caps.h"
#include "xfer_mode_rate.h"


/* Extended Capability Header */
#define CAP_HEADER_LENGTH		GENMASK(23, 8)
#define CAP_HEADER_ID			GENMASK(7, 0)

static int hci_extcap_hardware_id(struct i3c_hci *hci, void __iomem *base)
{
	hci->vendor_mipi_id	= readl(base + 0x04);
	hci->vendor_version_id	= readl(base + 0x08);
	hci->vendor_product_id	= readl(base + 0x0c);

	dev_info(&hci->master.dev, "vendor MIPI ID: %#x\n", hci->vendor_mipi_id);
	dev_info(&hci->master.dev, "vendor version ID: %#x\n", hci->vendor_version_id);
	dev_info(&hci->master.dev, "vendor product ID: %#x\n", hci->vendor_product_id);

	/* ought to go in a table if this grows too much */
	switch (hci->vendor_mipi_id) {
	case MIPI_VENDOR_NXP:
		hci->quirks |= HCI_QUIRK_RAW_CCC;
		DBG("raw CCC quirks set");
		break;
	}

	return 0;
}

static int hci_extcap_master_config(struct i3c_hci *hci, void __iomem *base)
{
	u32 master_config = readl(base + 0x04);
	unsigned int operation_mode = FIELD_GET(GENMASK(5, 4), master_config);
	static const char * const functionality[] = {
		"(unknown)", "master only", "target only",
		"primary/secondary master" };
	dev_info(&hci->master.dev, "operation mode: %s\n", functionality[operation_mode]);
	if (operation_mode & 0x1)
		return 0;
	dev_err(&hci->master.dev, "only master mode is currently supported\n");
	return -EOPNOTSUPP;
}

static int hci_extcap_multi_bus(struct i3c_hci *hci, void __iomem *base)
{
	u32 bus_instance = readl(base + 0x04);
	unsigned int count = FIELD_GET(GENMASK(3, 0), bus_instance);

	dev_info(&hci->master.dev, "%d bus instances\n", count);
	return 0;
}

static int hci_extcap_xfer_modes(struct i3c_hci *hci, void __iomem *base)
{
	u32 header = readl(base);
	u32 entries = FIELD_GET(CAP_HEADER_LENGTH, header) - 1;
	unsigned int index;

	dev_info(&hci->master.dev, "transfer mode table has %d entries\n",
		 entries);
	base += 4;  /* skip header */
	for (index = 0; index < entries; index++) {
		u32 mode_entry = readl(base);

		DBG("mode %d: 0x%08x", index, mode_entry);
		/* TODO: will be needed when I3C core does more than SDR */
		base += 4;
	}

	return 0;
}

static int hci_extcap_xfer_rates(struct i3c_hci *hci, void __iomem *base)
{
	u32 header = readl(base);
	u32 entries = FIELD_GET(CAP_HEADER_LENGTH, header) - 1;
	u32 rate_entry;
	unsigned int index, rate, rate_id, mode_id;

	base += 4;  /* skip header */

	dev_info(&hci->master.dev, "available data rates:\n");
	for (index = 0; index < entries; index++) {
		rate_entry = readl(base);
		DBG("entry %d: 0x%08x", index, rate_entry);
		rate = FIELD_GET(XFERRATE_ACTUAL_RATE_KHZ, rate_entry);
		rate_id = FIELD_GET(XFERRATE_RATE_ID, rate_entry);
		mode_id = FIELD_GET(XFERRATE_MODE_ID, rate_entry);
		dev_info(&hci->master.dev, "rate %d for %s = %d kHz\n",
			 rate_id,
			 mode_id == XFERRATE_MODE_I3C ? "I3C" :
			 mode_id == XFERRATE_MODE_I2C ? "I2C" :
			 "unknown mode",
			 rate);
		base += 4;
	}

	return 0;
}

static int hci_extcap_auto_command(struct i3c_hci *hci, void __iomem *base)
{
	u32 autocmd_ext_caps = readl(base + 0x04);
	unsigned int max_count = FIELD_GET(GENMASK(3, 0), autocmd_ext_caps);
	u32 autocmd_ext_config = readl(base + 0x08);
	unsigned int count = FIELD_GET(GENMASK(3, 0), autocmd_ext_config);

	dev_info(&hci->master.dev, "%d/%d active auto-command entries\n",
		 count, max_count);
	/* remember auto-command register location for later use */
	hci->AUTOCMD_regs = base;
	return 0;
}

static int hci_extcap_debug(struct i3c_hci *hci, void __iomem *base)
{
	dev_info(&hci->master.dev, "debug registers present\n");
	hci->DEBUG_regs = base;
	return 0;
}

static int hci_extcap_scheduled_cmd(struct i3c_hci *hci, void __iomem *base)
{
	dev_info(&hci->master.dev, "scheduled commands available\n");
	/* hci->schedcmd_regs = base; */
	return 0;
}

static int hci_extcap_non_curr_master(struct i3c_hci *hci, void __iomem *base)
{
	dev_info(&hci->master.dev, "Non-Current Master support available\n");
	/* hci->NCM_regs = base; */
	return 0;
}

static int hci_extcap_ccc_resp_conf(struct i3c_hci *hci, void __iomem *base)
{
	dev_info(&hci->master.dev, "CCC Response Configuration available\n");
	return 0;
}

static int hci_extcap_global_DAT(struct i3c_hci *hci, void __iomem *base)
{
	dev_info(&hci->master.dev, "Global DAT available\n");
	return 0;
}

static int hci_extcap_multilane(struct i3c_hci *hci, void __iomem *base)
{
	dev_info(&hci->master.dev, "Master Multi-Lane support available\n");
	return 0;
}

static int hci_extcap_ncm_multilane(struct i3c_hci *hci, void __iomem *base)
{
	dev_info(&hci->master.dev, "NCM Multi-Lane support available\n");
	return 0;
}

struct hci_ext_caps {
	u8  id;
	u16 min_length;
	int (*parser)(struct i3c_hci *hci, void __iomem *base);
};

#define EXT_CAP(_id, _highest_mandatory_reg_offset, _parser) \
	{ .id = (_id), .parser = (_parser), \
	  .min_length = (_highest_mandatory_reg_offset)/4 + 1 }

static const struct hci_ext_caps ext_capabilities[] = {
	EXT_CAP(0x01, 0x0c, hci_extcap_hardware_id),
	EXT_CAP(0x02, 0x04, hci_extcap_master_config),
	EXT_CAP(0x03, 0x04, hci_extcap_multi_bus),
	EXT_CAP(0x04, 0x24, hci_extcap_xfer_modes),
	EXT_CAP(0x05, 0x08, hci_extcap_auto_command),
	EXT_CAP(0x08, 0x40, hci_extcap_xfer_rates),
	EXT_CAP(0x0c, 0x10, hci_extcap_debug),
	EXT_CAP(0x0d, 0x0c, hci_extcap_scheduled_cmd),
	EXT_CAP(0x0e, 0x80, hci_extcap_non_curr_master), /* TODO confirm size */
	EXT_CAP(0x0f, 0x04, hci_extcap_ccc_resp_conf),
	EXT_CAP(0x10, 0x08, hci_extcap_global_DAT),
	EXT_CAP(0x9d, 0x04,	hci_extcap_multilane),
	EXT_CAP(0x9e, 0x04, hci_extcap_ncm_multilane),
};

static int hci_extcap_vendor_NXP(struct i3c_hci *hci, void __iomem *base)
{
	hci->vendor_data = (__force void *)base;
	dev_info(&hci->master.dev, "Build Date Info = %#x\n", readl(base + 1*4));
	/* reset the FPGA */
	writel(0xdeadbeef, base + 1*4);
	return 0;
}

struct hci_ext_cap_vendor_specific {
	u32 vendor;
	u8  cap;
	u16 min_length;
	int (*parser)(struct i3c_hci *hci, void __iomem *base);
};

#define EXT_CAP_VENDOR(_vendor, _cap, _highest_mandatory_reg_offset) \
	{ .vendor = (MIPI_VENDOR_##_vendor), .cap = (_cap), \
	  .parser = (hci_extcap_vendor_##_vendor), \
	  .min_length = (_highest_mandatory_reg_offset)/4 + 1 }

static const struct hci_ext_cap_vendor_specific vendor_ext_caps[] = {
	EXT_CAP_VENDOR(NXP, 0xc0, 0x20),
};

static int hci_extcap_vendor_specific(struct i3c_hci *hci, void __iomem *base,
				      u32 cap_id, u32 cap_length)
{
	const struct hci_ext_cap_vendor_specific *vendor_cap_entry;
	int i;

	vendor_cap_entry = NULL;
	for (i = 0; i < ARRAY_SIZE(vendor_ext_caps); i++) {
		if (vendor_ext_caps[i].vendor == hci->vendor_mipi_id &&
		    vendor_ext_caps[i].cap == cap_id) {
			vendor_cap_entry = &vendor_ext_caps[i];
			break;
		}
	}

	if (!vendor_cap_entry) {
		dev_notice(&hci->master.dev,
			   "unknown ext_cap 0x%02x for vendor 0x%02x\n",
			   cap_id, hci->vendor_mipi_id);
		return 0;
	}
	if (cap_length < vendor_cap_entry->min_length) {
		dev_err(&hci->master.dev,
			"ext_cap 0x%02x has size %d (expecting >= %d)\n",
			cap_id, cap_length, vendor_cap_entry->min_length);
		return -EINVAL;
	}
	return vendor_cap_entry->parser(hci, base);
}

int i3c_hci_parse_ext_caps(struct i3c_hci *hci)
{
	void __iomem *curr_cap = hci->EXTCAPS_regs;
	void __iomem *end = curr_cap + 0x1000; /* some arbitrary limit */
	u32 cap_header, cap_id, cap_length;
	const struct hci_ext_caps *cap_entry;
	int i, err = 0;

	if (!curr_cap)
		return 0;

	for (; !err && curr_cap < end; curr_cap += cap_length * 4) {
		cap_header = readl(curr_cap);
		cap_id = FIELD_GET(CAP_HEADER_ID, cap_header);
		cap_length = FIELD_GET(CAP_HEADER_LENGTH, cap_header);
		DBG("id=0x%02x length=%d", cap_id, cap_length);
		if (!cap_length)
			break;
		if (curr_cap + cap_length * 4 >= end) {
			dev_err(&hci->master.dev,
				"ext_cap 0x%02x has size %d (too big)\n",
				cap_id, cap_length);
			err = -EINVAL;
			break;
		}

		if (cap_id >= 0xc0 && cap_id <= 0xcf) {
			err = hci_extcap_vendor_specific(hci, curr_cap,
							 cap_id, cap_length);
			continue;
		}

		cap_entry = NULL;
		for (i = 0; i < ARRAY_SIZE(ext_capabilities); i++) {
			if (ext_capabilities[i].id == cap_id) {
				cap_entry = &ext_capabilities[i];
				break;
			}
		}
		if (!cap_entry) {
			dev_notice(&hci->master.dev,
				   "unknown ext_cap 0x%02x\n", cap_id);
		} else if (cap_length < cap_entry->min_length) {
			dev_err(&hci->master.dev,
				"ext_cap 0x%02x has size %d (expecting >= %d)\n",
				cap_id, cap_length, cap_entry->min_length);
			err = -EINVAL;
		} else {
			err = cap_entry->parser(hci, curr_cap);
		}
	}
	return err;
}