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/bug.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
21 #include <linux/completion.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/irq.h>
25 #include <linux/device.h>
26 #include <linux/spi/spi.h>
27 #include <linux/spi/spi_bitbang.h>
28 #include <linux/platform_device.h>
29 #include <linux/fsl_devices.h>
31 #include <linux/of_platform.h>
32 #include <linux/gpio.h>
33 #include <linux/of_gpio.h>
34 #include <linux/of_spi.h>
36 #include <sysdev/fsl_soc.h>
39 /* SPI Controller registers */
40 struct mpc83xx_spi_reg
{
50 /* SPI Controller mode register definitions */
51 #define SPMODE_LOOP (1 << 30)
52 #define SPMODE_CI_INACTIVEHIGH (1 << 29)
53 #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
54 #define SPMODE_DIV16 (1 << 27)
55 #define SPMODE_REV (1 << 26)
56 #define SPMODE_MS (1 << 25)
57 #define SPMODE_ENABLE (1 << 24)
58 #define SPMODE_LEN(x) ((x) << 20)
59 #define SPMODE_PM(x) ((x) << 16)
60 #define SPMODE_OP (1 << 14)
61 #define SPMODE_CG(x) ((x) << 7)
64 * Default for SPI Mode:
65 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
67 #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
68 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
70 /* SPIE register values */
71 #define SPIE_NE 0x00000200 /* Not empty */
72 #define SPIE_NF 0x00000100 /* Not full */
74 /* SPIM register values */
75 #define SPIM_NE 0x00000200 /* Not empty */
76 #define SPIM_NF 0x00000100 /* Not full */
78 /* SPI Controller driver's private data. */
80 struct mpc83xx_spi_reg __iomem
*base
;
82 /* rx & tx bufs from the spi_transfer */
86 /* functions to deal with different sized buffers */
87 void (*get_rx
) (u32 rx_data
, struct mpc83xx_spi
*);
88 u32(*get_tx
) (struct mpc83xx_spi
*);
93 unsigned nsecs
; /* (clock cycle time)/2 */
95 u32 spibrg
; /* SPIBRG input clock */
96 u32 rx_shift
; /* RX data reg shift when in qe mode */
97 u32 tx_shift
; /* TX data reg shift when in qe mode */
103 struct workqueue_struct
*workqueue
;
104 struct work_struct work
;
106 struct list_head queue
;
109 struct completion done
;
112 struct spi_mpc83xx_cs
{
113 /* functions to deal with different sized buffers */
114 void (*get_rx
) (u32 rx_data
, struct mpc83xx_spi
*);
115 u32 (*get_tx
) (struct mpc83xx_spi
*);
116 u32 rx_shift
; /* RX data reg shift when in qe mode */
117 u32 tx_shift
; /* TX data reg shift when in qe mode */
118 u32 hw_mode
; /* Holds HW mode register settings */
121 static inline void mpc83xx_spi_write_reg(__be32 __iomem
*reg
, u32 val
)
126 static inline u32
mpc83xx_spi_read_reg(__be32 __iomem
*reg
)
131 #define MPC83XX_SPI_RX_BUF(type) \
133 void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \
135 type *rx = mpc83xx_spi->rx; \
136 *rx++ = (type)(data >> mpc83xx_spi->rx_shift); \
137 mpc83xx_spi->rx = rx; \
140 #define MPC83XX_SPI_TX_BUF(type) \
142 u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi) \
145 const type *tx = mpc83xx_spi->tx; \
148 data = *tx++ << mpc83xx_spi->tx_shift; \
149 mpc83xx_spi->tx = tx; \
153 MPC83XX_SPI_RX_BUF(u8
)
154 MPC83XX_SPI_RX_BUF(u16
)
155 MPC83XX_SPI_RX_BUF(u32
)
156 MPC83XX_SPI_TX_BUF(u8
)
157 MPC83XX_SPI_TX_BUF(u16
)
158 MPC83XX_SPI_TX_BUF(u32
)
160 static void mpc83xx_spi_chipselect(struct spi_device
*spi
, int value
)
162 struct mpc83xx_spi
*mpc83xx_spi
= spi_master_get_devdata(spi
->master
);
163 struct fsl_spi_platform_data
*pdata
= spi
->dev
.parent
->platform_data
;
164 bool pol
= spi
->mode
& SPI_CS_HIGH
;
165 struct spi_mpc83xx_cs
*cs
= spi
->controller_state
;
167 if (value
== BITBANG_CS_INACTIVE
) {
168 if (pdata
->cs_control
)
169 pdata
->cs_control(spi
, !pol
);
172 if (value
== BITBANG_CS_ACTIVE
) {
173 u32 regval
= mpc83xx_spi_read_reg(&mpc83xx_spi
->base
->mode
);
175 mpc83xx_spi
->rx_shift
= cs
->rx_shift
;
176 mpc83xx_spi
->tx_shift
= cs
->tx_shift
;
177 mpc83xx_spi
->get_rx
= cs
->get_rx
;
178 mpc83xx_spi
->get_tx
= cs
->get_tx
;
180 if (cs
->hw_mode
!= regval
) {
182 __be32 __iomem
*mode
= &mpc83xx_spi
->base
->mode
;
184 regval
= cs
->hw_mode
;
185 /* Turn off IRQs locally to minimize time that
188 local_irq_save(flags
);
189 /* Turn off SPI unit prior changing mode */
190 mpc83xx_spi_write_reg(mode
, regval
& ~SPMODE_ENABLE
);
191 mpc83xx_spi_write_reg(mode
, regval
);
192 local_irq_restore(flags
);
194 if (pdata
->cs_control
)
195 pdata
->cs_control(spi
, pol
);
200 int mpc83xx_spi_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
202 struct mpc83xx_spi
*mpc83xx_spi
;
204 u8 bits_per_word
, pm
;
206 struct spi_mpc83xx_cs
*cs
= spi
->controller_state
;
208 mpc83xx_spi
= spi_master_get_devdata(spi
->master
);
211 bits_per_word
= t
->bits_per_word
;
218 /* spi_transfer level calls that work per-word */
220 bits_per_word
= spi
->bits_per_word
;
222 /* Make sure its a bit width we support [4..16, 32] */
223 if ((bits_per_word
< 4)
224 || ((bits_per_word
> 16) && (bits_per_word
!= 32)))
228 hz
= spi
->max_speed_hz
;
232 if (bits_per_word
<= 8) {
233 cs
->get_rx
= mpc83xx_spi_rx_buf_u8
;
234 cs
->get_tx
= mpc83xx_spi_tx_buf_u8
;
235 if (mpc83xx_spi
->qe_mode
) {
239 } else if (bits_per_word
<= 16) {
240 cs
->get_rx
= mpc83xx_spi_rx_buf_u16
;
241 cs
->get_tx
= mpc83xx_spi_tx_buf_u16
;
242 if (mpc83xx_spi
->qe_mode
) {
246 } else if (bits_per_word
<= 32) {
247 cs
->get_rx
= mpc83xx_spi_rx_buf_u32
;
248 cs
->get_tx
= mpc83xx_spi_tx_buf_u32
;
252 if (mpc83xx_spi
->qe_mode
&& spi
->mode
& SPI_LSB_FIRST
) {
254 if (bits_per_word
<= 8)
260 mpc83xx_spi
->rx_shift
= cs
->rx_shift
;
261 mpc83xx_spi
->tx_shift
= cs
->tx_shift
;
262 mpc83xx_spi
->get_rx
= cs
->get_rx
;
263 mpc83xx_spi
->get_tx
= cs
->get_tx
;
265 if (bits_per_word
== 32)
268 bits_per_word
= bits_per_word
- 1;
270 /* mask out bits we are going to set */
271 cs
->hw_mode
&= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
274 cs
->hw_mode
|= SPMODE_LEN(bits_per_word
);
276 if ((mpc83xx_spi
->spibrg
/ hz
) > 64) {
277 cs
->hw_mode
|= SPMODE_DIV16
;
278 pm
= mpc83xx_spi
->spibrg
/ (hz
* 64);
280 WARN_ONCE(pm
> 16, "%s: Requested speed is too low: %d Hz. "
281 "Will use %d Hz instead.\n", dev_name(&spi
->dev
),
282 hz
, mpc83xx_spi
->spibrg
/ 1024);
286 pm
= mpc83xx_spi
->spibrg
/ (hz
* 4);
290 cs
->hw_mode
|= SPMODE_PM(pm
);
291 regval
= mpc83xx_spi_read_reg(&mpc83xx_spi
->base
->mode
);
292 if (cs
->hw_mode
!= regval
) {
294 __be32 __iomem
*mode
= &mpc83xx_spi
->base
->mode
;
296 regval
= cs
->hw_mode
;
297 /* Turn off IRQs locally to minimize time
298 * that SPI is disabled
300 local_irq_save(flags
);
301 /* Turn off SPI unit prior changing mode */
302 mpc83xx_spi_write_reg(mode
, regval
& ~SPMODE_ENABLE
);
303 mpc83xx_spi_write_reg(mode
, regval
);
304 local_irq_restore(flags
);
309 static int mpc83xx_spi_bufs(struct spi_device
*spi
, struct spi_transfer
*t
)
311 struct mpc83xx_spi
*mpc83xx_spi
;
312 u32 word
, len
, bits_per_word
;
314 mpc83xx_spi
= spi_master_get_devdata(spi
->master
);
316 mpc83xx_spi
->tx
= t
->tx_buf
;
317 mpc83xx_spi
->rx
= t
->rx_buf
;
318 bits_per_word
= spi
->bits_per_word
;
319 if (t
->bits_per_word
)
320 bits_per_word
= t
->bits_per_word
;
322 if (bits_per_word
> 8) {
323 /* invalid length? */
328 if (bits_per_word
> 16) {
329 /* invalid length? */
334 mpc83xx_spi
->count
= len
;
336 INIT_COMPLETION(mpc83xx_spi
->done
);
339 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->mask
, SPIM_NE
);
342 word
= mpc83xx_spi
->get_tx(mpc83xx_spi
);
343 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->transmit
, word
);
345 wait_for_completion(&mpc83xx_spi
->done
);
347 /* disable rx ints */
348 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->mask
, 0);
350 return mpc83xx_spi
->count
;
353 static void mpc83xx_spi_work(struct work_struct
*work
)
355 struct mpc83xx_spi
*mpc83xx_spi
=
356 container_of(work
, struct mpc83xx_spi
, work
);
358 spin_lock_irq(&mpc83xx_spi
->lock
);
359 mpc83xx_spi
->busy
= 1;
360 while (!list_empty(&mpc83xx_spi
->queue
)) {
361 struct spi_message
*m
;
362 struct spi_device
*spi
;
363 struct spi_transfer
*t
= NULL
;
365 int status
, nsecs
= 50;
367 m
= container_of(mpc83xx_spi
->queue
.next
,
368 struct spi_message
, queue
);
369 list_del_init(&m
->queue
);
370 spin_unlock_irq(&mpc83xx_spi
->lock
);
375 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
376 if (t
->bits_per_word
|| t
->speed_hz
) {
377 /* Don't allow changes if CS is active */
381 status
= mpc83xx_spi_setup_transfer(spi
, t
);
387 mpc83xx_spi_chipselect(spi
, BITBANG_CS_ACTIVE
);
390 cs_change
= t
->cs_change
;
392 status
= mpc83xx_spi_bufs(spi
, t
);
397 m
->actual_length
+= t
->len
;
400 udelay(t
->delay_usecs
);
404 mpc83xx_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
410 m
->complete(m
->context
);
412 if (status
|| !cs_change
) {
414 mpc83xx_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
417 mpc83xx_spi_setup_transfer(spi
, NULL
);
419 spin_lock_irq(&mpc83xx_spi
->lock
);
421 mpc83xx_spi
->busy
= 0;
422 spin_unlock_irq(&mpc83xx_spi
->lock
);
425 static int mpc83xx_spi_setup(struct spi_device
*spi
)
427 struct mpc83xx_spi
*mpc83xx_spi
;
430 struct spi_mpc83xx_cs
*cs
= spi
->controller_state
;
432 if (!spi
->max_speed_hz
)
436 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
439 spi
->controller_state
= cs
;
441 mpc83xx_spi
= spi_master_get_devdata(spi
->master
);
443 hw_mode
= cs
->hw_mode
; /* Save orginal settings */
444 cs
->hw_mode
= mpc83xx_spi_read_reg(&mpc83xx_spi
->base
->mode
);
445 /* mask out bits we are going to set */
446 cs
->hw_mode
&= ~(SPMODE_CP_BEGIN_EDGECLK
| SPMODE_CI_INACTIVEHIGH
447 | SPMODE_REV
| SPMODE_LOOP
);
449 if (spi
->mode
& SPI_CPHA
)
450 cs
->hw_mode
|= SPMODE_CP_BEGIN_EDGECLK
;
451 if (spi
->mode
& SPI_CPOL
)
452 cs
->hw_mode
|= SPMODE_CI_INACTIVEHIGH
;
453 if (!(spi
->mode
& SPI_LSB_FIRST
))
454 cs
->hw_mode
|= SPMODE_REV
;
455 if (spi
->mode
& SPI_LOOP
)
456 cs
->hw_mode
|= SPMODE_LOOP
;
458 retval
= mpc83xx_spi_setup_transfer(spi
, NULL
);
460 cs
->hw_mode
= hw_mode
; /* Restore settings */
464 #if 0 /* Don't think this is needed */
465 /* NOTE we _need_ to call chipselect() early, ideally with adapter
466 * setup, unless the hardware defaults cooperate to avoid confusion
467 * between normal (active low) and inverted chipselects.
470 /* deselect chip (low or high) */
471 spin_lock(&mpc83xx_spi
->lock
);
472 if (!mpc83xx_spi
->busy
)
473 mpc83xx_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
474 spin_unlock(&mpc83xx_spi
->lock
);
479 static irqreturn_t
mpc83xx_spi_irq(s32 irq
, void *context_data
)
481 struct mpc83xx_spi
*mpc83xx_spi
= context_data
;
483 irqreturn_t ret
= IRQ_NONE
;
485 /* Get interrupt events(tx/rx) */
486 event
= mpc83xx_spi_read_reg(&mpc83xx_spi
->base
->event
);
488 /* We need handle RX first */
489 if (event
& SPIE_NE
) {
490 u32 rx_data
= mpc83xx_spi_read_reg(&mpc83xx_spi
->base
->receive
);
493 mpc83xx_spi
->get_rx(rx_data
, mpc83xx_spi
);
498 if ((event
& SPIE_NF
) == 0)
499 /* spin until TX is done */
501 mpc83xx_spi_read_reg(&mpc83xx_spi
->base
->event
)) &
505 mpc83xx_spi
->count
-= 1;
506 if (mpc83xx_spi
->count
) {
507 u32 word
= mpc83xx_spi
->get_tx(mpc83xx_spi
);
508 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->transmit
, word
);
510 complete(&mpc83xx_spi
->done
);
513 /* Clear the events */
514 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->event
, event
);
518 static int mpc83xx_spi_transfer(struct spi_device
*spi
,
519 struct spi_message
*m
)
521 struct mpc83xx_spi
*mpc83xx_spi
= spi_master_get_devdata(spi
->master
);
524 m
->actual_length
= 0;
525 m
->status
= -EINPROGRESS
;
527 spin_lock_irqsave(&mpc83xx_spi
->lock
, flags
);
528 list_add_tail(&m
->queue
, &mpc83xx_spi
->queue
);
529 queue_work(mpc83xx_spi
->workqueue
, &mpc83xx_spi
->work
);
530 spin_unlock_irqrestore(&mpc83xx_spi
->lock
, flags
);
536 static void mpc83xx_spi_cleanup(struct spi_device
*spi
)
538 kfree(spi
->controller_state
);
541 static struct spi_master
* __devinit
542 mpc83xx_spi_probe(struct device
*dev
, struct resource
*mem
, unsigned int irq
)
544 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
545 struct spi_master
*master
;
546 struct mpc83xx_spi
*mpc83xx_spi
;
550 master
= spi_alloc_master(dev
, sizeof(struct mpc83xx_spi
));
551 if (master
== NULL
) {
556 dev_set_drvdata(dev
, master
);
558 /* the spi->mode bits understood by this driver: */
559 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
560 | SPI_LSB_FIRST
| SPI_LOOP
;
562 master
->setup
= mpc83xx_spi_setup
;
563 master
->transfer
= mpc83xx_spi_transfer
;
564 master
->cleanup
= mpc83xx_spi_cleanup
;
566 mpc83xx_spi
= spi_master_get_devdata(master
);
567 mpc83xx_spi
->qe_mode
= pdata
->qe_mode
;
568 mpc83xx_spi
->get_rx
= mpc83xx_spi_rx_buf_u8
;
569 mpc83xx_spi
->get_tx
= mpc83xx_spi_tx_buf_u8
;
570 mpc83xx_spi
->spibrg
= pdata
->sysclk
;
572 mpc83xx_spi
->rx_shift
= 0;
573 mpc83xx_spi
->tx_shift
= 0;
574 if (mpc83xx_spi
->qe_mode
) {
575 mpc83xx_spi
->rx_shift
= 16;
576 mpc83xx_spi
->tx_shift
= 24;
579 init_completion(&mpc83xx_spi
->done
);
581 mpc83xx_spi
->base
= ioremap(mem
->start
, mem
->end
- mem
->start
+ 1);
582 if (mpc83xx_spi
->base
== NULL
) {
587 mpc83xx_spi
->irq
= irq
;
589 /* Register for SPI Interrupt */
590 ret
= request_irq(mpc83xx_spi
->irq
, mpc83xx_spi_irq
,
591 0, "mpc83xx_spi", mpc83xx_spi
);
596 master
->bus_num
= pdata
->bus_num
;
597 master
->num_chipselect
= pdata
->max_chipselect
;
599 /* SPI controller initializations */
600 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->mode
, 0);
601 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->mask
, 0);
602 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->command
, 0);
603 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->event
, 0xffffffff);
605 /* Enable SPI interface */
606 regval
= pdata
->initial_spmode
| SPMODE_INIT_VAL
| SPMODE_ENABLE
;
610 mpc83xx_spi_write_reg(&mpc83xx_spi
->base
->mode
, regval
);
611 spin_lock_init(&mpc83xx_spi
->lock
);
612 init_completion(&mpc83xx_spi
->done
);
613 INIT_WORK(&mpc83xx_spi
->work
, mpc83xx_spi_work
);
614 INIT_LIST_HEAD(&mpc83xx_spi
->queue
);
616 mpc83xx_spi
->workqueue
= create_singlethread_workqueue(
617 dev_name(master
->dev
.parent
));
618 if (mpc83xx_spi
->workqueue
== NULL
) {
623 ret
= spi_register_master(master
);
628 "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
629 dev_name(dev
), mpc83xx_spi
->base
, mpc83xx_spi
->irq
);
634 destroy_workqueue(mpc83xx_spi
->workqueue
);
636 free_irq(mpc83xx_spi
->irq
, mpc83xx_spi
);
638 iounmap(mpc83xx_spi
->base
);
640 spi_master_put(master
);
645 static int __devexit
mpc83xx_spi_remove(struct device
*dev
)
647 struct mpc83xx_spi
*mpc83xx_spi
;
648 struct spi_master
*master
;
650 master
= dev_get_drvdata(dev
);
651 mpc83xx_spi
= spi_master_get_devdata(master
);
653 flush_workqueue(mpc83xx_spi
->workqueue
);
654 destroy_workqueue(mpc83xx_spi
->workqueue
);
655 spi_unregister_master(master
);
657 free_irq(mpc83xx_spi
->irq
, mpc83xx_spi
);
658 iounmap(mpc83xx_spi
->base
);
663 struct mpc83xx_spi_probe_info
{
664 struct fsl_spi_platform_data pdata
;
669 static struct mpc83xx_spi_probe_info
*
670 to_of_pinfo(struct fsl_spi_platform_data
*pdata
)
672 return container_of(pdata
, struct mpc83xx_spi_probe_info
, pdata
);
675 static void mpc83xx_spi_cs_control(struct spi_device
*spi
, bool on
)
677 struct device
*dev
= spi
->dev
.parent
;
678 struct mpc83xx_spi_probe_info
*pinfo
= to_of_pinfo(dev
->platform_data
);
679 u16 cs
= spi
->chip_select
;
680 int gpio
= pinfo
->gpios
[cs
];
681 bool alow
= pinfo
->alow_flags
[cs
];
683 gpio_set_value(gpio
, on
^ alow
);
686 static int of_mpc83xx_spi_get_chipselects(struct device
*dev
)
688 struct device_node
*np
= dev_archdata_get_node(&dev
->archdata
);
689 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
690 struct mpc83xx_spi_probe_info
*pinfo
= to_of_pinfo(pdata
);
695 ngpios
= of_gpio_count(np
);
698 * SPI w/o chip-select line. One SPI device is still permitted
701 pdata
->max_chipselect
= 1;
705 pinfo
->gpios
= kmalloc(ngpios
* sizeof(*pinfo
->gpios
), GFP_KERNEL
);
708 memset(pinfo
->gpios
, -1, ngpios
* sizeof(*pinfo
->gpios
));
710 pinfo
->alow_flags
= kzalloc(ngpios
* sizeof(*pinfo
->alow_flags
),
712 if (!pinfo
->alow_flags
) {
714 goto err_alloc_flags
;
717 for (; i
< ngpios
; i
++) {
719 enum of_gpio_flags flags
;
721 gpio
= of_get_gpio_flags(np
, i
, &flags
);
722 if (!gpio_is_valid(gpio
)) {
723 dev_err(dev
, "invalid gpio #%d: %d\n", i
, gpio
);
727 ret
= gpio_request(gpio
, dev_name(dev
));
729 dev_err(dev
, "can't request gpio #%d: %d\n", i
, ret
);
733 pinfo
->gpios
[i
] = gpio
;
734 pinfo
->alow_flags
[i
] = flags
& OF_GPIO_ACTIVE_LOW
;
736 ret
= gpio_direction_output(pinfo
->gpios
[i
],
737 pinfo
->alow_flags
[i
]);
739 dev_err(dev
, "can't set output direction for gpio "
740 "#%d: %d\n", i
, ret
);
745 pdata
->max_chipselect
= ngpios
;
746 pdata
->cs_control
= mpc83xx_spi_cs_control
;
752 if (gpio_is_valid(pinfo
->gpios
[i
]))
753 gpio_free(pinfo
->gpios
[i
]);
757 kfree(pinfo
->alow_flags
);
758 pinfo
->alow_flags
= NULL
;
765 static int of_mpc83xx_spi_free_chipselects(struct device
*dev
)
767 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
768 struct mpc83xx_spi_probe_info
*pinfo
= to_of_pinfo(pdata
);
774 for (i
= 0; i
< pdata
->max_chipselect
; i
++) {
775 if (gpio_is_valid(pinfo
->gpios
[i
]))
776 gpio_free(pinfo
->gpios
[i
]);
780 kfree(pinfo
->alow_flags
);
784 static int __devinit
of_mpc83xx_spi_probe(struct of_device
*ofdev
,
785 const struct of_device_id
*ofid
)
787 struct device
*dev
= &ofdev
->dev
;
788 struct device_node
*np
= ofdev
->node
;
789 struct mpc83xx_spi_probe_info
*pinfo
;
790 struct fsl_spi_platform_data
*pdata
;
791 struct spi_master
*master
;
797 pinfo
= kzalloc(sizeof(*pinfo
), GFP_KERNEL
);
801 pdata
= &pinfo
->pdata
;
802 dev
->platform_data
= pdata
;
804 /* Allocate bus num dynamically. */
807 /* SPI controller is either clocked from QE or SoC clock. */
808 pdata
->sysclk
= get_brgfreq();
809 if (pdata
->sysclk
== -1) {
810 pdata
->sysclk
= fsl_get_sys_freq();
811 if (pdata
->sysclk
== -1) {
817 prop
= of_get_property(np
, "mode", NULL
);
818 if (prop
&& !strcmp(prop
, "cpu-qe"))
821 ret
= of_mpc83xx_spi_get_chipselects(dev
);
825 ret
= of_address_to_resource(np
, 0, &mem
);
829 ret
= of_irq_to_resource(np
, 0, &irq
);
835 master
= mpc83xx_spi_probe(dev
, &mem
, irq
.start
);
836 if (IS_ERR(master
)) {
837 ret
= PTR_ERR(master
);
841 of_register_spi_devices(master
, np
);
846 of_mpc83xx_spi_free_chipselects(dev
);
852 static int __devexit
of_mpc83xx_spi_remove(struct of_device
*ofdev
)
856 ret
= mpc83xx_spi_remove(&ofdev
->dev
);
859 of_mpc83xx_spi_free_chipselects(&ofdev
->dev
);
863 static const struct of_device_id of_mpc83xx_spi_match
[] = {
864 { .compatible
= "fsl,spi" },
867 MODULE_DEVICE_TABLE(of
, of_mpc83xx_spi_match
);
869 static struct of_platform_driver of_mpc83xx_spi_driver
= {
870 .name
= "mpc83xx_spi",
871 .match_table
= of_mpc83xx_spi_match
,
872 .probe
= of_mpc83xx_spi_probe
,
873 .remove
= __devexit_p(of_mpc83xx_spi_remove
),
876 #ifdef CONFIG_MPC832x_RDB
879 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
880 * only. The driver should go away soon, since newer MPC8323E-RDB's device
881 * tree can work with OpenFirmware driver. But for now we support old trees
884 static int __devinit
plat_mpc83xx_spi_probe(struct platform_device
*pdev
)
886 struct resource
*mem
;
888 struct spi_master
*master
;
890 if (!pdev
->dev
.platform_data
)
893 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
897 irq
= platform_get_irq(pdev
, 0);
901 master
= mpc83xx_spi_probe(&pdev
->dev
, mem
, irq
);
903 return PTR_ERR(master
);
907 static int __devexit
plat_mpc83xx_spi_remove(struct platform_device
*pdev
)
909 return mpc83xx_spi_remove(&pdev
->dev
);
912 MODULE_ALIAS("platform:mpc83xx_spi");
913 static struct platform_driver mpc83xx_spi_driver
= {
914 .probe
= plat_mpc83xx_spi_probe
,
915 .remove
= __exit_p(plat_mpc83xx_spi_remove
),
917 .name
= "mpc83xx_spi",
918 .owner
= THIS_MODULE
,
922 static bool legacy_driver_failed
;
924 static void __init
legacy_driver_register(void)
926 legacy_driver_failed
= platform_driver_register(&mpc83xx_spi_driver
);
929 static void __exit
legacy_driver_unregister(void)
931 if (legacy_driver_failed
)
933 platform_driver_unregister(&mpc83xx_spi_driver
);
936 static void __init
legacy_driver_register(void) {}
937 static void __exit
legacy_driver_unregister(void) {}
938 #endif /* CONFIG_MPC832x_RDB */
940 static int __init
mpc83xx_spi_init(void)
942 legacy_driver_register();
943 return of_register_platform_driver(&of_mpc83xx_spi_driver
);
946 static void __exit
mpc83xx_spi_exit(void)
948 of_unregister_platform_driver(&of_mpc83xx_spi_driver
);
949 legacy_driver_unregister();
952 module_init(mpc83xx_spi_init
);
953 module_exit(mpc83xx_spi_exit
);
955 MODULE_AUTHOR("Kumar Gala");
956 MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
957 MODULE_LICENSE("GPL");