2 * Freescale SPI controller driver.
4 * Maintainer: Kumar Gala
6 * Copyright (C) 2006 Polycom, Inc.
7 * Copyright 2010 Freescale Semiconductor, Inc.
9 * CPM SPI and QE buffer descriptors mode support:
10 * Copyright (c) 2009 MontaVista Software, Inc.
11 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/interrupt.h>
22 #include <linux/delay.h>
23 #include <linux/irq.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/spi_bitbang.h>
26 #include <linux/platform_device.h>
27 #include <linux/fsl_devices.h>
28 #include <linux/dma-mapping.h>
30 #include <linux/mutex.h>
32 #include <linux/of_platform.h>
33 #include <linux/of_address.h>
34 #include <linux/of_irq.h>
35 #include <linux/gpio.h>
36 #include <linux/of_gpio.h>
38 #include "spi-fsl-lib.h"
39 #include "spi-fsl-cpm.h"
40 #include "spi-fsl-spi.h"
42 static void fsl_spi_change_mode(struct spi_device
*spi
)
44 struct mpc8xxx_spi
*mspi
= spi_master_get_devdata(spi
->master
);
45 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
46 struct fsl_spi_reg
*reg_base
= mspi
->reg_base
;
47 __be32 __iomem
*mode
= ®_base
->mode
;
50 if (cs
->hw_mode
== mpc8xxx_spi_read_reg(mode
))
53 /* Turn off IRQs locally to minimize time that SPI is disabled. */
54 local_irq_save(flags
);
56 /* Turn off SPI unit prior changing mode */
57 mpc8xxx_spi_write_reg(mode
, cs
->hw_mode
& ~SPMODE_ENABLE
);
59 /* When in CPM mode, we need to reinit tx and rx. */
60 if (mspi
->flags
& SPI_CPM_MODE
) {
61 fsl_spi_cpm_reinit_txrx(mspi
);
63 mpc8xxx_spi_write_reg(mode
, cs
->hw_mode
);
64 local_irq_restore(flags
);
67 static void fsl_spi_chipselect(struct spi_device
*spi
, int value
)
69 struct mpc8xxx_spi
*mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
70 struct fsl_spi_platform_data
*pdata
;
71 bool pol
= spi
->mode
& SPI_CS_HIGH
;
72 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
74 pdata
= spi
->dev
.parent
->parent
->platform_data
;
76 if (value
== BITBANG_CS_INACTIVE
) {
77 if (pdata
->cs_control
)
78 pdata
->cs_control(spi
, !pol
);
81 if (value
== BITBANG_CS_ACTIVE
) {
82 mpc8xxx_spi
->rx_shift
= cs
->rx_shift
;
83 mpc8xxx_spi
->tx_shift
= cs
->tx_shift
;
84 mpc8xxx_spi
->get_rx
= cs
->get_rx
;
85 mpc8xxx_spi
->get_tx
= cs
->get_tx
;
87 fsl_spi_change_mode(spi
);
89 if (pdata
->cs_control
)
90 pdata
->cs_control(spi
, pol
);
94 static void fsl_spi_qe_cpu_set_shifts(u32
*rx_shift
, u32
*tx_shift
,
95 int bits_per_word
, int msb_first
)
100 if (bits_per_word
<= 8) {
103 } else if (bits_per_word
<= 16) {
108 if (bits_per_word
<= 8)
113 static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs
*cs
,
114 struct spi_device
*spi
,
115 struct mpc8xxx_spi
*mpc8xxx_spi
,
120 if (bits_per_word
<= 8) {
121 cs
->get_rx
= mpc8xxx_spi_rx_buf_u8
;
122 cs
->get_tx
= mpc8xxx_spi_tx_buf_u8
;
123 } else if (bits_per_word
<= 16) {
124 cs
->get_rx
= mpc8xxx_spi_rx_buf_u16
;
125 cs
->get_tx
= mpc8xxx_spi_tx_buf_u16
;
126 } else if (bits_per_word
<= 32) {
127 cs
->get_rx
= mpc8xxx_spi_rx_buf_u32
;
128 cs
->get_tx
= mpc8xxx_spi_tx_buf_u32
;
132 if (mpc8xxx_spi
->set_shifts
)
133 mpc8xxx_spi
->set_shifts(&cs
->rx_shift
, &cs
->tx_shift
,
135 !(spi
->mode
& SPI_LSB_FIRST
));
137 mpc8xxx_spi
->rx_shift
= cs
->rx_shift
;
138 mpc8xxx_spi
->tx_shift
= cs
->tx_shift
;
139 mpc8xxx_spi
->get_rx
= cs
->get_rx
;
140 mpc8xxx_spi
->get_tx
= cs
->get_tx
;
142 return bits_per_word
;
145 static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs
*cs
,
146 struct spi_device
*spi
,
149 /* QE uses Little Endian for words > 8
150 * so transform all words > 8 into 8 bits
151 * Unfortnatly that doesn't work for LSB so
152 * reject these for now */
153 /* Note: 32 bits word, LSB works iff
154 * tfcr/rfcr is set to CPMFCR_GBL */
155 if (spi
->mode
& SPI_LSB_FIRST
&&
158 if (bits_per_word
> 8)
159 return 8; /* pretend its 8 bits */
160 return bits_per_word
;
163 static int fsl_spi_setup_transfer(struct spi_device
*spi
,
164 struct spi_transfer
*t
)
166 struct mpc8xxx_spi
*mpc8xxx_spi
;
167 int bits_per_word
= 0;
170 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
172 mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
175 bits_per_word
= t
->bits_per_word
;
179 /* spi_transfer level calls that work per-word */
181 bits_per_word
= spi
->bits_per_word
;
183 /* Make sure its a bit width we support [4..16, 32] */
184 if ((bits_per_word
< 4)
185 || ((bits_per_word
> 16) && (bits_per_word
!= 32)))
189 hz
= spi
->max_speed_hz
;
191 if (!(mpc8xxx_spi
->flags
& SPI_CPM_MODE
))
192 bits_per_word
= mspi_apply_cpu_mode_quirks(cs
, spi
,
195 else if (mpc8xxx_spi
->flags
& SPI_QE
)
196 bits_per_word
= mspi_apply_qe_mode_quirks(cs
, spi
,
199 if (bits_per_word
< 0)
200 return bits_per_word
;
202 if (bits_per_word
== 32)
205 bits_per_word
= bits_per_word
- 1;
207 /* mask out bits we are going to set */
208 cs
->hw_mode
&= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
211 cs
->hw_mode
|= SPMODE_LEN(bits_per_word
);
213 if ((mpc8xxx_spi
->spibrg
/ hz
) > 64) {
214 cs
->hw_mode
|= SPMODE_DIV16
;
215 pm
= (mpc8xxx_spi
->spibrg
- 1) / (hz
* 64) + 1;
217 WARN_ONCE(pm
> 16, "%s: Requested speed is too low: %d Hz. "
218 "Will use %d Hz instead.\n", dev_name(&spi
->dev
),
219 hz
, mpc8xxx_spi
->spibrg
/ 1024);
223 pm
= (mpc8xxx_spi
->spibrg
- 1) / (hz
* 4) + 1;
228 cs
->hw_mode
|= SPMODE_PM(pm
);
230 fsl_spi_change_mode(spi
);
234 static int fsl_spi_cpu_bufs(struct mpc8xxx_spi
*mspi
,
235 struct spi_transfer
*t
, unsigned int len
)
238 struct fsl_spi_reg
*reg_base
= mspi
->reg_base
;
243 mpc8xxx_spi_write_reg(®_base
->mask
, SPIM_NE
);
246 word
= mspi
->get_tx(mspi
);
247 mpc8xxx_spi_write_reg(®_base
->transmit
, word
);
252 static int fsl_spi_bufs(struct spi_device
*spi
, struct spi_transfer
*t
,
255 struct mpc8xxx_spi
*mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
256 struct fsl_spi_reg
*reg_base
;
257 unsigned int len
= t
->len
;
261 reg_base
= mpc8xxx_spi
->reg_base
;
262 bits_per_word
= spi
->bits_per_word
;
263 if (t
->bits_per_word
)
264 bits_per_word
= t
->bits_per_word
;
266 if (bits_per_word
> 8) {
267 /* invalid length? */
272 if (bits_per_word
> 16) {
273 /* invalid length? */
279 mpc8xxx_spi
->tx
= t
->tx_buf
;
280 mpc8xxx_spi
->rx
= t
->rx_buf
;
282 INIT_COMPLETION(mpc8xxx_spi
->done
);
284 if (mpc8xxx_spi
->flags
& SPI_CPM_MODE
)
285 ret
= fsl_spi_cpm_bufs(mpc8xxx_spi
, t
, is_dma_mapped
);
287 ret
= fsl_spi_cpu_bufs(mpc8xxx_spi
, t
, len
);
291 wait_for_completion(&mpc8xxx_spi
->done
);
293 /* disable rx ints */
294 mpc8xxx_spi_write_reg(®_base
->mask
, 0);
296 if (mpc8xxx_spi
->flags
& SPI_CPM_MODE
)
297 fsl_spi_cpm_bufs_complete(mpc8xxx_spi
);
299 return mpc8xxx_spi
->count
;
302 static void fsl_spi_do_one_msg(struct spi_message
*m
)
304 struct spi_device
*spi
= m
->spi
;
305 struct spi_transfer
*t
;
306 unsigned int cs_change
;
307 const int nsecs
= 50;
312 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
313 if (t
->bits_per_word
|| t
->speed_hz
) {
314 /* Don't allow changes if CS is active */
318 status
= fsl_spi_setup_transfer(spi
, t
);
324 fsl_spi_chipselect(spi
, BITBANG_CS_ACTIVE
);
327 cs_change
= t
->cs_change
;
329 status
= fsl_spi_bufs(spi
, t
, m
->is_dma_mapped
);
334 m
->actual_length
+= t
->len
;
337 udelay(t
->delay_usecs
);
341 fsl_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
347 m
->complete(m
->context
);
349 if (status
|| !cs_change
) {
351 fsl_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
354 fsl_spi_setup_transfer(spi
, NULL
);
357 static int fsl_spi_setup(struct spi_device
*spi
)
359 struct mpc8xxx_spi
*mpc8xxx_spi
;
360 struct fsl_spi_reg
*reg_base
;
363 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
365 if (!spi
->max_speed_hz
)
369 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
372 spi
->controller_state
= cs
;
374 mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
376 reg_base
= mpc8xxx_spi
->reg_base
;
378 hw_mode
= cs
->hw_mode
; /* Save original settings */
379 cs
->hw_mode
= mpc8xxx_spi_read_reg(®_base
->mode
);
380 /* mask out bits we are going to set */
381 cs
->hw_mode
&= ~(SPMODE_CP_BEGIN_EDGECLK
| SPMODE_CI_INACTIVEHIGH
382 | SPMODE_REV
| SPMODE_LOOP
);
384 if (spi
->mode
& SPI_CPHA
)
385 cs
->hw_mode
|= SPMODE_CP_BEGIN_EDGECLK
;
386 if (spi
->mode
& SPI_CPOL
)
387 cs
->hw_mode
|= SPMODE_CI_INACTIVEHIGH
;
388 if (!(spi
->mode
& SPI_LSB_FIRST
))
389 cs
->hw_mode
|= SPMODE_REV
;
390 if (spi
->mode
& SPI_LOOP
)
391 cs
->hw_mode
|= SPMODE_LOOP
;
393 retval
= fsl_spi_setup_transfer(spi
, NULL
);
395 cs
->hw_mode
= hw_mode
; /* Restore settings */
399 /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
400 fsl_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
405 static void fsl_spi_cpu_irq(struct mpc8xxx_spi
*mspi
, u32 events
)
407 struct fsl_spi_reg
*reg_base
= mspi
->reg_base
;
409 /* We need handle RX first */
410 if (events
& SPIE_NE
) {
411 u32 rx_data
= mpc8xxx_spi_read_reg(®_base
->receive
);
414 mspi
->get_rx(rx_data
, mspi
);
417 if ((events
& SPIE_NF
) == 0)
418 /* spin until TX is done */
420 mpc8xxx_spi_read_reg(®_base
->event
)) &
424 /* Clear the events */
425 mpc8xxx_spi_write_reg(®_base
->event
, events
);
429 u32 word
= mspi
->get_tx(mspi
);
431 mpc8xxx_spi_write_reg(®_base
->transmit
, word
);
433 complete(&mspi
->done
);
437 static irqreturn_t
fsl_spi_irq(s32 irq
, void *context_data
)
439 struct mpc8xxx_spi
*mspi
= context_data
;
440 irqreturn_t ret
= IRQ_NONE
;
442 struct fsl_spi_reg
*reg_base
= mspi
->reg_base
;
444 /* Get interrupt events(tx/rx) */
445 events
= mpc8xxx_spi_read_reg(®_base
->event
);
449 dev_dbg(mspi
->dev
, "%s: events %x\n", __func__
, events
);
451 if (mspi
->flags
& SPI_CPM_MODE
)
452 fsl_spi_cpm_irq(mspi
, events
);
454 fsl_spi_cpu_irq(mspi
, events
);
459 static void fsl_spi_remove(struct mpc8xxx_spi
*mspi
)
461 iounmap(mspi
->reg_base
);
462 fsl_spi_cpm_free(mspi
);
465 static struct spi_master
* fsl_spi_probe(struct device
*dev
,
466 struct resource
*mem
, unsigned int irq
)
468 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
469 struct spi_master
*master
;
470 struct mpc8xxx_spi
*mpc8xxx_spi
;
471 struct fsl_spi_reg
*reg_base
;
475 master
= spi_alloc_master(dev
, sizeof(struct mpc8xxx_spi
));
476 if (master
== NULL
) {
481 dev_set_drvdata(dev
, master
);
483 ret
= mpc8xxx_spi_probe(dev
, mem
, irq
);
487 master
->setup
= fsl_spi_setup
;
489 mpc8xxx_spi
= spi_master_get_devdata(master
);
490 mpc8xxx_spi
->spi_do_one_msg
= fsl_spi_do_one_msg
;
491 mpc8xxx_spi
->spi_remove
= fsl_spi_remove
;
494 ret
= fsl_spi_cpm_init(mpc8xxx_spi
);
498 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
)
499 mpc8xxx_spi
->set_shifts
= fsl_spi_qe_cpu_set_shifts
;
501 if (mpc8xxx_spi
->set_shifts
)
502 /* 8 bits per word and MSB first */
503 mpc8xxx_spi
->set_shifts(&mpc8xxx_spi
->rx_shift
,
504 &mpc8xxx_spi
->tx_shift
, 8, 1);
506 mpc8xxx_spi
->reg_base
= ioremap(mem
->start
, resource_size(mem
));
507 if (mpc8xxx_spi
->reg_base
== NULL
) {
512 /* Register for SPI Interrupt */
513 ret
= request_irq(mpc8xxx_spi
->irq
, fsl_spi_irq
,
514 0, "fsl_spi", mpc8xxx_spi
);
519 reg_base
= mpc8xxx_spi
->reg_base
;
521 /* SPI controller initializations */
522 mpc8xxx_spi_write_reg(®_base
->mode
, 0);
523 mpc8xxx_spi_write_reg(®_base
->mask
, 0);
524 mpc8xxx_spi_write_reg(®_base
->command
, 0);
525 mpc8xxx_spi_write_reg(®_base
->event
, 0xffffffff);
527 /* Enable SPI interface */
528 regval
= pdata
->initial_spmode
| SPMODE_INIT_VAL
| SPMODE_ENABLE
;
529 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
)
532 mpc8xxx_spi_write_reg(®_base
->mode
, regval
);
534 ret
= spi_register_master(master
);
538 dev_info(dev
, "at 0x%p (irq = %d), %s mode\n", reg_base
,
539 mpc8xxx_spi
->irq
, mpc8xxx_spi_strmode(mpc8xxx_spi
->flags
));
544 free_irq(mpc8xxx_spi
->irq
, mpc8xxx_spi
);
546 iounmap(mpc8xxx_spi
->reg_base
);
548 fsl_spi_cpm_free(mpc8xxx_spi
);
551 spi_master_put(master
);
556 static void fsl_spi_cs_control(struct spi_device
*spi
, bool on
)
558 struct device
*dev
= spi
->dev
.parent
->parent
;
559 struct mpc8xxx_spi_probe_info
*pinfo
= to_of_pinfo(dev
->platform_data
);
560 u16 cs
= spi
->chip_select
;
561 int gpio
= pinfo
->gpios
[cs
];
562 bool alow
= pinfo
->alow_flags
[cs
];
564 gpio_set_value(gpio
, on
^ alow
);
567 static int of_fsl_spi_get_chipselects(struct device
*dev
)
569 struct device_node
*np
= dev
->of_node
;
570 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
571 struct mpc8xxx_spi_probe_info
*pinfo
= to_of_pinfo(pdata
);
576 ngpios
= of_gpio_count(np
);
579 * SPI w/o chip-select line. One SPI device is still permitted
582 pdata
->max_chipselect
= 1;
586 pinfo
->gpios
= kmalloc(ngpios
* sizeof(*pinfo
->gpios
), GFP_KERNEL
);
589 memset(pinfo
->gpios
, -1, ngpios
* sizeof(*pinfo
->gpios
));
591 pinfo
->alow_flags
= kzalloc(ngpios
* sizeof(*pinfo
->alow_flags
),
593 if (!pinfo
->alow_flags
) {
595 goto err_alloc_flags
;
598 for (; i
< ngpios
; i
++) {
600 enum of_gpio_flags flags
;
602 gpio
= of_get_gpio_flags(np
, i
, &flags
);
603 if (!gpio_is_valid(gpio
)) {
604 dev_err(dev
, "invalid gpio #%d: %d\n", i
, gpio
);
609 ret
= gpio_request(gpio
, dev_name(dev
));
611 dev_err(dev
, "can't request gpio #%d: %d\n", i
, ret
);
615 pinfo
->gpios
[i
] = gpio
;
616 pinfo
->alow_flags
[i
] = flags
& OF_GPIO_ACTIVE_LOW
;
618 ret
= gpio_direction_output(pinfo
->gpios
[i
],
619 pinfo
->alow_flags
[i
]);
621 dev_err(dev
, "can't set output direction for gpio "
622 "#%d: %d\n", i
, ret
);
627 pdata
->max_chipselect
= ngpios
;
628 pdata
->cs_control
= fsl_spi_cs_control
;
634 if (gpio_is_valid(pinfo
->gpios
[i
]))
635 gpio_free(pinfo
->gpios
[i
]);
639 kfree(pinfo
->alow_flags
);
640 pinfo
->alow_flags
= NULL
;
647 static int of_fsl_spi_free_chipselects(struct device
*dev
)
649 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
650 struct mpc8xxx_spi_probe_info
*pinfo
= to_of_pinfo(pdata
);
656 for (i
= 0; i
< pdata
->max_chipselect
; i
++) {
657 if (gpio_is_valid(pinfo
->gpios
[i
]))
658 gpio_free(pinfo
->gpios
[i
]);
662 kfree(pinfo
->alow_flags
);
666 static int of_fsl_spi_probe(struct platform_device
*ofdev
)
668 struct device
*dev
= &ofdev
->dev
;
669 struct device_node
*np
= ofdev
->dev
.of_node
;
670 struct spi_master
*master
;
675 ret
= of_mpc8xxx_spi_probe(ofdev
);
679 ret
= of_fsl_spi_get_chipselects(dev
);
683 ret
= of_address_to_resource(np
, 0, &mem
);
687 irq
= irq_of_parse_and_map(np
, 0);
693 master
= fsl_spi_probe(dev
, &mem
, irq
);
694 if (IS_ERR(master
)) {
695 ret
= PTR_ERR(master
);
702 of_fsl_spi_free_chipselects(dev
);
706 static int of_fsl_spi_remove(struct platform_device
*ofdev
)
710 ret
= mpc8xxx_spi_remove(&ofdev
->dev
);
713 of_fsl_spi_free_chipselects(&ofdev
->dev
);
717 static const struct of_device_id of_fsl_spi_match
[] = {
718 { .compatible
= "fsl,spi" },
721 MODULE_DEVICE_TABLE(of
, of_fsl_spi_match
);
723 static struct platform_driver of_fsl_spi_driver
= {
726 .owner
= THIS_MODULE
,
727 .of_match_table
= of_fsl_spi_match
,
729 .probe
= of_fsl_spi_probe
,
730 .remove
= of_fsl_spi_remove
,
733 #ifdef CONFIG_MPC832x_RDB
736 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
737 * only. The driver should go away soon, since newer MPC8323E-RDB's device
738 * tree can work with OpenFirmware driver. But for now we support old trees
741 static int plat_mpc8xxx_spi_probe(struct platform_device
*pdev
)
743 struct resource
*mem
;
745 struct spi_master
*master
;
747 if (!pdev
->dev
.platform_data
)
750 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
754 irq
= platform_get_irq(pdev
, 0);
758 master
= fsl_spi_probe(&pdev
->dev
, mem
, irq
);
759 return PTR_RET(master
);
762 static int plat_mpc8xxx_spi_remove(struct platform_device
*pdev
)
764 return mpc8xxx_spi_remove(&pdev
->dev
);
767 MODULE_ALIAS("platform:mpc8xxx_spi");
768 static struct platform_driver mpc8xxx_spi_driver
= {
769 .probe
= plat_mpc8xxx_spi_probe
,
770 .remove
= plat_mpc8xxx_spi_remove
,
772 .name
= "mpc8xxx_spi",
773 .owner
= THIS_MODULE
,
777 static bool legacy_driver_failed
;
779 static void __init
legacy_driver_register(void)
781 legacy_driver_failed
= platform_driver_register(&mpc8xxx_spi_driver
);
784 static void __exit
legacy_driver_unregister(void)
786 if (legacy_driver_failed
)
788 platform_driver_unregister(&mpc8xxx_spi_driver
);
791 static void __init
legacy_driver_register(void) {}
792 static void __exit
legacy_driver_unregister(void) {}
793 #endif /* CONFIG_MPC832x_RDB */
795 static int __init
fsl_spi_init(void)
797 legacy_driver_register();
798 return platform_driver_register(&of_fsl_spi_driver
);
800 module_init(fsl_spi_init
);
802 static void __exit
fsl_spi_exit(void)
804 platform_driver_unregister(&of_fsl_spi_driver
);
805 legacy_driver_unregister();
807 module_exit(fsl_spi_exit
);
809 MODULE_AUTHOR("Kumar Gala");
810 MODULE_DESCRIPTION("Simple Freescale SPI Driver");
811 MODULE_LICENSE("GPL");