Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / spi / spi-s3c24xx.c
1 /*
2  * Copyright (c) 2006 Ben Dooks
3  * Copyright 2006-2009 Simtec Electronics
4  *      Ben Dooks <ben@simtec.co.uk>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10 */
11
12 #include <linux/spinlock.h>
13 #include <linux/workqueue.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/clk.h>
19 #include <linux/platform_device.h>
20 #include <linux/gpio.h>
21 #include <linux/io.h>
22 #include <linux/slab.h>
23
24 #include <linux/spi/spi.h>
25 #include <linux/spi/spi_bitbang.h>
26 #include <linux/spi/s3c24xx.h>
27 #include <linux/module.h>
28
29 #include <plat/regs-spi.h>
30
31 #include <asm/fiq.h>
32
33 #include "spi-s3c24xx-fiq.h"
34
35 /**
36  * s3c24xx_spi_devstate - per device data
37  * @hz: Last frequency calculated for @sppre field.
38  * @mode: Last mode setting for the @spcon field.
39  * @spcon: Value to write to the SPCON register.
40  * @sppre: Value to write to the SPPRE register.
41  */
42 struct s3c24xx_spi_devstate {
43         unsigned int    hz;
44         unsigned int    mode;
45         u8              spcon;
46         u8              sppre;
47 };
48
49 enum spi_fiq_mode {
50         FIQ_MODE_NONE   = 0,
51         FIQ_MODE_TX     = 1,
52         FIQ_MODE_RX     = 2,
53         FIQ_MODE_TXRX   = 3,
54 };
55
56 struct s3c24xx_spi {
57         /* bitbang has to be first */
58         struct spi_bitbang       bitbang;
59         struct completion        done;
60
61         void __iomem            *regs;
62         int                      irq;
63         int                      len;
64         int                      count;
65
66         struct fiq_handler       fiq_handler;
67         enum spi_fiq_mode        fiq_mode;
68         unsigned char            fiq_inuse;
69         unsigned char            fiq_claimed;
70
71         void                    (*set_cs)(struct s3c2410_spi_info *spi,
72                                           int cs, int pol);
73
74         /* data buffers */
75         const unsigned char     *tx;
76         unsigned char           *rx;
77
78         struct clk              *clk;
79         struct spi_master       *master;
80         struct spi_device       *curdev;
81         struct device           *dev;
82         struct s3c2410_spi_info *pdata;
83 };
84
85 #define SPCON_DEFAULT (S3C2410_SPCON_MSTR | S3C2410_SPCON_SMOD_INT)
86 #define SPPIN_DEFAULT (S3C2410_SPPIN_KEEP)
87
88 static inline struct s3c24xx_spi *to_hw(struct spi_device *sdev)
89 {
90         return spi_master_get_devdata(sdev->master);
91 }
92
93 static void s3c24xx_spi_gpiocs(struct s3c2410_spi_info *spi, int cs, int pol)
94 {
95         gpio_set_value(spi->pin_cs, pol);
96 }
97
98 static void s3c24xx_spi_chipsel(struct spi_device *spi, int value)
99 {
100         struct s3c24xx_spi_devstate *cs = spi->controller_state;
101         struct s3c24xx_spi *hw = to_hw(spi);
102         unsigned int cspol = spi->mode & SPI_CS_HIGH ? 1 : 0;
103
104         /* change the chipselect state and the state of the spi engine clock */
105
106         switch (value) {
107         case BITBANG_CS_INACTIVE:
108                 hw->set_cs(hw->pdata, spi->chip_select, cspol^1);
109                 writeb(cs->spcon, hw->regs + S3C2410_SPCON);
110                 break;
111
112         case BITBANG_CS_ACTIVE:
113                 writeb(cs->spcon | S3C2410_SPCON_ENSCK,
114                        hw->regs + S3C2410_SPCON);
115                 hw->set_cs(hw->pdata, spi->chip_select, cspol);
116                 break;
117         }
118 }
119
120 static int s3c24xx_spi_update_state(struct spi_device *spi,
121                                     struct spi_transfer *t)
122 {
123         struct s3c24xx_spi *hw = to_hw(spi);
124         struct s3c24xx_spi_devstate *cs = spi->controller_state;
125         unsigned int bpw;
126         unsigned int hz;
127         unsigned int div;
128         unsigned long clk;
129
130         bpw = t ? t->bits_per_word : spi->bits_per_word;
131         hz  = t ? t->speed_hz : spi->max_speed_hz;
132
133         if (!bpw)
134                 bpw = 8;
135
136         if (!hz)
137                 hz = spi->max_speed_hz;
138
139         if (bpw != 8) {
140                 dev_err(&spi->dev, "invalid bits-per-word (%d)\n", bpw);
141                 return -EINVAL;
142         }
143
144         if (spi->mode != cs->mode) {
145                 u8 spcon = SPCON_DEFAULT | S3C2410_SPCON_ENSCK;
146
147                 if (spi->mode & SPI_CPHA)
148                         spcon |= S3C2410_SPCON_CPHA_FMTB;
149
150                 if (spi->mode & SPI_CPOL)
151                         spcon |= S3C2410_SPCON_CPOL_HIGH;
152
153                 cs->mode = spi->mode;
154                 cs->spcon = spcon;
155         }
156
157         if (cs->hz != hz) {
158                 clk = clk_get_rate(hw->clk);
159                 div = DIV_ROUND_UP(clk, hz * 2) - 1;
160
161                 if (div > 255)
162                         div = 255;
163
164                 dev_dbg(&spi->dev, "pre-scaler=%d (wanted %d, got %ld)\n",
165                         div, hz, clk / (2 * (div + 1)));
166
167                 cs->hz = hz;
168                 cs->sppre = div;
169         }
170
171         return 0;
172 }
173
174 static int s3c24xx_spi_setupxfer(struct spi_device *spi,
175                                  struct spi_transfer *t)
176 {
177         struct s3c24xx_spi_devstate *cs = spi->controller_state;
178         struct s3c24xx_spi *hw = to_hw(spi);
179         int ret;
180
181         ret = s3c24xx_spi_update_state(spi, t);
182         if (!ret)
183                 writeb(cs->sppre, hw->regs + S3C2410_SPPRE);
184
185         return ret;
186 }
187
188 static int s3c24xx_spi_setup(struct spi_device *spi)
189 {
190         struct s3c24xx_spi_devstate *cs = spi->controller_state;
191         struct s3c24xx_spi *hw = to_hw(spi);
192         int ret;
193
194         /* allocate settings on the first call */
195         if (!cs) {
196                 cs = kzalloc(sizeof(struct s3c24xx_spi_devstate), GFP_KERNEL);
197                 if (!cs) {
198                         dev_err(&spi->dev, "no memory for controller state\n");
199                         return -ENOMEM;
200                 }
201
202                 cs->spcon = SPCON_DEFAULT;
203                 cs->hz = -1;
204                 spi->controller_state = cs;
205         }
206
207         /* initialise the state from the device */
208         ret = s3c24xx_spi_update_state(spi, NULL);
209         if (ret)
210                 return ret;
211
212         spin_lock(&hw->bitbang.lock);
213         if (!hw->bitbang.busy) {
214                 hw->bitbang.chipselect(spi, BITBANG_CS_INACTIVE);
215                 /* need to ndelay for 0.5 clocktick ? */
216         }
217         spin_unlock(&hw->bitbang.lock);
218
219         return 0;
220 }
221
222 static void s3c24xx_spi_cleanup(struct spi_device *spi)
223 {
224         kfree(spi->controller_state);
225 }
226
227 static inline unsigned int hw_txbyte(struct s3c24xx_spi *hw, int count)
228 {
229         return hw->tx ? hw->tx[count] : 0;
230 }
231
232 #ifdef CONFIG_SPI_S3C24XX_FIQ
233 /* Support for FIQ based pseudo-DMA to improve the transfer speed.
234  *
235  * This code uses the assembly helper in spi_s3c24xx_spi.S which is
236  * used by the FIQ core to move data between main memory and the peripheral
237  * block. Since this is code running on the processor, there is no problem
238  * with cache coherency of the buffers, so we can use any buffer we like.
239  */
240
241 /**
242  * struct spi_fiq_code - FIQ code and header
243  * @length: The length of the code fragment, excluding this header.
244  * @ack_offset: The offset from @data to the word to place the IRQ ACK bit at.
245  * @data: The code itself to install as a FIQ handler.
246  */
247 struct spi_fiq_code {
248         u32     length;
249         u32     ack_offset;
250         u8      data[0];
251 };
252
253 extern struct spi_fiq_code s3c24xx_spi_fiq_txrx;
254 extern struct spi_fiq_code s3c24xx_spi_fiq_tx;
255 extern struct spi_fiq_code s3c24xx_spi_fiq_rx;
256
257 /**
258  * ack_bit - turn IRQ into IRQ acknowledgement bit
259  * @irq: The interrupt number
260  *
261  * Returns the bit to write to the interrupt acknowledge register.
262  */
263 static inline u32 ack_bit(unsigned int irq)
264 {
265         return 1 << (irq - IRQ_EINT0);
266 }
267
268 /**
269  * s3c24xx_spi_tryfiq - attempt to claim and setup FIQ for transfer
270  * @hw: The hardware state.
271  *
272  * Claim the FIQ handler (only one can be active at any one time) and
273  * then setup the correct transfer code for this transfer.
274  *
275  * This call updates all the necessary state information if successful,
276  * so the caller does not need to do anything more than start the transfer
277  * as normal, since the IRQ will have been re-routed to the FIQ handler.
278 */
279 static void s3c24xx_spi_tryfiq(struct s3c24xx_spi *hw)
280 {
281         struct pt_regs regs;
282         enum spi_fiq_mode mode;
283         struct spi_fiq_code *code;
284         int ret;
285
286         if (!hw->fiq_claimed) {
287                 /* try and claim fiq if we haven't got it, and if not
288                  * then return and simply use another transfer method */
289
290                 ret = claim_fiq(&hw->fiq_handler);
291                 if (ret)
292                         return;
293         }
294
295         if (hw->tx && !hw->rx)
296                 mode = FIQ_MODE_TX;
297         else if (hw->rx && !hw->tx)
298                 mode = FIQ_MODE_RX;
299         else
300                 mode = FIQ_MODE_TXRX;
301
302         regs.uregs[fiq_rspi] = (long)hw->regs;
303         regs.uregs[fiq_rrx]  = (long)hw->rx;
304         regs.uregs[fiq_rtx]  = (long)hw->tx + 1;
305         regs.uregs[fiq_rcount] = hw->len - 1;
306         regs.uregs[fiq_rirq] = (long)S3C24XX_VA_IRQ;
307
308         set_fiq_regs(&regs);
309
310         if (hw->fiq_mode != mode) {
311                 u32 *ack_ptr;
312
313                 hw->fiq_mode = mode;
314
315                 switch (mode) {
316                 case FIQ_MODE_TX:
317                         code = &s3c24xx_spi_fiq_tx;
318                         break;
319                 case FIQ_MODE_RX:
320                         code = &s3c24xx_spi_fiq_rx;
321                         break;
322                 case FIQ_MODE_TXRX:
323                         code = &s3c24xx_spi_fiq_txrx;
324                         break;
325                 default:
326                         code = NULL;
327                 }
328
329                 BUG_ON(!code);
330
331                 ack_ptr = (u32 *)&code->data[code->ack_offset];
332                 *ack_ptr = ack_bit(hw->irq);
333
334                 set_fiq_handler(&code->data, code->length);
335         }
336
337         s3c24xx_set_fiq(hw->irq, true);
338
339         hw->fiq_mode = mode;
340         hw->fiq_inuse = 1;
341 }
342
343 /**
344  * s3c24xx_spi_fiqop - FIQ core code callback
345  * @pw: Data registered with the handler
346  * @release: Whether this is a release or a return.
347  *
348  * Called by the FIQ code when another module wants to use the FIQ, so
349  * return whether we are currently using this or not and then update our
350  * internal state.
351  */
352 static int s3c24xx_spi_fiqop(void *pw, int release)
353 {
354         struct s3c24xx_spi *hw = pw;
355         int ret = 0;
356
357         if (release) {
358                 if (hw->fiq_inuse)
359                         ret = -EBUSY;
360
361                 /* note, we do not need to unroute the FIQ, as the FIQ
362                  * vector code de-routes it to signal the end of transfer */
363
364                 hw->fiq_mode = FIQ_MODE_NONE;
365                 hw->fiq_claimed = 0;
366         } else {
367                 hw->fiq_claimed = 1;
368         }
369
370         return ret;
371 }
372
373 /**
374  * s3c24xx_spi_initfiq - setup the information for the FIQ core
375  * @hw: The hardware state.
376  *
377  * Setup the fiq_handler block to pass to the FIQ core.
378  */
379 static inline void s3c24xx_spi_initfiq(struct s3c24xx_spi *hw)
380 {
381         hw->fiq_handler.dev_id = hw;
382         hw->fiq_handler.name = dev_name(hw->dev);
383         hw->fiq_handler.fiq_op = s3c24xx_spi_fiqop;
384 }
385
386 /**
387  * s3c24xx_spi_usefiq - return if we should be using FIQ.
388  * @hw: The hardware state.
389  *
390  * Return true if the platform data specifies whether this channel is
391  * allowed to use the FIQ.
392  */
393 static inline bool s3c24xx_spi_usefiq(struct s3c24xx_spi *hw)
394 {
395         return hw->pdata->use_fiq;
396 }
397
398 /**
399  * s3c24xx_spi_usingfiq - return if channel is using FIQ
400  * @spi: The hardware state.
401  *
402  * Return whether the channel is currently using the FIQ (separate from
403  * whether the FIQ is claimed).
404  */
405 static inline bool s3c24xx_spi_usingfiq(struct s3c24xx_spi *spi)
406 {
407         return spi->fiq_inuse;
408 }
409 #else
410
411 static inline void s3c24xx_spi_initfiq(struct s3c24xx_spi *s) { }
412 static inline void s3c24xx_spi_tryfiq(struct s3c24xx_spi *s) { }
413 static inline bool s3c24xx_spi_usefiq(struct s3c24xx_spi *s) { return false; }
414 static inline bool s3c24xx_spi_usingfiq(struct s3c24xx_spi *s) { return false; }
415
416 #endif /* CONFIG_SPI_S3C24XX_FIQ */
417
418 static int s3c24xx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
419 {
420         struct s3c24xx_spi *hw = to_hw(spi);
421
422         hw->tx = t->tx_buf;
423         hw->rx = t->rx_buf;
424         hw->len = t->len;
425         hw->count = 0;
426
427         init_completion(&hw->done);
428
429         hw->fiq_inuse = 0;
430         if (s3c24xx_spi_usefiq(hw) && t->len >= 3)
431                 s3c24xx_spi_tryfiq(hw);
432
433         /* send the first byte */
434         writeb(hw_txbyte(hw, 0), hw->regs + S3C2410_SPTDAT);
435
436         wait_for_completion(&hw->done);
437         return hw->count;
438 }
439
440 static irqreturn_t s3c24xx_spi_irq(int irq, void *dev)
441 {
442         struct s3c24xx_spi *hw = dev;
443         unsigned int spsta = readb(hw->regs + S3C2410_SPSTA);
444         unsigned int count = hw->count;
445
446         if (spsta & S3C2410_SPSTA_DCOL) {
447                 dev_dbg(hw->dev, "data-collision\n");
448                 complete(&hw->done);
449                 goto irq_done;
450         }
451
452         if (!(spsta & S3C2410_SPSTA_READY)) {
453                 dev_dbg(hw->dev, "spi not ready for tx?\n");
454                 complete(&hw->done);
455                 goto irq_done;
456         }
457
458         if (!s3c24xx_spi_usingfiq(hw)) {
459                 hw->count++;
460
461                 if (hw->rx)
462                         hw->rx[count] = readb(hw->regs + S3C2410_SPRDAT);
463
464                 count++;
465
466                 if (count < hw->len)
467                         writeb(hw_txbyte(hw, count), hw->regs + S3C2410_SPTDAT);
468                 else
469                         complete(&hw->done);
470         } else {
471                 hw->count = hw->len;
472                 hw->fiq_inuse = 0;
473
474                 if (hw->rx)
475                         hw->rx[hw->len-1] = readb(hw->regs + S3C2410_SPRDAT);
476
477                 complete(&hw->done);
478         }
479
480  irq_done:
481         return IRQ_HANDLED;
482 }
483
484 static void s3c24xx_spi_initialsetup(struct s3c24xx_spi *hw)
485 {
486         /* for the moment, permanently enable the clock */
487
488         clk_enable(hw->clk);
489
490         /* program defaults into the registers */
491
492         writeb(0xff, hw->regs + S3C2410_SPPRE);
493         writeb(SPPIN_DEFAULT, hw->regs + S3C2410_SPPIN);
494         writeb(SPCON_DEFAULT, hw->regs + S3C2410_SPCON);
495
496         if (hw->pdata) {
497                 if (hw->set_cs == s3c24xx_spi_gpiocs)
498                         gpio_direction_output(hw->pdata->pin_cs, 1);
499
500                 if (hw->pdata->gpio_setup)
501                         hw->pdata->gpio_setup(hw->pdata, 1);
502         }
503 }
504
505 static int s3c24xx_spi_probe(struct platform_device *pdev)
506 {
507         struct s3c2410_spi_info *pdata;
508         struct s3c24xx_spi *hw;
509         struct spi_master *master;
510         struct resource *res;
511         int err = 0;
512
513         master = spi_alloc_master(&pdev->dev, sizeof(struct s3c24xx_spi));
514         if (master == NULL) {
515                 dev_err(&pdev->dev, "No memory for spi_master\n");
516                 return -ENOMEM;
517         }
518
519         hw = spi_master_get_devdata(master);
520         memset(hw, 0, sizeof(struct s3c24xx_spi));
521
522         hw->master = master;
523         hw->pdata = pdata = dev_get_platdata(&pdev->dev);
524         hw->dev = &pdev->dev;
525
526         if (pdata == NULL) {
527                 dev_err(&pdev->dev, "No platform data supplied\n");
528                 err = -ENOENT;
529                 goto err_no_pdata;
530         }
531
532         platform_set_drvdata(pdev, hw);
533         init_completion(&hw->done);
534
535         /* initialise fiq handler */
536
537         s3c24xx_spi_initfiq(hw);
538
539         /* setup the master state. */
540
541         /* the spi->mode bits understood by this driver: */
542         master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
543
544         master->num_chipselect = hw->pdata->num_cs;
545         master->bus_num = pdata->bus_num;
546
547         /* setup the state for the bitbang driver */
548
549         hw->bitbang.master         = hw->master;
550         hw->bitbang.setup_transfer = s3c24xx_spi_setupxfer;
551         hw->bitbang.chipselect     = s3c24xx_spi_chipsel;
552         hw->bitbang.txrx_bufs      = s3c24xx_spi_txrx;
553
554         hw->master->setup  = s3c24xx_spi_setup;
555         hw->master->cleanup = s3c24xx_spi_cleanup;
556
557         dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);
558
559         /* find and map our resources */
560         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
561         hw->regs = devm_ioremap_resource(&pdev->dev, res);
562         if (IS_ERR(hw->regs)) {
563                 err = PTR_ERR(hw->regs);
564                 goto err_no_pdata;
565         }
566
567         hw->irq = platform_get_irq(pdev, 0);
568         if (hw->irq < 0) {
569                 dev_err(&pdev->dev, "No IRQ specified\n");
570                 err = -ENOENT;
571                 goto err_no_pdata;
572         }
573
574         err = devm_request_irq(&pdev->dev, hw->irq, s3c24xx_spi_irq, 0,
575                                 pdev->name, hw);
576         if (err) {
577                 dev_err(&pdev->dev, "Cannot claim IRQ\n");
578                 goto err_no_pdata;
579         }
580
581         hw->clk = devm_clk_get(&pdev->dev, "spi");
582         if (IS_ERR(hw->clk)) {
583                 dev_err(&pdev->dev, "No clock for device\n");
584                 err = PTR_ERR(hw->clk);
585                 goto err_no_pdata;
586         }
587
588         /* setup any gpio we can */
589
590         if (!pdata->set_cs) {
591                 if (pdata->pin_cs < 0) {
592                         dev_err(&pdev->dev, "No chipselect pin\n");
593                         err = -EINVAL;
594                         goto err_register;
595                 }
596
597                 err = devm_gpio_request(&pdev->dev, pdata->pin_cs,
598                                         dev_name(&pdev->dev));
599                 if (err) {
600                         dev_err(&pdev->dev, "Failed to get gpio for cs\n");
601                         goto err_register;
602                 }
603
604                 hw->set_cs = s3c24xx_spi_gpiocs;
605                 gpio_direction_output(pdata->pin_cs, 1);
606         } else
607                 hw->set_cs = pdata->set_cs;
608
609         s3c24xx_spi_initialsetup(hw);
610
611         /* register our spi controller */
612
613         err = spi_bitbang_start(&hw->bitbang);
614         if (err) {
615                 dev_err(&pdev->dev, "Failed to register SPI master\n");
616                 goto err_register;
617         }
618
619         return 0;
620
621  err_register:
622         clk_disable(hw->clk);
623
624  err_no_pdata:
625         spi_master_put(hw->master);
626         return err;
627 }
628
629 static int s3c24xx_spi_remove(struct platform_device *dev)
630 {
631         struct s3c24xx_spi *hw = platform_get_drvdata(dev);
632
633         spi_bitbang_stop(&hw->bitbang);
634         clk_disable(hw->clk);
635         spi_master_put(hw->master);
636         return 0;
637 }
638
639
640 #ifdef CONFIG_PM
641
642 static int s3c24xx_spi_suspend(struct device *dev)
643 {
644         struct s3c24xx_spi *hw = dev_get_drvdata(dev);
645
646         if (hw->pdata && hw->pdata->gpio_setup)
647                 hw->pdata->gpio_setup(hw->pdata, 0);
648
649         clk_disable(hw->clk);
650         return 0;
651 }
652
653 static int s3c24xx_spi_resume(struct device *dev)
654 {
655         struct s3c24xx_spi *hw = dev_get_drvdata(dev);
656
657         s3c24xx_spi_initialsetup(hw);
658         return 0;
659 }
660
661 static const struct dev_pm_ops s3c24xx_spi_pmops = {
662         .suspend        = s3c24xx_spi_suspend,
663         .resume         = s3c24xx_spi_resume,
664 };
665
666 #define S3C24XX_SPI_PMOPS &s3c24xx_spi_pmops
667 #else
668 #define S3C24XX_SPI_PMOPS NULL
669 #endif /* CONFIG_PM */
670
671 MODULE_ALIAS("platform:s3c2410-spi");
672 static struct platform_driver s3c24xx_spi_driver = {
673         .probe          = s3c24xx_spi_probe,
674         .remove         = s3c24xx_spi_remove,
675         .driver         = {
676                 .name   = "s3c2410-spi",
677                 .owner  = THIS_MODULE,
678                 .pm     = S3C24XX_SPI_PMOPS,
679         },
680 };
681 module_platform_driver(s3c24xx_spi_driver);
682
683 MODULE_DESCRIPTION("S3C24XX SPI Driver");
684 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
685 MODULE_LICENSE("GPL");