ast_mode.c 34 KB

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