]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/spi/spi_mpc83xx.c
spi: move more spi_setup() functionality into core
[net-next-2.6.git] / drivers / spi / spi_mpc83xx.c
CommitLineData
ccf06998
KG
1/*
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>
35b4b3c0
AV
17#include <linux/errno.h>
18#include <linux/err.h>
ccf06998
KG
19#include <linux/completion.h>
20#include <linux/interrupt.h>
21#include <linux/delay.h>
22#include <linux/irq.h>
23#include <linux/device.h>
24#include <linux/spi/spi.h>
25#include <linux/spi/spi_bitbang.h>
26#include <linux/platform_device.h>
27#include <linux/fsl_devices.h>
35b4b3c0
AV
28#include <linux/of.h>
29#include <linux/of_platform.h>
30#include <linux/gpio.h>
31#include <linux/of_gpio.h>
32#include <linux/of_spi.h>
ccf06998 33
35b4b3c0 34#include <sysdev/fsl_soc.h>
ccf06998
KG
35#include <asm/irq.h>
36#include <asm/io.h>
37
38/* SPI Controller registers */
39struct mpc83xx_spi_reg {
40 u8 res1[0x20];
41 __be32 mode;
42 __be32 event;
43 __be32 mask;
44 __be32 command;
45 __be32 transmit;
46 __be32 receive;
47};
48
49/* SPI Controller mode register definitions */
2a485d7a 50#define SPMODE_LOOP (1 << 30)
ccf06998
KG
51#define SPMODE_CI_INACTIVEHIGH (1 << 29)
52#define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
53#define SPMODE_DIV16 (1 << 27)
54#define SPMODE_REV (1 << 26)
55#define SPMODE_MS (1 << 25)
56#define SPMODE_ENABLE (1 << 24)
57#define SPMODE_LEN(x) ((x) << 20)
58#define SPMODE_PM(x) ((x) << 16)
f29ba280 59#define SPMODE_OP (1 << 14)
c9bfcb31 60#define SPMODE_CG(x) ((x) << 7)
ccf06998
KG
61
62/*
63 * Default for SPI Mode:
64 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
65 */
66#define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
67 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
68
69/* SPIE register values */
70#define SPIE_NE 0x00000200 /* Not empty */
71#define SPIE_NF 0x00000100 /* Not full */
72
73/* SPIM register values */
74#define SPIM_NE 0x00000200 /* Not empty */
75#define SPIM_NF 0x00000100 /* Not full */
76
77/* SPI Controller driver's private data. */
78struct mpc83xx_spi {
ccf06998
KG
79 struct mpc83xx_spi_reg __iomem *base;
80
81 /* rx & tx bufs from the spi_transfer */
82 const void *tx;
83 void *rx;
84
85 /* functions to deal with different sized buffers */
86 void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
87 u32(*get_tx) (struct mpc83xx_spi *);
88
89 unsigned int count;
35b4b3c0 90 unsigned int irq;
ccf06998
KG
91
92 unsigned nsecs; /* (clock cycle time)/2 */
93
e24a4d1e 94 u32 spibrg; /* SPIBRG input clock */
f29ba280
JT
95 u32 rx_shift; /* RX data reg shift when in qe mode */
96 u32 tx_shift; /* TX data reg shift when in qe mode */
97
98 bool qe_mode;
99
c9bfcb31
JT
100 u8 busy;
101
102 struct workqueue_struct *workqueue;
103 struct work_struct work;
104
105 struct list_head queue;
106 spinlock_t lock;
107
108 struct completion done;
109};
110
111struct spi_mpc83xx_cs {
112 /* functions to deal with different sized buffers */
113 void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
114 u32 (*get_tx) (struct mpc83xx_spi *);
115 u32 rx_shift; /* RX data reg shift when in qe mode */
116 u32 tx_shift; /* TX data reg shift when in qe mode */
117 u32 hw_mode; /* Holds HW mode register settings */
ccf06998
KG
118};
119
120static inline void mpc83xx_spi_write_reg(__be32 __iomem * reg, u32 val)
121{
122 out_be32(reg, val);
123}
124
125static inline u32 mpc83xx_spi_read_reg(__be32 __iomem * reg)
126{
127 return in_be32(reg);
128}
129
130#define MPC83XX_SPI_RX_BUF(type) \
34c8a20c 131static \
ccf06998
KG
132void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \
133{ \
134 type * rx = mpc83xx_spi->rx; \
f29ba280 135 *rx++ = (type)(data >> mpc83xx_spi->rx_shift); \
ccf06998
KG
136 mpc83xx_spi->rx = rx; \
137}
138
139#define MPC83XX_SPI_TX_BUF(type) \
34c8a20c 140static \
ccf06998
KG
141u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi) \
142{ \
143 u32 data; \
144 const type * tx = mpc83xx_spi->tx; \
4b1badf5
DB
145 if (!tx) \
146 return 0; \
f29ba280 147 data = *tx++ << mpc83xx_spi->tx_shift; \
ccf06998
KG
148 mpc83xx_spi->tx = tx; \
149 return data; \
150}
151
152MPC83XX_SPI_RX_BUF(u8)
153MPC83XX_SPI_RX_BUF(u16)
154MPC83XX_SPI_RX_BUF(u32)
155MPC83XX_SPI_TX_BUF(u8)
156MPC83XX_SPI_TX_BUF(u16)
157MPC83XX_SPI_TX_BUF(u32)
158
159static void mpc83xx_spi_chipselect(struct spi_device *spi, int value)
160{
364fdbc0
AV
161 struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master);
162 struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
163 bool pol = spi->mode & SPI_CS_HIGH;
c9bfcb31 164 struct spi_mpc83xx_cs *cs = spi->controller_state;
ccf06998 165
ccf06998 166 if (value == BITBANG_CS_INACTIVE) {
364fdbc0
AV
167 if (pdata->cs_control)
168 pdata->cs_control(spi, !pol);
ccf06998
KG
169 }
170
171 if (value == BITBANG_CS_ACTIVE) {
172 u32 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
a44648b0 173
c9bfcb31
JT
174 mpc83xx_spi->rx_shift = cs->rx_shift;
175 mpc83xx_spi->tx_shift = cs->tx_shift;
176 mpc83xx_spi->get_rx = cs->get_rx;
177 mpc83xx_spi->get_tx = cs->get_tx;
178
179 if (cs->hw_mode != regval) {
180 unsigned long flags;
34c8a20c 181 __be32 __iomem *mode = &mpc83xx_spi->base->mode;
c9bfcb31
JT
182
183 regval = cs->hw_mode;
184 /* Turn off IRQs locally to minimize time that
185 * SPI is disabled
186 */
187 local_irq_save(flags);
188 /* Turn off SPI unit prior changing mode */
34c8a20c
AV
189 mpc83xx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
190 mpc83xx_spi_write_reg(mode, regval);
c9bfcb31 191 local_irq_restore(flags);
ccf06998 192 }
364fdbc0
AV
193 if (pdata->cs_control)
194 pdata->cs_control(spi, pol);
ccf06998
KG
195 }
196}
197
198static
199int mpc83xx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
200{
201 struct mpc83xx_spi *mpc83xx_spi;
202 u32 regval;
c9bfcb31 203 u8 bits_per_word, pm;
ccf06998 204 u32 hz;
c9bfcb31 205 struct spi_mpc83xx_cs *cs = spi->controller_state;
ccf06998
KG
206
207 mpc83xx_spi = spi_master_get_devdata(spi->master);
208
209 if (t) {
210 bits_per_word = t->bits_per_word;
211 hz = t->speed_hz;
212 } else {
213 bits_per_word = 0;
214 hz = 0;
215 }
216
217 /* spi_transfer level calls that work per-word */
218 if (!bits_per_word)
219 bits_per_word = spi->bits_per_word;
220
221 /* Make sure its a bit width we support [4..16, 32] */
222 if ((bits_per_word < 4)
223 || ((bits_per_word > 16) && (bits_per_word != 32)))
224 return -EINVAL;
225
c9bfcb31
JT
226 if (!hz)
227 hz = spi->max_speed_hz;
228
229 cs->rx_shift = 0;
230 cs->tx_shift = 0;
ccf06998 231 if (bits_per_word <= 8) {
c9bfcb31
JT
232 cs->get_rx = mpc83xx_spi_rx_buf_u8;
233 cs->get_tx = mpc83xx_spi_tx_buf_u8;
f29ba280 234 if (mpc83xx_spi->qe_mode) {
c9bfcb31
JT
235 cs->rx_shift = 16;
236 cs->tx_shift = 24;
f29ba280 237 }
ccf06998 238 } else if (bits_per_word <= 16) {
c9bfcb31
JT
239 cs->get_rx = mpc83xx_spi_rx_buf_u16;
240 cs->get_tx = mpc83xx_spi_tx_buf_u16;
f29ba280 241 if (mpc83xx_spi->qe_mode) {
c9bfcb31
JT
242 cs->rx_shift = 16;
243 cs->tx_shift = 16;
f29ba280 244 }
ccf06998 245 } else if (bits_per_word <= 32) {
c9bfcb31
JT
246 cs->get_rx = mpc83xx_spi_rx_buf_u32;
247 cs->get_tx = mpc83xx_spi_tx_buf_u32;
ccf06998
KG
248 } else
249 return -EINVAL;
250
35cc0b97 251 if (mpc83xx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) {
c9bfcb31 252 cs->tx_shift = 0;
35cc0b97 253 if (bits_per_word <= 8)
c9bfcb31 254 cs->rx_shift = 8;
35cc0b97 255 else
c9bfcb31 256 cs->rx_shift = 0;
35cc0b97
AV
257 }
258
c9bfcb31
JT
259 mpc83xx_spi->rx_shift = cs->rx_shift;
260 mpc83xx_spi->tx_shift = cs->tx_shift;
261 mpc83xx_spi->get_rx = cs->get_rx;
262 mpc83xx_spi->get_tx = cs->get_tx;
ccf06998
KG
263
264 if (bits_per_word == 32)
265 bits_per_word = 0;
266 else
267 bits_per_word = bits_per_word - 1;
268
32421daa 269 /* mask out bits we are going to set */
c9bfcb31
JT
270 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
271 | SPMODE_PM(0xF));
272
273 cs->hw_mode |= SPMODE_LEN(bits_per_word);
274
a61f5345 275 if ((mpc83xx_spi->spibrg / hz) > 64) {
53604dbe 276 cs->hw_mode |= SPMODE_DIV16;
a61f5345
CG
277 pm = mpc83xx_spi->spibrg / (hz * 64);
278 if (pm > 16) {
53604dbe
PK
279 dev_err(&spi->dev, "Requested speed is too "
280 "low: %d Hz. Will use %d Hz instead.\n",
281 hz, mpc83xx_spi->spibrg / 1024);
282 pm = 16;
c9bfcb31 283 }
a61f5345 284 } else
c9bfcb31 285 pm = mpc83xx_spi->spibrg / (hz * 4);
a61f5345
CG
286 if (pm)
287 pm--;
288
289 cs->hw_mode |= SPMODE_PM(pm);
c9bfcb31
JT
290 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
291 if (cs->hw_mode != regval) {
292 unsigned long flags;
34c8a20c 293 __be32 __iomem *mode = &mpc83xx_spi->base->mode;
c9bfcb31
JT
294
295 regval = cs->hw_mode;
296 /* Turn off IRQs locally to minimize time
297 * that SPI is disabled
298 */
299 local_irq_save(flags);
300 /* Turn off SPI unit prior changing mode */
34c8a20c
AV
301 mpc83xx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
302 mpc83xx_spi_write_reg(mode, regval);
c9bfcb31
JT
303 local_irq_restore(flags);
304 }
305 return 0;
306}
ccf06998 307
c9bfcb31
JT
308static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
309{
310 struct mpc83xx_spi *mpc83xx_spi;
311 u32 word, len, bits_per_word;
ccf06998 312
c9bfcb31
JT
313 mpc83xx_spi = spi_master_get_devdata(spi->master);
314
315 mpc83xx_spi->tx = t->tx_buf;
316 mpc83xx_spi->rx = t->rx_buf;
317 bits_per_word = spi->bits_per_word;
318 if (t->bits_per_word)
319 bits_per_word = t->bits_per_word;
320 len = t->len;
aa77d96b
PK
321 if (bits_per_word > 8) {
322 /* invalid length? */
323 if (len & 1)
324 return -EINVAL;
c9bfcb31 325 len /= 2;
aa77d96b
PK
326 }
327 if (bits_per_word > 16) {
328 /* invalid length? */
329 if (len & 1)
330 return -EINVAL;
c9bfcb31 331 len /= 2;
aa77d96b 332 }
c9bfcb31 333 mpc83xx_spi->count = len;
aa77d96b 334
c9bfcb31
JT
335 INIT_COMPLETION(mpc83xx_spi->done);
336
337 /* enable rx ints */
338 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE);
339
340 /* transmit word */
341 word = mpc83xx_spi->get_tx(mpc83xx_spi);
342 mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
343
344 wait_for_completion(&mpc83xx_spi->done);
345
346 /* disable rx ints */
347 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
348
349 return mpc83xx_spi->count;
350}
351
352static void mpc83xx_spi_work(struct work_struct *work)
353{
354 struct mpc83xx_spi *mpc83xx_spi =
355 container_of(work, struct mpc83xx_spi, work);
356
357 spin_lock_irq(&mpc83xx_spi->lock);
358 mpc83xx_spi->busy = 1;
359 while (!list_empty(&mpc83xx_spi->queue)) {
360 struct spi_message *m;
361 struct spi_device *spi;
362 struct spi_transfer *t = NULL;
363 unsigned cs_change;
364 int status, nsecs = 50;
365
366 m = container_of(mpc83xx_spi->queue.next,
367 struct spi_message, queue);
368 list_del_init(&m->queue);
369 spin_unlock_irq(&mpc83xx_spi->lock);
370
371 spi = m->spi;
372 cs_change = 1;
373 status = 0;
374 list_for_each_entry(t, &m->transfers, transfer_list) {
375 if (t->bits_per_word || t->speed_hz) {
376 /* Don't allow changes if CS is active */
377 status = -EINVAL;
378
379 if (cs_change)
380 status = mpc83xx_spi_setup_transfer(spi, t);
381 if (status < 0)
382 break;
383 }
384
385 if (cs_change)
386 mpc83xx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
387 cs_change = t->cs_change;
388 if (t->len)
389 status = mpc83xx_spi_bufs(spi, t);
390 if (status) {
391 status = -EMSGSIZE;
392 break;
393 }
394 m->actual_length += t->len;
395
396 if (t->delay_usecs)
397 udelay(t->delay_usecs);
398
399 if (cs_change) {
400 ndelay(nsecs);
401 mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
402 ndelay(nsecs);
403 }
404 }
405
406 m->status = status;
407 m->complete(m->context);
408
409 if (status || !cs_change) {
410 ndelay(nsecs);
411 mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
412 }
413
414 mpc83xx_spi_setup_transfer(spi, NULL);
415
416 spin_lock_irq(&mpc83xx_spi->lock);
417 }
418 mpc83xx_spi->busy = 0;
419 spin_unlock_irq(&mpc83xx_spi->lock);
ccf06998
KG
420}
421
422static int mpc83xx_spi_setup(struct spi_device *spi)
423{
ccf06998
KG
424 struct mpc83xx_spi *mpc83xx_spi;
425 int retval;
c9bfcb31
JT
426 u32 hw_mode;
427 struct spi_mpc83xx_cs *cs = spi->controller_state;
ccf06998
KG
428
429 if (!spi->max_speed_hz)
430 return -EINVAL;
431
c9bfcb31
JT
432 if (!cs) {
433 cs = kzalloc(sizeof *cs, GFP_KERNEL);
434 if (!cs)
435 return -ENOMEM;
436 spi->controller_state = cs;
437 }
ccf06998
KG
438 mpc83xx_spi = spi_master_get_devdata(spi->master);
439
c9bfcb31
JT
440 hw_mode = cs->hw_mode; /* Save orginal settings */
441 cs->hw_mode = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
442 /* mask out bits we are going to set */
443 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
444 | SPMODE_REV | SPMODE_LOOP);
445
446 if (spi->mode & SPI_CPHA)
447 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
448 if (spi->mode & SPI_CPOL)
449 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
450 if (!(spi->mode & SPI_LSB_FIRST))
451 cs->hw_mode |= SPMODE_REV;
452 if (spi->mode & SPI_LOOP)
453 cs->hw_mode |= SPMODE_LOOP;
454
ccf06998 455 retval = mpc83xx_spi_setup_transfer(spi, NULL);
c9bfcb31
JT
456 if (retval < 0) {
457 cs->hw_mode = hw_mode; /* Restore settings */
ccf06998 458 return retval;
c9bfcb31 459 }
ccf06998 460
c9bfcb31 461#if 0 /* Don't think this is needed */
ccf06998
KG
462 /* NOTE we _need_ to call chipselect() early, ideally with adapter
463 * setup, unless the hardware defaults cooperate to avoid confusion
464 * between normal (active low) and inverted chipselects.
465 */
466
467 /* deselect chip (low or high) */
c9bfcb31
JT
468 spin_lock(&mpc83xx_spi->lock);
469 if (!mpc83xx_spi->busy)
470 mpc83xx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
471 spin_unlock(&mpc83xx_spi->lock);
472#endif
ccf06998
KG
473 return 0;
474}
475
34c8a20c 476static irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data)
ccf06998
KG
477{
478 struct mpc83xx_spi *mpc83xx_spi = context_data;
479 u32 event;
480 irqreturn_t ret = IRQ_NONE;
481
482 /* Get interrupt events(tx/rx) */
483 event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event);
484
485 /* We need handle RX first */
486 if (event & SPIE_NE) {
487 u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive);
488
489 if (mpc83xx_spi->rx)
490 mpc83xx_spi->get_rx(rx_data, mpc83xx_spi);
491
492 ret = IRQ_HANDLED;
493 }
494
495 if ((event & SPIE_NF) == 0)
496 /* spin until TX is done */
497 while (((event =
498 mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) &
499 SPIE_NF) == 0)
500 cpu_relax();
501
502 mpc83xx_spi->count -= 1;
503 if (mpc83xx_spi->count) {
65e213cd
JA
504 u32 word = mpc83xx_spi->get_tx(mpc83xx_spi);
505 mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
ccf06998
KG
506 } else {
507 complete(&mpc83xx_spi->done);
508 }
509
510 /* Clear the events */
511 mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event);
512
513 return ret;
514}
c9bfcb31
JT
515static int mpc83xx_spi_transfer(struct spi_device *spi,
516 struct spi_message *m)
517{
518 struct mpc83xx_spi *mpc83xx_spi = spi_master_get_devdata(spi->master);
519 unsigned long flags;
520
521 m->actual_length = 0;
522 m->status = -EINPROGRESS;
523
524 spin_lock_irqsave(&mpc83xx_spi->lock, flags);
525 list_add_tail(&m->queue, &mpc83xx_spi->queue);
526 queue_work(mpc83xx_spi->workqueue, &mpc83xx_spi->work);
527 spin_unlock_irqrestore(&mpc83xx_spi->lock, flags);
528
529 return 0;
530}
531
532
533static void mpc83xx_spi_cleanup(struct spi_device *spi)
534{
535 kfree(spi->controller_state);
536}
ccf06998 537
35b4b3c0
AV
538static struct spi_master * __devinit
539mpc83xx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
ccf06998 540{
35b4b3c0 541 struct fsl_spi_platform_data *pdata = dev->platform_data;
ccf06998
KG
542 struct spi_master *master;
543 struct mpc83xx_spi *mpc83xx_spi;
ccf06998
KG
544 u32 regval;
545 int ret = 0;
546
35b4b3c0 547 master = spi_alloc_master(dev, sizeof(struct mpc83xx_spi));
ccf06998
KG
548 if (master == NULL) {
549 ret = -ENOMEM;
550 goto err;
551 }
552
35b4b3c0 553 dev_set_drvdata(dev, master);
ccf06998 554
e7db06b5
DB
555 /* the spi->mode bits understood by this driver: */
556 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
557 | SPI_LSB_FIRST | SPI_LOOP;
558
c9bfcb31
JT
559 master->setup = mpc83xx_spi_setup;
560 master->transfer = mpc83xx_spi_transfer;
561 master->cleanup = mpc83xx_spi_cleanup;
562
ccf06998 563 mpc83xx_spi = spi_master_get_devdata(master);
f29ba280 564 mpc83xx_spi->qe_mode = pdata->qe_mode;
ccf06998
KG
565 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
566 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
59a0ea50 567 mpc83xx_spi->spibrg = pdata->sysclk;
e24a4d1e 568
f29ba280
JT
569 mpc83xx_spi->rx_shift = 0;
570 mpc83xx_spi->tx_shift = 0;
571 if (mpc83xx_spi->qe_mode) {
572 mpc83xx_spi->rx_shift = 16;
573 mpc83xx_spi->tx_shift = 24;
574 }
575
ccf06998
KG
576 init_completion(&mpc83xx_spi->done);
577
35b4b3c0 578 mpc83xx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
ccf06998
KG
579 if (mpc83xx_spi->base == NULL) {
580 ret = -ENOMEM;
581 goto put_master;
582 }
583
35b4b3c0 584 mpc83xx_spi->irq = irq;
ccf06998
KG
585
586 /* Register for SPI Interrupt */
587 ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
588 0, "mpc83xx_spi", mpc83xx_spi);
589
590 if (ret != 0)
591 goto unmap_io;
592
593 master->bus_num = pdata->bus_num;
594 master->num_chipselect = pdata->max_chipselect;
595
596 /* SPI controller initializations */
597 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
598 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
599 mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0);
600 mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff);
601
602 /* Enable SPI interface */
603 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
f29ba280
JT
604 if (pdata->qe_mode)
605 regval |= SPMODE_OP;
606
ccf06998 607 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
c9bfcb31
JT
608 spin_lock_init(&mpc83xx_spi->lock);
609 init_completion(&mpc83xx_spi->done);
610 INIT_WORK(&mpc83xx_spi->work, mpc83xx_spi_work);
611 INIT_LIST_HEAD(&mpc83xx_spi->queue);
ccf06998 612
c9bfcb31 613 mpc83xx_spi->workqueue = create_singlethread_workqueue(
6c7377ab 614 dev_name(master->dev.parent));
c9bfcb31
JT
615 if (mpc83xx_spi->workqueue == NULL) {
616 ret = -EBUSY;
ccf06998 617 goto free_irq;
c9bfcb31
JT
618 }
619
620 ret = spi_register_master(master);
621 if (ret < 0)
622 goto unreg_master;
ccf06998
KG
623
624 printk(KERN_INFO
625 "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
35b4b3c0 626 dev_name(dev), mpc83xx_spi->base, mpc83xx_spi->irq);
ccf06998 627
35b4b3c0 628 return master;
ccf06998 629
c9bfcb31
JT
630unreg_master:
631 destroy_workqueue(mpc83xx_spi->workqueue);
ccf06998
KG
632free_irq:
633 free_irq(mpc83xx_spi->irq, mpc83xx_spi);
634unmap_io:
635 iounmap(mpc83xx_spi->base);
636put_master:
637 spi_master_put(master);
ccf06998 638err:
35b4b3c0 639 return ERR_PTR(ret);
ccf06998
KG
640}
641
35b4b3c0 642static int __devexit mpc83xx_spi_remove(struct device *dev)
ccf06998
KG
643{
644 struct mpc83xx_spi *mpc83xx_spi;
645 struct spi_master *master;
646
35b4b3c0 647 master = dev_get_drvdata(dev);
ccf06998
KG
648 mpc83xx_spi = spi_master_get_devdata(master);
649
c9bfcb31
JT
650 flush_workqueue(mpc83xx_spi->workqueue);
651 destroy_workqueue(mpc83xx_spi->workqueue);
652 spi_unregister_master(master);
653
ccf06998
KG
654 free_irq(mpc83xx_spi->irq, mpc83xx_spi);
655 iounmap(mpc83xx_spi->base);
ccf06998
KG
656
657 return 0;
658}
659
35b4b3c0
AV
660struct mpc83xx_spi_probe_info {
661 struct fsl_spi_platform_data pdata;
662 int *gpios;
663 bool *alow_flags;
664};
665
666static struct mpc83xx_spi_probe_info *
667to_of_pinfo(struct fsl_spi_platform_data *pdata)
668{
669 return container_of(pdata, struct mpc83xx_spi_probe_info, pdata);
670}
671
672static void mpc83xx_spi_cs_control(struct spi_device *spi, bool on)
673{
674 struct device *dev = spi->dev.parent;
675 struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
676 u16 cs = spi->chip_select;
677 int gpio = pinfo->gpios[cs];
678 bool alow = pinfo->alow_flags[cs];
679
680 gpio_set_value(gpio, on ^ alow);
681}
682
683static int of_mpc83xx_spi_get_chipselects(struct device *dev)
684{
685 struct device_node *np = dev_archdata_get_node(&dev->archdata);
686 struct fsl_spi_platform_data *pdata = dev->platform_data;
687 struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(pdata);
688 unsigned int ngpios;
689 int i = 0;
690 int ret;
691
692 ngpios = of_gpio_count(np);
693 if (!ngpios) {
694 /*
695 * SPI w/o chip-select line. One SPI device is still permitted
696 * though.
697 */
698 pdata->max_chipselect = 1;
699 return 0;
700 }
701
02141546 702 pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
35b4b3c0
AV
703 if (!pinfo->gpios)
704 return -ENOMEM;
02141546 705 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
35b4b3c0 706
02141546 707 pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
35b4b3c0
AV
708 GFP_KERNEL);
709 if (!pinfo->alow_flags) {
710 ret = -ENOMEM;
711 goto err_alloc_flags;
712 }
713
714 for (; i < ngpios; i++) {
715 int gpio;
716 enum of_gpio_flags flags;
717
718 gpio = of_get_gpio_flags(np, i, &flags);
719 if (!gpio_is_valid(gpio)) {
720 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
721 goto err_loop;
722 }
723
724 ret = gpio_request(gpio, dev_name(dev));
725 if (ret) {
726 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
727 goto err_loop;
728 }
729
730 pinfo->gpios[i] = gpio;
731 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
732
733 ret = gpio_direction_output(pinfo->gpios[i],
734 pinfo->alow_flags[i]);
735 if (ret) {
736 dev_err(dev, "can't set output direction for gpio "
737 "#%d: %d\n", i, ret);
738 goto err_loop;
739 }
740 }
741
742 pdata->max_chipselect = ngpios;
743 pdata->cs_control = mpc83xx_spi_cs_control;
744
745 return 0;
746
747err_loop:
748 while (i >= 0) {
749 if (gpio_is_valid(pinfo->gpios[i]))
750 gpio_free(pinfo->gpios[i]);
751 i--;
752 }
753
754 kfree(pinfo->alow_flags);
755 pinfo->alow_flags = NULL;
756err_alloc_flags:
757 kfree(pinfo->gpios);
758 pinfo->gpios = NULL;
759 return ret;
760}
761
762static int of_mpc83xx_spi_free_chipselects(struct device *dev)
763{
764 struct fsl_spi_platform_data *pdata = dev->platform_data;
765 struct mpc83xx_spi_probe_info *pinfo = to_of_pinfo(pdata);
766 int i;
767
768 if (!pinfo->gpios)
769 return 0;
770
771 for (i = 0; i < pdata->max_chipselect; i++) {
772 if (gpio_is_valid(pinfo->gpios[i]))
773 gpio_free(pinfo->gpios[i]);
774 }
775
776 kfree(pinfo->gpios);
777 kfree(pinfo->alow_flags);
778 return 0;
779}
780
781static int __devinit of_mpc83xx_spi_probe(struct of_device *ofdev,
782 const struct of_device_id *ofid)
783{
784 struct device *dev = &ofdev->dev;
785 struct device_node *np = ofdev->node;
786 struct mpc83xx_spi_probe_info *pinfo;
787 struct fsl_spi_platform_data *pdata;
788 struct spi_master *master;
789 struct resource mem;
790 struct resource irq;
791 const void *prop;
792 int ret = -ENOMEM;
793
794 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
795 if (!pinfo)
796 return -ENOMEM;
797
798 pdata = &pinfo->pdata;
799 dev->platform_data = pdata;
800
801 /* Allocate bus num dynamically. */
802 pdata->bus_num = -1;
803
804 /* SPI controller is either clocked from QE or SoC clock. */
805 pdata->sysclk = get_brgfreq();
806 if (pdata->sysclk == -1) {
807 pdata->sysclk = fsl_get_sys_freq();
808 if (pdata->sysclk == -1) {
809 ret = -ENODEV;
810 goto err_clk;
811 }
812 }
813
814 prop = of_get_property(np, "mode", NULL);
815 if (prop && !strcmp(prop, "cpu-qe"))
816 pdata->qe_mode = 1;
817
818 ret = of_mpc83xx_spi_get_chipselects(dev);
819 if (ret)
820 goto err;
821
822 ret = of_address_to_resource(np, 0, &mem);
823 if (ret)
824 goto err;
825
826 ret = of_irq_to_resource(np, 0, &irq);
827 if (!ret) {
828 ret = -EINVAL;
829 goto err;
830 }
831
832 master = mpc83xx_spi_probe(dev, &mem, irq.start);
833 if (IS_ERR(master)) {
834 ret = PTR_ERR(master);
835 goto err;
836 }
837
838 of_register_spi_devices(master, np);
839
840 return 0;
841
842err:
843 of_mpc83xx_spi_free_chipselects(dev);
844err_clk:
845 kfree(pinfo);
846 return ret;
847}
848
849static int __devexit of_mpc83xx_spi_remove(struct of_device *ofdev)
850{
851 int ret;
852
853 ret = mpc83xx_spi_remove(&ofdev->dev);
854 if (ret)
855 return ret;
856 of_mpc83xx_spi_free_chipselects(&ofdev->dev);
857 return 0;
858}
859
860static const struct of_device_id of_mpc83xx_spi_match[] = {
861 { .compatible = "fsl,spi" },
862 {},
863};
864MODULE_DEVICE_TABLE(of, of_mpc83xx_spi_match);
865
866static struct of_platform_driver of_mpc83xx_spi_driver = {
867 .name = "mpc83xx_spi",
868 .match_table = of_mpc83xx_spi_match,
869 .probe = of_mpc83xx_spi_probe,
870 .remove = __devexit_p(of_mpc83xx_spi_remove),
871};
872
873#ifdef CONFIG_MPC832x_RDB
874/*
875 * XXX XXX XXX
876 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
877 * only. The driver should go away soon, since newer MPC8323E-RDB's device
878 * tree can work with OpenFirmware driver. But for now we support old trees
879 * as well.
880 */
881static int __devinit plat_mpc83xx_spi_probe(struct platform_device *pdev)
882{
883 struct resource *mem;
884 unsigned int irq;
885 struct spi_master *master;
886
887 if (!pdev->dev.platform_data)
888 return -EINVAL;
889
890 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
891 if (!mem)
892 return -EINVAL;
893
894 irq = platform_get_irq(pdev, 0);
895 if (!irq)
896 return -EINVAL;
897
898 master = mpc83xx_spi_probe(&pdev->dev, mem, irq);
899 if (IS_ERR(master))
900 return PTR_ERR(master);
901 return 0;
902}
903
904static int __devexit plat_mpc83xx_spi_remove(struct platform_device *pdev)
905{
906 return mpc83xx_spi_remove(&pdev->dev);
907}
908
7e38c3c4 909MODULE_ALIAS("platform:mpc83xx_spi");
ccf06998 910static struct platform_driver mpc83xx_spi_driver = {
35b4b3c0
AV
911 .probe = plat_mpc83xx_spi_probe,
912 .remove = __exit_p(plat_mpc83xx_spi_remove),
ccf06998 913 .driver = {
7e38c3c4
KS
914 .name = "mpc83xx_spi",
915 .owner = THIS_MODULE,
ccf06998
KG
916 },
917};
918
35b4b3c0
AV
919static bool legacy_driver_failed;
920
921static void __init legacy_driver_register(void)
922{
923 legacy_driver_failed = platform_driver_register(&mpc83xx_spi_driver);
924}
925
926static void __exit legacy_driver_unregister(void)
927{
928 if (legacy_driver_failed)
929 return;
930 platform_driver_unregister(&mpc83xx_spi_driver);
931}
932#else
933static void __init legacy_driver_register(void) {}
934static void __exit legacy_driver_unregister(void) {}
935#endif /* CONFIG_MPC832x_RDB */
936
ccf06998
KG
937static int __init mpc83xx_spi_init(void)
938{
35b4b3c0
AV
939 legacy_driver_register();
940 return of_register_platform_driver(&of_mpc83xx_spi_driver);
ccf06998
KG
941}
942
943static void __exit mpc83xx_spi_exit(void)
944{
35b4b3c0
AV
945 of_unregister_platform_driver(&of_mpc83xx_spi_driver);
946 legacy_driver_unregister();
ccf06998
KG
947}
948
949module_init(mpc83xx_spi_init);
950module_exit(mpc83xx_spi_exit);
951
952MODULE_AUTHOR("Kumar Gala");
953MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
954MODULE_LICENSE("GPL");