| 1 |
// SPDX-License-Identifier: BSD-3-Clause |
| 2 |
/* |
| 3 |
* Copyright (c) 2020, MIPI Alliance, Inc. |
| 4 |
* |
| 5 |
* Author: Nicolas Pitre <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <linux/bitfield.h> |
| 9 |
#include <linux/device.h> |
| 10 |
#include <linux/errno.h> |
| 11 |
#include <linux/i3c/master.h> |
| 12 |
#include <linux/kernel.h> |
| 13 |
#include <linux/io.h> |
| 14 |
|
| 15 |
#include "hci.h" |
| 16 |
#include "ext_caps.h" |
| 17 |
#include "xfer_mode_rate.h" |
| 18 |
|
| 19 |
/* Extended Capability Header */ |
| 20 |
#define CAP_HEADER_LENGTH GENMASK(23, 8) |
| 21 |
#define CAP_HEADER_ID GENMASK(7, 0) |
| 22 |
|
| 23 |
static int hci_extcap_hardware_id(struct i3c_hci *hci, void __iomem *base) |
| 24 |
{ |
| 25 |
hci->vendor_mipi_id = readl(base + 0x04); |
| 26 |
hci->vendor_version_id = readl(base + 0x08); |
| 27 |
hci->vendor_product_id = readl(base + 0x0c); |
| 28 |
|
| 29 |
dev_dbg(&hci->master.dev, "vendor MIPI ID: %#x\n", hci->vendor_mipi_id); |
| 30 |
dev_dbg(&hci->master.dev, "vendor version ID: %#x\n", hci->vendor_version_id); |
| 31 |
dev_dbg(&hci->master.dev, "vendor product ID: %#x\n", hci->vendor_product_id); |
| 32 |
|
| 33 |
/* ought to go in a table if this grows too much */ |
| 34 |
switch (hci->vendor_mipi_id) { |
| 35 |
case MIPI_VENDOR_NXP: |
| 36 |
hci->quirks |= HCI_QUIRK_RAW_CCC; |
| 37 |
dev_dbg(&hci->master.dev, "raw CCC quirks set"); |
| 38 |
break; |
| 39 |
} |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
static int hci_extcap_master_config(struct i3c_hci *hci, void __iomem *base) |
| 45 |
{ |
| 46 |
u32 master_config = readl(base + 0x04); |
| 47 |
unsigned int operation_mode = FIELD_GET(GENMASK(5, 4), master_config); |
| 48 |
static const char * const functionality[] = { |
| 49 |
"(unknown)", "master only", "target only", |
| 50 |
"primary/secondary master" }; |
| 51 |
dev_dbg(&hci->master.dev, "operation mode: %s\n", functionality[operation_mode]); |
| 52 |
if (operation_mode & 0x1) |
| 53 |
return 0; |
| 54 |
dev_err(&hci->master.dev, "only master mode is currently supported\n"); |
| 55 |
return -EOPNOTSUPP; |
| 56 |
} |
| 57 |
|
| 58 |
static int hci_extcap_multi_bus(struct i3c_hci *hci, void __iomem *base) |
| 59 |
{ |
| 60 |
u32 bus_instance = readl(base + 0x04); |
| 61 |
unsigned int count = FIELD_GET(GENMASK(3, 0), bus_instance); |
| 62 |
|
| 63 |
dev_dbg(&hci->master.dev, "%d bus instances\n", count); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
static int hci_extcap_xfer_modes(struct i3c_hci *hci, void __iomem *base) |
| 68 |
{ |
| 69 |
u32 header = readl(base); |
| 70 |
u32 entries = FIELD_GET(CAP_HEADER_LENGTH, header) - 1; |
| 71 |
unsigned int index; |
| 72 |
|
| 73 |
dev_dbg(&hci->master.dev, "transfer mode table has %d entries\n", entries); |
| 74 |
base += 4; /* skip header */ |
| 75 |
for (index = 0; index < entries; index++) { |
| 76 |
u32 mode_entry = readl(base); |
| 77 |
|
| 78 |
dev_dbg(&hci->master.dev, "mode %d: 0x%08x", |
| 79 |
index, mode_entry); |
| 80 |
/* TODO: will be needed when I3C core does more than SDR */ |
| 81 |
base += 4; |
| 82 |
} |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
static int hci_extcap_xfer_rates(struct i3c_hci *hci, void __iomem *base) |
| 88 |
{ |
| 89 |
u32 header = readl(base); |
| 90 |
u32 entries = FIELD_GET(CAP_HEADER_LENGTH, header) - 1; |
| 91 |
u32 rate_entry; |
| 92 |
unsigned int index, rate, rate_id, mode_id; |
| 93 |
|
| 94 |
base += 4; /* skip header */ |
| 95 |
|
| 96 |
dev_dbg(&hci->master.dev, "available data rates:\n"); |
| 97 |
for (index = 0; index < entries; index++) { |
| 98 |
rate_entry = readl(base); |
| 99 |
dev_dbg(&hci->master.dev, "entry %d: 0x%08x", |
| 100 |
index, rate_entry); |
| 101 |
rate = FIELD_GET(XFERRATE_ACTUAL_RATE_KHZ, rate_entry); |
| 102 |
rate_id = FIELD_GET(XFERRATE_RATE_ID, rate_entry); |
| 103 |
mode_id = FIELD_GET(XFERRATE_MODE_ID, rate_entry); |
| 104 |
dev_dbg(&hci->master.dev, "rate %d for %s = %d kHz\n", |
| 105 |
rate_id, |
| 106 |
mode_id == XFERRATE_MODE_I3C ? "I3C" : |
| 107 |
mode_id == XFERRATE_MODE_I2C ? "I2C" : |
| 108 |
"unknown mode", |
| 109 |
rate); |
| 110 |
base += 4; |
| 111 |
} |
| 112 |
|
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
static int hci_extcap_auto_command(struct i3c_hci *hci, void __iomem *base) |
| 117 |
{ |
| 118 |
u32 autocmd_ext_caps = readl(base + 0x04); |
| 119 |
unsigned int max_count = FIELD_GET(GENMASK(3, 0), autocmd_ext_caps); |
| 120 |
u32 autocmd_ext_config = readl(base + 0x08); |
| 121 |
unsigned int count = FIELD_GET(GENMASK(3, 0), autocmd_ext_config); |
| 122 |
|
| 123 |
dev_dbg(&hci->master.dev, "%d/%d active auto-command entries\n", |
| 124 |
count, max_count); |
| 125 |
/* remember auto-command register location for later use */ |
| 126 |
hci->AUTOCMD_regs = base; |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
static int hci_extcap_debug(struct i3c_hci *hci, void __iomem *base) |
| 131 |
{ |
| 132 |
dev_dbg(&hci->master.dev, "debug registers present\n"); |
| 133 |
hci->DEBUG_regs = base; |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static int hci_extcap_scheduled_cmd(struct i3c_hci *hci, void __iomem *base) |
| 138 |
{ |
| 139 |
dev_dbg(&hci->master.dev, "scheduled commands available\n"); |
| 140 |
/* hci->schedcmd_regs = base; */ |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
static int hci_extcap_non_curr_master(struct i3c_hci *hci, void __iomem *base) |
| 145 |
{ |
| 146 |
dev_dbg(&hci->master.dev, "Non-Current Master support available\n"); |
| 147 |
/* hci->NCM_regs = base; */ |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
static int hci_extcap_ccc_resp_conf(struct i3c_hci *hci, void __iomem *base) |
| 152 |
{ |
| 153 |
dev_dbg(&hci->master.dev, "CCC Response Configuration available\n"); |
| 154 |
return 0; |
| 155 |
} |
| 156 |
|
| 157 |
static int hci_extcap_global_DAT(struct i3c_hci *hci, void __iomem *base) |
| 158 |
{ |
| 159 |
dev_dbg(&hci->master.dev, "Global DAT available\n"); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static int hci_extcap_multilane(struct i3c_hci *hci, void __iomem *base) |
| 164 |
{ |
| 165 |
dev_dbg(&hci->master.dev, "Master Multi-Lane support available\n"); |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
static int hci_extcap_ncm_multilane(struct i3c_hci *hci, void __iomem *base) |
| 170 |
{ |
| 171 |
dev_dbg(&hci->master.dev, "NCM Multi-Lane support available\n"); |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
struct hci_ext_caps { |
| 176 |
u8 id; |
| 177 |
u16 min_length; |
| 178 |
int (*parser)(struct i3c_hci *hci, void __iomem *base); |
| 179 |
}; |
| 180 |
|
| 181 |
#define EXT_CAP(_id, _highest_mandatory_reg_offset, _parser) \ |
| 182 |
{ .id = (_id), .parser = (_parser), \ |
| 183 |
.min_length = (_highest_mandatory_reg_offset)/4 + 1 } |
| 184 |
|
| 185 |
static const struct hci_ext_caps ext_capabilities[] = { |
| 186 |
EXT_CAP(0x01, 0x0c, hci_extcap_hardware_id), |
| 187 |
EXT_CAP(0x02, 0x04, hci_extcap_master_config), |
| 188 |
EXT_CAP(0x03, 0x04, hci_extcap_multi_bus), |
| 189 |
EXT_CAP(0x04, 0x24, hci_extcap_xfer_modes), |
| 190 |
EXT_CAP(0x05, 0x08, hci_extcap_auto_command), |
| 191 |
EXT_CAP(0x08, 0x40, hci_extcap_xfer_rates), |
| 192 |
EXT_CAP(0x0c, 0x10, hci_extcap_debug), |
| 193 |
EXT_CAP(0x0d, 0x0c, hci_extcap_scheduled_cmd), |
| 194 |
EXT_CAP(0x0e, 0x80, hci_extcap_non_curr_master), /* TODO confirm size */ |
| 195 |
EXT_CAP(0x0f, 0x04, hci_extcap_ccc_resp_conf), |
| 196 |
EXT_CAP(0x10, 0x08, hci_extcap_global_DAT), |
| 197 |
EXT_CAP(0x9d, 0x04, hci_extcap_multilane), |
| 198 |
EXT_CAP(0x9e, 0x04, hci_extcap_ncm_multilane), |
| 199 |
}; |
| 200 |
|
| 201 |
static int hci_extcap_vendor_NXP(struct i3c_hci *hci, void __iomem *base) |
| 202 |
{ |
| 203 |
hci->vendor_data = (__force void *)base; |
| 204 |
dev_dbg(&hci->master.dev, "Build Date Info = %#x\n", readl(base + 1 * 4)); |
| 205 |
/* reset the FPGA */ |
| 206 |
writel(0xdeadbeef, base + 1*4); |
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
struct hci_ext_cap_vendor_specific { |
| 211 |
u32 vendor; |
| 212 |
u8 cap; |
| 213 |
u16 min_length; |
| 214 |
int (*parser)(struct i3c_hci *hci, void __iomem *base); |
| 215 |
}; |
| 216 |
|
| 217 |
#define EXT_CAP_VENDOR(_vendor, _cap, _highest_mandatory_reg_offset) \ |
| 218 |
{ .vendor = (MIPI_VENDOR_##_vendor), .cap = (_cap), \ |
| 219 |
.parser = (hci_extcap_vendor_##_vendor), \ |
| 220 |
.min_length = (_highest_mandatory_reg_offset)/4 + 1 } |
| 221 |
|
| 222 |
static const struct hci_ext_cap_vendor_specific vendor_ext_caps[] = { |
| 223 |
EXT_CAP_VENDOR(NXP, 0xc0, 0x20), |
| 224 |
}; |
| 225 |
|
| 226 |
static int hci_extcap_vendor_specific(struct i3c_hci *hci, void __iomem *base, |
| 227 |
u32 cap_id, u32 cap_length) |
| 228 |
{ |
| 229 |
const struct hci_ext_cap_vendor_specific *vendor_cap_entry; |
| 230 |
int i; |
| 231 |
|
| 232 |
vendor_cap_entry = NULL; |
| 233 |
for (i = 0; i < ARRAY_SIZE(vendor_ext_caps); i++) { |
| 234 |
if (vendor_ext_caps[i].vendor == hci->vendor_mipi_id && |
| 235 |
vendor_ext_caps[i].cap == cap_id) { |
| 236 |
vendor_cap_entry = &vendor_ext_caps[i]; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if (!vendor_cap_entry) { |
| 242 |
dev_dbg(&hci->master.dev, "unknown ext_cap 0x%02x for vendor 0x%02x\n", |
| 243 |
cap_id, hci->vendor_mipi_id); |
| 244 |
return 0; |
| 245 |
} |
| 246 |
if (cap_length < vendor_cap_entry->min_length) { |
| 247 |
dev_err(&hci->master.dev, |
| 248 |
"ext_cap 0x%02x has size %d (expecting >= %d)\n", |
| 249 |
cap_id, cap_length, vendor_cap_entry->min_length); |
| 250 |
return -EINVAL; |
| 251 |
} |
| 252 |
return vendor_cap_entry->parser(hci, base); |
| 253 |
} |
| 254 |
|
| 255 |
int i3c_hci_parse_ext_caps(struct i3c_hci *hci) |
| 256 |
{ |
| 257 |
void __iomem *curr_cap = hci->EXTCAPS_regs; |
| 258 |
void __iomem *end = curr_cap + 0x1000; /* some arbitrary limit */ |
| 259 |
u32 cap_header, cap_id, cap_length; |
| 260 |
const struct hci_ext_caps *cap_entry; |
| 261 |
int i, err = 0; |
| 262 |
|
| 263 |
if (!curr_cap) |
| 264 |
return 0; |
| 265 |
|
| 266 |
for (; !err && curr_cap < end; curr_cap += cap_length * 4) { |
| 267 |
cap_header = readl(curr_cap); |
| 268 |
cap_id = FIELD_GET(CAP_HEADER_ID, cap_header); |
| 269 |
cap_length = FIELD_GET(CAP_HEADER_LENGTH, cap_header); |
| 270 |
dev_dbg(&hci->master.dev, "id=0x%02x length=%d", |
| 271 |
cap_id, cap_length); |
| 272 |
if (!cap_id || !cap_length) |
| 273 |
break; |
| 274 |
if (curr_cap + cap_length * 4 >= end) { |
| 275 |
dev_err(&hci->master.dev, |
| 276 |
"ext_cap 0x%02x has size %d (too big)\n", |
| 277 |
cap_id, cap_length); |
| 278 |
err = -EINVAL; |
| 279 |
break; |
| 280 |
} |
| 281 |
|
| 282 |
if (cap_id >= 0xc0 && cap_id <= 0xcf) { |
| 283 |
err = hci_extcap_vendor_specific(hci, curr_cap, |
| 284 |
cap_id, cap_length); |
| 285 |
continue; |
| 286 |
} |
| 287 |
|
| 288 |
cap_entry = NULL; |
| 289 |
for (i = 0; i < ARRAY_SIZE(ext_capabilities); i++) { |
| 290 |
if (ext_capabilities[i].id == cap_id) { |
| 291 |
cap_entry = &ext_capabilities[i]; |
| 292 |
break; |
| 293 |
} |
| 294 |
} |
| 295 |
if (!cap_entry) { |
| 296 |
dev_dbg(&hci->master.dev, "unknown ext_cap 0x%02x\n", cap_id); |
| 297 |
} else if (cap_length < cap_entry->min_length) { |
| 298 |
dev_err(&hci->master.dev, |
| 299 |
"ext_cap 0x%02x has size %d (expecting >= %d)\n", |
| 300 |
cap_id, cap_length, cap_entry->min_length); |
| 301 |
err = -EINVAL; |
| 302 |
} else { |
| 303 |
err = cap_entry->parser(hci, curr_cap); |
| 304 |
} |
| 305 |
} |
| 306 |
return err; |
| 307 |
} |