blob: 95cc6948a2ba3f1579f39525c8d70eaec8c9ca66 [file] [log] [blame]
David Brownellfdb3c182007-02-12 00:52:37 -08001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * MicroWire interface driver for OMAP
David Brownellfdb3c182007-02-12 00:52:37 -08003 *
4 * Copyright 2003 MontaVista Software Inc. <source@mvista.com>
5 *
6 * Ported to 2.6 OMAP uwire interface.
7 * Copyright (C) 2004 Texas Instruments.
8 *
9 * Generalization patches by Juha Yrjola <juha.yrjola@nokia.com>
10 *
11 * Copyright (C) 2005 David Brownell (ported to 2.6 SPI interface)
12 * Copyright (C) 2006 Nokia
13 *
14 * Many updates by Imre Deak <imre.deak@nokia.com>
15 *
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.
20 *
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.
31 *
32 * You should have received a copy of the GNU General Public License along
33 * with this program; if not, write to the Free Software Foundation, Inc.,
34 * 675 Mass Ave, Cambridge, MA 02139, USA.
35 */
36#include <linux/kernel.h>
37#include <linux/init.h>
38#include <linux/delay.h>
39#include <linux/platform_device.h>
40#include <linux/workqueue.h>
41#include <linux/interrupt.h>
42#include <linux/err.h>
43#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
David Brownellfdb3c182007-02-12 00:52:37 -080045
46#include <linux/spi/spi.h>
47#include <linux/spi/spi_bitbang.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040048#include <linux/module.h>
David Brownellfdb3c182007-02-12 00:52:37 -080049
David Brownellfdb3c182007-02-12 00:52:37 -080050#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010051#include <mach/hardware.h>
David Brownellfdb3c182007-02-12 00:52:37 -080052#include <asm/io.h>
53#include <asm/mach-types.h>
54
Tony Lindgren70c494c2012-09-19 10:46:56 -070055#include <mach/mux.h>
Tony Lindgren68cb7002012-08-31 17:04:35 -070056
57#include <mach/omap7xx.h> /* OMAP7XX_IO_CONF registers */
David Brownellfdb3c182007-02-12 00:52:37 -080058
59
60/* FIXME address is now a platform device resource,
61 * and irqs should show there too...
62 */
63#define UWIRE_BASE_PHYS 0xFFFB3000
David Brownellfdb3c182007-02-12 00:52:37 -080064
65/* uWire Registers: */
66#define UWIRE_IO_SIZE 0x20
67#define UWIRE_TDR 0x00
68#define UWIRE_RDR 0x00
69#define UWIRE_CSR 0x01
70#define UWIRE_SR1 0x02
71#define UWIRE_SR2 0x03
72#define UWIRE_SR3 0x04
73#define UWIRE_SR4 0x05
74#define UWIRE_SR5 0x06
75
76/* CSR bits */
77#define RDRB (1 << 15)
78#define CSRB (1 << 14)
79#define START (1 << 13)
80#define CS_CMD (1 << 12)
81
82/* SR1 or SR2 bits */
83#define UWIRE_READ_FALLING_EDGE 0x0001
84#define UWIRE_READ_RISING_EDGE 0x0000
85#define UWIRE_WRITE_FALLING_EDGE 0x0000
86#define UWIRE_WRITE_RISING_EDGE 0x0002
87#define UWIRE_CS_ACTIVE_LOW 0x0000
88#define UWIRE_CS_ACTIVE_HIGH 0x0004
89#define UWIRE_FREQ_DIV_2 0x0000
90#define UWIRE_FREQ_DIV_4 0x0008
91#define UWIRE_FREQ_DIV_8 0x0010
92#define UWIRE_CHK_READY 0x0020
93#define UWIRE_CLK_INVERTED 0x0040
94
95
96struct uwire_spi {
97 struct spi_bitbang bitbang;
98 struct clk *ck;
99};
100
101struct uwire_state {
David Brownellfdb3c182007-02-12 00:52:37 -0800102 unsigned div1_idx;
103};
104
105/* REVISIT compile time constant for idx_shift? */
Russell King55c381e2008-09-04 14:07:22 +0100106/*
107 * Or, put it in a structure which is used throughout the driver;
108 * that avoids having to issue two loads for each bit of static data.
109 */
David Brownellfdb3c182007-02-12 00:52:37 -0800110static unsigned int uwire_idx_shift;
Russell King55c381e2008-09-04 14:07:22 +0100111static void __iomem *uwire_base;
David Brownellfdb3c182007-02-12 00:52:37 -0800112
113static inline void uwire_write_reg(int idx, u16 val)
114{
Russell King55c381e2008-09-04 14:07:22 +0100115 __raw_writew(val, uwire_base + (idx << uwire_idx_shift));
David Brownellfdb3c182007-02-12 00:52:37 -0800116}
117
118static inline u16 uwire_read_reg(int idx)
119{
Russell King55c381e2008-09-04 14:07:22 +0100120 return __raw_readw(uwire_base + (idx << uwire_idx_shift));
David Brownellfdb3c182007-02-12 00:52:37 -0800121}
122
123static inline void omap_uwire_configure_mode(u8 cs, unsigned long flags)
124{
125 u16 w, val = 0;
126 int shift, reg;
127
128 if (flags & UWIRE_CLK_INVERTED)
129 val ^= 0x03;
130 val = flags & 0x3f;
131 if (cs & 1)
132 shift = 6;
133 else
134 shift = 0;
135 if (cs <= 1)
136 reg = UWIRE_SR1;
137 else
138 reg = UWIRE_SR2;
139
140 w = uwire_read_reg(reg);
141 w &= ~(0x3f << shift);
142 w |= val << shift;
143 uwire_write_reg(reg, w);
144}
145
146static int wait_uwire_csr_flag(u16 mask, u16 val, int might_not_catch)
147{
148 u16 w;
149 int c = 0;
150 unsigned long max_jiffies = jiffies + HZ;
151
152 for (;;) {
153 w = uwire_read_reg(UWIRE_CSR);
154 if ((w & mask) == val)
155 break;
156 if (time_after(jiffies, max_jiffies)) {
157 printk(KERN_ERR "%s: timeout. reg=%#06x "
158 "mask=%#06x val=%#06x\n",
Harvey Harrisonb687d2a2008-04-28 02:14:19 -0700159 __func__, w, mask, val);
David Brownellfdb3c182007-02-12 00:52:37 -0800160 return -1;
161 }
162 c++;
163 if (might_not_catch && c > 64)
164 break;
165 }
166 return 0;
167}
168
169static void uwire_set_clk1_div(int div1_idx)
170{
171 u16 w;
172
173 w = uwire_read_reg(UWIRE_SR3);
174 w &= ~(0x03 << 1);
175 w |= div1_idx << 1;
176 uwire_write_reg(UWIRE_SR3, w);
177}
178
179static void uwire_chipselect(struct spi_device *spi, int value)
180{
181 struct uwire_state *ust = spi->controller_state;
182 u16 w;
183 int old_cs;
184
185
186 BUG_ON(wait_uwire_csr_flag(CSRB, 0, 0));
187
188 w = uwire_read_reg(UWIRE_CSR);
189 old_cs = (w >> 10) & 0x03;
190 if (value == BITBANG_CS_INACTIVE || old_cs != spi->chip_select) {
191 /* Deselect this CS, or the previous CS */
192 w &= ~CS_CMD;
193 uwire_write_reg(UWIRE_CSR, w);
194 }
195 /* activate specfied chipselect */
196 if (value == BITBANG_CS_ACTIVE) {
197 uwire_set_clk1_div(ust->div1_idx);
198 /* invert clock? */
199 if (spi->mode & SPI_CPOL)
200 uwire_write_reg(UWIRE_SR4, 1);
201 else
202 uwire_write_reg(UWIRE_SR4, 0);
203
204 w = spi->chip_select << 10;
205 w |= CS_CMD;
206 uwire_write_reg(UWIRE_CSR, w);
207 }
208}
209
210static int uwire_txrx(struct spi_device *spi, struct spi_transfer *t)
211{
David Brownellfdb3c182007-02-12 00:52:37 -0800212 unsigned len = t->len;
Axel Lin790fc552014-02-15 14:26:21 +0800213 unsigned bits = t->bits_per_word ? : spi->bits_per_word;
David Brownellfdb3c182007-02-12 00:52:37 -0800214 unsigned bytes;
215 u16 val, w;
Joe Perchesa419aef2009-08-18 11:18:35 -0700216 int status = 0;
David Brownellfdb3c182007-02-12 00:52:37 -0800217
218 if (!t->tx_buf && !t->rx_buf)
219 return 0;
220
221 /* Microwire doesn't read and write concurrently */
222 if (t->tx_buf && t->rx_buf)
223 return -EPERM;
224
225 w = spi->chip_select << 10;
226 w |= CS_CMD;
227
228 if (t->tx_buf) {
229 const u8 *buf = t->tx_buf;
230
231 /* NOTE: DMA could be used for TX transfers */
232
233 /* write one or two bytes at a time */
234 while (len >= 1) {
235 /* tx bit 15 is first sent; we byteswap multibyte words
236 * (msb-first) on the way out from memory.
237 */
238 val = *buf++;
239 if (bits > 8) {
240 bytes = 2;
241 val |= *buf++ << 8;
242 } else
243 bytes = 1;
244 val <<= 16 - bits;
245
246#ifdef VERBOSE
247 pr_debug("%s: write-%d =%04x\n",
Kay Sievers6c7377a2009-03-24 16:38:21 -0700248 dev_name(&spi->dev), bits, val);
David Brownellfdb3c182007-02-12 00:52:37 -0800249#endif
250 if (wait_uwire_csr_flag(CSRB, 0, 0))
251 goto eio;
252
253 uwire_write_reg(UWIRE_TDR, val);
254
255 /* start write */
256 val = START | w | (bits << 5);
257
258 uwire_write_reg(UWIRE_CSR, val);
259 len -= bytes;
260
261 /* Wait till write actually starts.
262 * This is needed with MPU clock 60+ MHz.
263 * REVISIT: we may not have time to catch it...
264 */
265 if (wait_uwire_csr_flag(CSRB, CSRB, 1))
266 goto eio;
267
268 status += bytes;
269 }
270
271 /* REVISIT: save this for later to get more i/o overlap */
272 if (wait_uwire_csr_flag(CSRB, 0, 0))
273 goto eio;
274
275 } else if (t->rx_buf) {
276 u8 *buf = t->rx_buf;
277
278 /* read one or two bytes at a time */
279 while (len) {
280 if (bits > 8) {
281 bytes = 2;
282 } else
283 bytes = 1;
284
285 /* start read */
286 val = START | w | (bits << 0);
287 uwire_write_reg(UWIRE_CSR, val);
288 len -= bytes;
289
290 /* Wait till read actually starts */
291 (void) wait_uwire_csr_flag(CSRB, CSRB, 1);
292
293 if (wait_uwire_csr_flag(RDRB | CSRB,
294 RDRB, 0))
295 goto eio;
296
297 /* rx bit 0 is last received; multibyte words will
298 * be properly byteswapped on the way to memory.
299 */
300 val = uwire_read_reg(UWIRE_RDR);
301 val &= (1 << bits) - 1;
302 *buf++ = (u8) val;
303 if (bytes == 2)
304 *buf++ = val >> 8;
305 status += bytes;
306#ifdef VERBOSE
307 pr_debug("%s: read-%d =%04x\n",
Kay Sievers6c7377a2009-03-24 16:38:21 -0700308 dev_name(&spi->dev), bits, val);
David Brownellfdb3c182007-02-12 00:52:37 -0800309#endif
310
311 }
312 }
313 return status;
314eio:
315 return -EIO;
316}
317
318static int uwire_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
319{
320 struct uwire_state *ust = spi->controller_state;
321 struct uwire_spi *uwire;
322 unsigned flags = 0;
David Brownellfdb3c182007-02-12 00:52:37 -0800323 unsigned hz;
324 unsigned long rate;
325 int div1_idx;
326 int div1;
327 int div2;
328 int status;
329
330 uwire = spi_master_get_devdata(spi->master);
331
David Brownellfdb3c182007-02-12 00:52:37 -0800332 /* mode 0..3, clock inverted separately;
333 * standard nCS signaling;
334 * don't treat DI=high as "not ready"
335 */
336 if (spi->mode & SPI_CS_HIGH)
337 flags |= UWIRE_CS_ACTIVE_HIGH;
338
339 if (spi->mode & SPI_CPOL)
340 flags |= UWIRE_CLK_INVERTED;
341
342 switch (spi->mode & (SPI_CPOL | SPI_CPHA)) {
343 case SPI_MODE_0:
344 case SPI_MODE_3:
Imre Deake5f1b192007-05-23 13:58:20 -0700345 flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_RISING_EDGE;
David Brownellfdb3c182007-02-12 00:52:37 -0800346 break;
347 case SPI_MODE_1:
348 case SPI_MODE_2:
Imre Deake5f1b192007-05-23 13:58:20 -0700349 flags |= UWIRE_WRITE_RISING_EDGE | UWIRE_READ_FALLING_EDGE;
David Brownellfdb3c182007-02-12 00:52:37 -0800350 break;
351 }
352
353 /* assume it's already enabled */
354 rate = clk_get_rate(uwire->ck);
355
356 hz = spi->max_speed_hz;
357 if (t != NULL && t->speed_hz)
358 hz = t->speed_hz;
359
360 if (!hz) {
Kay Sievers6c7377a2009-03-24 16:38:21 -0700361 pr_debug("%s: zero speed?\n", dev_name(&spi->dev));
David Brownellfdb3c182007-02-12 00:52:37 -0800362 status = -EINVAL;
363 goto done;
364 }
365
366 /* F_INT = mpu_xor_clk / DIV1 */
367 for (div1_idx = 0; div1_idx < 4; div1_idx++) {
368 switch (div1_idx) {
369 case 0:
370 div1 = 2;
371 break;
372 case 1:
373 div1 = 4;
374 break;
375 case 2:
376 div1 = 7;
377 break;
378 default:
379 case 3:
380 div1 = 10;
381 break;
382 }
383 div2 = (rate / div1 + hz - 1) / hz;
384 if (div2 <= 8)
385 break;
386 }
387 if (div1_idx == 4) {
388 pr_debug("%s: lowest clock %ld, need %d\n",
Kay Sievers6c7377a2009-03-24 16:38:21 -0700389 dev_name(&spi->dev), rate / 10 / 8, hz);
David Brownellfdb3c182007-02-12 00:52:37 -0800390 status = -EDOM;
391 goto done;
392 }
393
394 /* we have to cache this and reset in uwire_chipselect as this is a
395 * global parameter and another uwire device can change it under
396 * us */
397 ust->div1_idx = div1_idx;
398 uwire_set_clk1_div(div1_idx);
399
400 rate /= div1;
401
402 switch (div2) {
403 case 0:
404 case 1:
405 case 2:
406 flags |= UWIRE_FREQ_DIV_2;
407 rate /= 2;
408 break;
409 case 3:
410 case 4:
411 flags |= UWIRE_FREQ_DIV_4;
412 rate /= 4;
413 break;
414 case 5:
415 case 6:
416 case 7:
417 case 8:
418 flags |= UWIRE_FREQ_DIV_8;
419 rate /= 8;
420 break;
421 }
422 omap_uwire_configure_mode(spi->chip_select, flags);
423 pr_debug("%s: uwire flags %02x, armxor %lu KHz, SCK %lu KHz\n",
Harvey Harrisonb687d2a2008-04-28 02:14:19 -0700424 __func__, flags,
David Brownellfdb3c182007-02-12 00:52:37 -0800425 clk_get_rate(uwire->ck) / 1000,
426 rate / 1000);
427 status = 0;
428done:
429 return status;
430}
431
432static int uwire_setup(struct spi_device *spi)
433{
434 struct uwire_state *ust = spi->controller_state;
435
436 if (ust == NULL) {
437 ust = kzalloc(sizeof(*ust), GFP_KERNEL);
438 if (ust == NULL)
439 return -ENOMEM;
440 spi->controller_state = ust;
441 }
442
443 return uwire_setup_transfer(spi, NULL);
444}
445
David Brownellbb2d1c32007-02-20 13:58:19 -0800446static void uwire_cleanup(struct spi_device *spi)
David Brownellfdb3c182007-02-12 00:52:37 -0800447{
448 kfree(spi->controller_state);
449}
450
451static void uwire_off(struct uwire_spi *uwire)
452{
453 uwire_write_reg(UWIRE_SR3, 0);
454 clk_disable(uwire->ck);
455 clk_put(uwire->ck);
456 spi_master_put(uwire->bitbang.master);
457}
458
Grant Likely2deff8d2013-02-05 13:27:35 +0000459static int uwire_probe(struct platform_device *pdev)
David Brownellfdb3c182007-02-12 00:52:37 -0800460{
461 struct spi_master *master;
462 struct uwire_spi *uwire;
463 int status;
464
465 master = spi_alloc_master(&pdev->dev, sizeof *uwire);
466 if (!master)
467 return -ENODEV;
468
469 uwire = spi_master_get_devdata(master);
Russell King55c381e2008-09-04 14:07:22 +0100470
471 uwire_base = ioremap(UWIRE_BASE_PHYS, UWIRE_IO_SIZE);
472 if (!uwire_base) {
473 dev_dbg(&pdev->dev, "can't ioremap UWIRE\n");
474 spi_master_put(master);
475 return -ENOMEM;
476 }
477
Jingoo Han24b5a822013-05-23 19:20:40 +0900478 platform_set_drvdata(pdev, uwire);
David Brownellfdb3c182007-02-12 00:52:37 -0800479
Russell Kingb1ad3792009-01-22 19:41:20 +0000480 uwire->ck = clk_get(&pdev->dev, "fck");
481 if (IS_ERR(uwire->ck)) {
482 status = PTR_ERR(uwire->ck);
483 dev_dbg(&pdev->dev, "no functional clock?\n");
David Brownellfdb3c182007-02-12 00:52:37 -0800484 spi_master_put(master);
Russell Kingb1ad3792009-01-22 19:41:20 +0000485 return status;
David Brownellfdb3c182007-02-12 00:52:37 -0800486 }
487 clk_enable(uwire->ck);
488
Alistair Buxton7a8f48f2009-09-22 10:04:51 +0100489 if (cpu_is_omap7xx())
David Brownellfdb3c182007-02-12 00:52:37 -0800490 uwire_idx_shift = 1;
491 else
492 uwire_idx_shift = 2;
493
494 uwire_write_reg(UWIRE_SR3, 1);
495
David Brownelle7db06b2009-06-17 16:26:04 -0700496 /* the spi->mode bits understood by this driver: */
497 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Axel Lin790fc552014-02-15 14:26:21 +0800498 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 16);
David Brownell70d60272009-06-30 11:41:27 -0700499 master->flags = SPI_MASTER_HALF_DUPLEX;
500
David Brownellfdb3c182007-02-12 00:52:37 -0800501 master->bus_num = 2; /* "official" */
502 master->num_chipselect = 4;
503 master->setup = uwire_setup;
504 master->cleanup = uwire_cleanup;
505
506 uwire->bitbang.master = master;
507 uwire->bitbang.chipselect = uwire_chipselect;
508 uwire->bitbang.setup_transfer = uwire_setup_transfer;
509 uwire->bitbang.txrx_bufs = uwire_txrx;
510
511 status = spi_bitbang_start(&uwire->bitbang);
Russell King55c381e2008-09-04 14:07:22 +0100512 if (status < 0) {
David Brownellfdb3c182007-02-12 00:52:37 -0800513 uwire_off(uwire);
Russell King55c381e2008-09-04 14:07:22 +0100514 iounmap(uwire_base);
515 }
David Brownellfdb3c182007-02-12 00:52:37 -0800516 return status;
517}
518
Grant Likely2deff8d2013-02-05 13:27:35 +0000519static int uwire_remove(struct platform_device *pdev)
David Brownellfdb3c182007-02-12 00:52:37 -0800520{
Jingoo Han24b5a822013-05-23 19:20:40 +0900521 struct uwire_spi *uwire = platform_get_drvdata(pdev);
David Brownellfdb3c182007-02-12 00:52:37 -0800522 int status;
523
524 // FIXME remove all child devices, somewhere ...
525
526 status = spi_bitbang_stop(&uwire->bitbang);
527 uwire_off(uwire);
Russell King55c381e2008-09-04 14:07:22 +0100528 iounmap(uwire_base);
David Brownellfdb3c182007-02-12 00:52:37 -0800529 return status;
530}
531
Kay Sievers7e38c3c2008-04-10 21:29:20 -0700532/* work with hotplug and coldplug */
533MODULE_ALIAS("platform:omap_uwire");
534
David Brownellfdb3c182007-02-12 00:52:37 -0800535static struct platform_driver uwire_driver = {
536 .driver = {
537 .name = "omap_uwire",
David Brownellfdb3c182007-02-12 00:52:37 -0800538 .owner = THIS_MODULE,
539 },
Wolfram Sang93e9c902013-10-08 22:35:39 +0200540 .probe = uwire_probe,
541 .remove = uwire_remove,
David Brownellfdb3c182007-02-12 00:52:37 -0800542 // suspend ... unuse ck
543 // resume ... use ck
544};
545
546static int __init omap_uwire_init(void)
547{
548 /* FIXME move these into the relevant board init code. also, include
549 * H3 support; it uses tsc2101 like H2 (on a different chipselect).
550 */
551
552 if (machine_is_omap_h2()) {
553 /* defaults: W21 SDO, U18 SDI, V19 SCL */
554 omap_cfg_reg(N14_1610_UWIRE_CS0);
555 omap_cfg_reg(N15_1610_UWIRE_CS1);
556 }
557 if (machine_is_omap_perseus2()) {
558 /* configure pins: MPU_UW_nSCS1, MPU_UW_SDO, MPU_UW_SCLK */
Alistair Buxton7a8f48f2009-09-22 10:04:51 +0100559 int val = omap_readl(OMAP7XX_IO_CONF_9) & ~0x00EEE000;
560 omap_writel(val | 0x00AAA000, OMAP7XX_IO_CONF_9);
David Brownellfdb3c182007-02-12 00:52:37 -0800561 }
562
Wolfram Sang93e9c902013-10-08 22:35:39 +0200563 return platform_driver_register(&uwire_driver);
David Brownellfdb3c182007-02-12 00:52:37 -0800564}
565
566static void __exit omap_uwire_exit(void)
567{
568 platform_driver_unregister(&uwire_driver);
569}
570
571subsys_initcall(omap_uwire_init);
572module_exit(omap_uwire_exit);
573
574MODULE_LICENSE("GPL");
575