18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
45 #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
48 #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
63 #define SPMODE_LOOP (1 << 30)
64 #define SPMODE_CI_INACTIVEHIGH (1 << 29)
65 #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
66 #define SPMODE_DIV16 (1 << 27)
67 #define SPMODE_REV (1 << 26)
68 #define SPMODE_MS (1 << 25)
69 #define SPMODE_ENABLE (1 << 24)
70 #define SPMODE_LEN(x) ((x) << 20)
71 #define SPMODE_PM(x) ((x) << 16)
72 #define SPMODE_OP (1 << 14)
73 #define SPMODE_CG(x) ((x) << 7)
79 #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
80 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
83 #define SPIE_NE 0x00000200
84 #define SPIE_NF 0x00000100
87 #define SPIM_NE 0x00000200
88 #define SPIM_NF 0x00000100
90 #define SPIE_TXB 0x00000200
91 #define SPIE_RXB 0x00000100
94 #define SPCOM_STR (1 << 23)
96 #define SPI_PRAM_SIZE 0x100
97 #define SPI_MRBLR ((unsigned int)PAGE_SIZE)
99 static void *fsl_dummy_rx;
101 static int fsl_dummy_rx_refcnt;
111 if (cs->
hw_mode == mpc8xxx_spi_read_reg(mode))
124 QE_CR_PROTOCOL_UNSPECIFIED, 0);
135 mpc8xxx_spi_write_reg(mode, cs->
hw_mode);
146 pdata = spi->
dev.parent->parent->platform_data;
159 fsl_spi_change_mode(spi);
168 struct mpc8xxx_spi *mpc8xxx_spi,
173 if (bits_per_word <= 8) {
180 }
else if (bits_per_word <= 16) {
187 }
else if (bits_per_word <= 32) {
196 if (bits_per_word <= 8)
206 return bits_per_word;
222 if (bits_per_word > 8)
224 return bits_per_word;
227 static int fsl_spi_setup_transfer(
struct spi_device *spi,
230 struct mpc8xxx_spi *mpc8xxx_spi;
231 int bits_per_word = 0;
236 mpc8xxx_spi = spi_master_get_devdata(spi->
master);
248 if ((bits_per_word < 4)
249 || ((bits_per_word > 16) && (bits_per_word != 32)))
256 bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
260 bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
263 if (bits_per_word < 0)
264 return bits_per_word;
266 if (bits_per_word == 32)
269 bits_per_word = bits_per_word - 1;
277 if ((mpc8xxx_spi->
spibrg / hz) > 64) {
279 pm = (mpc8xxx_spi->
spibrg - 1) / (hz * 64) + 1;
281 WARN_ONCE(pm > 16,
"%s: Requested speed is too low: %d Hz. "
282 "Will use %d Hz instead.\n", dev_name(&spi->
dev),
283 hz, mpc8xxx_spi->
spibrg / 1024);
287 pm = (mpc8xxx_spi->
spibrg - 1) / (hz * 4) + 1;
294 fsl_spi_change_mode(spi);
298 static void fsl_spi_cpm_bufs_start(
struct mpc8xxx_spi *mspi)
303 unsigned int xfer_ofs;
327 static int fsl_spi_cpm_bufs(
struct mpc8xxx_spi *mspi,
352 void *nonconst_tx = (
void *)mspi->
tx;
357 dev_err(dev,
"unable to map tx dma\n");
368 dev_err(dev,
"unable to map rx dma\n");
382 fsl_spi_cpm_bufs_start(mspi);
392 static void fsl_spi_cpm_bufs_complete(
struct mpc8xxx_spi *mspi)
404 static int fsl_spi_cpu_bufs(
struct mpc8xxx_spi *mspi,
416 word = mspi->
get_tx(mspi);
417 mpc8xxx_spi_write_reg(®_base->
transmit, word);
425 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->
master);
427 unsigned int len = t->
len;
436 if (bits_per_word > 8) {
442 if (bits_per_word > 16) {
455 ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
457 ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
464 mpc8xxx_spi_write_reg(®_base->
mask, 0);
467 fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
469 return mpc8xxx_spi->
count;
477 const int nsecs = 50;
488 status = fsl_spi_setup_transfer(spi, t);
519 if (status || !cs_change) {
524 fsl_spi_setup_transfer(spi,
NULL);
527 static int fsl_spi_setup(
struct spi_device *spi)
529 struct mpc8xxx_spi *mpc8xxx_spi;
544 mpc8xxx_spi = spi_master_get_devdata(spi->
master);
549 cs->
hw_mode = mpc8xxx_spi_read_reg(®_base->
mode);
563 retval = fsl_spi_setup_transfer(spi,
NULL);
571 static void fsl_spi_cpm_irq(
struct mpc8xxx_spi *mspi,
u32 events)
576 dev_dbg(mspi->
dev,
"%s: bd datlen %d, count %d\n", __func__,
580 if (len > mspi->
count) {
586 mpc8xxx_spi_write_reg(®_base->
event, events);
590 fsl_spi_cpm_bufs_start(mspi);
595 static void fsl_spi_cpu_irq(
struct mpc8xxx_spi *mspi,
u32 events)
604 mspi->
get_rx(rx_data, mspi);
610 mpc8xxx_spi_read_reg(®_base->
event)) &
615 mpc8xxx_spi_write_reg(®_base->
event, events);
621 mpc8xxx_spi_write_reg(®_base->
transmit, word);
629 struct mpc8xxx_spi *mspi = context_data;
635 events = mpc8xxx_spi_read_reg(®_base->
event);
639 dev_dbg(mspi->
dev,
"%s: events %x\n", __func__, events);
642 fsl_spi_cpm_irq(mspi, events);
644 fsl_spi_cpu_irq(mspi, events);
649 static void *fsl_spi_alloc_dummy_rx(
void)
656 fsl_dummy_rx_refcnt++;
663 static void fsl_spi_free_dummy_rx(
void)
667 switch (fsl_dummy_rx_refcnt) {
676 fsl_dummy_rx_refcnt--;
683 static unsigned long fsl_spi_cpm_get_pram(
struct mpc8xxx_spi *mspi)
690 unsigned long pram_ofs = -
ENOMEM;
696 if (mspi->
flags &
SPI_QE && iprop && size ==
sizeof(*iprop) * 4)
703 QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
708 if (spi_base ==
NULL)
731 static int fsl_spi_cpm_init(
struct mpc8xxx_spi *mspi)
737 unsigned long pram_ofs;
738 unsigned long bds_ofs;
743 if (!fsl_spi_alloc_dummy_rx())
748 if (iprop && size ==
sizeof(*iprop))
753 dev_warn(dev,
"cell-index unspecified, assuming SPI1");
756 mspi->
subblock = QE_CR_SUBBLOCK_SPI1;
759 mspi->
subblock = QE_CR_SUBBLOCK_SPI2;
764 pram_ofs = fsl_spi_cpm_get_pram(mspi);
766 dev_err(dev,
"can't allocate spi parameter ram\n");
771 sizeof(*mspi->
rx_bd), 8);
773 dev_err(dev,
"can't allocate bds\n");
780 dev_err(dev,
"unable to map dummy tx buffer\n");
787 dev_err(dev,
"unable to map dummy rx buffer\n");
822 fsl_spi_free_dummy_rx();
826 static void fsl_spi_cpm_free(
struct mpc8xxx_spi *mspi)
837 fsl_spi_free_dummy_rx();
840 static void fsl_spi_remove(
struct mpc8xxx_spi *mspi)
843 fsl_spi_cpm_free(mspi);
851 struct mpc8xxx_spi *mpc8xxx_spi;
857 if (master ==
NULL) {
868 master->
setup = fsl_spi_setup;
870 mpc8xxx_spi = spi_master_get_devdata(master);
875 ret = fsl_spi_cpm_init(mpc8xxx_spi);
892 0,
"fsl_spi", mpc8xxx_spi);
900 mpc8xxx_spi_write_reg(®_base->
mode, 0);
901 mpc8xxx_spi_write_reg(®_base->
mask, 0);
902 mpc8xxx_spi_write_reg(®_base->
command, 0);
903 mpc8xxx_spi_write_reg(®_base->
event, 0xffffffff);
910 mpc8xxx_spi_write_reg(®_base->
mode, regval);
916 dev_info(dev,
"at 0x%p (irq = %d), %s mode\n", reg_base,
926 fsl_spi_cpm_free(mpc8xxx_spi);
929 spi_master_put(master);
934 static void fsl_spi_cs_control(
struct spi_device *spi,
bool on)
936 struct device *dev = spi->
dev.parent->parent;
945 static int of_fsl_spi_get_chipselects(
struct device *dev)
954 ngpios = of_gpio_count(np);
973 goto err_alloc_flags;
976 for (; i < ngpios; i++) {
980 gpio = of_get_gpio_flags(np, i, &flags);
981 if (!gpio_is_valid(gpio)) {
982 dev_err(dev,
"invalid gpio #%d: %d\n", i, gpio);
989 dev_err(dev,
"can't request gpio #%d: %d\n", i, ret);
999 dev_err(dev,
"can't set output direction for gpio "
1000 "#%d: %d\n", i, ret);
1012 if (gpio_is_valid(pinfo->
gpios[i]))
1025 static int of_fsl_spi_free_chipselects(
struct device *dev)
1035 if (gpio_is_valid(pinfo->
gpios[i]))
1057 ret = of_fsl_spi_get_chipselects(dev);
1071 master = fsl_spi_probe(dev, &mem, irq.
start);
1072 if (IS_ERR(master)) {
1073 ret = PTR_ERR(master);
1080 of_fsl_spi_free_chipselects(dev);
1091 of_fsl_spi_free_chipselects(&ofdev->
dev);
1095 static const struct of_device_id of_fsl_spi_match[] = {
1096 { .compatible =
"fsl,spi" },
1105 .of_match_table = of_fsl_spi_match,
1107 .probe = of_fsl_spi_probe,
1111 #ifdef CONFIG_MPC832x_RDB
1125 if (!pdev->
dev.platform_data)
1136 master = fsl_spi_probe(&pdev->
dev, mem, irq);
1138 return PTR_ERR(master);
1149 .
probe = plat_mpc8xxx_spi_probe,
1152 .name =
"mpc8xxx_spi",
1157 static bool legacy_driver_failed;
1159 static void __init legacy_driver_register(
void)
1164 static void __exit legacy_driver_unregister(
void)
1166 if (legacy_driver_failed)
1171 static void __init legacy_driver_register(
void) {}
1172 static void __exit legacy_driver_unregister(
void) {}
1175 static int __init fsl_spi_init(
void)
1177 legacy_driver_register();
1182 static void __exit fsl_spi_exit(
void)
1185 legacy_driver_unregister();