ast_mode.c 34 KB

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