Blame view
drivers/spi/spi-meson-spicc.c
22.2 KB
454fa271b spi: Add Meson SP... |
1 2 3 4 5 6 7 8 9 10 11 |
/* * Driver for Amlogic Meson SPI communication controller (SPICC) * * Copyright (C) BayLibre, SAS * Author: Neil Armstrong <narmstrong@baylibre.com> * * SPDX-License-Identifier: GPL-2.0+ */ #include <linux/bitfield.h> #include <linux/clk.h> |
a6cda1f90 spi: meson-spicc:... |
12 |
#include <linux/clk-provider.h> |
454fa271b spi: Add Meson SP... |
13 14 15 16 17 |
#include <linux/device.h> #include <linux/io.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/of.h> |
a6cda1f90 spi: meson-spicc:... |
18 |
#include <linux/of_device.h> |
454fa271b spi: Add Meson SP... |
19 20 21 22 23 |
#include <linux/platform_device.h> #include <linux/spi/spi.h> #include <linux/types.h> #include <linux/interrupt.h> #include <linux/reset.h> |
454fa271b spi: Add Meson SP... |
24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* * The Meson SPICC controller could support DMA based transfers, but is not * implemented by the vendor code, and while having the registers documentation * it has never worked on the GXL Hardware. * The PIO mode is the only mode implemented, and due to badly designed HW : * - all transfers are cutted in 16 words burst because the FIFO hangs on * TX underflow, and there is no TX "Half-Empty" interrupt, so we go by * FIFO max size chunk only * - CS management is dumb, and goes UP between every burst, so is really a * "Data Valid" signal than a Chip Select, GPIO link should be used instead * to have a CS go down over the full transfer */ |
454fa271b spi: Add Meson SP... |
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 |
#define SPICC_MAX_BURST 128 /* Register Map */ #define SPICC_RXDATA 0x00 #define SPICC_TXDATA 0x04 #define SPICC_CONREG 0x08 #define SPICC_ENABLE BIT(0) #define SPICC_MODE_MASTER BIT(1) #define SPICC_XCH BIT(2) #define SPICC_SMC BIT(3) #define SPICC_POL BIT(4) #define SPICC_PHA BIT(5) #define SPICC_SSCTL BIT(6) #define SPICC_SSPOL BIT(7) #define SPICC_DRCTL_MASK GENMASK(9, 8) #define SPICC_DRCTL_IGNORE 0 #define SPICC_DRCTL_FALLING 1 #define SPICC_DRCTL_LOWLEVEL 2 #define SPICC_CS_MASK GENMASK(13, 12) #define SPICC_DATARATE_MASK GENMASK(18, 16) #define SPICC_DATARATE_DIV4 0 #define SPICC_DATARATE_DIV8 1 #define SPICC_DATARATE_DIV16 2 #define SPICC_DATARATE_DIV32 3 #define SPICC_BITLENGTH_MASK GENMASK(24, 19) #define SPICC_BURSTLENGTH_MASK GENMASK(31, 25) #define SPICC_INTREG 0x0c #define SPICC_TE_EN BIT(0) /* TX FIFO Empty Interrupt */ #define SPICC_TH_EN BIT(1) /* TX FIFO Half-Full Interrupt */ #define SPICC_TF_EN BIT(2) /* TX FIFO Full Interrupt */ #define SPICC_RR_EN BIT(3) /* RX FIFO Ready Interrupt */ #define SPICC_RH_EN BIT(4) /* RX FIFO Half-Full Interrupt */ #define SPICC_RF_EN BIT(5) /* RX FIFO Full Interrupt */ #define SPICC_RO_EN BIT(6) /* RX FIFO Overflow Interrupt */ #define SPICC_TC_EN BIT(7) /* Transfert Complete Interrupt */ #define SPICC_DMAREG 0x10 #define SPICC_DMA_ENABLE BIT(0) #define SPICC_TXFIFO_THRESHOLD_MASK GENMASK(5, 1) #define SPICC_RXFIFO_THRESHOLD_MASK GENMASK(10, 6) #define SPICC_READ_BURST_MASK GENMASK(14, 11) #define SPICC_WRITE_BURST_MASK GENMASK(18, 15) #define SPICC_DMA_URGENT BIT(19) #define SPICC_DMA_THREADID_MASK GENMASK(25, 20) #define SPICC_DMA_BURSTNUM_MASK GENMASK(31, 26) #define SPICC_STATREG 0x14 #define SPICC_TE BIT(0) /* TX FIFO Empty Interrupt */ #define SPICC_TH BIT(1) /* TX FIFO Half-Full Interrupt */ #define SPICC_TF BIT(2) /* TX FIFO Full Interrupt */ #define SPICC_RR BIT(3) /* RX FIFO Ready Interrupt */ #define SPICC_RH BIT(4) /* RX FIFO Half-Full Interrupt */ #define SPICC_RF BIT(5) /* RX FIFO Full Interrupt */ #define SPICC_RO BIT(6) /* RX FIFO Overflow Interrupt */ #define SPICC_TC BIT(7) /* Transfert Complete Interrupt */ #define SPICC_PERIODREG 0x18 #define SPICC_PERIOD GENMASK(14, 0) /* Wait cycles */ #define SPICC_TESTREG 0x1c #define SPICC_TXCNT_MASK GENMASK(4, 0) /* TX FIFO Counter */ #define SPICC_RXCNT_MASK GENMASK(9, 5) /* RX FIFO Counter */ #define SPICC_SMSTATUS_MASK GENMASK(12, 10) /* State Machine Status */ #define SPICC_LBC_RO BIT(13) /* Loop Back Control Read-Only */ #define SPICC_LBC_W1 BIT(14) /* Loop Back Control Write-Only */ #define SPICC_SWAP_RO BIT(14) /* RX FIFO Data Swap Read-Only */ #define SPICC_SWAP_W1 BIT(15) /* RX FIFO Data Swap Write-Only */ #define SPICC_DLYCTL_RO_MASK GENMASK(20, 15) /* Delay Control Read-Only */ |
f27bff479 spi: meson-spicc:... |
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
#define SPICC_MO_DELAY_MASK GENMASK(17, 16) /* Master Output Delay */ #define SPICC_MO_NO_DELAY 0 #define SPICC_MO_DELAY_1_CYCLE 1 #define SPICC_MO_DELAY_2_CYCLE 2 #define SPICC_MO_DELAY_3_CYCLE 3 #define SPICC_MI_DELAY_MASK GENMASK(19, 18) /* Master Input Delay */ #define SPICC_MI_NO_DELAY 0 #define SPICC_MI_DELAY_1_CYCLE 1 #define SPICC_MI_DELAY_2_CYCLE 2 #define SPICC_MI_DELAY_3_CYCLE 3 #define SPICC_MI_CAP_DELAY_MASK GENMASK(21, 20) /* Master Capture Delay */ #define SPICC_CAP_AHEAD_2_CYCLE 0 #define SPICC_CAP_AHEAD_1_CYCLE 1 #define SPICC_CAP_NO_DELAY 2 #define SPICC_CAP_DELAY_1_CYCLE 3 |
454fa271b spi: Add Meson SP... |
123 124 125 126 127 128 |
#define SPICC_FIFORST_RO_MASK GENMASK(22, 21) /* FIFO Softreset Read-Only */ #define SPICC_FIFORST_W1_MASK GENMASK(23, 22) /* FIFO Softreset Write-Only */ #define SPICC_DRADDR 0x20 /* Read Address of DMA */ #define SPICC_DWADDR 0x24 /* Write Address of DMA */ |
a6cda1f90 spi: meson-spicc:... |
129 |
#define SPICC_ENH_CTL0 0x38 /* Enhanced Feature */ |
3e0cf4d3f spi: meson-spicc:... |
130 131 132 |
#define SPICC_ENH_CLK_CS_DELAY_MASK GENMASK(15, 0) #define SPICC_ENH_DATARATE_MASK GENMASK(23, 16) #define SPICC_ENH_DATARATE_EN BIT(24) |
a6cda1f90 spi: meson-spicc:... |
133 134 135 136 137 |
#define SPICC_ENH_MOSI_OEN BIT(25) #define SPICC_ENH_CLK_OEN BIT(26) #define SPICC_ENH_CS_OEN BIT(27) #define SPICC_ENH_CLK_CS_DELAY_EN BIT(28) #define SPICC_ENH_MAIN_CLK_AO BIT(29) |
454fa271b spi: Add Meson SP... |
138 139 |
#define writel_bits_relaxed(mask, val, addr) \ writel_relaxed((readl_relaxed(addr) & ~(mask)) | (val), addr) |
a6cda1f90 spi: meson-spicc:... |
140 |
struct meson_spicc_data { |
3196816ff spi: meson-spicc:... |
141 |
unsigned int max_speed_hz; |
8791068da spi: meson-spicc:... |
142 |
unsigned int min_speed_hz; |
0eb707ac7 spi: meson-spicc:... |
143 |
unsigned int fifo_size; |
a6cda1f90 spi: meson-spicc:... |
144 |
bool has_oen; |
3e0cf4d3f spi: meson-spicc:... |
145 |
bool has_enhance_clk_div; |
4e3d32205 spi: meson-spicc:... |
146 |
bool has_pclk; |
a6cda1f90 spi: meson-spicc:... |
147 |
}; |
454fa271b spi: Add Meson SP... |
148 149 150 151 152 |
struct meson_spicc_device { struct spi_master *master; struct platform_device *pdev; void __iomem *base; struct clk *core; |
4e3d32205 spi: meson-spicc:... |
153 |
struct clk *pclk; |
3e0cf4d3f spi: meson-spicc:... |
154 |
struct clk *clk; |
454fa271b spi: Add Meson SP... |
155 156 |
struct spi_message *message; struct spi_transfer *xfer; |
a6cda1f90 spi: meson-spicc:... |
157 |
const struct meson_spicc_data *data; |
454fa271b spi: Add Meson SP... |
158 159 160 161 |
u8 *tx_buf; u8 *rx_buf; unsigned int bytes_per_word; unsigned long tx_remain; |
454fa271b spi: Add Meson SP... |
162 |
unsigned long rx_remain; |
454fa271b spi: Add Meson SP... |
163 |
unsigned long xfer_remain; |
454fa271b spi: Add Meson SP... |
164 |
}; |
a6cda1f90 spi: meson-spicc:... |
165 166 167 168 169 170 171 172 173 174 175 176 |
static void meson_spicc_oen_enable(struct meson_spicc_device *spicc) { u32 conf; if (!spicc->data->has_oen) return; conf = readl_relaxed(spicc->base + SPICC_ENH_CTL0) | SPICC_ENH_MOSI_OEN | SPICC_ENH_CLK_OEN | SPICC_ENH_CS_OEN; writel_relaxed(conf, spicc->base + SPICC_ENH_CTL0); } |
454fa271b spi: Add Meson SP... |
177 178 179 180 181 182 183 184 |
static inline bool meson_spicc_txfull(struct meson_spicc_device *spicc) { return !!FIELD_GET(SPICC_TF, readl_relaxed(spicc->base + SPICC_STATREG)); } static inline bool meson_spicc_rxready(struct meson_spicc_device *spicc) { |
0eb707ac7 spi: meson-spicc:... |
185 |
return FIELD_GET(SPICC_RH | SPICC_RR | SPICC_RF, |
454fa271b spi: Add Meson SP... |
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 |
readl_relaxed(spicc->base + SPICC_STATREG)); } static inline u32 meson_spicc_pull_data(struct meson_spicc_device *spicc) { unsigned int bytes = spicc->bytes_per_word; unsigned int byte_shift = 0; u32 data = 0; u8 byte; while (bytes--) { byte = *spicc->tx_buf++; data |= (byte & 0xff) << byte_shift; byte_shift += 8; } spicc->tx_remain--; return data; } static inline void meson_spicc_push_data(struct meson_spicc_device *spicc, u32 data) { unsigned int bytes = spicc->bytes_per_word; unsigned int byte_shift = 0; u8 byte; while (bytes--) { byte = (data >> byte_shift) & 0xff; *spicc->rx_buf++ = byte; byte_shift += 8; } spicc->rx_remain--; } static inline void meson_spicc_rx(struct meson_spicc_device *spicc) { /* Empty RX FIFO */ while (spicc->rx_remain && meson_spicc_rxready(spicc)) meson_spicc_push_data(spicc, readl_relaxed(spicc->base + SPICC_RXDATA)); } static inline void meson_spicc_tx(struct meson_spicc_device *spicc) { /* Fill Up TX FIFO */ while (spicc->tx_remain && !meson_spicc_txfull(spicc)) writel_relaxed(meson_spicc_pull_data(spicc), spicc->base + SPICC_TXDATA); } |
0eb707ac7 spi: meson-spicc:... |
239 |
static inline void meson_spicc_setup_burst(struct meson_spicc_device *spicc) |
454fa271b spi: Add Meson SP... |
240 |
{ |
454fa271b spi: Add Meson SP... |
241 |
|
0eb707ac7 spi: meson-spicc:... |
242 243 244 245 |
unsigned int burst_len = min_t(unsigned int, spicc->xfer_remain / spicc->bytes_per_word, spicc->data->fifo_size); |
454fa271b spi: Add Meson SP... |
246 247 248 249 |
/* Setup Xfer variables */ spicc->tx_remain = burst_len; spicc->rx_remain = burst_len; spicc->xfer_remain -= burst_len * spicc->bytes_per_word; |
454fa271b spi: Add Meson SP... |
250 251 252 253 |
/* Setup burst length */ writel_bits_relaxed(SPICC_BURSTLENGTH_MASK, FIELD_PREP(SPICC_BURSTLENGTH_MASK, |
0eb707ac7 spi: meson-spicc:... |
254 |
burst_len - 1), |
454fa271b spi: Add Meson SP... |
255 256 257 258 259 260 261 262 263 |
spicc->base + SPICC_CONREG); /* Fill TX FIFO */ meson_spicc_tx(spicc); } static irqreturn_t meson_spicc_irq(int irq, void *data) { struct meson_spicc_device *spicc = (void *) data; |
454fa271b spi: Add Meson SP... |
264 |
|
0eb707ac7 spi: meson-spicc:... |
265 |
writel_bits_relaxed(SPICC_TC, SPICC_TC, spicc->base + SPICC_STATREG); |
454fa271b spi: Add Meson SP... |
266 267 268 |
/* Empty RX FIFO */ meson_spicc_rx(spicc); |
0eb707ac7 spi: meson-spicc:... |
269 270 271 |
if (!spicc->xfer_remain) { /* Disable all IRQs */ writel(0, spicc->base + SPICC_INTREG); |
454fa271b spi: Add Meson SP... |
272 |
|
0eb707ac7 spi: meson-spicc:... |
273 |
spi_finalize_current_transfer(spicc->master); |
454fa271b spi: Add Meson SP... |
274 |
|
0eb707ac7 spi: meson-spicc:... |
275 |
return IRQ_HANDLED; |
454fa271b spi: Add Meson SP... |
276 |
} |
0eb707ac7 spi: meson-spicc:... |
277 278 |
/* Setup burst */ meson_spicc_setup_burst(spicc); |
454fa271b spi: Add Meson SP... |
279 |
|
0eb707ac7 spi: meson-spicc:... |
280 281 |
/* Start burst */ writel_bits_relaxed(SPICC_XCH, SPICC_XCH, spicc->base + SPICC_CONREG); |
454fa271b spi: Add Meson SP... |
282 283 284 |
return IRQ_HANDLED; } |
f27bff479 spi: meson-spicc:... |
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
static void meson_spicc_auto_io_delay(struct meson_spicc_device *spicc) { u32 div, hz; u32 mi_delay, cap_delay; u32 conf; if (spicc->data->has_enhance_clk_div) { div = FIELD_GET(SPICC_ENH_DATARATE_MASK, readl_relaxed(spicc->base + SPICC_ENH_CTL0)); div++; div <<= 1; } else { div = FIELD_GET(SPICC_DATARATE_MASK, readl_relaxed(spicc->base + SPICC_CONREG)); div += 2; div = 1 << div; } mi_delay = SPICC_MI_NO_DELAY; cap_delay = SPICC_CAP_AHEAD_2_CYCLE; hz = clk_get_rate(spicc->clk); if (hz >= 100000000) cap_delay = SPICC_CAP_DELAY_1_CYCLE; else if (hz >= 80000000) cap_delay = SPICC_CAP_NO_DELAY; else if (hz >= 40000000) cap_delay = SPICC_CAP_AHEAD_1_CYCLE; else if (div >= 16) mi_delay = SPICC_MI_DELAY_3_CYCLE; else if (div >= 8) mi_delay = SPICC_MI_DELAY_2_CYCLE; else if (div >= 6) mi_delay = SPICC_MI_DELAY_1_CYCLE; conf = readl_relaxed(spicc->base + SPICC_TESTREG); conf &= ~(SPICC_MO_DELAY_MASK | SPICC_MI_DELAY_MASK | SPICC_MI_CAP_DELAY_MASK); conf |= FIELD_PREP(SPICC_MI_DELAY_MASK, mi_delay); conf |= FIELD_PREP(SPICC_MI_CAP_DELAY_MASK, cap_delay); writel_relaxed(conf, spicc->base + SPICC_TESTREG); } |
454fa271b spi: Add Meson SP... |
327 328 329 330 331 332 333 |
static void meson_spicc_setup_xfer(struct meson_spicc_device *spicc, struct spi_transfer *xfer) { u32 conf, conf_orig; /* Read original configuration */ conf = conf_orig = readl_relaxed(spicc->base + SPICC_CONREG); |
454fa271b spi: Add Meson SP... |
334 335 336 337 338 339 340 341 |
/* Setup word width */ conf &= ~SPICC_BITLENGTH_MASK; conf |= FIELD_PREP(SPICC_BITLENGTH_MASK, (spicc->bytes_per_word << 3) - 1); /* Ignore if unchanged */ if (conf != conf_orig) writel_relaxed(conf, spicc->base + SPICC_CONREG); |
3e0cf4d3f spi: meson-spicc:... |
342 343 |
clk_set_rate(spicc->clk, xfer->speed_hz); |
f27bff479 spi: meson-spicc:... |
344 345 |
meson_spicc_auto_io_delay(spicc); |
0eb707ac7 spi: meson-spicc:... |
346 347 348 349 350 351 |
writel_relaxed(0, spicc->base + SPICC_DMAREG); } static void meson_spicc_reset_fifo(struct meson_spicc_device *spicc) { |
0eb707ac7 spi: meson-spicc:... |
352 353 354 355 356 357 358 359 360 |
if (spicc->data->has_oen) writel_bits_relaxed(SPICC_ENH_MAIN_CLK_AO, SPICC_ENH_MAIN_CLK_AO, spicc->base + SPICC_ENH_CTL0); writel_bits_relaxed(SPICC_FIFORST_W1_MASK, SPICC_FIFORST_W1_MASK, spicc->base + SPICC_TESTREG); while (meson_spicc_rxready(spicc)) |
d9b883aea spi: spi-meson-sp... |
361 |
readl_relaxed(spicc->base + SPICC_RXDATA); |
0eb707ac7 spi: meson-spicc:... |
362 363 364 365 |
if (spicc->data->has_oen) writel_bits_relaxed(SPICC_ENH_MAIN_CLK_AO, 0, spicc->base + SPICC_ENH_CTL0); |
454fa271b spi: Add Meson SP... |
366 367 368 369 370 371 372 |
} static int meson_spicc_transfer_one(struct spi_master *master, struct spi_device *spi, struct spi_transfer *xfer) { struct meson_spicc_device *spicc = spi_master_get_devdata(master); |
454fa271b spi: Add Meson SP... |
373 374 375 376 377 378 379 380 381 382 383 384 |
/* Store current transfer */ spicc->xfer = xfer; /* Setup transfer parameters */ spicc->tx_buf = (u8 *)xfer->tx_buf; spicc->rx_buf = (u8 *)xfer->rx_buf; spicc->xfer_remain = xfer->len; /* Pre-calculate word size */ spicc->bytes_per_word = DIV_ROUND_UP(spicc->xfer->bits_per_word, 8); |
0eb707ac7 spi: meson-spicc:... |
385 386 |
if (xfer->len % spicc->bytes_per_word) return -EINVAL; |
454fa271b spi: Add Meson SP... |
387 388 |
/* Setup transfer parameters */ meson_spicc_setup_xfer(spicc, xfer); |
0eb707ac7 spi: meson-spicc:... |
389 |
meson_spicc_reset_fifo(spicc); |
454fa271b spi: Add Meson SP... |
390 |
|
0eb707ac7 spi: meson-spicc:... |
391 392 |
/* Setup burst */ meson_spicc_setup_burst(spicc); |
454fa271b spi: Add Meson SP... |
393 394 395 396 397 |
/* Start burst */ writel_bits_relaxed(SPICC_XCH, SPICC_XCH, spicc->base + SPICC_CONREG); /* Enable interrupts */ |
0eb707ac7 spi: meson-spicc:... |
398 |
writel_relaxed(SPICC_TC_EN, spicc->base + SPICC_INTREG); |
454fa271b spi: Add Meson SP... |
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
return 1; } static int meson_spicc_prepare_message(struct spi_master *master, struct spi_message *message) { struct meson_spicc_device *spicc = spi_master_get_devdata(master); struct spi_device *spi = message->spi; u32 conf = 0; /* Store current message */ spicc->message = message; /* Enable Master */ conf |= SPICC_ENABLE; conf |= SPICC_MODE_MASTER; /* SMC = 0 */ /* Setup transfer mode */ if (spi->mode & SPI_CPOL) conf |= SPICC_POL; else conf &= ~SPICC_POL; if (spi->mode & SPI_CPHA) conf |= SPICC_PHA; else conf &= ~SPICC_PHA; /* SSCTL = 0 */ if (spi->mode & SPI_CS_HIGH) conf |= SPICC_SSPOL; else conf &= ~SPICC_SSPOL; if (spi->mode & SPI_READY) conf |= FIELD_PREP(SPICC_DRCTL_MASK, SPICC_DRCTL_LOWLEVEL); else conf |= FIELD_PREP(SPICC_DRCTL_MASK, SPICC_DRCTL_IGNORE); /* Select CS */ conf |= FIELD_PREP(SPICC_CS_MASK, spi->chip_select); /* Default Clock rate core/4 */ /* Default 8bit word */ conf |= FIELD_PREP(SPICC_BITLENGTH_MASK, 8 - 1); writel_relaxed(conf, spicc->base + SPICC_CONREG); /* Setup no wait cycles by default */ writel_relaxed(0, spicc->base + SPICC_PERIODREG); |
0eb707ac7 spi: meson-spicc:... |
454 |
writel_bits_relaxed(SPICC_LBC_W1, 0, spicc->base + SPICC_TESTREG); |
454fa271b spi: Add Meson SP... |
455 456 457 458 459 460 461 462 463 464 |
return 0; } static int meson_spicc_unprepare_transfer(struct spi_master *master) { struct meson_spicc_device *spicc = spi_master_get_devdata(master); /* Disable all IRQs */ writel(0, spicc->base + SPICC_INTREG); |
454fa271b spi: Add Meson SP... |
465 466 467 468 469 470 471 |
device_reset_optional(&spicc->pdev->dev); return 0; } static int meson_spicc_setup(struct spi_device *spi) { |
454fa271b spi: Add Meson SP... |
472 473 |
if (!spi->controller_state) spi->controller_state = spi_master_get_devdata(spi->master); |
454fa271b spi: Add Meson SP... |
474 |
|
cd8fb859a spi: meson-spicc:... |
475 |
return 0; |
454fa271b spi: Add Meson SP... |
476 477 478 479 |
} static void meson_spicc_cleanup(struct spi_device *spi) { |
454fa271b spi: Add Meson SP... |
480 481 |
spi->controller_state = NULL; } |
3e0cf4d3f spi: meson-spicc:... |
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
/* * The Clock Mux * x-----------------x x------------x x------\ * |---| pow2 fixed div |---| pow2 div |----| | * | x-----------------x x------------x | | * src ---| | mux |-- out * | x-----------------x x------------x | | * |---| enh fixed div |---| enh div |0---| | * x-----------------x x------------x x------/ * * Clk path for GX series: * src -> pow2 fixed div -> pow2 div -> out * * Clk path for AXG series: * src -> pow2 fixed div -> pow2 div -> mux -> out * src -> enh fixed div -> enh div -> mux -> out |
4e3d32205 spi: meson-spicc:... |
498 499 500 501 |
* * Clk path for G12A series: * pclk -> pow2 fixed div -> pow2 div -> mux -> out * pclk -> enh fixed div -> enh div -> mux -> out |
3e0cf4d3f spi: meson-spicc:... |
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
*/ static int meson_spicc_clk_init(struct meson_spicc_device *spicc) { struct device *dev = &spicc->pdev->dev; struct clk_fixed_factor *pow2_fixed_div, *enh_fixed_div; struct clk_divider *pow2_div, *enh_div; struct clk_mux *mux; struct clk_init_data init; struct clk *clk; struct clk_parent_data parent_data[2]; char name[64]; memset(&init, 0, sizeof(init)); memset(&parent_data, 0, sizeof(parent_data)); init.parent_data = parent_data; /* algorithm for pow2 div: rate = freq / 4 / (2 ^ N) */ pow2_fixed_div = devm_kzalloc(dev, sizeof(*pow2_fixed_div), GFP_KERNEL); if (!pow2_fixed_div) return -ENOMEM; snprintf(name, sizeof(name), "%s#pow2_fixed_div", dev_name(dev)); init.name = name; init.ops = &clk_fixed_factor_ops; init.flags = 0; |
4e3d32205 spi: meson-spicc:... |
530 531 532 533 |
if (spicc->data->has_pclk) parent_data[0].hw = __clk_get_hw(spicc->pclk); else parent_data[0].hw = __clk_get_hw(spicc->core); |
3e0cf4d3f spi: meson-spicc:... |
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
init.num_parents = 1; pow2_fixed_div->mult = 1, pow2_fixed_div->div = 4, pow2_fixed_div->hw.init = &init; clk = devm_clk_register(dev, &pow2_fixed_div->hw); if (WARN_ON(IS_ERR(clk))) return PTR_ERR(clk); pow2_div = devm_kzalloc(dev, sizeof(*pow2_div), GFP_KERNEL); if (!pow2_div) return -ENOMEM; snprintf(name, sizeof(name), "%s#pow2_div", dev_name(dev)); init.name = name; init.ops = &clk_divider_ops; init.flags = CLK_SET_RATE_PARENT; parent_data[0].hw = &pow2_fixed_div->hw; init.num_parents = 1; pow2_div->shift = 16, pow2_div->width = 3, pow2_div->flags = CLK_DIVIDER_POWER_OF_TWO, pow2_div->reg = spicc->base + SPICC_CONREG; pow2_div->hw.init = &init; clk = devm_clk_register(dev, &pow2_div->hw); if (WARN_ON(IS_ERR(clk))) return PTR_ERR(clk); if (!spicc->data->has_enhance_clk_div) { spicc->clk = clk; return 0; } /* algorithm for enh div: rate = freq / 2 / (N + 1) */ enh_fixed_div = devm_kzalloc(dev, sizeof(*enh_fixed_div), GFP_KERNEL); if (!enh_fixed_div) return -ENOMEM; snprintf(name, sizeof(name), "%s#enh_fixed_div", dev_name(dev)); init.name = name; init.ops = &clk_fixed_factor_ops; init.flags = 0; |
4e3d32205 spi: meson-spicc:... |
580 581 582 583 |
if (spicc->data->has_pclk) parent_data[0].hw = __clk_get_hw(spicc->pclk); else parent_data[0].hw = __clk_get_hw(spicc->core); |
3e0cf4d3f spi: meson-spicc:... |
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
init.num_parents = 1; enh_fixed_div->mult = 1, enh_fixed_div->div = 2, enh_fixed_div->hw.init = &init; clk = devm_clk_register(dev, &enh_fixed_div->hw); if (WARN_ON(IS_ERR(clk))) return PTR_ERR(clk); enh_div = devm_kzalloc(dev, sizeof(*enh_div), GFP_KERNEL); if (!enh_div) return -ENOMEM; snprintf(name, sizeof(name), "%s#enh_div", dev_name(dev)); init.name = name; init.ops = &clk_divider_ops; init.flags = CLK_SET_RATE_PARENT; parent_data[0].hw = &enh_fixed_div->hw; init.num_parents = 1; enh_div->shift = 16, enh_div->width = 8, enh_div->reg = spicc->base + SPICC_ENH_CTL0; enh_div->hw.init = &init; clk = devm_clk_register(dev, &enh_div->hw); if (WARN_ON(IS_ERR(clk))) return PTR_ERR(clk); mux = devm_kzalloc(dev, sizeof(*mux), GFP_KERNEL); if (!mux) return -ENOMEM; snprintf(name, sizeof(name), "%s#sel", dev_name(dev)); init.name = name; init.ops = &clk_mux_ops; parent_data[0].hw = &pow2_div->hw; parent_data[1].hw = &enh_div->hw; init.num_parents = 2; init.flags = CLK_SET_RATE_PARENT; mux->mask = 0x1, mux->shift = 24, mux->reg = spicc->base + SPICC_ENH_CTL0; mux->hw.init = &init; spicc->clk = devm_clk_register(dev, &mux->hw); if (WARN_ON(IS_ERR(spicc->clk))) return PTR_ERR(spicc->clk); return 0; } |
454fa271b spi: Add Meson SP... |
637 638 639 640 |
static int meson_spicc_probe(struct platform_device *pdev) { struct spi_master *master; struct meson_spicc_device *spicc; |
4e3d32205 spi: meson-spicc:... |
641 |
int ret, irq; |
454fa271b spi: Add Meson SP... |
642 643 644 645 646 647 648 649 650 |
master = spi_alloc_master(&pdev->dev, sizeof(*spicc)); if (!master) { dev_err(&pdev->dev, "master allocation failed "); return -ENOMEM; } spicc = spi_master_get_devdata(master); spicc->master = master; |
a6cda1f90 spi: meson-spicc:... |
651 652 653 654 655 656 657 |
spicc->data = of_device_get_match_data(&pdev->dev); if (!spicc->data) { dev_err(&pdev->dev, "failed to get match data "); ret = -EINVAL; goto out_master; } |
454fa271b spi: Add Meson SP... |
658 659 |
spicc->pdev = pdev; platform_set_drvdata(pdev, spicc); |
362385c04 spi: meson-spicc:... |
660 |
spicc->base = devm_platform_ioremap_resource(pdev, 0); |
454fa271b spi: Add Meson SP... |
661 662 663 664 665 666 |
if (IS_ERR(spicc->base)) { dev_err(&pdev->dev, "io resource mapping failed "); ret = PTR_ERR(spicc->base); goto out_master; } |
3e0cf4d3f spi: meson-spicc:... |
667 668 669 |
/* Set master mode and enable controller */ writel_relaxed(SPICC_ENABLE | SPICC_MODE_MASTER, spicc->base + SPICC_CONREG); |
454fa271b spi: Add Meson SP... |
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
/* Disable all IRQs */ writel_relaxed(0, spicc->base + SPICC_INTREG); irq = platform_get_irq(pdev, 0); ret = devm_request_irq(&pdev->dev, irq, meson_spicc_irq, 0, NULL, spicc); if (ret) { dev_err(&pdev->dev, "irq request failed "); goto out_master; } spicc->core = devm_clk_get(&pdev->dev, "core"); if (IS_ERR(spicc->core)) { dev_err(&pdev->dev, "core clock request failed "); ret = PTR_ERR(spicc->core); goto out_master; } |
4e3d32205 spi: meson-spicc:... |
689 690 691 692 693 694 695 696 697 |
if (spicc->data->has_pclk) { spicc->pclk = devm_clk_get(&pdev->dev, "pclk"); if (IS_ERR(spicc->pclk)) { dev_err(&pdev->dev, "pclk clock request failed "); ret = PTR_ERR(spicc->pclk); goto out_master; } } |
454fa271b spi: Add Meson SP... |
698 699 700 701 702 703 |
ret = clk_prepare_enable(spicc->core); if (ret) { dev_err(&pdev->dev, "core clock enable failed "); goto out_master; } |
4e3d32205 spi: meson-spicc:... |
704 705 706 707 708 709 710 |
ret = clk_prepare_enable(spicc->pclk); if (ret) { dev_err(&pdev->dev, "pclk clock enable failed "); goto out_master; } |
454fa271b spi: Add Meson SP... |
711 712 713 714 715 716 717 718 719 720 721 |
device_reset_optional(&pdev->dev); master->num_chipselect = 4; master->dev.of_node = pdev->dev.of_node; master->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH; master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(24) | SPI_BPW_MASK(16) | SPI_BPW_MASK(8); master->flags = (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX); |
8791068da spi: meson-spicc:... |
722 |
master->min_speed_hz = spicc->data->min_speed_hz; |
4e3d32205 spi: meson-spicc:... |
723 |
master->max_speed_hz = spicc->data->max_speed_hz; |
454fa271b spi: Add Meson SP... |
724 725 726 727 728 |
master->setup = meson_spicc_setup; master->cleanup = meson_spicc_cleanup; master->prepare_message = meson_spicc_prepare_message; master->unprepare_transfer_hardware = meson_spicc_unprepare_transfer; master->transfer_one = meson_spicc_transfer_one; |
cd8fb859a spi: meson-spicc:... |
729 |
master->use_gpio_descriptors = true; |
454fa271b spi: Add Meson SP... |
730 |
|
a6cda1f90 spi: meson-spicc:... |
731 |
meson_spicc_oen_enable(spicc); |
3e0cf4d3f spi: meson-spicc:... |
732 733 734 735 736 737 |
ret = meson_spicc_clk_init(spicc); if (ret) { dev_err(&pdev->dev, "clock registration failed "); goto out_master; } |
454fa271b spi: Add Meson SP... |
738 |
ret = devm_spi_register_master(&pdev->dev, master); |
ded5fa4e8 spi: meson-spicc:... |
739 740 741 742 743 |
if (ret) { dev_err(&pdev->dev, "spi master registration failed "); goto out_clk; } |
454fa271b spi: Add Meson SP... |
744 |
|
ded5fa4e8 spi: meson-spicc:... |
745 746 747 748 |
return 0; out_clk: clk_disable_unprepare(spicc->core); |
4e3d32205 spi: meson-spicc:... |
749 |
clk_disable_unprepare(spicc->pclk); |
454fa271b spi: Add Meson SP... |
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
out_master: spi_master_put(master); return ret; } static int meson_spicc_remove(struct platform_device *pdev) { struct meson_spicc_device *spicc = platform_get_drvdata(pdev); /* Disable SPI */ writel(0, spicc->base + SPICC_CONREG); clk_disable_unprepare(spicc->core); |
4e3d32205 spi: meson-spicc:... |
765 |
clk_disable_unprepare(spicc->pclk); |
454fa271b spi: Add Meson SP... |
766 767 768 |
return 0; } |
a6cda1f90 spi: meson-spicc:... |
769 |
static const struct meson_spicc_data meson_spicc_gx_data = { |
3196816ff spi: meson-spicc:... |
770 |
.max_speed_hz = 30000000, |
8791068da spi: meson-spicc:... |
771 |
.min_speed_hz = 325000, |
0eb707ac7 spi: meson-spicc:... |
772 |
.fifo_size = 16, |
a6cda1f90 spi: meson-spicc:... |
773 774 775 |
}; static const struct meson_spicc_data meson_spicc_axg_data = { |
3196816ff spi: meson-spicc:... |
776 |
.max_speed_hz = 80000000, |
8791068da spi: meson-spicc:... |
777 |
.min_speed_hz = 325000, |
0eb707ac7 spi: meson-spicc:... |
778 |
.fifo_size = 16, |
a6cda1f90 spi: meson-spicc:... |
779 |
.has_oen = true, |
3e0cf4d3f spi: meson-spicc:... |
780 |
.has_enhance_clk_div = true, |
a6cda1f90 spi: meson-spicc:... |
781 |
}; |
4e3d32205 spi: meson-spicc:... |
782 783 784 785 786 787 788 789 |
static const struct meson_spicc_data meson_spicc_g12a_data = { .max_speed_hz = 166666666, .min_speed_hz = 50000, .fifo_size = 15, .has_oen = true, .has_enhance_clk_div = true, .has_pclk = true, }; |
454fa271b spi: Add Meson SP... |
790 |
static const struct of_device_id meson_spicc_of_match[] = { |
a6cda1f90 spi: meson-spicc:... |
791 792 793 794 795 796 797 798 |
{ .compatible = "amlogic,meson-gx-spicc", .data = &meson_spicc_gx_data, }, { .compatible = "amlogic,meson-axg-spicc", .data = &meson_spicc_axg_data, }, |
4e3d32205 spi: meson-spicc:... |
799 800 801 802 |
{ .compatible = "amlogic,meson-g12a-spicc", .data = &meson_spicc_g12a_data, }, |
454fa271b spi: Add Meson SP... |
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 |
{ /* sentinel */ } }; MODULE_DEVICE_TABLE(of, meson_spicc_of_match); static struct platform_driver meson_spicc_driver = { .probe = meson_spicc_probe, .remove = meson_spicc_remove, .driver = { .name = "meson-spicc", .of_match_table = of_match_ptr(meson_spicc_of_match), }, }; module_platform_driver(meson_spicc_driver); MODULE_DESCRIPTION("Meson SPI Communication Controller driver"); MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>"); MODULE_LICENSE("GPL"); |