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