2 * Copyright (c) 2009 Nuvoton technology.
3 * Wan ZongShun <mcuos.com@gmail.com>
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.
11 #include <linux/init.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>
23 #include <linux/slab.h>
25 #include <linux/spi/spi.h>
26 #include <linux/spi/spi_bitbang.h>
28 #include <mach/nuc900_spi.h>
30 /* usi registers offset */
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
50 struct spi_bitbang bitbang
;
51 struct completion done
;
56 const unsigned char *tx
;
59 struct resource
*ioarea
;
60 struct spi_master
*master
;
61 struct spi_device
*curdev
;
63 struct nuc900_spi_info
*pdata
;
68 static inline struct nuc900_spi
*to_hw(struct spi_device
*sdev
)
70 return spi_master_get_devdata(sdev
->master
);
73 static void nuc900_slave_select(struct spi_device
*spi
, unsigned int ssr
)
75 struct nuc900_spi
*hw
= to_hw(spi
);
77 unsigned int cs
= spi
->mode
& SPI_CS_HIGH
? 1 : 0;
78 unsigned int cpol
= spi
->mode
& SPI_CPOL
? 1 : 0;
81 spin_lock_irqsave(&hw
->lock
, flags
);
83 val
= __raw_readl(hw
->regs
+ USI_SSR
);
95 __raw_writel(val
, hw
->regs
+ USI_SSR
);
97 val
= __raw_readl(hw
->regs
+ USI_CNT
);
104 __raw_writel(val
, hw
->regs
+ USI_CNT
);
106 spin_unlock_irqrestore(&hw
->lock
, flags
);
109 static void nuc900_spi_chipsel(struct spi_device
*spi
, int value
)
112 case BITBANG_CS_INACTIVE
:
113 nuc900_slave_select(spi
, 0);
116 case BITBANG_CS_ACTIVE
:
117 nuc900_slave_select(spi
, 1);
122 static void nuc900_spi_setup_txnum(struct nuc900_spi
*hw
,
128 spin_lock_irqsave(&hw
->lock
, flags
);
130 val
= __raw_readl(hw
->regs
+ USI_CNT
);
135 val
|= txnum
<< 0x08;
137 __raw_writel(val
, hw
->regs
+ USI_CNT
);
139 spin_unlock_irqrestore(&hw
->lock
, flags
);
143 static void nuc900_spi_setup_txbitlen(struct nuc900_spi
*hw
,
144 unsigned int txbitlen
)
149 spin_lock_irqsave(&hw
->lock
, flags
);
151 val
= __raw_readl(hw
->regs
+ USI_CNT
);
153 val
|= (txbitlen
<< 0x03);
155 __raw_writel(val
, hw
->regs
+ USI_CNT
);
157 spin_unlock_irqrestore(&hw
->lock
, flags
);
160 static void nuc900_spi_gobusy(struct nuc900_spi
*hw
)
165 spin_lock_irqsave(&hw
->lock
, flags
);
167 val
= __raw_readl(hw
->regs
+ USI_CNT
);
171 __raw_writel(val
, hw
->regs
+ USI_CNT
);
173 spin_unlock_irqrestore(&hw
->lock
, flags
);
176 static int nuc900_spi_setupxfer(struct spi_device
*spi
,
177 struct spi_transfer
*t
)
182 static int nuc900_spi_setup(struct spi_device
*spi
)
187 static inline unsigned int hw_txbyte(struct nuc900_spi
*hw
, int count
)
189 return hw
->tx
? hw
->tx
[count
] : 0;
192 static int nuc900_spi_txrx(struct spi_device
*spi
, struct spi_transfer
*t
)
194 struct nuc900_spi
*hw
= to_hw(spi
);
201 __raw_writel(hw_txbyte(hw
, 0x0), hw
->regs
+ USI_TX0
);
203 nuc900_spi_gobusy(hw
);
205 wait_for_completion(&hw
->done
);
210 static irqreturn_t
nuc900_spi_irq(int irq
, void *dev
)
212 struct nuc900_spi
*hw
= dev
;
214 unsigned int count
= hw
->count
;
216 status
= __raw_readl(hw
->regs
+ USI_CNT
);
217 __raw_writel(status
, hw
->regs
+ USI_CNT
);
219 if (status
& ENFLG
) {
223 hw
->rx
[count
] = __raw_readl(hw
->regs
+ USI_RX0
);
226 if (count
< hw
->len
) {
227 __raw_writel(hw_txbyte(hw
, count
), hw
->regs
+ USI_TX0
);
228 nuc900_spi_gobusy(hw
);
240 static void nuc900_tx_edge(struct nuc900_spi
*hw
, unsigned int edge
)
245 spin_lock_irqsave(&hw
->lock
, flags
);
247 val
= __raw_readl(hw
->regs
+ USI_CNT
);
253 __raw_writel(val
, hw
->regs
+ USI_CNT
);
255 spin_unlock_irqrestore(&hw
->lock
, flags
);
258 static void nuc900_rx_edge(struct nuc900_spi
*hw
, unsigned int edge
)
263 spin_lock_irqsave(&hw
->lock
, flags
);
265 val
= __raw_readl(hw
->regs
+ USI_CNT
);
271 __raw_writel(val
, hw
->regs
+ USI_CNT
);
273 spin_unlock_irqrestore(&hw
->lock
, flags
);
276 static void nuc900_send_first(struct nuc900_spi
*hw
, unsigned int lsb
)
281 spin_lock_irqsave(&hw
->lock
, flags
);
283 val
= __raw_readl(hw
->regs
+ USI_CNT
);
289 __raw_writel(val
, hw
->regs
+ USI_CNT
);
291 spin_unlock_irqrestore(&hw
->lock
, flags
);
294 static void nuc900_set_sleep(struct nuc900_spi
*hw
, unsigned int sleep
)
299 spin_lock_irqsave(&hw
->lock
, flags
);
301 val
= __raw_readl(hw
->regs
+ USI_CNT
);
304 val
|= (sleep
<< 12);
306 val
&= ~(0x0f << 12);
307 __raw_writel(val
, hw
->regs
+ USI_CNT
);
309 spin_unlock_irqrestore(&hw
->lock
, flags
);
312 static void nuc900_enable_int(struct nuc900_spi
*hw
)
317 spin_lock_irqsave(&hw
->lock
, flags
);
319 val
= __raw_readl(hw
->regs
+ USI_CNT
);
323 __raw_writel(val
, hw
->regs
+ USI_CNT
);
325 spin_unlock_irqrestore(&hw
->lock
, flags
);
328 static void nuc900_set_divider(struct nuc900_spi
*hw
)
330 __raw_writel(hw
->pdata
->divider
, hw
->regs
+ USI_DIV
);
333 static void nuc900_init_spi(struct nuc900_spi
*hw
)
336 spin_lock_init(&hw
->lock
);
338 nuc900_tx_edge(hw
, hw
->pdata
->txneg
);
339 nuc900_rx_edge(hw
, hw
->pdata
->rxneg
);
340 nuc900_send_first(hw
, hw
->pdata
->lsb
);
341 nuc900_set_sleep(hw
, hw
->pdata
->sleep
);
342 nuc900_spi_setup_txbitlen(hw
, hw
->pdata
->txbitlen
);
343 nuc900_spi_setup_txnum(hw
, hw
->pdata
->txnum
);
344 nuc900_set_divider(hw
);
345 nuc900_enable_int(hw
);
348 static int __devinit
nuc900_spi_probe(struct platform_device
*pdev
)
350 struct nuc900_spi
*hw
;
351 struct spi_master
*master
;
354 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct nuc900_spi
));
355 if (master
== NULL
) {
356 dev_err(&pdev
->dev
, "No memory for spi_master\n");
361 hw
= spi_master_get_devdata(master
);
362 memset(hw
, 0, sizeof(struct nuc900_spi
));
364 hw
->master
= spi_master_get(master
);
365 hw
->pdata
= pdev
->dev
.platform_data
;
366 hw
->dev
= &pdev
->dev
;
368 if (hw
->pdata
== NULL
) {
369 dev_err(&pdev
->dev
, "No platform data supplied\n");
374 platform_set_drvdata(pdev
, hw
);
375 init_completion(&hw
->done
);
377 master
->mode_bits
= SPI_MODE_0
;
378 master
->num_chipselect
= hw
->pdata
->num_cs
;
379 master
->bus_num
= hw
->pdata
->bus_num
;
380 hw
->bitbang
.master
= hw
->master
;
381 hw
->bitbang
.setup_transfer
= nuc900_spi_setupxfer
;
382 hw
->bitbang
.chipselect
= nuc900_spi_chipsel
;
383 hw
->bitbang
.txrx_bufs
= nuc900_spi_txrx
;
384 hw
->bitbang
.master
->setup
= nuc900_spi_setup
;
386 hw
->res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
387 if (hw
->res
== NULL
) {
388 dev_err(&pdev
->dev
, "Cannot get IORESOURCE_MEM\n");
393 hw
->ioarea
= request_mem_region(hw
->res
->start
,
394 resource_size(hw
->res
), pdev
->name
);
396 if (hw
->ioarea
== NULL
) {
397 dev_err(&pdev
->dev
, "Cannot reserve region\n");
402 hw
->regs
= ioremap(hw
->res
->start
, resource_size(hw
->res
));
403 if (hw
->regs
== NULL
) {
404 dev_err(&pdev
->dev
, "Cannot map IO\n");
409 hw
->irq
= platform_get_irq(pdev
, 0);
411 dev_err(&pdev
->dev
, "No IRQ specified\n");
416 err
= request_irq(hw
->irq
, nuc900_spi_irq
, 0, pdev
->name
, hw
);
418 dev_err(&pdev
->dev
, "Cannot claim IRQ\n");
422 hw
->clk
= clk_get(&pdev
->dev
, "spi");
423 if (IS_ERR(hw
->clk
)) {
424 dev_err(&pdev
->dev
, "No clock for device\n");
425 err
= PTR_ERR(hw
->clk
);
429 mfp_set_groupg(&pdev
->dev
);
432 err
= spi_bitbang_start(&hw
->bitbang
);
434 dev_err(&pdev
->dev
, "Failed to register SPI master\n");
441 clk_disable(hw
->clk
);
444 free_irq(hw
->irq
, hw
);
448 release_mem_region(hw
->res
->start
, resource_size(hw
->res
));
451 spi_master_put(hw
->master
);
457 static int __devexit
nuc900_spi_remove(struct platform_device
*dev
)
459 struct nuc900_spi
*hw
= platform_get_drvdata(dev
);
461 free_irq(hw
->irq
, hw
);
463 platform_set_drvdata(dev
, NULL
);
465 spi_bitbang_stop(&hw
->bitbang
);
467 clk_disable(hw
->clk
);
472 release_mem_region(hw
->res
->start
, resource_size(hw
->res
));
475 spi_master_put(hw
->master
);
479 static struct platform_driver nuc900_spi_driver
= {
480 .probe
= nuc900_spi_probe
,
481 .remove
= __devexit_p(nuc900_spi_remove
),
483 .name
= "nuc900-spi",
484 .owner
= THIS_MODULE
,
488 static int __init
nuc900_spi_init(void)
490 return platform_driver_register(&nuc900_spi_driver
);
493 static void __exit
nuc900_spi_exit(void)
495 platform_driver_unregister(&nuc900_spi_driver
);
498 module_init(nuc900_spi_init
);
499 module_exit(nuc900_spi_exit
);
501 MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
502 MODULE_DESCRIPTION("nuc900 spi driver!");
503 MODULE_LICENSE("GPL");
504 MODULE_ALIAS("platform:nuc900-spi");