2 * omap_uwire.c -- 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 Yrjölä <juha.yrjola@nokia.com>
11 * Copyright (C) 2005 David Brownell (ported to 2.6 SPI interface)
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version.
18 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, write to the Free Software Foundation, Inc.,
31 * 675 Mass Ave, Cambridge, MA 02139, USA.
33 #include <linux/config.h>
34 #include <linux/kernel.h>
35 #include <linux/init.h>
36 #include <linux/delay.h>
37 #include <linux/platform_device.h>
38 #include <linux/interrupt.h>
39 #include <linux/err.h>
40 #include <linux/clk.h>
42 #include <linux/spi/spi.h>
43 #include <linux/spi/spi_bitbang.h>
45 #include <asm/system.h>
47 #include <asm/hardware.h>
49 #include <asm/mach-types.h>
51 #include <asm/arch/mux.h>
52 #include <asm/arch/omap730.h> /* OMAP730_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
59 #define UWIRE_BASE ((void *__iomem)IO_ADDRESS(UWIRE_BASE_PHYS))
61 /* uWire Registers: */
62 #define UWIRE_IO_SIZE 0x20
63 #define UWIRE_TDR 0x00
64 #define UWIRE_RDR 0x00
65 #define UWIRE_CSR 0x01
66 #define UWIRE_SR1 0x02
67 #define UWIRE_SR2 0x03
68 #define UWIRE_SR3 0x04
69 #define UWIRE_SR4 0x05
70 #define UWIRE_SR5 0x06
73 #define RDRB (1 << 15)
74 #define CSRB (1 << 14)
75 #define START (1 << 13)
76 #define CS_CMD (1 << 12)
79 #define UWIRE_READ_FALLING_EDGE 0x0000
80 #define UWIRE_READ_RISING_EDGE 0x0001
81 #define UWIRE_WRITE_FALLING_EDGE 0x0000
82 #define UWIRE_WRITE_RISING_EDGE 0x0002
83 #define UWIRE_CS_ACTIVE_LOW 0x0000
84 #define UWIRE_CS_ACTIVE_HIGH 0x0004
85 #define UWIRE_FREQ_DIV_2 0x0000
86 #define UWIRE_FREQ_DIV_4 0x0008
87 #define UWIRE_FREQ_DIV_8 0x0010
88 #define UWIRE_CHK_READY 0x0020
89 #define UWIRE_CLK_INVERTED 0x0040
93 struct spi_bitbang bitbang;
97 /* REVISIT compile time constant for idx_shift? */
98 static unsigned int uwire_idx_shift;
100 static inline void uwire_write_reg(int idx, u16 val)
102 __raw_writew(val, UWIRE_BASE + (idx << uwire_idx_shift));
105 static inline u16 uwire_read_reg(int idx)
107 return __raw_readw(UWIRE_BASE + (idx << uwire_idx_shift));
110 static inline void omap_uwire_configure_mode(u8 cs, unsigned long flags)
115 if (flags & UWIRE_CLK_INVERTED)
127 w = uwire_read_reg(reg);
128 w &= ~(0x3f << shift);
130 uwire_write_reg(reg, w);
133 static int wait_uwire_csr_flag(u16 mask, u16 val, int might_not_catch)
137 unsigned long max_jiffies = jiffies + HZ;
140 w = uwire_read_reg(UWIRE_CSR);
141 if ((w & mask) == val)
143 if (time_after(jiffies, max_jiffies)) {
144 printk(KERN_ERR "%s: timeout. reg=%#06x "
145 "mask=%#06x val=%#06x\n",
146 __FUNCTION__, w, mask, val);
150 if (might_not_catch && c > 64)
156 static void uwire_chipselect(struct spi_device *spi, int value)
161 BUG_ON(wait_uwire_csr_flag(CSRB, 0, 0));
163 w = uwire_read_reg(UWIRE_CSR);
164 old_cs = (w >> 10) & 0x03;
165 if (value == BITBANG_CS_INACTIVE || old_cs != spi->chip_select) {
166 /* Deselect this CS, or the previous CS */
168 uwire_write_reg(UWIRE_CSR, w);
170 /* activate specfied chipselect */
171 if (value == BITBANG_CS_ACTIVE) {
173 if (spi->mode & SPI_CPOL)
174 uwire_write_reg(UWIRE_SR4, 1);
176 uwire_write_reg(UWIRE_SR4, 0);
178 w = spi->chip_select << 10;
180 uwire_write_reg(UWIRE_CSR, w);
184 static int uwire_txrx(struct spi_device *spi, struct spi_transfer *t)
186 unsigned len = t->len;
187 unsigned bits = spi->bits_per_word;
192 if (!t->tx_buf && !t->rx_buf)
195 /* Microwire doesn't read and write concurrently */
196 if (t->tx_buf && t->rx_buf)
199 w = spi->chip_select << 10;
203 const u8 *buf = t->tx_buf;
205 /* NOTE: DMA could be used for TX transfers */
207 /* write one or two bytes at a time */
209 /* tx is msb-aligned */
211 if (len > 1 && (!bits || bits > 8)) {
217 if (!bits || bits > 8)
224 pr_debug("%s: write-%d =%04x\n",
225 spi->dev.bus_id, bits, val);
227 uwire_write_reg(UWIRE_TDR, val);
230 val = START | w | (bits << 5);
231 if (wait_uwire_csr_flag(CSRB, 0, 0))
234 uwire_write_reg(UWIRE_CSR, val);
237 /* Wait till write actually starts.
238 * This is needed with MPU clock 60+ MHz.
239 * REVISIT: we may not have time to catch it...
241 if (wait_uwire_csr_flag(CSRB, CSRB, 1))
247 /* REVISIT: save this for later to get more i/o overlap */
248 if (wait_uwire_csr_flag(CSRB, 0, 0))
251 } else if (t->rx_buf) {
254 /* read one or two bytes at a time */
256 if (len > 1 && (!bits || bits > 8)) {
261 if (!bits || bits > 8)
267 val = START | w | (bits << 0);
268 uwire_write_reg(UWIRE_CSR, val);
271 /* Wait till read actually starts */
272 (void) wait_uwire_csr_flag(CSRB, CSRB, 1);
274 if (wait_uwire_csr_flag(RDRB | CSRB,
278 /* rx is lsb-aligned */
279 val = uwire_read_reg(UWIRE_RDR);
280 val &= (1 << bits) - 1;
286 pr_debug("%s: read-%d =%04x\n",
287 spi->dev.bus_id, bits, val);
297 static int uwire_setup(struct spi_device *spi)
299 struct uwire_spi *uwire;
308 uwire = spi_master_get_devdata(spi->master);
310 if (spi->chip_select > 3) {
311 pr_debug("%s: cs%d?\n", spi->dev.bus_id, spi->chip_select);
316 if (spi->bits_per_word > 16) {
317 pr_debug("%s: wordsize %d?\n", spi->dev.bus_id,
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_FALLING_EDGE;
340 flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_RISING_EDGE;
344 /* assume it's already enabled */
345 rate = clk_get_rate(uwire->ck);
347 /* F_INT = mpu_per_clk / DIV1 */
348 for (div1_idx = 0; div1_idx < 4; div1_idx++) {
364 div2 = (rate / div1 + spi->max_speed_hz - 1) /
370 pr_debug("%s: lowest clock %ld, need %d\n",
371 spi->dev.bus_id, rate / 10 / 8, spi->max_speed_hz);
376 w = uwire_read_reg(UWIRE_SR3);
379 uwire_write_reg(UWIRE_SR3, w);
387 flags |= UWIRE_FREQ_DIV_2;
392 flags |= UWIRE_FREQ_DIV_4;
399 flags |= UWIRE_FREQ_DIV_8;
403 omap_uwire_configure_mode(spi->chip_select, flags);
404 pr_debug("%s: uwire flags %02x, armper %lu KHz, SCK %lu KHz\n",
406 clk_get_rate(uwire->ck) / 1000,
413 static void uwire_off(struct uwire_spi *uwire)
415 uwire_write_reg(UWIRE_SR3, 0);
416 clk_disable(uwire->ck);
418 spi_master_put(uwire->bitbang.master);
421 static int uwire_probe(struct platform_device *pdev)
423 struct spi_master *master;
424 struct uwire_spi *uwire;
427 master = spi_alloc_master(&pdev->dev, sizeof *uwire);
431 uwire = spi_master_get_devdata(master);
432 dev_set_drvdata(&pdev->dev, uwire);
434 uwire->ck = clk_get(&pdev->dev, "armper_ck");
435 if (!uwire->ck || IS_ERR(uwire->ck)) {
436 dev_dbg(&pdev->dev, "no mpu_per_clk ?\n");
437 spi_master_put(master);
440 clk_enable(uwire->ck);
442 if (cpu_is_omap730())
447 uwire_write_reg(UWIRE_SR3, 1);
449 master->bus_num = 2; /* "official" */
450 master->num_chipselect = 4;
451 master->setup = uwire_setup;
453 uwire->bitbang.master = master;
454 uwire->bitbang.chipselect = uwire_chipselect;
455 uwire->bitbang.txrx_bufs = uwire_txrx;
457 status = spi_bitbang_start(&uwire->bitbang);
463 static int uwire_remove(struct platform_device *pdev)
465 struct uwire_spi *uwire = dev_get_drvdata(&pdev->dev);
468 // FIXME remove all child devices, somewhere ...
470 status = spi_bitbang_stop(&uwire->bitbang);
475 static struct platform_driver uwire_driver = {
477 .name = "omap_uwire",
478 .bus = &platform_bus_type,
479 .owner = THIS_MODULE,
481 .probe = uwire_probe,
482 .remove = uwire_remove,
483 // suspend ... unuse ck
487 static int __init omap_uwire_init(void)
489 /* FIXME move these into the relevant board init code. also, include
490 * H3 support; it uses tsc2101 like H2 (on a different chipselect).
493 if (machine_is_omap_h2()) {
494 /* defaults: W21 SDO, U18 SDI, V19 SCL */
495 omap_cfg_reg(N14_1610_UWIRE_CS0);
496 omap_cfg_reg(N15_1610_UWIRE_CS1);
498 if (machine_is_omap_perseus2()) {
499 /* configure pins: MPU_UW_nSCS1, MPU_UW_SDO, MPU_UW_SCLK */
500 int val = omap_readl(OMAP730_IO_CONF_9) & ~0x00EEE000;
501 omap_writel(val | 0x00AAA000, OMAP730_IO_CONF_9);
504 return platform_driver_register(&uwire_driver);
507 static void __exit omap_uwire_exit(void)
509 platform_driver_unregister(&uwire_driver);
512 subsys_initcall(omap_uwire_init);
513 module_exit(omap_uwire_exit);
515 MODULE_LICENSE("GPL");