| 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/bitmap.h> |
| 10 |
#include <linux/device.h> |
| 11 |
#include <linux/errno.h> |
| 12 |
#include <linux/i3c/master.h> |
| 13 |
#include <linux/io.h> |
| 14 |
|
| 15 |
#include "hci.h" |
| 16 |
#include "dat.h" |
| 17 |
|
| 18 |
/* |
| 19 |
* Device Address Table Structure |
| 20 |
*/ |
| 21 |
|
| 22 |
#define DAT_1_AUTOCMD_HDR_CODE W1_MASK(58, 51) |
| 23 |
#define DAT_1_AUTOCMD_MODE W1_MASK(50, 48) |
| 24 |
#define DAT_1_AUTOCMD_VALUE W1_MASK(47, 40) |
| 25 |
#define DAT_1_AUTOCMD_MASK W1_MASK(39, 32) |
| 26 |
/* DAT_0_I2C_DEVICE W0_BIT_(31) */ |
| 27 |
#define DAT_0_DEV_NACK_RETRY_CNT W0_MASK(30, 29) |
| 28 |
#define DAT_0_RING_ID W0_MASK(28, 26) |
| 29 |
#define DAT_0_DYNADDR_PARITY W0_BIT_(23) |
| 30 |
#define DAT_0_DYNAMIC_ADDRESS W0_MASK(22, 16) |
| 31 |
#define DAT_0_TS W0_BIT_(15) |
| 32 |
#define DAT_0_MR_REJECT W0_BIT_(14) |
| 33 |
/* DAT_0_SIR_REJECT W0_BIT_(13) */ |
| 34 |
/* DAT_0_IBI_PAYLOAD W0_BIT_(12) */ |
| 35 |
#define DAT_0_STATIC_ADDRESS W0_MASK(6, 0) |
| 36 |
|
| 37 |
#define dat_w0_read(i) hci->DAT[i].w0 |
| 38 |
#define dat_w1_read(i) hci->DAT[i].w1 |
| 39 |
#define dat_w0_write(i, v) hci_dat_w0_write(hci, i, v) |
| 40 |
#define dat_w1_write(i, v) hci_dat_w1_write(hci, i, v) |
| 41 |
|
| 42 |
static inline void hci_dat_w0_write(struct i3c_hci *hci, int i, u32 v) |
| 43 |
{ |
| 44 |
hci->DAT[i].w0 = v; |
| 45 |
writel(v, hci->DAT_regs + i * 8); |
| 46 |
} |
| 47 |
|
| 48 |
static inline void hci_dat_w1_write(struct i3c_hci *hci, int i, u32 v) |
| 49 |
{ |
| 50 |
hci->DAT[i].w1 = v; |
| 51 |
writel(v, hci->DAT_regs + i * 8 + 4); |
| 52 |
} |
| 53 |
|
| 54 |
static int hci_dat_v1_init(struct i3c_hci *hci) |
| 55 |
{ |
| 56 |
struct device *dev = hci->master.dev.parent; |
| 57 |
unsigned int dat_idx; |
| 58 |
|
| 59 |
if (!hci->DAT_regs) { |
| 60 |
dev_err(&hci->master.dev, |
| 61 |
"only DAT in register space is supported at the moment\n"); |
| 62 |
return -EOPNOTSUPP; |
| 63 |
} |
| 64 |
if (hci->DAT_entry_size != 8) { |
| 65 |
dev_err(&hci->master.dev, |
| 66 |
"only 8-bytes DAT entries are supported at the moment\n"); |
| 67 |
return -EOPNOTSUPP; |
| 68 |
} |
| 69 |
|
| 70 |
if (!hci->DAT) { |
| 71 |
hci->DAT = devm_kcalloc(dev, hci->DAT_entries, hci->DAT_entry_size, GFP_KERNEL); |
| 72 |
if (!hci->DAT) |
| 73 |
return -ENOMEM; |
| 74 |
} |
| 75 |
|
| 76 |
if (!hci->DAT_data) { |
| 77 |
/* use a bitmap for faster free slot search */ |
| 78 |
hci->DAT_data = devm_bitmap_zalloc(dev, hci->DAT_entries, GFP_KERNEL); |
| 79 |
if (!hci->DAT_data) |
| 80 |
return -ENOMEM; |
| 81 |
|
| 82 |
/* clear them */ |
| 83 |
for (dat_idx = 0; dat_idx < hci->DAT_entries; dat_idx++) { |
| 84 |
dat_w0_write(dat_idx, 0); |
| 85 |
dat_w1_write(dat_idx, 0); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
static int hci_dat_v1_alloc_entry(struct i3c_hci *hci) |
| 93 |
{ |
| 94 |
unsigned int dat_idx; |
| 95 |
int ret; |
| 96 |
|
| 97 |
if (!hci->DAT_data) { |
| 98 |
ret = hci_dat_v1_init(hci); |
| 99 |
if (ret) |
| 100 |
return ret; |
| 101 |
} |
| 102 |
dat_idx = find_first_zero_bit(hci->DAT_data, hci->DAT_entries); |
| 103 |
if (dat_idx >= hci->DAT_entries) |
| 104 |
return -ENOENT; |
| 105 |
__set_bit(dat_idx, hci->DAT_data); |
| 106 |
|
| 107 |
/* default flags */ |
| 108 |
dat_w0_write(dat_idx, DAT_0_SIR_REJECT | DAT_0_MR_REJECT); |
| 109 |
|
| 110 |
return dat_idx; |
| 111 |
} |
| 112 |
|
| 113 |
static void hci_dat_v1_free_entry(struct i3c_hci *hci, unsigned int dat_idx) |
| 114 |
{ |
| 115 |
dat_w0_write(dat_idx, 0); |
| 116 |
dat_w1_write(dat_idx, 0); |
| 117 |
if (hci->DAT_data) |
| 118 |
__clear_bit(dat_idx, hci->DAT_data); |
| 119 |
} |
| 120 |
|
| 121 |
static void hci_dat_v1_set_dynamic_addr(struct i3c_hci *hci, |
| 122 |
unsigned int dat_idx, u8 address) |
| 123 |
{ |
| 124 |
u32 dat_w0; |
| 125 |
|
| 126 |
dat_w0 = dat_w0_read(dat_idx); |
| 127 |
dat_w0 &= ~(DAT_0_DYNAMIC_ADDRESS | DAT_0_DYNADDR_PARITY); |
| 128 |
dat_w0 |= FIELD_PREP(DAT_0_DYNAMIC_ADDRESS, address) | |
| 129 |
(parity8(address) ? 0 : DAT_0_DYNADDR_PARITY); |
| 130 |
dat_w0_write(dat_idx, dat_w0); |
| 131 |
} |
| 132 |
|
| 133 |
static void hci_dat_v1_set_static_addr(struct i3c_hci *hci, |
| 134 |
unsigned int dat_idx, u8 address) |
| 135 |
{ |
| 136 |
u32 dat_w0; |
| 137 |
|
| 138 |
dat_w0 = dat_w0_read(dat_idx); |
| 139 |
dat_w0 &= ~DAT_0_STATIC_ADDRESS; |
| 140 |
dat_w0 |= FIELD_PREP(DAT_0_STATIC_ADDRESS, address); |
| 141 |
dat_w0_write(dat_idx, dat_w0); |
| 142 |
} |
| 143 |
|
| 144 |
static void hci_dat_v1_set_flags(struct i3c_hci *hci, unsigned int dat_idx, |
| 145 |
u32 w0_flags, u32 w1_flags) |
| 146 |
{ |
| 147 |
u32 dat_w0, dat_w1; |
| 148 |
|
| 149 |
dat_w0 = dat_w0_read(dat_idx); |
| 150 |
dat_w1 = dat_w1_read(dat_idx); |
| 151 |
dat_w0 |= w0_flags; |
| 152 |
dat_w1 |= w1_flags; |
| 153 |
dat_w0_write(dat_idx, dat_w0); |
| 154 |
dat_w1_write(dat_idx, dat_w1); |
| 155 |
} |
| 156 |
|
| 157 |
static void hci_dat_v1_clear_flags(struct i3c_hci *hci, unsigned int dat_idx, |
| 158 |
u32 w0_flags, u32 w1_flags) |
| 159 |
{ |
| 160 |
u32 dat_w0, dat_w1; |
| 161 |
|
| 162 |
dat_w0 = dat_w0_read(dat_idx); |
| 163 |
dat_w1 = dat_w1_read(dat_idx); |
| 164 |
dat_w0 &= ~w0_flags; |
| 165 |
dat_w1 &= ~w1_flags; |
| 166 |
dat_w0_write(dat_idx, dat_w0); |
| 167 |
dat_w1_write(dat_idx, dat_w1); |
| 168 |
} |
| 169 |
|
| 170 |
static int hci_dat_v1_get_index(struct i3c_hci *hci, u8 dev_addr) |
| 171 |
{ |
| 172 |
unsigned int dat_idx; |
| 173 |
u32 dat_w0; |
| 174 |
|
| 175 |
for_each_set_bit(dat_idx, hci->DAT_data, hci->DAT_entries) { |
| 176 |
dat_w0 = dat_w0_read(dat_idx); |
| 177 |
if (FIELD_GET(DAT_0_DYNAMIC_ADDRESS, dat_w0) == dev_addr) |
| 178 |
return dat_idx; |
| 179 |
} |
| 180 |
|
| 181 |
return -ENODEV; |
| 182 |
} |
| 183 |
|
| 184 |
static void hci_dat_v1_restore(struct i3c_hci *hci) |
| 185 |
{ |
| 186 |
for (int i = 0; i < hci->DAT_entries; i++) { |
| 187 |
writel(hci->DAT[i].w0, hci->DAT_regs + i * 8); |
| 188 |
writel(hci->DAT[i].w1, hci->DAT_regs + i * 8 + 4); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
const struct hci_dat_ops mipi_i3c_hci_dat_v1 = { |
| 193 |
.init = hci_dat_v1_init, |
| 194 |
.alloc_entry = hci_dat_v1_alloc_entry, |
| 195 |
.free_entry = hci_dat_v1_free_entry, |
| 196 |
.set_dynamic_addr = hci_dat_v1_set_dynamic_addr, |
| 197 |
.set_static_addr = hci_dat_v1_set_static_addr, |
| 198 |
.set_flags = hci_dat_v1_set_flags, |
| 199 |
.clear_flags = hci_dat_v1_clear_flags, |
| 200 |
.get_index = hci_dat_v1_get_index, |
| 201 |
.restore = hci_dat_v1_restore, |
| 202 |
}; |