]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/video/omap/sossi.c
omapfb: Add support for omap framebuffer
[linux-2.6-omap-h63xx.git] / drivers / video / omap / sossi.c
1 /*
2  * File: drivers/video/omap/omap1/sossi.c
3  *
4  * OMAP1 Special OptimiSed Screen Interface support
5  *
6  * Copyright (C) 2004-2005 Nokia Corporation
7  * Author: Juha Yrjölä <juha.yrjola@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation; either version 2 of the License, or (at your
12  * option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23 #include <linux/module.h>
24 #include <linux/mm.h>
25 #include <linux/clk.h>
26
27 #include <asm/io.h>
28
29 #include <asm/arch/dma.h>
30 #include <asm/arch/omapfb.h>
31
32 #include "lcdc.h"
33
34 #define MODULE_NAME             "omapfb-sossi"
35
36 #define OMAP_SOSSI_BASE         0xfffbac00
37 #define SOSSI_ID_REG            0x00
38 #define SOSSI_INIT1_REG         0x04
39 #define SOSSI_INIT2_REG         0x08
40 #define SOSSI_INIT3_REG         0x0c
41 #define SOSSI_FIFO_REG          0x10
42 #define SOSSI_REOTABLE_REG      0x14
43 #define SOSSI_TEARING_REG       0x18
44 #define SOSSI_INIT1B_REG        0x1c
45 #define SOSSI_FIFOB_REG         0x20
46
47 #define DMA_GSCR          0xfffedc04
48 #define DMA_LCD_CCR       0xfffee3c2
49 #define DMA_LCD_CTRL      0xfffee3c4
50 #define DMA_LCD_LCH_CTRL  0xfffee3ea
51
52 #define CONF_SOSSI_RESET_R      (1 << 23)
53
54 #define RD_ACCESS               0
55 #define WR_ACCESS               1
56
57 #define SOSSI_MAX_XMIT_BYTES    (512 * 1024)
58
59 static struct {
60         void __iomem    *base;
61         struct clk      *fck;
62         unsigned long   fck_hz;
63         spinlock_t      lock;
64         int             bus_pick_count;
65         int             bus_pick_width;
66         int             tearsync_mode;
67         int             tearsync_line;
68         void            (*lcdc_callback)(void *data);
69         void            *lcdc_callback_data;
70         int             vsync_dma_pending;
71         /* timing for read and write access */
72         int             clk_div;
73         u8              clk_tw0[2];
74         u8              clk_tw1[2];
75         /* if last_access is the same as current we don't have to change
76          * the timings
77          */
78         int             last_access;
79
80         struct omapfb_device    *fbdev;
81 } sossi;
82
83 static inline u32 sossi_read_reg(int reg)
84 {
85         return readl(sossi.base + reg);
86 }
87
88 static inline u16 sossi_read_reg16(int reg)
89 {
90         return readw(sossi.base + reg);
91 }
92
93 static inline u8 sossi_read_reg8(int reg)
94 {
95         return readb(sossi.base + reg);
96 }
97
98 static inline void sossi_write_reg(int reg, u32 value)
99 {
100         writel(value, sossi.base + reg);
101 }
102
103 static inline void sossi_write_reg16(int reg, u16 value)
104 {
105         writew(value, sossi.base + reg);
106 }
107
108 static inline void sossi_write_reg8(int reg, u8 value)
109 {
110         writeb(value, sossi.base + reg);
111 }
112
113 static void sossi_set_bits(int reg, u32 bits)
114 {
115         sossi_write_reg(reg, sossi_read_reg(reg) | bits);
116 }
117
118 static void sossi_clear_bits(int reg, u32 bits)
119 {
120         sossi_write_reg(reg, sossi_read_reg(reg) & ~bits);
121 }
122
123 #define HZ_TO_PS(x)     (1000000000 / (x / 1000))
124
125 static u32 ps_to_sossi_ticks(u32 ps, int div)
126 {
127         u32 clk_period = HZ_TO_PS(sossi.fck_hz) * div;
128         return (clk_period + ps - 1) / clk_period;
129 }
130
131 static int calc_rd_timings(struct extif_timings *t)
132 {
133         u32 tw0, tw1;
134         int reon, reoff, recyc, actim;
135         int div = t->clk_div;
136
137         /* Make sure that after conversion it still holds that:
138          * reoff > reon, recyc >= reoff, actim > reon
139          */
140         reon = ps_to_sossi_ticks(t->re_on_time, div);
141         /* reon will be exactly one sossi tick */
142         if (reon > 1)
143                 return -1;
144
145         reoff = ps_to_sossi_ticks(t->re_off_time, div);
146
147         if (reoff <= reon)
148                 reoff = reon + 1;
149
150         tw0 = reoff - reon;
151         if (tw0 > 0x10)
152                 return -1;
153
154         recyc = ps_to_sossi_ticks(t->re_cycle_time, div);
155         if (recyc <= reoff)
156                 recyc = reoff + 1;
157
158         tw1 = recyc - tw0;
159         /* values less then 3 result in the SOSSI block resetting itself */
160         if (tw1 < 3)
161                 tw1 = 3;
162         if (tw1 > 0x40)
163                 return -1;
164
165         actim = ps_to_sossi_ticks(t->access_time, div);
166         if (actim < reoff)
167                 actim++;
168         /* access time (data hold time) will be exactly one sossi
169          * tick
170          */
171         if (actim - reoff > 1)
172                 return -1;
173
174         t->tim[0] = tw0 - 1;
175         t->tim[1] = tw1 - 1;
176
177         return 0;
178 }
179
180 static int calc_wr_timings(struct extif_timings *t)
181 {
182         u32 tw0, tw1;
183         int weon, weoff, wecyc;
184         int div = t->clk_div;
185
186         /* Make sure that after conversion it still holds that:
187          * weoff > weon, wecyc >= weoff
188          */
189         weon = ps_to_sossi_ticks(t->we_on_time, div);
190         /* weon will be exactly one sossi tick */
191         if (weon > 1)
192                 return -1;
193
194         weoff = ps_to_sossi_ticks(t->we_off_time, div);
195         if (weoff <= weon)
196                 weoff = weon + 1;
197         tw0 = weoff - weon;
198         if (tw0 > 0x10)
199                 return -1;
200
201         wecyc = ps_to_sossi_ticks(t->we_cycle_time, div);
202         if (wecyc <= weoff)
203                 wecyc = weoff + 1;
204
205         tw1 = wecyc - tw0;
206         /* values less then 3 result in the SOSSI block resetting itself */
207         if (tw1 < 3)
208                 tw1 = 3;
209         if (tw1 > 0x40)
210                 return -1;
211
212         t->tim[2] = tw0 - 1;
213         t->tim[3] = tw1 - 1;
214
215         return 0;
216 }
217
218 static void _set_timing(int div, int tw0, int tw1)
219 {
220         u32 l;
221
222 #ifdef VERBOSE
223         dev_dbg(sossi.fbdev->dev, "Using TW0 = %d, TW1 = %d, div = %d\n",
224                  tw0 + 1, tw1 + 1, div);
225 #endif
226
227         clk_set_rate(sossi.fck, sossi.fck_hz / div);
228         clk_enable(sossi.fck);
229         l = sossi_read_reg(SOSSI_INIT1_REG);
230         l &= ~((0x0f << 20) | (0x3f << 24));
231         l |= (tw0 << 20) | (tw1 << 24);
232         sossi_write_reg(SOSSI_INIT1_REG, l);
233         clk_disable(sossi.fck);
234 }
235
236 static void _set_bits_per_cycle(int bus_pick_count, int bus_pick_width)
237 {
238         u32 l;
239
240         l = sossi_read_reg(SOSSI_INIT3_REG);
241         l &= ~0x3ff;
242         l |= ((bus_pick_count - 1) << 5) | ((bus_pick_width - 1) & 0x1f);
243         sossi_write_reg(SOSSI_INIT3_REG, l);
244 }
245
246 static void _set_tearsync_mode(int mode, unsigned line)
247 {
248         u32 l;
249
250         l = sossi_read_reg(SOSSI_TEARING_REG);
251         l &= ~(((1 << 11) - 1) << 15);
252         l |= line << 15;
253         l &= ~(0x3 << 26);
254         l |= mode << 26;
255         sossi_write_reg(SOSSI_TEARING_REG, l);
256         if (mode)
257                 sossi_set_bits(SOSSI_INIT2_REG, 1 << 6);        /* TE logic */
258         else
259                 sossi_clear_bits(SOSSI_INIT2_REG, 1 << 6);
260 }
261
262 static inline void set_timing(int access)
263 {
264         if (access != sossi.last_access) {
265                 sossi.last_access = access;
266                 _set_timing(sossi.clk_div,
267                             sossi.clk_tw0[access], sossi.clk_tw1[access]);
268         }
269 }
270
271 static void sossi_start_transfer(void)
272 {
273         /* WE */
274         sossi_clear_bits(SOSSI_INIT2_REG, 1 << 4);
275         /* CS active low */
276         sossi_clear_bits(SOSSI_INIT1_REG, 1 << 30);
277 }
278
279 static void sossi_stop_transfer(void)
280 {
281         /* WE */
282         sossi_set_bits(SOSSI_INIT2_REG, 1 << 4);
283         /* CS active low */
284         sossi_set_bits(SOSSI_INIT1_REG, 1 << 30);
285 }
286
287 static void wait_end_of_write(void)
288 {
289         /* Before reading we must check if some writings are going on */
290         while (!(sossi_read_reg(SOSSI_INIT2_REG) & (1 << 3)));
291 }
292
293 static void send_data(const void *data, unsigned int len)
294 {
295         while (len >= 4) {
296                 sossi_write_reg(SOSSI_FIFO_REG, *(const u32 *) data);
297                 len -= 4;
298                 data += 4;
299         }
300         while (len >= 2) {
301                 sossi_write_reg16(SOSSI_FIFO_REG, *(const u16 *) data);
302                 len -= 2;
303                 data += 2;
304         }
305         while (len) {
306                 sossi_write_reg8(SOSSI_FIFO_REG, *(const u8 *) data);
307                 len--;
308                 data++;
309         }
310 }
311
312 static void set_cycles(unsigned int len)
313 {
314         unsigned long nr_cycles = len / (sossi.bus_pick_width / 8);
315
316         BUG_ON((nr_cycles - 1) & ~0x3ffff);
317
318         sossi_clear_bits(SOSSI_INIT1_REG, 0x3ffff);
319         sossi_set_bits(SOSSI_INIT1_REG, (nr_cycles - 1) & 0x3ffff);
320 }
321
322 static int sossi_convert_timings(struct extif_timings *t)
323 {
324         int r = 0;
325         int div = t->clk_div;
326
327         t->converted = 0;
328
329         if (div <= 0 || div > 8)
330                 return -1;
331
332         /* no CS on SOSSI, so ignore cson, csoff, cs_pulsewidth */
333         if ((r = calc_rd_timings(t)) < 0)
334                 return r;
335
336         if ((r = calc_wr_timings(t)) < 0)
337                 return r;
338
339         t->tim[4] = div;
340
341         t->converted = 1;
342
343         return 0;
344 }
345
346 static void sossi_set_timings(const struct extif_timings *t)
347 {
348         BUG_ON(!t->converted);
349
350         sossi.clk_tw0[RD_ACCESS] = t->tim[0];
351         sossi.clk_tw1[RD_ACCESS] = t->tim[1];
352
353         sossi.clk_tw0[WR_ACCESS] = t->tim[2];
354         sossi.clk_tw1[WR_ACCESS] = t->tim[3];
355
356         sossi.clk_div = t->tim[4];
357 }
358
359 static void sossi_get_clk_info(u32 *clk_period, u32 *max_clk_div)
360 {
361         *clk_period = HZ_TO_PS(sossi.fck_hz);
362         *max_clk_div = 8;
363 }
364
365 static void sossi_set_bits_per_cycle(int bpc)
366 {
367         int bus_pick_count, bus_pick_width;
368
369         /* We set explicitly the the bus_pick_count as well, although
370          * with remapping/reordering disabled it will be calculated by HW
371          * as (32 / bus_pick_width).
372          */
373         switch (bpc) {
374         case 8:
375                 bus_pick_count = 4;
376                 bus_pick_width = 8;
377                 break;
378         case 16:
379                 bus_pick_count = 2;
380                 bus_pick_width = 16;
381                 break;
382         default:
383                 BUG();
384                 return;
385         }
386         sossi.bus_pick_width = bus_pick_width;
387         sossi.bus_pick_count = bus_pick_count;
388 }
389
390 static int sossi_setup_tearsync(unsigned pin_cnt,
391                                 unsigned hs_pulse_time, unsigned vs_pulse_time,
392                                 int hs_pol_inv, int vs_pol_inv, int div)
393 {
394         int hs, vs;
395         u32 l;
396
397         if (pin_cnt != 1 || div < 1 || div > 8)
398                 return -EINVAL;
399
400         hs = ps_to_sossi_ticks(hs_pulse_time, div);
401         vs = ps_to_sossi_ticks(vs_pulse_time, div);
402         if (vs < 8 || vs <= hs || vs >= (1 << 12))
403                 return -EDOM;
404         vs /= 8;
405         vs--;
406         if (hs > 8)
407                 hs = 8;
408         if (hs)
409                 hs--;
410
411         dev_dbg(sossi.fbdev->dev,
412                 "setup_tearsync: hs %d vs %d hs_inv %d vs_inv %d\n",
413                 hs, vs, hs_pol_inv, vs_pol_inv);
414
415         clk_enable(sossi.fck);
416         l = sossi_read_reg(SOSSI_TEARING_REG);
417         l &= ~((1 << 15) - 1);
418         l |= vs << 3;
419         l |= hs;
420         if (hs_pol_inv)
421                 l |= 1 << 29;
422         else
423                 l &= ~(1 << 29);
424         if (vs_pol_inv)
425                 l |= 1 << 28;
426         else
427                 l &= ~(1 << 28);
428         sossi_write_reg(SOSSI_TEARING_REG, l);
429         clk_disable(sossi.fck);
430
431         return 0;
432 }
433
434 static int sossi_enable_tearsync(int enable, unsigned line)
435 {
436         int mode;
437
438         dev_dbg(sossi.fbdev->dev, "tearsync %d line %d\n", enable, line);
439         if (line >= 1 << 11)
440                 return -EINVAL;
441         if (enable) {
442                 if (line)
443                         mode = 2;               /* HS or VS */
444                 else
445                         mode = 3;               /* VS only */
446         } else
447                 mode = 0;
448         sossi.tearsync_line = line;
449         sossi.tearsync_mode = mode;
450
451         return 0;
452 }
453
454 static void sossi_write_command(const void *data, unsigned int len)
455 {
456         clk_enable(sossi.fck);
457         set_timing(WR_ACCESS);
458         _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
459         /* CMD#/DATA */
460         sossi_clear_bits(SOSSI_INIT1_REG, 1 << 18);
461         set_cycles(len);
462         sossi_start_transfer();
463         send_data(data, len);
464         sossi_stop_transfer();
465         wait_end_of_write();
466         clk_disable(sossi.fck);
467 }
468
469 static void sossi_write_data(const void *data, unsigned int len)
470 {
471         clk_enable(sossi.fck);
472         set_timing(WR_ACCESS);
473         _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
474         /* CMD#/DATA */
475         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
476         set_cycles(len);
477         sossi_start_transfer();
478         send_data(data, len);
479         sossi_stop_transfer();
480         wait_end_of_write();
481         clk_disable(sossi.fck);
482 }
483
484 static void sossi_transfer_area(int width, int height,
485                                 void (callback)(void *data), void *data)
486 {
487         BUG_ON(callback == NULL);
488
489         sossi.lcdc_callback = callback;
490         sossi.lcdc_callback_data = data;
491
492         clk_enable(sossi.fck);
493         set_timing(WR_ACCESS);
494         _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
495         _set_tearsync_mode(sossi.tearsync_mode, sossi.tearsync_line);
496         /* CMD#/DATA */
497         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
498         set_cycles(width * height * sossi.bus_pick_width / 8);
499
500         sossi_start_transfer();
501         if (sossi.tearsync_mode) {
502                 /* Wait for the sync signal and start the transfer only
503                  * then. We can't seem to be able to use HW sync DMA for
504                  * this since LCD DMA shows huge latencies, as if it
505                  * would ignore some of the DMA requests from SoSSI.
506                  */
507                 unsigned long flags;
508
509                 spin_lock_irqsave(&sossi.lock, flags);
510                 sossi.vsync_dma_pending++;
511                 spin_unlock_irqrestore(&sossi.lock, flags);
512         } else
513                 /* Just start the transfer right away. */
514                 omap_enable_lcd_dma();
515 }
516
517 static void sossi_dma_callback(void *data)
518 {
519         omap_stop_lcd_dma();
520         sossi_stop_transfer();
521         clk_disable(sossi.fck);
522         sossi.lcdc_callback(sossi.lcdc_callback_data);
523 }
524
525 static void sossi_read_data(void *data, unsigned int len)
526 {
527         clk_enable(sossi.fck);
528         set_timing(RD_ACCESS);
529         _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
530         /* CMD#/DATA */
531         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
532         set_cycles(len);
533         sossi_start_transfer();
534         while (len >= 4) {
535                 *(u32 *) data = sossi_read_reg(SOSSI_FIFO_REG);
536                 len -= 4;
537                 data += 4;
538         }
539         while (len >= 2) {
540                 *(u16 *) data = sossi_read_reg16(SOSSI_FIFO_REG);
541                 len -= 2;
542                 data += 2;
543         }
544         while (len) {
545                 *(u8 *) data = sossi_read_reg8(SOSSI_FIFO_REG);
546                 len--;
547                 data++;
548         }
549         sossi_stop_transfer();
550         clk_disable(sossi.fck);
551 }
552
553 static irqreturn_t sossi_match_irq(int irq, void *data)
554 {
555         unsigned long flags;
556
557         spin_lock_irqsave(&sossi.lock, flags);
558         if (sossi.vsync_dma_pending) {
559                 sossi.vsync_dma_pending--;
560                 omap_enable_lcd_dma();
561         }
562         spin_unlock_irqrestore(&sossi.lock, flags);
563         return IRQ_HANDLED;
564 }
565
566 static int sossi_init(struct omapfb_device *fbdev)
567 {
568         u32 l, k;
569         struct clk *fck;
570         struct clk *dpll1out_ck;
571         int r;
572
573         sossi.base = (void __iomem *)IO_ADDRESS(OMAP_SOSSI_BASE);
574         sossi.fbdev = fbdev;
575         spin_lock_init(&sossi.lock);
576
577         dpll1out_ck = clk_get(fbdev->dev, "ck_dpll1out");
578         if (IS_ERR(dpll1out_ck)) {
579                 dev_err(fbdev->dev, "can't get DPLL1OUT clock\n");
580                 return PTR_ERR(dpll1out_ck);
581         }
582         /* We need the parent clock rate, which we might divide further
583          * depending on the timing requirements of the controller. See
584          * _set_timings.
585          */
586         sossi.fck_hz = clk_get_rate(dpll1out_ck);
587         clk_put(dpll1out_ck);
588
589         fck = clk_get(fbdev->dev, "ck_sossi");
590         if (IS_ERR(fck)) {
591                 dev_err(fbdev->dev, "can't get SoSSI functional clock\n");
592                 return PTR_ERR(fck);
593         }
594         sossi.fck = fck;
595
596         /* Reset and enable the SoSSI module */
597         l = omap_readl(MOD_CONF_CTRL_1);
598         l |= CONF_SOSSI_RESET_R;
599         omap_writel(l, MOD_CONF_CTRL_1);
600         l &= ~CONF_SOSSI_RESET_R;
601         omap_writel(l, MOD_CONF_CTRL_1);
602
603         clk_enable(sossi.fck);
604         l = omap_readl(ARM_IDLECT2);
605         l &= ~(1 << 8);                 /* DMACK_REQ */
606         omap_writel(l, ARM_IDLECT2);
607
608         l = sossi_read_reg(SOSSI_INIT2_REG);
609         /* Enable and reset the SoSSI block */
610         l |= (1 << 0) | (1 << 1);
611         sossi_write_reg(SOSSI_INIT2_REG, l);
612         /* Take SoSSI out of reset */
613         l &= ~(1 << 1);
614         sossi_write_reg(SOSSI_INIT2_REG, l);
615
616         sossi_write_reg(SOSSI_ID_REG, 0);
617         l = sossi_read_reg(SOSSI_ID_REG);
618         k = sossi_read_reg(SOSSI_ID_REG);
619
620         if (l != 0x55555555 || k != 0xaaaaaaaa) {
621                 dev_err(fbdev->dev,
622                         "invalid SoSSI sync pattern: %08x, %08x\n", l, k);
623                 r = -ENODEV;
624                 goto err;
625         }
626
627         if ((r = omap_lcdc_set_dma_callback(sossi_dma_callback, NULL)) < 0) {
628                 dev_err(fbdev->dev, "can't get LCDC IRQ\n");
629                 r = -ENODEV;
630                 goto err;
631         }
632
633         l = sossi_read_reg(SOSSI_ID_REG); /* Component code */
634         l = sossi_read_reg(SOSSI_ID_REG);
635         dev_info(fbdev->dev, "SoSSI version %d.%d initialized\n",
636                 l >> 16, l & 0xffff);
637
638         l = sossi_read_reg(SOSSI_INIT1_REG);
639         l |= (1 << 19); /* DMA_MODE */
640         l &= ~(1 << 31); /* REORDERING */
641         sossi_write_reg(SOSSI_INIT1_REG, l);
642
643         if ((r = request_irq(INT_SOSSI_MATCH, sossi_match_irq, IRQT_FALLING,
644              "sossi_match", sossi.fbdev->dev)) < 0) {
645                 dev_err(sossi.fbdev->dev, "can't get SoSSI match IRQ\n");
646                 goto err;
647         }
648
649         clk_disable(sossi.fck);
650         return 0;
651
652 err:
653         clk_disable(sossi.fck);
654         clk_put(sossi.fck);
655         return r;
656 }
657
658 static void sossi_cleanup(void)
659 {
660         omap_lcdc_free_dma_callback();
661         clk_put(sossi.fck);
662 }
663
664 struct lcd_ctrl_extif sossi_extif = {
665         .init                   = sossi_init,
666         .cleanup                = sossi_cleanup,
667         .get_clk_info           = sossi_get_clk_info,
668         .convert_timings        = sossi_convert_timings,
669         .set_timings            = sossi_set_timings,
670         .set_bits_per_cycle     = sossi_set_bits_per_cycle,
671         .setup_tearsync         = sossi_setup_tearsync,
672         .enable_tearsync        = sossi_enable_tearsync,
673         .write_command          = sossi_write_command,
674         .read_data              = sossi_read_data,
675         .write_data             = sossi_write_data,
676         .transfer_area          = sossi_transfer_area,
677
678         .max_transmit_size      = SOSSI_MAX_XMIT_BYTES,
679 };
680