444293136ac5c7165abb43b4d5f62e2c148e3b98
[pandora-kernel.git] / drivers / video / omap2 / dss / dsi.c
1 /*
2  * linux/drivers/video/omap2/dss/dsi.c
3  *
4  * Copyright (C) 2009 Nokia Corporation
5  * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 as published by
9  * the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #define DSS_SUBSYS_NAME "DSI"
21
22 #include <linux/kernel.h>
23 #include <linux/io.h>
24 #include <linux/clk.h>
25 #include <linux/device.h>
26 #include <linux/err.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/workqueue.h>
30 #include <linux/mutex.h>
31 #include <linux/seq_file.h>
32 #include <linux/kfifo.h>
33
34 #include <mach/board.h>
35 #include <mach/display.h>
36 #include <mach/clock.h>
37
38 #include "dss.h"
39
40 /*#define VERBOSE_IRQ*/
41
42 #define DSI_BASE                0x4804FC00
43
44 struct dsi_reg { u16 idx; };
45
46 #define DSI_REG(idx)            ((const struct dsi_reg) { idx })
47
48 #define DSI_SZ_REGS             SZ_1K
49 /* DSI Protocol Engine */
50
51 #define DSI_REVISION                    DSI_REG(0x0000)
52 #define DSI_SYSCONFIG                   DSI_REG(0x0010)
53 #define DSI_SYSSTATUS                   DSI_REG(0x0014)
54 #define DSI_IRQSTATUS                   DSI_REG(0x0018)
55 #define DSI_IRQENABLE                   DSI_REG(0x001C)
56 #define DSI_CTRL                        DSI_REG(0x0040)
57 #define DSI_COMPLEXIO_CFG1              DSI_REG(0x0048)
58 #define DSI_COMPLEXIO_IRQ_STATUS        DSI_REG(0x004C)
59 #define DSI_COMPLEXIO_IRQ_ENABLE        DSI_REG(0x0050)
60 #define DSI_CLK_CTRL                    DSI_REG(0x0054)
61 #define DSI_TIMING1                     DSI_REG(0x0058)
62 #define DSI_TIMING2                     DSI_REG(0x005C)
63 #define DSI_VM_TIMING1                  DSI_REG(0x0060)
64 #define DSI_VM_TIMING2                  DSI_REG(0x0064)
65 #define DSI_VM_TIMING3                  DSI_REG(0x0068)
66 #define DSI_CLK_TIMING                  DSI_REG(0x006C)
67 #define DSI_TX_FIFO_VC_SIZE             DSI_REG(0x0070)
68 #define DSI_RX_FIFO_VC_SIZE             DSI_REG(0x0074)
69 #define DSI_COMPLEXIO_CFG2              DSI_REG(0x0078)
70 #define DSI_RX_FIFO_VC_FULLNESS         DSI_REG(0x007C)
71 #define DSI_VM_TIMING4                  DSI_REG(0x0080)
72 #define DSI_TX_FIFO_VC_EMPTINESS        DSI_REG(0x0084)
73 #define DSI_VM_TIMING5                  DSI_REG(0x0088)
74 #define DSI_VM_TIMING6                  DSI_REG(0x008C)
75 #define DSI_VM_TIMING7                  DSI_REG(0x0090)
76 #define DSI_STOPCLK_TIMING              DSI_REG(0x0094)
77 #define DSI_VC_CTRL(n)                  DSI_REG(0x0100 + (n * 0x20))
78 #define DSI_VC_TE(n)                    DSI_REG(0x0104 + (n * 0x20))
79 #define DSI_VC_LONG_PACKET_HEADER(n)    DSI_REG(0x0108 + (n * 0x20))
80 #define DSI_VC_LONG_PACKET_PAYLOAD(n)   DSI_REG(0x010C + (n * 0x20))
81 #define DSI_VC_SHORT_PACKET_HEADER(n)   DSI_REG(0x0110 + (n * 0x20))
82 #define DSI_VC_IRQSTATUS(n)             DSI_REG(0x0118 + (n * 0x20))
83 #define DSI_VC_IRQENABLE(n)             DSI_REG(0x011C + (n * 0x20))
84
85 /* DSIPHY_SCP */
86
87 #define DSI_DSIPHY_CFG0                 DSI_REG(0x200 + 0x0000)
88 #define DSI_DSIPHY_CFG1                 DSI_REG(0x200 + 0x0004)
89 #define DSI_DSIPHY_CFG2                 DSI_REG(0x200 + 0x0008)
90 #define DSI_DSIPHY_CFG5                 DSI_REG(0x200 + 0x0014)
91
92 /* DSI_PLL_CTRL_SCP */
93
94 #define DSI_PLL_CONTROL                 DSI_REG(0x300 + 0x0000)
95 #define DSI_PLL_STATUS                  DSI_REG(0x300 + 0x0004)
96 #define DSI_PLL_GO                      DSI_REG(0x300 + 0x0008)
97 #define DSI_PLL_CONFIGURATION1          DSI_REG(0x300 + 0x000C)
98 #define DSI_PLL_CONFIGURATION2          DSI_REG(0x300 + 0x0010)
99
100 #define REG_GET(idx, start, end) \
101         FLD_GET(dsi_read_reg(idx), start, end)
102
103 #define REG_FLD_MOD(idx, val, start, end) \
104         dsi_write_reg(idx, FLD_MOD(dsi_read_reg(idx), val, start, end))
105
106 /* Global interrupts */
107 #define DSI_IRQ_VC0             (1 << 0)
108 #define DSI_IRQ_VC1             (1 << 1)
109 #define DSI_IRQ_VC2             (1 << 2)
110 #define DSI_IRQ_VC3             (1 << 3)
111 #define DSI_IRQ_WAKEUP          (1 << 4)
112 #define DSI_IRQ_RESYNC          (1 << 5)
113 #define DSI_IRQ_PLL_LOCK        (1 << 7)
114 #define DSI_IRQ_PLL_UNLOCK      (1 << 8)
115 #define DSI_IRQ_PLL_RECALL      (1 << 9)
116 #define DSI_IRQ_COMPLEXIO_ERR   (1 << 10)
117 #define DSI_IRQ_HS_TX_TIMEOUT   (1 << 14)
118 #define DSI_IRQ_LP_RX_TIMEOUT   (1 << 15)
119 #define DSI_IRQ_TE_TRIGGER      (1 << 16)
120 #define DSI_IRQ_ACK_TRIGGER     (1 << 17)
121 #define DSI_IRQ_SYNC_LOST       (1 << 18)
122 #define DSI_IRQ_LDO_POWER_GOOD  (1 << 19)
123 #define DSI_IRQ_TA_TIMEOUT      (1 << 20)
124 #define DSI_IRQ_ERROR_MASK \
125         (DSI_IRQ_HS_TX_TIMEOUT | DSI_IRQ_LP_RX_TIMEOUT | DSI_IRQ_SYNC_LOST | \
126         DSI_IRQ_TA_TIMEOUT)
127 #define DSI_IRQ_CHANNEL_MASK    0xf
128
129 /* Virtual channel interrupts */
130 #define DSI_VC_IRQ_CS           (1 << 0)
131 #define DSI_VC_IRQ_ECC_CORR     (1 << 1)
132 #define DSI_VC_IRQ_PACKET_SENT  (1 << 2)
133 #define DSI_VC_IRQ_FIFO_TX_OVF  (1 << 3)
134 #define DSI_VC_IRQ_FIFO_RX_OVF  (1 << 4)
135 #define DSI_VC_IRQ_BTA          (1 << 5)
136 #define DSI_VC_IRQ_ECC_NO_CORR  (1 << 6)
137 #define DSI_VC_IRQ_FIFO_TX_UDF  (1 << 7)
138 #define DSI_VC_IRQ_PP_BUSY_CHANGE (1 << 8)
139 #define DSI_VC_IRQ_ERROR_MASK \
140         (DSI_VC_IRQ_CS | DSI_VC_IRQ_ECC_CORR | DSI_VC_IRQ_FIFO_TX_OVF | \
141         DSI_VC_IRQ_FIFO_RX_OVF | DSI_VC_IRQ_ECC_NO_CORR | \
142         DSI_VC_IRQ_FIFO_TX_UDF)
143
144 /* ComplexIO interrupts */
145 #define DSI_CIO_IRQ_ERRSYNCESC1         (1 << 0)
146 #define DSI_CIO_IRQ_ERRSYNCESC2         (1 << 1)
147 #define DSI_CIO_IRQ_ERRSYNCESC3         (1 << 2)
148 #define DSI_CIO_IRQ_ERRESC1             (1 << 5)
149 #define DSI_CIO_IRQ_ERRESC2             (1 << 6)
150 #define DSI_CIO_IRQ_ERRESC3             (1 << 7)
151 #define DSI_CIO_IRQ_ERRCONTROL1         (1 << 10)
152 #define DSI_CIO_IRQ_ERRCONTROL2         (1 << 11)
153 #define DSI_CIO_IRQ_ERRCONTROL3         (1 << 12)
154 #define DSI_CIO_IRQ_STATEULPS1          (1 << 15)
155 #define DSI_CIO_IRQ_STATEULPS2          (1 << 16)
156 #define DSI_CIO_IRQ_STATEULPS3          (1 << 17)
157 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_1  (1 << 20)
158 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_1  (1 << 21)
159 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_2  (1 << 22)
160 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_2  (1 << 23)
161 #define DSI_CIO_IRQ_ERRCONTENTIONLP0_3  (1 << 24)
162 #define DSI_CIO_IRQ_ERRCONTENTIONLP1_3  (1 << 25)
163 #define DSI_CIO_IRQ_ULPSACTIVENOT_ALL0  (1 << 30)
164 #define DSI_CIO_IRQ_ULPSACTIVENOT_ALL1  (1 << 31)
165
166 #define DSI_DT_DCS_SHORT_WRITE_0        0x05
167 #define DSI_DT_DCS_SHORT_WRITE_1        0x15
168 #define DSI_DT_DCS_READ                 0x06
169 #define DSI_DT_SET_MAX_RET_PKG_SIZE     0x37
170 #define DSI_DT_NULL_PACKET              0x09
171 #define DSI_DT_DCS_LONG_WRITE           0x39
172
173 #define DSI_DT_RX_ACK_WITH_ERR          0x02
174 #define DSI_DT_RX_DCS_LONG_READ         0x1c
175 #define DSI_DT_RX_SHORT_READ_1          0x21
176 #define DSI_DT_RX_SHORT_READ_2          0x22
177
178 #define FINT_MAX 2100000
179 #define FINT_MIN 750000
180 #define REGN_MAX (1 << 7)
181 #define REGM_MAX ((1 << 11) - 1)
182 #define REGM3_MAX (1 << 4)
183 #define REGM4_MAX (1 << 4)
184
185 enum fifo_size {
186         DSI_FIFO_SIZE_0         = 0,
187         DSI_FIFO_SIZE_32        = 1,
188         DSI_FIFO_SIZE_64        = 2,
189         DSI_FIFO_SIZE_96        = 3,
190         DSI_FIFO_SIZE_128       = 4,
191 };
192
193 #define DSI_CMD_FIFO_LEN 16
194
195 struct dsi_cmd_update {
196         int bytespp;
197         u16 x;
198         u16 y;
199         u16 w;
200         u16 h;
201 };
202
203 struct dsi_cmd_mem_read {
204         void *buf;
205         size_t size;
206         u16 x;
207         u16 y;
208         u16 w;
209         u16 h;
210         size_t *ret_size;
211         struct completion *completion;
212 };
213
214 struct dsi_cmd_test {
215         int test_num;
216         int *result;
217         struct completion *completion;
218 };
219
220 enum dsi_cmd {
221         DSI_CMD_UPDATE,
222         DSI_CMD_AUTOUPDATE,
223         DSI_CMD_SYNC,
224         DSI_CMD_MEM_READ,
225         DSI_CMD_TEST,
226         DSI_CMD_SET_TE,
227         DSI_CMD_SET_UPDATE_MODE,
228         DSI_CMD_SET_ROTATE,
229         DSI_CMD_SET_MIRROR,
230 };
231
232 struct dsi_cmd_item {
233         struct omap_display *display;
234
235         enum dsi_cmd cmd;
236
237         union {
238                 struct dsi_cmd_update r;
239                 struct completion *sync;
240                 struct dsi_cmd_mem_read mem_read;
241                 struct dsi_cmd_test test;
242                 int te;
243                 enum omap_dss_update_mode update_mode;
244                 int rotate;
245                 int mirror;
246         } u;
247 };
248
249 static struct
250 {
251         void __iomem    *base;
252
253         unsigned long   dsi1_pll_fclk;  /* Hz */
254         unsigned long   dsi2_pll_fclk;  /* Hz */
255         unsigned long   dsiphy;         /* Hz */
256         unsigned long   ddr_clk;        /* Hz */
257
258         struct {
259                 struct omap_display *display;
260                 enum fifo_size fifo_size;
261                 int dest_per;   /* destination peripheral 0-3 */
262         } vc[4];
263
264         struct mutex lock;
265
266         unsigned pll_locked;
267
268         struct completion bta_completion;
269
270         struct work_struct framedone_work;
271         struct work_struct process_work;
272         struct workqueue_struct *workqueue;
273
274         enum omap_dss_update_mode user_update_mode;
275         enum omap_dss_update_mode target_update_mode;
276         enum omap_dss_update_mode update_mode;
277         int use_te;
278         int framedone_scheduled; /* helps to catch strange framedone bugs */
279
280         unsigned long cache_req_pck;
281         unsigned long cache_clk_freq;
282         struct dsi_clock_info cache_cinfo;
283
284         struct kfifo      *cmd_fifo;
285         spinlock_t        cmd_lock;
286         struct completion cmd_done;
287         atomic_t          cmd_fifo_full;
288         atomic_t          cmd_pending;
289
290         bool autoupdate_setup;
291
292 #ifdef DEBUG
293         ktime_t perf_setup_time;
294         ktime_t perf_start_time;
295         int perf_measure_frames;
296
297         struct {
298                 int x, y, w, h;
299                 int bytespp;
300         } update_region;
301
302 #endif
303         int debug_process;
304         int debug_read;
305         int debug_write;
306 } dsi;
307
308 #ifdef DEBUG
309 static unsigned int dsi_perf;
310 module_param_named(dsi_perf, dsi_perf, bool, 0644);
311 #endif
312
313 static void dsi_process_cmd_fifo(struct work_struct *work);
314 static void dsi_push_update(struct omap_display *display,
315                 int x, int y, int w, int h);
316 static void dsi_push_autoupdate(struct omap_display *display);
317
318 static inline void dsi_write_reg(const struct dsi_reg idx, u32 val)
319 {
320         __raw_writel(val, dsi.base + idx.idx);
321 }
322
323 static inline u32 dsi_read_reg(const struct dsi_reg idx)
324 {
325         return __raw_readl(dsi.base + idx.idx);
326 }
327
328
329 void dsi_save_context(void)
330 {
331 }
332
333 void dsi_restore_context(void)
334 {
335 }
336
337 static inline int wait_for_bit_change(const struct dsi_reg idx, int bitnum,
338                 int value)
339 {
340         int t = 100000;
341
342         while (REG_GET(idx, bitnum, bitnum) != value) {
343                 if (--t == 0)
344                         return !value;
345         }
346
347         return value;
348 }
349
350 #ifdef DEBUG
351 static void perf_mark_setup(void)
352 {
353         dsi.perf_setup_time = ktime_get();
354 }
355
356 static void perf_mark_start(void)
357 {
358         dsi.perf_start_time = ktime_get();
359 }
360
361 static void perf_show(const char *name)
362 {
363         ktime_t t, setup_time, trans_time;
364         u32 total_bytes;
365         u32 setup_us, trans_us, total_us;
366         const int numframes = 100;
367         static u32 s_trans_us, s_min_us = 0xffffffff, s_max_us;
368
369         if (!dsi_perf)
370                 return;
371
372         if (dsi.update_mode == OMAP_DSS_UPDATE_DISABLED)
373                 return;
374
375         t = ktime_get();
376
377         setup_time = ktime_sub(dsi.perf_start_time, dsi.perf_setup_time);
378         setup_us = (u32)ktime_to_us(setup_time);
379         if (setup_us == 0)
380                 setup_us = 1;
381
382         trans_time = ktime_sub(t, dsi.perf_start_time);
383         trans_us = (u32)ktime_to_us(trans_time);
384         if (trans_us == 0)
385                 trans_us = 1;
386
387         total_us = setup_us + trans_us;
388
389         total_bytes = dsi.update_region.w *
390                 dsi.update_region.h *
391                 dsi.update_region.bytespp;
392
393         if (dsi.update_mode == OMAP_DSS_UPDATE_AUTO) {
394                 dsi.perf_measure_frames++;
395
396                 if (trans_us < s_min_us)
397                         s_min_us = trans_us;
398
399                 if (trans_us > s_max_us)
400                         s_max_us = trans_us;
401
402                 s_trans_us += trans_us;
403
404                 if (dsi.perf_measure_frames < numframes)
405                         return;
406
407                 DSSINFO("%s update: %d frames in %u us "
408                                 "(min/max/avg %u/%u/%u), %u fps\n",
409                                 name, numframes,
410                                 s_trans_us,
411                                 s_min_us,
412                                 s_max_us,
413                                 s_trans_us / numframes,
414                                 1000*1000 / (s_trans_us / numframes));
415
416                 dsi.perf_measure_frames = 0;
417                 s_trans_us = 0;
418                 s_min_us = 0xffffffff;
419                 s_max_us = 0;
420         } else {
421                 DSSINFO("%s update %u us + %u us = %u us (%uHz), %u bytes, "
422                                 "%u kbytes/sec\n",
423                                 name,
424                                 setup_us,
425                                 trans_us,
426                                 total_us,
427                                 1000*1000 / total_us,
428                                 total_bytes,
429                                 total_bytes * 1000 / total_us);
430         }
431 }
432 #else
433 #define perf_mark_setup()
434 #define perf_mark_start()
435 #define perf_show(x)
436 #endif
437
438 static void print_irq_status(u32 status)
439 {
440 #ifndef VERBOSE_IRQ
441         if ((status & ~DSI_IRQ_CHANNEL_MASK) == 0)
442                 return;
443 #endif
444         printk(KERN_DEBUG "DSI IRQ: 0x%x: ", status);
445
446 #define PIS(x) \
447         if (status & DSI_IRQ_##x) \
448                 printk(#x " ");
449 #ifdef VERBOSE_IRQ
450         PIS(VC0);
451         PIS(VC1);
452         PIS(VC2);
453         PIS(VC3);
454 #endif
455         PIS(WAKEUP);
456         PIS(RESYNC);
457         PIS(PLL_LOCK);
458         PIS(PLL_UNLOCK);
459         PIS(PLL_RECALL);
460         PIS(COMPLEXIO_ERR);
461         PIS(HS_TX_TIMEOUT);
462         PIS(LP_RX_TIMEOUT);
463         PIS(TE_TRIGGER);
464         PIS(ACK_TRIGGER);
465         PIS(SYNC_LOST);
466         PIS(LDO_POWER_GOOD);
467         PIS(TA_TIMEOUT);
468 #undef PIS
469
470         printk("\n");
471 }
472
473 static void print_irq_status_vc(int channel, u32 status)
474 {
475 #ifndef VERBOSE_IRQ
476         if ((status & ~DSI_VC_IRQ_PACKET_SENT) == 0)
477                 return;
478 #endif
479         printk(KERN_DEBUG "DSI VC(%d) IRQ 0x%x: ", channel, status);
480
481 #define PIS(x) \
482         if (status & DSI_VC_IRQ_##x) \
483                 printk(#x " ");
484         PIS(CS);
485         PIS(ECC_CORR);
486 #ifdef VERBOSE_IRQ
487         PIS(PACKET_SENT);
488 #endif
489         PIS(FIFO_TX_OVF);
490         PIS(FIFO_RX_OVF);
491         PIS(BTA);
492         PIS(ECC_NO_CORR);
493         PIS(FIFO_TX_UDF);
494         PIS(PP_BUSY_CHANGE);
495 #undef PIS
496         printk("\n");
497 }
498
499 static void print_irq_status_cio(u32 status)
500 {
501         printk(KERN_DEBUG "DSI CIO IRQ 0x%x: ", status);
502
503 #define PIS(x) \
504         if (status & DSI_CIO_IRQ_##x) \
505                 printk(#x " ");
506         PIS(ERRSYNCESC1);
507         PIS(ERRSYNCESC2);
508         PIS(ERRSYNCESC3);
509         PIS(ERRESC1);
510         PIS(ERRESC2);
511         PIS(ERRESC3);
512         PIS(ERRCONTROL1);
513         PIS(ERRCONTROL2);
514         PIS(ERRCONTROL3);
515         PIS(STATEULPS1);
516         PIS(STATEULPS2);
517         PIS(STATEULPS3);
518         PIS(ERRCONTENTIONLP0_1);
519         PIS(ERRCONTENTIONLP1_1);
520         PIS(ERRCONTENTIONLP0_2);
521         PIS(ERRCONTENTIONLP1_2);
522         PIS(ERRCONTENTIONLP0_3);
523         PIS(ERRCONTENTIONLP1_3);
524         PIS(ULPSACTIVENOT_ALL0);
525         PIS(ULPSACTIVENOT_ALL1);
526 #undef PIS
527
528         printk("\n");
529 }
530
531 static int debug_irq;
532
533 /* called from dss */
534 void dsi_irq_handler(void)
535 {
536         u32 irqstatus, vcstatus, ciostatus;
537         int i;
538
539         irqstatus = dsi_read_reg(DSI_IRQSTATUS);
540
541         if (irqstatus & DSI_IRQ_ERROR_MASK) {
542                 DSSERR("DSI error, irqstatus %x\n", irqstatus);
543                 print_irq_status(irqstatus);
544         } else if (debug_irq) {
545                 print_irq_status(irqstatus);
546         }
547
548         for (i = 0; i < 4; ++i) {
549                 if ((irqstatus & (1<<i)) == 0)
550                         continue;
551
552                 vcstatus = dsi_read_reg(DSI_VC_IRQSTATUS(i));
553
554                 if (vcstatus & DSI_VC_IRQ_BTA)
555                         complete(&dsi.bta_completion);
556
557                 if (vcstatus & DSI_VC_IRQ_ERROR_MASK) {
558                         DSSERR("DSI VC(%d) error, vc irqstatus %x\n",
559                                        i, vcstatus);
560                         print_irq_status_vc(i, vcstatus);
561                 } else if (debug_irq) {
562                         print_irq_status_vc(i, vcstatus);
563                 }
564
565                 dsi_write_reg(DSI_VC_IRQSTATUS(i), vcstatus);
566         }
567
568         if (irqstatus & DSI_IRQ_COMPLEXIO_ERR) {
569                 ciostatus = dsi_read_reg(DSI_COMPLEXIO_IRQ_STATUS);
570
571                 dsi_write_reg(DSI_COMPLEXIO_IRQ_STATUS, ciostatus);
572
573                 DSSERR("DSI CIO error, cio irqstatus %x\n", ciostatus);
574                 print_irq_status_cio(ciostatus);
575         }
576
577         dsi_write_reg(DSI_IRQSTATUS, irqstatus & ~DSI_IRQ_CHANNEL_MASK);
578 }
579
580
581 static void _dsi_initialize_irq(void)
582 {
583         u32 l;
584         int i;
585
586         /* disable all interrupts */
587         dsi_write_reg(DSI_IRQENABLE, 0);
588         for (i = 0; i < 4; ++i)
589                 dsi_write_reg(DSI_VC_IRQENABLE(i), 0);
590         dsi_write_reg(DSI_COMPLEXIO_IRQ_ENABLE, 0);
591
592         /* clear interrupt status */
593         l = dsi_read_reg(DSI_IRQSTATUS);
594         dsi_write_reg(DSI_IRQSTATUS, l & ~DSI_IRQ_CHANNEL_MASK);
595
596         for (i = 0; i < 4; ++i) {
597                 l = dsi_read_reg(DSI_VC_IRQSTATUS(i));
598                 dsi_write_reg(DSI_VC_IRQSTATUS(i), l);
599         }
600
601         l = dsi_read_reg(DSI_COMPLEXIO_IRQ_STATUS);
602         dsi_write_reg(DSI_COMPLEXIO_IRQ_STATUS, l);
603
604         /* enable error irqs */
605         l = DSI_IRQ_ERROR_MASK;
606         dsi_write_reg(DSI_IRQENABLE, l);
607
608         l = DSI_VC_IRQ_ERROR_MASK;
609         for (i = 0; i < 4; ++i)
610                 dsi_write_reg(DSI_VC_IRQENABLE(i), l);
611
612         /* XXX zonda responds incorrectly, causing control error:
613            Exit from LP-ESC mode to LP11 uses wrong transition states on the
614            data lines LP0 and LN0. */
615         dsi_write_reg(DSI_COMPLEXIO_IRQ_ENABLE,
616                         -1 & (~DSI_CIO_IRQ_ERRCONTROL2));
617 }
618
619 static void dsi_vc_enable_bta_irq(int channel)
620 {
621         u32 l;
622
623         l = dsi_read_reg(DSI_VC_IRQENABLE(channel));
624         l |= DSI_VC_IRQ_BTA;
625         dsi_write_reg(DSI_VC_IRQENABLE(channel), l);
626 }
627
628 static void dsi_vc_disable_bta_irq(int channel)
629 {
630         u32 l;
631
632         l = dsi_read_reg(DSI_VC_IRQENABLE(channel));
633         l &= ~DSI_VC_IRQ_BTA;
634         dsi_write_reg(DSI_VC_IRQENABLE(channel), l);
635 }
636
637 /* DSI func clock. this could also be DSI2_PLL_FCLK */
638 static inline void enable_clocks(bool enable)
639 {
640         if (enable)
641                 dss_clk_enable(DSS_CLK_ICK | DSS_CLK_FCK1);
642         else
643                 dss_clk_disable(DSS_CLK_ICK | DSS_CLK_FCK1);
644 }
645
646 /* source clock for DSI PLL. this could also be PCLKFREE */
647 static inline void dsi_enable_pll_clock(bool enable)
648 {
649         if (enable)
650                 dss_clk_enable(DSS_CLK_FCK2);
651         else
652                 dss_clk_disable(DSS_CLK_FCK2);
653
654         if (enable && dsi.pll_locked) {
655                 if (wait_for_bit_change(DSI_PLL_STATUS, 1, 1) != 1)
656                         DSSERR("cannot lock PLL when enabling clocks\n");
657         }
658 }
659
660 #ifdef DEBUG
661 static void _dsi_print_reset_status(void)
662 {
663         u32 l;
664
665         if (!dss_debug)
666                 return;
667
668         /* A dummy read using the SCP interface to any DSIPHY register is
669          * required after DSIPHY reset to complete the reset of the DSI complex
670          * I/O. */
671         l = dsi_read_reg(DSI_DSIPHY_CFG5);
672
673         printk(KERN_DEBUG "DSI resets: ");
674
675         l = dsi_read_reg(DSI_PLL_STATUS);
676         printk("PLL (%d) ", FLD_GET(l, 0, 0));
677
678         l = dsi_read_reg(DSI_COMPLEXIO_CFG1);
679         printk("CIO (%d) ", FLD_GET(l, 29, 29));
680
681         l = dsi_read_reg(DSI_DSIPHY_CFG5);
682         printk("PHY (%x, %d, %d, %d)\n",
683                         FLD_GET(l, 28, 26),
684                         FLD_GET(l, 29, 29),
685                         FLD_GET(l, 30, 30),
686                         FLD_GET(l, 31, 31));
687 }
688 #else
689 #define _dsi_print_reset_status()
690 #endif
691
692 static inline int dsi_if_enable(bool enable)
693 {
694         DSSDBG("dsi_if_enable(%d)\n", enable);
695
696         enable = enable ? 1 : 0;
697         REG_FLD_MOD(DSI_CTRL, enable, 0, 0); /* IF_EN */
698
699         if (wait_for_bit_change(DSI_CTRL, 0, enable) != enable) {
700                         DSSERR("Failed to set dsi_if_enable to %d\n", enable);
701                         return -EIO;
702         }
703
704         return 0;
705 }
706
707 static unsigned long dsi_fclk_rate(void)
708 {
709         unsigned long r;
710
711         if (dss_get_dsi_clk_source() == 0) {
712                 /* DSI FCLK source is DSS1_ALWON_FCK, which is dss1_fck */
713                 r = dss_clk_get_rate(DSS_CLK_FCK1);
714         } else {
715                 /* DSI FCLK source is DSI2_PLL_FCLK */
716                 r = dsi.dsi2_pll_fclk;
717         }
718
719         return r;
720 }
721
722 static int dsi_set_lp_clk_divisor(void)
723 {
724         int n;
725         unsigned long dsi_fclk;
726         unsigned long mhz;
727
728         /* LP_CLK_DIVISOR, DSI fclk/n, should be 20MHz - 32kHz */
729
730         dsi_fclk = dsi_fclk_rate();
731
732         for (n = 1; n < (1 << 13) - 1; ++n) {
733                 mhz = dsi_fclk / n;
734                 if (mhz <= 20*1000*1000)
735                         break;
736         }
737
738         if (n == (1 << 13) - 1) {
739                 DSSERR("Failed to find LP_CLK_DIVISOR\n");
740                 return -EINVAL;
741         }
742
743         DSSDBG("LP_CLK_DIV %d, LP_CLK %ld\n", n, mhz);
744
745         REG_FLD_MOD(DSI_CLK_CTRL, n, 12, 0);    /* LP_CLK_DIVISOR */
746         if (dsi_fclk > 30*1000*1000)
747                 REG_FLD_MOD(DSI_CLK_CTRL, 1, 21, 21); /* LP_RX_SYNCHRO_ENABLE */
748
749         return 0;
750 }
751
752
753 enum dsi_pll_power_state {
754         DSI_PLL_POWER_OFF       = 0x0,
755         DSI_PLL_POWER_ON_HSCLK  = 0x1,
756         DSI_PLL_POWER_ON_ALL    = 0x2,
757         DSI_PLL_POWER_ON_DIV    = 0x3,
758 };
759
760 static int dsi_pll_power(enum dsi_pll_power_state state)
761 {
762         int t = 0;
763
764         REG_FLD_MOD(DSI_CLK_CTRL, state, 31, 30);       /* PLL_PWR_CMD */
765
766         /* PLL_PWR_STATUS */
767         while (FLD_GET(dsi_read_reg(DSI_CLK_CTRL), 29, 28) != state) {
768                 udelay(1);
769                 if (t++ > 1000) {
770                         DSSERR("Failed to set DSI PLL power mode to %d\n",
771                                         state);
772                         return -ENODEV;
773                 }
774         }
775
776         return 0;
777 }
778
779 int dsi_pll_calc_pck(bool is_tft, unsigned long req_pck,
780                 struct dsi_clock_info *cinfo)
781 {
782         struct dsi_clock_info cur, best;
783         int min_fck_per_pck;
784         int match = 0;
785
786         if (req_pck == dsi.cache_req_pck &&
787                         dsi.cache_cinfo.clkin == dss_clk_get_rate(DSS_CLK_FCK2)) {
788                 DSSDBG("DSI clock info found from cache\n");
789                 *cinfo = dsi.cache_cinfo;
790                 return 0;
791         }
792
793         min_fck_per_pck = CONFIG_OMAP2_DSS_MIN_FCK_PER_PCK;
794
795         if (min_fck_per_pck &&
796                 req_pck * min_fck_per_pck > DISPC_MAX_FCK) {
797                 DSSERR("Requested pixel clock not possible with the current "
798                                 "OMAP2_DSS_MIN_FCK_PER_PCK setting. Turning "
799                                 "the constraint off.\n");
800                 min_fck_per_pck = 0;
801         }
802
803         DSSDBG("dsi_pll_calc\n");
804
805 retry:
806         memset(&best, 0, sizeof(best));
807
808         memset(&cur, 0, sizeof(cur));
809         cur.clkin = dss_clk_get_rate(DSS_CLK_FCK2);
810         cur.use_dss2_fck = 1;
811         cur.highfreq = 0;
812
813         /* no highfreq: 0.75MHz < Fint = clkin / regn < 2.1MHz */
814         /* highfreq: 0.75MHz < Fint = clkin / (2*regn) < 2.1MHz */
815         /* To reduce PLL lock time, keep Fint high (around 2 MHz) */
816         for (cur.regn = 1; cur.regn < REGN_MAX; ++cur.regn) {
817                 if (cur.highfreq == 0)
818                         cur.fint = cur.clkin / cur.regn;
819                 else
820                         cur.fint = cur.clkin / (2 * cur.regn);
821
822                 if (cur.fint > FINT_MAX || cur.fint < FINT_MIN)
823                         continue;
824
825                 /* DSIPHY(MHz) = (2 * regm / regn) * (clkin / (highfreq + 1)) */
826                 for (cur.regm = 1; cur.regm < REGM_MAX; ++cur.regm) {
827                         unsigned long a, b;
828
829                         a = 2 * cur.regm * (cur.clkin/1000);
830                         b = cur.regn * (cur.highfreq + 1);
831                         cur.dsiphy = a / b * 1000;
832
833                         if (cur.dsiphy > 1800 * 1000 * 1000)
834                                 break;
835
836                         /* DSI1_PLL_FCLK(MHz) = DSIPHY(MHz) / regm3  < 173MHz */
837                         for (cur.regm3 = 1; cur.regm3 < REGM3_MAX;
838                                         ++cur.regm3) {
839                                 cur.dsi1_pll_fclk = cur.dsiphy / cur.regm3;
840
841                                 /* this will narrow down the search a bit,
842                                  * but still give pixclocks below what was
843                                  * requested */
844                                 if (cur.dsi1_pll_fclk  < req_pck)
845                                         break;
846
847                                 if (cur.dsi1_pll_fclk > DISPC_MAX_FCK)
848                                         continue;
849
850                                 if (min_fck_per_pck &&
851                                         cur.dsi1_pll_fclk <
852                                                 req_pck * min_fck_per_pck)
853                                         continue;
854
855                                 match = 1;
856
857                                 find_lck_pck_divs(is_tft, req_pck,
858                                                 cur.dsi1_pll_fclk,
859                                                 &cur.lck_div,
860                                                 &cur.pck_div);
861
862                                 cur.lck = cur.dsi1_pll_fclk / cur.lck_div;
863                                 cur.pck = cur.lck / cur.pck_div;
864
865                                 if (abs(cur.pck - req_pck) <
866                                                 abs(best.pck - req_pck)) {
867                                         best = cur;
868
869                                         if (cur.pck == req_pck)
870                                                 goto found;
871                                 }
872                         }
873                 }
874         }
875 found:
876         if (!match) {
877                 if (min_fck_per_pck) {
878                         DSSERR("Could not find suitable clock settings.\n"
879                                         "Turning FCK/PCK constraint off and"
880                                         "trying again.\n");
881                         min_fck_per_pck = 0;
882                         goto retry;
883                 }
884
885                 DSSERR("Could not find suitable clock settings.\n");
886
887                 return -EINVAL;
888         }
889
890         /* DSI2_PLL_FCLK (regm4) is not used. Set it to something sane. */
891         best.regm4 = best.dsiphy / 48000000;
892         if (best.regm4 > REGM4_MAX)
893                 best.regm4 = REGM4_MAX;
894         else if (best.regm4 == 0)
895                 best.regm4 = 1;
896         best.dsi2_pll_fclk = best.dsiphy / best.regm4;
897
898         if (cinfo)
899                 *cinfo = best;
900
901         dsi.cache_req_pck = req_pck;
902         dsi.cache_clk_freq = 0;
903         dsi.cache_cinfo = best;
904
905         return 0;
906 }
907
908 static int dsi_pll_calc_ddrfreq(unsigned long clk_freq,
909                 struct dsi_clock_info *cinfo)
910 {
911         struct dsi_clock_info cur, best;
912         const bool use_dss2_fck = 1;
913         unsigned long datafreq;
914
915         DSSDBG("dsi_pll_calc_ddrfreq\n");
916
917         if (clk_freq == dsi.cache_clk_freq &&
918                         dsi.cache_cinfo.clkin == dss_clk_get_rate(DSS_CLK_FCK2)) {
919                 DSSDBG("DSI clock info found from cache\n");
920                 *cinfo = dsi.cache_cinfo;
921                 return 0;
922         }
923
924         datafreq = clk_freq * 4;
925
926         memset(&best, 0, sizeof(best));
927
928         memset(&cur, 0, sizeof(cur));
929         cur.use_dss2_fck = use_dss2_fck;
930         if (use_dss2_fck) {
931                 cur.clkin = dss_clk_get_rate(DSS_CLK_FCK2);
932                 cur.highfreq = 0;
933         } else {
934                 cur.clkin = dispc_pclk_rate();
935                 if (cur.clkin < 32000000)
936                         cur.highfreq = 0;
937                 else
938                         cur.highfreq = 1;
939         }
940
941         /* no highfreq: 0.75MHz < Fint = clkin / regn < 2.1MHz */
942         /* highfreq: 0.75MHz < Fint = clkin / (2*regn) < 2.1MHz */
943         /* To reduce PLL lock time, keep Fint high (around 2 MHz) */
944         for (cur.regn = 1; cur.regn < REGN_MAX; ++cur.regn) {
945                 if (cur.highfreq == 0)
946                         cur.fint = cur.clkin / cur.regn;
947                 else
948                         cur.fint = cur.clkin / (2 * cur.regn);
949
950                 if (cur.fint > FINT_MAX || cur.fint < FINT_MIN)
951                         continue;
952
953                 /* DSIPHY(MHz) = (2 * regm / regn) * (clkin / (highfreq + 1)) */
954                 for (cur.regm = 1; cur.regm < REGM_MAX; ++cur.regm) {
955                         unsigned long a, b;
956
957                         a = 2 * cur.regm * (cur.clkin/1000);
958                         b = cur.regn * (cur.highfreq + 1);
959                         cur.dsiphy = a / b * 1000;
960
961                         if (cur.dsiphy > 1800 * 1000 * 1000)
962                                 break;
963
964                         if (abs(cur.dsiphy - datafreq) <
965                                         abs(best.dsiphy - datafreq)) {
966                                 best = cur;
967                                 /* DSSDBG("best %ld\n", best.dsiphy); */
968                         }
969
970                         if (cur.dsiphy == datafreq)
971                                 goto found;
972                 }
973         }
974 found:
975         /* DSI1_PLL_FCLK (regm3) is not used. Set it to something sane. */
976         best.regm3 = best.dsiphy / 48000000;
977         if (best.regm3 > REGM3_MAX)
978                 best.regm3 = REGM3_MAX;
979         else if (best.regm3 == 0)
980                 best.regm3 = 1;
981         best.dsi1_pll_fclk = best.dsiphy / best.regm3;
982
983         /* DSI2_PLL_FCLK (regm4) is not used. Set it to something sane. */
984         best.regm4 = best.dsiphy / 48000000;
985         if (best.regm4 > REGM4_MAX)
986                 best.regm4 = REGM4_MAX;
987         else if (best.regm4 == 0)
988                 best.regm4 = 1;
989         best.dsi2_pll_fclk = best.dsiphy / best.regm4;
990
991         if (cinfo)
992                 *cinfo = best;
993
994         dsi.cache_clk_freq = clk_freq;
995         dsi.cache_req_pck = 0;
996         dsi.cache_cinfo = best;
997
998         return 0;
999 }
1000
1001 int dsi_pll_program(struct dsi_clock_info *cinfo)
1002 {
1003         int r = 0;
1004         u32 l;
1005
1006         DSSDBG("dsi_pll_program\n");
1007
1008         dsi.dsiphy = cinfo->dsiphy;
1009         dsi.ddr_clk = dsi.dsiphy / 4;
1010         dsi.dsi1_pll_fclk = cinfo->dsi1_pll_fclk;
1011         dsi.dsi2_pll_fclk = cinfo->dsi2_pll_fclk;
1012
1013         DSSDBG("DSI Fint %ld\n", cinfo->fint);
1014
1015         DSSDBG("clkin (%s) rate %ld, highfreq %d\n",
1016                         cinfo->use_dss2_fck ? "dss2_fck" : "pclkfree",
1017                         cinfo->clkin,
1018                         cinfo->highfreq);
1019
1020         /* DSIPHY == CLKIN4DDR */
1021         DSSDBG("DSIPHY = 2 * %d / %d * %lu / %d = %lu\n",
1022                         cinfo->regm,
1023                         cinfo->regn,
1024                         cinfo->clkin,
1025                         cinfo->highfreq + 1,
1026                         cinfo->dsiphy);
1027
1028         DSSDBG("Data rate on 1 DSI lane %ld Mbps\n",
1029                         dsi.dsiphy / 1000 / 1000 / 2);
1030
1031         DSSDBG("Clock lane freq %ld Hz\n", dsi.ddr_clk);
1032
1033         DSSDBG("regm3 = %d, dsi1_pll_fclk = %lu\n",
1034                         cinfo->regm3, cinfo->dsi1_pll_fclk);
1035         DSSDBG("regm4 = %d, dsi2_pll_fclk = %lu\n",
1036                         cinfo->regm4, cinfo->dsi2_pll_fclk);
1037
1038         REG_FLD_MOD(DSI_PLL_CONTROL, 0, 0, 0); /* DSI_PLL_AUTOMODE = manual */
1039
1040         l = dsi_read_reg(DSI_PLL_CONFIGURATION1);
1041         l = FLD_MOD(l, 1, 0, 0);                /* DSI_PLL_STOPMODE */
1042         l = FLD_MOD(l, cinfo->regn - 1, 7, 1);  /* DSI_PLL_REGN */
1043         l = FLD_MOD(l, cinfo->regm, 18, 8);     /* DSI_PLL_REGM */
1044         l = FLD_MOD(l, cinfo->regm3 - 1, 22, 19);       /* DSI_CLOCK_DIV */
1045         l = FLD_MOD(l, cinfo->regm4 - 1, 26, 23);       /* DSIPROTO_CLOCK_DIV */
1046         dsi_write_reg(DSI_PLL_CONFIGURATION1, l);
1047
1048         l = dsi_read_reg(DSI_PLL_CONFIGURATION2);
1049         l = FLD_MOD(l, 7, 4, 1);                /* DSI_PLL_FREQSEL */
1050         /* DSI_PLL_CLKSEL */
1051         l = FLD_MOD(l, cinfo->use_dss2_fck ? 0 : 1, 11, 11);
1052         l = FLD_MOD(l, cinfo->highfreq, 12, 12);        /* DSI_PLL_HIGHFREQ */
1053         l = FLD_MOD(l, 1, 13, 13);              /* DSI_PLL_REFEN */
1054         l = FLD_MOD(l, 0, 14, 14);              /* DSIPHY_CLKINEN */
1055         l = FLD_MOD(l, 1, 20, 20);              /* DSI_HSDIVBYPASS */
1056         dsi_write_reg(DSI_PLL_CONFIGURATION2, l);
1057
1058         REG_FLD_MOD(DSI_PLL_GO, 1, 0, 0); /* DSI_PLL_GO */
1059
1060         if (wait_for_bit_change(DSI_PLL_GO, 0, 0) != 0) {
1061                 DSSERR("dsi pll go bit not going down.\n");
1062                 r = -EIO;
1063                 goto err;
1064         }
1065
1066         if (wait_for_bit_change(DSI_PLL_STATUS, 1, 1) != 1) {
1067                 DSSERR("cannot lock PLL\n");
1068                 r = -EIO;
1069                 goto err;
1070         }
1071
1072         dsi.pll_locked = 1;
1073
1074         l = dsi_read_reg(DSI_PLL_CONFIGURATION2);
1075         l = FLD_MOD(l, 0, 0, 0);        /* DSI_PLL_IDLE */
1076         l = FLD_MOD(l, 0, 5, 5);        /* DSI_PLL_PLLLPMODE */
1077         l = FLD_MOD(l, 0, 6, 6);        /* DSI_PLL_LOWCURRSTBY */
1078         l = FLD_MOD(l, 0, 7, 7);        /* DSI_PLL_TIGHTPHASELOCK */
1079         l = FLD_MOD(l, 0, 8, 8);        /* DSI_PLL_DRIFTGUARDEN */
1080         l = FLD_MOD(l, 0, 10, 9);       /* DSI_PLL_LOCKSEL */
1081         l = FLD_MOD(l, 1, 13, 13);      /* DSI_PLL_REFEN */
1082         l = FLD_MOD(l, 1, 14, 14);      /* DSIPHY_CLKINEN */
1083         l = FLD_MOD(l, 0, 15, 15);      /* DSI_BYPASSEN */
1084         l = FLD_MOD(l, 1, 16, 16);      /* DSS_CLOCK_EN */
1085         l = FLD_MOD(l, 0, 17, 17);      /* DSS_CLOCK_PWDN */
1086         l = FLD_MOD(l, 1, 18, 18);      /* DSI_PROTO_CLOCK_EN */
1087         l = FLD_MOD(l, 0, 19, 19);      /* DSI_PROTO_CLOCK_PWDN */
1088         l = FLD_MOD(l, 0, 20, 20);      /* DSI_HSDIVBYPASS */
1089         dsi_write_reg(DSI_PLL_CONFIGURATION2, l);
1090
1091         DSSDBG("PLL config done\n");
1092 err:
1093         return r;
1094 }
1095
1096 int dsi_pll_init(bool enable_hsclk, bool enable_hsdiv)
1097 {
1098         int r = 0;
1099         enum dsi_pll_power_state pwstate;
1100         struct dispc_clock_info cinfo;
1101
1102         DSSDBG("PLL init\n");
1103
1104         enable_clocks(1);
1105         dsi_enable_pll_clock(1);
1106
1107         /* configure dispc fck and pixel clock to something sane */
1108         r = dispc_calc_clock_div(1, 48 * 1000 * 1000, &cinfo);
1109         if (r)
1110                 goto err0;
1111
1112         r = dispc_set_clock_div(&cinfo);
1113         if (r) {
1114                 DSSERR("Failed to set basic clocks\n");
1115                 goto err0;
1116         }
1117
1118         r = dss_dsi_power_up();
1119         if (r)
1120                 goto err0;
1121
1122         /* PLL does not come out of reset without this... */
1123         dispc_pck_free_enable(1);
1124
1125         if (wait_for_bit_change(DSI_PLL_STATUS, 0, 1) != 1) {
1126                 DSSERR("PLL not coming out of reset.\n");
1127                 r = -ENODEV;
1128                 goto err1;
1129         }
1130
1131         /* ... but if left on, we get problems when planes do not
1132          * fill the whole display. No idea about this XXX */
1133         dispc_pck_free_enable(0);
1134
1135         if (enable_hsclk && enable_hsdiv)
1136                 pwstate = DSI_PLL_POWER_ON_ALL;
1137         else if (enable_hsclk)
1138                 pwstate = DSI_PLL_POWER_ON_HSCLK;
1139         else if (enable_hsdiv)
1140                 pwstate = DSI_PLL_POWER_ON_DIV;
1141         else
1142                 pwstate = DSI_PLL_POWER_OFF;
1143
1144         r = dsi_pll_power(pwstate);
1145
1146         if (r)
1147                 goto err1;
1148
1149         DSSDBG("PLL init done\n");
1150
1151         return 0;
1152 err1:
1153         dss_dsi_power_down();
1154 err0:
1155         enable_clocks(0);
1156         dsi_enable_pll_clock(0);
1157         return r;
1158 }
1159
1160 void dsi_pll_uninit(void)
1161 {
1162         enable_clocks(0);
1163         dsi_enable_pll_clock(0);
1164
1165         dsi.pll_locked = 0;
1166         dsi_pll_power(DSI_PLL_POWER_OFF);
1167         dss_dsi_power_down();
1168         DSSDBG("PLL uninit done\n");
1169 }
1170
1171 unsigned long dsi_get_dsi1_pll_rate(void)
1172 {
1173         return dsi.dsi1_pll_fclk;
1174 }
1175
1176 unsigned long dsi_get_dsi2_pll_rate(void)
1177 {
1178         return dsi.dsi2_pll_fclk;
1179 }
1180
1181 void dsi_dump_clocks(struct seq_file *s)
1182 {
1183         int clksel;
1184
1185         enable_clocks(1);
1186
1187         clksel = REG_GET(DSI_PLL_CONFIGURATION2, 11, 11);
1188
1189         seq_printf(s, "- dsi -\n");
1190
1191         seq_printf(s, "dsi fclk source = %s\n",
1192                         dss_get_dsi_clk_source() == 0 ?
1193                         "dss1_alwon_fclk" : "dsi2_pll_fclk");
1194
1195         seq_printf(s, "dsi pll source = %s\n",
1196                         clksel == 0 ?
1197                         "dss2_alwon_fclk" : "pclkfree");
1198
1199         seq_printf(s,   "DSIPHY\t\t%lu\nDDR_CLK\t\t%lu\n",
1200                         dsi.dsiphy, dsi.ddr_clk);
1201
1202         seq_printf(s,   "dsi1_pll_fck\t%lu (%s)\n"
1203                         "dsi2_pll_fck\t%lu (%s)\n",
1204                         dsi.dsi1_pll_fclk,
1205                         dss_get_dispc_clk_source() == 0 ? "off" : "on",
1206                         dsi.dsi2_pll_fclk,
1207                         dss_get_dsi_clk_source() == 0 ? "off" : "on");
1208
1209         enable_clocks(0);
1210 }
1211
1212 void dsi_dump_regs(struct seq_file *s)
1213 {
1214 #define DUMPREG(r) seq_printf(s, "%-35s %08x\n", #r, dsi_read_reg(r))
1215
1216         dss_clk_enable(DSS_CLK_ICK | DSS_CLK_FCK1);
1217
1218         DUMPREG(DSI_REVISION);
1219         DUMPREG(DSI_SYSCONFIG);
1220         DUMPREG(DSI_SYSSTATUS);
1221         DUMPREG(DSI_IRQSTATUS);
1222         DUMPREG(DSI_IRQENABLE);
1223         DUMPREG(DSI_CTRL);
1224         DUMPREG(DSI_COMPLEXIO_CFG1);
1225         DUMPREG(DSI_COMPLEXIO_IRQ_STATUS);
1226         DUMPREG(DSI_COMPLEXIO_IRQ_ENABLE);
1227         DUMPREG(DSI_CLK_CTRL);
1228         DUMPREG(DSI_TIMING1);
1229         DUMPREG(DSI_TIMING2);
1230         DUMPREG(DSI_VM_TIMING1);
1231         DUMPREG(DSI_VM_TIMING2);
1232         DUMPREG(DSI_VM_TIMING3);
1233         DUMPREG(DSI_CLK_TIMING);
1234         DUMPREG(DSI_TX_FIFO_VC_SIZE);
1235         DUMPREG(DSI_RX_FIFO_VC_SIZE);
1236         DUMPREG(DSI_COMPLEXIO_CFG2);
1237         DUMPREG(DSI_RX_FIFO_VC_FULLNESS);
1238         DUMPREG(DSI_VM_TIMING4);
1239         DUMPREG(DSI_TX_FIFO_VC_EMPTINESS);
1240         DUMPREG(DSI_VM_TIMING5);
1241         DUMPREG(DSI_VM_TIMING6);
1242         DUMPREG(DSI_VM_TIMING7);
1243         DUMPREG(DSI_STOPCLK_TIMING);
1244
1245         DUMPREG(DSI_VC_CTRL(0));
1246         DUMPREG(DSI_VC_TE(0));
1247         DUMPREG(DSI_VC_LONG_PACKET_HEADER(0));
1248         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(0));
1249         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(0));
1250         DUMPREG(DSI_VC_IRQSTATUS(0));
1251         DUMPREG(DSI_VC_IRQENABLE(0));
1252
1253         DUMPREG(DSI_VC_CTRL(1));
1254         DUMPREG(DSI_VC_TE(1));
1255         DUMPREG(DSI_VC_LONG_PACKET_HEADER(1));
1256         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(1));
1257         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(1));
1258         DUMPREG(DSI_VC_IRQSTATUS(1));
1259         DUMPREG(DSI_VC_IRQENABLE(1));
1260
1261         DUMPREG(DSI_VC_CTRL(2));
1262         DUMPREG(DSI_VC_TE(2));
1263         DUMPREG(DSI_VC_LONG_PACKET_HEADER(2));
1264         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(2));
1265         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(2));
1266         DUMPREG(DSI_VC_IRQSTATUS(2));
1267         DUMPREG(DSI_VC_IRQENABLE(2));
1268
1269         DUMPREG(DSI_VC_CTRL(3));
1270         DUMPREG(DSI_VC_TE(3));
1271         DUMPREG(DSI_VC_LONG_PACKET_HEADER(3));
1272         DUMPREG(DSI_VC_LONG_PACKET_PAYLOAD(3));
1273         DUMPREG(DSI_VC_SHORT_PACKET_HEADER(3));
1274         DUMPREG(DSI_VC_IRQSTATUS(3));
1275         DUMPREG(DSI_VC_IRQENABLE(3));
1276
1277         DUMPREG(DSI_DSIPHY_CFG0);
1278         DUMPREG(DSI_DSIPHY_CFG1);
1279         DUMPREG(DSI_DSIPHY_CFG2);
1280         DUMPREG(DSI_DSIPHY_CFG5);
1281
1282         DUMPREG(DSI_PLL_CONTROL);
1283         DUMPREG(DSI_PLL_STATUS);
1284         DUMPREG(DSI_PLL_GO);
1285         DUMPREG(DSI_PLL_CONFIGURATION1);
1286         DUMPREG(DSI_PLL_CONFIGURATION2);
1287
1288         dss_clk_disable(DSS_CLK_ICK | DSS_CLK_FCK1);
1289 #undef DUMPREG
1290 }
1291
1292 enum dsi_complexio_power_state {
1293         DSI_COMPLEXIO_POWER_OFF         = 0x0,
1294         DSI_COMPLEXIO_POWER_ON          = 0x1,
1295         DSI_COMPLEXIO_POWER_ULPS        = 0x2,
1296 };
1297
1298 static int dsi_complexio_power(enum dsi_complexio_power_state state)
1299 {
1300         int t = 0;
1301
1302         /* PWR_CMD */
1303         REG_FLD_MOD(DSI_COMPLEXIO_CFG1, state, 28, 27);
1304
1305         /* PWR_STATUS */
1306         while (FLD_GET(dsi_read_reg(DSI_COMPLEXIO_CFG1), 26, 25) != state) {
1307                 udelay(1);
1308                 if (t++ > 1000) {
1309                         DSSERR("failed to set complexio power state to "
1310                                         "%d\n", state);
1311                         return -ENODEV;
1312                 }
1313         }
1314
1315         return 0;
1316 }
1317
1318 static void dsi_complexio_config(struct omap_display *display)
1319 {
1320         u32 r;
1321
1322         int clk_lane   = display->hw_config.u.dsi.clk_lane;
1323         int data1_lane = display->hw_config.u.dsi.data1_lane;
1324         int data2_lane = display->hw_config.u.dsi.data2_lane;
1325         int clk_pol    = display->hw_config.u.dsi.clk_pol;
1326         int data1_pol  = display->hw_config.u.dsi.data1_pol;
1327         int data2_pol  = display->hw_config.u.dsi.data2_pol;
1328
1329         r = dsi_read_reg(DSI_COMPLEXIO_CFG1);
1330         r = FLD_MOD(r, clk_lane, 2, 0);
1331         r = FLD_MOD(r, clk_pol, 3, 3);
1332         r = FLD_MOD(r, data1_lane, 6, 4);
1333         r = FLD_MOD(r, data1_pol, 7, 7);
1334         r = FLD_MOD(r, data2_lane, 10, 8);
1335         r = FLD_MOD(r, data2_pol, 11, 11);
1336         dsi_write_reg(DSI_COMPLEXIO_CFG1, r);
1337
1338         /* The configuration of the DSI complex I/O (number of data lanes,
1339            position, differential order) should not be changed while
1340            DSS.DSI_CLK_CRTRL[20] LP_CLK_ENABLE bit is set to 1. In order for
1341            the hardware to take into account a new configuration of the complex
1342            I/O (done in DSS.DSI_COMPLEXIO_CFG1 register), it is recommended to
1343            follow this sequence: First set the DSS.DSI_CTRL[0] IF_EN bit to 1,
1344            then reset the DSS.DSI_CTRL[0] IF_EN to 0, then set
1345            DSS.DSI_CLK_CTRL[20] LP_CLK_ENABLE to 1 and finally set again the
1346            DSS.DSI_CTRL[0] IF_EN bit to 1. If the sequence is not followed, the
1347            DSI complex I/O configuration is unknown. */
1348
1349         /*
1350         REG_FLD_MOD(DSI_CTRL, 1, 0, 0);
1351         REG_FLD_MOD(DSI_CTRL, 0, 0, 0);
1352         REG_FLD_MOD(DSI_CLK_CTRL, 1, 20, 20);
1353         REG_FLD_MOD(DSI_CTRL, 1, 0, 0);
1354         */
1355 }
1356
1357 static inline unsigned ns2ddr(unsigned ns)
1358 {
1359         /* convert time in ns to ddr ticks, rounding up */
1360         return (ns * (dsi.ddr_clk/1000/1000) + 999) / 1000;
1361 }
1362
1363 static inline unsigned ddr2ns(unsigned ddr)
1364 {
1365         return ddr * 1000 * 1000 / (dsi.ddr_clk / 1000);
1366 }
1367
1368 static void dsi_complexio_timings(void)
1369 {
1370         u32 r;
1371         u32 ths_prepare, ths_prepare_ths_zero, ths_trail, ths_exit;
1372         u32 tlpx_half, tclk_trail, tclk_zero;
1373         u32 tclk_prepare;
1374
1375         /* calculate timings */
1376
1377         /* 1 * DDR_CLK = 2 * UI */
1378
1379         /* min 40ns + 4*UI      max 85ns + 6*UI */
1380         ths_prepare = ns2ddr(59) + 2;
1381
1382         /* min 145ns + 10*UI */
1383         ths_prepare_ths_zero = ns2ddr(145) + 5;
1384
1385         /* min max(8*UI, 60ns+4*UI) */
1386         ths_trail = max((unsigned)4, ns2ddr(60) + 2);
1387
1388         /* min 100ns */
1389         ths_exit = ns2ddr(100);
1390
1391         /* tlpx min 50n */
1392         tlpx_half = ns2ddr(25);
1393
1394         /* min 60ns */
1395         tclk_trail = ns2ddr(60);
1396
1397         /* min 38ns, max 95ns */
1398         tclk_prepare = ns2ddr(38);
1399
1400         /* min tclk-prepare + tclk-zero = 300ns */
1401         tclk_zero = ns2ddr(300 - 38);
1402
1403         DSSDBG("ths_prepare %u (%uns), ths_prepare_ths_zero %u (%uns)\n",
1404                 ths_prepare, ddr2ns(ths_prepare),
1405                 ths_prepare_ths_zero, ddr2ns(ths_prepare_ths_zero));
1406         DSSDBG("ths_trail %u (%uns), ths_exit %u (%uns)\n",
1407                         ths_trail, ddr2ns(ths_trail),
1408                         ths_exit, ddr2ns(ths_exit));
1409
1410         DSSDBG("tlpx_half %u (%uns), tclk_trail %u (%uns), "
1411                         "tclk_zero %u (%uns)\n",
1412                         tlpx_half, ddr2ns(tlpx_half),
1413                         tclk_trail, ddr2ns(tclk_trail),
1414                         tclk_zero, ddr2ns(tclk_zero));
1415         DSSDBG("tclk_prepare %u (%uns)\n",
1416                         tclk_prepare, ddr2ns(tclk_prepare));
1417
1418         /* program timings */
1419
1420         r = dsi_read_reg(DSI_DSIPHY_CFG0);
1421         r = FLD_MOD(r, ths_prepare, 31, 24);
1422         r = FLD_MOD(r, ths_prepare_ths_zero, 23, 16);
1423         r = FLD_MOD(r, ths_trail, 15, 8);
1424         r = FLD_MOD(r, ths_exit, 7, 0);
1425         dsi_write_reg(DSI_DSIPHY_CFG0, r);
1426
1427         r = dsi_read_reg(DSI_DSIPHY_CFG1);
1428         r = FLD_MOD(r, tlpx_half, 22, 16);
1429         r = FLD_MOD(r, tclk_trail, 15, 8);
1430         r = FLD_MOD(r, tclk_zero, 7, 0);
1431         dsi_write_reg(DSI_DSIPHY_CFG1, r);
1432
1433         r = dsi_read_reg(DSI_DSIPHY_CFG2);
1434         r = FLD_MOD(r, tclk_prepare, 7, 0);
1435         dsi_write_reg(DSI_DSIPHY_CFG2, r);
1436 }
1437
1438
1439 static int dsi_complexio_init(struct omap_display *display)
1440 {
1441         int r = 0;
1442
1443         DSSDBG("dsi_complexio_init\n");
1444
1445         /* CIO_CLK_ICG, enable L3 clk to CIO */
1446         REG_FLD_MOD(DSI_CLK_CTRL, 1, 14, 14);
1447
1448         /* A dummy read using the SCP interface to any DSIPHY register is
1449          * required after DSIPHY reset to complete the reset of the DSI complex
1450          * I/O. */
1451         dsi_read_reg(DSI_DSIPHY_CFG5);
1452
1453         if (wait_for_bit_change(DSI_DSIPHY_CFG5, 30, 1) != 1) {
1454                 DSSERR("ComplexIO PHY not coming out of reset.\n");
1455                 r = -ENODEV;
1456                 goto err;
1457         }
1458
1459         dsi_complexio_config(display);
1460
1461         r = dsi_complexio_power(DSI_COMPLEXIO_POWER_ON);
1462
1463         if (r)
1464                 goto err;
1465
1466         if (wait_for_bit_change(DSI_COMPLEXIO_CFG1, 29, 1) != 1) {
1467                 DSSERR("ComplexIO not coming out of reset.\n");
1468                 r = -ENODEV;
1469                 goto err;
1470         }
1471
1472         if (wait_for_bit_change(DSI_COMPLEXIO_CFG1, 21, 1) != 1) {
1473                 DSSERR("ComplexIO LDO power down.\n");
1474                 r = -ENODEV;
1475                 goto err;
1476         }
1477
1478         dsi_complexio_timings();
1479
1480         /*
1481            The configuration of the DSI complex I/O (number of data lanes,
1482            position, differential order) should not be changed while
1483            DSS.DSI_CLK_CRTRL[20] LP_CLK_ENABLE bit is set to 1. For the
1484            hardware to recognize a new configuration of the complex I/O (done
1485            in DSS.DSI_COMPLEXIO_CFG1 register), it is recommended to follow
1486            this sequence: First set the DSS.DSI_CTRL[0] IF_EN bit to 1, next
1487            reset the DSS.DSI_CTRL[0] IF_EN to 0, then set DSS.DSI_CLK_CTRL[20]
1488            LP_CLK_ENABLE to 1, and finally, set again the DSS.DSI_CTRL[0] IF_EN
1489            bit to 1. If the sequence is not followed, the DSi complex I/O
1490            configuration is undetermined.
1491            */
1492         dsi_if_enable(1);
1493         dsi_if_enable(0);
1494         REG_FLD_MOD(DSI_CLK_CTRL, 1, 20, 20); /* LP_CLK_ENABLE */
1495         dsi_if_enable(1);
1496         dsi_if_enable(0);
1497
1498         DSSDBG("CIO init done\n");
1499 err:
1500         return r;
1501 }
1502
1503 static void dsi_complexio_uninit(void)
1504 {
1505         dsi_complexio_power(DSI_COMPLEXIO_POWER_OFF);
1506 }
1507
1508 static int _dsi_wait_reset(void)
1509 {
1510         int i = 0;
1511
1512         while (REG_GET(DSI_SYSSTATUS, 0, 0) == 0) {
1513                 if (i++ > 5) {
1514                         DSSERR("soft reset failed\n");
1515                         return -ENODEV;
1516                 }
1517                 udelay(1);
1518         }
1519
1520         return 0;
1521 }
1522
1523 static int _dsi_reset(void)
1524 {
1525         /* Soft reset */
1526         REG_FLD_MOD(DSI_SYSCONFIG, 1, 1, 1);
1527         return _dsi_wait_reset();
1528 }
1529
1530
1531 static void dsi_config_tx_fifo(enum fifo_size size1, enum fifo_size size2,
1532                 enum fifo_size size3, enum fifo_size size4)
1533 {
1534         u32 r = 0;
1535         int add = 0;
1536         int i;
1537
1538         dsi.vc[0].fifo_size = size1;
1539         dsi.vc[1].fifo_size = size2;
1540         dsi.vc[2].fifo_size = size3;
1541         dsi.vc[3].fifo_size = size4;
1542
1543         for (i = 0; i < 4; i++) {
1544                 u8 v;
1545                 int size = dsi.vc[i].fifo_size;
1546
1547                 if (add + size > 4) {
1548                         DSSERR("Illegal FIFO configuration\n");
1549                         BUG();
1550                 }
1551
1552                 v = FLD_VAL(add, 2, 0) | FLD_VAL(size, 7, 4);
1553                 r |= v << (8 * i);
1554                 /*DSSDBG("TX FIFO vc %d: size %d, add %d\n", i, size, add); */
1555                 add += size;
1556         }
1557
1558         dsi_write_reg(DSI_TX_FIFO_VC_SIZE, r);
1559 }
1560
1561 static void dsi_config_rx_fifo(enum fifo_size size1, enum fifo_size size2,
1562                 enum fifo_size size3, enum fifo_size size4)
1563 {
1564         u32 r = 0;
1565         int add = 0;
1566         int i;
1567
1568         dsi.vc[0].fifo_size = size1;
1569         dsi.vc[1].fifo_size = size2;
1570         dsi.vc[2].fifo_size = size3;
1571         dsi.vc[3].fifo_size = size4;
1572
1573         for (i = 0; i < 4; i++) {
1574                 u8 v;
1575                 int size = dsi.vc[i].fifo_size;
1576
1577                 if (add + size > 4) {
1578                         DSSERR("Illegal FIFO configuration\n");
1579                         BUG();
1580                 }
1581
1582                 v = FLD_VAL(add, 2, 0) | FLD_VAL(size, 7, 4);
1583                 r |= v << (8 * i);
1584                 /*DSSDBG("RX FIFO vc %d: size %d, add %d\n", i, size, add); */
1585                 add += size;
1586         }
1587
1588         dsi_write_reg(DSI_RX_FIFO_VC_SIZE, r);
1589 }
1590
1591 static int dsi_force_tx_stop_mode_io(void)
1592 {
1593         u32 r;
1594
1595         r = dsi_read_reg(DSI_TIMING1);
1596         r = FLD_MOD(r, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
1597         dsi_write_reg(DSI_TIMING1, r);
1598
1599         if (wait_for_bit_change(DSI_TIMING1, 15, 0) != 0) {
1600                 DSSERR("TX_STOP bit not going down\n");
1601                 return -EIO;
1602         }
1603
1604         return 0;
1605 }
1606
1607 static void dsi_vc_print_status(int channel)
1608 {
1609         u32 r;
1610
1611         r = dsi_read_reg(DSI_VC_CTRL(channel));
1612         DSSDBG("vc %d: TX_FIFO_NOT_EMPTY %d, BTA_EN %d, VC_BUSY %d, "
1613                         "TX_FIFO_FULL %d, RX_FIFO_NOT_EMPTY %d, ",
1614                         channel,
1615                         FLD_GET(r, 5, 5),
1616                         FLD_GET(r, 6, 6),
1617                         FLD_GET(r, 15, 15),
1618                         FLD_GET(r, 16, 16),
1619                         FLD_GET(r, 20, 20));
1620
1621         r = dsi_read_reg(DSI_TX_FIFO_VC_EMPTINESS);
1622         DSSDBG("EMPTINESS %d\n", (r >> (8 * channel)) & 0xff);
1623 }
1624
1625 static void dsi_vc_config(int channel)
1626 {
1627         u32 r;
1628
1629         DSSDBG("dsi_vc_config %d\n", channel);
1630
1631         r = dsi_read_reg(DSI_VC_CTRL(channel));
1632
1633         r = FLD_MOD(r, 0, 1, 1); /* SOURCE, 0 = L4 */
1634         r = FLD_MOD(r, 0, 2, 2); /* BTA_SHORT_EN  */
1635         r = FLD_MOD(r, 0, 3, 3); /* BTA_LONG_EN */
1636         r = FLD_MOD(r, 0, 4, 4); /* MODE, 0 = command */
1637         r = FLD_MOD(r, 1, 7, 7); /* CS_TX_EN */
1638         r = FLD_MOD(r, 1, 8, 8); /* ECC_TX_EN */
1639         r = FLD_MOD(r, 0, 9, 9); /* MODE_SPEED, high speed on/off */
1640
1641         r = FLD_MOD(r, 4, 29, 27); /* DMA_RX_REQ_NB = no dma */
1642         r = FLD_MOD(r, 4, 23, 21); /* DMA_TX_REQ_NB = no dma */
1643
1644         dsi_write_reg(DSI_VC_CTRL(channel), r);
1645 }
1646
1647 static void dsi_vc_config_vp(int channel)
1648 {
1649         u32 r;
1650
1651         DSSDBG("dsi_vc_config_vp\n");
1652
1653         r = dsi_read_reg(DSI_VC_CTRL(channel));
1654
1655         r = FLD_MOD(r, 1, 1, 1); /* SOURCE, 1 = video port */
1656         r = FLD_MOD(r, 0, 2, 2); /* BTA_SHORT_EN */
1657         r = FLD_MOD(r, 0, 3, 3); /* BTA_LONG_EN */
1658         r = FLD_MOD(r, 0, 4, 4); /* MODE, 0 = command */
1659         r = FLD_MOD(r, 1, 7, 7); /* CS_TX_EN */
1660         r = FLD_MOD(r, 1, 8, 8); /* ECC_TX_EN */
1661         r = FLD_MOD(r, 1, 9, 9); /* MODE_SPEED, high speed on/off */
1662
1663         r = FLD_MOD(r, 4, 29, 27); /* DMA_RX_REQ_NB = no dma */
1664         r = FLD_MOD(r, 4, 23, 21); /* DMA_TX_REQ_NB = no dma */
1665
1666         dsi_write_reg(DSI_VC_CTRL(channel), r);
1667 }
1668
1669
1670 static int dsi_vc_enable(int channel, bool enable)
1671 {
1672         DSSDBG("dsi_vc_enable channel %d, enable %d\n", channel, enable);
1673
1674         enable = enable ? 1 : 0;
1675
1676         REG_FLD_MOD(DSI_VC_CTRL(channel), enable, 0, 0);
1677
1678         if (wait_for_bit_change(DSI_VC_CTRL(channel), 0, enable) != enable) {
1679                         DSSERR("Failed to set dsi_vc_enable to %d\n", enable);
1680                         return -EIO;
1681         }
1682
1683         return 0;
1684 }
1685
1686 static void dsi_vc_enable_hs(int channel, bool enable)
1687 {
1688         DSSDBG("dsi_vc_enable_hs(%d, %d)\n", channel, enable);
1689
1690         dsi_vc_enable(channel, 0);
1691         dsi_if_enable(0);
1692
1693         REG_FLD_MOD(DSI_VC_CTRL(channel), enable, 9, 9);
1694
1695         dsi_vc_enable(channel, 1);
1696         dsi_if_enable(1);
1697
1698         dsi_force_tx_stop_mode_io();
1699 }
1700
1701 static void dsi_vc_flush_long_data(int channel)
1702 {
1703         while (REG_GET(DSI_VC_CTRL(channel), 20, 20)) {
1704                 u32 val;
1705                 val = dsi_read_reg(DSI_VC_SHORT_PACKET_HEADER(channel));
1706                 DSSDBG("\t\tb1 %#02x b2 %#02x b3 %#02x b4 %#02x\n",
1707                                 (val >> 0) & 0xff,
1708                                 (val >> 8) & 0xff,
1709                                 (val >> 16) & 0xff,
1710                                 (val >> 24) & 0xff);
1711         }
1712 }
1713
1714 static void dsi_show_rx_ack_with_err(u16 err)
1715 {
1716         DSSERR("\tACK with ERROR (%#x):\n", err);
1717         if (err & (1 << 0))
1718                 DSSERR("\t\tSoT Error\n");
1719         if (err & (1 << 1))
1720                 DSSERR("\t\tSoT Sync Error\n");
1721         if (err & (1 << 2))
1722                 DSSERR("\t\tEoT Sync Error\n");
1723         if (err & (1 << 3))
1724                 DSSERR("\t\tEscape Mode Entry Command Error\n");
1725         if (err & (1 << 4))
1726                 DSSERR("\t\tLP Transmit Sync Error\n");
1727         if (err & (1 << 5))
1728                 DSSERR("\t\tHS Receive Timeout Error\n");
1729         if (err & (1 << 6))
1730                 DSSERR("\t\tFalse Control Error\n");
1731         if (err & (1 << 7))
1732                 DSSERR("\t\t(reserved7)\n");
1733         if (err & (1 << 8))
1734                 DSSERR("\t\tECC Error, single-bit (corrected)\n");
1735         if (err & (1 << 9))
1736                 DSSERR("\t\tECC Error, multi-bit (not corrected)\n");
1737         if (err & (1 << 10))
1738                 DSSERR("\t\tChecksum Error\n");
1739         if (err & (1 << 11))
1740                 DSSERR("\t\tData type not recognized\n");
1741         if (err & (1 << 12))
1742                 DSSERR("\t\tInvalid VC ID\n");
1743         if (err & (1 << 13))
1744                 DSSERR("\t\tInvalid Transmission Length\n");
1745         if (err & (1 << 14))
1746                 DSSERR("\t\t(reserved14)\n");
1747         if (err & (1 << 15))
1748                 DSSERR("\t\tDSI Protocol Violation\n");
1749 }
1750
1751 static u16 dsi_vc_flush_receive_data(int channel)
1752 {
1753         /* RX_FIFO_NOT_EMPTY */
1754         while (REG_GET(DSI_VC_CTRL(channel), 20, 20)) {
1755                 u32 val;
1756                 u8 dt;
1757                 val = dsi_read_reg(DSI_VC_SHORT_PACKET_HEADER(channel));
1758                 DSSDBG("\trawval %#08x\n", val);
1759                 dt = FLD_GET(val, 5, 0);
1760                 if (dt == DSI_DT_RX_ACK_WITH_ERR) {
1761                         u16 err = FLD_GET(val, 23, 8);
1762                         dsi_show_rx_ack_with_err(err);
1763                 } else if (dt == DSI_DT_RX_SHORT_READ_1) {
1764                         DSSDBG("\tDCS short response, 1 byte: %#x\n",
1765                                         FLD_GET(val, 23, 8));
1766                 } else if (dt == DSI_DT_RX_SHORT_READ_2) {
1767                         DSSDBG("\tDCS short response, 2 byte: %#x\n",
1768                                         FLD_GET(val, 23, 8));
1769                 } else if (dt == DSI_DT_RX_DCS_LONG_READ) {
1770                         DSSDBG("\tDCS long response, len %d\n",
1771                                         FLD_GET(val, 23, 8));
1772                         dsi_vc_flush_long_data(channel);
1773                 } else {
1774                         DSSERR("\tunknown datatype 0x%02x\n", dt);
1775                 }
1776         }
1777         return 0;
1778 }
1779
1780 static int dsi_vc_send_bta(int channel)
1781 {
1782         unsigned long tmo;
1783
1784         /*DSSDBG("dsi_vc_send_bta_sync %d\n", channel); */
1785
1786         if (REG_GET(DSI_VC_CTRL(channel), 20, 20)) {    /* RX_FIFO_NOT_EMPTY */
1787                 DSSERR("rx fifo not empty when sending BTA, dumping data:\n");
1788                 dsi_vc_flush_receive_data(channel);
1789         }
1790
1791         REG_FLD_MOD(DSI_VC_CTRL(channel), 1, 6, 6); /* BTA_EN */
1792
1793         tmo = jiffies + msecs_to_jiffies(10);
1794         while (REG_GET(DSI_VC_CTRL(channel), 6, 6) == 1) {
1795                 if (time_after(jiffies, tmo)) {
1796                         DSSERR("Failed to send BTA\n");
1797                         return -EIO;
1798                 }
1799         }
1800
1801         return 0;
1802 }
1803
1804 static int dsi_vc_send_bta_sync(int channel)
1805 {
1806         int r = 0;
1807
1808         init_completion(&dsi.bta_completion);
1809
1810         dsi_vc_enable_bta_irq(channel);
1811
1812         r = dsi_vc_send_bta(channel);
1813         if (r)
1814                 goto err;
1815
1816         if (wait_for_completion_timeout(&dsi.bta_completion,
1817                                 msecs_to_jiffies(500)) == 0) {
1818                 DSSERR("Failed to receive BTA\n");
1819                 r = -EIO;
1820                 goto err;
1821         }
1822 err:
1823         dsi_vc_disable_bta_irq(channel);
1824
1825         return r;
1826 }
1827
1828 static inline void dsi_vc_write_long_header(int channel, u8 data_type,
1829                 u16 len, u8 ecc)
1830 {
1831         u32 val;
1832         u8 data_id;
1833
1834         /*data_id = data_type | channel << 6; */
1835         data_id = data_type | dsi.vc[channel].dest_per << 6;
1836
1837         val = FLD_VAL(data_id, 7, 0) | FLD_VAL(len, 23, 8) |
1838                 FLD_VAL(ecc, 31, 24);
1839
1840         dsi_write_reg(DSI_VC_LONG_PACKET_HEADER(channel), val);
1841 }
1842
1843 static inline void dsi_vc_write_long_payload(int channel,
1844                 u8 b1, u8 b2, u8 b3, u8 b4)
1845 {
1846         u32 val;
1847
1848         val = b4 << 24 | b3 << 16 | b2 << 8  | b1 << 0;
1849
1850 /*      DSSDBG("\twriting %02x, %02x, %02x, %02x (%#010x)\n",
1851                         b1, b2, b3, b4, val); */
1852
1853         dsi_write_reg(DSI_VC_LONG_PACKET_PAYLOAD(channel), val);
1854 }
1855
1856 static int dsi_vc_send_long(int channel, u8 data_type, u8 *data, u16 len,
1857                 u8 ecc)
1858 {
1859         /*u32 val; */
1860         int i;
1861         u8 *p;
1862         int r = 0;
1863         u8 b1, b2, b3, b4;
1864
1865         if (dsi.debug_write)
1866                 DSSDBG("dsi_vc_send_long, %d bytes\n", len);
1867
1868         /* len + header */
1869         if (dsi.vc[channel].fifo_size * 32 * 4 < len + 4) {
1870                 DSSERR("unable to send long packet: packet too long.\n");
1871                 return -EINVAL;
1872         }
1873
1874         dsi_vc_write_long_header(channel, data_type, len, ecc);
1875
1876         /*dsi_vc_print_status(0); */
1877
1878         p = data;
1879         for (i = 0; i < len >> 2; i++) {
1880                 if (dsi.debug_write)
1881                         DSSDBG("\tsending full packet %d\n", i);
1882                 /*dsi_vc_print_status(0); */
1883
1884                 b1 = *p++;
1885                 b2 = *p++;
1886                 b3 = *p++;
1887                 b4 = *p++;
1888
1889                 dsi_vc_write_long_payload(channel, b1, b2, b3, b4);
1890         }
1891
1892         i = len % 4;
1893         if (i) {
1894                 b1 = 0; b2 = 0; b3 = 0;
1895
1896                 if (dsi.debug_write)
1897                         DSSDBG("\tsending remainder bytes %d\n", i);
1898
1899                 switch (i) {
1900                 case 3:
1901                         b1 = *p++;
1902                         b2 = *p++;
1903                         b3 = *p++;
1904                         break;
1905                 case 2:
1906                         b1 = *p++;
1907                         b2 = *p++;
1908                         break;
1909                 case 1:
1910                         b1 = *p++;
1911                         break;
1912                 }
1913
1914                 dsi_vc_write_long_payload(channel, b1, b2, b3, 0);
1915         }
1916
1917         return r;
1918 }
1919
1920 static int dsi_vc_send_short(int channel, u8 data_type, u16 data, u8 ecc)
1921 {
1922         u32 r;
1923         u8 data_id;
1924
1925         if (dsi.debug_write)
1926                 DSSDBG("dsi_vc_send_short(ch%d, dt %#x, b1 %#x, b2 %#x)\n",
1927                                 channel,
1928                                 data_type, data & 0xff, (data >> 8) & 0xff);
1929
1930         if (FLD_GET(dsi_read_reg(DSI_VC_CTRL(channel)), 16, 16)) {
1931                 DSSERR("ERROR FIFO FULL, aborting transfer\n");
1932                 return -EINVAL;
1933         }
1934
1935         data_id = data_type | channel << 6;
1936
1937         r = (data_id << 0) | (data << 8) | (ecc << 24);
1938
1939         dsi_write_reg(DSI_VC_SHORT_PACKET_HEADER(channel), r);
1940
1941         return 0;
1942 }
1943
1944 int dsi_vc_send_null(int channel)
1945 {
1946         u8 nullpkg[] = {0, 0, 0, 0};
1947         return dsi_vc_send_long(0, DSI_DT_NULL_PACKET, nullpkg, 4, 0);
1948 }
1949 EXPORT_SYMBOL(dsi_vc_send_null);
1950
1951 int dsi_vc_dcs_write_nosync(int channel, u8 *data, int len)
1952 {
1953         int r;
1954
1955         BUG_ON(len == 0);
1956
1957         if (len == 1) {
1958                 r = dsi_vc_send_short(channel, DSI_DT_DCS_SHORT_WRITE_0,
1959                                 data[0], 0);
1960         } else if (len == 2) {
1961                 r = dsi_vc_send_short(channel, DSI_DT_DCS_SHORT_WRITE_1,
1962                                 data[0] | (data[1] << 8), 0);
1963         } else {
1964                 /* 0x39 = DCS Long Write */
1965                 r = dsi_vc_send_long(channel, DSI_DT_DCS_LONG_WRITE,
1966                                 data, len, 0);
1967         }
1968
1969         return r;
1970 }
1971 EXPORT_SYMBOL(dsi_vc_dcs_write_nosync);
1972
1973 int dsi_vc_dcs_write(int channel, u8 *data, int len)
1974 {
1975         int r;
1976
1977         r = dsi_vc_dcs_write_nosync(channel, data, len);
1978         if (r)
1979                 return r;
1980
1981         /* Some devices need time to process the msg in low power mode.
1982            This also makes the write synchronous, and checks that
1983            the peripheral is still alive */
1984         r = dsi_vc_send_bta_sync(channel);
1985
1986         return r;
1987 }
1988 EXPORT_SYMBOL(dsi_vc_dcs_write);
1989
1990 int dsi_vc_dcs_read(int channel, u8 dcs_cmd, u8 *buf, int buflen)
1991 {
1992         u32 val;
1993         u8 dt;
1994         int r;
1995
1996         if (dsi.debug_read)
1997                 DSSDBG("dsi_vc_dcs_read\n");
1998
1999         r = dsi_vc_send_short(channel, DSI_DT_DCS_READ, dcs_cmd, 0);
2000         if (r)
2001                 return r;
2002
2003         r = dsi_vc_send_bta_sync(channel);
2004         if (r)
2005                 return r;
2006
2007         if (REG_GET(DSI_VC_CTRL(channel), 20, 20) == 0) {       /* RX_FIFO_NOT_EMPTY */
2008                 DSSERR("RX fifo empty when trying to read.\n");
2009                 return -EIO;
2010         }
2011
2012         val = dsi_read_reg(DSI_VC_SHORT_PACKET_HEADER(channel));
2013         if (dsi.debug_read)
2014                 DSSDBG("\theader: %08x\n", val);
2015         dt = FLD_GET(val, 5, 0);
2016         if (dt == DSI_DT_RX_ACK_WITH_ERR) {
2017                 u16 err = FLD_GET(val, 23, 8);
2018                 dsi_show_rx_ack_with_err(err);
2019                 return -1;
2020
2021         } else if (dt == DSI_DT_RX_SHORT_READ_1) {
2022                 u8 data = FLD_GET(val, 15, 8);
2023                 if (dsi.debug_read)
2024                         DSSDBG("\tDCS short response, 1 byte: %02x\n", data);
2025
2026                 if (buflen < 1)
2027                         return -1;
2028
2029                 buf[0] = data;
2030
2031                 return 1;
2032         } else if (dt == DSI_DT_RX_SHORT_READ_2) {
2033                 u16 data = FLD_GET(val, 23, 8);
2034                 if (dsi.debug_read)
2035                         DSSDBG("\tDCS short response, 2 byte: %04x\n", data);
2036
2037                 if (buflen < 2)
2038                         return -1;
2039
2040                 buf[0] = data & 0xff;
2041                 buf[1] = (data >> 8) & 0xff;
2042
2043                 return 2;
2044         } else if (dt == DSI_DT_RX_DCS_LONG_READ) {
2045                 int w;
2046                 int len = FLD_GET(val, 23, 8);
2047                 if (dsi.debug_read)
2048                         DSSDBG("\tDCS long response, len %d\n", len);
2049
2050                 if (len > buflen)
2051                         return -1;
2052
2053                 /* two byte checksum ends the packet, not included in len */
2054                 for (w = 0; w < len + 2;) {
2055                         int b;
2056                         val = dsi_read_reg(DSI_VC_SHORT_PACKET_HEADER(channel));
2057                         if (dsi.debug_read)
2058                                 DSSDBG("\t\t%02x %02x %02x %02x\n",
2059                                                 (val >> 0) & 0xff,
2060                                                 (val >> 8) & 0xff,
2061                                                 (val >> 16) & 0xff,
2062                                                 (val >> 24) & 0xff);
2063
2064                         for (b = 0; b < 4; ++b) {
2065                                 if (w < len)
2066                                         buf[w] = (val >> (b * 8)) & 0xff;
2067                                 /* we discard the 2 byte checksum */
2068                                 ++w;
2069                         }
2070                 }
2071
2072                 return len;
2073
2074         } else {
2075                 DSSERR("\tunknown datatype 0x%02x\n", dt);
2076                 return -1;
2077         }
2078 }
2079 EXPORT_SYMBOL(dsi_vc_dcs_read);
2080
2081
2082 int dsi_vc_set_max_rx_packet_size(int channel, u16 len)
2083 {
2084         return dsi_vc_send_short(channel, DSI_DT_SET_MAX_RET_PKG_SIZE,
2085                         len, 0);
2086 }
2087 EXPORT_SYMBOL(dsi_vc_set_max_rx_packet_size);
2088
2089
2090 static int dsi_set_lp_rx_timeout(int ns, int x4, int x16)
2091 {
2092         u32 r;
2093         unsigned long fck;
2094         int ticks;
2095
2096         /* ticks in DSI_FCK */
2097
2098         fck = dsi_fclk_rate();
2099         ticks = (fck / 1000 / 1000) * ns / 1000;
2100
2101         if (ticks > 0x1fff) {
2102                 DSSERR("LP_TX_TO too high\n");
2103                 return -EINVAL;
2104         }
2105
2106         r = dsi_read_reg(DSI_TIMING2);
2107         r = FLD_MOD(r, 1, 15, 15);      /* LP_RX_TO */
2108         r = FLD_MOD(r, x16, 14, 14);    /* LP_RX_TO_X16 */
2109         r = FLD_MOD(r, x4, 13, 13);     /* LP_RX_TO_X4 */
2110         r = FLD_MOD(r, ticks, 12, 0);   /* LP_RX_COUNTER */
2111         dsi_write_reg(DSI_TIMING2, r);
2112
2113         DSSDBG("LP_RX_TO %ld ns (%#x ticks)\n",
2114                         (ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1) * 1000) /
2115                         (fck / 1000 / 1000),
2116                         ticks);
2117
2118         return 0;
2119 }
2120
2121 static int dsi_set_ta_timeout(int ns, int x8, int x16)
2122 {
2123         u32 r;
2124         unsigned long fck;
2125         int ticks;
2126
2127         /* ticks in DSI_FCK */
2128
2129         fck = dsi_fclk_rate();
2130         ticks = (fck / 1000 / 1000) * ns / 1000;
2131
2132         if (ticks > 0x1fff) {
2133                 DSSERR("TA_TO too high\n");
2134                 return -EINVAL;
2135         }
2136
2137         r = dsi_read_reg(DSI_TIMING1);
2138         r = FLD_MOD(r, 1, 31, 31);      /* TA_TO */
2139         r = FLD_MOD(r, x16, 30, 30);    /* TA_TO_X16 */
2140         r = FLD_MOD(r, x8, 29, 29);     /* TA_TO_X8 */
2141         r = FLD_MOD(r, ticks, 28, 16);  /* TA_TO_COUNTER */
2142         dsi_write_reg(DSI_TIMING1, r);
2143
2144         DSSDBG("TA_TO %ld ns (%#x ticks)\n",
2145                         (ticks * (x16 ? 16 : 1) * (x8 ? 8 : 1) * 1000) /
2146                         (fck / 1000 / 1000),
2147                         ticks);
2148
2149         return 0;
2150 }
2151
2152 static int dsi_set_stop_state_counter(int ns, int x4, int x16)
2153 {
2154         u32 r;
2155         unsigned long fck;
2156         int ticks;
2157
2158         /* ticks in DSI_FCK */
2159
2160         fck = dsi_fclk_rate();
2161         ticks = (fck / 1000 / 1000) * ns / 1000;
2162
2163         if (ticks > 0x1fff) {
2164                 DSSERR("STOP_STATE_COUNTER_IO too high\n");
2165                 return -EINVAL;
2166         }
2167
2168         r = dsi_read_reg(DSI_TIMING1);
2169         r = FLD_MOD(r, 1, 15, 15);      /* FORCE_TX_STOP_MODE_IO */
2170         r = FLD_MOD(r, x16, 14, 14);    /* STOP_STATE_X16_IO */
2171         r = FLD_MOD(r, x4, 13, 13);     /* STOP_STATE_X4_IO */
2172         r = FLD_MOD(r, ticks, 12, 0);   /* STOP_STATE_COUNTER_IO */
2173         dsi_write_reg(DSI_TIMING1, r);
2174
2175         DSSDBG("STOP_STATE_COUNTER %ld ns (%#x ticks)\n",
2176                         (ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1) * 1000) /
2177                         (fck / 1000 / 1000),
2178                         ticks);
2179
2180         return 0;
2181 }
2182
2183 static int dsi_set_hs_tx_timeout(int ns, int x4, int x16)
2184 {
2185         u32 r;
2186         unsigned long fck;
2187         int ticks;
2188
2189         /* ticks in TxByteClkHS */
2190
2191         fck = dsi.ddr_clk / 4;
2192         ticks = (fck / 1000 / 1000) * ns / 1000;
2193
2194         if (ticks > 0x1fff) {
2195                 DSSERR("HS_TX_TO too high\n");
2196                 return -EINVAL;
2197         }
2198
2199         r = dsi_read_reg(DSI_TIMING2);
2200         r = FLD_MOD(r, 1, 31, 31);      /* HS_TX_TO */
2201         r = FLD_MOD(r, x16, 30, 30);    /* HS_TX_TO_X16 */
2202         r = FLD_MOD(r, x4, 29, 29);     /* HS_TX_TO_X8 (4 really) */
2203         r = FLD_MOD(r, ticks, 28, 16);  /* HS_TX_TO_COUNTER */
2204         dsi_write_reg(DSI_TIMING2, r);
2205
2206         DSSDBG("HS_TX_TO %ld ns (%#x ticks)\n",
2207                         (ticks * (x16 ? 16 : 1) * (x4 ? 4 : 1) * 1000) /
2208                         (fck / 1000 / 1000),
2209                         ticks);
2210
2211         return 0;
2212 }
2213 static int dsi_proto_config(struct omap_display *display)
2214 {
2215         u32 r;
2216         int buswidth = 0;
2217
2218         dsi_config_tx_fifo(DSI_FIFO_SIZE_128,
2219                         DSI_FIFO_SIZE_0,
2220                         DSI_FIFO_SIZE_0,
2221                         DSI_FIFO_SIZE_0);
2222
2223         dsi_config_rx_fifo(DSI_FIFO_SIZE_128,
2224                         DSI_FIFO_SIZE_0,
2225                         DSI_FIFO_SIZE_0,
2226                         DSI_FIFO_SIZE_0);
2227
2228         /* XXX what values for the timeouts? */
2229         dsi_set_stop_state_counter(1000, 0, 0);
2230
2231         dsi_set_ta_timeout(50000, 1, 1);
2232
2233         /* 3000ns * 16 */
2234         dsi_set_lp_rx_timeout(3000, 0, 1);
2235
2236         /* 10000ns * 4 */
2237         dsi_set_hs_tx_timeout(10000, 1, 0);
2238
2239         switch (display->ctrl->pixel_size) {
2240         case 16:
2241                 buswidth = 0;
2242                 break;
2243         case 18:
2244                 buswidth = 1;
2245                 break;
2246         case 24:
2247                 buswidth = 2;
2248                 break;
2249         default:
2250                 BUG();
2251         }
2252
2253         r = dsi_read_reg(DSI_CTRL);
2254         r = FLD_MOD(r, 1, 1, 1);        /* CS_RX_EN */
2255         r = FLD_MOD(r, 1, 2, 2);        /* ECC_RX_EN */
2256         r = FLD_MOD(r, 1, 3, 3);        /* TX_FIFO_ARBITRATION */
2257         /* XXX what should the ratio be */
2258         r = FLD_MOD(r, 0, 4, 4);        /* VP_CLK_RATIO, VP_PCLK = VP_CLK/2 */
2259         r = FLD_MOD(r, buswidth, 7, 6); /* VP_DATA_BUS_WIDTH */
2260         r = FLD_MOD(r, 0, 8, 8);        /* VP_CLK_POL */
2261         r = FLD_MOD(r, 2, 13, 12);      /* LINE_BUFFER, 2 lines */
2262         r = FLD_MOD(r, 1, 14, 14);      /* TRIGGER_RESET_MODE */
2263         r = FLD_MOD(r, 1, 19, 19);      /* EOT_ENABLE */
2264         r = FLD_MOD(r, 1, 24, 24);      /* DCS_CMD_ENABLE */
2265         r = FLD_MOD(r, 0, 25, 25);      /* DCS_CMD_CODE, 1=start, 0=continue */
2266
2267         dsi_write_reg(DSI_CTRL, r);
2268
2269         /* we configure vc0 for L4 communication, and
2270          * vc1 for dispc */
2271         dsi_vc_config(0);
2272         dsi_vc_config_vp(1);
2273
2274         /* set all vc targets to peripheral 0 */
2275         dsi.vc[0].dest_per = 0;
2276         dsi.vc[1].dest_per = 0;
2277         dsi.vc[2].dest_per = 0;
2278         dsi.vc[3].dest_per = 0;
2279
2280         return 0;
2281 }
2282
2283 static void dsi_proto_timings(void)
2284 {
2285         int tlpx_half, tclk_zero, tclk_prepare, tclk_trail;
2286         int tclk_pre, tclk_post;
2287         int ddr_clk_pre, ddr_clk_post;
2288         u32 r;
2289
2290         r = dsi_read_reg(DSI_DSIPHY_CFG1);
2291         tlpx_half = FLD_GET(r, 22, 16);
2292         tclk_trail = FLD_GET(r, 15, 8);
2293         tclk_zero = FLD_GET(r, 7, 0);
2294
2295         r = dsi_read_reg(DSI_DSIPHY_CFG2);
2296         tclk_prepare = FLD_GET(r, 7, 0);
2297
2298         /* min 8*UI */
2299         tclk_pre = 20;
2300         /* min 60ns + 52*UI */
2301         tclk_post = ns2ddr(60) + 26;
2302
2303         ddr_clk_pre = (tclk_pre + tlpx_half*2 + tclk_zero + tclk_prepare) / 4;
2304         ddr_clk_post = (tclk_post + tclk_trail) / 4;
2305
2306         r = dsi_read_reg(DSI_CLK_TIMING);
2307         r = FLD_MOD(r, ddr_clk_pre, 15, 8);
2308         r = FLD_MOD(r, ddr_clk_post, 7, 0);
2309         dsi_write_reg(DSI_CLK_TIMING, r);
2310
2311         DSSDBG("ddr_clk_pre %d, ddr_clk_post %d\n",
2312                         ddr_clk_pre,
2313                         ddr_clk_post);
2314 }
2315
2316
2317 #define DSI_DECL_VARS \
2318         int __dsi_cb = 0; u32 __dsi_cv = 0;
2319
2320 #define DSI_FLUSH(ch) \
2321         if (__dsi_cb > 0) { \
2322                 /*DSSDBG("sending long packet %#010x\n", __dsi_cv);*/ \
2323                 dsi_write_reg(DSI_VC_LONG_PACKET_PAYLOAD(ch), __dsi_cv); \
2324                 __dsi_cb = __dsi_cv = 0; \
2325         }
2326
2327 #define DSI_PUSH(ch, data) \
2328         do { \
2329                 __dsi_cv |= (data) << (__dsi_cb * 8); \
2330                 /*DSSDBG("cv = %#010x, cb = %d\n", __dsi_cv, __dsi_cb);*/ \
2331                 if (++__dsi_cb > 3) \
2332                         DSI_FLUSH(ch); \
2333         } while (0)
2334
2335 static int dsi_update_screen_l4(struct omap_display *display,
2336                         int x, int y, int w, int h)
2337 {
2338         /* Note: supports only 24bit colors in 32bit container */
2339         int first = 1;
2340         int fifo_stalls = 0;
2341         int max_dsi_packet_size;
2342         int max_data_per_packet;
2343         int max_pixels_per_packet;
2344         int pixels_left;
2345         int bytespp = 3;
2346         int scr_width;
2347         u32 __iomem *data;
2348         int start_offset;
2349         int horiz_inc;
2350         int current_x;
2351         struct omap_overlay *ovl;
2352
2353         debug_irq = 0;
2354
2355         DSSDBG("dsi_update_screen_l4 (%d,%d %dx%d)\n",
2356                         x, y, w, h);
2357
2358         ovl = display->manager->overlays[0];
2359
2360         if (ovl->info.color_mode != OMAP_DSS_COLOR_RGB24U)
2361                 return -EINVAL;
2362
2363         if (display->ctrl->pixel_size != 24)
2364                 return -EINVAL;
2365
2366         scr_width = ovl->info.screen_width;
2367         data = ovl->info.vaddr;
2368
2369         start_offset = scr_width * y + x;
2370         horiz_inc = scr_width - w;
2371         current_x = x;
2372
2373         /* We need header(4) + DCSCMD(1) + pixels(numpix*bytespp) bytes
2374          * in fifo */
2375
2376         /* When using CPU, max long packet size is TX buffer size */
2377         max_dsi_packet_size = dsi.vc[0].fifo_size * 32 * 4;
2378
2379         /* we seem to get better perf if we divide the tx fifo to half,
2380            and while the other half is being sent, we fill the other half
2381            max_dsi_packet_size /= 2; */
2382
2383         max_data_per_packet = max_dsi_packet_size - 4 - 1;
2384
2385         max_pixels_per_packet = max_data_per_packet / bytespp;
2386
2387         DSSDBG("max_pixels_per_packet %d\n", max_pixels_per_packet);
2388
2389         display->ctrl->setup_update(display, x, y, w, h);
2390
2391         pixels_left = w * h;
2392
2393         DSSDBG("total pixels %d\n", pixels_left);
2394
2395         data += start_offset;
2396
2397 #ifdef DEBUG
2398         dsi.update_region.x = x;
2399         dsi.update_region.y = y;
2400         dsi.update_region.w = w;
2401         dsi.update_region.h = h;
2402         dsi.update_region.bytespp = bytespp;
2403 #endif
2404
2405         perf_mark_start();
2406
2407         while (pixels_left > 0) {
2408                 /* 0x2c = write_memory_start */
2409                 /* 0x3c = write_memory_continue */
2410                 u8 dcs_cmd = first ? 0x2c : 0x3c;
2411                 int pixels;
2412                 DSI_DECL_VARS;
2413                 first = 0;
2414
2415 #if 1
2416                 /* using fifo not empty */
2417                 /* TX_FIFO_NOT_EMPTY */
2418                 while (FLD_GET(dsi_read_reg(DSI_VC_CTRL(0)), 5, 5)) {
2419                         udelay(1);
2420                         fifo_stalls++;
2421                         if (fifo_stalls > 0xfffff) {
2422                                 DSSERR("fifo stalls overflow, pixels left %d\n",
2423                                                 pixels_left);
2424                                 dsi_if_enable(0);
2425                                 return -EIO;
2426                         }
2427                 }
2428 #elif 1
2429                 /* using fifo emptiness */
2430                 while ((REG_GET(DSI_TX_FIFO_VC_EMPTINESS, 7, 0)+1)*4 <
2431                                 max_dsi_packet_size) {
2432                         fifo_stalls++;
2433                         if (fifo_stalls > 0xfffff) {
2434                                 DSSERR("fifo stalls overflow, pixels left %d\n",
2435                                                pixels_left);
2436                                 dsi_if_enable(0);
2437                                 return -EIO;
2438                         }
2439                 }
2440 #else
2441                 while ((REG_GET(DSI_TX_FIFO_VC_EMPTINESS, 7, 0)+1)*4 == 0) {
2442                         fifo_stalls++;
2443                         if (fifo_stalls > 0xfffff) {
2444                                 DSSERR("fifo stalls overflow, pixels left %d\n",
2445                                                pixels_left);
2446                                 dsi_if_enable(0);
2447                                 return -EIO;
2448                         }
2449                 }
2450 #endif
2451                 pixels = min(max_pixels_per_packet, pixels_left);
2452
2453                 pixels_left -= pixels;
2454
2455                 dsi_vc_write_long_header(0, DSI_DT_DCS_LONG_WRITE,
2456                                 1 + pixels * bytespp, 0);
2457
2458                 DSI_PUSH(0, dcs_cmd);
2459
2460                 while (pixels-- > 0) {
2461                         u32 pix = __raw_readl(data++);
2462
2463                         DSI_PUSH(0, (pix >> 16) & 0xff);
2464                         DSI_PUSH(0, (pix >> 8) & 0xff);
2465                         DSI_PUSH(0, (pix >> 0) & 0xff);
2466
2467                         current_x++;
2468                         if (current_x == x+w) {
2469                                 current_x = x;
2470                                 data += horiz_inc;
2471                         }
2472                 }
2473
2474                 DSI_FLUSH(0);
2475         }
2476
2477         perf_show("L4");
2478
2479         return 0;
2480 }
2481
2482 #if 0
2483 static void dsi_clear_screen_l4(struct omap_display *display,
2484                         int x, int y, int w, int h)
2485 {
2486         int first = 1;
2487         int fifo_stalls = 0;
2488         int max_dsi_packet_size;
2489         int max_data_per_packet;
2490         int max_pixels_per_packet;
2491         int pixels_left;
2492         int bytespp = 3;
2493         int pixnum;
2494
2495         debug_irq = 0;
2496
2497         DSSDBG("dsi_clear_screen_l4 (%d,%d %dx%d)\n",
2498                         x, y, w, h);
2499
2500         if (display->ctrl->bpp != 24)
2501                 return -EINVAL;
2502
2503         /* We need header(4) + DCSCMD(1) + pixels(numpix*bytespp)
2504          * bytes in fifo */
2505
2506         /* When using CPU, max long packet size is TX buffer size */
2507         max_dsi_packet_size = dsi.vc[0].fifo_size * 32 * 4;
2508
2509         max_data_per_packet = max_dsi_packet_size - 4 - 1;
2510
2511         max_pixels_per_packet = max_data_per_packet / bytespp;
2512
2513         enable_clocks(1);
2514
2515         display->ctrl->setup_update(display, x, y, w, h);
2516
2517         pixels_left = w * h;
2518
2519         dsi.update_region.x = x;
2520         dsi.update_region.y = y;
2521         dsi.update_region.w = w;
2522         dsi.update_region.h = h;
2523         dsi.update_region.bytespp = bytespp;
2524
2525         start_measuring();
2526
2527         pixnum = 0;
2528
2529         while (pixels_left > 0) {
2530                 /* 0x2c = write_memory_start */
2531                 /* 0x3c = write_memory_continue */
2532                 u8 dcs_cmd = first ? 0x2c : 0x3c;
2533                 int pixels;
2534                 DSI_DECL_VARS;
2535                 first = 0;
2536
2537                 /* TX_FIFO_NOT_EMPTY */
2538                 while (FLD_GET(dsi_read_reg(DSI_VC_CTRL(0)), 5, 5)) {
2539                         fifo_stalls++;
2540                         if (fifo_stalls > 0xfffff) {
2541                                 DSSERR("fifo stalls overflow\n");
2542                                 dsi_if_enable(0);
2543                                 enable_clocks(0);
2544                                 return;
2545                         }
2546                 }
2547
2548                 pixels = min(max_pixels_per_packet, pixels_left);
2549
2550                 pixels_left -= pixels;
2551
2552                 dsi_vc_write_long_header(0, DSI_DT_DCS_LONG_WRITE,
2553                                 1 + pixels * bytespp, 0);
2554
2555                 DSI_PUSH(0, dcs_cmd);
2556
2557                 while (pixels-- > 0) {
2558                         u32 pix;
2559
2560                         pix = 0x000000;
2561
2562                         DSI_PUSH(0, (pix >> 16) & 0xff);
2563                         DSI_PUSH(0, (pix >> 8) & 0xff);
2564                         DSI_PUSH(0, (pix >> 0) & 0xff);
2565                 }
2566
2567                 DSI_FLUSH(0);
2568         }
2569
2570         enable_clocks(0);
2571
2572         end_measuring("L4 CLEAR");
2573 }
2574 #endif
2575
2576 static void dsi_setup_update_dispc(struct omap_display *display,
2577                         u16 x, u16 y, u16 w, u16 h)
2578 {
2579         DSSDBG("dsi_setup_update_dispc(%d,%d %dx%d)\n",
2580                         x, y, w, h);
2581
2582 #ifdef DEBUG
2583         dsi.update_region.x = x;
2584         dsi.update_region.y = y;
2585         dsi.update_region.w = w;
2586         dsi.update_region.h = h;
2587         dsi.update_region.bytespp = 3; // XXX
2588 #endif
2589
2590         dispc_setup_partial_planes(display, &x, &y, &w, &h);
2591
2592         dispc_set_lcd_size(w, h);
2593 }
2594
2595 static void dsi_setup_autoupdate_dispc(struct omap_display *display)
2596 {
2597         u16 w, h;
2598
2599         display->get_resolution(display, &w, &h);
2600
2601 #ifdef DEBUG
2602         dsi.update_region.x = 0;
2603         dsi.update_region.y = 0;
2604         dsi.update_region.w = w;
2605         dsi.update_region.h = h;
2606         dsi.update_region.bytespp = 3; // XXX
2607 #endif
2608
2609         /* the overlay settings may not have been applied, if we were in manual
2610          * mode earlier, so do it here */
2611         display->manager->apply(display->manager);
2612
2613         dispc_set_lcd_size(w, h);
2614
2615         dsi.autoupdate_setup = 0;
2616 }
2617
2618 static void dsi_update_screen_dispc(struct omap_display *display,
2619                 u16 x, u16 y, u16 w, u16 h)
2620 {
2621         int bytespp = 3;
2622         int total_len;
2623         int line_packet_len;
2624         u32 l;
2625
2626         if (dsi.update_mode == OMAP_DSS_UPDATE_MANUAL)
2627                 DSSDBG("dsi_update_screen_dispc(%d,%d %dx%d)\n",
2628                                 x, y, w, h);
2629
2630         /* TODO: one packet could be longer, I think? Max is the line buffer */
2631         line_packet_len = w * bytespp + 1;      /* 1 byte for DCS cmd */
2632         total_len = line_packet_len * h;
2633
2634         display->ctrl->setup_update(display, x, y, w, h);
2635
2636         if (0)
2637                 dsi_vc_print_status(1);
2638
2639         perf_mark_start();
2640
2641         l = FLD_VAL(total_len, 23, 0); /* TE_SIZE */
2642         dsi_write_reg(DSI_VC_TE(1), l);
2643
2644         dsi_vc_write_long_header(1, DSI_DT_DCS_LONG_WRITE, line_packet_len, 0);
2645
2646         if (dsi.use_te)
2647                 l = FLD_MOD(l, 1, 30, 30); /* TE_EN */
2648         else
2649                 l = FLD_MOD(l, 1, 31, 31); /* TE_START */
2650         dsi_write_reg(DSI_VC_TE(1), l);
2651
2652         dispc_enable_lcd_out(1);
2653
2654         if (dsi.use_te)
2655                 dsi_vc_send_bta(1);
2656 }
2657
2658 static void framedone_callback(void *data, u32 mask)
2659 {
2660         if (dsi.framedone_scheduled) {
2661                 DSSERR("Framedone already scheduled. Bogus FRAMEDONE IRQ?\n");
2662                 return;
2663         }
2664
2665         dsi.framedone_scheduled = 1;
2666
2667         /* We get FRAMEDONE when DISPC has finished sending pixels and turns
2668          * itself off. However, DSI still has the pixels in its buffers, and
2669          * is sending the data. Thus we have to wait until we can do a new
2670          * transfer or turn the clocks off. We do that in a separate work
2671          * func. */
2672         queue_work(dsi.workqueue, &dsi.framedone_work);
2673 }
2674
2675 static void framedone_worker(struct work_struct *work)
2676 {
2677         u32 l;
2678         unsigned long tmo;
2679         int i = 0;
2680
2681         l = REG_GET(DSI_VC_TE(1), 23, 0); /* TE_SIZE */
2682
2683         /* There shouldn't be much stuff in DSI buffers, if any, so we'll
2684          * just busyloop */
2685         if (l > 0) {
2686                 tmo = jiffies + msecs_to_jiffies(50);
2687                 while (REG_GET(DSI_VC_TE(1), 23, 0) > 0) { /* TE_SIZE */
2688                         i++;
2689                         if (time_after(jiffies, tmo)) {
2690                                 DSSERR("timeout waiting TE_SIZE to zero\n");
2691                                 break;
2692                         }
2693                         cpu_relax();
2694                 }
2695         }
2696
2697         if (REG_GET(DSI_VC_TE(1), 30, 30))
2698                 DSSERR("TE_EN not zero\n");
2699
2700         if (REG_GET(DSI_VC_TE(1), 31, 31))
2701                 DSSERR("TE_START not zero\n");
2702
2703         perf_show("DISPC");
2704
2705         if (dsi.update_mode == OMAP_DSS_UPDATE_MANUAL)
2706                 DSSDBG("FRAMEDONE\n");
2707
2708 #if 0
2709         if (l)
2710                 DSSWARN("FRAMEDONE irq too early, %d bytes, %d loops\n", l, i);
2711 #else
2712         if (l > 1024*3)
2713                 DSSWARN("FRAMEDONE irq too early, %d bytes, %d loops\n", l, i);
2714 #endif
2715
2716 #ifdef CONFIG_OMAP2_DSS_FAKE_VSYNC
2717         dispc_fake_vsync_irq();
2718 #endif
2719         dsi.framedone_scheduled = 0;
2720
2721         /* XXX check that fifo is not full. otherwise we would sleep and never
2722          * get to process_cmd_fifo below */
2723         /* We check for target_update_mode, not update_mode. No reason to push
2724          * new updates if we're turning auto update off */
2725         if (dsi.target_update_mode == OMAP_DSS_UPDATE_AUTO)
2726                 dsi_push_autoupdate(dsi.vc[1].display);
2727
2728         atomic_set(&dsi.cmd_pending, 0);
2729         dsi_process_cmd_fifo(NULL);
2730 }
2731
2732 static void dsi_start_auto_update(struct omap_display *display)
2733 {
2734         DSSDBG("starting auto update\n");
2735
2736         dsi.autoupdate_setup = 1;
2737
2738         dsi_push_autoupdate(display);
2739 }
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753 /* FIFO functions */
2754
2755 static void dsi_signal_fifo_waiters(void)
2756 {
2757         if (atomic_read(&dsi.cmd_fifo_full) > 0) {
2758                 DSSDBG("SIGNALING: Fifo not full for waiter!\n");
2759                 complete(&dsi.cmd_done);
2760                 atomic_dec(&dsi.cmd_fifo_full);
2761         }
2762 }
2763
2764 /* returns 1 for async op, and 0 for sync op */
2765 static int dsi_do_update(struct omap_display *display,
2766                 struct dsi_cmd_update *upd)
2767 {
2768         int r;
2769         u16 x = upd->x, y = upd->y, w = upd->w, h = upd->h;
2770         u16 dw, dh;
2771
2772         if (dsi.update_mode == OMAP_DSS_UPDATE_DISABLED)
2773                 return 0;
2774
2775         if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
2776                 return 0;
2777
2778         display->get_resolution(display, &dw, &dh);
2779         if  (x > dw || y > dh)
2780                 return 0;
2781
2782         if (x + w > dw)
2783                 w = dw - x;
2784
2785         if (y + h > dh)
2786                 h = dh - y;
2787
2788         DSSDBGF("%d,%d %dx%d", x, y, w, h);
2789
2790         perf_mark_setup();
2791
2792         if (display->manager->caps & OMAP_DSS_OVL_MGR_CAP_DISPC) {
2793                 dsi_setup_update_dispc(display, x, y, w, h);
2794                 dsi_update_screen_dispc(display, x, y, w, h);
2795                 return 1;
2796         } else {
2797                 r = dsi_update_screen_l4(display, x, y, w, h);
2798                 if (r)
2799                         DSSERR("L4 update failed\n");
2800                 return 0;
2801         }
2802 }
2803
2804 /* returns 1 for async op, and 0 for sync op */
2805 static int dsi_do_autoupdate(struct omap_display *display)
2806 {
2807         int r;
2808         u16 w, h;
2809
2810         if (dsi.update_mode == OMAP_DSS_UPDATE_DISABLED)
2811                 return 0;
2812
2813         if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
2814                 return 0;
2815
2816         display->get_resolution(display, &w, &h);
2817
2818         perf_mark_setup();
2819
2820         if (display->manager->caps & OMAP_DSS_OVL_MGR_CAP_DISPC) {
2821                 if (dsi.autoupdate_setup)
2822                         dsi_setup_autoupdate_dispc(display);
2823                 dsi_update_screen_dispc(display, 0, 0, w, h);
2824                 return 1;
2825         } else {
2826                 r = dsi_update_screen_l4(display, 0, 0, w, h);
2827                 if (r)
2828                         DSSERR("L4 update failed\n");
2829                 return 0;
2830         }
2831 }
2832
2833 static void dsi_do_cmd_mem_read(struct omap_display *display,
2834                 struct dsi_cmd_mem_read *mem_read)
2835 {
2836         int r;
2837         r = display->ctrl->memory_read(display,
2838                         mem_read->buf,
2839                         mem_read->size,
2840                         mem_read->x,
2841                         mem_read->y,
2842                         mem_read->w,
2843                         mem_read->h);
2844
2845         *mem_read->ret_size = (size_t)r;
2846         complete(mem_read->completion);
2847 }
2848
2849 static void dsi_do_cmd_test(struct omap_display *display,
2850                 struct dsi_cmd_test *test)
2851 {
2852         int r = 0;
2853
2854         DSSDBGF("");
2855
2856         if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
2857                 return;
2858
2859         /* run test first in low speed mode */
2860         dsi_vc_enable_hs(0, 0);
2861
2862         if (display->ctrl->run_test) {
2863                 r = display->ctrl->run_test(display, test->test_num);
2864                 if (r)
2865                         goto end;
2866         }
2867
2868         if (display->panel->run_test) {
2869                 r = display->panel->run_test(display, test->test_num);
2870                 if (r)
2871                         goto end;
2872         }
2873
2874         /* then in high speed */
2875         dsi_vc_enable_hs(0, 1);
2876
2877         if (display->ctrl->run_test) {
2878                 r = display->ctrl->run_test(display, test->test_num);
2879                 if (r)
2880                         goto end;
2881         }
2882
2883         if (display->panel->run_test)
2884                 r = display->panel->run_test(display, test->test_num);
2885
2886 end:
2887         dsi_vc_enable_hs(0, 1);
2888
2889         *test->result = r;
2890         complete(test->completion);
2891
2892         DSSDBG("test end\n");
2893 }
2894
2895 static void dsi_do_cmd_set_te(struct omap_display *display, bool enable)
2896 {
2897         dsi.use_te = enable;
2898
2899         if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
2900                 return;
2901
2902         display->ctrl->enable_te(display, enable);
2903
2904         if (enable) {
2905                 /* disable LP_RX_TO, so that we can receive TE.
2906                  * Time to wait for TE is longer than the timer allows */
2907                 REG_FLD_MOD(DSI_TIMING2, 0, 15, 15); /* LP_RX_TO */
2908         } else {
2909                 REG_FLD_MOD(DSI_TIMING2, 1, 15, 15); /* LP_RX_TO */
2910         }
2911 }
2912
2913 static void dsi_do_cmd_set_update_mode(struct omap_display *display,
2914                enum omap_dss_update_mode mode)
2915 {
2916         dsi.update_mode = mode;
2917
2918         if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
2919                 return;
2920
2921         if (mode == OMAP_DSS_UPDATE_AUTO)
2922                 dsi_start_auto_update(display);
2923 }
2924
2925 static void dsi_process_cmd_fifo(struct work_struct *work)
2926 {
2927         int len;
2928         struct dsi_cmd_item p;
2929         unsigned long flags;
2930         struct omap_display *display;
2931         int exit = 0;
2932
2933         if (dsi.debug_process)
2934                 DSSDBGF("");
2935
2936         if (atomic_cmpxchg(&dsi.cmd_pending, 0, 1) == 1) {
2937                 if (dsi.debug_process)
2938                         DSSDBG("cmd pending, skip process\n");
2939                 return;
2940         }
2941
2942         while (!exit) {
2943                 spin_lock_irqsave(dsi.cmd_fifo->lock, flags);
2944
2945                 len = __kfifo_get(dsi.cmd_fifo, (unsigned char *)&p,
2946                                   sizeof(p));
2947                 if (len == 0) {
2948                         if (dsi.debug_process)
2949                                 DSSDBG("nothing more in fifo, atomic clear\n");
2950                         atomic_set(&dsi.cmd_pending, 0);
2951                         spin_unlock_irqrestore(dsi.cmd_fifo->lock, flags);
2952                         break;
2953                 }
2954
2955                 spin_unlock_irqrestore(dsi.cmd_fifo->lock, flags);
2956
2957                 BUG_ON(len != sizeof(p));
2958
2959                 display = p.display;
2960
2961                 if (dsi.debug_process)
2962                         DSSDBG("processing cmd %d\n", p.cmd);
2963
2964                 switch (p.cmd) {
2965                 case DSI_CMD_UPDATE:
2966                         if (dsi_do_update(display, &p.u.r)) {
2967                                 if (dsi.debug_process)
2968                                         DSSDBG("async update\n");
2969                                 exit = 1;
2970                         } else {
2971                                 if (dsi.debug_process)
2972                                         DSSDBG("sync update\n");
2973                         }
2974                         break;
2975
2976                 case DSI_CMD_AUTOUPDATE:
2977                         if (dsi_do_autoupdate(display)) {
2978                                 if (dsi.debug_process)
2979                                         DSSDBG("async autoupdate\n");
2980                                 exit = 1;
2981                         } else {
2982                                 if (dsi.debug_process)
2983                                         DSSDBG("sync autoupdate\n");
2984                         }
2985                         break;
2986
2987                 case DSI_CMD_SYNC:
2988                         if (dsi.debug_process)
2989                                 DSSDBG("Signaling SYNC done!\n");
2990                         complete(p.u.sync);
2991                         break;
2992
2993                 case DSI_CMD_MEM_READ:
2994                         dsi_do_cmd_mem_read(display, &p.u.mem_read);
2995                         break;
2996
2997                 case DSI_CMD_TEST:
2998                         dsi_do_cmd_test(display, &p.u.test);
2999                         break;
3000
3001                 case DSI_CMD_SET_TE:
3002                         dsi_do_cmd_set_te(display, p.u.te);
3003                         break;
3004
3005                 case DSI_CMD_SET_UPDATE_MODE:
3006                         dsi_do_cmd_set_update_mode(display, p.u.update_mode);
3007                         break;
3008
3009                 case DSI_CMD_SET_ROTATE:
3010                         display->ctrl->set_rotate(display, p.u.rotate);
3011                         if (dsi.update_mode == OMAP_DSS_UPDATE_AUTO)
3012                                 dsi.autoupdate_setup = 1;
3013                         break;
3014
3015                 case DSI_CMD_SET_MIRROR:
3016                         display->ctrl->set_mirror(display, p.u.mirror);
3017                         break;
3018
3019                 default:
3020                         BUG();
3021                 }
3022         }
3023
3024         if (dsi.debug_process)
3025                 DSSDBG("exit dsi_process_cmd_fifo\n");
3026
3027         dsi_signal_fifo_waiters();
3028 }
3029
3030 static void dsi_push_cmd(struct dsi_cmd_item *p)
3031 {
3032         int ret;
3033
3034         if (dsi.debug_process)
3035                 DSSDBGF("");
3036
3037         while (1) {
3038                 unsigned long flags;
3039                 unsigned avail, used;
3040
3041                 spin_lock_irqsave(dsi.cmd_fifo->lock, flags);
3042                 used = __kfifo_len(dsi.cmd_fifo) / sizeof(struct dsi_cmd_item);
3043                 avail = DSI_CMD_FIFO_LEN - used;
3044
3045                 if (dsi.debug_process)
3046                         DSSDBG("%u/%u items left in fifo\n", avail, used);
3047
3048                 if (avail == 0) {
3049                         if (dsi.debug_process)
3050                                 DSSDBG("cmd fifo full, waiting...\n");
3051                         spin_unlock_irqrestore(dsi.cmd_fifo->lock, flags);
3052                         atomic_inc(&dsi.cmd_fifo_full);
3053                         wait_for_completion(&dsi.cmd_done);
3054                         if (dsi.debug_process)
3055                                 DSSDBG("cmd fifo not full, woke up\n");
3056                         continue;
3057                 }
3058
3059                 ret = __kfifo_put(dsi.cmd_fifo, (unsigned char *)p,
3060                                 sizeof(*p));
3061
3062                 spin_unlock_irqrestore(dsi.cmd_fifo->lock, flags);
3063
3064                 BUG_ON(ret != sizeof(*p));
3065
3066                 break;
3067         }
3068
3069         queue_work(dsi.workqueue, &dsi.process_work);
3070 }
3071
3072 static void dsi_push_update(struct omap_display *display,
3073                 int x, int y, int w, int h)
3074 {
3075         struct dsi_cmd_item p;
3076
3077         p.display = display;
3078         p.cmd = DSI_CMD_UPDATE;
3079
3080         p.u.r.x = x;
3081         p.u.r.y = y;
3082         p.u.r.w = w;
3083         p.u.r.h = h;
3084
3085         DSSDBG("pushing UPDATE %d,%d %dx%d\n", x, y, w, h);
3086
3087         dsi_push_cmd(&p);
3088 }
3089
3090 static void dsi_push_autoupdate(struct omap_display *display)
3091 {
3092         struct dsi_cmd_item p;
3093
3094         p.display = display;
3095         p.cmd = DSI_CMD_AUTOUPDATE;
3096
3097         dsi_push_cmd(&p);
3098 }
3099
3100 static void dsi_push_sync(struct omap_display *display,
3101                 struct completion *sync_comp)
3102 {
3103         struct dsi_cmd_item p;
3104
3105         p.display = display;
3106         p.cmd = DSI_CMD_SYNC;
3107         p.u.sync = sync_comp;
3108
3109         DSSDBG("pushing SYNC\n");
3110
3111         dsi_push_cmd(&p);
3112 }
3113
3114 static void dsi_push_mem_read(struct omap_display *display,
3115                 struct dsi_cmd_mem_read *mem_read)
3116 {
3117         struct dsi_cmd_item p;
3118
3119         p.display = display;
3120         p.cmd = DSI_CMD_MEM_READ;
3121         p.u.mem_read = *mem_read;
3122
3123         DSSDBG("pushing MEM_READ\n");
3124
3125         dsi_push_cmd(&p);
3126 }
3127
3128 static void dsi_push_test(struct omap_display *display, int test_num,
3129                 int *result, struct completion *completion)
3130 {
3131         struct dsi_cmd_item p;
3132
3133         p.display = display;
3134         p.cmd = DSI_CMD_TEST;
3135         p.u.test.test_num = test_num;
3136         p.u.test.result = result;
3137         p.u.test.completion = completion;
3138
3139         DSSDBG("pushing TEST\n");
3140
3141         dsi_push_cmd(&p);
3142 }
3143
3144 static void dsi_push_set_te(struct omap_display *display, bool enable)
3145 {
3146         struct dsi_cmd_item p;
3147
3148         p.display = display;
3149         p.cmd = DSI_CMD_SET_TE;
3150         p.u.te = enable;
3151
3152         DSSDBG("pushing SET_TE\n");
3153
3154         dsi_push_cmd(&p);
3155 }
3156
3157 static void dsi_push_set_update_mode(struct omap_display *display,
3158                 enum omap_dss_update_mode mode)
3159 {
3160         struct dsi_cmd_item p;
3161
3162         p.display = display;
3163         p.cmd = DSI_CMD_SET_UPDATE_MODE;
3164         p.u.update_mode = mode;
3165
3166         DSSDBG("pushing SET_UPDATE_MODE\n");
3167
3168         dsi_push_cmd(&p);
3169 }
3170
3171 static void dsi_push_set_rotate(struct omap_display *display, int rotate)
3172 {
3173         struct dsi_cmd_item p;
3174
3175         p.display = display;
3176         p.cmd = DSI_CMD_SET_ROTATE;
3177         p.u.rotate = rotate;
3178
3179         DSSDBG("pushing SET_ROTATE\n");
3180
3181         dsi_push_cmd(&p);
3182 }
3183
3184 static void dsi_push_set_mirror(struct omap_display *display, int mirror)
3185 {
3186         struct dsi_cmd_item p;
3187
3188         p.display = display;
3189         p.cmd = DSI_CMD_SET_MIRROR;
3190         p.u.mirror = mirror;
3191
3192         DSSDBG("pushing SET_MIRROR\n");
3193
3194         dsi_push_cmd(&p);
3195 }
3196
3197 static int dsi_wait_sync(struct omap_display *display)
3198 {
3199         long wait = msecs_to_jiffies(60000);
3200         struct completion compl;
3201
3202         DSSDBGF("");
3203
3204         init_completion(&compl);
3205         dsi_push_sync(display, &compl);
3206
3207         DSSDBG("Waiting for SYNC to happen...\n");
3208         wait = wait_for_completion_timeout(&compl, wait);
3209         DSSDBG("Released from SYNC\n");
3210
3211         if (wait == 0) {
3212                 DSSERR("timeout waiting sync\n");
3213                 return -ETIME;
3214         }
3215
3216         return 0;
3217 }
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230 /* Display funcs */
3231
3232 static int dsi_display_init_dispc(struct omap_display *display)
3233 {
3234         int r;
3235
3236         r = omap_dispc_register_isr(framedone_callback, NULL,
3237                         DISPC_IRQ_FRAMEDONE);
3238         if (r) {
3239                 DSSERR("can't get FRAMEDONE irq\n");
3240                 return r;
3241         }
3242
3243         dispc_set_lcd_display_type(OMAP_DSS_LCD_DISPLAY_TFT);
3244
3245         dispc_set_parallel_interface_mode(OMAP_DSS_PARALLELMODE_DSI);
3246         dispc_enable_fifohandcheck(1);
3247
3248         dispc_set_tft_data_lines(display->ctrl->pixel_size);
3249
3250         {
3251                 struct omap_video_timings timings = {
3252                         .hsw            = 1,
3253                         .hfp            = 1,
3254                         .hbp            = 1,
3255                         .vsw            = 1,
3256                         .vfp            = 0,
3257                         .vbp            = 0,
3258                 };
3259
3260                 dispc_set_lcd_timings(&timings);
3261         }
3262
3263         return 0;
3264 }
3265
3266 static void dsi_display_uninit_dispc(struct omap_display *display)
3267 {
3268         omap_dispc_unregister_isr(framedone_callback, NULL,
3269                         DISPC_IRQ_FRAMEDONE);
3270 }
3271
3272 static int dsi_display_init_dsi(struct omap_display *display)
3273 {
3274         struct dsi_clock_info cinfo;
3275         int r;
3276
3277         _dsi_print_reset_status();
3278
3279         r = dsi_pll_init(1, 0);
3280         if (r)
3281                 goto err0;
3282
3283         r = dsi_pll_calc_ddrfreq(display->hw_config.u.dsi.ddr_clk_hz, &cinfo);
3284         if (r)
3285                 goto err1;
3286
3287         r = dsi_pll_program(&cinfo);
3288         if (r)
3289                 goto err1;
3290
3291         DSSDBG("PLL OK\n");
3292
3293         r = dsi_complexio_init(display);
3294         if (r)
3295                 goto err1;
3296
3297         _dsi_print_reset_status();
3298
3299         dsi_proto_timings();
3300         dsi_set_lp_clk_divisor();
3301
3302         if (1)
3303                 _dsi_print_reset_status();
3304
3305         r = dsi_proto_config(display);
3306         if (r)
3307                 goto err2;
3308
3309         /* enable interface */
3310         dsi_vc_enable(0, 1);
3311         dsi_vc_enable(1, 1);
3312         dsi_if_enable(1);
3313         dsi_force_tx_stop_mode_io();
3314
3315         if (display->ctrl && display->ctrl->enable) {
3316                 r = display->ctrl->enable(display);
3317                 if (r)
3318                         goto err3;
3319         }
3320
3321         if (display->panel && display->panel->enable) {
3322                 r = display->panel->enable(display);
3323                 if (r)
3324                         goto err4;
3325         }
3326
3327         /* enable high-speed after initial config */
3328         dsi_vc_enable_hs(0, 1);
3329
3330         return 0;
3331 err4:
3332         if (display->ctrl && display->ctrl->disable)
3333                 display->ctrl->disable(display);
3334 err3:
3335         dsi_if_enable(0);
3336 err2:
3337         dsi_complexio_uninit();
3338 err1:
3339         dsi_pll_uninit();
3340 err0:
3341         return r;
3342 }
3343
3344 static void dsi_display_uninit_dsi(struct omap_display *display)
3345 {
3346         if (display->panel && display->panel->disable)
3347                 display->panel->disable(display);
3348         if (display->ctrl && display->ctrl->disable)
3349                 display->ctrl->disable(display);
3350
3351         dsi_complexio_uninit();
3352         dsi_pll_uninit();
3353 }
3354
3355 static int dsi_core_init(void)
3356 {
3357         /* Autoidle */
3358         REG_FLD_MOD(DSI_SYSCONFIG, 1, 0, 0);
3359
3360         /* ENWAKEUP */
3361         REG_FLD_MOD(DSI_SYSCONFIG, 1, 2, 2);
3362
3363         /* SIDLEMODE smart-idle */
3364         REG_FLD_MOD(DSI_SYSCONFIG, 2, 4, 3);
3365
3366         _dsi_initialize_irq();
3367
3368         return 0;
3369 }
3370
3371 static int dsi_display_enable(struct omap_display *display)
3372 {
3373         int r = 0;
3374
3375         DSSDBG("dsi_display_enable\n");
3376
3377         mutex_lock(&dsi.lock);
3378
3379         if (display->state != OMAP_DSS_DISPLAY_DISABLED) {
3380                 DSSERR("display already enabled\n");
3381                 r = -EINVAL;
3382                 goto err0;
3383         }
3384
3385         enable_clocks(1);
3386         dsi_enable_pll_clock(1);
3387
3388         r = _dsi_reset();
3389         if (r)
3390                 return r;
3391
3392         dsi_core_init();
3393
3394         r = dsi_display_init_dispc(display);
3395         if (r)
3396                 goto err1;
3397
3398         r = dsi_display_init_dsi(display);
3399         if (r)
3400                 goto err2;
3401
3402         display->state = OMAP_DSS_DISPLAY_ACTIVE;
3403
3404         if (dsi.use_te)
3405                 dsi_push_set_te(display, 1);
3406
3407         dsi_push_set_update_mode(display, dsi.user_update_mode);
3408         dsi.target_update_mode = dsi.user_update_mode;
3409
3410         mutex_unlock(&dsi.lock);
3411
3412         return dsi_wait_sync(display);
3413
3414 err2:
3415         dsi_display_uninit_dispc(display);
3416 err1:
3417         enable_clocks(0);
3418         dsi_enable_pll_clock(0);
3419 err0:
3420         mutex_unlock(&dsi.lock);
3421         DSSDBG("dsi_display_enable FAILED\n");
3422         return r;
3423 }
3424
3425 static void dsi_display_disable(struct omap_display *display)
3426 {
3427         DSSDBG("dsi_display_disable\n");
3428
3429         mutex_lock(&dsi.lock);
3430
3431         if (display->state == OMAP_DSS_DISPLAY_DISABLED ||
3432                         display->state == OMAP_DSS_DISPLAY_SUSPENDED)
3433                 goto end;
3434
3435         if (dsi.target_update_mode != OMAP_DSS_UPDATE_DISABLED) {
3436                 dsi_push_set_update_mode(display, OMAP_DSS_UPDATE_DISABLED);
3437                 dsi.target_update_mode = OMAP_DSS_UPDATE_DISABLED;
3438         }
3439
3440         dsi_wait_sync(display);
3441
3442         display->state = OMAP_DSS_DISPLAY_DISABLED;
3443
3444         dsi_display_uninit_dispc(display);
3445
3446         dsi_display_uninit_dsi(display);
3447
3448         enable_clocks(0);
3449         dsi_enable_pll_clock(0);
3450 end:
3451         mutex_unlock(&dsi.lock);
3452 }
3453
3454 static int dsi_display_suspend(struct omap_display *display)
3455 {
3456         DSSDBG("dsi_display_suspend\n");
3457
3458         dsi_display_disable(display);
3459
3460         display->state = OMAP_DSS_DISPLAY_SUSPENDED;
3461
3462         return 0;
3463 }
3464
3465 static int dsi_display_resume(struct omap_display *display)
3466 {
3467         DSSDBG("dsi_display_resume\n");
3468
3469         display->state = OMAP_DSS_DISPLAY_DISABLED;
3470         return dsi_display_enable(display);
3471 }
3472
3473 static int dsi_display_update(struct omap_display *display,
3474                         u16 x, u16 y, u16 w, u16 h)
3475 {
3476         DSSDBG("dsi_display_update(%d,%d %dx%d)\n", x, y, w, h);
3477
3478         if (w == 0 || h == 0)
3479                 return 0;
3480
3481         mutex_lock(&dsi.lock);
3482
3483         if (dsi.target_update_mode == OMAP_DSS_UPDATE_MANUAL)
3484                 dsi_push_update(display, x, y, w, h);
3485         /* XXX else return error? */
3486
3487         mutex_unlock(&dsi.lock);
3488
3489         return 0;
3490 }
3491
3492 static int dsi_display_sync(struct omap_display *display)
3493 {
3494         DSSDBGF("");
3495         return dsi_wait_sync(display);
3496 }
3497
3498 static int dsi_display_set_update_mode(struct omap_display *display,
3499                 enum omap_dss_update_mode mode)
3500 {
3501         DSSDBGF("%d", mode);
3502
3503         mutex_lock(&dsi.lock);
3504
3505         if (dsi.target_update_mode != mode) {
3506                 dsi_push_set_update_mode(display, mode);
3507
3508                 dsi.target_update_mode = mode;
3509                 dsi.user_update_mode = mode;
3510         }
3511
3512         mutex_unlock(&dsi.lock);
3513
3514         return dsi_wait_sync(display);
3515 }
3516
3517 static enum omap_dss_update_mode dsi_display_get_update_mode(
3518                 struct omap_display *display)
3519 {
3520         return dsi.update_mode;
3521 }
3522
3523 static int dsi_display_enable_te(struct omap_display *display, bool enable)
3524 {
3525         DSSDBGF("%d", enable);
3526
3527         if (!display->ctrl->enable_te)
3528                 return -ENOENT;
3529
3530         dsi_push_set_te(display, enable);
3531
3532         return dsi_wait_sync(display);
3533 }
3534
3535 static int dsi_display_get_te(struct omap_display *display)
3536 {
3537         return dsi.use_te;
3538 }
3539
3540
3541
3542 static int dsi_display_set_rotate(struct omap_display *display, u8 rotate)
3543 {
3544         DSSDBGF("%d", rotate);
3545
3546         if (!display->ctrl->set_rotate || !display->ctrl->get_rotate)
3547                 return -EINVAL;
3548
3549         dsi_push_set_rotate(display, rotate);
3550
3551         return dsi_wait_sync(display);
3552 }
3553
3554 static u8 dsi_display_get_rotate(struct omap_display *display)
3555 {
3556         if (!display->ctrl->set_rotate || !display->ctrl->get_rotate)
3557                 return 0;
3558
3559         return display->ctrl->get_rotate(display);
3560 }
3561
3562 static int dsi_display_set_mirror(struct omap_display *display, bool mirror)
3563 {
3564         DSSDBGF("%d", mirror);
3565
3566         if (!display->ctrl->set_mirror || !display->ctrl->get_mirror)
3567                 return -EINVAL;
3568
3569         dsi_push_set_mirror(display, mirror);
3570
3571         return dsi_wait_sync(display);
3572 }
3573
3574 static bool dsi_display_get_mirror(struct omap_display *display)
3575 {
3576         if (!display->ctrl->set_mirror || !display->ctrl->get_mirror)
3577                 return 0;
3578
3579         return display->ctrl->get_mirror(display);
3580 }
3581
3582 static int dsi_display_run_test(struct omap_display *display, int test_num)
3583 {
3584         long wait = msecs_to_jiffies(60000);
3585         struct completion compl;
3586         int result;
3587
3588         if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
3589                 return -EIO;
3590
3591         DSSDBGF("%d", test_num);
3592
3593         init_completion(&compl);
3594
3595         dsi_push_test(display, test_num, &result, &compl);
3596
3597         DSSDBG("Waiting for SYNC to happen...\n");
3598         wait = wait_for_completion_timeout(&compl, wait);
3599         DSSDBG("Released from SYNC\n");
3600
3601         if (wait == 0) {
3602                 DSSERR("timeout waiting test sync\n");
3603                 return -ETIME;
3604         }
3605
3606         return result;
3607 }
3608
3609 static int dsi_display_memory_read(struct omap_display *display,
3610                 void *buf, size_t size,
3611                 u16 x, u16 y, u16 w, u16 h)
3612 {
3613         long wait = msecs_to_jiffies(60000);
3614         struct completion compl;
3615         struct dsi_cmd_mem_read mem_read;
3616         size_t ret_size;
3617
3618         DSSDBGF("");
3619
3620         if (!display->ctrl->memory_read)
3621                 return -EINVAL;
3622
3623         if (display->state != OMAP_DSS_DISPLAY_ACTIVE)
3624                 return -EIO;
3625
3626         init_completion(&compl);
3627
3628         mem_read.x = x;
3629         mem_read.y = y;
3630         mem_read.w = w;
3631         mem_read.h = h;
3632         mem_read.buf = buf;
3633         mem_read.size = size;
3634         mem_read.ret_size = &ret_size;
3635         mem_read.completion = &compl;
3636
3637         dsi_push_mem_read(display, &mem_read);
3638
3639         DSSDBG("Waiting for SYNC to happen...\n");
3640         wait = wait_for_completion_timeout(&compl, wait);
3641         DSSDBG("Released from SYNC\n");
3642
3643         if (wait == 0) {
3644                 DSSERR("timeout waiting mem read sync\n");
3645                 return -ETIME;
3646         }
3647
3648         return ret_size;
3649 }
3650
3651 static void dsi_configure_overlay(struct omap_overlay *ovl)
3652 {
3653         unsigned low, high, size;
3654         enum omap_burst_size burst;
3655         enum omap_plane plane = ovl->id;
3656
3657         burst = OMAP_DSS_BURST_16x32;
3658         size = 16 * 32 / 8;
3659
3660         dispc_set_burst_size(plane, burst);
3661
3662         high = dispc_get_plane_fifo_size(plane) - size;
3663         low = 0;
3664         dispc_setup_plane_fifo(plane, low, high);
3665 }
3666
3667 void dsi_init_display(struct omap_display *display)
3668 {
3669         DSSDBG("DSI init\n");
3670
3671         display->enable = dsi_display_enable;
3672         display->disable = dsi_display_disable;
3673         display->suspend = dsi_display_suspend;
3674         display->resume = dsi_display_resume;
3675         display->update = dsi_display_update;
3676         display->sync = dsi_display_sync;
3677         display->set_update_mode = dsi_display_set_update_mode;
3678         display->get_update_mode = dsi_display_get_update_mode;
3679         display->enable_te = dsi_display_enable_te;
3680         display->get_te = dsi_display_get_te;
3681
3682         display->get_rotate = dsi_display_get_rotate;
3683         display->set_rotate = dsi_display_set_rotate;
3684
3685         display->get_mirror = dsi_display_get_mirror;
3686         display->set_mirror = dsi_display_set_mirror;
3687
3688         display->run_test = dsi_display_run_test;
3689         display->memory_read = dsi_display_memory_read;
3690
3691         display->configure_overlay = dsi_configure_overlay;
3692
3693         display->caps = OMAP_DSS_DISPLAY_CAP_MANUAL_UPDATE;
3694
3695         dsi.vc[0].display = display;
3696         dsi.vc[1].display = display;
3697 }
3698
3699 int dsi_init(void)
3700 {
3701         u32 rev;
3702
3703         spin_lock_init(&dsi.cmd_lock);
3704         dsi.cmd_fifo = kfifo_alloc(
3705                         DSI_CMD_FIFO_LEN * sizeof(struct dsi_cmd_item),
3706                         GFP_KERNEL,
3707                         &dsi.cmd_lock);
3708
3709         init_completion(&dsi.cmd_done);
3710         atomic_set(&dsi.cmd_fifo_full, 0);
3711         atomic_set(&dsi.cmd_pending, 0);
3712
3713         init_completion(&dsi.bta_completion);
3714
3715         dsi.workqueue = create_singlethread_workqueue("dsi");
3716         INIT_WORK(&dsi.framedone_work, framedone_worker);
3717         INIT_WORK(&dsi.process_work, dsi_process_cmd_fifo);
3718
3719         mutex_init(&dsi.lock);
3720
3721         dsi.target_update_mode = OMAP_DSS_UPDATE_DISABLED;
3722         dsi.user_update_mode = OMAP_DSS_UPDATE_DISABLED;
3723
3724         dsi.base = ioremap(DSI_BASE, DSI_SZ_REGS);
3725         if (!dsi.base) {
3726                 DSSERR("can't ioremap DSI\n");
3727                 return -ENOMEM;
3728         }
3729
3730         enable_clocks(1);
3731
3732         rev = dsi_read_reg(DSI_REVISION);
3733         printk(KERN_INFO "OMAP DSI rev %d.%d\n",
3734                FLD_GET(rev, 7, 4), FLD_GET(rev, 3, 0));
3735
3736         enable_clocks(0);
3737
3738         return 0;
3739 }
3740
3741 void dsi_exit(void)
3742 {
3743         flush_workqueue(dsi.workqueue);
3744         destroy_workqueue(dsi.workqueue);
3745
3746         iounmap(dsi.base);
3747
3748         kfifo_free(dsi.cmd_fifo);
3749
3750         DSSDBG("omap_dsi_exit\n");
3751 }
3752