2 * OMAP1 Special OptimiSed Screen Interface support
4 * Copyright (C) 2004-2005 Nokia Corporation
5 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include <linux/module.h>
23 #include <linux/clk.h>
24 #include <linux/irq.h>
28 #include <mach/omapfb.h>
32 #define MODULE_NAME "omapfb-sossi"
34 #define OMAP_SOSSI_BASE 0xfffbac00
35 #define SOSSI_ID_REG 0x00
36 #define SOSSI_INIT1_REG 0x04
37 #define SOSSI_INIT2_REG 0x08
38 #define SOSSI_INIT3_REG 0x0c
39 #define SOSSI_FIFO_REG 0x10
40 #define SOSSI_REOTABLE_REG 0x14
41 #define SOSSI_TEARING_REG 0x18
42 #define SOSSI_INIT1B_REG 0x1c
43 #define SOSSI_FIFOB_REG 0x20
45 #define DMA_GSCR 0xfffedc04
46 #define DMA_LCD_CCR 0xfffee3c2
47 #define DMA_LCD_CTRL 0xfffee3c4
48 #define DMA_LCD_LCH_CTRL 0xfffee3ea
50 #define CONF_SOSSI_RESET_R (1 << 23)
55 #define SOSSI_MAX_XMIT_BYTES (512 * 1024)
66 void (*lcdc_callback)(void *data);
67 void *lcdc_callback_data;
68 int vsync_dma_pending;
69 /* timing for read and write access */
74 * if last_access is the same as current we don't have to change
79 struct omapfb_device *fbdev;
82 static inline u32 sossi_read_reg(int reg)
84 return readl(sossi.base + reg);
87 static inline u16 sossi_read_reg16(int reg)
89 return readw(sossi.base + reg);
92 static inline u8 sossi_read_reg8(int reg)
94 return readb(sossi.base + reg);
97 static inline void sossi_write_reg(int reg, u32 value)
99 writel(value, sossi.base + reg);
102 static inline void sossi_write_reg16(int reg, u16 value)
104 writew(value, sossi.base + reg);
107 static inline void sossi_write_reg8(int reg, u8 value)
109 writeb(value, sossi.base + reg);
112 static void sossi_set_bits(int reg, u32 bits)
114 sossi_write_reg(reg, sossi_read_reg(reg) | bits);
117 static void sossi_clear_bits(int reg, u32 bits)
119 sossi_write_reg(reg, sossi_read_reg(reg) & ~bits);
122 #define HZ_TO_PS(x) (1000000000 / (x / 1000))
124 static u32 ps_to_sossi_ticks(u32 ps, int div)
126 u32 clk_period = HZ_TO_PS(sossi.fck_hz) * div;
127 return (clk_period + ps - 1) / clk_period;
130 static int calc_rd_timings(struct extif_timings *t)
133 int reon, reoff, recyc, actim;
134 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);
169 * access time (data hold time) will be exactly one sossi
172 if (actim - reoff > 1)
181 static int calc_wr_timings(struct extif_timings *t)
184 int weon, weoff, wecyc;
185 int div = t->clk_div;
188 * Make sure that after conversion it still holds that:
189 * weoff > weon, wecyc >= weoff
191 weon = ps_to_sossi_ticks(t->we_on_time, div);
192 /* weon will be exactly one sossi tick */
196 weoff = ps_to_sossi_ticks(t->we_off_time, div);
203 wecyc = ps_to_sossi_ticks(t->we_cycle_time, div);
208 /* values less then 3 result in the SOSSI block resetting itself */
220 static void _set_timing(int div, int tw0, int tw1)
225 dev_dbg(sossi.fbdev->dev, "Using TW0 = %d, TW1 = %d, div = %d\n",
226 tw0 + 1, tw1 + 1, div);
229 clk_set_rate(sossi.fck, sossi.fck_hz / div);
230 clk_enable(sossi.fck);
231 l = sossi_read_reg(SOSSI_INIT1_REG);
232 l &= ~((0x0f << 20) | (0x3f << 24));
233 l |= (tw0 << 20) | (tw1 << 24);
234 sossi_write_reg(SOSSI_INIT1_REG, l);
235 clk_disable(sossi.fck);
238 static void _set_bits_per_cycle(int bus_pick_count, int bus_pick_width)
242 l = sossi_read_reg(SOSSI_INIT3_REG);
244 l |= ((bus_pick_count - 1) << 5) | ((bus_pick_width - 1) & 0x1f);
245 sossi_write_reg(SOSSI_INIT3_REG, l);
248 static void _set_tearsync_mode(int mode, unsigned line)
252 l = sossi_read_reg(SOSSI_TEARING_REG);
253 l &= ~(((1 << 11) - 1) << 15);
257 sossi_write_reg(SOSSI_TEARING_REG, l);
259 sossi_set_bits(SOSSI_INIT2_REG, 1 << 6); /* TE logic */
261 sossi_clear_bits(SOSSI_INIT2_REG, 1 << 6);
264 static inline void set_timing(int access)
266 if (access != sossi.last_access) {
267 sossi.last_access = access;
268 _set_timing(sossi.clk_div,
269 sossi.clk_tw0[access], sossi.clk_tw1[access]);
273 static void sossi_start_transfer(void)
276 sossi_clear_bits(SOSSI_INIT2_REG, 1 << 4);
278 sossi_clear_bits(SOSSI_INIT1_REG, 1 << 30);
281 static void sossi_stop_transfer(void)
284 sossi_set_bits(SOSSI_INIT2_REG, 1 << 4);
286 sossi_set_bits(SOSSI_INIT1_REG, 1 << 30);
289 static void wait_end_of_write(void)
291 /* Before reading we must check if some writings are going on */
292 while (!(sossi_read_reg(SOSSI_INIT2_REG) & (1 << 3)));
295 static void send_data(const void *data, unsigned int len)
298 sossi_write_reg(SOSSI_FIFO_REG, *(const u32 *) data);
303 sossi_write_reg16(SOSSI_FIFO_REG, *(const u16 *) data);
308 sossi_write_reg8(SOSSI_FIFO_REG, *(const u8 *) data);
314 static void set_cycles(unsigned int len)
316 unsigned long nr_cycles = len / (sossi.bus_pick_width / 8);
318 BUG_ON((nr_cycles - 1) & ~0x3ffff);
320 sossi_clear_bits(SOSSI_INIT1_REG, 0x3ffff);
321 sossi_set_bits(SOSSI_INIT1_REG, (nr_cycles - 1) & 0x3ffff);
324 static int sossi_convert_timings(struct extif_timings *t)
327 int div = t->clk_div;
331 if (div <= 0 || div > 8)
334 /* no CS on SOSSI, so ignore cson, csoff, cs_pulsewidth */
335 if ((r = calc_rd_timings(t)) < 0)
338 if ((r = calc_wr_timings(t)) < 0)
348 static void sossi_set_timings(const struct extif_timings *t)
350 BUG_ON(!t->converted);
352 sossi.clk_tw0[RD_ACCESS] = t->tim[0];
353 sossi.clk_tw1[RD_ACCESS] = t->tim[1];
355 sossi.clk_tw0[WR_ACCESS] = t->tim[2];
356 sossi.clk_tw1[WR_ACCESS] = t->tim[3];
358 sossi.clk_div = t->tim[4];
361 static void sossi_get_clk_info(u32 *clk_period, u32 *max_clk_div)
363 *clk_period = HZ_TO_PS(sossi.fck_hz);
367 static void sossi_set_bits_per_cycle(int bpc)
369 int bus_pick_count, bus_pick_width;
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).
389 sossi.bus_pick_width = bus_pick_width;
390 sossi.bus_pick_count = bus_pick_count;
393 static int sossi_setup_tearsync(unsigned pin_cnt,
394 unsigned hs_pulse_time, unsigned vs_pulse_time,
395 int hs_pol_inv, int vs_pol_inv, int div)
400 if (pin_cnt != 1 || div < 1 || div > 8)
403 hs = ps_to_sossi_ticks(hs_pulse_time, div);
404 vs = ps_to_sossi_ticks(vs_pulse_time, div);
405 if (vs < 8 || vs <= hs || vs >= (1 << 12))
414 dev_dbg(sossi.fbdev->dev,
415 "setup_tearsync: hs %d vs %d hs_inv %d vs_inv %d\n",
416 hs, vs, hs_pol_inv, vs_pol_inv);
418 clk_enable(sossi.fck);
419 l = sossi_read_reg(SOSSI_TEARING_REG);
420 l &= ~((1 << 15) - 1);
431 sossi_write_reg(SOSSI_TEARING_REG, l);
432 clk_disable(sossi.fck);
437 static int sossi_enable_tearsync(int enable, unsigned line)
441 dev_dbg(sossi.fbdev->dev, "tearsync %d line %d\n", enable, line);
446 mode = 2; /* HS or VS */
448 mode = 3; /* VS only */
451 sossi.tearsync_line = line;
452 sossi.tearsync_mode = mode;
457 static void sossi_write_command(const void *data, unsigned int len)
459 clk_enable(sossi.fck);
460 set_timing(WR_ACCESS);
461 _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
463 sossi_clear_bits(SOSSI_INIT1_REG, 1 << 18);
465 sossi_start_transfer();
466 send_data(data, len);
467 sossi_stop_transfer();
469 clk_disable(sossi.fck);
472 static void sossi_write_data(const void *data, unsigned int len)
474 clk_enable(sossi.fck);
475 set_timing(WR_ACCESS);
476 _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
478 sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
480 sossi_start_transfer();
481 send_data(data, len);
482 sossi_stop_transfer();
484 clk_disable(sossi.fck);
487 static void sossi_transfer_area(int width, int height,
488 void (callback)(void *data), void *data)
490 BUG_ON(callback == NULL);
492 sossi.lcdc_callback = callback;
493 sossi.lcdc_callback_data = data;
495 clk_enable(sossi.fck);
496 set_timing(WR_ACCESS);
497 _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
498 _set_tearsync_mode(sossi.tearsync_mode, sossi.tearsync_line);
500 sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
501 set_cycles(width * height * sossi.bus_pick_width / 8);
503 sossi_start_transfer();
504 if (sossi.tearsync_mode) {
506 * Wait for the sync signal and start the transfer only
507 * then. We can't seem to be able to use HW sync DMA for
508 * this since LCD DMA shows huge latencies, as if it
509 * would ignore some of the DMA requests from SoSSI.
513 spin_lock_irqsave(&sossi.lock, flags);
514 sossi.vsync_dma_pending++;
515 spin_unlock_irqrestore(&sossi.lock, flags);
517 /* Just start the transfer right away. */
518 omap_enable_lcd_dma();
521 static void sossi_dma_callback(void *data)
524 sossi_stop_transfer();
525 clk_disable(sossi.fck);
526 sossi.lcdc_callback(sossi.lcdc_callback_data);
529 static void sossi_read_data(void *data, unsigned int len)
531 clk_enable(sossi.fck);
532 set_timing(RD_ACCESS);
533 _set_bits_per_cycle(sossi.bus_pick_count, sossi.bus_pick_width);
535 sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
537 sossi_start_transfer();
539 *(u32 *) data = sossi_read_reg(SOSSI_FIFO_REG);
544 *(u16 *) data = sossi_read_reg16(SOSSI_FIFO_REG);
549 *(u8 *) data = sossi_read_reg8(SOSSI_FIFO_REG);
553 sossi_stop_transfer();
554 clk_disable(sossi.fck);
557 static irqreturn_t sossi_match_irq(int irq, void *data)
561 spin_lock_irqsave(&sossi.lock, flags);
562 if (sossi.vsync_dma_pending) {
563 sossi.vsync_dma_pending--;
564 omap_enable_lcd_dma();
566 spin_unlock_irqrestore(&sossi.lock, flags);
570 static int sossi_init(struct omapfb_device *fbdev)
574 struct clk *dpll1out_ck;
577 sossi.base = ioremap(OMAP_SOSSI_BASE, SZ_1K);
579 dev_err(fbdev->dev, "can't ioremap SoSSI\n");
584 spin_lock_init(&sossi.lock);
586 dpll1out_ck = clk_get(fbdev->dev, "ck_dpll1out");
587 if (IS_ERR(dpll1out_ck)) {
588 dev_err(fbdev->dev, "can't get DPLL1OUT clock\n");
589 return PTR_ERR(dpll1out_ck);
592 * We need the parent clock rate, which we might divide further
593 * depending on the timing requirements of the controller. See
596 sossi.fck_hz = clk_get_rate(dpll1out_ck);
597 clk_put(dpll1out_ck);
599 fck = clk_get(fbdev->dev, "ck_sossi");
601 dev_err(fbdev->dev, "can't get SoSSI functional clock\n");
606 /* Reset and enable the SoSSI module */
607 l = omap_readl(MOD_CONF_CTRL_1);
608 l |= CONF_SOSSI_RESET_R;
609 omap_writel(l, MOD_CONF_CTRL_1);
610 l &= ~CONF_SOSSI_RESET_R;
611 omap_writel(l, MOD_CONF_CTRL_1);
613 clk_enable(sossi.fck);
614 l = omap_readl(ARM_IDLECT2);
615 l &= ~(1 << 8); /* DMACK_REQ */
616 omap_writel(l, ARM_IDLECT2);
618 l = sossi_read_reg(SOSSI_INIT2_REG);
619 /* Enable and reset the SoSSI block */
620 l |= (1 << 0) | (1 << 1);
621 sossi_write_reg(SOSSI_INIT2_REG, l);
622 /* Take SoSSI out of reset */
624 sossi_write_reg(SOSSI_INIT2_REG, l);
626 sossi_write_reg(SOSSI_ID_REG, 0);
627 l = sossi_read_reg(SOSSI_ID_REG);
628 k = sossi_read_reg(SOSSI_ID_REG);
630 if (l != 0x55555555 || k != 0xaaaaaaaa) {
632 "invalid SoSSI sync pattern: %08x, %08x\n", l, k);
637 if ((r = omap_lcdc_set_dma_callback(sossi_dma_callback, NULL)) < 0) {
638 dev_err(fbdev->dev, "can't get LCDC IRQ\n");
643 l = sossi_read_reg(SOSSI_ID_REG); /* Component code */
644 l = sossi_read_reg(SOSSI_ID_REG);
645 dev_info(fbdev->dev, "SoSSI version %d.%d initialized\n",
646 l >> 16, l & 0xffff);
648 l = sossi_read_reg(SOSSI_INIT1_REG);
649 l |= (1 << 19); /* DMA_MODE */
650 l &= ~(1 << 31); /* REORDERING */
651 sossi_write_reg(SOSSI_INIT1_REG, l);
653 if ((r = request_irq(INT_1610_SoSSI_MATCH, sossi_match_irq,
654 IRQ_TYPE_EDGE_FALLING,
655 "sossi_match", sossi.fbdev->dev)) < 0) {
656 dev_err(sossi.fbdev->dev, "can't get SoSSI match IRQ\n");
660 clk_disable(sossi.fck);
664 clk_disable(sossi.fck);
669 static void sossi_cleanup(void)
671 omap_lcdc_free_dma_callback();
676 struct lcd_ctrl_extif omap1_ext_if = {
678 .cleanup = sossi_cleanup,
679 .get_clk_info = sossi_get_clk_info,
680 .convert_timings = sossi_convert_timings,
681 .set_timings = sossi_set_timings,
682 .set_bits_per_cycle = sossi_set_bits_per_cycle,
683 .setup_tearsync = sossi_setup_tearsync,
684 .enable_tearsync = sossi_enable_tearsync,
685 .write_command = sossi_write_command,
686 .read_data = sossi_read_data,
687 .write_data = sossi_write_data,
688 .transfer_area = sossi_transfer_area,
690 .max_transmit_size = SOSSI_MAX_XMIT_BYTES,