2 * File: drivers/video/omap/omap1/sossi.c
4 * OMAP1 Special OptimiSed Screen Interface support
6 * Copyright (C) 2004-2005 Nokia Corporation
7 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
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.
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.
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.
23 #include <linux/module.h>
25 #include <linux/clk.h>
29 #include <asm/arch/dma.h>
30 #include <asm/arch/omapfb.h>
34 #define MODULE_NAME "omapfb-sossi"
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
47 #define DMA_GSCR 0xfffedc04
48 #define DMA_LCD_CCR 0xfffee3c2
49 #define DMA_LCD_CTRL 0xfffee3c4
50 #define DMA_LCD_LCH_CTRL 0xfffee3ea
52 #define CONF_SOSSI_RESET_R (1 << 23)
57 #define SOSSI_MAX_XMIT_BYTES (512 * 1024)
68 void (*lcdc_callback)(void *data);
69 void *lcdc_callback_data;
70 int vsync_dma_pending;
71 /* timing for read and write access */
75 /* if last_access is the same as current we don't have to change
80 struct omapfb_device *fbdev;
83 static inline u32 sossi_read_reg(int reg)
85 return readl(sossi.base + reg);
88 static inline u16 sossi_read_reg16(int reg)
90 return readw(sossi.base + reg);
93 static inline u8 sossi_read_reg8(int reg)
95 return readb(sossi.base + reg);
98 static inline void sossi_write_reg(int reg, u32 value)
100 writel(value, sossi.base + reg);
103 static inline void sossi_write_reg16(int reg, u16 value)
105 writew(value, sossi.base + reg);
108 static inline void sossi_write_reg8(int reg, u8 value)
110 writeb(value, sossi.base + reg);
113 static void sossi_set_bits(int reg, u32 bits)
115 sossi_write_reg(reg, sossi_read_reg(reg) | bits);
118 static void sossi_clear_bits(int reg, u32 bits)
120 sossi_write_reg(reg, sossi_read_reg(reg) & ~bits);
123 #define HZ_TO_PS(x) (1000000000 / (x / 1000))
125 static u32 ps_to_sossi_ticks(u32 ps, int div)
127 u32 clk_period = HZ_TO_PS(sossi.fck_hz) * div;
128 return (clk_period + ps - 1) / clk_period;
131 static int calc_rd_timings(struct extif_timings *t)
134 int reon, reoff, recyc, actim;
135 int div = t->clk_div;
137 /* Make sure that after conversion it still holds that:
138 * reoff > reon, recyc >= reoff, actim > reon
140 reon = ps_to_sossi_ticks(t->re_on_time, div);
141 /* reon will be exactly one sossi tick */
145 reoff = ps_to_sossi_ticks(t->re_off_time, div);
154 recyc = ps_to_sossi_ticks(t->re_cycle_time, div);
159 /* values less then 3 result in the SOSSI block resetting itself */
165 actim = ps_to_sossi_ticks(t->access_time, div);
168 /* access time (data hold time) will be exactly one sossi
171 if (actim - reoff > 1)
180 static int calc_wr_timings(struct extif_timings *t)
183 int weon, weoff, wecyc;
184 int div = t->clk_div;
186 /* Make sure that after conversion it still holds that:
187 * weoff > weon, wecyc >= weoff
189 weon = ps_to_sossi_ticks(t->we_on_time, div);
190 /* weon will be exactly one sossi tick */
194 weoff = ps_to_sossi_ticks(t->we_off_time, div);
201 wecyc = ps_to_sossi_ticks(t->we_cycle_time, div);
206 /* values less then 3 result in the SOSSI block resetting itself */
218 static void _set_timing(int div, int tw0, int tw1)
223 dev_dbg(sossi.fbdev->dev, "Using TW0 = %d, TW1 = %d, div = %d\n",
224 tw0 + 1, tw1 + 1, div);
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);
236 static void _set_bits_per_cycle(int bus_pick_count, int bus_pick_width)
240 l = sossi_read_reg(SOSSI_INIT3_REG);
242 l |= ((bus_pick_count - 1) << 5) | ((bus_pick_width - 1) & 0x1f);
243 sossi_write_reg(SOSSI_INIT3_REG, l);
246 static void _set_tearsync_mode(int mode, unsigned line)
250 l = sossi_read_reg(SOSSI_TEARING_REG);
251 l &= ~(((1 << 11) - 1) << 15);
255 sossi_write_reg(SOSSI_TEARING_REG, l);
257 sossi_set_bits(SOSSI_INIT2_REG, 1 << 6); /* TE logic */
259 sossi_clear_bits(SOSSI_INIT2_REG, 1 << 6);
262 static inline void set_timing(int access)
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]);
271 static void sossi_start_transfer(void)
274 sossi_clear_bits(SOSSI_INIT2_REG, 1 << 4);
276 sossi_clear_bits(SOSSI_INIT1_REG, 1 << 30);
279 static void sossi_stop_transfer(void)
282 sossi_set_bits(SOSSI_INIT2_REG, 1 << 4);
284 sossi_set_bits(SOSSI_INIT1_REG, 1 << 30);
287 static void wait_end_of_write(void)
289 /* Before reading we must check if some writings are going on */
290 while (!(sossi_read_reg(SOSSI_INIT2_REG) & (1 << 3)));
293 static void send_data(const void *data, unsigned int len)
296 sossi_write_reg(SOSSI_FIFO_REG, *(const u32 *) data);
301 sossi_write_reg16(SOSSI_FIFO_REG, *(const u16 *) data);
306 sossi_write_reg8(SOSSI_FIFO_REG, *(const u8 *) data);
312 static void set_cycles(unsigned int len)
314 unsigned long nr_cycles = len / (sossi.bus_pick_width / 8);
316 BUG_ON((nr_cycles - 1) & ~0x3ffff);
318 sossi_clear_bits(SOSSI_INIT1_REG, 0x3ffff);
319 sossi_set_bits(SOSSI_INIT1_REG, (nr_cycles - 1) & 0x3ffff);
322 static int sossi_convert_timings(struct extif_timings *t)
325 int div = t->clk_div;
329 if (div <= 0 || div > 8)
332 /* no CS on SOSSI, so ignore cson, csoff, cs_pulsewidth */
333 if ((r = calc_rd_timings(t)) < 0)
336 if ((r = calc_wr_timings(t)) < 0)
346 static void sossi_set_timings(const struct extif_timings *t)
348 BUG_ON(!t->converted);
350 sossi.clk_tw0[RD_ACCESS] = t->tim[0];
351 sossi.clk_tw1[RD_ACCESS] = t->tim[1];
353 sossi.clk_tw0[WR_ACCESS] = t->tim[2];
354 sossi.clk_tw1[WR_ACCESS] = t->tim[3];
356 sossi.clk_div = t->tim[4];
359 static void sossi_get_clk_info(u32 *clk_period, u32 *max_clk_div)
361 *clk_period = HZ_TO_PS(sossi.fck_hz);
365 static void sossi_set_bits_per_cycle(int bpc)
367 int bus_pick_count, bus_pick_width;
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).
386 sossi.bus_pick_width = bus_pick_width;
387 sossi.bus_pick_count = bus_pick_count;
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)
397 if (pin_cnt != 1 || div < 1 || div > 8)
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))
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);
415 clk_enable(sossi.fck);
416 l = sossi_read_reg(SOSSI_TEARING_REG);
417 l &= ~((1 << 15) - 1);
428 sossi_write_reg(SOSSI_TEARING_REG, l);
429 clk_disable(sossi.fck);
434 static int sossi_enable_tearsync(int enable, unsigned line)
438 dev_dbg(sossi.fbdev->dev, "tearsync %d line %d\n", enable, line);
443 mode = 2; /* HS or VS */
445 mode = 3; /* VS only */
448 sossi.tearsync_line = line;
449 sossi.tearsync_mode = mode;
454 static void sossi_write_command(const void *data, unsigned int len)
456 clk_enable(sossi.fck);
457 set_timing(WR_ACCESS);
458 _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
460 sossi_clear_bits(SOSSI_INIT1_REG, 1 << 18);
462 sossi_start_transfer();
463 send_data(data, len);
464 sossi_stop_transfer();
466 clk_disable(sossi.fck);
469 static void sossi_write_data(const void *data, unsigned int len)
471 clk_enable(sossi.fck);
472 set_timing(WR_ACCESS);
473 _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
475 sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
477 sossi_start_transfer();
478 send_data(data, len);
479 sossi_stop_transfer();
481 clk_disable(sossi.fck);
484 static void sossi_transfer_area(int width, int height,
485 void (callback)(void *data), void *data)
487 BUG_ON(callback == NULL);
489 sossi.lcdc_callback = callback;
490 sossi.lcdc_callback_data = data;
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);
497 sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
498 set_cycles(width * height * sossi.bus_pick_width / 8);
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.
509 spin_lock_irqsave(&sossi.lock, flags);
510 sossi.vsync_dma_pending++;
511 spin_unlock_irqrestore(&sossi.lock, flags);
513 /* Just start the transfer right away. */
514 omap_enable_lcd_dma();
517 static void sossi_dma_callback(void *data)
520 sossi_stop_transfer();
521 clk_disable(sossi.fck);
522 sossi.lcdc_callback(sossi.lcdc_callback_data);
525 static void sossi_read_data(void *data, unsigned int len)
527 clk_enable(sossi.fck);
528 set_timing(RD_ACCESS);
529 _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
531 sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
533 sossi_start_transfer();
535 *(u32 *) data = sossi_read_reg(SOSSI_FIFO_REG);
540 *(u16 *) data = sossi_read_reg16(SOSSI_FIFO_REG);
545 *(u8 *) data = sossi_read_reg8(SOSSI_FIFO_REG);
549 sossi_stop_transfer();
550 clk_disable(sossi.fck);
553 static irqreturn_t sossi_match_irq(int irq, void *data)
557 spin_lock_irqsave(&sossi.lock, flags);
558 if (sossi.vsync_dma_pending) {
559 sossi.vsync_dma_pending--;
560 omap_enable_lcd_dma();
562 spin_unlock_irqrestore(&sossi.lock, flags);
566 static int sossi_init(struct omapfb_device *fbdev)
570 struct clk *dpll1out_ck;
573 sossi.base = (void __iomem *)IO_ADDRESS(OMAP_SOSSI_BASE);
575 spin_lock_init(&sossi.lock);
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);
582 /* We need the parent clock rate, which we might divide further
583 * depending on the timing requirements of the controller. See
586 sossi.fck_hz = clk_get_rate(dpll1out_ck);
587 clk_put(dpll1out_ck);
589 fck = clk_get(fbdev->dev, "ck_sossi");
591 dev_err(fbdev->dev, "can't get SoSSI functional clock\n");
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);
603 clk_enable(sossi.fck);
604 l = omap_readl(ARM_IDLECT2);
605 l &= ~(1 << 8); /* DMACK_REQ */
606 omap_writel(l, ARM_IDLECT2);
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 */
614 sossi_write_reg(SOSSI_INIT2_REG, l);
616 sossi_write_reg(SOSSI_ID_REG, 0);
617 l = sossi_read_reg(SOSSI_ID_REG);
618 k = sossi_read_reg(SOSSI_ID_REG);
620 if (l != 0x55555555 || k != 0xaaaaaaaa) {
622 "invalid SoSSI sync pattern: %08x, %08x\n", l, k);
627 if ((r = omap_lcdc_set_dma_callback(sossi_dma_callback, NULL)) < 0) {
628 dev_err(fbdev->dev, "can't get LCDC IRQ\n");
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);
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);
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");
649 clk_disable(sossi.fck);
653 clk_disable(sossi.fck);
658 static void sossi_cleanup(void)
660 omap_lcdc_free_dma_callback();
664 struct lcd_ctrl_extif sossi_extif = {
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,
678 .max_transmit_size = SOSSI_MAX_XMIT_BYTES,