blob: f12e27cdbf49fc483d5c26029bcd46d10c5def20 [file] [log] [blame]
Grant Likelyca632f52011-06-06 01:16:30 -06001/*
Wan ZongShun30eaed02009-12-01 14:29:20 +00002 * 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 *
Grant Likelyca632f52011-06-06 01:16:30 -06009 */
Wan ZongShun30eaed02009-12-01 14:29:20 +000010
Axel Lin00d29522011-11-24 11:10:51 +080011#include <linux/module.h>
Wan ZongShun30eaed02009-12-01 14:29:20 +000012#include <linux/init.h>
13#include <linux/spinlock.h>
14#include <linux/workqueue.h>
15#include <linux/interrupt.h>
16#include <linux/delay.h>
17#include <linux/errno.h>
18#include <linux/err.h>
19#include <linux/clk.h>
20#include <linux/device.h>
21#include <linux/platform_device.h>
22#include <linux/gpio.h>
23#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Wan ZongShun30eaed02009-12-01 14:29:20 +000025
26#include <linux/spi/spi.h>
27#include <linux/spi/spi_bitbang.h>
28
Arnd Bergmann54ecf4f2012-08-24 15:18:36 +020029#include <linux/platform_data/spi-nuc900.h>
Wan ZongShun30eaed02009-12-01 14:29:20 +000030
31/* usi registers offset */
32#define USI_CNT 0x00
33#define USI_DIV 0x04
34#define USI_SSR 0x08
35#define USI_RX0 0x10
36#define USI_TX0 0x10
37
38/* usi register bit */
39#define ENINT (0x01 << 17)
40#define ENFLG (0x01 << 16)
Axel Linfb534f12014-03-21 13:24:14 +080041#define SLEEP (0x0f << 12)
Wan ZongShun30eaed02009-12-01 14:29:20 +000042#define TXNUM (0x03 << 8)
Axel Linfb534f12014-03-21 13:24:14 +080043#define TXBITLEN (0x1f << 3)
Wan ZongShun30eaed02009-12-01 14:29:20 +000044#define TXNEG (0x01 << 2)
45#define RXNEG (0x01 << 1)
46#define LSB (0x01 << 10)
47#define SELECTLEV (0x01 << 2)
48#define SELECTPOL (0x01 << 31)
49#define SELECTSLAVE 0x01
50#define GOBUSY 0x01
51
52struct nuc900_spi {
53 struct spi_bitbang bitbang;
54 struct completion done;
55 void __iomem *regs;
56 int irq;
57 int len;
58 int count;
59 const unsigned char *tx;
60 unsigned char *rx;
61 struct clk *clk;
Wan ZongShun30eaed02009-12-01 14:29:20 +000062 struct spi_master *master;
Wan ZongShun30eaed02009-12-01 14:29:20 +000063 struct nuc900_spi_info *pdata;
64 spinlock_t lock;
Wan ZongShun30eaed02009-12-01 14:29:20 +000065};
66
67static inline struct nuc900_spi *to_hw(struct spi_device *sdev)
68{
69 return spi_master_get_devdata(sdev->master);
70}
71
72static 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
108static 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
Axel Linfb534f12014-03-21 13:24:14 +0800121static void nuc900_spi_setup_txnum(struct nuc900_spi *hw, unsigned int txnum)
Wan ZongShun30eaed02009-12-01 14:29:20 +0000122{
123 unsigned int val;
124 unsigned long flags;
125
126 spin_lock_irqsave(&hw->lock, flags);
127
Axel Linfb534f12014-03-21 13:24:14 +0800128 val = __raw_readl(hw->regs + USI_CNT) & ~TXNUM;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000129
Axel Linfb534f12014-03-21 13:24:14 +0800130 if (txnum)
Wan ZongShun30eaed02009-12-01 14:29:20 +0000131 val |= txnum << 0x08;
132
133 __raw_writel(val, hw->regs + USI_CNT);
134
135 spin_unlock_irqrestore(&hw->lock, flags);
136
137}
138
139static void nuc900_spi_setup_txbitlen(struct nuc900_spi *hw,
140 unsigned int txbitlen)
141{
142 unsigned int val;
143 unsigned long flags;
144
145 spin_lock_irqsave(&hw->lock, flags);
146
Axel Linfb534f12014-03-21 13:24:14 +0800147 val = __raw_readl(hw->regs + USI_CNT) & ~TXBITLEN;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000148
149 val |= (txbitlen << 0x03);
150
151 __raw_writel(val, hw->regs + USI_CNT);
152
153 spin_unlock_irqrestore(&hw->lock, flags);
154}
155
156static void nuc900_spi_gobusy(struct nuc900_spi *hw)
157{
158 unsigned int val;
159 unsigned long flags;
160
161 spin_lock_irqsave(&hw->lock, flags);
162
163 val = __raw_readl(hw->regs + USI_CNT);
164
165 val |= GOBUSY;
166
167 __raw_writel(val, hw->regs + USI_CNT);
168
169 spin_unlock_irqrestore(&hw->lock, flags);
170}
171
Wan ZongShun30eaed02009-12-01 14:29:20 +0000172static inline unsigned int hw_txbyte(struct nuc900_spi *hw, int count)
173{
174 return hw->tx ? hw->tx[count] : 0;
175}
176
177static int nuc900_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
178{
179 struct nuc900_spi *hw = to_hw(spi);
180
181 hw->tx = t->tx_buf;
182 hw->rx = t->rx_buf;
183 hw->len = t->len;
184 hw->count = 0;
185
186 __raw_writel(hw_txbyte(hw, 0x0), hw->regs + USI_TX0);
187
188 nuc900_spi_gobusy(hw);
189
190 wait_for_completion(&hw->done);
191
192 return hw->count;
193}
194
195static irqreturn_t nuc900_spi_irq(int irq, void *dev)
196{
197 struct nuc900_spi *hw = dev;
198 unsigned int status;
199 unsigned int count = hw->count;
200
201 status = __raw_readl(hw->regs + USI_CNT);
202 __raw_writel(status, hw->regs + USI_CNT);
203
204 if (status & ENFLG) {
205 hw->count++;
206
207 if (hw->rx)
208 hw->rx[count] = __raw_readl(hw->regs + USI_RX0);
209 count++;
210
211 if (count < hw->len) {
212 __raw_writel(hw_txbyte(hw, count), hw->regs + USI_TX0);
213 nuc900_spi_gobusy(hw);
214 } else {
215 complete(&hw->done);
216 }
217
218 return IRQ_HANDLED;
219 }
220
221 complete(&hw->done);
222 return IRQ_HANDLED;
223}
224
225static void nuc900_tx_edge(struct nuc900_spi *hw, unsigned int edge)
226{
227 unsigned int val;
228 unsigned long flags;
229
230 spin_lock_irqsave(&hw->lock, flags);
231
232 val = __raw_readl(hw->regs + USI_CNT);
233
234 if (edge)
235 val |= TXNEG;
236 else
237 val &= ~TXNEG;
238 __raw_writel(val, hw->regs + USI_CNT);
239
240 spin_unlock_irqrestore(&hw->lock, flags);
241}
242
243static void nuc900_rx_edge(struct nuc900_spi *hw, unsigned int edge)
244{
245 unsigned int val;
246 unsigned long flags;
247
248 spin_lock_irqsave(&hw->lock, flags);
249
250 val = __raw_readl(hw->regs + USI_CNT);
251
252 if (edge)
253 val |= RXNEG;
254 else
255 val &= ~RXNEG;
256 __raw_writel(val, hw->regs + USI_CNT);
257
258 spin_unlock_irqrestore(&hw->lock, flags);
259}
260
261static void nuc900_send_first(struct nuc900_spi *hw, unsigned int lsb)
262{
263 unsigned int val;
264 unsigned long flags;
265
266 spin_lock_irqsave(&hw->lock, flags);
267
268 val = __raw_readl(hw->regs + USI_CNT);
269
270 if (lsb)
271 val |= LSB;
272 else
273 val &= ~LSB;
274 __raw_writel(val, hw->regs + USI_CNT);
275
276 spin_unlock_irqrestore(&hw->lock, flags);
277}
278
279static void nuc900_set_sleep(struct nuc900_spi *hw, unsigned int sleep)
280{
281 unsigned int val;
282 unsigned long flags;
283
284 spin_lock_irqsave(&hw->lock, flags);
285
Axel Linfb534f12014-03-21 13:24:14 +0800286 val = __raw_readl(hw->regs + USI_CNT) & ~SLEEP;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000287
288 if (sleep)
289 val |= (sleep << 12);
Axel Linfb534f12014-03-21 13:24:14 +0800290
Wan ZongShun30eaed02009-12-01 14:29:20 +0000291 __raw_writel(val, hw->regs + USI_CNT);
292
293 spin_unlock_irqrestore(&hw->lock, flags);
294}
295
296static void nuc900_enable_int(struct nuc900_spi *hw)
297{
298 unsigned int val;
299 unsigned long flags;
300
301 spin_lock_irqsave(&hw->lock, flags);
302
303 val = __raw_readl(hw->regs + USI_CNT);
304
305 val |= ENINT;
306
307 __raw_writel(val, hw->regs + USI_CNT);
308
309 spin_unlock_irqrestore(&hw->lock, flags);
310}
311
312static void nuc900_set_divider(struct nuc900_spi *hw)
313{
314 __raw_writel(hw->pdata->divider, hw->regs + USI_DIV);
315}
316
317static void nuc900_init_spi(struct nuc900_spi *hw)
318{
319 clk_enable(hw->clk);
320 spin_lock_init(&hw->lock);
321
322 nuc900_tx_edge(hw, hw->pdata->txneg);
323 nuc900_rx_edge(hw, hw->pdata->rxneg);
324 nuc900_send_first(hw, hw->pdata->lsb);
325 nuc900_set_sleep(hw, hw->pdata->sleep);
326 nuc900_spi_setup_txbitlen(hw, hw->pdata->txbitlen);
327 nuc900_spi_setup_txnum(hw, hw->pdata->txnum);
328 nuc900_set_divider(hw);
329 nuc900_enable_int(hw);
330}
331
Grant Likelyfd4a3192012-12-07 16:57:14 +0000332static int nuc900_spi_probe(struct platform_device *pdev)
Wan ZongShun30eaed02009-12-01 14:29:20 +0000333{
334 struct nuc900_spi *hw;
335 struct spi_master *master;
Axel Lin8120ff82014-02-08 11:15:57 +0800336 struct resource *res;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000337 int err = 0;
338
339 master = spi_alloc_master(&pdev->dev, sizeof(struct nuc900_spi));
340 if (master == NULL) {
341 dev_err(&pdev->dev, "No memory for spi_master\n");
Jingoo Han75194592013-12-09 19:18:18 +0900342 return -ENOMEM;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000343 }
344
345 hw = spi_master_get_devdata(master);
Axel Lin94c69f72013-09-10 15:43:41 +0800346 hw->master = master;
Jingoo Han8074cf02013-07-30 16:58:59 +0900347 hw->pdata = dev_get_platdata(&pdev->dev);
Wan ZongShun30eaed02009-12-01 14:29:20 +0000348
349 if (hw->pdata == NULL) {
350 dev_err(&pdev->dev, "No platform data supplied\n");
351 err = -ENOENT;
352 goto err_pdata;
353 }
354
355 platform_set_drvdata(pdev, hw);
356 init_completion(&hw->done);
357
Axel Lin044d0bb2013-08-15 14:11:21 +0800358 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000359 master->num_chipselect = hw->pdata->num_cs;
360 master->bus_num = hw->pdata->bus_num;
361 hw->bitbang.master = hw->master;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000362 hw->bitbang.chipselect = nuc900_spi_chipsel;
363 hw->bitbang.txrx_bufs = nuc900_spi_txrx;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000364
Axel Lin8120ff82014-02-08 11:15:57 +0800365 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
366 hw->regs = devm_ioremap_resource(&pdev->dev, res);
Jingoo Han75194592013-12-09 19:18:18 +0900367 if (IS_ERR(hw->regs)) {
368 err = PTR_ERR(hw->regs);
Wan ZongShun30eaed02009-12-01 14:29:20 +0000369 goto err_pdata;
370 }
371
Wan ZongShun30eaed02009-12-01 14:29:20 +0000372 hw->irq = platform_get_irq(pdev, 0);
373 if (hw->irq < 0) {
374 dev_err(&pdev->dev, "No IRQ specified\n");
375 err = -ENOENT;
Jingoo Han75194592013-12-09 19:18:18 +0900376 goto err_pdata;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000377 }
378
Jingoo Han75194592013-12-09 19:18:18 +0900379 err = devm_request_irq(&pdev->dev, hw->irq, nuc900_spi_irq, 0,
380 pdev->name, hw);
Wan ZongShun30eaed02009-12-01 14:29:20 +0000381 if (err) {
382 dev_err(&pdev->dev, "Cannot claim IRQ\n");
Jingoo Han75194592013-12-09 19:18:18 +0900383 goto err_pdata;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000384 }
385
Jingoo Han75194592013-12-09 19:18:18 +0900386 hw->clk = devm_clk_get(&pdev->dev, "spi");
Wan ZongShun30eaed02009-12-01 14:29:20 +0000387 if (IS_ERR(hw->clk)) {
388 dev_err(&pdev->dev, "No clock for device\n");
389 err = PTR_ERR(hw->clk);
Jingoo Han75194592013-12-09 19:18:18 +0900390 goto err_pdata;
Wan ZongShun30eaed02009-12-01 14:29:20 +0000391 }
392
Axel Lin97371fa2011-11-25 00:23:28 +0100393 mfp_set_groupg(&pdev->dev, NULL);
Wan ZongShun30eaed02009-12-01 14:29:20 +0000394 nuc900_init_spi(hw);
395
396 err = spi_bitbang_start(&hw->bitbang);
397 if (err) {
398 dev_err(&pdev->dev, "Failed to register SPI master\n");
399 goto err_register;
400 }
401
402 return 0;
403
404err_register:
405 clk_disable(hw->clk);
Wan ZongShun30eaed02009-12-01 14:29:20 +0000406err_pdata:
Joe Perchesbc3f67a2010-11-14 19:04:47 -0800407 spi_master_put(hw->master);
Wan ZongShun30eaed02009-12-01 14:29:20 +0000408 return err;
409}
410
Grant Likelyfd4a3192012-12-07 16:57:14 +0000411static int nuc900_spi_remove(struct platform_device *dev)
Wan ZongShun30eaed02009-12-01 14:29:20 +0000412{
413 struct nuc900_spi *hw = platform_get_drvdata(dev);
414
Axel Lin708a7e42011-05-15 07:33:28 +0800415 spi_bitbang_stop(&hw->bitbang);
Wan ZongShun30eaed02009-12-01 14:29:20 +0000416 clk_disable(hw->clk);
Wan ZongShun30eaed02009-12-01 14:29:20 +0000417 spi_master_put(hw->master);
418 return 0;
419}
420
421static struct platform_driver nuc900_spi_driver = {
422 .probe = nuc900_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000423 .remove = nuc900_spi_remove,
Wan ZongShun30eaed02009-12-01 14:29:20 +0000424 .driver = {
425 .name = "nuc900-spi",
426 .owner = THIS_MODULE,
427 },
428};
Grant Likely940ab882011-10-05 11:29:49 -0600429module_platform_driver(nuc900_spi_driver);
Wan ZongShun30eaed02009-12-01 14:29:20 +0000430
431MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
432MODULE_DESCRIPTION("nuc900 spi driver!");
433MODULE_LICENSE("GPL");
434MODULE_ALIAS("platform:nuc900-spi");