ast_mode.c 34 KB

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