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 <asm/mach-types.h>
48 #include <linux/soc/ti/omap1-io.h>
49 #include <linux/soc/ti/omap1-soc.h>
50 #include <linux/soc/ti/omap1-mux.h>
52 /* FIXME address is now a platform device resource,
53 * and irqs should show there too...
55 #define UWIRE_BASE_PHYS 0xFFFB3000
57 /* uWire Registers: */
58 #define UWIRE_IO_SIZE 0x20
59 #define UWIRE_TDR 0x00
60 #define UWIRE_RDR 0x00
61 #define UWIRE_CSR 0x01
62 #define UWIRE_SR1 0x02
63 #define UWIRE_SR2 0x03
64 #define UWIRE_SR3 0x04
65 #define UWIRE_SR4 0x05
66 #define UWIRE_SR5 0x06
69 #define RDRB (1 << 15)
70 #define CSRB (1 << 14)
71 #define START (1 << 13)
72 #define CS_CMD (1 << 12)
75 #define UWIRE_READ_FALLING_EDGE 0x0001
76 #define UWIRE_READ_RISING_EDGE 0x0000
77 #define UWIRE_WRITE_FALLING_EDGE 0x0000
78 #define UWIRE_WRITE_RISING_EDGE 0x0002
79 #define UWIRE_CS_ACTIVE_LOW 0x0000
80 #define UWIRE_CS_ACTIVE_HIGH 0x0004
81 #define UWIRE_FREQ_DIV_2 0x0000
82 #define UWIRE_FREQ_DIV_4 0x0008
83 #define UWIRE_FREQ_DIV_8 0x0010
84 #define UWIRE_CHK_READY 0x0020
85 #define UWIRE_CLK_INVERTED 0x0040
89 struct spi_bitbang bitbang;
97 /* REVISIT compile time constant for idx_shift? */
99 * Or, put it in a structure which is used throughout the driver;
100 * that avoids having to issue two loads for each bit of static data.
102 static unsigned int uwire_idx_shift = 2;
103 static void __iomem *uwire_base;
105 static inline void uwire_write_reg(int idx, u16 val)
107 __raw_writew(val, uwire_base + (idx << uwire_idx_shift));
110 static inline u16 uwire_read_reg(int idx)
112 return __raw_readw(uwire_base + (idx << uwire_idx_shift));
115 static inline void omap_uwire_configure_mode(u8 cs, unsigned long flags)
120 if (flags & UWIRE_CLK_INVERTED)
132 w = uwire_read_reg(reg);
133 w &= ~(0x3f << shift);
135 uwire_write_reg(reg, w);
138 static int wait_uwire_csr_flag(u16 mask, u16 val, int might_not_catch)
142 unsigned long max_jiffies = jiffies + HZ;
145 w = uwire_read_reg(UWIRE_CSR);
146 if ((w & mask) == val)
148 if (time_after(jiffies, max_jiffies)) {
149 printk(KERN_ERR "%s: timeout. reg=%#06x "
150 "mask=%#06x val=%#06x\n",
151 __func__, w, mask, val);
155 if (might_not_catch && c > 64)
161 static void uwire_set_clk1_div(int div1_idx)
165 w = uwire_read_reg(UWIRE_SR3);
168 uwire_write_reg(UWIRE_SR3, w);
171 static void uwire_chipselect(struct spi_device *spi, int value)
173 struct uwire_state *ust = spi->controller_state;
178 BUG_ON(wait_uwire_csr_flag(CSRB, 0, 0));
180 w = uwire_read_reg(UWIRE_CSR);
181 old_cs = (w >> 10) & 0x03;
182 if (value == BITBANG_CS_INACTIVE || old_cs != spi_get_chipselect(spi, 0)) {
183 /* Deselect this CS, or the previous CS */
185 uwire_write_reg(UWIRE_CSR, w);
187 /* activate specfied chipselect */
188 if (value == BITBANG_CS_ACTIVE) {
189 uwire_set_clk1_div(ust->div1_idx);
191 if (spi->mode & SPI_CPOL)
192 uwire_write_reg(UWIRE_SR4, 1);
194 uwire_write_reg(UWIRE_SR4, 0);
196 w = spi_get_chipselect(spi, 0) << 10;
198 uwire_write_reg(UWIRE_CSR, w);
202 static int uwire_txrx(struct spi_device *spi, struct spi_transfer *t)
204 unsigned len = t->len;
205 unsigned bits = t->bits_per_word;
210 if (!t->tx_buf && !t->rx_buf)
213 w = spi_get_chipselect(spi, 0) << 10;
217 const u8 *buf = t->tx_buf;
219 /* NOTE: DMA could be used for TX transfers */
221 /* write one or two bytes at a time */
223 /* tx bit 15 is first sent; we byteswap multibyte words
224 * (msb-first) on the way out from memory.
235 pr_debug("%s: write-%d =%04x\n",
236 dev_name(&spi->dev), bits, val);
238 if (wait_uwire_csr_flag(CSRB, 0, 0))
241 uwire_write_reg(UWIRE_TDR, val);
244 val = START | w | (bits << 5);
246 uwire_write_reg(UWIRE_CSR, val);
249 /* Wait till write actually starts.
250 * This is needed with MPU clock 60+ MHz.
251 * REVISIT: we may not have time to catch it...
253 if (wait_uwire_csr_flag(CSRB, CSRB, 1))
259 /* REVISIT: save this for later to get more i/o overlap */
260 if (wait_uwire_csr_flag(CSRB, 0, 0))
263 } else if (t->rx_buf) {
266 /* read one or two bytes at a time */
274 val = START | w | (bits << 0);
275 uwire_write_reg(UWIRE_CSR, val);
278 /* Wait till read actually starts */
279 (void) wait_uwire_csr_flag(CSRB, CSRB, 1);
281 if (wait_uwire_csr_flag(RDRB | CSRB,
285 /* rx bit 0 is last received; multibyte words will
286 * be properly byteswapped on the way to memory.
288 val = uwire_read_reg(UWIRE_RDR);
289 val &= (1 << bits) - 1;
295 pr_debug("%s: read-%d =%04x\n",
296 dev_name(&spi->dev), bits, val);
306 static int uwire_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
308 struct uwire_state *ust = spi->controller_state;
309 struct uwire_spi *uwire;
318 uwire = spi_master_get_devdata(spi->master);
320 /* mode 0..3, clock inverted separately;
321 * standard nCS signaling;
322 * don't treat DI=high as "not ready"
324 if (spi->mode & SPI_CS_HIGH)
325 flags |= UWIRE_CS_ACTIVE_HIGH;
327 if (spi->mode & SPI_CPOL)
328 flags |= UWIRE_CLK_INVERTED;
330 switch (spi->mode & SPI_MODE_X_MASK) {
333 flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_RISING_EDGE;
337 flags |= UWIRE_WRITE_RISING_EDGE | UWIRE_READ_FALLING_EDGE;
341 /* assume it's already enabled */
342 rate = clk_get_rate(uwire->ck);
347 hz = spi->max_speed_hz;
350 pr_debug("%s: zero speed?\n", dev_name(&spi->dev));
355 /* F_INT = mpu_xor_clk / DIV1 */
356 for (div1_idx = 0; div1_idx < 4; div1_idx++) {
372 div2 = (rate / div1 + hz - 1) / hz;
377 pr_debug("%s: lowest clock %ld, need %d\n",
378 dev_name(&spi->dev), rate / 10 / 8, hz);
383 /* we have to cache this and reset in uwire_chipselect as this is a
384 * global parameter and another uwire device can change it under
386 ust->div1_idx = div1_idx;
387 uwire_set_clk1_div(div1_idx);
395 flags |= UWIRE_FREQ_DIV_2;
400 flags |= UWIRE_FREQ_DIV_4;
407 flags |= UWIRE_FREQ_DIV_8;
411 omap_uwire_configure_mode(spi_get_chipselect(spi, 0), flags);
412 pr_debug("%s: uwire flags %02x, armxor %lu KHz, SCK %lu KHz\n",
414 clk_get_rate(uwire->ck) / 1000,
421 static int uwire_setup(struct spi_device *spi)
423 struct uwire_state *ust = spi->controller_state;
424 bool initial_setup = false;
428 ust = kzalloc(sizeof(*ust), GFP_KERNEL);
431 spi->controller_state = ust;
432 initial_setup = true;
435 status = uwire_setup_transfer(spi, NULL);
436 if (status && initial_setup)
442 static void uwire_cleanup(struct spi_device *spi)
444 kfree(spi->controller_state);
447 static void uwire_off(struct uwire_spi *uwire)
449 uwire_write_reg(UWIRE_SR3, 0);
450 clk_disable_unprepare(uwire->ck);
451 spi_master_put(uwire->bitbang.master);
454 static int uwire_probe(struct platform_device *pdev)
456 struct spi_master *master;
457 struct uwire_spi *uwire;
460 master = spi_alloc_master(&pdev->dev, sizeof(*uwire));
464 uwire = spi_master_get_devdata(master);
466 uwire_base = devm_ioremap(&pdev->dev, UWIRE_BASE_PHYS, UWIRE_IO_SIZE);
468 dev_dbg(&pdev->dev, "can't ioremap UWIRE\n");
469 spi_master_put(master);
473 platform_set_drvdata(pdev, uwire);
475 uwire->ck = devm_clk_get(&pdev->dev, "fck");
476 if (IS_ERR(uwire->ck)) {
477 status = PTR_ERR(uwire->ck);
478 dev_dbg(&pdev->dev, "no functional clock?\n");
479 spi_master_put(master);
482 clk_prepare_enable(uwire->ck);
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_CONTROLLER_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 void 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);
518 /* work with hotplug and coldplug */
519 MODULE_ALIAS("platform:omap_uwire");
521 static struct platform_driver uwire_driver = {
523 .name = "omap_uwire",
525 .probe = uwire_probe,
526 .remove_new = uwire_remove,
527 // suspend ... unuse ck
531 static int __init omap_uwire_init(void)
533 return platform_driver_register(&uwire_driver);
536 static void __exit omap_uwire_exit(void)
538 platform_driver_unregister(&uwire_driver);
541 subsys_initcall(omap_uwire_init);
542 module_exit(omap_uwire_exit);
544 MODULE_LICENSE("GPL");