ipu_idmac.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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. * @return: Returns 0 on success or negative error code on failure. This
  666. * function will fail if the buffer is set to ready.
  667. */
  668. /* Called under spin_lock(_irqsave)(&ichan->lock) */
  669. static int ipu_update_channel_buffer(struct idmac_channel *ichan,
  670. int buffer_n, dma_addr_t phyaddr)
  671. {
  672. enum ipu_channel channel = ichan->dma_chan.chan_id;
  673. uint32_t reg;
  674. unsigned long flags;
  675. spin_lock_irqsave(&ipu_data.lock, flags);
  676. if (buffer_n == 0) {
  677. reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
  678. if (reg & (1UL << channel)) {
  679. ipu_ic_disable_task(&ipu_data, channel);
  680. ichan->status = IPU_CHANNEL_READY;
  681. }
  682. /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 0) */
  683. idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
  684. 0x0008UL, IPU_IMA_ADDR);
  685. idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
  686. } else {
  687. reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
  688. if (reg & (1UL << channel)) {
  689. ipu_ic_disable_task(&ipu_data, channel);
  690. ichan->status = IPU_CHANNEL_READY;
  691. }
  692. /* Check if double-buffering is already enabled */
  693. reg = idmac_read_ipureg(&ipu_data, IPU_CHA_DB_MODE_SEL);
  694. if (!(reg & (1UL << channel)))
  695. idmac_write_ipureg(&ipu_data, reg | (1UL << channel),
  696. IPU_CHA_DB_MODE_SEL);
  697. /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 1) */
  698. idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
  699. 0x0009UL, IPU_IMA_ADDR);
  700. idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
  701. }
  702. spin_unlock_irqrestore(&ipu_data.lock, flags);
  703. return 0;
  704. }
  705. /* Called under spin_lock_irqsave(&ichan->lock) */
  706. static int ipu_submit_buffer(struct idmac_channel *ichan,
  707. struct idmac_tx_desc *desc, struct scatterlist *sg, int buf_idx)
  708. {
  709. unsigned int chan_id = ichan->dma_chan.chan_id;
  710. struct device *dev = &ichan->dma_chan.dev->device;
  711. int ret;
  712. if (async_tx_test_ack(&desc->txd))
  713. return -EINTR;
  714. /*
  715. * On first invocation this shouldn't be necessary, the call to
  716. * ipu_init_channel_buffer() above will set addresses for us, so we
  717. * could make it conditional on status >= IPU_CHANNEL_ENABLED, but
  718. * doing it again shouldn't hurt either.
  719. */
  720. ret = ipu_update_channel_buffer(ichan, buf_idx,
  721. sg_dma_address(sg));
  722. if (ret < 0) {
  723. dev_err(dev, "Updating sg %p on channel 0x%x buffer %d failed!\n",
  724. sg, chan_id, buf_idx);
  725. return ret;
  726. }
  727. ipu_select_buffer(chan_id, buf_idx);
  728. dev_dbg(dev, "Updated sg %p on channel 0x%x buffer %d\n",
  729. sg, chan_id, buf_idx);
  730. return 0;
  731. }
  732. /* Called under spin_lock_irqsave(&ichan->lock) */
  733. static int ipu_submit_channel_buffers(struct idmac_channel *ichan,
  734. struct idmac_tx_desc *desc)
  735. {
  736. struct scatterlist *sg;
  737. int i, ret = 0;
  738. for (i = 0, sg = desc->sg; i < 2 && sg; i++) {
  739. if (!ichan->sg[i]) {
  740. ichan->sg[i] = sg;
  741. ret = ipu_submit_buffer(ichan, desc, sg, i);
  742. if (ret < 0)
  743. return ret;
  744. sg = sg_next(sg);
  745. }
  746. }
  747. return ret;
  748. }
  749. static dma_cookie_t idmac_tx_submit(struct dma_async_tx_descriptor *tx)
  750. {
  751. struct idmac_tx_desc *desc = to_tx_desc(tx);
  752. struct idmac_channel *ichan = to_idmac_chan(tx->chan);
  753. struct idmac *idmac = to_idmac(tx->chan->device);
  754. struct ipu *ipu = to_ipu(idmac);
  755. struct device *dev = &ichan->dma_chan.dev->device;
  756. dma_cookie_t cookie;
  757. unsigned long flags;
  758. int ret;
  759. /* Sanity check */
  760. if (!list_empty(&desc->list)) {
  761. /* The descriptor doesn't belong to client */
  762. dev_err(dev, "Descriptor %p not prepared!\n", tx);
  763. return -EBUSY;
  764. }
  765. mutex_lock(&ichan->chan_mutex);
  766. async_tx_clear_ack(tx);
  767. if (ichan->status < IPU_CHANNEL_READY) {
  768. struct idmac_video_param *video = &ichan->params.video;
  769. /*
  770. * Initial buffer assignment - the first two sg-entries from
  771. * the descriptor will end up in the IDMAC buffers
  772. */
  773. dma_addr_t dma_1 = sg_is_last(desc->sg) ? 0 :
  774. sg_dma_address(&desc->sg[1]);
  775. WARN_ON(ichan->sg[0] || ichan->sg[1]);
  776. cookie = ipu_init_channel_buffer(ichan,
  777. video->out_pixel_fmt,
  778. video->out_width,
  779. video->out_height,
  780. video->out_stride,
  781. IPU_ROTATE_NONE,
  782. sg_dma_address(&desc->sg[0]),
  783. dma_1);
  784. if (cookie < 0)
  785. goto out;
  786. }
  787. dev_dbg(dev, "Submitting sg %p\n", &desc->sg[0]);
  788. cookie = ichan->dma_chan.cookie;
  789. if (++cookie < 0)
  790. cookie = 1;
  791. /* from dmaengine.h: "last cookie value returned to client" */
  792. ichan->dma_chan.cookie = cookie;
  793. tx->cookie = cookie;
  794. /* ipu->lock can be taken under ichan->lock, but not v.v. */
  795. spin_lock_irqsave(&ichan->lock, flags);
  796. list_add_tail(&desc->list, &ichan->queue);
  797. /* submit_buffers() atomically verifies and fills empty sg slots */
  798. ret = ipu_submit_channel_buffers(ichan, desc);
  799. spin_unlock_irqrestore(&ichan->lock, flags);
  800. if (ret < 0) {
  801. cookie = ret;
  802. goto dequeue;
  803. }
  804. if (ichan->status < IPU_CHANNEL_ENABLED) {
  805. ret = ipu_enable_channel(idmac, ichan);
  806. if (ret < 0) {
  807. cookie = ret;
  808. goto dequeue;
  809. }
  810. }
  811. dump_idmac_reg(ipu);
  812. dequeue:
  813. if (cookie < 0) {
  814. spin_lock_irqsave(&ichan->lock, flags);
  815. list_del_init(&desc->list);
  816. spin_unlock_irqrestore(&ichan->lock, flags);
  817. tx->cookie = cookie;
  818. ichan->dma_chan.cookie = cookie;
  819. }
  820. out:
  821. mutex_unlock(&ichan->chan_mutex);
  822. return cookie;
  823. }
  824. /* Called with ichan->chan_mutex held */
  825. static int idmac_desc_alloc(struct idmac_channel *ichan, int n)
  826. {
  827. struct idmac_tx_desc *desc = vmalloc(n * sizeof(struct idmac_tx_desc));
  828. struct idmac *idmac = to_idmac(ichan->dma_chan.device);
  829. if (!desc)
  830. return -ENOMEM;
  831. /* No interrupts, just disable the tasklet for a moment */
  832. tasklet_disable(&to_ipu(idmac)->tasklet);
  833. ichan->n_tx_desc = n;
  834. ichan->desc = desc;
  835. INIT_LIST_HEAD(&ichan->queue);
  836. INIT_LIST_HEAD(&ichan->free_list);
  837. while (n--) {
  838. struct dma_async_tx_descriptor *txd = &desc->txd;
  839. memset(txd, 0, sizeof(*txd));
  840. dma_async_tx_descriptor_init(txd, &ichan->dma_chan);
  841. txd->tx_submit = idmac_tx_submit;
  842. list_add(&desc->list, &ichan->free_list);
  843. desc++;
  844. }
  845. tasklet_enable(&to_ipu(idmac)->tasklet);
  846. return 0;
  847. }
  848. /**
  849. * ipu_init_channel() - initialize an IPU channel.
  850. * @idmac: IPU DMAC context.
  851. * @ichan: pointer to the channel object.
  852. * @return 0 on success or negative error code on failure.
  853. */
  854. static int ipu_init_channel(struct idmac *idmac, struct idmac_channel *ichan)
  855. {
  856. union ipu_channel_param *params = &ichan->params;
  857. uint32_t ipu_conf;
  858. enum ipu_channel channel = ichan->dma_chan.chan_id;
  859. unsigned long flags;
  860. uint32_t reg;
  861. struct ipu *ipu = to_ipu(idmac);
  862. int ret = 0, n_desc = 0;
  863. dev_dbg(ipu->dev, "init channel = %d\n", channel);
  864. if (channel != IDMAC_SDC_0 && channel != IDMAC_SDC_1 &&
  865. channel != IDMAC_IC_7)
  866. return -EINVAL;
  867. spin_lock_irqsave(&ipu->lock, flags);
  868. switch (channel) {
  869. case IDMAC_IC_7:
  870. n_desc = 16;
  871. reg = idmac_read_icreg(ipu, IC_CONF);
  872. idmac_write_icreg(ipu, reg & ~IC_CONF_CSI_MEM_WR_EN, IC_CONF);
  873. break;
  874. case IDMAC_IC_0:
  875. n_desc = 16;
  876. reg = idmac_read_ipureg(ipu, IPU_FS_PROC_FLOW);
  877. idmac_write_ipureg(ipu, reg & ~FS_ENC_IN_VALID, IPU_FS_PROC_FLOW);
  878. ret = ipu_ic_init_prpenc(ipu, params, true);
  879. break;
  880. case IDMAC_SDC_0:
  881. case IDMAC_SDC_1:
  882. n_desc = 4;
  883. default:
  884. break;
  885. }
  886. ipu->channel_init_mask |= 1L << channel;
  887. /* Enable IPU sub module */
  888. ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) |
  889. ipu_channel_conf_mask(channel);
  890. idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
  891. spin_unlock_irqrestore(&ipu->lock, flags);
  892. if (n_desc && !ichan->desc)
  893. ret = idmac_desc_alloc(ichan, n_desc);
  894. dump_idmac_reg(ipu);
  895. return ret;
  896. }
  897. /**
  898. * ipu_uninit_channel() - uninitialize an IPU channel.
  899. * @idmac: IPU DMAC context.
  900. * @ichan: pointer to the channel object.
  901. */
  902. static void ipu_uninit_channel(struct idmac *idmac, struct idmac_channel *ichan)
  903. {
  904. enum ipu_channel channel = ichan->dma_chan.chan_id;
  905. unsigned long flags;
  906. uint32_t reg;
  907. unsigned long chan_mask = 1UL << channel;
  908. uint32_t ipu_conf;
  909. struct ipu *ipu = to_ipu(idmac);
  910. spin_lock_irqsave(&ipu->lock, flags);
  911. if (!(ipu->channel_init_mask & chan_mask)) {
  912. dev_err(ipu->dev, "Channel already uninitialized %d\n",
  913. channel);
  914. spin_unlock_irqrestore(&ipu->lock, flags);
  915. return;
  916. }
  917. /* Reset the double buffer */
  918. reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
  919. idmac_write_ipureg(ipu, reg & ~chan_mask, IPU_CHA_DB_MODE_SEL);
  920. ichan->sec_chan_en = false;
  921. switch (channel) {
  922. case IDMAC_IC_7:
  923. reg = idmac_read_icreg(ipu, IC_CONF);
  924. idmac_write_icreg(ipu, reg & ~(IC_CONF_RWS_EN | IC_CONF_PRPENC_EN),
  925. IC_CONF);
  926. break;
  927. case IDMAC_IC_0:
  928. reg = idmac_read_icreg(ipu, IC_CONF);
  929. idmac_write_icreg(ipu, reg & ~(IC_CONF_PRPENC_EN | IC_CONF_PRPENC_CSC1),
  930. IC_CONF);
  931. break;
  932. case IDMAC_SDC_0:
  933. case IDMAC_SDC_1:
  934. default:
  935. break;
  936. }
  937. ipu->channel_init_mask &= ~(1L << channel);
  938. ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) &
  939. ~ipu_channel_conf_mask(channel);
  940. idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
  941. spin_unlock_irqrestore(&ipu->lock, flags);
  942. ichan->n_tx_desc = 0;
  943. vfree(ichan->desc);
  944. ichan->desc = NULL;
  945. }
  946. /**
  947. * ipu_disable_channel() - disable an IPU channel.
  948. * @idmac: IPU DMAC context.
  949. * @ichan: channel object pointer.
  950. * @wait_for_stop: flag to set whether to wait for channel end of frame or
  951. * return immediately.
  952. * @return: 0 on success or negative error code on failure.
  953. */
  954. static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
  955. bool wait_for_stop)
  956. {
  957. enum ipu_channel channel = ichan->dma_chan.chan_id;
  958. struct ipu *ipu = to_ipu(idmac);
  959. uint32_t reg;
  960. unsigned long flags;
  961. unsigned long chan_mask = 1UL << channel;
  962. unsigned int timeout;
  963. if (wait_for_stop && channel != IDMAC_SDC_1 && channel != IDMAC_SDC_0) {
  964. timeout = 40;
  965. /* This waiting always fails. Related to spurious irq problem */
  966. while ((idmac_read_icreg(ipu, IDMAC_CHA_BUSY) & chan_mask) ||
  967. (ipu_channel_status(ipu, channel) == TASK_STAT_ACTIVE)) {
  968. timeout--;
  969. msleep(10);
  970. if (!timeout) {
  971. dev_dbg(ipu->dev,
  972. "Warning: timeout waiting for channel %u to "
  973. "stop: buf0_rdy = 0x%08X, buf1_rdy = 0x%08X, "
  974. "busy = 0x%08X, tstat = 0x%08X\n", channel,
  975. idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
  976. idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
  977. idmac_read_icreg(ipu, IDMAC_CHA_BUSY),
  978. idmac_read_ipureg(ipu, IPU_TASKS_STAT));
  979. break;
  980. }
  981. }
  982. dev_dbg(ipu->dev, "timeout = %d * 10ms\n", 40 - timeout);
  983. }
  984. /* SDC BG and FG must be disabled before DMA is disabled */
  985. if (wait_for_stop && (channel == IDMAC_SDC_0 ||
  986. channel == IDMAC_SDC_1)) {
  987. for (timeout = 5;
  988. timeout && !ipu_irq_status(ichan->eof_irq); timeout--)
  989. msleep(5);
  990. }
  991. spin_lock_irqsave(&ipu->lock, flags);
  992. /* Disable IC task */
  993. ipu_ic_disable_task(ipu, channel);
  994. /* Disable DMA channel(s) */
  995. reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
  996. idmac_write_icreg(ipu, reg & ~chan_mask, IDMAC_CHA_EN);
  997. /*
  998. * Problem (observed with channel DMAIC_7): after enabling the channel
  999. * and initialising buffers, there comes an interrupt with current still
  1000. * pointing at buffer 0, whereas it should use buffer 0 first and only
  1001. * generate an interrupt when it is done, then current should already
  1002. * point to buffer 1. This spurious interrupt also comes on channel
  1003. * DMASDC_0. With DMAIC_7 normally, is we just leave the ISR after the
  1004. * first interrupt, there comes the second with current correctly
  1005. * pointing to buffer 1 this time. But sometimes this second interrupt
  1006. * doesn't come and the channel hangs. Clearing BUFx_RDY when disabling
  1007. * the channel seems to prevent the channel from hanging, but it doesn't
  1008. * prevent the spurious interrupt. This might also be unsafe. Think
  1009. * about the IDMAC controller trying to switch to a buffer, when we
  1010. * clear the ready bit, and re-enable it a moment later.
  1011. */
  1012. reg = idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY);
  1013. idmac_write_ipureg(ipu, 0, IPU_CHA_BUF0_RDY);
  1014. idmac_write_ipureg(ipu, reg & ~(1UL << channel), IPU_CHA_BUF0_RDY);
  1015. reg = idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY);
  1016. idmac_write_ipureg(ipu, 0, IPU_CHA_BUF1_RDY);
  1017. idmac_write_ipureg(ipu, reg & ~(1UL << channel), IPU_CHA_BUF1_RDY);
  1018. spin_unlock_irqrestore(&ipu->lock, flags);
  1019. return 0;
  1020. }
  1021. static struct scatterlist *idmac_sg_next(struct idmac_channel *ichan,
  1022. struct idmac_tx_desc **desc, struct scatterlist *sg)
  1023. {
  1024. struct scatterlist *sgnew = sg ? sg_next(sg) : NULL;
  1025. if (sgnew)
  1026. /* next sg-element in this list */
  1027. return sgnew;
  1028. if ((*desc)->list.next == &ichan->queue)
  1029. /* No more descriptors on the queue */
  1030. return NULL;
  1031. /* Fetch next descriptor */
  1032. *desc = list_entry((*desc)->list.next, struct idmac_tx_desc, list);
  1033. return (*desc)->sg;
  1034. }
  1035. /*
  1036. * We have several possibilities here:
  1037. * current BUF next BUF
  1038. *
  1039. * not last sg next not last sg
  1040. * not last sg next last sg
  1041. * last sg first sg from next descriptor
  1042. * last sg NULL
  1043. *
  1044. * Besides, the descriptor queue might be empty or not. We process all these
  1045. * cases carefully.
  1046. */
  1047. static irqreturn_t idmac_interrupt(int irq, void *dev_id)
  1048. {
  1049. struct idmac_channel *ichan = dev_id;
  1050. struct device *dev = &ichan->dma_chan.dev->device;
  1051. unsigned int chan_id = ichan->dma_chan.chan_id;
  1052. struct scatterlist **sg, *sgnext, *sgnew = NULL;
  1053. /* Next transfer descriptor */
  1054. struct idmac_tx_desc *desc, *descnew;
  1055. dma_async_tx_callback callback;
  1056. void *callback_param;
  1057. bool done = false;
  1058. u32 ready0, ready1, curbuf, err;
  1059. unsigned long flags;
  1060. /* IDMAC has cleared the respective BUFx_RDY bit, we manage the buffer */
  1061. dev_dbg(dev, "IDMAC irq %d, buf %d\n", irq, ichan->active_buffer);
  1062. spin_lock_irqsave(&ipu_data.lock, flags);
  1063. ready0 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
  1064. ready1 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
  1065. curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
  1066. err = idmac_read_ipureg(&ipu_data, IPU_INT_STAT_4);
  1067. if (err & (1 << chan_id)) {
  1068. idmac_write_ipureg(&ipu_data, 1 << chan_id, IPU_INT_STAT_4);
  1069. spin_unlock_irqrestore(&ipu_data.lock, flags);
  1070. /*
  1071. * Doing this
  1072. * ichan->sg[0] = ichan->sg[1] = NULL;
  1073. * you can force channel re-enable on the next tx_submit(), but
  1074. * this is dirty - think about descriptors with multiple
  1075. * sg elements.
  1076. */
  1077. dev_warn(dev, "NFB4EOF on channel %d, ready %x, %x, cur %x\n",
  1078. chan_id, ready0, ready1, curbuf);
  1079. return IRQ_HANDLED;
  1080. }
  1081. spin_unlock_irqrestore(&ipu_data.lock, flags);
  1082. /* Other interrupts do not interfere with this channel */
  1083. spin_lock(&ichan->lock);
  1084. if (unlikely(chan_id != IDMAC_SDC_0 && chan_id != IDMAC_SDC_1 &&
  1085. ((curbuf >> chan_id) & 1) == ichan->active_buffer &&
  1086. !list_is_last(ichan->queue.next, &ichan->queue))) {
  1087. int i = 100;
  1088. /* This doesn't help. See comment in ipu_disable_channel() */
  1089. while (--i) {
  1090. curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
  1091. if (((curbuf >> chan_id) & 1) != ichan->active_buffer)
  1092. break;
  1093. cpu_relax();
  1094. }
  1095. if (!i) {
  1096. spin_unlock(&ichan->lock);
  1097. dev_dbg(dev,
  1098. "IRQ on active buffer on channel %x, active "
  1099. "%d, ready %x, %x, current %x!\n", chan_id,
  1100. ichan->active_buffer, ready0, ready1, curbuf);
  1101. return IRQ_NONE;
  1102. } else
  1103. dev_dbg(dev,
  1104. "Buffer deactivated on channel %x, active "
  1105. "%d, ready %x, %x, current %x, rest %d!\n", chan_id,
  1106. ichan->active_buffer, ready0, ready1, curbuf, i);
  1107. }
  1108. if (unlikely((ichan->active_buffer && (ready1 >> chan_id) & 1) ||
  1109. (!ichan->active_buffer && (ready0 >> chan_id) & 1)
  1110. )) {
  1111. spin_unlock(&ichan->lock);
  1112. dev_dbg(dev,
  1113. "IRQ with active buffer still ready on channel %x, "
  1114. "active %d, ready %x, %x!\n", chan_id,
  1115. ichan->active_buffer, ready0, ready1);
  1116. return IRQ_NONE;
  1117. }
  1118. if (unlikely(list_empty(&ichan->queue))) {
  1119. ichan->sg[ichan->active_buffer] = NULL;
  1120. spin_unlock(&ichan->lock);
  1121. dev_err(dev,
  1122. "IRQ without queued buffers on channel %x, active %d, "
  1123. "ready %x, %x!\n", chan_id,
  1124. ichan->active_buffer, ready0, ready1);
  1125. return IRQ_NONE;
  1126. }
  1127. /*
  1128. * active_buffer is a software flag, it shows which buffer we are
  1129. * currently expecting back from the hardware, IDMAC should be
  1130. * processing the other buffer already
  1131. */
  1132. sg = &ichan->sg[ichan->active_buffer];
  1133. sgnext = ichan->sg[!ichan->active_buffer];
  1134. if (!*sg) {
  1135. spin_unlock(&ichan->lock);
  1136. return IRQ_HANDLED;
  1137. }
  1138. desc = list_entry(ichan->queue.next, struct idmac_tx_desc, list);
  1139. descnew = desc;
  1140. dev_dbg(dev, "IDMAC irq %d, dma 0x%08x, next dma 0x%08x, current %d, curbuf 0x%08x\n",
  1141. irq, sg_dma_address(*sg), sgnext ? sg_dma_address(sgnext) : 0, ichan->active_buffer, curbuf);
  1142. /* Find the descriptor of sgnext */
  1143. sgnew = idmac_sg_next(ichan, &descnew, *sg);
  1144. if (sgnext != sgnew)
  1145. dev_err(dev, "Submitted buffer %p, next buffer %p\n", sgnext, sgnew);
  1146. /*
  1147. * if sgnext == NULL sg must be the last element in a scatterlist and
  1148. * queue must be empty
  1149. */
  1150. if (unlikely(!sgnext)) {
  1151. if (!WARN_ON(sg_next(*sg)))
  1152. dev_dbg(dev, "Underrun on channel %x\n", chan_id);
  1153. ichan->sg[!ichan->active_buffer] = sgnew;
  1154. if (unlikely(sgnew)) {
  1155. ipu_submit_buffer(ichan, descnew, sgnew, !ichan->active_buffer);
  1156. } else {
  1157. spin_lock_irqsave(&ipu_data.lock, flags);
  1158. ipu_ic_disable_task(&ipu_data, chan_id);
  1159. spin_unlock_irqrestore(&ipu_data.lock, flags);
  1160. ichan->status = IPU_CHANNEL_READY;
  1161. /* Continue to check for complete descriptor */
  1162. }
  1163. }
  1164. /* Calculate and submit the next sg element */
  1165. sgnew = idmac_sg_next(ichan, &descnew, sgnew);
  1166. if (unlikely(!sg_next(*sg)) || !sgnext) {
  1167. /*
  1168. * Last element in scatterlist done, remove from the queue,
  1169. * _init for debugging
  1170. */
  1171. list_del_init(&desc->list);
  1172. done = true;
  1173. }
  1174. *sg = sgnew;
  1175. if (likely(sgnew) &&
  1176. ipu_submit_buffer(ichan, descnew, sgnew, ichan->active_buffer) < 0) {
  1177. callback = desc->txd.callback;
  1178. callback_param = desc->txd.callback_param;
  1179. spin_unlock(&ichan->lock);
  1180. callback(callback_param);
  1181. spin_lock(&ichan->lock);
  1182. }
  1183. /* Flip the active buffer - even if update above failed */
  1184. ichan->active_buffer = !ichan->active_buffer;
  1185. if (done)
  1186. ichan->completed = desc->txd.cookie;
  1187. callback = desc->txd.callback;
  1188. callback_param = desc->txd.callback_param;
  1189. spin_unlock(&ichan->lock);
  1190. if (done && (desc->txd.flags & DMA_PREP_INTERRUPT) && callback)
  1191. callback(callback_param);
  1192. return IRQ_HANDLED;
  1193. }
  1194. static void ipu_gc_tasklet(unsigned long arg)
  1195. {
  1196. struct ipu *ipu = (struct ipu *)arg;
  1197. int i;
  1198. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1199. struct idmac_channel *ichan = ipu->channel + i;
  1200. struct idmac_tx_desc *desc;
  1201. unsigned long flags;
  1202. struct scatterlist *sg;
  1203. int j, k;
  1204. for (j = 0; j < ichan->n_tx_desc; j++) {
  1205. desc = ichan->desc + j;
  1206. spin_lock_irqsave(&ichan->lock, flags);
  1207. if (async_tx_test_ack(&desc->txd)) {
  1208. list_move(&desc->list, &ichan->free_list);
  1209. for_each_sg(desc->sg, sg, desc->sg_len, k) {
  1210. if (ichan->sg[0] == sg)
  1211. ichan->sg[0] = NULL;
  1212. else if (ichan->sg[1] == sg)
  1213. ichan->sg[1] = NULL;
  1214. }
  1215. async_tx_clear_ack(&desc->txd);
  1216. }
  1217. spin_unlock_irqrestore(&ichan->lock, flags);
  1218. }
  1219. }
  1220. }
  1221. /* Allocate and initialise a transfer descriptor. */
  1222. static struct dma_async_tx_descriptor *idmac_prep_slave_sg(struct dma_chan *chan,
  1223. struct scatterlist *sgl, unsigned int sg_len,
  1224. enum dma_data_direction direction, unsigned long tx_flags)
  1225. {
  1226. struct idmac_channel *ichan = to_idmac_chan(chan);
  1227. struct idmac_tx_desc *desc = NULL;
  1228. struct dma_async_tx_descriptor *txd = NULL;
  1229. unsigned long flags;
  1230. /* We only can handle these three channels so far */
  1231. if (chan->chan_id != IDMAC_SDC_0 && chan->chan_id != IDMAC_SDC_1 &&
  1232. chan->chan_id != IDMAC_IC_7)
  1233. return NULL;
  1234. if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE) {
  1235. dev_err(chan->device->dev, "Invalid DMA direction %d!\n", direction);
  1236. return NULL;
  1237. }
  1238. mutex_lock(&ichan->chan_mutex);
  1239. spin_lock_irqsave(&ichan->lock, flags);
  1240. if (!list_empty(&ichan->free_list)) {
  1241. desc = list_entry(ichan->free_list.next,
  1242. struct idmac_tx_desc, list);
  1243. list_del_init(&desc->list);
  1244. desc->sg_len = sg_len;
  1245. desc->sg = sgl;
  1246. txd = &desc->txd;
  1247. txd->flags = tx_flags;
  1248. }
  1249. spin_unlock_irqrestore(&ichan->lock, flags);
  1250. mutex_unlock(&ichan->chan_mutex);
  1251. tasklet_schedule(&to_ipu(to_idmac(chan->device))->tasklet);
  1252. return txd;
  1253. }
  1254. /* Re-select the current buffer and re-activate the channel */
  1255. static void idmac_issue_pending(struct dma_chan *chan)
  1256. {
  1257. struct idmac_channel *ichan = to_idmac_chan(chan);
  1258. struct idmac *idmac = to_idmac(chan->device);
  1259. struct ipu *ipu = to_ipu(idmac);
  1260. unsigned long flags;
  1261. /* This is not always needed, but doesn't hurt either */
  1262. spin_lock_irqsave(&ipu->lock, flags);
  1263. ipu_select_buffer(chan->chan_id, ichan->active_buffer);
  1264. spin_unlock_irqrestore(&ipu->lock, flags);
  1265. /*
  1266. * Might need to perform some parts of initialisation from
  1267. * ipu_enable_channel(), but not all, we do not want to reset to buffer
  1268. * 0, don't need to set priority again either, but re-enabling the task
  1269. * and the channel might be a good idea.
  1270. */
  1271. }
  1272. static void __idmac_terminate_all(struct dma_chan *chan)
  1273. {
  1274. struct idmac_channel *ichan = to_idmac_chan(chan);
  1275. struct idmac *idmac = to_idmac(chan->device);
  1276. unsigned long flags;
  1277. int i;
  1278. ipu_disable_channel(idmac, ichan,
  1279. ichan->status >= IPU_CHANNEL_ENABLED);
  1280. tasklet_disable(&to_ipu(idmac)->tasklet);
  1281. /* ichan->queue is modified in ISR, have to spinlock */
  1282. spin_lock_irqsave(&ichan->lock, flags);
  1283. list_splice_init(&ichan->queue, &ichan->free_list);
  1284. if (ichan->desc)
  1285. for (i = 0; i < ichan->n_tx_desc; i++) {
  1286. struct idmac_tx_desc *desc = ichan->desc + i;
  1287. if (list_empty(&desc->list))
  1288. /* Descriptor was prepared, but not submitted */
  1289. list_add(&desc->list, &ichan->free_list);
  1290. async_tx_clear_ack(&desc->txd);
  1291. }
  1292. ichan->sg[0] = NULL;
  1293. ichan->sg[1] = NULL;
  1294. spin_unlock_irqrestore(&ichan->lock, flags);
  1295. tasklet_enable(&to_ipu(idmac)->tasklet);
  1296. ichan->status = IPU_CHANNEL_INITIALIZED;
  1297. }
  1298. static void idmac_terminate_all(struct dma_chan *chan)
  1299. {
  1300. struct idmac_channel *ichan = to_idmac_chan(chan);
  1301. mutex_lock(&ichan->chan_mutex);
  1302. __idmac_terminate_all(chan);
  1303. mutex_unlock(&ichan->chan_mutex);
  1304. }
  1305. #ifdef DEBUG
  1306. static irqreturn_t ic_sof_irq(int irq, void *dev_id)
  1307. {
  1308. struct idmac_channel *ichan = dev_id;
  1309. printk(KERN_DEBUG "Got SOF IRQ %d on Channel %d\n",
  1310. irq, ichan->dma_chan.chan_id);
  1311. disable_irq_nosync(irq);
  1312. return IRQ_HANDLED;
  1313. }
  1314. static irqreturn_t ic_eof_irq(int irq, void *dev_id)
  1315. {
  1316. struct idmac_channel *ichan = dev_id;
  1317. printk(KERN_DEBUG "Got EOF IRQ %d on Channel %d\n",
  1318. irq, ichan->dma_chan.chan_id);
  1319. disable_irq_nosync(irq);
  1320. return IRQ_HANDLED;
  1321. }
  1322. static int ic_sof = -EINVAL, ic_eof = -EINVAL;
  1323. #endif
  1324. static int idmac_alloc_chan_resources(struct dma_chan *chan)
  1325. {
  1326. struct idmac_channel *ichan = to_idmac_chan(chan);
  1327. struct idmac *idmac = to_idmac(chan->device);
  1328. int ret;
  1329. /* dmaengine.c now guarantees to only offer free channels */
  1330. BUG_ON(chan->client_count > 1);
  1331. WARN_ON(ichan->status != IPU_CHANNEL_FREE);
  1332. chan->cookie = 1;
  1333. ichan->completed = -ENXIO;
  1334. ret = ipu_irq_map(chan->chan_id);
  1335. if (ret < 0)
  1336. goto eimap;
  1337. ichan->eof_irq = ret;
  1338. /*
  1339. * Important to first disable the channel, because maybe someone
  1340. * used it before us, e.g., the bootloader
  1341. */
  1342. ipu_disable_channel(idmac, ichan, true);
  1343. ret = ipu_init_channel(idmac, ichan);
  1344. if (ret < 0)
  1345. goto eichan;
  1346. ret = request_irq(ichan->eof_irq, idmac_interrupt, 0,
  1347. ichan->eof_name, ichan);
  1348. if (ret < 0)
  1349. goto erirq;
  1350. #ifdef DEBUG
  1351. if (chan->chan_id == IDMAC_IC_7) {
  1352. ic_sof = ipu_irq_map(69);
  1353. if (ic_sof > 0)
  1354. request_irq(ic_sof, ic_sof_irq, 0, "IC SOF", ichan);
  1355. ic_eof = ipu_irq_map(70);
  1356. if (ic_eof > 0)
  1357. request_irq(ic_eof, ic_eof_irq, 0, "IC EOF", ichan);
  1358. }
  1359. #endif
  1360. ichan->status = IPU_CHANNEL_INITIALIZED;
  1361. dev_dbg(&chan->dev->device, "Found channel 0x%x, irq %d\n",
  1362. chan->chan_id, ichan->eof_irq);
  1363. return ret;
  1364. erirq:
  1365. ipu_uninit_channel(idmac, ichan);
  1366. eichan:
  1367. ipu_irq_unmap(chan->chan_id);
  1368. eimap:
  1369. return ret;
  1370. }
  1371. static void idmac_free_chan_resources(struct dma_chan *chan)
  1372. {
  1373. struct idmac_channel *ichan = to_idmac_chan(chan);
  1374. struct idmac *idmac = to_idmac(chan->device);
  1375. mutex_lock(&ichan->chan_mutex);
  1376. __idmac_terminate_all(chan);
  1377. if (ichan->status > IPU_CHANNEL_FREE) {
  1378. #ifdef DEBUG
  1379. if (chan->chan_id == IDMAC_IC_7) {
  1380. if (ic_sof > 0) {
  1381. free_irq(ic_sof, ichan);
  1382. ipu_irq_unmap(69);
  1383. ic_sof = -EINVAL;
  1384. }
  1385. if (ic_eof > 0) {
  1386. free_irq(ic_eof, ichan);
  1387. ipu_irq_unmap(70);
  1388. ic_eof = -EINVAL;
  1389. }
  1390. }
  1391. #endif
  1392. free_irq(ichan->eof_irq, ichan);
  1393. ipu_irq_unmap(chan->chan_id);
  1394. }
  1395. ichan->status = IPU_CHANNEL_FREE;
  1396. ipu_uninit_channel(idmac, ichan);
  1397. mutex_unlock(&ichan->chan_mutex);
  1398. tasklet_schedule(&to_ipu(idmac)->tasklet);
  1399. }
  1400. static enum dma_status idmac_is_tx_complete(struct dma_chan *chan,
  1401. dma_cookie_t cookie, dma_cookie_t *done, dma_cookie_t *used)
  1402. {
  1403. struct idmac_channel *ichan = to_idmac_chan(chan);
  1404. if (done)
  1405. *done = ichan->completed;
  1406. if (used)
  1407. *used = chan->cookie;
  1408. if (cookie != chan->cookie)
  1409. return DMA_ERROR;
  1410. return DMA_SUCCESS;
  1411. }
  1412. static int __init ipu_idmac_init(struct ipu *ipu)
  1413. {
  1414. struct idmac *idmac = &ipu->idmac;
  1415. struct dma_device *dma = &idmac->dma;
  1416. int i;
  1417. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  1418. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  1419. /* Compulsory common fields */
  1420. dma->dev = ipu->dev;
  1421. dma->device_alloc_chan_resources = idmac_alloc_chan_resources;
  1422. dma->device_free_chan_resources = idmac_free_chan_resources;
  1423. dma->device_is_tx_complete = idmac_is_tx_complete;
  1424. dma->device_issue_pending = idmac_issue_pending;
  1425. /* Compulsory for DMA_SLAVE fields */
  1426. dma->device_prep_slave_sg = idmac_prep_slave_sg;
  1427. dma->device_terminate_all = idmac_terminate_all;
  1428. INIT_LIST_HEAD(&dma->channels);
  1429. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1430. struct idmac_channel *ichan = ipu->channel + i;
  1431. struct dma_chan *dma_chan = &ichan->dma_chan;
  1432. spin_lock_init(&ichan->lock);
  1433. mutex_init(&ichan->chan_mutex);
  1434. ichan->status = IPU_CHANNEL_FREE;
  1435. ichan->sec_chan_en = false;
  1436. ichan->completed = -ENXIO;
  1437. snprintf(ichan->eof_name, sizeof(ichan->eof_name), "IDMAC EOF %d", i);
  1438. dma_chan->device = &idmac->dma;
  1439. dma_chan->cookie = 1;
  1440. dma_chan->chan_id = i;
  1441. list_add_tail(&dma_chan->device_node, &dma->channels);
  1442. }
  1443. idmac_write_icreg(ipu, 0x00000070, IDMAC_CONF);
  1444. return dma_async_device_register(&idmac->dma);
  1445. }
  1446. static void __exit ipu_idmac_exit(struct ipu *ipu)
  1447. {
  1448. int i;
  1449. struct idmac *idmac = &ipu->idmac;
  1450. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1451. struct idmac_channel *ichan = ipu->channel + i;
  1452. idmac_terminate_all(&ichan->dma_chan);
  1453. idmac_prep_slave_sg(&ichan->dma_chan, NULL, 0, DMA_NONE, 0);
  1454. }
  1455. dma_async_device_unregister(&idmac->dma);
  1456. }
  1457. /*****************************************************************************
  1458. * IPU common probe / remove
  1459. */
  1460. static int __init ipu_probe(struct platform_device *pdev)
  1461. {
  1462. struct ipu_platform_data *pdata = pdev->dev.platform_data;
  1463. struct resource *mem_ipu, *mem_ic;
  1464. int ret;
  1465. spin_lock_init(&ipu_data.lock);
  1466. mem_ipu = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1467. mem_ic = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1468. if (!pdata || !mem_ipu || !mem_ic)
  1469. return -EINVAL;
  1470. ipu_data.dev = &pdev->dev;
  1471. platform_set_drvdata(pdev, &ipu_data);
  1472. ret = platform_get_irq(pdev, 0);
  1473. if (ret < 0)
  1474. goto err_noirq;
  1475. ipu_data.irq_fn = ret;
  1476. ret = platform_get_irq(pdev, 1);
  1477. if (ret < 0)
  1478. goto err_noirq;
  1479. ipu_data.irq_err = ret;
  1480. ipu_data.irq_base = pdata->irq_base;
  1481. dev_dbg(&pdev->dev, "fn irq %u, err irq %u, irq-base %u\n",
  1482. ipu_data.irq_fn, ipu_data.irq_err, ipu_data.irq_base);
  1483. /* Remap IPU common registers */
  1484. ipu_data.reg_ipu = ioremap(mem_ipu->start,
  1485. mem_ipu->end - mem_ipu->start + 1);
  1486. if (!ipu_data.reg_ipu) {
  1487. ret = -ENOMEM;
  1488. goto err_ioremap_ipu;
  1489. }
  1490. /* Remap Image Converter and Image DMA Controller registers */
  1491. ipu_data.reg_ic = ioremap(mem_ic->start,
  1492. mem_ic->end - mem_ic->start + 1);
  1493. if (!ipu_data.reg_ic) {
  1494. ret = -ENOMEM;
  1495. goto err_ioremap_ic;
  1496. }
  1497. /* Get IPU clock */
  1498. ipu_data.ipu_clk = clk_get(&pdev->dev, NULL);
  1499. if (IS_ERR(ipu_data.ipu_clk)) {
  1500. ret = PTR_ERR(ipu_data.ipu_clk);
  1501. goto err_clk_get;
  1502. }
  1503. /* Make sure IPU HSP clock is running */
  1504. clk_enable(ipu_data.ipu_clk);
  1505. /* Disable all interrupts */
  1506. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_1);
  1507. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_2);
  1508. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_3);
  1509. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_4);
  1510. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_5);
  1511. dev_dbg(&pdev->dev, "%s @ 0x%08lx, fn irq %u, err irq %u\n", pdev->name,
  1512. (unsigned long)mem_ipu->start, ipu_data.irq_fn, ipu_data.irq_err);
  1513. ret = ipu_irq_attach_irq(&ipu_data, pdev);
  1514. if (ret < 0)
  1515. goto err_attach_irq;
  1516. /* Initialize DMA engine */
  1517. ret = ipu_idmac_init(&ipu_data);
  1518. if (ret < 0)
  1519. goto err_idmac_init;
  1520. tasklet_init(&ipu_data.tasklet, ipu_gc_tasklet, (unsigned long)&ipu_data);
  1521. ipu_data.dev = &pdev->dev;
  1522. dev_dbg(ipu_data.dev, "IPU initialized\n");
  1523. return 0;
  1524. err_idmac_init:
  1525. err_attach_irq:
  1526. ipu_irq_detach_irq(&ipu_data, pdev);
  1527. clk_disable(ipu_data.ipu_clk);
  1528. clk_put(ipu_data.ipu_clk);
  1529. err_clk_get:
  1530. iounmap(ipu_data.reg_ic);
  1531. err_ioremap_ic:
  1532. iounmap(ipu_data.reg_ipu);
  1533. err_ioremap_ipu:
  1534. err_noirq:
  1535. dev_err(&pdev->dev, "Failed to probe IPU: %d\n", ret);
  1536. return ret;
  1537. }
  1538. static int __exit ipu_remove(struct platform_device *pdev)
  1539. {
  1540. struct ipu *ipu = platform_get_drvdata(pdev);
  1541. ipu_idmac_exit(ipu);
  1542. ipu_irq_detach_irq(ipu, pdev);
  1543. clk_disable(ipu->ipu_clk);
  1544. clk_put(ipu->ipu_clk);
  1545. iounmap(ipu->reg_ic);
  1546. iounmap(ipu->reg_ipu);
  1547. tasklet_kill(&ipu->tasklet);
  1548. platform_set_drvdata(pdev, NULL);
  1549. return 0;
  1550. }
  1551. /*
  1552. * We need two MEM resources - with IPU-common and Image Converter registers,
  1553. * including PF_CONF and IDMAC_* registers, and two IRQs - function and error
  1554. */
  1555. static struct platform_driver ipu_platform_driver = {
  1556. .driver = {
  1557. .name = "ipu-core",
  1558. .owner = THIS_MODULE,
  1559. },
  1560. .remove = __exit_p(ipu_remove),
  1561. };
  1562. static int __init ipu_init(void)
  1563. {
  1564. return platform_driver_probe(&ipu_platform_driver, ipu_probe);
  1565. }
  1566. subsys_initcall(ipu_init);
  1567. MODULE_DESCRIPTION("IPU core driver");
  1568. MODULE_LICENSE("GPL v2");
  1569. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  1570. MODULE_ALIAS("platform:ipu-core");