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 */
374 list_for_each_entry(t, &m->transfers, transfer_list) {
377 cs_change = t->cs_change;
378 if ((first->bits_per_word != t->bits_per_word) ||
379 (first->speed_hz != t->speed_hz)) {
381 "bits_per_word/speed_hz cannot change while CS is active\n");
388 list_for_each_entry(t, &m->transfers, transfer_list) {
390 status = fsl_spi_setup_transfer(spi, t);
395 fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
398 cs_change = t->cs_change;
400 status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
405 m->actual_length += t->len;
408 udelay(t->delay_usecs);
412 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
418 spi_finalize_current_message(master);
420 if (status || !cs_change) {
422 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
425 fsl_spi_setup_transfer(spi, NULL);
429 static int fsl_spi_setup(struct spi_device *spi)
431 struct mpc8xxx_spi *mpc8xxx_spi;
432 struct fsl_spi_reg *reg_base;
435 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
437 if (!spi->max_speed_hz)
441 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
444 spi_set_ctldata(spi, cs);
446 mpc8xxx_spi = spi_master_get_devdata(spi->master);
448 reg_base = mpc8xxx_spi->reg_base;
450 hw_mode = cs->hw_mode; /* Save original settings */
451 cs->hw_mode = mpc8xxx_spi_read_reg(®_base->mode);
452 /* mask out bits we are going to set */
453 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
454 | SPMODE_REV | SPMODE_LOOP);
456 if (spi->mode & SPI_CPHA)
457 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
458 if (spi->mode & SPI_CPOL)
459 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
460 if (!(spi->mode & SPI_LSB_FIRST))
461 cs->hw_mode |= SPMODE_REV;
462 if (spi->mode & SPI_LOOP)
463 cs->hw_mode |= SPMODE_LOOP;
465 retval = fsl_spi_setup_transfer(spi, NULL);
467 cs->hw_mode = hw_mode; /* Restore settings */
471 if (mpc8xxx_spi->type == TYPE_GRLIB) {
472 if (gpio_is_valid(spi->cs_gpio)) {
475 retval = gpio_request(spi->cs_gpio,
476 dev_name(&spi->dev));
480 desel = !(spi->mode & SPI_CS_HIGH);
481 retval = gpio_direction_output(spi->cs_gpio, desel);
483 gpio_free(spi->cs_gpio);
486 } else if (spi->cs_gpio != -ENOENT) {
487 if (spi->cs_gpio < 0)
491 /* When spi->cs_gpio == -ENOENT, a hole in the phandle list
492 * indicates to use native chipselect if present, or allow for
493 * an always selected chip
497 /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
498 fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
503 static void fsl_spi_cleanup(struct spi_device *spi)
505 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
506 struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
508 if (mpc8xxx_spi->type == TYPE_GRLIB && gpio_is_valid(spi->cs_gpio))
509 gpio_free(spi->cs_gpio);
512 spi_set_ctldata(spi, NULL);
515 static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
517 struct fsl_spi_reg *reg_base = mspi->reg_base;
519 /* We need handle RX first */
520 if (events & SPIE_NE) {
521 u32 rx_data = mpc8xxx_spi_read_reg(®_base->receive);
524 mspi->get_rx(rx_data, mspi);
527 if ((events & SPIE_NF) == 0)
528 /* spin until TX is done */
530 mpc8xxx_spi_read_reg(®_base->event)) &
534 /* Clear the events */
535 mpc8xxx_spi_write_reg(®_base->event, events);
539 u32 word = mspi->get_tx(mspi);
541 mpc8xxx_spi_write_reg(®_base->transmit, word);
543 complete(&mspi->done);
547 static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
549 struct mpc8xxx_spi *mspi = context_data;
550 irqreturn_t ret = IRQ_NONE;
552 struct fsl_spi_reg *reg_base = mspi->reg_base;
554 /* Get interrupt events(tx/rx) */
555 events = mpc8xxx_spi_read_reg(®_base->event);
559 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
561 if (mspi->flags & SPI_CPM_MODE)
562 fsl_spi_cpm_irq(mspi, events);
564 fsl_spi_cpu_irq(mspi, events);
569 static void fsl_spi_grlib_cs_control(struct spi_device *spi, bool on)
571 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
572 struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
574 u16 cs = spi->chip_select;
576 if (gpio_is_valid(spi->cs_gpio)) {
577 gpio_set_value(spi->cs_gpio, on);
578 } else if (cs < mpc8xxx_spi->native_chipselects) {
579 slvsel = mpc8xxx_spi_read_reg(®_base->slvsel);
580 slvsel = on ? (slvsel | (1 << cs)) : (slvsel & ~(1 << cs));
581 mpc8xxx_spi_write_reg(®_base->slvsel, slvsel);
585 static void fsl_spi_grlib_probe(struct device *dev)
587 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
588 struct spi_master *master = dev_get_drvdata(dev);
589 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
590 struct fsl_spi_reg *reg_base = mpc8xxx_spi->reg_base;
594 capabilities = mpc8xxx_spi_read_reg(®_base->cap);
596 mpc8xxx_spi->set_shifts = fsl_spi_grlib_set_shifts;
597 mbits = SPCAP_MAXWLEN(capabilities);
599 mpc8xxx_spi->max_bits_per_word = mbits + 1;
601 mpc8xxx_spi->native_chipselects = 0;
602 if (SPCAP_SSEN(capabilities)) {
603 mpc8xxx_spi->native_chipselects = SPCAP_SSSZ(capabilities);
604 mpc8xxx_spi_write_reg(®_base->slvsel, 0xffffffff);
606 master->num_chipselect = mpc8xxx_spi->native_chipselects;
607 pdata->cs_control = fsl_spi_grlib_cs_control;
610 static struct spi_master * fsl_spi_probe(struct device *dev,
611 struct resource *mem, unsigned int irq)
613 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
614 struct spi_master *master;
615 struct mpc8xxx_spi *mpc8xxx_spi;
616 struct fsl_spi_reg *reg_base;
620 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
621 if (master == NULL) {
626 dev_set_drvdata(dev, master);
628 mpc8xxx_spi_probe(dev, mem, irq);
630 master->setup = fsl_spi_setup;
631 master->cleanup = fsl_spi_cleanup;
632 master->transfer_one_message = fsl_spi_do_one_msg;
634 mpc8xxx_spi = spi_master_get_devdata(master);
635 mpc8xxx_spi->max_bits_per_word = 32;
636 mpc8xxx_spi->type = fsl_spi_get_type(dev);
638 ret = fsl_spi_cpm_init(mpc8xxx_spi);
642 mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem);
643 if (IS_ERR(mpc8xxx_spi->reg_base)) {
644 ret = PTR_ERR(mpc8xxx_spi->reg_base);
648 if (mpc8xxx_spi->type == TYPE_GRLIB)
649 fsl_spi_grlib_probe(dev);
651 master->bits_per_word_mask =
652 (SPI_BPW_RANGE_MASK(4, 16) | SPI_BPW_MASK(32)) &
653 SPI_BPW_RANGE_MASK(1, mpc8xxx_spi->max_bits_per_word);
655 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
656 mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
658 if (mpc8xxx_spi->set_shifts)
659 /* 8 bits per word and MSB first */
660 mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
661 &mpc8xxx_spi->tx_shift, 8, 1);
663 /* Register for SPI Interrupt */
664 ret = devm_request_irq(dev, mpc8xxx_spi->irq, fsl_spi_irq,
665 0, "fsl_spi", mpc8xxx_spi);
670 reg_base = mpc8xxx_spi->reg_base;
672 /* SPI controller initializations */
673 mpc8xxx_spi_write_reg(®_base->mode, 0);
674 mpc8xxx_spi_write_reg(®_base->mask, 0);
675 mpc8xxx_spi_write_reg(®_base->command, 0);
676 mpc8xxx_spi_write_reg(®_base->event, 0xffffffff);
678 /* Enable SPI interface */
679 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
680 if (mpc8xxx_spi->max_bits_per_word < 8) {
681 regval &= ~SPMODE_LEN(0xF);
682 regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1);
684 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
687 mpc8xxx_spi_write_reg(®_base->mode, regval);
689 ret = devm_spi_register_master(dev, master);
693 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
694 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
699 fsl_spi_cpm_free(mpc8xxx_spi);
701 spi_master_put(master);
706 static void fsl_spi_cs_control(struct spi_device *spi, bool on)
708 struct device *dev = spi->dev.parent->parent;
709 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
710 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
711 u16 cs = spi->chip_select;
713 if (cs < pinfo->ngpios) {
714 int gpio = pinfo->gpios[cs];
715 bool alow = pinfo->alow_flags[cs];
717 gpio_set_value(gpio, on ^ alow);
719 if (WARN_ON_ONCE(cs > pinfo->ngpios || !pinfo->immr_spi_cs))
721 iowrite32be(on ? SPI_BOOT_SEL_BIT : 0, pinfo->immr_spi_cs);
725 static int of_fsl_spi_get_chipselects(struct device *dev)
727 struct device_node *np = dev->of_node;
728 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
729 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
730 bool spisel_boot = IS_ENABLED(CONFIG_FSL_SOC) &&
731 of_property_read_bool(np, "fsl,spisel_boot");
736 ngpios = of_gpio_count(np);
737 ngpios = max(ngpios, 0);
738 if (ngpios == 0 && !spisel_boot) {
740 * SPI w/o chip-select line. One SPI device is still permitted
743 pdata->max_chipselect = 1;
747 pinfo->ngpios = ngpios;
748 pinfo->gpios = kmalloc_array(ngpios, sizeof(*pinfo->gpios),
752 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
754 pinfo->alow_flags = kcalloc(ngpios, sizeof(*pinfo->alow_flags),
756 if (!pinfo->alow_flags) {
758 goto err_alloc_flags;
761 for (; i < ngpios; i++) {
763 enum of_gpio_flags flags;
765 gpio = of_get_gpio_flags(np, i, &flags);
766 if (!gpio_is_valid(gpio)) {
767 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
772 ret = gpio_request(gpio, dev_name(dev));
774 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
778 pinfo->gpios[i] = gpio;
779 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
781 ret = gpio_direction_output(pinfo->gpios[i],
782 pinfo->alow_flags[i]);
785 "can't set output direction for gpio #%d: %d\n",
791 #if IS_ENABLED(CONFIG_FSL_SOC)
793 pinfo->immr_spi_cs = ioremap(get_immrbase() + IMMR_SPI_CS_OFFSET, 4);
794 if (!pinfo->immr_spi_cs) {
802 pdata->max_chipselect = ngpios + spisel_boot;
803 pdata->cs_control = fsl_spi_cs_control;
809 if (gpio_is_valid(pinfo->gpios[i]))
810 gpio_free(pinfo->gpios[i]);
814 kfree(pinfo->alow_flags);
815 pinfo->alow_flags = NULL;
822 static int of_fsl_spi_free_chipselects(struct device *dev)
824 struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
825 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
831 for (i = 0; i < pdata->max_chipselect; i++) {
832 if (gpio_is_valid(pinfo->gpios[i]))
833 gpio_free(pinfo->gpios[i]);
837 kfree(pinfo->alow_flags);
841 static int of_fsl_spi_probe(struct platform_device *ofdev)
843 struct device *dev = &ofdev->dev;
844 struct device_node *np = ofdev->dev.of_node;
845 struct spi_master *master;
850 ret = of_mpc8xxx_spi_probe(ofdev);
854 type = fsl_spi_get_type(&ofdev->dev);
855 if (type == TYPE_FSL) {
856 ret = of_fsl_spi_get_chipselects(dev);
861 ret = of_address_to_resource(np, 0, &mem);
865 irq = irq_of_parse_and_map(np, 0);
871 master = fsl_spi_probe(dev, &mem, irq);
872 if (IS_ERR(master)) {
873 ret = PTR_ERR(master);
880 irq_dispose_mapping(irq);
881 if (type == TYPE_FSL)
882 of_fsl_spi_free_chipselects(dev);
886 static int of_fsl_spi_remove(struct platform_device *ofdev)
888 struct spi_master *master = platform_get_drvdata(ofdev);
889 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
891 fsl_spi_cpm_free(mpc8xxx_spi);
892 if (mpc8xxx_spi->type == TYPE_FSL)
893 of_fsl_spi_free_chipselects(&ofdev->dev);
897 static struct platform_driver of_fsl_spi_driver = {
900 .of_match_table = of_fsl_spi_match,
902 .probe = of_fsl_spi_probe,
903 .remove = of_fsl_spi_remove,
906 #ifdef CONFIG_MPC832x_RDB
909 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
910 * only. The driver should go away soon, since newer MPC8323E-RDB's device
911 * tree can work with OpenFirmware driver. But for now we support old trees
914 static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
916 struct resource *mem;
918 struct spi_master *master;
920 if (!dev_get_platdata(&pdev->dev))
923 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
927 irq = platform_get_irq(pdev, 0);
931 master = fsl_spi_probe(&pdev->dev, mem, irq);
932 return PTR_ERR_OR_ZERO(master);
935 static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
937 struct spi_master *master = platform_get_drvdata(pdev);
938 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(master);
940 fsl_spi_cpm_free(mpc8xxx_spi);
945 MODULE_ALIAS("platform:mpc8xxx_spi");
946 static struct platform_driver mpc8xxx_spi_driver = {
947 .probe = plat_mpc8xxx_spi_probe,
948 .remove = plat_mpc8xxx_spi_remove,
950 .name = "mpc8xxx_spi",
954 static bool legacy_driver_failed;
956 static void __init legacy_driver_register(void)
958 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
961 static void __exit legacy_driver_unregister(void)
963 if (legacy_driver_failed)
965 platform_driver_unregister(&mpc8xxx_spi_driver);
968 static void __init legacy_driver_register(void) {}
969 static void __exit legacy_driver_unregister(void) {}
970 #endif /* CONFIG_MPC832x_RDB */
972 static int __init fsl_spi_init(void)
974 legacy_driver_register();
975 return platform_driver_register(&of_fsl_spi_driver);
977 module_init(fsl_spi_init);
979 static void __exit fsl_spi_exit(void)
981 platform_driver_unregister(&of_fsl_spi_driver);
982 legacy_driver_unregister();
984 module_exit(fsl_spi_exit);
986 MODULE_AUTHOR("Kumar Gala");
987 MODULE_DESCRIPTION("Simple Freescale SPI Driver");
988 MODULE_LICENSE("GPL");