mpc52xx_spi: add gpio chipselect
[safe/jmp/linux-2.6] / drivers / spi / mpc52xx_spi.c
1 /*
2  * MPC52xx SPI bus driver.
3  *
4  * Copyright (C) 2008 Secret Lab Technologies Ltd.
5  *
6  * This file is released under the GPLv2
7  *
8  * This is the driver for the MPC5200's dedicated SPI controller.
9  *
10  * Note: this driver does not support the MPC5200 PSC in SPI mode.  For
11  * that driver see drivers/spi/mpc52xx_psc_spi.c
12  */
13
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/of_platform.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/mpc52xx_spi.h>
22 #include <linux/of_spi.h>
23 #include <linux/io.h>
24 #include <linux/of_gpio.h>
25 #include <asm/time.h>
26 #include <asm/mpc52xx.h>
27
28 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
29 MODULE_DESCRIPTION("MPC52xx SPI (non-PSC) Driver");
30 MODULE_LICENSE("GPL");
31
32 /* Register offsets */
33 #define SPI_CTRL1       0x00
34 #define SPI_CTRL1_SPIE          (1 << 7)
35 #define SPI_CTRL1_SPE           (1 << 6)
36 #define SPI_CTRL1_MSTR          (1 << 4)
37 #define SPI_CTRL1_CPOL          (1 << 3)
38 #define SPI_CTRL1_CPHA          (1 << 2)
39 #define SPI_CTRL1_SSOE          (1 << 1)
40 #define SPI_CTRL1_LSBFE         (1 << 0)
41
42 #define SPI_CTRL2       0x01
43 #define SPI_BRR         0x04
44
45 #define SPI_STATUS      0x05
46 #define SPI_STATUS_SPIF         (1 << 7)
47 #define SPI_STATUS_WCOL         (1 << 6)
48 #define SPI_STATUS_MODF         (1 << 4)
49
50 #define SPI_DATA        0x09
51 #define SPI_PORTDATA    0x0d
52 #define SPI_DATADIR     0x10
53
54 /* FSM state return values */
55 #define FSM_STOP        0       /* Nothing more for the state machine to */
56                                 /* do.  If something interesting happens */
57                                 /* then and IRQ will be received */
58 #define FSM_POLL        1       /* need to poll for completion, an IRQ is */
59                                 /* not expected */
60 #define FSM_CONTINUE    2       /* Keep iterating the state machine */
61
62 /* Driver internal data */
63 struct mpc52xx_spi {
64         struct spi_master *master;
65         u32 sysclk;
66         void __iomem *regs;
67         int irq0;       /* MODF irq */
68         int irq1;       /* SPIF irq */
69         int ipb_freq;
70
71         /* Statistics */
72         int msg_count;
73         int wcol_count;
74         int wcol_ticks;
75         u32 wcol_tx_timestamp;
76         int modf_count;
77         int byte_count;
78
79         struct list_head queue;         /* queue of pending messages */
80         spinlock_t lock;
81         struct work_struct work;
82
83         /* Details of current transfer (length, and buffer pointers) */
84         struct spi_message *message;    /* current message */
85         struct spi_transfer *transfer;  /* current transfer */
86         int (*state)(int irq, struct mpc52xx_spi *ms, u8 status, u8 data);
87         int len;
88         int timestamp;
89         u8 *rx_buf;
90         const u8 *tx_buf;
91         int cs_change;
92         int gpio_cs_count;
93         unsigned int *gpio_cs;
94 };
95
96 /*
97  * CS control function
98  */
99 static void mpc52xx_spi_chipsel(struct mpc52xx_spi *ms, int value)
100 {
101         int cs;
102
103         if (ms->gpio_cs_count > 0) {
104                 cs = ms->message->spi->chip_select;
105                 gpio_set_value(ms->gpio_cs[cs], value ? 0 : 1);
106         } else
107                 out_8(ms->regs + SPI_PORTDATA, value ? 0 : 0x08);
108 }
109
110 /*
111  * Start a new transfer.  This is called both by the idle state
112  * for the first transfer in a message, and by the wait state when the
113  * previous transfer in a message is complete.
114  */
115 static void mpc52xx_spi_start_transfer(struct mpc52xx_spi *ms)
116 {
117         ms->rx_buf = ms->transfer->rx_buf;
118         ms->tx_buf = ms->transfer->tx_buf;
119         ms->len = ms->transfer->len;
120
121         /* Activate the chip select */
122         if (ms->cs_change)
123                 mpc52xx_spi_chipsel(ms, 1);
124         ms->cs_change = ms->transfer->cs_change;
125
126         /* Write out the first byte */
127         ms->wcol_tx_timestamp = get_tbl();
128         if (ms->tx_buf)
129                 out_8(ms->regs + SPI_DATA, *ms->tx_buf++);
130         else
131                 out_8(ms->regs + SPI_DATA, 0);
132 }
133
134 /* Forward declaration of state handlers */
135 static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms,
136                                          u8 status, u8 data);
137 static int mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms,
138                                      u8 status, u8 data);
139
140 /*
141  * IDLE state
142  *
143  * No transfers are in progress; if another transfer is pending then retrieve
144  * it and kick it off.  Otherwise, stop processing the state machine
145  */
146 static int
147 mpc52xx_spi_fsmstate_idle(int irq, struct mpc52xx_spi *ms, u8 status, u8 data)
148 {
149         struct spi_device *spi;
150         int spr, sppr;
151         u8 ctrl1;
152
153         if (status && (irq != NO_IRQ))
154                 dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n",
155                         status);
156
157         /* Check if there is another transfer waiting. */
158         if (list_empty(&ms->queue))
159                 return FSM_STOP;
160
161         /* get the head of the queue */
162         ms->message = list_first_entry(&ms->queue, struct spi_message, queue);
163         list_del_init(&ms->message->queue);
164
165         /* Setup the controller parameters */
166         ctrl1 = SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR;
167         spi = ms->message->spi;
168         if (spi->mode & SPI_CPHA)
169                 ctrl1 |= SPI_CTRL1_CPHA;
170         if (spi->mode & SPI_CPOL)
171                 ctrl1 |= SPI_CTRL1_CPOL;
172         if (spi->mode & SPI_LSB_FIRST)
173                 ctrl1 |= SPI_CTRL1_LSBFE;
174         out_8(ms->regs + SPI_CTRL1, ctrl1);
175
176         /* Setup the controller speed */
177         /* minimum divider is '2'.  Also, add '1' to force rounding the
178          * divider up. */
179         sppr = ((ms->ipb_freq / ms->message->spi->max_speed_hz) + 1) >> 1;
180         spr = 0;
181         if (sppr < 1)
182                 sppr = 1;
183         while (((sppr - 1) & ~0x7) != 0) {
184                 sppr = (sppr + 1) >> 1; /* add '1' to force rounding up */
185                 spr++;
186         }
187         sppr--;         /* sppr quantity in register is offset by 1 */
188         if (spr > 7) {
189                 /* Don't overrun limits of SPI baudrate register */
190                 spr = 7;
191                 sppr = 7;
192         }
193         out_8(ms->regs + SPI_BRR, sppr << 4 | spr); /* Set speed */
194
195         ms->cs_change = 1;
196         ms->transfer = container_of(ms->message->transfers.next,
197                                     struct spi_transfer, transfer_list);
198
199         mpc52xx_spi_start_transfer(ms);
200         ms->state = mpc52xx_spi_fsmstate_transfer;
201
202         return FSM_CONTINUE;
203 }
204
205 /*
206  * TRANSFER state
207  *
208  * In the middle of a transfer.  If the SPI core has completed processing
209  * a byte, then read out the received data and write out the next byte
210  * (unless this transfer is finished; in which case go on to the wait
211  * state)
212  */
213 static int mpc52xx_spi_fsmstate_transfer(int irq, struct mpc52xx_spi *ms,
214                                          u8 status, u8 data)
215 {
216         if (!status)
217                 return ms->irq0 ? FSM_STOP : FSM_POLL;
218
219         if (status & SPI_STATUS_WCOL) {
220                 /* The SPI controller is stoopid.  At slower speeds, it may
221                  * raise the SPIF flag before the state machine is actually
222                  * finished, which causes a collision (internal to the state
223                  * machine only).  The manual recommends inserting a delay
224                  * between receiving the interrupt and sending the next byte,
225                  * but it can also be worked around simply by retrying the
226                  * transfer which is what we do here. */
227                 ms->wcol_count++;
228                 ms->wcol_ticks += get_tbl() - ms->wcol_tx_timestamp;
229                 ms->wcol_tx_timestamp = get_tbl();
230                 data = 0;
231                 if (ms->tx_buf)
232                         data = *(ms->tx_buf-1);
233                 out_8(ms->regs + SPI_DATA, data); /* try again */
234                 return FSM_CONTINUE;
235         } else if (status & SPI_STATUS_MODF) {
236                 ms->modf_count++;
237                 dev_err(&ms->master->dev, "mode fault\n");
238                 mpc52xx_spi_chipsel(ms, 0);
239                 ms->message->status = -EIO;
240                 ms->message->complete(ms->message->context);
241                 ms->state = mpc52xx_spi_fsmstate_idle;
242                 return FSM_CONTINUE;
243         }
244
245         /* Read data out of the spi device */
246         ms->byte_count++;
247         if (ms->rx_buf)
248                 *ms->rx_buf++ = data;
249
250         /* Is the transfer complete? */
251         ms->len--;
252         if (ms->len == 0) {
253                 ms->timestamp = get_tbl();
254                 ms->timestamp += ms->transfer->delay_usecs * tb_ticks_per_usec;
255                 ms->state = mpc52xx_spi_fsmstate_wait;
256                 return FSM_CONTINUE;
257         }
258
259         /* Write out the next byte */
260         ms->wcol_tx_timestamp = get_tbl();
261         if (ms->tx_buf)
262                 out_8(ms->regs + SPI_DATA, *ms->tx_buf++);
263         else
264                 out_8(ms->regs + SPI_DATA, 0);
265
266         return FSM_CONTINUE;
267 }
268
269 /*
270  * WAIT state
271  *
272  * A transfer has completed; need to wait for the delay period to complete
273  * before starting the next transfer
274  */
275 static int
276 mpc52xx_spi_fsmstate_wait(int irq, struct mpc52xx_spi *ms, u8 status, u8 data)
277 {
278         if (status && irq)
279                 dev_err(&ms->master->dev, "spurious irq, status=0x%.2x\n",
280                         status);
281
282         if (((int)get_tbl()) - ms->timestamp < 0)
283                 return FSM_POLL;
284
285         ms->message->actual_length += ms->transfer->len;
286
287         /* Check if there is another transfer in this message.  If there
288          * aren't then deactivate CS, notify sender, and drop back to idle
289          * to start the next message. */
290         if (ms->transfer->transfer_list.next == &ms->message->transfers) {
291                 ms->msg_count++;
292                 mpc52xx_spi_chipsel(ms, 0);
293                 ms->message->status = 0;
294                 ms->message->complete(ms->message->context);
295                 ms->state = mpc52xx_spi_fsmstate_idle;
296                 return FSM_CONTINUE;
297         }
298
299         /* There is another transfer; kick it off */
300
301         if (ms->cs_change)
302                 mpc52xx_spi_chipsel(ms, 0);
303
304         ms->transfer = container_of(ms->transfer->transfer_list.next,
305                                     struct spi_transfer, transfer_list);
306         mpc52xx_spi_start_transfer(ms);
307         ms->state = mpc52xx_spi_fsmstate_transfer;
308         return FSM_CONTINUE;
309 }
310
311 /**
312  * mpc52xx_spi_fsm_process - Finite State Machine iteration function
313  * @irq: irq number that triggered the FSM or 0 for polling
314  * @ms: pointer to mpc52xx_spi driver data
315  */
316 static void mpc52xx_spi_fsm_process(int irq, struct mpc52xx_spi *ms)
317 {
318         int rc = FSM_CONTINUE;
319         u8 status, data;
320
321         while (rc == FSM_CONTINUE) {
322                 /* Interrupt cleared by read of STATUS followed by
323                  * read of DATA registers */
324                 status = in_8(ms->regs + SPI_STATUS);
325                 data = in_8(ms->regs + SPI_DATA);
326                 rc = ms->state(irq, ms, status, data);
327         }
328
329         if (rc == FSM_POLL)
330                 schedule_work(&ms->work);
331 }
332
333 /**
334  * mpc52xx_spi_irq - IRQ handler
335  */
336 static irqreturn_t mpc52xx_spi_irq(int irq, void *_ms)
337 {
338         struct mpc52xx_spi *ms = _ms;
339         spin_lock(&ms->lock);
340         mpc52xx_spi_fsm_process(irq, ms);
341         spin_unlock(&ms->lock);
342         return IRQ_HANDLED;
343 }
344
345 /**
346  * mpc52xx_spi_wq - Workqueue function for polling the state machine
347  */
348 static void mpc52xx_spi_wq(struct work_struct *work)
349 {
350         struct mpc52xx_spi *ms = container_of(work, struct mpc52xx_spi, work);
351         unsigned long flags;
352
353         spin_lock_irqsave(&ms->lock, flags);
354         mpc52xx_spi_fsm_process(0, ms);
355         spin_unlock_irqrestore(&ms->lock, flags);
356 }
357
358 /*
359  * spi_master ops
360  */
361
362 static int mpc52xx_spi_setup(struct spi_device *spi)
363 {
364         if (spi->bits_per_word % 8)
365                 return -EINVAL;
366
367         if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST))
368                 return -EINVAL;
369
370         if (spi->chip_select >= spi->master->num_chipselect)
371                 return -EINVAL;
372
373         return 0;
374 }
375
376 static int mpc52xx_spi_transfer(struct spi_device *spi, struct spi_message *m)
377 {
378         struct mpc52xx_spi *ms = spi_master_get_devdata(spi->master);
379         unsigned long flags;
380
381         m->actual_length = 0;
382         m->status = -EINPROGRESS;
383
384         spin_lock_irqsave(&ms->lock, flags);
385         list_add_tail(&m->queue, &ms->queue);
386         spin_unlock_irqrestore(&ms->lock, flags);
387         schedule_work(&ms->work);
388
389         return 0;
390 }
391
392 /*
393  * OF Platform Bus Binding
394  */
395 static int __devinit mpc52xx_spi_probe(struct of_device *op,
396                                        const struct of_device_id *match)
397 {
398         struct spi_master *master;
399         struct mpc52xx_spi *ms;
400         void __iomem *regs;
401         u8 ctrl1;
402         int rc, i = 0;
403         int gpio_cs;
404
405         /* MMIO registers */
406         dev_dbg(&op->dev, "probing mpc5200 SPI device\n");
407         regs = of_iomap(op->node, 0);
408         if (!regs)
409                 return -ENODEV;
410
411         /* initialize the device */
412         ctrl1 = SPI_CTRL1_SPIE | SPI_CTRL1_SPE | SPI_CTRL1_MSTR;
413         out_8(regs + SPI_CTRL1, ctrl1);
414         out_8(regs + SPI_CTRL2, 0x0);
415         out_8(regs + SPI_DATADIR, 0xe); /* Set output pins */
416         out_8(regs + SPI_PORTDATA, 0x8);        /* Deassert /SS signal */
417
418         /* Clear the status register and re-read it to check for a MODF
419          * failure.  This driver cannot currently handle multiple masters
420          * on the SPI bus.  This fault will also occur if the SPI signals
421          * are not connected to any pins (port_config setting) */
422         in_8(regs + SPI_STATUS);
423         out_8(regs + SPI_CTRL1, ctrl1);
424
425         in_8(regs + SPI_DATA);
426         if (in_8(regs + SPI_STATUS) & SPI_STATUS_MODF) {
427                 dev_err(&op->dev, "mode fault; is port_config correct?\n");
428                 rc = -EIO;
429                 goto err_init;
430         }
431
432         dev_dbg(&op->dev, "allocating spi_master struct\n");
433         master = spi_alloc_master(&op->dev, sizeof *ms);
434         if (!master) {
435                 rc = -ENOMEM;
436                 goto err_alloc;
437         }
438
439         master->bus_num = -1;
440         master->setup = mpc52xx_spi_setup;
441         master->transfer = mpc52xx_spi_transfer;
442         master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
443
444         dev_set_drvdata(&op->dev, master);
445
446         ms = spi_master_get_devdata(master);
447         ms->master = master;
448         ms->regs = regs;
449         ms->irq0 = irq_of_parse_and_map(op->node, 0);
450         ms->irq1 = irq_of_parse_and_map(op->node, 1);
451         ms->state = mpc52xx_spi_fsmstate_idle;
452         ms->ipb_freq = mpc5xxx_get_bus_frequency(op->node);
453         ms->gpio_cs_count = of_gpio_count(op->node);
454         if (ms->gpio_cs_count > 0) {
455                 master->num_chipselect = ms->gpio_cs_count;
456                 ms->gpio_cs = kmalloc(ms->gpio_cs_count * sizeof(unsigned int),
457                                 GFP_KERNEL);
458                 if (!ms->gpio_cs) {
459                         rc = -ENOMEM;
460                         goto err_alloc;
461                 }
462
463                 for (i = 0; i < ms->gpio_cs_count; i++) {
464                         gpio_cs = of_get_gpio(op->node, i);
465                         if (gpio_cs < 0) {
466                                 dev_err(&op->dev,
467                                         "could not parse the gpio field "
468                                         "in oftree\n");
469                                 rc = -ENODEV;
470                                 goto err_gpio;
471                         }
472
473                         rc = gpio_request(gpio_cs, dev_name(&op->dev));
474                         if (rc) {
475                                 dev_err(&op->dev,
476                                         "can't request spi cs gpio #%d "
477                                         "on gpio line %d\n", i, gpio_cs);
478                                 goto err_gpio;
479                         }
480
481                         gpio_direction_output(gpio_cs, 1);
482                         ms->gpio_cs[i] = gpio_cs;
483                 }
484         } else
485                 master->num_chipselect = 1;
486
487         spin_lock_init(&ms->lock);
488         INIT_LIST_HEAD(&ms->queue);
489         INIT_WORK(&ms->work, mpc52xx_spi_wq);
490
491         /* Decide if interrupts can be used */
492         if (ms->irq0 && ms->irq1) {
493                 rc = request_irq(ms->irq0, mpc52xx_spi_irq, IRQF_SAMPLE_RANDOM,
494                                   "mpc5200-spi-modf", ms);
495                 rc |= request_irq(ms->irq1, mpc52xx_spi_irq, IRQF_SAMPLE_RANDOM,
496                                   "mpc5200-spi-spiF", ms);
497                 if (rc) {
498                         free_irq(ms->irq0, ms);
499                         free_irq(ms->irq1, ms);
500                         ms->irq0 = ms->irq1 = 0;
501                 }
502         } else {
503                 /* operate in polled mode */
504                 ms->irq0 = ms->irq1 = 0;
505         }
506
507         if (!ms->irq0)
508                 dev_info(&op->dev, "using polled mode\n");
509
510         dev_dbg(&op->dev, "registering spi_master struct\n");
511         rc = spi_register_master(master);
512         if (rc)
513                 goto err_register;
514
515         of_register_spi_devices(master, op->node);
516         dev_info(&ms->master->dev, "registered MPC5200 SPI bus\n");
517
518         return rc;
519
520  err_register:
521         dev_err(&ms->master->dev, "initialization failed\n");
522         spi_master_put(master);
523  err_gpio:
524         while (i-- > 0)
525                 gpio_free(ms->gpio_cs[i]);
526
527         if (ms->gpio_cs != NULL)
528                 kfree(ms->gpio_cs);
529  err_alloc:
530  err_init:
531         iounmap(regs);
532         return rc;
533 }
534
535 static int __devexit mpc52xx_spi_remove(struct of_device *op)
536 {
537         struct spi_master *master = dev_get_drvdata(&op->dev);
538         struct mpc52xx_spi *ms = spi_master_get_devdata(master);
539         int i;
540
541         free_irq(ms->irq0, ms);
542         free_irq(ms->irq1, ms);
543
544         for (i = 0; i < ms->gpio_cs_count; i++)
545                 gpio_free(ms->gpio_cs[i]);
546
547         if (ms->gpio_cs != NULL)
548                 kfree(ms->gpio_cs);
549
550         spi_unregister_master(master);
551         spi_master_put(master);
552         iounmap(ms->regs);
553
554         return 0;
555 }
556
557 static struct of_device_id mpc52xx_spi_match[] __devinitdata = {
558         { .compatible = "fsl,mpc5200-spi", },
559         {}
560 };
561 MODULE_DEVICE_TABLE(of, mpc52xx_spi_match);
562
563 static struct of_platform_driver mpc52xx_spi_of_driver = {
564         .owner = THIS_MODULE,
565         .name = "mpc52xx-spi",
566         .match_table = mpc52xx_spi_match,
567         .probe = mpc52xx_spi_probe,
568         .remove = __exit_p(mpc52xx_spi_remove),
569 };
570
571 static int __init mpc52xx_spi_init(void)
572 {
573         return of_register_platform_driver(&mpc52xx_spi_of_driver);
574 }
575 module_init(mpc52xx_spi_init);
576
577 static void __exit mpc52xx_spi_exit(void)
578 {
579         of_unregister_platform_driver(&mpc52xx_spi_of_driver);
580 }
581 module_exit(mpc52xx_spi_exit);
582