dispc.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /*
  2. * OMAP2 display controller support
  3. *
  4. * Copyright (C) 2005 Nokia Corporation
  5. * Author: Imre Deak <imre.deak@nokia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/mm.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/clk.h>
  26. #include <linux/io.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <plat/sram.h>
  30. #include <plat/board.h>
  31. #include "omapfb.h"
  32. #include "dispc.h"
  33. #define MODULE_NAME "dispc"
  34. #define DSS_BASE 0x48050000
  35. #define DSS_SYSCONFIG 0x0010
  36. #define DISPC_BASE 0x48050400
  37. /* DISPC common */
  38. #define DISPC_REVISION 0x0000
  39. #define DISPC_SYSCONFIG 0x0010
  40. #define DISPC_SYSSTATUS 0x0014
  41. #define DISPC_IRQSTATUS 0x0018
  42. #define DISPC_IRQENABLE 0x001C
  43. #define DISPC_CONTROL 0x0040
  44. #define DISPC_CONFIG 0x0044
  45. #define DISPC_CAPABLE 0x0048
  46. #define DISPC_DEFAULT_COLOR0 0x004C
  47. #define DISPC_DEFAULT_COLOR1 0x0050
  48. #define DISPC_TRANS_COLOR0 0x0054
  49. #define DISPC_TRANS_COLOR1 0x0058
  50. #define DISPC_LINE_STATUS 0x005C
  51. #define DISPC_LINE_NUMBER 0x0060
  52. #define DISPC_TIMING_H 0x0064
  53. #define DISPC_TIMING_V 0x0068
  54. #define DISPC_POL_FREQ 0x006C
  55. #define DISPC_DIVISOR 0x0070
  56. #define DISPC_SIZE_DIG 0x0078
  57. #define DISPC_SIZE_LCD 0x007C
  58. #define DISPC_DATA_CYCLE1 0x01D4
  59. #define DISPC_DATA_CYCLE2 0x01D8
  60. #define DISPC_DATA_CYCLE3 0x01DC
  61. /* DISPC GFX plane */
  62. #define DISPC_GFX_BA0 0x0080
  63. #define DISPC_GFX_BA1 0x0084
  64. #define DISPC_GFX_POSITION 0x0088
  65. #define DISPC_GFX_SIZE 0x008C
  66. #define DISPC_GFX_ATTRIBUTES 0x00A0
  67. #define DISPC_GFX_FIFO_THRESHOLD 0x00A4
  68. #define DISPC_GFX_FIFO_SIZE_STATUS 0x00A8
  69. #define DISPC_GFX_ROW_INC 0x00AC
  70. #define DISPC_GFX_PIXEL_INC 0x00B0
  71. #define DISPC_GFX_WINDOW_SKIP 0x00B4
  72. #define DISPC_GFX_TABLE_BA 0x00B8
  73. /* DISPC Video plane 1/2 */
  74. #define DISPC_VID1_BASE 0x00BC
  75. #define DISPC_VID2_BASE 0x014C
  76. /* Offsets into DISPC_VID1/2_BASE */
  77. #define DISPC_VID_BA0 0x0000
  78. #define DISPC_VID_BA1 0x0004
  79. #define DISPC_VID_POSITION 0x0008
  80. #define DISPC_VID_SIZE 0x000C
  81. #define DISPC_VID_ATTRIBUTES 0x0010
  82. #define DISPC_VID_FIFO_THRESHOLD 0x0014
  83. #define DISPC_VID_FIFO_SIZE_STATUS 0x0018
  84. #define DISPC_VID_ROW_INC 0x001C
  85. #define DISPC_VID_PIXEL_INC 0x0020
  86. #define DISPC_VID_FIR 0x0024
  87. #define DISPC_VID_PICTURE_SIZE 0x0028
  88. #define DISPC_VID_ACCU0 0x002C
  89. #define DISPC_VID_ACCU1 0x0030
  90. /* 8 elements in 8 byte increments */
  91. #define DISPC_VID_FIR_COEF_H0 0x0034
  92. /* 8 elements in 8 byte increments */
  93. #define DISPC_VID_FIR_COEF_HV0 0x0038
  94. /* 5 elements in 4 byte increments */
  95. #define DISPC_VID_CONV_COEF0 0x0074
  96. #define DISPC_IRQ_FRAMEMASK 0x0001
  97. #define DISPC_IRQ_VSYNC 0x0002
  98. #define DISPC_IRQ_EVSYNC_EVEN 0x0004
  99. #define DISPC_IRQ_EVSYNC_ODD 0x0008
  100. #define DISPC_IRQ_ACBIAS_COUNT_STAT 0x0010
  101. #define DISPC_IRQ_PROG_LINE_NUM 0x0020
  102. #define DISPC_IRQ_GFX_FIFO_UNDERFLOW 0x0040
  103. #define DISPC_IRQ_GFX_END_WIN 0x0080
  104. #define DISPC_IRQ_PAL_GAMMA_MASK 0x0100
  105. #define DISPC_IRQ_OCP_ERR 0x0200
  106. #define DISPC_IRQ_VID1_FIFO_UNDERFLOW 0x0400
  107. #define DISPC_IRQ_VID1_END_WIN 0x0800
  108. #define DISPC_IRQ_VID2_FIFO_UNDERFLOW 0x1000
  109. #define DISPC_IRQ_VID2_END_WIN 0x2000
  110. #define DISPC_IRQ_SYNC_LOST 0x4000
  111. #define DISPC_IRQ_MASK_ALL 0x7fff
  112. #define DISPC_IRQ_MASK_ERROR (DISPC_IRQ_GFX_FIFO_UNDERFLOW | \
  113. DISPC_IRQ_VID1_FIFO_UNDERFLOW | \
  114. DISPC_IRQ_VID2_FIFO_UNDERFLOW | \
  115. DISPC_IRQ_SYNC_LOST)
  116. #define RFBI_CONTROL 0x48050040
  117. #define MAX_PALETTE_SIZE (256 * 16)
  118. #define FLD_MASK(pos, len) (((1 << len) - 1) << pos)
  119. #define MOD_REG_FLD(reg, mask, val) \
  120. dispc_write_reg((reg), (dispc_read_reg(reg) & ~(mask)) | (val));
  121. #define OMAP2_SRAM_START 0x40200000
  122. /* Maximum size, in reality this is smaller if SRAM is partially locked. */
  123. #define OMAP2_SRAM_SIZE 0xa0000 /* 640k */
  124. /* We support the SDRAM / SRAM types. See OMAPFB_PLANE_MEMTYPE_* in omapfb.h */
  125. #define DISPC_MEMTYPE_NUM 2
  126. #define RESMAP_SIZE(_page_cnt) \
  127. ((_page_cnt + (sizeof(unsigned long) * 8) - 1) / 8)
  128. #define RESMAP_PTR(_res_map, _page_nr) \
  129. (((_res_map)->map) + (_page_nr) / (sizeof(unsigned long) * 8))
  130. #define RESMAP_MASK(_page_nr) \
  131. (1 << ((_page_nr) & (sizeof(unsigned long) * 8 - 1)))
  132. struct resmap {
  133. unsigned long start;
  134. unsigned page_cnt;
  135. unsigned long *map;
  136. };
  137. #define MAX_IRQ_HANDLERS 4
  138. static struct {
  139. void __iomem *base;
  140. struct omapfb_mem_desc mem_desc;
  141. struct resmap *res_map[DISPC_MEMTYPE_NUM];
  142. atomic_t map_count[OMAPFB_PLANE_NUM];
  143. dma_addr_t palette_paddr;
  144. void *palette_vaddr;
  145. int ext_mode;
  146. struct {
  147. u32 irq_mask;
  148. void (*callback)(void *);
  149. void *data;
  150. } irq_handlers[MAX_IRQ_HANDLERS];
  151. struct completion frame_done;
  152. int fir_hinc[OMAPFB_PLANE_NUM];
  153. int fir_vinc[OMAPFB_PLANE_NUM];
  154. struct clk *dss_ick, *dss1_fck;
  155. struct clk *dss_54m_fck;
  156. enum omapfb_update_mode update_mode;
  157. struct omapfb_device *fbdev;
  158. struct omapfb_color_key color_key;
  159. } dispc;
  160. static void enable_lcd_clocks(int enable);
  161. static void inline dispc_write_reg(int idx, u32 val)
  162. {
  163. __raw_writel(val, dispc.base + idx);
  164. }
  165. static u32 inline dispc_read_reg(int idx)
  166. {
  167. u32 l = __raw_readl(dispc.base + idx);
  168. return l;
  169. }
  170. /* Select RFBI or bypass mode */
  171. static void enable_rfbi_mode(int enable)
  172. {
  173. void __iomem *rfbi_control;
  174. u32 l;
  175. l = dispc_read_reg(DISPC_CONTROL);
  176. /* Enable RFBI, GPIO0/1 */
  177. l &= ~((1 << 11) | (1 << 15) | (1 << 16));
  178. l |= enable ? (1 << 11) : 0;
  179. /* RFBI En: GPIO0/1=10 RFBI Dis: GPIO0/1=11 */
  180. l |= 1 << 15;
  181. l |= enable ? 0 : (1 << 16);
  182. dispc_write_reg(DISPC_CONTROL, l);
  183. /* Set bypass mode in RFBI module */
  184. rfbi_control = ioremap(RFBI_CONTROL, SZ_1K);
  185. if (!rfbi_control) {
  186. pr_err("Unable to ioremap rfbi_control\n");
  187. return;
  188. }
  189. l = __raw_readl(rfbi_control);
  190. l |= enable ? 0 : (1 << 1);
  191. __raw_writel(l, rfbi_control);
  192. iounmap(rfbi_control);
  193. }
  194. static void set_lcd_data_lines(int data_lines)
  195. {
  196. u32 l;
  197. int code = 0;
  198. switch (data_lines) {
  199. case 12:
  200. code = 0;
  201. break;
  202. case 16:
  203. code = 1;
  204. break;
  205. case 18:
  206. code = 2;
  207. break;
  208. case 24:
  209. code = 3;
  210. break;
  211. default:
  212. BUG();
  213. }
  214. l = dispc_read_reg(DISPC_CONTROL);
  215. l &= ~(0x03 << 8);
  216. l |= code << 8;
  217. dispc_write_reg(DISPC_CONTROL, l);
  218. }
  219. static void set_load_mode(int mode)
  220. {
  221. BUG_ON(mode & ~(DISPC_LOAD_CLUT_ONLY | DISPC_LOAD_FRAME_ONLY |
  222. DISPC_LOAD_CLUT_ONCE_FRAME));
  223. MOD_REG_FLD(DISPC_CONFIG, 0x03 << 1, mode << 1);
  224. }
  225. void omap_dispc_set_lcd_size(int x, int y)
  226. {
  227. BUG_ON((x > (1 << 11)) || (y > (1 << 11)));
  228. enable_lcd_clocks(1);
  229. MOD_REG_FLD(DISPC_SIZE_LCD, FLD_MASK(16, 11) | FLD_MASK(0, 11),
  230. ((y - 1) << 16) | (x - 1));
  231. enable_lcd_clocks(0);
  232. }
  233. EXPORT_SYMBOL(omap_dispc_set_lcd_size);
  234. void omap_dispc_set_digit_size(int x, int y)
  235. {
  236. BUG_ON((x > (1 << 11)) || (y > (1 << 11)));
  237. enable_lcd_clocks(1);
  238. MOD_REG_FLD(DISPC_SIZE_DIG, FLD_MASK(16, 11) | FLD_MASK(0, 11),
  239. ((y - 1) << 16) | (x - 1));
  240. enable_lcd_clocks(0);
  241. }
  242. EXPORT_SYMBOL(omap_dispc_set_digit_size);
  243. static void setup_plane_fifo(int plane, int ext_mode)
  244. {
  245. const u32 ftrs_reg[] = { DISPC_GFX_FIFO_THRESHOLD,
  246. DISPC_VID1_BASE + DISPC_VID_FIFO_THRESHOLD,
  247. DISPC_VID2_BASE + DISPC_VID_FIFO_THRESHOLD };
  248. const u32 fsz_reg[] = { DISPC_GFX_FIFO_SIZE_STATUS,
  249. DISPC_VID1_BASE + DISPC_VID_FIFO_SIZE_STATUS,
  250. DISPC_VID2_BASE + DISPC_VID_FIFO_SIZE_STATUS };
  251. int low, high;
  252. u32 l;
  253. BUG_ON(plane > 2);
  254. l = dispc_read_reg(fsz_reg[plane]);
  255. l &= FLD_MASK(0, 11);
  256. if (ext_mode) {
  257. low = l * 3 / 4;
  258. high = l;
  259. } else {
  260. low = l / 4;
  261. high = l * 3 / 4;
  262. }
  263. MOD_REG_FLD(ftrs_reg[plane], FLD_MASK(16, 12) | FLD_MASK(0, 12),
  264. (high << 16) | low);
  265. }
  266. void omap_dispc_enable_lcd_out(int enable)
  267. {
  268. enable_lcd_clocks(1);
  269. MOD_REG_FLD(DISPC_CONTROL, 1, enable ? 1 : 0);
  270. enable_lcd_clocks(0);
  271. }
  272. EXPORT_SYMBOL(omap_dispc_enable_lcd_out);
  273. void omap_dispc_enable_digit_out(int enable)
  274. {
  275. enable_lcd_clocks(1);
  276. MOD_REG_FLD(DISPC_CONTROL, 1 << 1, enable ? 1 << 1 : 0);
  277. enable_lcd_clocks(0);
  278. }
  279. EXPORT_SYMBOL(omap_dispc_enable_digit_out);
  280. static inline int _setup_plane(int plane, int channel_out,
  281. u32 paddr, int screen_width,
  282. int pos_x, int pos_y, int width, int height,
  283. int color_mode)
  284. {
  285. const u32 at_reg[] = { DISPC_GFX_ATTRIBUTES,
  286. DISPC_VID1_BASE + DISPC_VID_ATTRIBUTES,
  287. DISPC_VID2_BASE + DISPC_VID_ATTRIBUTES };
  288. const u32 ba_reg[] = { DISPC_GFX_BA0, DISPC_VID1_BASE + DISPC_VID_BA0,
  289. DISPC_VID2_BASE + DISPC_VID_BA0 };
  290. const u32 ps_reg[] = { DISPC_GFX_POSITION,
  291. DISPC_VID1_BASE + DISPC_VID_POSITION,
  292. DISPC_VID2_BASE + DISPC_VID_POSITION };
  293. const u32 sz_reg[] = { DISPC_GFX_SIZE,
  294. DISPC_VID1_BASE + DISPC_VID_PICTURE_SIZE,
  295. DISPC_VID2_BASE + DISPC_VID_PICTURE_SIZE };
  296. const u32 ri_reg[] = { DISPC_GFX_ROW_INC,
  297. DISPC_VID1_BASE + DISPC_VID_ROW_INC,
  298. DISPC_VID2_BASE + DISPC_VID_ROW_INC };
  299. const u32 vs_reg[] = { 0, DISPC_VID1_BASE + DISPC_VID_SIZE,
  300. DISPC_VID2_BASE + DISPC_VID_SIZE };
  301. int chout_shift, burst_shift;
  302. int chout_val;
  303. int color_code;
  304. int bpp;
  305. int cconv_en;
  306. int set_vsize;
  307. u32 l;
  308. #ifdef VERBOSE
  309. dev_dbg(dispc.fbdev->dev, "plane %d channel %d paddr %#08x scr_width %d"
  310. " pos_x %d pos_y %d width %d height %d color_mode %d\n",
  311. plane, channel_out, paddr, screen_width, pos_x, pos_y,
  312. width, height, color_mode);
  313. #endif
  314. set_vsize = 0;
  315. switch (plane) {
  316. case OMAPFB_PLANE_GFX:
  317. burst_shift = 6;
  318. chout_shift = 8;
  319. break;
  320. case OMAPFB_PLANE_VID1:
  321. case OMAPFB_PLANE_VID2:
  322. burst_shift = 14;
  323. chout_shift = 16;
  324. set_vsize = 1;
  325. break;
  326. default:
  327. return -EINVAL;
  328. }
  329. switch (channel_out) {
  330. case OMAPFB_CHANNEL_OUT_LCD:
  331. chout_val = 0;
  332. break;
  333. case OMAPFB_CHANNEL_OUT_DIGIT:
  334. chout_val = 1;
  335. break;
  336. default:
  337. return -EINVAL;
  338. }
  339. cconv_en = 0;
  340. switch (color_mode) {
  341. case OMAPFB_COLOR_RGB565:
  342. color_code = DISPC_RGB_16_BPP;
  343. bpp = 16;
  344. break;
  345. case OMAPFB_COLOR_YUV422:
  346. if (plane == 0)
  347. return -EINVAL;
  348. color_code = DISPC_UYVY_422;
  349. cconv_en = 1;
  350. bpp = 16;
  351. break;
  352. case OMAPFB_COLOR_YUY422:
  353. if (plane == 0)
  354. return -EINVAL;
  355. color_code = DISPC_YUV2_422;
  356. cconv_en = 1;
  357. bpp = 16;
  358. break;
  359. default:
  360. return -EINVAL;
  361. }
  362. l = dispc_read_reg(at_reg[plane]);
  363. l &= ~(0x0f << 1);
  364. l |= color_code << 1;
  365. l &= ~(1 << 9);
  366. l |= cconv_en << 9;
  367. l &= ~(0x03 << burst_shift);
  368. l |= DISPC_BURST_8x32 << burst_shift;
  369. l &= ~(1 << chout_shift);
  370. l |= chout_val << chout_shift;
  371. dispc_write_reg(at_reg[plane], l);
  372. dispc_write_reg(ba_reg[plane], paddr);
  373. MOD_REG_FLD(ps_reg[plane],
  374. FLD_MASK(16, 11) | FLD_MASK(0, 11), (pos_y << 16) | pos_x);
  375. MOD_REG_FLD(sz_reg[plane], FLD_MASK(16, 11) | FLD_MASK(0, 11),
  376. ((height - 1) << 16) | (width - 1));
  377. if (set_vsize) {
  378. /* Set video size if set_scale hasn't set it */
  379. if (!dispc.fir_vinc[plane])
  380. MOD_REG_FLD(vs_reg[plane],
  381. FLD_MASK(16, 11), (height - 1) << 16);
  382. if (!dispc.fir_hinc[plane])
  383. MOD_REG_FLD(vs_reg[plane],
  384. FLD_MASK(0, 11), width - 1);
  385. }
  386. dispc_write_reg(ri_reg[plane], (screen_width - width) * bpp / 8 + 1);
  387. return height * screen_width * bpp / 8;
  388. }
  389. static int omap_dispc_setup_plane(int plane, int channel_out,
  390. unsigned long offset,
  391. int screen_width,
  392. int pos_x, int pos_y, int width, int height,
  393. int color_mode)
  394. {
  395. u32 paddr;
  396. int r;
  397. if ((unsigned)plane > dispc.mem_desc.region_cnt)
  398. return -EINVAL;
  399. paddr = dispc.mem_desc.region[plane].paddr + offset;
  400. enable_lcd_clocks(1);
  401. r = _setup_plane(plane, channel_out, paddr,
  402. screen_width,
  403. pos_x, pos_y, width, height, color_mode);
  404. enable_lcd_clocks(0);
  405. return r;
  406. }
  407. static void write_firh_reg(int plane, int reg, u32 value)
  408. {
  409. u32 base;
  410. if (plane == 1)
  411. base = DISPC_VID1_BASE + DISPC_VID_FIR_COEF_H0;
  412. else
  413. base = DISPC_VID2_BASE + DISPC_VID_FIR_COEF_H0;
  414. dispc_write_reg(base + reg * 8, value);
  415. }
  416. static void write_firhv_reg(int plane, int reg, u32 value)
  417. {
  418. u32 base;
  419. if (plane == 1)
  420. base = DISPC_VID1_BASE + DISPC_VID_FIR_COEF_HV0;
  421. else
  422. base = DISPC_VID2_BASE + DISPC_VID_FIR_COEF_HV0;
  423. dispc_write_reg(base + reg * 8, value);
  424. }
  425. static void set_upsampling_coef_table(int plane)
  426. {
  427. const u32 coef[][2] = {
  428. { 0x00800000, 0x00800000 },
  429. { 0x0D7CF800, 0x037B02FF },
  430. { 0x1E70F5FF, 0x0C6F05FE },
  431. { 0x335FF5FE, 0x205907FB },
  432. { 0xF74949F7, 0x00404000 },
  433. { 0xF55F33FB, 0x075920FE },
  434. { 0xF5701EFE, 0x056F0CFF },
  435. { 0xF87C0DFF, 0x027B0300 },
  436. };
  437. int i;
  438. for (i = 0; i < 8; i++) {
  439. write_firh_reg(plane, i, coef[i][0]);
  440. write_firhv_reg(plane, i, coef[i][1]);
  441. }
  442. }
  443. static int omap_dispc_set_scale(int plane,
  444. int orig_width, int orig_height,
  445. int out_width, int out_height)
  446. {
  447. const u32 at_reg[] = { 0, DISPC_VID1_BASE + DISPC_VID_ATTRIBUTES,
  448. DISPC_VID2_BASE + DISPC_VID_ATTRIBUTES };
  449. const u32 vs_reg[] = { 0, DISPC_VID1_BASE + DISPC_VID_SIZE,
  450. DISPC_VID2_BASE + DISPC_VID_SIZE };
  451. const u32 fir_reg[] = { 0, DISPC_VID1_BASE + DISPC_VID_FIR,
  452. DISPC_VID2_BASE + DISPC_VID_FIR };
  453. u32 l;
  454. int fir_hinc;
  455. int fir_vinc;
  456. if ((unsigned)plane > OMAPFB_PLANE_NUM)
  457. return -ENODEV;
  458. if (plane == OMAPFB_PLANE_GFX &&
  459. (out_width != orig_width || out_height != orig_height))
  460. return -EINVAL;
  461. enable_lcd_clocks(1);
  462. if (orig_width < out_width) {
  463. /*
  464. * Upsampling.
  465. * Currently you can only scale both dimensions in one way.
  466. */
  467. if (orig_height > out_height ||
  468. orig_width * 8 < out_width ||
  469. orig_height * 8 < out_height) {
  470. enable_lcd_clocks(0);
  471. return -EINVAL;
  472. }
  473. set_upsampling_coef_table(plane);
  474. } else if (orig_width > out_width) {
  475. /* Downsampling not yet supported
  476. */
  477. enable_lcd_clocks(0);
  478. return -EINVAL;
  479. }
  480. if (!orig_width || orig_width == out_width)
  481. fir_hinc = 0;
  482. else
  483. fir_hinc = 1024 * orig_width / out_width;
  484. if (!orig_height || orig_height == out_height)
  485. fir_vinc = 0;
  486. else
  487. fir_vinc = 1024 * orig_height / out_height;
  488. dispc.fir_hinc[plane] = fir_hinc;
  489. dispc.fir_vinc[plane] = fir_vinc;
  490. MOD_REG_FLD(fir_reg[plane],
  491. FLD_MASK(16, 12) | FLD_MASK(0, 12),
  492. ((fir_vinc & 4095) << 16) |
  493. (fir_hinc & 4095));
  494. dev_dbg(dispc.fbdev->dev, "out_width %d out_height %d orig_width %d "
  495. "orig_height %d fir_hinc %d fir_vinc %d\n",
  496. out_width, out_height, orig_width, orig_height,
  497. fir_hinc, fir_vinc);
  498. MOD_REG_FLD(vs_reg[plane],
  499. FLD_MASK(16, 11) | FLD_MASK(0, 11),
  500. ((out_height - 1) << 16) | (out_width - 1));
  501. l = dispc_read_reg(at_reg[plane]);
  502. l &= ~(0x03 << 5);
  503. l |= fir_hinc ? (1 << 5) : 0;
  504. l |= fir_vinc ? (1 << 6) : 0;
  505. dispc_write_reg(at_reg[plane], l);
  506. enable_lcd_clocks(0);
  507. return 0;
  508. }
  509. static int omap_dispc_enable_plane(int plane, int enable)
  510. {
  511. const u32 at_reg[] = { DISPC_GFX_ATTRIBUTES,
  512. DISPC_VID1_BASE + DISPC_VID_ATTRIBUTES,
  513. DISPC_VID2_BASE + DISPC_VID_ATTRIBUTES };
  514. if ((unsigned int)plane > dispc.mem_desc.region_cnt)
  515. return -EINVAL;
  516. enable_lcd_clocks(1);
  517. MOD_REG_FLD(at_reg[plane], 1, enable ? 1 : 0);
  518. enable_lcd_clocks(0);
  519. return 0;
  520. }
  521. static int omap_dispc_set_color_key(struct omapfb_color_key *ck)
  522. {
  523. u32 df_reg, tr_reg;
  524. int shift, val;
  525. switch (ck->channel_out) {
  526. case OMAPFB_CHANNEL_OUT_LCD:
  527. df_reg = DISPC_DEFAULT_COLOR0;
  528. tr_reg = DISPC_TRANS_COLOR0;
  529. shift = 10;
  530. break;
  531. case OMAPFB_CHANNEL_OUT_DIGIT:
  532. df_reg = DISPC_DEFAULT_COLOR1;
  533. tr_reg = DISPC_TRANS_COLOR1;
  534. shift = 12;
  535. break;
  536. default:
  537. return -EINVAL;
  538. }
  539. switch (ck->key_type) {
  540. case OMAPFB_COLOR_KEY_DISABLED:
  541. val = 0;
  542. break;
  543. case OMAPFB_COLOR_KEY_GFX_DST:
  544. val = 1;
  545. break;
  546. case OMAPFB_COLOR_KEY_VID_SRC:
  547. val = 3;
  548. break;
  549. default:
  550. return -EINVAL;
  551. }
  552. enable_lcd_clocks(1);
  553. MOD_REG_FLD(DISPC_CONFIG, FLD_MASK(shift, 2), val << shift);
  554. if (val != 0)
  555. dispc_write_reg(tr_reg, ck->trans_key);
  556. dispc_write_reg(df_reg, ck->background);
  557. enable_lcd_clocks(0);
  558. dispc.color_key = *ck;
  559. return 0;
  560. }
  561. static int omap_dispc_get_color_key(struct omapfb_color_key *ck)
  562. {
  563. *ck = dispc.color_key;
  564. return 0;
  565. }
  566. static void load_palette(void)
  567. {
  568. }
  569. static int omap_dispc_set_update_mode(enum omapfb_update_mode mode)
  570. {
  571. int r = 0;
  572. if (mode != dispc.update_mode) {
  573. switch (mode) {
  574. case OMAPFB_AUTO_UPDATE:
  575. case OMAPFB_MANUAL_UPDATE:
  576. enable_lcd_clocks(1);
  577. omap_dispc_enable_lcd_out(1);
  578. dispc.update_mode = mode;
  579. break;
  580. case OMAPFB_UPDATE_DISABLED:
  581. init_completion(&dispc.frame_done);
  582. omap_dispc_enable_lcd_out(0);
  583. if (!wait_for_completion_timeout(&dispc.frame_done,
  584. msecs_to_jiffies(500))) {
  585. dev_err(dispc.fbdev->dev,
  586. "timeout waiting for FRAME DONE\n");
  587. }
  588. dispc.update_mode = mode;
  589. enable_lcd_clocks(0);
  590. break;
  591. default:
  592. r = -EINVAL;
  593. }
  594. }
  595. return r;
  596. }
  597. static void omap_dispc_get_caps(int plane, struct omapfb_caps *caps)
  598. {
  599. caps->ctrl |= OMAPFB_CAPS_PLANE_RELOCATE_MEM;
  600. if (plane > 0)
  601. caps->ctrl |= OMAPFB_CAPS_PLANE_SCALE;
  602. caps->plane_color |= (1 << OMAPFB_COLOR_RGB565) |
  603. (1 << OMAPFB_COLOR_YUV422) |
  604. (1 << OMAPFB_COLOR_YUY422);
  605. if (plane == 0)
  606. caps->plane_color |= (1 << OMAPFB_COLOR_CLUT_8BPP) |
  607. (1 << OMAPFB_COLOR_CLUT_4BPP) |
  608. (1 << OMAPFB_COLOR_CLUT_2BPP) |
  609. (1 << OMAPFB_COLOR_CLUT_1BPP) |
  610. (1 << OMAPFB_COLOR_RGB444);
  611. }
  612. static enum omapfb_update_mode omap_dispc_get_update_mode(void)
  613. {
  614. return dispc.update_mode;
  615. }
  616. static void setup_color_conv_coef(void)
  617. {
  618. u32 mask = FLD_MASK(16, 11) | FLD_MASK(0, 11);
  619. int cf1_reg = DISPC_VID1_BASE + DISPC_VID_CONV_COEF0;
  620. int cf2_reg = DISPC_VID2_BASE + DISPC_VID_CONV_COEF0;
  621. int at1_reg = DISPC_VID1_BASE + DISPC_VID_ATTRIBUTES;
  622. int at2_reg = DISPC_VID2_BASE + DISPC_VID_ATTRIBUTES;
  623. const struct color_conv_coef {
  624. int ry, rcr, rcb, gy, gcr, gcb, by, bcr, bcb;
  625. int full_range;
  626. } ctbl_bt601_5 = {
  627. 298, 409, 0, 298, -208, -100, 298, 0, 517, 0,
  628. };
  629. const struct color_conv_coef *ct;
  630. #define CVAL(x, y) (((x & 2047) << 16) | (y & 2047))
  631. ct = &ctbl_bt601_5;
  632. MOD_REG_FLD(cf1_reg, mask, CVAL(ct->rcr, ct->ry));
  633. MOD_REG_FLD(cf1_reg + 4, mask, CVAL(ct->gy, ct->rcb));
  634. MOD_REG_FLD(cf1_reg + 8, mask, CVAL(ct->gcb, ct->gcr));
  635. MOD_REG_FLD(cf1_reg + 12, mask, CVAL(ct->bcr, ct->by));
  636. MOD_REG_FLD(cf1_reg + 16, mask, CVAL(0, ct->bcb));
  637. MOD_REG_FLD(cf2_reg, mask, CVAL(ct->rcr, ct->ry));
  638. MOD_REG_FLD(cf2_reg + 4, mask, CVAL(ct->gy, ct->rcb));
  639. MOD_REG_FLD(cf2_reg + 8, mask, CVAL(ct->gcb, ct->gcr));
  640. MOD_REG_FLD(cf2_reg + 12, mask, CVAL(ct->bcr, ct->by));
  641. MOD_REG_FLD(cf2_reg + 16, mask, CVAL(0, ct->bcb));
  642. #undef CVAL
  643. MOD_REG_FLD(at1_reg, (1 << 11), ct->full_range);
  644. MOD_REG_FLD(at2_reg, (1 << 11), ct->full_range);
  645. }
  646. static void calc_ck_div(int is_tft, int pck, int *lck_div, int *pck_div)
  647. {
  648. unsigned long fck, lck;
  649. *lck_div = 1;
  650. pck = max(1, pck);
  651. fck = clk_get_rate(dispc.dss1_fck);
  652. lck = fck;
  653. *pck_div = (lck + pck - 1) / pck;
  654. if (is_tft)
  655. *pck_div = max(2, *pck_div);
  656. else
  657. *pck_div = max(3, *pck_div);
  658. if (*pck_div > 255) {
  659. *pck_div = 255;
  660. lck = pck * *pck_div;
  661. *lck_div = fck / lck;
  662. BUG_ON(*lck_div < 1);
  663. if (*lck_div > 255) {
  664. *lck_div = 255;
  665. dev_warn(dispc.fbdev->dev, "pixclock %d kHz too low.\n",
  666. pck / 1000);
  667. }
  668. }
  669. }
  670. static void set_lcd_tft_mode(int enable)
  671. {
  672. u32 mask;
  673. mask = 1 << 3;
  674. MOD_REG_FLD(DISPC_CONTROL, mask, enable ? mask : 0);
  675. }
  676. static void set_lcd_timings(void)
  677. {
  678. u32 l;
  679. int lck_div, pck_div;
  680. struct lcd_panel *panel = dispc.fbdev->panel;
  681. int is_tft = panel->config & OMAP_LCDC_PANEL_TFT;
  682. unsigned long fck;
  683. l = dispc_read_reg(DISPC_TIMING_H);
  684. l &= ~(FLD_MASK(0, 6) | FLD_MASK(8, 8) | FLD_MASK(20, 8));
  685. l |= ( max(1, (min(64, panel->hsw))) - 1 ) << 0;
  686. l |= ( max(1, (min(256, panel->hfp))) - 1 ) << 8;
  687. l |= ( max(1, (min(256, panel->hbp))) - 1 ) << 20;
  688. dispc_write_reg(DISPC_TIMING_H, l);
  689. l = dispc_read_reg(DISPC_TIMING_V);
  690. l &= ~(FLD_MASK(0, 6) | FLD_MASK(8, 8) | FLD_MASK(20, 8));
  691. l |= ( max(1, (min(64, panel->vsw))) - 1 ) << 0;
  692. l |= ( max(0, (min(255, panel->vfp))) - 0 ) << 8;
  693. l |= ( max(0, (min(255, panel->vbp))) - 0 ) << 20;
  694. dispc_write_reg(DISPC_TIMING_V, l);
  695. l = dispc_read_reg(DISPC_POL_FREQ);
  696. l &= ~FLD_MASK(12, 6);
  697. l |= (panel->config & OMAP_LCDC_SIGNAL_MASK) << 12;
  698. l |= panel->acb & 0xff;
  699. dispc_write_reg(DISPC_POL_FREQ, l);
  700. calc_ck_div(is_tft, panel->pixel_clock * 1000, &lck_div, &pck_div);
  701. l = dispc_read_reg(DISPC_DIVISOR);
  702. l &= ~(FLD_MASK(16, 8) | FLD_MASK(0, 8));
  703. l |= (lck_div << 16) | (pck_div << 0);
  704. dispc_write_reg(DISPC_DIVISOR, l);
  705. /* update panel info with the exact clock */
  706. fck = clk_get_rate(dispc.dss1_fck);
  707. panel->pixel_clock = fck / lck_div / pck_div / 1000;
  708. }
  709. static void recalc_irq_mask(void)
  710. {
  711. int i;
  712. unsigned long irq_mask = DISPC_IRQ_MASK_ERROR;
  713. for (i = 0; i < MAX_IRQ_HANDLERS; i++) {
  714. if (!dispc.irq_handlers[i].callback)
  715. continue;
  716. irq_mask |= dispc.irq_handlers[i].irq_mask;
  717. }
  718. enable_lcd_clocks(1);
  719. MOD_REG_FLD(DISPC_IRQENABLE, 0x7fff, irq_mask);
  720. enable_lcd_clocks(0);
  721. }
  722. int omap_dispc_request_irq(unsigned long irq_mask, void (*callback)(void *data),
  723. void *data)
  724. {
  725. int i;
  726. BUG_ON(callback == NULL);
  727. for (i = 0; i < MAX_IRQ_HANDLERS; i++) {
  728. if (dispc.irq_handlers[i].callback)
  729. continue;
  730. dispc.irq_handlers[i].irq_mask = irq_mask;
  731. dispc.irq_handlers[i].callback = callback;
  732. dispc.irq_handlers[i].data = data;
  733. recalc_irq_mask();
  734. return 0;
  735. }
  736. return -EBUSY;
  737. }
  738. EXPORT_SYMBOL(omap_dispc_request_irq);
  739. void omap_dispc_free_irq(unsigned long irq_mask, void (*callback)(void *data),
  740. void *data)
  741. {
  742. int i;
  743. for (i = 0; i < MAX_IRQ_HANDLERS; i++) {
  744. if (dispc.irq_handlers[i].callback == callback &&
  745. dispc.irq_handlers[i].data == data) {
  746. dispc.irq_handlers[i].irq_mask = 0;
  747. dispc.irq_handlers[i].callback = NULL;
  748. dispc.irq_handlers[i].data = NULL;
  749. recalc_irq_mask();
  750. return;
  751. }
  752. }
  753. BUG();
  754. }
  755. EXPORT_SYMBOL(omap_dispc_free_irq);
  756. static irqreturn_t omap_dispc_irq_handler(int irq, void *dev)
  757. {
  758. u32 stat;
  759. int i = 0;
  760. enable_lcd_clocks(1);
  761. stat = dispc_read_reg(DISPC_IRQSTATUS);
  762. if (stat & DISPC_IRQ_FRAMEMASK)
  763. complete(&dispc.frame_done);
  764. if (stat & DISPC_IRQ_MASK_ERROR) {
  765. if (printk_ratelimit()) {
  766. dev_err(dispc.fbdev->dev, "irq error status %04x\n",
  767. stat & 0x7fff);
  768. }
  769. }
  770. for (i = 0; i < MAX_IRQ_HANDLERS; i++) {
  771. if (unlikely(dispc.irq_handlers[i].callback &&
  772. (stat & dispc.irq_handlers[i].irq_mask)))
  773. dispc.irq_handlers[i].callback(
  774. dispc.irq_handlers[i].data);
  775. }
  776. dispc_write_reg(DISPC_IRQSTATUS, stat);
  777. enable_lcd_clocks(0);
  778. return IRQ_HANDLED;
  779. }
  780. static int get_dss_clocks(void)
  781. {
  782. dispc.dss_ick = clk_get(&dispc.fbdev->dssdev->dev, "ick");
  783. if (IS_ERR(dispc.dss_ick)) {
  784. dev_err(dispc.fbdev->dev, "can't get ick\n");
  785. return PTR_ERR(dispc.dss_ick);
  786. }
  787. dispc.dss1_fck = clk_get(&dispc.fbdev->dssdev->dev, "fck");
  788. if (IS_ERR(dispc.dss1_fck)) {
  789. dev_err(dispc.fbdev->dev, "can't get dss1_fck\n");
  790. clk_put(dispc.dss_ick);
  791. return PTR_ERR(dispc.dss1_fck);
  792. }
  793. dispc.dss_54m_fck = clk_get(&dispc.fbdev->dssdev->dev, "tv_clk");
  794. if (IS_ERR(dispc.dss_54m_fck)) {
  795. dev_err(dispc.fbdev->dev, "can't get tv_fck\n");
  796. clk_put(dispc.dss_ick);
  797. clk_put(dispc.dss1_fck);
  798. return PTR_ERR(dispc.dss_54m_fck);
  799. }
  800. return 0;
  801. }
  802. static void put_dss_clocks(void)
  803. {
  804. clk_put(dispc.dss_54m_fck);
  805. clk_put(dispc.dss1_fck);
  806. clk_put(dispc.dss_ick);
  807. }
  808. static void enable_lcd_clocks(int enable)
  809. {
  810. if (enable) {
  811. clk_enable(dispc.dss_ick);
  812. clk_enable(dispc.dss1_fck);
  813. } else {
  814. clk_disable(dispc.dss1_fck);
  815. clk_disable(dispc.dss_ick);
  816. }
  817. }
  818. static void enable_digit_clocks(int enable)
  819. {
  820. if (enable)
  821. clk_enable(dispc.dss_54m_fck);
  822. else
  823. clk_disable(dispc.dss_54m_fck);
  824. }
  825. static void omap_dispc_suspend(void)
  826. {
  827. if (dispc.update_mode == OMAPFB_AUTO_UPDATE) {
  828. init_completion(&dispc.frame_done);
  829. omap_dispc_enable_lcd_out(0);
  830. if (!wait_for_completion_timeout(&dispc.frame_done,
  831. msecs_to_jiffies(500))) {
  832. dev_err(dispc.fbdev->dev,
  833. "timeout waiting for FRAME DONE\n");
  834. }
  835. enable_lcd_clocks(0);
  836. }
  837. }
  838. static void omap_dispc_resume(void)
  839. {
  840. if (dispc.update_mode == OMAPFB_AUTO_UPDATE) {
  841. enable_lcd_clocks(1);
  842. if (!dispc.ext_mode) {
  843. set_lcd_timings();
  844. load_palette();
  845. }
  846. omap_dispc_enable_lcd_out(1);
  847. }
  848. }
  849. static int omap_dispc_update_window(struct fb_info *fbi,
  850. struct omapfb_update_window *win,
  851. void (*complete_callback)(void *arg),
  852. void *complete_callback_data)
  853. {
  854. return dispc.update_mode == OMAPFB_UPDATE_DISABLED ? -ENODEV : 0;
  855. }
  856. static int mmap_kern(struct omapfb_mem_region *region)
  857. {
  858. struct vm_struct *kvma;
  859. struct vm_area_struct vma;
  860. pgprot_t pgprot;
  861. unsigned long vaddr;
  862. kvma = get_vm_area(region->size, VM_IOREMAP);
  863. if (kvma == NULL) {
  864. dev_err(dispc.fbdev->dev, "can't get kernel vm area\n");
  865. return -ENOMEM;
  866. }
  867. vma.vm_mm = &init_mm;
  868. vaddr = (unsigned long)kvma->addr;
  869. pgprot = pgprot_writecombine(pgprot_kernel);
  870. vma.vm_start = vaddr;
  871. vma.vm_end = vaddr + region->size;
  872. if (io_remap_pfn_range(&vma, vaddr, region->paddr >> PAGE_SHIFT,
  873. region->size, pgprot) < 0) {
  874. dev_err(dispc.fbdev->dev, "kernel mmap for FBMEM failed\n");
  875. return -EAGAIN;
  876. }
  877. region->vaddr = (void *)vaddr;
  878. return 0;
  879. }
  880. static void mmap_user_open(struct vm_area_struct *vma)
  881. {
  882. int plane = (int)vma->vm_private_data;
  883. atomic_inc(&dispc.map_count[plane]);
  884. }
  885. static void mmap_user_close(struct vm_area_struct *vma)
  886. {
  887. int plane = (int)vma->vm_private_data;
  888. atomic_dec(&dispc.map_count[plane]);
  889. }
  890. static const struct vm_operations_struct mmap_user_ops = {
  891. .open = mmap_user_open,
  892. .close = mmap_user_close,
  893. };
  894. static int omap_dispc_mmap_user(struct fb_info *info,
  895. struct vm_area_struct *vma)
  896. {
  897. struct omapfb_plane_struct *plane = info->par;
  898. unsigned long off;
  899. unsigned long start;
  900. u32 len;
  901. if (vma->vm_end - vma->vm_start == 0)
  902. return 0;
  903. if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
  904. return -EINVAL;
  905. off = vma->vm_pgoff << PAGE_SHIFT;
  906. start = info->fix.smem_start;
  907. len = info->fix.smem_len;
  908. if (off >= len)
  909. return -EINVAL;
  910. if ((vma->vm_end - vma->vm_start + off) > len)
  911. return -EINVAL;
  912. off += start;
  913. vma->vm_pgoff = off >> PAGE_SHIFT;
  914. vma->vm_flags |= VM_IO | VM_RESERVED;
  915. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  916. vma->vm_ops = &mmap_user_ops;
  917. vma->vm_private_data = (void *)plane->idx;
  918. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  919. vma->vm_end - vma->vm_start, vma->vm_page_prot))
  920. return -EAGAIN;
  921. /* vm_ops.open won't be called for mmap itself. */
  922. atomic_inc(&dispc.map_count[plane->idx]);
  923. return 0;
  924. }
  925. static void unmap_kern(struct omapfb_mem_region *region)
  926. {
  927. vunmap(region->vaddr);
  928. }
  929. static int alloc_palette_ram(void)
  930. {
  931. dispc.palette_vaddr = dma_alloc_writecombine(dispc.fbdev->dev,
  932. MAX_PALETTE_SIZE, &dispc.palette_paddr, GFP_KERNEL);
  933. if (dispc.palette_vaddr == NULL) {
  934. dev_err(dispc.fbdev->dev, "failed to alloc palette memory\n");
  935. return -ENOMEM;
  936. }
  937. return 0;
  938. }
  939. static void free_palette_ram(void)
  940. {
  941. dma_free_writecombine(dispc.fbdev->dev, MAX_PALETTE_SIZE,
  942. dispc.palette_vaddr, dispc.palette_paddr);
  943. }
  944. static int alloc_fbmem(struct omapfb_mem_region *region)
  945. {
  946. region->vaddr = dma_alloc_writecombine(dispc.fbdev->dev,
  947. region->size, &region->paddr, GFP_KERNEL);
  948. if (region->vaddr == NULL) {
  949. dev_err(dispc.fbdev->dev, "unable to allocate FB DMA memory\n");
  950. return -ENOMEM;
  951. }
  952. return 0;
  953. }
  954. static void free_fbmem(struct omapfb_mem_region *region)
  955. {
  956. dma_free_writecombine(dispc.fbdev->dev, region->size,
  957. region->vaddr, region->paddr);
  958. }
  959. static struct resmap *init_resmap(unsigned long start, size_t size)
  960. {
  961. unsigned page_cnt;
  962. struct resmap *res_map;
  963. page_cnt = PAGE_ALIGN(size) / PAGE_SIZE;
  964. res_map =
  965. kzalloc(sizeof(struct resmap) + RESMAP_SIZE(page_cnt), GFP_KERNEL);
  966. if (res_map == NULL)
  967. return NULL;
  968. res_map->start = start;
  969. res_map->page_cnt = page_cnt;
  970. res_map->map = (unsigned long *)(res_map + 1);
  971. return res_map;
  972. }
  973. static void cleanup_resmap(struct resmap *res_map)
  974. {
  975. kfree(res_map);
  976. }
  977. static inline int resmap_mem_type(unsigned long start)
  978. {
  979. if (start >= OMAP2_SRAM_START &&
  980. start < OMAP2_SRAM_START + OMAP2_SRAM_SIZE)
  981. return OMAPFB_MEMTYPE_SRAM;
  982. else
  983. return OMAPFB_MEMTYPE_SDRAM;
  984. }
  985. static inline int resmap_page_reserved(struct resmap *res_map, unsigned page_nr)
  986. {
  987. return *RESMAP_PTR(res_map, page_nr) & RESMAP_MASK(page_nr) ? 1 : 0;
  988. }
  989. static inline void resmap_reserve_page(struct resmap *res_map, unsigned page_nr)
  990. {
  991. BUG_ON(resmap_page_reserved(res_map, page_nr));
  992. *RESMAP_PTR(res_map, page_nr) |= RESMAP_MASK(page_nr);
  993. }
  994. static inline void resmap_free_page(struct resmap *res_map, unsigned page_nr)
  995. {
  996. BUG_ON(!resmap_page_reserved(res_map, page_nr));
  997. *RESMAP_PTR(res_map, page_nr) &= ~RESMAP_MASK(page_nr);
  998. }
  999. static void resmap_reserve_region(unsigned long start, size_t size)
  1000. {
  1001. struct resmap *res_map;
  1002. unsigned start_page;
  1003. unsigned end_page;
  1004. int mtype;
  1005. unsigned i;
  1006. mtype = resmap_mem_type(start);
  1007. res_map = dispc.res_map[mtype];
  1008. dev_dbg(dispc.fbdev->dev, "reserve mem type %d start %08lx size %d\n",
  1009. mtype, start, size);
  1010. start_page = (start - res_map->start) / PAGE_SIZE;
  1011. end_page = start_page + PAGE_ALIGN(size) / PAGE_SIZE;
  1012. for (i = start_page; i < end_page; i++)
  1013. resmap_reserve_page(res_map, i);
  1014. }
  1015. static void resmap_free_region(unsigned long start, size_t size)
  1016. {
  1017. struct resmap *res_map;
  1018. unsigned start_page;
  1019. unsigned end_page;
  1020. unsigned i;
  1021. int mtype;
  1022. mtype = resmap_mem_type(start);
  1023. res_map = dispc.res_map[mtype];
  1024. dev_dbg(dispc.fbdev->dev, "free mem type %d start %08lx size %d\n",
  1025. mtype, start, size);
  1026. start_page = (start - res_map->start) / PAGE_SIZE;
  1027. end_page = start_page + PAGE_ALIGN(size) / PAGE_SIZE;
  1028. for (i = start_page; i < end_page; i++)
  1029. resmap_free_page(res_map, i);
  1030. }
  1031. static unsigned long resmap_alloc_region(int mtype, size_t size)
  1032. {
  1033. unsigned i;
  1034. unsigned total;
  1035. unsigned start_page;
  1036. unsigned long start;
  1037. struct resmap *res_map = dispc.res_map[mtype];
  1038. BUG_ON(mtype >= DISPC_MEMTYPE_NUM || res_map == NULL || !size);
  1039. size = PAGE_ALIGN(size) / PAGE_SIZE;
  1040. start_page = 0;
  1041. total = 0;
  1042. for (i = 0; i < res_map->page_cnt; i++) {
  1043. if (resmap_page_reserved(res_map, i)) {
  1044. start_page = i + 1;
  1045. total = 0;
  1046. } else if (++total == size)
  1047. break;
  1048. }
  1049. if (total < size)
  1050. return 0;
  1051. start = res_map->start + start_page * PAGE_SIZE;
  1052. resmap_reserve_region(start, size * PAGE_SIZE);
  1053. return start;
  1054. }
  1055. /* Note that this will only work for user mappings, we don't deal with
  1056. * kernel mappings here, so fbcon will keep using the old region.
  1057. */
  1058. static int omap_dispc_setup_mem(int plane, size_t size, int mem_type,
  1059. unsigned long *paddr)
  1060. {
  1061. struct omapfb_mem_region *rg;
  1062. unsigned long new_addr = 0;
  1063. if ((unsigned)plane > dispc.mem_desc.region_cnt)
  1064. return -EINVAL;
  1065. if (mem_type >= DISPC_MEMTYPE_NUM)
  1066. return -EINVAL;
  1067. if (dispc.res_map[mem_type] == NULL)
  1068. return -ENOMEM;
  1069. rg = &dispc.mem_desc.region[plane];
  1070. if (size == rg->size && mem_type == rg->type)
  1071. return 0;
  1072. if (atomic_read(&dispc.map_count[plane]))
  1073. return -EBUSY;
  1074. if (rg->size != 0)
  1075. resmap_free_region(rg->paddr, rg->size);
  1076. if (size != 0) {
  1077. new_addr = resmap_alloc_region(mem_type, size);
  1078. if (!new_addr) {
  1079. /* Reallocate old region. */
  1080. resmap_reserve_region(rg->paddr, rg->size);
  1081. return -ENOMEM;
  1082. }
  1083. }
  1084. rg->paddr = new_addr;
  1085. rg->size = size;
  1086. rg->type = mem_type;
  1087. *paddr = new_addr;
  1088. return 0;
  1089. }
  1090. static int setup_fbmem(struct omapfb_mem_desc *req_md)
  1091. {
  1092. struct omapfb_mem_region *rg;
  1093. int i;
  1094. int r;
  1095. unsigned long mem_start[DISPC_MEMTYPE_NUM];
  1096. unsigned long mem_end[DISPC_MEMTYPE_NUM];
  1097. if (!req_md->region_cnt) {
  1098. dev_err(dispc.fbdev->dev, "no memory regions defined\n");
  1099. return -ENOENT;
  1100. }
  1101. rg = &req_md->region[0];
  1102. memset(mem_start, 0xff, sizeof(mem_start));
  1103. memset(mem_end, 0, sizeof(mem_end));
  1104. for (i = 0; i < req_md->region_cnt; i++, rg++) {
  1105. int mtype;
  1106. if (rg->paddr) {
  1107. rg->alloc = 0;
  1108. if (rg->vaddr == NULL) {
  1109. rg->map = 1;
  1110. if ((r = mmap_kern(rg)) < 0)
  1111. return r;
  1112. }
  1113. } else {
  1114. if (rg->type != OMAPFB_MEMTYPE_SDRAM) {
  1115. dev_err(dispc.fbdev->dev,
  1116. "unsupported memory type\n");
  1117. return -EINVAL;
  1118. }
  1119. rg->alloc = rg->map = 1;
  1120. if ((r = alloc_fbmem(rg)) < 0)
  1121. return r;
  1122. }
  1123. mtype = rg->type;
  1124. if (rg->paddr < mem_start[mtype])
  1125. mem_start[mtype] = rg->paddr;
  1126. if (rg->paddr + rg->size > mem_end[mtype])
  1127. mem_end[mtype] = rg->paddr + rg->size;
  1128. }
  1129. for (i = 0; i < DISPC_MEMTYPE_NUM; i++) {
  1130. unsigned long start;
  1131. size_t size;
  1132. if (mem_end[i] == 0)
  1133. continue;
  1134. start = mem_start[i];
  1135. size = mem_end[i] - start;
  1136. dispc.res_map[i] = init_resmap(start, size);
  1137. r = -ENOMEM;
  1138. if (dispc.res_map[i] == NULL)
  1139. goto fail;
  1140. /* Initial state is that everything is reserved. This
  1141. * includes possible holes as well, which will never be
  1142. * freed.
  1143. */
  1144. resmap_reserve_region(start, size);
  1145. }
  1146. dispc.mem_desc = *req_md;
  1147. return 0;
  1148. fail:
  1149. for (i = 0; i < DISPC_MEMTYPE_NUM; i++) {
  1150. if (dispc.res_map[i] != NULL)
  1151. cleanup_resmap(dispc.res_map[i]);
  1152. }
  1153. return r;
  1154. }
  1155. static void cleanup_fbmem(void)
  1156. {
  1157. struct omapfb_mem_region *rg;
  1158. int i;
  1159. for (i = 0; i < DISPC_MEMTYPE_NUM; i++) {
  1160. if (dispc.res_map[i] != NULL)
  1161. cleanup_resmap(dispc.res_map[i]);
  1162. }
  1163. rg = &dispc.mem_desc.region[0];
  1164. for (i = 0; i < dispc.mem_desc.region_cnt; i++, rg++) {
  1165. if (rg->alloc)
  1166. free_fbmem(rg);
  1167. else {
  1168. if (rg->map)
  1169. unmap_kern(rg);
  1170. }
  1171. }
  1172. }
  1173. static int omap_dispc_init(struct omapfb_device *fbdev, int ext_mode,
  1174. struct omapfb_mem_desc *req_vram)
  1175. {
  1176. int r;
  1177. u32 l;
  1178. struct lcd_panel *panel = fbdev->panel;
  1179. void __iomem *ram_fw_base;
  1180. int tmo = 10000;
  1181. int skip_init = 0;
  1182. int i;
  1183. memset(&dispc, 0, sizeof(dispc));
  1184. dispc.base = ioremap(DISPC_BASE, SZ_1K);
  1185. if (!dispc.base) {
  1186. dev_err(fbdev->dev, "can't ioremap DISPC\n");
  1187. return -ENOMEM;
  1188. }
  1189. dispc.fbdev = fbdev;
  1190. dispc.ext_mode = ext_mode;
  1191. init_completion(&dispc.frame_done);
  1192. if ((r = get_dss_clocks()) < 0)
  1193. goto fail0;
  1194. enable_lcd_clocks(1);
  1195. #ifdef CONFIG_FB_OMAP_BOOTLOADER_INIT
  1196. l = dispc_read_reg(DISPC_CONTROL);
  1197. /* LCD enabled ? */
  1198. if (l & 1) {
  1199. pr_info("omapfb: skipping hardware initialization\n");
  1200. skip_init = 1;
  1201. }
  1202. #endif
  1203. if (!skip_init) {
  1204. /* Reset monitoring works only w/ the 54M clk */
  1205. enable_digit_clocks(1);
  1206. /* Soft reset */
  1207. MOD_REG_FLD(DISPC_SYSCONFIG, 1 << 1, 1 << 1);
  1208. while (!(dispc_read_reg(DISPC_SYSSTATUS) & 1)) {
  1209. if (!--tmo) {
  1210. dev_err(dispc.fbdev->dev, "soft reset failed\n");
  1211. r = -ENODEV;
  1212. enable_digit_clocks(0);
  1213. goto fail1;
  1214. }
  1215. }
  1216. enable_digit_clocks(0);
  1217. }
  1218. /* Enable smart standby/idle, autoidle and wakeup */
  1219. l = dispc_read_reg(DISPC_SYSCONFIG);
  1220. l &= ~((3 << 12) | (3 << 3));
  1221. l |= (2 << 12) | (2 << 3) | (1 << 2) | (1 << 0);
  1222. dispc_write_reg(DISPC_SYSCONFIG, l);
  1223. omap_writel(1 << 0, DSS_BASE + DSS_SYSCONFIG);
  1224. /* Set functional clock autogating */
  1225. l = dispc_read_reg(DISPC_CONFIG);
  1226. l |= 1 << 9;
  1227. dispc_write_reg(DISPC_CONFIG, l);
  1228. l = dispc_read_reg(DISPC_IRQSTATUS);
  1229. dispc_write_reg(DISPC_IRQSTATUS, l);
  1230. recalc_irq_mask();
  1231. if ((r = request_irq(INT_24XX_DSS_IRQ, omap_dispc_irq_handler,
  1232. 0, MODULE_NAME, fbdev)) < 0) {
  1233. dev_err(dispc.fbdev->dev, "can't get DSS IRQ\n");
  1234. goto fail1;
  1235. }
  1236. /* L3 firewall setting: enable access to OCM RAM */
  1237. ram_fw_base = ioremap(0x68005000, SZ_1K);
  1238. if (!ram_fw_base) {
  1239. dev_err(dispc.fbdev->dev, "Cannot ioremap to enable OCM RAM\n");
  1240. goto fail1;
  1241. }
  1242. __raw_writel(0x402000b0, ram_fw_base + 0xa0);
  1243. iounmap(ram_fw_base);
  1244. if ((r = alloc_palette_ram()) < 0)
  1245. goto fail2;
  1246. if ((r = setup_fbmem(req_vram)) < 0)
  1247. goto fail3;
  1248. if (!skip_init) {
  1249. for (i = 0; i < dispc.mem_desc.region_cnt; i++) {
  1250. memset(dispc.mem_desc.region[i].vaddr, 0,
  1251. dispc.mem_desc.region[i].size);
  1252. }
  1253. /* Set logic clock to fck, pixel clock to fck/2 for now */
  1254. MOD_REG_FLD(DISPC_DIVISOR, FLD_MASK(16, 8), 1 << 16);
  1255. MOD_REG_FLD(DISPC_DIVISOR, FLD_MASK(0, 8), 2 << 0);
  1256. setup_plane_fifo(0, ext_mode);
  1257. setup_plane_fifo(1, ext_mode);
  1258. setup_plane_fifo(2, ext_mode);
  1259. setup_color_conv_coef();
  1260. set_lcd_tft_mode(panel->config & OMAP_LCDC_PANEL_TFT);
  1261. set_load_mode(DISPC_LOAD_FRAME_ONLY);
  1262. if (!ext_mode) {
  1263. set_lcd_data_lines(panel->data_lines);
  1264. omap_dispc_set_lcd_size(panel->x_res, panel->y_res);
  1265. set_lcd_timings();
  1266. } else
  1267. set_lcd_data_lines(panel->bpp);
  1268. enable_rfbi_mode(ext_mode);
  1269. }
  1270. l = dispc_read_reg(DISPC_REVISION);
  1271. pr_info("omapfb: DISPC version %d.%d initialized\n",
  1272. l >> 4 & 0x0f, l & 0x0f);
  1273. enable_lcd_clocks(0);
  1274. return 0;
  1275. fail3:
  1276. free_palette_ram();
  1277. fail2:
  1278. free_irq(INT_24XX_DSS_IRQ, fbdev);
  1279. fail1:
  1280. enable_lcd_clocks(0);
  1281. put_dss_clocks();
  1282. fail0:
  1283. iounmap(dispc.base);
  1284. return r;
  1285. }
  1286. static void omap_dispc_cleanup(void)
  1287. {
  1288. int i;
  1289. omap_dispc_set_update_mode(OMAPFB_UPDATE_DISABLED);
  1290. /* This will also disable clocks that are on */
  1291. for (i = 0; i < dispc.mem_desc.region_cnt; i++)
  1292. omap_dispc_enable_plane(i, 0);
  1293. cleanup_fbmem();
  1294. free_palette_ram();
  1295. free_irq(INT_24XX_DSS_IRQ, dispc.fbdev);
  1296. put_dss_clocks();
  1297. iounmap(dispc.base);
  1298. }
  1299. const struct lcd_ctrl omap2_int_ctrl = {
  1300. .name = "internal",
  1301. .init = omap_dispc_init,
  1302. .cleanup = omap_dispc_cleanup,
  1303. .get_caps = omap_dispc_get_caps,
  1304. .set_update_mode = omap_dispc_set_update_mode,
  1305. .get_update_mode = omap_dispc_get_update_mode,
  1306. .update_window = omap_dispc_update_window,
  1307. .suspend = omap_dispc_suspend,
  1308. .resume = omap_dispc_resume,
  1309. .setup_plane = omap_dispc_setup_plane,
  1310. .setup_mem = omap_dispc_setup_mem,
  1311. .set_scale = omap_dispc_set_scale,
  1312. .enable_plane = omap_dispc_enable_plane,
  1313. .set_color_key = omap_dispc_set_color_key,
  1314. .get_color_key = omap_dispc_get_color_key,
  1315. .mmap = omap_dispc_mmap_user,
  1316. };