blob: 0fd0ec4d3a7d1899e742a5d7005f4f986eab7293 [file] [log] [blame]
Kumar Galaccf06992006-05-20 15:00:15 -07001/*
Anton Vorontsov575c5802009-06-18 16:49:08 -07002 * MPC8xxx SPI controller driver.
Kumar Galaccf06992006-05-20 15:00:15 -07003 *
4 * Maintainer: Kumar Gala
5 *
6 * Copyright (C) 2006 Polycom, Inc.
7 *
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.
12 */
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/types.h>
16#include <linux/kernel.h>
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -070017#include <linux/bug.h>
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070018#include <linux/errno.h>
19#include <linux/err.h>
Anton Vorontsov9effb952009-06-18 16:49:05 -070020#include <linux/io.h>
Kumar Galaccf06992006-05-20 15:00:15 -070021#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>
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070030#include <linux/of.h>
31#include <linux/of_platform.h>
32#include <linux/gpio.h>
33#include <linux/of_gpio.h>
34#include <linux/of_spi.h>
Kumar Galaccf06992006-05-20 15:00:15 -070035
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070036#include <sysdev/fsl_soc.h>
Kumar Galaccf06992006-05-20 15:00:15 -070037#include <asm/irq.h>
Kumar Galaccf06992006-05-20 15:00:15 -070038
39/* SPI Controller registers */
Anton Vorontsov575c5802009-06-18 16:49:08 -070040struct mpc8xxx_spi_reg {
Kumar Galaccf06992006-05-20 15:00:15 -070041 u8 res1[0x20];
42 __be32 mode;
43 __be32 event;
44 __be32 mask;
45 __be32 command;
46 __be32 transmit;
47 __be32 receive;
48};
49
50/* SPI Controller mode register definitions */
Anton Vorontsov2a485d72007-07-31 00:38:45 -070051#define SPMODE_LOOP (1 << 30)
Kumar Galaccf06992006-05-20 15:00:15 -070052#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)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -070060#define SPMODE_OP (1 << 14)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -070061#define SPMODE_CG(x) ((x) << 7)
Kumar Galaccf06992006-05-20 15:00:15 -070062
63/*
64 * Default for SPI Mode:
65 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
66 */
67#define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
68 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
69
70/* SPIE register values */
71#define SPIE_NE 0x00000200 /* Not empty */
72#define SPIE_NF 0x00000100 /* Not full */
73
74/* SPIM register values */
75#define SPIM_NE 0x00000200 /* Not empty */
76#define SPIM_NF 0x00000100 /* Not full */
77
78/* SPI Controller driver's private data. */
Anton Vorontsov575c5802009-06-18 16:49:08 -070079struct mpc8xxx_spi {
80 struct mpc8xxx_spi_reg __iomem *base;
Kumar Galaccf06992006-05-20 15:00:15 -070081
82 /* rx & tx bufs from the spi_transfer */
83 const void *tx;
84 void *rx;
85
86 /* functions to deal with different sized buffers */
Anton Vorontsov575c5802009-06-18 16:49:08 -070087 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
88 u32(*get_tx) (struct mpc8xxx_spi *);
Kumar Galaccf06992006-05-20 15:00:15 -070089
90 unsigned int count;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -070091 unsigned int irq;
Kumar Galaccf06992006-05-20 15:00:15 -070092
93 unsigned nsecs; /* (clock cycle time)/2 */
94
Anton Vorontsove24a4d12007-08-10 13:01:01 -070095 u32 spibrg; /* SPIBRG input clock */
Joakim Tjernlundf29ba282007-07-17 04:04:12 -070096 u32 rx_shift; /* RX data reg shift when in qe mode */
97 u32 tx_shift; /* TX data reg shift when in qe mode */
98
99 bool qe_mode;
100
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700101 struct workqueue_struct *workqueue;
102 struct work_struct work;
103
104 struct list_head queue;
105 spinlock_t lock;
106
107 struct completion done;
108};
109
Anton Vorontsov575c5802009-06-18 16:49:08 -0700110struct spi_mpc8xxx_cs {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700111 /* functions to deal with different sized buffers */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700112 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
113 u32 (*get_tx) (struct mpc8xxx_spi *);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700114 u32 rx_shift; /* RX data reg shift when in qe mode */
115 u32 tx_shift; /* TX data reg shift when in qe mode */
116 u32 hw_mode; /* Holds HW mode register settings */
Kumar Galaccf06992006-05-20 15:00:15 -0700117};
118
Anton Vorontsov575c5802009-06-18 16:49:08 -0700119static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
Kumar Galaccf06992006-05-20 15:00:15 -0700120{
121 out_be32(reg, val);
122}
123
Anton Vorontsov575c5802009-06-18 16:49:08 -0700124static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
Kumar Galaccf06992006-05-20 15:00:15 -0700125{
126 return in_be32(reg);
127}
128
129#define MPC83XX_SPI_RX_BUF(type) \
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700130static \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700131void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
Kumar Galaccf06992006-05-20 15:00:15 -0700132{ \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700133 type *rx = mpc8xxx_spi->rx; \
134 *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
135 mpc8xxx_spi->rx = rx; \
Kumar Galaccf06992006-05-20 15:00:15 -0700136}
137
138#define MPC83XX_SPI_TX_BUF(type) \
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700139static \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700140u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
Kumar Galaccf06992006-05-20 15:00:15 -0700141{ \
142 u32 data; \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700143 const type *tx = mpc8xxx_spi->tx; \
David Brownell4b1badf2006-12-29 16:48:39 -0800144 if (!tx) \
145 return 0; \
Anton Vorontsov575c5802009-06-18 16:49:08 -0700146 data = *tx++ << mpc8xxx_spi->tx_shift; \
147 mpc8xxx_spi->tx = tx; \
Kumar Galaccf06992006-05-20 15:00:15 -0700148 return data; \
149}
150
151MPC83XX_SPI_RX_BUF(u8)
152MPC83XX_SPI_RX_BUF(u16)
153MPC83XX_SPI_RX_BUF(u32)
154MPC83XX_SPI_TX_BUF(u8)
155MPC83XX_SPI_TX_BUF(u16)
156MPC83XX_SPI_TX_BUF(u32)
157
Anton Vorontsov575c5802009-06-18 16:49:08 -0700158static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
Kumar Galaccf06992006-05-20 15:00:15 -0700159{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700160 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700161 struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
162 bool pol = spi->mode & SPI_CS_HIGH;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700163 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700164
Kumar Galaccf06992006-05-20 15:00:15 -0700165 if (value == BITBANG_CS_INACTIVE) {
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700166 if (pdata->cs_control)
167 pdata->cs_control(spi, !pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700168 }
169
170 if (value == BITBANG_CS_ACTIVE) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700171 u32 regval = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
Anton Vorontsova44648b2007-08-10 13:01:02 -0700172
Anton Vorontsov575c5802009-06-18 16:49:08 -0700173 mpc8xxx_spi->rx_shift = cs->rx_shift;
174 mpc8xxx_spi->tx_shift = cs->tx_shift;
175 mpc8xxx_spi->get_rx = cs->get_rx;
176 mpc8xxx_spi->get_tx = cs->get_tx;
Kumar Galaccf06992006-05-20 15:00:15 -0700177
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700178 if (cs->hw_mode != regval) {
179 unsigned long flags;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700180 __be32 __iomem *mode = &mpc8xxx_spi->base->mode;
Kumar Galaccf06992006-05-20 15:00:15 -0700181
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700182 regval = cs->hw_mode;
183 /* Turn off IRQs locally to minimize time that
184 * SPI is disabled
185 */
186 local_irq_save(flags);
187 /* Turn off SPI unit prior changing mode */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700188 mpc8xxx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
189 mpc8xxx_spi_write_reg(mode, regval);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700190 local_irq_restore(flags);
Kumar Galaccf06992006-05-20 15:00:15 -0700191 }
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700192 if (pdata->cs_control)
193 pdata->cs_control(spi, pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700194 }
195}
196
197static
Anton Vorontsov575c5802009-06-18 16:49:08 -0700198int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
Kumar Galaccf06992006-05-20 15:00:15 -0700199{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700200 struct mpc8xxx_spi *mpc8xxx_spi;
Kumar Galaccf06992006-05-20 15:00:15 -0700201 u32 regval;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700202 u8 bits_per_word, pm;
Kumar Galaccf06992006-05-20 15:00:15 -0700203 u32 hz;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700204 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700205
Anton Vorontsov575c5802009-06-18 16:49:08 -0700206 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kumar Galaccf06992006-05-20 15:00:15 -0700207
208 if (t) {
209 bits_per_word = t->bits_per_word;
210 hz = t->speed_hz;
211 } else {
212 bits_per_word = 0;
213 hz = 0;
214 }
215
216 /* spi_transfer level calls that work per-word */
217 if (!bits_per_word)
218 bits_per_word = spi->bits_per_word;
219
220 /* Make sure its a bit width we support [4..16, 32] */
221 if ((bits_per_word < 4)
222 || ((bits_per_word > 16) && (bits_per_word != 32)))
223 return -EINVAL;
224
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700225 if (!hz)
226 hz = spi->max_speed_hz;
227
228 cs->rx_shift = 0;
229 cs->tx_shift = 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700230 if (bits_per_word <= 8) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700231 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
232 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
233 if (mpc8xxx_spi->qe_mode) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700234 cs->rx_shift = 16;
235 cs->tx_shift = 24;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700236 }
Kumar Galaccf06992006-05-20 15:00:15 -0700237 } else if (bits_per_word <= 16) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700238 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
239 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
240 if (mpc8xxx_spi->qe_mode) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700241 cs->rx_shift = 16;
242 cs->tx_shift = 16;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700243 }
Kumar Galaccf06992006-05-20 15:00:15 -0700244 } else if (bits_per_word <= 32) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700245 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
246 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
Kumar Galaccf06992006-05-20 15:00:15 -0700247 } else
248 return -EINVAL;
249
Anton Vorontsov575c5802009-06-18 16:49:08 -0700250 if (mpc8xxx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700251 cs->tx_shift = 0;
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700252 if (bits_per_word <= 8)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700253 cs->rx_shift = 8;
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700254 else
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700255 cs->rx_shift = 0;
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700256 }
257
Anton Vorontsov575c5802009-06-18 16:49:08 -0700258 mpc8xxx_spi->rx_shift = cs->rx_shift;
259 mpc8xxx_spi->tx_shift = cs->tx_shift;
260 mpc8xxx_spi->get_rx = cs->get_rx;
261 mpc8xxx_spi->get_tx = cs->get_tx;
Kumar Galaccf06992006-05-20 15:00:15 -0700262
263 if (bits_per_word == 32)
264 bits_per_word = 0;
265 else
266 bits_per_word = bits_per_word - 1;
267
Anton Vorontsov32421da2007-07-31 00:38:41 -0700268 /* mask out bits we are going to set */
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700269 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
270 | SPMODE_PM(0xF));
Kumar Galaccf06992006-05-20 15:00:15 -0700271
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700272 cs->hw_mode |= SPMODE_LEN(bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700273
Anton Vorontsov575c5802009-06-18 16:49:08 -0700274 if ((mpc8xxx_spi->spibrg / hz) > 64) {
Peter Korsgaard53604db2008-09-13 02:33:14 -0700275 cs->hw_mode |= SPMODE_DIV16;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700276 pm = mpc8xxx_spi->spibrg / (hz * 64);
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700277
278 WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
279 "Will use %d Hz instead.\n", dev_name(&spi->dev),
Anton Vorontsov575c5802009-06-18 16:49:08 -0700280 hz, mpc8xxx_spi->spibrg / 1024);
Anton Vorontsovfd8a11e2009-06-18 16:49:01 -0700281 if (pm > 16)
Peter Korsgaard53604db2008-09-13 02:33:14 -0700282 pm = 16;
Chen Gonga61f5342008-07-23 21:29:52 -0700283 } else
Anton Vorontsov575c5802009-06-18 16:49:08 -0700284 pm = mpc8xxx_spi->spibrg / (hz * 4);
Chen Gonga61f5342008-07-23 21:29:52 -0700285 if (pm)
286 pm--;
287
288 cs->hw_mode |= SPMODE_PM(pm);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700289 regval = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700290 if (cs->hw_mode != regval) {
291 unsigned long flags;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700292 __be32 __iomem *mode = &mpc8xxx_spi->base->mode;
David Brownelldccd5732007-07-17 04:04:02 -0700293
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700294 regval = cs->hw_mode;
295 /* Turn off IRQs locally to minimize time
296 * that SPI is disabled
297 */
298 local_irq_save(flags);
299 /* Turn off SPI unit prior changing mode */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700300 mpc8xxx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
301 mpc8xxx_spi_write_reg(mode, regval);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700302 local_irq_restore(flags);
Kumar Galaccf06992006-05-20 15:00:15 -0700303 }
Kumar Galaccf06992006-05-20 15:00:15 -0700304 return 0;
305}
306
Anton Vorontsov575c5802009-06-18 16:49:08 -0700307static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
Kumar Galaccf06992006-05-20 15:00:15 -0700308{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700309 struct mpc8xxx_spi *mpc8xxx_spi;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700310 u32 word, len, bits_per_word;
Kumar Galaccf06992006-05-20 15:00:15 -0700311
Anton Vorontsov575c5802009-06-18 16:49:08 -0700312 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Kumar Galaccf06992006-05-20 15:00:15 -0700313
Anton Vorontsov575c5802009-06-18 16:49:08 -0700314 mpc8xxx_spi->tx = t->tx_buf;
315 mpc8xxx_spi->rx = t->rx_buf;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700316 bits_per_word = spi->bits_per_word;
317 if (t->bits_per_word)
318 bits_per_word = t->bits_per_word;
319 len = t->len;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700320 if (bits_per_word > 8) {
321 /* invalid length? */
322 if (len & 1)
323 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700324 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700325 }
326 if (bits_per_word > 16) {
327 /* invalid length? */
328 if (len & 1)
329 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700330 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700331 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700332 mpc8xxx_spi->count = len;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700333
Anton Vorontsov575c5802009-06-18 16:49:08 -0700334 INIT_COMPLETION(mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700335
336 /* enable rx ints */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700337 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, SPIM_NE);
Kumar Galaccf06992006-05-20 15:00:15 -0700338
339 /* transmit word */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700340 word = mpc8xxx_spi->get_tx(mpc8xxx_spi);
341 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word);
Kumar Galaccf06992006-05-20 15:00:15 -0700342
Anton Vorontsov575c5802009-06-18 16:49:08 -0700343 wait_for_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700344
345 /* disable rx ints */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700346 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
Kumar Galaccf06992006-05-20 15:00:15 -0700347
Anton Vorontsov575c5802009-06-18 16:49:08 -0700348 return mpc8xxx_spi->count;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700349}
350
Anton Vorontsov575c5802009-06-18 16:49:08 -0700351static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700352{
353 struct spi_device *spi = m->spi;
354 struct spi_transfer *t;
355 unsigned int cs_change;
356 const int nsecs = 50;
357 int status;
358
359 cs_change = 1;
360 status = 0;
361 list_for_each_entry(t, &m->transfers, transfer_list) {
362 if (t->bits_per_word || t->speed_hz) {
363 /* Don't allow changes if CS is active */
364 status = -EINVAL;
365
366 if (cs_change)
Anton Vorontsov575c5802009-06-18 16:49:08 -0700367 status = mpc8xxx_spi_setup_transfer(spi, t);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700368 if (status < 0)
369 break;
370 }
371
372 if (cs_change) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700373 mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700374 ndelay(nsecs);
375 }
376 cs_change = t->cs_change;
377 if (t->len)
Anton Vorontsov575c5802009-06-18 16:49:08 -0700378 status = mpc8xxx_spi_bufs(spi, t);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700379 if (status) {
380 status = -EMSGSIZE;
381 break;
382 }
383 m->actual_length += t->len;
384
385 if (t->delay_usecs)
386 udelay(t->delay_usecs);
387
388 if (cs_change) {
389 ndelay(nsecs);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700390 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700391 ndelay(nsecs);
392 }
393 }
394
395 m->status = status;
396 m->complete(m->context);
397
398 if (status || !cs_change) {
399 ndelay(nsecs);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700400 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700401 }
402
Anton Vorontsov575c5802009-06-18 16:49:08 -0700403 mpc8xxx_spi_setup_transfer(spi, NULL);
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700404}
405
Anton Vorontsov575c5802009-06-18 16:49:08 -0700406static void mpc8xxx_spi_work(struct work_struct *work)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700407{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700408 struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700409 work);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700410
Anton Vorontsov575c5802009-06-18 16:49:08 -0700411 spin_lock_irq(&mpc8xxx_spi->lock);
412 while (!list_empty(&mpc8xxx_spi->queue)) {
413 struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
Anton Vorontsovb9b9af12009-06-18 16:49:06 -0700414 struct spi_message, queue);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700415
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700416 list_del_init(&m->queue);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700417 spin_unlock_irq(&mpc8xxx_spi->lock);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700418
Anton Vorontsov575c5802009-06-18 16:49:08 -0700419 mpc8xxx_spi_do_one_msg(m);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700420
Anton Vorontsov575c5802009-06-18 16:49:08 -0700421 spin_lock_irq(&mpc8xxx_spi->lock);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700422 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700423 spin_unlock_irq(&mpc8xxx_spi->lock);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700424}
425
Anton Vorontsov575c5802009-06-18 16:49:08 -0700426static int mpc8xxx_spi_setup(struct spi_device *spi)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700427{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700428 struct mpc8xxx_spi *mpc8xxx_spi;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700429 int retval;
430 u32 hw_mode;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700431 struct spi_mpc8xxx_cs *cs = spi->controller_state;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700432
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700433 if (!spi->max_speed_hz)
434 return -EINVAL;
435
436 if (!cs) {
437 cs = kzalloc(sizeof *cs, GFP_KERNEL);
438 if (!cs)
439 return -ENOMEM;
440 spi->controller_state = cs;
441 }
Anton Vorontsov575c5802009-06-18 16:49:08 -0700442 mpc8xxx_spi = spi_master_get_devdata(spi->master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700443
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700444 hw_mode = cs->hw_mode; /* Save orginal settings */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700445 cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700446 /* mask out bits we are going to set */
447 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
448 | SPMODE_REV | SPMODE_LOOP);
449
450 if (spi->mode & SPI_CPHA)
451 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
452 if (spi->mode & SPI_CPOL)
453 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
454 if (!(spi->mode & SPI_LSB_FIRST))
455 cs->hw_mode |= SPMODE_REV;
456 if (spi->mode & SPI_LOOP)
457 cs->hw_mode |= SPMODE_LOOP;
458
Anton Vorontsov575c5802009-06-18 16:49:08 -0700459 retval = mpc8xxx_spi_setup_transfer(spi, NULL);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700460 if (retval < 0) {
461 cs->hw_mode = hw_mode; /* Restore settings */
462 return retval;
463 }
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700464 return 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700465}
466
Anton Vorontsov575c5802009-06-18 16:49:08 -0700467static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
Kumar Galaccf06992006-05-20 15:00:15 -0700468{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700469 struct mpc8xxx_spi *mpc8xxx_spi = context_data;
Kumar Galaccf06992006-05-20 15:00:15 -0700470 u32 event;
471 irqreturn_t ret = IRQ_NONE;
472
473 /* Get interrupt events(tx/rx) */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700474 event = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event);
Kumar Galaccf06992006-05-20 15:00:15 -0700475
476 /* We need handle RX first */
477 if (event & SPIE_NE) {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700478 u32 rx_data = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->receive);
Kumar Galaccf06992006-05-20 15:00:15 -0700479
Anton Vorontsov575c5802009-06-18 16:49:08 -0700480 if (mpc8xxx_spi->rx)
481 mpc8xxx_spi->get_rx(rx_data, mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700482
483 ret = IRQ_HANDLED;
484 }
485
486 if ((event & SPIE_NF) == 0)
487 /* spin until TX is done */
488 while (((event =
Anton Vorontsov575c5802009-06-18 16:49:08 -0700489 mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event)) &
Kumar Galaccf06992006-05-20 15:00:15 -0700490 SPIE_NF) == 0)
Anton Vorontsov9effb952009-06-18 16:49:05 -0700491 cpu_relax();
Kumar Galaccf06992006-05-20 15:00:15 -0700492
Anton Vorontsov575c5802009-06-18 16:49:08 -0700493 mpc8xxx_spi->count -= 1;
494 if (mpc8xxx_spi->count) {
495 u32 word = mpc8xxx_spi->get_tx(mpc8xxx_spi);
496 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word);
Kumar Galaccf06992006-05-20 15:00:15 -0700497 } else {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700498 complete(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700499 }
500
501 /* Clear the events */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700502 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, event);
Kumar Galaccf06992006-05-20 15:00:15 -0700503
504 return ret;
505}
Anton Vorontsov575c5802009-06-18 16:49:08 -0700506static int mpc8xxx_spi_transfer(struct spi_device *spi,
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700507 struct spi_message *m)
508{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700509 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700510 unsigned long flags;
511
512 m->actual_length = 0;
513 m->status = -EINPROGRESS;
514
Anton Vorontsov575c5802009-06-18 16:49:08 -0700515 spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
516 list_add_tail(&m->queue, &mpc8xxx_spi->queue);
517 queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
518 spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700519
520 return 0;
521}
522
523
Anton Vorontsov575c5802009-06-18 16:49:08 -0700524static void mpc8xxx_spi_cleanup(struct spi_device *spi)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700525{
526 kfree(spi->controller_state);
527}
Kumar Galaccf06992006-05-20 15:00:15 -0700528
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700529static struct spi_master * __devinit
Anton Vorontsov575c5802009-06-18 16:49:08 -0700530mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
Kumar Galaccf06992006-05-20 15:00:15 -0700531{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700532 struct fsl_spi_platform_data *pdata = dev->platform_data;
Kumar Galaccf06992006-05-20 15:00:15 -0700533 struct spi_master *master;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700534 struct mpc8xxx_spi *mpc8xxx_spi;
Kumar Galaccf06992006-05-20 15:00:15 -0700535 u32 regval;
536 int ret = 0;
537
Anton Vorontsov575c5802009-06-18 16:49:08 -0700538 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
Kumar Galaccf06992006-05-20 15:00:15 -0700539 if (master == NULL) {
540 ret = -ENOMEM;
541 goto err;
542 }
543
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700544 dev_set_drvdata(dev, master);
Kumar Galaccf06992006-05-20 15:00:15 -0700545
David Brownelle7db06b2009-06-17 16:26:04 -0700546 /* the spi->mode bits understood by this driver: */
547 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
548 | SPI_LSB_FIRST | SPI_LOOP;
549
Anton Vorontsov575c5802009-06-18 16:49:08 -0700550 master->setup = mpc8xxx_spi_setup;
551 master->transfer = mpc8xxx_spi_transfer;
552 master->cleanup = mpc8xxx_spi_cleanup;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700553
Anton Vorontsov575c5802009-06-18 16:49:08 -0700554 mpc8xxx_spi = spi_master_get_devdata(master);
555 mpc8xxx_spi->qe_mode = pdata->qe_mode;
556 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
557 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
558 mpc8xxx_spi->spibrg = pdata->sysclk;
Anton Vorontsove24a4d12007-08-10 13:01:01 -0700559
Anton Vorontsov575c5802009-06-18 16:49:08 -0700560 mpc8xxx_spi->rx_shift = 0;
561 mpc8xxx_spi->tx_shift = 0;
562 if (mpc8xxx_spi->qe_mode) {
563 mpc8xxx_spi->rx_shift = 16;
564 mpc8xxx_spi->tx_shift = 24;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700565 }
566
Anton Vorontsov575c5802009-06-18 16:49:08 -0700567 init_completion(&mpc8xxx_spi->done);
Kumar Galaccf06992006-05-20 15:00:15 -0700568
Anton Vorontsov575c5802009-06-18 16:49:08 -0700569 mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
570 if (mpc8xxx_spi->base == NULL) {
Kumar Galaccf06992006-05-20 15:00:15 -0700571 ret = -ENOMEM;
572 goto put_master;
573 }
574
Anton Vorontsov575c5802009-06-18 16:49:08 -0700575 mpc8xxx_spi->irq = irq;
Kumar Galaccf06992006-05-20 15:00:15 -0700576
577 /* Register for SPI Interrupt */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700578 ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
579 0, "mpc8xxx_spi", mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700580
581 if (ret != 0)
582 goto unmap_io;
583
584 master->bus_num = pdata->bus_num;
585 master->num_chipselect = pdata->max_chipselect;
586
587 /* SPI controller initializations */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700588 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
589 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
590 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
591 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
Kumar Galaccf06992006-05-20 15:00:15 -0700592
593 /* Enable SPI interface */
594 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700595 if (pdata->qe_mode)
596 regval |= SPMODE_OP;
597
Anton Vorontsov575c5802009-06-18 16:49:08 -0700598 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
599 spin_lock_init(&mpc8xxx_spi->lock);
600 init_completion(&mpc8xxx_spi->done);
601 INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
602 INIT_LIST_HEAD(&mpc8xxx_spi->queue);
Kumar Galaccf06992006-05-20 15:00:15 -0700603
Anton Vorontsov575c5802009-06-18 16:49:08 -0700604 mpc8xxx_spi->workqueue = create_singlethread_workqueue(
Kay Sievers6c7377a2009-03-24 16:38:21 -0700605 dev_name(master->dev.parent));
Anton Vorontsov575c5802009-06-18 16:49:08 -0700606 if (mpc8xxx_spi->workqueue == NULL) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700607 ret = -EBUSY;
Kumar Galaccf06992006-05-20 15:00:15 -0700608 goto free_irq;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700609 }
610
611 ret = spi_register_master(master);
612 if (ret < 0)
613 goto unreg_master;
Kumar Galaccf06992006-05-20 15:00:15 -0700614
615 printk(KERN_INFO
Anton Vorontsov575c5802009-06-18 16:49:08 -0700616 "%s: MPC8xxx SPI Controller driver at 0x%p (irq = %d)\n",
617 dev_name(dev), mpc8xxx_spi->base, mpc8xxx_spi->irq);
Kumar Galaccf06992006-05-20 15:00:15 -0700618
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700619 return master;
Kumar Galaccf06992006-05-20 15:00:15 -0700620
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700621unreg_master:
Anton Vorontsov575c5802009-06-18 16:49:08 -0700622 destroy_workqueue(mpc8xxx_spi->workqueue);
Kumar Galaccf06992006-05-20 15:00:15 -0700623free_irq:
Anton Vorontsov575c5802009-06-18 16:49:08 -0700624 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
Kumar Galaccf06992006-05-20 15:00:15 -0700625unmap_io:
Anton Vorontsov575c5802009-06-18 16:49:08 -0700626 iounmap(mpc8xxx_spi->base);
Kumar Galaccf06992006-05-20 15:00:15 -0700627put_master:
628 spi_master_put(master);
Kumar Galaccf06992006-05-20 15:00:15 -0700629err:
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700630 return ERR_PTR(ret);
Kumar Galaccf06992006-05-20 15:00:15 -0700631}
632
Anton Vorontsov575c5802009-06-18 16:49:08 -0700633static int __devexit mpc8xxx_spi_remove(struct device *dev)
Kumar Galaccf06992006-05-20 15:00:15 -0700634{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700635 struct mpc8xxx_spi *mpc8xxx_spi;
Kumar Galaccf06992006-05-20 15:00:15 -0700636 struct spi_master *master;
637
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700638 master = dev_get_drvdata(dev);
Anton Vorontsov575c5802009-06-18 16:49:08 -0700639 mpc8xxx_spi = spi_master_get_devdata(master);
Kumar Galaccf06992006-05-20 15:00:15 -0700640
Anton Vorontsov575c5802009-06-18 16:49:08 -0700641 flush_workqueue(mpc8xxx_spi->workqueue);
642 destroy_workqueue(mpc8xxx_spi->workqueue);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700643 spi_unregister_master(master);
644
Anton Vorontsov575c5802009-06-18 16:49:08 -0700645 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
646 iounmap(mpc8xxx_spi->base);
Kumar Galaccf06992006-05-20 15:00:15 -0700647
648 return 0;
649}
650
Anton Vorontsov575c5802009-06-18 16:49:08 -0700651struct mpc8xxx_spi_probe_info {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700652 struct fsl_spi_platform_data pdata;
653 int *gpios;
654 bool *alow_flags;
655};
656
Anton Vorontsov575c5802009-06-18 16:49:08 -0700657static struct mpc8xxx_spi_probe_info *
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700658to_of_pinfo(struct fsl_spi_platform_data *pdata)
659{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700660 return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700661}
662
Anton Vorontsov575c5802009-06-18 16:49:08 -0700663static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700664{
665 struct device *dev = spi->dev.parent;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700666 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700667 u16 cs = spi->chip_select;
668 int gpio = pinfo->gpios[cs];
669 bool alow = pinfo->alow_flags[cs];
670
671 gpio_set_value(gpio, on ^ alow);
672}
673
Anton Vorontsov575c5802009-06-18 16:49:08 -0700674static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700675{
676 struct device_node *np = dev_archdata_get_node(&dev->archdata);
677 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700678 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700679 unsigned int ngpios;
680 int i = 0;
681 int ret;
682
683 ngpios = of_gpio_count(np);
684 if (!ngpios) {
685 /*
686 * SPI w/o chip-select line. One SPI device is still permitted
687 * though.
688 */
689 pdata->max_chipselect = 1;
690 return 0;
691 }
692
Roel Kluin02141542009-06-16 15:31:15 -0700693 pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700694 if (!pinfo->gpios)
695 return -ENOMEM;
Roel Kluin02141542009-06-16 15:31:15 -0700696 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700697
Roel Kluin02141542009-06-16 15:31:15 -0700698 pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700699 GFP_KERNEL);
700 if (!pinfo->alow_flags) {
701 ret = -ENOMEM;
702 goto err_alloc_flags;
703 }
704
705 for (; i < ngpios; i++) {
706 int gpio;
707 enum of_gpio_flags flags;
708
709 gpio = of_get_gpio_flags(np, i, &flags);
710 if (!gpio_is_valid(gpio)) {
711 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
712 goto err_loop;
713 }
714
715 ret = gpio_request(gpio, dev_name(dev));
716 if (ret) {
717 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
718 goto err_loop;
719 }
720
721 pinfo->gpios[i] = gpio;
722 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
723
724 ret = gpio_direction_output(pinfo->gpios[i],
725 pinfo->alow_flags[i]);
726 if (ret) {
727 dev_err(dev, "can't set output direction for gpio "
728 "#%d: %d\n", i, ret);
729 goto err_loop;
730 }
731 }
732
733 pdata->max_chipselect = ngpios;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700734 pdata->cs_control = mpc8xxx_spi_cs_control;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700735
736 return 0;
737
738err_loop:
739 while (i >= 0) {
740 if (gpio_is_valid(pinfo->gpios[i]))
741 gpio_free(pinfo->gpios[i]);
742 i--;
743 }
744
745 kfree(pinfo->alow_flags);
746 pinfo->alow_flags = NULL;
747err_alloc_flags:
748 kfree(pinfo->gpios);
749 pinfo->gpios = NULL;
750 return ret;
751}
752
Anton Vorontsov575c5802009-06-18 16:49:08 -0700753static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700754{
755 struct fsl_spi_platform_data *pdata = dev->platform_data;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700756 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700757 int i;
758
759 if (!pinfo->gpios)
760 return 0;
761
762 for (i = 0; i < pdata->max_chipselect; i++) {
763 if (gpio_is_valid(pinfo->gpios[i]))
764 gpio_free(pinfo->gpios[i]);
765 }
766
767 kfree(pinfo->gpios);
768 kfree(pinfo->alow_flags);
769 return 0;
770}
771
Anton Vorontsov575c5802009-06-18 16:49:08 -0700772static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700773 const struct of_device_id *ofid)
774{
775 struct device *dev = &ofdev->dev;
776 struct device_node *np = ofdev->node;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700777 struct mpc8xxx_spi_probe_info *pinfo;
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700778 struct fsl_spi_platform_data *pdata;
779 struct spi_master *master;
780 struct resource mem;
781 struct resource irq;
782 const void *prop;
783 int ret = -ENOMEM;
784
785 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
786 if (!pinfo)
787 return -ENOMEM;
788
789 pdata = &pinfo->pdata;
790 dev->platform_data = pdata;
791
792 /* Allocate bus num dynamically. */
793 pdata->bus_num = -1;
794
795 /* SPI controller is either clocked from QE or SoC clock. */
796 pdata->sysclk = get_brgfreq();
797 if (pdata->sysclk == -1) {
798 pdata->sysclk = fsl_get_sys_freq();
799 if (pdata->sysclk == -1) {
800 ret = -ENODEV;
801 goto err_clk;
802 }
803 }
804
805 prop = of_get_property(np, "mode", NULL);
806 if (prop && !strcmp(prop, "cpu-qe"))
807 pdata->qe_mode = 1;
808
Anton Vorontsov575c5802009-06-18 16:49:08 -0700809 ret = of_mpc8xxx_spi_get_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700810 if (ret)
811 goto err;
812
813 ret = of_address_to_resource(np, 0, &mem);
814 if (ret)
815 goto err;
816
817 ret = of_irq_to_resource(np, 0, &irq);
818 if (!ret) {
819 ret = -EINVAL;
820 goto err;
821 }
822
Anton Vorontsov575c5802009-06-18 16:49:08 -0700823 master = mpc8xxx_spi_probe(dev, &mem, irq.start);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700824 if (IS_ERR(master)) {
825 ret = PTR_ERR(master);
826 goto err;
827 }
828
829 of_register_spi_devices(master, np);
830
831 return 0;
832
833err:
Anton Vorontsov575c5802009-06-18 16:49:08 -0700834 of_mpc8xxx_spi_free_chipselects(dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700835err_clk:
836 kfree(pinfo);
837 return ret;
838}
839
Anton Vorontsov575c5802009-06-18 16:49:08 -0700840static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700841{
842 int ret;
843
Anton Vorontsov575c5802009-06-18 16:49:08 -0700844 ret = mpc8xxx_spi_remove(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700845 if (ret)
846 return ret;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700847 of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700848 return 0;
849}
850
Anton Vorontsov575c5802009-06-18 16:49:08 -0700851static const struct of_device_id of_mpc8xxx_spi_match[] = {
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700852 { .compatible = "fsl,spi" },
853 {},
854};
Anton Vorontsov575c5802009-06-18 16:49:08 -0700855MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700856
Anton Vorontsov575c5802009-06-18 16:49:08 -0700857static struct of_platform_driver of_mpc8xxx_spi_driver = {
858 .name = "mpc8xxx_spi",
859 .match_table = of_mpc8xxx_spi_match,
860 .probe = of_mpc8xxx_spi_probe,
861 .remove = __devexit_p(of_mpc8xxx_spi_remove),
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700862};
863
864#ifdef CONFIG_MPC832x_RDB
865/*
866 * XXX XXX XXX
867 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
868 * only. The driver should go away soon, since newer MPC8323E-RDB's device
869 * tree can work with OpenFirmware driver. But for now we support old trees
870 * as well.
871 */
Anton Vorontsov575c5802009-06-18 16:49:08 -0700872static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700873{
874 struct resource *mem;
875 unsigned int irq;
876 struct spi_master *master;
877
878 if (!pdev->dev.platform_data)
879 return -EINVAL;
880
881 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
882 if (!mem)
883 return -EINVAL;
884
885 irq = platform_get_irq(pdev, 0);
886 if (!irq)
887 return -EINVAL;
888
Anton Vorontsov575c5802009-06-18 16:49:08 -0700889 master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700890 if (IS_ERR(master))
891 return PTR_ERR(master);
892 return 0;
893}
894
Anton Vorontsov575c5802009-06-18 16:49:08 -0700895static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700896{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700897 return mpc8xxx_spi_remove(&pdev->dev);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700898}
899
Anton Vorontsov575c5802009-06-18 16:49:08 -0700900MODULE_ALIAS("platform:mpc8xxx_spi");
901static struct platform_driver mpc8xxx_spi_driver = {
902 .probe = plat_mpc8xxx_spi_probe,
903 .remove = __exit_p(plat_mpc8xxx_spi_remove),
Kumar Galaccf06992006-05-20 15:00:15 -0700904 .driver = {
Anton Vorontsov575c5802009-06-18 16:49:08 -0700905 .name = "mpc8xxx_spi",
Kay Sievers7e38c3c2008-04-10 21:29:20 -0700906 .owner = THIS_MODULE,
Kumar Galaccf06992006-05-20 15:00:15 -0700907 },
908};
909
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700910static bool legacy_driver_failed;
911
912static void __init legacy_driver_register(void)
913{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700914 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700915}
916
917static void __exit legacy_driver_unregister(void)
918{
919 if (legacy_driver_failed)
920 return;
Anton Vorontsov575c5802009-06-18 16:49:08 -0700921 platform_driver_unregister(&mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700922}
923#else
924static void __init legacy_driver_register(void) {}
925static void __exit legacy_driver_unregister(void) {}
926#endif /* CONFIG_MPC832x_RDB */
927
Anton Vorontsov575c5802009-06-18 16:49:08 -0700928static int __init mpc8xxx_spi_init(void)
Kumar Galaccf06992006-05-20 15:00:15 -0700929{
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700930 legacy_driver_register();
Anton Vorontsov575c5802009-06-18 16:49:08 -0700931 return of_register_platform_driver(&of_mpc8xxx_spi_driver);
Kumar Galaccf06992006-05-20 15:00:15 -0700932}
933
Anton Vorontsov575c5802009-06-18 16:49:08 -0700934static void __exit mpc8xxx_spi_exit(void)
Kumar Galaccf06992006-05-20 15:00:15 -0700935{
Anton Vorontsov575c5802009-06-18 16:49:08 -0700936 of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
Anton Vorontsov35b4b3c2009-03-31 15:24:37 -0700937 legacy_driver_unregister();
Kumar Galaccf06992006-05-20 15:00:15 -0700938}
939
Anton Vorontsov575c5802009-06-18 16:49:08 -0700940module_init(mpc8xxx_spi_init);
941module_exit(mpc8xxx_spi_exit);
Kumar Galaccf06992006-05-20 15:00:15 -0700942
943MODULE_AUTHOR("Kumar Gala");
Anton Vorontsov575c5802009-06-18 16:49:08 -0700944MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
Kumar Galaccf06992006-05-20 15:00:15 -0700945MODULE_LICENSE("GPL");