]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/spi/spi_mpc83xx.c
spi_mpc83xx: turn off SPI unit while switching mode
[mv-sheeva.git] / drivers / spi / spi_mpc83xx.c
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>
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 */
31 struct 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)
50 #define SPMODE_OP               (1 << 14)
51
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. */
68 struct 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;
89         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
94         void (*activate_cs) (u8 cs, u8 polarity);
95         void (*deactivate_cs) (u8 cs, u8 polarity);
96 };
97
98 static inline void mpc83xx_spi_write_reg(__be32 __iomem * reg, u32 val)
99 {
100         out_be32(reg, val);
101 }
102
103 static inline u32 mpc83xx_spi_read_reg(__be32 __iomem * reg)
104 {
105         return in_be32(reg);
106 }
107
108 #define MPC83XX_SPI_RX_BUF(type)                                          \
109 void mpc83xx_spi_rx_buf_##type(u32 data, struct mpc83xx_spi *mpc83xx_spi) \
110 {                                                                         \
111         type * rx = mpc83xx_spi->rx;                                      \
112         *rx++ = (type)(data >> mpc83xx_spi->rx_shift);                    \
113         mpc83xx_spi->rx = rx;                                             \
114 }
115
116 #define MPC83XX_SPI_TX_BUF(type)                                \
117 u32 mpc83xx_spi_tx_buf_##type(struct mpc83xx_spi *mpc83xx_spi)  \
118 {                                                               \
119         u32 data;                                               \
120         const type * tx = mpc83xx_spi->tx;                      \
121         if (!tx)                                                \
122                 return 0;                                       \
123         data = *tx++ << mpc83xx_spi->tx_shift;                  \
124         mpc83xx_spi->tx = tx;                                   \
125         return data;                                            \
126 }
127
128 MPC83XX_SPI_RX_BUF(u8)
129 MPC83XX_SPI_RX_BUF(u16)
130 MPC83XX_SPI_RX_BUF(u32)
131 MPC83XX_SPI_TX_BUF(u8)
132 MPC83XX_SPI_TX_BUF(u16)
133 MPC83XX_SPI_TX_BUF(u32)
134
135 static 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 */
156                 regval &= ~0x38ff0000;
157
158                 if (spi->mode & SPI_CPHA)
159                         regval |= SPMODE_CP_BEGIN_EDGECLK;
160                 if (spi->mode & SPI_CPOL)
161                         regval |= SPMODE_CI_INACTIVEHIGH;
162
163                 regval |= SPMODE_LEN(len);
164
165                 if ((mpc83xx_spi->sysclk / spi->max_speed_hz) >= 64) {
166                         u8 pm = mpc83xx_spi->sysclk / (spi->max_speed_hz * 64);
167                         if (pm > 0x0f) {
168                                 printk(KERN_WARNING "MPC83xx SPI: SPICLK can't be less then a SYSCLK/1024!\n"
169                                                 "Requested SPICLK is %d Hz. Will use %d Hz instead.\n",
170                                                 spi->max_speed_hz, mpc83xx_spi->sysclk / 1024);
171                                 pm = 0x0f;
172                         }
173                         regval |= SPMODE_PM(pm) | SPMODE_DIV16;
174                 } else {
175                         u8 pm = mpc83xx_spi->sysclk / (spi->max_speed_hz * 4);
176                         regval |= SPMODE_PM(pm);
177                 }
178
179                 /* Turn off SPI unit prior changing mode */
180                 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
181                 mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
182                 if (mpc83xx_spi->activate_cs)
183                         mpc83xx_spi->activate_cs(spi->chip_select, pol);
184         }
185 }
186
187 static
188 int mpc83xx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
189 {
190         struct mpc83xx_spi *mpc83xx_spi;
191         u32 regval;
192         u8 bits_per_word;
193         u32 hz;
194
195         mpc83xx_spi = spi_master_get_devdata(spi->master);
196
197         if (t) {
198                 bits_per_word = t->bits_per_word;
199                 hz = t->speed_hz;
200         } else {
201                 bits_per_word = 0;
202                 hz = 0;
203         }
204
205         /* spi_transfer level calls that work per-word */
206         if (!bits_per_word)
207                 bits_per_word = spi->bits_per_word;
208
209         /* Make sure its a bit width we support [4..16, 32] */
210         if ((bits_per_word < 4)
211             || ((bits_per_word > 16) && (bits_per_word != 32)))
212                 return -EINVAL;
213
214         mpc83xx_spi->rx_shift = 0;
215         mpc83xx_spi->tx_shift = 0;
216         if (bits_per_word <= 8) {
217                 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
218                 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
219                 if (mpc83xx_spi->qe_mode) {
220                         mpc83xx_spi->rx_shift = 16;
221                         mpc83xx_spi->tx_shift = 24;
222                 }
223         } else if (bits_per_word <= 16) {
224                 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u16;
225                 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u16;
226                 if (mpc83xx_spi->qe_mode) {
227                         mpc83xx_spi->rx_shift = 16;
228                         mpc83xx_spi->tx_shift = 16;
229                 }
230         } else if (bits_per_word <= 32) {
231                 mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u32;
232                 mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u32;
233         } else
234                 return -EINVAL;
235
236         /* nsecs = (clock period)/2 */
237         if (!hz)
238                 hz = spi->max_speed_hz;
239         mpc83xx_spi->nsecs = (1000000000 / 2) / hz;
240         if (mpc83xx_spi->nsecs > MAX_UDELAY_MS * 1000)
241                 return -EINVAL;
242
243         if (bits_per_word == 32)
244                 bits_per_word = 0;
245         else
246                 bits_per_word = bits_per_word - 1;
247
248         regval = mpc83xx_spi_read_reg(&mpc83xx_spi->base->mode);
249
250         /* Mask out bits_per_wordgth */
251         regval &= 0xff0fffff;
252         regval |= SPMODE_LEN(bits_per_word);
253
254         /* Turn off SPI unit prior changing mode */
255         mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
256         mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
257
258         return 0;
259 }
260
261 /* the spi->mode bits understood by this driver: */
262 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
263
264 static int mpc83xx_spi_setup(struct spi_device *spi)
265 {
266         struct spi_bitbang *bitbang;
267         struct mpc83xx_spi *mpc83xx_spi;
268         int retval;
269
270         if (spi->mode & ~MODEBITS) {
271                 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
272                         spi->mode & ~MODEBITS);
273                 return -EINVAL;
274         }
275
276         if (!spi->max_speed_hz)
277                 return -EINVAL;
278
279         bitbang = spi_master_get_devdata(spi->master);
280         mpc83xx_spi = spi_master_get_devdata(spi->master);
281
282         if (!spi->bits_per_word)
283                 spi->bits_per_word = 8;
284
285         retval = mpc83xx_spi_setup_transfer(spi, NULL);
286         if (retval < 0)
287                 return retval;
288
289         dev_dbg(&spi->dev, "%s, mode %d, %u bits/w, %u nsec\n",
290                 __FUNCTION__, spi->mode & (SPI_CPOL | SPI_CPHA),
291                 spi->bits_per_word, 2 * mpc83xx_spi->nsecs);
292
293         /* NOTE we _need_ to call chipselect() early, ideally with adapter
294          * setup, unless the hardware defaults cooperate to avoid confusion
295          * between normal (active low) and inverted chipselects.
296          */
297
298         /* deselect chip (low or high) */
299         spin_lock(&bitbang->lock);
300         if (!bitbang->busy) {
301                 bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
302                 ndelay(mpc83xx_spi->nsecs);
303         }
304         spin_unlock(&bitbang->lock);
305
306         return 0;
307 }
308
309 static int mpc83xx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
310 {
311         struct mpc83xx_spi *mpc83xx_spi;
312         u32 word;
313
314         mpc83xx_spi = spi_master_get_devdata(spi->master);
315
316         mpc83xx_spi->tx = t->tx_buf;
317         mpc83xx_spi->rx = t->rx_buf;
318         mpc83xx_spi->count = t->len;
319         INIT_COMPLETION(mpc83xx_spi->done);
320
321         /* enable rx ints */
322         mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, SPIM_NE);
323
324         /* transmit word */
325         word = mpc83xx_spi->get_tx(mpc83xx_spi);
326         mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit, word);
327
328         wait_for_completion(&mpc83xx_spi->done);
329
330         /* disable rx ints */
331         mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
332
333         return t->len - mpc83xx_spi->count;
334 }
335
336 irqreturn_t mpc83xx_spi_irq(s32 irq, void *context_data)
337 {
338         struct mpc83xx_spi *mpc83xx_spi = context_data;
339         u32 event;
340         irqreturn_t ret = IRQ_NONE;
341
342         /* Get interrupt events(tx/rx) */
343         event = mpc83xx_spi_read_reg(&mpc83xx_spi->base->event);
344
345         /* We need handle RX first */
346         if (event & SPIE_NE) {
347                 u32 rx_data = mpc83xx_spi_read_reg(&mpc83xx_spi->base->receive);
348
349                 if (mpc83xx_spi->rx)
350                         mpc83xx_spi->get_rx(rx_data, mpc83xx_spi);
351
352                 ret = IRQ_HANDLED;
353         }
354
355         if ((event & SPIE_NF) == 0)
356                 /* spin until TX is done */
357                 while (((event =
358                          mpc83xx_spi_read_reg(&mpc83xx_spi->base->event)) &
359                                                 SPIE_NF) == 0)
360                          cpu_relax();
361
362         mpc83xx_spi->count -= 1;
363         if (mpc83xx_spi->count) {
364                 if (mpc83xx_spi->tx) {
365                         u32 word = mpc83xx_spi->get_tx(mpc83xx_spi);
366                         mpc83xx_spi_write_reg(&mpc83xx_spi->base->transmit,
367                                               word);
368                 }
369         } else {
370                 complete(&mpc83xx_spi->done);
371         }
372
373         /* Clear the events */
374         mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, event);
375
376         return ret;
377 }
378
379 static int __init mpc83xx_spi_probe(struct platform_device *dev)
380 {
381         struct spi_master *master;
382         struct mpc83xx_spi *mpc83xx_spi;
383         struct fsl_spi_platform_data *pdata;
384         struct resource *r;
385         u32 regval;
386         int ret = 0;
387
388         /* Get resources(memory, IRQ) associated with the device */
389         master = spi_alloc_master(&dev->dev, sizeof(struct mpc83xx_spi));
390
391         if (master == NULL) {
392                 ret = -ENOMEM;
393                 goto err;
394         }
395
396         platform_set_drvdata(dev, master);
397         pdata = dev->dev.platform_data;
398
399         if (pdata == NULL) {
400                 ret = -ENODEV;
401                 goto free_master;
402         }
403
404         r = platform_get_resource(dev, IORESOURCE_MEM, 0);
405         if (r == NULL) {
406                 ret = -ENODEV;
407                 goto free_master;
408         }
409         mpc83xx_spi = spi_master_get_devdata(master);
410         mpc83xx_spi->bitbang.master = spi_master_get(master);
411         mpc83xx_spi->bitbang.chipselect = mpc83xx_spi_chipselect;
412         mpc83xx_spi->bitbang.setup_transfer = mpc83xx_spi_setup_transfer;
413         mpc83xx_spi->bitbang.txrx_bufs = mpc83xx_spi_bufs;
414         mpc83xx_spi->sysclk = pdata->sysclk;
415         mpc83xx_spi->activate_cs = pdata->activate_cs;
416         mpc83xx_spi->deactivate_cs = pdata->deactivate_cs;
417         mpc83xx_spi->qe_mode = pdata->qe_mode;
418         mpc83xx_spi->get_rx = mpc83xx_spi_rx_buf_u8;
419         mpc83xx_spi->get_tx = mpc83xx_spi_tx_buf_u8;
420
421         mpc83xx_spi->rx_shift = 0;
422         mpc83xx_spi->tx_shift = 0;
423         if (mpc83xx_spi->qe_mode) {
424                 mpc83xx_spi->rx_shift = 16;
425                 mpc83xx_spi->tx_shift = 24;
426         }
427
428         mpc83xx_spi->bitbang.master->setup = mpc83xx_spi_setup;
429         init_completion(&mpc83xx_spi->done);
430
431         mpc83xx_spi->base = ioremap(r->start, r->end - r->start + 1);
432         if (mpc83xx_spi->base == NULL) {
433                 ret = -ENOMEM;
434                 goto put_master;
435         }
436
437         mpc83xx_spi->irq = platform_get_irq(dev, 0);
438
439         if (mpc83xx_spi->irq < 0) {
440                 ret = -ENXIO;
441                 goto unmap_io;
442         }
443
444         /* Register for SPI Interrupt */
445         ret = request_irq(mpc83xx_spi->irq, mpc83xx_spi_irq,
446                           0, "mpc83xx_spi", mpc83xx_spi);
447
448         if (ret != 0)
449                 goto unmap_io;
450
451         master->bus_num = pdata->bus_num;
452         master->num_chipselect = pdata->max_chipselect;
453
454         /* SPI controller initializations */
455         mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, 0);
456         mpc83xx_spi_write_reg(&mpc83xx_spi->base->mask, 0);
457         mpc83xx_spi_write_reg(&mpc83xx_spi->base->command, 0);
458         mpc83xx_spi_write_reg(&mpc83xx_spi->base->event, 0xffffffff);
459
460         /* Enable SPI interface */
461         regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
462         if (pdata->qe_mode)
463                 regval |= SPMODE_OP;
464
465         mpc83xx_spi_write_reg(&mpc83xx_spi->base->mode, regval);
466
467         ret = spi_bitbang_start(&mpc83xx_spi->bitbang);
468
469         if (ret != 0)
470                 goto free_irq;
471
472         printk(KERN_INFO
473                "%s: MPC83xx SPI Controller driver at 0x%p (irq = %d)\n",
474                dev->dev.bus_id, mpc83xx_spi->base, mpc83xx_spi->irq);
475
476         return ret;
477
478 free_irq:
479         free_irq(mpc83xx_spi->irq, mpc83xx_spi);
480 unmap_io:
481         iounmap(mpc83xx_spi->base);
482 put_master:
483         spi_master_put(master);
484 free_master:
485         kfree(master);
486 err:
487         return ret;
488 }
489
490 static int __devexit mpc83xx_spi_remove(struct platform_device *dev)
491 {
492         struct mpc83xx_spi *mpc83xx_spi;
493         struct spi_master *master;
494
495         master = platform_get_drvdata(dev);
496         mpc83xx_spi = spi_master_get_devdata(master);
497
498         spi_bitbang_stop(&mpc83xx_spi->bitbang);
499         free_irq(mpc83xx_spi->irq, mpc83xx_spi);
500         iounmap(mpc83xx_spi->base);
501         spi_master_put(mpc83xx_spi->bitbang.master);
502
503         return 0;
504 }
505
506 static struct platform_driver mpc83xx_spi_driver = {
507         .probe = mpc83xx_spi_probe,
508         .remove = __devexit_p(mpc83xx_spi_remove),
509         .driver = {
510                    .name = "mpc83xx_spi",
511         },
512 };
513
514 static int __init mpc83xx_spi_init(void)
515 {
516         return platform_driver_register(&mpc83xx_spi_driver);
517 }
518
519 static void __exit mpc83xx_spi_exit(void)
520 {
521         platform_driver_unregister(&mpc83xx_spi_driver);
522 }
523
524 module_init(mpc83xx_spi_init);
525 module_exit(mpc83xx_spi_exit);
526
527 MODULE_AUTHOR("Kumar Gala");
528 MODULE_DESCRIPTION("Simple MPC83xx SPI Driver");
529 MODULE_LICENSE("GPL");