Lines Matching +full:spi +full:- +full:controller

1 // SPDX-License-Identifier: GPL-2.0
5 * Driver for KEBA SPI host controller type 2 FPGA IP core
10 #include <linux/spi/spi.h>
42 #define KSPI2_SPEED_HZ_MIN(kspi) (kspi->base_speed_hz / 65536)
43 #define KSPI2_SPEED_HZ_MAX(kspi) (kspi->base_speed_hz / 2)
57 u32 base_speed_hz; /* SPI base clock frequency in HZ */
70 * The SPI controller has an IN_USE bit for locking access to the in kspi2_inuse_lock()
71 * controller. This enables the use of the SPI controller by other none in kspi2_inuse_lock()
74 * If the SPI controller is free, then the first read returns in kspi2_inuse_lock()
75 * IN_USE == 0. After that the SPI controller is locked and further in kspi2_inuse_lock()
78 * The SPI controller is unlocked by writing 1 into IN_USE. in kspi2_inuse_lock()
80 * The IN_USE bit acts as a hardware semaphore for the SPI controller. in kspi2_inuse_lock()
83 ret = readb_poll_timeout(kspi->base + KSPI2_STATUS_REG, in kspi2_inuse_lock()
87 dev_warn(&kspi->auxdev->auxdev.dev, "%s err!\n", __func__); in kspi2_inuse_lock()
94 /* unlock the controller by writing 1 into IN_USE */ in kspi2_inuse_unlock()
95 iowrite8(KSPI2_STATUS_IN_USE, kspi->base + KSPI2_STATUS_REG); in kspi2_inuse_unlock()
102 /* lock hardware semaphore before actual use of controller */ in kspi2_prepare_hardware()
110 /* unlock hardware semaphore after actual use of controller */ in kspi2_unprepare_hardware()
125 if ((kspi->base_speed_hz >> (div + 1)) <= max_speed_hz) in kspi2_calc_minimal_divider()
136 if (val != (kspi->control_shadow & mask)) { in kspi2_write_control_reg()
137 kspi->control_shadow = (kspi->control_shadow & ~mask) | val; in kspi2_write_control_reg()
138 iowrite8(kspi->control_shadow, kspi->base + KSPI2_CONTROL_REG); in kspi2_write_control_reg()
148 iowrite8(tx, kspi->base + KSPI2_DATA_REG); in kspi2_txrx_byte()
151 ret = readb_poll_timeout(kspi->base + KSPI2_STATUS_REG, in kspi2_txrx_byte()
159 *rx = ioread8(kspi->base + KSPI2_DATA_REG); in kspi2_txrx_byte()
171 for (i = 0; i < t->len; i++) { in kspi2_process_transfer()
172 if (t->tx_buf) in kspi2_process_transfer()
173 tx = ((const u8 *)t->tx_buf)[i]; in kspi2_process_transfer()
179 if (t->rx_buf) in kspi2_process_transfer()
180 ((u8 *)t->rx_buf)[i] = rx; in kspi2_process_transfer()
187 struct spi_device *spi, in kspi2_setup_transfer() argument
190 u32 max_speed_hz = spi->max_speed_hz; in kspi2_setup_transfer()
194 * spi_device (spi) has default parameters. Some of these can be in kspi2_setup_transfer()
197 if (t->bits_per_word && ((t->bits_per_word % 8) != 0)) { in kspi2_setup_transfer()
198 dev_err(&spi->dev, "Word width %d not supported!\n", in kspi2_setup_transfer()
199 t->bits_per_word); in kspi2_setup_transfer()
201 return -EINVAL; in kspi2_setup_transfer()
204 if (t->speed_hz && (t->speed_hz < max_speed_hz)) in kspi2_setup_transfer()
205 max_speed_hz = t->speed_hz; in kspi2_setup_transfer()
214 struct spi_device *spi, in kspi2_transfer_one() argument
220 ret = kspi2_setup_transfer(kspi, spi, t); in kspi2_transfer_one()
224 if (t->len) { in kspi2_transfer_one()
233 static void kspi2_set_cs(struct spi_device *spi, bool enable) in kspi2_set_cs() argument
235 struct spi_controller *host = spi->controller; in kspi2_set_cs()
238 /* controller is using active low chip select signals by design */ in kspi2_set_cs()
240 iowrite8(spi_get_chipselect(spi, 0), kspi->base + KSPI2_CS_NR_REG); in kspi2_set_cs()
242 iowrite8(KSPI2_CS_NR_NONE, kspi->base + KSPI2_CS_NR_REG); in kspi2_set_cs()
249 struct spi_device *spi = msg->spi; in kspi2_prepare_message() local
252 /* setup SPI clock phase and polarity */ in kspi2_prepare_message()
253 if (spi->mode & SPI_CPHA) in kspi2_prepare_message()
255 if (spi->mode & SPI_CPOL) in kspi2_prepare_message()
262 static int kspi2_setup(struct spi_device *spi) in kspi2_setup() argument
264 struct kspi2 *kspi = spi_controller_get_devdata(spi->controller); in kspi2_setup()
268 * and directly before the SPI transfer starts. in kspi2_setup()
271 if (spi->mode & ~KSPI2_MODE_BITS) { in kspi2_setup()
272 dev_err(&spi->dev, "Mode %d not supported!\n", spi->mode); in kspi2_setup()
274 return -EINVAL; in kspi2_setup()
277 if ((spi->bits_per_word % 8) != 0) { in kspi2_setup()
278 dev_err(&spi->dev, "Word width %d not supported!\n", in kspi2_setup()
279 spi->bits_per_word); in kspi2_setup()
281 return -EINVAL; in kspi2_setup()
284 if ((spi->max_speed_hz == 0) || in kspi2_setup()
285 (spi->max_speed_hz > KSPI2_SPEED_HZ_MAX(kspi))) in kspi2_setup()
286 spi->max_speed_hz = KSPI2_SPEED_HZ_MAX(kspi); in kspi2_setup()
288 if (spi->max_speed_hz < KSPI2_SPEED_HZ_MIN(kspi)) { in kspi2_setup()
289 dev_err(&spi->dev, "Requested speed of %d Hz is too low!\n", in kspi2_setup()
290 spi->max_speed_hz); in kspi2_setup()
292 return -EINVAL; in kspi2_setup()
302 for (i = 0; i < kspi->device_size; i++) { in kspi2_unregister_devices()
303 struct spi_device *device = kspi->device[i]; in kspi2_unregister_devices()
312 struct spi_board_info *info = kspi->auxdev->info; in kspi2_register_devices()
315 /* register all known SPI devices */ in kspi2_register_devices()
316 for (i = 0; i < kspi->auxdev->info_size; i++) { in kspi2_register_devices()
317 struct spi_device *device = spi_new_device(kspi->host, &info[i]); in kspi2_register_devices()
322 return -ENODEV; in kspi2_register_devices()
324 kspi->device[i] = device; in kspi2_register_devices()
332 iowrite8(KSPI2_CONTROL_INIT, kspi->base + KSPI2_CONTROL_REG); in kspi2_init()
333 kspi->control_shadow = KSPI2_CONTROL_INIT; in kspi2_init()
335 iowrite8(KSPI2_CS_NR_NONE, kspi->base + KSPI2_CS_NR_REG); in kspi2_init()
341 struct device *dev = &auxdev->dev; in kspi2_probe()
349 return -ENOMEM; in kspi2_probe()
351 kspi->auxdev = container_of(auxdev, struct keba_spi_auxdev, auxdev); in kspi2_probe()
352 kspi->host = host; in kspi2_probe()
353 kspi->device = devm_kcalloc(dev, kspi->auxdev->info_size, in kspi2_probe()
354 sizeof(*kspi->device), GFP_KERNEL); in kspi2_probe()
355 if (!kspi->device) in kspi2_probe()
356 return -ENOMEM; in kspi2_probe()
357 kspi->device_size = kspi->auxdev->info_size; in kspi2_probe()
360 kspi->base = devm_ioremap_resource(dev, &kspi->auxdev->io); in kspi2_probe()
361 if (IS_ERR(kspi->base)) in kspi2_probe()
362 return PTR_ERR(kspi->base); in kspi2_probe()
364 /* read the SPI base clock frequency */ in kspi2_probe()
365 clk_reg = ioread8(kspi->base + KSPI2_CLK_FREQ_REG); in kspi2_probe()
368 kspi->base_speed_hz = 62500000; break; in kspi2_probe()
370 kspi->base_speed_hz = 33333333; break; in kspi2_probe()
372 kspi->base_speed_hz = 125000000; break; in kspi2_probe()
374 kspi->base_speed_hz = 50000000; break; in kspi2_probe()
376 kspi->base_speed_hz = 100000000; break; in kspi2_probe()
378 dev_err(dev, "Undefined SPI base clock frequency!\n"); in kspi2_probe()
379 return -ENODEV; in kspi2_probe()
384 host->bus_num = -1; in kspi2_probe()
385 host->num_chipselect = KSPI2_NUM_CS; in kspi2_probe()
386 host->mode_bits = KSPI2_MODE_BITS; in kspi2_probe()
387 host->setup = kspi2_setup; in kspi2_probe()
388 host->prepare_transfer_hardware = kspi2_prepare_hardware; in kspi2_probe()
389 host->unprepare_transfer_hardware = kspi2_unprepare_hardware; in kspi2_probe()
390 host->prepare_message = kspi2_prepare_message; in kspi2_probe()
391 host->set_cs = kspi2_set_cs; in kspi2_probe()
392 host->transfer_one = kspi2_transfer_one; in kspi2_probe()
416 { .name = "keba.spi" },
430 MODULE_DESCRIPTION("KEBA SPI host controller driver");