ipu_idmac.c 47 KB

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