blob: b95085a46f903a8723345c72cf8640dea3ee9c9c [file] [log] [blame]
Kumar Galaccf06992006-05-20 15:00:15 -07001/*
2 * MPC83xx SPI controller driver.
3 *
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>
17#include <linux/completion.h>
18#include <linux/interrupt.h>
19#include <linux/delay.h>
20#include <linux/irq.h>
21#include <linux/device.h>
22#include <linux/spi/spi.h>
23#include <linux/spi/spi_bitbang.h>
24#include <linux/platform_device.h>
25#include <linux/fsl_devices.h>
26
27#include <asm/irq.h>
28#include <asm/io.h>
29
30/* SPI Controller registers */
31struct mpc83xx_spi_reg {
32 u8 res1[0x20];
33 __be32 mode;
34 __be32 event;
35 __be32 mask;
36 __be32 command;
37 __be32 transmit;
38 __be32 receive;
39};
40
41/* SPI Controller mode register definitions */
Anton Vorontsov2a485d72007-07-31 00:38:45 -070042#define SPMODE_LOOP (1 << 30)
Kumar Galaccf06992006-05-20 15:00:15 -070043#define SPMODE_CI_INACTIVEHIGH (1 << 29)
44#define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
45#define SPMODE_DIV16 (1 << 27)
46#define SPMODE_REV (1 << 26)
47#define SPMODE_MS (1 << 25)
48#define SPMODE_ENABLE (1 << 24)
49#define SPMODE_LEN(x) ((x) << 20)
50#define SPMODE_PM(x) ((x) << 16)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -070051#define SPMODE_OP (1 << 14)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -070052#define SPMODE_CG(x) ((x) << 7)
Kumar Galaccf06992006-05-20 15:00:15 -070053
54/*
55 * Default for SPI Mode:
56 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
57 */
58#define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
59 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
60
61/* SPIE register values */
62#define SPIE_NE 0x00000200 /* Not empty */
63#define SPIE_NF 0x00000100 /* Not full */
64
65/* SPIM register values */
66#define SPIM_NE 0x00000200 /* Not empty */
67#define SPIM_NF 0x00000100 /* Not full */
68
69/* SPI Controller driver's private data. */
70struct mpc83xx_spi {
Kumar Galaccf06992006-05-20 15:00:15 -070071 struct mpc83xx_spi_reg __iomem *base;
72
73 /* rx & tx bufs from the spi_transfer */
74 const void *tx;
75 void *rx;
76
77 /* functions to deal with different sized buffers */
78 void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
79 u32(*get_tx) (struct mpc83xx_spi *);
80
81 unsigned int count;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -070082 int irq;
Kumar Galaccf06992006-05-20 15:00:15 -070083
84 unsigned nsecs; /* (clock cycle time)/2 */
85
Anton Vorontsove24a4d12007-08-10 13:01:01 -070086 u32 spibrg; /* SPIBRG input clock */
Joakim Tjernlundf29ba282007-07-17 04:04:12 -070087 u32 rx_shift; /* RX data reg shift when in qe mode */
88 u32 tx_shift; /* TX data reg shift when in qe mode */
89
90 bool qe_mode;
91
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -070092 u8 busy;
93
94 struct workqueue_struct *workqueue;
95 struct work_struct work;
96
97 struct list_head queue;
98 spinlock_t lock;
99
100 struct completion done;
101};
102
103struct spi_mpc83xx_cs {
104 /* functions to deal with different sized buffers */
105 void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
106 u32 (*get_tx) (struct mpc83xx_spi *);
107 u32 rx_shift; /* RX data reg shift when in qe mode */
108 u32 tx_shift; /* TX data reg shift when in qe mode */
109 u32 hw_mode; /* Holds HW mode register settings */
Kumar Galaccf06992006-05-20 15:00:15 -0700110};
111
112static inline void mpc83xx_spi_write_reg(__be32 __iomem * reg, u32 val)
113{
114 out_be32(reg, val);
115}
116
117static inline u32 mpc83xx_spi_read_reg(__be32 __iomem * reg)
118{
119 return in_be32(reg);
120}
121
122#define MPC83XX_SPI_RX_BUF(type) \
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700123static \
Kumar Galaccf06992006-05-20 15:00:15 -0700124void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \
125{ \
126 type * rx = mpc83xx_spi->rx; \
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700127 *rx++ = (type)(data >> mpc83xx_spi->rx_shift); \
Kumar Galaccf06992006-05-20 15:00:15 -0700128 mpc83xx_spi->rx = rx; \
129}
130
131#define MPC83XX_SPI_TX_BUF(type) \
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700132static \
Kumar Galaccf06992006-05-20 15:00:15 -0700133u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi) \
134{ \
135 u32 data; \
136 const type * tx = mpc83xx_spi->tx; \
David Brownell4b1badf2006-12-29 16:48:39 -0800137 if (!tx) \
138 return 0; \
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700139 data = *tx++ << mpc83xx_spi->tx_shift; \
Kumar Galaccf06992006-05-20 15:00:15 -0700140 mpc83xx_spi->tx = tx; \
141 return data; \
142}
143
144MPC83XX_SPI_RX_BUF(u8)
145MPC83XX_SPI_RX_BUF(u16)
146MPC83XX_SPI_RX_BUF(u32)
147MPC83XX_SPI_TX_BUF(u8)
148MPC83XX_SPI_TX_BUF(u16)
149MPC83XX_SPI_TX_BUF(u32)
150
151static void mpc83xx_spi_chipselect(struct spi_device *spi, int value)
152{
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700153 struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master);
154 struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
155 bool pol = spi->mode & SPI_CS_HIGH;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700156 struct spi_mpc83xx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700157
Kumar Galaccf06992006-05-20 15:00:15 -0700158 if (value == BITBANG_CS_INACTIVE) {
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700159 if (pdata->cs_control)
160 pdata->cs_control(spi, !pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700161 }
162
163 if (value == BITBANG_CS_ACTIVE) {
164 u32 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
Anton Vorontsova44648b2007-08-10 13:01:02 -0700165
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700166 mpc83xx_spi->rx_shift = cs->rx_shift;
167 mpc83xx_spi->tx_shift = cs->tx_shift;
168 mpc83xx_spi->get_rx = cs->get_rx;
169 mpc83xx_spi->get_tx = cs->get_tx;
Kumar Galaccf06992006-05-20 15:00:15 -0700170
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700171 if (cs->hw_mode != regval) {
172 unsigned long flags;
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700173 __be32 __iomem *mode = &mpc83xx_spi->base->mode;
Kumar Galaccf06992006-05-20 15:00:15 -0700174
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700175 regval = cs->hw_mode;
176 /* Turn off IRQs locally to minimize time that
177 * SPI is disabled
178 */
179 local_irq_save(flags);
180 /* Turn off SPI unit prior changing mode */
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700181 mpc83xx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
182 mpc83xx_spi_write_reg(mode, regval);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700183 local_irq_restore(flags);
Kumar Galaccf06992006-05-20 15:00:15 -0700184 }
Anton Vorontsov364fdbc2009-03-31 15:24:36 -0700185 if (pdata->cs_control)
186 pdata->cs_control(spi, pol);
Kumar Galaccf06992006-05-20 15:00:15 -0700187 }
188}
189
190static
191int mpc83xx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
192{
193 struct mpc83xx_spi *mpc83xx_spi;
194 u32 regval;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700195 u8 bits_per_word, pm;
Kumar Galaccf06992006-05-20 15:00:15 -0700196 u32 hz;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700197 struct spi_mpc83xx_cs *cs = spi->controller_state;
Kumar Galaccf06992006-05-20 15:00:15 -0700198
199 mpc83xx_spi = spi_master_get_devdata(spi->master);
200
201 if (t) {
202 bits_per_word = t->bits_per_word;
203 hz = t->speed_hz;
204 } else {
205 bits_per_word = 0;
206 hz = 0;
207 }
208
209 /* spi_transfer level calls that work per-word */
210 if (!bits_per_word)
211 bits_per_word = spi->bits_per_word;
212
213 /* Make sure its a bit width we support [4..16, 32] */
214 if ((bits_per_word < 4)
215 || ((bits_per_word > 16) && (bits_per_word != 32)))
216 return -EINVAL;
217
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700218 if (!hz)
219 hz = spi->max_speed_hz;
220
221 cs->rx_shift = 0;
222 cs->tx_shift = 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700223 if (bits_per_word <= 8) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700224 cs->get_rx = mpc83xx_spi_rx_buf_u8;
225 cs->get_tx = mpc83xx_spi_tx_buf_u8;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700226 if (mpc83xx_spi->qe_mode) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700227 cs->rx_shift = 16;
228 cs->tx_shift = 24;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700229 }
Kumar Galaccf06992006-05-20 15:00:15 -0700230 } else if (bits_per_word <= 16) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700231 cs->get_rx = mpc83xx_spi_rx_buf_u16;
232 cs->get_tx = mpc83xx_spi_tx_buf_u16;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700233 if (mpc83xx_spi->qe_mode) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700234 cs->rx_shift = 16;
235 cs->tx_shift = 16;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700236 }
Kumar Galaccf06992006-05-20 15:00:15 -0700237 } else if (bits_per_word <= 32) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700238 cs->get_rx = mpc83xx_spi_rx_buf_u32;
239 cs->get_tx = mpc83xx_spi_tx_buf_u32;
Kumar Galaccf06992006-05-20 15:00:15 -0700240 } else
241 return -EINVAL;
242
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700243 if (mpc83xx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) {
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700244 cs->tx_shift = 0;
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700245 if (bits_per_word <= 8)
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700246 cs->rx_shift = 8;
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700247 else
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700248 cs->rx_shift = 0;
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700249 }
250
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700251 mpc83xx_spi->rx_shift = cs->rx_shift;
252 mpc83xx_spi->tx_shift = cs->tx_shift;
253 mpc83xx_spi->get_rx = cs->get_rx;
254 mpc83xx_spi->get_tx = cs->get_tx;
Kumar Galaccf06992006-05-20 15:00:15 -0700255
256 if (bits_per_word == 32)
257 bits_per_word = 0;
258 else
259 bits_per_word = bits_per_word - 1;
260
Anton Vorontsov32421da2007-07-31 00:38:41 -0700261 /* mask out bits we are going to set */
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700262 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
263 | SPMODE_PM(0xF));
Kumar Galaccf06992006-05-20 15:00:15 -0700264
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700265 cs->hw_mode |= SPMODE_LEN(bits_per_word);
Kumar Galaccf06992006-05-20 15:00:15 -0700266
Chen Gonga61f5342008-07-23 21:29:52 -0700267 if ((mpc83xx_spi->spibrg / hz) > 64) {
Peter Korsgaard53604db2008-09-13 02:33:14 -0700268 cs->hw_mode |= SPMODE_DIV16;
Chen Gonga61f5342008-07-23 21:29:52 -0700269 pm = mpc83xx_spi->spibrg / (hz * 64);
270 if (pm > 16) {
Peter Korsgaard53604db2008-09-13 02:33:14 -0700271 dev_err(&spi->dev, "Requested speed is too "
272 "low: %d Hz. Will use %d Hz instead.\n",
273 hz, mpc83xx_spi->spibrg / 1024);
274 pm = 16;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700275 }
Chen Gonga61f5342008-07-23 21:29:52 -0700276 } else
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700277 pm = mpc83xx_spi->spibrg / (hz * 4);
Chen Gonga61f5342008-07-23 21:29:52 -0700278 if (pm)
279 pm--;
280
281 cs->hw_mode |= SPMODE_PM(pm);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700282 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
283 if (cs->hw_mode != regval) {
284 unsigned long flags;
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700285 __be32 __iomem *mode = &mpc83xx_spi->base->mode;
David Brownelldccd5732007-07-17 04:04:02 -0700286
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700287 regval = cs->hw_mode;
288 /* Turn off IRQs locally to minimize time
289 * that SPI is disabled
290 */
291 local_irq_save(flags);
292 /* Turn off SPI unit prior changing mode */
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700293 mpc83xx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
294 mpc83xx_spi_write_reg(mode, regval);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700295 local_irq_restore(flags);
Kumar Galaccf06992006-05-20 15:00:15 -0700296 }
Kumar Galaccf06992006-05-20 15:00:15 -0700297 return 0;
298}
299
300static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
301{
302 struct mpc83xx_spi *mpc83xx_spi;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700303 u32 word, len, bits_per_word;
Kumar Galaccf06992006-05-20 15:00:15 -0700304
305 mpc83xx_spi = spi_master_get_devdata(spi->master);
306
307 mpc83xx_spi->tx = t->tx_buf;
308 mpc83xx_spi->rx = t->rx_buf;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700309 bits_per_word = spi->bits_per_word;
310 if (t->bits_per_word)
311 bits_per_word = t->bits_per_word;
312 len = t->len;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700313 if (bits_per_word > 8) {
314 /* invalid length? */
315 if (len & 1)
316 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700317 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700318 }
319 if (bits_per_word > 16) {
320 /* invalid length? */
321 if (len & 1)
322 return -EINVAL;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700323 len /= 2;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700324 }
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700325 mpc83xx_spi->count = len;
Peter Korsgaardaa77d962008-09-13 02:33:15 -0700326
Kumar Galaccf06992006-05-20 15:00:15 -0700327 INIT_COMPLETION(mpc83xx_spi->done);
328
329 /* enable rx ints */
330 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE);
331
332 /* transmit word */
333 word = mpc83xx_spi->get_tx(mpc83xx_spi);
334 mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
335
336 wait_for_completion(&mpc83xx_spi->done);
337
338 /* disable rx ints */
339 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
340
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700341 return mpc83xx_spi->count;
342}
343
344static void mpc83xx_spi_work(struct work_struct *work)
345{
346 struct mpc83xx_spi *mpc83xx_spi =
347 container_of(work, struct mpc83xx_spi, work);
348
349 spin_lock_irq(&mpc83xx_spi->lock);
350 mpc83xx_spi->busy = 1;
351 while (!list_empty(&mpc83xx_spi->queue)) {
352 struct spi_message *m;
353 struct spi_device *spi;
354 struct spi_transfer *t = NULL;
355 unsigned cs_change;
356 int status, nsecs = 50;
357
358 m = container_of(mpc83xx_spi->queue.next,
359 struct spi_message, queue);
360 list_del_init(&m->queue);
361 spin_unlock_irq(&mpc83xx_spi->lock);
362
363 spi = m->spi;
364 cs_change = 1;
365 status = 0;
366 list_for_each_entry(t, &m->transfers, transfer_list) {
367 if (t->bits_per_word || t->speed_hz) {
368 /* Don't allow changes if CS is active */
369 status = -EINVAL;
370
371 if (cs_change)
372 status = mpc83xx_spi_setup_transfer(spi, t);
373 if (status < 0)
374 break;
375 }
376
377 if (cs_change)
378 mpc83xx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
379 cs_change = t->cs_change;
380 if (t->len)
381 status = mpc83xx_spi_bufs(spi, t);
382 if (status) {
383 status = -EMSGSIZE;
384 break;
385 }
386 m->actual_length += t->len;
387
388 if (t->delay_usecs)
389 udelay(t->delay_usecs);
390
391 if (cs_change) {
392 ndelay(nsecs);
393 mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
394 ndelay(nsecs);
395 }
396 }
397
398 m->status = status;
399 m->complete(m->context);
400
401 if (status || !cs_change) {
402 ndelay(nsecs);
403 mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
404 }
405
406 mpc83xx_spi_setup_transfer(spi, NULL);
407
408 spin_lock_irq(&mpc83xx_spi->lock);
409 }
410 mpc83xx_spi->busy = 0;
411 spin_unlock_irq(&mpc83xx_spi->lock);
412}
413
414/* the spi->mode bits understood by this driver: */
415#define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
416 | SPI_LSB_FIRST | SPI_LOOP)
417
418static int mpc83xx_spi_setup(struct spi_device *spi)
419{
420 struct mpc83xx_spi *mpc83xx_spi;
421 int retval;
422 u32 hw_mode;
423 struct spi_mpc83xx_cs *cs = spi->controller_state;
424
425 if (spi->mode & ~MODEBITS) {
426 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
427 spi->mode & ~MODEBITS);
428 return -EINVAL;
429 }
430
431 if (!spi->max_speed_hz)
432 return -EINVAL;
433
434 if (!cs) {
435 cs = kzalloc(sizeof *cs, GFP_KERNEL);
436 if (!cs)
437 return -ENOMEM;
438 spi->controller_state = cs;
439 }
440 mpc83xx_spi = spi_master_get_devdata(spi->master);
441
442 if (!spi->bits_per_word)
443 spi->bits_per_word = 8;
444
445 hw_mode = cs->hw_mode; /* Save orginal settings */
446 cs->hw_mode = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
447 /* mask out bits we are going to set */
448 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
449 | SPMODE_REV | SPMODE_LOOP);
450
451 if (spi->mode & SPI_CPHA)
452 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
453 if (spi->mode & SPI_CPOL)
454 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
455 if (!(spi->mode & SPI_LSB_FIRST))
456 cs->hw_mode |= SPMODE_REV;
457 if (spi->mode & SPI_LOOP)
458 cs->hw_mode |= SPMODE_LOOP;
459
460 retval = mpc83xx_spi_setup_transfer(spi, NULL);
461 if (retval < 0) {
462 cs->hw_mode = hw_mode; /* Restore settings */
463 return retval;
464 }
465
466 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u Hz\n",
467 __func__, spi->mode & (SPI_CPOL | SPI_CPHA),
468 spi->bits_per_word, spi->max_speed_hz);
469#if 0 /* Don't think this is needed */
470 /* NOTE we _need_ to call chipselect() early, ideally with adapter
471 * setup, unless the hardware defaults cooperate to avoid confusion
472 * between normal (active low) and inverted chipselects.
473 */
474
475 /* deselect chip (low or high) */
476 spin_lock(&mpc83xx_spi->lock);
477 if (!mpc83xx_spi->busy)
478 mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
479 spin_unlock(&mpc83xx_spi->lock);
480#endif
481 return 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700482}
483
Anton Vorontsov34c8a202009-03-31 15:24:35 -0700484static irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data)
Kumar Galaccf06992006-05-20 15:00:15 -0700485{
486 struct mpc83xx_spi *mpc83xx_spi = context_data;
487 u32 event;
488 irqreturn_t ret = IRQ_NONE;
489
490 /* Get interrupt events(tx/rx) */
491 event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event);
492
493 /* We need handle RX first */
494 if (event & SPIE_NE) {
495 u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive);
496
497 if (mpc83xx_spi->rx)
498 mpc83xx_spi->get_rx(rx_data, mpc83xx_spi);
499
500 ret = IRQ_HANDLED;
501 }
502
503 if ((event & SPIE_NF) == 0)
504 /* spin until TX is done */
505 while (((event =
506 mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) &
507 SPIE_NF) == 0)
508 cpu_relax();
509
510 mpc83xx_spi->count -= 1;
511 if (mpc83xx_spi->count) {
Jan Andersson65e213c2007-09-11 15:23:30 -0700512 u32 word = mpc83xx_spi->get_tx(mpc83xx_spi);
513 mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
Kumar Galaccf06992006-05-20 15:00:15 -0700514 } else {
515 complete(&mpc83xx_spi->done);
516 }
517
518 /* Clear the events */
519 mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event);
520
521 return ret;
522}
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700523static int mpc83xx_spi_transfer(struct spi_device *spi,
524 struct spi_message *m)
525{
526 struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master);
527 unsigned long flags;
528
529 m->actual_length = 0;
530 m->status = -EINPROGRESS;
531
532 spin_lock_irqsave(&mpc83xx_spi->lock, flags);
533 list_add_tail(&m->queue, &mpc83xx_spi->queue);
534 queue_work(mpc83xx_spi->workqueue, &mpc83xx_spi->work);
535 spin_unlock_irqrestore(&mpc83xx_spi->lock, flags);
536
537 return 0;
538}
539
540
541static void mpc83xx_spi_cleanup(struct spi_device *spi)
542{
543 kfree(spi->controller_state);
544}
Kumar Galaccf06992006-05-20 15:00:15 -0700545
546static int __init mpc83xx_spi_probe(struct platform_device *dev)
547{
548 struct spi_master *master;
549 struct mpc83xx_spi *mpc83xx_spi;
550 struct fsl_spi_platform_data *pdata;
551 struct resource *r;
552 u32 regval;
553 int ret = 0;
554
555 /* Get resources(memory, IRQ) associated with the device */
556 master = spi_alloc_master(&dev->dev, sizeof(struct mpc83xx_spi));
557
558 if (master == NULL) {
559 ret = -ENOMEM;
560 goto err;
561 }
562
563 platform_set_drvdata(dev, master);
564 pdata = dev->dev.platform_data;
565
566 if (pdata == NULL) {
567 ret = -ENODEV;
568 goto free_master;
569 }
570
571 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
572 if (r == NULL) {
573 ret = -ENODEV;
574 goto free_master;
575 }
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700576 master->setup = mpc83xx_spi_setup;
577 master->transfer = mpc83xx_spi_transfer;
578 master->cleanup = mpc83xx_spi_cleanup;
579
Kumar Galaccf06992006-05-20 15:00:15 -0700580 mpc83xx_spi = spi_master_get_devdata(master);
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700581 mpc83xx_spi->qe_mode = pdata->qe_mode;
Kumar Galaccf06992006-05-20 15:00:15 -0700582 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
583 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
Anton Vorontsov59a0ea52008-01-24 18:40:03 +0300584 mpc83xx_spi->spibrg = pdata->sysclk;
Anton Vorontsove24a4d12007-08-10 13:01:01 -0700585
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700586 mpc83xx_spi->rx_shift = 0;
587 mpc83xx_spi->tx_shift = 0;
588 if (mpc83xx_spi->qe_mode) {
589 mpc83xx_spi->rx_shift = 16;
590 mpc83xx_spi->tx_shift = 24;
591 }
592
Kumar Galaccf06992006-05-20 15:00:15 -0700593 init_completion(&mpc83xx_spi->done);
594
595 mpc83xx_spi->base = ioremap(r->start, r->end - r->start + 1);
596 if (mpc83xx_spi->base == NULL) {
597 ret = -ENOMEM;
598 goto put_master;
599 }
600
601 mpc83xx_spi->irq = platform_get_irq(dev, 0);
602
603 if (mpc83xx_spi->irq < 0) {
604 ret = -ENXIO;
605 goto unmap_io;
606 }
607
608 /* Register for SPI Interrupt */
609 ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
610 0, "mpc83xx_spi", mpc83xx_spi);
611
612 if (ret != 0)
613 goto unmap_io;
614
615 master->bus_num = pdata->bus_num;
616 master->num_chipselect = pdata->max_chipselect;
617
618 /* SPI controller initializations */
619 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
620 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
621 mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0);
622 mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff);
623
624 /* Enable SPI interface */
625 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700626 if (pdata->qe_mode)
627 regval |= SPMODE_OP;
628
Kumar Galaccf06992006-05-20 15:00:15 -0700629 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700630 spin_lock_init(&mpc83xx_spi->lock);
631 init_completion(&mpc83xx_spi->done);
632 INIT_WORK(&mpc83xx_spi->work, mpc83xx_spi_work);
633 INIT_LIST_HEAD(&mpc83xx_spi->queue);
Kumar Galaccf06992006-05-20 15:00:15 -0700634
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700635 mpc83xx_spi->workqueue = create_singlethread_workqueue(
Kay Sievers6c7377a2009-03-24 16:38:21 -0700636 dev_name(master->dev.parent));
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700637 if (mpc83xx_spi->workqueue == NULL) {
638 ret = -EBUSY;
Kumar Galaccf06992006-05-20 15:00:15 -0700639 goto free_irq;
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700640 }
641
642 ret = spi_register_master(master);
643 if (ret < 0)
644 goto unreg_master;
Kumar Galaccf06992006-05-20 15:00:15 -0700645
646 printk(KERN_INFO
647 "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
Kay Sievers6c7377a2009-03-24 16:38:21 -0700648 dev_name(&dev->dev), mpc83xx_spi->base, mpc83xx_spi->irq);
Kumar Galaccf06992006-05-20 15:00:15 -0700649
650 return ret;
651
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700652unreg_master:
653 destroy_workqueue(mpc83xx_spi->workqueue);
Kumar Galaccf06992006-05-20 15:00:15 -0700654free_irq:
655 free_irq(mpc83xx_spi->irq, mpc83xx_spi);
656unmap_io:
657 iounmap(mpc83xx_spi->base);
658put_master:
659 spi_master_put(master);
660free_master:
661 kfree(master);
662err:
663 return ret;
664}
665
David Brownelld1e44d92007-10-16 01:27:46 -0700666static int __exit mpc83xx_spi_remove(struct platform_device *dev)
Kumar Galaccf06992006-05-20 15:00:15 -0700667{
668 struct mpc83xx_spi *mpc83xx_spi;
669 struct spi_master *master;
670
671 master = platform_get_drvdata(dev);
672 mpc83xx_spi = spi_master_get_devdata(master);
673
Joakim Tjernlundc9bfcb32008-05-12 14:02:30 -0700674 flush_workqueue(mpc83xx_spi->workqueue);
675 destroy_workqueue(mpc83xx_spi->workqueue);
676 spi_unregister_master(master);
677
Kumar Galaccf06992006-05-20 15:00:15 -0700678 free_irq(mpc83xx_spi->irq, mpc83xx_spi);
679 iounmap(mpc83xx_spi->base);
Kumar Galaccf06992006-05-20 15:00:15 -0700680
681 return 0;
682}
683
Kay Sievers7e38c3c2008-04-10 21:29:20 -0700684MODULE_ALIAS("platform:mpc83xx_spi");
Kumar Galaccf06992006-05-20 15:00:15 -0700685static struct platform_driver mpc83xx_spi_driver = {
David Brownelld1e44d92007-10-16 01:27:46 -0700686 .remove = __exit_p(mpc83xx_spi_remove),
Kumar Galaccf06992006-05-20 15:00:15 -0700687 .driver = {
Kay Sievers7e38c3c2008-04-10 21:29:20 -0700688 .name = "mpc83xx_spi",
689 .owner = THIS_MODULE,
Kumar Galaccf06992006-05-20 15:00:15 -0700690 },
691};
692
693static int __init mpc83xx_spi_init(void)
694{
David Brownelld1e44d92007-10-16 01:27:46 -0700695 return platform_driver_probe(&mpc83xx_spi_driver, mpc83xx_spi_probe);
Kumar Galaccf06992006-05-20 15:00:15 -0700696}
697
698static void __exit mpc83xx_spi_exit(void)
699{
700 platform_driver_unregister(&mpc83xx_spi_driver);
701}
702
703module_init(mpc83xx_spi_init);
704module_exit(mpc83xx_spi_exit);
705
706MODULE_AUTHOR("Kumar Gala");
707MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
708MODULE_LICENSE("GPL");