| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* PCI glue code for MIPI I3C HCI driver |
| 4 |
* |
| 5 |
* Copyright (C) 2024 Intel Corporation |
| 6 |
* |
| 7 |
* Author: Jarkko Nikula <[email protected]> |
| 8 |
*/ |
| 9 |
#include <linux/acpi.h> |
| 10 |
#include <linux/bitfield.h> |
| 11 |
#include <linux/debugfs.h> |
| 12 |
#include <linux/idr.h> |
| 13 |
#include <linux/iopoll.h> |
| 14 |
#include <linux/kernel.h> |
| 15 |
#include <linux/mfd/core.h> |
| 16 |
#include <linux/module.h> |
| 17 |
#include <linux/pci.h> |
| 18 |
#include <linux/platform_data/mipi-i3c-hci.h> |
| 19 |
#include <linux/platform_device.h> |
| 20 |
#include <linux/pm_qos.h> |
| 21 |
#include <linux/pm_runtime.h> |
| 22 |
|
| 23 |
/* |
| 24 |
* There can up to 15 instances, but implementations have at most 2 at this |
| 25 |
* time. |
| 26 |
*/ |
| 27 |
#define INST_MAX 2 |
| 28 |
|
| 29 |
struct mipi_i3c_hci_pci { |
| 30 |
struct pci_dev *pci; |
| 31 |
void __iomem *base; |
| 32 |
const struct mipi_i3c_hci_pci_info *info; |
| 33 |
void *private; |
| 34 |
}; |
| 35 |
|
| 36 |
struct mipi_i3c_hci_pci_info { |
| 37 |
int (*init)(struct mipi_i3c_hci_pci *hci); |
| 38 |
void (*exit)(struct mipi_i3c_hci_pci *hci); |
| 39 |
const char *name; |
| 40 |
int id[INST_MAX]; |
| 41 |
u32 instance_offset[INST_MAX]; |
| 42 |
int instance_count; |
| 43 |
}; |
| 44 |
|
| 45 |
#define INTEL_PRIV_OFFSET 0x2b0 |
| 46 |
#define INTEL_RESETS 0x04 |
| 47 |
#define INTEL_RESETS_RESET BIT(0) |
| 48 |
#define INTEL_RESETS_RESET_DONE BIT(1) |
| 49 |
#define INTEL_RESETS_TIMEOUT_US (10 * USEC_PER_MSEC) |
| 50 |
|
| 51 |
#define INTEL_ACTIVELTR 0x0c |
| 52 |
#define INTEL_IDLELTR 0x10 |
| 53 |
|
| 54 |
#define INTEL_LTR_REQ BIT(15) |
| 55 |
#define INTEL_LTR_SCALE_MASK GENMASK(11, 10) |
| 56 |
#define INTEL_LTR_SCALE_1US FIELD_PREP(INTEL_LTR_SCALE_MASK, 2) |
| 57 |
#define INTEL_LTR_SCALE_32US FIELD_PREP(INTEL_LTR_SCALE_MASK, 3) |
| 58 |
#define INTEL_LTR_VALUE_MASK GENMASK(9, 0) |
| 59 |
|
| 60 |
struct intel_host { |
| 61 |
void __iomem *priv; |
| 62 |
u32 active_ltr; |
| 63 |
u32 idle_ltr; |
| 64 |
struct dentry *debugfs_root; |
| 65 |
}; |
| 66 |
|
| 67 |
static void intel_cache_ltr(struct intel_host *host) |
| 68 |
{ |
| 69 |
host->active_ltr = readl(host->priv + INTEL_ACTIVELTR); |
| 70 |
host->idle_ltr = readl(host->priv + INTEL_IDLELTR); |
| 71 |
} |
| 72 |
|
| 73 |
static void intel_ltr_set(struct device *dev, s32 val) |
| 74 |
{ |
| 75 |
struct mipi_i3c_hci_pci *hci = dev_get_drvdata(dev); |
| 76 |
struct intel_host *host = hci->private; |
| 77 |
u32 ltr; |
| 78 |
|
| 79 |
/* |
| 80 |
* Program latency tolerance (LTR) accordingly what has been asked |
| 81 |
* by the PM QoS layer or disable it in case we were passed |
| 82 |
* negative value or PM_QOS_LATENCY_ANY. |
| 83 |
*/ |
| 84 |
ltr = readl(host->priv + INTEL_ACTIVELTR); |
| 85 |
|
| 86 |
if (val == PM_QOS_LATENCY_ANY || val < 0) { |
| 87 |
ltr &= ~INTEL_LTR_REQ; |
| 88 |
} else { |
| 89 |
ltr |= INTEL_LTR_REQ; |
| 90 |
ltr &= ~INTEL_LTR_SCALE_MASK; |
| 91 |
ltr &= ~INTEL_LTR_VALUE_MASK; |
| 92 |
|
| 93 |
if (val > INTEL_LTR_VALUE_MASK) { |
| 94 |
val >>= 5; |
| 95 |
if (val > INTEL_LTR_VALUE_MASK) |
| 96 |
val = INTEL_LTR_VALUE_MASK; |
| 97 |
ltr |= INTEL_LTR_SCALE_32US | val; |
| 98 |
} else { |
| 99 |
ltr |= INTEL_LTR_SCALE_1US | val; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if (ltr == host->active_ltr) |
| 104 |
return; |
| 105 |
|
| 106 |
writel(ltr, host->priv + INTEL_ACTIVELTR); |
| 107 |
writel(ltr, host->priv + INTEL_IDLELTR); |
| 108 |
|
| 109 |
/* Cache the values into intel_host structure */ |
| 110 |
intel_cache_ltr(host); |
| 111 |
} |
| 112 |
|
| 113 |
static void intel_ltr_expose(struct device *dev) |
| 114 |
{ |
| 115 |
dev->power.set_latency_tolerance = intel_ltr_set; |
| 116 |
dev_pm_qos_expose_latency_tolerance(dev); |
| 117 |
} |
| 118 |
|
| 119 |
static void intel_ltr_hide(struct device *dev) |
| 120 |
{ |
| 121 |
dev_pm_qos_hide_latency_tolerance(dev); |
| 122 |
dev->power.set_latency_tolerance = NULL; |
| 123 |
} |
| 124 |
|
| 125 |
static void intel_add_debugfs(struct mipi_i3c_hci_pci *hci) |
| 126 |
{ |
| 127 |
struct dentry *dir = debugfs_create_dir(dev_name(&hci->pci->dev), NULL); |
| 128 |
struct intel_host *host = hci->private; |
| 129 |
|
| 130 |
intel_cache_ltr(host); |
| 131 |
|
| 132 |
host->debugfs_root = dir; |
| 133 |
debugfs_create_x32("active_ltr", 0444, dir, &host->active_ltr); |
| 134 |
debugfs_create_x32("idle_ltr", 0444, dir, &host->idle_ltr); |
| 135 |
} |
| 136 |
|
| 137 |
static void intel_remove_debugfs(struct mipi_i3c_hci_pci *hci) |
| 138 |
{ |
| 139 |
struct intel_host *host = hci->private; |
| 140 |
|
| 141 |
debugfs_remove_recursive(host->debugfs_root); |
| 142 |
} |
| 143 |
|
| 144 |
static void intel_reset(void __iomem *priv) |
| 145 |
{ |
| 146 |
u32 reg; |
| 147 |
|
| 148 |
/* Assert reset, wait for completion and release reset */ |
| 149 |
writel(0, priv + INTEL_RESETS); |
| 150 |
readl_poll_timeout(priv + INTEL_RESETS, reg, |
| 151 |
reg & INTEL_RESETS_RESET_DONE, 0, |
| 152 |
INTEL_RESETS_TIMEOUT_US); |
| 153 |
writel(INTEL_RESETS_RESET, priv + INTEL_RESETS); |
| 154 |
} |
| 155 |
|
| 156 |
static int intel_i3c_init(struct mipi_i3c_hci_pci *hci) |
| 157 |
{ |
| 158 |
struct intel_host *host = devm_kzalloc(&hci->pci->dev, sizeof(*host), GFP_KERNEL); |
| 159 |
void __iomem *priv = hci->base + INTEL_PRIV_OFFSET; |
| 160 |
|
| 161 |
if (!host) |
| 162 |
return -ENOMEM; |
| 163 |
|
| 164 |
dma_set_mask_and_coherent(&hci->pci->dev, DMA_BIT_MASK(64)); |
| 165 |
|
| 166 |
hci->pci->d3cold_delay = 0; |
| 167 |
|
| 168 |
hci->private = host; |
| 169 |
host->priv = priv; |
| 170 |
|
| 171 |
intel_reset(priv); |
| 172 |
|
| 173 |
intel_ltr_expose(&hci->pci->dev); |
| 174 |
intel_add_debugfs(hci); |
| 175 |
|
| 176 |
return 0; |
| 177 |
} |
| 178 |
|
| 179 |
static void intel_i3c_exit(struct mipi_i3c_hci_pci *hci) |
| 180 |
{ |
| 181 |
intel_remove_debugfs(hci); |
| 182 |
intel_ltr_hide(&hci->pci->dev); |
| 183 |
} |
| 184 |
|
| 185 |
static const struct mipi_i3c_hci_pci_info intel_mi_1_info = { |
| 186 |
.init = intel_i3c_init, |
| 187 |
.exit = intel_i3c_exit, |
| 188 |
.name = "intel-lpss-i3c", |
| 189 |
.id = {0, 1}, |
| 190 |
.instance_offset = {0, 0x400}, |
| 191 |
.instance_count = 2, |
| 192 |
}; |
| 193 |
|
| 194 |
static const struct mipi_i3c_hci_pci_info intel_mi_2_info = { |
| 195 |
.init = intel_i3c_init, |
| 196 |
.exit = intel_i3c_exit, |
| 197 |
.name = "intel-lpss-i3c", |
| 198 |
.id = {2, 3}, |
| 199 |
.instance_offset = {0, 0x400}, |
| 200 |
.instance_count = 2, |
| 201 |
}; |
| 202 |
|
| 203 |
static const struct mipi_i3c_hci_pci_info intel_si_2_info = { |
| 204 |
.init = intel_i3c_init, |
| 205 |
.exit = intel_i3c_exit, |
| 206 |
.name = "intel-lpss-i3c", |
| 207 |
.id = {2}, |
| 208 |
.instance_offset = {0}, |
| 209 |
.instance_count = 1, |
| 210 |
}; |
| 211 |
|
| 212 |
static void mipi_i3c_hci_pci_rpm_allow(struct device *dev) |
| 213 |
{ |
| 214 |
pm_runtime_put(dev); |
| 215 |
pm_runtime_allow(dev); |
| 216 |
} |
| 217 |
|
| 218 |
static void mipi_i3c_hci_pci_rpm_forbid(struct device *dev) |
| 219 |
{ |
| 220 |
pm_runtime_forbid(dev); |
| 221 |
pm_runtime_get_sync(dev); |
| 222 |
} |
| 223 |
|
| 224 |
struct mipi_i3c_hci_pci_cell_data { |
| 225 |
struct mipi_i3c_hci_platform_data pdata; |
| 226 |
struct resource res; |
| 227 |
}; |
| 228 |
|
| 229 |
static void mipi_i3c_hci_pci_setup_cell(struct mipi_i3c_hci_pci *hci, int idx, |
| 230 |
struct mipi_i3c_hci_pci_cell_data *data, |
| 231 |
struct mfd_cell *cell) |
| 232 |
{ |
| 233 |
data->pdata.base_regs = hci->base + hci->info->instance_offset[idx]; |
| 234 |
|
| 235 |
data->res = DEFINE_RES_IRQ(0); |
| 236 |
|
| 237 |
cell->name = hci->info->name; |
| 238 |
cell->id = hci->info->id[idx]; |
| 239 |
cell->platform_data = &data->pdata; |
| 240 |
cell->pdata_size = sizeof(data->pdata); |
| 241 |
cell->num_resources = 1; |
| 242 |
cell->resources = &data->res; |
| 243 |
} |
| 244 |
|
| 245 |
#define mipi_i3c_hci_pci_alloc(h, x) kzalloc_objs(*(x), (h)->info->instance_count) |
| 246 |
|
| 247 |
static int mipi_i3c_hci_pci_add_instances(struct mipi_i3c_hci_pci *hci) |
| 248 |
{ |
| 249 |
struct mipi_i3c_hci_pci_cell_data *data __free(kfree) = mipi_i3c_hci_pci_alloc(hci, data); |
| 250 |
struct mfd_cell *cells __free(kfree) = mipi_i3c_hci_pci_alloc(hci, cells); |
| 251 |
int irq = pci_irq_vector(hci->pci, 0); |
| 252 |
int nr = hci->info->instance_count; |
| 253 |
|
| 254 |
if (!cells || !data) |
| 255 |
return -ENOMEM; |
| 256 |
|
| 257 |
for (int i = 0; i < nr; i++) |
| 258 |
mipi_i3c_hci_pci_setup_cell(hci, i, data + i, cells + i); |
| 259 |
|
| 260 |
return mfd_add_devices(&hci->pci->dev, 0, cells, nr, NULL, irq, NULL); |
| 261 |
} |
| 262 |
|
| 263 |
static int mipi_i3c_hci_pci_probe(struct pci_dev *pci, |
| 264 |
const struct pci_device_id *id) |
| 265 |
{ |
| 266 |
struct mipi_i3c_hci_pci *hci; |
| 267 |
int ret; |
| 268 |
|
| 269 |
hci = devm_kzalloc(&pci->dev, sizeof(*hci), GFP_KERNEL); |
| 270 |
if (!hci) |
| 271 |
return -ENOMEM; |
| 272 |
|
| 273 |
hci->pci = pci; |
| 274 |
|
| 275 |
ret = pcim_enable_device(pci); |
| 276 |
if (ret) |
| 277 |
return ret; |
| 278 |
|
| 279 |
pci_set_master(pci); |
| 280 |
|
| 281 |
hci->base = pcim_iomap_region(pci, 0, pci_name(pci)); |
| 282 |
if (IS_ERR(hci->base)) |
| 283 |
return PTR_ERR(hci->base); |
| 284 |
|
| 285 |
ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_ALL_TYPES); |
| 286 |
if (ret < 0) |
| 287 |
return ret; |
| 288 |
|
| 289 |
hci->info = (const struct mipi_i3c_hci_pci_info *)id->driver_data; |
| 290 |
|
| 291 |
ret = hci->info->init ? hci->info->init(hci) : 0; |
| 292 |
if (ret) |
| 293 |
return ret; |
| 294 |
|
| 295 |
ret = mipi_i3c_hci_pci_add_instances(hci); |
| 296 |
if (ret) |
| 297 |
goto err_exit; |
| 298 |
|
| 299 |
pci_set_drvdata(pci, hci); |
| 300 |
|
| 301 |
mipi_i3c_hci_pci_rpm_allow(&pci->dev); |
| 302 |
|
| 303 |
return 0; |
| 304 |
|
| 305 |
err_exit: |
| 306 |
if (hci->info->exit) |
| 307 |
hci->info->exit(hci); |
| 308 |
return ret; |
| 309 |
} |
| 310 |
|
| 311 |
static void mipi_i3c_hci_pci_remove(struct pci_dev *pci) |
| 312 |
{ |
| 313 |
struct mipi_i3c_hci_pci *hci = pci_get_drvdata(pci); |
| 314 |
|
| 315 |
if (hci->info->exit) |
| 316 |
hci->info->exit(hci); |
| 317 |
|
| 318 |
mipi_i3c_hci_pci_rpm_forbid(&pci->dev); |
| 319 |
|
| 320 |
mfd_remove_devices(&pci->dev); |
| 321 |
} |
| 322 |
|
| 323 |
/* PM ops must exist for PCI to put a device to a low power state */ |
| 324 |
static const struct dev_pm_ops mipi_i3c_hci_pci_pm_ops = { |
| 325 |
}; |
| 326 |
|
| 327 |
static const struct pci_device_id mipi_i3c_hci_pci_devices[] = { |
| 328 |
/* Wildcat Lake-U */ |
| 329 |
{ PCI_VDEVICE(INTEL, 0x4d7c), (kernel_ulong_t)&intel_mi_1_info}, |
| 330 |
{ PCI_VDEVICE(INTEL, 0x4d6f), (kernel_ulong_t)&intel_si_2_info}, |
| 331 |
/* Panther Lake-H */ |
| 332 |
{ PCI_VDEVICE(INTEL, 0xe37c), (kernel_ulong_t)&intel_mi_1_info}, |
| 333 |
{ PCI_VDEVICE(INTEL, 0xe36f), (kernel_ulong_t)&intel_si_2_info}, |
| 334 |
/* Panther Lake-P */ |
| 335 |
{ PCI_VDEVICE(INTEL, 0xe47c), (kernel_ulong_t)&intel_mi_1_info}, |
| 336 |
{ PCI_VDEVICE(INTEL, 0xe46f), (kernel_ulong_t)&intel_si_2_info}, |
| 337 |
/* Nova Lake-S */ |
| 338 |
{ PCI_VDEVICE(INTEL, 0x6e2c), (kernel_ulong_t)&intel_mi_1_info}, |
| 339 |
{ PCI_VDEVICE(INTEL, 0x6e2d), (kernel_ulong_t)&intel_mi_2_info}, |
| 340 |
{ }, |
| 341 |
}; |
| 342 |
MODULE_DEVICE_TABLE(pci, mipi_i3c_hci_pci_devices); |
| 343 |
|
| 344 |
static struct pci_driver mipi_i3c_hci_pci_driver = { |
| 345 |
.name = "mipi_i3c_hci_pci", |
| 346 |
.id_table = mipi_i3c_hci_pci_devices, |
| 347 |
.probe = mipi_i3c_hci_pci_probe, |
| 348 |
.remove = mipi_i3c_hci_pci_remove, |
| 349 |
.driver = { |
| 350 |
.pm = pm_ptr(&mipi_i3c_hci_pci_pm_ops) |
| 351 |
}, |
| 352 |
}; |
| 353 |
|
| 354 |
module_pci_driver(mipi_i3c_hci_pci_driver); |
| 355 |
|
| 356 |
MODULE_AUTHOR("Jarkko Nikula <[email protected]>"); |
| 357 |
MODULE_LICENSE("GPL"); |
| 358 |
MODULE_DESCRIPTION("MIPI I3C HCI driver on PCI bus"); |