ast_mode.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. /* ast is different - we will force move buffers out of VRAM */
  436. static int ast_crtc_do_set_base(struct drm_crtc *crtc,
  437. struct drm_framebuffer *fb,
  438. int x, int y, int atomic)
  439. {
  440. struct ast_private *ast = crtc->dev->dev_private;
  441. struct drm_gem_object *obj;
  442. struct ast_framebuffer *ast_fb;
  443. struct ast_bo *bo;
  444. int ret;
  445. u64 gpu_addr;
  446. /* push the previous fb to system ram */
  447. if (!atomic && fb) {
  448. ast_fb = to_ast_framebuffer(fb);
  449. obj = ast_fb->obj;
  450. bo = gem_to_ast_bo(obj);
  451. ret = ast_bo_reserve(bo, false);
  452. if (ret)
  453. return ret;
  454. ast_bo_push_sysram(bo);
  455. ast_bo_unreserve(bo);
  456. }
  457. ast_fb = to_ast_framebuffer(crtc->primary->fb);
  458. obj = ast_fb->obj;
  459. bo = gem_to_ast_bo(obj);
  460. ret = ast_bo_reserve(bo, false);
  461. if (ret)
  462. return ret;
  463. ret = ast_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  464. if (ret) {
  465. ast_bo_unreserve(bo);
  466. return ret;
  467. }
  468. if (&ast->fbdev->afb == ast_fb) {
  469. /* if pushing console in kmap it */
  470. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  471. if (ret)
  472. DRM_ERROR("failed to kmap fbcon\n");
  473. else
  474. ast_fbdev_set_base(ast, gpu_addr);
  475. }
  476. ast_bo_unreserve(bo);
  477. ast_set_start_address_crt1(crtc, (u32)gpu_addr);
  478. return 0;
  479. }
  480. static int ast_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  481. struct drm_framebuffer *old_fb)
  482. {
  483. return ast_crtc_do_set_base(crtc, old_fb, x, y, 0);
  484. }
  485. static int ast_crtc_mode_set(struct drm_crtc *crtc,
  486. struct drm_display_mode *mode,
  487. struct drm_display_mode *adjusted_mode,
  488. int x, int y,
  489. struct drm_framebuffer *old_fb)
  490. {
  491. struct drm_device *dev = crtc->dev;
  492. struct ast_private *ast = crtc->dev->dev_private;
  493. struct ast_vbios_mode_info vbios_mode;
  494. bool ret;
  495. if (ast->chip == AST1180) {
  496. DRM_ERROR("AST 1180 modesetting not supported\n");
  497. return -EINVAL;
  498. }
  499. ret = ast_get_vbios_mode_info(crtc, mode, adjusted_mode, &vbios_mode);
  500. if (ret == false)
  501. return -EINVAL;
  502. ast_open_key(ast);
  503. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa1, 0xff, 0x04);
  504. ast_set_std_reg(crtc, adjusted_mode, &vbios_mode);
  505. ast_set_crtc_reg(crtc, adjusted_mode, &vbios_mode);
  506. ast_set_offset_reg(crtc);
  507. ast_set_dclk_reg(dev, adjusted_mode, &vbios_mode);
  508. ast_set_ext_reg(crtc, adjusted_mode, &vbios_mode);
  509. ast_set_sync_reg(dev, adjusted_mode, &vbios_mode);
  510. ast_set_dac_reg(crtc, adjusted_mode, &vbios_mode);
  511. ast_crtc_mode_set_base(crtc, x, y, old_fb);
  512. return 0;
  513. }
  514. static void ast_crtc_disable(struct drm_crtc *crtc)
  515. {
  516. }
  517. static void ast_crtc_prepare(struct drm_crtc *crtc)
  518. {
  519. }
  520. static void ast_crtc_commit(struct drm_crtc *crtc)
  521. {
  522. struct ast_private *ast = crtc->dev->dev_private;
  523. ast_set_index_reg_mask(ast, AST_IO_SEQ_PORT, 0x1, 0xdf, 0);
  524. }
  525. static const struct drm_crtc_helper_funcs ast_crtc_helper_funcs = {
  526. .dpms = ast_crtc_dpms,
  527. .mode_set = ast_crtc_mode_set,
  528. .mode_set_base = ast_crtc_mode_set_base,
  529. .disable = ast_crtc_disable,
  530. .load_lut = ast_crtc_load_lut,
  531. .prepare = ast_crtc_prepare,
  532. .commit = ast_crtc_commit,
  533. };
  534. static void ast_crtc_reset(struct drm_crtc *crtc)
  535. {
  536. }
  537. static int ast_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  538. u16 *blue, uint32_t size)
  539. {
  540. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  541. int i;
  542. /* userspace palettes are always correct as is */
  543. for (i = 0; i < size; i++) {
  544. ast_crtc->lut_r[i] = red[i] >> 8;
  545. ast_crtc->lut_g[i] = green[i] >> 8;
  546. ast_crtc->lut_b[i] = blue[i] >> 8;
  547. }
  548. ast_crtc_load_lut(crtc);
  549. return 0;
  550. }
  551. static void ast_crtc_destroy(struct drm_crtc *crtc)
  552. {
  553. drm_crtc_cleanup(crtc);
  554. kfree(crtc);
  555. }
  556. static const struct drm_crtc_funcs ast_crtc_funcs = {
  557. .cursor_set = ast_cursor_set,
  558. .cursor_move = ast_cursor_move,
  559. .reset = ast_crtc_reset,
  560. .set_config = drm_crtc_helper_set_config,
  561. .gamma_set = ast_crtc_gamma_set,
  562. .destroy = ast_crtc_destroy,
  563. };
  564. static int ast_crtc_init(struct drm_device *dev)
  565. {
  566. struct ast_crtc *crtc;
  567. int i;
  568. crtc = kzalloc(sizeof(struct ast_crtc), GFP_KERNEL);
  569. if (!crtc)
  570. return -ENOMEM;
  571. drm_crtc_init(dev, &crtc->base, &ast_crtc_funcs);
  572. drm_mode_crtc_set_gamma_size(&crtc->base, 256);
  573. drm_crtc_helper_add(&crtc->base, &ast_crtc_helper_funcs);
  574. for (i = 0; i < 256; i++) {
  575. crtc->lut_r[i] = i;
  576. crtc->lut_g[i] = i;
  577. crtc->lut_b[i] = i;
  578. }
  579. return 0;
  580. }
  581. static void ast_encoder_destroy(struct drm_encoder *encoder)
  582. {
  583. drm_encoder_cleanup(encoder);
  584. kfree(encoder);
  585. }
  586. static struct drm_encoder *ast_best_single_encoder(struct drm_connector *connector)
  587. {
  588. int enc_id = connector->encoder_ids[0];
  589. /* pick the encoder ids */
  590. if (enc_id)
  591. return drm_encoder_find(connector->dev, enc_id);
  592. return NULL;
  593. }
  594. static const struct drm_encoder_funcs ast_enc_funcs = {
  595. .destroy = ast_encoder_destroy,
  596. };
  597. static void ast_encoder_dpms(struct drm_encoder *encoder, int mode)
  598. {
  599. }
  600. static void ast_encoder_mode_set(struct drm_encoder *encoder,
  601. struct drm_display_mode *mode,
  602. struct drm_display_mode *adjusted_mode)
  603. {
  604. }
  605. static void ast_encoder_prepare(struct drm_encoder *encoder)
  606. {
  607. }
  608. static void ast_encoder_commit(struct drm_encoder *encoder)
  609. {
  610. }
  611. static const struct drm_encoder_helper_funcs ast_enc_helper_funcs = {
  612. .dpms = ast_encoder_dpms,
  613. .prepare = ast_encoder_prepare,
  614. .commit = ast_encoder_commit,
  615. .mode_set = ast_encoder_mode_set,
  616. };
  617. static int ast_encoder_init(struct drm_device *dev)
  618. {
  619. struct ast_encoder *ast_encoder;
  620. ast_encoder = kzalloc(sizeof(struct ast_encoder), GFP_KERNEL);
  621. if (!ast_encoder)
  622. return -ENOMEM;
  623. drm_encoder_init(dev, &ast_encoder->base, &ast_enc_funcs,
  624. DRM_MODE_ENCODER_DAC, NULL);
  625. drm_encoder_helper_add(&ast_encoder->base, &ast_enc_helper_funcs);
  626. ast_encoder->base.possible_crtcs = 1;
  627. return 0;
  628. }
  629. static int ast_get_modes(struct drm_connector *connector)
  630. {
  631. struct ast_connector *ast_connector = to_ast_connector(connector);
  632. struct ast_private *ast = connector->dev->dev_private;
  633. struct edid *edid;
  634. int ret;
  635. bool flags = false;
  636. if (ast->tx_chip_type == AST_TX_DP501) {
  637. ast->dp501_maxclk = 0xff;
  638. edid = kmalloc(128, GFP_KERNEL);
  639. if (!edid)
  640. return -ENOMEM;
  641. flags = ast_dp501_read_edid(connector->dev, (u8 *)edid);
  642. if (flags)
  643. ast->dp501_maxclk = ast_get_dp501_max_clk(connector->dev);
  644. else
  645. kfree(edid);
  646. }
  647. if (!flags)
  648. edid = drm_get_edid(connector, &ast_connector->i2c->adapter);
  649. if (edid) {
  650. drm_mode_connector_update_edid_property(&ast_connector->base, edid);
  651. ret = drm_add_edid_modes(connector, edid);
  652. kfree(edid);
  653. return ret;
  654. } else
  655. drm_mode_connector_update_edid_property(&ast_connector->base, NULL);
  656. return 0;
  657. }
  658. static int ast_mode_valid(struct drm_connector *connector,
  659. struct drm_display_mode *mode)
  660. {
  661. struct ast_private *ast = connector->dev->dev_private;
  662. int flags = MODE_NOMODE;
  663. uint32_t jtemp;
  664. if (ast->support_wide_screen) {
  665. if ((mode->hdisplay == 1680) && (mode->vdisplay == 1050))
  666. return MODE_OK;
  667. if ((mode->hdisplay == 1280) && (mode->vdisplay == 800))
  668. return MODE_OK;
  669. if ((mode->hdisplay == 1440) && (mode->vdisplay == 900))
  670. return MODE_OK;
  671. if ((mode->hdisplay == 1360) && (mode->vdisplay == 768))
  672. return MODE_OK;
  673. if ((mode->hdisplay == 1600) && (mode->vdisplay == 900))
  674. return MODE_OK;
  675. if ((ast->chip == AST2100) || (ast->chip == AST2200) || (ast->chip == AST2300) || (ast->chip == AST2400) || (ast->chip == AST1180)) {
  676. if ((mode->hdisplay == 1920) && (mode->vdisplay == 1080))
  677. return MODE_OK;
  678. if ((mode->hdisplay == 1920) && (mode->vdisplay == 1200)) {
  679. jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  680. if (jtemp & 0x01)
  681. return MODE_NOMODE;
  682. else
  683. return MODE_OK;
  684. }
  685. }
  686. }
  687. switch (mode->hdisplay) {
  688. case 640:
  689. if (mode->vdisplay == 480) flags = MODE_OK;
  690. break;
  691. case 800:
  692. if (mode->vdisplay == 600) flags = MODE_OK;
  693. break;
  694. case 1024:
  695. if (mode->vdisplay == 768) flags = MODE_OK;
  696. break;
  697. case 1280:
  698. if (mode->vdisplay == 1024) flags = MODE_OK;
  699. break;
  700. case 1600:
  701. if (mode->vdisplay == 1200) flags = MODE_OK;
  702. break;
  703. default:
  704. return flags;
  705. }
  706. return flags;
  707. }
  708. static void ast_connector_destroy(struct drm_connector *connector)
  709. {
  710. struct ast_connector *ast_connector = to_ast_connector(connector);
  711. ast_i2c_destroy(ast_connector->i2c);
  712. drm_connector_unregister(connector);
  713. drm_connector_cleanup(connector);
  714. kfree(connector);
  715. }
  716. static const struct drm_connector_helper_funcs ast_connector_helper_funcs = {
  717. .mode_valid = ast_mode_valid,
  718. .get_modes = ast_get_modes,
  719. .best_encoder = ast_best_single_encoder,
  720. };
  721. static const struct drm_connector_funcs ast_connector_funcs = {
  722. .dpms = drm_helper_connector_dpms,
  723. .fill_modes = drm_helper_probe_single_connector_modes,
  724. .destroy = ast_connector_destroy,
  725. };
  726. static int ast_connector_init(struct drm_device *dev)
  727. {
  728. struct ast_connector *ast_connector;
  729. struct drm_connector *connector;
  730. struct drm_encoder *encoder;
  731. ast_connector = kzalloc(sizeof(struct ast_connector), GFP_KERNEL);
  732. if (!ast_connector)
  733. return -ENOMEM;
  734. connector = &ast_connector->base;
  735. drm_connector_init(dev, connector, &ast_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  736. drm_connector_helper_add(connector, &ast_connector_helper_funcs);
  737. connector->interlace_allowed = 0;
  738. connector->doublescan_allowed = 0;
  739. drm_connector_register(connector);
  740. connector->polled = DRM_CONNECTOR_POLL_CONNECT;
  741. encoder = list_first_entry(&dev->mode_config.encoder_list, struct drm_encoder, head);
  742. drm_mode_connector_attach_encoder(connector, encoder);
  743. ast_connector->i2c = ast_i2c_create(dev);
  744. if (!ast_connector->i2c)
  745. DRM_ERROR("failed to add ddc bus for connector\n");
  746. return 0;
  747. }
  748. /* allocate cursor cache and pin at start of VRAM */
  749. static int ast_cursor_init(struct drm_device *dev)
  750. {
  751. struct ast_private *ast = dev->dev_private;
  752. int size;
  753. int ret;
  754. struct drm_gem_object *obj;
  755. struct ast_bo *bo;
  756. uint64_t gpu_addr;
  757. size = (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE) * AST_DEFAULT_HWC_NUM;
  758. ret = ast_gem_create(dev, size, true, &obj);
  759. if (ret)
  760. return ret;
  761. bo = gem_to_ast_bo(obj);
  762. ret = ast_bo_reserve(bo, false);
  763. if (unlikely(ret != 0))
  764. goto fail;
  765. ret = ast_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  766. ast_bo_unreserve(bo);
  767. if (ret)
  768. goto fail;
  769. /* kmap the object */
  770. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &ast->cache_kmap);
  771. if (ret)
  772. goto fail;
  773. ast->cursor_cache = obj;
  774. ast->cursor_cache_gpu_addr = gpu_addr;
  775. DRM_DEBUG_KMS("pinned cursor cache at %llx\n", ast->cursor_cache_gpu_addr);
  776. return 0;
  777. fail:
  778. return ret;
  779. }
  780. static void ast_cursor_fini(struct drm_device *dev)
  781. {
  782. struct ast_private *ast = dev->dev_private;
  783. ttm_bo_kunmap(&ast->cache_kmap);
  784. drm_gem_object_unreference_unlocked(ast->cursor_cache);
  785. }
  786. int ast_mode_init(struct drm_device *dev)
  787. {
  788. ast_cursor_init(dev);
  789. ast_crtc_init(dev);
  790. ast_encoder_init(dev);
  791. ast_connector_init(dev);
  792. return 0;
  793. }
  794. void ast_mode_fini(struct drm_device *dev)
  795. {
  796. ast_cursor_fini(dev);
  797. }
  798. static int get_clock(void *i2c_priv)
  799. {
  800. struct ast_i2c_chan *i2c = i2c_priv;
  801. struct ast_private *ast = i2c->dev->dev_private;
  802. uint32_t val;
  803. val = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x10) >> 4;
  804. return val & 1 ? 1 : 0;
  805. }
  806. static int get_data(void *i2c_priv)
  807. {
  808. struct ast_i2c_chan *i2c = i2c_priv;
  809. struct ast_private *ast = i2c->dev->dev_private;
  810. uint32_t val;
  811. val = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x20) >> 5;
  812. return val & 1 ? 1 : 0;
  813. }
  814. static void set_clock(void *i2c_priv, int clock)
  815. {
  816. struct ast_i2c_chan *i2c = i2c_priv;
  817. struct ast_private *ast = i2c->dev->dev_private;
  818. int i;
  819. u8 ujcrb7, jtemp;
  820. for (i = 0; i < 0x10000; i++) {
  821. ujcrb7 = ((clock & 0x01) ? 0 : 1);
  822. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xfe, ujcrb7);
  823. jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x01);
  824. if (ujcrb7 == jtemp)
  825. break;
  826. }
  827. }
  828. static void set_data(void *i2c_priv, int data)
  829. {
  830. struct ast_i2c_chan *i2c = i2c_priv;
  831. struct ast_private *ast = i2c->dev->dev_private;
  832. int i;
  833. u8 ujcrb7, jtemp;
  834. for (i = 0; i < 0x10000; i++) {
  835. ujcrb7 = ((data & 0x01) ? 0 : 1) << 2;
  836. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0xfb, ujcrb7);
  837. jtemp = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xb7, 0x04);
  838. if (ujcrb7 == jtemp)
  839. break;
  840. }
  841. }
  842. static struct ast_i2c_chan *ast_i2c_create(struct drm_device *dev)
  843. {
  844. struct ast_i2c_chan *i2c;
  845. int ret;
  846. i2c = kzalloc(sizeof(struct ast_i2c_chan), GFP_KERNEL);
  847. if (!i2c)
  848. return NULL;
  849. i2c->adapter.owner = THIS_MODULE;
  850. i2c->adapter.class = I2C_CLASS_DDC;
  851. i2c->adapter.dev.parent = &dev->pdev->dev;
  852. i2c->dev = dev;
  853. i2c_set_adapdata(&i2c->adapter, i2c);
  854. snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
  855. "AST i2c bit bus");
  856. i2c->adapter.algo_data = &i2c->bit;
  857. i2c->bit.udelay = 20;
  858. i2c->bit.timeout = 2;
  859. i2c->bit.data = i2c;
  860. i2c->bit.setsda = set_data;
  861. i2c->bit.setscl = set_clock;
  862. i2c->bit.getsda = get_data;
  863. i2c->bit.getscl = get_clock;
  864. ret = i2c_bit_add_bus(&i2c->adapter);
  865. if (ret) {
  866. DRM_ERROR("Failed to register bit i2c\n");
  867. goto out_free;
  868. }
  869. return i2c;
  870. out_free:
  871. kfree(i2c);
  872. return NULL;
  873. }
  874. static void ast_i2c_destroy(struct ast_i2c_chan *i2c)
  875. {
  876. if (!i2c)
  877. return;
  878. i2c_del_adapter(&i2c->adapter);
  879. kfree(i2c);
  880. }
  881. static void ast_show_cursor(struct drm_crtc *crtc)
  882. {
  883. struct ast_private *ast = crtc->dev->dev_private;
  884. u8 jreg;
  885. jreg = 0x2;
  886. /* enable ARGB cursor */
  887. jreg |= 1;
  888. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, jreg);
  889. }
  890. static void ast_hide_cursor(struct drm_crtc *crtc)
  891. {
  892. struct ast_private *ast = crtc->dev->dev_private;
  893. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, 0x00);
  894. }
  895. static u32 copy_cursor_image(u8 *src, u8 *dst, int width, int height)
  896. {
  897. union {
  898. u32 ul;
  899. u8 b[4];
  900. } srcdata32[2], data32;
  901. union {
  902. u16 us;
  903. u8 b[2];
  904. } data16;
  905. u32 csum = 0;
  906. s32 alpha_dst_delta, last_alpha_dst_delta;
  907. u8 *srcxor, *dstxor;
  908. int i, j;
  909. u32 per_pixel_copy, two_pixel_copy;
  910. alpha_dst_delta = AST_MAX_HWC_WIDTH << 1;
  911. last_alpha_dst_delta = alpha_dst_delta - (width << 1);
  912. srcxor = src;
  913. dstxor = (u8 *)dst + last_alpha_dst_delta + (AST_MAX_HWC_HEIGHT - height) * alpha_dst_delta;
  914. per_pixel_copy = width & 1;
  915. two_pixel_copy = width >> 1;
  916. for (j = 0; j < height; j++) {
  917. for (i = 0; i < two_pixel_copy; i++) {
  918. srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
  919. srcdata32[1].ul = *((u32 *)(srcxor + 4)) & 0xf0f0f0f0;
  920. data32.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
  921. data32.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
  922. data32.b[2] = srcdata32[1].b[1] | (srcdata32[1].b[0] >> 4);
  923. data32.b[3] = srcdata32[1].b[3] | (srcdata32[1].b[2] >> 4);
  924. writel(data32.ul, dstxor);
  925. csum += data32.ul;
  926. dstxor += 4;
  927. srcxor += 8;
  928. }
  929. for (i = 0; i < per_pixel_copy; i++) {
  930. srcdata32[0].ul = *((u32 *)srcxor) & 0xf0f0f0f0;
  931. data16.b[0] = srcdata32[0].b[1] | (srcdata32[0].b[0] >> 4);
  932. data16.b[1] = srcdata32[0].b[3] | (srcdata32[0].b[2] >> 4);
  933. writew(data16.us, dstxor);
  934. csum += (u32)data16.us;
  935. dstxor += 2;
  936. srcxor += 4;
  937. }
  938. dstxor += last_alpha_dst_delta;
  939. }
  940. return csum;
  941. }
  942. static int ast_cursor_set(struct drm_crtc *crtc,
  943. struct drm_file *file_priv,
  944. uint32_t handle,
  945. uint32_t width,
  946. uint32_t height)
  947. {
  948. struct ast_private *ast = crtc->dev->dev_private;
  949. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  950. struct drm_gem_object *obj;
  951. struct ast_bo *bo;
  952. uint64_t gpu_addr;
  953. u32 csum;
  954. int ret;
  955. struct ttm_bo_kmap_obj uobj_map;
  956. u8 *src, *dst;
  957. bool src_isiomem, dst_isiomem;
  958. if (!handle) {
  959. ast_hide_cursor(crtc);
  960. return 0;
  961. }
  962. if (width > AST_MAX_HWC_WIDTH || height > AST_MAX_HWC_HEIGHT)
  963. return -EINVAL;
  964. obj = drm_gem_object_lookup(file_priv, handle);
  965. if (!obj) {
  966. DRM_ERROR("Cannot find cursor object %x for crtc\n", handle);
  967. return -ENOENT;
  968. }
  969. bo = gem_to_ast_bo(obj);
  970. ret = ast_bo_reserve(bo, false);
  971. if (ret)
  972. goto fail;
  973. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &uobj_map);
  974. src = ttm_kmap_obj_virtual(&uobj_map, &src_isiomem);
  975. dst = ttm_kmap_obj_virtual(&ast->cache_kmap, &dst_isiomem);
  976. if (src_isiomem == true)
  977. DRM_ERROR("src cursor bo should be in main memory\n");
  978. if (dst_isiomem == false)
  979. DRM_ERROR("dst bo should be in VRAM\n");
  980. dst += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
  981. /* do data transfer to cursor cache */
  982. csum = copy_cursor_image(src, dst, width, height);
  983. /* write checksum + signature */
  984. ttm_bo_kunmap(&uobj_map);
  985. ast_bo_unreserve(bo);
  986. {
  987. u8 *dst = (u8 *)ast->cache_kmap.virtual + (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor + AST_HWC_SIZE;
  988. writel(csum, dst);
  989. writel(width, dst + AST_HWC_SIGNATURE_SizeX);
  990. writel(height, dst + AST_HWC_SIGNATURE_SizeY);
  991. writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
  992. writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
  993. /* set pattern offset */
  994. gpu_addr = ast->cursor_cache_gpu_addr;
  995. gpu_addr += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
  996. gpu_addr >>= 3;
  997. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, gpu_addr & 0xff);
  998. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, (gpu_addr >> 8) & 0xff);
  999. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, (gpu_addr >> 16) & 0xff);
  1000. }
  1001. ast_crtc->cursor_width = width;
  1002. ast_crtc->cursor_height = height;
  1003. ast_crtc->offset_x = AST_MAX_HWC_WIDTH - width;
  1004. ast_crtc->offset_y = AST_MAX_HWC_WIDTH - height;
  1005. ast->next_cursor = (ast->next_cursor + 1) % AST_DEFAULT_HWC_NUM;
  1006. ast_show_cursor(crtc);
  1007. drm_gem_object_unreference_unlocked(obj);
  1008. return 0;
  1009. fail:
  1010. drm_gem_object_unreference_unlocked(obj);
  1011. return ret;
  1012. }
  1013. static int ast_cursor_move(struct drm_crtc *crtc,
  1014. int x, int y)
  1015. {
  1016. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  1017. struct ast_private *ast = crtc->dev->dev_private;
  1018. int x_offset, y_offset;
  1019. u8 *sig;
  1020. sig = (u8 *)ast->cache_kmap.virtual + (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor + AST_HWC_SIZE;
  1021. writel(x, sig + AST_HWC_SIGNATURE_X);
  1022. writel(y, sig + AST_HWC_SIGNATURE_Y);
  1023. x_offset = ast_crtc->offset_x;
  1024. y_offset = ast_crtc->offset_y;
  1025. if (x < 0) {
  1026. x_offset = (-x) + ast_crtc->offset_x;
  1027. x = 0;
  1028. }
  1029. if (y < 0) {
  1030. y_offset = (-y) + ast_crtc->offset_y;
  1031. y = 0;
  1032. }
  1033. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc2, x_offset);
  1034. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc3, y_offset);
  1035. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc4, (x & 0xff));
  1036. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc5, ((x >> 8) & 0x0f));
  1037. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc6, (y & 0xff));
  1038. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc7, ((y >> 8) & 0x07));
  1039. /* dummy write to fire HWC */
  1040. ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xCB, 0xFF, 0x00);
  1041. return 0;
  1042. }