ipu_idmac.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. /*
  2. * Copyright (C) 2008
  3. * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
  4. *
  5. * Copyright (C) 2005-2007 Freescale Semiconductor, Inc. All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/err.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/delay.h>
  16. #include <linux/list.h>
  17. #include <linux/clk.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/string.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <mach/ipu.h>
  23. #include "ipu_intern.h"
  24. #define FS_VF_IN_VALID 0x00000002
  25. #define FS_ENC_IN_VALID 0x00000001
  26. static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
  27. bool wait_for_stop);
  28. /*
  29. * There can be only one, we could allocate it dynamically, but then we'd have
  30. * to add an extra parameter to some functions, and use something as ugly as
  31. * struct ipu *ipu = to_ipu(to_idmac(ichan->dma_chan.device));
  32. * in the ISR
  33. */
  34. static struct ipu ipu_data;
  35. #define to_ipu(id) container_of(id, struct ipu, idmac)
  36. static u32 __idmac_read_icreg(struct ipu *ipu, unsigned long reg)
  37. {
  38. return __raw_readl(ipu->reg_ic + reg);
  39. }
  40. #define idmac_read_icreg(ipu, reg) __idmac_read_icreg(ipu, reg - IC_CONF)
  41. static void __idmac_write_icreg(struct ipu *ipu, u32 value, unsigned long reg)
  42. {
  43. __raw_writel(value, ipu->reg_ic + reg);
  44. }
  45. #define idmac_write_icreg(ipu, v, reg) __idmac_write_icreg(ipu, v, reg - IC_CONF)
  46. static u32 idmac_read_ipureg(struct ipu *ipu, unsigned long reg)
  47. {
  48. return __raw_readl(ipu->reg_ipu + reg);
  49. }
  50. static void idmac_write_ipureg(struct ipu *ipu, u32 value, unsigned long reg)
  51. {
  52. __raw_writel(value, ipu->reg_ipu + reg);
  53. }
  54. /*****************************************************************************
  55. * IPU / IC common functions
  56. */
  57. static void dump_idmac_reg(struct ipu *ipu)
  58. {
  59. dev_dbg(ipu->dev, "IDMAC_CONF 0x%x, IC_CONF 0x%x, IDMAC_CHA_EN 0x%x, "
  60. "IDMAC_CHA_PRI 0x%x, IDMAC_CHA_BUSY 0x%x\n",
  61. idmac_read_icreg(ipu, IDMAC_CONF),
  62. idmac_read_icreg(ipu, IC_CONF),
  63. idmac_read_icreg(ipu, IDMAC_CHA_EN),
  64. idmac_read_icreg(ipu, IDMAC_CHA_PRI),
  65. idmac_read_icreg(ipu, IDMAC_CHA_BUSY));
  66. dev_dbg(ipu->dev, "BUF0_RDY 0x%x, BUF1_RDY 0x%x, CUR_BUF 0x%x, "
  67. "DB_MODE 0x%x, TASKS_STAT 0x%x\n",
  68. idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
  69. idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
  70. idmac_read_ipureg(ipu, IPU_CHA_CUR_BUF),
  71. idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL),
  72. idmac_read_ipureg(ipu, IPU_TASKS_STAT));
  73. }
  74. static uint32_t bytes_per_pixel(enum pixel_fmt fmt)
  75. {
  76. switch (fmt) {
  77. case IPU_PIX_FMT_GENERIC: /* generic data */
  78. case IPU_PIX_FMT_RGB332:
  79. case IPU_PIX_FMT_YUV420P:
  80. case IPU_PIX_FMT_YUV422P:
  81. default:
  82. return 1;
  83. case IPU_PIX_FMT_RGB565:
  84. case IPU_PIX_FMT_YUYV:
  85. case IPU_PIX_FMT_UYVY:
  86. return 2;
  87. case IPU_PIX_FMT_BGR24:
  88. case IPU_PIX_FMT_RGB24:
  89. return 3;
  90. case IPU_PIX_FMT_GENERIC_32: /* generic data */
  91. case IPU_PIX_FMT_BGR32:
  92. case IPU_PIX_FMT_RGB32:
  93. case IPU_PIX_FMT_ABGR32:
  94. return 4;
  95. }
  96. }
  97. /* Enable direct write to memory by the Camera Sensor Interface */
  98. static void ipu_ic_enable_task(struct ipu *ipu, enum ipu_channel channel)
  99. {
  100. uint32_t ic_conf, mask;
  101. switch (channel) {
  102. case IDMAC_IC_0:
  103. mask = IC_CONF_PRPENC_EN;
  104. break;
  105. case IDMAC_IC_7:
  106. mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
  107. break;
  108. default:
  109. return;
  110. }
  111. ic_conf = idmac_read_icreg(ipu, IC_CONF) | mask;
  112. idmac_write_icreg(ipu, ic_conf, IC_CONF);
  113. }
  114. /* Called under spin_lock_irqsave(&ipu_data.lock) */
  115. static void ipu_ic_disable_task(struct ipu *ipu, enum ipu_channel channel)
  116. {
  117. uint32_t ic_conf, mask;
  118. switch (channel) {
  119. case IDMAC_IC_0:
  120. mask = IC_CONF_PRPENC_EN;
  121. break;
  122. case IDMAC_IC_7:
  123. mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
  124. break;
  125. default:
  126. return;
  127. }
  128. ic_conf = idmac_read_icreg(ipu, IC_CONF) & ~mask;
  129. idmac_write_icreg(ipu, ic_conf, IC_CONF);
  130. }
  131. static uint32_t ipu_channel_status(struct ipu *ipu, enum ipu_channel channel)
  132. {
  133. uint32_t stat = TASK_STAT_IDLE;
  134. uint32_t task_stat_reg = idmac_read_ipureg(ipu, IPU_TASKS_STAT);
  135. switch (channel) {
  136. case IDMAC_IC_7:
  137. stat = (task_stat_reg & TSTAT_CSI2MEM_MASK) >>
  138. TSTAT_CSI2MEM_OFFSET;
  139. break;
  140. case IDMAC_IC_0:
  141. case IDMAC_SDC_0:
  142. case IDMAC_SDC_1:
  143. default:
  144. break;
  145. }
  146. return stat;
  147. }
  148. struct chan_param_mem_planar {
  149. /* Word 0 */
  150. u32 xv:10;
  151. u32 yv:10;
  152. u32 xb:12;
  153. u32 yb:12;
  154. u32 res1:2;
  155. u32 nsb:1;
  156. u32 lnpb:6;
  157. u32 ubo_l:11;
  158. u32 ubo_h:15;
  159. u32 vbo_l:17;
  160. u32 vbo_h:9;
  161. u32 res2:3;
  162. u32 fw:12;
  163. u32 fh_l:8;
  164. u32 fh_h:4;
  165. u32 res3:28;
  166. /* Word 1 */
  167. u32 eba0;
  168. u32 eba1;
  169. u32 bpp:3;
  170. u32 sl:14;
  171. u32 pfs:3;
  172. u32 bam:3;
  173. u32 res4:2;
  174. u32 npb:6;
  175. u32 res5:1;
  176. u32 sat:2;
  177. u32 res6:30;
  178. } __attribute__ ((packed));
  179. struct chan_param_mem_interleaved {
  180. /* Word 0 */
  181. u32 xv:10;
  182. u32 yv:10;
  183. u32 xb:12;
  184. u32 yb:12;
  185. u32 sce:1;
  186. u32 res1:1;
  187. u32 nsb:1;
  188. u32 lnpb:6;
  189. u32 sx:10;
  190. u32 sy_l:1;
  191. u32 sy_h:9;
  192. u32 ns:10;
  193. u32 sm:10;
  194. u32 sdx_l:3;
  195. u32 sdx_h:2;
  196. u32 sdy:5;
  197. u32 sdrx:1;
  198. u32 sdry:1;
  199. u32 sdr1:1;
  200. u32 res2:2;
  201. u32 fw:12;
  202. u32 fh_l:8;
  203. u32 fh_h:4;
  204. u32 res3:28;
  205. /* Word 1 */
  206. u32 eba0;
  207. u32 eba1;
  208. u32 bpp:3;
  209. u32 sl:14;
  210. u32 pfs:3;
  211. u32 bam:3;
  212. u32 res4:2;
  213. u32 npb:6;
  214. u32 res5:1;
  215. u32 sat:2;
  216. u32 scc:1;
  217. u32 ofs0:5;
  218. u32 ofs1:5;
  219. u32 ofs2:5;
  220. u32 ofs3:5;
  221. u32 wid0:3;
  222. u32 wid1:3;
  223. u32 wid2:3;
  224. u32 wid3:3;
  225. u32 dec_sel:1;
  226. u32 res6:28;
  227. } __attribute__ ((packed));
  228. union chan_param_mem {
  229. struct chan_param_mem_planar pp;
  230. struct chan_param_mem_interleaved ip;
  231. };
  232. static void ipu_ch_param_set_plane_offset(union chan_param_mem *params,
  233. u32 u_offset, u32 v_offset)
  234. {
  235. params->pp.ubo_l = u_offset & 0x7ff;
  236. params->pp.ubo_h = u_offset >> 11;
  237. params->pp.vbo_l = v_offset & 0x1ffff;
  238. params->pp.vbo_h = v_offset >> 17;
  239. }
  240. static void ipu_ch_param_set_size(union chan_param_mem *params,
  241. uint32_t pixel_fmt, uint16_t width,
  242. uint16_t height, uint16_t stride)
  243. {
  244. u32 u_offset;
  245. u32 v_offset;
  246. params->pp.fw = width - 1;
  247. params->pp.fh_l = height - 1;
  248. params->pp.fh_h = (height - 1) >> 8;
  249. params->pp.sl = stride - 1;
  250. switch (pixel_fmt) {
  251. case IPU_PIX_FMT_GENERIC:
  252. /*Represents 8-bit Generic data */
  253. params->pp.bpp = 3;
  254. params->pp.pfs = 7;
  255. params->pp.npb = 31;
  256. params->pp.sat = 2; /* SAT = use 32-bit access */
  257. break;
  258. case IPU_PIX_FMT_GENERIC_32:
  259. /*Represents 32-bit Generic data */
  260. params->pp.bpp = 0;
  261. params->pp.pfs = 7;
  262. params->pp.npb = 7;
  263. params->pp.sat = 2; /* SAT = use 32-bit access */
  264. break;
  265. case IPU_PIX_FMT_RGB565:
  266. params->ip.bpp = 2;
  267. params->ip.pfs = 4;
  268. params->ip.npb = 7;
  269. params->ip.sat = 2; /* SAT = 32-bit access */
  270. params->ip.ofs0 = 0; /* Red bit offset */
  271. params->ip.ofs1 = 5; /* Green bit offset */
  272. params->ip.ofs2 = 11; /* Blue bit offset */
  273. params->ip.ofs3 = 16; /* Alpha bit offset */
  274. params->ip.wid0 = 4; /* Red bit width - 1 */
  275. params->ip.wid1 = 5; /* Green bit width - 1 */
  276. params->ip.wid2 = 4; /* Blue bit width - 1 */
  277. break;
  278. case IPU_PIX_FMT_BGR24:
  279. params->ip.bpp = 1; /* 24 BPP & RGB PFS */
  280. params->ip.pfs = 4;
  281. params->ip.npb = 7;
  282. params->ip.sat = 2; /* SAT = 32-bit access */
  283. params->ip.ofs0 = 0; /* Red bit offset */
  284. params->ip.ofs1 = 8; /* Green bit offset */
  285. params->ip.ofs2 = 16; /* Blue bit offset */
  286. params->ip.ofs3 = 24; /* Alpha bit offset */
  287. params->ip.wid0 = 7; /* Red bit width - 1 */
  288. params->ip.wid1 = 7; /* Green bit width - 1 */
  289. params->ip.wid2 = 7; /* Blue bit width - 1 */
  290. break;
  291. case IPU_PIX_FMT_RGB24:
  292. params->ip.bpp = 1; /* 24 BPP & RGB PFS */
  293. params->ip.pfs = 4;
  294. params->ip.npb = 7;
  295. params->ip.sat = 2; /* SAT = 32-bit access */
  296. params->ip.ofs0 = 16; /* Red bit offset */
  297. params->ip.ofs1 = 8; /* Green bit offset */
  298. params->ip.ofs2 = 0; /* Blue bit offset */
  299. params->ip.ofs3 = 24; /* Alpha bit offset */
  300. params->ip.wid0 = 7; /* Red bit width - 1 */
  301. params->ip.wid1 = 7; /* Green bit width - 1 */
  302. params->ip.wid2 = 7; /* Blue bit width - 1 */
  303. break;
  304. case IPU_PIX_FMT_BGRA32:
  305. case IPU_PIX_FMT_BGR32:
  306. case IPU_PIX_FMT_ABGR32:
  307. params->ip.bpp = 0;
  308. params->ip.pfs = 4;
  309. params->ip.npb = 7;
  310. params->ip.sat = 2; /* SAT = 32-bit access */
  311. params->ip.ofs0 = 8; /* Red bit offset */
  312. params->ip.ofs1 = 16; /* Green bit offset */
  313. params->ip.ofs2 = 24; /* Blue bit offset */
  314. params->ip.ofs3 = 0; /* Alpha bit offset */
  315. params->ip.wid0 = 7; /* Red bit width - 1 */
  316. params->ip.wid1 = 7; /* Green bit width - 1 */
  317. params->ip.wid2 = 7; /* Blue bit width - 1 */
  318. params->ip.wid3 = 7; /* Alpha bit width - 1 */
  319. break;
  320. case IPU_PIX_FMT_RGBA32:
  321. case IPU_PIX_FMT_RGB32:
  322. params->ip.bpp = 0;
  323. params->ip.pfs = 4;
  324. params->ip.npb = 7;
  325. params->ip.sat = 2; /* SAT = 32-bit access */
  326. params->ip.ofs0 = 24; /* Red bit offset */
  327. params->ip.ofs1 = 16; /* Green bit offset */
  328. params->ip.ofs2 = 8; /* Blue bit offset */
  329. params->ip.ofs3 = 0; /* Alpha bit offset */
  330. params->ip.wid0 = 7; /* Red bit width - 1 */
  331. params->ip.wid1 = 7; /* Green bit width - 1 */
  332. params->ip.wid2 = 7; /* Blue bit width - 1 */
  333. params->ip.wid3 = 7; /* Alpha bit width - 1 */
  334. break;
  335. case IPU_PIX_FMT_UYVY:
  336. params->ip.bpp = 2;
  337. params->ip.pfs = 6;
  338. params->ip.npb = 7;
  339. params->ip.sat = 2; /* SAT = 32-bit access */
  340. break;
  341. case IPU_PIX_FMT_YUV420P2:
  342. case IPU_PIX_FMT_YUV420P:
  343. params->ip.bpp = 3;
  344. params->ip.pfs = 3;
  345. params->ip.npb = 7;
  346. params->ip.sat = 2; /* SAT = 32-bit access */
  347. u_offset = stride * height;
  348. v_offset = u_offset + u_offset / 4;
  349. ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
  350. break;
  351. case IPU_PIX_FMT_YVU422P:
  352. params->ip.bpp = 3;
  353. params->ip.pfs = 2;
  354. params->ip.npb = 7;
  355. params->ip.sat = 2; /* SAT = 32-bit access */
  356. v_offset = stride * height;
  357. u_offset = v_offset + v_offset / 2;
  358. ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
  359. break;
  360. case IPU_PIX_FMT_YUV422P:
  361. params->ip.bpp = 3;
  362. params->ip.pfs = 2;
  363. params->ip.npb = 7;
  364. params->ip.sat = 2; /* SAT = 32-bit access */
  365. u_offset = stride * height;
  366. v_offset = u_offset + u_offset / 2;
  367. ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
  368. break;
  369. default:
  370. dev_err(ipu_data.dev,
  371. "mx3 ipu: unimplemented pixel format %d\n", pixel_fmt);
  372. break;
  373. }
  374. params->pp.nsb = 1;
  375. }
  376. static void ipu_ch_param_set_burst_size(union chan_param_mem *params,
  377. uint16_t burst_pixels)
  378. {
  379. params->pp.npb = burst_pixels - 1;
  380. }
  381. static void ipu_ch_param_set_buffer(union chan_param_mem *params,
  382. dma_addr_t buf0, dma_addr_t buf1)
  383. {
  384. params->pp.eba0 = buf0;
  385. params->pp.eba1 = buf1;
  386. }
  387. static void ipu_ch_param_set_rotation(union chan_param_mem *params,
  388. enum ipu_rotate_mode rotate)
  389. {
  390. params->pp.bam = rotate;
  391. }
  392. static void ipu_write_param_mem(uint32_t addr, uint32_t *data,
  393. uint32_t num_words)
  394. {
  395. for (; num_words > 0; num_words--) {
  396. dev_dbg(ipu_data.dev,
  397. "write param mem - addr = 0x%08X, data = 0x%08X\n",
  398. addr, *data);
  399. idmac_write_ipureg(&ipu_data, addr, IPU_IMA_ADDR);
  400. idmac_write_ipureg(&ipu_data, *data++, IPU_IMA_DATA);
  401. addr++;
  402. if ((addr & 0x7) == 5) {
  403. addr &= ~0x7; /* set to word 0 */
  404. addr += 8; /* increment to next row */
  405. }
  406. }
  407. }
  408. static int calc_resize_coeffs(uint32_t in_size, uint32_t out_size,
  409. uint32_t *resize_coeff,
  410. uint32_t *downsize_coeff)
  411. {
  412. uint32_t temp_size;
  413. uint32_t temp_downsize;
  414. *resize_coeff = 1 << 13;
  415. *downsize_coeff = 1 << 13;
  416. /* Cannot downsize more than 8:1 */
  417. if (out_size << 3 < in_size)
  418. return -EINVAL;
  419. /* compute downsizing coefficient */
  420. temp_downsize = 0;
  421. temp_size = in_size;
  422. while (temp_size >= out_size * 2 && temp_downsize < 2) {
  423. temp_size >>= 1;
  424. temp_downsize++;
  425. }
  426. *downsize_coeff = temp_downsize;
  427. /*
  428. * compute resizing coefficient using the following formula:
  429. * resize_coeff = M*(SI -1)/(SO - 1)
  430. * where M = 2^13, SI - input size, SO - output size
  431. */
  432. *resize_coeff = (8192L * (temp_size - 1)) / (out_size - 1);
  433. if (*resize_coeff >= 16384L) {
  434. dev_err(ipu_data.dev, "Warning! Overflow on resize coeff.\n");
  435. *resize_coeff = 0x3FFF;
  436. }
  437. dev_dbg(ipu_data.dev, "resizing from %u -> %u pixels, "
  438. "downsize=%u, resize=%u.%lu (reg=%u)\n", in_size, out_size,
  439. *downsize_coeff, *resize_coeff >= 8192L ? 1 : 0,
  440. ((*resize_coeff & 0x1FFF) * 10000L) / 8192L, *resize_coeff);
  441. return 0;
  442. }
  443. static enum ipu_color_space format_to_colorspace(enum pixel_fmt fmt)
  444. {
  445. switch (fmt) {
  446. case IPU_PIX_FMT_RGB565:
  447. case IPU_PIX_FMT_BGR24:
  448. case IPU_PIX_FMT_RGB24:
  449. case IPU_PIX_FMT_BGR32:
  450. case IPU_PIX_FMT_RGB32:
  451. return IPU_COLORSPACE_RGB;
  452. default:
  453. return IPU_COLORSPACE_YCBCR;
  454. }
  455. }
  456. static int ipu_ic_init_prpenc(struct ipu *ipu,
  457. union ipu_channel_param *params, bool src_is_csi)
  458. {
  459. uint32_t reg, ic_conf;
  460. uint32_t downsize_coeff, resize_coeff;
  461. enum ipu_color_space in_fmt, out_fmt;
  462. /* Setup vertical resizing */
  463. calc_resize_coeffs(params->video.in_height,
  464. params->video.out_height,
  465. &resize_coeff, &downsize_coeff);
  466. reg = (downsize_coeff << 30) | (resize_coeff << 16);
  467. /* Setup horizontal resizing */
  468. calc_resize_coeffs(params->video.in_width,
  469. params->video.out_width,
  470. &resize_coeff, &downsize_coeff);
  471. reg |= (downsize_coeff << 14) | resize_coeff;
  472. /* Setup color space conversion */
  473. in_fmt = format_to_colorspace(params->video.in_pixel_fmt);
  474. out_fmt = format_to_colorspace(params->video.out_pixel_fmt);
  475. /*
  476. * Colourspace conversion unsupported yet - see _init_csc() in
  477. * Freescale sources
  478. */
  479. if (in_fmt != out_fmt) {
  480. dev_err(ipu->dev, "Colourspace conversion unsupported!\n");
  481. return -EOPNOTSUPP;
  482. }
  483. idmac_write_icreg(ipu, reg, IC_PRP_ENC_RSC);
  484. ic_conf = idmac_read_icreg(ipu, IC_CONF);
  485. if (src_is_csi)
  486. ic_conf &= ~IC_CONF_RWS_EN;
  487. else
  488. ic_conf |= IC_CONF_RWS_EN;
  489. idmac_write_icreg(ipu, ic_conf, IC_CONF);
  490. return 0;
  491. }
  492. static uint32_t dma_param_addr(uint32_t dma_ch)
  493. {
  494. /* Channel Parameter Memory */
  495. return 0x10000 | (dma_ch << 4);
  496. }
  497. static void ipu_channel_set_priority(struct ipu *ipu, enum ipu_channel channel,
  498. bool prio)
  499. {
  500. u32 reg = idmac_read_icreg(ipu, IDMAC_CHA_PRI);
  501. if (prio)
  502. reg |= 1UL << channel;
  503. else
  504. reg &= ~(1UL << channel);
  505. idmac_write_icreg(ipu, reg, IDMAC_CHA_PRI);
  506. dump_idmac_reg(ipu);
  507. }
  508. static uint32_t ipu_channel_conf_mask(enum ipu_channel channel)
  509. {
  510. uint32_t mask;
  511. switch (channel) {
  512. case IDMAC_IC_0:
  513. case IDMAC_IC_7:
  514. mask = IPU_CONF_CSI_EN | IPU_CONF_IC_EN;
  515. break;
  516. case IDMAC_SDC_0:
  517. case IDMAC_SDC_1:
  518. mask = IPU_CONF_SDC_EN | IPU_CONF_DI_EN;
  519. break;
  520. default:
  521. mask = 0;
  522. break;
  523. }
  524. return mask;
  525. }
  526. /**
  527. * ipu_enable_channel() - enable an IPU channel.
  528. * @idmac: IPU DMAC context.
  529. * @ichan: IDMAC channel.
  530. * @return: 0 on success or negative error code on failure.
  531. */
  532. static int ipu_enable_channel(struct idmac *idmac, struct idmac_channel *ichan)
  533. {
  534. struct ipu *ipu = to_ipu(idmac);
  535. enum ipu_channel channel = ichan->dma_chan.chan_id;
  536. uint32_t reg;
  537. unsigned long flags;
  538. spin_lock_irqsave(&ipu->lock, flags);
  539. /* Reset to buffer 0 */
  540. idmac_write_ipureg(ipu, 1UL << channel, IPU_CHA_CUR_BUF);
  541. ichan->active_buffer = 0;
  542. ichan->status = IPU_CHANNEL_ENABLED;
  543. switch (channel) {
  544. case IDMAC_SDC_0:
  545. case IDMAC_SDC_1:
  546. case IDMAC_IC_7:
  547. ipu_channel_set_priority(ipu, channel, true);
  548. default:
  549. break;
  550. }
  551. reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
  552. idmac_write_icreg(ipu, reg | (1UL << channel), IDMAC_CHA_EN);
  553. ipu_ic_enable_task(ipu, channel);
  554. spin_unlock_irqrestore(&ipu->lock, flags);
  555. return 0;
  556. }
  557. /**
  558. * ipu_init_channel_buffer() - initialize a buffer for logical IPU channel.
  559. * @ichan: IDMAC channel.
  560. * @pixel_fmt: pixel format of buffer. Pixel format is a FOURCC ASCII code.
  561. * @width: width of buffer in pixels.
  562. * @height: height of buffer in pixels.
  563. * @stride: stride length of buffer in pixels.
  564. * @rot_mode: rotation mode of buffer. A rotation setting other than
  565. * IPU_ROTATE_VERT_FLIP should only be used for input buffers of
  566. * rotation channels.
  567. * @phyaddr_0: buffer 0 physical address.
  568. * @phyaddr_1: buffer 1 physical address. Setting this to a value other than
  569. * NULL enables double buffering mode.
  570. * @return: 0 on success or negative error code on failure.
  571. */
  572. static int ipu_init_channel_buffer(struct idmac_channel *ichan,
  573. enum pixel_fmt pixel_fmt,
  574. uint16_t width, uint16_t height,
  575. uint32_t stride,
  576. enum ipu_rotate_mode rot_mode,
  577. dma_addr_t phyaddr_0, dma_addr_t phyaddr_1)
  578. {
  579. enum ipu_channel channel = ichan->dma_chan.chan_id;
  580. struct idmac *idmac = to_idmac(ichan->dma_chan.device);
  581. struct ipu *ipu = to_ipu(idmac);
  582. union chan_param_mem params = {};
  583. unsigned long flags;
  584. uint32_t reg;
  585. uint32_t stride_bytes;
  586. stride_bytes = stride * bytes_per_pixel(pixel_fmt);
  587. if (stride_bytes % 4) {
  588. dev_err(ipu->dev,
  589. "Stride length must be 32-bit aligned, stride = %d, bytes = %d\n",
  590. stride, stride_bytes);
  591. return -EINVAL;
  592. }
  593. /* IC channel's stride must be a multiple of 8 pixels */
  594. if ((channel <= IDMAC_IC_13) && (stride % 8)) {
  595. dev_err(ipu->dev, "Stride must be 8 pixel multiple\n");
  596. return -EINVAL;
  597. }
  598. /* Build parameter memory data for DMA channel */
  599. ipu_ch_param_set_size(&params, pixel_fmt, width, height, stride_bytes);
  600. ipu_ch_param_set_buffer(&params, phyaddr_0, phyaddr_1);
  601. ipu_ch_param_set_rotation(&params, rot_mode);
  602. /* Some channels (rotation) have restriction on burst length */
  603. switch (channel) {
  604. case IDMAC_IC_7: /* Hangs with burst 8, 16, other values
  605. invalid - Table 44-30 */
  606. /*
  607. ipu_ch_param_set_burst_size(&params, 8);
  608. */
  609. break;
  610. case IDMAC_SDC_0:
  611. case IDMAC_SDC_1:
  612. /* In original code only IPU_PIX_FMT_RGB565 was setting burst */
  613. ipu_ch_param_set_burst_size(&params, 16);
  614. break;
  615. case IDMAC_IC_0:
  616. default:
  617. break;
  618. }
  619. spin_lock_irqsave(&ipu->lock, flags);
  620. ipu_write_param_mem(dma_param_addr(channel), (uint32_t *)&params, 10);
  621. reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
  622. if (phyaddr_1)
  623. reg |= 1UL << channel;
  624. else
  625. reg &= ~(1UL << channel);
  626. idmac_write_ipureg(ipu, reg, IPU_CHA_DB_MODE_SEL);
  627. ichan->status = IPU_CHANNEL_READY;
  628. spin_unlock_irqrestore(&ipu->lock, flags);
  629. return 0;
  630. }
  631. /**
  632. * ipu_select_buffer() - mark a channel's buffer as ready.
  633. * @channel: channel ID.
  634. * @buffer_n: buffer number to mark ready.
  635. */
  636. static void ipu_select_buffer(enum ipu_channel channel, int buffer_n)
  637. {
  638. /* No locking - this is a write-one-to-set register, cleared by IPU */
  639. if (buffer_n == 0)
  640. /* Mark buffer 0 as ready. */
  641. idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF0_RDY);
  642. else
  643. /* Mark buffer 1 as ready. */
  644. idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF1_RDY);
  645. }
  646. /**
  647. * ipu_update_channel_buffer() - update physical address of a channel buffer.
  648. * @ichan: IDMAC channel.
  649. * @buffer_n: buffer number to update.
  650. * 0 or 1 are the only valid values.
  651. * @phyaddr: buffer physical address.
  652. */
  653. /* Called under spin_lock(_irqsave)(&ichan->lock) */
  654. static void ipu_update_channel_buffer(struct idmac_channel *ichan,
  655. int buffer_n, dma_addr_t phyaddr)
  656. {
  657. enum ipu_channel channel = ichan->dma_chan.chan_id;
  658. uint32_t reg;
  659. unsigned long flags;
  660. spin_lock_irqsave(&ipu_data.lock, flags);
  661. if (buffer_n == 0) {
  662. reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
  663. if (reg & (1UL << channel)) {
  664. ipu_ic_disable_task(&ipu_data, channel);
  665. ichan->status = IPU_CHANNEL_READY;
  666. }
  667. /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 0) */
  668. idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
  669. 0x0008UL, IPU_IMA_ADDR);
  670. idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
  671. } else {
  672. reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
  673. if (reg & (1UL << channel)) {
  674. ipu_ic_disable_task(&ipu_data, channel);
  675. ichan->status = IPU_CHANNEL_READY;
  676. }
  677. /* Check if double-buffering is already enabled */
  678. reg = idmac_read_ipureg(&ipu_data, IPU_CHA_DB_MODE_SEL);
  679. if (!(reg & (1UL << channel)))
  680. idmac_write_ipureg(&ipu_data, reg | (1UL << channel),
  681. IPU_CHA_DB_MODE_SEL);
  682. /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 1) */
  683. idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
  684. 0x0009UL, IPU_IMA_ADDR);
  685. idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
  686. }
  687. spin_unlock_irqrestore(&ipu_data.lock, flags);
  688. }
  689. /* Called under spin_lock_irqsave(&ichan->lock) */
  690. static int ipu_submit_buffer(struct idmac_channel *ichan,
  691. struct idmac_tx_desc *desc, struct scatterlist *sg, int buf_idx)
  692. {
  693. unsigned int chan_id = ichan->dma_chan.chan_id;
  694. struct device *dev = &ichan->dma_chan.dev->device;
  695. if (async_tx_test_ack(&desc->txd))
  696. return -EINTR;
  697. /*
  698. * On first invocation this shouldn't be necessary, the call to
  699. * ipu_init_channel_buffer() above will set addresses for us, so we
  700. * could make it conditional on status >= IPU_CHANNEL_ENABLED, but
  701. * doing it again shouldn't hurt either.
  702. */
  703. ipu_update_channel_buffer(ichan, buf_idx, sg_dma_address(sg));
  704. ipu_select_buffer(chan_id, buf_idx);
  705. dev_dbg(dev, "Updated sg %p on channel 0x%x buffer %d\n",
  706. sg, chan_id, buf_idx);
  707. return 0;
  708. }
  709. /* Called under spin_lock_irqsave(&ichan->lock) */
  710. static int ipu_submit_channel_buffers(struct idmac_channel *ichan,
  711. struct idmac_tx_desc *desc)
  712. {
  713. struct scatterlist *sg;
  714. int i, ret = 0;
  715. for (i = 0, sg = desc->sg; i < 2 && sg; i++) {
  716. if (!ichan->sg[i]) {
  717. ichan->sg[i] = sg;
  718. ret = ipu_submit_buffer(ichan, desc, sg, i);
  719. if (ret < 0)
  720. return ret;
  721. sg = sg_next(sg);
  722. }
  723. }
  724. return ret;
  725. }
  726. static dma_cookie_t idmac_tx_submit(struct dma_async_tx_descriptor *tx)
  727. {
  728. struct idmac_tx_desc *desc = to_tx_desc(tx);
  729. struct idmac_channel *ichan = to_idmac_chan(tx->chan);
  730. struct idmac *idmac = to_idmac(tx->chan->device);
  731. struct ipu *ipu = to_ipu(idmac);
  732. struct device *dev = &ichan->dma_chan.dev->device;
  733. dma_cookie_t cookie;
  734. unsigned long flags;
  735. int ret;
  736. /* Sanity check */
  737. if (!list_empty(&desc->list)) {
  738. /* The descriptor doesn't belong to client */
  739. dev_err(dev, "Descriptor %p not prepared!\n", tx);
  740. return -EBUSY;
  741. }
  742. mutex_lock(&ichan->chan_mutex);
  743. async_tx_clear_ack(tx);
  744. if (ichan->status < IPU_CHANNEL_READY) {
  745. struct idmac_video_param *video = &ichan->params.video;
  746. /*
  747. * Initial buffer assignment - the first two sg-entries from
  748. * the descriptor will end up in the IDMAC buffers
  749. */
  750. dma_addr_t dma_1 = sg_is_last(desc->sg) ? 0 :
  751. sg_dma_address(&desc->sg[1]);
  752. WARN_ON(ichan->sg[0] || ichan->sg[1]);
  753. cookie = ipu_init_channel_buffer(ichan,
  754. video->out_pixel_fmt,
  755. video->out_width,
  756. video->out_height,
  757. video->out_stride,
  758. IPU_ROTATE_NONE,
  759. sg_dma_address(&desc->sg[0]),
  760. dma_1);
  761. if (cookie < 0)
  762. goto out;
  763. }
  764. dev_dbg(dev, "Submitting sg %p\n", &desc->sg[0]);
  765. cookie = ichan->dma_chan.cookie;
  766. if (++cookie < 0)
  767. cookie = 1;
  768. /* from dmaengine.h: "last cookie value returned to client" */
  769. ichan->dma_chan.cookie = cookie;
  770. tx->cookie = cookie;
  771. /* ipu->lock can be taken under ichan->lock, but not v.v. */
  772. spin_lock_irqsave(&ichan->lock, flags);
  773. list_add_tail(&desc->list, &ichan->queue);
  774. /* submit_buffers() atomically verifies and fills empty sg slots */
  775. ret = ipu_submit_channel_buffers(ichan, desc);
  776. spin_unlock_irqrestore(&ichan->lock, flags);
  777. if (ret < 0) {
  778. cookie = ret;
  779. goto dequeue;
  780. }
  781. if (ichan->status < IPU_CHANNEL_ENABLED) {
  782. ret = ipu_enable_channel(idmac, ichan);
  783. if (ret < 0) {
  784. cookie = ret;
  785. goto dequeue;
  786. }
  787. }
  788. dump_idmac_reg(ipu);
  789. dequeue:
  790. if (cookie < 0) {
  791. spin_lock_irqsave(&ichan->lock, flags);
  792. list_del_init(&desc->list);
  793. spin_unlock_irqrestore(&ichan->lock, flags);
  794. tx->cookie = cookie;
  795. ichan->dma_chan.cookie = cookie;
  796. }
  797. out:
  798. mutex_unlock(&ichan->chan_mutex);
  799. return cookie;
  800. }
  801. /* Called with ichan->chan_mutex held */
  802. static int idmac_desc_alloc(struct idmac_channel *ichan, int n)
  803. {
  804. struct idmac_tx_desc *desc = vmalloc(n * sizeof(struct idmac_tx_desc));
  805. struct idmac *idmac = to_idmac(ichan->dma_chan.device);
  806. if (!desc)
  807. return -ENOMEM;
  808. /* No interrupts, just disable the tasklet for a moment */
  809. tasklet_disable(&to_ipu(idmac)->tasklet);
  810. ichan->n_tx_desc = n;
  811. ichan->desc = desc;
  812. INIT_LIST_HEAD(&ichan->queue);
  813. INIT_LIST_HEAD(&ichan->free_list);
  814. while (n--) {
  815. struct dma_async_tx_descriptor *txd = &desc->txd;
  816. memset(txd, 0, sizeof(*txd));
  817. dma_async_tx_descriptor_init(txd, &ichan->dma_chan);
  818. txd->tx_submit = idmac_tx_submit;
  819. list_add(&desc->list, &ichan->free_list);
  820. desc++;
  821. }
  822. tasklet_enable(&to_ipu(idmac)->tasklet);
  823. return 0;
  824. }
  825. /**
  826. * ipu_init_channel() - initialize an IPU channel.
  827. * @idmac: IPU DMAC context.
  828. * @ichan: pointer to the channel object.
  829. * @return 0 on success or negative error code on failure.
  830. */
  831. static int ipu_init_channel(struct idmac *idmac, struct idmac_channel *ichan)
  832. {
  833. union ipu_channel_param *params = &ichan->params;
  834. uint32_t ipu_conf;
  835. enum ipu_channel channel = ichan->dma_chan.chan_id;
  836. unsigned long flags;
  837. uint32_t reg;
  838. struct ipu *ipu = to_ipu(idmac);
  839. int ret = 0, n_desc = 0;
  840. dev_dbg(ipu->dev, "init channel = %d\n", channel);
  841. if (channel != IDMAC_SDC_0 && channel != IDMAC_SDC_1 &&
  842. channel != IDMAC_IC_7)
  843. return -EINVAL;
  844. spin_lock_irqsave(&ipu->lock, flags);
  845. switch (channel) {
  846. case IDMAC_IC_7:
  847. n_desc = 16;
  848. reg = idmac_read_icreg(ipu, IC_CONF);
  849. idmac_write_icreg(ipu, reg & ~IC_CONF_CSI_MEM_WR_EN, IC_CONF);
  850. break;
  851. case IDMAC_IC_0:
  852. n_desc = 16;
  853. reg = idmac_read_ipureg(ipu, IPU_FS_PROC_FLOW);
  854. idmac_write_ipureg(ipu, reg & ~FS_ENC_IN_VALID, IPU_FS_PROC_FLOW);
  855. ret = ipu_ic_init_prpenc(ipu, params, true);
  856. break;
  857. case IDMAC_SDC_0:
  858. case IDMAC_SDC_1:
  859. n_desc = 4;
  860. default:
  861. break;
  862. }
  863. ipu->channel_init_mask |= 1L << channel;
  864. /* Enable IPU sub module */
  865. ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) |
  866. ipu_channel_conf_mask(channel);
  867. idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
  868. spin_unlock_irqrestore(&ipu->lock, flags);
  869. if (n_desc && !ichan->desc)
  870. ret = idmac_desc_alloc(ichan, n_desc);
  871. dump_idmac_reg(ipu);
  872. return ret;
  873. }
  874. /**
  875. * ipu_uninit_channel() - uninitialize an IPU channel.
  876. * @idmac: IPU DMAC context.
  877. * @ichan: pointer to the channel object.
  878. */
  879. static void ipu_uninit_channel(struct idmac *idmac, struct idmac_channel *ichan)
  880. {
  881. enum ipu_channel channel = ichan->dma_chan.chan_id;
  882. unsigned long flags;
  883. uint32_t reg;
  884. unsigned long chan_mask = 1UL << channel;
  885. uint32_t ipu_conf;
  886. struct ipu *ipu = to_ipu(idmac);
  887. spin_lock_irqsave(&ipu->lock, flags);
  888. if (!(ipu->channel_init_mask & chan_mask)) {
  889. dev_err(ipu->dev, "Channel already uninitialized %d\n",
  890. channel);
  891. spin_unlock_irqrestore(&ipu->lock, flags);
  892. return;
  893. }
  894. /* Reset the double buffer */
  895. reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
  896. idmac_write_ipureg(ipu, reg & ~chan_mask, IPU_CHA_DB_MODE_SEL);
  897. ichan->sec_chan_en = false;
  898. switch (channel) {
  899. case IDMAC_IC_7:
  900. reg = idmac_read_icreg(ipu, IC_CONF);
  901. idmac_write_icreg(ipu, reg & ~(IC_CONF_RWS_EN | IC_CONF_PRPENC_EN),
  902. IC_CONF);
  903. break;
  904. case IDMAC_IC_0:
  905. reg = idmac_read_icreg(ipu, IC_CONF);
  906. idmac_write_icreg(ipu, reg & ~(IC_CONF_PRPENC_EN | IC_CONF_PRPENC_CSC1),
  907. IC_CONF);
  908. break;
  909. case IDMAC_SDC_0:
  910. case IDMAC_SDC_1:
  911. default:
  912. break;
  913. }
  914. ipu->channel_init_mask &= ~(1L << channel);
  915. ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) &
  916. ~ipu_channel_conf_mask(channel);
  917. idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
  918. spin_unlock_irqrestore(&ipu->lock, flags);
  919. ichan->n_tx_desc = 0;
  920. vfree(ichan->desc);
  921. ichan->desc = NULL;
  922. }
  923. /**
  924. * ipu_disable_channel() - disable an IPU channel.
  925. * @idmac: IPU DMAC context.
  926. * @ichan: channel object pointer.
  927. * @wait_for_stop: flag to set whether to wait for channel end of frame or
  928. * return immediately.
  929. * @return: 0 on success or negative error code on failure.
  930. */
  931. static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
  932. bool wait_for_stop)
  933. {
  934. enum ipu_channel channel = ichan->dma_chan.chan_id;
  935. struct ipu *ipu = to_ipu(idmac);
  936. uint32_t reg;
  937. unsigned long flags;
  938. unsigned long chan_mask = 1UL << channel;
  939. unsigned int timeout;
  940. if (wait_for_stop && channel != IDMAC_SDC_1 && channel != IDMAC_SDC_0) {
  941. timeout = 40;
  942. /* This waiting always fails. Related to spurious irq problem */
  943. while ((idmac_read_icreg(ipu, IDMAC_CHA_BUSY) & chan_mask) ||
  944. (ipu_channel_status(ipu, channel) == TASK_STAT_ACTIVE)) {
  945. timeout--;
  946. msleep(10);
  947. if (!timeout) {
  948. dev_dbg(ipu->dev,
  949. "Warning: timeout waiting for channel %u to "
  950. "stop: buf0_rdy = 0x%08X, buf1_rdy = 0x%08X, "
  951. "busy = 0x%08X, tstat = 0x%08X\n", channel,
  952. idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
  953. idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
  954. idmac_read_icreg(ipu, IDMAC_CHA_BUSY),
  955. idmac_read_ipureg(ipu, IPU_TASKS_STAT));
  956. break;
  957. }
  958. }
  959. dev_dbg(ipu->dev, "timeout = %d * 10ms\n", 40 - timeout);
  960. }
  961. /* SDC BG and FG must be disabled before DMA is disabled */
  962. if (wait_for_stop && (channel == IDMAC_SDC_0 ||
  963. channel == IDMAC_SDC_1)) {
  964. for (timeout = 5;
  965. timeout && !ipu_irq_status(ichan->eof_irq); timeout--)
  966. msleep(5);
  967. }
  968. spin_lock_irqsave(&ipu->lock, flags);
  969. /* Disable IC task */
  970. ipu_ic_disable_task(ipu, channel);
  971. /* Disable DMA channel(s) */
  972. reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
  973. idmac_write_icreg(ipu, reg & ~chan_mask, IDMAC_CHA_EN);
  974. spin_unlock_irqrestore(&ipu->lock, flags);
  975. return 0;
  976. }
  977. static struct scatterlist *idmac_sg_next(struct idmac_channel *ichan,
  978. struct idmac_tx_desc **desc, struct scatterlist *sg)
  979. {
  980. struct scatterlist *sgnew = sg ? sg_next(sg) : NULL;
  981. if (sgnew)
  982. /* next sg-element in this list */
  983. return sgnew;
  984. if ((*desc)->list.next == &ichan->queue)
  985. /* No more descriptors on the queue */
  986. return NULL;
  987. /* Fetch next descriptor */
  988. *desc = list_entry((*desc)->list.next, struct idmac_tx_desc, list);
  989. return (*desc)->sg;
  990. }
  991. /*
  992. * We have several possibilities here:
  993. * current BUF next BUF
  994. *
  995. * not last sg next not last sg
  996. * not last sg next last sg
  997. * last sg first sg from next descriptor
  998. * last sg NULL
  999. *
  1000. * Besides, the descriptor queue might be empty or not. We process all these
  1001. * cases carefully.
  1002. */
  1003. static irqreturn_t idmac_interrupt(int irq, void *dev_id)
  1004. {
  1005. struct idmac_channel *ichan = dev_id;
  1006. struct device *dev = &ichan->dma_chan.dev->device;
  1007. unsigned int chan_id = ichan->dma_chan.chan_id;
  1008. struct scatterlist **sg, *sgnext, *sgnew = NULL;
  1009. /* Next transfer descriptor */
  1010. struct idmac_tx_desc *desc, *descnew;
  1011. dma_async_tx_callback callback;
  1012. void *callback_param;
  1013. bool done = false;
  1014. u32 ready0, ready1, curbuf, err;
  1015. unsigned long flags;
  1016. /* IDMAC has cleared the respective BUFx_RDY bit, we manage the buffer */
  1017. dev_dbg(dev, "IDMAC irq %d, buf %d\n", irq, ichan->active_buffer);
  1018. spin_lock_irqsave(&ipu_data.lock, flags);
  1019. ready0 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
  1020. ready1 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
  1021. curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
  1022. err = idmac_read_ipureg(&ipu_data, IPU_INT_STAT_4);
  1023. if (err & (1 << chan_id)) {
  1024. idmac_write_ipureg(&ipu_data, 1 << chan_id, IPU_INT_STAT_4);
  1025. spin_unlock_irqrestore(&ipu_data.lock, flags);
  1026. /*
  1027. * Doing this
  1028. * ichan->sg[0] = ichan->sg[1] = NULL;
  1029. * you can force channel re-enable on the next tx_submit(), but
  1030. * this is dirty - think about descriptors with multiple
  1031. * sg elements.
  1032. */
  1033. dev_warn(dev, "NFB4EOF on channel %d, ready %x, %x, cur %x\n",
  1034. chan_id, ready0, ready1, curbuf);
  1035. return IRQ_HANDLED;
  1036. }
  1037. spin_unlock_irqrestore(&ipu_data.lock, flags);
  1038. /* Other interrupts do not interfere with this channel */
  1039. spin_lock(&ichan->lock);
  1040. if (unlikely((ichan->active_buffer && (ready1 >> chan_id) & 1) ||
  1041. (!ichan->active_buffer && (ready0 >> chan_id) & 1)
  1042. )) {
  1043. spin_unlock(&ichan->lock);
  1044. dev_dbg(dev,
  1045. "IRQ with active buffer still ready on channel %x, "
  1046. "active %d, ready %x, %x!\n", chan_id,
  1047. ichan->active_buffer, ready0, ready1);
  1048. return IRQ_NONE;
  1049. }
  1050. if (unlikely(list_empty(&ichan->queue))) {
  1051. ichan->sg[ichan->active_buffer] = NULL;
  1052. spin_unlock(&ichan->lock);
  1053. dev_err(dev,
  1054. "IRQ without queued buffers on channel %x, active %d, "
  1055. "ready %x, %x!\n", chan_id,
  1056. ichan->active_buffer, ready0, ready1);
  1057. return IRQ_NONE;
  1058. }
  1059. /*
  1060. * active_buffer is a software flag, it shows which buffer we are
  1061. * currently expecting back from the hardware, IDMAC should be
  1062. * processing the other buffer already
  1063. */
  1064. sg = &ichan->sg[ichan->active_buffer];
  1065. sgnext = ichan->sg[!ichan->active_buffer];
  1066. if (!*sg) {
  1067. spin_unlock(&ichan->lock);
  1068. return IRQ_HANDLED;
  1069. }
  1070. desc = list_entry(ichan->queue.next, struct idmac_tx_desc, list);
  1071. descnew = desc;
  1072. dev_dbg(dev, "IDMAC irq %d, dma 0x%08x, next dma 0x%08x, current %d, curbuf 0x%08x\n",
  1073. irq, sg_dma_address(*sg), sgnext ? sg_dma_address(sgnext) : 0, ichan->active_buffer, curbuf);
  1074. /* Find the descriptor of sgnext */
  1075. sgnew = idmac_sg_next(ichan, &descnew, *sg);
  1076. if (sgnext != sgnew)
  1077. dev_err(dev, "Submitted buffer %p, next buffer %p\n", sgnext, sgnew);
  1078. /*
  1079. * if sgnext == NULL sg must be the last element in a scatterlist and
  1080. * queue must be empty
  1081. */
  1082. if (unlikely(!sgnext)) {
  1083. if (!WARN_ON(sg_next(*sg)))
  1084. dev_dbg(dev, "Underrun on channel %x\n", chan_id);
  1085. ichan->sg[!ichan->active_buffer] = sgnew;
  1086. if (unlikely(sgnew)) {
  1087. ipu_submit_buffer(ichan, descnew, sgnew, !ichan->active_buffer);
  1088. } else {
  1089. spin_lock_irqsave(&ipu_data.lock, flags);
  1090. ipu_ic_disable_task(&ipu_data, chan_id);
  1091. spin_unlock_irqrestore(&ipu_data.lock, flags);
  1092. ichan->status = IPU_CHANNEL_READY;
  1093. /* Continue to check for complete descriptor */
  1094. }
  1095. }
  1096. /* Calculate and submit the next sg element */
  1097. sgnew = idmac_sg_next(ichan, &descnew, sgnew);
  1098. if (unlikely(!sg_next(*sg)) || !sgnext) {
  1099. /*
  1100. * Last element in scatterlist done, remove from the queue,
  1101. * _init for debugging
  1102. */
  1103. list_del_init(&desc->list);
  1104. done = true;
  1105. }
  1106. *sg = sgnew;
  1107. if (likely(sgnew) &&
  1108. ipu_submit_buffer(ichan, descnew, sgnew, ichan->active_buffer) < 0) {
  1109. callback = descnew->txd.callback;
  1110. callback_param = descnew->txd.callback_param;
  1111. spin_unlock(&ichan->lock);
  1112. if (callback)
  1113. callback(callback_param);
  1114. spin_lock(&ichan->lock);
  1115. }
  1116. /* Flip the active buffer - even if update above failed */
  1117. ichan->active_buffer = !ichan->active_buffer;
  1118. if (done)
  1119. ichan->completed = desc->txd.cookie;
  1120. callback = desc->txd.callback;
  1121. callback_param = desc->txd.callback_param;
  1122. spin_unlock(&ichan->lock);
  1123. if (done && (desc->txd.flags & DMA_PREP_INTERRUPT) && callback)
  1124. callback(callback_param);
  1125. return IRQ_HANDLED;
  1126. }
  1127. static void ipu_gc_tasklet(unsigned long arg)
  1128. {
  1129. struct ipu *ipu = (struct ipu *)arg;
  1130. int i;
  1131. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1132. struct idmac_channel *ichan = ipu->channel + i;
  1133. struct idmac_tx_desc *desc;
  1134. unsigned long flags;
  1135. struct scatterlist *sg;
  1136. int j, k;
  1137. for (j = 0; j < ichan->n_tx_desc; j++) {
  1138. desc = ichan->desc + j;
  1139. spin_lock_irqsave(&ichan->lock, flags);
  1140. if (async_tx_test_ack(&desc->txd)) {
  1141. list_move(&desc->list, &ichan->free_list);
  1142. for_each_sg(desc->sg, sg, desc->sg_len, k) {
  1143. if (ichan->sg[0] == sg)
  1144. ichan->sg[0] = NULL;
  1145. else if (ichan->sg[1] == sg)
  1146. ichan->sg[1] = NULL;
  1147. }
  1148. async_tx_clear_ack(&desc->txd);
  1149. }
  1150. spin_unlock_irqrestore(&ichan->lock, flags);
  1151. }
  1152. }
  1153. }
  1154. /* Allocate and initialise a transfer descriptor. */
  1155. static struct dma_async_tx_descriptor *idmac_prep_slave_sg(struct dma_chan *chan,
  1156. struct scatterlist *sgl, unsigned int sg_len,
  1157. enum dma_data_direction direction, unsigned long tx_flags)
  1158. {
  1159. struct idmac_channel *ichan = to_idmac_chan(chan);
  1160. struct idmac_tx_desc *desc = NULL;
  1161. struct dma_async_tx_descriptor *txd = NULL;
  1162. unsigned long flags;
  1163. /* We only can handle these three channels so far */
  1164. if (chan->chan_id != IDMAC_SDC_0 && chan->chan_id != IDMAC_SDC_1 &&
  1165. chan->chan_id != IDMAC_IC_7)
  1166. return NULL;
  1167. if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE) {
  1168. dev_err(chan->device->dev, "Invalid DMA direction %d!\n", direction);
  1169. return NULL;
  1170. }
  1171. mutex_lock(&ichan->chan_mutex);
  1172. spin_lock_irqsave(&ichan->lock, flags);
  1173. if (!list_empty(&ichan->free_list)) {
  1174. desc = list_entry(ichan->free_list.next,
  1175. struct idmac_tx_desc, list);
  1176. list_del_init(&desc->list);
  1177. desc->sg_len = sg_len;
  1178. desc->sg = sgl;
  1179. txd = &desc->txd;
  1180. txd->flags = tx_flags;
  1181. }
  1182. spin_unlock_irqrestore(&ichan->lock, flags);
  1183. mutex_unlock(&ichan->chan_mutex);
  1184. tasklet_schedule(&to_ipu(to_idmac(chan->device))->tasklet);
  1185. return txd;
  1186. }
  1187. /* Re-select the current buffer and re-activate the channel */
  1188. static void idmac_issue_pending(struct dma_chan *chan)
  1189. {
  1190. struct idmac_channel *ichan = to_idmac_chan(chan);
  1191. struct idmac *idmac = to_idmac(chan->device);
  1192. struct ipu *ipu = to_ipu(idmac);
  1193. unsigned long flags;
  1194. /* This is not always needed, but doesn't hurt either */
  1195. spin_lock_irqsave(&ipu->lock, flags);
  1196. ipu_select_buffer(chan->chan_id, ichan->active_buffer);
  1197. spin_unlock_irqrestore(&ipu->lock, flags);
  1198. /*
  1199. * Might need to perform some parts of initialisation from
  1200. * ipu_enable_channel(), but not all, we do not want to reset to buffer
  1201. * 0, don't need to set priority again either, but re-enabling the task
  1202. * and the channel might be a good idea.
  1203. */
  1204. }
  1205. static int __idmac_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  1206. unsigned long arg)
  1207. {
  1208. struct idmac_channel *ichan = to_idmac_chan(chan);
  1209. struct idmac *idmac = to_idmac(chan->device);
  1210. unsigned long flags;
  1211. int i;
  1212. /* Only supports DMA_TERMINATE_ALL */
  1213. if (cmd != DMA_TERMINATE_ALL)
  1214. return -ENXIO;
  1215. ipu_disable_channel(idmac, ichan,
  1216. ichan->status >= IPU_CHANNEL_ENABLED);
  1217. tasklet_disable(&to_ipu(idmac)->tasklet);
  1218. /* ichan->queue is modified in ISR, have to spinlock */
  1219. spin_lock_irqsave(&ichan->lock, flags);
  1220. list_splice_init(&ichan->queue, &ichan->free_list);
  1221. if (ichan->desc)
  1222. for (i = 0; i < ichan->n_tx_desc; i++) {
  1223. struct idmac_tx_desc *desc = ichan->desc + i;
  1224. if (list_empty(&desc->list))
  1225. /* Descriptor was prepared, but not submitted */
  1226. list_add(&desc->list, &ichan->free_list);
  1227. async_tx_clear_ack(&desc->txd);
  1228. }
  1229. ichan->sg[0] = NULL;
  1230. ichan->sg[1] = NULL;
  1231. spin_unlock_irqrestore(&ichan->lock, flags);
  1232. tasklet_enable(&to_ipu(idmac)->tasklet);
  1233. ichan->status = IPU_CHANNEL_INITIALIZED;
  1234. return 0;
  1235. }
  1236. static int idmac_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  1237. unsigned long arg)
  1238. {
  1239. struct idmac_channel *ichan = to_idmac_chan(chan);
  1240. int ret;
  1241. mutex_lock(&ichan->chan_mutex);
  1242. ret = __idmac_control(chan, cmd, arg);
  1243. mutex_unlock(&ichan->chan_mutex);
  1244. return ret;
  1245. }
  1246. #ifdef DEBUG
  1247. static irqreturn_t ic_sof_irq(int irq, void *dev_id)
  1248. {
  1249. struct idmac_channel *ichan = dev_id;
  1250. printk(KERN_DEBUG "Got SOF IRQ %d on Channel %d\n",
  1251. irq, ichan->dma_chan.chan_id);
  1252. disable_irq_nosync(irq);
  1253. return IRQ_HANDLED;
  1254. }
  1255. static irqreturn_t ic_eof_irq(int irq, void *dev_id)
  1256. {
  1257. struct idmac_channel *ichan = dev_id;
  1258. printk(KERN_DEBUG "Got EOF IRQ %d on Channel %d\n",
  1259. irq, ichan->dma_chan.chan_id);
  1260. disable_irq_nosync(irq);
  1261. return IRQ_HANDLED;
  1262. }
  1263. static int ic_sof = -EINVAL, ic_eof = -EINVAL;
  1264. #endif
  1265. static int idmac_alloc_chan_resources(struct dma_chan *chan)
  1266. {
  1267. struct idmac_channel *ichan = to_idmac_chan(chan);
  1268. struct idmac *idmac = to_idmac(chan->device);
  1269. int ret;
  1270. /* dmaengine.c now guarantees to only offer free channels */
  1271. BUG_ON(chan->client_count > 1);
  1272. WARN_ON(ichan->status != IPU_CHANNEL_FREE);
  1273. chan->cookie = 1;
  1274. ichan->completed = -ENXIO;
  1275. ret = ipu_irq_map(chan->chan_id);
  1276. if (ret < 0)
  1277. goto eimap;
  1278. ichan->eof_irq = ret;
  1279. /*
  1280. * Important to first disable the channel, because maybe someone
  1281. * used it before us, e.g., the bootloader
  1282. */
  1283. ipu_disable_channel(idmac, ichan, true);
  1284. ret = ipu_init_channel(idmac, ichan);
  1285. if (ret < 0)
  1286. goto eichan;
  1287. ret = request_irq(ichan->eof_irq, idmac_interrupt, 0,
  1288. ichan->eof_name, ichan);
  1289. if (ret < 0)
  1290. goto erirq;
  1291. #ifdef DEBUG
  1292. if (chan->chan_id == IDMAC_IC_7) {
  1293. ic_sof = ipu_irq_map(69);
  1294. if (ic_sof > 0)
  1295. request_irq(ic_sof, ic_sof_irq, 0, "IC SOF", ichan);
  1296. ic_eof = ipu_irq_map(70);
  1297. if (ic_eof > 0)
  1298. request_irq(ic_eof, ic_eof_irq, 0, "IC EOF", ichan);
  1299. }
  1300. #endif
  1301. ichan->status = IPU_CHANNEL_INITIALIZED;
  1302. dev_dbg(&chan->dev->device, "Found channel 0x%x, irq %d\n",
  1303. chan->chan_id, ichan->eof_irq);
  1304. return ret;
  1305. erirq:
  1306. ipu_uninit_channel(idmac, ichan);
  1307. eichan:
  1308. ipu_irq_unmap(chan->chan_id);
  1309. eimap:
  1310. return ret;
  1311. }
  1312. static void idmac_free_chan_resources(struct dma_chan *chan)
  1313. {
  1314. struct idmac_channel *ichan = to_idmac_chan(chan);
  1315. struct idmac *idmac = to_idmac(chan->device);
  1316. mutex_lock(&ichan->chan_mutex);
  1317. __idmac_control(chan, DMA_TERMINATE_ALL, 0);
  1318. if (ichan->status > IPU_CHANNEL_FREE) {
  1319. #ifdef DEBUG
  1320. if (chan->chan_id == IDMAC_IC_7) {
  1321. if (ic_sof > 0) {
  1322. free_irq(ic_sof, ichan);
  1323. ipu_irq_unmap(69);
  1324. ic_sof = -EINVAL;
  1325. }
  1326. if (ic_eof > 0) {
  1327. free_irq(ic_eof, ichan);
  1328. ipu_irq_unmap(70);
  1329. ic_eof = -EINVAL;
  1330. }
  1331. }
  1332. #endif
  1333. free_irq(ichan->eof_irq, ichan);
  1334. ipu_irq_unmap(chan->chan_id);
  1335. }
  1336. ichan->status = IPU_CHANNEL_FREE;
  1337. ipu_uninit_channel(idmac, ichan);
  1338. mutex_unlock(&ichan->chan_mutex);
  1339. tasklet_schedule(&to_ipu(idmac)->tasklet);
  1340. }
  1341. static enum dma_status idmac_tx_status(struct dma_chan *chan,
  1342. dma_cookie_t cookie, struct dma_tx_state *txstate)
  1343. {
  1344. struct idmac_channel *ichan = to_idmac_chan(chan);
  1345. dma_set_tx_state(txstate, ichan->completed, chan->cookie, 0);
  1346. if (cookie != chan->cookie)
  1347. return DMA_ERROR;
  1348. return DMA_SUCCESS;
  1349. }
  1350. static int __init ipu_idmac_init(struct ipu *ipu)
  1351. {
  1352. struct idmac *idmac = &ipu->idmac;
  1353. struct dma_device *dma = &idmac->dma;
  1354. int i;
  1355. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  1356. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  1357. /* Compulsory common fields */
  1358. dma->dev = ipu->dev;
  1359. dma->device_alloc_chan_resources = idmac_alloc_chan_resources;
  1360. dma->device_free_chan_resources = idmac_free_chan_resources;
  1361. dma->device_tx_status = idmac_tx_status;
  1362. dma->device_issue_pending = idmac_issue_pending;
  1363. /* Compulsory for DMA_SLAVE fields */
  1364. dma->device_prep_slave_sg = idmac_prep_slave_sg;
  1365. dma->device_control = idmac_control;
  1366. INIT_LIST_HEAD(&dma->channels);
  1367. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1368. struct idmac_channel *ichan = ipu->channel + i;
  1369. struct dma_chan *dma_chan = &ichan->dma_chan;
  1370. spin_lock_init(&ichan->lock);
  1371. mutex_init(&ichan->chan_mutex);
  1372. ichan->status = IPU_CHANNEL_FREE;
  1373. ichan->sec_chan_en = false;
  1374. ichan->completed = -ENXIO;
  1375. snprintf(ichan->eof_name, sizeof(ichan->eof_name), "IDMAC EOF %d", i);
  1376. dma_chan->device = &idmac->dma;
  1377. dma_chan->cookie = 1;
  1378. dma_chan->chan_id = i;
  1379. list_add_tail(&dma_chan->device_node, &dma->channels);
  1380. }
  1381. idmac_write_icreg(ipu, 0x00000070, IDMAC_CONF);
  1382. return dma_async_device_register(&idmac->dma);
  1383. }
  1384. static void __exit ipu_idmac_exit(struct ipu *ipu)
  1385. {
  1386. int i;
  1387. struct idmac *idmac = &ipu->idmac;
  1388. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1389. struct idmac_channel *ichan = ipu->channel + i;
  1390. idmac_control(&ichan->dma_chan, DMA_TERMINATE_ALL, 0);
  1391. idmac_prep_slave_sg(&ichan->dma_chan, NULL, 0, DMA_NONE, 0);
  1392. }
  1393. dma_async_device_unregister(&idmac->dma);
  1394. }
  1395. /*****************************************************************************
  1396. * IPU common probe / remove
  1397. */
  1398. static int __init ipu_probe(struct platform_device *pdev)
  1399. {
  1400. struct ipu_platform_data *pdata = pdev->dev.platform_data;
  1401. struct resource *mem_ipu, *mem_ic;
  1402. int ret;
  1403. spin_lock_init(&ipu_data.lock);
  1404. mem_ipu = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1405. mem_ic = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1406. if (!pdata || !mem_ipu || !mem_ic)
  1407. return -EINVAL;
  1408. ipu_data.dev = &pdev->dev;
  1409. platform_set_drvdata(pdev, &ipu_data);
  1410. ret = platform_get_irq(pdev, 0);
  1411. if (ret < 0)
  1412. goto err_noirq;
  1413. ipu_data.irq_fn = ret;
  1414. ret = platform_get_irq(pdev, 1);
  1415. if (ret < 0)
  1416. goto err_noirq;
  1417. ipu_data.irq_err = ret;
  1418. ipu_data.irq_base = pdata->irq_base;
  1419. dev_dbg(&pdev->dev, "fn irq %u, err irq %u, irq-base %u\n",
  1420. ipu_data.irq_fn, ipu_data.irq_err, ipu_data.irq_base);
  1421. /* Remap IPU common registers */
  1422. ipu_data.reg_ipu = ioremap(mem_ipu->start,
  1423. mem_ipu->end - mem_ipu->start + 1);
  1424. if (!ipu_data.reg_ipu) {
  1425. ret = -ENOMEM;
  1426. goto err_ioremap_ipu;
  1427. }
  1428. /* Remap Image Converter and Image DMA Controller registers */
  1429. ipu_data.reg_ic = ioremap(mem_ic->start,
  1430. mem_ic->end - mem_ic->start + 1);
  1431. if (!ipu_data.reg_ic) {
  1432. ret = -ENOMEM;
  1433. goto err_ioremap_ic;
  1434. }
  1435. /* Get IPU clock */
  1436. ipu_data.ipu_clk = clk_get(&pdev->dev, NULL);
  1437. if (IS_ERR(ipu_data.ipu_clk)) {
  1438. ret = PTR_ERR(ipu_data.ipu_clk);
  1439. goto err_clk_get;
  1440. }
  1441. /* Make sure IPU HSP clock is running */
  1442. clk_enable(ipu_data.ipu_clk);
  1443. /* Disable all interrupts */
  1444. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_1);
  1445. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_2);
  1446. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_3);
  1447. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_4);
  1448. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_5);
  1449. dev_dbg(&pdev->dev, "%s @ 0x%08lx, fn irq %u, err irq %u\n", pdev->name,
  1450. (unsigned long)mem_ipu->start, ipu_data.irq_fn, ipu_data.irq_err);
  1451. ret = ipu_irq_attach_irq(&ipu_data, pdev);
  1452. if (ret < 0)
  1453. goto err_attach_irq;
  1454. /* Initialize DMA engine */
  1455. ret = ipu_idmac_init(&ipu_data);
  1456. if (ret < 0)
  1457. goto err_idmac_init;
  1458. tasklet_init(&ipu_data.tasklet, ipu_gc_tasklet, (unsigned long)&ipu_data);
  1459. ipu_data.dev = &pdev->dev;
  1460. dev_dbg(ipu_data.dev, "IPU initialized\n");
  1461. return 0;
  1462. err_idmac_init:
  1463. err_attach_irq:
  1464. ipu_irq_detach_irq(&ipu_data, pdev);
  1465. clk_disable(ipu_data.ipu_clk);
  1466. clk_put(ipu_data.ipu_clk);
  1467. err_clk_get:
  1468. iounmap(ipu_data.reg_ic);
  1469. err_ioremap_ic:
  1470. iounmap(ipu_data.reg_ipu);
  1471. err_ioremap_ipu:
  1472. err_noirq:
  1473. dev_err(&pdev->dev, "Failed to probe IPU: %d\n", ret);
  1474. return ret;
  1475. }
  1476. static int __exit ipu_remove(struct platform_device *pdev)
  1477. {
  1478. struct ipu *ipu = platform_get_drvdata(pdev);
  1479. ipu_idmac_exit(ipu);
  1480. ipu_irq_detach_irq(ipu, pdev);
  1481. clk_disable(ipu->ipu_clk);
  1482. clk_put(ipu->ipu_clk);
  1483. iounmap(ipu->reg_ic);
  1484. iounmap(ipu->reg_ipu);
  1485. tasklet_kill(&ipu->tasklet);
  1486. platform_set_drvdata(pdev, NULL);
  1487. return 0;
  1488. }
  1489. /*
  1490. * We need two MEM resources - with IPU-common and Image Converter registers,
  1491. * including PF_CONF and IDMAC_* registers, and two IRQs - function and error
  1492. */
  1493. static struct platform_driver ipu_platform_driver = {
  1494. .driver = {
  1495. .name = "ipu-core",
  1496. .owner = THIS_MODULE,
  1497. },
  1498. .remove = __exit_p(ipu_remove),
  1499. };
  1500. static int __init ipu_init(void)
  1501. {
  1502. return platform_driver_probe(&ipu_platform_driver, ipu_probe);
  1503. }
  1504. subsys_initcall(ipu_init);
  1505. MODULE_DESCRIPTION("IPU core driver");
  1506. MODULE_LICENSE("GPL v2");
  1507. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  1508. MODULE_ALIAS("platform:ipu-core");