ipu_common.c 33 KB

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