2 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright (C) 2008 Juergen Beisert
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation
17 * 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1301, USA.
21 #include <linux/clk.h>
22 #include <linux/completion.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/gpio.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
29 #include <linux/irq.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/platform_device.h>
33 #include <linux/spi/spi.h>
34 #include <linux/spi/spi_bitbang.h>
35 #include <linux/types.h>
39 #define DRIVER_NAME "spi_imx"
41 #define MXC_CSPIRXDATA 0x00
42 #define MXC_CSPITXDATA 0x04
43 #define MXC_CSPICTRL 0x08
44 #define MXC_CSPIINT 0x0c
45 #define MXC_RESET 0x1c
47 /* generic defines to abstract from the different register layouts */
48 #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
49 #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
51 struct spi_imx_config {
52 unsigned int speed_hz;
59 struct spi_bitbang bitbang;
61 struct completion xfer_done;
65 unsigned long spi_clk;
69 void (*tx)(struct spi_imx_data *);
70 void (*rx)(struct spi_imx_data *);
73 unsigned int txfifo; /* number of words pushed in tx FIFO */
75 /* SoC specific functions */
76 void (*intctrl)(struct spi_imx_data *, int);
77 int (*config)(struct spi_imx_data *, struct spi_imx_config *);
78 void (*trigger)(struct spi_imx_data *);
79 int (*rx_available)(struct spi_imx_data *);
82 #define MXC_SPI_BUF_RX(type) \
83 static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
85 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
87 if (spi_imx->rx_buf) { \
88 *(type *)spi_imx->rx_buf = val; \
89 spi_imx->rx_buf += sizeof(type); \
93 #define MXC_SPI_BUF_TX(type) \
94 static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
98 if (spi_imx->tx_buf) { \
99 val = *(type *)spi_imx->tx_buf; \
100 spi_imx->tx_buf += sizeof(type); \
103 spi_imx->count -= sizeof(type); \
105 writel(val, spi_imx->base + MXC_CSPITXDATA); \
115 /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
116 * (which is currently not the case in this driver)
118 static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
119 256, 384, 512, 768, 1024};
122 static unsigned int spi_imx_clkdiv_1(unsigned int fin,
132 for (i = 2; i < max; i++)
133 if (fspi * mxc_clkdivs[i] >= fin)
139 /* MX1, MX31, MX35 */
140 static unsigned int spi_imx_clkdiv_2(unsigned int fin,
145 for (i = 0; i < 7; i++) {
146 if (fspi * div >= fin)
154 #define MX31_INTREG_TEEN (1 << 0)
155 #define MX31_INTREG_RREN (1 << 3)
157 #define MX31_CSPICTRL_ENABLE (1 << 0)
158 #define MX31_CSPICTRL_MASTER (1 << 1)
159 #define MX31_CSPICTRL_XCH (1 << 2)
160 #define MX31_CSPICTRL_POL (1 << 4)
161 #define MX31_CSPICTRL_PHA (1 << 5)
162 #define MX31_CSPICTRL_SSCTL (1 << 6)
163 #define MX31_CSPICTRL_SSPOL (1 << 7)
164 #define MX31_CSPICTRL_BC_SHIFT 8
165 #define MX35_CSPICTRL_BL_SHIFT 20
166 #define MX31_CSPICTRL_CS_SHIFT 24
167 #define MX35_CSPICTRL_CS_SHIFT 12
168 #define MX31_CSPICTRL_DR_SHIFT 16
170 #define MX31_CSPISTATUS 0x14
171 #define MX31_STATUS_RR (1 << 3)
173 /* These functions also work for the i.MX35, but be aware that
174 * the i.MX35 has a slightly different register layout for bits
175 * we do not use here.
177 static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
179 unsigned int val = 0;
181 if (enable & MXC_INT_TE)
182 val |= MX31_INTREG_TEEN;
183 if (enable & MXC_INT_RR)
184 val |= MX31_INTREG_RREN;
186 writel(val, spi_imx->base + MXC_CSPIINT);
189 static void mx31_trigger(struct spi_imx_data *spi_imx)
193 reg = readl(spi_imx->base + MXC_CSPICTRL);
194 reg |= MX31_CSPICTRL_XCH;
195 writel(reg, spi_imx->base + MXC_CSPICTRL);
198 static int mx31_config(struct spi_imx_data *spi_imx,
199 struct spi_imx_config *config)
201 unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
203 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
204 MX31_CSPICTRL_DR_SHIFT;
207 reg |= (config->bpw - 1) << MX31_CSPICTRL_BC_SHIFT;
208 else if (cpu_is_mx35()) {
209 reg |= (config->bpw - 1) << MX35_CSPICTRL_BL_SHIFT;
210 reg |= MX31_CSPICTRL_SSCTL;
213 if (config->mode & SPI_CPHA)
214 reg |= MX31_CSPICTRL_PHA;
215 if (config->mode & SPI_CPOL)
216 reg |= MX31_CSPICTRL_POL;
217 if (config->mode & SPI_CS_HIGH)
218 reg |= MX31_CSPICTRL_SSPOL;
219 if (config->cs < 0) {
221 reg |= (config->cs + 32) << MX31_CSPICTRL_CS_SHIFT;
222 else if (cpu_is_mx35())
223 reg |= (config->cs + 32) << MX35_CSPICTRL_CS_SHIFT;
226 writel(reg, spi_imx->base + MXC_CSPICTRL);
231 static int mx31_rx_available(struct spi_imx_data *spi_imx)
233 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
236 #define MX27_INTREG_RR (1 << 4)
237 #define MX27_INTREG_TEEN (1 << 9)
238 #define MX27_INTREG_RREN (1 << 13)
240 #define MX27_CSPICTRL_POL (1 << 5)
241 #define MX27_CSPICTRL_PHA (1 << 6)
242 #define MX27_CSPICTRL_SSPOL (1 << 8)
243 #define MX27_CSPICTRL_XCH (1 << 9)
244 #define MX27_CSPICTRL_ENABLE (1 << 10)
245 #define MX27_CSPICTRL_MASTER (1 << 11)
246 #define MX27_CSPICTRL_DR_SHIFT 14
247 #define MX27_CSPICTRL_CS_SHIFT 19
249 static void mx27_intctrl(struct spi_imx_data *spi_imx, int enable)
251 unsigned int val = 0;
253 if (enable & MXC_INT_TE)
254 val |= MX27_INTREG_TEEN;
255 if (enable & MXC_INT_RR)
256 val |= MX27_INTREG_RREN;
258 writel(val, spi_imx->base + MXC_CSPIINT);
261 static void mx27_trigger(struct spi_imx_data *spi_imx)
265 reg = readl(spi_imx->base + MXC_CSPICTRL);
266 reg |= MX27_CSPICTRL_XCH;
267 writel(reg, spi_imx->base + MXC_CSPICTRL);
270 static int mx27_config(struct spi_imx_data *spi_imx,
271 struct spi_imx_config *config)
273 unsigned int reg = MX27_CSPICTRL_ENABLE | MX27_CSPICTRL_MASTER;
275 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, config->speed_hz) <<
276 MX27_CSPICTRL_DR_SHIFT;
277 reg |= config->bpw - 1;
279 if (config->mode & SPI_CPHA)
280 reg |= MX27_CSPICTRL_PHA;
281 if (config->mode & SPI_CPOL)
282 reg |= MX27_CSPICTRL_POL;
283 if (config->mode & SPI_CS_HIGH)
284 reg |= MX27_CSPICTRL_SSPOL;
286 reg |= (config->cs + 32) << MX27_CSPICTRL_CS_SHIFT;
288 writel(reg, spi_imx->base + MXC_CSPICTRL);
293 static int mx27_rx_available(struct spi_imx_data *spi_imx)
295 return readl(spi_imx->base + MXC_CSPIINT) & MX27_INTREG_RR;
298 #define MX1_INTREG_RR (1 << 3)
299 #define MX1_INTREG_TEEN (1 << 8)
300 #define MX1_INTREG_RREN (1 << 11)
302 #define MX1_CSPICTRL_POL (1 << 4)
303 #define MX1_CSPICTRL_PHA (1 << 5)
304 #define MX1_CSPICTRL_XCH (1 << 8)
305 #define MX1_CSPICTRL_ENABLE (1 << 9)
306 #define MX1_CSPICTRL_MASTER (1 << 10)
307 #define MX1_CSPICTRL_DR_SHIFT 13
309 static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
311 unsigned int val = 0;
313 if (enable & MXC_INT_TE)
314 val |= MX1_INTREG_TEEN;
315 if (enable & MXC_INT_RR)
316 val |= MX1_INTREG_RREN;
318 writel(val, spi_imx->base + MXC_CSPIINT);
321 static void mx1_trigger(struct spi_imx_data *spi_imx)
325 reg = readl(spi_imx->base + MXC_CSPICTRL);
326 reg |= MX1_CSPICTRL_XCH;
327 writel(reg, spi_imx->base + MXC_CSPICTRL);
330 static int mx1_config(struct spi_imx_data *spi_imx,
331 struct spi_imx_config *config)
333 unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
335 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, config->speed_hz) <<
336 MX1_CSPICTRL_DR_SHIFT;
337 reg |= config->bpw - 1;
339 if (config->mode & SPI_CPHA)
340 reg |= MX1_CSPICTRL_PHA;
341 if (config->mode & SPI_CPOL)
342 reg |= MX1_CSPICTRL_POL;
344 writel(reg, spi_imx->base + MXC_CSPICTRL);
349 static int mx1_rx_available(struct spi_imx_data *spi_imx)
351 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
354 static void spi_imx_chipselect(struct spi_device *spi, int is_active)
356 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
358 int gpio = spi_imx->chipselect[spi->chip_select];
359 struct spi_imx_config config;
361 if (spi->mode & SPI_CS_HIGH)
364 if (is_active == BITBANG_CS_INACTIVE) {
366 gpio_set_value(gpio, !cs);
370 config.bpw = spi->bits_per_word;
371 config.speed_hz = spi->max_speed_hz;
372 config.mode = spi->mode;
373 config.cs = spi_imx->chipselect[spi->chip_select];
375 spi_imx->config(spi_imx, &config);
377 /* Initialize the functions for transfer */
378 if (config.bpw <= 8) {
379 spi_imx->rx = spi_imx_buf_rx_u8;
380 spi_imx->tx = spi_imx_buf_tx_u8;
381 } else if (config.bpw <= 16) {
382 spi_imx->rx = spi_imx_buf_rx_u16;
383 spi_imx->tx = spi_imx_buf_tx_u16;
384 } else if (config.bpw <= 32) {
385 spi_imx->rx = spi_imx_buf_rx_u32;
386 spi_imx->tx = spi_imx_buf_tx_u32;
391 gpio_set_value(gpio, cs);
396 static void spi_imx_push(struct spi_imx_data *spi_imx)
398 while (spi_imx->txfifo < 8) {
401 spi_imx->tx(spi_imx);
405 spi_imx->trigger(spi_imx);
408 static irqreturn_t spi_imx_isr(int irq, void *dev_id)
410 struct spi_imx_data *spi_imx = dev_id;
412 while (spi_imx->rx_available(spi_imx)) {
413 spi_imx->rx(spi_imx);
417 if (spi_imx->count) {
418 spi_imx_push(spi_imx);
422 if (spi_imx->txfifo) {
423 /* No data left to push, but still waiting for rx data,
424 * enable receive data available interrupt.
426 spi_imx->intctrl(spi_imx, MXC_INT_RR);
430 spi_imx->intctrl(spi_imx, 0);
431 complete(&spi_imx->xfer_done);
436 static int spi_imx_setupxfer(struct spi_device *spi,
437 struct spi_transfer *t)
439 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
440 struct spi_imx_config config;
442 config.bpw = t ? t->bits_per_word : spi->bits_per_word;
443 config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
444 config.mode = spi->mode;
446 if (!config.speed_hz)
447 config.speed_hz = spi->max_speed_hz;
449 config.bpw = spi->bits_per_word;
450 if (!config.speed_hz)
451 config.speed_hz = spi->max_speed_hz;
453 spi_imx->config(spi_imx, &config);
458 static int spi_imx_transfer(struct spi_device *spi,
459 struct spi_transfer *transfer)
461 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
463 spi_imx->tx_buf = transfer->tx_buf;
464 spi_imx->rx_buf = transfer->rx_buf;
465 spi_imx->count = transfer->len;
468 init_completion(&spi_imx->xfer_done);
470 spi_imx_push(spi_imx);
472 spi_imx->intctrl(spi_imx, MXC_INT_TE);
474 wait_for_completion(&spi_imx->xfer_done);
476 return transfer->len;
479 static int spi_imx_setup(struct spi_device *spi)
481 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
482 int gpio = spi_imx->chipselect[spi->chip_select];
484 if (!spi->bits_per_word)
485 spi->bits_per_word = 8;
487 pr_debug("%s: mode %d, %u bpw, %d hz\n", __func__,
488 spi->mode, spi->bits_per_word, spi->max_speed_hz);
491 gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);
493 spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
498 static void spi_imx_cleanup(struct spi_device *spi)
502 static int __init spi_imx_probe(struct platform_device *pdev)
504 struct spi_imx_master *mxc_platform_info;
505 struct spi_master *master;
506 struct spi_imx_data *spi_imx;
507 struct resource *res;
510 mxc_platform_info = (struct spi_imx_master *)pdev->dev.platform_data;
511 if (!mxc_platform_info) {
512 dev_err(&pdev->dev, "can't get the platform data\n");
516 master = spi_alloc_master(&pdev->dev, sizeof(struct spi_imx_data));
520 platform_set_drvdata(pdev, master);
522 master->bus_num = pdev->id;
523 master->num_chipselect = mxc_platform_info->num_chipselect;
525 spi_imx = spi_master_get_devdata(master);
526 spi_imx->bitbang.master = spi_master_get(master);
527 spi_imx->chipselect = mxc_platform_info->chipselect;
529 for (i = 0; i < master->num_chipselect; i++) {
530 if (spi_imx->chipselect[i] < 0)
532 ret = gpio_request(spi_imx->chipselect[i], DRIVER_NAME);
536 if (spi_imx->chipselect[i] >= 0)
537 gpio_free(spi_imx->chipselect[i--]);
538 dev_err(&pdev->dev, "can't get cs gpios");
543 spi_imx->bitbang.chipselect = spi_imx_chipselect;
544 spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
545 spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
546 spi_imx->bitbang.master->setup = spi_imx_setup;
547 spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
549 init_completion(&spi_imx->xfer_done);
551 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
553 dev_err(&pdev->dev, "can't get platform resource\n");
558 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
559 dev_err(&pdev->dev, "request_mem_region failed\n");
564 spi_imx->base = ioremap(res->start, resource_size(res));
565 if (!spi_imx->base) {
567 goto out_release_mem;
570 spi_imx->irq = platform_get_irq(pdev, 0);
576 ret = request_irq(spi_imx->irq, spi_imx_isr, 0, DRIVER_NAME, spi_imx);
578 dev_err(&pdev->dev, "can't get irq%d: %d\n", spi_imx->irq, ret);
582 if (cpu_is_mx31() || cpu_is_mx35()) {
583 spi_imx->intctrl = mx31_intctrl;
584 spi_imx->config = mx31_config;
585 spi_imx->trigger = mx31_trigger;
586 spi_imx->rx_available = mx31_rx_available;
587 } else if (cpu_is_mx27() || cpu_is_mx21()) {
588 spi_imx->intctrl = mx27_intctrl;
589 spi_imx->config = mx27_config;
590 spi_imx->trigger = mx27_trigger;
591 spi_imx->rx_available = mx27_rx_available;
592 } else if (cpu_is_mx1()) {
593 spi_imx->intctrl = mx1_intctrl;
594 spi_imx->config = mx1_config;
595 spi_imx->trigger = mx1_trigger;
596 spi_imx->rx_available = mx1_rx_available;
600 spi_imx->clk = clk_get(&pdev->dev, NULL);
601 if (IS_ERR(spi_imx->clk)) {
602 dev_err(&pdev->dev, "unable to get clock\n");
603 ret = PTR_ERR(spi_imx->clk);
607 clk_enable(spi_imx->clk);
608 spi_imx->spi_clk = clk_get_rate(spi_imx->clk);
610 if (!cpu_is_mx31() || !cpu_is_mx35())
611 writel(1, spi_imx->base + MXC_RESET);
613 spi_imx->intctrl(spi_imx, 0);
615 ret = spi_bitbang_start(&spi_imx->bitbang);
617 dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
621 dev_info(&pdev->dev, "probed\n");
626 clk_disable(spi_imx->clk);
627 clk_put(spi_imx->clk);
629 free_irq(spi_imx->irq, spi_imx);
631 iounmap(spi_imx->base);
633 release_mem_region(res->start, resource_size(res));
635 for (i = 0; i < master->num_chipselect; i++)
636 if (spi_imx->chipselect[i] >= 0)
637 gpio_free(spi_imx->chipselect[i]);
639 spi_master_put(master);
641 platform_set_drvdata(pdev, NULL);
645 static int __exit spi_imx_remove(struct platform_device *pdev)
647 struct spi_master *master = platform_get_drvdata(pdev);
648 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
649 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
652 spi_bitbang_stop(&spi_imx->bitbang);
654 writel(0, spi_imx->base + MXC_CSPICTRL);
655 clk_disable(spi_imx->clk);
656 clk_put(spi_imx->clk);
657 free_irq(spi_imx->irq, spi_imx);
658 iounmap(spi_imx->base);
660 for (i = 0; i < master->num_chipselect; i++)
661 if (spi_imx->chipselect[i] >= 0)
662 gpio_free(spi_imx->chipselect[i]);
664 spi_master_put(master);
666 release_mem_region(res->start, resource_size(res));
668 platform_set_drvdata(pdev, NULL);
673 static struct platform_driver spi_imx_driver = {
676 .owner = THIS_MODULE,
678 .probe = spi_imx_probe,
679 .remove = __exit_p(spi_imx_remove),
682 static int __init spi_imx_init(void)
684 return platform_driver_register(&spi_imx_driver);
687 static void __exit spi_imx_exit(void)
689 platform_driver_unregister(&spi_imx_driver);
692 module_init(spi_imx_init);
693 module_exit(spi_imx_exit);
695 MODULE_DESCRIPTION("SPI Master Controller driver");
696 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
697 MODULE_LICENSE("GPL");