ipu_common.c 32 KB

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