blob: 4caad4f12c9df6ef90d8e5abe23c187a6424e9c9 [file] [log] [blame]
Stefano Babic575001e2010-10-13 12:16:35 +02001/*
2 * Porting to u-boot:
3 *
4 * (C) Copyright 2010
5 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
6 *
7 * Linux IPU driver for MX51:
8 *
9 * (C) Copyright 2005-2010 Freescale Semiconductor, Inc.
10 *
11 * See file CREDITS for list of people who contributed to this
12 * project.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
28 */
29
30/* #define DEBUG */
31#include <common.h>
32#include <linux/types.h>
33#include <linux/err.h>
34#include <asm/io.h>
35#include <asm/errno.h>
36#include <asm/arch/imx-regs.h>
37#include <asm/arch/crm_regs.h>
38#include "ipu.h"
39#include "ipu_regs.h"
40
41extern struct mxc_ccm_reg *mxc_ccm;
42extern u32 *ipu_cpmem_base;
43
44struct ipu_ch_param_word {
45 uint32_t data[5];
46 uint32_t res[3];
47};
48
49struct ipu_ch_param {
50 struct ipu_ch_param_word word[2];
51};
52
53#define ipu_ch_param_addr(ch) (((struct ipu_ch_param *)ipu_cpmem_base) + (ch))
54
55#define _param_word(base, w) \
56 (((struct ipu_ch_param *)(base))->word[(w)].data)
57
58#define ipu_ch_param_set_field(base, w, bit, size, v) { \
59 int i = (bit) / 32; \
60 int off = (bit) % 32; \
61 _param_word(base, w)[i] |= (v) << off; \
62 if (((bit) + (size) - 1) / 32 > i) { \
63 _param_word(base, w)[i + 1] |= (v) >> (off ? (32 - off) : 0); \
64 } \
65}
66
67#define ipu_ch_param_mod_field(base, w, bit, size, v) { \
68 int i = (bit) / 32; \
69 int off = (bit) % 32; \
70 u32 mask = (1UL << size) - 1; \
71 u32 temp = _param_word(base, w)[i]; \
72 temp &= ~(mask << off); \
73 _param_word(base, w)[i] = temp | (v) << off; \
74 if (((bit) + (size) - 1) / 32 > i) { \
75 temp = _param_word(base, w)[i + 1]; \
76 temp &= ~(mask >> (32 - off)); \
77 _param_word(base, w)[i + 1] = \
78 temp | ((v) >> (off ? (32 - off) : 0)); \
79 } \
80}
81
82#define ipu_ch_param_read_field(base, w, bit, size) ({ \
83 u32 temp2; \
84 int i = (bit) / 32; \
85 int off = (bit) % 32; \
86 u32 mask = (1UL << size) - 1; \
87 u32 temp1 = _param_word(base, w)[i]; \
88 temp1 = mask & (temp1 >> off); \
89 if (((bit)+(size) - 1) / 32 > i) { \
90 temp2 = _param_word(base, w)[i + 1]; \
91 temp2 &= mask >> (off ? (32 - off) : 0); \
92 temp1 |= temp2 << (off ? (32 - off) : 0); \
93 } \
94 temp1; \
95})
96
97
98void clk_enable(struct clk *clk)
99{
100 if (clk) {
101 if (clk->usecount++ == 0) {
102 clk->enable(clk);
103 }
104 }
105}
106
107void clk_disable(struct clk *clk)
108{
109 if (clk) {
110 if (!(--clk->usecount)) {
111 if (clk->disable)
112 clk->disable(clk);
113 }
114 }
115}
116
117int clk_get_usecount(struct clk *clk)
118{
119 if (clk == NULL)
120 return 0;
121
122 return clk->usecount;
123}
124
125u32 clk_get_rate(struct clk *clk)
126{
127 if (!clk)
128 return 0;
129
130 return clk->rate;
131}
132
133struct clk *clk_get_parent(struct clk *clk)
134{
135 if (!clk)
136 return 0;
137
138 return clk->parent;
139}
140
141int clk_set_rate(struct clk *clk, unsigned long rate)
142{
143 if (clk && clk->set_rate)
144 clk->set_rate(clk, rate);
145 return clk->rate;
146}
147
148long clk_round_rate(struct clk *clk, unsigned long rate)
149{
150 if (clk == NULL || !clk->round_rate)
151 return 0;
152
153 return clk->round_rate(clk, rate);
154}
155
156int clk_set_parent(struct clk *clk, struct clk *parent)
157{
158 clk->parent = parent;
159 if (clk->set_parent)
160 return clk->set_parent(clk, parent);
161 return 0;
162}
163
164static int clk_ipu_enable(struct clk *clk)
165{
166 u32 reg;
167
168 reg = __raw_readl(clk->enable_reg);
169 reg |= MXC_CCM_CCGR_CG_MASK << clk->enable_shift;
170 __raw_writel(reg, clk->enable_reg);
171
172 /* Handshake with IPU when certain clock rates are changed. */
173 reg = __raw_readl(&mxc_ccm->ccdr);
174 reg &= ~MXC_CCM_CCDR_IPU_HS_MASK;
175 __raw_writel(reg, &mxc_ccm->ccdr);
176
177 /* Handshake with IPU when LPM is entered as its enabled. */
178 reg = __raw_readl(&mxc_ccm->clpcr);
179 reg &= ~MXC_CCM_CLPCR_BYPASS_IPU_LPM_HS;
180 __raw_writel(reg, &mxc_ccm->clpcr);
181
182 return 0;
183}
184
185static void clk_ipu_disable(struct clk *clk)
186{
187 u32 reg;
188
189 reg = __raw_readl(clk->enable_reg);
190 reg &= ~(MXC_CCM_CCGR_CG_MASK << clk->enable_shift);
191 __raw_writel(reg, clk->enable_reg);
192
193 /*
194 * No handshake with IPU whe dividers are changed
195 * as its not enabled.
196 */
197 reg = __raw_readl(&mxc_ccm->ccdr);
198 reg |= MXC_CCM_CCDR_IPU_HS_MASK;
199 __raw_writel(reg, &mxc_ccm->ccdr);
200
201 /* No handshake with IPU when LPM is entered as its not enabled. */
202 reg = __raw_readl(&mxc_ccm->clpcr);
203 reg |= MXC_CCM_CLPCR_BYPASS_IPU_LPM_HS;
204 __raw_writel(reg, &mxc_ccm->clpcr);
205}
206
207
208static struct clk ipu_clk = {
209 .name = "ipu_clk",
210 .rate = 133000000,
211 .enable_reg = (u32 *)(MXC_CCM_BASE +
212 offsetof(struct mxc_ccm_reg, CCGR5)),
213 .enable_shift = MXC_CCM_CCGR5_CG5_OFFSET,
214 .enable = clk_ipu_enable,
215 .disable = clk_ipu_disable,
216 .usecount = 0,
217};
218
219/* Globals */
220struct clk *g_ipu_clk;
221unsigned char g_ipu_clk_enabled;
222struct clk *g_di_clk[2];
223struct clk *g_pixel_clk[2];
224unsigned char g_dc_di_assignment[10];
225uint32_t g_channel_init_mask;
226uint32_t g_channel_enable_mask;
227
228static int ipu_dc_use_count;
229static int ipu_dp_use_count;
230static int ipu_dmfc_use_count;
231static int ipu_di_use_count[2];
232
233u32 *ipu_cpmem_base;
234u32 *ipu_dc_tmpl_reg;
235
236/* Static functions */
237
238static inline void ipu_ch_param_set_high_priority(uint32_t ch)
239{
240 ipu_ch_param_mod_field(ipu_ch_param_addr(ch), 1, 93, 2, 1);
241};
242
243static inline uint32_t channel_2_dma(ipu_channel_t ch, ipu_buffer_t type)
244{
245 return ((uint32_t) ch >> (6 * type)) & 0x3F;
246};
247
248/* Either DP BG or DP FG can be graphic window */
249static inline int ipu_is_dp_graphic_chan(uint32_t dma_chan)
250{
251 return (dma_chan == 23 || dma_chan == 27);
252}
253
254static inline int ipu_is_dmfc_chan(uint32_t dma_chan)
255{
256 return ((dma_chan >= 23) && (dma_chan <= 29));
257}
258
259
260static inline void ipu_ch_param_set_buffer(uint32_t ch, int bufNum,
261 dma_addr_t phyaddr)
262{
263 ipu_ch_param_mod_field(ipu_ch_param_addr(ch), 1, 29 * bufNum, 29,
264 phyaddr / 8);
265};
266
267#define idma_is_valid(ch) (ch != NO_DMA)
268#define idma_mask(ch) (idma_is_valid(ch) ? (1UL << (ch & 0x1F)) : 0)
269#define idma_is_set(reg, dma) (__raw_readl(reg(dma)) & idma_mask(dma))
270
271static void ipu_pixel_clk_recalc(struct clk *clk)
272{
273 u32 div = __raw_readl(DI_BS_CLKGEN0(clk->id));
274 if (div == 0)
275 clk->rate = 0;
276 else
277 clk->rate = (clk->parent->rate * 16) / div;
278}
279
280static unsigned long ipu_pixel_clk_round_rate(struct clk *clk,
281 unsigned long rate)
282{
283 u32 div, div1;
284 u32 tmp;
285 /*
286 * Calculate divider
287 * Fractional part is 4 bits,
288 * so simply multiply by 2^4 to get fractional part.
289 */
290 tmp = (clk->parent->rate * 16);
291 div = tmp / rate;
292
293 if (div < 0x10) /* Min DI disp clock divider is 1 */
294 div = 0x10;
295 if (div & ~0xFEF)
296 div &= 0xFF8;
297 else {
298 div1 = div & 0xFE0;
299 if ((tmp/div1 - tmp/div) < rate / 4)
300 div = div1;
301 else
302 div &= 0xFF8;
303 }
304 return (clk->parent->rate * 16) / div;
305}
306
307static int ipu_pixel_clk_set_rate(struct clk *clk, unsigned long rate)
308{
309 u32 div = (clk->parent->rate * 16) / rate;
310
311 __raw_writel(div, DI_BS_CLKGEN0(clk->id));
312
313 /* Setup pixel clock timing */
314 __raw_writel((div / 16) << 16, DI_BS_CLKGEN1(clk->id));
315
316 clk->rate = (clk->parent->rate * 16) / div;
317 return 0;
318}
319
320static int ipu_pixel_clk_enable(struct clk *clk)
321{
322 u32 disp_gen = __raw_readl(IPU_DISP_GEN);
323 disp_gen |= clk->id ? DI1_COUNTER_RELEASE : DI0_COUNTER_RELEASE;
324 __raw_writel(disp_gen, IPU_DISP_GEN);
325
326 return 0;
327}
328
329static void ipu_pixel_clk_disable(struct clk *clk)
330{
331 u32 disp_gen = __raw_readl(IPU_DISP_GEN);
332 disp_gen &= clk->id ? ~DI1_COUNTER_RELEASE : ~DI0_COUNTER_RELEASE;
333 __raw_writel(disp_gen, IPU_DISP_GEN);
334
335}
336
337static int ipu_pixel_clk_set_parent(struct clk *clk, struct clk *parent)
338{
339 u32 di_gen = __raw_readl(DI_GENERAL(clk->id));
340
341 if (parent == g_ipu_clk)
342 di_gen &= ~DI_GEN_DI_CLK_EXT;
343 else if (!IS_ERR(g_di_clk[clk->id]) && parent == g_di_clk[clk->id])
344 di_gen |= DI_GEN_DI_CLK_EXT;
345 else
346 return -EINVAL;
347
348 __raw_writel(di_gen, DI_GENERAL(clk->id));
349 ipu_pixel_clk_recalc(clk);
350 return 0;
351}
352
353static struct clk pixel_clk[] = {
354 {
355 .name = "pixel_clk",
356 .id = 0,
357 .recalc = ipu_pixel_clk_recalc,
358 .set_rate = ipu_pixel_clk_set_rate,
359 .round_rate = ipu_pixel_clk_round_rate,
360 .set_parent = ipu_pixel_clk_set_parent,
361 .enable = ipu_pixel_clk_enable,
362 .disable = ipu_pixel_clk_disable,
363 .usecount = 0,
364 },
365 {
366 .name = "pixel_clk",
367 .id = 1,
368 .recalc = ipu_pixel_clk_recalc,
369 .set_rate = ipu_pixel_clk_set_rate,
370 .round_rate = ipu_pixel_clk_round_rate,
371 .set_parent = ipu_pixel_clk_set_parent,
372 .enable = ipu_pixel_clk_enable,
373 .disable = ipu_pixel_clk_disable,
374 .usecount = 0,
375 },
376};
377
378/*
379 * This function resets IPU
380 */
381void ipu_reset(void)
382{
383 u32 *reg;
384 u32 value;
385
386 reg = (u32 *)SRC_BASE_ADDR;
387 value = __raw_readl(reg);
388 value = value | SW_IPU_RST;
389 __raw_writel(value, reg);
390}
391
392/*
393 * This function is called by the driver framework to initialize the IPU
394 * hardware.
395 *
396 * @param dev The device structure for the IPU passed in by the
397 * driver framework.
398 *
399 * @return Returns 0 on success or negative error code on error
400 */
401int ipu_probe(void)
402{
403 unsigned long ipu_base;
Fabio Estevam913db792012-05-31 07:23:57 +0000404#if defined CONFIG_MX51
Stefano Babic575001e2010-10-13 12:16:35 +0200405 u32 temp;
406
407 u32 *reg_hsc_mcd = (u32 *)MIPI_HSC_BASE_ADDR;
408 u32 *reg_hsc_mxt_conf = (u32 *)(MIPI_HSC_BASE_ADDR + 0x800);
409
410 __raw_writel(0xF00, reg_hsc_mcd);
411
412 /* CSI mode reserved*/
413 temp = __raw_readl(reg_hsc_mxt_conf);
414 __raw_writel(temp | 0x0FF, reg_hsc_mxt_conf);
415
416 temp = __raw_readl(reg_hsc_mxt_conf);
417 __raw_writel(temp | 0x10000, reg_hsc_mxt_conf);
Fabio Estevam913db792012-05-31 07:23:57 +0000418#endif
Stefano Babic575001e2010-10-13 12:16:35 +0200419
420 ipu_base = IPU_CTRL_BASE_ADDR;
421 ipu_cpmem_base = (u32 *)(ipu_base + IPU_CPMEM_REG_BASE);
422 ipu_dc_tmpl_reg = (u32 *)(ipu_base + IPU_DC_TMPL_REG_BASE);
423
424 g_pixel_clk[0] = &pixel_clk[0];
425 g_pixel_clk[1] = &pixel_clk[1];
426
427 g_ipu_clk = &ipu_clk;
428 debug("ipu_clk = %u\n", clk_get_rate(g_ipu_clk));
429
430 ipu_reset();
431
432 clk_set_parent(g_pixel_clk[0], g_ipu_clk);
433 clk_set_parent(g_pixel_clk[1], g_ipu_clk);
434 clk_enable(g_ipu_clk);
435
436 g_di_clk[0] = NULL;
437 g_di_clk[1] = NULL;
438
439 __raw_writel(0x807FFFFF, IPU_MEM_RST);
440 while (__raw_readl(IPU_MEM_RST) & 0x80000000)
441 ;
442
443 ipu_init_dc_mappings();
444
445 __raw_writel(0, IPU_INT_CTRL(5));
446 __raw_writel(0, IPU_INT_CTRL(6));
447 __raw_writel(0, IPU_INT_CTRL(9));
448 __raw_writel(0, IPU_INT_CTRL(10));
449
450 /* DMFC Init */
451 ipu_dmfc_init(DMFC_NORMAL, 1);
452
453 /* Set sync refresh channels as high priority */
454 __raw_writel(0x18800000L, IDMAC_CHA_PRI(0));
455
456 /* Set MCU_T to divide MCU access window into 2 */
457 __raw_writel(0x00400000L | (IPU_MCU_T_DEFAULT << 18), IPU_DISP_GEN);
458
459 clk_disable(g_ipu_clk);
460
461 return 0;
462}
463
464void ipu_dump_registers(void)
465{
466 debug("IPU_CONF = \t0x%08X\n", __raw_readl(IPU_CONF));
467 debug("IDMAC_CONF = \t0x%08X\n", __raw_readl(IDMAC_CONF));
468 debug("IDMAC_CHA_EN1 = \t0x%08X\n",
469 __raw_readl(IDMAC_CHA_EN(0)));
470 debug("IDMAC_CHA_EN2 = \t0x%08X\n",
471 __raw_readl(IDMAC_CHA_EN(32)));
472 debug("IDMAC_CHA_PRI1 = \t0x%08X\n",
473 __raw_readl(IDMAC_CHA_PRI(0)));
474 debug("IDMAC_CHA_PRI2 = \t0x%08X\n",
475 __raw_readl(IDMAC_CHA_PRI(32)));
476 debug("IPU_CHA_DB_MODE_SEL0 = \t0x%08X\n",
477 __raw_readl(IPU_CHA_DB_MODE_SEL(0)));
478 debug("IPU_CHA_DB_MODE_SEL1 = \t0x%08X\n",
479 __raw_readl(IPU_CHA_DB_MODE_SEL(32)));
480 debug("DMFC_WR_CHAN = \t0x%08X\n",
481 __raw_readl(DMFC_WR_CHAN));
482 debug("DMFC_WR_CHAN_DEF = \t0x%08X\n",
483 __raw_readl(DMFC_WR_CHAN_DEF));
484 debug("DMFC_DP_CHAN = \t0x%08X\n",
485 __raw_readl(DMFC_DP_CHAN));
486 debug("DMFC_DP_CHAN_DEF = \t0x%08X\n",
487 __raw_readl(DMFC_DP_CHAN_DEF));
488 debug("DMFC_IC_CTRL = \t0x%08X\n",
489 __raw_readl(DMFC_IC_CTRL));
490 debug("IPU_FS_PROC_FLOW1 = \t0x%08X\n",
491 __raw_readl(IPU_FS_PROC_FLOW1));
492 debug("IPU_FS_PROC_FLOW2 = \t0x%08X\n",
493 __raw_readl(IPU_FS_PROC_FLOW2));
494 debug("IPU_FS_PROC_FLOW3 = \t0x%08X\n",
495 __raw_readl(IPU_FS_PROC_FLOW3));
496 debug("IPU_FS_DISP_FLOW1 = \t0x%08X\n",
497 __raw_readl(IPU_FS_DISP_FLOW1));
498}
499
500/*
501 * This function is called to initialize a logical IPU channel.
502 *
503 * @param channel Input parameter for the logical channel ID to init.
504 *
505 * @param params Input parameter containing union of channel
506 * initialization parameters.
507 *
508 * @return Returns 0 on success or negative error code on fail
509 */
510int32_t ipu_init_channel(ipu_channel_t channel, ipu_channel_params_t *params)
511{
512 int ret = 0;
513 uint32_t ipu_conf;
514
515 debug("init channel = %d\n", IPU_CHAN_ID(channel));
516
517 if (g_ipu_clk_enabled == 0) {
518 g_ipu_clk_enabled = 1;
519 clk_enable(g_ipu_clk);
520 }
521
522
523 if (g_channel_init_mask & (1L << IPU_CHAN_ID(channel))) {
524 printf("Warning: channel already initialized %d\n",
525 IPU_CHAN_ID(channel));
526 }
527
528 ipu_conf = __raw_readl(IPU_CONF);
529
530 switch (channel) {
531 case MEM_DC_SYNC:
532 if (params->mem_dc_sync.di > 1) {
533 ret = -EINVAL;
534 goto err;
535 }
536
537 g_dc_di_assignment[1] = params->mem_dc_sync.di;
538 ipu_dc_init(1, params->mem_dc_sync.di,
539 params->mem_dc_sync.interlaced);
540 ipu_di_use_count[params->mem_dc_sync.di]++;
541 ipu_dc_use_count++;
542 ipu_dmfc_use_count++;
543 break;
544 case MEM_BG_SYNC:
545 if (params->mem_dp_bg_sync.di > 1) {
546 ret = -EINVAL;
547 goto err;
548 }
549
550 g_dc_di_assignment[5] = params->mem_dp_bg_sync.di;
551 ipu_dp_init(channel, params->mem_dp_bg_sync.in_pixel_fmt,
552 params->mem_dp_bg_sync.out_pixel_fmt);
553 ipu_dc_init(5, params->mem_dp_bg_sync.di,
554 params->mem_dp_bg_sync.interlaced);
555 ipu_di_use_count[params->mem_dp_bg_sync.di]++;
556 ipu_dc_use_count++;
557 ipu_dp_use_count++;
558 ipu_dmfc_use_count++;
559 break;
560 case MEM_FG_SYNC:
561 ipu_dp_init(channel, params->mem_dp_fg_sync.in_pixel_fmt,
562 params->mem_dp_fg_sync.out_pixel_fmt);
563
564 ipu_dc_use_count++;
565 ipu_dp_use_count++;
566 ipu_dmfc_use_count++;
567 break;
568 default:
569 printf("Missing channel initialization\n");
570 break;
571 }
572
573 /* Enable IPU sub module */
574 g_channel_init_mask |= 1L << IPU_CHAN_ID(channel);
575 if (ipu_dc_use_count == 1)
576 ipu_conf |= IPU_CONF_DC_EN;
577 if (ipu_dp_use_count == 1)
578 ipu_conf |= IPU_CONF_DP_EN;
579 if (ipu_dmfc_use_count == 1)
580 ipu_conf |= IPU_CONF_DMFC_EN;
581 if (ipu_di_use_count[0] == 1) {
582 ipu_conf |= IPU_CONF_DI0_EN;
583 }
584 if (ipu_di_use_count[1] == 1) {
585 ipu_conf |= IPU_CONF_DI1_EN;
586 }
587
588 __raw_writel(ipu_conf, IPU_CONF);
589
590err:
591 return ret;
592}
593
594/*
595 * This function is called to uninitialize a logical IPU channel.
596 *
597 * @param channel Input parameter for the logical channel ID to uninit.
598 */
599void ipu_uninit_channel(ipu_channel_t channel)
600{
601 uint32_t reg;
602 uint32_t in_dma, out_dma = 0;
603 uint32_t ipu_conf;
604
605 if ((g_channel_init_mask & (1L << IPU_CHAN_ID(channel))) == 0) {
606 debug("Channel already uninitialized %d\n",
607 IPU_CHAN_ID(channel));
608 return;
609 }
610
611 /*
612 * Make sure channel is disabled
613 * Get input and output dma channels
614 */
615 in_dma = channel_2_dma(channel, IPU_OUTPUT_BUFFER);
616 out_dma = channel_2_dma(channel, IPU_VIDEO_IN_BUFFER);
617
618 if (idma_is_set(IDMAC_CHA_EN, in_dma) ||
619 idma_is_set(IDMAC_CHA_EN, out_dma)) {
620 printf(
621 "Channel %d is not disabled, disable first\n",
622 IPU_CHAN_ID(channel));
623 return;
624 }
625
626 ipu_conf = __raw_readl(IPU_CONF);
627
628 /* Reset the double buffer */
629 reg = __raw_readl(IPU_CHA_DB_MODE_SEL(in_dma));
630 __raw_writel(reg & ~idma_mask(in_dma), IPU_CHA_DB_MODE_SEL(in_dma));
631 reg = __raw_readl(IPU_CHA_DB_MODE_SEL(out_dma));
632 __raw_writel(reg & ~idma_mask(out_dma), IPU_CHA_DB_MODE_SEL(out_dma));
633
634 switch (channel) {
635 case MEM_DC_SYNC:
636 ipu_dc_uninit(1);
637 ipu_di_use_count[g_dc_di_assignment[1]]--;
638 ipu_dc_use_count--;
639 ipu_dmfc_use_count--;
640 break;
641 case MEM_BG_SYNC:
642 ipu_dp_uninit(channel);
643 ipu_dc_uninit(5);
644 ipu_di_use_count[g_dc_di_assignment[5]]--;
645 ipu_dc_use_count--;
646 ipu_dp_use_count--;
647 ipu_dmfc_use_count--;
648 break;
649 case MEM_FG_SYNC:
650 ipu_dp_uninit(channel);
651 ipu_dc_use_count--;
652 ipu_dp_use_count--;
653 ipu_dmfc_use_count--;
654 break;
655 default:
656 break;
657 }
658
659 g_channel_init_mask &= ~(1L << IPU_CHAN_ID(channel));
660
661 if (ipu_dc_use_count == 0)
662 ipu_conf &= ~IPU_CONF_DC_EN;
663 if (ipu_dp_use_count == 0)
664 ipu_conf &= ~IPU_CONF_DP_EN;
665 if (ipu_dmfc_use_count == 0)
666 ipu_conf &= ~IPU_CONF_DMFC_EN;
667 if (ipu_di_use_count[0] == 0) {
668 ipu_conf &= ~IPU_CONF_DI0_EN;
669 }
670 if (ipu_di_use_count[1] == 0) {
671 ipu_conf &= ~IPU_CONF_DI1_EN;
672 }
673
674 __raw_writel(ipu_conf, IPU_CONF);
675
676 if (ipu_conf == 0) {
677 clk_disable(g_ipu_clk);
678 g_ipu_clk_enabled = 0;
679 }
680
681}
682
683static inline void ipu_ch_param_dump(int ch)
684{
685#ifdef DEBUG
686 struct ipu_ch_param *p = ipu_ch_param_addr(ch);
687 debug("ch %d word 0 - %08X %08X %08X %08X %08X\n", ch,
688 p->word[0].data[0], p->word[0].data[1], p->word[0].data[2],
689 p->word[0].data[3], p->word[0].data[4]);
690 debug("ch %d word 1 - %08X %08X %08X %08X %08X\n", ch,
691 p->word[1].data[0], p->word[1].data[1], p->word[1].data[2],
692 p->word[1].data[3], p->word[1].data[4]);
693 debug("PFS 0x%x, ",
694 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 85, 4));
695 debug("BPP 0x%x, ",
696 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 0, 107, 3));
697 debug("NPB 0x%x\n",
698 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 78, 7));
699
700 debug("FW %d, ",
701 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 0, 125, 13));
702 debug("FH %d, ",
703 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 0, 138, 12));
704 debug("Stride %d\n",
705 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 102, 14));
706
707 debug("Width0 %d+1, ",
708 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 116, 3));
709 debug("Width1 %d+1, ",
710 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 119, 3));
711 debug("Width2 %d+1, ",
712 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 122, 3));
713 debug("Width3 %d+1, ",
714 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 125, 3));
715 debug("Offset0 %d, ",
716 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 128, 5));
717 debug("Offset1 %d, ",
718 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 133, 5));
719 debug("Offset2 %d, ",
720 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 138, 5));
721 debug("Offset3 %d\n",
722 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 143, 5));
723#endif
724}
725
726static inline void ipu_ch_params_set_packing(struct ipu_ch_param *p,
727 int red_width, int red_offset,
728 int green_width, int green_offset,
729 int blue_width, int blue_offset,
730 int alpha_width, int alpha_offset)
731{
732 /* Setup red width and offset */
733 ipu_ch_param_set_field(p, 1, 116, 3, red_width - 1);
734 ipu_ch_param_set_field(p, 1, 128, 5, red_offset);
735 /* Setup green width and offset */
736 ipu_ch_param_set_field(p, 1, 119, 3, green_width - 1);
737 ipu_ch_param_set_field(p, 1, 133, 5, green_offset);
738 /* Setup blue width and offset */
739 ipu_ch_param_set_field(p, 1, 122, 3, blue_width - 1);
740 ipu_ch_param_set_field(p, 1, 138, 5, blue_offset);
741 /* Setup alpha width and offset */
742 ipu_ch_param_set_field(p, 1, 125, 3, alpha_width - 1);
743 ipu_ch_param_set_field(p, 1, 143, 5, alpha_offset);
744}
745
746static void ipu_ch_param_init(int ch,
747 uint32_t pixel_fmt, uint32_t width,
748 uint32_t height, uint32_t stride,
749 uint32_t u, uint32_t v,
750 uint32_t uv_stride, dma_addr_t addr0,
751 dma_addr_t addr1)
752{
753 uint32_t u_offset = 0;
754 uint32_t v_offset = 0;
755 struct ipu_ch_param params;
756
757 memset(&params, 0, sizeof(params));
758
759 ipu_ch_param_set_field(&params, 0, 125, 13, width - 1);
760
761 if ((ch == 8) || (ch == 9) || (ch == 10)) {
762 ipu_ch_param_set_field(&params, 0, 138, 12, (height / 2) - 1);
763 ipu_ch_param_set_field(&params, 1, 102, 14, (stride * 2) - 1);
764 } else {
765 ipu_ch_param_set_field(&params, 0, 138, 12, height - 1);
766 ipu_ch_param_set_field(&params, 1, 102, 14, stride - 1);
767 }
768
769 ipu_ch_param_set_field(&params, 1, 0, 29, addr0 >> 3);
770 ipu_ch_param_set_field(&params, 1, 29, 29, addr1 >> 3);
771
772 switch (pixel_fmt) {
773 case IPU_PIX_FMT_GENERIC:
774 /*Represents 8-bit Generic data */
775 ipu_ch_param_set_field(&params, 0, 107, 3, 5); /* bits/pixel */
776 ipu_ch_param_set_field(&params, 1, 85, 4, 6); /* pix format */
777 ipu_ch_param_set_field(&params, 1, 78, 7, 63); /* burst size */
778
779 break;
780 case IPU_PIX_FMT_GENERIC_32:
781 /*Represents 32-bit Generic data */
782 break;
783 case IPU_PIX_FMT_RGB565:
784 ipu_ch_param_set_field(&params, 0, 107, 3, 3); /* bits/pixel */
785 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
786 ipu_ch_param_set_field(&params, 1, 78, 7, 15); /* burst size */
787
788 ipu_ch_params_set_packing(&params, 5, 0, 6, 5, 5, 11, 8, 16);
789 break;
790 case IPU_PIX_FMT_BGR24:
791 ipu_ch_param_set_field(&params, 0, 107, 3, 1); /* bits/pixel */
792 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
793 ipu_ch_param_set_field(&params, 1, 78, 7, 19); /* burst size */
794
795 ipu_ch_params_set_packing(&params, 8, 0, 8, 8, 8, 16, 8, 24);
796 break;
797 case IPU_PIX_FMT_RGB24:
798 case IPU_PIX_FMT_YUV444:
799 ipu_ch_param_set_field(&params, 0, 107, 3, 1); /* bits/pixel */
800 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
801 ipu_ch_param_set_field(&params, 1, 78, 7, 19); /* burst size */
802
803 ipu_ch_params_set_packing(&params, 8, 16, 8, 8, 8, 0, 8, 24);
804 break;
805 case IPU_PIX_FMT_BGRA32:
806 case IPU_PIX_FMT_BGR32:
807 ipu_ch_param_set_field(&params, 0, 107, 3, 0); /* bits/pixel */
808 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
809 ipu_ch_param_set_field(&params, 1, 78, 7, 15); /* burst size */
810
811 ipu_ch_params_set_packing(&params, 8, 8, 8, 16, 8, 24, 8, 0);
812 break;
813 case IPU_PIX_FMT_RGBA32:
814 case IPU_PIX_FMT_RGB32:
815 ipu_ch_param_set_field(&params, 0, 107, 3, 0); /* bits/pixel */
816 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
817 ipu_ch_param_set_field(&params, 1, 78, 7, 15); /* burst size */
818
819 ipu_ch_params_set_packing(&params, 8, 24, 8, 16, 8, 8, 8, 0);
820 break;
821 case IPU_PIX_FMT_ABGR32:
822 ipu_ch_param_set_field(&params, 0, 107, 3, 0); /* bits/pixel */
823 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
824
825 ipu_ch_params_set_packing(&params, 8, 0, 8, 8, 8, 16, 8, 24);
826 break;
827 case IPU_PIX_FMT_UYVY:
828 ipu_ch_param_set_field(&params, 0, 107, 3, 3); /* bits/pixel */
829 ipu_ch_param_set_field(&params, 1, 85, 4, 0xA); /* pix format */
830 ipu_ch_param_set_field(&params, 1, 78, 7, 15); /* burst size */
831 break;
832 case IPU_PIX_FMT_YUYV:
833 ipu_ch_param_set_field(&params, 0, 107, 3, 3); /* bits/pixel */
834 ipu_ch_param_set_field(&params, 1, 85, 4, 0x8); /* pix format */
835 ipu_ch_param_set_field(&params, 1, 78, 7, 31); /* burst size */
836 break;
837 case IPU_PIX_FMT_YUV420P2:
838 case IPU_PIX_FMT_YUV420P:
839 ipu_ch_param_set_field(&params, 1, 85, 4, 2); /* pix format */
840
841 if (uv_stride < stride / 2)
842 uv_stride = stride / 2;
843
844 u_offset = stride * height;
845 v_offset = u_offset + (uv_stride * height / 2);
846 /* burst size */
847 if ((ch == 8) || (ch == 9) || (ch == 10)) {
848 ipu_ch_param_set_field(&params, 1, 78, 7, 15);
849 uv_stride = uv_stride*2;
850 } else {
851 ipu_ch_param_set_field(&params, 1, 78, 7, 31);
852 }
853 break;
854 case IPU_PIX_FMT_YVU422P:
855 /* BPP & pixel format */
856 ipu_ch_param_set_field(&params, 1, 85, 4, 1); /* pix format */
857 ipu_ch_param_set_field(&params, 1, 78, 7, 31); /* burst size */
858
859 if (uv_stride < stride / 2)
860 uv_stride = stride / 2;
861
862 v_offset = (v == 0) ? stride * height : v;
863 u_offset = (u == 0) ? v_offset + v_offset / 2 : u;
864 break;
865 case IPU_PIX_FMT_YUV422P:
866 /* BPP & pixel format */
867 ipu_ch_param_set_field(&params, 1, 85, 4, 1); /* pix format */
868 ipu_ch_param_set_field(&params, 1, 78, 7, 31); /* burst size */
869
870 if (uv_stride < stride / 2)
871 uv_stride = stride / 2;
872
873 u_offset = (u == 0) ? stride * height : u;
874 v_offset = (v == 0) ? u_offset + u_offset / 2 : v;
875 break;
876 case IPU_PIX_FMT_NV12:
877 /* BPP & pixel format */
878 ipu_ch_param_set_field(&params, 1, 85, 4, 4); /* pix format */
879 ipu_ch_param_set_field(&params, 1, 78, 7, 31); /* burst size */
880 uv_stride = stride;
881 u_offset = (u == 0) ? stride * height : u;
882 break;
883 default:
884 puts("mxc ipu: unimplemented pixel format\n");
885 break;
886 }
887
888
889 if (uv_stride)
890 ipu_ch_param_set_field(&params, 1, 128, 14, uv_stride - 1);
891
892 /* Get the uv offset from user when need cropping */
893 if (u || v) {
894 u_offset = u;
895 v_offset = v;
896 }
897
898 /* UBO and VBO are 22-bit */
899 if (u_offset/8 > 0x3fffff)
900 puts("The value of U offset exceeds IPU limitation\n");
901 if (v_offset/8 > 0x3fffff)
902 puts("The value of V offset exceeds IPU limitation\n");
903
904 ipu_ch_param_set_field(&params, 0, 46, 22, u_offset / 8);
905 ipu_ch_param_set_field(&params, 0, 68, 22, v_offset / 8);
906
907 debug("initializing idma ch %d @ %p\n", ch, ipu_ch_param_addr(ch));
908 memcpy(ipu_ch_param_addr(ch), &params, sizeof(params));
909};
910
911/*
912 * This function is called to initialize a buffer for logical IPU channel.
913 *
914 * @param channel Input parameter for the logical channel ID.
915 *
916 * @param type Input parameter which buffer to initialize.
917 *
918 * @param pixel_fmt Input parameter for pixel format of buffer.
919 * Pixel format is a FOURCC ASCII code.
920 *
921 * @param width Input parameter for width of buffer in pixels.
922 *
923 * @param height Input parameter for height of buffer in pixels.
924 *
925 * @param stride Input parameter for stride length of buffer
926 * in pixels.
927 *
928 * @param phyaddr_0 Input parameter buffer 0 physical address.
929 *
930 * @param phyaddr_1 Input parameter buffer 1 physical address.
931 * Setting this to a value other than NULL enables
932 * double buffering mode.
933 *
934 * @param u private u offset for additional cropping,
935 * zero if not used.
936 *
937 * @param v private v offset for additional cropping,
938 * zero if not used.
939 *
940 * @return Returns 0 on success or negative error code on fail
941 */
942int32_t ipu_init_channel_buffer(ipu_channel_t channel, ipu_buffer_t type,
943 uint32_t pixel_fmt,
944 uint16_t width, uint16_t height,
945 uint32_t stride,
946 dma_addr_t phyaddr_0, dma_addr_t phyaddr_1,
947 uint32_t u, uint32_t v)
948{
949 uint32_t reg;
950 uint32_t dma_chan;
951
952 dma_chan = channel_2_dma(channel, type);
953 if (!idma_is_valid(dma_chan))
954 return -EINVAL;
955
956 if (stride < width * bytes_per_pixel(pixel_fmt))
957 stride = width * bytes_per_pixel(pixel_fmt);
958
959 if (stride % 4) {
960 printf(
961 "Stride not 32-bit aligned, stride = %d\n", stride);
962 return -EINVAL;
963 }
964 /* Build parameter memory data for DMA channel */
965 ipu_ch_param_init(dma_chan, pixel_fmt, width, height, stride, u, v, 0,
966 phyaddr_0, phyaddr_1);
967
968 if (ipu_is_dmfc_chan(dma_chan)) {
969 ipu_dmfc_set_wait4eot(dma_chan, width);
970 }
971
972 if (idma_is_set(IDMAC_CHA_PRI, dma_chan))
973 ipu_ch_param_set_high_priority(dma_chan);
974
975 ipu_ch_param_dump(dma_chan);
976
977 reg = __raw_readl(IPU_CHA_DB_MODE_SEL(dma_chan));
978 if (phyaddr_1)
979 reg |= idma_mask(dma_chan);
980 else
981 reg &= ~idma_mask(dma_chan);
982 __raw_writel(reg, IPU_CHA_DB_MODE_SEL(dma_chan));
983
984 /* Reset to buffer 0 */
985 __raw_writel(idma_mask(dma_chan), IPU_CHA_CUR_BUF(dma_chan));
986
987 return 0;
988}
989
990/*
991 * This function enables a logical channel.
992 *
993 * @param channel Input parameter for the logical channel ID.
994 *
995 * @return This function returns 0 on success or negative error code on
996 * fail.
997 */
998int32_t ipu_enable_channel(ipu_channel_t channel)
999{
1000 uint32_t reg;
1001 uint32_t in_dma;
1002 uint32_t out_dma;
1003
1004 if (g_channel_enable_mask & (1L << IPU_CHAN_ID(channel))) {
1005 printf("Warning: channel already enabled %d\n",
1006 IPU_CHAN_ID(channel));
1007 }
1008
1009 /* Get input and output dma channels */
1010 out_dma = channel_2_dma(channel, IPU_OUTPUT_BUFFER);
1011 in_dma = channel_2_dma(channel, IPU_VIDEO_IN_BUFFER);
1012
1013 if (idma_is_valid(in_dma)) {
1014 reg = __raw_readl(IDMAC_CHA_EN(in_dma));
1015 __raw_writel(reg | idma_mask(in_dma), IDMAC_CHA_EN(in_dma));
1016 }
1017 if (idma_is_valid(out_dma)) {
1018 reg = __raw_readl(IDMAC_CHA_EN(out_dma));
1019 __raw_writel(reg | idma_mask(out_dma), IDMAC_CHA_EN(out_dma));
1020 }
1021
1022 if ((channel == MEM_DC_SYNC) || (channel == MEM_BG_SYNC) ||
1023 (channel == MEM_FG_SYNC))
1024 ipu_dp_dc_enable(channel);
1025
1026 g_channel_enable_mask |= 1L << IPU_CHAN_ID(channel);
1027
1028 return 0;
1029}
1030
1031/*
1032 * This function clear buffer ready for a logical channel.
1033 *
1034 * @param channel Input parameter for the logical channel ID.
1035 *
1036 * @param type Input parameter which buffer to clear.
1037 *
1038 * @param bufNum Input parameter for which buffer number clear
1039 * ready state.
1040 *
1041 */
1042void ipu_clear_buffer_ready(ipu_channel_t channel, ipu_buffer_t type,
1043 uint32_t bufNum)
1044{
1045 uint32_t dma_ch = channel_2_dma(channel, type);
1046
1047 if (!idma_is_valid(dma_ch))
1048 return;
1049
1050 __raw_writel(0xF0000000, IPU_GPR); /* write one to clear */
1051 if (bufNum == 0) {
1052 if (idma_is_set(IPU_CHA_BUF0_RDY, dma_ch)) {
1053 __raw_writel(idma_mask(dma_ch),
1054 IPU_CHA_BUF0_RDY(dma_ch));
1055 }
1056 } else {
1057 if (idma_is_set(IPU_CHA_BUF1_RDY, dma_ch)) {
1058 __raw_writel(idma_mask(dma_ch),
1059 IPU_CHA_BUF1_RDY(dma_ch));
1060 }
1061 }
1062 __raw_writel(0x0, IPU_GPR); /* write one to set */
1063}
1064
1065/*
1066 * This function disables a logical channel.
1067 *
1068 * @param channel Input parameter for the logical channel ID.
1069 *
1070 * @param wait_for_stop Flag to set whether to wait for channel end
1071 * of frame or return immediately.
1072 *
1073 * @return This function returns 0 on success or negative error code on
1074 * fail.
1075 */
1076int32_t ipu_disable_channel(ipu_channel_t channel)
1077{
1078 uint32_t reg;
1079 uint32_t in_dma;
1080 uint32_t out_dma;
1081
1082 if ((g_channel_enable_mask & (1L << IPU_CHAN_ID(channel))) == 0) {
1083 debug("Channel already disabled %d\n",
1084 IPU_CHAN_ID(channel));
1085 return 0;
1086 }
1087
1088 /* Get input and output dma channels */
1089 out_dma = channel_2_dma(channel, IPU_OUTPUT_BUFFER);
1090 in_dma = channel_2_dma(channel, IPU_VIDEO_IN_BUFFER);
1091
1092 if ((idma_is_valid(in_dma) &&
1093 !idma_is_set(IDMAC_CHA_EN, in_dma))
1094 && (idma_is_valid(out_dma) &&
1095 !idma_is_set(IDMAC_CHA_EN, out_dma)))
1096 return -EINVAL;
1097
1098 if ((channel == MEM_BG_SYNC) || (channel == MEM_FG_SYNC) ||
1099 (channel == MEM_DC_SYNC)) {
1100 ipu_dp_dc_disable(channel, 0);
1101 }
1102
1103 /* Disable DMA channel(s) */
1104 if (idma_is_valid(in_dma)) {
1105 reg = __raw_readl(IDMAC_CHA_EN(in_dma));
1106 __raw_writel(reg & ~idma_mask(in_dma), IDMAC_CHA_EN(in_dma));
1107 __raw_writel(idma_mask(in_dma), IPU_CHA_CUR_BUF(in_dma));
1108 }
1109 if (idma_is_valid(out_dma)) {
1110 reg = __raw_readl(IDMAC_CHA_EN(out_dma));
1111 __raw_writel(reg & ~idma_mask(out_dma), IDMAC_CHA_EN(out_dma));
1112 __raw_writel(idma_mask(out_dma), IPU_CHA_CUR_BUF(out_dma));
1113 }
1114
1115 g_channel_enable_mask &= ~(1L << IPU_CHAN_ID(channel));
1116
1117 /* Set channel buffers NOT to be ready */
1118 if (idma_is_valid(in_dma)) {
1119 ipu_clear_buffer_ready(channel, IPU_VIDEO_IN_BUFFER, 0);
1120 ipu_clear_buffer_ready(channel, IPU_VIDEO_IN_BUFFER, 1);
1121 }
1122 if (idma_is_valid(out_dma)) {
1123 ipu_clear_buffer_ready(channel, IPU_OUTPUT_BUFFER, 0);
1124 ipu_clear_buffer_ready(channel, IPU_OUTPUT_BUFFER, 1);
1125 }
1126
1127 return 0;
1128}
1129
1130uint32_t bytes_per_pixel(uint32_t fmt)
1131{
1132 switch (fmt) {
1133 case IPU_PIX_FMT_GENERIC: /*generic data */
1134 case IPU_PIX_FMT_RGB332:
1135 case IPU_PIX_FMT_YUV420P:
1136 case IPU_PIX_FMT_YUV422P:
1137 return 1;
1138 break;
1139 case IPU_PIX_FMT_RGB565:
1140 case IPU_PIX_FMT_YUYV:
1141 case IPU_PIX_FMT_UYVY:
1142 return 2;
1143 break;
1144 case IPU_PIX_FMT_BGR24:
1145 case IPU_PIX_FMT_RGB24:
1146 return 3;
1147 break;
1148 case IPU_PIX_FMT_GENERIC_32: /*generic data */
1149 case IPU_PIX_FMT_BGR32:
1150 case IPU_PIX_FMT_BGRA32:
1151 case IPU_PIX_FMT_RGB32:
1152 case IPU_PIX_FMT_RGBA32:
1153 case IPU_PIX_FMT_ABGR32:
1154 return 4;
1155 break;
1156 default:
1157 return 1;
1158 break;
1159 }
1160 return 0;
1161}
1162
1163ipu_color_space_t format_to_colorspace(uint32_t fmt)
1164{
1165 switch (fmt) {
1166 case IPU_PIX_FMT_RGB666:
1167 case IPU_PIX_FMT_RGB565:
1168 case IPU_PIX_FMT_BGR24:
1169 case IPU_PIX_FMT_RGB24:
1170 case IPU_PIX_FMT_BGR32:
1171 case IPU_PIX_FMT_BGRA32:
1172 case IPU_PIX_FMT_RGB32:
1173 case IPU_PIX_FMT_RGBA32:
1174 case IPU_PIX_FMT_ABGR32:
1175 case IPU_PIX_FMT_LVDS666:
1176 case IPU_PIX_FMT_LVDS888:
1177 return RGB;
1178 break;
1179
1180 default:
1181 return YCbCr;
1182 break;
1183 }
1184 return RGB;
1185}