Lines Matching +full:spi +full:- +full:mode
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MPC52xx PSC in SPI mode driver.
20 #include <linux/spi/spi.h>
47 static int mpc52xx_psc_spi_transfer_setup(struct spi_device *spi, in mpc52xx_psc_spi_transfer_setup() argument
50 struct mpc52xx_psc_spi_cs *cs = spi->controller_state; in mpc52xx_psc_spi_transfer_setup()
52 cs->speed_hz = (t && t->speed_hz) in mpc52xx_psc_spi_transfer_setup()
53 ? t->speed_hz : spi->max_speed_hz; in mpc52xx_psc_spi_transfer_setup()
54 cs->bits_per_word = (t && t->bits_per_word) in mpc52xx_psc_spi_transfer_setup()
55 ? t->bits_per_word : spi->bits_per_word; in mpc52xx_psc_spi_transfer_setup()
56 cs->bits_per_word = ((cs->bits_per_word + 7) / 8) * 8; in mpc52xx_psc_spi_transfer_setup()
60 static void mpc52xx_psc_spi_activate_cs(struct spi_device *spi) in mpc52xx_psc_spi_activate_cs() argument
62 struct mpc52xx_psc_spi_cs *cs = spi->controller_state; in mpc52xx_psc_spi_activate_cs()
63 struct mpc52xx_psc_spi *mps = spi_controller_get_devdata(spi->controller); in mpc52xx_psc_spi_activate_cs()
64 struct mpc52xx_psc __iomem *psc = mps->psc; in mpc52xx_psc_spi_activate_cs()
68 sicr = in_be32(&psc->sicr); in mpc52xx_psc_spi_activate_cs()
71 if (spi->mode & SPI_CPHA) in mpc52xx_psc_spi_activate_cs()
75 if (spi->mode & SPI_CPOL) in mpc52xx_psc_spi_activate_cs()
80 if (spi->mode & SPI_LSB_FIRST) in mpc52xx_psc_spi_activate_cs()
84 out_be32(&psc->sicr, sicr); in mpc52xx_psc_spi_activate_cs()
87 * Because psc->ccr is defined as 16bit register instead of 32bit in mpc52xx_psc_spi_activate_cs()
90 ccr = in_be16((u16 __iomem *)&psc->ccr); in mpc52xx_psc_spi_activate_cs()
92 if (cs->speed_hz) in mpc52xx_psc_spi_activate_cs()
93 ccr |= (MCLK / cs->speed_hz - 1) & 0xFF; in mpc52xx_psc_spi_activate_cs()
94 else /* by default SPI Clk 1MHz */ in mpc52xx_psc_spi_activate_cs()
95 ccr |= (MCLK / 1000000 - 1) & 0xFF; in mpc52xx_psc_spi_activate_cs()
96 out_be16((u16 __iomem *)&psc->ccr, ccr); in mpc52xx_psc_spi_activate_cs()
97 mps->bits_per_word = cs->bits_per_word; in mpc52xx_psc_spi_activate_cs()
104 static int mpc52xx_psc_spi_transfer_rxtx(struct spi_device *spi, in mpc52xx_psc_spi_transfer_rxtx() argument
107 struct mpc52xx_psc_spi *mps = spi_controller_get_devdata(spi->controller); in mpc52xx_psc_spi_transfer_rxtx()
108 struct mpc52xx_psc __iomem *psc = mps->psc; in mpc52xx_psc_spi_transfer_rxtx()
109 struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo; in mpc52xx_psc_spi_transfer_rxtx()
112 unsigned char *rx_buf = (unsigned char *)t->rx_buf; in mpc52xx_psc_spi_transfer_rxtx()
113 unsigned char *tx_buf = (unsigned char *)t->tx_buf; in mpc52xx_psc_spi_transfer_rxtx()
119 if (!t->tx_buf && !t->rx_buf && t->len) in mpc52xx_psc_spi_transfer_rxtx()
120 return -EINVAL; in mpc52xx_psc_spi_transfer_rxtx()
123 out_8(&psc->command, MPC52xx_PSC_TX_ENABLE | MPC52xx_PSC_RX_ENABLE); in mpc52xx_psc_spi_transfer_rxtx()
124 while (rb < t->len) { in mpc52xx_psc_spi_transfer_rxtx()
125 if (t->len - rb > MPC52xx_PSC_BUFSIZE) { in mpc52xx_psc_spi_transfer_rxtx()
129 send_at_once = t->len - sb; in mpc52xx_psc_spi_transfer_rxtx()
130 rfalarm = MPC52xx_PSC_BUFSIZE - (t->len - rb); in mpc52xx_psc_spi_transfer_rxtx()
134 dev_dbg(&spi->dev, "send %d bytes...\n", send_at_once); in mpc52xx_psc_spi_transfer_rxtx()
135 for (; send_at_once; sb++, send_at_once--) { in mpc52xx_psc_spi_transfer_rxtx()
138 out_8(&psc->ircr2, 0x01); in mpc52xx_psc_spi_transfer_rxtx()
141 out_8(&psc->mpc52xx_psc_buffer_8, tx_buf[sb]); in mpc52xx_psc_spi_transfer_rxtx()
143 out_8(&psc->mpc52xx_psc_buffer_8, 0); in mpc52xx_psc_spi_transfer_rxtx()
151 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1); in mpc52xx_psc_spi_transfer_rxtx()
152 if (t->len - rb == 1) { in mpc52xx_psc_spi_transfer_rxtx()
153 out_8(&psc->mode, 0); in mpc52xx_psc_spi_transfer_rxtx()
155 out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL); in mpc52xx_psc_spi_transfer_rxtx()
156 out_be16(&fifo->rfalarm, rfalarm); in mpc52xx_psc_spi_transfer_rxtx()
158 out_be16(&psc->mpc52xx_psc_imr, MPC52xx_PSC_IMR_RXRDY); in mpc52xx_psc_spi_transfer_rxtx()
159 wait_for_completion(&mps->done); in mpc52xx_psc_spi_transfer_rxtx()
160 recv_at_once = in_be16(&fifo->rfnum); in mpc52xx_psc_spi_transfer_rxtx()
161 dev_dbg(&spi->dev, "%d bytes received\n", recv_at_once); in mpc52xx_psc_spi_transfer_rxtx()
165 for (; recv_at_once; rb++, recv_at_once--) in mpc52xx_psc_spi_transfer_rxtx()
166 rx_buf[rb] = in_8(&psc->mpc52xx_psc_buffer_8); in mpc52xx_psc_spi_transfer_rxtx()
168 for (; recv_at_once; rb++, recv_at_once--) in mpc52xx_psc_spi_transfer_rxtx()
169 in_8(&psc->mpc52xx_psc_buffer_8); in mpc52xx_psc_spi_transfer_rxtx()
173 out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE); in mpc52xx_psc_spi_transfer_rxtx()
181 struct spi_device *spi; in mpc52xx_psc_spi_transfer_one_message() local
186 spi = m->spi; in mpc52xx_psc_spi_transfer_one_message()
189 list_for_each_entry (t, &m->transfers, transfer_list) { in mpc52xx_psc_spi_transfer_one_message()
190 if (t->bits_per_word || t->speed_hz) { in mpc52xx_psc_spi_transfer_one_message()
191 status = mpc52xx_psc_spi_transfer_setup(spi, t); in mpc52xx_psc_spi_transfer_one_message()
197 mpc52xx_psc_spi_activate_cs(spi); in mpc52xx_psc_spi_transfer_one_message()
198 cs_change = t->cs_change; in mpc52xx_psc_spi_transfer_one_message()
200 status = mpc52xx_psc_spi_transfer_rxtx(spi, t); in mpc52xx_psc_spi_transfer_one_message()
203 m->actual_length += t->len; in mpc52xx_psc_spi_transfer_one_message()
208 m->status = status; in mpc52xx_psc_spi_transfer_one_message()
210 mpc52xx_psc_spi_transfer_setup(spi, NULL); in mpc52xx_psc_spi_transfer_one_message()
217 static int mpc52xx_psc_spi_setup(struct spi_device *spi) in mpc52xx_psc_spi_setup() argument
219 struct mpc52xx_psc_spi_cs *cs = spi->controller_state; in mpc52xx_psc_spi_setup()
221 if (spi->bits_per_word%8) in mpc52xx_psc_spi_setup()
222 return -EINVAL; in mpc52xx_psc_spi_setup()
227 return -ENOMEM; in mpc52xx_psc_spi_setup()
228 spi->controller_state = cs; in mpc52xx_psc_spi_setup()
231 cs->bits_per_word = spi->bits_per_word; in mpc52xx_psc_spi_setup()
232 cs->speed_hz = spi->max_speed_hz; in mpc52xx_psc_spi_setup()
237 static void mpc52xx_psc_spi_cleanup(struct spi_device *spi) in mpc52xx_psc_spi_cleanup() argument
239 kfree(spi->controller_state); in mpc52xx_psc_spi_cleanup()
244 struct mpc52xx_psc __iomem *psc = mps->psc; in mpc52xx_psc_spi_port_config()
245 struct mpc52xx_psc_fifo __iomem *fifo = mps->fifo; in mpc52xx_psc_spi_port_config()
256 out_8(&psc->command, MPC52xx_PSC_RST_RX); in mpc52xx_psc_spi_port_config()
257 out_8(&psc->command, MPC52xx_PSC_RST_TX); in mpc52xx_psc_spi_port_config()
258 out_8(&psc->command, MPC52xx_PSC_TX_DISABLE | MPC52xx_PSC_RX_DISABLE); in mpc52xx_psc_spi_port_config()
261 out_be16(&psc->mpc52xx_psc_imr, 0); in mpc52xx_psc_spi_port_config()
262 out_8(&psc->command, MPC52xx_PSC_SEL_MODE_REG_1); in mpc52xx_psc_spi_port_config()
263 out_8(&fifo->rfcntl, 0); in mpc52xx_psc_spi_port_config()
264 out_8(&psc->mode, MPC52xx_PSC_MODE_FFULL); in mpc52xx_psc_spi_port_config()
266 /* Configure 8bit codec mode as a SPI host and use EOF flags */ in mpc52xx_psc_spi_port_config()
268 out_be32(&psc->sicr, 0x0180C800); in mpc52xx_psc_spi_port_config()
269 out_be16((u16 __iomem *)&psc->ccr, 0x070F); /* default SPI Clk 1MHz */ in mpc52xx_psc_spi_port_config()
272 out_8(&psc->ctur, 0x00); in mpc52xx_psc_spi_port_config()
273 out_8(&psc->ctlr, 0x84); in mpc52xx_psc_spi_port_config()
275 mps->bits_per_word = 8; in mpc52xx_psc_spi_port_config()
283 struct mpc52xx_psc __iomem *psc = mps->psc; in mpc52xx_psc_spi_isr()
286 if (in_be16(&psc->mpc52xx_psc_isr) & MPC52xx_PSC_IMR_RXRDY) { in mpc52xx_psc_spi_isr()
287 out_be16(&psc->mpc52xx_psc_imr, 0); in mpc52xx_psc_spi_isr()
288 complete(&mps->done); in mpc52xx_psc_spi_isr()
296 struct device *dev = &pdev->dev; in mpc52xx_psc_spi_of_probe()
304 return -ENOMEM; in mpc52xx_psc_spi_of_probe()
309 /* the spi->mode bits understood by this driver: */ in mpc52xx_psc_spi_of_probe()
310 host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST; in mpc52xx_psc_spi_of_probe()
312 ret = device_property_read_u32(dev, "cell-index", &bus_num); in mpc52xx_psc_spi_of_probe()
314 return dev_err_probe(dev, ret ? : -EINVAL, "Invalid cell-index property\n"); in mpc52xx_psc_spi_of_probe()
315 host->bus_num = bus_num + 1; in mpc52xx_psc_spi_of_probe()
317 host->num_chipselect = 255; in mpc52xx_psc_spi_of_probe()
318 host->setup = mpc52xx_psc_spi_setup; in mpc52xx_psc_spi_of_probe()
319 host->transfer_one_message = mpc52xx_psc_spi_transfer_one_message; in mpc52xx_psc_spi_of_probe()
320 host->cleanup = mpc52xx_psc_spi_cleanup; in mpc52xx_psc_spi_of_probe()
322 device_set_node(&host->dev, dev_fwnode(dev)); in mpc52xx_psc_spi_of_probe()
324 mps->psc = devm_platform_get_and_ioremap_resource(pdev, 0, NULL); in mpc52xx_psc_spi_of_probe()
325 if (IS_ERR(mps->psc)) in mpc52xx_psc_spi_of_probe()
326 return dev_err_probe(dev, PTR_ERR(mps->psc), "could not ioremap I/O port range\n"); in mpc52xx_psc_spi_of_probe()
329 mps->fifo = ((void __iomem *)mps->psc) + sizeof(struct mpc52xx_psc); in mpc52xx_psc_spi_of_probe()
331 mps->irq = platform_get_irq(pdev, 0); in mpc52xx_psc_spi_of_probe()
332 if (mps->irq < 0) in mpc52xx_psc_spi_of_probe()
333 return mps->irq; in mpc52xx_psc_spi_of_probe()
335 ret = devm_request_irq(dev, mps->irq, mpc52xx_psc_spi_isr, 0, in mpc52xx_psc_spi_of_probe()
336 "mpc52xx-psc-spi", mps); in mpc52xx_psc_spi_of_probe()
340 ret = mpc52xx_psc_spi_port_config(host->bus_num, mps); in mpc52xx_psc_spi_of_probe()
342 return dev_err_probe(dev, ret, "can't configure PSC! Is it capable of SPI?\n"); in mpc52xx_psc_spi_of_probe()
344 init_completion(&mps->done); in mpc52xx_psc_spi_of_probe()
350 { .compatible = "fsl,mpc5200-psc-spi", },
351 { .compatible = "mpc5200-psc-spi", }, /* old */
360 .name = "mpc52xx-psc-spi",
367 MODULE_DESCRIPTION("MPC52xx PSC SPI Driver");