fsl-diu-fb.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  1. /*
  2. * Copyright 2008 Freescale Semiconductor, Inc. All Rights Reserved.
  3. *
  4. * Freescale DIU Frame Buffer device driver
  5. *
  6. * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
  7. * Paul Widmer <paul.widmer@freescale.com>
  8. * Srikanth Srinivasan <srikanth.srinivasan@freescale.com>
  9. * York Sun <yorksun@freescale.com>
  10. *
  11. * Based on imxfb.c Copyright (C) 2004 S.Hauer, Pengutronix
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/errno.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <linux/fb.h>
  25. #include <linux/init.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/clk.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_irq.h>
  35. #include <sysdev/fsl_soc.h>
  36. #include <linux/fsl-diu-fb.h>
  37. #include "edid.h"
  38. #define NUM_AOIS 5 /* 1 for plane 0, 2 for planes 1 & 2 each */
  39. /* HW cursor parameters */
  40. #define MAX_CURS 32
  41. /* INT_STATUS/INT_MASK field descriptions */
  42. #define INT_VSYNC 0x01 /* Vsync interrupt */
  43. #define INT_VSYNC_WB 0x02 /* Vsync interrupt for write back operation */
  44. #define INT_UNDRUN 0x04 /* Under run exception interrupt */
  45. #define INT_PARERR 0x08 /* Display parameters error interrupt */
  46. #define INT_LS_BF_VS 0x10 /* Lines before vsync. interrupt */
  47. /*
  48. * List of supported video modes
  49. *
  50. * The first entry is the default video mode. The remain entries are in
  51. * order if increasing resolution and frequency. The 320x240-60 mode is
  52. * the initial AOI for the second and third planes.
  53. */
  54. static struct fb_videomode fsl_diu_mode_db[] = {
  55. {
  56. .refresh = 60,
  57. .xres = 1024,
  58. .yres = 768,
  59. .pixclock = 15385,
  60. .left_margin = 160,
  61. .right_margin = 24,
  62. .upper_margin = 29,
  63. .lower_margin = 3,
  64. .hsync_len = 136,
  65. .vsync_len = 6,
  66. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  67. .vmode = FB_VMODE_NONINTERLACED
  68. },
  69. {
  70. .refresh = 60,
  71. .xres = 320,
  72. .yres = 240,
  73. .pixclock = 79440,
  74. .left_margin = 16,
  75. .right_margin = 16,
  76. .upper_margin = 16,
  77. .lower_margin = 5,
  78. .hsync_len = 48,
  79. .vsync_len = 1,
  80. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  81. .vmode = FB_VMODE_NONINTERLACED
  82. },
  83. {
  84. .refresh = 60,
  85. .xres = 640,
  86. .yres = 480,
  87. .pixclock = 39722,
  88. .left_margin = 48,
  89. .right_margin = 16,
  90. .upper_margin = 33,
  91. .lower_margin = 10,
  92. .hsync_len = 96,
  93. .vsync_len = 2,
  94. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  95. .vmode = FB_VMODE_NONINTERLACED
  96. },
  97. {
  98. .refresh = 72,
  99. .xres = 640,
  100. .yres = 480,
  101. .pixclock = 32052,
  102. .left_margin = 128,
  103. .right_margin = 24,
  104. .upper_margin = 28,
  105. .lower_margin = 9,
  106. .hsync_len = 40,
  107. .vsync_len = 3,
  108. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  109. .vmode = FB_VMODE_NONINTERLACED
  110. },
  111. {
  112. .refresh = 75,
  113. .xres = 640,
  114. .yres = 480,
  115. .pixclock = 31747,
  116. .left_margin = 120,
  117. .right_margin = 16,
  118. .upper_margin = 16,
  119. .lower_margin = 1,
  120. .hsync_len = 64,
  121. .vsync_len = 3,
  122. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  123. .vmode = FB_VMODE_NONINTERLACED
  124. },
  125. {
  126. .refresh = 90,
  127. .xres = 640,
  128. .yres = 480,
  129. .pixclock = 25057,
  130. .left_margin = 120,
  131. .right_margin = 32,
  132. .upper_margin = 14,
  133. .lower_margin = 25,
  134. .hsync_len = 40,
  135. .vsync_len = 14,
  136. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  137. .vmode = FB_VMODE_NONINTERLACED
  138. },
  139. {
  140. .refresh = 100,
  141. .xres = 640,
  142. .yres = 480,
  143. .pixclock = 22272,
  144. .left_margin = 48,
  145. .right_margin = 32,
  146. .upper_margin = 17,
  147. .lower_margin = 22,
  148. .hsync_len = 128,
  149. .vsync_len = 12,
  150. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  151. .vmode = FB_VMODE_NONINTERLACED
  152. },
  153. {
  154. .refresh = 60,
  155. .xres = 800,
  156. .yres = 480,
  157. .pixclock = 33805,
  158. .left_margin = 96,
  159. .right_margin = 24,
  160. .upper_margin = 10,
  161. .lower_margin = 3,
  162. .hsync_len = 72,
  163. .vsync_len = 7,
  164. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  165. .vmode = FB_VMODE_NONINTERLACED
  166. },
  167. {
  168. .refresh = 60,
  169. .xres = 800,
  170. .yres = 600,
  171. .pixclock = 25000,
  172. .left_margin = 88,
  173. .right_margin = 40,
  174. .upper_margin = 23,
  175. .lower_margin = 1,
  176. .hsync_len = 128,
  177. .vsync_len = 4,
  178. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  179. .vmode = FB_VMODE_NONINTERLACED
  180. },
  181. {
  182. .refresh = 60,
  183. .xres = 854,
  184. .yres = 480,
  185. .pixclock = 31518,
  186. .left_margin = 104,
  187. .right_margin = 16,
  188. .upper_margin = 13,
  189. .lower_margin = 1,
  190. .hsync_len = 88,
  191. .vsync_len = 3,
  192. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  193. .vmode = FB_VMODE_NONINTERLACED
  194. },
  195. {
  196. .refresh = 70,
  197. .xres = 1024,
  198. .yres = 768,
  199. .pixclock = 16886,
  200. .left_margin = 3,
  201. .right_margin = 3,
  202. .upper_margin = 2,
  203. .lower_margin = 2,
  204. .hsync_len = 40,
  205. .vsync_len = 18,
  206. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  207. .vmode = FB_VMODE_NONINTERLACED
  208. },
  209. {
  210. .refresh = 75,
  211. .xres = 1024,
  212. .yres = 768,
  213. .pixclock = 15009,
  214. .left_margin = 3,
  215. .right_margin = 3,
  216. .upper_margin = 2,
  217. .lower_margin = 2,
  218. .hsync_len = 80,
  219. .vsync_len = 32,
  220. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  221. .vmode = FB_VMODE_NONINTERLACED
  222. },
  223. {
  224. .refresh = 60,
  225. .xres = 1280,
  226. .yres = 480,
  227. .pixclock = 18939,
  228. .left_margin = 353,
  229. .right_margin = 47,
  230. .upper_margin = 39,
  231. .lower_margin = 4,
  232. .hsync_len = 8,
  233. .vsync_len = 2,
  234. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  235. .vmode = FB_VMODE_NONINTERLACED
  236. },
  237. {
  238. .refresh = 60,
  239. .xres = 1280,
  240. .yres = 720,
  241. .pixclock = 13426,
  242. .left_margin = 192,
  243. .right_margin = 64,
  244. .upper_margin = 22,
  245. .lower_margin = 1,
  246. .hsync_len = 136,
  247. .vsync_len = 3,
  248. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  249. .vmode = FB_VMODE_NONINTERLACED
  250. },
  251. {
  252. .refresh = 60,
  253. .xres = 1280,
  254. .yres = 1024,
  255. .pixclock = 9375,
  256. .left_margin = 38,
  257. .right_margin = 128,
  258. .upper_margin = 2,
  259. .lower_margin = 7,
  260. .hsync_len = 216,
  261. .vsync_len = 37,
  262. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  263. .vmode = FB_VMODE_NONINTERLACED
  264. },
  265. {
  266. .refresh = 70,
  267. .xres = 1280,
  268. .yres = 1024,
  269. .pixclock = 9380,
  270. .left_margin = 6,
  271. .right_margin = 6,
  272. .upper_margin = 4,
  273. .lower_margin = 4,
  274. .hsync_len = 60,
  275. .vsync_len = 94,
  276. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  277. .vmode = FB_VMODE_NONINTERLACED
  278. },
  279. {
  280. .refresh = 75,
  281. .xres = 1280,
  282. .yres = 1024,
  283. .pixclock = 9380,
  284. .left_margin = 6,
  285. .right_margin = 6,
  286. .upper_margin = 4,
  287. .lower_margin = 4,
  288. .hsync_len = 60,
  289. .vsync_len = 15,
  290. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  291. .vmode = FB_VMODE_NONINTERLACED
  292. },
  293. {
  294. .refresh = 60,
  295. .xres = 1920,
  296. .yres = 1080,
  297. .pixclock = 5787,
  298. .left_margin = 328,
  299. .right_margin = 120,
  300. .upper_margin = 34,
  301. .lower_margin = 1,
  302. .hsync_len = 208,
  303. .vsync_len = 3,
  304. .sync = FB_SYNC_COMP_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  305. .vmode = FB_VMODE_NONINTERLACED
  306. },
  307. };
  308. static char *fb_mode;
  309. static unsigned long default_bpp = 32;
  310. static enum fsl_diu_monitor_port monitor_port;
  311. static char *monitor_string;
  312. #if defined(CONFIG_NOT_COHERENT_CACHE)
  313. static u8 *coherence_data;
  314. static size_t coherence_data_size;
  315. static unsigned int d_cache_line_size;
  316. #endif
  317. static DEFINE_SPINLOCK(diu_lock);
  318. enum mfb_index {
  319. PLANE0 = 0, /* Plane 0, only one AOI that fills the screen */
  320. PLANE1_AOI0, /* Plane 1, first AOI */
  321. PLANE1_AOI1, /* Plane 1, second AOI */
  322. PLANE2_AOI0, /* Plane 2, first AOI */
  323. PLANE2_AOI1, /* Plane 2, second AOI */
  324. };
  325. struct mfb_info {
  326. enum mfb_index index;
  327. char *id;
  328. int registered;
  329. unsigned long pseudo_palette[16];
  330. struct diu_ad *ad;
  331. unsigned char g_alpha;
  332. unsigned int count;
  333. int x_aoi_d; /* aoi display x offset to physical screen */
  334. int y_aoi_d; /* aoi display y offset to physical screen */
  335. struct fsl_diu_data *parent;
  336. };
  337. /**
  338. * struct fsl_diu_data - per-DIU data structure
  339. * @dma_addr: DMA address of this structure
  340. * @fsl_diu_info: fb_info objects, one per AOI
  341. * @dev_attr: sysfs structure
  342. * @irq: IRQ
  343. * @monitor_port: the monitor port this DIU is connected to
  344. * @diu_reg: pointer to the DIU hardware registers
  345. * @reg_lock: spinlock for register access
  346. * @dummy_aoi: video buffer for the 4x4 32-bit dummy AOI
  347. * dummy_ad: DIU Area Descriptor for the dummy AOI
  348. * @ad[]: Area Descriptors for each real AOI
  349. * @gamma: gamma color table
  350. * @cursor: hardware cursor data
  351. * @blank_cursor: blank cursor for hiding cursor
  352. * @next_cursor: scratch space to build load cursor
  353. * @edid_data: EDID information buffer
  354. * @has_edid: whether or not the EDID buffer is valid
  355. *
  356. * This data structure must be allocated with 32-byte alignment, so that the
  357. * internal fields can be aligned properly.
  358. */
  359. struct fsl_diu_data {
  360. dma_addr_t dma_addr;
  361. struct fb_info fsl_diu_info[NUM_AOIS];
  362. struct mfb_info mfb[NUM_AOIS];
  363. struct device_attribute dev_attr;
  364. unsigned int irq;
  365. enum fsl_diu_monitor_port monitor_port;
  366. struct diu __iomem *diu_reg;
  367. spinlock_t reg_lock;
  368. u8 dummy_aoi[4 * 4 * 4];
  369. struct diu_ad dummy_ad __aligned(8);
  370. struct diu_ad ad[NUM_AOIS] __aligned(8);
  371. u8 gamma[256 * 3] __aligned(32);
  372. /* It's easier to parse the cursor data as little-endian */
  373. __le16 cursor[MAX_CURS * MAX_CURS] __aligned(32);
  374. /* Blank cursor data -- used to hide the cursor */
  375. __le16 blank_cursor[MAX_CURS * MAX_CURS] __aligned(32);
  376. /* Scratch cursor data -- used to build new cursor */
  377. __le16 next_cursor[MAX_CURS * MAX_CURS] __aligned(32);
  378. uint8_t edid_data[EDID_LENGTH];
  379. bool has_edid;
  380. } __aligned(32);
  381. /* Determine the DMA address of a member of the fsl_diu_data structure */
  382. #define DMA_ADDR(p, f) ((p)->dma_addr + offsetof(struct fsl_diu_data, f))
  383. static const struct mfb_info mfb_template[] = {
  384. {
  385. .index = PLANE0,
  386. .id = "Panel0",
  387. .registered = 0,
  388. .count = 0,
  389. .x_aoi_d = 0,
  390. .y_aoi_d = 0,
  391. },
  392. {
  393. .index = PLANE1_AOI0,
  394. .id = "Panel1 AOI0",
  395. .registered = 0,
  396. .g_alpha = 0xff,
  397. .count = 0,
  398. .x_aoi_d = 0,
  399. .y_aoi_d = 0,
  400. },
  401. {
  402. .index = PLANE1_AOI1,
  403. .id = "Panel1 AOI1",
  404. .registered = 0,
  405. .g_alpha = 0xff,
  406. .count = 0,
  407. .x_aoi_d = 0,
  408. .y_aoi_d = 480,
  409. },
  410. {
  411. .index = PLANE2_AOI0,
  412. .id = "Panel2 AOI0",
  413. .registered = 0,
  414. .g_alpha = 0xff,
  415. .count = 0,
  416. .x_aoi_d = 640,
  417. .y_aoi_d = 0,
  418. },
  419. {
  420. .index = PLANE2_AOI1,
  421. .id = "Panel2 AOI1",
  422. .registered = 0,
  423. .g_alpha = 0xff,
  424. .count = 0,
  425. .x_aoi_d = 640,
  426. .y_aoi_d = 480,
  427. },
  428. };
  429. #ifdef DEBUG
  430. static void __attribute__ ((unused)) fsl_diu_dump(struct diu __iomem *hw)
  431. {
  432. mb();
  433. pr_debug("DIU: desc=%08x,%08x,%08x, gamma=%08x palette=%08x "
  434. "cursor=%08x curs_pos=%08x diu_mode=%08x bgnd=%08x "
  435. "disp_size=%08x hsyn_para=%08x vsyn_para=%08x syn_pol=%08x "
  436. "thresholds=%08x int_mask=%08x plut=%08x\n",
  437. hw->desc[0], hw->desc[1], hw->desc[2], hw->gamma,
  438. hw->palette, hw->cursor, hw->curs_pos, hw->diu_mode,
  439. hw->bgnd, hw->disp_size, hw->hsyn_para, hw->vsyn_para,
  440. hw->syn_pol, hw->thresholds, hw->int_mask, hw->plut);
  441. rmb();
  442. }
  443. #endif
  444. /**
  445. * fsl_diu_name_to_port - convert a port name to a monitor port enum
  446. *
  447. * Takes the name of a monitor port ("dvi", "lvds", or "dlvds") and returns
  448. * the enum fsl_diu_monitor_port that corresponds to that string.
  449. *
  450. * For compatibility with older versions, a number ("0", "1", or "2") is also
  451. * supported.
  452. *
  453. * If the string is unknown, DVI is assumed.
  454. *
  455. * If the particular port is not supported by the platform, another port
  456. * (platform-specific) is chosen instead.
  457. */
  458. static enum fsl_diu_monitor_port fsl_diu_name_to_port(const char *s)
  459. {
  460. enum fsl_diu_monitor_port port = FSL_DIU_PORT_DVI;
  461. unsigned long val;
  462. if (s) {
  463. if (!kstrtoul(s, 10, &val) && (val <= 2))
  464. port = (enum fsl_diu_monitor_port) val;
  465. else if (strncmp(s, "lvds", 4) == 0)
  466. port = FSL_DIU_PORT_LVDS;
  467. else if (strncmp(s, "dlvds", 5) == 0)
  468. port = FSL_DIU_PORT_DLVDS;
  469. }
  470. if (diu_ops.valid_monitor_port)
  471. port = diu_ops.valid_monitor_port(port);
  472. return port;
  473. }
  474. /*
  475. * Workaround for failed writing desc register of planes.
  476. * Needed with MPC5121 DIU rev 2.0 silicon.
  477. */
  478. void wr_reg_wa(u32 *reg, u32 val)
  479. {
  480. do {
  481. out_be32(reg, val);
  482. } while (in_be32(reg) != val);
  483. }
  484. static void fsl_diu_enable_panel(struct fb_info *info)
  485. {
  486. struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
  487. struct diu_ad *ad = mfbi->ad;
  488. struct fsl_diu_data *data = mfbi->parent;
  489. struct diu __iomem *hw = data->diu_reg;
  490. switch (mfbi->index) {
  491. case PLANE0:
  492. wr_reg_wa(&hw->desc[0], ad->paddr);
  493. break;
  494. case PLANE1_AOI0:
  495. cmfbi = &data->mfb[2];
  496. if (hw->desc[1] != ad->paddr) { /* AOI0 closed */
  497. if (cmfbi->count > 0) /* AOI1 open */
  498. ad->next_ad =
  499. cpu_to_le32(cmfbi->ad->paddr);
  500. else
  501. ad->next_ad = 0;
  502. wr_reg_wa(&hw->desc[1], ad->paddr);
  503. }
  504. break;
  505. case PLANE2_AOI0:
  506. cmfbi = &data->mfb[4];
  507. if (hw->desc[2] != ad->paddr) { /* AOI0 closed */
  508. if (cmfbi->count > 0) /* AOI1 open */
  509. ad->next_ad =
  510. cpu_to_le32(cmfbi->ad->paddr);
  511. else
  512. ad->next_ad = 0;
  513. wr_reg_wa(&hw->desc[2], ad->paddr);
  514. }
  515. break;
  516. case PLANE1_AOI1:
  517. pmfbi = &data->mfb[1];
  518. ad->next_ad = 0;
  519. if (hw->desc[1] == data->dummy_ad.paddr)
  520. wr_reg_wa(&hw->desc[1], ad->paddr);
  521. else /* AOI0 open */
  522. pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
  523. break;
  524. case PLANE2_AOI1:
  525. pmfbi = &data->mfb[3];
  526. ad->next_ad = 0;
  527. if (hw->desc[2] == data->dummy_ad.paddr)
  528. wr_reg_wa(&hw->desc[2], ad->paddr);
  529. else /* AOI0 was open */
  530. pmfbi->ad->next_ad = cpu_to_le32(ad->paddr);
  531. break;
  532. }
  533. }
  534. static void fsl_diu_disable_panel(struct fb_info *info)
  535. {
  536. struct mfb_info *pmfbi, *cmfbi, *mfbi = info->par;
  537. struct diu_ad *ad = mfbi->ad;
  538. struct fsl_diu_data *data = mfbi->parent;
  539. struct diu __iomem *hw = data->diu_reg;
  540. switch (mfbi->index) {
  541. case PLANE0:
  542. wr_reg_wa(&hw->desc[0], 0);
  543. break;
  544. case PLANE1_AOI0:
  545. cmfbi = &data->mfb[2];
  546. if (cmfbi->count > 0) /* AOI1 is open */
  547. wr_reg_wa(&hw->desc[1], cmfbi->ad->paddr);
  548. /* move AOI1 to the first */
  549. else /* AOI1 was closed */
  550. wr_reg_wa(&hw->desc[1], data->dummy_ad.paddr);
  551. /* close AOI 0 */
  552. break;
  553. case PLANE2_AOI0:
  554. cmfbi = &data->mfb[4];
  555. if (cmfbi->count > 0) /* AOI1 is open */
  556. wr_reg_wa(&hw->desc[2], cmfbi->ad->paddr);
  557. /* move AOI1 to the first */
  558. else /* AOI1 was closed */
  559. wr_reg_wa(&hw->desc[2], data->dummy_ad.paddr);
  560. /* close AOI 0 */
  561. break;
  562. case PLANE1_AOI1:
  563. pmfbi = &data->mfb[1];
  564. if (hw->desc[1] != ad->paddr) {
  565. /* AOI1 is not the first in the chain */
  566. if (pmfbi->count > 0)
  567. /* AOI0 is open, must be the first */
  568. pmfbi->ad->next_ad = 0;
  569. } else /* AOI1 is the first in the chain */
  570. wr_reg_wa(&hw->desc[1], data->dummy_ad.paddr);
  571. /* close AOI 1 */
  572. break;
  573. case PLANE2_AOI1:
  574. pmfbi = &data->mfb[3];
  575. if (hw->desc[2] != ad->paddr) {
  576. /* AOI1 is not the first in the chain */
  577. if (pmfbi->count > 0)
  578. /* AOI0 is open, must be the first */
  579. pmfbi->ad->next_ad = 0;
  580. } else /* AOI1 is the first in the chain */
  581. wr_reg_wa(&hw->desc[2], data->dummy_ad.paddr);
  582. /* close AOI 1 */
  583. break;
  584. }
  585. }
  586. static void enable_lcdc(struct fb_info *info)
  587. {
  588. struct mfb_info *mfbi = info->par;
  589. struct fsl_diu_data *data = mfbi->parent;
  590. struct diu __iomem *hw = data->diu_reg;
  591. out_be32(&hw->diu_mode, MFB_MODE1);
  592. }
  593. static void disable_lcdc(struct fb_info *info)
  594. {
  595. struct mfb_info *mfbi = info->par;
  596. struct fsl_diu_data *data = mfbi->parent;
  597. struct diu __iomem *hw = data->diu_reg;
  598. out_be32(&hw->diu_mode, 0);
  599. }
  600. static void adjust_aoi_size_position(struct fb_var_screeninfo *var,
  601. struct fb_info *info)
  602. {
  603. struct mfb_info *lower_aoi_mfbi, *upper_aoi_mfbi, *mfbi = info->par;
  604. struct fsl_diu_data *data = mfbi->parent;
  605. int available_height, upper_aoi_bottom;
  606. enum mfb_index index = mfbi->index;
  607. int lower_aoi_is_open, upper_aoi_is_open;
  608. __u32 base_plane_width, base_plane_height, upper_aoi_height;
  609. base_plane_width = data->fsl_diu_info[0].var.xres;
  610. base_plane_height = data->fsl_diu_info[0].var.yres;
  611. if (mfbi->x_aoi_d < 0)
  612. mfbi->x_aoi_d = 0;
  613. if (mfbi->y_aoi_d < 0)
  614. mfbi->y_aoi_d = 0;
  615. switch (index) {
  616. case PLANE0:
  617. if (mfbi->x_aoi_d != 0)
  618. mfbi->x_aoi_d = 0;
  619. if (mfbi->y_aoi_d != 0)
  620. mfbi->y_aoi_d = 0;
  621. break;
  622. case PLANE1_AOI0:
  623. case PLANE2_AOI0:
  624. lower_aoi_mfbi = data->fsl_diu_info[index+1].par;
  625. lower_aoi_is_open = lower_aoi_mfbi->count > 0 ? 1 : 0;
  626. if (var->xres > base_plane_width)
  627. var->xres = base_plane_width;
  628. if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
  629. mfbi->x_aoi_d = base_plane_width - var->xres;
  630. if (lower_aoi_is_open)
  631. available_height = lower_aoi_mfbi->y_aoi_d;
  632. else
  633. available_height = base_plane_height;
  634. if (var->yres > available_height)
  635. var->yres = available_height;
  636. if ((mfbi->y_aoi_d + var->yres) > available_height)
  637. mfbi->y_aoi_d = available_height - var->yres;
  638. break;
  639. case PLANE1_AOI1:
  640. case PLANE2_AOI1:
  641. upper_aoi_mfbi = data->fsl_diu_info[index-1].par;
  642. upper_aoi_height = data->fsl_diu_info[index-1].var.yres;
  643. upper_aoi_bottom = upper_aoi_mfbi->y_aoi_d + upper_aoi_height;
  644. upper_aoi_is_open = upper_aoi_mfbi->count > 0 ? 1 : 0;
  645. if (var->xres > base_plane_width)
  646. var->xres = base_plane_width;
  647. if ((mfbi->x_aoi_d + var->xres) > base_plane_width)
  648. mfbi->x_aoi_d = base_plane_width - var->xres;
  649. if (mfbi->y_aoi_d < 0)
  650. mfbi->y_aoi_d = 0;
  651. if (upper_aoi_is_open) {
  652. if (mfbi->y_aoi_d < upper_aoi_bottom)
  653. mfbi->y_aoi_d = upper_aoi_bottom;
  654. available_height = base_plane_height
  655. - upper_aoi_bottom;
  656. } else
  657. available_height = base_plane_height;
  658. if (var->yres > available_height)
  659. var->yres = available_height;
  660. if ((mfbi->y_aoi_d + var->yres) > base_plane_height)
  661. mfbi->y_aoi_d = base_plane_height - var->yres;
  662. break;
  663. }
  664. }
  665. /*
  666. * Checks to see if the hardware supports the state requested by var passed
  667. * in. This function does not alter the hardware state! If the var passed in
  668. * is slightly off by what the hardware can support then we alter the var
  669. * PASSED in to what we can do. If the hardware doesn't support mode change
  670. * a -EINVAL will be returned by the upper layers.
  671. */
  672. static int fsl_diu_check_var(struct fb_var_screeninfo *var,
  673. struct fb_info *info)
  674. {
  675. if (var->xres_virtual < var->xres)
  676. var->xres_virtual = var->xres;
  677. if (var->yres_virtual < var->yres)
  678. var->yres_virtual = var->yres;
  679. if (var->xoffset + info->var.xres > info->var.xres_virtual)
  680. var->xoffset = info->var.xres_virtual - info->var.xres;
  681. if (var->yoffset + info->var.yres > info->var.yres_virtual)
  682. var->yoffset = info->var.yres_virtual - info->var.yres;
  683. if ((var->bits_per_pixel != 32) && (var->bits_per_pixel != 24) &&
  684. (var->bits_per_pixel != 16))
  685. var->bits_per_pixel = default_bpp;
  686. switch (var->bits_per_pixel) {
  687. case 16:
  688. var->red.length = 5;
  689. var->red.offset = 11;
  690. var->red.msb_right = 0;
  691. var->green.length = 6;
  692. var->green.offset = 5;
  693. var->green.msb_right = 0;
  694. var->blue.length = 5;
  695. var->blue.offset = 0;
  696. var->blue.msb_right = 0;
  697. var->transp.length = 0;
  698. var->transp.offset = 0;
  699. var->transp.msb_right = 0;
  700. break;
  701. case 24:
  702. var->red.length = 8;
  703. var->red.offset = 0;
  704. var->red.msb_right = 0;
  705. var->green.length = 8;
  706. var->green.offset = 8;
  707. var->green.msb_right = 0;
  708. var->blue.length = 8;
  709. var->blue.offset = 16;
  710. var->blue.msb_right = 0;
  711. var->transp.length = 0;
  712. var->transp.offset = 0;
  713. var->transp.msb_right = 0;
  714. break;
  715. case 32:
  716. var->red.length = 8;
  717. var->red.offset = 16;
  718. var->red.msb_right = 0;
  719. var->green.length = 8;
  720. var->green.offset = 8;
  721. var->green.msb_right = 0;
  722. var->blue.length = 8;
  723. var->blue.offset = 0;
  724. var->blue.msb_right = 0;
  725. var->transp.length = 8;
  726. var->transp.offset = 24;
  727. var->transp.msb_right = 0;
  728. break;
  729. }
  730. var->height = -1;
  731. var->width = -1;
  732. var->grayscale = 0;
  733. /* Copy nonstd field to/from sync for fbset usage */
  734. var->sync |= var->nonstd;
  735. var->nonstd |= var->sync;
  736. adjust_aoi_size_position(var, info);
  737. return 0;
  738. }
  739. static void set_fix(struct fb_info *info)
  740. {
  741. struct fb_fix_screeninfo *fix = &info->fix;
  742. struct fb_var_screeninfo *var = &info->var;
  743. struct mfb_info *mfbi = info->par;
  744. strncpy(fix->id, mfbi->id, sizeof(fix->id));
  745. fix->line_length = var->xres_virtual * var->bits_per_pixel / 8;
  746. fix->type = FB_TYPE_PACKED_PIXELS;
  747. fix->accel = FB_ACCEL_NONE;
  748. fix->visual = FB_VISUAL_TRUECOLOR;
  749. fix->xpanstep = 1;
  750. fix->ypanstep = 1;
  751. }
  752. static void update_lcdc(struct fb_info *info)
  753. {
  754. struct fb_var_screeninfo *var = &info->var;
  755. struct mfb_info *mfbi = info->par;
  756. struct fsl_diu_data *data = mfbi->parent;
  757. struct diu __iomem *hw;
  758. int i, j;
  759. u8 *gamma_table_base;
  760. u32 temp;
  761. hw = data->diu_reg;
  762. if (diu_ops.set_monitor_port)
  763. diu_ops.set_monitor_port(data->monitor_port);
  764. gamma_table_base = data->gamma;
  765. /* Prep for DIU init - gamma table, cursor table */
  766. for (i = 0; i <= 2; i++)
  767. for (j = 0; j <= 255; j++)
  768. *gamma_table_base++ = j;
  769. if (diu_ops.set_gamma_table)
  770. diu_ops.set_gamma_table(data->monitor_port, data->gamma);
  771. disable_lcdc(info);
  772. /* Program DIU registers */
  773. out_be32(&hw->gamma, DMA_ADDR(data, gamma));
  774. out_be32(&hw->bgnd, 0x007F7F7F); /* Set background to grey */
  775. out_be32(&hw->disp_size, (var->yres << 16) | var->xres);
  776. /* Horizontal and vertical configuration register */
  777. temp = var->left_margin << 22 | /* BP_H */
  778. var->hsync_len << 11 | /* PW_H */
  779. var->right_margin; /* FP_H */
  780. out_be32(&hw->hsyn_para, temp);
  781. temp = var->upper_margin << 22 | /* BP_V */
  782. var->vsync_len << 11 | /* PW_V */
  783. var->lower_margin; /* FP_V */
  784. out_be32(&hw->vsyn_para, temp);
  785. diu_ops.set_pixel_clock(var->pixclock);
  786. #ifndef CONFIG_PPC_MPC512x
  787. /*
  788. * The PLUT register is defined differently on the MPC5121 than it
  789. * is on other SOCs. Unfortunately, there's no documentation that
  790. * explains how it's supposed to be programmed, so for now, we leave
  791. * it at the default value on the MPC5121.
  792. *
  793. * For other SOCs, program it for the highest priority, which will
  794. * reduce the chance of underrun. Technically, we should scale the
  795. * priority to match the screen resolution, but doing that properly
  796. * requires delicate fine-tuning for each use-case.
  797. */
  798. out_be32(&hw->plut, 0x01F5F666);
  799. #endif
  800. /* Enable the DIU */
  801. enable_lcdc(info);
  802. }
  803. static int map_video_memory(struct fb_info *info)
  804. {
  805. u32 smem_len = info->fix.line_length * info->var.yres_virtual;
  806. void *p;
  807. p = alloc_pages_exact(smem_len, GFP_DMA | __GFP_ZERO);
  808. if (!p) {
  809. dev_err(info->dev, "unable to allocate fb memory\n");
  810. return -ENOMEM;
  811. }
  812. mutex_lock(&info->mm_lock);
  813. info->screen_base = p;
  814. info->fix.smem_start = virt_to_phys(info->screen_base);
  815. info->fix.smem_len = smem_len;
  816. mutex_unlock(&info->mm_lock);
  817. info->screen_size = info->fix.smem_len;
  818. return 0;
  819. }
  820. static void unmap_video_memory(struct fb_info *info)
  821. {
  822. void *p = info->screen_base;
  823. size_t l = info->fix.smem_len;
  824. mutex_lock(&info->mm_lock);
  825. info->screen_base = NULL;
  826. info->fix.smem_start = 0;
  827. info->fix.smem_len = 0;
  828. mutex_unlock(&info->mm_lock);
  829. if (p)
  830. free_pages_exact(p, l);
  831. }
  832. /*
  833. * Using the fb_var_screeninfo in fb_info we set the aoi of this
  834. * particular framebuffer. It is a light version of fsl_diu_set_par.
  835. */
  836. static int fsl_diu_set_aoi(struct fb_info *info)
  837. {
  838. struct fb_var_screeninfo *var = &info->var;
  839. struct mfb_info *mfbi = info->par;
  840. struct diu_ad *ad = mfbi->ad;
  841. /* AOI should not be greater than display size */
  842. ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
  843. ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
  844. return 0;
  845. }
  846. /**
  847. * fsl_diu_get_pixel_format: return the pixel format for a given color depth
  848. *
  849. * The pixel format is a 32-bit value that determine which bits in each
  850. * pixel are to be used for each color. This is the default function used
  851. * if the platform does not define its own version.
  852. */
  853. static u32 fsl_diu_get_pixel_format(unsigned int bits_per_pixel)
  854. {
  855. #define PF_BYTE_F 0x10000000
  856. #define PF_ALPHA_C_MASK 0x0E000000
  857. #define PF_ALPHA_C_SHIFT 25
  858. #define PF_BLUE_C_MASK 0x01800000
  859. #define PF_BLUE_C_SHIFT 23
  860. #define PF_GREEN_C_MASK 0x00600000
  861. #define PF_GREEN_C_SHIFT 21
  862. #define PF_RED_C_MASK 0x00180000
  863. #define PF_RED_C_SHIFT 19
  864. #define PF_PALETTE 0x00040000
  865. #define PF_PIXEL_S_MASK 0x00030000
  866. #define PF_PIXEL_S_SHIFT 16
  867. #define PF_COMP_3_MASK 0x0000F000
  868. #define PF_COMP_3_SHIFT 12
  869. #define PF_COMP_2_MASK 0x00000F00
  870. #define PF_COMP_2_SHIFT 8
  871. #define PF_COMP_1_MASK 0x000000F0
  872. #define PF_COMP_1_SHIFT 4
  873. #define PF_COMP_0_MASK 0x0000000F
  874. #define PF_COMP_0_SHIFT 0
  875. #define MAKE_PF(alpha, red, green, blue, size, c0, c1, c2, c3) \
  876. cpu_to_le32(PF_BYTE_F | (alpha << PF_ALPHA_C_SHIFT) | \
  877. (blue << PF_BLUE_C_SHIFT) | (green << PF_GREEN_C_SHIFT) | \
  878. (red << PF_RED_C_SHIFT) | (c3 << PF_COMP_3_SHIFT) | \
  879. (c2 << PF_COMP_2_SHIFT) | (c1 << PF_COMP_1_SHIFT) | \
  880. (c0 << PF_COMP_0_SHIFT) | (size << PF_PIXEL_S_SHIFT))
  881. switch (bits_per_pixel) {
  882. case 32:
  883. /* 0x88883316 */
  884. return MAKE_PF(3, 2, 1, 0, 3, 8, 8, 8, 8);
  885. case 24:
  886. /* 0x88082219 */
  887. return MAKE_PF(4, 0, 1, 2, 2, 8, 8, 8, 0);
  888. case 16:
  889. /* 0x65053118 */
  890. return MAKE_PF(4, 2, 1, 0, 1, 5, 6, 5, 0);
  891. default:
  892. pr_err("fsl-diu: unsupported color depth %u\n", bits_per_pixel);
  893. return 0;
  894. }
  895. }
  896. /*
  897. * Copies a cursor image from user space to the proper place in driver
  898. * memory so that the hardware can display the cursor image.
  899. *
  900. * Cursor data is represented as a sequence of 'width' bits packed into bytes.
  901. * That is, the first 8 bits are in the first byte, the second 8 bits in the
  902. * second byte, and so on. Therefore, the each row of the cursor is (width +
  903. * 7) / 8 bytes of 'data'
  904. *
  905. * The DIU only supports cursors up to 32x32 (MAX_CURS). We reject cursors
  906. * larger than this, so we already know that 'width' <= 32. Therefore, we can
  907. * simplify our code by using a 32-bit big-endian integer ("line") to read in
  908. * a single line of pixels, and only look at the top 'width' bits of that
  909. * integer.
  910. *
  911. * This could result in an unaligned 32-bit read. For example, if the cursor
  912. * is 24x24, then the first three bytes of 'image' contain the pixel data for
  913. * the top line of the cursor. We do a 32-bit read of 'image', but we look
  914. * only at the top 24 bits. Then we increment 'image' by 3 bytes. The next
  915. * read is unaligned. The only problem is that we might read past the end of
  916. * 'image' by 1-3 bytes, but that should not cause any problems.
  917. */
  918. static void fsl_diu_load_cursor_image(struct fb_info *info,
  919. const void *image, uint16_t bg, uint16_t fg,
  920. unsigned int width, unsigned int height)
  921. {
  922. struct mfb_info *mfbi = info->par;
  923. struct fsl_diu_data *data = mfbi->parent;
  924. __le16 *cursor = data->cursor;
  925. __le16 _fg = cpu_to_le16(fg);
  926. __le16 _bg = cpu_to_le16(bg);
  927. unsigned int h, w;
  928. for (h = 0; h < height; h++) {
  929. uint32_t mask = 1 << 31;
  930. uint32_t line = be32_to_cpup(image);
  931. for (w = 0; w < width; w++) {
  932. cursor[w] = (line & mask) ? _fg : _bg;
  933. mask >>= 1;
  934. }
  935. cursor += MAX_CURS;
  936. image += DIV_ROUND_UP(width, 8);
  937. }
  938. }
  939. /*
  940. * Set a hardware cursor. The image data for the cursor is passed via the
  941. * fb_cursor object.
  942. */
  943. static int fsl_diu_cursor(struct fb_info *info, struct fb_cursor *cursor)
  944. {
  945. struct mfb_info *mfbi = info->par;
  946. struct fsl_diu_data *data = mfbi->parent;
  947. struct diu __iomem *hw = data->diu_reg;
  948. if (cursor->image.width > MAX_CURS || cursor->image.height > MAX_CURS)
  949. return -EINVAL;
  950. /* The cursor size has changed */
  951. if (cursor->set & FB_CUR_SETSIZE) {
  952. /*
  953. * The DIU cursor is a fixed size, so when we get this
  954. * message, instead of resizing the cursor, we just clear
  955. * all the image data, in expectation of new data. However,
  956. * in tests this control does not appear to be normally
  957. * called.
  958. */
  959. memset(data->cursor, 0, sizeof(data->cursor));
  960. }
  961. /* The cursor position has changed (cursor->image.dx|dy) */
  962. if (cursor->set & FB_CUR_SETPOS) {
  963. uint32_t xx, yy;
  964. yy = (cursor->image.dy - info->var.yoffset) & 0x7ff;
  965. xx = (cursor->image.dx - info->var.xoffset) & 0x7ff;
  966. out_be32(&hw->curs_pos, yy << 16 | xx);
  967. }
  968. /*
  969. * FB_CUR_SETIMAGE - the cursor image has changed
  970. * FB_CUR_SETCMAP - the cursor colors has changed
  971. * FB_CUR_SETSHAPE - the cursor bitmask has changed
  972. */
  973. if (cursor->set & (FB_CUR_SETSHAPE | FB_CUR_SETCMAP | FB_CUR_SETIMAGE)) {
  974. /*
  975. * Determine the size of the cursor image data. Normally,
  976. * it's 8x16.
  977. */
  978. unsigned int image_size =
  979. DIV_ROUND_UP(cursor->image.width, 8) *
  980. cursor->image.height;
  981. unsigned int image_words =
  982. DIV_ROUND_UP(image_size, sizeof(uint32_t));
  983. unsigned int bg_idx = cursor->image.bg_color;
  984. unsigned int fg_idx = cursor->image.fg_color;
  985. uint32_t *image, *source, *mask;
  986. uint16_t fg, bg;
  987. unsigned int i;
  988. if (info->state != FBINFO_STATE_RUNNING)
  989. return 0;
  990. bg = ((info->cmap.red[bg_idx] & 0xf8) << 7) |
  991. ((info->cmap.green[bg_idx] & 0xf8) << 2) |
  992. ((info->cmap.blue[bg_idx] & 0xf8) >> 3) |
  993. 1 << 15;
  994. fg = ((info->cmap.red[fg_idx] & 0xf8) << 7) |
  995. ((info->cmap.green[fg_idx] & 0xf8) << 2) |
  996. ((info->cmap.blue[fg_idx] & 0xf8) >> 3) |
  997. 1 << 15;
  998. /* Use 32-bit operations on the data to improve performance */
  999. image = (uint32_t *)data->next_cursor;
  1000. source = (uint32_t *)cursor->image.data;
  1001. mask = (uint32_t *)cursor->mask;
  1002. if (cursor->rop == ROP_XOR)
  1003. for (i = 0; i < image_words; i++)
  1004. image[i] = source[i] ^ mask[i];
  1005. else
  1006. for (i = 0; i < image_words; i++)
  1007. image[i] = source[i] & mask[i];
  1008. fsl_diu_load_cursor_image(info, image, bg, fg,
  1009. cursor->image.width, cursor->image.height);
  1010. }
  1011. /*
  1012. * Show or hide the cursor. The cursor data is always stored in the
  1013. * 'cursor' memory block, and the actual cursor position is always in
  1014. * the DIU's CURS_POS register. To hide the cursor, we redirect the
  1015. * CURSOR register to a blank cursor. The show the cursor, we
  1016. * redirect the CURSOR register to the real cursor data.
  1017. */
  1018. if (cursor->enable)
  1019. out_be32(&hw->cursor, DMA_ADDR(data, cursor));
  1020. else
  1021. out_be32(&hw->cursor, DMA_ADDR(data, blank_cursor));
  1022. return 0;
  1023. }
  1024. /*
  1025. * Using the fb_var_screeninfo in fb_info we set the resolution of this
  1026. * particular framebuffer. This function alters the fb_fix_screeninfo stored
  1027. * in fb_info. It does not alter var in fb_info since we are using that
  1028. * data. This means we depend on the data in var inside fb_info to be
  1029. * supported by the hardware. fsl_diu_check_var is always called before
  1030. * fsl_diu_set_par to ensure this.
  1031. */
  1032. static int fsl_diu_set_par(struct fb_info *info)
  1033. {
  1034. unsigned long len;
  1035. struct fb_var_screeninfo *var = &info->var;
  1036. struct mfb_info *mfbi = info->par;
  1037. struct fsl_diu_data *data = mfbi->parent;
  1038. struct diu_ad *ad = mfbi->ad;
  1039. struct diu __iomem *hw;
  1040. hw = data->diu_reg;
  1041. set_fix(info);
  1042. len = info->var.yres_virtual * info->fix.line_length;
  1043. /* Alloc & dealloc each time resolution/bpp change */
  1044. if (len != info->fix.smem_len) {
  1045. if (info->fix.smem_start)
  1046. unmap_video_memory(info);
  1047. /* Memory allocation for framebuffer */
  1048. if (map_video_memory(info)) {
  1049. dev_err(info->dev, "unable to allocate fb memory 1\n");
  1050. return -ENOMEM;
  1051. }
  1052. }
  1053. if (diu_ops.get_pixel_format)
  1054. ad->pix_fmt = diu_ops.get_pixel_format(data->monitor_port,
  1055. var->bits_per_pixel);
  1056. else
  1057. ad->pix_fmt = fsl_diu_get_pixel_format(var->bits_per_pixel);
  1058. ad->addr = cpu_to_le32(info->fix.smem_start);
  1059. ad->src_size_g_alpha = cpu_to_le32((var->yres_virtual << 12) |
  1060. var->xres_virtual) | mfbi->g_alpha;
  1061. /* AOI should not be greater than display size */
  1062. ad->aoi_size = cpu_to_le32((var->yres << 16) | var->xres);
  1063. ad->offset_xyi = cpu_to_le32((var->yoffset << 16) | var->xoffset);
  1064. ad->offset_xyd = cpu_to_le32((mfbi->y_aoi_d << 16) | mfbi->x_aoi_d);
  1065. /* Disable chroma keying function */
  1066. ad->ckmax_r = 0;
  1067. ad->ckmax_g = 0;
  1068. ad->ckmax_b = 0;
  1069. ad->ckmin_r = 255;
  1070. ad->ckmin_g = 255;
  1071. ad->ckmin_b = 255;
  1072. if (mfbi->index == PLANE0)
  1073. update_lcdc(info);
  1074. return 0;
  1075. }
  1076. static inline __u32 CNVT_TOHW(__u32 val, __u32 width)
  1077. {
  1078. return ((val << width) + 0x7FFF - val) >> 16;
  1079. }
  1080. /*
  1081. * Set a single color register. The values supplied have a 16 bit magnitude
  1082. * which needs to be scaled in this function for the hardware. Things to take
  1083. * into consideration are how many color registers, if any, are supported with
  1084. * the current color visual. With truecolor mode no color palettes are
  1085. * supported. Here a pseudo palette is created which we store the value in
  1086. * pseudo_palette in struct fb_info. For pseudocolor mode we have a limited
  1087. * color palette.
  1088. */
  1089. static int fsl_diu_setcolreg(unsigned int regno, unsigned int red,
  1090. unsigned int green, unsigned int blue,
  1091. unsigned int transp, struct fb_info *info)
  1092. {
  1093. int ret = 1;
  1094. /*
  1095. * If greyscale is true, then we convert the RGB value
  1096. * to greyscale no matter what visual we are using.
  1097. */
  1098. if (info->var.grayscale)
  1099. red = green = blue = (19595 * red + 38470 * green +
  1100. 7471 * blue) >> 16;
  1101. switch (info->fix.visual) {
  1102. case FB_VISUAL_TRUECOLOR:
  1103. /*
  1104. * 16-bit True Colour. We encode the RGB value
  1105. * according to the RGB bitfield information.
  1106. */
  1107. if (regno < 16) {
  1108. u32 *pal = info->pseudo_palette;
  1109. u32 v;
  1110. red = CNVT_TOHW(red, info->var.red.length);
  1111. green = CNVT_TOHW(green, info->var.green.length);
  1112. blue = CNVT_TOHW(blue, info->var.blue.length);
  1113. transp = CNVT_TOHW(transp, info->var.transp.length);
  1114. v = (red << info->var.red.offset) |
  1115. (green << info->var.green.offset) |
  1116. (blue << info->var.blue.offset) |
  1117. (transp << info->var.transp.offset);
  1118. pal[regno] = v;
  1119. ret = 0;
  1120. }
  1121. break;
  1122. }
  1123. return ret;
  1124. }
  1125. /*
  1126. * Pan (or wrap, depending on the `vmode' field) the display using the
  1127. * 'xoffset' and 'yoffset' fields of the 'var' structure. If the values
  1128. * don't fit, return -EINVAL.
  1129. */
  1130. static int fsl_diu_pan_display(struct fb_var_screeninfo *var,
  1131. struct fb_info *info)
  1132. {
  1133. if ((info->var.xoffset == var->xoffset) &&
  1134. (info->var.yoffset == var->yoffset))
  1135. return 0; /* No change, do nothing */
  1136. if (var->xoffset + info->var.xres > info->var.xres_virtual
  1137. || var->yoffset + info->var.yres > info->var.yres_virtual)
  1138. return -EINVAL;
  1139. info->var.xoffset = var->xoffset;
  1140. info->var.yoffset = var->yoffset;
  1141. if (var->vmode & FB_VMODE_YWRAP)
  1142. info->var.vmode |= FB_VMODE_YWRAP;
  1143. else
  1144. info->var.vmode &= ~FB_VMODE_YWRAP;
  1145. fsl_diu_set_aoi(info);
  1146. return 0;
  1147. }
  1148. static int fsl_diu_ioctl(struct fb_info *info, unsigned int cmd,
  1149. unsigned long arg)
  1150. {
  1151. struct mfb_info *mfbi = info->par;
  1152. struct diu_ad *ad = mfbi->ad;
  1153. struct mfb_chroma_key ck;
  1154. unsigned char global_alpha;
  1155. struct aoi_display_offset aoi_d;
  1156. __u32 pix_fmt;
  1157. void __user *buf = (void __user *)arg;
  1158. if (!arg)
  1159. return -EINVAL;
  1160. dev_dbg(info->dev, "ioctl %08x (dir=%s%s type=%u nr=%u size=%u)\n", cmd,
  1161. _IOC_DIR(cmd) & _IOC_READ ? "R" : "",
  1162. _IOC_DIR(cmd) & _IOC_WRITE ? "W" : "",
  1163. _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
  1164. switch (cmd) {
  1165. case MFB_SET_PIXFMT_OLD:
  1166. dev_warn(info->dev,
  1167. "MFB_SET_PIXFMT value of 0x%08x is deprecated.\n",
  1168. MFB_SET_PIXFMT_OLD);
  1169. case MFB_SET_PIXFMT:
  1170. if (copy_from_user(&pix_fmt, buf, sizeof(pix_fmt)))
  1171. return -EFAULT;
  1172. ad->pix_fmt = pix_fmt;
  1173. break;
  1174. case MFB_GET_PIXFMT_OLD:
  1175. dev_warn(info->dev,
  1176. "MFB_GET_PIXFMT value of 0x%08x is deprecated.\n",
  1177. MFB_GET_PIXFMT_OLD);
  1178. case MFB_GET_PIXFMT:
  1179. pix_fmt = ad->pix_fmt;
  1180. if (copy_to_user(buf, &pix_fmt, sizeof(pix_fmt)))
  1181. return -EFAULT;
  1182. break;
  1183. case MFB_SET_AOID:
  1184. if (copy_from_user(&aoi_d, buf, sizeof(aoi_d)))
  1185. return -EFAULT;
  1186. mfbi->x_aoi_d = aoi_d.x_aoi_d;
  1187. mfbi->y_aoi_d = aoi_d.y_aoi_d;
  1188. fsl_diu_check_var(&info->var, info);
  1189. fsl_diu_set_aoi(info);
  1190. break;
  1191. case MFB_GET_AOID:
  1192. aoi_d.x_aoi_d = mfbi->x_aoi_d;
  1193. aoi_d.y_aoi_d = mfbi->y_aoi_d;
  1194. if (copy_to_user(buf, &aoi_d, sizeof(aoi_d)))
  1195. return -EFAULT;
  1196. break;
  1197. case MFB_GET_ALPHA:
  1198. global_alpha = mfbi->g_alpha;
  1199. if (copy_to_user(buf, &global_alpha, sizeof(global_alpha)))
  1200. return -EFAULT;
  1201. break;
  1202. case MFB_SET_ALPHA:
  1203. /* set panel information */
  1204. if (copy_from_user(&global_alpha, buf, sizeof(global_alpha)))
  1205. return -EFAULT;
  1206. ad->src_size_g_alpha = (ad->src_size_g_alpha & (~0xff)) |
  1207. (global_alpha & 0xff);
  1208. mfbi->g_alpha = global_alpha;
  1209. break;
  1210. case MFB_SET_CHROMA_KEY:
  1211. /* set panel winformation */
  1212. if (copy_from_user(&ck, buf, sizeof(ck)))
  1213. return -EFAULT;
  1214. if (ck.enable &&
  1215. (ck.red_max < ck.red_min ||
  1216. ck.green_max < ck.green_min ||
  1217. ck.blue_max < ck.blue_min))
  1218. return -EINVAL;
  1219. if (!ck.enable) {
  1220. ad->ckmax_r = 0;
  1221. ad->ckmax_g = 0;
  1222. ad->ckmax_b = 0;
  1223. ad->ckmin_r = 255;
  1224. ad->ckmin_g = 255;
  1225. ad->ckmin_b = 255;
  1226. } else {
  1227. ad->ckmax_r = ck.red_max;
  1228. ad->ckmax_g = ck.green_max;
  1229. ad->ckmax_b = ck.blue_max;
  1230. ad->ckmin_r = ck.red_min;
  1231. ad->ckmin_g = ck.green_min;
  1232. ad->ckmin_b = ck.blue_min;
  1233. }
  1234. break;
  1235. #ifdef CONFIG_PPC_MPC512x
  1236. case MFB_SET_GAMMA: {
  1237. struct fsl_diu_data *data = mfbi->parent;
  1238. if (copy_from_user(data->gamma, buf, sizeof(data->gamma)))
  1239. return -EFAULT;
  1240. setbits32(&data->diu_reg->gamma, 0); /* Force table reload */
  1241. break;
  1242. }
  1243. case MFB_GET_GAMMA: {
  1244. struct fsl_diu_data *data = mfbi->parent;
  1245. if (copy_to_user(buf, data->gamma, sizeof(data->gamma)))
  1246. return -EFAULT;
  1247. break;
  1248. }
  1249. #endif
  1250. default:
  1251. dev_err(info->dev, "unknown ioctl command (0x%08X)\n", cmd);
  1252. return -ENOIOCTLCMD;
  1253. }
  1254. return 0;
  1255. }
  1256. static inline void fsl_diu_enable_interrupts(struct fsl_diu_data *data)
  1257. {
  1258. u32 int_mask = INT_UNDRUN; /* enable underrun detection */
  1259. if (IS_ENABLED(CONFIG_NOT_COHERENT_CACHE))
  1260. int_mask |= INT_VSYNC; /* enable vertical sync */
  1261. clrbits32(&data->diu_reg->int_mask, int_mask);
  1262. }
  1263. /* turn on fb if count == 1
  1264. */
  1265. static int fsl_diu_open(struct fb_info *info, int user)
  1266. {
  1267. struct mfb_info *mfbi = info->par;
  1268. int res = 0;
  1269. /* free boot splash memory on first /dev/fb0 open */
  1270. if ((mfbi->index == PLANE0) && diu_ops.release_bootmem)
  1271. diu_ops.release_bootmem();
  1272. spin_lock(&diu_lock);
  1273. mfbi->count++;
  1274. if (mfbi->count == 1) {
  1275. fsl_diu_check_var(&info->var, info);
  1276. res = fsl_diu_set_par(info);
  1277. if (res < 0)
  1278. mfbi->count--;
  1279. else {
  1280. fsl_diu_enable_interrupts(mfbi->parent);
  1281. fsl_diu_enable_panel(info);
  1282. }
  1283. }
  1284. spin_unlock(&diu_lock);
  1285. return res;
  1286. }
  1287. /* turn off fb if count == 0
  1288. */
  1289. static int fsl_diu_release(struct fb_info *info, int user)
  1290. {
  1291. struct mfb_info *mfbi = info->par;
  1292. int res = 0;
  1293. spin_lock(&diu_lock);
  1294. mfbi->count--;
  1295. if (mfbi->count == 0) {
  1296. struct fsl_diu_data *data = mfbi->parent;
  1297. bool disable = true;
  1298. int i;
  1299. /* Disable interrupts only if all AOIs are closed */
  1300. for (i = 0; i < NUM_AOIS; i++) {
  1301. struct mfb_info *mi = data->fsl_diu_info[i].par;
  1302. if (mi->count)
  1303. disable = false;
  1304. }
  1305. if (disable)
  1306. out_be32(&data->diu_reg->int_mask, 0xffffffff);
  1307. fsl_diu_disable_panel(info);
  1308. }
  1309. spin_unlock(&diu_lock);
  1310. return res;
  1311. }
  1312. static struct fb_ops fsl_diu_ops = {
  1313. .owner = THIS_MODULE,
  1314. .fb_check_var = fsl_diu_check_var,
  1315. .fb_set_par = fsl_diu_set_par,
  1316. .fb_setcolreg = fsl_diu_setcolreg,
  1317. .fb_pan_display = fsl_diu_pan_display,
  1318. .fb_fillrect = cfb_fillrect,
  1319. .fb_copyarea = cfb_copyarea,
  1320. .fb_imageblit = cfb_imageblit,
  1321. .fb_ioctl = fsl_diu_ioctl,
  1322. .fb_open = fsl_diu_open,
  1323. .fb_release = fsl_diu_release,
  1324. .fb_cursor = fsl_diu_cursor,
  1325. };
  1326. static int install_fb(struct fb_info *info)
  1327. {
  1328. int rc;
  1329. struct mfb_info *mfbi = info->par;
  1330. struct fsl_diu_data *data = mfbi->parent;
  1331. const char *aoi_mode, *init_aoi_mode = "320x240";
  1332. struct fb_videomode *db = fsl_diu_mode_db;
  1333. unsigned int dbsize = ARRAY_SIZE(fsl_diu_mode_db);
  1334. int has_default_mode = 1;
  1335. info->var.activate = FB_ACTIVATE_NOW;
  1336. info->fbops = &fsl_diu_ops;
  1337. info->flags = FBINFO_DEFAULT | FBINFO_VIRTFB | FBINFO_PARTIAL_PAN_OK |
  1338. FBINFO_READS_FAST;
  1339. info->pseudo_palette = mfbi->pseudo_palette;
  1340. rc = fb_alloc_cmap(&info->cmap, 16, 0);
  1341. if (rc)
  1342. return rc;
  1343. if (mfbi->index == PLANE0) {
  1344. if (data->has_edid) {
  1345. /* Now build modedb from EDID */
  1346. fb_edid_to_monspecs(data->edid_data, &info->monspecs);
  1347. fb_videomode_to_modelist(info->monspecs.modedb,
  1348. info->monspecs.modedb_len,
  1349. &info->modelist);
  1350. db = info->monspecs.modedb;
  1351. dbsize = info->monspecs.modedb_len;
  1352. }
  1353. aoi_mode = fb_mode;
  1354. } else {
  1355. aoi_mode = init_aoi_mode;
  1356. }
  1357. rc = fb_find_mode(&info->var, info, aoi_mode, db, dbsize, NULL,
  1358. default_bpp);
  1359. if (!rc) {
  1360. /*
  1361. * For plane 0 we continue and look into
  1362. * driver's internal modedb.
  1363. */
  1364. if ((mfbi->index == PLANE0) && data->has_edid)
  1365. has_default_mode = 0;
  1366. else
  1367. return -EINVAL;
  1368. }
  1369. if (!has_default_mode) {
  1370. rc = fb_find_mode(&info->var, info, aoi_mode, fsl_diu_mode_db,
  1371. ARRAY_SIZE(fsl_diu_mode_db), NULL, default_bpp);
  1372. if (rc)
  1373. has_default_mode = 1;
  1374. }
  1375. /* Still not found, use preferred mode from database if any */
  1376. if (!has_default_mode && info->monspecs.modedb) {
  1377. struct fb_monspecs *specs = &info->monspecs;
  1378. struct fb_videomode *modedb = &specs->modedb[0];
  1379. /*
  1380. * Get preferred timing. If not found,
  1381. * first mode in database will be used.
  1382. */
  1383. if (specs->misc & FB_MISC_1ST_DETAIL) {
  1384. int i;
  1385. for (i = 0; i < specs->modedb_len; i++) {
  1386. if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
  1387. modedb = &specs->modedb[i];
  1388. break;
  1389. }
  1390. }
  1391. }
  1392. info->var.bits_per_pixel = default_bpp;
  1393. fb_videomode_to_var(&info->var, modedb);
  1394. }
  1395. if (fsl_diu_check_var(&info->var, info)) {
  1396. dev_err(info->dev, "fsl_diu_check_var failed\n");
  1397. unmap_video_memory(info);
  1398. fb_dealloc_cmap(&info->cmap);
  1399. return -EINVAL;
  1400. }
  1401. if (register_framebuffer(info) < 0) {
  1402. dev_err(info->dev, "register_framebuffer failed\n");
  1403. unmap_video_memory(info);
  1404. fb_dealloc_cmap(&info->cmap);
  1405. return -EINVAL;
  1406. }
  1407. mfbi->registered = 1;
  1408. dev_info(info->dev, "%s registered successfully\n", mfbi->id);
  1409. return 0;
  1410. }
  1411. static void uninstall_fb(struct fb_info *info)
  1412. {
  1413. struct mfb_info *mfbi = info->par;
  1414. if (!mfbi->registered)
  1415. return;
  1416. unregister_framebuffer(info);
  1417. unmap_video_memory(info);
  1418. if (&info->cmap)
  1419. fb_dealloc_cmap(&info->cmap);
  1420. mfbi->registered = 0;
  1421. }
  1422. static irqreturn_t fsl_diu_isr(int irq, void *dev_id)
  1423. {
  1424. struct diu __iomem *hw = dev_id;
  1425. uint32_t status = in_be32(&hw->int_status);
  1426. if (status) {
  1427. /* This is the workaround for underrun */
  1428. if (status & INT_UNDRUN) {
  1429. out_be32(&hw->diu_mode, 0);
  1430. udelay(1);
  1431. out_be32(&hw->diu_mode, 1);
  1432. }
  1433. #if defined(CONFIG_NOT_COHERENT_CACHE)
  1434. else if (status & INT_VSYNC) {
  1435. unsigned int i;
  1436. for (i = 0; i < coherence_data_size;
  1437. i += d_cache_line_size)
  1438. __asm__ __volatile__ (
  1439. "dcbz 0, %[input]"
  1440. ::[input]"r"(&coherence_data[i]));
  1441. }
  1442. #endif
  1443. return IRQ_HANDLED;
  1444. }
  1445. return IRQ_NONE;
  1446. }
  1447. #ifdef CONFIG_PM
  1448. /*
  1449. * Power management hooks. Note that we won't be called from IRQ context,
  1450. * unlike the blank functions above, so we may sleep.
  1451. */
  1452. static int fsl_diu_suspend(struct platform_device *ofdev, pm_message_t state)
  1453. {
  1454. struct fsl_diu_data *data;
  1455. data = dev_get_drvdata(&ofdev->dev);
  1456. disable_lcdc(data->fsl_diu_info);
  1457. return 0;
  1458. }
  1459. static int fsl_diu_resume(struct platform_device *ofdev)
  1460. {
  1461. struct fsl_diu_data *data;
  1462. unsigned int i;
  1463. data = dev_get_drvdata(&ofdev->dev);
  1464. fsl_diu_enable_interrupts(data);
  1465. update_lcdc(data->fsl_diu_info);
  1466. for (i = 0; i < NUM_AOIS; i++) {
  1467. if (data->mfb[i].count)
  1468. fsl_diu_enable_panel(&data->fsl_diu_info[i]);
  1469. }
  1470. return 0;
  1471. }
  1472. #else
  1473. #define fsl_diu_suspend NULL
  1474. #define fsl_diu_resume NULL
  1475. #endif /* CONFIG_PM */
  1476. static ssize_t store_monitor(struct device *device,
  1477. struct device_attribute *attr, const char *buf, size_t count)
  1478. {
  1479. enum fsl_diu_monitor_port old_monitor_port;
  1480. struct fsl_diu_data *data =
  1481. container_of(attr, struct fsl_diu_data, dev_attr);
  1482. old_monitor_port = data->monitor_port;
  1483. data->monitor_port = fsl_diu_name_to_port(buf);
  1484. if (old_monitor_port != data->monitor_port) {
  1485. /* All AOIs need adjust pixel format
  1486. * fsl_diu_set_par only change the pixsel format here
  1487. * unlikely to fail. */
  1488. unsigned int i;
  1489. for (i=0; i < NUM_AOIS; i++)
  1490. fsl_diu_set_par(&data->fsl_diu_info[i]);
  1491. }
  1492. return count;
  1493. }
  1494. static ssize_t show_monitor(struct device *device,
  1495. struct device_attribute *attr, char *buf)
  1496. {
  1497. struct fsl_diu_data *data =
  1498. container_of(attr, struct fsl_diu_data, dev_attr);
  1499. switch (data->monitor_port) {
  1500. case FSL_DIU_PORT_DVI:
  1501. return sprintf(buf, "DVI\n");
  1502. case FSL_DIU_PORT_LVDS:
  1503. return sprintf(buf, "Single-link LVDS\n");
  1504. case FSL_DIU_PORT_DLVDS:
  1505. return sprintf(buf, "Dual-link LVDS\n");
  1506. }
  1507. return 0;
  1508. }
  1509. static int fsl_diu_probe(struct platform_device *pdev)
  1510. {
  1511. struct device_node *np = pdev->dev.of_node;
  1512. struct mfb_info *mfbi;
  1513. struct fsl_diu_data *data;
  1514. dma_addr_t dma_addr; /* DMA addr of fsl_diu_data struct */
  1515. const void *prop;
  1516. unsigned int i;
  1517. int ret;
  1518. data = dmam_alloc_coherent(&pdev->dev, sizeof(struct fsl_diu_data),
  1519. &dma_addr, GFP_DMA | __GFP_ZERO);
  1520. if (!data)
  1521. return -ENOMEM;
  1522. data->dma_addr = dma_addr;
  1523. /*
  1524. * dma_alloc_coherent() uses a page allocator, so the address is
  1525. * always page-aligned. We need the memory to be 32-byte aligned,
  1526. * so that's good. However, if one day the allocator changes, we
  1527. * need to catch that. It's not worth the effort to handle unaligned
  1528. * alloctions now because it's highly unlikely to ever be a problem.
  1529. */
  1530. if ((unsigned long)data & 31) {
  1531. dev_err(&pdev->dev, "misaligned allocation");
  1532. ret = -ENOMEM;
  1533. goto error;
  1534. }
  1535. spin_lock_init(&data->reg_lock);
  1536. for (i = 0; i < NUM_AOIS; i++) {
  1537. struct fb_info *info = &data->fsl_diu_info[i];
  1538. info->device = &pdev->dev;
  1539. info->par = &data->mfb[i];
  1540. /*
  1541. * We store the physical address of the AD in the reserved
  1542. * 'paddr' field of the AD itself.
  1543. */
  1544. data->ad[i].paddr = DMA_ADDR(data, ad[i]);
  1545. info->fix.smem_start = 0;
  1546. /* Initialize the AOI data structure */
  1547. mfbi = info->par;
  1548. memcpy(mfbi, &mfb_template[i], sizeof(struct mfb_info));
  1549. mfbi->parent = data;
  1550. mfbi->ad = &data->ad[i];
  1551. }
  1552. /* Get the EDID data from the device tree, if present */
  1553. prop = of_get_property(np, "edid", &ret);
  1554. if (prop && ret == EDID_LENGTH) {
  1555. memcpy(data->edid_data, prop, EDID_LENGTH);
  1556. data->has_edid = true;
  1557. }
  1558. data->diu_reg = of_iomap(np, 0);
  1559. if (!data->diu_reg) {
  1560. dev_err(&pdev->dev, "cannot map DIU registers\n");
  1561. ret = -EFAULT;
  1562. goto error;
  1563. }
  1564. /* Get the IRQ of the DIU */
  1565. data->irq = irq_of_parse_and_map(np, 0);
  1566. if (!data->irq) {
  1567. dev_err(&pdev->dev, "could not get DIU IRQ\n");
  1568. ret = -EINVAL;
  1569. goto error;
  1570. }
  1571. data->monitor_port = monitor_port;
  1572. /* Initialize the dummy Area Descriptor */
  1573. data->dummy_ad.addr = cpu_to_le32(DMA_ADDR(data, dummy_aoi));
  1574. data->dummy_ad.pix_fmt = 0x88882317;
  1575. data->dummy_ad.src_size_g_alpha = cpu_to_le32((4 << 12) | 4);
  1576. data->dummy_ad.aoi_size = cpu_to_le32((4 << 16) | 2);
  1577. data->dummy_ad.offset_xyi = 0;
  1578. data->dummy_ad.offset_xyd = 0;
  1579. data->dummy_ad.next_ad = 0;
  1580. data->dummy_ad.paddr = DMA_ADDR(data, dummy_ad);
  1581. /*
  1582. * Let DIU continue to display splash screen if it was pre-initialized
  1583. * by the bootloader; otherwise, clear the display.
  1584. */
  1585. if (in_be32(&data->diu_reg->diu_mode) == MFB_MODE0)
  1586. out_be32(&data->diu_reg->desc[0], 0);
  1587. out_be32(&data->diu_reg->desc[1], data->dummy_ad.paddr);
  1588. out_be32(&data->diu_reg->desc[2], data->dummy_ad.paddr);
  1589. /*
  1590. * Older versions of U-Boot leave interrupts enabled, so disable
  1591. * all of them and clear the status register.
  1592. */
  1593. out_be32(&data->diu_reg->int_mask, 0xffffffff);
  1594. in_be32(&data->diu_reg->int_status);
  1595. ret = request_irq(data->irq, fsl_diu_isr, 0, "fsl-diu-fb",
  1596. data->diu_reg);
  1597. if (ret) {
  1598. dev_err(&pdev->dev, "could not claim irq\n");
  1599. goto error;
  1600. }
  1601. for (i = 0; i < NUM_AOIS; i++) {
  1602. ret = install_fb(&data->fsl_diu_info[i]);
  1603. if (ret) {
  1604. dev_err(&pdev->dev, "could not register fb %d\n", i);
  1605. free_irq(data->irq, data->diu_reg);
  1606. goto error;
  1607. }
  1608. }
  1609. sysfs_attr_init(&data->dev_attr.attr);
  1610. data->dev_attr.attr.name = "monitor";
  1611. data->dev_attr.attr.mode = S_IRUGO|S_IWUSR;
  1612. data->dev_attr.show = show_monitor;
  1613. data->dev_attr.store = store_monitor;
  1614. ret = device_create_file(&pdev->dev, &data->dev_attr);
  1615. if (ret) {
  1616. dev_err(&pdev->dev, "could not create sysfs file %s\n",
  1617. data->dev_attr.attr.name);
  1618. }
  1619. dev_set_drvdata(&pdev->dev, data);
  1620. return 0;
  1621. error:
  1622. for (i = 0; i < NUM_AOIS; i++)
  1623. uninstall_fb(&data->fsl_diu_info[i]);
  1624. iounmap(data->diu_reg);
  1625. return ret;
  1626. }
  1627. static int fsl_diu_remove(struct platform_device *pdev)
  1628. {
  1629. struct fsl_diu_data *data;
  1630. int i;
  1631. data = dev_get_drvdata(&pdev->dev);
  1632. disable_lcdc(&data->fsl_diu_info[0]);
  1633. free_irq(data->irq, data->diu_reg);
  1634. for (i = 0; i < NUM_AOIS; i++)
  1635. uninstall_fb(&data->fsl_diu_info[i]);
  1636. iounmap(data->diu_reg);
  1637. return 0;
  1638. }
  1639. #ifndef MODULE
  1640. static int __init fsl_diu_setup(char *options)
  1641. {
  1642. char *opt;
  1643. unsigned long val;
  1644. if (!options || !*options)
  1645. return 0;
  1646. while ((opt = strsep(&options, ",")) != NULL) {
  1647. if (!*opt)
  1648. continue;
  1649. if (!strncmp(opt, "monitor=", 8)) {
  1650. monitor_port = fsl_diu_name_to_port(opt + 8);
  1651. } else if (!strncmp(opt, "bpp=", 4)) {
  1652. if (!kstrtoul(opt + 4, 10, &val))
  1653. default_bpp = val;
  1654. } else
  1655. fb_mode = opt;
  1656. }
  1657. return 0;
  1658. }
  1659. #endif
  1660. static const struct of_device_id fsl_diu_match[] = {
  1661. #ifdef CONFIG_PPC_MPC512x
  1662. {
  1663. .compatible = "fsl,mpc5121-diu",
  1664. },
  1665. #endif
  1666. {
  1667. .compatible = "fsl,diu",
  1668. },
  1669. {}
  1670. };
  1671. MODULE_DEVICE_TABLE(of, fsl_diu_match);
  1672. static struct platform_driver fsl_diu_driver = {
  1673. .driver = {
  1674. .name = "fsl-diu-fb",
  1675. .of_match_table = fsl_diu_match,
  1676. },
  1677. .probe = fsl_diu_probe,
  1678. .remove = fsl_diu_remove,
  1679. .suspend = fsl_diu_suspend,
  1680. .resume = fsl_diu_resume,
  1681. };
  1682. static int __init fsl_diu_init(void)
  1683. {
  1684. #ifdef CONFIG_NOT_COHERENT_CACHE
  1685. struct device_node *np;
  1686. const u32 *prop;
  1687. #endif
  1688. int ret;
  1689. #ifndef MODULE
  1690. char *option;
  1691. /*
  1692. * For kernel boot options (in 'video=xxxfb:<options>' format)
  1693. */
  1694. if (fb_get_options("fslfb", &option))
  1695. return -ENODEV;
  1696. fsl_diu_setup(option);
  1697. #else
  1698. monitor_port = fsl_diu_name_to_port(monitor_string);
  1699. #endif
  1700. /*
  1701. * Must to verify set_pixel_clock. If not implement on platform,
  1702. * then that means that there is no platform support for the DIU.
  1703. */
  1704. if (!diu_ops.set_pixel_clock)
  1705. return -ENODEV;
  1706. pr_info("Freescale Display Interface Unit (DIU) framebuffer driver\n");
  1707. #ifdef CONFIG_NOT_COHERENT_CACHE
  1708. np = of_find_node_by_type(NULL, "cpu");
  1709. if (!np) {
  1710. pr_err("fsl-diu-fb: can't find 'cpu' device node\n");
  1711. return -ENODEV;
  1712. }
  1713. prop = of_get_property(np, "d-cache-size", NULL);
  1714. if (prop == NULL) {
  1715. pr_err("fsl-diu-fb: missing 'd-cache-size' property' "
  1716. "in 'cpu' node\n");
  1717. of_node_put(np);
  1718. return -ENODEV;
  1719. }
  1720. /*
  1721. * Freescale PLRU requires 13/8 times the cache size to do a proper
  1722. * displacement flush
  1723. */
  1724. coherence_data_size = be32_to_cpup(prop) * 13;
  1725. coherence_data_size /= 8;
  1726. pr_debug("fsl-diu-fb: coherence data size is %zu bytes\n",
  1727. coherence_data_size);
  1728. prop = of_get_property(np, "d-cache-line-size", NULL);
  1729. if (prop == NULL) {
  1730. pr_err("fsl-diu-fb: missing 'd-cache-line-size' property' "
  1731. "in 'cpu' node\n");
  1732. of_node_put(np);
  1733. return -ENODEV;
  1734. }
  1735. d_cache_line_size = be32_to_cpup(prop);
  1736. pr_debug("fsl-diu-fb: cache lines size is %u bytes\n",
  1737. d_cache_line_size);
  1738. of_node_put(np);
  1739. coherence_data = vmalloc(coherence_data_size);
  1740. if (!coherence_data)
  1741. return -ENOMEM;
  1742. #endif
  1743. ret = platform_driver_register(&fsl_diu_driver);
  1744. if (ret) {
  1745. pr_err("fsl-diu-fb: failed to register platform driver\n");
  1746. #if defined(CONFIG_NOT_COHERENT_CACHE)
  1747. vfree(coherence_data);
  1748. #endif
  1749. }
  1750. return ret;
  1751. }
  1752. static void __exit fsl_diu_exit(void)
  1753. {
  1754. platform_driver_unregister(&fsl_diu_driver);
  1755. #if defined(CONFIG_NOT_COHERENT_CACHE)
  1756. vfree(coherence_data);
  1757. #endif
  1758. }
  1759. module_init(fsl_diu_init);
  1760. module_exit(fsl_diu_exit);
  1761. MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
  1762. MODULE_DESCRIPTION("Freescale DIU framebuffer driver");
  1763. MODULE_LICENSE("GPL");
  1764. module_param_named(mode, fb_mode, charp, 0);
  1765. MODULE_PARM_DESC(mode,
  1766. "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
  1767. module_param_named(bpp, default_bpp, ulong, 0);
  1768. MODULE_PARM_DESC(bpp, "Specify bit-per-pixel if not specified in 'mode'");
  1769. module_param_named(monitor, monitor_string, charp, 0);
  1770. MODULE_PARM_DESC(monitor, "Specify the monitor port "
  1771. "(\"dvi\", \"lvds\", or \"dlvds\") if supported by the platform");