]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/video/omap/sossi.c
ARM: OMAP: SoSSI: workaround for HW timing bug
[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
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/mm.h>
27 #include <linux/clk.h>
28
29 #include <asm/io.h>
30
31 #include <asm/arch/dma.h>
32 #include <asm/arch/omapfb.h>
33
34 #include "lcdc.h"
35
36 /* #define OMAPFB_DBG 1 */
37
38 #include "debug.h"
39
40 #define MODULE_NAME             "omapfb-sossi"
41
42 #define OMAP_SOSSI_BASE         0xfffbac00
43 #define SOSSI_ID_REG            0x00
44 #define SOSSI_INIT1_REG         0x04
45 #define SOSSI_INIT2_REG         0x08
46 #define SOSSI_INIT3_REG         0x0c
47 #define SOSSI_FIFO_REG          0x10
48 #define SOSSI_REOTABLE_REG      0x14
49 #define SOSSI_TEARING_REG       0x18
50 #define SOSSI_INIT1B_REG        0x1c
51 #define SOSSI_FIFOB_REG         0x20
52
53 #define DMA_GSCR          0xfffedc04
54 #define DMA_LCD_CCR       0xfffee3c2
55 #define DMA_LCD_CTRL      0xfffee3c4
56 #define DMA_LCD_LCH_CTRL  0xfffee3ea
57
58 #define RD_ACCESS               0
59 #define WR_ACCESS               1
60
61 #define SOSSI_MAX_XMIT_BYTES    (512 * 1024)
62
63 #define pr_err(fmt, args...) printk(KERN_ERR MODULE_NAME ": " fmt, ## args)
64
65 static struct sossi {
66         int             base;
67         unsigned long   dpll_khz;
68         int             bus_pick_width;
69         void            (*lcdc_callback)(void *data);
70         void            *lcdc_callback_data;
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 } sossi;
80
81 struct lcd_ctrl_extif sossi_extif;
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 MOD_CONF_CTRL_1   0xfffe1110
124 #define CONF_SOSSI_RESET_R      (1 << 23)
125 #define CONF_MOD_SOSSI_CLK_EN_R (1 << 16)
126
127 static void sossi_dma_callback(void *data);
128
129 static int sossi_init(void)
130 {
131         u32 l, k;
132         struct clk *dpll_clk;
133         int r;
134
135         sossi.base = IO_ADDRESS(OMAP_SOSSI_BASE);
136
137         dpll_clk = clk_get(NULL, "ck_dpll1");
138         if (IS_ERR(dpll_clk)) {
139                 pr_err("can't get dpll1 clock\n");
140                 return PTR_ERR(dpll_clk);
141         }
142
143         sossi.dpll_khz = clk_get_rate(dpll_clk) / 1000;
144         clk_put(dpll_clk);
145
146         sossi_extif.max_transmit_size = SOSSI_MAX_XMIT_BYTES;
147
148         /* Reset and enable the SoSSI module */
149         l = omap_readl(MOD_CONF_CTRL_1);
150         l |= CONF_SOSSI_RESET_R;
151         omap_writel(l, MOD_CONF_CTRL_1);
152         l &= ~CONF_SOSSI_RESET_R;
153         omap_writel(l, MOD_CONF_CTRL_1);
154
155         l |= CONF_MOD_SOSSI_CLK_EN_R;
156         omap_writel(l, MOD_CONF_CTRL_1);
157
158         omap_writel(omap_readl(ARM_IDLECT2) | (1 << 11), ARM_IDLECT2);
159         omap_writel(omap_readl(ARM_IDLECT1) | (1 << 6), ARM_IDLECT1);
160
161         l = sossi_read_reg(SOSSI_INIT2_REG);
162         /* Enable and reset the SoSSI block */
163         l |= (1 << 0) | (1 << 1);
164         sossi_write_reg(SOSSI_INIT2_REG, l);
165         /* Take SoSSI out of reset */
166         l &= ~(1 << 1);
167         sossi_write_reg(SOSSI_INIT2_REG, l);
168
169         sossi_write_reg(SOSSI_ID_REG, 0);
170         l = sossi_read_reg(SOSSI_ID_REG);
171         k = sossi_read_reg(SOSSI_ID_REG);
172
173         if (l != 0x55555555 || k != 0xaaaaaaaa) {
174                 pr_err("Invalid SoSSI sync pattern: %08x, %08x\n", l, k);
175                 return -ENODEV;
176         }
177
178         if ((r = omap_lcdc_set_dma_callback(sossi_dma_callback, NULL)) < 0) {
179                 pr_err("can't get LCDC IRQ\n");
180                 return r;
181         }
182
183         l = sossi_read_reg(SOSSI_ID_REG); /* Component code */
184         l = sossi_read_reg(SOSSI_ID_REG);
185         pr_info(KERN_INFO MODULE_NAME ": version %d.%d initialized\n",
186                         l >> 16, l & 0xffff);
187
188         l = sossi_read_reg(SOSSI_INIT1_REG);
189         l |= (1 << 19); /* DMA_MODE */
190         l &= ~(1 << 31); /* REORDERING */
191         sossi_write_reg(SOSSI_INIT1_REG, l);
192
193         return 0;
194 }
195
196 static void sossi_cleanup(void)
197 {
198         omap_lcdc_free_dma_callback();
199 }
200
201 #define KHZ_TO_PS(x)    (1000000000 / (x))
202
203 static void sossi_get_clk_info(u32 *clk_period, u32 *max_clk_div)
204 {
205         *clk_period = KHZ_TO_PS(sossi.dpll_khz);
206         *max_clk_div = 8;
207 }
208
209 static u32 ps_to_sossi_ticks(u32 ps, int div)
210 {
211         u32 clk_period = KHZ_TO_PS(sossi.dpll_khz) * div;
212         return (clk_period + ps - 1) / clk_period;
213 }
214
215 static int calc_rd_timings(struct extif_timings *t)
216 {
217         u32 tw0, tw1;
218         int reon, reoff, recyc, actim;
219         int div = t->clk_div;
220
221         /* Make sure that after conversion it still holds that:
222          * reoff > reon, recyc >= reoff, actim > reon
223          */
224         reon = ps_to_sossi_ticks(t->re_on_time, div);
225         /* reon will be exactly one sossi tick */
226         if (reon > 1)
227                 return -1;
228
229         reoff = ps_to_sossi_ticks(t->re_off_time, div);
230
231         if (reoff <= reon)
232                 reoff = reon + 1;
233
234         tw0 = reoff - reon;
235         if (tw0 > 0x10)
236                 return -1;
237
238         recyc = ps_to_sossi_ticks(t->re_cycle_time, div);
239         if (recyc <= reoff)
240                 recyc = reoff + 1;
241
242         tw1 = recyc - reoff;
243         /* values less then 3 result in the SOSSI block resetting itself */
244         if (tw1 < 3)
245                 tw1 = 3;
246         if (tw1 > 0x40)
247                 return -1;
248
249         actim = ps_to_sossi_ticks(t->access_time, div);
250         if (actim < reoff)
251                 actim++;
252         /* access time (data hold time) will be exactly one sossi
253          * tick
254          */
255         if (actim - reoff > 1)
256                 return -1;
257
258         t->tim[0] = tw0 - 1;
259         t->tim[1] = tw1 - 1;
260
261         return 0;
262 }
263
264 static int calc_wr_timings(struct extif_timings *t)
265 {
266         u32 tw0, tw1;
267         int weon, weoff, wecyc;
268         int div = t->clk_div;
269
270         /* Make sure that after conversion it still holds that:
271          * weoff > weon, wecyc >= weoff
272          */
273         weon = ps_to_sossi_ticks(t->we_on_time, div);
274         /* weon will be exactly one sossi tick */
275         if (weon > 1)
276                 return -1;
277
278         weoff = ps_to_sossi_ticks(t->we_off_time, div);
279         if (weoff <= weon)
280                 weoff = weon + 1;
281         tw0 = weoff - weon;
282         if (tw0 > 0x10)
283                 return -1;
284
285         wecyc = ps_to_sossi_ticks(t->we_cycle_time, div);
286         if (wecyc <= weoff)
287                 wecyc = weoff + 1;
288
289         tw1 = wecyc - weoff;
290         /* values less then 3 result in the SOSSI block resetting itself */
291         if (tw1 < 3)
292                 tw1 = 3;
293         if (tw1 > 0x40)
294                 return -1;
295
296         t->tim[2] = tw0 - 1;
297         t->tim[3] = tw1 - 1;
298
299         return 0;
300 }
301
302 static int sossi_convert_timings(struct extif_timings *t)
303 {
304         int r = 0;
305         int div = t->clk_div;
306
307         t->converted = 0;
308
309         if (div <= 0 || div > 8)
310                 return -1;
311
312         /* no CS on SOSSI, so ignore cson, csoff, cs_pulsewidth */
313         if ((r = calc_rd_timings(t)) < 0)
314                 return r;
315
316         if ((r = calc_wr_timings(t)) < 0)
317                 return r;
318
319         t->tim[4] = div - 1;
320
321         t->converted = 1;
322
323         return 0;
324 }
325
326 static void sossi_set_timings(const struct extif_timings *t)
327 {
328         BUG_ON(!t->converted);
329
330         sossi.clk_tw0[RD_ACCESS] = t->tim[0];
331         sossi.clk_tw1[RD_ACCESS] = t->tim[1];
332
333         sossi.clk_tw0[WR_ACCESS] = t->tim[2];
334         sossi.clk_tw1[WR_ACCESS] = t->tim[3];
335
336         sossi.clk_div = t->tim[4];
337 }
338
339 static void _set_timing(int div, int tw0, int tw1)
340 {
341         u32 l;
342
343         DBGPRINT(2, "Using TW0 = %d, TW1 = %d, div = %d\n",
344                  tw0 + 1, tw1 + 1, div + 1);
345
346         l = omap_readl(MOD_CONF_CTRL_1);
347         l &= ~(7 << 17);
348         l |= div << 17;
349         omap_writel(l, MOD_CONF_CTRL_1);
350
351         l = sossi_read_reg(SOSSI_INIT1_REG);
352         l &= ~((0x0f << 20) | (0x3f << 24));
353         l |= (tw0 << 20) | (tw1 << 24);
354         sossi_write_reg(SOSSI_INIT1_REG, l);
355 }
356
357 static inline void set_timing(int access)
358 {
359         if (access != sossi.last_access) {
360                 sossi.last_access = access;
361                 _set_timing(sossi.clk_div,
362                             sossi.clk_tw0[access], sossi.clk_tw1[access]);
363         }
364 }
365
366 static void sossi_set_bits_per_cycle(int bpc)
367 {
368         u32 l;
369         int bus_pick_count, bus_pick_width;
370
371         DBGPRINT(2, "bits_per_cycle %d\n", bpc);
372         /* We set explicitly the the bus_pick_count as well, although
373          * with remapping/reordering disabled it will be calculated by HW
374          * as (32 / bus_pick_width).
375          */
376         switch (bpc) {
377         case 8:
378                 bus_pick_count = 4;
379                 bus_pick_width = 8;
380                 break;
381         case 16:
382                 bus_pick_count = 2;
383                 bus_pick_width = 16;
384                 break;
385         default:
386                 BUG();
387                 return;
388         }
389         l = sossi_read_reg(SOSSI_INIT3_REG);
390         sossi.bus_pick_width = bus_pick_width;
391         l &= ~0x3ff;
392         l |= ((bus_pick_count - 1) << 5) | ((bus_pick_width - 1) & 0x1f);
393         sossi_write_reg(SOSSI_INIT3_REG, l);
394 }
395
396 static void sossi_start_transfer(void)
397 {
398         /* WE */
399         sossi_clear_bits(SOSSI_INIT2_REG, 1 << 4);
400         /* CS active low */
401         sossi_clear_bits(SOSSI_INIT1_REG, 1 << 30);
402         /* FIXME: locking? */
403 }
404
405 static void sossi_stop_transfer(void)
406 {
407         /* WE */
408         sossi_set_bits(SOSSI_INIT2_REG, 1 << 4);
409         /* CS active low */
410         sossi_set_bits(SOSSI_INIT1_REG, 1 << 30);
411         /* FIXME: locking? */
412 }
413
414 static void wait_end_of_write(void)
415 {
416         /* Before reading we must check if some writings are going on */
417         while (!(sossi_read_reg(SOSSI_INIT2_REG) & (1 << 3)));
418 }
419
420 static void send_data(const void *data, unsigned int len)
421 {
422         while (len >= 4) {
423                 sossi_write_reg(SOSSI_FIFO_REG, *(const u32 *) data);
424                 len -= 4;
425                 data += 4;
426         }
427         while (len >= 2) {
428                 sossi_write_reg16(SOSSI_FIFO_REG, *(const u16 *) data);
429                 len -= 2;
430                 data += 2;
431         }
432         while (len) {
433                 sossi_write_reg8(SOSSI_FIFO_REG, *(const u8 *) data);
434                 len--;
435                 data++;
436         }
437 }
438
439 static void set_cycles(unsigned int len)
440 {
441         unsigned long nr_cycles = len / (sossi.bus_pick_width / 8);
442
443         BUG_ON((nr_cycles - 1) & ~0x3ffff);
444
445         sossi_clear_bits(SOSSI_INIT1_REG, 0x3ffff);
446         sossi_set_bits(SOSSI_INIT1_REG, (nr_cycles - 1) & 0x3ffff);
447 }
448
449 static void sossi_write_command(const void *data, unsigned int len)
450 {
451         set_timing(WR_ACCESS);
452         /* CMD#/DATA */
453         sossi_clear_bits(SOSSI_INIT1_REG, 1 << 18);
454         set_cycles(len);
455         sossi_start_transfer();
456         send_data(data, len);
457         sossi_stop_transfer();
458         wait_end_of_write();
459 }
460
461 static void sossi_write_data(const void *data, unsigned int len)
462 {
463         set_timing(WR_ACCESS);
464         /* CMD#/DATA */
465         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
466         set_cycles(len);
467         sossi_start_transfer();
468         send_data(data, len);
469         sossi_stop_transfer();
470         wait_end_of_write();
471 }
472
473 static void sossi_transfer_area(int width, int height,
474                                 void (callback)(void *data), void *data)
475 {
476         BUG_ON(callback == NULL);
477
478         sossi.lcdc_callback = callback;
479         sossi.lcdc_callback_data = data;
480
481         set_timing(WR_ACCESS);
482         /* CMD#/DATA */
483         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
484         set_cycles(width * height * sossi.bus_pick_width / 8);
485
486         DBGPRINT(2, "SOSSI_INIT1_REG %08x\n", sossi_read_reg(SOSSI_INIT1_REG));
487
488         sossi_start_transfer();
489         omap_enable_lcd_dma();
490 }
491
492 static void sossi_dma_callback(void *data)
493 {
494         omap_stop_lcd_dma();
495         sossi_stop_transfer();
496         sossi.lcdc_callback(sossi.lcdc_callback_data);
497 }
498
499 static void sossi_read_data(void *data, unsigned int len)
500 {
501         set_timing(RD_ACCESS);
502         /* CMD#/DATA */
503         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
504         set_cycles(len);
505         sossi_start_transfer();
506         while (len >= 4) {
507                 *(u32 *) data = sossi_read_reg(SOSSI_FIFO_REG);
508                 len -= 4;
509                 data += 4;
510         }
511         while (len >= 2) {
512                 *(u16 *) data = sossi_read_reg16(SOSSI_FIFO_REG);
513                 len -= 2;
514                 data += 2;
515         }
516         while (len) {
517                 *(u8 *) data = sossi_read_reg8(SOSSI_FIFO_REG);
518                 len--;
519                 data++;
520         }
521         sossi_stop_transfer();
522 }
523
524 struct lcd_ctrl_extif sossi_extif = {
525         .init                   = sossi_init,
526         .cleanup                = sossi_cleanup,
527         .get_clk_info           = sossi_get_clk_info,
528         .convert_timings        = sossi_convert_timings,
529         .set_timings            = sossi_set_timings,
530         .set_bits_per_cycle     = sossi_set_bits_per_cycle,
531         .write_command          = sossi_write_command,
532         .read_data              = sossi_read_data,
533         .write_data             = sossi_write_data,
534         .transfer_area          = sossi_transfer_area,
535 };