Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / video / fbdev / omap / sossi.c
blob66aff6cd1df0800d556a2b4c1ac12a944873e86c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * OMAP1 Special OptimiSed Screen Interface support
5 * Copyright (C) 2004-2005 Nokia Corporation
6 * Author: Juha Yrjölä <juha.yrjola@nokia.com>
7 */
8 #include <linux/module.h>
9 #include <linux/mm.h>
10 #include <linux/clk.h>
11 #include <linux/irq.h>
12 #include <linux/io.h>
13 #include <linux/interrupt.h>
15 #include <linux/omap-dma.h>
16 #include <linux/soc/ti/omap1-io.h>
18 #include "omapfb.h"
19 #include "lcd_dma.h"
20 #include "lcdc.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)
42 #define RD_ACCESS 0
43 #define WR_ACCESS 1
45 #define SOSSI_MAX_XMIT_BYTES (512 * 1024)
47 static struct {
48 void __iomem *base;
49 struct clk *fck;
50 unsigned long fck_hz;
51 spinlock_t lock;
52 int bus_pick_count;
53 int bus_pick_width;
54 int tearsync_mode;
55 int tearsync_line;
56 void (*lcdc_callback)(void *data);
57 void *lcdc_callback_data;
58 int vsync_dma_pending;
59 /* timing for read and write access */
60 int clk_div;
61 u8 clk_tw0[2];
62 u8 clk_tw1[2];
64 * if last_access is the same as current we don't have to change
65 * the timings
67 int last_access;
69 struct omapfb_device *fbdev;
70 } sossi;
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)
122 u32 tw0, tw1;
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 */
132 if (reon > 1)
133 return -1;
135 reoff = ps_to_sossi_ticks(t->re_off_time, div);
137 if (reoff <= reon)
138 reoff = reon + 1;
140 tw0 = reoff - reon;
141 if (tw0 > 0x10)
142 return -1;
144 recyc = ps_to_sossi_ticks(t->re_cycle_time, div);
145 if (recyc <= reoff)
146 recyc = reoff + 1;
148 tw1 = recyc - tw0;
149 /* values less then 3 result in the SOSSI block resetting itself */
150 if (tw1 < 3)
151 tw1 = 3;
152 if (tw1 > 0x40)
153 return -1;
155 actim = ps_to_sossi_ticks(t->access_time, div);
156 if (actim < reoff)
157 actim++;
159 * access time (data hold time) will be exactly one sossi
160 * tick
162 if (actim - reoff > 1)
163 return -1;
165 t->tim[0] = tw0 - 1;
166 t->tim[1] = tw1 - 1;
168 return 0;
171 static int calc_wr_timings(struct extif_timings *t)
173 u32 tw0, tw1;
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 */
183 if (weon > 1)
184 return -1;
186 weoff = ps_to_sossi_ticks(t->we_off_time, div);
187 if (weoff <= weon)
188 weoff = weon + 1;
189 tw0 = weoff - weon;
190 if (tw0 > 0x10)
191 return -1;
193 wecyc = ps_to_sossi_ticks(t->we_cycle_time, div);
194 if (wecyc <= weoff)
195 wecyc = weoff + 1;
197 tw1 = wecyc - tw0;
198 /* values less then 3 result in the SOSSI block resetting itself */
199 if (tw1 < 3)
200 tw1 = 3;
201 if (tw1 > 0x40)
202 return -1;
204 t->tim[2] = tw0 - 1;
205 t->tim[3] = tw1 - 1;
207 return 0;
210 static void _set_timing(int div, int tw0, int tw1)
212 u32 l;
214 #ifdef VERBOSE
215 dev_dbg(sossi.fbdev->dev, "Using TW0 = %d, TW1 = %d, div = %d\n",
216 tw0 + 1, tw1 + 1, div);
217 #endif
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)
230 u32 l;
232 l = sossi_read_reg(SOSSI_INIT3_REG);
233 l &= ~0x3ff;
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)
240 u32 l;
242 l = sossi_read_reg(SOSSI_TEARING_REG);
243 l &= ~(((1 << 11) - 1) << 15);
244 l |= line << 15;
245 l &= ~(0x3 << 26);
246 l |= mode << 26;
247 sossi_write_reg(SOSSI_TEARING_REG, l);
248 if (mode)
249 sossi_set_bits(SOSSI_INIT2_REG, 1 << 6); /* TE logic */
250 else
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)
265 /* WE */
266 sossi_clear_bits(SOSSI_INIT2_REG, 1 << 4);
267 /* CS active low */
268 sossi_clear_bits(SOSSI_INIT1_REG, 1 << 30);
271 static void sossi_stop_transfer(void)
273 /* WE */
274 sossi_set_bits(SOSSI_INIT2_REG, 1 << 4);
275 /* CS active low */
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)
287 while (len >= 4) {
288 sossi_write_reg(SOSSI_FIFO_REG, *(const u32 *) data);
289 len -= 4;
290 data += 4;
292 while (len >= 2) {
293 sossi_write_reg16(SOSSI_FIFO_REG, *(const u16 *) data);
294 len -= 2;
295 data += 2;
297 while (len) {
298 sossi_write_reg8(SOSSI_FIFO_REG, *(const u8 *) data);
299 len--;
300 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)
316 int r = 0;
317 int div = t->clk_div;
319 t->converted = 0;
321 if (div <= 0 || div > 8)
322 return -1;
324 /* no CS on SOSSI, so ignore cson, csoff, cs_pulsewidth */
325 if ((r = calc_rd_timings(t)) < 0)
326 return r;
328 if ((r = calc_wr_timings(t)) < 0)
329 return r;
331 t->tim[4] = div;
333 t->converted = 1;
335 return 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);
354 *max_clk_div = 8;
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).
366 switch (bpc) {
367 case 8:
368 bus_pick_count = 4;
369 bus_pick_width = 8;
370 break;
371 case 16:
372 bus_pick_count = 2;
373 bus_pick_width = 16;
374 break;
375 default:
376 BUG();
377 return;
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)
387 int hs, vs;
388 u32 l;
390 if (pin_cnt != 1 || div < 1 || div > 8)
391 return -EINVAL;
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))
396 return -EDOM;
397 vs /= 8;
398 vs--;
399 if (hs > 8)
400 hs = 8;
401 if (hs)
402 hs--;
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);
411 l |= vs << 3;
412 l |= hs;
413 if (hs_pol_inv)
414 l |= 1 << 29;
415 else
416 l &= ~(1 << 29);
417 if (vs_pol_inv)
418 l |= 1 << 28;
419 else
420 l &= ~(1 << 28);
421 sossi_write_reg(SOSSI_TEARING_REG, l);
422 clk_disable(sossi.fck);
424 return 0;
427 static int sossi_enable_tearsync(int enable, unsigned line)
429 int mode;
431 dev_dbg(sossi.fbdev->dev, "tearsync %d line %d\n", enable, line);
432 if (line >= 1 << 11)
433 return -EINVAL;
434 if (enable) {
435 if (line)
436 mode = 2; /* HS or VS */
437 else
438 mode = 3; /* VS only */
439 } else
440 mode = 0;
441 sossi.tearsync_line = line;
442 sossi.tearsync_mode = mode;
444 return 0;
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);
452 /* CMD#/DATA */
453 sossi_clear_bits(SOSSI_INIT1_REG, 1 << 18);
454 set_cycles(len);
455 sossi_start_transfer();
456 send_data(data, len);
457 sossi_stop_transfer();
458 wait_end_of_write();
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);
467 /* CMD#/DATA */
468 sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
469 set_cycles(len);
470 sossi_start_transfer();
471 send_data(data, len);
472 sossi_stop_transfer();
473 wait_end_of_write();
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);
489 /* CMD#/DATA */
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.
501 unsigned long flags;
503 spin_lock_irqsave(&sossi.lock, flags);
504 sossi.vsync_dma_pending++;
505 spin_unlock_irqrestore(&sossi.lock, flags);
506 } else
507 /* Just start the transfer right away. */
508 omap_enable_lcd_dma();
511 static void sossi_dma_callback(void *data)
513 omap_stop_lcd_dma();
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);
524 /* CMD#/DATA */
525 sossi_set_bits(SOSSI_INIT1_REG, 1 << 18);
526 set_cycles(len);
527 sossi_start_transfer();
528 while (len >= 4) {
529 *(u32 *) data = sossi_read_reg(SOSSI_FIFO_REG);
530 len -= 4;
531 data += 4;
533 while (len >= 2) {
534 *(u16 *) data = sossi_read_reg16(SOSSI_FIFO_REG);
535 len -= 2;
536 data += 2;
538 while (len) {
539 *(u8 *) data = sossi_read_reg8(SOSSI_FIFO_REG);
540 len--;
541 data++;
543 sossi_stop_transfer();
544 clk_disable(sossi.fck);
547 static irqreturn_t sossi_match_irq(int irq, void *data)
549 unsigned long flags;
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);
557 return IRQ_HANDLED;
560 static int sossi_init(struct omapfb_device *fbdev)
562 u32 l, k;
563 struct clk *fck;
564 struct clk *dpll1out_ck;
565 int r;
567 sossi.base = ioremap(OMAP_SOSSI_BASE, SZ_1K);
568 if (!sossi.base) {
569 dev_err(fbdev->dev, "can't ioremap SoSSI\n");
570 return -ENOMEM;
573 sossi.fbdev = fbdev;
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
584 * _set_timings.
586 sossi.fck_hz = clk_get_rate(dpll1out_ck);
587 clk_put(dpll1out_ck);
589 fck = clk_get(fbdev->dev, "ck_sossi");
590 if (IS_ERR(fck)) {
591 dev_err(fbdev->dev, "can't get SoSSI functional clock\n");
592 return PTR_ERR(fck);
594 sossi.fck = fck;
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 */
613 l &= ~(1 << 1);
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) {
621 dev_err(fbdev->dev,
622 "invalid SoSSI sync pattern: %08x, %08x\n", l, k);
623 r = -ENODEV;
624 goto err;
627 if ((r = omap_lcdc_set_dma_callback(sossi_dma_callback, NULL)) < 0) {
628 dev_err(fbdev->dev, "can't get LCDC IRQ\n");
629 r = -ENODEV;
630 goto err;
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");
647 goto err;
650 clk_disable(sossi.fck);
651 return 0;
653 err:
654 clk_disable_unprepare(sossi.fck);
655 clk_put(sossi.fck);
656 return r;
659 static void sossi_cleanup(void)
661 omap_lcdc_free_dma_callback();
662 clk_unprepare(sossi.fck);
663 clk_put(sossi.fck);
664 iounmap(sossi.base);
667 struct lcd_ctrl_extif omap1_ext_if = {
668 .init = sossi_init,
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,