[PATCH] ARM: OMAP: edge selection for McSPI / uWire
[pandora-kernel.git] / drivers / spi / omap_uwire.c
1 /*
2  * omap_uwire.c -- MicroWire interface driver for OMAP
3  *
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 Yrjölä <juha.yrjola@nokia.com>
10  *
11  * Copyright (C) 2005 David Brownell (ported to 2.6 SPI interface)
12  *
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.
17  *
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.
28  *
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.
32  */
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>
41
42 #include <linux/spi/spi.h>
43 #include <linux/spi/spi_bitbang.h>
44
45 #include <asm/system.h>
46 #include <asm/irq.h>
47 #include <asm/hardware.h>
48 #include <asm/io.h>
49 #include <asm/mach-types.h>
50
51 #include <asm/arch/mux.h>
52 #include <asm/arch/omap730.h>   /* OMAP730_IO_CONF registers */
53
54
55 /* FIXME address is now a platform device resource,
56  * and irqs should show there too...
57  */
58 #define UWIRE_BASE_PHYS         0xFFFB3000
59 #define UWIRE_BASE              ((void *__iomem)IO_ADDRESS(UWIRE_BASE_PHYS))
60
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
71
72 /* CSR bits */
73 #define RDRB    (1 << 15)
74 #define CSRB    (1 << 14)
75 #define START   (1 << 13)
76 #define CS_CMD  (1 << 12)
77
78 /* SR1 or SR2 bits */
79 #define UWIRE_READ_FALLING_EDGE         0x0001
80 #define UWIRE_READ_RISING_EDGE          0x0000
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
90
91
92 struct uwire_spi {
93         struct spi_bitbang      bitbang;
94         struct clk              *ck;
95 };
96
97 struct uwire_state {
98         unsigned        bits_per_word;
99         unsigned        div1_idx;
100 };
101
102 /* REVISIT compile time constant for idx_shift? */
103 static unsigned int uwire_idx_shift;
104
105 static inline void uwire_write_reg(int idx, u16 val)
106 {
107         __raw_writew(val, UWIRE_BASE + (idx << uwire_idx_shift));
108 }
109
110 static inline u16 uwire_read_reg(int idx)
111 {
112         return __raw_readw(UWIRE_BASE + (idx << uwire_idx_shift));
113 }
114
115 static inline void omap_uwire_configure_mode(u8 cs, unsigned long flags)
116 {
117         u16     w, val = 0;
118         int     shift, reg;
119
120         if (flags & UWIRE_CLK_INVERTED)
121                 val ^= 0x03;
122         val = flags & 0x3f;
123         if (cs & 1)
124                 shift = 6;
125         else
126                 shift = 0;
127         if (cs <= 1)
128                 reg = UWIRE_SR1;
129         else
130                 reg = UWIRE_SR2;
131
132         w = uwire_read_reg(reg);
133         w &= ~(0x3f << shift);
134         w |= val << shift;
135         uwire_write_reg(reg, w);
136 }
137
138 static int wait_uwire_csr_flag(u16 mask, u16 val, int might_not_catch)
139 {
140         u16 w;
141         int c = 0;
142         unsigned long max_jiffies = jiffies + HZ;
143
144         for (;;) {
145                 w = uwire_read_reg(UWIRE_CSR);
146                 if ((w & mask) == val)
147                         break;
148                 if (time_after(jiffies, max_jiffies)) {
149                         printk(KERN_ERR "%s: timeout. reg=%#06x "
150                                         "mask=%#06x val=%#06x\n",
151                                __FUNCTION__, w, mask, val);
152                         return -1;
153                 }
154                 c++;
155                 if (might_not_catch && c > 64)
156                         break;
157         }
158         return 0;
159 }
160
161 static void uwire_set_clk1_div(int div1_idx)
162 {
163         u16 w;
164
165         w = uwire_read_reg(UWIRE_SR3);
166         w &= ~(0x03 << 1);
167         w |= div1_idx << 1;
168         uwire_write_reg(UWIRE_SR3, w);
169 }
170
171 static void uwire_chipselect(struct spi_device *spi, int value)
172 {
173         struct  uwire_state *ust = spi->controller_state;
174         u16     w;
175         int     old_cs;
176
177
178         BUG_ON(wait_uwire_csr_flag(CSRB, 0, 0));
179
180         w = uwire_read_reg(UWIRE_CSR);
181         old_cs = (w >> 10) & 0x03;
182         if (value == BITBANG_CS_INACTIVE || old_cs != spi->chip_select) {
183                 /* Deselect this CS, or the previous CS */
184                 w &= ~CS_CMD;
185                 uwire_write_reg(UWIRE_CSR, w);
186         }
187         /* activate specfied chipselect */
188         if (value == BITBANG_CS_ACTIVE) {
189                 uwire_set_clk1_div(ust->div1_idx);
190                 /* invert clock? */
191                 if (spi->mode & SPI_CPOL)
192                         uwire_write_reg(UWIRE_SR4, 1);
193                 else
194                         uwire_write_reg(UWIRE_SR4, 0);
195
196                 w = spi->chip_select << 10;
197                 w |= CS_CMD;
198                 uwire_write_reg(UWIRE_CSR, w);
199         }
200 }
201
202 static int uwire_txrx(struct spi_device *spi, struct spi_transfer *t)
203 {
204         struct uwire_state *ust = spi->controller_state;
205         unsigned        len = t->len;
206         unsigned        bits = ust->bits_per_word;
207         unsigned        bytes;
208         u16             val, w;
209         int             status = 0;;
210
211         if (!t->tx_buf && !t->rx_buf)
212                 return 0;
213
214         /* Microwire doesn't read and write concurrently */
215         if (t->tx_buf && t->rx_buf)
216                 return -EPERM;
217
218         w = spi->chip_select << 10;
219         w |= CS_CMD;
220
221         if (t->tx_buf) {
222                 const u8        *buf = t->tx_buf;
223
224                 /* NOTE:  DMA could be used for TX transfers */
225
226                 /* write one or two bytes at a time */
227                 while (len >= 1) {
228                         /* tx bit 15 is first sent; we byteswap multibyte words
229                          * (msb-first) on the way out from memory.
230                          */
231                         val = *buf++;
232                         if (bits > 8) {
233                                 bytes = 2;
234                                 val |= *buf++ << 8;
235                         } else
236                                 bytes = 1;
237                         val <<= 16 - bits;
238
239 #ifdef  VERBOSE
240                         pr_debug("%s: write-%d =%04x\n",
241                                         spi->dev.bus_id, bits, val);
242 #endif
243                         uwire_write_reg(UWIRE_TDR, val);
244
245                         /* start write */
246                         val = START | w | (bits << 5);
247                         if (wait_uwire_csr_flag(CSRB, 0, 0))
248                                 goto eio;
249
250                         uwire_write_reg(UWIRE_CSR, val);
251                         len -= bytes;
252
253                         /* Wait till write actually starts.
254                          * This is needed with MPU clock 60+ MHz.
255                          * REVISIT: we may not have time to catch it...
256                          */
257                         if (wait_uwire_csr_flag(CSRB, CSRB, 1))
258                                 goto eio;
259
260                         status += bytes;
261                 }
262
263                 /* REVISIT:  save this for later to get more i/o overlap */
264                 if (wait_uwire_csr_flag(CSRB, 0, 0))
265                         goto eio;
266
267         } else if (t->rx_buf) {
268                 u8              *buf = t->rx_buf;
269
270                 /* read one or two bytes at a time */
271                 while (len) {
272                         if (bits > 8) {
273                                 bytes = 2;
274                         } else
275                                 bytes = 1;
276
277                         /* start read */
278                         val = START | w | (bits << 0);
279                         uwire_write_reg(UWIRE_CSR, val);
280                         len -= bytes;
281
282                         /* Wait till read actually starts */
283                         (void) wait_uwire_csr_flag(CSRB, CSRB, 1);
284
285                         if (wait_uwire_csr_flag(RDRB | CSRB,
286                                                 RDRB, 0))
287                                 goto eio;
288
289                         /* rx bit 0 is last received; multibyte words will
290                          * be properly byteswapped on the way to memory.
291                          */
292                         val = uwire_read_reg(UWIRE_RDR);
293                         val &= (1 << bits) - 1;
294                         *buf++ = (u8) val;
295                         if (bytes == 2)
296                                 *buf++ = val >> 8;
297                         status += bytes;
298 #ifdef  VERBOSE
299                         pr_debug("%s: read-%d =%04x\n",
300                                         spi->dev.bus_id, bits, val);
301 #endif
302
303                 }
304         }
305         return status;
306 eio:
307         return -EIO;
308 }
309
310 static int uwire_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
311 {
312         struct uwire_state      *ust = spi->controller_state;
313         struct uwire_spi        *uwire;
314         unsigned                flags = 0;
315         unsigned                bits;
316         unsigned                hz;
317         unsigned long           rate;
318         int                     div1_idx;
319         int                     div1;
320         int                     div2;
321         int                     status;
322
323         uwire = spi_master_get_devdata(spi->master);
324
325         if (spi->chip_select > 3) {
326                 pr_debug("%s: cs%d?\n", spi->dev.bus_id, spi->chip_select);
327                 status = -ENODEV;
328                 goto done;
329         }
330
331         bits = spi->bits_per_word;
332         if (t != NULL && t->bits_per_word)
333                 bits = t->bits_per_word;
334         if (!bits)
335                 bits = 8;
336
337         if (spi->bits_per_word > 16) {
338                 pr_debug("%s: wordsize %d?\n", spi->dev.bus_id,
339                                 spi->bits_per_word);
340                 status = -ENODEV;
341                 goto done;
342         }
343         ust->bits_per_word = bits;
344
345         /* mode 0..3, clock inverted separately;
346          * standard nCS signaling;
347          * don't treat DI=high as "not ready"
348          */
349         if (spi->mode & SPI_CS_HIGH)
350                 flags |= UWIRE_CS_ACTIVE_HIGH;
351
352         if (spi->mode & SPI_CPOL)
353                 flags |= UWIRE_CLK_INVERTED;
354
355         switch (spi->mode & (SPI_CPOL | SPI_CPHA)) {
356         case SPI_MODE_0:
357         case SPI_MODE_3:
358                 flags |= UWIRE_WRITE_RISING_EDGE | UWIRE_READ_FALLING_EDGE;
359                 break;
360         case SPI_MODE_1:
361         case SPI_MODE_2:
362                 flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_RISING_EDGE;
363                 break;
364         }
365
366         /* assume it's already enabled */
367         rate = clk_get_rate(uwire->ck);
368
369         hz = spi->max_speed_hz;
370         if (t != NULL && t->speed_hz)
371                 hz = t->speed_hz;
372
373         if (!hz) {
374                 pr_debug("%s: zero speed?\n", spi->dev.bus_id, hz);
375                 status = -EINVAL;
376                 goto done;
377         }
378
379         /* F_INT = mpu_per_clk / DIV1 */
380         for (div1_idx = 0; div1_idx < 4; div1_idx++) {
381                 switch (div1_idx) {
382                 case 0:
383                         div1 = 2;
384                         break;
385                 case 1:
386                         div1 = 4;
387                         break;
388                 case 2:
389                         div1 = 7;
390                         break;
391                 default:
392                 case 3:
393                         div1 = 10;
394                         break;
395                 }
396                 div2 = (rate / div1 + hz - 1) / hz;
397                 if (div2 <= 8)
398                         break;
399         }
400         if (div1_idx == 4) {
401                 pr_debug("%s: lowest clock %ld, need %d\n",
402                         spi->dev.bus_id, rate / 10 / 8, hz);
403                         status = -EDOM;
404                         goto done;
405         }
406
407         /* we have to cache this and reset in uwire_chipselect as this is a
408          * global parameter and another uwire device can change it under
409          * us */
410         ust->div1_idx = div1_idx;
411         uwire_set_clk1_div(div1_idx);
412
413         rate /= div1;
414
415         switch (div2) {
416         case 0:
417         case 1:
418         case 2:
419                 flags |= UWIRE_FREQ_DIV_2;
420                 rate /= 2;
421                 break;
422         case 3:
423         case 4:
424                 flags |= UWIRE_FREQ_DIV_4;
425                 rate /= 4;
426                 break;
427         case 5:
428         case 6:
429         case 7:
430         case 8:
431                 flags |= UWIRE_FREQ_DIV_8;
432                 rate /= 8;
433                 break;
434         }
435         omap_uwire_configure_mode(spi->chip_select, flags);
436         pr_debug("%s: uwire flags %02x, armper %lu KHz, SCK %lu KHz\n",
437                         __FUNCTION__, flags,
438                         clk_get_rate(uwire->ck) / 1000,
439                         rate / 1000);
440         status = 0;
441 done:
442         return status;
443 }
444
445 static int uwire_setup(struct spi_device *spi)
446 {
447         struct uwire_state *ust = spi->controller_state;
448
449         if (ust == NULL) {
450                 ust = kzalloc(sizeof(*ust), SLAB_KERNEL);
451                 if (ust == NULL)
452                         return -ENOMEM;
453                 spi->controller_state = ust;
454         }
455
456         return uwire_setup_transfer(spi, NULL);
457 }
458
459 static void uwire_cleanup(const struct spi_device *spi)
460 {
461         kfree(spi->controller_state);
462 }
463
464 static void uwire_off(struct uwire_spi *uwire)
465 {
466         uwire_write_reg(UWIRE_SR3, 0);
467         clk_disable(uwire->ck);
468         clk_put(uwire->ck);
469         spi_master_put(uwire->bitbang.master);
470 }
471
472 static int uwire_probe(struct platform_device *pdev)
473 {
474         struct spi_master       *master;
475         struct uwire_spi        *uwire;
476         int                     status;
477
478         master = spi_alloc_master(&pdev->dev, sizeof *uwire);
479         if (!master)
480                 return -ENODEV;
481
482         uwire = spi_master_get_devdata(master);
483         dev_set_drvdata(&pdev->dev, uwire);
484
485         uwire->ck = clk_get(&pdev->dev, "armper_ck");
486         if (!uwire->ck || IS_ERR(uwire->ck)) {
487                 dev_dbg(&pdev->dev, "no mpu_per_clk ?\n");
488                 spi_master_put(master);
489                 return -ENODEV;
490         }
491         clk_enable(uwire->ck);
492
493         if (cpu_is_omap730())
494                 uwire_idx_shift = 1;
495         else
496                 uwire_idx_shift = 2;
497
498         uwire_write_reg(UWIRE_SR3, 1);
499
500         master->bus_num = 2;    /* "official" */
501         master->num_chipselect = 4;
502         master->setup = uwire_setup;
503         master->cleanup = uwire_cleanup;
504
505         uwire->bitbang.master = master;
506         uwire->bitbang.chipselect = uwire_chipselect;
507         uwire->bitbang.setup_transfer = uwire_setup_transfer;
508         uwire->bitbang.txrx_bufs = uwire_txrx;
509
510         status = spi_bitbang_start(&uwire->bitbang);
511         if (status < 0)
512                 uwire_off(uwire);
513         return status;
514 }
515
516 static int uwire_remove(struct platform_device *pdev)
517 {
518         struct uwire_spi        *uwire = dev_get_drvdata(&pdev->dev);
519         int                     status;
520
521         // FIXME remove all child devices, somewhere ...
522
523         status = spi_bitbang_stop(&uwire->bitbang);
524         uwire_off(uwire);
525         return status;
526 }
527
528 static struct platform_driver uwire_driver = {
529         .driver = {
530                 .name           = "omap_uwire",
531                 .bus            = &platform_bus_type,
532                 .owner          = THIS_MODULE,
533         },
534         .probe          = uwire_probe,
535         .remove         = uwire_remove,
536         // suspend ... unuse ck
537         // resume ... use ck
538 };
539
540 static int __init omap_uwire_init(void)
541 {
542         /* FIXME move these into the relevant board init code. also, include
543          * H3 support; it uses tsc2101 like H2 (on a different chipselect).
544          */
545
546         if (machine_is_omap_h2()) {
547                 /* defaults: W21 SDO, U18 SDI, V19 SCL */
548                 omap_cfg_reg(N14_1610_UWIRE_CS0);
549                 omap_cfg_reg(N15_1610_UWIRE_CS1);
550         }
551         if (machine_is_omap_perseus2()) {
552                 /* configure pins: MPU_UW_nSCS1, MPU_UW_SDO, MPU_UW_SCLK */
553                 int val = omap_readl(OMAP730_IO_CONF_9) & ~0x00EEE000;
554                 omap_writel(val | 0x00AAA000, OMAP730_IO_CONF_9);
555         }
556
557         return platform_driver_register(&uwire_driver);
558 }
559
560 static void __exit omap_uwire_exit(void)
561 {
562         platform_driver_unregister(&uwire_driver);
563 }
564
565 subsys_initcall(omap_uwire_init);
566 module_exit(omap_uwire_exit);
567
568 MODULE_LICENSE("GPL");