blob: bd1ef0a800dc957b987f70c9641152021f1320e1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefano Babiced5939d2010-10-13 12:16:35 +02002/*
3 * Porting to u-boot:
4 *
5 * (C) Copyright 2010
6 * Stefano Babic, DENX Software Engineering, sbabic@denx.de
7 *
8 * Linux IPU driver for MX51:
9 *
10 * (C) Copyright 2005-2010 Freescale Semiconductor, Inc.
Stefano Babiced5939d2010-10-13 12:16:35 +020011 */
12
13/* #define DEBUG */
Tom Rinidec7ea02024-05-20 13:35:03 -060014#include <config.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glassdbd79542020-05-10 11:40:11 -060016#include <linux/delay.h>
Stefano Babiced5939d2010-10-13 12:16:35 +020017#include <linux/types.h>
18#include <linux/err.h>
19#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090020#include <linux/errno.h>
Stefano Babiced5939d2010-10-13 12:16:35 +020021#include <asm/arch/imx-regs.h>
22#include <asm/arch/crm_regs.h>
Fabio Estevam331fcb52017-09-06 13:49:31 -030023#include <asm/arch/sys_proto.h>
Peng Fanb2e31502016-03-09 16:07:21 +080024#include <div64.h>
Stefano Babiced5939d2010-10-13 12:16:35 +020025#include "ipu.h"
26#include "ipu_regs.h"
27
28extern struct mxc_ccm_reg *mxc_ccm;
29extern u32 *ipu_cpmem_base;
30
31struct ipu_ch_param_word {
32 uint32_t data[5];
33 uint32_t res[3];
34};
35
36struct ipu_ch_param {
37 struct ipu_ch_param_word word[2];
38};
39
40#define ipu_ch_param_addr(ch) (((struct ipu_ch_param *)ipu_cpmem_base) + (ch))
41
42#define _param_word(base, w) \
43 (((struct ipu_ch_param *)(base))->word[(w)].data)
44
45#define ipu_ch_param_set_field(base, w, bit, size, v) { \
46 int i = (bit) / 32; \
47 int off = (bit) % 32; \
48 _param_word(base, w)[i] |= (v) << off; \
49 if (((bit) + (size) - 1) / 32 > i) { \
50 _param_word(base, w)[i + 1] |= (v) >> (off ? (32 - off) : 0); \
51 } \
52}
53
54#define ipu_ch_param_mod_field(base, w, bit, size, v) { \
55 int i = (bit) / 32; \
56 int off = (bit) % 32; \
57 u32 mask = (1UL << size) - 1; \
58 u32 temp = _param_word(base, w)[i]; \
59 temp &= ~(mask << off); \
60 _param_word(base, w)[i] = temp | (v) << off; \
61 if (((bit) + (size) - 1) / 32 > i) { \
62 temp = _param_word(base, w)[i + 1]; \
63 temp &= ~(mask >> (32 - off)); \
64 _param_word(base, w)[i + 1] = \
65 temp | ((v) >> (off ? (32 - off) : 0)); \
66 } \
67}
68
69#define ipu_ch_param_read_field(base, w, bit, size) ({ \
70 u32 temp2; \
71 int i = (bit) / 32; \
72 int off = (bit) % 32; \
73 u32 mask = (1UL << size) - 1; \
74 u32 temp1 = _param_word(base, w)[i]; \
75 temp1 = mask & (temp1 >> off); \
76 if (((bit)+(size) - 1) / 32 > i) { \
77 temp2 = _param_word(base, w)[i + 1]; \
78 temp2 &= mask >> (off ? (32 - off) : 0); \
79 temp1 |= temp2 << (off ? (32 - off) : 0); \
80 } \
81 temp1; \
82})
83
Liu Yingdec2aef2012-10-06 04:16:04 +000084#define IPU_SW_RST_TOUT_USEC (10000)
Stefano Babiced5939d2010-10-13 12:16:35 +020085
Fabio Estevam331fcb52017-09-06 13:49:31 -030086#define IPUV3_CLK_MX51 133000000
87#define IPUV3_CLK_MX53 200000000
88#define IPUV3_CLK_MX6Q 264000000
89#define IPUV3_CLK_MX6DL 198000000
90
Stefano Babiced5939d2010-10-13 12:16:35 +020091void clk_enable(struct clk *clk)
92{
93 if (clk) {
94 if (clk->usecount++ == 0) {
95 clk->enable(clk);
96 }
97 }
98}
99
100void clk_disable(struct clk *clk)
101{
102 if (clk) {
103 if (!(--clk->usecount)) {
104 if (clk->disable)
105 clk->disable(clk);
106 }
107 }
108}
109
110int clk_get_usecount(struct clk *clk)
111{
112 if (clk == NULL)
113 return 0;
114
115 return clk->usecount;
116}
117
118u32 clk_get_rate(struct clk *clk)
119{
120 if (!clk)
121 return 0;
122
123 return clk->rate;
124}
125
126struct clk *clk_get_parent(struct clk *clk)
127{
128 if (!clk)
129 return 0;
130
131 return clk->parent;
132}
133
134int clk_set_rate(struct clk *clk, unsigned long rate)
135{
Peng Fanfcb41e22018-01-02 15:25:36 +0800136 if (!clk)
137 return 0;
138
139 if (clk->set_rate)
Stefano Babiced5939d2010-10-13 12:16:35 +0200140 clk->set_rate(clk, rate);
Peng Fanfcb41e22018-01-02 15:25:36 +0800141
Stefano Babiced5939d2010-10-13 12:16:35 +0200142 return clk->rate;
143}
144
145long clk_round_rate(struct clk *clk, unsigned long rate)
146{
147 if (clk == NULL || !clk->round_rate)
148 return 0;
149
150 return clk->round_rate(clk, rate);
151}
152
153int clk_set_parent(struct clk *clk, struct clk *parent)
154{
155 clk->parent = parent;
156 if (clk->set_parent)
157 return clk->set_parent(clk, parent);
158 return 0;
159}
160
161static int clk_ipu_enable(struct clk *clk)
162{
163 u32 reg;
164
165 reg = __raw_readl(clk->enable_reg);
166 reg |= MXC_CCM_CCGR_CG_MASK << clk->enable_shift;
167 __raw_writel(reg, clk->enable_reg);
168
Eric Nelsone4279542012-09-21 07:33:51 +0000169#if defined(CONFIG_MX51) || defined(CONFIG_MX53)
Stefano Babiced5939d2010-10-13 12:16:35 +0200170 /* Handshake with IPU when certain clock rates are changed. */
171 reg = __raw_readl(&mxc_ccm->ccdr);
172 reg &= ~MXC_CCM_CCDR_IPU_HS_MASK;
173 __raw_writel(reg, &mxc_ccm->ccdr);
174
175 /* Handshake with IPU when LPM is entered as its enabled. */
176 reg = __raw_readl(&mxc_ccm->clpcr);
177 reg &= ~MXC_CCM_CLPCR_BYPASS_IPU_LPM_HS;
178 __raw_writel(reg, &mxc_ccm->clpcr);
Fabio Estevam581a17e2012-05-31 07:23:58 +0000179#endif
Stefano Babiced5939d2010-10-13 12:16:35 +0200180 return 0;
181}
182
183static void clk_ipu_disable(struct clk *clk)
184{
185 u32 reg;
186
187 reg = __raw_readl(clk->enable_reg);
188 reg &= ~(MXC_CCM_CCGR_CG_MASK << clk->enable_shift);
189 __raw_writel(reg, clk->enable_reg);
190
Eric Nelsone4279542012-09-21 07:33:51 +0000191#if defined(CONFIG_MX51) || defined(CONFIG_MX53)
Stefano Babiced5939d2010-10-13 12:16:35 +0200192 /*
193 * No handshake with IPU whe dividers are changed
194 * as its not enabled.
195 */
196 reg = __raw_readl(&mxc_ccm->ccdr);
197 reg |= MXC_CCM_CCDR_IPU_HS_MASK;
198 __raw_writel(reg, &mxc_ccm->ccdr);
199
200 /* No handshake with IPU when LPM is entered as its not enabled. */
201 reg = __raw_readl(&mxc_ccm->clpcr);
202 reg |= MXC_CCM_CLPCR_BYPASS_IPU_LPM_HS;
203 __raw_writel(reg, &mxc_ccm->clpcr);
Fabio Estevam581a17e2012-05-31 07:23:58 +0000204#endif
Stefano Babiced5939d2010-10-13 12:16:35 +0200205}
206
Stefano Babiced5939d2010-10-13 12:16:35 +0200207static struct clk ipu_clk = {
208 .name = "ipu_clk",
Eric Nelsone4279542012-09-21 07:33:51 +0000209#if defined(CONFIG_MX51) || defined(CONFIG_MX53)
Fabio Estevam2e1b9292012-05-31 07:23:59 +0000210 .enable_reg = (u32 *)(CCM_BASE_ADDR +
Stefano Babiced5939d2010-10-13 12:16:35 +0200211 offsetof(struct mxc_ccm_reg, CCGR5)),
Benoît Thébaudeau461a00a2012-09-27 10:21:00 +0000212 .enable_shift = MXC_CCM_CCGR5_IPU_OFFSET,
Eric Nelsone4279542012-09-21 07:33:51 +0000213#else
214 .enable_reg = (u32 *)(CCM_BASE_ADDR +
215 offsetof(struct mxc_ccm_reg, CCGR3)),
216 .enable_shift = MXC_CCM_CCGR3_IPU1_IPU_DI0_OFFSET,
217#endif
Stefano Babiced5939d2010-10-13 12:16:35 +0200218 .enable = clk_ipu_enable,
219 .disable = clk_ipu_disable,
220 .usecount = 0,
221};
222
Tom Rini6a5dccc2022-11-16 13:10:41 -0500223#if !defined CFG_SYS_LDB_CLOCK
224#define CFG_SYS_LDB_CLOCK 65000000
Heiko Schocher086f0812015-04-20 07:52:21 +0200225#endif
226
Eric Nelson5018c552012-05-31 07:24:02 +0000227static struct clk ldb_clk = {
228 .name = "ldb_clk",
Tom Rini6a5dccc2022-11-16 13:10:41 -0500229 .rate = CFG_SYS_LDB_CLOCK,
Eric Nelson5018c552012-05-31 07:24:02 +0000230 .usecount = 0,
231};
232
Stefano Babiced5939d2010-10-13 12:16:35 +0200233/* Globals */
234struct clk *g_ipu_clk;
Eric Nelson5018c552012-05-31 07:24:02 +0000235struct clk *g_ldb_clk;
Stefano Babiced5939d2010-10-13 12:16:35 +0200236unsigned char g_ipu_clk_enabled;
237struct clk *g_di_clk[2];
238struct clk *g_pixel_clk[2];
239unsigned char g_dc_di_assignment[10];
240uint32_t g_channel_init_mask;
241uint32_t g_channel_enable_mask;
242
243static int ipu_dc_use_count;
244static int ipu_dp_use_count;
245static int ipu_dmfc_use_count;
246static int ipu_di_use_count[2];
247
248u32 *ipu_cpmem_base;
249u32 *ipu_dc_tmpl_reg;
250
251/* Static functions */
252
253static inline void ipu_ch_param_set_high_priority(uint32_t ch)
254{
255 ipu_ch_param_mod_field(ipu_ch_param_addr(ch), 1, 93, 2, 1);
256};
257
258static inline uint32_t channel_2_dma(ipu_channel_t ch, ipu_buffer_t type)
259{
260 return ((uint32_t) ch >> (6 * type)) & 0x3F;
261};
262
263/* Either DP BG or DP FG can be graphic window */
264static inline int ipu_is_dp_graphic_chan(uint32_t dma_chan)
265{
266 return (dma_chan == 23 || dma_chan == 27);
267}
268
269static inline int ipu_is_dmfc_chan(uint32_t dma_chan)
270{
271 return ((dma_chan >= 23) && (dma_chan <= 29));
272}
273
Stefano Babiced5939d2010-10-13 12:16:35 +0200274static inline void ipu_ch_param_set_buffer(uint32_t ch, int bufNum,
275 dma_addr_t phyaddr)
276{
277 ipu_ch_param_mod_field(ipu_ch_param_addr(ch), 1, 29 * bufNum, 29,
278 phyaddr / 8);
279};
280
281#define idma_is_valid(ch) (ch != NO_DMA)
282#define idma_mask(ch) (idma_is_valid(ch) ? (1UL << (ch & 0x1F)) : 0)
283#define idma_is_set(reg, dma) (__raw_readl(reg(dma)) & idma_mask(dma))
284
285static void ipu_pixel_clk_recalc(struct clk *clk)
286{
Peng Fanb2e31502016-03-09 16:07:21 +0800287 u32 div;
288 u64 final_rate = (unsigned long long)clk->parent->rate * 16;
289
290 div = __raw_readl(DI_BS_CLKGEN0(clk->id));
291 debug("read BS_CLKGEN0 div:%d, final_rate:%lld, prate:%ld\n",
292 div, final_rate, clk->parent->rate);
293
294 clk->rate = 0;
295 if (div != 0) {
296 do_div(final_rate, div);
297 clk->rate = final_rate;
298 }
Stefano Babiced5939d2010-10-13 12:16:35 +0200299}
300
301static unsigned long ipu_pixel_clk_round_rate(struct clk *clk,
302 unsigned long rate)
303{
Peng Fanb2e31502016-03-09 16:07:21 +0800304 u64 div, final_rate;
305 u32 remainder;
306 u64 parent_rate = (unsigned long long)clk->parent->rate * 16;
307
Stefano Babiced5939d2010-10-13 12:16:35 +0200308 /*
309 * Calculate divider
310 * Fractional part is 4 bits,
311 * so simply multiply by 2^4 to get fractional part.
312 */
Peng Fanb2e31502016-03-09 16:07:21 +0800313 div = parent_rate;
314 remainder = do_div(div, rate);
315 /* Round the divider value */
316 if (remainder > (rate / 2))
317 div++;
Stefano Babiced5939d2010-10-13 12:16:35 +0200318 if (div < 0x10) /* Min DI disp clock divider is 1 */
319 div = 0x10;
320 if (div & ~0xFEF)
321 div &= 0xFF8;
322 else {
Peng Fanb2e31502016-03-09 16:07:21 +0800323 /* Round up divider if it gets us closer to desired pix clk */
324 if ((div & 0xC) == 0xC) {
325 div += 0x10;
326 div &= ~0xF;
327 }
Stefano Babiced5939d2010-10-13 12:16:35 +0200328 }
Peng Fanb2e31502016-03-09 16:07:21 +0800329 final_rate = parent_rate;
330 do_div(final_rate, div);
331
332 return final_rate;
Stefano Babiced5939d2010-10-13 12:16:35 +0200333}
334
335static int ipu_pixel_clk_set_rate(struct clk *clk, unsigned long rate)
336{
Peng Fanb2e31502016-03-09 16:07:21 +0800337 u64 div, parent_rate;
338 u32 remainder;
339
340 parent_rate = (unsigned long long)clk->parent->rate * 16;
341 div = parent_rate;
342 remainder = do_div(div, rate);
343 /* Round the divider value */
344 if (remainder > (rate / 2))
345 div++;
346
347 /* Round up divider if it gets us closer to desired pix clk */
348 if ((div & 0xC) == 0xC) {
349 div += 0x10;
350 div &= ~0xF;
351 }
352 if (div > 0x1000)
353 debug("Overflow, DI_BS_CLKGEN0 div:0x%x\n", (u32)div);
Stefano Babiced5939d2010-10-13 12:16:35 +0200354
355 __raw_writel(div, DI_BS_CLKGEN0(clk->id));
356
Peng Fanb2e31502016-03-09 16:07:21 +0800357 /*
358 * Setup pixel clock timing
359 * Down time is half of period
360 */
Stefano Babiced5939d2010-10-13 12:16:35 +0200361 __raw_writel((div / 16) << 16, DI_BS_CLKGEN1(clk->id));
362
Peng Fan93fd8c22016-04-28 10:07:53 +0800363 do_div(parent_rate, div);
364
365 clk->rate = parent_rate;
Peng Fanb2e31502016-03-09 16:07:21 +0800366
Stefano Babiced5939d2010-10-13 12:16:35 +0200367 return 0;
368}
369
370static int ipu_pixel_clk_enable(struct clk *clk)
371{
372 u32 disp_gen = __raw_readl(IPU_DISP_GEN);
373 disp_gen |= clk->id ? DI1_COUNTER_RELEASE : DI0_COUNTER_RELEASE;
374 __raw_writel(disp_gen, IPU_DISP_GEN);
375
376 return 0;
377}
378
379static void ipu_pixel_clk_disable(struct clk *clk)
380{
381 u32 disp_gen = __raw_readl(IPU_DISP_GEN);
382 disp_gen &= clk->id ? ~DI1_COUNTER_RELEASE : ~DI0_COUNTER_RELEASE;
383 __raw_writel(disp_gen, IPU_DISP_GEN);
384
385}
386
387static int ipu_pixel_clk_set_parent(struct clk *clk, struct clk *parent)
388{
389 u32 di_gen = __raw_readl(DI_GENERAL(clk->id));
390
391 if (parent == g_ipu_clk)
392 di_gen &= ~DI_GEN_DI_CLK_EXT;
Eric Nelson5018c552012-05-31 07:24:02 +0000393 else if (!IS_ERR(g_di_clk[clk->id]) && parent == g_ldb_clk)
Stefano Babiced5939d2010-10-13 12:16:35 +0200394 di_gen |= DI_GEN_DI_CLK_EXT;
395 else
396 return -EINVAL;
397
398 __raw_writel(di_gen, DI_GENERAL(clk->id));
399 ipu_pixel_clk_recalc(clk);
400 return 0;
401}
402
403static struct clk pixel_clk[] = {
404 {
405 .name = "pixel_clk",
406 .id = 0,
407 .recalc = ipu_pixel_clk_recalc,
408 .set_rate = ipu_pixel_clk_set_rate,
409 .round_rate = ipu_pixel_clk_round_rate,
410 .set_parent = ipu_pixel_clk_set_parent,
411 .enable = ipu_pixel_clk_enable,
412 .disable = ipu_pixel_clk_disable,
413 .usecount = 0,
414 },
415 {
416 .name = "pixel_clk",
417 .id = 1,
418 .recalc = ipu_pixel_clk_recalc,
419 .set_rate = ipu_pixel_clk_set_rate,
420 .round_rate = ipu_pixel_clk_round_rate,
421 .set_parent = ipu_pixel_clk_set_parent,
422 .enable = ipu_pixel_clk_enable,
423 .disable = ipu_pixel_clk_disable,
424 .usecount = 0,
425 },
426};
427
428/*
429 * This function resets IPU
430 */
Jeroen Hofsteea175d302014-10-08 22:57:47 +0200431static void ipu_reset(void)
Stefano Babiced5939d2010-10-13 12:16:35 +0200432{
433 u32 *reg;
434 u32 value;
Liu Yingdec2aef2012-10-06 04:16:04 +0000435 int timeout = IPU_SW_RST_TOUT_USEC;
Stefano Babiced5939d2010-10-13 12:16:35 +0200436
437 reg = (u32 *)SRC_BASE_ADDR;
438 value = __raw_readl(reg);
439 value = value | SW_IPU_RST;
440 __raw_writel(value, reg);
Liu Yingdec2aef2012-10-06 04:16:04 +0000441
442 while (__raw_readl(reg) & SW_IPU_RST) {
443 udelay(1);
444 if (!(timeout--)) {
445 printf("ipu software reset timeout\n");
446 break;
447 }
448 };
Stefano Babiced5939d2010-10-13 12:16:35 +0200449}
450
451/*
452 * This function is called by the driver framework to initialize the IPU
453 * hardware.
454 *
455 * @param dev The device structure for the IPU passed in by the
456 * driver framework.
457 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100458 * Return: Returns 0 on success or negative error code on error
Stefano Babiced5939d2010-10-13 12:16:35 +0200459 */
460int ipu_probe(void)
461{
462 unsigned long ipu_base;
Fabio Estevam0dfdcac2012-05-31 07:23:57 +0000463#if defined CONFIG_MX51
Stefano Babiced5939d2010-10-13 12:16:35 +0200464 u32 temp;
465
466 u32 *reg_hsc_mcd = (u32 *)MIPI_HSC_BASE_ADDR;
467 u32 *reg_hsc_mxt_conf = (u32 *)(MIPI_HSC_BASE_ADDR + 0x800);
468
469 __raw_writel(0xF00, reg_hsc_mcd);
470
471 /* CSI mode reserved*/
472 temp = __raw_readl(reg_hsc_mxt_conf);
473 __raw_writel(temp | 0x0FF, reg_hsc_mxt_conf);
474
475 temp = __raw_readl(reg_hsc_mxt_conf);
476 __raw_writel(temp | 0x10000, reg_hsc_mxt_conf);
Fabio Estevam0dfdcac2012-05-31 07:23:57 +0000477#endif
Stefano Babiced5939d2010-10-13 12:16:35 +0200478
479 ipu_base = IPU_CTRL_BASE_ADDR;
480 ipu_cpmem_base = (u32 *)(ipu_base + IPU_CPMEM_REG_BASE);
481 ipu_dc_tmpl_reg = (u32 *)(ipu_base + IPU_DC_TMPL_REG_BASE);
482
483 g_pixel_clk[0] = &pixel_clk[0];
484 g_pixel_clk[1] = &pixel_clk[1];
485
486 g_ipu_clk = &ipu_clk;
Fabio Estevam331fcb52017-09-06 13:49:31 -0300487#if defined(CONFIG_MX51)
488 g_ipu_clk->rate = IPUV3_CLK_MX51;
489#elif defined(CONFIG_MX53)
490 g_ipu_clk->rate = IPUV3_CLK_MX53;
491#else
492 g_ipu_clk->rate = is_mx6sdl() ? IPUV3_CLK_MX6DL : IPUV3_CLK_MX6Q;
493#endif
Stefano Babiced5939d2010-10-13 12:16:35 +0200494 debug("ipu_clk = %u\n", clk_get_rate(g_ipu_clk));
Eric Nelson5018c552012-05-31 07:24:02 +0000495 g_ldb_clk = &ldb_clk;
496 debug("ldb_clk = %u\n", clk_get_rate(g_ldb_clk));
Stefano Babiced5939d2010-10-13 12:16:35 +0200497 ipu_reset();
498
499 clk_set_parent(g_pixel_clk[0], g_ipu_clk);
500 clk_set_parent(g_pixel_clk[1], g_ipu_clk);
501 clk_enable(g_ipu_clk);
502
503 g_di_clk[0] = NULL;
504 g_di_clk[1] = NULL;
505
506 __raw_writel(0x807FFFFF, IPU_MEM_RST);
507 while (__raw_readl(IPU_MEM_RST) & 0x80000000)
508 ;
509
510 ipu_init_dc_mappings();
511
512 __raw_writel(0, IPU_INT_CTRL(5));
513 __raw_writel(0, IPU_INT_CTRL(6));
514 __raw_writel(0, IPU_INT_CTRL(9));
515 __raw_writel(0, IPU_INT_CTRL(10));
516
517 /* DMFC Init */
518 ipu_dmfc_init(DMFC_NORMAL, 1);
519
520 /* Set sync refresh channels as high priority */
521 __raw_writel(0x18800000L, IDMAC_CHA_PRI(0));
522
523 /* Set MCU_T to divide MCU access window into 2 */
524 __raw_writel(0x00400000L | (IPU_MCU_T_DEFAULT << 18), IPU_DISP_GEN);
525
526 clk_disable(g_ipu_clk);
527
528 return 0;
529}
530
531void ipu_dump_registers(void)
532{
533 debug("IPU_CONF = \t0x%08X\n", __raw_readl(IPU_CONF));
534 debug("IDMAC_CONF = \t0x%08X\n", __raw_readl(IDMAC_CONF));
535 debug("IDMAC_CHA_EN1 = \t0x%08X\n",
536 __raw_readl(IDMAC_CHA_EN(0)));
537 debug("IDMAC_CHA_EN2 = \t0x%08X\n",
538 __raw_readl(IDMAC_CHA_EN(32)));
539 debug("IDMAC_CHA_PRI1 = \t0x%08X\n",
540 __raw_readl(IDMAC_CHA_PRI(0)));
541 debug("IDMAC_CHA_PRI2 = \t0x%08X\n",
542 __raw_readl(IDMAC_CHA_PRI(32)));
543 debug("IPU_CHA_DB_MODE_SEL0 = \t0x%08X\n",
544 __raw_readl(IPU_CHA_DB_MODE_SEL(0)));
545 debug("IPU_CHA_DB_MODE_SEL1 = \t0x%08X\n",
546 __raw_readl(IPU_CHA_DB_MODE_SEL(32)));
547 debug("DMFC_WR_CHAN = \t0x%08X\n",
548 __raw_readl(DMFC_WR_CHAN));
549 debug("DMFC_WR_CHAN_DEF = \t0x%08X\n",
550 __raw_readl(DMFC_WR_CHAN_DEF));
551 debug("DMFC_DP_CHAN = \t0x%08X\n",
552 __raw_readl(DMFC_DP_CHAN));
553 debug("DMFC_DP_CHAN_DEF = \t0x%08X\n",
554 __raw_readl(DMFC_DP_CHAN_DEF));
555 debug("DMFC_IC_CTRL = \t0x%08X\n",
556 __raw_readl(DMFC_IC_CTRL));
557 debug("IPU_FS_PROC_FLOW1 = \t0x%08X\n",
558 __raw_readl(IPU_FS_PROC_FLOW1));
559 debug("IPU_FS_PROC_FLOW2 = \t0x%08X\n",
560 __raw_readl(IPU_FS_PROC_FLOW2));
561 debug("IPU_FS_PROC_FLOW3 = \t0x%08X\n",
562 __raw_readl(IPU_FS_PROC_FLOW3));
563 debug("IPU_FS_DISP_FLOW1 = \t0x%08X\n",
564 __raw_readl(IPU_FS_DISP_FLOW1));
565}
566
567/*
568 * This function is called to initialize a logical IPU channel.
569 *
570 * @param channel Input parameter for the logical channel ID to init.
571 *
572 * @param params Input parameter containing union of channel
573 * initialization parameters.
574 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100575 * Return: Returns 0 on success or negative error code on fail
Stefano Babiced5939d2010-10-13 12:16:35 +0200576 */
577int32_t ipu_init_channel(ipu_channel_t channel, ipu_channel_params_t *params)
578{
579 int ret = 0;
580 uint32_t ipu_conf;
581
582 debug("init channel = %d\n", IPU_CHAN_ID(channel));
583
584 if (g_ipu_clk_enabled == 0) {
585 g_ipu_clk_enabled = 1;
586 clk_enable(g_ipu_clk);
587 }
588
Stefano Babiced5939d2010-10-13 12:16:35 +0200589 if (g_channel_init_mask & (1L << IPU_CHAN_ID(channel))) {
590 printf("Warning: channel already initialized %d\n",
591 IPU_CHAN_ID(channel));
592 }
593
594 ipu_conf = __raw_readl(IPU_CONF);
595
596 switch (channel) {
597 case MEM_DC_SYNC:
598 if (params->mem_dc_sync.di > 1) {
599 ret = -EINVAL;
600 goto err;
601 }
602
603 g_dc_di_assignment[1] = params->mem_dc_sync.di;
604 ipu_dc_init(1, params->mem_dc_sync.di,
605 params->mem_dc_sync.interlaced);
606 ipu_di_use_count[params->mem_dc_sync.di]++;
607 ipu_dc_use_count++;
608 ipu_dmfc_use_count++;
609 break;
610 case MEM_BG_SYNC:
611 if (params->mem_dp_bg_sync.di > 1) {
612 ret = -EINVAL;
613 goto err;
614 }
615
616 g_dc_di_assignment[5] = params->mem_dp_bg_sync.di;
617 ipu_dp_init(channel, params->mem_dp_bg_sync.in_pixel_fmt,
618 params->mem_dp_bg_sync.out_pixel_fmt);
619 ipu_dc_init(5, params->mem_dp_bg_sync.di,
620 params->mem_dp_bg_sync.interlaced);
621 ipu_di_use_count[params->mem_dp_bg_sync.di]++;
622 ipu_dc_use_count++;
623 ipu_dp_use_count++;
624 ipu_dmfc_use_count++;
625 break;
626 case MEM_FG_SYNC:
627 ipu_dp_init(channel, params->mem_dp_fg_sync.in_pixel_fmt,
628 params->mem_dp_fg_sync.out_pixel_fmt);
629
630 ipu_dc_use_count++;
631 ipu_dp_use_count++;
632 ipu_dmfc_use_count++;
633 break;
634 default:
635 printf("Missing channel initialization\n");
636 break;
637 }
638
639 /* Enable IPU sub module */
640 g_channel_init_mask |= 1L << IPU_CHAN_ID(channel);
641 if (ipu_dc_use_count == 1)
642 ipu_conf |= IPU_CONF_DC_EN;
643 if (ipu_dp_use_count == 1)
644 ipu_conf |= IPU_CONF_DP_EN;
645 if (ipu_dmfc_use_count == 1)
646 ipu_conf |= IPU_CONF_DMFC_EN;
647 if (ipu_di_use_count[0] == 1) {
648 ipu_conf |= IPU_CONF_DI0_EN;
649 }
650 if (ipu_di_use_count[1] == 1) {
651 ipu_conf |= IPU_CONF_DI1_EN;
652 }
653
654 __raw_writel(ipu_conf, IPU_CONF);
655
656err:
657 return ret;
658}
659
660/*
661 * This function is called to uninitialize a logical IPU channel.
662 *
663 * @param channel Input parameter for the logical channel ID to uninit.
664 */
665void ipu_uninit_channel(ipu_channel_t channel)
666{
667 uint32_t reg;
668 uint32_t in_dma, out_dma = 0;
669 uint32_t ipu_conf;
670
671 if ((g_channel_init_mask & (1L << IPU_CHAN_ID(channel))) == 0) {
672 debug("Channel already uninitialized %d\n",
673 IPU_CHAN_ID(channel));
674 return;
675 }
676
677 /*
678 * Make sure channel is disabled
679 * Get input and output dma channels
680 */
681 in_dma = channel_2_dma(channel, IPU_OUTPUT_BUFFER);
682 out_dma = channel_2_dma(channel, IPU_VIDEO_IN_BUFFER);
683
684 if (idma_is_set(IDMAC_CHA_EN, in_dma) ||
685 idma_is_set(IDMAC_CHA_EN, out_dma)) {
686 printf(
687 "Channel %d is not disabled, disable first\n",
688 IPU_CHAN_ID(channel));
689 return;
690 }
691
692 ipu_conf = __raw_readl(IPU_CONF);
693
694 /* Reset the double buffer */
695 reg = __raw_readl(IPU_CHA_DB_MODE_SEL(in_dma));
696 __raw_writel(reg & ~idma_mask(in_dma), IPU_CHA_DB_MODE_SEL(in_dma));
697 reg = __raw_readl(IPU_CHA_DB_MODE_SEL(out_dma));
698 __raw_writel(reg & ~idma_mask(out_dma), IPU_CHA_DB_MODE_SEL(out_dma));
699
700 switch (channel) {
701 case MEM_DC_SYNC:
702 ipu_dc_uninit(1);
703 ipu_di_use_count[g_dc_di_assignment[1]]--;
704 ipu_dc_use_count--;
705 ipu_dmfc_use_count--;
706 break;
707 case MEM_BG_SYNC:
708 ipu_dp_uninit(channel);
709 ipu_dc_uninit(5);
710 ipu_di_use_count[g_dc_di_assignment[5]]--;
711 ipu_dc_use_count--;
712 ipu_dp_use_count--;
713 ipu_dmfc_use_count--;
714 break;
715 case MEM_FG_SYNC:
716 ipu_dp_uninit(channel);
717 ipu_dc_use_count--;
718 ipu_dp_use_count--;
719 ipu_dmfc_use_count--;
720 break;
721 default:
722 break;
723 }
724
725 g_channel_init_mask &= ~(1L << IPU_CHAN_ID(channel));
726
727 if (ipu_dc_use_count == 0)
728 ipu_conf &= ~IPU_CONF_DC_EN;
729 if (ipu_dp_use_count == 0)
730 ipu_conf &= ~IPU_CONF_DP_EN;
731 if (ipu_dmfc_use_count == 0)
732 ipu_conf &= ~IPU_CONF_DMFC_EN;
733 if (ipu_di_use_count[0] == 0) {
734 ipu_conf &= ~IPU_CONF_DI0_EN;
735 }
736 if (ipu_di_use_count[1] == 0) {
737 ipu_conf &= ~IPU_CONF_DI1_EN;
738 }
739
740 __raw_writel(ipu_conf, IPU_CONF);
741
742 if (ipu_conf == 0) {
743 clk_disable(g_ipu_clk);
744 g_ipu_clk_enabled = 0;
745 }
746
747}
748
749static inline void ipu_ch_param_dump(int ch)
750{
751#ifdef DEBUG
752 struct ipu_ch_param *p = ipu_ch_param_addr(ch);
753 debug("ch %d word 0 - %08X %08X %08X %08X %08X\n", ch,
754 p->word[0].data[0], p->word[0].data[1], p->word[0].data[2],
755 p->word[0].data[3], p->word[0].data[4]);
756 debug("ch %d word 1 - %08X %08X %08X %08X %08X\n", ch,
757 p->word[1].data[0], p->word[1].data[1], p->word[1].data[2],
758 p->word[1].data[3], p->word[1].data[4]);
759 debug("PFS 0x%x, ",
760 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 85, 4));
761 debug("BPP 0x%x, ",
762 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 0, 107, 3));
763 debug("NPB 0x%x\n",
764 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 78, 7));
765
766 debug("FW %d, ",
767 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 0, 125, 13));
768 debug("FH %d, ",
769 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 0, 138, 12));
770 debug("Stride %d\n",
771 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 102, 14));
772
773 debug("Width0 %d+1, ",
774 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 116, 3));
775 debug("Width1 %d+1, ",
776 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 119, 3));
777 debug("Width2 %d+1, ",
778 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 122, 3));
779 debug("Width3 %d+1, ",
780 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 125, 3));
781 debug("Offset0 %d, ",
782 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 128, 5));
783 debug("Offset1 %d, ",
784 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 133, 5));
785 debug("Offset2 %d, ",
786 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 138, 5));
787 debug("Offset3 %d\n",
788 ipu_ch_param_read_field(ipu_ch_param_addr(ch), 1, 143, 5));
789#endif
790}
791
792static inline void ipu_ch_params_set_packing(struct ipu_ch_param *p,
793 int red_width, int red_offset,
794 int green_width, int green_offset,
795 int blue_width, int blue_offset,
796 int alpha_width, int alpha_offset)
797{
798 /* Setup red width and offset */
799 ipu_ch_param_set_field(p, 1, 116, 3, red_width - 1);
800 ipu_ch_param_set_field(p, 1, 128, 5, red_offset);
801 /* Setup green width and offset */
802 ipu_ch_param_set_field(p, 1, 119, 3, green_width - 1);
803 ipu_ch_param_set_field(p, 1, 133, 5, green_offset);
804 /* Setup blue width and offset */
805 ipu_ch_param_set_field(p, 1, 122, 3, blue_width - 1);
806 ipu_ch_param_set_field(p, 1, 138, 5, blue_offset);
807 /* Setup alpha width and offset */
808 ipu_ch_param_set_field(p, 1, 125, 3, alpha_width - 1);
809 ipu_ch_param_set_field(p, 1, 143, 5, alpha_offset);
810}
811
812static void ipu_ch_param_init(int ch,
813 uint32_t pixel_fmt, uint32_t width,
814 uint32_t height, uint32_t stride,
815 uint32_t u, uint32_t v,
816 uint32_t uv_stride, dma_addr_t addr0,
817 dma_addr_t addr1)
818{
819 uint32_t u_offset = 0;
820 uint32_t v_offset = 0;
821 struct ipu_ch_param params;
822
823 memset(&params, 0, sizeof(params));
824
825 ipu_ch_param_set_field(&params, 0, 125, 13, width - 1);
826
827 if ((ch == 8) || (ch == 9) || (ch == 10)) {
828 ipu_ch_param_set_field(&params, 0, 138, 12, (height / 2) - 1);
829 ipu_ch_param_set_field(&params, 1, 102, 14, (stride * 2) - 1);
830 } else {
831 ipu_ch_param_set_field(&params, 0, 138, 12, height - 1);
832 ipu_ch_param_set_field(&params, 1, 102, 14, stride - 1);
833 }
834
835 ipu_ch_param_set_field(&params, 1, 0, 29, addr0 >> 3);
836 ipu_ch_param_set_field(&params, 1, 29, 29, addr1 >> 3);
837
838 switch (pixel_fmt) {
839 case IPU_PIX_FMT_GENERIC:
840 /*Represents 8-bit Generic data */
841 ipu_ch_param_set_field(&params, 0, 107, 3, 5); /* bits/pixel */
842 ipu_ch_param_set_field(&params, 1, 85, 4, 6); /* pix format */
843 ipu_ch_param_set_field(&params, 1, 78, 7, 63); /* burst size */
844
845 break;
846 case IPU_PIX_FMT_GENERIC_32:
847 /*Represents 32-bit Generic data */
848 break;
849 case IPU_PIX_FMT_RGB565:
850 ipu_ch_param_set_field(&params, 0, 107, 3, 3); /* bits/pixel */
851 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
852 ipu_ch_param_set_field(&params, 1, 78, 7, 15); /* burst size */
853
854 ipu_ch_params_set_packing(&params, 5, 0, 6, 5, 5, 11, 8, 16);
855 break;
856 case IPU_PIX_FMT_BGR24:
857 ipu_ch_param_set_field(&params, 0, 107, 3, 1); /* bits/pixel */
858 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
859 ipu_ch_param_set_field(&params, 1, 78, 7, 19); /* burst size */
860
861 ipu_ch_params_set_packing(&params, 8, 0, 8, 8, 8, 16, 8, 24);
862 break;
863 case IPU_PIX_FMT_RGB24:
864 case IPU_PIX_FMT_YUV444:
865 ipu_ch_param_set_field(&params, 0, 107, 3, 1); /* bits/pixel */
866 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
867 ipu_ch_param_set_field(&params, 1, 78, 7, 19); /* burst size */
868
869 ipu_ch_params_set_packing(&params, 8, 16, 8, 8, 8, 0, 8, 24);
870 break;
871 case IPU_PIX_FMT_BGRA32:
872 case IPU_PIX_FMT_BGR32:
873 ipu_ch_param_set_field(&params, 0, 107, 3, 0); /* bits/pixel */
874 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
875 ipu_ch_param_set_field(&params, 1, 78, 7, 15); /* burst size */
876
877 ipu_ch_params_set_packing(&params, 8, 8, 8, 16, 8, 24, 8, 0);
878 break;
879 case IPU_PIX_FMT_RGBA32:
880 case IPU_PIX_FMT_RGB32:
881 ipu_ch_param_set_field(&params, 0, 107, 3, 0); /* bits/pixel */
882 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
883 ipu_ch_param_set_field(&params, 1, 78, 7, 15); /* burst size */
884
885 ipu_ch_params_set_packing(&params, 8, 24, 8, 16, 8, 8, 8, 0);
886 break;
887 case IPU_PIX_FMT_ABGR32:
888 ipu_ch_param_set_field(&params, 0, 107, 3, 0); /* bits/pixel */
889 ipu_ch_param_set_field(&params, 1, 85, 4, 7); /* pix format */
890
891 ipu_ch_params_set_packing(&params, 8, 0, 8, 8, 8, 16, 8, 24);
892 break;
893 case IPU_PIX_FMT_UYVY:
894 ipu_ch_param_set_field(&params, 0, 107, 3, 3); /* bits/pixel */
895 ipu_ch_param_set_field(&params, 1, 85, 4, 0xA); /* pix format */
896 ipu_ch_param_set_field(&params, 1, 78, 7, 15); /* burst size */
897 break;
898 case IPU_PIX_FMT_YUYV:
899 ipu_ch_param_set_field(&params, 0, 107, 3, 3); /* bits/pixel */
900 ipu_ch_param_set_field(&params, 1, 85, 4, 0x8); /* pix format */
901 ipu_ch_param_set_field(&params, 1, 78, 7, 31); /* burst size */
902 break;
903 case IPU_PIX_FMT_YUV420P2:
904 case IPU_PIX_FMT_YUV420P:
905 ipu_ch_param_set_field(&params, 1, 85, 4, 2); /* pix format */
906
907 if (uv_stride < stride / 2)
908 uv_stride = stride / 2;
909
910 u_offset = stride * height;
911 v_offset = u_offset + (uv_stride * height / 2);
912 /* burst size */
913 if ((ch == 8) || (ch == 9) || (ch == 10)) {
914 ipu_ch_param_set_field(&params, 1, 78, 7, 15);
915 uv_stride = uv_stride*2;
916 } else {
917 ipu_ch_param_set_field(&params, 1, 78, 7, 31);
918 }
919 break;
920 case IPU_PIX_FMT_YVU422P:
921 /* BPP & pixel format */
922 ipu_ch_param_set_field(&params, 1, 85, 4, 1); /* pix format */
923 ipu_ch_param_set_field(&params, 1, 78, 7, 31); /* burst size */
924
925 if (uv_stride < stride / 2)
926 uv_stride = stride / 2;
927
928 v_offset = (v == 0) ? stride * height : v;
929 u_offset = (u == 0) ? v_offset + v_offset / 2 : u;
930 break;
931 case IPU_PIX_FMT_YUV422P:
932 /* BPP & pixel format */
933 ipu_ch_param_set_field(&params, 1, 85, 4, 1); /* pix format */
934 ipu_ch_param_set_field(&params, 1, 78, 7, 31); /* burst size */
935
936 if (uv_stride < stride / 2)
937 uv_stride = stride / 2;
938
939 u_offset = (u == 0) ? stride * height : u;
940 v_offset = (v == 0) ? u_offset + u_offset / 2 : v;
941 break;
942 case IPU_PIX_FMT_NV12:
943 /* BPP & pixel format */
944 ipu_ch_param_set_field(&params, 1, 85, 4, 4); /* pix format */
945 ipu_ch_param_set_field(&params, 1, 78, 7, 31); /* burst size */
946 uv_stride = stride;
947 u_offset = (u == 0) ? stride * height : u;
948 break;
949 default:
950 puts("mxc ipu: unimplemented pixel format\n");
951 break;
952 }
953
Stefano Babiced5939d2010-10-13 12:16:35 +0200954 if (uv_stride)
955 ipu_ch_param_set_field(&params, 1, 128, 14, uv_stride - 1);
956
957 /* Get the uv offset from user when need cropping */
958 if (u || v) {
959 u_offset = u;
960 v_offset = v;
961 }
962
963 /* UBO and VBO are 22-bit */
964 if (u_offset/8 > 0x3fffff)
965 puts("The value of U offset exceeds IPU limitation\n");
966 if (v_offset/8 > 0x3fffff)
967 puts("The value of V offset exceeds IPU limitation\n");
968
969 ipu_ch_param_set_field(&params, 0, 46, 22, u_offset / 8);
970 ipu_ch_param_set_field(&params, 0, 68, 22, v_offset / 8);
971
972 debug("initializing idma ch %d @ %p\n", ch, ipu_ch_param_addr(ch));
973 memcpy(ipu_ch_param_addr(ch), &params, sizeof(params));
974};
975
976/*
977 * This function is called to initialize a buffer for logical IPU channel.
978 *
979 * @param channel Input parameter for the logical channel ID.
980 *
981 * @param type Input parameter which buffer to initialize.
982 *
983 * @param pixel_fmt Input parameter for pixel format of buffer.
984 * Pixel format is a FOURCC ASCII code.
985 *
986 * @param width Input parameter for width of buffer in pixels.
987 *
988 * @param height Input parameter for height of buffer in pixels.
989 *
990 * @param stride Input parameter for stride length of buffer
991 * in pixels.
992 *
993 * @param phyaddr_0 Input parameter buffer 0 physical address.
994 *
995 * @param phyaddr_1 Input parameter buffer 1 physical address.
996 * Setting this to a value other than NULL enables
997 * double buffering mode.
998 *
999 * @param u private u offset for additional cropping,
1000 * zero if not used.
1001 *
1002 * @param v private v offset for additional cropping,
1003 * zero if not used.
1004 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001005 * Return: Returns 0 on success or negative error code on fail
Stefano Babiced5939d2010-10-13 12:16:35 +02001006 */
1007int32_t ipu_init_channel_buffer(ipu_channel_t channel, ipu_buffer_t type,
1008 uint32_t pixel_fmt,
1009 uint16_t width, uint16_t height,
1010 uint32_t stride,
1011 dma_addr_t phyaddr_0, dma_addr_t phyaddr_1,
1012 uint32_t u, uint32_t v)
1013{
1014 uint32_t reg;
1015 uint32_t dma_chan;
1016
1017 dma_chan = channel_2_dma(channel, type);
1018 if (!idma_is_valid(dma_chan))
1019 return -EINVAL;
1020
1021 if (stride < width * bytes_per_pixel(pixel_fmt))
1022 stride = width * bytes_per_pixel(pixel_fmt);
1023
1024 if (stride % 4) {
1025 printf(
1026 "Stride not 32-bit aligned, stride = %d\n", stride);
1027 return -EINVAL;
1028 }
1029 /* Build parameter memory data for DMA channel */
1030 ipu_ch_param_init(dma_chan, pixel_fmt, width, height, stride, u, v, 0,
1031 phyaddr_0, phyaddr_1);
1032
1033 if (ipu_is_dmfc_chan(dma_chan)) {
1034 ipu_dmfc_set_wait4eot(dma_chan, width);
1035 }
1036
1037 if (idma_is_set(IDMAC_CHA_PRI, dma_chan))
1038 ipu_ch_param_set_high_priority(dma_chan);
1039
1040 ipu_ch_param_dump(dma_chan);
1041
1042 reg = __raw_readl(IPU_CHA_DB_MODE_SEL(dma_chan));
1043 if (phyaddr_1)
1044 reg |= idma_mask(dma_chan);
1045 else
1046 reg &= ~idma_mask(dma_chan);
1047 __raw_writel(reg, IPU_CHA_DB_MODE_SEL(dma_chan));
1048
1049 /* Reset to buffer 0 */
1050 __raw_writel(idma_mask(dma_chan), IPU_CHA_CUR_BUF(dma_chan));
1051
1052 return 0;
1053}
1054
1055/*
1056 * This function enables a logical channel.
1057 *
1058 * @param channel Input parameter for the logical channel ID.
1059 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001060 * Return: This function returns 0 on success or negative error code on
Stefano Babiced5939d2010-10-13 12:16:35 +02001061 * fail.
1062 */
1063int32_t ipu_enable_channel(ipu_channel_t channel)
1064{
1065 uint32_t reg;
1066 uint32_t in_dma;
1067 uint32_t out_dma;
1068
1069 if (g_channel_enable_mask & (1L << IPU_CHAN_ID(channel))) {
1070 printf("Warning: channel already enabled %d\n",
1071 IPU_CHAN_ID(channel));
1072 }
1073
1074 /* Get input and output dma channels */
1075 out_dma = channel_2_dma(channel, IPU_OUTPUT_BUFFER);
1076 in_dma = channel_2_dma(channel, IPU_VIDEO_IN_BUFFER);
1077
1078 if (idma_is_valid(in_dma)) {
1079 reg = __raw_readl(IDMAC_CHA_EN(in_dma));
1080 __raw_writel(reg | idma_mask(in_dma), IDMAC_CHA_EN(in_dma));
1081 }
1082 if (idma_is_valid(out_dma)) {
1083 reg = __raw_readl(IDMAC_CHA_EN(out_dma));
1084 __raw_writel(reg | idma_mask(out_dma), IDMAC_CHA_EN(out_dma));
1085 }
1086
1087 if ((channel == MEM_DC_SYNC) || (channel == MEM_BG_SYNC) ||
1088 (channel == MEM_FG_SYNC))
1089 ipu_dp_dc_enable(channel);
1090
1091 g_channel_enable_mask |= 1L << IPU_CHAN_ID(channel);
1092
1093 return 0;
1094}
1095
1096/*
1097 * This function clear buffer ready for a logical channel.
1098 *
1099 * @param channel Input parameter for the logical channel ID.
1100 *
1101 * @param type Input parameter which buffer to clear.
1102 *
1103 * @param bufNum Input parameter for which buffer number clear
1104 * ready state.
1105 *
1106 */
1107void ipu_clear_buffer_ready(ipu_channel_t channel, ipu_buffer_t type,
1108 uint32_t bufNum)
1109{
1110 uint32_t dma_ch = channel_2_dma(channel, type);
1111
1112 if (!idma_is_valid(dma_ch))
1113 return;
1114
1115 __raw_writel(0xF0000000, IPU_GPR); /* write one to clear */
1116 if (bufNum == 0) {
1117 if (idma_is_set(IPU_CHA_BUF0_RDY, dma_ch)) {
1118 __raw_writel(idma_mask(dma_ch),
1119 IPU_CHA_BUF0_RDY(dma_ch));
1120 }
1121 } else {
1122 if (idma_is_set(IPU_CHA_BUF1_RDY, dma_ch)) {
1123 __raw_writel(idma_mask(dma_ch),
1124 IPU_CHA_BUF1_RDY(dma_ch));
1125 }
1126 }
1127 __raw_writel(0x0, IPU_GPR); /* write one to set */
1128}
1129
1130/*
1131 * This function disables a logical channel.
1132 *
1133 * @param channel Input parameter for the logical channel ID.
1134 *
1135 * @param wait_for_stop Flag to set whether to wait for channel end
1136 * of frame or return immediately.
1137 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001138 * Return: This function returns 0 on success or negative error code on
Stefano Babiced5939d2010-10-13 12:16:35 +02001139 * fail.
1140 */
1141int32_t ipu_disable_channel(ipu_channel_t channel)
1142{
1143 uint32_t reg;
1144 uint32_t in_dma;
1145 uint32_t out_dma;
1146
1147 if ((g_channel_enable_mask & (1L << IPU_CHAN_ID(channel))) == 0) {
1148 debug("Channel already disabled %d\n",
1149 IPU_CHAN_ID(channel));
1150 return 0;
1151 }
1152
1153 /* Get input and output dma channels */
1154 out_dma = channel_2_dma(channel, IPU_OUTPUT_BUFFER);
1155 in_dma = channel_2_dma(channel, IPU_VIDEO_IN_BUFFER);
1156
1157 if ((idma_is_valid(in_dma) &&
1158 !idma_is_set(IDMAC_CHA_EN, in_dma))
1159 && (idma_is_valid(out_dma) &&
1160 !idma_is_set(IDMAC_CHA_EN, out_dma)))
1161 return -EINVAL;
1162
1163 if ((channel == MEM_BG_SYNC) || (channel == MEM_FG_SYNC) ||
1164 (channel == MEM_DC_SYNC)) {
1165 ipu_dp_dc_disable(channel, 0);
1166 }
1167
1168 /* Disable DMA channel(s) */
1169 if (idma_is_valid(in_dma)) {
1170 reg = __raw_readl(IDMAC_CHA_EN(in_dma));
1171 __raw_writel(reg & ~idma_mask(in_dma), IDMAC_CHA_EN(in_dma));
1172 __raw_writel(idma_mask(in_dma), IPU_CHA_CUR_BUF(in_dma));
1173 }
1174 if (idma_is_valid(out_dma)) {
1175 reg = __raw_readl(IDMAC_CHA_EN(out_dma));
1176 __raw_writel(reg & ~idma_mask(out_dma), IDMAC_CHA_EN(out_dma));
1177 __raw_writel(idma_mask(out_dma), IPU_CHA_CUR_BUF(out_dma));
1178 }
1179
1180 g_channel_enable_mask &= ~(1L << IPU_CHAN_ID(channel));
1181
1182 /* Set channel buffers NOT to be ready */
1183 if (idma_is_valid(in_dma)) {
1184 ipu_clear_buffer_ready(channel, IPU_VIDEO_IN_BUFFER, 0);
1185 ipu_clear_buffer_ready(channel, IPU_VIDEO_IN_BUFFER, 1);
1186 }
1187 if (idma_is_valid(out_dma)) {
1188 ipu_clear_buffer_ready(channel, IPU_OUTPUT_BUFFER, 0);
1189 ipu_clear_buffer_ready(channel, IPU_OUTPUT_BUFFER, 1);
1190 }
1191
1192 return 0;
1193}
1194
1195uint32_t bytes_per_pixel(uint32_t fmt)
1196{
1197 switch (fmt) {
1198 case IPU_PIX_FMT_GENERIC: /*generic data */
1199 case IPU_PIX_FMT_RGB332:
1200 case IPU_PIX_FMT_YUV420P:
1201 case IPU_PIX_FMT_YUV422P:
1202 return 1;
1203 break;
1204 case IPU_PIX_FMT_RGB565:
1205 case IPU_PIX_FMT_YUYV:
1206 case IPU_PIX_FMT_UYVY:
1207 return 2;
1208 break;
1209 case IPU_PIX_FMT_BGR24:
1210 case IPU_PIX_FMT_RGB24:
1211 return 3;
1212 break;
1213 case IPU_PIX_FMT_GENERIC_32: /*generic data */
1214 case IPU_PIX_FMT_BGR32:
1215 case IPU_PIX_FMT_BGRA32:
1216 case IPU_PIX_FMT_RGB32:
1217 case IPU_PIX_FMT_RGBA32:
1218 case IPU_PIX_FMT_ABGR32:
1219 return 4;
1220 break;
1221 default:
1222 return 1;
1223 break;
1224 }
1225 return 0;
1226}
1227
1228ipu_color_space_t format_to_colorspace(uint32_t fmt)
1229{
1230 switch (fmt) {
1231 case IPU_PIX_FMT_RGB666:
1232 case IPU_PIX_FMT_RGB565:
1233 case IPU_PIX_FMT_BGR24:
1234 case IPU_PIX_FMT_RGB24:
1235 case IPU_PIX_FMT_BGR32:
1236 case IPU_PIX_FMT_BGRA32:
1237 case IPU_PIX_FMT_RGB32:
1238 case IPU_PIX_FMT_RGBA32:
1239 case IPU_PIX_FMT_ABGR32:
1240 case IPU_PIX_FMT_LVDS666:
1241 case IPU_PIX_FMT_LVDS888:
1242 return RGB;
1243 break;
1244
1245 default:
1246 return YCbCr;
1247 break;
1248 }
1249 return RGB;
1250}
Heiko Schocher27298832015-04-20 07:53:48 +02001251
1252/* should be removed when clk framework is availiable */
1253int ipu_set_ldb_clock(int rate)
1254{
1255 ldb_clk.rate = rate;
1256
1257 return 0;
1258}
Anatolij Gustschin3e7ad7d2017-09-04 23:33:45 +02001259
1260bool ipu_clk_enabled(void)
1261{
1262 return g_ipu_clk_enabled;
1263}