2 * MPC83xx SPI controller driver.
4 * Maintainer: Kumar Gala
6 * Copyright (C) 2006 Polycom, Inc.
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.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/completion.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
20 #include <linux/irq.h>
21 #include <linux/device.h>
22 #include <linux/spi/spi.h>
23 #include <linux/spi/spi_bitbang.h>
24 #include <linux/platform_device.h>
25 #include <linux/fsl_devices.h>
30 /* SPI Controller registers */
31 struct mpc83xx_spi_reg {
41 /* SPI Controller mode register definitions */
42 #define SPMODE_CI_INACTIVEHIGH (1 << 29)
43 #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
44 #define SPMODE_DIV16 (1 << 27)
45 #define SPMODE_REV (1 << 26)
46 #define SPMODE_MS (1 << 25)
47 #define SPMODE_ENABLE (1 << 24)
48 #define SPMODE_LEN(x) ((x) << 20)
49 #define SPMODE_PM(x) ((x) << 16)
50 #define SPMODE_OP (1 << 14)
53 * Default for SPI Mode:
54 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
56 #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
57 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
59 /* SPIE register values */
60 #define SPIE_NE 0x00000200 /* Not empty */
61 #define SPIE_NF 0x00000100 /* Not full */
63 /* SPIM register values */
64 #define SPIM_NE 0x00000200 /* Not empty */
65 #define SPIM_NF 0x00000100 /* Not full */
67 /* SPI Controller driver's private data. */
69 /* bitbang has to be first */
70 struct spi_bitbang bitbang;
71 struct completion done;
73 struct mpc83xx_spi_reg __iomem *base;
75 /* rx & tx bufs from the spi_transfer */
79 /* functions to deal with different sized buffers */
80 void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
81 u32(*get_tx) (struct mpc83xx_spi *);
86 unsigned nsecs; /* (clock cycle time)/2 */
89 u32 rx_shift; /* RX data reg shift when in qe mode */
90 u32 tx_shift; /* TX data reg shift when in qe mode */
94 void (*activate_cs) (u8 cs, u8 polarity);
95 void (*deactivate_cs) (u8 cs, u8 polarity);
98 static inline void mpc83xx_spi_write_reg(__be32 __iomem * reg, u32 val)
103 static inline u32 mpc83xx_spi_read_reg(__be32 __iomem * reg)
108 #define MPC83XX_SPI_RX_BUF(type) \
109 void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \
111 type * rx = mpc83xx_spi->rx; \
112 *rx++ = (type)(data >> mpc83xx_spi->rx_shift); \
113 mpc83xx_spi->rx = rx; \
116 #define MPC83XX_SPI_TX_BUF(type) \
117 u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi) \
120 const type * tx = mpc83xx_spi->tx; \
123 data = *tx++ << mpc83xx_spi->tx_shift; \
124 mpc83xx_spi->tx = tx; \
128 MPC83XX_SPI_RX_BUF(u8)
129 MPC83XX_SPI_RX_BUF(u16)
130 MPC83XX_SPI_RX_BUF(u32)
131 MPC83XX_SPI_TX_BUF(u8)
132 MPC83XX_SPI_TX_BUF(u16)
133 MPC83XX_SPI_TX_BUF(u32)
135 static void mpc83xx_spi_chipselect(struct spi_device *spi, int value)
137 struct mpc83xx_spi *mpc83xx_spi;
138 u8 pol = spi->mode & SPI_CS_HIGH ? 1 : 0;
140 mpc83xx_spi = spi_master_get_devdata(spi->master);
142 if (value == BITBANG_CS_INACTIVE) {
143 if (mpc83xx_spi->deactivate_cs)
144 mpc83xx_spi->deactivate_cs(spi->chip_select, pol);
147 if (value == BITBANG_CS_ACTIVE) {
148 u32 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
149 u32 len = spi->bits_per_word;
155 /* mask out bits we are going to set */
156 regval &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
157 | SPMODE_LEN(0xF) | SPMODE_DIV16
158 | SPMODE_PM(0xF) | SPMODE_REV);
160 if (spi->mode & SPI_CPHA)
161 regval |= SPMODE_CP_BEGIN_EDGECLK;
162 if (spi->mode & SPI_CPOL)
163 regval |= SPMODE_CI_INACTIVEHIGH;
164 if (!(spi->mode & SPI_LSB_FIRST))
165 regval |= SPMODE_REV;
167 regval |= SPMODE_LEN(len);
169 if ((mpc83xx_spi->sysclk / spi->max_speed_hz) >= 64) {
170 u8 pm = mpc83xx_spi->sysclk / (spi->max_speed_hz * 64);
172 printk(KERN_WARNING "MPC83xx SPI: SPICLK can't be less then a SYSCLK/1024!\n"
173 "Requested SPICLK is %d Hz. Will use %d Hz instead.\n",
174 spi->max_speed_hz, mpc83xx_spi->sysclk / 1024);
177 regval |= SPMODE_PM(pm) | SPMODE_DIV16;
179 u8 pm = mpc83xx_spi->sysclk / (spi->max_speed_hz * 4);
180 regval |= SPMODE_PM(pm);
183 /* Turn off SPI unit prior changing mode */
184 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
185 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
186 if (mpc83xx_spi->activate_cs)
187 mpc83xx_spi->activate_cs(spi->chip_select, pol);
192 int mpc83xx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
194 struct mpc83xx_spi *mpc83xx_spi;
199 mpc83xx_spi = spi_master_get_devdata(spi->master);
202 bits_per_word = t->bits_per_word;
209 /* spi_transfer level calls that work per-word */
211 bits_per_word = spi->bits_per_word;
213 /* Make sure its a bit width we support [4..16, 32] */
214 if ((bits_per_word < 4)
215 || ((bits_per_word > 16) && (bits_per_word != 32)))
218 mpc83xx_spi->rx_shift = 0;
219 mpc83xx_spi->tx_shift = 0;
220 if (bits_per_word <= 8) {
221 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
222 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
223 if (mpc83xx_spi->qe_mode) {
224 mpc83xx_spi->rx_shift = 16;
225 mpc83xx_spi->tx_shift = 24;
227 } else if (bits_per_word <= 16) {
228 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u16;
229 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u16;
230 if (mpc83xx_spi->qe_mode) {
231 mpc83xx_spi->rx_shift = 16;
232 mpc83xx_spi->tx_shift = 16;
234 } else if (bits_per_word <= 32) {
235 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u32;
236 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u32;
240 /* nsecs = (clock period)/2 */
242 hz = spi->max_speed_hz;
243 mpc83xx_spi->nsecs = (1000000000 / 2) / hz;
244 if (mpc83xx_spi->nsecs > MAX_UDELAY_MS * 1000)
247 if (bits_per_word == 32)
250 bits_per_word = bits_per_word - 1;
252 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
254 /* mask out bits we are going to set */
255 regval &= ~(SPMODE_LEN(0xF) | SPMODE_REV);
256 regval |= SPMODE_LEN(bits_per_word);
257 if (!(spi->mode & SPI_LSB_FIRST))
258 regval |= SPMODE_REV;
260 /* Turn off SPI unit prior changing mode */
261 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
262 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
267 /* the spi->mode bits understood by this driver: */
268 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST)
270 static int mpc83xx_spi_setup(struct spi_device *spi)
272 struct spi_bitbang *bitbang;
273 struct mpc83xx_spi *mpc83xx_spi;
276 if (spi->mode & ~MODEBITS) {
277 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
278 spi->mode & ~MODEBITS);
282 if (!spi->max_speed_hz)
285 bitbang = spi_master_get_devdata(spi->master);
286 mpc83xx_spi = spi_master_get_devdata(spi->master);
288 if (!spi->bits_per_word)
289 spi->bits_per_word = 8;
291 retval = mpc83xx_spi_setup_transfer(spi, NULL);
295 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec\n",
296 __FUNCTION__, spi->mode & (SPI_CPOL | SPI_CPHA),
297 spi->bits_per_word, 2 * mpc83xx_spi->nsecs);
299 /* NOTE we _need_ to call chipselect() early, ideally with adapter
300 * setup, unless the hardware defaults cooperate to avoid confusion
301 * between normal (active low) and inverted chipselects.
304 /* deselect chip (low or high) */
305 spin_lock(&bitbang->lock);
306 if (!bitbang->busy) {
307 bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
308 ndelay(mpc83xx_spi->nsecs);
310 spin_unlock(&bitbang->lock);
315 static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
317 struct mpc83xx_spi *mpc83xx_spi;
320 mpc83xx_spi = spi_master_get_devdata(spi->master);
322 mpc83xx_spi->tx = t->tx_buf;
323 mpc83xx_spi->rx = t->rx_buf;
324 mpc83xx_spi->count = t->len;
325 INIT_COMPLETION(mpc83xx_spi->done);
328 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE);
331 word = mpc83xx_spi->get_tx(mpc83xx_spi);
332 mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
334 wait_for_completion(&mpc83xx_spi->done);
336 /* disable rx ints */
337 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
339 return t->len - mpc83xx_spi->count;
342 irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data)
344 struct mpc83xx_spi *mpc83xx_spi = context_data;
346 irqreturn_t ret = IRQ_NONE;
348 /* Get interrupt events(tx/rx) */
349 event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event);
351 /* We need handle RX first */
352 if (event & SPIE_NE) {
353 u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive);
356 mpc83xx_spi->get_rx(rx_data, mpc83xx_spi);
361 if ((event & SPIE_NF) == 0)
362 /* spin until TX is done */
364 mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) &
368 mpc83xx_spi->count -= 1;
369 if (mpc83xx_spi->count) {
370 if (mpc83xx_spi->tx) {
371 u32 word = mpc83xx_spi->get_tx(mpc83xx_spi);
372 mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit,
376 complete(&mpc83xx_spi->done);
379 /* Clear the events */
380 mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event);
385 static int __init mpc83xx_spi_probe(struct platform_device *dev)
387 struct spi_master *master;
388 struct mpc83xx_spi *mpc83xx_spi;
389 struct fsl_spi_platform_data *pdata;
394 /* Get resources(memory, IRQ) associated with the device */
395 master = spi_alloc_master(&dev->dev, sizeof(struct mpc83xx_spi));
397 if (master == NULL) {
402 platform_set_drvdata(dev, master);
403 pdata = dev->dev.platform_data;
410 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
415 mpc83xx_spi = spi_master_get_devdata(master);
416 mpc83xx_spi->bitbang.master = spi_master_get(master);
417 mpc83xx_spi->bitbang.chipselect = mpc83xx_spi_chipselect;
418 mpc83xx_spi->bitbang.setup_transfer = mpc83xx_spi_setup_transfer;
419 mpc83xx_spi->bitbang.txrx_bufs = mpc83xx_spi_bufs;
420 mpc83xx_spi->sysclk = pdata->sysclk;
421 mpc83xx_spi->activate_cs = pdata->activate_cs;
422 mpc83xx_spi->deactivate_cs = pdata->deactivate_cs;
423 mpc83xx_spi->qe_mode = pdata->qe_mode;
424 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
425 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
427 mpc83xx_spi->rx_shift = 0;
428 mpc83xx_spi->tx_shift = 0;
429 if (mpc83xx_spi->qe_mode) {
430 mpc83xx_spi->rx_shift = 16;
431 mpc83xx_spi->tx_shift = 24;
434 mpc83xx_spi->bitbang.master->setup = mpc83xx_spi_setup;
435 init_completion(&mpc83xx_spi->done);
437 mpc83xx_spi->base = ioremap(r->start, r->end - r->start + 1);
438 if (mpc83xx_spi->base == NULL) {
443 mpc83xx_spi->irq = platform_get_irq(dev, 0);
445 if (mpc83xx_spi->irq < 0) {
450 /* Register for SPI Interrupt */
451 ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
452 0, "mpc83xx_spi", mpc83xx_spi);
457 master->bus_num = pdata->bus_num;
458 master->num_chipselect = pdata->max_chipselect;
460 /* SPI controller initializations */
461 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
462 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
463 mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0);
464 mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff);
466 /* Enable SPI interface */
467 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
471 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
473 ret = spi_bitbang_start(&mpc83xx_spi->bitbang);
479 "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
480 dev->dev.bus_id, mpc83xx_spi->base, mpc83xx_spi->irq);
485 free_irq(mpc83xx_spi->irq, mpc83xx_spi);
487 iounmap(mpc83xx_spi->base);
489 spi_master_put(master);
496 static int __devexit mpc83xx_spi_remove(struct platform_device *dev)
498 struct mpc83xx_spi *mpc83xx_spi;
499 struct spi_master *master;
501 master = platform_get_drvdata(dev);
502 mpc83xx_spi = spi_master_get_devdata(master);
504 spi_bitbang_stop(&mpc83xx_spi->bitbang);
505 free_irq(mpc83xx_spi->irq, mpc83xx_spi);
506 iounmap(mpc83xx_spi->base);
507 spi_master_put(mpc83xx_spi->bitbang.master);
512 static struct platform_driver mpc83xx_spi_driver = {
513 .probe = mpc83xx_spi_probe,
514 .remove = __devexit_p(mpc83xx_spi_remove),
516 .name = "mpc83xx_spi",
520 static int __init mpc83xx_spi_init(void)
522 return platform_driver_register(&mpc83xx_spi_driver);
525 static void __exit mpc83xx_spi_exit(void)
527 platform_driver_unregister(&mpc83xx_spi_driver);
530 module_init(mpc83xx_spi_init);
531 module_exit(mpc83xx_spi_exit);
533 MODULE_AUTHOR("Kumar Gala");
534 MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
535 MODULE_LICENSE("GPL");