spi_mpc8xxx: Fix uninitialized variable
[linux-2.6-block.git] / drivers / spi / spi_mpc8xxx.c
CommitLineData
ccf06998 1/*
575c5807 2 * MPC8xxx SPI controller driver.
ccf06998
KG
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>
fd8a11e1 17#include <linux/bug.h>
35b4b3c0
AV
18#include <linux/errno.h>
19#include <linux/err.h>
9effb959 20#include <linux/io.h>
ccf06998
KG
21#include <linux/completion.h>
22#include <linux/interrupt.h>
23#include <linux/delay.h>
24#include <linux/irq.h>
25#include <linux/device.h>
26#include <linux/spi/spi.h>
27#include <linux/spi/spi_bitbang.h>
28#include <linux/platform_device.h>
29#include <linux/fsl_devices.h>
35b4b3c0
AV
30#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>
ccf06998 35
35b4b3c0 36#include <sysdev/fsl_soc.h>
ccf06998 37#include <asm/irq.h>
ccf06998
KG
38
39/* SPI Controller registers */
575c5807 40struct mpc8xxx_spi_reg {
ccf06998
KG
41 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 */
2a485d7a 51#define SPMODE_LOOP (1 << 30)
ccf06998
KG
52#define SPMODE_CI_INACTIVEHIGH (1 << 29)
53#define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
54#define SPMODE_DIV16 (1 << 27)
55#define SPMODE_REV (1 << 26)
56#define SPMODE_MS (1 << 25)
57#define SPMODE_ENABLE (1 << 24)
58#define SPMODE_LEN(x) ((x) << 20)
59#define SPMODE_PM(x) ((x) << 16)
f29ba280 60#define SPMODE_OP (1 << 14)
c9bfcb31 61#define SPMODE_CG(x) ((x) << 7)
ccf06998
KG
62
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. */
575c5807
AV
79struct mpc8xxx_spi {
80 struct mpc8xxx_spi_reg __iomem *base;
ccf06998
KG
81
82 /* rx & tx bufs from the spi_transfer */
83 const void *tx;
84 void *rx;
85
86 /* functions to deal with different sized buffers */
575c5807
AV
87 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
88 u32(*get_tx) (struct mpc8xxx_spi *);
ccf06998
KG
89
90 unsigned int count;
35b4b3c0 91 unsigned int irq;
ccf06998
KG
92
93 unsigned nsecs; /* (clock cycle time)/2 */
94
e24a4d1e 95 u32 spibrg; /* SPIBRG input clock */
f29ba280
JT
96 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
c9bfcb31
JT
101 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
575c5807 110struct spi_mpc8xxx_cs {
c9bfcb31 111 /* functions to deal with different sized buffers */
575c5807
AV
112 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
113 u32 (*get_tx) (struct mpc8xxx_spi *);
c9bfcb31
JT
114 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 */
ccf06998
KG
117};
118
575c5807 119static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
ccf06998
KG
120{
121 out_be32(reg, val);
122}
123
575c5807 124static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
ccf06998
KG
125{
126 return in_be32(reg);
127}
128
129#define MPC83XX_SPI_RX_BUF(type) \
34c8a20c 130static \
575c5807 131void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
ccf06998 132{ \
575c5807
AV
133 type *rx = mpc8xxx_spi->rx; \
134 *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
135 mpc8xxx_spi->rx = rx; \
ccf06998
KG
136}
137
138#define MPC83XX_SPI_TX_BUF(type) \
34c8a20c 139static \
575c5807 140u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
ccf06998
KG
141{ \
142 u32 data; \
575c5807 143 const type *tx = mpc8xxx_spi->tx; \
4b1badf5
DB
144 if (!tx) \
145 return 0; \
575c5807
AV
146 data = *tx++ << mpc8xxx_spi->tx_shift; \
147 mpc8xxx_spi->tx = tx; \
ccf06998
KG
148 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
575c5807 158static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
ccf06998 159{
575c5807 160 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
364fdbc0
AV
161 struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
162 bool pol = spi->mode & SPI_CS_HIGH;
575c5807 163 struct spi_mpc8xxx_cs *cs = spi->controller_state;
ccf06998 164
ccf06998 165 if (value == BITBANG_CS_INACTIVE) {
364fdbc0
AV
166 if (pdata->cs_control)
167 pdata->cs_control(spi, !pol);
ccf06998
KG
168 }
169
170 if (value == BITBANG_CS_ACTIVE) {
575c5807 171 u32 regval = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
a44648b0 172
575c5807
AV
173 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;
c9bfcb31
JT
177
178 if (cs->hw_mode != regval) {
179 unsigned long flags;
575c5807 180 __be32 __iomem *mode = &mpc8xxx_spi->base->mode;
c9bfcb31
JT
181
182 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 */
575c5807
AV
188 mpc8xxx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
189 mpc8xxx_spi_write_reg(mode, regval);
c9bfcb31 190 local_irq_restore(flags);
ccf06998 191 }
364fdbc0
AV
192 if (pdata->cs_control)
193 pdata->cs_control(spi, pol);
ccf06998
KG
194 }
195}
196
197static
575c5807 198int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
ccf06998 199{
575c5807 200 struct mpc8xxx_spi *mpc8xxx_spi;
ccf06998 201 u32 regval;
c9bfcb31 202 u8 bits_per_word, pm;
ccf06998 203 u32 hz;
575c5807 204 struct spi_mpc8xxx_cs *cs = spi->controller_state;
ccf06998 205
575c5807 206 mpc8xxx_spi = spi_master_get_devdata(spi->master);
ccf06998
KG
207
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
c9bfcb31
JT
225 if (!hz)
226 hz = spi->max_speed_hz;
227
228 cs->rx_shift = 0;
229 cs->tx_shift = 0;
ccf06998 230 if (bits_per_word <= 8) {
575c5807
AV
231 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
232 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
233 if (mpc8xxx_spi->qe_mode) {
c9bfcb31
JT
234 cs->rx_shift = 16;
235 cs->tx_shift = 24;
f29ba280 236 }
ccf06998 237 } else if (bits_per_word <= 16) {
575c5807
AV
238 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
239 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
240 if (mpc8xxx_spi->qe_mode) {
c9bfcb31
JT
241 cs->rx_shift = 16;
242 cs->tx_shift = 16;
f29ba280 243 }
ccf06998 244 } else if (bits_per_word <= 32) {
575c5807
AV
245 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
246 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
ccf06998
KG
247 } else
248 return -EINVAL;
249
575c5807 250 if (mpc8xxx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) {
c9bfcb31 251 cs->tx_shift = 0;
35cc0b97 252 if (bits_per_word <= 8)
c9bfcb31 253 cs->rx_shift = 8;
35cc0b97 254 else
c9bfcb31 255 cs->rx_shift = 0;
35cc0b97
AV
256 }
257
575c5807
AV
258 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;
ccf06998
KG
262
263 if (bits_per_word == 32)
264 bits_per_word = 0;
265 else
266 bits_per_word = bits_per_word - 1;
267
32421daa 268 /* mask out bits we are going to set */
c9bfcb31
JT
269 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
270 | SPMODE_PM(0xF));
271
272 cs->hw_mode |= SPMODE_LEN(bits_per_word);
273
575c5807 274 if ((mpc8xxx_spi->spibrg / hz) > 64) {
53604dbe 275 cs->hw_mode |= SPMODE_DIV16;
575c5807 276 pm = mpc8xxx_spi->spibrg / (hz * 64);
fd8a11e1
AV
277
278 WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
279 "Will use %d Hz instead.\n", dev_name(&spi->dev),
575c5807 280 hz, mpc8xxx_spi->spibrg / 1024);
fd8a11e1 281 if (pm > 16)
53604dbe 282 pm = 16;
a61f5345 283 } else
575c5807 284 pm = mpc8xxx_spi->spibrg / (hz * 4);
a61f5345
CG
285 if (pm)
286 pm--;
287
288 cs->hw_mode |= SPMODE_PM(pm);
575c5807 289 regval = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
c9bfcb31
JT
290 if (cs->hw_mode != regval) {
291 unsigned long flags;
575c5807 292 __be32 __iomem *mode = &mpc8xxx_spi->base->mode;
c9bfcb31
JT
293
294 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 */
575c5807
AV
300 mpc8xxx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
301 mpc8xxx_spi_write_reg(mode, regval);
c9bfcb31
JT
302 local_irq_restore(flags);
303 }
304 return 0;
305}
ccf06998 306
575c5807 307static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
c9bfcb31 308{
575c5807 309 struct mpc8xxx_spi *mpc8xxx_spi;
c9bfcb31 310 u32 word, len, bits_per_word;
ccf06998 311
575c5807 312 mpc8xxx_spi = spi_master_get_devdata(spi->master);
c9bfcb31 313
575c5807
AV
314 mpc8xxx_spi->tx = t->tx_buf;
315 mpc8xxx_spi->rx = t->rx_buf;
c9bfcb31
JT
316 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;
aa77d96b
PK
320 if (bits_per_word > 8) {
321 /* invalid length? */
322 if (len & 1)
323 return -EINVAL;
c9bfcb31 324 len /= 2;
aa77d96b
PK
325 }
326 if (bits_per_word > 16) {
327 /* invalid length? */
328 if (len & 1)
329 return -EINVAL;
c9bfcb31 330 len /= 2;
aa77d96b 331 }
575c5807 332 mpc8xxx_spi->count = len;
aa77d96b 333
575c5807 334 INIT_COMPLETION(mpc8xxx_spi->done);
c9bfcb31
JT
335
336 /* enable rx ints */
575c5807 337 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, SPIM_NE);
c9bfcb31
JT
338
339 /* transmit word */
575c5807
AV
340 word = mpc8xxx_spi->get_tx(mpc8xxx_spi);
341 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word);
c9bfcb31 342
575c5807 343 wait_for_completion(&mpc8xxx_spi->done);
c9bfcb31
JT
344
345 /* disable rx ints */
575c5807 346 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
c9bfcb31 347
575c5807 348 return mpc8xxx_spi->count;
c9bfcb31
JT
349}
350
575c5807 351static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
c9bfcb31 352{
b9b9af11
AV
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)
575c5807 367 status = mpc8xxx_spi_setup_transfer(spi, t);
b9b9af11 368 if (status < 0)
c9bfcb31 369 break;
b9b9af11 370 }
c9bfcb31 371
b9b9af11 372 if (cs_change) {
575c5807 373 mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
b9b9af11
AV
374 ndelay(nsecs);
375 }
376 cs_change = t->cs_change;
377 if (t->len)
575c5807 378 status = mpc8xxx_spi_bufs(spi, t);
b9b9af11
AV
379 if (status) {
380 status = -EMSGSIZE;
381 break;
c9bfcb31 382 }
b9b9af11 383 m->actual_length += t->len;
c9bfcb31 384
b9b9af11
AV
385 if (t->delay_usecs)
386 udelay(t->delay_usecs);
c9bfcb31 387
b9b9af11 388 if (cs_change) {
c9bfcb31 389 ndelay(nsecs);
575c5807 390 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
b9b9af11 391 ndelay(nsecs);
c9bfcb31 392 }
b9b9af11
AV
393 }
394
395 m->status = status;
396 m->complete(m->context);
397
398 if (status || !cs_change) {
399 ndelay(nsecs);
575c5807 400 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
b9b9af11
AV
401 }
402
575c5807 403 mpc8xxx_spi_setup_transfer(spi, NULL);
b9b9af11
AV
404}
405
575c5807 406static void mpc8xxx_spi_work(struct work_struct *work)
b9b9af11 407{
575c5807 408 struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
b9b9af11
AV
409 work);
410
575c5807
AV
411 spin_lock_irq(&mpc8xxx_spi->lock);
412 while (!list_empty(&mpc8xxx_spi->queue)) {
413 struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
b9b9af11
AV
414 struct spi_message, queue);
415
416 list_del_init(&m->queue);
575c5807 417 spin_unlock_irq(&mpc8xxx_spi->lock);
c9bfcb31 418
575c5807 419 mpc8xxx_spi_do_one_msg(m);
c9bfcb31 420
575c5807 421 spin_lock_irq(&mpc8xxx_spi->lock);
c9bfcb31 422 }
575c5807 423 spin_unlock_irq(&mpc8xxx_spi->lock);
ccf06998
KG
424}
425
575c5807 426static int mpc8xxx_spi_setup(struct spi_device *spi)
ccf06998 427{
575c5807 428 struct mpc8xxx_spi *mpc8xxx_spi;
ccf06998 429 int retval;
c9bfcb31 430 u32 hw_mode;
575c5807 431 struct spi_mpc8xxx_cs *cs = spi->controller_state;
ccf06998
KG
432
433 if (!spi->max_speed_hz)
434 return -EINVAL;
435
c9bfcb31
JT
436 if (!cs) {
437 cs = kzalloc(sizeof *cs, GFP_KERNEL);
438 if (!cs)
439 return -ENOMEM;
440 spi->controller_state = cs;
441 }
575c5807 442 mpc8xxx_spi = spi_master_get_devdata(spi->master);
ccf06998 443
c9bfcb31 444 hw_mode = cs->hw_mode; /* Save orginal settings */
575c5807 445 cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
c9bfcb31
JT
446 /* 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
575c5807 459 retval = mpc8xxx_spi_setup_transfer(spi, NULL);
c9bfcb31
JT
460 if (retval < 0) {
461 cs->hw_mode = hw_mode; /* Restore settings */
ccf06998 462 return retval;
c9bfcb31 463 }
ccf06998
KG
464 return 0;
465}
466
575c5807 467static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
ccf06998 468{
575c5807 469 struct mpc8xxx_spi *mpc8xxx_spi = context_data;
ccf06998
KG
470 u32 event;
471 irqreturn_t ret = IRQ_NONE;
472
473 /* Get interrupt events(tx/rx) */
575c5807 474 event = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event);
ccf06998
KG
475
476 /* We need handle RX first */
477 if (event & SPIE_NE) {
575c5807 478 u32 rx_data = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->receive);
ccf06998 479
575c5807
AV
480 if (mpc8xxx_spi->rx)
481 mpc8xxx_spi->get_rx(rx_data, mpc8xxx_spi);
ccf06998
KG
482
483 ret = IRQ_HANDLED;
484 }
485
486 if ((event & SPIE_NF) == 0)
487 /* spin until TX is done */
488 while (((event =
575c5807 489 mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event)) &
ccf06998 490 SPIE_NF) == 0)
9effb959 491 cpu_relax();
ccf06998 492
575c5807
AV
493 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);
ccf06998 497 } else {
575c5807 498 complete(&mpc8xxx_spi->done);
ccf06998
KG
499 }
500
501 /* Clear the events */
575c5807 502 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, event);
ccf06998
KG
503
504 return ret;
505}
575c5807 506static int mpc8xxx_spi_transfer(struct spi_device *spi,
c9bfcb31
JT
507 struct spi_message *m)
508{
575c5807 509 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
c9bfcb31
JT
510 unsigned long flags;
511
512 m->actual_length = 0;
513 m->status = -EINPROGRESS;
514
575c5807
AV
515 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);
c9bfcb31
JT
519
520 return 0;
521}
522
523
575c5807 524static void mpc8xxx_spi_cleanup(struct spi_device *spi)
c9bfcb31
JT
525{
526 kfree(spi->controller_state);
527}
ccf06998 528
35b4b3c0 529static struct spi_master * __devinit
575c5807 530mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
ccf06998 531{
35b4b3c0 532 struct fsl_spi_platform_data *pdata = dev->platform_data;
ccf06998 533 struct spi_master *master;
575c5807 534 struct mpc8xxx_spi *mpc8xxx_spi;
ccf06998
KG
535 u32 regval;
536 int ret = 0;
537
575c5807 538 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
ccf06998
KG
539 if (master == NULL) {
540 ret = -ENOMEM;
541 goto err;
542 }
543
35b4b3c0 544 dev_set_drvdata(dev, master);
ccf06998 545
e7db06b5
DB
546 /* 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
575c5807
AV
550 master->setup = mpc8xxx_spi_setup;
551 master->transfer = mpc8xxx_spi_transfer;
552 master->cleanup = mpc8xxx_spi_cleanup;
553
554 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;
559
560 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;
f29ba280
JT
565 }
566
575c5807 567 init_completion(&mpc8xxx_spi->done);
ccf06998 568
575c5807
AV
569 mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
570 if (mpc8xxx_spi->base == NULL) {
ccf06998
KG
571 ret = -ENOMEM;
572 goto put_master;
573 }
574
575c5807 575 mpc8xxx_spi->irq = irq;
ccf06998
KG
576
577 /* Register for SPI Interrupt */
575c5807
AV
578 ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
579 0, "mpc8xxx_spi", mpc8xxx_spi);
ccf06998
KG
580
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 */
575c5807
AV
588 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);
ccf06998
KG
592
593 /* Enable SPI interface */
594 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
f29ba280
JT
595 if (pdata->qe_mode)
596 regval |= SPMODE_OP;
597
575c5807
AV
598 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);
ccf06998 603
575c5807 604 mpc8xxx_spi->workqueue = create_singlethread_workqueue(
6c7377ab 605 dev_name(master->dev.parent));
575c5807 606 if (mpc8xxx_spi->workqueue == NULL) {
c9bfcb31 607 ret = -EBUSY;
ccf06998 608 goto free_irq;
c9bfcb31
JT
609 }
610
611 ret = spi_register_master(master);
612 if (ret < 0)
613 goto unreg_master;
ccf06998
KG
614
615 printk(KERN_INFO
575c5807
AV
616 "%s: MPC8xxx SPI Controller driver at 0x%p (irq = %d)\n",
617 dev_name(dev), mpc8xxx_spi->base, mpc8xxx_spi->irq);
ccf06998 618
35b4b3c0 619 return master;
ccf06998 620
c9bfcb31 621unreg_master:
575c5807 622 destroy_workqueue(mpc8xxx_spi->workqueue);
ccf06998 623free_irq:
575c5807 624 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
ccf06998 625unmap_io:
575c5807 626 iounmap(mpc8xxx_spi->base);
ccf06998
KG
627put_master:
628 spi_master_put(master);
ccf06998 629err:
35b4b3c0 630 return ERR_PTR(ret);
ccf06998
KG
631}
632
575c5807 633static int __devexit mpc8xxx_spi_remove(struct device *dev)
ccf06998 634{
575c5807 635 struct mpc8xxx_spi *mpc8xxx_spi;
ccf06998
KG
636 struct spi_master *master;
637
35b4b3c0 638 master = dev_get_drvdata(dev);
575c5807 639 mpc8xxx_spi = spi_master_get_devdata(master);
ccf06998 640
575c5807
AV
641 flush_workqueue(mpc8xxx_spi->workqueue);
642 destroy_workqueue(mpc8xxx_spi->workqueue);
c9bfcb31
JT
643 spi_unregister_master(master);
644
575c5807
AV
645 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
646 iounmap(mpc8xxx_spi->base);
ccf06998
KG
647
648 return 0;
649}
650
575c5807 651struct mpc8xxx_spi_probe_info {
35b4b3c0
AV
652 struct fsl_spi_platform_data pdata;
653 int *gpios;
654 bool *alow_flags;
655};
656
575c5807 657static struct mpc8xxx_spi_probe_info *
35b4b3c0
AV
658to_of_pinfo(struct fsl_spi_platform_data *pdata)
659{
575c5807 660 return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
35b4b3c0
AV
661}
662
575c5807 663static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
35b4b3c0
AV
664{
665 struct device *dev = spi->dev.parent;
575c5807 666 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
35b4b3c0
AV
667 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
575c5807 674static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
35b4b3c0
AV
675{
676 struct device_node *np = dev_archdata_get_node(&dev->archdata);
677 struct fsl_spi_platform_data *pdata = dev->platform_data;
575c5807 678 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
35b4b3c0
AV
679 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
02141546 693 pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
35b4b3c0
AV
694 if (!pinfo->gpios)
695 return -ENOMEM;
02141546 696 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
35b4b3c0 697
02141546 698 pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
35b4b3c0
AV
699 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);
783058fd 712 ret = gpio;
35b4b3c0
AV
713 goto err_loop;
714 }
715
716 ret = gpio_request(gpio, dev_name(dev));
717 if (ret) {
718 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
719 goto err_loop;
720 }
721
722 pinfo->gpios[i] = gpio;
723 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
724
725 ret = gpio_direction_output(pinfo->gpios[i],
726 pinfo->alow_flags[i]);
727 if (ret) {
728 dev_err(dev, "can't set output direction for gpio "
729 "#%d: %d\n", i, ret);
730 goto err_loop;
731 }
732 }
733
734 pdata->max_chipselect = ngpios;
575c5807 735 pdata->cs_control = mpc8xxx_spi_cs_control;
35b4b3c0
AV
736
737 return 0;
738
739err_loop:
740 while (i >= 0) {
741 if (gpio_is_valid(pinfo->gpios[i]))
742 gpio_free(pinfo->gpios[i]);
743 i--;
744 }
745
746 kfree(pinfo->alow_flags);
747 pinfo->alow_flags = NULL;
748err_alloc_flags:
749 kfree(pinfo->gpios);
750 pinfo->gpios = NULL;
751 return ret;
752}
753
575c5807 754static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
35b4b3c0
AV
755{
756 struct fsl_spi_platform_data *pdata = dev->platform_data;
575c5807 757 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
35b4b3c0
AV
758 int i;
759
760 if (!pinfo->gpios)
761 return 0;
762
763 for (i = 0; i < pdata->max_chipselect; i++) {
764 if (gpio_is_valid(pinfo->gpios[i]))
765 gpio_free(pinfo->gpios[i]);
766 }
767
768 kfree(pinfo->gpios);
769 kfree(pinfo->alow_flags);
770 return 0;
771}
772
575c5807 773static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
35b4b3c0
AV
774 const struct of_device_id *ofid)
775{
776 struct device *dev = &ofdev->dev;
777 struct device_node *np = ofdev->node;
575c5807 778 struct mpc8xxx_spi_probe_info *pinfo;
35b4b3c0
AV
779 struct fsl_spi_platform_data *pdata;
780 struct spi_master *master;
781 struct resource mem;
782 struct resource irq;
783 const void *prop;
784 int ret = -ENOMEM;
785
786 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
787 if (!pinfo)
788 return -ENOMEM;
789
790 pdata = &pinfo->pdata;
791 dev->platform_data = pdata;
792
793 /* Allocate bus num dynamically. */
794 pdata->bus_num = -1;
795
796 /* SPI controller is either clocked from QE or SoC clock. */
797 pdata->sysclk = get_brgfreq();
798 if (pdata->sysclk == -1) {
799 pdata->sysclk = fsl_get_sys_freq();
800 if (pdata->sysclk == -1) {
801 ret = -ENODEV;
802 goto err_clk;
803 }
804 }
805
806 prop = of_get_property(np, "mode", NULL);
807 if (prop && !strcmp(prop, "cpu-qe"))
808 pdata->qe_mode = 1;
809
575c5807 810 ret = of_mpc8xxx_spi_get_chipselects(dev);
35b4b3c0
AV
811 if (ret)
812 goto err;
813
814 ret = of_address_to_resource(np, 0, &mem);
815 if (ret)
816 goto err;
817
818 ret = of_irq_to_resource(np, 0, &irq);
819 if (!ret) {
820 ret = -EINVAL;
821 goto err;
822 }
823
575c5807 824 master = mpc8xxx_spi_probe(dev, &mem, irq.start);
35b4b3c0
AV
825 if (IS_ERR(master)) {
826 ret = PTR_ERR(master);
827 goto err;
828 }
829
830 of_register_spi_devices(master, np);
831
832 return 0;
833
834err:
575c5807 835 of_mpc8xxx_spi_free_chipselects(dev);
35b4b3c0
AV
836err_clk:
837 kfree(pinfo);
838 return ret;
839}
840
575c5807 841static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
35b4b3c0
AV
842{
843 int ret;
844
575c5807 845 ret = mpc8xxx_spi_remove(&ofdev->dev);
35b4b3c0
AV
846 if (ret)
847 return ret;
575c5807 848 of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
35b4b3c0
AV
849 return 0;
850}
851
575c5807 852static const struct of_device_id of_mpc8xxx_spi_match[] = {
35b4b3c0
AV
853 { .compatible = "fsl,spi" },
854 {},
855};
575c5807 856MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
35b4b3c0 857
575c5807
AV
858static struct of_platform_driver of_mpc8xxx_spi_driver = {
859 .name = "mpc8xxx_spi",
860 .match_table = of_mpc8xxx_spi_match,
861 .probe = of_mpc8xxx_spi_probe,
862 .remove = __devexit_p(of_mpc8xxx_spi_remove),
35b4b3c0
AV
863};
864
865#ifdef CONFIG_MPC832x_RDB
866/*
867 * XXX XXX XXX
868 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
869 * only. The driver should go away soon, since newer MPC8323E-RDB's device
870 * tree can work with OpenFirmware driver. But for now we support old trees
871 * as well.
872 */
575c5807 873static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
35b4b3c0
AV
874{
875 struct resource *mem;
876 unsigned int irq;
877 struct spi_master *master;
878
879 if (!pdev->dev.platform_data)
880 return -EINVAL;
881
882 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
883 if (!mem)
884 return -EINVAL;
885
886 irq = platform_get_irq(pdev, 0);
887 if (!irq)
888 return -EINVAL;
889
575c5807 890 master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
35b4b3c0
AV
891 if (IS_ERR(master))
892 return PTR_ERR(master);
893 return 0;
894}
895
575c5807 896static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
35b4b3c0 897{
575c5807 898 return mpc8xxx_spi_remove(&pdev->dev);
35b4b3c0
AV
899}
900
575c5807
AV
901MODULE_ALIAS("platform:mpc8xxx_spi");
902static struct platform_driver mpc8xxx_spi_driver = {
903 .probe = plat_mpc8xxx_spi_probe,
904 .remove = __exit_p(plat_mpc8xxx_spi_remove),
ccf06998 905 .driver = {
575c5807 906 .name = "mpc8xxx_spi",
7e38c3c4 907 .owner = THIS_MODULE,
ccf06998
KG
908 },
909};
910
35b4b3c0
AV
911static bool legacy_driver_failed;
912
913static void __init legacy_driver_register(void)
914{
575c5807 915 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
35b4b3c0
AV
916}
917
918static void __exit legacy_driver_unregister(void)
919{
920 if (legacy_driver_failed)
921 return;
575c5807 922 platform_driver_unregister(&mpc8xxx_spi_driver);
35b4b3c0
AV
923}
924#else
925static void __init legacy_driver_register(void) {}
926static void __exit legacy_driver_unregister(void) {}
927#endif /* CONFIG_MPC832x_RDB */
928
575c5807 929static int __init mpc8xxx_spi_init(void)
ccf06998 930{
35b4b3c0 931 legacy_driver_register();
575c5807 932 return of_register_platform_driver(&of_mpc8xxx_spi_driver);
ccf06998
KG
933}
934
575c5807 935static void __exit mpc8xxx_spi_exit(void)
ccf06998 936{
575c5807 937 of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
35b4b3c0 938 legacy_driver_unregister();
ccf06998
KG
939}
940
575c5807
AV
941module_init(mpc8xxx_spi_init);
942module_exit(mpc8xxx_spi_exit);
ccf06998
KG
943
944MODULE_AUTHOR("Kumar Gala");
575c5807 945MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
ccf06998 946MODULE_LICENSE("GPL");