ipu_idmac.c 46 KB

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