]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/spi/omap_uwire.c
ARM: OMAP: Add SPI master driver for OMAP1 uWire controller
[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
160         BUG_ON(wait_uwire_csr_flag(CSRB, 0, 0));
161
162         /* activate/deactivate specfied chipselect */
163         if (value == BITBANG_CS_ACTIVE) {
164                 /* invert clock? */
165                 if (spi->mode & SPI_CPOL)
166                         uwire_write_reg(UWIRE_SR4, 1);
167                 else
168                         uwire_write_reg(UWIRE_SR4, 0);
169
170                 w = spi->chip_select << 10;
171                 w |= CS_CMD;
172         } else
173                 w = 0;
174         uwire_write_reg(UWIRE_CSR, w);
175 }
176
177 static int uwire_txrx(struct spi_device *spi, struct spi_transfer *t)
178 {
179         unsigned        len = t->len;
180         unsigned        bits = spi->bits_per_word;
181         unsigned        bytes;
182         u16             val, w;
183         int             status = 0;;
184
185         if (!t->tx_buf && !t->rx_buf)
186                 return 0;
187
188         /* Microwire doesn't read and write concurrently */
189         if (t->tx_buf && t->rx_buf)
190                 return -EPERM;
191
192         w = spi->chip_select << 10;
193         w |= CS_CMD;
194
195         if (t->tx_buf) {
196                 const u8        *buf = t->tx_buf;
197
198                 /* NOTE:  DMA could be used for TX transfers */
199
200                 /* write one or two bytes at a time */
201                 while (len >= 1) {
202                         /* tx is msb-aligned */
203                         val = *buf++;
204                         if (len > 1 && (!bits || bits > 8)) {
205                                 if (!bits)
206                                         bits = 16;
207                                 bytes = 2;
208                                 val |= *buf++ << 8;
209                         } else {
210                                 if (!bits || bits > 8)
211                                         bits = 8;
212                                 bytes = 1;
213                         }
214                         val <<= 16 - bits;
215
216 #ifdef  VERBOSE
217                         pr_debug("%s: write-%d =%04x\n",
218                                         spi->dev.bus_id, bits, val);
219 #endif
220                         uwire_write_reg(UWIRE_TDR, val);
221
222                         /* start write */
223                         val = START | w | (bits << 5);
224                         if (wait_uwire_csr_flag(CSRB, 0, 0))
225                                 goto eio;
226
227                         uwire_write_reg(UWIRE_CSR, val);
228                         len -= bytes;
229
230                         /* Wait till write actually starts.
231                          * This is needed with MPU clock 60+ MHz.
232                          * REVISIT: we may not have time to catch it...
233                          */
234                         if (wait_uwire_csr_flag(CSRB, CSRB, 1))
235                                 goto eio;
236
237                         status += bytes;
238                 }
239
240                 /* REVISIT:  save this for later to get more i/o overlap */
241                 if (wait_uwire_csr_flag(CSRB, 0, 0))
242                         goto eio;
243
244         } else if (t->rx_buf) {
245                 u8              *buf = t->rx_buf;
246
247                 /* read one or two bytes at a time */
248                 while (len) {
249                         if (len > 1 && (!bits || bits > 8)) {
250                                 if (!bits)
251                                         bits = 16;
252                                 bytes = 2;
253                         } else {
254                                 if (!bits || bits > 8)
255                                         bits = 8;
256                                 bytes = 1;
257                         }
258
259                         /* start read */
260                         val = START | w | (bits << 0);
261                         uwire_write_reg(UWIRE_CSR, val);
262                         len -= bytes;
263
264                         /* Wait till read actually starts */
265                         (void) wait_uwire_csr_flag(CSRB, CSRB, 1);
266
267                         if (wait_uwire_csr_flag(RDRB | CSRB,
268                                                 RDRB, 0))
269                                 goto eio;
270
271                         /* rx is lsb-aligned */
272                         val = uwire_read_reg(UWIRE_RDR);
273                         val &= (1 << bits) - 1;
274                         *buf++ = (u8) val;
275                         if (bytes == 2)
276                                 *buf++ = val >> 8;
277                         status += len;
278 #ifdef  VERBOSE
279                         pr_debug("%s: read-%d =%04x\n",
280                                         spi->dev.bus_id, bits, val);
281 #endif
282
283                 }
284         }
285         return status;
286 eio:
287         return -EIO;
288 }
289
290 static int uwire_setup(struct spi_device *spi)
291 {
292         struct uwire_spi        *uwire;
293         unsigned                flags = 0;
294         unsigned long           rate;
295         u16                     div1;
296         int                     status;
297
298         uwire = spi_master_get_devdata(spi->master);
299
300         if (spi->chip_select > 3) {
301                 pr_debug("%s: cs%d?\n", spi->dev.bus_id, spi->chip_select);
302                 status = -ENODEV;
303                 goto done;
304         }
305
306         if (spi->bits_per_word > 16) {
307                 pr_debug("%s: wordsize %d?\n", spi->dev.bus_id,
308                                 spi->bits_per_word);
309                 status = -ENODEV;
310                 goto done;
311         }
312
313         /* mode 0..3, clock inverted separately;
314          * standard nCS signaling;
315          * don't treat DI=high as "not ready"
316          */
317         if (spi->mode & SPI_CS_HIGH)
318                 flags |= UWIRE_CS_ACTIVE_HIGH;
319
320         if (spi->mode & SPI_CPOL)
321                 flags |= UWIRE_CLK_INVERTED;
322
323         switch (spi->mode & (SPI_CPOL | SPI_CPHA)) {
324         case SPI_MODE_0:
325         case SPI_MODE_3:
326                 flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_FALLING_EDGE;
327                 break;
328         case SPI_MODE_1:
329         case SPI_MODE_2:
330                 flags |= UWIRE_WRITE_FALLING_EDGE | UWIRE_READ_RISING_EDGE;
331                 break;
332         }
333
334         /* assume it's already enabled */
335         rate = clk_get_rate(uwire->ck);
336
337         /* F_INT = mpu_per_clk / DIV1 */
338         div1 = (uwire_read_reg(UWIRE_SR3) >> 1) & 0x3;
339         switch (div1) {
340         case 0: rate /= 2; break;
341         case 1: rate /= 4; break;
342         case 2: rate /= 7; break;
343         case 3: rate /= 10; break;
344         }
345
346         /* SCLK = F_INT / DIV2 */
347         rate >>= 1;
348         if (rate <= spi->max_speed_hz)
349                 flags |= UWIRE_FREQ_DIV_2;
350         else {
351                 rate >>= 1;
352                 if (rate <= spi->max_speed_hz)
353                         flags |= UWIRE_FREQ_DIV_4;
354                 else {
355                         rate >>= 1;
356                         if (rate <= spi->max_speed_hz)
357                                 flags |= UWIRE_FREQ_DIV_8;
358                         else {
359                                 /* REVISIT:  we could change DIV2 */
360                                 pr_debug("%s: lowest clock %ld, need %d, "
361                                                 "div1 %d\n",
362                                         spi->dev.bus_id, rate,
363                                         spi->max_speed_hz, div1);
364                                 status = -EDOM;
365                                 goto done;
366                         }
367                 }
368         }
369         omap_uwire_configure_mode(spi->chip_select, flags);
370         pr_debug("%s: uwire flags %02x, armper %lu KHz, SCK %lu KHz\n",
371                         __FUNCTION__, flags,
372                         clk_get_rate(uwire->ck) / 1000,
373                         rate / 1000);
374         status = 0;
375 done:
376         return status;
377 }
378
379 static void uwire_off(struct uwire_spi *uwire)
380 {
381         uwire_write_reg(UWIRE_SR3, 0);
382         clk_disable(uwire->ck);
383         clk_put(uwire->ck);
384         spi_master_put(uwire->bitbang.master);
385 }
386
387 static int uwire_probe(struct platform_device *pdev)
388 {
389         struct spi_master       *master;
390         struct uwire_spi        *uwire;
391         int                     status;
392
393         master = spi_alloc_master(&pdev->dev, sizeof *uwire);
394         if (!master)
395                 return -ENODEV;
396
397         uwire = spi_master_get_devdata(master);
398         dev_set_drvdata(&pdev->dev, uwire);
399
400         uwire->ck = clk_get(&pdev->dev, "armper_ck");
401         if (!uwire->ck || IS_ERR(uwire->ck)) {
402                 dev_dbg(&pdev->dev, "no mpu_per_clk ?\n");
403                 spi_master_put(master);
404                 return -ENODEV;
405         }
406         clk_enable(uwire->ck);
407
408         if (cpu_is_omap730())
409                 uwire_idx_shift = 1;
410         else
411                 uwire_idx_shift = 2;
412
413         uwire_write_reg(UWIRE_SR3, 1);
414
415         master->bus_num = 2;    /* "official" */
416         master->num_chipselect = 4;
417         master->setup = uwire_setup;
418
419         uwire->bitbang.master = master;
420         uwire->bitbang.chipselect = uwire_chipselect;
421         uwire->bitbang.txrx_bufs = uwire_txrx;
422
423         status = spi_bitbang_start(&uwire->bitbang);
424         if (status < 0)
425                 uwire_off(uwire);
426         return status;
427 }
428
429 static int uwire_remove(struct platform_device *pdev)
430 {
431         struct uwire_spi        *uwire = dev_get_drvdata(&pdev->dev);
432         int                     status;
433
434         // FIXME remove all child devices, somewhere ...
435
436         status = spi_bitbang_stop(&uwire->bitbang);
437         uwire_off(uwire);
438         return status;
439 }
440
441 static struct platform_driver uwire_driver = {
442         .driver = {
443                 .name           = "omap_uwire",
444                 .bus            = &platform_bus_type,
445                 .owner          = THIS_MODULE,
446         },
447         .probe          = uwire_probe,
448         .remove         = uwire_remove,
449         // suspend ... unuse ck
450         // resume ... use ck
451 };
452
453 static int __init omap_uwire_init(void)
454 {
455         /* FIXME move these into the relevant board init code. also, include
456          * H3 support; it uses tsc2101 like H2 (on a different chipselect).
457          */
458
459         if (machine_is_omap_h2()) {
460                 /* defaults: W21 SDO, U18 SDI, V19 SCL */
461                 omap_cfg_reg(N14_1610_UWIRE_CS0);
462                 omap_cfg_reg(N15_1610_UWIRE_CS1);
463         }
464         if (machine_is_omap_perseus2()) {
465                 /* configure pins: MPU_UW_nSCS1, MPU_UW_SDO, MPU_UW_SCLK */
466                 int val = omap_readl(OMAP730_IO_CONF_9) & ~0x00EEE000;
467                 omap_writel(val | 0x00AAA000, OMAP730_IO_CONF_9);
468         }
469
470         return platform_driver_register(&uwire_driver);
471 }
472
473 static void __exit omap_uwire_exit(void)
474 {
475         platform_driver_unregister(&uwire_driver);
476 }
477
478 subsys_initcall(omap_uwire_init);
479 module_exit(omap_uwire_exit);
480
481 MODULE_LICENSE("GPL");