ipu_idmac.c 48 KB

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