2 * MPC512x PSC in SPI mode driver.
4 * Copyright (C) 2007,2008 Freescale Semiconductor Inc.
5 * Original port from 52xx driver:
6 * Hongjun Chen <hong-jun.chen@freescale.com>
8 * Fork of mpc52xx_psc_spi.c:
9 * Copyright (C) 2006 TOPTICA Photonics AG., Dragos Carp
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/errno.h>
21 #include <linux/interrupt.h>
22 #include <linux/of_address.h>
23 #include <linux/of_platform.h>
24 #include <linux/completion.h>
26 #include <linux/delay.h>
27 #include <linux/clk.h>
28 #include <linux/spi/spi.h>
29 #include <linux/fsl_devices.h>
30 #include <linux/gpio.h>
31 #include <asm/mpc52xx_psc.h>
33 struct mpc512x_psc_spi {
34 void (*cs_control)(struct spi_device *spi, bool on);
36 /* driver internal data */
37 struct mpc52xx_psc __iomem *psc;
38 struct mpc512x_psc_fifo __iomem *fifo;
44 struct completion txisrdone;
47 /* controller state */
48 struct mpc512x_psc_spi_cs {
53 /* set clock freq, clock ramp, bits per work
54 * if t is NULL then reset the values to the default values
56 static int mpc512x_psc_spi_transfer_setup(struct spi_device *spi,
57 struct spi_transfer *t)
59 struct mpc512x_psc_spi_cs *cs = spi->controller_state;
61 cs->speed_hz = (t && t->speed_hz)
62 ? t->speed_hz : spi->max_speed_hz;
63 cs->bits_per_word = (t && t->bits_per_word)
64 ? t->bits_per_word : spi->bits_per_word;
65 cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8;
69 static void mpc512x_psc_spi_activate_cs(struct spi_device *spi)
71 struct mpc512x_psc_spi_cs *cs = spi->controller_state;
72 struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
73 struct mpc52xx_psc __iomem *psc = mps->psc;
79 sicr = in_be32(&psc->sicr);
81 /* Set clock phase and polarity */
82 if (spi->mode & SPI_CPHA)
87 if (spi->mode & SPI_CPOL)
92 if (spi->mode & SPI_LSB_FIRST)
96 out_be32(&psc->sicr, sicr);
98 ccr = in_be32(&psc->ccr);
100 speed = cs->speed_hz;
102 speed = 1000000; /* default 1MHz */
103 bclkdiv = (mps->mclk_rate / speed) - 1;
105 ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
106 out_be32(&psc->ccr, ccr);
107 mps->bits_per_word = cs->bits_per_word;
109 if (mps->cs_control && gpio_is_valid(spi->cs_gpio))
110 mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 1 : 0);
113 static void mpc512x_psc_spi_deactivate_cs(struct spi_device *spi)
115 struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
117 if (mps->cs_control && gpio_is_valid(spi->cs_gpio))
118 mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
122 /* extract and scale size field in txsz or rxsz */
123 #define MPC512x_PSC_FIFO_SZ(sz) ((sz & 0x7ff) << 2);
127 static int mpc512x_psc_spi_transfer_rxtx(struct spi_device *spi,
128 struct spi_transfer *t)
130 struct mpc512x_psc_spi *mps = spi_master_get_devdata(spi->master);
131 struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
132 size_t tx_len = t->len;
133 size_t rx_len = t->len;
134 u8 *tx_buf = (u8 *)t->tx_buf;
135 u8 *rx_buf = (u8 *)t->rx_buf;
137 if (!tx_buf && !rx_buf && t->len)
140 while (rx_len || tx_len) {
148 * send the TX bytes in as large a chunk as possible
149 * but neither exceed the TX nor the RX FIFOs
151 fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->txsz));
152 txcount = min(fifosz, tx_len);
153 fifosz = MPC512x_PSC_FIFO_SZ(in_be32(&fifo->rxsz));
154 fifosz -= in_be32(&fifo->rxcnt) + 1;
155 txcount = min(fifosz, txcount);
158 /* fill the TX FIFO */
159 while (txcount-- > 0) {
160 data = tx_buf ? *tx_buf++ : 0;
161 if (tx_len == EOFBYTE && t->cs_change)
162 setbits32(&fifo->txcmd,
163 MPC512x_PSC_FIFO_EOF);
164 out_8(&fifo->txdata_8, data);
168 /* have the ISR trigger when the TX FIFO is empty */
169 INIT_COMPLETION(mps->txisrdone);
170 out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
171 out_be32(&fifo->tximr, MPC512x_PSC_FIFO_EMPTY);
172 wait_for_completion(&mps->txisrdone);
176 * consume as much RX data as the FIFO holds, while we
177 * iterate over the transfer's TX data length
179 * only insist in draining all the remaining RX bytes
180 * when the TX bytes were exhausted (that's at the very
181 * end of this transfer, not when still iterating over
182 * the transfer's chunks)
188 * grab whatever was in the FIFO when we started
189 * looking, don't bother fetching what was added to
190 * the FIFO while we read from it -- we'll return
191 * here eventually and prefer sending out remaining
194 fifosz = in_be32(&fifo->rxcnt);
195 rxcount = min(fifosz, rx_len);
196 while (rxcount-- > 0) {
197 data = in_8(&fifo->rxdata_8);
204 * come back later if there still is TX data to send,
205 * bail out of the RX drain loop if all of the TX data
206 * was sent and all of the RX data was received (i.e.
207 * when the transmission has completed)
215 * TX data transmission has completed while RX data
216 * is still pending -- that's a transient situation
217 * which depends on wire speed and specific
218 * hardware implementation details (buffering) yet
219 * should resolve very quickly
221 * just yield for a moment to not hog the CPU for
222 * too long when running SPI at low speed
224 * the timeout range is rather arbitrary and tries
225 * to balance throughput against system load; the
226 * chosen values result in a minimal timeout of 50
227 * times 10us and thus work at speeds as low as
228 * some 20kbps, while the maximum timeout at the
229 * transfer's end could be 5ms _if_ nothing else
230 * ticks in the system _and_ RX data still wasn't
231 * received, which only occurs in situations that
232 * are exceptional; removing the unpredictability
233 * of the timeout either decreases throughput
234 * (longer timeouts), or puts more load on the
235 * system (fixed short timeouts) or requires the
236 * use of a timeout API instead of a counter and an
237 * unknown inner delay
239 usleep_range(10, 100);
241 } while (--rxtries > 0);
242 if (!tx_len && rx_len && !rxtries) {
244 * not enough RX bytes even after several retries
245 * and the resulting rather long timeout?
247 rxcount = in_be32(&fifo->rxcnt);
249 "short xfer, missing %zd RX bytes, FIFO level %zd\n",
254 * drain and drop RX data which "should not be there" in
255 * the first place, for undisturbed transmission this turns
256 * into a NOP (except for the FIFO level fetch)
258 if (!tx_len && !rx_len) {
259 while (in_be32(&fifo->rxcnt))
260 in_8(&fifo->rxdata_8);
267 static int mpc512x_psc_spi_msg_xfer(struct spi_master *master,
268 struct spi_message *m)
270 struct spi_device *spi;
273 struct spi_transfer *t;
278 list_for_each_entry(t, &m->transfers, transfer_list) {
279 if (t->bits_per_word || t->speed_hz) {
280 status = mpc512x_psc_spi_transfer_setup(spi, t);
286 mpc512x_psc_spi_activate_cs(spi);
287 cs_change = t->cs_change;
289 status = mpc512x_psc_spi_transfer_rxtx(spi, t);
292 m->actual_length += t->len;
295 udelay(t->delay_usecs);
298 mpc512x_psc_spi_deactivate_cs(spi);
302 m->complete(m->context);
304 if (status || !cs_change)
305 mpc512x_psc_spi_deactivate_cs(spi);
307 mpc512x_psc_spi_transfer_setup(spi, NULL);
309 spi_finalize_current_message(master);
313 static int mpc512x_psc_spi_prep_xfer_hw(struct spi_master *master)
315 struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
316 struct mpc52xx_psc __iomem *psc = mps->psc;
318 dev_dbg(&master->dev, "%s()\n", __func__);
322 out_8(&psc->mode, 0x0);
324 /* enable transmitter/receiver */
325 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
330 static int mpc512x_psc_spi_unprep_xfer_hw(struct spi_master *master)
332 struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
333 struct mpc52xx_psc __iomem *psc = mps->psc;
334 struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
336 dev_dbg(&master->dev, "%s()\n", __func__);
338 /* disable transmitter/receiver and fifo interrupt */
339 out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
340 out_be32(&fifo->tximr, 0);
345 static int mpc512x_psc_spi_setup(struct spi_device *spi)
347 struct mpc512x_psc_spi_cs *cs = spi->controller_state;
350 if (spi->bits_per_word % 8)
354 cs = kzalloc(sizeof *cs, GFP_KERNEL);
358 if (gpio_is_valid(spi->cs_gpio)) {
359 ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
361 dev_err(&spi->dev, "can't get CS gpio: %d\n",
366 gpio_direction_output(spi->cs_gpio,
367 spi->mode & SPI_CS_HIGH ? 0 : 1);
370 spi->controller_state = cs;
373 cs->bits_per_word = spi->bits_per_word;
374 cs->speed_hz = spi->max_speed_hz;
379 static void mpc512x_psc_spi_cleanup(struct spi_device *spi)
381 if (gpio_is_valid(spi->cs_gpio))
382 gpio_free(spi->cs_gpio);
383 kfree(spi->controller_state);
386 static int mpc512x_psc_spi_port_config(struct spi_master *master,
387 struct mpc512x_psc_spi *mps)
389 struct mpc52xx_psc __iomem *psc = mps->psc;
390 struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
396 /* Reset the PSC into a known state */
397 out_8(&psc->command, MPC52xx_PSC_RST_RX);
398 out_8(&psc->command, MPC52xx_PSC_RST_TX);
399 out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
401 /* Disable psc interrupts all useful interrupts are in fifo */
402 out_be16(&psc->isr_imr.imr, 0);
404 /* Disable fifo interrupts, will be enabled later */
405 out_be32(&fifo->tximr, 0);
406 out_be32(&fifo->rximr, 0);
408 /* Setup fifo slice address and size */
409 /*out_be32(&fifo->txsz, 0x0fe00004);*/
410 /*out_be32(&fifo->rxsz, 0x0ff00004);*/
412 sicr = 0x01000000 | /* SIM = 0001 -- 8 bit */
413 0x00800000 | /* GenClk = 1 -- internal clk */
414 0x00008000 | /* SPI = 1 */
415 0x00004000 | /* MSTR = 1 -- SPI master */
416 0x00000800; /* UseEOF = 1 -- SS low until EOF */
418 out_be32(&psc->sicr, sicr);
420 ccr = in_be32(&psc->ccr);
422 speed = 1000000; /* default 1MHz */
423 bclkdiv = (mps->mclk_rate / speed) - 1;
424 ccr |= (((bclkdiv & 0xff) << 16) | (((bclkdiv >> 8) & 0xff) << 8));
425 out_be32(&psc->ccr, ccr);
427 /* Set 2ms DTL delay */
428 out_8(&psc->ctur, 0x00);
429 out_8(&psc->ctlr, 0x82);
431 /* we don't use the alarms */
432 out_be32(&fifo->rxalarm, 0xfff);
433 out_be32(&fifo->txalarm, 0);
435 /* Enable FIFO slices for Rx/Tx */
436 out_be32(&fifo->rxcmd,
437 MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
438 out_be32(&fifo->txcmd,
439 MPC512x_PSC_FIFO_ENABLE_SLICE | MPC512x_PSC_FIFO_ENABLE_DMA);
441 mps->bits_per_word = 8;
446 static irqreturn_t mpc512x_psc_spi_isr(int irq, void *dev_id)
448 struct mpc512x_psc_spi *mps = (struct mpc512x_psc_spi *)dev_id;
449 struct mpc512x_psc_fifo __iomem *fifo = mps->fifo;
451 /* clear interrupt and wake up the rx/tx routine */
452 if (in_be32(&fifo->txisr) &
453 in_be32(&fifo->tximr) & MPC512x_PSC_FIFO_EMPTY) {
454 out_be32(&fifo->txisr, MPC512x_PSC_FIFO_EMPTY);
455 out_be32(&fifo->tximr, 0);
456 complete(&mps->txisrdone);
462 static void mpc512x_spi_cs_control(struct spi_device *spi, bool onoff)
464 gpio_set_value(spi->cs_gpio, onoff);
467 /* bus_num is used only for the case dev->platform_data == NULL */
468 static int mpc512x_psc_spi_do_probe(struct device *dev, u32 regaddr,
469 u32 size, unsigned int irq,
472 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
473 struct mpc512x_psc_spi *mps;
474 struct spi_master *master;
481 master = spi_alloc_master(dev, sizeof *mps);
485 dev_set_drvdata(dev, master);
486 mps = spi_master_get_devdata(master);
490 mps->cs_control = mpc512x_spi_cs_control;
491 master->bus_num = bus_num;
493 mps->cs_control = pdata->cs_control;
494 master->bus_num = pdata->bus_num;
495 master->num_chipselect = pdata->max_chipselect;
498 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
499 master->setup = mpc512x_psc_spi_setup;
500 master->prepare_transfer_hardware = mpc512x_psc_spi_prep_xfer_hw;
501 master->transfer_one_message = mpc512x_psc_spi_msg_xfer;
502 master->unprepare_transfer_hardware = mpc512x_psc_spi_unprep_xfer_hw;
503 master->cleanup = mpc512x_psc_spi_cleanup;
504 master->dev.of_node = dev->of_node;
506 tempp = ioremap(regaddr, size);
508 dev_err(dev, "could not ioremap I/O port range\n");
514 (struct mpc512x_psc_fifo *)(tempp + sizeof(struct mpc52xx_psc));
516 ret = request_irq(mps->irq, mpc512x_psc_spi_isr, IRQF_SHARED,
517 "mpc512x-psc-spi", mps);
520 init_completion(&mps->txisrdone);
522 psc_num = master->bus_num;
523 snprintf(clk_name, sizeof(clk_name), "psc%d_mclk", psc_num);
524 clk = devm_clk_get(dev, clk_name);
529 ret = clk_prepare_enable(clk);
533 mps->mclk_rate = clk_get_rate(clk);
535 ret = mpc512x_psc_spi_port_config(master, mps);
539 ret = devm_spi_register_master(dev, master);
546 clk_disable_unprepare(mps->clk_mclk);
548 free_irq(mps->irq, mps);
552 spi_master_put(master);
557 static int mpc512x_psc_spi_do_remove(struct device *dev)
559 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
560 struct mpc512x_psc_spi *mps = spi_master_get_devdata(master);
562 clk_disable_unprepare(mps->clk_mclk);
563 free_irq(mps->irq, mps);
570 static int mpc512x_psc_spi_of_probe(struct platform_device *op)
572 const u32 *regaddr_p;
573 u64 regaddr64, size64;
576 regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL);
578 dev_err(&op->dev, "Invalid PSC address\n");
581 regaddr64 = of_translate_address(op->dev.of_node, regaddr_p);
583 /* get PSC id (0..11, used by port_config) */
584 id = of_alias_get_id(op->dev.of_node, "spi");
586 dev_err(&op->dev, "no alias id for %s\n",
587 op->dev.of_node->full_name);
591 return mpc512x_psc_spi_do_probe(&op->dev, (u32) regaddr64, (u32) size64,
592 irq_of_parse_and_map(op->dev.of_node, 0), id);
595 static int mpc512x_psc_spi_of_remove(struct platform_device *op)
597 return mpc512x_psc_spi_do_remove(&op->dev);
600 static struct of_device_id mpc512x_psc_spi_of_match[] = {
601 { .compatible = "fsl,mpc5121-psc-spi", },
605 MODULE_DEVICE_TABLE(of, mpc512x_psc_spi_of_match);
607 static struct platform_driver mpc512x_psc_spi_of_driver = {
608 .probe = mpc512x_psc_spi_of_probe,
609 .remove = mpc512x_psc_spi_of_remove,
611 .name = "mpc512x-psc-spi",
612 .owner = THIS_MODULE,
613 .of_match_table = mpc512x_psc_spi_of_match,
616 module_platform_driver(mpc512x_psc_spi_of_driver);
618 MODULE_AUTHOR("John Rigby");
619 MODULE_DESCRIPTION("MPC512x PSC SPI Driver");
620 MODULE_LICENSE("GPL");