2 * MicroWire interface driver for OMAP
4 * Copyright 2003 MontaVista Software Inc. <source@mvista.com>
6 * Ported to 2.6 OMAP uwire interface.
7 * Copyright (C) 2004 Texas Instruments.
9 * Generalization patches by Juha Yrjola <juha.yrjola@nokia.com>
11 * Copyright (C) 2005 David Brownell (ported to 2.6 SPI interface)
12 * Copyright (C) 2006 Nokia
14 * Many updates by Imre Deak <imre.deak@nokia.com>
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version.
21 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <linux/kernel.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
35 #include <linux/platform_device.h>
36 #include <linux/interrupt.h>
37 #include <linux/err.h>
38 #include <linux/clk.h>
39 #include <linux/slab.h>
40 #include <linux/device.h>
42 #include <linux/spi/spi.h>
43 #include <linux/spi/spi_bitbang.h>
44 #include <linux/module.h>
47 #include <mach/hardware.h>
48 #include <asm/mach-types.h>
52 #include <mach/omap7xx.h> /* OMAP7XX_IO_CONF registers */
55 /* FIXME address is now a platform device resource,
56 * and irqs should show there too...
58 #define UWIRE_BASE_PHYS 0xFFFB3000
60 /* uWire Registers: */
61 #define UWIRE_IO_SIZE 0x20
62 #define UWIRE_TDR 0x00
63 #define UWIRE_RDR 0x00
64 #define UWIRE_CSR 0x01
65 #define UWIRE_SR1 0x02
66 #define UWIRE_SR2 0x03
67 #define UWIRE_SR3 0x04
68 #define UWIRE_SR4 0x05
69 #define UWIRE_SR5 0x06
72 #define RDRB (1 << 15)
73 #define CSRB (1 << 14)
74 #define START (1 << 13)
75 #define CS_CMD (1 << 12)
78 #define UWIRE_READ_FALLING_EDGE 0x0001
79 #define UWIRE_READ_RISING_EDGE 0x0000
80 #define UWIRE_WRITE_FALLING_EDGE 0x0000
81 #define UWIRE_WRITE_RISING_EDGE 0x0002
82 #define UWIRE_CS_ACTIVE_LOW 0x0000
83 #define UWIRE_CS_ACTIVE_HIGH 0x0004
84 #define UWIRE_FREQ_DIV_2 0x0000
85 #define UWIRE_FREQ_DIV_4 0x0008
86 #define UWIRE_FREQ_DIV_8 0x0010
87 #define UWIRE_CHK_READY 0x0020
88 #define UWIRE_CLK_INVERTED 0x0040
92 struct spi_bitbang bitbang
;
100 /* REVISIT compile time constant for idx_shift? */
102 * Or, put it in a structure which is used throughout the driver;
103 * that avoids having to issue two loads for each bit of static data.
105 static unsigned int uwire_idx_shift
;
106 static void __iomem
*uwire_base
;
108 static inline void uwire_write_reg(int idx
, u16 val
)
110 __raw_writew(val
, uwire_base
+ (idx
<< uwire_idx_shift
));
113 static inline u16
uwire_read_reg(int idx
)
115 return __raw_readw(uwire_base
+ (idx
<< uwire_idx_shift
));
118 static inline void omap_uwire_configure_mode(u8 cs
, unsigned long flags
)
123 if (flags
& UWIRE_CLK_INVERTED
)
135 w
= uwire_read_reg(reg
);
136 w
&= ~(0x3f << shift
);
138 uwire_write_reg(reg
, w
);
141 static int wait_uwire_csr_flag(u16 mask
, u16 val
, int might_not_catch
)
145 unsigned long max_jiffies
= jiffies
+ HZ
;
148 w
= uwire_read_reg(UWIRE_CSR
);
149 if ((w
& mask
) == val
)
151 if (time_after(jiffies
, max_jiffies
)) {
152 printk(KERN_ERR
"%s: timeout. reg=%#06x "
153 "mask=%#06x val=%#06x\n",
154 __func__
, w
, mask
, val
);
158 if (might_not_catch
&& c
> 64)
164 static void uwire_set_clk1_div(int div1_idx
)
168 w
= uwire_read_reg(UWIRE_SR3
);
171 uwire_write_reg(UWIRE_SR3
, w
);
174 static void uwire_chipselect(struct spi_device
*spi
, int value
)
176 struct uwire_state
*ust
= spi
->controller_state
;
181 BUG_ON(wait_uwire_csr_flag(CSRB
, 0, 0));
183 w
= uwire_read_reg(UWIRE_CSR
);
184 old_cs
= (w
>> 10) & 0x03;
185 if (value
== BITBANG_CS_INACTIVE
|| old_cs
!= spi
->chip_select
) {
186 /* Deselect this CS, or the previous CS */
188 uwire_write_reg(UWIRE_CSR
, w
);
190 /* activate specfied chipselect */
191 if (value
== BITBANG_CS_ACTIVE
) {
192 uwire_set_clk1_div(ust
->div1_idx
);
194 if (spi
->mode
& SPI_CPOL
)
195 uwire_write_reg(UWIRE_SR4
, 1);
197 uwire_write_reg(UWIRE_SR4
, 0);
199 w
= spi
->chip_select
<< 10;
201 uwire_write_reg(UWIRE_CSR
, w
);
205 static int uwire_txrx(struct spi_device
*spi
, struct spi_transfer
*t
)
207 unsigned len
= t
->len
;
208 unsigned bits
= t
->bits_per_word
? : spi
->bits_per_word
;
213 if (!t
->tx_buf
&& !t
->rx_buf
)
216 w
= spi
->chip_select
<< 10;
220 const u8
*buf
= t
->tx_buf
;
222 /* NOTE: DMA could be used for TX transfers */
224 /* write one or two bytes at a time */
226 /* tx bit 15 is first sent; we byteswap multibyte words
227 * (msb-first) on the way out from memory.
238 pr_debug("%s: write-%d =%04x\n",
239 dev_name(&spi
->dev
), bits
, val
);
241 if (wait_uwire_csr_flag(CSRB
, 0, 0))
244 uwire_write_reg(UWIRE_TDR
, val
);
247 val
= START
| w
| (bits
<< 5);
249 uwire_write_reg(UWIRE_CSR
, val
);
252 /* Wait till write actually starts.
253 * This is needed with MPU clock 60+ MHz.
254 * REVISIT: we may not have time to catch it...
256 if (wait_uwire_csr_flag(CSRB
, CSRB
, 1))
262 /* REVISIT: save this for later to get more i/o overlap */
263 if (wait_uwire_csr_flag(CSRB
, 0, 0))
266 } else if (t
->rx_buf
) {
269 /* read one or two bytes at a time */
277 val
= START
| w
| (bits
<< 0);
278 uwire_write_reg(UWIRE_CSR
, val
);
281 /* Wait till read actually starts */
282 (void) wait_uwire_csr_flag(CSRB
, CSRB
, 1);
284 if (wait_uwire_csr_flag(RDRB
| CSRB
,
288 /* rx bit 0 is last received; multibyte words will
289 * be properly byteswapped on the way to memory.
291 val
= uwire_read_reg(UWIRE_RDR
);
292 val
&= (1 << bits
) - 1;
298 pr_debug("%s: read-%d =%04x\n",
299 dev_name(&spi
->dev
), bits
, val
);
309 static int uwire_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
311 struct uwire_state
*ust
= spi
->controller_state
;
312 struct uwire_spi
*uwire
;
321 uwire
= spi_master_get_devdata(spi
->master
);
323 /* mode 0..3, clock inverted separately;
324 * standard nCS signaling;
325 * don't treat DI=high as "not ready"
327 if (spi
->mode
& SPI_CS_HIGH
)
328 flags
|= UWIRE_CS_ACTIVE_HIGH
;
330 if (spi
->mode
& SPI_CPOL
)
331 flags
|= UWIRE_CLK_INVERTED
;
333 switch (spi
->mode
& (SPI_CPOL
| SPI_CPHA
)) {
336 flags
|= UWIRE_WRITE_FALLING_EDGE
| UWIRE_READ_RISING_EDGE
;
340 flags
|= UWIRE_WRITE_RISING_EDGE
| UWIRE_READ_FALLING_EDGE
;
344 /* assume it's already enabled */
345 rate
= clk_get_rate(uwire
->ck
);
347 hz
= spi
->max_speed_hz
;
348 if (t
!= NULL
&& t
->speed_hz
)
352 pr_debug("%s: zero speed?\n", dev_name(&spi
->dev
));
357 /* F_INT = mpu_xor_clk / DIV1 */
358 for (div1_idx
= 0; div1_idx
< 4; div1_idx
++) {
374 div2
= (rate
/ div1
+ hz
- 1) / hz
;
379 pr_debug("%s: lowest clock %ld, need %d\n",
380 dev_name(&spi
->dev
), rate
/ 10 / 8, hz
);
385 /* we have to cache this and reset in uwire_chipselect as this is a
386 * global parameter and another uwire device can change it under
388 ust
->div1_idx
= div1_idx
;
389 uwire_set_clk1_div(div1_idx
);
397 flags
|= UWIRE_FREQ_DIV_2
;
402 flags
|= UWIRE_FREQ_DIV_4
;
409 flags
|= UWIRE_FREQ_DIV_8
;
413 omap_uwire_configure_mode(spi
->chip_select
, flags
);
414 pr_debug("%s: uwire flags %02x, armxor %lu KHz, SCK %lu KHz\n",
416 clk_get_rate(uwire
->ck
) / 1000,
423 static int uwire_setup(struct spi_device
*spi
)
425 struct uwire_state
*ust
= spi
->controller_state
;
428 ust
= kzalloc(sizeof(*ust
), GFP_KERNEL
);
431 spi
->controller_state
= ust
;
434 return uwire_setup_transfer(spi
, NULL
);
437 static void uwire_cleanup(struct spi_device
*spi
)
439 kfree(spi
->controller_state
);
442 static void uwire_off(struct uwire_spi
*uwire
)
444 uwire_write_reg(UWIRE_SR3
, 0);
445 clk_disable(uwire
->ck
);
446 spi_master_put(uwire
->bitbang
.master
);
449 static int uwire_probe(struct platform_device
*pdev
)
451 struct spi_master
*master
;
452 struct uwire_spi
*uwire
;
455 master
= spi_alloc_master(&pdev
->dev
, sizeof *uwire
);
459 uwire
= spi_master_get_devdata(master
);
461 uwire_base
= devm_ioremap(&pdev
->dev
, UWIRE_BASE_PHYS
, UWIRE_IO_SIZE
);
463 dev_dbg(&pdev
->dev
, "can't ioremap UWIRE\n");
464 spi_master_put(master
);
468 platform_set_drvdata(pdev
, uwire
);
470 uwire
->ck
= devm_clk_get(&pdev
->dev
, "fck");
471 if (IS_ERR(uwire
->ck
)) {
472 status
= PTR_ERR(uwire
->ck
);
473 dev_dbg(&pdev
->dev
, "no functional clock?\n");
474 spi_master_put(master
);
477 clk_enable(uwire
->ck
);
479 if (cpu_is_omap7xx())
484 uwire_write_reg(UWIRE_SR3
, 1);
486 /* the spi->mode bits understood by this driver: */
487 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
488 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(1, 16);
489 master
->flags
= SPI_MASTER_HALF_DUPLEX
;
491 master
->bus_num
= 2; /* "official" */
492 master
->num_chipselect
= 4;
493 master
->setup
= uwire_setup
;
494 master
->cleanup
= uwire_cleanup
;
496 uwire
->bitbang
.master
= master
;
497 uwire
->bitbang
.chipselect
= uwire_chipselect
;
498 uwire
->bitbang
.setup_transfer
= uwire_setup_transfer
;
499 uwire
->bitbang
.txrx_bufs
= uwire_txrx
;
501 status
= spi_bitbang_start(&uwire
->bitbang
);
508 static int uwire_remove(struct platform_device
*pdev
)
510 struct uwire_spi
*uwire
= platform_get_drvdata(pdev
);
512 // FIXME remove all child devices, somewhere ...
514 spi_bitbang_stop(&uwire
->bitbang
);
519 /* work with hotplug and coldplug */
520 MODULE_ALIAS("platform:omap_uwire");
522 static struct platform_driver uwire_driver
= {
524 .name
= "omap_uwire",
526 .probe
= uwire_probe
,
527 .remove
= uwire_remove
,
528 // suspend ... unuse ck
532 static int __init
omap_uwire_init(void)
534 /* FIXME move these into the relevant board init code. also, include
535 * H3 support; it uses tsc2101 like H2 (on a different chipselect).
538 if (machine_is_omap_h2()) {
539 /* defaults: W21 SDO, U18 SDI, V19 SCL */
540 omap_cfg_reg(N14_1610_UWIRE_CS0
);
541 omap_cfg_reg(N15_1610_UWIRE_CS1
);
543 if (machine_is_omap_perseus2()) {
544 /* configure pins: MPU_UW_nSCS1, MPU_UW_SDO, MPU_UW_SCLK */
545 int val
= omap_readl(OMAP7XX_IO_CONF_9
) & ~0x00EEE000;
546 omap_writel(val
| 0x00AAA000, OMAP7XX_IO_CONF_9
);
549 return platform_driver_register(&uwire_driver
);
552 static void __exit
omap_uwire_exit(void)
554 platform_driver_unregister(&uwire_driver
);
557 subsys_initcall(omap_uwire_init
);
558 module_exit(omap_uwire_exit
);
560 MODULE_LICENSE("GPL");