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>
14 * Copyright (c) 2012 Aeroflex Gaisler AB.
15 * Author: Andreas Larsson <andreas@gaisler.com>
17 * This program is free software; you can redistribute it and/or modify it
18 * under the terms of the GNU General Public License as published by the
19 * Free Software Foundation; either version 2 of the License, or (at your
20 * option) any later version.
22 #include <linux/delay.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/fsl_devices.h>
25 #include <linux/gpio.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/mutex.h>
33 #include <linux/of_address.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_gpio.h>
36 #include <linux/of_platform.h>
37 #include <linux/platform_device.h>
38 #include <linux/spi/spi.h>
39 #include <linux/spi/spi_bitbang.h>
40 #include <linux/types.h>
43 #include <sysdev/fsl_soc.h>
46 /* Specific to the MPC8306/MPC8309 */
47 #define IMMR_SPI_CS_OFFSET 0x14c
48 #define SPI_BOOT_SEL_BIT 0x80000000
50 #include "spi-fsl-lib.h"
51 #include "spi-fsl-cpm.h"
52 #include "spi-fsl-spi.h"
57 struct fsl_spi_match_data {
61 static struct fsl_spi_match_data of_fsl_spi_fsl_config = {
65 static struct fsl_spi_match_data of_fsl_spi_grlib_config = {
69 static const struct of_device_id of_fsl_spi_match[] = {
71 .compatible = "fsl,spi",
72 .data = &of_fsl_spi_fsl_config,
75 .compatible = "aeroflexgaisler,spictrl",
76 .data = &of_fsl_spi_grlib_config,
80 MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
82 static int fsl_spi_get_type(struct device *dev)
84 const struct of_device_id *match;
87 match = of_match_node(of_fsl_spi_match, dev->of_node);
88 if (match && match->data)
89 return ((struct fsl_spi_match_data *)match->data)->type;
94 static void fsl_spi_change_mode(struct spi_device *spi)
96 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
97 struct spi_mpc8xxx_cs *cs = spi->controller_state;
98 struct fsl_spi_reg *reg_base = mspi->reg_base;
99 __be32 __iomem *mode = ®_base->mode;
102 if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
105 /* Turn off IRQs locally to minimize time that SPI is disabled. */
106 local_irq_save(flags);
108 /* Turn off SPI unit prior changing mode */
109 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
111 /* When in CPM mode, we need to reinit tx and rx. */
112 if (mspi->flags & SPI_CPM_MODE) {
113 fsl_spi_cpm_reinit_txrx(mspi);
115 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
116 local_irq_restore(flags);
119 static void fsl_spi_chipselect(struct spi_device *spi, int value)
121 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
122 struct fsl_spi_platform_data *pdata;
123 bool pol = spi->mode & SPI_CS_HIGH;
124 struct spi_mpc8xxx_cs *cs = spi->controller_state;
126 pdata = spi->dev.parent->parent->platform_data;
128 if (value == BITBANG_CS_INACTIVE) {
129 if (pdata->cs_control)
130 pdata->cs_control(spi, !pol);
133 if (value == BITBANG_CS_ACTIVE) {
134 mpc8xxx_spi->rx_shift = cs->rx_shift;
135 mpc8xxx_spi->tx_shift = cs->tx_shift;
136 mpc8xxx_spi->get_rx = cs->get_rx;
137 mpc8xxx_spi->get_tx = cs->get_tx;
139 fsl_spi_change_mode(spi);
141 if (pdata->cs_control)
142 pdata->cs_control(spi, pol);
146 static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift,
147 int bits_per_word, int msb_first)
152 if (bits_per_word <= 8) {
155 } else if (bits_per_word <= 16) {
160 if (bits_per_word <= 8)
165 static void fsl_spi_grlib_set_shifts(u32 *rx_shift, u32 *tx_shift,
166 int bits_per_word, int msb_first)
170 if (bits_per_word <= 16) {
172 *rx_shift = 16; /* LSB in bit 16 */
173 *tx_shift = 32 - bits_per_word; /* MSB in bit 31 */
175 *rx_shift = 16 - bits_per_word; /* MSB in bit 15 */
180 static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
181 struct spi_device *spi,
182 struct mpc8xxx_spi *mpc8xxx_spi,
187 if (bits_per_word <= 8) {
188 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
189 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
190 } else if (bits_per_word <= 16) {
191 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
192 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
193 } else if (bits_per_word <= 32) {
194 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
195 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
199 if (mpc8xxx_spi->set_shifts)
200 mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift,
202 !(spi->mode & SPI_LSB_FIRST));
204 mpc8xxx_spi->rx_shift = cs->rx_shift;
205 mpc8xxx_spi->tx_shift = cs->tx_shift;
206 mpc8xxx_spi->get_rx = cs->get_rx;
207 mpc8xxx_spi->get_tx = cs->get_tx;
209 return bits_per_word;
212 static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
213 struct spi_device *spi,
216 /* QE uses Little Endian for words > 8
217 * so transform all words > 8 into 8 bits
218 * Unfortnatly that doesn't work for LSB so
219 * reject these for now */
220 /* Note: 32 bits word, LSB works iff
221 * tfcr/rfcr is set to CPMFCR_GBL */
222 if (spi->mode & SPI_LSB_FIRST &&
225 if (bits_per_word > 8)
226 return 8; /* pretend its 8 bits */
227 return bits_per_word;
230 static int fsl_spi_setup_transfer(struct spi_device *spi,
231 struct spi_transfer *t)
233 struct mpc8xxx_spi *mpc8xxx_spi;
234 int bits_per_word = 0;
237 struct spi_mpc8xxx_cs *cs = spi->controller_state;
239 mpc8xxx_spi = spi_master_get_devdata(spi->master);
242 bits_per_word = t->bits_per_word;
246 /* spi_transfer level calls that work per-word */
248 bits_per_word = spi->bits_per_word;
251 hz = spi->max_speed_hz;
253 if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
254 bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
257 else if (mpc8xxx_spi->flags & SPI_QE)
258 bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
261 if (bits_per_word < 0)
262 return bits_per_word;
264 if (bits_per_word == 32)
267 bits_per_word = bits_per_word - 1;
269 /* mask out bits we are going to set */
270 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
273 cs->hw_mode |= SPMODE_LEN(bits_per_word);
275 if ((mpc8xxx_spi->spibrg / hz) > 64) {
276 cs->hw_mode |= SPMODE_DIV16;
277 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
279 "%s: Requested speed is too low: %d Hz. Will use %d Hz instead.\n",
280 dev_name(&spi->dev), hz, mpc8xxx_spi->spibrg / 1024);
284 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
289 cs->hw_mode |= SPMODE_PM(pm);
291 fsl_spi_change_mode(spi);
295 static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
296 struct spi_transfer *t, unsigned int len)
299 struct fsl_spi_reg *reg_base = mspi->reg_base;
304 mpc8xxx_spi_write_reg(®_base->mask, SPIM_NE);
307 word = mspi->get_tx(mspi);
308 mpc8xxx_spi_write_reg(®_base->transmit, word);
313 static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
316 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
317 struct fsl_spi_reg *reg_base;
318 unsigned int len = t->len;
322 reg_base = mpc8xxx_spi->reg_base;
323 bits_per_word = spi->bits_per_word;
324 if (t->bits_per_word)
325 bits_per_word = t->bits_per_word;
327 if (bits_per_word > 8) {
328 /* invalid length? */
333 if (bits_per_word > 16) {
334 /* invalid length? */
340 mpc8xxx_spi->tx = t->tx_buf;
341 mpc8xxx_spi->rx = t->rx_buf;
343 reinit_completion(&mpc8xxx_spi->done);
345 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
346 ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
348 ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
352 wait_for_completion(&mpc8xxx_spi->done);
354 /* disable rx ints */
355 mpc8xxx_spi_write_reg(®_base->mask, 0);
357 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
358 fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
360 return mpc8xxx_spi->count;
363 static int fsl_spi_do_one_msg(struct spi_master *master,
364 struct spi_message *m)
366 struct spi_device *spi = m->spi;
367 struct spi_transfer *t, *first;
368 unsigned int cs_change;
369 const int nsecs = 50;
372 /* Don't allow changes if CS is active */
373 first = list_first_entry(&m->transfers, struct spi_transfer,
375 list_for_each_entry(t, &m->transfers, transfer_list) {
376 if ((first->bits_per_word != t->bits_per_word) ||
377 (first->speed_hz != t->speed_hz)) {
379 "bits_per_word/speed_hz should be same for the same SPI transfer\n");
386 list_for_each_entry(t, &m->transfers, transfer_list) {
388 status = fsl_spi_setup_transfer(spi, t);
393 fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
396 cs_change = t->cs_change;
398 status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
403 m->actual_length += t->len;
406 udelay(t->delay_usecs);
410 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
416 spi_finalize_current_message(master);
418 if (status || !cs_change) {
420 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
423 fsl_spi_setup_transfer(spi, NULL);
427 static int fsl_spi_setup(struct spi_device *spi)
429 struct mpc8xxx_spi *mpc8xxx_spi;
430 struct fsl_spi_reg *reg_base;
433 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
435 if (!spi->max_speed_hz)
439 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
442 spi_set_ctldata(spi, cs);
444 mpc8xxx_spi = spi_master_get_devdata(spi->master);
446 reg_base = mpc8xxx_spi->reg_base;
448 hw_mode = cs->hw_mode; /* Save original settings */
449 cs->hw_mode = mpc8xxx_spi_read_reg(®_base->mode);
450 /* mask out bits we are going to set */
451 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
452 | SPMODE_REV | SPMODE_LOOP);
454 if (spi->mode & SPI_CPHA)
455 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
456 if (spi->mode & SPI_CPOL)
457 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
458 if (!(spi->mode & SPI_LSB_FIRST))
459 cs->hw_mode |= SPMODE_REV;
460 if (spi->mode & SPI_LOOP)
461 cs->hw_mode |= SPMODE_LOOP;
463 retval = fsl_spi_setup_transfer(spi, NULL);
465 cs->hw_mode = hw_mode; /* Restore settings */
469 if (mpc8xxx_spi->type == TYPE_GRLIB) {
470 if (gpio_is_valid(spi->cs_gpio)) {
473 retval = gpio_request(spi->cs_gpio,
474 dev_name(&spi->dev));
478 desel = !(spi->mode & SPI_CS_HIGH);
479 retval = gpio_direction_output(spi->cs_gpio, desel);
481 gpio_free(spi->cs_gpio);
484 } else if (spi->cs_gpio != -ENOENT) {
485 if (spi->cs_gpio < 0)
489 /* When spi->cs_gpio == -ENOENT, a hole in the phandle list
490 * indicates to use native chipselect if present, or allow for
491 * an always selected chip
495 /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
496 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
501 static void fsl_spi_cleanup(struct spi_device *spi)
503 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
504 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
506 if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio))
507 gpio_free(spi->cs_gpio);
510 spi_set_ctldata(spi, NULL);
513 static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
515 struct fsl_spi_reg *reg_base = mspi->reg_base;
517 /* We need handle RX first */
518 if (events & SPIE_NE) {
519 u32 rx_data = mpc8xxx_spi_read_reg(®_base->receive);
522 mspi->get_rx(rx_data, mspi);
525 if ((events & SPIE_NF) == 0)
526 /* spin until TX is done */
528 mpc8xxx_spi_read_reg(®_base->event)) &
532 /* Clear the events */
533 mpc8xxx_spi_write_reg(®_base->event, events);
537 u32 word = mspi->get_tx(mspi);
539 mpc8xxx_spi_write_reg(®_base->transmit, word);
541 complete(&mspi->done);
545 static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
547 struct mpc8xxx_spi *mspi = context_data;
548 irqreturn_t ret = IRQ_NONE;
550 struct fsl_spi_reg *reg_base = mspi->reg_base;
552 /* Get interrupt events(tx/rx) */
553 events = mpc8xxx_spi_read_reg(®_base->event);
557 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
559 if (mspi->flags & SPI_CPM_MODE)
560 fsl_spi_cpm_irq(mspi, events);
562 fsl_spi_cpu_irq(mspi, events);
567 static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on)
569 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
570 struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
572 u16 cs = spi->chip_select;
574 if (gpio_is_valid(spi->cs_gpio)) {
575 gpio_set_value(spi->cs_gpio, on);
576 } else if (cs < mpc8xxx_spi->native_chipselects) {
577 slvsel = mpc8xxx_spi_read_reg(®_base->slvsel);
578 slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs));
579 mpc8xxx_spi_write_reg(®_base->slvsel, slvsel);
583 static void fsl_spi_grlib_probe(struct device *dev)
585 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
586 struct spi_master *master = dev_get_drvdata(dev);
587 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
588 struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
592 capabilities = mpc8xxx_spi_read_reg(®_base->cap);
594 mpc8xxx_spi->set_shifts = fsl_spi_grlib_set_shifts;
595 mbits = SPCAP_MAXWLEN(capabilities);
597 mpc8xxx_spi->max_bits_per_word = mbits + 1;
599 mpc8xxx_spi->native_chipselects = 0;
600 if (SPCAP_SSEN(capabilities)) {
601 mpc8xxx_spi->native_chipselects = SPCAP_SSSZ(capabilities);
602 mpc8xxx_spi_write_reg(®_base->slvsel, 0xffffffff);
604 master->num_chipselect = mpc8xxx_spi->native_chipselects;
605 pdata->cs_control = fsl_spi_grlib_cs_control;
608 static struct spi_master * fsl_spi_probe(struct device *dev,
609 struct resource *mem, unsigned int irq)
611 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
612 struct spi_master *master;
613 struct mpc8xxx_spi *mpc8xxx_spi;
614 struct fsl_spi_reg *reg_base;
618 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
619 if (master == NULL) {
624 dev_set_drvdata(dev, master);
626 mpc8xxx_spi_probe(dev, mem, irq);
628 master->setup = fsl_spi_setup;
629 master->cleanup = fsl_spi_cleanup;
630 master->transfer_one_message = fsl_spi_do_one_msg;
632 mpc8xxx_spi = spi_master_get_devdata(master);
633 mpc8xxx_spi->max_bits_per_word = 32;
634 mpc8xxx_spi->type = fsl_spi_get_type(dev);
636 ret = fsl_spi_cpm_init(mpc8xxx_spi);
640 mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
641 if (IS_ERR(mpc8xxx_spi->reg_base)) {
642 ret = PTR_ERR(mpc8xxx_spi->reg_base);
646 if (mpc8xxx_spi->type == TYPE_GRLIB)
647 fsl_spi_grlib_probe(dev);
649 master->bits_per_word_mask =
650 (SPI_BPW_RANGE_MASK(4, 16) | SPI_BPW_MASK(32)) &
651 SPI_BPW_RANGE_MASK(1, mpc8xxx_spi->max_bits_per_word);
653 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
654 mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
656 if (mpc8xxx_spi->set_shifts)
657 /* 8 bits per word and MSB first */
658 mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
659 &mpc8xxx_spi->tx_shift, 8, 1);
661 /* Register for SPI Interrupt */
662 ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_spi_irq,
663 0, "fsl_spi", mpc8xxx_spi);
668 reg_base = mpc8xxx_spi->reg_base;
670 /* SPI controller initializations */
671 mpc8xxx_spi_write_reg(®_base->mode, 0);
672 mpc8xxx_spi_write_reg(®_base->mask, 0);
673 mpc8xxx_spi_write_reg(®_base->command, 0);
674 mpc8xxx_spi_write_reg(®_base->event, 0xffffffff);
676 /* Enable SPI interface */
677 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
678 if (mpc8xxx_spi->max_bits_per_word < 8) {
679 regval &= ~SPMODE_LEN(0xF);
680 regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1);
682 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
685 mpc8xxx_spi_write_reg(®_base->mode, regval);
687 ret = devm_spi_register_master(dev, master);
691 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
692 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
697 fsl_spi_cpm_free(mpc8xxx_spi);
699 spi_master_put(master);
704 static void fsl_spi_cs_control(struct spi_device *spi, bool on)
706 struct device *dev = spi->dev.parent->parent;
707 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
708 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
709 u16 cs = spi->chip_select;
711 if (cs < pinfo->ngpios) {
712 int gpio = pinfo->gpios[cs];
713 bool alow = pinfo->alow_flags[cs];
715 gpio_set_value(gpio, on ^ alow);
717 if (WARN_ON_ONCE(cs > pinfo->ngpios || !pinfo->immr_spi_cs))
719 iowrite32be(on ? SPI_BOOT_SEL_BIT : 0, pinfo->immr_spi_cs);
723 static int of_fsl_spi_get_chipselects(struct device *dev)
725 struct device_node *np = dev->of_node;
726 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
727 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
728 bool spisel_boot = IS_ENABLED(CONFIG_FSL_SOC) &&
729 of_property_read_bool(np, "fsl,spisel_boot");
734 ngpios = of_gpio_count(np);
735 ngpios = max(ngpios, 0);
736 if (ngpios == 0 && !spisel_boot) {
738 * SPI w/o chip-select line. One SPI device is still permitted
741 pdata->max_chipselect = 1;
745 pinfo->ngpios = ngpios;
746 pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios),
750 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
752 pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags),
754 if (!pinfo->alow_flags) {
756 goto err_alloc_flags;
759 for (; i < ngpios; i++) {
761 enum of_gpio_flags flags;
763 gpio = of_get_gpio_flags(np, i, &flags);
764 if (!gpio_is_valid(gpio)) {
765 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
770 ret = gpio_request(gpio, dev_name(dev));
772 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
776 pinfo->gpios[i] = gpio;
777 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
779 ret = gpio_direction_output(pinfo->gpios[i],
780 pinfo->alow_flags[i]);
783 "can't set output direction for gpio #%d: %d\n",
789 #if IS_ENABLED(CONFIG_FSL_SOC)
791 pinfo->immr_spi_cs = ioremap(get_immrbase() + IMMR_SPI_CS_OFFSET, 4);
792 if (!pinfo->immr_spi_cs) {
800 pdata->max_chipselect = ngpios + spisel_boot;
801 pdata->cs_control = fsl_spi_cs_control;
807 if (gpio_is_valid(pinfo->gpios[i]))
808 gpio_free(pinfo->gpios[i]);
812 kfree(pinfo->alow_flags);
813 pinfo->alow_flags = NULL;
820 static int of_fsl_spi_free_chipselects(struct device *dev)
822 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
823 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
829 for (i = 0; i < pdata->max_chipselect; i++) {
830 if (gpio_is_valid(pinfo->gpios[i]))
831 gpio_free(pinfo->gpios[i]);
835 kfree(pinfo->alow_flags);
839 static int of_fsl_spi_probe(struct platform_device *ofdev)
841 struct device *dev = &ofdev->dev;
842 struct device_node *np = ofdev->dev.of_node;
843 struct spi_master *master;
848 ret = of_mpc8xxx_spi_probe(ofdev);
852 type = fsl_spi_get_type(&ofdev->dev);
853 if (type == TYPE_FSL) {
854 ret = of_fsl_spi_get_chipselects(dev);
859 ret = of_address_to_resource(np, 0, &mem);
863 irq = irq_of_parse_and_map(np, 0);
869 master = fsl_spi_probe(dev, &mem, irq);
870 if (IS_ERR(master)) {
871 ret = PTR_ERR(master);
878 irq_dispose_mapping(irq);
879 if (type == TYPE_FSL)
880 of_fsl_spi_free_chipselects(dev);
884 static int of_fsl_spi_remove(struct platform_device *ofdev)
886 struct spi_master *master = platform_get_drvdata(ofdev);
887 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
889 fsl_spi_cpm_free(mpc8xxx_spi);
890 if (mpc8xxx_spi->type == TYPE_FSL)
891 of_fsl_spi_free_chipselects(&ofdev->dev);
895 static struct platform_driver of_fsl_spi_driver = {
898 .of_match_table = of_fsl_spi_match,
900 .probe = of_fsl_spi_probe,
901 .remove = of_fsl_spi_remove,
904 #ifdef CONFIG_MPC832x_RDB
907 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
908 * only. The driver should go away soon, since newer MPC8323E-RDB's device
909 * tree can work with OpenFirmware driver. But for now we support old trees
912 static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
914 struct resource *mem;
916 struct spi_master *master;
918 if (!dev_get_platdata(&pdev->dev))
921 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
925 irq = platform_get_irq(pdev, 0);
929 master = fsl_spi_probe(&pdev->dev, mem, irq);
930 return PTR_ERR_OR_ZERO(master);
933 static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
935 struct spi_master *master = platform_get_drvdata(pdev);
936 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
938 fsl_spi_cpm_free(mpc8xxx_spi);
943 MODULE_ALIAS("platform:mpc8xxx_spi");
944 static struct platform_driver mpc8xxx_spi_driver = {
945 .probe = plat_mpc8xxx_spi_probe,
946 .remove = plat_mpc8xxx_spi_remove,
948 .name = "mpc8xxx_spi",
952 static bool legacy_driver_failed;
954 static void __init legacy_driver_register(void)
956 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
959 static void __exit legacy_driver_unregister(void)
961 if (legacy_driver_failed)
963 platform_driver_unregister(&mpc8xxx_spi_driver);
966 static void __init legacy_driver_register(void) {}
967 static void __exit legacy_driver_unregister(void) {}
968 #endif /* CONFIG_MPC832x_RDB */
970 static int __init fsl_spi_init(void)
972 legacy_driver_register();
973 return platform_driver_register(&of_fsl_spi_driver);
975 module_init(fsl_spi_init);
977 static void __exit fsl_spi_exit(void)
979 platform_driver_unregister(&of_fsl_spi_driver);
980 legacy_driver_unregister();
982 module_exit(fsl_spi_exit);
984 MODULE_AUTHOR("Kumar Gala");
985 MODULE_DESCRIPTION("Simple Freescale SPI Driver");
986 MODULE_LICENSE("GPL");