Merge tag 'backport/v3.14.24-ltsi-rc1/sh-tmu-to-v3.18-rc1' into backport/v3.14.24...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / spi / spi-nuc900.c
1 /*
2  * Copyright (c) 2009 Nuvoton technology.
3  * Wan ZongShun <mcuos.com@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  */
10
11 #include <linux/module.h>
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/device.h>
20 #include <linux/platform_device.h>
21 #include <linux/gpio.h>
22 #include <linux/io.h>
23 #include <linux/slab.h>
24
25 #include <linux/spi/spi.h>
26 #include <linux/spi/spi_bitbang.h>
27
28 #include <linux/platform_data/spi-nuc900.h>
29
30 /* usi registers offset */
31 #define USI_CNT         0x00
32 #define USI_DIV         0x04
33 #define USI_SSR         0x08
34 #define USI_RX0         0x10
35 #define USI_TX0         0x10
36
37 /* usi register bit */
38 #define ENINT           (0x01 << 17)
39 #define ENFLG           (0x01 << 16)
40 #define TXNUM           (0x03 << 8)
41 #define TXNEG           (0x01 << 2)
42 #define RXNEG           (0x01 << 1)
43 #define LSB             (0x01 << 10)
44 #define SELECTLEV       (0x01 << 2)
45 #define SELECTPOL       (0x01 << 31)
46 #define SELECTSLAVE     0x01
47 #define GOBUSY          0x01
48
49 struct nuc900_spi {
50         struct spi_bitbang       bitbang;
51         struct completion        done;
52         void __iomem            *regs;
53         int                      irq;
54         int                      len;
55         int                      count;
56         const unsigned char     *tx;
57         unsigned char           *rx;
58         struct clk              *clk;
59         struct spi_master       *master;
60         struct spi_device       *curdev;
61         struct device           *dev;
62         struct nuc900_spi_info *pdata;
63         spinlock_t              lock;
64         struct resource         *res;
65 };
66
67 static inline struct nuc900_spi *to_hw(struct spi_device *sdev)
68 {
69         return spi_master_get_devdata(sdev->master);
70 }
71
72 static void nuc900_slave_select(struct spi_device *spi, unsigned int ssr)
73 {
74         struct nuc900_spi *hw = to_hw(spi);
75         unsigned int val;
76         unsigned int cs = spi->mode & SPI_CS_HIGH ? 1 : 0;
77         unsigned int cpol = spi->mode & SPI_CPOL ? 1 : 0;
78         unsigned long flags;
79
80         spin_lock_irqsave(&hw->lock, flags);
81
82         val = __raw_readl(hw->regs + USI_SSR);
83
84         if (!cs)
85                 val &= ~SELECTLEV;
86         else
87                 val |= SELECTLEV;
88
89         if (!ssr)
90                 val &= ~SELECTSLAVE;
91         else
92                 val |= SELECTSLAVE;
93
94         __raw_writel(val, hw->regs + USI_SSR);
95
96         val = __raw_readl(hw->regs + USI_CNT);
97
98         if (!cpol)
99                 val &= ~SELECTPOL;
100         else
101                 val |= SELECTPOL;
102
103         __raw_writel(val, hw->regs + USI_CNT);
104
105         spin_unlock_irqrestore(&hw->lock, flags);
106 }
107
108 static void nuc900_spi_chipsel(struct spi_device *spi, int value)
109 {
110         switch (value) {
111         case BITBANG_CS_INACTIVE:
112                 nuc900_slave_select(spi, 0);
113                 break;
114
115         case BITBANG_CS_ACTIVE:
116                 nuc900_slave_select(spi, 1);
117                 break;
118         }
119 }
120
121 static void nuc900_spi_setup_txnum(struct nuc900_spi *hw,
122                                                         unsigned int txnum)
123 {
124         unsigned int val;
125         unsigned long flags;
126
127         spin_lock_irqsave(&hw->lock, flags);
128
129         val = __raw_readl(hw->regs + USI_CNT);
130
131         if (!txnum)
132                 val &= ~TXNUM;
133         else
134                 val |= txnum << 0x08;
135
136         __raw_writel(val, hw->regs + USI_CNT);
137
138         spin_unlock_irqrestore(&hw->lock, flags);
139
140 }
141
142 static void nuc900_spi_setup_txbitlen(struct nuc900_spi *hw,
143                                                         unsigned int txbitlen)
144 {
145         unsigned int val;
146         unsigned long flags;
147
148         spin_lock_irqsave(&hw->lock, flags);
149
150         val = __raw_readl(hw->regs + USI_CNT);
151
152         val |= (txbitlen << 0x03);
153
154         __raw_writel(val, hw->regs + USI_CNT);
155
156         spin_unlock_irqrestore(&hw->lock, flags);
157 }
158
159 static void nuc900_spi_gobusy(struct nuc900_spi *hw)
160 {
161         unsigned int val;
162         unsigned long flags;
163
164         spin_lock_irqsave(&hw->lock, flags);
165
166         val = __raw_readl(hw->regs + USI_CNT);
167
168         val |= GOBUSY;
169
170         __raw_writel(val, hw->regs + USI_CNT);
171
172         spin_unlock_irqrestore(&hw->lock, flags);
173 }
174
175 static inline unsigned int hw_txbyte(struct nuc900_spi *hw, int count)
176 {
177         return hw->tx ? hw->tx[count] : 0;
178 }
179
180 static int nuc900_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
181 {
182         struct nuc900_spi *hw = to_hw(spi);
183
184         hw->tx = t->tx_buf;
185         hw->rx = t->rx_buf;
186         hw->len = t->len;
187         hw->count = 0;
188
189         __raw_writel(hw_txbyte(hw, 0x0), hw->regs + USI_TX0);
190
191         nuc900_spi_gobusy(hw);
192
193         wait_for_completion(&hw->done);
194
195         return hw->count;
196 }
197
198 static irqreturn_t nuc900_spi_irq(int irq, void *dev)
199 {
200         struct nuc900_spi *hw = dev;
201         unsigned int status;
202         unsigned int count = hw->count;
203
204         status = __raw_readl(hw->regs + USI_CNT);
205         __raw_writel(status, hw->regs + USI_CNT);
206
207         if (status & ENFLG) {
208                 hw->count++;
209
210                 if (hw->rx)
211                         hw->rx[count] = __raw_readl(hw->regs + USI_RX0);
212                 count++;
213
214                 if (count < hw->len) {
215                         __raw_writel(hw_txbyte(hw, count), hw->regs + USI_TX0);
216                         nuc900_spi_gobusy(hw);
217                 } else {
218                         complete(&hw->done);
219                 }
220
221                 return IRQ_HANDLED;
222         }
223
224         complete(&hw->done);
225         return IRQ_HANDLED;
226 }
227
228 static void nuc900_tx_edge(struct nuc900_spi *hw, unsigned int edge)
229 {
230         unsigned int val;
231         unsigned long flags;
232
233         spin_lock_irqsave(&hw->lock, flags);
234
235         val = __raw_readl(hw->regs + USI_CNT);
236
237         if (edge)
238                 val |= TXNEG;
239         else
240                 val &= ~TXNEG;
241         __raw_writel(val, hw->regs + USI_CNT);
242
243         spin_unlock_irqrestore(&hw->lock, flags);
244 }
245
246 static void nuc900_rx_edge(struct nuc900_spi *hw, unsigned int edge)
247 {
248         unsigned int val;
249         unsigned long flags;
250
251         spin_lock_irqsave(&hw->lock, flags);
252
253         val = __raw_readl(hw->regs + USI_CNT);
254
255         if (edge)
256                 val |= RXNEG;
257         else
258                 val &= ~RXNEG;
259         __raw_writel(val, hw->regs + USI_CNT);
260
261         spin_unlock_irqrestore(&hw->lock, flags);
262 }
263
264 static void nuc900_send_first(struct nuc900_spi *hw, unsigned int lsb)
265 {
266         unsigned int val;
267         unsigned long flags;
268
269         spin_lock_irqsave(&hw->lock, flags);
270
271         val = __raw_readl(hw->regs + USI_CNT);
272
273         if (lsb)
274                 val |= LSB;
275         else
276                 val &= ~LSB;
277         __raw_writel(val, hw->regs + USI_CNT);
278
279         spin_unlock_irqrestore(&hw->lock, flags);
280 }
281
282 static void nuc900_set_sleep(struct nuc900_spi *hw, unsigned int sleep)
283 {
284         unsigned int val;
285         unsigned long flags;
286
287         spin_lock_irqsave(&hw->lock, flags);
288
289         val = __raw_readl(hw->regs + USI_CNT);
290
291         if (sleep)
292                 val |= (sleep << 12);
293         else
294                 val &= ~(0x0f << 12);
295         __raw_writel(val, hw->regs + USI_CNT);
296
297         spin_unlock_irqrestore(&hw->lock, flags);
298 }
299
300 static void nuc900_enable_int(struct nuc900_spi *hw)
301 {
302         unsigned int val;
303         unsigned long flags;
304
305         spin_lock_irqsave(&hw->lock, flags);
306
307         val = __raw_readl(hw->regs + USI_CNT);
308
309         val |= ENINT;
310
311         __raw_writel(val, hw->regs + USI_CNT);
312
313         spin_unlock_irqrestore(&hw->lock, flags);
314 }
315
316 static void nuc900_set_divider(struct nuc900_spi *hw)
317 {
318         __raw_writel(hw->pdata->divider, hw->regs + USI_DIV);
319 }
320
321 static void nuc900_init_spi(struct nuc900_spi *hw)
322 {
323         clk_enable(hw->clk);
324         spin_lock_init(&hw->lock);
325
326         nuc900_tx_edge(hw, hw->pdata->txneg);
327         nuc900_rx_edge(hw, hw->pdata->rxneg);
328         nuc900_send_first(hw, hw->pdata->lsb);
329         nuc900_set_sleep(hw, hw->pdata->sleep);
330         nuc900_spi_setup_txbitlen(hw, hw->pdata->txbitlen);
331         nuc900_spi_setup_txnum(hw, hw->pdata->txnum);
332         nuc900_set_divider(hw);
333         nuc900_enable_int(hw);
334 }
335
336 static int nuc900_spi_probe(struct platform_device *pdev)
337 {
338         struct nuc900_spi *hw;
339         struct spi_master *master;
340         int err = 0;
341
342         master = spi_alloc_master(&pdev->dev, sizeof(struct nuc900_spi));
343         if (master == NULL) {
344                 dev_err(&pdev->dev, "No memory for spi_master\n");
345                 return -ENOMEM;
346         }
347
348         hw = spi_master_get_devdata(master);
349         hw->master = master;
350         hw->pdata  = dev_get_platdata(&pdev->dev);
351         hw->dev = &pdev->dev;
352
353         if (hw->pdata == NULL) {
354                 dev_err(&pdev->dev, "No platform data supplied\n");
355                 err = -ENOENT;
356                 goto err_pdata;
357         }
358
359         platform_set_drvdata(pdev, hw);
360         init_completion(&hw->done);
361
362         master->mode_bits          = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
363         if (hw->pdata->lsb)
364                 master->mode_bits |= SPI_LSB_FIRST;
365         master->num_chipselect     = hw->pdata->num_cs;
366         master->bus_num            = hw->pdata->bus_num;
367         hw->bitbang.master         = hw->master;
368         hw->bitbang.chipselect     = nuc900_spi_chipsel;
369         hw->bitbang.txrx_bufs      = nuc900_spi_txrx;
370
371         hw->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
372         hw->regs = devm_ioremap_resource(&pdev->dev, hw->res);
373         if (IS_ERR(hw->regs)) {
374                 err = PTR_ERR(hw->regs);
375                 goto err_pdata;
376         }
377
378         hw->irq = platform_get_irq(pdev, 0);
379         if (hw->irq < 0) {
380                 dev_err(&pdev->dev, "No IRQ specified\n");
381                 err = -ENOENT;
382                 goto err_pdata;
383         }
384
385         err = devm_request_irq(&pdev->dev, hw->irq, nuc900_spi_irq, 0,
386                                 pdev->name, hw);
387         if (err) {
388                 dev_err(&pdev->dev, "Cannot claim IRQ\n");
389                 goto err_pdata;
390         }
391
392         hw->clk = devm_clk_get(&pdev->dev, "spi");
393         if (IS_ERR(hw->clk)) {
394                 dev_err(&pdev->dev, "No clock for device\n");
395                 err = PTR_ERR(hw->clk);
396                 goto err_pdata;
397         }
398
399         mfp_set_groupg(&pdev->dev, NULL);
400         nuc900_init_spi(hw);
401
402         err = spi_bitbang_start(&hw->bitbang);
403         if (err) {
404                 dev_err(&pdev->dev, "Failed to register SPI master\n");
405                 goto err_register;
406         }
407
408         return 0;
409
410 err_register:
411         clk_disable(hw->clk);
412 err_pdata:
413         spi_master_put(hw->master);
414         return err;
415 }
416
417 static int nuc900_spi_remove(struct platform_device *dev)
418 {
419         struct nuc900_spi *hw = platform_get_drvdata(dev);
420
421         spi_bitbang_stop(&hw->bitbang);
422         clk_disable(hw->clk);
423         spi_master_put(hw->master);
424         return 0;
425 }
426
427 static struct platform_driver nuc900_spi_driver = {
428         .probe          = nuc900_spi_probe,
429         .remove         = nuc900_spi_remove,
430         .driver         = {
431                 .name   = "nuc900-spi",
432                 .owner  = THIS_MODULE,
433         },
434 };
435 module_platform_driver(nuc900_spi_driver);
436
437 MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
438 MODULE_DESCRIPTION("nuc900 spi driver!");
439 MODULE_LICENSE("GPL");
440 MODULE_ALIAS("platform:nuc900-spi");