blob: 72812bb9b40fe72365cbb52eab5bcbf745280aff [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 */
42#define SPMODE_CI_INACTIVEHIGH (1 << 29)
43#define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
44#define SPMODE_DIV16 (1 << 27)
45#define SPMODE_REV (1 << 26)
46#define SPMODE_MS (1 << 25)
47#define SPMODE_ENABLE (1 << 24)
48#define SPMODE_LEN(x) ((x) << 20)
49#define SPMODE_PM(x) ((x) << 16)
Joakim Tjernlundf29ba282007-07-17 04:04:12 -070050#define SPMODE_OP (1 << 14)
Kumar Galaccf06992006-05-20 15:00:15 -070051
52/*
53 * Default for SPI Mode:
54 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
55 */
56#define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
57 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
58
59/* SPIE register values */
60#define SPIE_NE 0x00000200 /* Not empty */
61#define SPIE_NF 0x00000100 /* Not full */
62
63/* SPIM register values */
64#define SPIM_NE 0x00000200 /* Not empty */
65#define SPIM_NF 0x00000100 /* Not full */
66
67/* SPI Controller driver's private data. */
68struct mpc83xx_spi {
69 /* bitbang has to be first */
70 struct spi_bitbang bitbang;
71 struct completion done;
72
73 struct mpc83xx_spi_reg __iomem *base;
74
75 /* rx & tx bufs from the spi_transfer */
76 const void *tx;
77 void *rx;
78
79 /* functions to deal with different sized buffers */
80 void (*get_rx) (u32 rx_data, struct mpc83xx_spi *);
81 u32(*get_tx) (struct mpc83xx_spi *);
82
83 unsigned int count;
84 u32 irq;
85
86 unsigned nsecs; /* (clock cycle time)/2 */
87
88 u32 sysclk;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -070089 u32 rx_shift; /* RX data reg shift when in qe mode */
90 u32 tx_shift; /* TX data reg shift when in qe mode */
91
92 bool qe_mode;
93
Kumar Galaccf06992006-05-20 15:00:15 -070094 void (*activate_cs) (u8 cs, u8 polarity);
95 void (*deactivate_cs) (u8 cs, u8 polarity);
96};
97
98static inline void mpc83xx_spi_write_reg(__be32 __iomem * reg, u32 val)
99{
100 out_be32(reg, val);
101}
102
103static inline u32 mpc83xx_spi_read_reg(__be32 __iomem * reg)
104{
105 return in_be32(reg);
106}
107
108#define MPC83XX_SPI_RX_BUF(type) \
109void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \
110{ \
111 type * rx = mpc83xx_spi->rx; \
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700112 *rx++ = (type)(data >> mpc83xx_spi->rx_shift); \
Kumar Galaccf06992006-05-20 15:00:15 -0700113 mpc83xx_spi->rx = rx; \
114}
115
116#define MPC83XX_SPI_TX_BUF(type) \
117u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi) \
118{ \
119 u32 data; \
120 const type * tx = mpc83xx_spi->tx; \
David Brownell4b1badf2006-12-29 16:48:39 -0800121 if (!tx) \
122 return 0; \
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700123 data = *tx++ << mpc83xx_spi->tx_shift; \
Kumar Galaccf06992006-05-20 15:00:15 -0700124 mpc83xx_spi->tx = tx; \
125 return data; \
126}
127
128MPC83XX_SPI_RX_BUF(u8)
129MPC83XX_SPI_RX_BUF(u16)
130MPC83XX_SPI_RX_BUF(u32)
131MPC83XX_SPI_TX_BUF(u8)
132MPC83XX_SPI_TX_BUF(u16)
133MPC83XX_SPI_TX_BUF(u32)
134
135static void mpc83xx_spi_chipselect(struct spi_device *spi, int value)
136{
137 struct mpc83xx_spi *mpc83xx_spi;
138 u8 pol = spi->mode & SPI_CS_HIGH ? 1 : 0;
139
140 mpc83xx_spi = spi_master_get_devdata(spi->master);
141
142 if (value == BITBANG_CS_INACTIVE) {
143 if (mpc83xx_spi->deactivate_cs)
144 mpc83xx_spi->deactivate_cs(spi->chip_select, pol);
145 }
146
147 if (value == BITBANG_CS_ACTIVE) {
148 u32 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
149 u32 len = spi->bits_per_word;
150 if (len == 32)
151 len = 0;
152 else
153 len = len - 1;
154
155 /* mask out bits we are going to set */
Anton Vorontsov32421da2007-07-31 00:38:41 -0700156 regval &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
157 | SPMODE_LEN(0xF) | SPMODE_DIV16
158 | SPMODE_PM(0xF) | SPMODE_REV);
Kumar Galaccf06992006-05-20 15:00:15 -0700159
160 if (spi->mode & SPI_CPHA)
161 regval |= SPMODE_CP_BEGIN_EDGECLK;
162 if (spi->mode & SPI_CPOL)
163 regval |= SPMODE_CI_INACTIVEHIGH;
Anton Vorontsov32421da2007-07-31 00:38:41 -0700164 if (!(spi->mode & SPI_LSB_FIRST))
165 regval |= SPMODE_REV;
Kumar Galaccf06992006-05-20 15:00:15 -0700166
167 regval |= SPMODE_LEN(len);
168
169 if ((mpc83xx_spi->sysclk / spi->max_speed_hz) >= 64) {
170 u8 pm = mpc83xx_spi->sysclk / (spi->max_speed_hz * 64);
Clifford Wolf698ca472007-07-17 04:04:06 -0700171 if (pm > 0x0f) {
172 printk(KERN_WARNING "MPC83xx SPI: SPICLK can't be less then a SYSCLK/1024!\n"
173 "Requested SPICLK is %d Hz. Will use %d Hz instead.\n",
174 spi->max_speed_hz, mpc83xx_spi->sysclk / 1024);
175 pm = 0x0f;
176 }
Kumar Galaccf06992006-05-20 15:00:15 -0700177 regval |= SPMODE_PM(pm) | SPMODE_DIV16;
178 } else {
179 u8 pm = mpc83xx_spi->sysclk / (spi->max_speed_hz * 4);
180 regval |= SPMODE_PM(pm);
181 }
182
Anton Vorontsov49bb2302007-07-31 00:38:40 -0700183 /* Turn off SPI unit prior changing mode */
184 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
Kumar Galaccf06992006-05-20 15:00:15 -0700185 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
186 if (mpc83xx_spi->activate_cs)
187 mpc83xx_spi->activate_cs(spi->chip_select, pol);
188 }
189}
190
191static
192int mpc83xx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
193{
194 struct mpc83xx_spi *mpc83xx_spi;
195 u32 regval;
196 u8 bits_per_word;
197 u32 hz;
198
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 Tjernlundf29ba282007-07-17 04:04:12 -0700218 mpc83xx_spi->rx_shift = 0;
219 mpc83xx_spi->tx_shift = 0;
Kumar Galaccf06992006-05-20 15:00:15 -0700220 if (bits_per_word <= 8) {
221 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
222 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700223 if (mpc83xx_spi->qe_mode) {
224 mpc83xx_spi->rx_shift = 16;
225 mpc83xx_spi->tx_shift = 24;
226 }
Kumar Galaccf06992006-05-20 15:00:15 -0700227 } else if (bits_per_word <= 16) {
228 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u16;
229 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u16;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700230 if (mpc83xx_spi->qe_mode) {
231 mpc83xx_spi->rx_shift = 16;
232 mpc83xx_spi->tx_shift = 16;
233 }
Kumar Galaccf06992006-05-20 15:00:15 -0700234 } else if (bits_per_word <= 32) {
235 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u32;
236 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u32;
237 } else
238 return -EINVAL;
239
Anton Vorontsov35cc0b92007-07-31 00:38:42 -0700240 if (mpc83xx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) {
241 mpc83xx_spi->tx_shift = 0;
242 if (bits_per_word <= 8)
243 mpc83xx_spi->rx_shift = 8;
244 else
245 mpc83xx_spi->rx_shift = 0;
246 }
247
Kumar Galaccf06992006-05-20 15:00:15 -0700248 /* nsecs = (clock period)/2 */
249 if (!hz)
250 hz = spi->max_speed_hz;
251 mpc83xx_spi->nsecs = (1000000000 / 2) / hz;
252 if (mpc83xx_spi->nsecs > MAX_UDELAY_MS * 1000)
253 return -EINVAL;
254
255 if (bits_per_word == 32)
256 bits_per_word = 0;
257 else
258 bits_per_word = bits_per_word - 1;
259
260 regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
261
Anton Vorontsov32421da2007-07-31 00:38:41 -0700262 /* mask out bits we are going to set */
263 regval &= ~(SPMODE_LEN(0xF) | SPMODE_REV);
Kumar Galaccf06992006-05-20 15:00:15 -0700264 regval |= SPMODE_LEN(bits_per_word);
Anton Vorontsov32421da2007-07-31 00:38:41 -0700265 if (!(spi->mode & SPI_LSB_FIRST))
266 regval |= SPMODE_REV;
Kumar Galaccf06992006-05-20 15:00:15 -0700267
Anton Vorontsov49bb2302007-07-31 00:38:40 -0700268 /* Turn off SPI unit prior changing mode */
269 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
Kumar Galaccf06992006-05-20 15:00:15 -0700270 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
271
272 return 0;
273}
274
David Brownelldccd5732007-07-17 04:04:02 -0700275/* the spi->mode bits understood by this driver: */
Anton Vorontsov32421da2007-07-31 00:38:41 -0700276#define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST)
David Brownelldccd5732007-07-17 04:04:02 -0700277
Kumar Galaccf06992006-05-20 15:00:15 -0700278static int mpc83xx_spi_setup(struct spi_device *spi)
279{
280 struct spi_bitbang *bitbang;
281 struct mpc83xx_spi *mpc83xx_spi;
282 int retval;
283
David Brownelldccd5732007-07-17 04:04:02 -0700284 if (spi->mode & ~MODEBITS) {
285 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
286 spi->mode & ~MODEBITS);
287 return -EINVAL;
288 }
289
Kumar Galaccf06992006-05-20 15:00:15 -0700290 if (!spi->max_speed_hz)
291 return -EINVAL;
292
293 bitbang = spi_master_get_devdata(spi->master);
294 mpc83xx_spi = spi_master_get_devdata(spi->master);
295
296 if (!spi->bits_per_word)
297 spi->bits_per_word = 8;
298
299 retval = mpc83xx_spi_setup_transfer(spi, NULL);
300 if (retval < 0)
301 return retval;
302
303 dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec\n",
304 __FUNCTION__, spi->mode & (SPI_CPOL | SPI_CPHA),
305 spi->bits_per_word, 2 * mpc83xx_spi->nsecs);
306
307 /* NOTE we _need_ to call chipselect() early, ideally with adapter
308 * setup, unless the hardware defaults cooperate to avoid confusion
309 * between normal (active low) and inverted chipselects.
310 */
311
312 /* deselect chip (low or high) */
313 spin_lock(&bitbang->lock);
314 if (!bitbang->busy) {
315 bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
316 ndelay(mpc83xx_spi->nsecs);
317 }
318 spin_unlock(&bitbang->lock);
319
320 return 0;
321}
322
323static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
324{
325 struct mpc83xx_spi *mpc83xx_spi;
326 u32 word;
327
328 mpc83xx_spi = spi_master_get_devdata(spi->master);
329
330 mpc83xx_spi->tx = t->tx_buf;
331 mpc83xx_spi->rx = t->rx_buf;
332 mpc83xx_spi->count = t->len;
333 INIT_COMPLETION(mpc83xx_spi->done);
334
335 /* enable rx ints */
336 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE);
337
338 /* transmit word */
339 word = mpc83xx_spi->get_tx(mpc83xx_spi);
340 mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
341
342 wait_for_completion(&mpc83xx_spi->done);
343
344 /* disable rx ints */
345 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
346
347 return t->len - mpc83xx_spi->count;
348}
349
David Howells7d12e782006-10-05 14:55:46 +0100350irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data)
Kumar Galaccf06992006-05-20 15:00:15 -0700351{
352 struct mpc83xx_spi *mpc83xx_spi = context_data;
353 u32 event;
354 irqreturn_t ret = IRQ_NONE;
355
356 /* Get interrupt events(tx/rx) */
357 event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event);
358
359 /* We need handle RX first */
360 if (event & SPIE_NE) {
361 u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive);
362
363 if (mpc83xx_spi->rx)
364 mpc83xx_spi->get_rx(rx_data, mpc83xx_spi);
365
366 ret = IRQ_HANDLED;
367 }
368
369 if ((event & SPIE_NF) == 0)
370 /* spin until TX is done */
371 while (((event =
372 mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) &
373 SPIE_NF) == 0)
374 cpu_relax();
375
376 mpc83xx_spi->count -= 1;
377 if (mpc83xx_spi->count) {
378 if (mpc83xx_spi->tx) {
379 u32 word = mpc83xx_spi->get_tx(mpc83xx_spi);
380 mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit,
381 word);
382 }
383 } else {
384 complete(&mpc83xx_spi->done);
385 }
386
387 /* Clear the events */
388 mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event);
389
390 return ret;
391}
392
393static int __init mpc83xx_spi_probe(struct platform_device *dev)
394{
395 struct spi_master *master;
396 struct mpc83xx_spi *mpc83xx_spi;
397 struct fsl_spi_platform_data *pdata;
398 struct resource *r;
399 u32 regval;
400 int ret = 0;
401
402 /* Get resources(memory, IRQ) associated with the device */
403 master = spi_alloc_master(&dev->dev, sizeof(struct mpc83xx_spi));
404
405 if (master == NULL) {
406 ret = -ENOMEM;
407 goto err;
408 }
409
410 platform_set_drvdata(dev, master);
411 pdata = dev->dev.platform_data;
412
413 if (pdata == NULL) {
414 ret = -ENODEV;
415 goto free_master;
416 }
417
418 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
419 if (r == NULL) {
420 ret = -ENODEV;
421 goto free_master;
422 }
Kumar Galaccf06992006-05-20 15:00:15 -0700423 mpc83xx_spi = spi_master_get_devdata(master);
424 mpc83xx_spi->bitbang.master = spi_master_get(master);
425 mpc83xx_spi->bitbang.chipselect = mpc83xx_spi_chipselect;
426 mpc83xx_spi->bitbang.setup_transfer = mpc83xx_spi_setup_transfer;
427 mpc83xx_spi->bitbang.txrx_bufs = mpc83xx_spi_bufs;
428 mpc83xx_spi->sysclk = pdata->sysclk;
429 mpc83xx_spi->activate_cs = pdata->activate_cs;
430 mpc83xx_spi->deactivate_cs = pdata->deactivate_cs;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700431 mpc83xx_spi->qe_mode = pdata->qe_mode;
Kumar Galaccf06992006-05-20 15:00:15 -0700432 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
433 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
434
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700435 mpc83xx_spi->rx_shift = 0;
436 mpc83xx_spi->tx_shift = 0;
437 if (mpc83xx_spi->qe_mode) {
438 mpc83xx_spi->rx_shift = 16;
439 mpc83xx_spi->tx_shift = 24;
440 }
441
Kumar Galaccf06992006-05-20 15:00:15 -0700442 mpc83xx_spi->bitbang.master->setup = mpc83xx_spi_setup;
443 init_completion(&mpc83xx_spi->done);
444
445 mpc83xx_spi->base = ioremap(r->start, r->end - r->start + 1);
446 if (mpc83xx_spi->base == NULL) {
447 ret = -ENOMEM;
448 goto put_master;
449 }
450
451 mpc83xx_spi->irq = platform_get_irq(dev, 0);
452
453 if (mpc83xx_spi->irq < 0) {
454 ret = -ENXIO;
455 goto unmap_io;
456 }
457
458 /* Register for SPI Interrupt */
459 ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
460 0, "mpc83xx_spi", mpc83xx_spi);
461
462 if (ret != 0)
463 goto unmap_io;
464
465 master->bus_num = pdata->bus_num;
466 master->num_chipselect = pdata->max_chipselect;
467
468 /* SPI controller initializations */
469 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
470 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
471 mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0);
472 mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff);
473
474 /* Enable SPI interface */
475 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
Joakim Tjernlundf29ba282007-07-17 04:04:12 -0700476 if (pdata->qe_mode)
477 regval |= SPMODE_OP;
478
Kumar Galaccf06992006-05-20 15:00:15 -0700479 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
480
481 ret = spi_bitbang_start(&mpc83xx_spi->bitbang);
482
483 if (ret != 0)
484 goto free_irq;
485
486 printk(KERN_INFO
487 "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
488 dev->dev.bus_id, mpc83xx_spi->base, mpc83xx_spi->irq);
489
490 return ret;
491
492free_irq:
493 free_irq(mpc83xx_spi->irq, mpc83xx_spi);
494unmap_io:
495 iounmap(mpc83xx_spi->base);
496put_master:
497 spi_master_put(master);
498free_master:
499 kfree(master);
500err:
501 return ret;
502}
503
504static int __devexit mpc83xx_spi_remove(struct platform_device *dev)
505{
506 struct mpc83xx_spi *mpc83xx_spi;
507 struct spi_master *master;
508
509 master = platform_get_drvdata(dev);
510 mpc83xx_spi = spi_master_get_devdata(master);
511
512 spi_bitbang_stop(&mpc83xx_spi->bitbang);
513 free_irq(mpc83xx_spi->irq, mpc83xx_spi);
514 iounmap(mpc83xx_spi->base);
515 spi_master_put(mpc83xx_spi->bitbang.master);
516
517 return 0;
518}
519
520static struct platform_driver mpc83xx_spi_driver = {
521 .probe = mpc83xx_spi_probe,
522 .remove = __devexit_p(mpc83xx_spi_remove),
523 .driver = {
524 .name = "mpc83xx_spi",
525 },
526};
527
528static int __init mpc83xx_spi_init(void)
529{
530 return platform_driver_register(&mpc83xx_spi_driver);
531}
532
533static void __exit mpc83xx_spi_exit(void)
534{
535 platform_driver_unregister(&mpc83xx_spi_driver);
536}
537
538module_init(mpc83xx_spi_init);
539module_exit(mpc83xx_spi_exit);
540
541MODULE_AUTHOR("Kumar Gala");
542MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
543MODULE_LICENSE("GPL");