ipu_idmac.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  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. int i = 100;
  1087. /* This doesn't help. See comment in ipu_disable_channel() */
  1088. while (--i) {
  1089. curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
  1090. if (((curbuf >> chan_id) & 1) != ichan->active_buffer)
  1091. break;
  1092. cpu_relax();
  1093. }
  1094. if (!i) {
  1095. spin_unlock(&ichan->lock);
  1096. dev_dbg(dev,
  1097. "IRQ on active buffer on channel %x, active "
  1098. "%d, ready %x, %x, current %x!\n", chan_id,
  1099. ichan->active_buffer, ready0, ready1, curbuf);
  1100. return IRQ_NONE;
  1101. } else
  1102. dev_dbg(dev,
  1103. "Buffer deactivated on channel %x, active "
  1104. "%d, ready %x, %x, current %x, rest %d!\n", chan_id,
  1105. ichan->active_buffer, ready0, ready1, curbuf, i);
  1106. }
  1107. if (unlikely((ichan->active_buffer && (ready1 >> chan_id) & 1) ||
  1108. (!ichan->active_buffer && (ready0 >> chan_id) & 1)
  1109. )) {
  1110. spin_unlock(&ichan->lock);
  1111. dev_dbg(dev,
  1112. "IRQ with active buffer still ready on channel %x, "
  1113. "active %d, ready %x, %x!\n", chan_id,
  1114. ichan->active_buffer, ready0, ready1);
  1115. return IRQ_NONE;
  1116. }
  1117. if (unlikely(list_empty(&ichan->queue))) {
  1118. ichan->sg[ichan->active_buffer] = NULL;
  1119. spin_unlock(&ichan->lock);
  1120. dev_err(dev,
  1121. "IRQ without queued buffers on channel %x, active %d, "
  1122. "ready %x, %x!\n", chan_id,
  1123. ichan->active_buffer, ready0, ready1);
  1124. return IRQ_NONE;
  1125. }
  1126. /*
  1127. * active_buffer is a software flag, it shows which buffer we are
  1128. * currently expecting back from the hardware, IDMAC should be
  1129. * processing the other buffer already
  1130. */
  1131. sg = &ichan->sg[ichan->active_buffer];
  1132. sgnext = ichan->sg[!ichan->active_buffer];
  1133. if (!*sg) {
  1134. spin_unlock(&ichan->lock);
  1135. return IRQ_HANDLED;
  1136. }
  1137. desc = list_entry(ichan->queue.next, struct idmac_tx_desc, list);
  1138. descnew = desc;
  1139. dev_dbg(dev, "IDMAC irq %d, dma 0x%08x, next dma 0x%08x, current %d, curbuf 0x%08x\n",
  1140. irq, sg_dma_address(*sg), sgnext ? sg_dma_address(sgnext) : 0, ichan->active_buffer, curbuf);
  1141. /* Find the descriptor of sgnext */
  1142. sgnew = idmac_sg_next(ichan, &descnew, *sg);
  1143. if (sgnext != sgnew)
  1144. dev_err(dev, "Submitted buffer %p, next buffer %p\n", sgnext, sgnew);
  1145. /*
  1146. * if sgnext == NULL sg must be the last element in a scatterlist and
  1147. * queue must be empty
  1148. */
  1149. if (unlikely(!sgnext)) {
  1150. if (!WARN_ON(sg_next(*sg)))
  1151. dev_dbg(dev, "Underrun on channel %x\n", chan_id);
  1152. ichan->sg[!ichan->active_buffer] = sgnew;
  1153. if (unlikely(sgnew)) {
  1154. ipu_submit_buffer(ichan, descnew, sgnew, !ichan->active_buffer);
  1155. } else {
  1156. spin_lock_irqsave(&ipu_data.lock, flags);
  1157. ipu_ic_disable_task(&ipu_data, chan_id);
  1158. spin_unlock_irqrestore(&ipu_data.lock, flags);
  1159. ichan->status = IPU_CHANNEL_READY;
  1160. /* Continue to check for complete descriptor */
  1161. }
  1162. }
  1163. /* Calculate and submit the next sg element */
  1164. sgnew = idmac_sg_next(ichan, &descnew, sgnew);
  1165. if (unlikely(!sg_next(*sg)) || !sgnext) {
  1166. /*
  1167. * Last element in scatterlist done, remove from the queue,
  1168. * _init for debugging
  1169. */
  1170. list_del_init(&desc->list);
  1171. done = true;
  1172. }
  1173. *sg = sgnew;
  1174. if (likely(sgnew) &&
  1175. ipu_submit_buffer(ichan, descnew, sgnew, ichan->active_buffer) < 0) {
  1176. callback = desc->txd.callback;
  1177. callback_param = desc->txd.callback_param;
  1178. spin_unlock(&ichan->lock);
  1179. callback(callback_param);
  1180. spin_lock(&ichan->lock);
  1181. }
  1182. /* Flip the active buffer - even if update above failed */
  1183. ichan->active_buffer = !ichan->active_buffer;
  1184. if (done)
  1185. ichan->completed = desc->txd.cookie;
  1186. callback = desc->txd.callback;
  1187. callback_param = desc->txd.callback_param;
  1188. spin_unlock(&ichan->lock);
  1189. if (done && (desc->txd.flags & DMA_PREP_INTERRUPT) && callback)
  1190. callback(callback_param);
  1191. return IRQ_HANDLED;
  1192. }
  1193. static void ipu_gc_tasklet(unsigned long arg)
  1194. {
  1195. struct ipu *ipu = (struct ipu *)arg;
  1196. int i;
  1197. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1198. struct idmac_channel *ichan = ipu->channel + i;
  1199. struct idmac_tx_desc *desc;
  1200. unsigned long flags;
  1201. struct scatterlist *sg;
  1202. int j, k;
  1203. for (j = 0; j < ichan->n_tx_desc; j++) {
  1204. desc = ichan->desc + j;
  1205. spin_lock_irqsave(&ichan->lock, flags);
  1206. if (async_tx_test_ack(&desc->txd)) {
  1207. list_move(&desc->list, &ichan->free_list);
  1208. for_each_sg(desc->sg, sg, desc->sg_len, k) {
  1209. if (ichan->sg[0] == sg)
  1210. ichan->sg[0] = NULL;
  1211. else if (ichan->sg[1] == sg)
  1212. ichan->sg[1] = NULL;
  1213. }
  1214. async_tx_clear_ack(&desc->txd);
  1215. }
  1216. spin_unlock_irqrestore(&ichan->lock, flags);
  1217. }
  1218. }
  1219. }
  1220. /* Allocate and initialise a transfer descriptor. */
  1221. static struct dma_async_tx_descriptor *idmac_prep_slave_sg(struct dma_chan *chan,
  1222. struct scatterlist *sgl, unsigned int sg_len,
  1223. enum dma_data_direction direction, unsigned long tx_flags)
  1224. {
  1225. struct idmac_channel *ichan = to_idmac_chan(chan);
  1226. struct idmac_tx_desc *desc = NULL;
  1227. struct dma_async_tx_descriptor *txd = NULL;
  1228. unsigned long flags;
  1229. /* We only can handle these three channels so far */
  1230. if (chan->chan_id != IDMAC_SDC_0 && chan->chan_id != IDMAC_SDC_1 &&
  1231. chan->chan_id != IDMAC_IC_7)
  1232. return NULL;
  1233. if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE) {
  1234. dev_err(chan->device->dev, "Invalid DMA direction %d!\n", direction);
  1235. return NULL;
  1236. }
  1237. mutex_lock(&ichan->chan_mutex);
  1238. spin_lock_irqsave(&ichan->lock, flags);
  1239. if (!list_empty(&ichan->free_list)) {
  1240. desc = list_entry(ichan->free_list.next,
  1241. struct idmac_tx_desc, list);
  1242. list_del_init(&desc->list);
  1243. desc->sg_len = sg_len;
  1244. desc->sg = sgl;
  1245. txd = &desc->txd;
  1246. txd->flags = tx_flags;
  1247. }
  1248. spin_unlock_irqrestore(&ichan->lock, flags);
  1249. mutex_unlock(&ichan->chan_mutex);
  1250. tasklet_schedule(&to_ipu(to_idmac(chan->device))->tasklet);
  1251. return txd;
  1252. }
  1253. /* Re-select the current buffer and re-activate the channel */
  1254. static void idmac_issue_pending(struct dma_chan *chan)
  1255. {
  1256. struct idmac_channel *ichan = to_idmac_chan(chan);
  1257. struct idmac *idmac = to_idmac(chan->device);
  1258. struct ipu *ipu = to_ipu(idmac);
  1259. unsigned long flags;
  1260. /* This is not always needed, but doesn't hurt either */
  1261. spin_lock_irqsave(&ipu->lock, flags);
  1262. ipu_select_buffer(chan->chan_id, ichan->active_buffer);
  1263. spin_unlock_irqrestore(&ipu->lock, flags);
  1264. /*
  1265. * Might need to perform some parts of initialisation from
  1266. * ipu_enable_channel(), but not all, we do not want to reset to buffer
  1267. * 0, don't need to set priority again either, but re-enabling the task
  1268. * and the channel might be a good idea.
  1269. */
  1270. }
  1271. static void __idmac_terminate_all(struct dma_chan *chan)
  1272. {
  1273. struct idmac_channel *ichan = to_idmac_chan(chan);
  1274. struct idmac *idmac = to_idmac(chan->device);
  1275. unsigned long flags;
  1276. int i;
  1277. ipu_disable_channel(idmac, ichan,
  1278. ichan->status >= IPU_CHANNEL_ENABLED);
  1279. tasklet_disable(&to_ipu(idmac)->tasklet);
  1280. /* ichan->queue is modified in ISR, have to spinlock */
  1281. spin_lock_irqsave(&ichan->lock, flags);
  1282. list_splice_init(&ichan->queue, &ichan->free_list);
  1283. if (ichan->desc)
  1284. for (i = 0; i < ichan->n_tx_desc; i++) {
  1285. struct idmac_tx_desc *desc = ichan->desc + i;
  1286. if (list_empty(&desc->list))
  1287. /* Descriptor was prepared, but not submitted */
  1288. list_add(&desc->list, &ichan->free_list);
  1289. async_tx_clear_ack(&desc->txd);
  1290. }
  1291. ichan->sg[0] = NULL;
  1292. ichan->sg[1] = NULL;
  1293. spin_unlock_irqrestore(&ichan->lock, flags);
  1294. tasklet_enable(&to_ipu(idmac)->tasklet);
  1295. ichan->status = IPU_CHANNEL_INITIALIZED;
  1296. }
  1297. static void idmac_terminate_all(struct dma_chan *chan)
  1298. {
  1299. struct idmac_channel *ichan = to_idmac_chan(chan);
  1300. mutex_lock(&ichan->chan_mutex);
  1301. __idmac_terminate_all(chan);
  1302. mutex_unlock(&ichan->chan_mutex);
  1303. }
  1304. #ifdef DEBUG
  1305. static irqreturn_t ic_sof_irq(int irq, void *dev_id)
  1306. {
  1307. struct idmac_channel *ichan = dev_id;
  1308. printk(KERN_DEBUG "Got SOF IRQ %d on Channel %d\n",
  1309. irq, ichan->dma_chan.chan_id);
  1310. disable_irq(irq);
  1311. return IRQ_HANDLED;
  1312. }
  1313. static irqreturn_t ic_eof_irq(int irq, void *dev_id)
  1314. {
  1315. struct idmac_channel *ichan = dev_id;
  1316. printk(KERN_DEBUG "Got EOF IRQ %d on Channel %d\n",
  1317. irq, ichan->dma_chan.chan_id);
  1318. disable_irq(irq);
  1319. return IRQ_HANDLED;
  1320. }
  1321. static int ic_sof = -EINVAL, ic_eof = -EINVAL;
  1322. #endif
  1323. static int idmac_alloc_chan_resources(struct dma_chan *chan)
  1324. {
  1325. struct idmac_channel *ichan = to_idmac_chan(chan);
  1326. struct idmac *idmac = to_idmac(chan->device);
  1327. int ret;
  1328. /* dmaengine.c now guarantees to only offer free channels */
  1329. BUG_ON(chan->client_count > 1);
  1330. WARN_ON(ichan->status != IPU_CHANNEL_FREE);
  1331. chan->cookie = 1;
  1332. ichan->completed = -ENXIO;
  1333. ret = ipu_irq_map(chan->chan_id);
  1334. if (ret < 0)
  1335. goto eimap;
  1336. ichan->eof_irq = ret;
  1337. /*
  1338. * Important to first disable the channel, because maybe someone
  1339. * used it before us, e.g., the bootloader
  1340. */
  1341. ipu_disable_channel(idmac, ichan, true);
  1342. ret = ipu_init_channel(idmac, ichan);
  1343. if (ret < 0)
  1344. goto eichan;
  1345. ret = request_irq(ichan->eof_irq, idmac_interrupt, 0,
  1346. ichan->eof_name, ichan);
  1347. if (ret < 0)
  1348. goto erirq;
  1349. #ifdef DEBUG
  1350. if (chan->chan_id == IDMAC_IC_7) {
  1351. ic_sof = ipu_irq_map(69);
  1352. if (ic_sof > 0)
  1353. request_irq(ic_sof, ic_sof_irq, 0, "IC SOF", ichan);
  1354. ic_eof = ipu_irq_map(70);
  1355. if (ic_eof > 0)
  1356. request_irq(ic_eof, ic_eof_irq, 0, "IC EOF", ichan);
  1357. }
  1358. #endif
  1359. ichan->status = IPU_CHANNEL_INITIALIZED;
  1360. dev_dbg(&chan->dev->device, "Found channel 0x%x, irq %d\n",
  1361. chan->chan_id, ichan->eof_irq);
  1362. return ret;
  1363. erirq:
  1364. ipu_uninit_channel(idmac, ichan);
  1365. eichan:
  1366. ipu_irq_unmap(chan->chan_id);
  1367. eimap:
  1368. return ret;
  1369. }
  1370. static void idmac_free_chan_resources(struct dma_chan *chan)
  1371. {
  1372. struct idmac_channel *ichan = to_idmac_chan(chan);
  1373. struct idmac *idmac = to_idmac(chan->device);
  1374. mutex_lock(&ichan->chan_mutex);
  1375. __idmac_terminate_all(chan);
  1376. if (ichan->status > IPU_CHANNEL_FREE) {
  1377. #ifdef DEBUG
  1378. if (chan->chan_id == IDMAC_IC_7) {
  1379. if (ic_sof > 0) {
  1380. free_irq(ic_sof, ichan);
  1381. ipu_irq_unmap(69);
  1382. ic_sof = -EINVAL;
  1383. }
  1384. if (ic_eof > 0) {
  1385. free_irq(ic_eof, ichan);
  1386. ipu_irq_unmap(70);
  1387. ic_eof = -EINVAL;
  1388. }
  1389. }
  1390. #endif
  1391. free_irq(ichan->eof_irq, ichan);
  1392. ipu_irq_unmap(chan->chan_id);
  1393. }
  1394. ichan->status = IPU_CHANNEL_FREE;
  1395. ipu_uninit_channel(idmac, ichan);
  1396. mutex_unlock(&ichan->chan_mutex);
  1397. tasklet_schedule(&to_ipu(idmac)->tasklet);
  1398. }
  1399. static enum dma_status idmac_is_tx_complete(struct dma_chan *chan,
  1400. dma_cookie_t cookie, dma_cookie_t *done, dma_cookie_t *used)
  1401. {
  1402. struct idmac_channel *ichan = to_idmac_chan(chan);
  1403. if (done)
  1404. *done = ichan->completed;
  1405. if (used)
  1406. *used = chan->cookie;
  1407. if (cookie != chan->cookie)
  1408. return DMA_ERROR;
  1409. return DMA_SUCCESS;
  1410. }
  1411. static int __init ipu_idmac_init(struct ipu *ipu)
  1412. {
  1413. struct idmac *idmac = &ipu->idmac;
  1414. struct dma_device *dma = &idmac->dma;
  1415. int i;
  1416. dma_cap_set(DMA_SLAVE, dma->cap_mask);
  1417. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  1418. /* Compulsory common fields */
  1419. dma->dev = ipu->dev;
  1420. dma->device_alloc_chan_resources = idmac_alloc_chan_resources;
  1421. dma->device_free_chan_resources = idmac_free_chan_resources;
  1422. dma->device_is_tx_complete = idmac_is_tx_complete;
  1423. dma->device_issue_pending = idmac_issue_pending;
  1424. /* Compulsory for DMA_SLAVE fields */
  1425. dma->device_prep_slave_sg = idmac_prep_slave_sg;
  1426. dma->device_terminate_all = idmac_terminate_all;
  1427. INIT_LIST_HEAD(&dma->channels);
  1428. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1429. struct idmac_channel *ichan = ipu->channel + i;
  1430. struct dma_chan *dma_chan = &ichan->dma_chan;
  1431. spin_lock_init(&ichan->lock);
  1432. mutex_init(&ichan->chan_mutex);
  1433. ichan->status = IPU_CHANNEL_FREE;
  1434. ichan->sec_chan_en = false;
  1435. ichan->completed = -ENXIO;
  1436. snprintf(ichan->eof_name, sizeof(ichan->eof_name), "IDMAC EOF %d", i);
  1437. dma_chan->device = &idmac->dma;
  1438. dma_chan->cookie = 1;
  1439. dma_chan->chan_id = i;
  1440. list_add_tail(&dma_chan->device_node, &dma->channels);
  1441. }
  1442. idmac_write_icreg(ipu, 0x00000070, IDMAC_CONF);
  1443. return dma_async_device_register(&idmac->dma);
  1444. }
  1445. static void __exit ipu_idmac_exit(struct ipu *ipu)
  1446. {
  1447. int i;
  1448. struct idmac *idmac = &ipu->idmac;
  1449. for (i = 0; i < IPU_CHANNELS_NUM; i++) {
  1450. struct idmac_channel *ichan = ipu->channel + i;
  1451. idmac_terminate_all(&ichan->dma_chan);
  1452. idmac_prep_slave_sg(&ichan->dma_chan, NULL, 0, DMA_NONE, 0);
  1453. }
  1454. dma_async_device_unregister(&idmac->dma);
  1455. }
  1456. /*****************************************************************************
  1457. * IPU common probe / remove
  1458. */
  1459. static int __init ipu_probe(struct platform_device *pdev)
  1460. {
  1461. struct ipu_platform_data *pdata = pdev->dev.platform_data;
  1462. struct resource *mem_ipu, *mem_ic;
  1463. int ret;
  1464. spin_lock_init(&ipu_data.lock);
  1465. mem_ipu = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1466. mem_ic = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1467. if (!pdata || !mem_ipu || !mem_ic)
  1468. return -EINVAL;
  1469. ipu_data.dev = &pdev->dev;
  1470. platform_set_drvdata(pdev, &ipu_data);
  1471. ret = platform_get_irq(pdev, 0);
  1472. if (ret < 0)
  1473. goto err_noirq;
  1474. ipu_data.irq_fn = ret;
  1475. ret = platform_get_irq(pdev, 1);
  1476. if (ret < 0)
  1477. goto err_noirq;
  1478. ipu_data.irq_err = ret;
  1479. ipu_data.irq_base = pdata->irq_base;
  1480. dev_dbg(&pdev->dev, "fn irq %u, err irq %u, irq-base %u\n",
  1481. ipu_data.irq_fn, ipu_data.irq_err, ipu_data.irq_base);
  1482. /* Remap IPU common registers */
  1483. ipu_data.reg_ipu = ioremap(mem_ipu->start,
  1484. mem_ipu->end - mem_ipu->start + 1);
  1485. if (!ipu_data.reg_ipu) {
  1486. ret = -ENOMEM;
  1487. goto err_ioremap_ipu;
  1488. }
  1489. /* Remap Image Converter and Image DMA Controller registers */
  1490. ipu_data.reg_ic = ioremap(mem_ic->start,
  1491. mem_ic->end - mem_ic->start + 1);
  1492. if (!ipu_data.reg_ic) {
  1493. ret = -ENOMEM;
  1494. goto err_ioremap_ic;
  1495. }
  1496. /* Get IPU clock */
  1497. ipu_data.ipu_clk = clk_get(&pdev->dev, NULL);
  1498. if (IS_ERR(ipu_data.ipu_clk)) {
  1499. ret = PTR_ERR(ipu_data.ipu_clk);
  1500. goto err_clk_get;
  1501. }
  1502. /* Make sure IPU HSP clock is running */
  1503. clk_enable(ipu_data.ipu_clk);
  1504. /* Disable all interrupts */
  1505. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_1);
  1506. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_2);
  1507. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_3);
  1508. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_4);
  1509. idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_5);
  1510. dev_dbg(&pdev->dev, "%s @ 0x%08lx, fn irq %u, err irq %u\n", pdev->name,
  1511. (unsigned long)mem_ipu->start, ipu_data.irq_fn, ipu_data.irq_err);
  1512. ret = ipu_irq_attach_irq(&ipu_data, pdev);
  1513. if (ret < 0)
  1514. goto err_attach_irq;
  1515. /* Initialize DMA engine */
  1516. ret = ipu_idmac_init(&ipu_data);
  1517. if (ret < 0)
  1518. goto err_idmac_init;
  1519. tasklet_init(&ipu_data.tasklet, ipu_gc_tasklet, (unsigned long)&ipu_data);
  1520. ipu_data.dev = &pdev->dev;
  1521. dev_dbg(ipu_data.dev, "IPU initialized\n");
  1522. return 0;
  1523. err_idmac_init:
  1524. err_attach_irq:
  1525. ipu_irq_detach_irq(&ipu_data, pdev);
  1526. clk_disable(ipu_data.ipu_clk);
  1527. clk_put(ipu_data.ipu_clk);
  1528. err_clk_get:
  1529. iounmap(ipu_data.reg_ic);
  1530. err_ioremap_ic:
  1531. iounmap(ipu_data.reg_ipu);
  1532. err_ioremap_ipu:
  1533. err_noirq:
  1534. dev_err(&pdev->dev, "Failed to probe IPU: %d\n", ret);
  1535. return ret;
  1536. }
  1537. static int __exit ipu_remove(struct platform_device *pdev)
  1538. {
  1539. struct ipu *ipu = platform_get_drvdata(pdev);
  1540. ipu_idmac_exit(ipu);
  1541. ipu_irq_detach_irq(ipu, pdev);
  1542. clk_disable(ipu->ipu_clk);
  1543. clk_put(ipu->ipu_clk);
  1544. iounmap(ipu->reg_ic);
  1545. iounmap(ipu->reg_ipu);
  1546. tasklet_kill(&ipu->tasklet);
  1547. platform_set_drvdata(pdev, NULL);
  1548. return 0;
  1549. }
  1550. /*
  1551. * We need two MEM resources - with IPU-common and Image Converter registers,
  1552. * including PF_CONF and IDMAC_* registers, and two IRQs - function and error
  1553. */
  1554. static struct platform_driver ipu_platform_driver = {
  1555. .driver = {
  1556. .name = "ipu-core",
  1557. .owner = THIS_MODULE,
  1558. },
  1559. .remove = __exit_p(ipu_remove),
  1560. };
  1561. static int __init ipu_init(void)
  1562. {
  1563. return platform_driver_probe(&ipu_platform_driver, ipu_probe);
  1564. }
  1565. subsys_initcall(ipu_init);
  1566. MODULE_DESCRIPTION("IPU core driver");
  1567. MODULE_LICENSE("GPL v2");
  1568. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  1569. MODULE_ALIAS("platform:ipu-core");