]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/video/omap/sossi.c
986f639f58895b1c038c665bebdce7b19d8d2966
[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         if (tw1 > 0x40)
244                 return -1;
245
246         actim = ps_to_sossi_ticks(t->access_time, div);
247         if (actim < reoff)
248                 actim++;
249         /* access time (data hold time) will be exactly one sossi
250          * tick
251          */
252         if (actim - reoff > 1)
253                 return -1;
254
255         t->tim[0] = tw0 - 1;
256         t->tim[1] = tw1 - 1;
257
258         return 0;
259 }
260
261 static int calc_wr_timings(struct extif_timings *t)
262 {
263         u32 tw0, tw1;
264         int weon, weoff, wecyc;
265         int div = t->clk_div;
266
267         /* Make sure that after conversion it still holds that:
268          * weoff > weon, wecyc >= weoff
269          */
270         weon = ps_to_sossi_ticks(t->we_on_time, div);
271         /* weon will be exactly one sossi tick */
272         if (weon > 1)
273                 return -1;
274
275         weoff = ps_to_sossi_ticks(t->we_off_time, div);
276         if (weoff <= weon)
277                 weoff = weon + 1;
278         tw0 = weoff - weon;
279         if (tw0 > 0x10)
280                 return -1;
281
282         wecyc = ps_to_sossi_ticks(t->we_cycle_time, div);
283         if (wecyc <= weoff)
284                 wecyc = weoff + 1;
285
286         tw1 = wecyc - weoff;
287         if (tw1 > 0x40)
288                 return -1;
289
290         t->tim[2] = tw0 - 1;
291         t->tim[3] = tw1 - 1;
292
293         return 0;
294 }
295
296 static int sossi_convert_timings(struct extif_timings *t)
297 {
298         int r = 0;
299         int div = t->clk_div;
300
301         t->converted = 0;
302
303         if (div <= 0 || div > 8)
304                 return -1;
305
306         /* no CS on SOSSI, so ignore cson, csoff, cs_pulsewidth */
307         if ((r = calc_rd_timings(t)) < 0)
308                 return r;
309
310         if ((r = calc_wr_timings(t)) < 0)
311                 return r;
312
313         t->tim[4] = div - 1;
314
315         t->converted = 1;
316
317         return 0;
318 }
319
320 static void sossi_set_timings(const struct extif_timings *t)
321 {
322         BUG_ON(!t->converted);
323
324         sossi.clk_tw0[RD_ACCESS] = t->tim[0];
325         sossi.clk_tw1[RD_ACCESS] = t->tim[1];
326
327         sossi.clk_tw0[WR_ACCESS] = t->tim[2];
328         sossi.clk_tw1[WR_ACCESS] = t->tim[3];
329
330         sossi.clk_div = t->tim[4];
331 }
332
333 static void _set_timing(int div, int tw0, int tw1)
334 {
335         u32 l;
336
337         DBGPRINT(2, "Using TW0 = %d, TW1 = %d, div = %d\n",
338                  tw0 + 1, tw1 + 1, div + 1);
339
340         l = omap_readl(MOD_CONF_CTRL_1);
341         l &= ~(7 << 17);
342         l |= div << 17;
343         omap_writel(l, MOD_CONF_CTRL_1);
344
345         l = sossi_read_reg(SOSSI_INIT1_REG);
346         l &= ~((0x0f << 20) | (0x3f << 24));
347         l |= (tw0 << 20) | (tw1 << 24);
348         sossi_write_reg(SOSSI_INIT1_REG, l);
349 }
350
351 static inline void set_timing(int access)
352 {
353         if (access != sossi.last_access) {
354                 sossi.last_access = access;
355                 _set_timing(sossi.clk_div,
356                             sossi.clk_tw0[access], sossi.clk_tw1[access]);
357         }
358 }
359
360 static void sossi_set_bits_per_cycle(int bpc)
361 {
362         u32 l;
363         int bus_pick_count, bus_pick_width;
364
365         DBGPRINT(2, "bits_per_cycle %d\n", bpc);
366         /* We set explicitly the the bus_pick_count as well, although
367          * with remapping/reordering disabled it will be calculated by HW
368          * as (32 / bus_pick_width).
369          */
370         switch (bpc) {
371         case 8:
372                 bus_pick_count = 4;
373                 bus_pick_width = 8;
374                 break;
375         case 16:
376                 bus_pick_count = 2;
377                 bus_pick_width = 16;
378                 break;
379         default:
380                 BUG();
381                 return;
382         }
383         l = sossi_read_reg(SOSSI_INIT3_REG);
384         sossi.bus_pick_width = bus_pick_width;
385         l &= ~0x3ff;
386         l |= ((bus_pick_count - 1) << 5) | ((bus_pick_width - 1) & 0x1f);
387         sossi_write_reg(SOSSI_INIT3_REG, l);
388 }
389
390 static void sossi_start_transfer(void)
391 {
392         /* WE */
393         sossi_clear_bits(SOSSI_INIT2_REG, 1 << 4);
394         /* CS active low */
395         sossi_clear_bits(SOSSI_INIT1_REG, 1 << 30);
396         /* FIXME: locking? */
397 }
398
399 static void sossi_stop_transfer(void)
400 {
401         /* WE */
402         sossi_set_bits(SOSSI_INIT2_REG, 1 << 4);
403         /* CS active low */
404         sossi_set_bits(SOSSI_INIT1_REG, 1 << 30);
405         /* FIXME: locking? */
406 }
407
408 static void wait_end_of_write(void)
409 {
410         /* Before reading we must check if some writings are going on */
411         while (!(sossi_read_reg(SOSSI_INIT2_REG) & (1 << 3)));
412 }
413
414 static void send_data(const void *data, unsigned int len)
415 {
416         while (len >= 4) {
417                 sossi_write_reg(SOSSI_FIFO_REG, *(const u32 *) data);
418                 len -= 4;
419                 data += 4;
420         }
421         while (len >= 2) {
422                 sossi_write_reg16(SOSSI_FIFO_REG, *(const u16 *) data);
423                 len -= 2;
424                 data += 2;
425         }
426         while (len) {
427                 sossi_write_reg8(SOSSI_FIFO_REG, *(const u8 *) data);
428                 len--;
429                 data++;
430         }
431 }
432
433 static void set_cycles(unsigned int len)
434 {
435         unsigned long nr_cycles = len / (sossi.bus_pick_width / 8);
436
437         BUG_ON((nr_cycles - 1) & ~0x3ffff);
438
439         sossi_clear_bits(SOSSI_INIT1_REG, 0x3ffff);
440         sossi_set_bits(SOSSI_INIT1_REG, (nr_cycles - 1) & 0x3ffff);
441 }
442
443 static void sossi_write_command(const void *data, unsigned int len)
444 {
445         set_timing(WR_ACCESS);
446         /* CMD#/DATA */
447         sossi_clear_bits(SOSSI_INIT1_REG, 1 << 18);
448         set_cycles(len);
449         sossi_start_transfer();
450         send_data(data, len);
451         sossi_stop_transfer();
452         wait_end_of_write();
453 }
454
455 static void sossi_write_data(const void *data, unsigned int len)
456 {
457         set_timing(WR_ACCESS);
458         /* CMD#/DATA */
459         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
460         set_cycles(len);
461         sossi_start_transfer();
462         send_data(data, len);
463         sossi_stop_transfer();
464         wait_end_of_write();
465 }
466
467 static void sossi_transfer_area(int width, int height,
468                                 void (callback)(void *data), void *data)
469 {
470         BUG_ON(callback == NULL);
471
472         sossi.lcdc_callback = callback;
473         sossi.lcdc_callback_data = data;
474
475         set_timing(WR_ACCESS);
476         /* CMD#/DATA */
477         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
478         set_cycles(width * height * sossi.bus_pick_width / 8);
479
480         DBGPRINT(2, "SOSSI_INIT1_REG %08x\n", sossi_read_reg(SOSSI_INIT1_REG));
481
482         sossi_start_transfer();
483         omap_enable_lcd_dma();
484 }
485
486 static void sossi_dma_callback(void *data)
487 {
488         omap_stop_lcd_dma();
489         sossi_stop_transfer();
490         sossi.lcdc_callback(sossi.lcdc_callback_data);
491 }
492
493 static void sossi_read_data(void *data, unsigned int len)
494 {
495         set_timing(RD_ACCESS);
496         /* CMD#/DATA */
497         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
498         set_cycles(len);
499         sossi_start_transfer();
500         while (len >= 4) {
501                 *(u32 *) data = sossi_read_reg(SOSSI_FIFO_REG);
502                 len -= 4;
503                 data += 4;
504         }
505         while (len >= 2) {
506                 *(u16 *) data = sossi_read_reg16(SOSSI_FIFO_REG);
507                 len -= 2;
508                 data += 2;
509         }
510         while (len) {
511                 *(u8 *) data = sossi_read_reg8(SOSSI_FIFO_REG);
512                 len--;
513                 data++;
514         }
515         sossi_stop_transfer();
516 }
517
518 struct lcd_ctrl_extif sossi_extif = {
519         .init                   = sossi_init,
520         .cleanup                = sossi_cleanup,
521         .get_clk_info           = sossi_get_clk_info,
522         .convert_timings        = sossi_convert_timings,
523         .set_timings            = sossi_set_timings,
524         .set_bits_per_cycle     = sossi_set_bits_per_cycle,
525         .write_command          = sossi_write_command,
526         .read_data              = sossi_read_data,
527         .write_data             = sossi_write_data,
528         .transfer_area          = sossi_transfer_area,
529 };