]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/spi/omap_uwire.c
h63xx: tsc2101 alsa sound support
[linux-2.6-omap-h63xx.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         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
90
91
92 struct uwire_spi {
93         struct spi_bitbang      bitbang;
94         struct clk              *ck;
95 };
96
97 /* REVISIT compile time constant for idx_shift? */
98 static unsigned int uwire_idx_shift;
99
100 static inline void uwire_write_reg(int idx, u16 val)
101 {
102         __raw_writew(val, UWIRE_BASE + (idx << uwire_idx_shift));
103 }
104
105 static inline u16 uwire_read_reg(int idx)
106 {
107         return __raw_readw(UWIRE_BASE + (idx << uwire_idx_shift));
108 }
109
110 static inline void omap_uwire_configure_mode(u8 cs, unsigned long flags)
111 {
112         u16     w, val = 0;
113         int     shift, reg;
114
115         if (flags & UWIRE_CLK_INVERTED)
116                 val ^= 0x03;
117         val = flags & 0x3f;
118         if (cs & 1)
119                 shift = 6;
120         else
121                 shift = 0;
122         if (cs <= 1)
123                 reg = UWIRE_SR1;
124         else
125                 reg = UWIRE_SR2;
126
127         w = uwire_read_reg(reg);
128         w &= ~(0x3f << shift);
129         w |= val << shift;
130         uwire_write_reg(reg, w);
131 }
132
133 static int wait_uwire_csr_flag(u16 mask, u16 val, int might_not_catch)
134 {
135         u16 w;
136         int c = 0;
137         unsigned long max_jiffies = jiffies + HZ;
138
139         for (;;) {
140                 w = uwire_read_reg(UWIRE_CSR);
141                 if ((w & mask) == val)
142                         break;
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);
147                         return -1;
148                 }
149                 c++;
150                 if (might_not_catch && c > 64)
151                         break;
152         }
153         return 0;
154 }
155
156 static void uwire_chipselect(struct spi_device *spi, int value)
157 {
158         u16     w;
159         int     old_cs;
160
161         BUG_ON(wait_uwire_csr_flag(CSRB, 0, 0));
162
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 */
167                 w &= ~CS_CMD;
168                 uwire_write_reg(UWIRE_CSR, w);
169         }
170         /* activate specfied chipselect */
171         if (value == BITBANG_CS_ACTIVE) {
172                 /* invert clock? */
173                 if (spi->mode & SPI_CPOL)
174                         uwire_write_reg(UWIRE_SR4, 1);
175                 else
176                         uwire_write_reg(UWIRE_SR4, 0);
177
178                 w = spi->chip_select << 10;
179                 w |= CS_CMD;
180                 uwire_write_reg(UWIRE_CSR, w);
181         }
182 }
183
184 static int uwire_txrx(struct spi_device *spi, struct spi_transfer *t)
185 {
186         unsigned        len = t->len;
187         unsigned        bits = spi->bits_per_word;
188         unsigned        bytes;
189         u16             val, w;
190         int             status = 0;;
191
192         if (!t->tx_buf && !t->rx_buf)
193                 return 0;
194
195         /* Microwire doesn't read and write concurrently */
196         if (t->tx_buf && t->rx_buf)
197                 return -EPERM;
198
199         w = spi->chip_select << 10;
200         w |= CS_CMD;
201
202         if (t->tx_buf) {
203                 const u8        *buf = t->tx_buf;
204
205                 /* NOTE:  DMA could be used for TX transfers */
206
207                 /* write one or two bytes at a time */
208                 while (len >= 1) {
209                         /* tx is msb-aligned */
210                         val = *buf++;
211                         if (len > 1 && (!bits || bits > 8)) {
212                                 if (!bits)
213                                         bits = 16;
214                                 bytes = 2;
215                                 val |= *buf++ << 8;
216                         } else {
217                                 if (!bits || bits > 8)
218                                         bits = 8;
219                                 bytes = 1;
220                         }
221                         val <<= 16 - bits;
222
223 #ifdef  VERBOSE
224                         pr_debug("%s: write-%d =%04x\n",
225                                         spi->dev.bus_id, bits, val);
226 #endif
227                         uwire_write_reg(UWIRE_TDR, val);
228
229                         /* start write */
230                         val = START | w | (bits << 5);
231                         if (wait_uwire_csr_flag(CSRB, 0, 0))
232                                 goto eio;
233
234                         uwire_write_reg(UWIRE_CSR, val);
235                         len -= bytes;
236
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...
240                          */
241                         if (wait_uwire_csr_flag(CSRB, CSRB, 1))
242                                 goto eio;
243
244                         status += bytes;
245                 }
246
247                 /* REVISIT:  save this for later to get more i/o overlap */
248                 if (wait_uwire_csr_flag(CSRB, 0, 0))
249                         goto eio;
250
251         } else if (t->rx_buf) {
252                 u8              *buf = t->rx_buf;
253
254                 /* read one or two bytes at a time */
255                 while (len) {
256                         if (len > 1 && (!bits || bits > 8)) {
257                                 if (!bits)
258                                         bits = 16;
259                                 bytes = 2;
260                         } else {
261                                 if (!bits || bits > 8)
262                                         bits = 8;
263                                 bytes = 1;
264                         }
265
266                         /* start read */
267                         val = START | w | (bits << 0);
268                         uwire_write_reg(UWIRE_CSR, val);
269                         len -= bytes;
270
271                         /* Wait till read actually starts */
272                         (void) wait_uwire_csr_flag(CSRB, CSRB, 1);
273
274                         if (wait_uwire_csr_flag(RDRB | CSRB,
275                                                 RDRB, 0))
276                                 goto eio;
277
278                         /* rx is lsb-aligned */
279                         val = uwire_read_reg(UWIRE_RDR);
280                         val &= (1 << bits) - 1;
281                         *buf++ = (u8) val;
282                         if (bytes == 2)
283                                 *buf++ = val >> 8;
284                         status += bytes;
285 #ifdef  VERBOSE
286                         pr_debug("%s: read-%d =%04x\n",
287                                         spi->dev.bus_id, bits, val);
288 #endif
289
290                 }
291         }
292         return status;
293 eio:
294         return -EIO;
295 }
296
297 static int uwire_setup(struct spi_device *spi)
298 {
299         struct uwire_spi        *uwire;
300         unsigned                flags = 0;
301         unsigned long           rate;
302         int                     div1_idx;
303         int                     div1;
304         int                     div2;
305         u16                     w;
306         int                     status;
307
308         uwire = spi_master_get_devdata(spi->master);
309
310         if (spi->chip_select > 3) {
311                 pr_debug("%s: cs%d?\n", spi->dev.bus_id, spi->chip_select);
312                 status = -ENODEV;
313                 goto done;
314         }
315
316         if (spi->bits_per_word > 16) {
317                 pr_debug("%s: wordsize %d?\n", spi->dev.bus_id,
318                                 spi->bits_per_word);
319                 status = -ENODEV;
320                 goto done;
321         }
322
323         /* mode 0..3, clock inverted separately;
324          * standard nCS signaling;
325          * don't treat DI=high as "not ready"
326          */
327         if (spi->mode & SPI_CS_HIGH)
328                 flags |= UWIRE_CS_ACTIVE_HIGH;
329
330         if (spi->mode & SPI_CPOL)
331                 flags |= UWIRE_CLK_INVERTED;
332
333         switch (spi->mode & (SPI_CPOL | SPI_CPHA)) {
334         case SPI_MODE_0:
335         case SPI_MODE_3:
336                 flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_FALLING_EDGE;
337                 break;
338         case SPI_MODE_1:
339         case SPI_MODE_2:
340                 flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_RISING_EDGE;
341                 break;
342         }
343
344         /* assume it's already enabled */
345         rate = clk_get_rate(uwire->ck);
346
347         /* F_INT = mpu_per_clk / DIV1 */
348         for (div1_idx = 0; div1_idx < 4; div1_idx++) {
349                 switch (div1_idx) {
350                 case 0:
351                         div1 = 2;
352                         break;
353                 case 1:
354                         div1 = 4;
355                         break;
356                 case 2:
357                         div1 = 7;
358                         break;
359                 default:
360                 case 3:
361                         div1 = 10;
362                         break;
363                 }
364                 div2 = (rate / div1 + spi->max_speed_hz - 1) /
365                         spi->max_speed_hz;
366                 if (div2 <= 8)
367                         break;
368         }
369         if (div1_idx == 4) {
370                 pr_debug("%s: lowest clock %ld, need %d\n",
371                         spi->dev.bus_id, rate / 10 / 8, spi->max_speed_hz);
372                         status = -EDOM;
373                         goto done;
374         }
375
376         w = uwire_read_reg(UWIRE_SR3);
377         w &= ~(0x03 << 1);
378         w |= div1_idx << 1;
379         uwire_write_reg(UWIRE_SR3, w);
380
381         rate /= div1;
382
383         switch (div2) {
384         case 0:
385         case 1:
386         case 2:
387                 flags |= UWIRE_FREQ_DIV_2;
388                 rate /= 2;
389                 break;
390         case 3:
391         case 4:
392                 flags |= UWIRE_FREQ_DIV_4;
393                 rate /= 4;
394                 break;
395         case 5:
396         case 6:
397         case 7:
398         case 8:
399                 flags |= UWIRE_FREQ_DIV_8;
400                 rate /= 8;
401                 break;
402         }
403         omap_uwire_configure_mode(spi->chip_select, flags);
404         pr_debug("%s: uwire flags %02x, armper %lu KHz, SCK %lu KHz\n",
405                         __FUNCTION__, flags,
406                         clk_get_rate(uwire->ck) / 1000,
407                         rate / 1000);
408         status = 0;
409 done:
410         return status;
411 }
412
413 static void uwire_off(struct uwire_spi *uwire)
414 {
415         uwire_write_reg(UWIRE_SR3, 0);
416         clk_disable(uwire->ck);
417         clk_put(uwire->ck);
418         spi_master_put(uwire->bitbang.master);
419 }
420
421 static int uwire_probe(struct platform_device *pdev)
422 {
423         struct spi_master       *master;
424         struct uwire_spi        *uwire;
425         int                     status;
426
427         master = spi_alloc_master(&pdev->dev, sizeof *uwire);
428         if (!master)
429                 return -ENODEV;
430
431         uwire = spi_master_get_devdata(master);
432         dev_set_drvdata(&pdev->dev, uwire);
433
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);
438                 return -ENODEV;
439         }
440         clk_enable(uwire->ck);
441
442         if (cpu_is_omap730())
443                 uwire_idx_shift = 1;
444         else
445                 uwire_idx_shift = 2;
446
447         uwire_write_reg(UWIRE_SR3, 1);
448
449         master->bus_num = 2;    /* "official" */
450         master->num_chipselect = 4;
451         master->setup = uwire_setup;
452
453         uwire->bitbang.master = master;
454         uwire->bitbang.chipselect = uwire_chipselect;
455         uwire->bitbang.txrx_bufs = uwire_txrx;
456
457         status = spi_bitbang_start(&uwire->bitbang);
458         if (status < 0)
459                 uwire_off(uwire);
460         return status;
461 }
462
463 static int uwire_remove(struct platform_device *pdev)
464 {
465         struct uwire_spi        *uwire = dev_get_drvdata(&pdev->dev);
466         int                     status;
467
468         // FIXME remove all child devices, somewhere ...
469
470         status = spi_bitbang_stop(&uwire->bitbang);
471         uwire_off(uwire);
472         return status;
473 }
474
475 static struct platform_driver uwire_driver = {
476         .driver = {
477                 .name           = "omap_uwire",
478                 .bus            = &platform_bus_type,
479                 .owner          = THIS_MODULE,
480         },
481         .probe          = uwire_probe,
482         .remove         = uwire_remove,
483         // suspend ... unuse ck
484         // resume ... use ck
485 };
486
487 static int __init omap_uwire_init(void)
488 {
489         /* FIXME move these into the relevant board init code. also, include
490          * H3 support; it uses tsc2101 like H2 (on a different chipselect).
491          */
492
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);
497         }
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);
502         }
503
504         return platform_driver_register(&uwire_driver);
505 }
506
507 static void __exit omap_uwire_exit(void)
508 {
509         platform_driver_unregister(&uwire_driver);
510 }
511
512 subsys_initcall(omap_uwire_init);
513 module_exit(omap_uwire_exit);
514
515 MODULE_LICENSE("GPL");