2 * MPC52xx PSC in SPI mode driver.
4 * Maintainer: Dragos Carp
6 * Copyright (C) 2006 TOPTICA Photonics AG.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/interrupt.h>
18 #include <linux/of_address.h>
19 #include <linux/of_platform.h>
20 #include <linux/workqueue.h>
21 #include <linux/completion.h>
23 #include <linux/delay.h>
24 #include <linux/spi/spi.h>
25 #include <linux/fsl_devices.h>
26 #include <linux/slab.h>
28 #include <asm/mpc52xx.h>
29 #include <asm/mpc52xx_psc.h>
31 #define MCLK 20000000 /* PSC port MClk in hz */
33 struct mpc52xx_psc_spi
{
34 /* fsl_spi_platform data */
35 void (*cs_control
)(struct spi_device
*spi
, bool on
);
38 /* driver internal data */
39 struct mpc52xx_psc __iomem
*psc
;
40 struct mpc52xx_psc_fifo __iomem
*fifo
;
45 struct workqueue_struct
*workqueue
;
46 struct work_struct work
;
48 struct list_head queue
;
51 struct completion done
;
54 /* controller state */
55 struct mpc52xx_psc_spi_cs
{
60 /* set clock freq, clock ramp, bits per work
61 * if t is NULL then reset the values to the default values
63 static int mpc52xx_psc_spi_transfer_setup(struct spi_device
*spi
,
64 struct spi_transfer
*t
)
66 struct mpc52xx_psc_spi_cs
*cs
= spi
->controller_state
;
68 cs
->speed_hz
= (t
&& t
->speed_hz
)
69 ? t
->speed_hz
: spi
->max_speed_hz
;
70 cs
->bits_per_word
= (t
&& t
->bits_per_word
)
71 ? t
->bits_per_word
: spi
->bits_per_word
;
72 cs
->bits_per_word
= ((cs
->bits_per_word
+ 7) / 8) * 8;
76 static void mpc52xx_psc_spi_activate_cs(struct spi_device
*spi
)
78 struct mpc52xx_psc_spi_cs
*cs
= spi
->controller_state
;
79 struct mpc52xx_psc_spi
*mps
= spi_master_get_devdata(spi
->master
);
80 struct mpc52xx_psc __iomem
*psc
= mps
->psc
;
84 sicr
= in_be32(&psc
->sicr
);
86 /* Set clock phase and polarity */
87 if (spi
->mode
& SPI_CPHA
)
91 if (spi
->mode
& SPI_CPOL
)
96 if (spi
->mode
& SPI_LSB_FIRST
)
100 out_be32(&psc
->sicr
, sicr
);
102 /* Set clock frequency and bits per word
103 * Because psc->ccr is defined as 16bit register instead of 32bit
104 * just set the lower byte of BitClkDiv
106 ccr
= in_be16((u16 __iomem
*)&psc
->ccr
);
109 ccr
|= (MCLK
/ cs
->speed_hz
- 1) & 0xFF;
110 else /* by default SPI Clk 1MHz */
111 ccr
|= (MCLK
/ 1000000 - 1) & 0xFF;
112 out_be16((u16 __iomem
*)&psc
->ccr
, ccr
);
113 mps
->bits_per_word
= cs
->bits_per_word
;
116 mps
->cs_control(spi
, (spi
->mode
& SPI_CS_HIGH
) ? 1 : 0);
119 static void mpc52xx_psc_spi_deactivate_cs(struct spi_device
*spi
)
121 struct mpc52xx_psc_spi
*mps
= spi_master_get_devdata(spi
->master
);
124 mps
->cs_control(spi
, (spi
->mode
& SPI_CS_HIGH
) ? 0 : 1);
127 #define MPC52xx_PSC_BUFSIZE (MPC52xx_PSC_RFNUM_MASK + 1)
128 /* wake up when 80% fifo full */
129 #define MPC52xx_PSC_RFALARM (MPC52xx_PSC_BUFSIZE * 20 / 100)
131 static int mpc52xx_psc_spi_transfer_rxtx(struct spi_device
*spi
,
132 struct spi_transfer
*t
)
134 struct mpc52xx_psc_spi
*mps
= spi_master_get_devdata(spi
->master
);
135 struct mpc52xx_psc __iomem
*psc
= mps
->psc
;
136 struct mpc52xx_psc_fifo __iomem
*fifo
= mps
->fifo
;
137 unsigned rb
= 0; /* number of bytes receieved */
138 unsigned sb
= 0; /* number of bytes sent */
139 unsigned char *rx_buf
= (unsigned char *)t
->rx_buf
;
140 unsigned char *tx_buf
= (unsigned char *)t
->tx_buf
;
142 unsigned send_at_once
= MPC52xx_PSC_BUFSIZE
;
143 unsigned recv_at_once
;
146 if (!t
->tx_buf
&& !t
->rx_buf
&& t
->len
)
149 /* enable transmiter/receiver */
150 out_8(&psc
->command
, MPC52xx_PSC_TX_ENABLE
| MPC52xx_PSC_RX_ENABLE
);
151 while (rb
< t
->len
) {
152 if (t
->len
- rb
> MPC52xx_PSC_BUFSIZE
) {
153 rfalarm
= MPC52xx_PSC_RFALARM
;
156 send_at_once
= t
->len
- sb
;
157 rfalarm
= MPC52xx_PSC_BUFSIZE
- (t
->len
- rb
);
161 dev_dbg(&spi
->dev
, "send %d bytes...\n", send_at_once
);
162 for (; send_at_once
; sb
++, send_at_once
--) {
163 /* set EOF flag before the last word is sent */
164 if (send_at_once
== 1 && last_block
)
165 out_8(&psc
->ircr2
, 0x01);
168 out_8(&psc
->mpc52xx_psc_buffer_8
, tx_buf
[sb
]);
170 out_8(&psc
->mpc52xx_psc_buffer_8
, 0);
174 /* enable interrupts and wait for wake up
175 * if just one byte is expected the Rx FIFO genererates no
176 * FFULL interrupt, so activate the RxRDY interrupt
178 out_8(&psc
->command
, MPC52xx_PSC_SEL_MODE_REG_1
);
179 if (t
->len
- rb
== 1) {
180 out_8(&psc
->mode
, 0);
182 out_8(&psc
->mode
, MPC52xx_PSC_MODE_FFULL
);
183 out_be16(&fifo
->rfalarm
, rfalarm
);
185 out_be16(&psc
->mpc52xx_psc_imr
, MPC52xx_PSC_IMR_RXRDY
);
186 wait_for_completion(&mps
->done
);
187 recv_at_once
= in_be16(&fifo
->rfnum
);
188 dev_dbg(&spi
->dev
, "%d bytes received\n", recv_at_once
);
190 send_at_once
= recv_at_once
;
192 for (; recv_at_once
; rb
++, recv_at_once
--)
193 rx_buf
[rb
] = in_8(&psc
->mpc52xx_psc_buffer_8
);
195 for (; recv_at_once
; rb
++, recv_at_once
--)
196 in_8(&psc
->mpc52xx_psc_buffer_8
);
199 /* disable transmiter/receiver */
200 out_8(&psc
->command
, MPC52xx_PSC_TX_DISABLE
| MPC52xx_PSC_RX_DISABLE
);
205 static void mpc52xx_psc_spi_work(struct work_struct
*work
)
207 struct mpc52xx_psc_spi
*mps
=
208 container_of(work
, struct mpc52xx_psc_spi
, work
);
210 spin_lock_irq(&mps
->lock
);
212 while (!list_empty(&mps
->queue
)) {
213 struct spi_message
*m
;
214 struct spi_device
*spi
;
215 struct spi_transfer
*t
= NULL
;
219 m
= container_of(mps
->queue
.next
, struct spi_message
, queue
);
220 list_del_init(&m
->queue
);
221 spin_unlock_irq(&mps
->lock
);
226 list_for_each_entry (t
, &m
->transfers
, transfer_list
) {
227 if (t
->bits_per_word
|| t
->speed_hz
) {
228 status
= mpc52xx_psc_spi_transfer_setup(spi
, t
);
234 mpc52xx_psc_spi_activate_cs(spi
);
235 cs_change
= t
->cs_change
;
237 status
= mpc52xx_psc_spi_transfer_rxtx(spi
, t
);
240 m
->actual_length
+= t
->len
;
243 udelay(t
->delay_usecs
);
246 mpc52xx_psc_spi_deactivate_cs(spi
);
250 m
->complete(m
->context
);
252 if (status
|| !cs_change
)
253 mpc52xx_psc_spi_deactivate_cs(spi
);
255 mpc52xx_psc_spi_transfer_setup(spi
, NULL
);
257 spin_lock_irq(&mps
->lock
);
260 spin_unlock_irq(&mps
->lock
);
263 static int mpc52xx_psc_spi_setup(struct spi_device
*spi
)
265 struct mpc52xx_psc_spi
*mps
= spi_master_get_devdata(spi
->master
);
266 struct mpc52xx_psc_spi_cs
*cs
= spi
->controller_state
;
269 if (spi
->bits_per_word
%8)
273 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
276 spi
->controller_state
= cs
;
279 cs
->bits_per_word
= spi
->bits_per_word
;
280 cs
->speed_hz
= spi
->max_speed_hz
;
282 spin_lock_irqsave(&mps
->lock
, flags
);
284 mpc52xx_psc_spi_deactivate_cs(spi
);
285 spin_unlock_irqrestore(&mps
->lock
, flags
);
290 static int mpc52xx_psc_spi_transfer(struct spi_device
*spi
,
291 struct spi_message
*m
)
293 struct mpc52xx_psc_spi
*mps
= spi_master_get_devdata(spi
->master
);
296 m
->actual_length
= 0;
297 m
->status
= -EINPROGRESS
;
299 spin_lock_irqsave(&mps
->lock
, flags
);
300 list_add_tail(&m
->queue
, &mps
->queue
);
301 queue_work(mps
->workqueue
, &mps
->work
);
302 spin_unlock_irqrestore(&mps
->lock
, flags
);
307 static void mpc52xx_psc_spi_cleanup(struct spi_device
*spi
)
309 kfree(spi
->controller_state
);
312 static int mpc52xx_psc_spi_port_config(int psc_id
, struct mpc52xx_psc_spi
*mps
)
314 struct mpc52xx_psc __iomem
*psc
= mps
->psc
;
315 struct mpc52xx_psc_fifo __iomem
*fifo
= mps
->fifo
;
319 /* default sysclk is 512MHz */
320 mclken_div
= (mps
->sysclk
? mps
->sysclk
: 512000000) / MCLK
;
321 ret
= mpc52xx_set_psc_clkdiv(psc_id
, mclken_div
);
325 /* Reset the PSC into a known state */
326 out_8(&psc
->command
, MPC52xx_PSC_RST_RX
);
327 out_8(&psc
->command
, MPC52xx_PSC_RST_TX
);
328 out_8(&psc
->command
, MPC52xx_PSC_TX_DISABLE
| MPC52xx_PSC_RX_DISABLE
);
330 /* Disable interrupts, interrupts are based on alarm level */
331 out_be16(&psc
->mpc52xx_psc_imr
, 0);
332 out_8(&psc
->command
, MPC52xx_PSC_SEL_MODE_REG_1
);
333 out_8(&fifo
->rfcntl
, 0);
334 out_8(&psc
->mode
, MPC52xx_PSC_MODE_FFULL
);
336 /* Configure 8bit codec mode as a SPI master and use EOF flags */
337 /* SICR_SIM_CODEC8|SICR_GENCLK|SICR_SPI|SICR_MSTR|SICR_USEEOF */
338 out_be32(&psc
->sicr
, 0x0180C800);
339 out_be16((u16 __iomem
*)&psc
->ccr
, 0x070F); /* default SPI Clk 1MHz */
341 /* Set 2ms DTL delay */
342 out_8(&psc
->ctur
, 0x00);
343 out_8(&psc
->ctlr
, 0x84);
345 mps
->bits_per_word
= 8;
350 static irqreturn_t
mpc52xx_psc_spi_isr(int irq
, void *dev_id
)
352 struct mpc52xx_psc_spi
*mps
= (struct mpc52xx_psc_spi
*)dev_id
;
353 struct mpc52xx_psc __iomem
*psc
= mps
->psc
;
355 /* disable interrupt and wake up the work queue */
356 if (in_be16(&psc
->mpc52xx_psc_isr
) & MPC52xx_PSC_IMR_RXRDY
) {
357 out_be16(&psc
->mpc52xx_psc_imr
, 0);
358 complete(&mps
->done
);
364 /* bus_num is used only for the case dev->platform_data == NULL */
365 static int mpc52xx_psc_spi_do_probe(struct device
*dev
, u32 regaddr
,
366 u32 size
, unsigned int irq
, s16 bus_num
)
368 struct fsl_spi_platform_data
*pdata
= dev_get_platdata(dev
);
369 struct mpc52xx_psc_spi
*mps
;
370 struct spi_master
*master
;
373 master
= spi_alloc_master(dev
, sizeof *mps
);
377 dev_set_drvdata(dev
, master
);
378 mps
= spi_master_get_devdata(master
);
380 /* the spi->mode bits understood by this driver: */
381 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
| SPI_LSB_FIRST
;
386 "probe called without platform data, no cs_control function will be called\n");
387 mps
->cs_control
= NULL
;
389 master
->bus_num
= bus_num
;
390 master
->num_chipselect
= 255;
392 mps
->cs_control
= pdata
->cs_control
;
393 mps
->sysclk
= pdata
->sysclk
;
394 master
->bus_num
= pdata
->bus_num
;
395 master
->num_chipselect
= pdata
->max_chipselect
;
397 master
->setup
= mpc52xx_psc_spi_setup
;
398 master
->transfer
= mpc52xx_psc_spi_transfer
;
399 master
->cleanup
= mpc52xx_psc_spi_cleanup
;
400 master
->dev
.of_node
= dev
->of_node
;
402 mps
->psc
= ioremap(regaddr
, size
);
404 dev_err(dev
, "could not ioremap I/O port range\n");
408 /* On the 5200, fifo regs are immediately ajacent to the psc regs */
409 mps
->fifo
= ((void __iomem
*)mps
->psc
) + sizeof(struct mpc52xx_psc
);
411 ret
= request_irq(mps
->irq
, mpc52xx_psc_spi_isr
, 0, "mpc52xx-psc-spi",
416 ret
= mpc52xx_psc_spi_port_config(master
->bus_num
, mps
);
418 dev_err(dev
, "can't configure PSC! Is it capable of SPI?\n");
422 spin_lock_init(&mps
->lock
);
423 init_completion(&mps
->done
);
424 INIT_WORK(&mps
->work
, mpc52xx_psc_spi_work
);
425 INIT_LIST_HEAD(&mps
->queue
);
427 mps
->workqueue
= create_singlethread_workqueue(
428 dev_name(master
->dev
.parent
));
429 if (mps
->workqueue
== NULL
) {
434 ret
= spi_register_master(master
);
441 destroy_workqueue(mps
->workqueue
);
443 free_irq(mps
->irq
, mps
);
447 spi_master_put(master
);
452 static int mpc52xx_psc_spi_of_probe(struct platform_device
*op
)
454 const u32
*regaddr_p
;
455 u64 regaddr64
, size64
;
458 regaddr_p
= of_get_address(op
->dev
.of_node
, 0, &size64
, NULL
);
460 dev_err(&op
->dev
, "Invalid PSC address\n");
463 regaddr64
= of_translate_address(op
->dev
.of_node
, regaddr_p
);
465 /* get PSC id (1..6, used by port_config) */
466 if (op
->dev
.platform_data
== NULL
) {
469 psc_nump
= of_get_property(op
->dev
.of_node
, "cell-index", NULL
);
470 if (!psc_nump
|| *psc_nump
> 5) {
471 dev_err(&op
->dev
, "Invalid cell-index property\n");
477 return mpc52xx_psc_spi_do_probe(&op
->dev
, (u32
)regaddr64
, (u32
)size64
,
478 irq_of_parse_and_map(op
->dev
.of_node
, 0), id
);
481 static int mpc52xx_psc_spi_of_remove(struct platform_device
*op
)
483 struct spi_master
*master
= spi_master_get(platform_get_drvdata(op
));
484 struct mpc52xx_psc_spi
*mps
= spi_master_get_devdata(master
);
486 flush_workqueue(mps
->workqueue
);
487 destroy_workqueue(mps
->workqueue
);
488 spi_unregister_master(master
);
489 free_irq(mps
->irq
, mps
);
492 spi_master_put(master
);
497 static const struct of_device_id mpc52xx_psc_spi_of_match
[] = {
498 { .compatible
= "fsl,mpc5200-psc-spi", },
499 { .compatible
= "mpc5200-psc-spi", }, /* old */
503 MODULE_DEVICE_TABLE(of
, mpc52xx_psc_spi_of_match
);
505 static struct platform_driver mpc52xx_psc_spi_of_driver
= {
506 .probe
= mpc52xx_psc_spi_of_probe
,
507 .remove
= mpc52xx_psc_spi_of_remove
,
509 .name
= "mpc52xx-psc-spi",
510 .owner
= THIS_MODULE
,
511 .of_match_table
= mpc52xx_psc_spi_of_match
,
514 module_platform_driver(mpc52xx_psc_spi_of_driver
);
516 MODULE_AUTHOR("Dragos Carp");
517 MODULE_DESCRIPTION("MPC52xx PSC SPI Driver");
518 MODULE_LICENSE("GPL");