ast_mode.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. * Parts based on xf86-video-ast
  4. * Copyright (c) 2005 ASPEED Technology Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  18. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * The above copyright notice and this permission notice (including the
  23. * next paragraph) shall be included in all copies or substantial portions
  24. * of the Software.
  25. *
  26. */
  27. /*
  28. * Authors: Dave Airlie <airlied@redhat.com>
  29. */
  30. #include <linux/export.h>
  31. #include <drm/drmP.h>
  32. #include <drm/drm_crtc.h>
  33. #include <drm/drm_crtc_helper.h>
  34. #include <drm/drm_plane_helper.h>
  35. #include "ast_drv.h"
  36. #include "ast_tables.h"
  37. static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev);
  38. static void ast_i2c_destroy(struct ast_i2c_chan *i2c);
  39. static int ast_cursor_set(struct drm_crtc *crtc,
  40. struct drm_file *file_priv,
  41. uint32_t handle,
  42. uint32_t width,
  43. uint32_t height);
  44. static int ast_cursor_move(struct drm_crtc *crtc,
  45. int x, int y);
  46. static inline void ast_load_palette_index(struct ast_private *ast,
  47. u8 index, u8 red, u8 green,
  48. u8 blue)
  49. {
  50. ast_io_write8(ast, AST_IO_DAC_INDEX_WRITE, index);
  51. ast_io_read8(ast, AST_IO_SEQ_PORT);
  52. ast_io_write8(ast, AST_IO_DAC_DATA, red);
  53. ast_io_read8(ast, AST_IO_SEQ_PORT);
  54. ast_io_write8(ast, AST_IO_DAC_DATA, green);
  55. ast_io_read8(ast, AST_IO_SEQ_PORT);
  56. ast_io_write8(ast, AST_IO_DAC_DATA, blue);
  57. ast_io_read8(ast, AST_IO_SEQ_PORT);
  58. }
  59. static void ast_crtc_load_lut(struct drm_crtc *crtc)
  60. {
  61. struct ast_private *ast = crtc->dev->dev_private;
  62. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  63. int i;
  64. if (!crtc->enabled)
  65. return;
  66. for (i = 0; i < 256; i++)
  67. ast_load_palette_index(ast, i, ast_crtc->lut_r[i],
  68. ast_crtc->lut_g[i], ast_crtc->lut_b[i]);
  69. }
  70. static bool ast_get_vbios_mode_info(struct drm_crtc *crtc, struct drm_display_mode *mode,
  71. struct drm_display_mode *adjusted_mode,
  72. struct ast_vbios_mode_info *vbios_mode)
  73. {
  74. struct ast_private *ast = crtc->dev->dev_private;
  75. u32 refresh_rate_index = 0, mode_id, color_index, refresh_rate;
  76. u32 hborder, vborder;
  77. bool check_sync;
  78. struct ast_vbios_enhtable *best = NULL;
  79. switch (crtc->primary->fb->bits_per_pixel) {
  80. case 8:
  81. vbios_mode->std_table = &vbios_stdtable[VGAModeIndex];
  82. color_index = VGAModeIndex - 1;
  83. break;
  84. case 16:
  85. vbios_mode->std_table = &vbios_stdtable[HiCModeIndex];
  86. color_index = HiCModeIndex;
  87. break;
  88. case 24:
  89. case 32:
  90. vbios_mode->std_table = &vbios_stdtable[TrueCModeIndex];
  91. color_index = TrueCModeIndex;
  92. break;
  93. default:
  94. return false;
  95. }
  96. switch (crtc->mode.crtc_hdisplay) {
  97. case 640:
  98. vbios_mode->enh_table = &res_640x480[refresh_rate_index];
  99. break;
  100. case 800:
  101. vbios_mode->enh_table = &res_800x600[refresh_rate_index];
  102. break;
  103. case 1024:
  104. vbios_mode->enh_table = &res_1024x768[refresh_rate_index];
  105. break;
  106. case 1280:
  107. if (crtc->mode.crtc_vdisplay == 800)
  108. vbios_mode->enh_table = &res_1280x800[refresh_rate_index];
  109. else
  110. vbios_mode->enh_table = &res_1280x1024[refresh_rate_index];
  111. break;
  112. case 1360:
  113. vbios_mode->enh_table = &res_1360x768[refresh_rate_index];
  114. break;
  115. case 1440:
  116. vbios_mode->enh_table = &res_1440x900[refresh_rate_index];
  117. break;
  118. case 1600:
  119. if (crtc->mode.crtc_vdisplay == 900)
  120. vbios_mode->enh_table = &res_1600x900[refresh_rate_index];
  121. else
  122. vbios_mode->enh_table = &res_1600x1200[refresh_rate_index];
  123. break;
  124. case 1680:
  125. vbios_mode->enh_table = &res_1680x1050[refresh_rate_index];
  126. break;
  127. case 1920:
  128. if (crtc->mode.crtc_vdisplay == 1080)
  129. vbios_mode->enh_table = &res_1920x1080[refresh_rate_index];
  130. else
  131. vbios_mode->enh_table = &res_1920x1200[refresh_rate_index];
  132. break;
  133. default:
  134. return false;
  135. }
  136. refresh_rate = drm_mode_vrefresh(mode);
  137. check_sync = vbios_mode->enh_table->flags & WideScreenMode;
  138. do {
  139. struct ast_vbios_enhtable *loop = vbios_mode->enh_table;
  140. while (loop->refresh_rate != 0xff) {
  141. if ((check_sync) &&
  142. (((mode->flags & DRM_MODE_FLAG_NVSYNC) &&
  143. (loop->flags & PVSync)) ||
  144. ((mode->flags & DRM_MODE_FLAG_PVSYNC) &&
  145. (loop->flags & NVSync)) ||
  146. ((mode->flags & DRM_MODE_FLAG_NHSYNC) &&
  147. (loop->flags & PHSync)) ||
  148. ((mode->flags & DRM_MODE_FLAG_PHSYNC) &&
  149. (loop->flags & NHSync)))) {
  150. loop++;
  151. continue;
  152. }
  153. if (loop->refresh_rate <= refresh_rate
  154. && (!best || loop->refresh_rate > best->refresh_rate))
  155. best = loop;
  156. loop++;
  157. }
  158. if (best || !check_sync)
  159. break;
  160. check_sync = 0;
  161. } while (1);
  162. if (best)
  163. vbios_mode->enh_table = best;
  164. hborder = (vbios_mode->enh_table->flags & HBorder) ? 8 : 0;
  165. vborder = (vbios_mode->enh_table->flags & VBorder) ? 8 : 0;
  166. adjusted_mode->crtc_htotal = vbios_mode->enh_table->ht;
  167. adjusted_mode->crtc_hblank_start = vbios_mode->enh_table->hde + hborder;
  168. adjusted_mode->crtc_hblank_end = vbios_mode->enh_table->ht - hborder;
  169. adjusted_mode->crtc_hsync_start = vbios_mode->enh_table->hde + hborder +
  170. vbios_mode->enh_table->hfp;
  171. adjusted_mode->crtc_hsync_end = (vbios_mode->enh_table->hde + hborder +
  172. vbios_mode->enh_table->hfp +
  173. vbios_mode->enh_table->hsync);
  174. adjusted_mode->crtc_vtotal = vbios_mode->enh_table->vt;
  175. adjusted_mode->crtc_vblank_start = vbios_mode->enh_table->vde + vborder;
  176. adjusted_mode->crtc_vblank_end = vbios_mode->enh_table->vt - vborder;
  177. adjusted_mode->crtc_vsync_start = vbios_mode->enh_table->vde + vborder +
  178. vbios_mode->enh_table->vfp;
  179. adjusted_mode->crtc_vsync_end = (vbios_mode->enh_table->vde + vborder +
  180. vbios_mode->enh_table->vfp +
  181. vbios_mode->enh_table->vsync);
  182. refresh_rate_index = vbios_mode->enh_table->refresh_rate_index;
  183. mode_id = vbios_mode->enh_table->mode_id;
  184. if (ast->chip == AST1180) {
  185. /* TODO 1180 */
  186. } else {
  187. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8c, (u8)((color_index & 0xf) << 4));
  188. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8d, refresh_rate_index & 0xff);
  189. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x8e, mode_id & 0xff);
  190. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0x00);
  191. if (vbios_mode->enh_table->flags & NewModeInfo) {
  192. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x91, 0xa8);
  193. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x92, crtc->primary->fb->bits_per_pixel);
  194. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x93, adjusted_mode->clock / 1000);
  195. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x94, adjusted_mode->crtc_hdisplay);
  196. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x95, adjusted_mode->crtc_hdisplay >> 8);
  197. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x96, adjusted_mode->crtc_vdisplay);
  198. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x97, adjusted_mode->crtc_vdisplay >> 8);
  199. }
  200. }
  201. return true;
  202. }
  203. static void ast_set_std_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
  204. struct ast_vbios_mode_info *vbios_mode)
  205. {
  206. struct ast_private *ast = crtc->dev->dev_private;
  207. struct ast_vbios_stdtable *stdtable;
  208. u32 i;
  209. u8 jreg;
  210. stdtable = vbios_mode->std_table;
  211. jreg = stdtable->misc;
  212. ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
  213. /* Set SEQ */
  214. ast_set_index_reg(ast, AST_IO_SEQ_PORT, 0x00, 0x03);
  215. for (i = 0; i < 4; i++) {
  216. jreg = stdtable->seq[i];
  217. if (!i)
  218. jreg |= 0x20;
  219. ast_set_index_reg(ast, AST_IO_SEQ_PORT, (i + 1) , jreg);
  220. }
  221. /* Set CRTC */
  222. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
  223. for (i = 0; i < 25; i++)
  224. ast_set_index_reg(ast, AST_IO_CRTC_PORT, i, stdtable->crtc[i]);
  225. /* set AR */
  226. jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
  227. for (i = 0; i < 20; i++) {
  228. jreg = stdtable->ar[i];
  229. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, (u8)i);
  230. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, jreg);
  231. }
  232. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x14);
  233. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x00);
  234. jreg = ast_io_read8(ast, AST_IO_INPUT_STATUS1_READ);
  235. ast_io_write8(ast, AST_IO_AR_PORT_WRITE, 0x20);
  236. /* Set GR */
  237. for (i = 0; i < 9; i++)
  238. ast_set_index_reg(ast, AST_IO_GR_PORT, i, stdtable->gr[i]);
  239. }
  240. static void ast_set_crtc_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
  241. struct ast_vbios_mode_info *vbios_mode)
  242. {
  243. struct ast_private *ast = crtc->dev->dev_private;
  244. u8 jreg05 = 0, jreg07 = 0, jreg09 = 0, jregAC = 0, jregAD = 0, jregAE = 0;
  245. u16 temp;
  246. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x00);
  247. temp = (mode->crtc_htotal >> 3) - 5;
  248. if (temp & 0x100)
  249. jregAC |= 0x01; /* HT D[8] */
  250. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x00, 0x00, temp);
  251. temp = (mode->crtc_hdisplay >> 3) - 1;
  252. if (temp & 0x100)
  253. jregAC |= 0x04; /* HDE D[8] */
  254. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x01, 0x00, temp);
  255. temp = (mode->crtc_hblank_start >> 3) - 1;
  256. if (temp & 0x100)
  257. jregAC |= 0x10; /* HBS D[8] */
  258. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x02, 0x00, temp);
  259. temp = ((mode->crtc_hblank_end >> 3) - 1) & 0x7f;
  260. if (temp & 0x20)
  261. jreg05 |= 0x80; /* HBE D[5] */
  262. if (temp & 0x40)
  263. jregAD |= 0x01; /* HBE D[5] */
  264. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x03, 0xE0, (temp & 0x1f));
  265. temp = (mode->crtc_hsync_start >> 3) - 1;
  266. if (temp & 0x100)
  267. jregAC |= 0x40; /* HRS D[5] */
  268. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x04, 0x00, temp);
  269. temp = ((mode->crtc_hsync_end >> 3) - 1) & 0x3f;
  270. if (temp & 0x20)
  271. jregAD |= 0x04; /* HRE D[5] */
  272. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x05, 0x60, (u8)((temp & 0x1f) | jreg05));
  273. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAC, 0x00, jregAC);
  274. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAD, 0x00, jregAD);
  275. /* vert timings */
  276. temp = (mode->crtc_vtotal) - 2;
  277. if (temp & 0x100)
  278. jreg07 |= 0x01;
  279. if (temp & 0x200)
  280. jreg07 |= 0x20;
  281. if (temp & 0x400)
  282. jregAE |= 0x01;
  283. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x06, 0x00, temp);
  284. temp = (mode->crtc_vsync_start) - 1;
  285. if (temp & 0x100)
  286. jreg07 |= 0x04;
  287. if (temp & 0x200)
  288. jreg07 |= 0x80;
  289. if (temp & 0x400)
  290. jregAE |= 0x08;
  291. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x10, 0x00, temp);
  292. temp = (mode->crtc_vsync_end - 1) & 0x3f;
  293. if (temp & 0x10)
  294. jregAE |= 0x20;
  295. if (temp & 0x20)
  296. jregAE |= 0x40;
  297. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x70, temp & 0xf);
  298. temp = mode->crtc_vdisplay - 1;
  299. if (temp & 0x100)
  300. jreg07 |= 0x02;
  301. if (temp & 0x200)
  302. jreg07 |= 0x40;
  303. if (temp & 0x400)
  304. jregAE |= 0x02;
  305. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x12, 0x00, temp);
  306. temp = mode->crtc_vblank_start - 1;
  307. if (temp & 0x100)
  308. jreg07 |= 0x08;
  309. if (temp & 0x200)
  310. jreg09 |= 0x20;
  311. if (temp & 0x400)
  312. jregAE |= 0x04;
  313. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x15, 0x00, temp);
  314. temp = mode->crtc_vblank_end - 1;
  315. if (temp & 0x100)
  316. jregAE |= 0x10;
  317. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x16, 0x00, temp);
  318. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x07, 0x00, jreg07);
  319. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x09, 0xdf, jreg09);
  320. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xAE, 0x00, (jregAE | 0x80));
  321. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x11, 0x7f, 0x80);
  322. }
  323. static void ast_set_offset_reg(struct drm_crtc *crtc)
  324. {
  325. struct ast_private *ast = crtc->dev->dev_private;
  326. u16 offset;
  327. offset = crtc->primary->fb->pitches[0] >> 3;
  328. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x13, (offset & 0xff));
  329. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xb0, (offset >> 8) & 0x3f);
  330. }
  331. static void ast_set_dclk_reg(struct drm_device *dev, struct drm_display_mode *mode,
  332. struct ast_vbios_mode_info *vbios_mode)
  333. {
  334. struct ast_private *ast = dev->dev_private;
  335. struct ast_vbios_dclk_info *clk_info;
  336. clk_info = &dclk_table[vbios_mode->enh_table->dclk_index];
  337. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc0, 0x00, clk_info->param1);
  338. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xc1, 0x00, clk_info->param2);
  339. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xbb, 0x0f,
  340. (clk_info->param3 & 0x80) | ((clk_info->param3 & 0x3) << 4));
  341. }
  342. static void ast_set_ext_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
  343. struct ast_vbios_mode_info *vbios_mode)
  344. {
  345. struct ast_private *ast = crtc->dev->dev_private;
  346. u8 jregA0 = 0, jregA3 = 0, jregA8 = 0;
  347. switch (crtc->primary->fb->bits_per_pixel) {
  348. case 8:
  349. jregA0 = 0x70;
  350. jregA3 = 0x01;
  351. jregA8 = 0x00;
  352. break;
  353. case 15:
  354. case 16:
  355. jregA0 = 0x70;
  356. jregA3 = 0x04;
  357. jregA8 = 0x02;
  358. break;
  359. case 32:
  360. jregA0 = 0x70;
  361. jregA3 = 0x08;
  362. jregA8 = 0x02;
  363. break;
  364. }
  365. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa0, 0x8f, jregA0);
  366. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xf0, jregA3);
  367. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa8, 0xfd, jregA8);
  368. /* Set Threshold */
  369. if (ast->chip == AST2300 || ast->chip == AST2400) {
  370. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x78);
  371. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x60);
  372. } else if (ast->chip == AST2100 ||
  373. ast->chip == AST1100 ||
  374. ast->chip == AST2200 ||
  375. ast->chip == AST2150) {
  376. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x3f);
  377. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x2f);
  378. } else {
  379. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa7, 0x2f);
  380. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa6, 0x1f);
  381. }
  382. }
  383. static void ast_set_sync_reg(struct drm_device *dev, struct drm_display_mode *mode,
  384. struct ast_vbios_mode_info *vbios_mode)
  385. {
  386. struct ast_private *ast = dev->dev_private;
  387. u8 jreg;
  388. jreg = ast_io_read8(ast, AST_IO_MISC_PORT_READ);
  389. jreg &= ~0xC0;
  390. if (vbios_mode->enh_table->flags & NVSync) jreg |= 0x80;
  391. if (vbios_mode->enh_table->flags & NHSync) jreg |= 0x40;
  392. ast_io_write8(ast, AST_IO_MISC_PORT_WRITE, jreg);
  393. }
  394. static bool ast_set_dac_reg(struct drm_crtc *crtc, struct drm_display_mode *mode,
  395. struct ast_vbios_mode_info *vbios_mode)
  396. {
  397. switch (crtc->primary->fb->bits_per_pixel) {
  398. case 8:
  399. break;
  400. default:
  401. return false;
  402. }
  403. return true;
  404. }
  405. static void ast_set_start_address_crt1(struct drm_crtc *crtc, unsigned offset)
  406. {
  407. struct ast_private *ast = crtc->dev->dev_private;
  408. u32 addr;
  409. addr = offset >> 2;
  410. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0d, (u8)(addr & 0xff));
  411. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0x0c, (u8)((addr >> 8) & 0xff));
  412. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xaf, (u8)((addr >> 16) & 0xff));
  413. }
  414. static void ast_crtc_dpms(struct drm_crtc *crtc, int mode)
  415. {
  416. struct ast_private *ast = crtc->dev->dev_private;
  417. if (ast->chip == AST1180)
  418. return;
  419. switch (mode) {
  420. case DRM_MODE_DPMS_ON:
  421. case DRM_MODE_DPMS_STANDBY:
  422. case DRM_MODE_DPMS_SUSPEND:
  423. ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0);
  424. if (ast->tx_chip_type == AST_TX_DP501)
  425. ast_set_dp501_video_output(crtc->dev, 1);
  426. ast_crtc_load_lut(crtc);
  427. break;
  428. case DRM_MODE_DPMS_OFF:
  429. if (ast->tx_chip_type == AST_TX_DP501)
  430. ast_set_dp501_video_output(crtc->dev, 0);
  431. ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0x20);
  432. break;
  433. }
  434. }
  435. static bool ast_crtc_mode_fixup(struct drm_crtc *crtc,
  436. const struct drm_display_mode *mode,
  437. struct drm_display_mode *adjusted_mode)
  438. {
  439. return true;
  440. }
  441. /* ast is different - we will force move buffers out of VRAM */
  442. static int ast_crtc_do_set_base(struct drm_crtc *crtc,
  443. struct drm_framebuffer *fb,
  444. int x, int y, int atomic)
  445. {
  446. struct ast_private *ast = crtc->dev->dev_private;
  447. struct drm_gem_object *obj;
  448. struct ast_framebuffer *ast_fb;
  449. struct ast_bo *bo;
  450. int ret;
  451. u64 gpu_addr;
  452. /* push the previous fb to system ram */
  453. if (!atomic && fb) {
  454. ast_fb = to_ast_framebuffer(fb);
  455. obj = ast_fb->obj;
  456. bo = gem_to_ast_bo(obj);
  457. ret = ast_bo_reserve(bo, false);
  458. if (ret)
  459. return ret;
  460. ast_bo_push_sysram(bo);
  461. ast_bo_unreserve(bo);
  462. }
  463. ast_fb = to_ast_framebuffer(crtc->primary->fb);
  464. obj = ast_fb->obj;
  465. bo = gem_to_ast_bo(obj);
  466. ret = ast_bo_reserve(bo, false);
  467. if (ret)
  468. return ret;
  469. ret = ast_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  470. if (ret) {
  471. ast_bo_unreserve(bo);
  472. return ret;
  473. }
  474. if (&ast->fbdev->afb == ast_fb) {
  475. /* if pushing console in kmap it */
  476. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  477. if (ret)
  478. DRM_ERROR("failed to kmap fbcon\n");
  479. }
  480. ast_bo_unreserve(bo);
  481. ast_set_start_address_crt1(crtc, (u32)gpu_addr);
  482. return 0;
  483. }
  484. static int ast_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  485. struct drm_framebuffer *old_fb)
  486. {
  487. return ast_crtc_do_set_base(crtc, old_fb, x, y, 0);
  488. }
  489. static int ast_crtc_mode_set(struct drm_crtc *crtc,
  490. struct drm_display_mode *mode,
  491. struct drm_display_mode *adjusted_mode,
  492. int x, int y,
  493. struct drm_framebuffer *old_fb)
  494. {
  495. struct drm_device *dev = crtc->dev;
  496. struct ast_private *ast = crtc->dev->dev_private;
  497. struct ast_vbios_mode_info vbios_mode;
  498. bool ret;
  499. if (ast->chip == AST1180) {
  500. DRM_ERROR("AST 1180 modesetting not supported\n");
  501. return -EINVAL;
  502. }
  503. ret = ast_get_vbios_mode_info(crtc, mode, adjusted_mode, &vbios_mode);
  504. if (ret == false)
  505. return -EINVAL;
  506. ast_open_key(ast);
  507. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa1, 0xff, 0x04);
  508. ast_set_std_reg(crtc, adjusted_mode, &vbios_mode);
  509. ast_set_crtc_reg(crtc, adjusted_mode, &vbios_mode);
  510. ast_set_offset_reg(crtc);
  511. ast_set_dclk_reg(dev, adjusted_mode, &vbios_mode);
  512. ast_set_ext_reg(crtc, adjusted_mode, &vbios_mode);
  513. ast_set_sync_reg(dev, adjusted_mode, &vbios_mode);
  514. ast_set_dac_reg(crtc, adjusted_mode, &vbios_mode);
  515. ast_crtc_mode_set_base(crtc, x, y, old_fb);
  516. return 0;
  517. }
  518. static void ast_crtc_disable(struct drm_crtc *crtc)
  519. {
  520. }
  521. static void ast_crtc_prepare(struct drm_crtc *crtc)
  522. {
  523. }
  524. static void ast_crtc_commit(struct drm_crtc *crtc)
  525. {
  526. struct ast_private *ast = crtc->dev->dev_private;
  527. ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0);
  528. }
  529. static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
  530. .dpms = ast_crtc_dpms,
  531. .mode_fixup = ast_crtc_mode_fixup,
  532. .mode_set = ast_crtc_mode_set,
  533. .mode_set_base = ast_crtc_mode_set_base,
  534. .disable = ast_crtc_disable,
  535. .load_lut = ast_crtc_load_lut,
  536. .prepare = ast_crtc_prepare,
  537. .commit = ast_crtc_commit,
  538. };
  539. static void ast_crtc_reset(struct drm_crtc *crtc)
  540. {
  541. }
  542. static void ast_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  543. u16 *blue, uint32_t start, uint32_t size)
  544. {
  545. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  546. int end = (start + size > 256) ? 256 : start + size, i;
  547. /* userspace palettes are always correct as is */
  548. for (i = start; i < end; i++) {
  549. ast_crtc->lut_r[i] = red[i] >> 8;
  550. ast_crtc->lut_g[i] = green[i] >> 8;
  551. ast_crtc->lut_b[i] = blue[i] >> 8;
  552. }
  553. ast_crtc_load_lut(crtc);
  554. }
  555. static void ast_crtc_destroy(struct drm_crtc *crtc)
  556. {
  557. drm_crtc_cleanup(crtc);
  558. kfree(crtc);
  559. }
  560. static const struct drm_crtc_funcs ast_crtc_funcs = {
  561. .cursor_set = ast_cursor_set,
  562. .cursor_move = ast_cursor_move,
  563. .reset = ast_crtc_reset,
  564. .set_config = drm_crtc_helper_set_config,
  565. .gamma_set = ast_crtc_gamma_set,
  566. .destroy = ast_crtc_destroy,
  567. };
  568. static int ast_crtc_init(struct drm_device *dev)
  569. {
  570. struct ast_crtc *crtc;
  571. int i;
  572. crtc = kzalloc(sizeof(struct ast_crtc), GFP_KERNEL);
  573. if (!crtc)
  574. return -ENOMEM;
  575. drm_crtc_init(dev, &crtc->base, &ast_crtc_funcs);
  576. drm_mode_crtc_set_gamma_size(&crtc->base, 256);
  577. drm_crtc_helper_add(&crtc->base, &ast_crtc_helper_funcs);
  578. for (i = 0; i < 256; i++) {
  579. crtc->lut_r[i] = i;
  580. crtc->lut_g[i] = i;
  581. crtc->lut_b[i] = i;
  582. }
  583. return 0;
  584. }
  585. static void ast_encoder_destroy(struct drm_encoder *encoder)
  586. {
  587. drm_encoder_cleanup(encoder);
  588. kfree(encoder);
  589. }
  590. static struct drm_encoder *ast_best_single_encoder(struct drm_connector *connector)
  591. {
  592. int enc_id = connector->encoder_ids[0];
  593. /* pick the encoder ids */
  594. if (enc_id)
  595. return drm_encoder_find(connector->dev, enc_id);
  596. return NULL;
  597. }
  598. static const struct drm_encoder_funcs ast_enc_funcs = {
  599. .destroy = ast_encoder_destroy,
  600. };
  601. static void ast_encoder_dpms(struct drm_encoder *encoder, int mode)
  602. {
  603. }
  604. static bool ast_mode_fixup(struct drm_encoder *encoder,
  605. const struct drm_display_mode *mode,
  606. struct drm_display_mode *adjusted_mode)
  607. {
  608. return true;
  609. }
  610. static void ast_encoder_mode_set(struct drm_encoder *encoder,
  611. struct drm_display_mode *mode,
  612. struct drm_display_mode *adjusted_mode)
  613. {
  614. }
  615. static void ast_encoder_prepare(struct drm_encoder *encoder)
  616. {
  617. }
  618. static void ast_encoder_commit(struct drm_encoder *encoder)
  619. {
  620. }
  621. static const struct drm_encoder_helper_funcs ast_enc_helper_funcs = {
  622. .dpms = ast_encoder_dpms,
  623. .mode_fixup = ast_mode_fixup,
  624. .prepare = ast_encoder_prepare,
  625. .commit = ast_encoder_commit,
  626. .mode_set = ast_encoder_mode_set,
  627. };
  628. static int ast_encoder_init(struct drm_device *dev)
  629. {
  630. struct ast_encoder *ast_encoder;
  631. ast_encoder = kzalloc(sizeof(struct ast_encoder), GFP_KERNEL);
  632. if (!ast_encoder)
  633. return -ENOMEM;
  634. drm_encoder_init(dev, &ast_encoder->base, &ast_enc_funcs,
  635. DRM_MODE_ENCODER_DAC);
  636. drm_encoder_helper_add(&ast_encoder->base, &ast_enc_helper_funcs);
  637. ast_encoder->base.possible_crtcs = 1;
  638. return 0;
  639. }
  640. static int ast_get_modes(struct drm_connector *connector)
  641. {
  642. struct ast_connector *ast_connector = to_ast_connector(connector);
  643. struct ast_private *ast = connector->dev->dev_private;
  644. struct edid *edid;
  645. int ret;
  646. bool flags = false;
  647. if (ast->tx_chip_type == AST_TX_DP501) {
  648. ast->dp501_maxclk = 0xff;
  649. edid = kmalloc(128, GFP_KERNEL);
  650. if (!edid)
  651. return -ENOMEM;
  652. flags = ast_dp501_read_edid(connector->dev, (u8 *)edid);
  653. if (flags)
  654. ast->dp501_maxclk = ast_get_dp501_max_clk(connector->dev);
  655. else
  656. kfree(edid);
  657. }
  658. if (!flags)
  659. edid = drm_get_edid(connector, &ast_connector->i2c->adapter);
  660. if (edid) {
  661. drm_mode_connector_update_edid_property(&ast_connector->base, edid);
  662. ret = drm_add_edid_modes(connector, edid);
  663. kfree(edid);
  664. return ret;
  665. } else
  666. drm_mode_connector_update_edid_property(&ast_connector->base, NULL);
  667. return 0;
  668. }
  669. static int ast_mode_valid(struct drm_connector *connector,
  670. struct drm_display_mode *mode)
  671. {
  672. struct ast_private *ast = connector->dev->dev_private;
  673. int flags = MODE_NOMODE;
  674. uint32_t jtemp;
  675. if (ast->support_wide_screen) {
  676. if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
  677. return MODE_OK;
  678. if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
  679. return MODE_OK;
  680. if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
  681. return MODE_OK;
  682. if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
  683. return MODE_OK;
  684. if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
  685. return MODE_OK;
  686. if ((ast->chip == AST2100) || (ast->chip == AST2200) || (ast->chip == AST2300) || (ast->chip == AST2400) || (ast->chip == AST1180)) {
  687. if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
  688. return MODE_OK;
  689. if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
  690. jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  691. if (jtemp & 0x01)
  692. return MODE_NOMODE;
  693. else
  694. return MODE_OK;
  695. }
  696. }
  697. }
  698. switch (mode->hdisplay) {
  699. case 640:
  700. if (mode->vdisplay == 480) flags = MODE_OK;
  701. break;
  702. case 800:
  703. if (mode->vdisplay == 600) flags = MODE_OK;
  704. break;
  705. case 1024:
  706. if (mode->vdisplay == 768) flags = MODE_OK;
  707. break;
  708. case 1280:
  709. if (mode->vdisplay == 1024) flags = MODE_OK;
  710. break;
  711. case 1600:
  712. if (mode->vdisplay == 1200) flags = MODE_OK;
  713. break;
  714. default:
  715. return flags;
  716. }
  717. return flags;
  718. }
  719. static void ast_connector_destroy(struct drm_connector *connector)
  720. {
  721. struct ast_connector *ast_connector = to_ast_connector(connector);
  722. ast_i2c_destroy(ast_connector->i2c);
  723. drm_connector_unregister(connector);
  724. drm_connector_cleanup(connector);
  725. kfree(connector);
  726. }
  727. static enum drm_connector_status
  728. ast_connector_detect(struct drm_connector *connector, bool force)
  729. {
  730. return connector_status_connected;
  731. }
  732. static const struct drm_connector_helper_funcs ast_connector_helper_funcs = {
  733. .mode_valid = ast_mode_valid,
  734. .get_modes = ast_get_modes,
  735. .best_encoder = ast_best_single_encoder,
  736. };
  737. static const struct drm_connector_funcs ast_connector_funcs = {
  738. .dpms = drm_helper_connector_dpms,
  739. .detect = ast_connector_detect,
  740. .fill_modes = drm_helper_probe_single_connector_modes,
  741. .destroy = ast_connector_destroy,
  742. };
  743. static int ast_connector_init(struct drm_device *dev)
  744. {
  745. struct ast_connector *ast_connector;
  746. struct drm_connector *connector;
  747. struct drm_encoder *encoder;
  748. ast_connector = kzalloc(sizeof(struct ast_connector), GFP_KERNEL);
  749. if (!ast_connector)
  750. return -ENOMEM;
  751. connector = &ast_connector->base;
  752. drm_connector_init(dev, connector, &ast_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  753. drm_connector_helper_add(connector, &ast_connector_helper_funcs);
  754. connector->interlace_allowed = 0;
  755. connector->doublescan_allowed = 0;
  756. drm_connector_register(connector);
  757. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  758. encoder = list_first_entry(&dev->mode_config.encoder_list, struct drm_encoder, head);
  759. drm_mode_connector_attach_encoder(connector, encoder);
  760. ast_connector->i2c = ast_i2c_create(dev);
  761. if (!ast_connector->i2c)
  762. DRM_ERROR("failed to add ddc bus for connector\n");
  763. return 0;
  764. }
  765. /* allocate cursor cache and pin at start of VRAM */
  766. static int ast_cursor_init(struct drm_device *dev)
  767. {
  768. struct ast_private *ast = dev->dev_private;
  769. int size;
  770. int ret;
  771. struct drm_gem_object *obj;
  772. struct ast_bo *bo;
  773. uint64_t gpu_addr;
  774. size = (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE) * AST_DEFAULT_HWC_NUM;
  775. ret = ast_gem_create(dev, size, true, &obj);
  776. if (ret)
  777. return ret;
  778. bo = gem_to_ast_bo(obj);
  779. ret = ast_bo_reserve(bo, false);
  780. if (unlikely(ret != 0))
  781. goto fail;
  782. ret = ast_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  783. ast_bo_unreserve(bo);
  784. if (ret)
  785. goto fail;
  786. /* kmap the object */
  787. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &ast->cache_kmap);
  788. if (ret)
  789. goto fail;
  790. ast->cursor_cache = obj;
  791. ast->cursor_cache_gpu_addr = gpu_addr;
  792. DRM_DEBUG_KMS("pinned cursor cache at %llx\n", ast->cursor_cache_gpu_addr);
  793. return 0;
  794. fail:
  795. return ret;
  796. }
  797. static void ast_cursor_fini(struct drm_device *dev)
  798. {
  799. struct ast_private *ast = dev->dev_private;
  800. ttm_bo_kunmap(&ast->cache_kmap);
  801. drm_gem_object_unreference_unlocked(ast->cursor_cache);
  802. }
  803. int ast_mode_init(struct drm_device *dev)
  804. {
  805. ast_cursor_init(dev);
  806. ast_crtc_init(dev);
  807. ast_encoder_init(dev);
  808. ast_connector_init(dev);
  809. return 0;
  810. }
  811. void ast_mode_fini(struct drm_device *dev)
  812. {
  813. ast_cursor_fini(dev);
  814. }
  815. static int get_clock(void *i2c_priv)
  816. {
  817. struct ast_i2c_chan *i2c = i2c_priv;
  818. struct ast_private *ast = i2c->dev->dev_private;
  819. uint32_t val;
  820. val = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4;
  821. return val & 1 ? 1 : 0;
  822. }
  823. static int get_data(void *i2c_priv)
  824. {
  825. struct ast_i2c_chan *i2c = i2c_priv;
  826. struct ast_private *ast = i2c->dev->dev_private;
  827. uint32_t val;
  828. val = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5;
  829. return val & 1 ? 1 : 0;
  830. }
  831. static void set_clock(void *i2c_priv, int clock)
  832. {
  833. struct ast_i2c_chan *i2c = i2c_priv;
  834. struct ast_private *ast = i2c->dev->dev_private;
  835. int i;
  836. u8 ujcrb7, jtemp;
  837. for (i = 0; i < 0x10000; i++) {
  838. ujcrb7 = ((clock & 0x01) ? 0 : 1);
  839. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xfe, ujcrb7);
  840. jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x01);
  841. if (ujcrb7 == jtemp)
  842. break;
  843. }
  844. }
  845. static void set_data(void *i2c_priv, int data)
  846. {
  847. struct ast_i2c_chan *i2c = i2c_priv;
  848. struct ast_private *ast = i2c->dev->dev_private;
  849. int i;
  850. u8 ujcrb7, jtemp;
  851. for (i = 0; i < 0x10000; i++) {
  852. ujcrb7 = ((data & 0x01) ? 0 : 1) << 2;
  853. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xfb, ujcrb7);
  854. jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x04);
  855. if (ujcrb7 == jtemp)
  856. break;
  857. }
  858. }
  859. static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev)
  860. {
  861. struct ast_i2c_chan *i2c;
  862. int ret;
  863. i2c = kzalloc(sizeof(struct ast_i2c_chan), GFP_KERNEL);
  864. if (!i2c)
  865. return NULL;
  866. i2c->adapter.owner = THIS_MODULE;
  867. i2c->adapter.class = I2C_CLASS_DDC;
  868. i2c->adapter.dev.parent = &dev->pdev->dev;
  869. i2c->dev = dev;
  870. i2c_set_adapdata(&i2c->adapter, i2c);
  871. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  872. "AST i2c bit bus");
  873. i2c->adapter.algo_data = &i2c->bit;
  874. i2c->bit.udelay = 20;
  875. i2c->bit.timeout = 2;
  876. i2c->bit.data = i2c;
  877. i2c->bit.setsda = set_data;
  878. i2c->bit.setscl = set_clock;
  879. i2c->bit.getsda = get_data;
  880. i2c->bit.getscl = get_clock;
  881. ret = i2c_bit_add_bus(&i2c->adapter);
  882. if (ret) {
  883. DRM_ERROR("Failed to register bit i2c\n");
  884. goto out_free;
  885. }
  886. return i2c;
  887. out_free:
  888. kfree(i2c);
  889. return NULL;
  890. }
  891. static void ast_i2c_destroy(struct ast_i2c_chan *i2c)
  892. {
  893. if (!i2c)
  894. return;
  895. i2c_del_adapter(&i2c->adapter);
  896. kfree(i2c);
  897. }
  898. static void ast_show_cursor(struct drm_crtc *crtc)
  899. {
  900. struct ast_private *ast = crtc->dev->dev_private;
  901. u8 jreg;
  902. jreg = 0x2;
  903. /* enable ARGB cursor */
  904. jreg |= 1;
  905. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, jreg);
  906. }
  907. static void ast_hide_cursor(struct drm_crtc *crtc)
  908. {
  909. struct ast_private *ast = crtc->dev->dev_private;
  910. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, 0x00);
  911. }
  912. static u32 copy_cursor_image(u8 *src, u8 *dst, int width, int height)
  913. {
  914. union {
  915. u32 ul;
  916. u8 b[4];
  917. } srcdata32[2], data32;
  918. union {
  919. u16 us;
  920. u8 b[2];
  921. } data16;
  922. u32 csum = 0;
  923. s32 alpha_dst_delta, last_alpha_dst_delta;
  924. u8 *srcxor, *dstxor;
  925. int i, j;
  926. u32 per_pixel_copy, two_pixel_copy;
  927. alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
  928. last_alpha_dst_delta = alpha_dst_delta - (width << 1);
  929. srcxor = src;
  930. dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
  931. per_pixel_copy = width & 1;
  932. two_pixel_copy = width >> 1;
  933. for (j = 0; j < height; j++) {
  934. for (i = 0; i < two_pixel_copy; i++) {
  935. srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
  936. srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
  937. data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
  938. data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
  939. data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
  940. data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);
  941. writel(data32.ul, dstxor);
  942. csum += data32.ul;
  943. dstxor += 4;
  944. srcxor += 8;
  945. }
  946. for (i = 0; i < per_pixel_copy; i++) {
  947. srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
  948. data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
  949. data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
  950. writew(data16.us, dstxor);
  951. csum += (u32)data16.us;
  952. dstxor += 2;
  953. srcxor += 4;
  954. }
  955. dstxor += last_alpha_dst_delta;
  956. }
  957. return csum;
  958. }
  959. static int ast_cursor_set(struct drm_crtc *crtc,
  960. struct drm_file *file_priv,
  961. uint32_t handle,
  962. uint32_t width,
  963. uint32_t height)
  964. {
  965. struct ast_private *ast = crtc->dev->dev_private;
  966. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  967. struct drm_gem_object *obj;
  968. struct ast_bo *bo;
  969. uint64_t gpu_addr;
  970. u32 csum;
  971. int ret;
  972. struct ttm_bo_kmap_obj uobj_map;
  973. u8 *src, *dst;
  974. bool src_isiomem, dst_isiomem;
  975. if (!handle) {
  976. ast_hide_cursor(crtc);
  977. return 0;
  978. }
  979. if (width > AST_MAX_HWC_WIDTH || height > AST_MAX_HWC_HEIGHT)
  980. return -EINVAL;
  981. obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
  982. if (!obj) {
  983. DRM_ERROR("Cannot find cursor object %x for crtc\n", handle);
  984. return -ENOENT;
  985. }
  986. bo = gem_to_ast_bo(obj);
  987. ret = ast_bo_reserve(bo, false);
  988. if (ret)
  989. goto fail;
  990. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &uobj_map);
  991. src = ttm_kmap_obj_virtual(&uobj_map, &src_isiomem);
  992. dst = ttm_kmap_obj_virtual(&ast->cache_kmap, &dst_isiomem);
  993. if (src_isiomem == true)
  994. DRM_ERROR("src cursor bo should be in main memory\n");
  995. if (dst_isiomem == false)
  996. DRM_ERROR("dst bo should be in VRAM\n");
  997. dst += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
  998. /* do data transfer to cursor cache */
  999. csum = copy_cursor_image(src, dst, width, height);
  1000. /* write checksum + signature */
  1001. ttm_bo_kunmap(&uobj_map);
  1002. ast_bo_unreserve(bo);
  1003. {
  1004. u8 *dst = (u8 *)ast->cache_kmap.virtual + (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor + AST_HWC_SIZE;
  1005. writel(csum, dst);
  1006. writel(width, dst + AST_HWC_SIGNATURE_SizeX);
  1007. writel(height, dst + AST_HWC_SIGNATURE_SizeY);
  1008. writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
  1009. writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
  1010. /* set pattern offset */
  1011. gpu_addr = ast->cursor_cache_gpu_addr;
  1012. gpu_addr += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
  1013. gpu_addr >>= 3;
  1014. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, gpu_addr & 0xff);
  1015. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, (gpu_addr >> 8) & 0xff);
  1016. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, (gpu_addr >> 16) & 0xff);
  1017. }
  1018. ast_crtc->cursor_width = width;
  1019. ast_crtc->cursor_height = height;
  1020. ast_crtc->offset_x = AST_MAX_HWC_WIDTH - width;
  1021. ast_crtc->offset_y = AST_MAX_HWC_WIDTH - height;
  1022. ast->next_cursor = (ast->next_cursor + 1) % AST_DEFAULT_HWC_NUM;
  1023. ast_show_cursor(crtc);
  1024. drm_gem_object_unreference_unlocked(obj);
  1025. return 0;
  1026. fail:
  1027. drm_gem_object_unreference_unlocked(obj);
  1028. return ret;
  1029. }
  1030. static int ast_cursor_move(struct drm_crtc *crtc,
  1031. int x, int y)
  1032. {
  1033. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  1034. struct ast_private *ast = crtc->dev->dev_private;
  1035. int x_offset, y_offset;
  1036. u8 *sig;
  1037. sig = (u8 *)ast->cache_kmap.virtual + (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor + AST_HWC_SIZE;
  1038. writel(x, sig + AST_HWC_SIGNATURE_X);
  1039. writel(y, sig + AST_HWC_SIGNATURE_Y);
  1040. x_offset = ast_crtc->offset_x;
  1041. y_offset = ast_crtc->offset_y;
  1042. if (x < 0) {
  1043. x_offset = (-x) + ast_crtc->offset_x;
  1044. x = 0;
  1045. }
  1046. if (y < 0) {
  1047. y_offset = (-y) + ast_crtc->offset_y;
  1048. y = 0;
  1049. }
  1050. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc2, x_offset);
  1051. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc3, y_offset);
  1052. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc4, (x & 0xff));
  1053. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc5, ((x >> 8) & 0x0f));
  1054. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc6, (y & 0xff));
  1055. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc7, ((y >> 8) & 0x07));
  1056. /* dummy write to fire HWC */
  1057. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xCB, 0xFF, 0x00);
  1058. return 0;
  1059. }