1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OMAP1 Special OptimiSed Screen Interface support
5 * Copyright (C) 2004-2005 Nokia Corporation
6 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
8 #include <linux/module.h>
10 #include <linux/clk.h>
11 #include <linux/irq.h>
13 #include <linux/interrupt.h>
15 #include <linux/omap-dma.h>
16 #include <linux/soc/ti/omap1-io.h>
22 #define MODULE_NAME "omapfb-sossi"
24 #define OMAP_SOSSI_BASE 0xfffbac00
25 #define SOSSI_ID_REG 0x00
26 #define SOSSI_INIT1_REG 0x04
27 #define SOSSI_INIT2_REG 0x08
28 #define SOSSI_INIT3_REG 0x0c
29 #define SOSSI_FIFO_REG 0x10
30 #define SOSSI_REOTABLE_REG 0x14
31 #define SOSSI_TEARING_REG 0x18
32 #define SOSSI_INIT1B_REG 0x1c
33 #define SOSSI_FIFOB_REG 0x20
35 #define DMA_GSCR 0xfffedc04
36 #define DMA_LCD_CCR 0xfffee3c2
37 #define DMA_LCD_CTRL 0xfffee3c4
38 #define DMA_LCD_LCH_CTRL 0xfffee3ea
40 #define CONF_SOSSI_RESET_R (1 << 23)
45 #define SOSSI_MAX_XMIT_BYTES (512 * 1024)
56 void (*lcdc_callback
)(void *data
);
57 void *lcdc_callback_data
;
58 int vsync_dma_pending
;
59 /* timing for read and write access */
64 * if last_access is the same as current we don't have to change
69 struct omapfb_device
*fbdev
;
72 static inline u32
sossi_read_reg(int reg
)
74 return readl(sossi
.base
+ reg
);
77 static inline u16
sossi_read_reg16(int reg
)
79 return readw(sossi
.base
+ reg
);
82 static inline u8
sossi_read_reg8(int reg
)
84 return readb(sossi
.base
+ reg
);
87 static inline void sossi_write_reg(int reg
, u32 value
)
89 writel(value
, sossi
.base
+ reg
);
92 static inline void sossi_write_reg16(int reg
, u16 value
)
94 writew(value
, sossi
.base
+ reg
);
97 static inline void sossi_write_reg8(int reg
, u8 value
)
99 writeb(value
, sossi
.base
+ reg
);
102 static void sossi_set_bits(int reg
, u32 bits
)
104 sossi_write_reg(reg
, sossi_read_reg(reg
) | bits
);
107 static void sossi_clear_bits(int reg
, u32 bits
)
109 sossi_write_reg(reg
, sossi_read_reg(reg
) & ~bits
);
112 #define HZ_TO_PS(x) (1000000000 / (x / 1000))
114 static u32
ps_to_sossi_ticks(u32 ps
, int div
)
116 u32 clk_period
= HZ_TO_PS(sossi
.fck_hz
) * div
;
117 return (clk_period
+ ps
- 1) / clk_period
;
120 static int calc_rd_timings(struct extif_timings
*t
)
123 int reon
, reoff
, recyc
, actim
;
124 int div
= t
->clk_div
;
127 * Make sure that after conversion it still holds that:
128 * reoff > reon, recyc >= reoff, actim > reon
130 reon
= ps_to_sossi_ticks(t
->re_on_time
, div
);
131 /* reon will be exactly one sossi tick */
135 reoff
= ps_to_sossi_ticks(t
->re_off_time
, div
);
144 recyc
= ps_to_sossi_ticks(t
->re_cycle_time
, div
);
149 /* values less then 3 result in the SOSSI block resetting itself */
155 actim
= ps_to_sossi_ticks(t
->access_time
, div
);
159 * access time (data hold time) will be exactly one sossi
162 if (actim
- reoff
> 1)
171 static int calc_wr_timings(struct extif_timings
*t
)
174 int weon
, weoff
, wecyc
;
175 int div
= t
->clk_div
;
178 * Make sure that after conversion it still holds that:
179 * weoff > weon, wecyc >= weoff
181 weon
= ps_to_sossi_ticks(t
->we_on_time
, div
);
182 /* weon will be exactly one sossi tick */
186 weoff
= ps_to_sossi_ticks(t
->we_off_time
, div
);
193 wecyc
= ps_to_sossi_ticks(t
->we_cycle_time
, div
);
198 /* values less then 3 result in the SOSSI block resetting itself */
210 static void _set_timing(int div
, int tw0
, int tw1
)
215 dev_dbg(sossi
.fbdev
->dev
, "Using TW0 = %d, TW1 = %d, div = %d\n",
216 tw0
+ 1, tw1
+ 1, div
);
219 clk_set_rate(sossi
.fck
, sossi
.fck_hz
/ div
);
220 clk_enable(sossi
.fck
);
221 l
= sossi_read_reg(SOSSI_INIT1_REG
);
222 l
&= ~((0x0f << 20) | (0x3f << 24));
223 l
|= (tw0
<< 20) | (tw1
<< 24);
224 sossi_write_reg(SOSSI_INIT1_REG
, l
);
225 clk_disable(sossi
.fck
);
228 static void _set_bits_per_cycle(int bus_pick_count
, int bus_pick_width
)
232 l
= sossi_read_reg(SOSSI_INIT3_REG
);
234 l
|= ((bus_pick_count
- 1) << 5) | ((bus_pick_width
- 1) & 0x1f);
235 sossi_write_reg(SOSSI_INIT3_REG
, l
);
238 static void _set_tearsync_mode(int mode
, unsigned line
)
242 l
= sossi_read_reg(SOSSI_TEARING_REG
);
243 l
&= ~(((1 << 11) - 1) << 15);
247 sossi_write_reg(SOSSI_TEARING_REG
, l
);
249 sossi_set_bits(SOSSI_INIT2_REG
, 1 << 6); /* TE logic */
251 sossi_clear_bits(SOSSI_INIT2_REG
, 1 << 6);
254 static inline void set_timing(int access
)
256 if (access
!= sossi
.last_access
) {
257 sossi
.last_access
= access
;
258 _set_timing(sossi
.clk_div
,
259 sossi
.clk_tw0
[access
], sossi
.clk_tw1
[access
]);
263 static void sossi_start_transfer(void)
266 sossi_clear_bits(SOSSI_INIT2_REG
, 1 << 4);
268 sossi_clear_bits(SOSSI_INIT1_REG
, 1 << 30);
271 static void sossi_stop_transfer(void)
274 sossi_set_bits(SOSSI_INIT2_REG
, 1 << 4);
276 sossi_set_bits(SOSSI_INIT1_REG
, 1 << 30);
279 static void wait_end_of_write(void)
281 /* Before reading we must check if some writings are going on */
282 while (!(sossi_read_reg(SOSSI_INIT2_REG
) & (1 << 3)));
285 static void send_data(const void *data
, unsigned int len
)
288 sossi_write_reg(SOSSI_FIFO_REG
, *(const u32
*) data
);
293 sossi_write_reg16(SOSSI_FIFO_REG
, *(const u16
*) data
);
298 sossi_write_reg8(SOSSI_FIFO_REG
, *(const u8
*) data
);
304 static void set_cycles(unsigned int len
)
306 unsigned long nr_cycles
= len
/ (sossi
.bus_pick_width
/ 8);
308 BUG_ON((nr_cycles
- 1) & ~0x3ffff);
310 sossi_clear_bits(SOSSI_INIT1_REG
, 0x3ffff);
311 sossi_set_bits(SOSSI_INIT1_REG
, (nr_cycles
- 1) & 0x3ffff);
314 static int sossi_convert_timings(struct extif_timings
*t
)
317 int div
= t
->clk_div
;
321 if (div
<= 0 || div
> 8)
324 /* no CS on SOSSI, so ignore cson, csoff, cs_pulsewidth */
325 if ((r
= calc_rd_timings(t
)) < 0)
328 if ((r
= calc_wr_timings(t
)) < 0)
338 static void sossi_set_timings(const struct extif_timings
*t
)
340 BUG_ON(!t
->converted
);
342 sossi
.clk_tw0
[RD_ACCESS
] = t
->tim
[0];
343 sossi
.clk_tw1
[RD_ACCESS
] = t
->tim
[1];
345 sossi
.clk_tw0
[WR_ACCESS
] = t
->tim
[2];
346 sossi
.clk_tw1
[WR_ACCESS
] = t
->tim
[3];
348 sossi
.clk_div
= t
->tim
[4];
351 static void sossi_get_clk_info(u32
*clk_period
, u32
*max_clk_div
)
353 *clk_period
= HZ_TO_PS(sossi
.fck_hz
);
357 static void sossi_set_bits_per_cycle(int bpc
)
359 int bus_pick_count
, bus_pick_width
;
362 * We set explicitly the bus_pick_count as well, although
363 * with remapping/reordering disabled it will be calculated by HW
364 * as (32 / bus_pick_width).
379 sossi
.bus_pick_width
= bus_pick_width
;
380 sossi
.bus_pick_count
= bus_pick_count
;
383 static int sossi_setup_tearsync(unsigned pin_cnt
,
384 unsigned hs_pulse_time
, unsigned vs_pulse_time
,
385 int hs_pol_inv
, int vs_pol_inv
, int div
)
390 if (pin_cnt
!= 1 || div
< 1 || div
> 8)
393 hs
= ps_to_sossi_ticks(hs_pulse_time
, div
);
394 vs
= ps_to_sossi_ticks(vs_pulse_time
, div
);
395 if (vs
< 8 || vs
<= hs
|| vs
>= (1 << 12))
404 dev_dbg(sossi
.fbdev
->dev
,
405 "setup_tearsync: hs %d vs %d hs_inv %d vs_inv %d\n",
406 hs
, vs
, hs_pol_inv
, vs_pol_inv
);
408 clk_enable(sossi
.fck
);
409 l
= sossi_read_reg(SOSSI_TEARING_REG
);
410 l
&= ~((1 << 15) - 1);
421 sossi_write_reg(SOSSI_TEARING_REG
, l
);
422 clk_disable(sossi
.fck
);
427 static int sossi_enable_tearsync(int enable
, unsigned line
)
431 dev_dbg(sossi
.fbdev
->dev
, "tearsync %d line %d\n", enable
, line
);
436 mode
= 2; /* HS or VS */
438 mode
= 3; /* VS only */
441 sossi
.tearsync_line
= line
;
442 sossi
.tearsync_mode
= mode
;
447 static void sossi_write_command(const void *data
, unsigned int len
)
449 clk_enable(sossi
.fck
);
450 set_timing(WR_ACCESS
);
451 _set_bits_per_cycle(sossi
.bus_pick_count
, sossi
.bus_pick_width
);
453 sossi_clear_bits(SOSSI_INIT1_REG
, 1 << 18);
455 sossi_start_transfer();
456 send_data(data
, len
);
457 sossi_stop_transfer();
459 clk_disable(sossi
.fck
);
462 static void sossi_write_data(const void *data
, unsigned int len
)
464 clk_enable(sossi
.fck
);
465 set_timing(WR_ACCESS
);
466 _set_bits_per_cycle(sossi
.bus_pick_count
, sossi
.bus_pick_width
);
468 sossi_set_bits(SOSSI_INIT1_REG
, 1 << 18);
470 sossi_start_transfer();
471 send_data(data
, len
);
472 sossi_stop_transfer();
474 clk_disable(sossi
.fck
);
477 static void sossi_transfer_area(int width
, int height
,
478 void (callback
)(void *data
), void *data
)
480 BUG_ON(callback
== NULL
);
482 sossi
.lcdc_callback
= callback
;
483 sossi
.lcdc_callback_data
= data
;
485 clk_enable(sossi
.fck
);
486 set_timing(WR_ACCESS
);
487 _set_bits_per_cycle(sossi
.bus_pick_count
, sossi
.bus_pick_width
);
488 _set_tearsync_mode(sossi
.tearsync_mode
, sossi
.tearsync_line
);
490 sossi_set_bits(SOSSI_INIT1_REG
, 1 << 18);
491 set_cycles(width
* height
* sossi
.bus_pick_width
/ 8);
493 sossi_start_transfer();
494 if (sossi
.tearsync_mode
) {
496 * Wait for the sync signal and start the transfer only
497 * then. We can't seem to be able to use HW sync DMA for
498 * this since LCD DMA shows huge latencies, as if it
499 * would ignore some of the DMA requests from SoSSI.
503 spin_lock_irqsave(&sossi
.lock
, flags
);
504 sossi
.vsync_dma_pending
++;
505 spin_unlock_irqrestore(&sossi
.lock
, flags
);
507 /* Just start the transfer right away. */
508 omap_enable_lcd_dma();
511 static void sossi_dma_callback(void *data
)
514 sossi_stop_transfer();
515 clk_disable(sossi
.fck
);
516 sossi
.lcdc_callback(sossi
.lcdc_callback_data
);
519 static void sossi_read_data(void *data
, unsigned int len
)
521 clk_enable(sossi
.fck
);
522 set_timing(RD_ACCESS
);
523 _set_bits_per_cycle(sossi
.bus_pick_count
, sossi
.bus_pick_width
);
525 sossi_set_bits(SOSSI_INIT1_REG
, 1 << 18);
527 sossi_start_transfer();
529 *(u32
*) data
= sossi_read_reg(SOSSI_FIFO_REG
);
534 *(u16
*) data
= sossi_read_reg16(SOSSI_FIFO_REG
);
539 *(u8
*) data
= sossi_read_reg8(SOSSI_FIFO_REG
);
543 sossi_stop_transfer();
544 clk_disable(sossi
.fck
);
547 static irqreturn_t
sossi_match_irq(int irq
, void *data
)
551 spin_lock_irqsave(&sossi
.lock
, flags
);
552 if (sossi
.vsync_dma_pending
) {
553 sossi
.vsync_dma_pending
--;
554 omap_enable_lcd_dma();
556 spin_unlock_irqrestore(&sossi
.lock
, flags
);
560 static int sossi_init(struct omapfb_device
*fbdev
)
564 struct clk
*dpll1out_ck
;
567 sossi
.base
= ioremap(OMAP_SOSSI_BASE
, SZ_1K
);
569 dev_err(fbdev
->dev
, "can't ioremap SoSSI\n");
574 spin_lock_init(&sossi
.lock
);
576 dpll1out_ck
= clk_get(fbdev
->dev
, "ck_dpll1out");
577 if (IS_ERR(dpll1out_ck
)) {
578 dev_err(fbdev
->dev
, "can't get DPLL1OUT clock\n");
579 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_prepare_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(fbdev
->ext_irq
, sossi_match_irq
,
644 IRQ_TYPE_EDGE_FALLING
,
645 "sossi_match", sossi
.fbdev
->dev
)) < 0) {
646 dev_err(sossi
.fbdev
->dev
, "can't get SoSSI match IRQ\n");
650 clk_disable(sossi
.fck
);
654 clk_disable_unprepare(sossi
.fck
);
659 static void sossi_cleanup(void)
661 omap_lcdc_free_dma_callback();
662 clk_unprepare(sossi
.fck
);
667 struct lcd_ctrl_extif omap1_ext_if
= {
669 .cleanup
= sossi_cleanup
,
670 .get_clk_info
= sossi_get_clk_info
,
671 .convert_timings
= sossi_convert_timings
,
672 .set_timings
= sossi_set_timings
,
673 .set_bits_per_cycle
= sossi_set_bits_per_cycle
,
674 .setup_tearsync
= sossi_setup_tearsync
,
675 .enable_tearsync
= sossi_enable_tearsync
,
676 .write_command
= sossi_write_command
,
677 .read_data
= sossi_read_data
,
678 .write_data
= sossi_write_data
,
679 .transfer_area
= sossi_transfer_area
,
681 .max_transmit_size
= SOSSI_MAX_XMIT_BYTES
,