]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/video/omap/sossi.c
ARM: OMAP: omapfb: main and LCD controller module changes
[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/config.h>
24 #include <linux/module.h>
25 #include <linux/mm.h>
26 #include <linux/clk.h>
27
28 #include <asm/io.h>
29
30 #include <asm/arch/dma.h>
31 #include <asm/arch/omapfb.h>
32
33 #include "lcdc.h"
34
35 #define MODULE_NAME             "omapfb-sossi"
36
37 #define OMAP_SOSSI_BASE         0xfffbac00
38 #define SOSSI_ID_REG            0x00
39 #define SOSSI_INIT1_REG         0x04
40 #define SOSSI_INIT2_REG         0x08
41 #define SOSSI_INIT3_REG         0x0c
42 #define SOSSI_FIFO_REG          0x10
43 #define SOSSI_REOTABLE_REG      0x14
44 #define SOSSI_TEARING_REG       0x18
45 #define SOSSI_INIT1B_REG        0x1c
46 #define SOSSI_FIFOB_REG         0x20
47
48 #define DMA_GSCR          0xfffedc04
49 #define DMA_LCD_CCR       0xfffee3c2
50 #define DMA_LCD_CTRL      0xfffee3c4
51 #define DMA_LCD_LCH_CTRL  0xfffee3ea
52
53 #define RD_ACCESS               0
54 #define WR_ACCESS               1
55
56 #define SOSSI_MAX_XMIT_BYTES    (512 * 1024)
57
58 static struct {
59         void __iomem    *base;
60         unsigned long   dpll_khz;
61         int             bus_pick_width;
62         void            (*lcdc_callback)(void *data);
63         void            *lcdc_callback_data;
64         /* timing for read and write access */
65         int             clk_div;
66         u8              clk_tw0[2];
67         u8              clk_tw1[2];
68         /* if last_access is the same as current we don't have to change
69          * the timings
70          */
71         int             last_access;
72
73         struct omapfb_device    *fbdev;
74         struct lcd_ctrl_extif   *extif;
75 } sossi;
76
77 static inline u32 sossi_read_reg(int reg)
78 {
79         return readl(sossi.base + reg);
80 }
81
82 static inline u16 sossi_read_reg16(int reg)
83 {
84         return readw(sossi.base + reg);
85 }
86
87 static inline u8 sossi_read_reg8(int reg)
88 {
89         return readb(sossi.base + reg);
90 }
91
92 static inline void sossi_write_reg(int reg, u32 value)
93 {
94         writel(value, sossi.base + reg);
95 }
96
97 static inline void sossi_write_reg16(int reg, u16 value)
98 {
99         writew(value, sossi.base + reg);
100 }
101
102 static inline void sossi_write_reg8(int reg, u8 value)
103 {
104         writeb(value, sossi.base + reg);
105 }
106
107 static void sossi_set_bits(int reg, u32 bits)
108 {
109         sossi_write_reg(reg, sossi_read_reg(reg) | bits);
110 }
111
112 static void sossi_clear_bits(int reg, u32 bits)
113 {
114         sossi_write_reg(reg, sossi_read_reg(reg) & ~bits);
115 }
116
117 #define MOD_CONF_CTRL_1   0xfffe1110
118 #define CONF_SOSSI_RESET_R      (1 << 23)
119 #define CONF_MOD_SOSSI_CLK_EN_R (1 << 16)
120
121 static void sossi_dma_callback(void *data);
122
123 #define KHZ_TO_PS(x)    (1000000000 / (x))
124
125 static u32 ps_to_sossi_ticks(u32 ps, int div)
126 {
127         u32 clk_period = KHZ_TO_PS(sossi.dpll_khz) * 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 + 1);
225 #endif
226
227         l = omap_readl(MOD_CONF_CTRL_1);
228         l &= ~(7 << 17);
229         l |= div << 17;
230         omap_writel(l, MOD_CONF_CTRL_1);
231
232         l = sossi_read_reg(SOSSI_INIT1_REG);
233         l &= ~((0x0f << 20) | (0x3f << 24));
234         l |= (tw0 << 20) | (tw1 << 24);
235         sossi_write_reg(SOSSI_INIT1_REG, l);
236 }
237
238 static inline void set_timing(int access)
239 {
240         if (access != sossi.last_access) {
241                 sossi.last_access = access;
242                 _set_timing(sossi.clk_div,
243                             sossi.clk_tw0[access], sossi.clk_tw1[access]);
244         }
245 }
246
247 static void sossi_start_transfer(void)
248 {
249         /* WE */
250         sossi_clear_bits(SOSSI_INIT2_REG, 1 << 4);
251         /* CS active low */
252         sossi_clear_bits(SOSSI_INIT1_REG, 1 << 30);
253         /* FIXME: locking? */
254 }
255
256 static void sossi_stop_transfer(void)
257 {
258         /* WE */
259         sossi_set_bits(SOSSI_INIT2_REG, 1 << 4);
260         /* CS active low */
261         sossi_set_bits(SOSSI_INIT1_REG, 1 << 30);
262         /* FIXME: locking? */
263 }
264
265 static void wait_end_of_write(void)
266 {
267         /* Before reading we must check if some writings are going on */
268         while (!(sossi_read_reg(SOSSI_INIT2_REG) & (1 << 3)));
269 }
270
271 static void send_data(const void *data, unsigned int len)
272 {
273         while (len >= 4) {
274                 sossi_write_reg(SOSSI_FIFO_REG, *(const u32 *) data);
275                 len -= 4;
276                 data += 4;
277         }
278         while (len >= 2) {
279                 sossi_write_reg16(SOSSI_FIFO_REG, *(const u16 *) data);
280                 len -= 2;
281                 data += 2;
282         }
283         while (len) {
284                 sossi_write_reg8(SOSSI_FIFO_REG, *(const u8 *) data);
285                 len--;
286                 data++;
287         }
288 }
289
290 static void set_cycles(unsigned int len)
291 {
292         unsigned long nr_cycles = len / (sossi.bus_pick_width / 8);
293
294         BUG_ON((nr_cycles - 1) & ~0x3ffff);
295
296         sossi_clear_bits(SOSSI_INIT1_REG, 0x3ffff);
297         sossi_set_bits(SOSSI_INIT1_REG, (nr_cycles - 1) & 0x3ffff);
298 }
299
300 static int sossi_convert_timings(struct extif_timings *t)
301 {
302         int r = 0;
303         int div = t->clk_div;
304
305         t->converted = 0;
306
307         if (div <= 0 || div > 8)
308                 return -1;
309
310         /* no CS on SOSSI, so ignore cson, csoff, cs_pulsewidth */
311         if ((r = calc_rd_timings(t)) < 0)
312                 return r;
313
314         if ((r = calc_wr_timings(t)) < 0)
315                 return r;
316
317         t->tim[4] = div - 1;
318
319         t->converted = 1;
320
321         return 0;
322 }
323
324 static void sossi_set_timings(const struct extif_timings *t)
325 {
326         BUG_ON(!t->converted);
327
328         sossi.clk_tw0[RD_ACCESS] = t->tim[0];
329         sossi.clk_tw1[RD_ACCESS] = t->tim[1];
330
331         sossi.clk_tw0[WR_ACCESS] = t->tim[2];
332         sossi.clk_tw1[WR_ACCESS] = t->tim[3];
333
334         sossi.clk_div = t->tim[4];
335 }
336
337 static void sossi_get_clk_info(u32 *clk_period, u32 *max_clk_div)
338 {
339         *clk_period = KHZ_TO_PS(sossi.dpll_khz);
340         *max_clk_div = 8;
341 }
342
343 static void sossi_set_bits_per_cycle(int bpc)
344 {
345         u32 l;
346         int bus_pick_count, bus_pick_width;
347
348         /* We set explicitly the the bus_pick_count as well, although
349          * with remapping/reordering disabled it will be calculated by HW
350          * as (32 / bus_pick_width).
351          */
352         switch (bpc) {
353         case 8:
354                 bus_pick_count = 4;
355                 bus_pick_width = 8;
356                 break;
357         case 16:
358                 bus_pick_count = 2;
359                 bus_pick_width = 16;
360                 break;
361         default:
362                 BUG();
363                 return;
364         }
365         l = sossi_read_reg(SOSSI_INIT3_REG);
366         sossi.bus_pick_width = bus_pick_width;
367         l &= ~0x3ff;
368         l |= ((bus_pick_count - 1) << 5) | ((bus_pick_width - 1) & 0x1f);
369         sossi_write_reg(SOSSI_INIT3_REG, l);
370 }
371
372 static void sossi_write_command(const void *data, unsigned int len)
373 {
374         set_timing(WR_ACCESS);
375         /* CMD#/DATA */
376         sossi_clear_bits(SOSSI_INIT1_REG, 1 << 18);
377         set_cycles(len);
378         sossi_start_transfer();
379         send_data(data, len);
380         sossi_stop_transfer();
381         wait_end_of_write();
382 }
383
384 static void sossi_write_data(const void *data, unsigned int len)
385 {
386         set_timing(WR_ACCESS);
387         /* CMD#/DATA */
388         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
389         set_cycles(len);
390         sossi_start_transfer();
391         send_data(data, len);
392         sossi_stop_transfer();
393         wait_end_of_write();
394 }
395
396 static void sossi_transfer_area(int width, int height,
397                                 void (callback)(void *data), void *data)
398 {
399         BUG_ON(callback == NULL);
400
401         sossi.lcdc_callback = callback;
402         sossi.lcdc_callback_data = data;
403
404         set_timing(WR_ACCESS);
405         /* CMD#/DATA */
406         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
407         set_cycles(width * height * sossi.bus_pick_width / 8);
408
409         sossi_start_transfer();
410         omap_enable_lcd_dma();
411 }
412
413 static void sossi_dma_callback(void *data)
414 {
415         omap_stop_lcd_dma();
416         sossi_stop_transfer();
417         sossi.lcdc_callback(sossi.lcdc_callback_data);
418 }
419
420 static void sossi_read_data(void *data, unsigned int len)
421 {
422         set_timing(RD_ACCESS);
423         /* CMD#/DATA */
424         sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
425         set_cycles(len);
426         sossi_start_transfer();
427         while (len >= 4) {
428                 *(u32 *) data = sossi_read_reg(SOSSI_FIFO_REG);
429                 len -= 4;
430                 data += 4;
431         }
432         while (len >= 2) {
433                 *(u16 *) data = sossi_read_reg16(SOSSI_FIFO_REG);
434                 len -= 2;
435                 data += 2;
436         }
437         while (len) {
438                 *(u8 *) data = sossi_read_reg8(SOSSI_FIFO_REG);
439                 len--;
440                 data++;
441         }
442         sossi_stop_transfer();
443 }
444
445 static int sossi_init(struct omapfb_device *fbdev)
446 {
447         u32 l, k;
448         struct clk *dpll_clk;
449         int r;
450
451         sossi.fbdev = fbdev;
452
453         sossi.base = (void __iomem *)IO_ADDRESS(OMAP_SOSSI_BASE);
454         dpll_clk = clk_get(fbdev->dev, "ck_dpll1");
455         if (IS_ERR(dpll_clk)) {
456                 dev_err(fbdev->dev, "can't get dpll1 clock\n");
457                 return PTR_ERR(dpll_clk);
458         }
459
460         sossi.dpll_khz = clk_get_rate(dpll_clk) / 1000;
461         clk_put(dpll_clk);
462
463         /* Reset and enable the SoSSI module */
464         l = omap_readl(MOD_CONF_CTRL_1);
465         l |= CONF_SOSSI_RESET_R;
466         omap_writel(l, MOD_CONF_CTRL_1);
467         l &= ~CONF_SOSSI_RESET_R;
468         omap_writel(l, MOD_CONF_CTRL_1);
469
470         l |= CONF_MOD_SOSSI_CLK_EN_R;
471         omap_writel(l, MOD_CONF_CTRL_1);
472
473         omap_writel(omap_readl(ARM_IDLECT2) | (1 << 11), ARM_IDLECT2);
474         omap_writel(omap_readl(ARM_IDLECT1) | (1 << 6), ARM_IDLECT1);
475
476         l = sossi_read_reg(SOSSI_INIT2_REG);
477         /* Enable and reset the SoSSI block */
478         l |= (1 << 0) | (1 << 1);
479         sossi_write_reg(SOSSI_INIT2_REG, l);
480         /* Take SoSSI out of reset */
481         l &= ~(1 << 1);
482         sossi_write_reg(SOSSI_INIT2_REG, l);
483
484         sossi_write_reg(SOSSI_ID_REG, 0);
485         l = sossi_read_reg(SOSSI_ID_REG);
486         k = sossi_read_reg(SOSSI_ID_REG);
487
488         if (l != 0x55555555 || k != 0xaaaaaaaa) {
489                 dev_err(fbdev->dev,
490                         "invalid SoSSI sync pattern: %08x, %08x\n", l, k);
491                 return -ENODEV;
492         }
493
494         if ((r = omap_lcdc_set_dma_callback(sossi_dma_callback, NULL)) < 0) {
495                 dev_err(fbdev->dev, "can't get LCDC IRQ\n");
496                 return r;
497         }
498
499         l = sossi_read_reg(SOSSI_ID_REG); /* Component code */
500         l = sossi_read_reg(SOSSI_ID_REG);
501         pr_info("omapfb: SoSSI version %d.%d initialized\n",
502                 l >> 16, l & 0xffff);
503
504         l = sossi_read_reg(SOSSI_INIT1_REG);
505         l |= (1 << 19); /* DMA_MODE */
506         l &= ~(1 << 31); /* REORDERING */
507         sossi_write_reg(SOSSI_INIT1_REG, l);
508
509         return 0;
510 }
511
512 static void sossi_cleanup(void)
513 {
514         omap_lcdc_free_dma_callback();
515 }
516
517 const struct lcd_ctrl_extif omap1_ext_if = {
518         .init                   = sossi_init,
519         .cleanup                = sossi_cleanup,
520         .get_clk_info           = sossi_get_clk_info,
521         .convert_timings        = sossi_convert_timings,
522         .set_timings            = sossi_set_timings,
523         .set_bits_per_cycle     = sossi_set_bits_per_cycle,
524         .write_command          = sossi_write_command,
525         .read_data              = sossi_read_data,
526         .write_data             = sossi_write_data,
527         .transfer_area          = sossi_transfer_area,
528
529         .max_transmit_size      = SOSSI_MAX_XMIT_BYTES,
530 };
531