ipu_common.c 33 KB

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