Lines Matching +full:mpc5200 +full:- +full:spi
1 // SPDX-License-Identifier: GPL-2.0-only
3 * MPC52xx SPI bus driver.
7 * This is the driver for the MPC5200's dedicated SPI controller.
9 * Note: this driver does not support the MPC5200 PSC in SPI mode. For
10 * that driver see drivers/spi/mpc52xx_psc_spi.c
18 #include <linux/spi/spi.h>
26 MODULE_DESCRIPTION("MPC52xx SPI (non-PSC) Driver");
99 if (ms->gpio_cs_count > 0) { in mpc52xx_spi_chipsel()
100 cs = ms->message->spi->chip_select; in mpc52xx_spi_chipsel()
101 gpio_set_value(ms->gpio_cs[cs], value ? 0 : 1); in mpc52xx_spi_chipsel()
103 out_8(ms->regs + SPI_PORTDATA, value ? 0 : 0x08); in mpc52xx_spi_chipsel()
113 ms->rx_buf = ms->transfer->rx_buf; in mpc52xx_spi_start_transfer()
114 ms->tx_buf = ms->transfer->tx_buf; in mpc52xx_spi_start_transfer()
115 ms->len = ms->transfer->len; in mpc52xx_spi_start_transfer()
118 if (ms->cs_change) in mpc52xx_spi_start_transfer()
120 ms->cs_change = ms->transfer->cs_change; in mpc52xx_spi_start_transfer()
123 ms->wcol_tx_timestamp = get_tbl(); in mpc52xx_spi_start_transfer()
124 if (ms->tx_buf) in mpc52xx_spi_start_transfer()
125 out_8(ms->regs + SPI_DATA, *ms->tx_buf++); in mpc52xx_spi_start_transfer()
127 out_8(ms->regs + SPI_DATA, 0); in mpc52xx_spi_start_transfer()
145 struct spi_device *spi; in mpc52xx_spi_fsmstate_idle() local
150 dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n", in mpc52xx_spi_fsmstate_idle()
154 if (list_empty(&ms->queue)) in mpc52xx_spi_fsmstate_idle()
158 ms->message = list_first_entry(&ms->queue, struct spi_message, queue); in mpc52xx_spi_fsmstate_idle()
159 list_del_init(&ms->message->queue); in mpc52xx_spi_fsmstate_idle()
163 spi = ms->message->spi; in mpc52xx_spi_fsmstate_idle()
164 if (spi->mode & SPI_CPHA) in mpc52xx_spi_fsmstate_idle()
166 if (spi->mode & SPI_CPOL) in mpc52xx_spi_fsmstate_idle()
168 if (spi->mode & SPI_LSB_FIRST) in mpc52xx_spi_fsmstate_idle()
170 out_8(ms->regs + SPI_CTRL1, ctrl1); in mpc52xx_spi_fsmstate_idle()
175 sppr = ((ms->ipb_freq / ms->message->spi->max_speed_hz) + 1) >> 1; in mpc52xx_spi_fsmstate_idle()
179 while (((sppr - 1) & ~0x7) != 0) { in mpc52xx_spi_fsmstate_idle()
183 sppr--; /* sppr quantity in register is offset by 1 */ in mpc52xx_spi_fsmstate_idle()
185 /* Don't overrun limits of SPI baudrate register */ in mpc52xx_spi_fsmstate_idle()
189 out_8(ms->regs + SPI_BRR, sppr << 4 | spr); /* Set speed */ in mpc52xx_spi_fsmstate_idle()
191 ms->cs_change = 1; in mpc52xx_spi_fsmstate_idle()
192 ms->transfer = container_of(ms->message->transfers.next, in mpc52xx_spi_fsmstate_idle()
196 ms->state = mpc52xx_spi_fsmstate_transfer; in mpc52xx_spi_fsmstate_idle()
204 * In the middle of a transfer. If the SPI core has completed processing
213 return ms->irq0 ? FSM_STOP : FSM_POLL; in mpc52xx_spi_fsmstate_transfer()
216 /* The SPI controller is stoopid. At slower speeds, it may in mpc52xx_spi_fsmstate_transfer()
223 ms->wcol_count++; in mpc52xx_spi_fsmstate_transfer()
224 ms->wcol_ticks += get_tbl() - ms->wcol_tx_timestamp; in mpc52xx_spi_fsmstate_transfer()
225 ms->wcol_tx_timestamp = get_tbl(); in mpc52xx_spi_fsmstate_transfer()
227 if (ms->tx_buf) in mpc52xx_spi_fsmstate_transfer()
228 data = *(ms->tx_buf - 1); in mpc52xx_spi_fsmstate_transfer()
229 out_8(ms->regs + SPI_DATA, data); /* try again */ in mpc52xx_spi_fsmstate_transfer()
232 ms->modf_count++; in mpc52xx_spi_fsmstate_transfer()
233 dev_err(&ms->master->dev, "mode fault\n"); in mpc52xx_spi_fsmstate_transfer()
235 ms->message->status = -EIO; in mpc52xx_spi_fsmstate_transfer()
236 if (ms->message->complete) in mpc52xx_spi_fsmstate_transfer()
237 ms->message->complete(ms->message->context); in mpc52xx_spi_fsmstate_transfer()
238 ms->state = mpc52xx_spi_fsmstate_idle; in mpc52xx_spi_fsmstate_transfer()
242 /* Read data out of the spi device */ in mpc52xx_spi_fsmstate_transfer()
243 ms->byte_count++; in mpc52xx_spi_fsmstate_transfer()
244 if (ms->rx_buf) in mpc52xx_spi_fsmstate_transfer()
245 *ms->rx_buf++ = data; in mpc52xx_spi_fsmstate_transfer()
248 ms->len--; in mpc52xx_spi_fsmstate_transfer()
249 if (ms->len == 0) { in mpc52xx_spi_fsmstate_transfer()
250 ms->timestamp = get_tbl(); in mpc52xx_spi_fsmstate_transfer()
251 ms->timestamp += ms->transfer->delay_usecs * tb_ticks_per_usec; in mpc52xx_spi_fsmstate_transfer()
252 ms->state = mpc52xx_spi_fsmstate_wait; in mpc52xx_spi_fsmstate_transfer()
257 ms->wcol_tx_timestamp = get_tbl(); in mpc52xx_spi_fsmstate_transfer()
258 if (ms->tx_buf) in mpc52xx_spi_fsmstate_transfer()
259 out_8(ms->regs + SPI_DATA, *ms->tx_buf++); in mpc52xx_spi_fsmstate_transfer()
261 out_8(ms->regs + SPI_DATA, 0); in mpc52xx_spi_fsmstate_transfer()
276 dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n", in mpc52xx_spi_fsmstate_wait()
279 if (((int)get_tbl()) - ms->timestamp < 0) in mpc52xx_spi_fsmstate_wait()
282 ms->message->actual_length += ms->transfer->len; in mpc52xx_spi_fsmstate_wait()
287 if (ms->transfer->transfer_list.next == &ms->message->transfers) { in mpc52xx_spi_fsmstate_wait()
288 ms->msg_count++; in mpc52xx_spi_fsmstate_wait()
290 ms->message->status = 0; in mpc52xx_spi_fsmstate_wait()
291 if (ms->message->complete) in mpc52xx_spi_fsmstate_wait()
292 ms->message->complete(ms->message->context); in mpc52xx_spi_fsmstate_wait()
293 ms->state = mpc52xx_spi_fsmstate_idle; in mpc52xx_spi_fsmstate_wait()
299 if (ms->cs_change) in mpc52xx_spi_fsmstate_wait()
302 ms->transfer = container_of(ms->transfer->transfer_list.next, in mpc52xx_spi_fsmstate_wait()
305 ms->state = mpc52xx_spi_fsmstate_transfer; in mpc52xx_spi_fsmstate_wait()
310 * mpc52xx_spi_fsm_process - Finite State Machine iteration function
322 status = in_8(ms->regs + SPI_STATUS); in mpc52xx_spi_fsm_process()
323 data = in_8(ms->regs + SPI_DATA); in mpc52xx_spi_fsm_process()
324 rc = ms->state(irq, ms, status, data); in mpc52xx_spi_fsm_process()
328 schedule_work(&ms->work); in mpc52xx_spi_fsm_process()
332 * mpc52xx_spi_irq - IRQ handler
337 spin_lock(&ms->lock); in mpc52xx_spi_irq()
339 spin_unlock(&ms->lock); in mpc52xx_spi_irq()
344 * mpc52xx_spi_wq - Workqueue function for polling the state machine
351 spin_lock_irqsave(&ms->lock, flags); in mpc52xx_spi_wq()
353 spin_unlock_irqrestore(&ms->lock, flags); in mpc52xx_spi_wq()
360 static int mpc52xx_spi_transfer(struct spi_device *spi, struct spi_message *m) in mpc52xx_spi_transfer() argument
362 struct mpc52xx_spi *ms = spi_master_get_devdata(spi->master); in mpc52xx_spi_transfer()
365 m->actual_length = 0; in mpc52xx_spi_transfer()
366 m->status = -EINPROGRESS; in mpc52xx_spi_transfer()
368 spin_lock_irqsave(&ms->lock, flags); in mpc52xx_spi_transfer()
369 list_add_tail(&m->queue, &ms->queue); in mpc52xx_spi_transfer()
370 spin_unlock_irqrestore(&ms->lock, flags); in mpc52xx_spi_transfer()
371 schedule_work(&ms->work); in mpc52xx_spi_transfer()
389 dev_dbg(&op->dev, "probing mpc5200 SPI device\n"); in mpc52xx_spi_probe()
390 regs = of_iomap(op->dev.of_node, 0); in mpc52xx_spi_probe()
392 return -ENODEV; in mpc52xx_spi_probe()
401 /* Clear the status register and re-read it to check for a MODF in mpc52xx_spi_probe()
403 * on the SPI bus. This fault will also occur if the SPI signals in mpc52xx_spi_probe()
410 dev_err(&op->dev, "mode fault; is port_config correct?\n"); in mpc52xx_spi_probe()
411 rc = -EIO; in mpc52xx_spi_probe()
415 dev_dbg(&op->dev, "allocating spi_master struct\n"); in mpc52xx_spi_probe()
416 master = spi_alloc_master(&op->dev, sizeof *ms); in mpc52xx_spi_probe()
418 rc = -ENOMEM; in mpc52xx_spi_probe()
422 master->transfer = mpc52xx_spi_transfer; in mpc52xx_spi_probe()
423 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST; in mpc52xx_spi_probe()
424 master->bits_per_word_mask = SPI_BPW_MASK(8); in mpc52xx_spi_probe()
425 master->dev.of_node = op->dev.of_node; in mpc52xx_spi_probe()
430 ms->master = master; in mpc52xx_spi_probe()
431 ms->regs = regs; in mpc52xx_spi_probe()
432 ms->irq0 = irq_of_parse_and_map(op->dev.of_node, 0); in mpc52xx_spi_probe()
433 ms->irq1 = irq_of_parse_and_map(op->dev.of_node, 1); in mpc52xx_spi_probe()
434 ms->state = mpc52xx_spi_fsmstate_idle; in mpc52xx_spi_probe()
435 ms->ipb_freq = mpc5xxx_get_bus_frequency(op->dev.of_node); in mpc52xx_spi_probe()
436 ms->gpio_cs_count = of_gpio_count(op->dev.of_node); in mpc52xx_spi_probe()
437 if (ms->gpio_cs_count > 0) { in mpc52xx_spi_probe()
438 master->num_chipselect = ms->gpio_cs_count; in mpc52xx_spi_probe()
439 ms->gpio_cs = kmalloc_array(ms->gpio_cs_count, in mpc52xx_spi_probe()
440 sizeof(*ms->gpio_cs), in mpc52xx_spi_probe()
442 if (!ms->gpio_cs) { in mpc52xx_spi_probe()
443 rc = -ENOMEM; in mpc52xx_spi_probe()
447 for (i = 0; i < ms->gpio_cs_count; i++) { in mpc52xx_spi_probe()
448 gpio_cs = of_get_gpio(op->dev.of_node, i); in mpc52xx_spi_probe()
450 dev_err(&op->dev, in mpc52xx_spi_probe()
452 rc = -ENODEV; in mpc52xx_spi_probe()
456 rc = gpio_request(gpio_cs, dev_name(&op->dev)); in mpc52xx_spi_probe()
458 dev_err(&op->dev, in mpc52xx_spi_probe()
459 "can't request spi cs gpio #%d on gpio line %d\n", in mpc52xx_spi_probe()
465 ms->gpio_cs[i] = gpio_cs; in mpc52xx_spi_probe()
469 spin_lock_init(&ms->lock); in mpc52xx_spi_probe()
470 INIT_LIST_HEAD(&ms->queue); in mpc52xx_spi_probe()
471 INIT_WORK(&ms->work, mpc52xx_spi_wq); in mpc52xx_spi_probe()
474 if (ms->irq0 && ms->irq1) { in mpc52xx_spi_probe()
475 rc = request_irq(ms->irq0, mpc52xx_spi_irq, 0, in mpc52xx_spi_probe()
476 "mpc5200-spi-modf", ms); in mpc52xx_spi_probe()
477 rc |= request_irq(ms->irq1, mpc52xx_spi_irq, 0, in mpc52xx_spi_probe()
478 "mpc5200-spi-spif", ms); in mpc52xx_spi_probe()
480 free_irq(ms->irq0, ms); in mpc52xx_spi_probe()
481 free_irq(ms->irq1, ms); in mpc52xx_spi_probe()
482 ms->irq0 = ms->irq1 = 0; in mpc52xx_spi_probe()
486 ms->irq0 = ms->irq1 = 0; in mpc52xx_spi_probe()
489 if (!ms->irq0) in mpc52xx_spi_probe()
490 dev_info(&op->dev, "using polled mode\n"); in mpc52xx_spi_probe()
492 dev_dbg(&op->dev, "registering spi_master struct\n"); in mpc52xx_spi_probe()
497 dev_info(&ms->master->dev, "registered MPC5200 SPI bus\n"); in mpc52xx_spi_probe()
502 dev_err(&ms->master->dev, "initialization failed\n"); in mpc52xx_spi_probe()
504 while (i-- > 0) in mpc52xx_spi_probe()
505 gpio_free(ms->gpio_cs[i]); in mpc52xx_spi_probe()
507 kfree(ms->gpio_cs); in mpc52xx_spi_probe()
522 free_irq(ms->irq0, ms); in mpc52xx_spi_remove()
523 free_irq(ms->irq1, ms); in mpc52xx_spi_remove()
525 for (i = 0; i < ms->gpio_cs_count; i++) in mpc52xx_spi_remove()
526 gpio_free(ms->gpio_cs[i]); in mpc52xx_spi_remove()
528 kfree(ms->gpio_cs); in mpc52xx_spi_remove()
530 iounmap(ms->regs); in mpc52xx_spi_remove()
537 { .compatible = "fsl,mpc5200-spi", },
544 .name = "mpc52xx-spi",