1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MPC52xx PSC in SPI mode driver.
5 * Maintainer: Dragos Carp
7 * Copyright (C) 2006 TOPTICA Photonics AG.
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <linux/interrupt.h>
14 #include <linux/of_address.h>
15 #include <linux/of_platform.h>
16 #include <linux/workqueue.h>
17 #include <linux/completion.h>
19 #include <linux/delay.h>
20 #include <linux/spi/spi.h>
21 #include <linux/fsl_devices.h>
22 #include <linux/slab.h>
23 #include <linux/of_irq.h>
25 #include <asm/mpc52xx.h>
26 #include <asm/mpc52xx_psc.h>
28 #define MCLK 20000000 /* PSC port MClk in hz */
30 struct mpc52xx_psc_spi {
31 /* fsl_spi_platform data */
32 void (*cs_control)(struct spi_device *spi, bool on);
35 /* driver internal data */
36 struct mpc52xx_psc __iomem *psc;
37 struct mpc52xx_psc_fifo __iomem *fifo;
42 struct work_struct work;
44 struct list_head queue;
47 struct completion done;
50 /* controller state */
51 struct mpc52xx_psc_spi_cs {
56 /* set clock freq, clock ramp, bits per work
57 * if t is NULL then reset the values to the default values
59 static int mpc52xx_psc_spi_transfer_setup(struct spi_device *spi,
60 struct spi_transfer *t)
62 struct mpc52xx_psc_spi_cs *cs = spi->controller_state;
64 cs->speed_hz = (t && t->speed_hz)
65 ? t->speed_hz : spi->max_speed_hz;
66 cs->bits_per_word = (t && t->bits_per_word)
67 ? t->bits_per_word : spi->bits_per_word;
68 cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8;
72 static void mpc52xx_psc_spi_activate_cs(struct spi_device *spi)
74 struct mpc52xx_psc_spi_cs *cs = spi->controller_state;
75 struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
76 struct mpc52xx_psc __iomem *psc = mps->psc;
80 sicr = in_be32(&psc->sicr);
82 /* Set clock phase and polarity */
83 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 /* Set clock frequency and bits per word
99 * Because psc->ccr is defined as 16bit register instead of 32bit
100 * just set the lower byte of BitClkDiv
102 ccr = in_be16((u16 __iomem *)&psc->ccr);
105 ccr |= (MCLK / cs->speed_hz - 1) & 0xFF;
106 else /* by default SPI Clk 1MHz */
107 ccr |= (MCLK / 1000000 - 1) & 0xFF;
108 out_be16((u16 __iomem *)&psc->ccr, ccr);
109 mps->bits_per_word = cs->bits_per_word;
112 mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 1 : 0);
115 static void mpc52xx_psc_spi_deactivate_cs(struct spi_device *spi)
117 struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
120 mps->cs_control(spi, (spi->mode & SPI_CS_HIGH) ? 0 : 1);
123 #define MPC52xx_PSC_BUFSIZE (MPC52xx_PSC_RFNUM_MASK + 1)
124 /* wake up when 80% fifo full */
125 #define MPC52xx_PSC_RFALARM (MPC52xx_PSC_BUFSIZE * 20 / 100)
127 static int mpc52xx_psc_spi_transfer_rxtx(struct spi_device *spi,
128 struct spi_transfer *t)
130 struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
131 struct mpc52xx_psc __iomem *psc = mps->psc;
132 struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo;
133 unsigned rb = 0; /* number of bytes receieved */
134 unsigned sb = 0; /* number of bytes sent */
135 unsigned char *rx_buf = (unsigned char *)t->rx_buf;
136 unsigned char *tx_buf = (unsigned char *)t->tx_buf;
138 unsigned send_at_once = MPC52xx_PSC_BUFSIZE;
139 unsigned recv_at_once;
142 if (!t->tx_buf && !t->rx_buf && t->len)
145 /* enable transmiter/receiver */
146 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE);
147 while (rb < t->len) {
148 if (t->len - rb > MPC52xx_PSC_BUFSIZE) {
149 rfalarm = MPC52xx_PSC_RFALARM;
152 send_at_once = t->len - sb;
153 rfalarm = MPC52xx_PSC_BUFSIZE - (t->len - rb);
157 dev_dbg(&spi->dev, "send %d bytes...\n", send_at_once);
158 for (; send_at_once; sb++, send_at_once--) {
159 /* set EOF flag before the last word is sent */
160 if (send_at_once == 1 && last_block)
161 out_8(&psc->ircr2, 0x01);
164 out_8(&psc->mpc52xx_psc_buffer_8, tx_buf[sb]);
166 out_8(&psc->mpc52xx_psc_buffer_8, 0);
170 /* enable interrupts and wait for wake up
171 * if just one byte is expected the Rx FIFO genererates no
172 * FFULL interrupt, so activate the RxRDY interrupt
174 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
175 if (t->len - rb == 1) {
176 out_8(&psc->mode, 0);
178 out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL);
179 out_be16(&fifo->rfalarm, rfalarm);
181 out_be16(&psc->mpc52xx_psc_imr, MPC52xx_PSC_IMR_RXRDY);
182 wait_for_completion(&mps->done);
183 recv_at_once = in_be16(&fifo->rfnum);
184 dev_dbg(&spi->dev, "%d bytes received\n", recv_at_once);
186 send_at_once = recv_at_once;
188 for (; recv_at_once; rb++, recv_at_once--)
189 rx_buf[rb] = in_8(&psc->mpc52xx_psc_buffer_8);
191 for (; recv_at_once; rb++, recv_at_once--)
192 in_8(&psc->mpc52xx_psc_buffer_8);
195 /* disable transmiter/receiver */
196 out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
201 static void mpc52xx_psc_spi_work(struct work_struct *work)
203 struct mpc52xx_psc_spi *mps =
204 container_of(work, struct mpc52xx_psc_spi, work);
206 spin_lock_irq(&mps->lock);
208 while (!list_empty(&mps->queue)) {
209 struct spi_message *m;
210 struct spi_device *spi;
211 struct spi_transfer *t = NULL;
215 m = container_of(mps->queue.next, struct spi_message, queue);
216 list_del_init(&m->queue);
217 spin_unlock_irq(&mps->lock);
222 list_for_each_entry (t, &m->transfers, transfer_list) {
223 if (t->bits_per_word || t->speed_hz) {
224 status = mpc52xx_psc_spi_transfer_setup(spi, t);
230 mpc52xx_psc_spi_activate_cs(spi);
231 cs_change = t->cs_change;
233 status = mpc52xx_psc_spi_transfer_rxtx(spi, t);
236 m->actual_length += t->len;
238 spi_transfer_delay_exec(t);
241 mpc52xx_psc_spi_deactivate_cs(spi);
246 m->complete(m->context);
248 if (status || !cs_change)
249 mpc52xx_psc_spi_deactivate_cs(spi);
251 mpc52xx_psc_spi_transfer_setup(spi, NULL);
253 spin_lock_irq(&mps->lock);
256 spin_unlock_irq(&mps->lock);
259 static int mpc52xx_psc_spi_setup(struct spi_device *spi)
261 struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
262 struct mpc52xx_psc_spi_cs *cs = spi->controller_state;
265 if (spi->bits_per_word%8)
269 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
272 spi->controller_state = cs;
275 cs->bits_per_word = spi->bits_per_word;
276 cs->speed_hz = spi->max_speed_hz;
278 spin_lock_irqsave(&mps->lock, flags);
280 mpc52xx_psc_spi_deactivate_cs(spi);
281 spin_unlock_irqrestore(&mps->lock, flags);
286 static int mpc52xx_psc_spi_transfer(struct spi_device *spi,
287 struct spi_message *m)
289 struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master);
292 m->actual_length = 0;
293 m->status = -EINPROGRESS;
295 spin_lock_irqsave(&mps->lock, flags);
296 list_add_tail(&m->queue, &mps->queue);
297 schedule_work(&mps->work);
298 spin_unlock_irqrestore(&mps->lock, flags);
303 static void mpc52xx_psc_spi_cleanup(struct spi_device *spi)
305 kfree(spi->controller_state);
308 static int mpc52xx_psc_spi_port_config(int psc_id, struct mpc52xx_psc_spi *mps)
310 struct mpc52xx_psc __iomem *psc = mps->psc;
311 struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo;
315 /* default sysclk is 512MHz */
316 mclken_div = (mps->sysclk ? mps->sysclk : 512000000) / MCLK;
317 ret = mpc52xx_set_psc_clkdiv(psc_id, mclken_div);
321 /* Reset the PSC into a known state */
322 out_8(&psc->command, MPC52xx_PSC_RST_RX);
323 out_8(&psc->command, MPC52xx_PSC_RST_TX);
324 out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE);
326 /* Disable interrupts, interrupts are based on alarm level */
327 out_be16(&psc->mpc52xx_psc_imr, 0);
328 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1);
329 out_8(&fifo->rfcntl, 0);
330 out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL);
332 /* Configure 8bit codec mode as a SPI master and use EOF flags */
333 /* SICR_SIM_CODEC8|SICR_GENCLK|SICR_SPI|SICR_MSTR|SICR_USEEOF */
334 out_be32(&psc->sicr, 0x0180C800);
335 out_be16((u16 __iomem *)&psc->ccr, 0x070F); /* default SPI Clk 1MHz */
337 /* Set 2ms DTL delay */
338 out_8(&psc->ctur, 0x00);
339 out_8(&psc->ctlr, 0x84);
341 mps->bits_per_word = 8;
346 static irqreturn_t mpc52xx_psc_spi_isr(int irq, void *dev_id)
348 struct mpc52xx_psc_spi *mps = (struct mpc52xx_psc_spi *)dev_id;
349 struct mpc52xx_psc __iomem *psc = mps->psc;
351 /* disable interrupt and wake up the work queue */
352 if (in_be16(&psc->mpc52xx_psc_isr) & MPC52xx_PSC_IMR_RXRDY) {
353 out_be16(&psc->mpc52xx_psc_imr, 0);
354 complete(&mps->done);
360 /* bus_num is used only for the case dev->platform_data == NULL */
361 static int mpc52xx_psc_spi_do_probe(struct device *dev, u32 regaddr,
362 u32 size, unsigned int irq, s16 bus_num)
364 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
365 struct mpc52xx_psc_spi *mps;
366 struct spi_master *master;
369 master = spi_alloc_master(dev, sizeof(*mps));
373 dev_set_drvdata(dev, master);
374 mps = spi_master_get_devdata(master);
376 /* the spi->mode bits understood by this driver: */
377 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
382 "probe called without platform data, no cs_control function will be called\n");
383 mps->cs_control = NULL;
385 master->bus_num = bus_num;
386 master->num_chipselect = 255;
388 mps->cs_control = pdata->cs_control;
389 mps->sysclk = pdata->sysclk;
390 master->bus_num = pdata->bus_num;
391 master->num_chipselect = pdata->max_chipselect;
393 master->setup = mpc52xx_psc_spi_setup;
394 master->transfer = mpc52xx_psc_spi_transfer;
395 master->cleanup = mpc52xx_psc_spi_cleanup;
396 master->dev.of_node = dev->of_node;
398 mps->psc = ioremap(regaddr, size);
400 dev_err(dev, "could not ioremap I/O port range\n");
404 /* On the 5200, fifo regs are immediately ajacent to the psc regs */
405 mps->fifo = ((void __iomem *)mps->psc) + sizeof(struct mpc52xx_psc);
407 ret = request_irq(mps->irq, mpc52xx_psc_spi_isr, 0, "mpc52xx-psc-spi",
412 ret = mpc52xx_psc_spi_port_config(master->bus_num, mps);
414 dev_err(dev, "can't configure PSC! Is it capable of SPI?\n");
418 spin_lock_init(&mps->lock);
419 init_completion(&mps->done);
420 INIT_WORK(&mps->work, mpc52xx_psc_spi_work);
421 INIT_LIST_HEAD(&mps->queue);
423 ret = spi_register_master(master);
430 free_irq(mps->irq, mps);
434 spi_master_put(master);
439 static int mpc52xx_psc_spi_of_probe(struct platform_device *op)
441 const u32 *regaddr_p;
442 u64 regaddr64, size64;
445 regaddr_p = of_get_address(op->dev.of_node, 0, &size64, NULL);
447 dev_err(&op->dev, "Invalid PSC address\n");
450 regaddr64 = of_translate_address(op->dev.of_node, regaddr_p);
452 /* get PSC id (1..6, used by port_config) */
453 if (op->dev.platform_data == NULL) {
456 psc_nump = of_get_property(op->dev.of_node, "cell-index", NULL);
457 if (!psc_nump || *psc_nump > 5) {
458 dev_err(&op->dev, "Invalid cell-index property\n");
464 return mpc52xx_psc_spi_do_probe(&op->dev, (u32)regaddr64, (u32)size64,
465 irq_of_parse_and_map(op->dev.of_node, 0), id);
468 static int mpc52xx_psc_spi_of_remove(struct platform_device *op)
470 struct spi_master *master = spi_master_get(platform_get_drvdata(op));
471 struct mpc52xx_psc_spi *mps = spi_master_get_devdata(master);
473 flush_work(&mps->work);
474 spi_unregister_master(master);
475 free_irq(mps->irq, mps);
478 spi_master_put(master);
483 static const struct of_device_id mpc52xx_psc_spi_of_match[] = {
484 { .compatible = "fsl,mpc5200-psc-spi", },
485 { .compatible = "mpc5200-psc-spi", }, /* old */
489 MODULE_DEVICE_TABLE(of, mpc52xx_psc_spi_of_match);
491 static struct platform_driver mpc52xx_psc_spi_of_driver = {
492 .probe = mpc52xx_psc_spi_of_probe,
493 .remove = mpc52xx_psc_spi_of_remove,
495 .name = "mpc52xx-psc-spi",
496 .of_match_table = mpc52xx_psc_spi_of_match,
499 module_platform_driver(mpc52xx_psc_spi_of_driver);
501 MODULE_AUTHOR("Dragos Carp");
502 MODULE_DESCRIPTION("MPC52xx PSC SPI Driver");
503 MODULE_LICENSE("GPL");