| 1 |
|
| 2 |
|
| 3 |
#include <device/mmio.h> |
| 4 |
#include <assert.h> |
| 5 |
#include <bootstate.h> |
| 6 |
#include <console/console.h> |
| 7 |
#include <delay.h> |
| 8 |
#include <device/device.h> |
| 9 |
#include <device/pci_def.h> |
| 10 |
#include <device/pci_ops.h> |
| 11 |
#include <intelblocks/cfg.h> |
| 12 |
#include <intelblocks/gspi.h> |
| 13 |
#include <intelblocks/lpss.h> |
| 14 |
#include <intelblocks/spi.h> |
| 15 |
#include <soc/iomap.h> |
| 16 |
#include <soc/pci_devs.h> |
| 17 |
#include <string.h> |
| 18 |
#include <timer.h> |
| 19 |
|
| 20 |
|
| 21 |
#define SSCR0 0x0 |
| 22 |
#define SSCR0_EDSS_0 (0 << 20) |
| 23 |
#define SSCR0_EDSS_1 (1 << 20) |
| 24 |
#define SSCR0_SCR_SHIFT (8) |
| 25 |
#define SSCR0_SCR_MASK (0xFFF) |
| 26 |
#define SSCR0_SSE_DISABLE (0 << 7) |
| 27 |
#define SSCR0_SSE_ENABLE (1 << 7) |
| 28 |
#define SSCR0_ECS_ON_CHIP (0 << 6) |
| 29 |
#define SSCR0_FRF_MOTOROLA (0 << 4) |
| 30 |
#define SSCR0_DSS_SHIFT (0) |
| 31 |
#define SSCR0_DSS_MASK (0xF) |
| 32 |
#define SSCR1 0x4 |
| 33 |
#define SSCR1_IFS_LOW (0 << 16) |
| 34 |
#define SSCR1_IFS_HIGH (1 << 16) |
| 35 |
#define SSCR1_SPH_FIRST (0 << 4) |
| 36 |
#define SSCR1_SPH_SECOND (1 << 4) |
| 37 |
#define SSCR1_SPO_LOW (0 << 3) |
| 38 |
#define SSCR1_SPO_HIGH (1 << 3) |
| 39 |
#define SSSR 0x8 |
| 40 |
#define SSSR_TUR (1 << 21) |
| 41 |
#define SSSR_TINT (1 << 19) |
| 42 |
#define SSSR_PINT (1 << 18) |
| 43 |
|
| 44 |
#define SSSR_ROR (1 << 7) |
| 45 |
#define SSSR_BSY (1 << 4) |
| 46 |
#define SSSR_RNE (1 << 3) |
| 47 |
#define SSSR_TNF (1 << 2) |
| 48 |
#define SSDR 0x10 |
| 49 |
#define SSTO 0x28 |
| 50 |
#define SITF 0x44 |
| 51 |
#define SITF_LEVEL_SHIFT (16) |
| 52 |
#define SITF_LEVEL_MASK (0x3f) |
| 53 |
#define SITF_LWM_SHIFT (8) |
| 54 |
#define SITF_LWM_MASK (0x3f) |
| 55 |
#define SITF_LWM(x) ((((x) - 1) & SITF_LWM_MASK) << SITF_LWM_SHIFT) |
| 56 |
#define SITF_HWM_SHIFT (0) |
| 57 |
#define SITF_HWM_MASK (0x3f) |
| 58 |
#define SITF_HWM(x) ((((x) - 1) & SITF_HWM_MASK) << SITF_HWM_SHIFT) |
| 59 |
#define SIRF 0x48 |
| 60 |
#define SIRF_LEVEL_SHIFT (8) |
| 61 |
#define SIRF_LEVEL_MASK (0x3f) |
| 62 |
#define SIRF_WM_SHIFT (0) |
| 63 |
#define SIRF_WM_MASK (0x3f) |
| 64 |
#define SIRF_WM(x) ((((x) - 1) & SIRF_WM_MASK) << SIRF_WM_SHIFT) |
| 65 |
|
| 66 |
|
| 67 |
#define CLOCKS 0x200 |
| 68 |
#define CLOCKS_UPDATE (1 << 31) |
| 69 |
#define CLOCKS_N_SHIFT (16) |
| 70 |
#define CLOCKS_N_MASK (0x7fff) |
| 71 |
#define CLOCKS_M_SHIFT (1) |
| 72 |
#define CLOCKS_M_MASK (0x7fff) |
| 73 |
#define CLOCKS_DISABLE (0 << 0) |
| 74 |
#define CLOCKS_ENABLE (1 << 0) |
| 75 |
#define RESETS 0x204 |
| 76 |
#define DMA_RESET (0 << 2) |
| 77 |
#define DMA_ACTIVE (1 << 2) |
| 78 |
#define CTRLR_RESET (0 << 0) |
| 79 |
#define CTRLR_ACTIVE (3 << 0) |
| 80 |
#define ACTIVELTR_VALUE 0x210 |
| 81 |
#define IDLELTR_VALUE 0x214 |
| 82 |
#define TX_BIT_COUNT 0x218 |
| 83 |
#define RX_BIT_COUNT 0x21c |
| 84 |
#define SSP_REG 0x220 |
| 85 |
#define DMA_FINISH_DISABLE (1 << 0) |
| 86 |
#define SPI_CS_CONTROL 0x224 |
| 87 |
#define CS_0_POL_SHIFT (12) |
| 88 |
#define CS_0_POL_MASK (1 << CS_0_POL_SHIFT) |
| 89 |
#define CS_POL_LOW (0) |
| 90 |
#define CS_POL_HIGH (1) |
| 91 |
#define CS_0 (0 << 8) |
| 92 |
#define CS_STATE_SHIFT (1) |
| 93 |
#define CS_STATE_MASK (1 << CS_STATE_SHIFT) |
| 94 |
#define CS_V1_STATE_LOW (0) |
| 95 |
#define CS_V1_STATE_HIGH (1) |
| 96 |
#define CS_MODE_HW (0 << 0) |
| 97 |
#define CS_MODE_SW (1 << 0) |
| 98 |
|
| 99 |
#define GSPI_DATA_BIT_LENGTH (8) |
| 100 |
#define GSPI_BUS_BASE(bar, bus) ((bar) + (bus) * 4 * KiB) |
| 101 |
|
| 102 |
|
| 103 |
static uintptr_t gspi_get_early_base(void) |
| 104 |
{ |
| 105 |
return EARLY_GSPI_BASE_ADDRESS; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
static const struct gspi_cfg *gspi_get_cfg(void) |
| 110 |
{ |
| 111 |
const struct soc_intel_common_config *common_config; |
| 112 |
common_config = chip_get_common_soc_structure(); |
| 113 |
|
| 114 |
return &common_config->gspi[0]; |
| 115 |
} |
| 116 |
|
| 117 |
#if defined(__SIMPLE_DEVICE__) |
| 118 |
|
| 119 |
static uintptr_t gspi_get_base_addr(int devfn, |
| 120 |
DEVTREE_CONST struct device *dev) |
| 121 |
{ |
| 122 |
pci_devfn_t pci_dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
| 123 |
return ALIGN_DOWN(pci_read_config32(pci_dev, PCI_BASE_ADDRESS_0), 16); |
| 124 |
} |
| 125 |
|
| 126 |
static void gspi_set_base_addr(int devfn, DEVTREE_CONST struct device *dev, |
| 127 |
uintptr_t base) |
| 128 |
{ |
| 129 |
pci_devfn_t pci_dev = PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn)); |
| 130 |
pci_write_config32(pci_dev, PCI_BASE_ADDRESS_0, base); |
| 131 |
pci_write_config32(pci_dev, PCI_COMMAND, PCI_COMMAND_MEMORY | |
| 132 |
PCI_COMMAND_MASTER); |
| 133 |
} |
| 134 |
|
| 135 |
void gspi_early_bar_init(void) |
| 136 |
{ |
| 137 |
unsigned int gspi_bus; |
| 138 |
const unsigned int gspi_max = CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_MAX; |
| 139 |
const struct gspi_cfg *cfg = gspi_get_cfg(); |
| 140 |
int devfn; |
| 141 |
uintptr_t gspi_base_addr; |
| 142 |
|
| 143 |
assert(gspi_max != 0); |
| 144 |
if (!cfg) { |
| 145 |
printk(BIOS_ERR, "%s: No GSPI config provided by SoC!\n", |
| 146 |
__func__); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
gspi_base_addr = gspi_get_early_base(); |
| 151 |
if (!gspi_base_addr) { |
| 152 |
printk(BIOS_ERR, "%s: GSPI base address provided is NULL!\n", |
| 153 |
__func__); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
for (gspi_bus = 0; gspi_bus < gspi_max; gspi_bus++) { |
| 158 |
if (!cfg[gspi_bus].early_init) |
| 159 |
continue; |
| 160 |
devfn = gspi_soc_bus_to_devfn(gspi_bus); |
| 161 |
gspi_set_base_addr(devfn, NULL, |
| 162 |
GSPI_BUS_BASE(gspi_base_addr, gspi_bus)); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
#else |
| 167 |
|
| 168 |
static uintptr_t gspi_get_base_addr(int devfn, struct device *dev) |
| 169 |
{ |
| 170 |
return ALIGN_DOWN(pci_read_config32(dev, PCI_BASE_ADDRESS_0), 16); |
| 171 |
} |
| 172 |
|
| 173 |
static void gspi_set_base_addr(int devfn, struct device *dev, uintptr_t base) |
| 174 |
{ |
| 175 |
pci_write_config32(dev, PCI_BASE_ADDRESS_0, base); |
| 176 |
pci_write_config32(dev, PCI_COMMAND, PCI_COMMAND_MEMORY | |
| 177 |
PCI_COMMAND_MASTER); |
| 178 |
} |
| 179 |
|
| 180 |
#endif |
| 181 |
|
| 182 |
static int gspi_read_bus_range(unsigned int *start, unsigned int *end) |
| 183 |
{ |
| 184 |
size_t i; |
| 185 |
const struct spi_ctrlr_buses *desc; |
| 186 |
|
| 187 |
for (i = 0; i < spi_ctrlr_bus_map_count; i++) { |
| 188 |
desc = &spi_ctrlr_bus_map[i]; |
| 189 |
|
| 190 |
if (desc->ctrlr != &gspi_ctrlr) |
| 191 |
continue; |
| 192 |
|
| 193 |
*start = desc->bus_start; |
| 194 |
*end = desc->bus_end; |
| 195 |
|
| 196 |
return 0; |
| 197 |
} |
| 198 |
return -1; |
| 199 |
} |
| 200 |
|
| 201 |
static int gspi_spi_to_gspi_bus(unsigned int spi_bus, unsigned int *gspi_bus) |
| 202 |
{ |
| 203 |
unsigned int start; |
| 204 |
unsigned int end; |
| 205 |
int ret; |
| 206 |
|
| 207 |
ret = gspi_read_bus_range(&start, &end); |
| 208 |
|
| 209 |
if (ret != 0 || (spi_bus < start) || (spi_bus > end)) |
| 210 |
return -1; |
| 211 |
|
| 212 |
*gspi_bus = spi_bus - start; |
| 213 |
|
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
static uintptr_t gspi_calc_base_addr(unsigned int gspi_bus) |
| 218 |
{ |
| 219 |
uintptr_t bus_base, gspi_base_addr; |
| 220 |
DEVTREE_CONST struct device *dev; |
| 221 |
int devfn = gspi_soc_bus_to_devfn(gspi_bus); |
| 222 |
|
| 223 |
if (devfn < 0) |
| 224 |
return 0; |
| 225 |
|
| 226 |
dev = pcidev_path_on_root(devfn); |
| 227 |
if (!dev || !dev->enabled) |
| 228 |
return 0; |
| 229 |
|
| 230 |
bus_base = gspi_get_base_addr(devfn, dev); |
| 231 |
if (bus_base) |
| 232 |
return bus_base; |
| 233 |
|
| 234 |
gspi_base_addr = gspi_get_early_base(); |
| 235 |
if (!gspi_base_addr) |
| 236 |
return 0; |
| 237 |
|
| 238 |
bus_base = GSPI_BUS_BASE(gspi_base_addr, gspi_bus); |
| 239 |
|
| 240 |
gspi_set_base_addr(devfn, dev, bus_base); |
| 241 |
return bus_base; |
| 242 |
} |
| 243 |
|
| 244 |
static uint32_t gspi_get_bus_clk_mhz(unsigned int gspi_bus) |
| 245 |
{ |
| 246 |
const struct gspi_cfg *cfg = gspi_get_cfg(); |
| 247 |
if (!cfg) |
| 248 |
return 0; |
| 249 |
return cfg[gspi_bus].speed_mhz; |
| 250 |
} |
| 251 |
|
| 252 |
static uintptr_t gspi_base[CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_MAX]; |
| 253 |
static uintptr_t gspi_get_bus_base_addr(unsigned int gspi_bus) |
| 254 |
{ |
| 255 |
if (!gspi_base[gspi_bus]) |
| 256 |
gspi_base[gspi_bus] = gspi_calc_base_addr(gspi_bus); |
| 257 |
|
| 258 |
return gspi_base[gspi_bus]; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
static void gspi_clear_cached_base(void *unused) |
| 267 |
{ |
| 268 |
memset(gspi_base, 0, sizeof(gspi_base)); |
| 269 |
} |
| 270 |
BOOT_STATE_INIT_ENTRY(BS_DEV_RESOURCES, BS_ON_EXIT, gspi_clear_cached_base, NULL); |
| 271 |
|
| 272 |
|
| 273 |
struct gspi_ctrlr_params { |
| 274 |
uintptr_t mmio_base; |
| 275 |
unsigned int gspi_bus; |
| 276 |
uint8_t *in; |
| 277 |
size_t bytesin; |
| 278 |
const uint8_t *out; |
| 279 |
size_t bytesout; |
| 280 |
}; |
| 281 |
|
| 282 |
static uint32_t gspi_read_mmio_reg(const struct gspi_ctrlr_params *p, |
| 283 |
uint32_t offset) |
| 284 |
{ |
| 285 |
assert(p->mmio_base != 0); |
| 286 |
return read32p(p->mmio_base + offset); |
| 287 |
} |
| 288 |
|
| 289 |
static void gspi_write_mmio_reg(const struct gspi_ctrlr_params *p, |
| 290 |
uint32_t offset, uint32_t value) |
| 291 |
{ |
| 292 |
assert(p->mmio_base != 0); |
| 293 |
write32p(p->mmio_base + offset, value); |
| 294 |
} |
| 295 |
|
| 296 |
static int gspi_ctrlr_params_init(struct gspi_ctrlr_params *p, |
| 297 |
unsigned int spi_bus) |
| 298 |
{ |
| 299 |
memset(p, 0, sizeof(*p)); |
| 300 |
|
| 301 |
if (gspi_spi_to_gspi_bus(spi_bus, &p->gspi_bus)) { |
| 302 |
printk(BIOS_ERR, "%s: No GSPI bus available for SPI bus %u.\n", |
| 303 |
__func__, spi_bus); |
| 304 |
return -1; |
| 305 |
} |
| 306 |
|
| 307 |
p->mmio_base = gspi_get_bus_base_addr(p->gspi_bus); |
| 308 |
if (!p->mmio_base) { |
| 309 |
printk(BIOS_ERR, "%s: Base addr is 0 for GSPI bus=%u.\n", |
| 310 |
__func__, p->gspi_bus); |
| 311 |
return -1; |
| 312 |
} |
| 313 |
|
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
enum cs_assert { |
| 318 |
CS_ASSERT, |
| 319 |
CS_DEASSERT, |
| 320 |
}; |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
static uint32_t gspi_csctrl_state_v2(uint32_t pol, enum cs_assert cs_assert) |
| 334 |
{ |
| 335 |
return cs_assert; |
| 336 |
} |
| 337 |
|
| 338 |
static uint32_t gspi_csctrl_state_v1(uint32_t pol, enum cs_assert cs_assert) |
| 339 |
{ |
| 340 |
uint32_t state; |
| 341 |
|
| 342 |
if (pol == CS_POL_HIGH) |
| 343 |
state = (cs_assert == CS_ASSERT) ? CS_V1_STATE_HIGH : |
| 344 |
CS_V1_STATE_LOW; |
| 345 |
else |
| 346 |
state = (cs_assert == CS_ASSERT) ? CS_V1_STATE_LOW : |
| 347 |
CS_V1_STATE_HIGH; |
| 348 |
|
| 349 |
return state; |
| 350 |
} |
| 351 |
|
| 352 |
static uint32_t gspi_csctrl_state(uint32_t pol, enum cs_assert cs_assert) |
| 353 |
{ |
| 354 |
if (CONFIG(SOC_INTEL_COMMON_BLOCK_GSPI_VERSION_2)) |
| 355 |
return gspi_csctrl_state_v2(pol, cs_assert); |
| 356 |
|
| 357 |
return gspi_csctrl_state_v1(pol, cs_assert); |
| 358 |
} |
| 359 |
|
| 360 |
static uint32_t gspi_csctrl_polarity_v2(enum spi_polarity active_pol) |
| 361 |
{ |
| 362 |
|
| 363 |
if (active_pol == SPI_POLARITY_LOW) |
| 364 |
return CS_POL_HIGH; |
| 365 |
return CS_POL_LOW; |
| 366 |
} |
| 367 |
|
| 368 |
static uint32_t gspi_csctrl_polarity_v1(enum spi_polarity active_pol) |
| 369 |
{ |
| 370 |
|
| 371 |
if (active_pol == SPI_POLARITY_LOW) |
| 372 |
return CS_POL_LOW; |
| 373 |
return CS_POL_HIGH; |
| 374 |
} |
| 375 |
|
| 376 |
static uint32_t gspi_csctrl_polarity(enum spi_polarity active_pol) |
| 377 |
{ |
| 378 |
if (CONFIG(SOC_INTEL_COMMON_BLOCK_GSPI_VERSION_2)) |
| 379 |
return gspi_csctrl_polarity_v2(active_pol); |
| 380 |
|
| 381 |
return gspi_csctrl_polarity_v1(active_pol); |
| 382 |
} |
| 383 |
|
| 384 |
static void __gspi_cs_change(const struct gspi_ctrlr_params *p, |
| 385 |
enum cs_assert cs_assert) |
| 386 |
{ |
| 387 |
uint32_t cs_ctrl, pol; |
| 388 |
cs_ctrl = gspi_read_mmio_reg(p, SPI_CS_CONTROL); |
| 389 |
|
| 390 |
cs_ctrl &= ~CS_STATE_MASK; |
| 391 |
|
| 392 |
pol = !!(cs_ctrl & CS_0_POL_MASK); |
| 393 |
cs_ctrl |= gspi_csctrl_state(pol, cs_assert) << CS_STATE_SHIFT; |
| 394 |
|
| 395 |
gspi_write_mmio_reg(p, SPI_CS_CONTROL, cs_ctrl); |
| 396 |
} |
| 397 |
|
| 398 |
static int gspi_cs_change(const struct spi_slave *dev, enum cs_assert cs_assert) |
| 399 |
{ |
| 400 |
struct gspi_ctrlr_params params, *p = ¶ms; |
| 401 |
|
| 402 |
if (gspi_ctrlr_params_init(p, dev->bus)) |
| 403 |
return -1; |
| 404 |
|
| 405 |
__gspi_cs_change(p, cs_assert); |
| 406 |
|
| 407 |
return 0; |
| 408 |
} |
| 409 |
|
| 410 |
int __weak gspi_get_soc_spi_cfg(unsigned int gspi_bus, |
| 411 |
struct spi_cfg *cfg) |
| 412 |
{ |
| 413 |
cfg->clk_phase = SPI_CLOCK_PHASE_FIRST; |
| 414 |
cfg->clk_polarity = SPI_POLARITY_LOW; |
| 415 |
cfg->cs_polarity = SPI_POLARITY_LOW; |
| 416 |
cfg->wire_mode = SPI_4_WIRE_MODE; |
| 417 |
cfg->data_bit_length = GSPI_DATA_BIT_LENGTH; |
| 418 |
|
| 419 |
return 0; |
| 420 |
} |
| 421 |
|
| 422 |
static int gspi_cs_assert(const struct spi_slave *dev) |
| 423 |
{ |
| 424 |
return gspi_cs_change(dev, CS_ASSERT); |
| 425 |
} |
| 426 |
|
| 427 |
static void gspi_cs_deassert(const struct spi_slave *dev) |
| 428 |
{ |
| 429 |
gspi_cs_change(dev, CS_DEASSERT); |
| 430 |
} |
| 431 |
|
| 432 |
static uint32_t gspi_get_clk_div(unsigned int gspi_bus) |
| 433 |
{ |
| 434 |
const uint32_t ref_clk_mhz = |
| 435 |
CONFIG_SOC_INTEL_COMMON_BLOCK_GSPI_CLOCK_MHZ; |
| 436 |
uint32_t gspi_clk_mhz = gspi_get_bus_clk_mhz(gspi_bus); |
| 437 |
|
| 438 |
if (!gspi_clk_mhz) |
| 439 |
gspi_clk_mhz = 1; |
| 440 |
|
| 441 |
assert(ref_clk_mhz != 0); |
| 442 |
return (DIV_ROUND_UP(ref_clk_mhz, gspi_clk_mhz) - 1) & SSCR0_SCR_MASK; |
| 443 |
} |
| 444 |
|
| 445 |
static int gspi_ctrlr_setup(const struct spi_slave *dev) |
| 446 |
{ |
| 447 |
struct spi_cfg cfg; |
| 448 |
int devfn; |
| 449 |
uint32_t cs_ctrl, sscr0, sscr1, clocks, sitf, sirf, pol; |
| 450 |
struct gspi_ctrlr_params params, *p = ¶ms; |
| 451 |
|
| 452 |
|
| 453 |
if (dev->cs != 0) { |
| 454 |
printk(BIOS_ERR, "%s: Invalid CS value: cs=%u.\n", __func__, |
| 455 |
dev->cs); |
| 456 |
return -1; |
| 457 |
} |
| 458 |
|
| 459 |
if (gspi_ctrlr_params_init(p, dev->bus)) |
| 460 |
return -1; |
| 461 |
|
| 462 |
|
| 463 |
if (gspi_get_soc_spi_cfg(p->gspi_bus, &cfg)) { |
| 464 |
printk(BIOS_ERR, "%s: Failed to get config for bus=%u.\n", |
| 465 |
__func__, p->gspi_bus); |
| 466 |
return -1; |
| 467 |
} |
| 468 |
|
| 469 |
devfn = gspi_soc_bus_to_devfn(p->gspi_bus); |
| 470 |
|
| 471 |
|
| 472 |
lpss_set_power_state(PCI_DEV(0, PCI_SLOT(devfn), PCI_FUNC(devfn)), STATE_D0); |
| 473 |
|
| 474 |
|
| 475 |
gspi_write_mmio_reg(p, RESETS, CTRLR_ACTIVE | DMA_RESET); |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
cs_ctrl = CS_MODE_SW | CS_0; |
| 485 |
pol = gspi_csctrl_polarity(cfg.cs_polarity); |
| 486 |
cs_ctrl |= pol << CS_0_POL_SHIFT; |
| 487 |
cs_ctrl |= gspi_csctrl_state(pol, CS_DEASSERT) << CS_STATE_SHIFT; |
| 488 |
gspi_write_mmio_reg(p, SPI_CS_CONTROL, cs_ctrl); |
| 489 |
|
| 490 |
|
| 491 |
gspi_write_mmio_reg(p, SSCR0, SSCR0_SSE_DISABLE); |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
sscr0 = gspi_get_clk_div(p->gspi_bus) << SSCR0_SCR_SHIFT; |
| 501 |
assert(GSPI_DATA_BIT_LENGTH == 8); |
| 502 |
sscr0 |= ((GSPI_DATA_BIT_LENGTH - 1) << SSCR0_DSS_SHIFT) | SSCR0_EDSS_0; |
| 503 |
sscr0 |= SSCR0_ECS_ON_CHIP | SSCR0_FRF_MOTOROLA; |
| 504 |
gspi_write_mmio_reg(p, SSCR0, sscr0); |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
sscr1 = (cfg.cs_polarity == SPI_POLARITY_LOW) ? SSCR1_IFS_LOW : |
| 513 |
SSCR1_IFS_HIGH; |
| 514 |
sscr1 |= (cfg.clk_phase == SPI_CLOCK_PHASE_FIRST) ? SSCR1_SPH_FIRST : |
| 515 |
SSCR1_SPH_SECOND; |
| 516 |
sscr1 |= (cfg.clk_polarity == SPI_POLARITY_LOW) ? SSCR1_SPO_LOW : |
| 517 |
SSCR1_SPO_HIGH; |
| 518 |
gspi_write_mmio_reg(p, SSCR1, sscr1); |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
clocks = (1 << CLOCKS_N_SHIFT) | (1 << CLOCKS_M_SHIFT) | CLOCKS_ENABLE | |
| 525 |
CLOCKS_UPDATE; |
| 526 |
gspi_write_mmio_reg(p, CLOCKS, clocks); |
| 527 |
udelay(10); |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
sitf = SITF_LWM(1) | SITF_HWM(1); |
| 535 |
gspi_write_mmio_reg(p, SITF, sitf); |
| 536 |
|
| 537 |
|
| 538 |
sirf = SIRF_WM(1); |
| 539 |
gspi_write_mmio_reg(p, SIRF, sirf); |
| 540 |
|
| 541 |
|
| 542 |
sscr0 |= SSCR0_SSE_ENABLE; |
| 543 |
gspi_write_mmio_reg(p, SSCR0, sscr0); |
| 544 |
|
| 545 |
return 0; |
| 546 |
} |
| 547 |
|
| 548 |
static uint32_t gspi_read_status(const struct gspi_ctrlr_params *p) |
| 549 |
{ |
| 550 |
return gspi_read_mmio_reg(p, SSSR); |
| 551 |
} |
| 552 |
|
| 553 |
static void gspi_clear_status(const struct gspi_ctrlr_params *p) |
| 554 |
{ |
| 555 |
const uint32_t sssr = SSSR_TUR | SSSR_TINT | SSSR_PINT | SSSR_ROR; |
| 556 |
gspi_write_mmio_reg(p, SSSR, sssr); |
| 557 |
} |
| 558 |
|
| 559 |
static bool gspi_rx_fifo_empty(const struct gspi_ctrlr_params *p) |
| 560 |
{ |
| 561 |
return !(gspi_read_status(p) & SSSR_RNE); |
| 562 |
} |
| 563 |
|
| 564 |
static bool gspi_tx_fifo_full(const struct gspi_ctrlr_params *p) |
| 565 |
{ |
| 566 |
return !(gspi_read_status(p) & SSSR_TNF); |
| 567 |
} |
| 568 |
|
| 569 |
static bool gspi_rx_fifo_overrun(const struct gspi_ctrlr_params *p) |
| 570 |
{ |
| 571 |
if (gspi_read_status(p) & SSSR_ROR) { |
| 572 |
printk(BIOS_ERR, "%s:GSPI receive FIFO overrun!" |
| 573 |
" (bus=%u).\n", __func__, p->gspi_bus); |
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
return false; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
static uint8_t gspi_read_byte(const struct gspi_ctrlr_params *p) |
| 582 |
{ |
| 583 |
return gspi_read_mmio_reg(p, SSDR) & 0xFF; |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
static void gspi_write_byte(const struct gspi_ctrlr_params *p, uint8_t data) |
| 588 |
{ |
| 589 |
return gspi_write_mmio_reg(p, SSDR, data); |
| 590 |
} |
| 591 |
|
| 592 |
static void gspi_read_data(struct gspi_ctrlr_params *p) |
| 593 |
{ |
| 594 |
*(p->in) = gspi_read_byte(p); |
| 595 |
p->in++; |
| 596 |
p->bytesin--; |
| 597 |
} |
| 598 |
|
| 599 |
static void gspi_write_data(struct gspi_ctrlr_params *p) |
| 600 |
{ |
| 601 |
gspi_write_byte(p, *(p->out)); |
| 602 |
p->out++; |
| 603 |
p->bytesout--; |
| 604 |
} |
| 605 |
|
| 606 |
static void gspi_read_dummy(struct gspi_ctrlr_params *p) |
| 607 |
{ |
| 608 |
gspi_read_byte(p); |
| 609 |
p->bytesin--; |
| 610 |
} |
| 611 |
|
| 612 |
static void gspi_write_dummy(struct gspi_ctrlr_params *p) |
| 613 |
{ |
| 614 |
gspi_write_byte(p, 0); |
| 615 |
p->bytesout--; |
| 616 |
} |
| 617 |
|
| 618 |
static int gspi_ctrlr_flush(const struct gspi_ctrlr_params *p) |
| 619 |
{ |
| 620 |
const uint32_t timeout_ms = 500; |
| 621 |
struct stopwatch sw; |
| 622 |
|
| 623 |
|
| 624 |
stopwatch_init_msecs_expire(&sw, timeout_ms); |
| 625 |
|
| 626 |
while (!gspi_rx_fifo_empty(p)) { |
| 627 |
if (stopwatch_expired(&sw)) { |
| 628 |
printk(BIOS_ERR, "%s: Rx FIFO not empty after 500ms! " |
| 629 |
"(bus=%u)\n", __func__, p->gspi_bus); |
| 630 |
return -1; |
| 631 |
} |
| 632 |
|
| 633 |
gspi_read_byte(p); |
| 634 |
} |
| 635 |
|
| 636 |
return 0; |
| 637 |
} |
| 638 |
|
| 639 |
static int __gspi_xfer(struct gspi_ctrlr_params *p) |
| 640 |
{ |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
void (*fn_read)(struct gspi_ctrlr_params *p) = gspi_read_data; |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
void (*fn_write)(struct gspi_ctrlr_params *p) = gspi_write_data; |
| 655 |
|
| 656 |
if (!p->bytesin) { |
| 657 |
p->bytesin = p->bytesout; |
| 658 |
fn_read = gspi_read_dummy; |
| 659 |
} |
| 660 |
|
| 661 |
if (!p->bytesout) { |
| 662 |
p->bytesout = p->bytesin; |
| 663 |
fn_write = gspi_write_dummy; |
| 664 |
} |
| 665 |
|
| 666 |
while (p->bytesout || p->bytesin) { |
| 667 |
if (p->bytesout && !gspi_tx_fifo_full(p)) |
| 668 |
fn_write(p); |
| 669 |
if (p->bytesin && !gspi_rx_fifo_empty(p)) { |
| 670 |
if (gspi_rx_fifo_overrun(p)) |
| 671 |
return -1; |
| 672 |
fn_read(p); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
return 0; |
| 677 |
} |
| 678 |
|
| 679 |
static int gspi_ctrlr_xfer(const struct spi_slave *dev, |
| 680 |
const void *dout, size_t bytesout, |
| 681 |
void *din, size_t bytesin) |
| 682 |
{ |
| 683 |
struct gspi_ctrlr_params params; |
| 684 |
struct gspi_ctrlr_params *p = ¶ms; |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
if (!bytesin && !bytesout) { |
| 696 |
printk(BIOS_ERR, "%s: Both in and out bytes cannot be zero!\n", |
| 697 |
__func__); |
| 698 |
return -1; |
| 699 |
} else if (bytesin && bytesout && (bytesin != bytesout)) { |
| 700 |
printk(BIOS_ERR, "%s: bytesin(%zd) != bytesout(%zd)\n", |
| 701 |
__func__, bytesin, bytesout); |
| 702 |
return -1; |
| 703 |
} |
| 704 |
if ((bytesin && !din) || (bytesout && !dout)) { |
| 705 |
printk(BIOS_ERR, "%s: in/out buffer is NULL!\n", __func__); |
| 706 |
return -1; |
| 707 |
} |
| 708 |
|
| 709 |
if (gspi_ctrlr_params_init(p, dev->bus)) |
| 710 |
return -1; |
| 711 |
|
| 712 |
|
| 713 |
if (gspi_ctrlr_flush(p)) |
| 714 |
return -1; |
| 715 |
|
| 716 |
|
| 717 |
gspi_clear_status(p); |
| 718 |
|
| 719 |
p->in = din; |
| 720 |
p->bytesin = bytesin; |
| 721 |
p->out = dout; |
| 722 |
p->bytesout = bytesout; |
| 723 |
|
| 724 |
return __gspi_xfer(p); |
| 725 |
} |
| 726 |
|
| 727 |
const struct spi_ctrlr gspi_ctrlr = { |
| 728 |
.claim_bus = gspi_cs_assert, |
| 729 |
.release_bus = gspi_cs_deassert, |
| 730 |
.setup = gspi_ctrlr_setup, |
| 731 |
.xfer = gspi_ctrlr_xfer, |
| 732 |
.max_xfer_size = SPI_CTRLR_DEFAULT_MAX_XFER_SIZE, |
| 733 |
}; |