osd.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * (C) Copyright 2010
  3. * Dirk Eibach, Guntermann & Drunck GmbH, eibach@gdsys.de
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <i2c.h>
  25. #include <asm/io.h>
  26. #include <gdsys_fpga.h>
  27. #define CH7301_I2C_ADDR 0x75
  28. #define ICS8N3QV01_I2C_ADDR 0x6E
  29. #define ICS8N3QV01_FREF 114285
  30. #define SIL1178_MASTER_I2C_ADDRESS 0x38
  31. #define SIL1178_SLAVE_I2C_ADDRESS 0x39
  32. #define PIXCLK_640_480_60 25180000
  33. #define BASE_WIDTH 32
  34. #define BASE_HEIGHT 16
  35. #define BUFSIZE (BASE_WIDTH * BASE_HEIGHT)
  36. enum {
  37. CH7301_CM = 0x1c, /* Clock Mode Register */
  38. CH7301_IC = 0x1d, /* Input Clock Register */
  39. CH7301_GPIO = 0x1e, /* GPIO Control Register */
  40. CH7301_IDF = 0x1f, /* Input Data Format Register */
  41. CH7301_CD = 0x20, /* Connection Detect Register */
  42. CH7301_DC = 0x21, /* DAC Control Register */
  43. CH7301_HPD = 0x23, /* Hot Plug Detection Register */
  44. CH7301_TCTL = 0x31, /* DVI Control Input Register */
  45. CH7301_TPCP = 0x33, /* DVI PLL Charge Pump Ctrl Register */
  46. CH7301_TPD = 0x34, /* DVI PLL Divide Register */
  47. CH7301_TPVT = 0x35, /* DVI PLL Supply Control Register */
  48. CH7301_TPF = 0x36, /* DVI PLL Filter Register */
  49. CH7301_TCT = 0x37, /* DVI Clock Test Register */
  50. CH7301_TSTP = 0x48, /* Test Pattern Register */
  51. CH7301_PM = 0x49, /* Power Management register */
  52. CH7301_VID = 0x4a, /* Version ID Register */
  53. CH7301_DID = 0x4b, /* Device ID Register */
  54. CH7301_DSP = 0x56, /* DVI Sync polarity Register */
  55. };
  56. #if defined(CONFIG_SYS_ICS8N3QV01) || defined(CONFIG_SYS_SIL1178)
  57. static void fpga_iic_write(unsigned screen, u8 slave, u8 reg, u8 data)
  58. {
  59. ihs_fpga_t *fpga = (ihs_fpga_t *) CONFIG_SYS_FPGA_BASE(screen);
  60. ihs_i2c_t *i2c = &fpga->i2c;
  61. while (in_le16(&fpga->extended_interrupt) & (1 << 12))
  62. ;
  63. out_le16(&i2c->write_mailbox_ext, reg | (data << 8));
  64. out_le16(&i2c->write_mailbox, 0xc400 | (slave << 1));
  65. }
  66. static u8 fpga_iic_read(unsigned screen, u8 slave, u8 reg)
  67. {
  68. ihs_fpga_t *fpga = (ihs_fpga_t *) CONFIG_SYS_FPGA_BASE(screen);
  69. ihs_i2c_t *i2c = &fpga->i2c;
  70. unsigned int ctr = 0;
  71. while (in_le16(&fpga->extended_interrupt) & (1 << 12))
  72. ;
  73. out_le16(&fpga->extended_interrupt, 1 << 14);
  74. out_le16(&i2c->write_mailbox_ext, reg);
  75. out_le16(&i2c->write_mailbox, 0xc000 | (slave << 1));
  76. while (!(in_le16(&fpga->extended_interrupt) & (1 << 14))) {
  77. udelay(100000);
  78. if (ctr++ > 5) {
  79. printf("iic receive timeout\n");
  80. break;
  81. }
  82. }
  83. return in_le16(&i2c->read_mailbox_ext) >> 8;
  84. }
  85. #endif
  86. #ifdef CONFIG_SYS_MPC92469AC
  87. static void mpc92469ac_calc_parameters(unsigned int fout,
  88. unsigned int *post_div, unsigned int *feedback_div)
  89. {
  90. unsigned int n = *post_div;
  91. unsigned int m = *feedback_div;
  92. unsigned int a;
  93. unsigned int b = 14745600 / 16;
  94. if (fout < 50169600)
  95. n = 8;
  96. else if (fout < 100339199)
  97. n = 4;
  98. else if (fout < 200678399)
  99. n = 2;
  100. else
  101. n = 1;
  102. a = fout * n + (b / 2); /* add b/2 for proper rounding */
  103. m = a / b;
  104. *post_div = n;
  105. *feedback_div = m;
  106. }
  107. static void mpc92469ac_set(unsigned screen, unsigned int fout)
  108. {
  109. ihs_fpga_t *fpga = (ihs_fpga_t *) CONFIG_SYS_FPGA_BASE(screen);
  110. unsigned int n;
  111. unsigned int m;
  112. unsigned int bitval = 0;
  113. mpc92469ac_calc_parameters(fout, &n, &m);
  114. switch (n) {
  115. case 1:
  116. bitval = 0x00;
  117. break;
  118. case 2:
  119. bitval = 0x01;
  120. break;
  121. case 4:
  122. bitval = 0x02;
  123. break;
  124. case 8:
  125. bitval = 0x03;
  126. break;
  127. }
  128. out_le16(&fpga->mpc3w_control, (bitval << 9) | m);
  129. }
  130. #endif
  131. #ifdef CONFIG_SYS_ICS8N3QV01
  132. static void ics8n3qv01_calc_parameters(unsigned int fout,
  133. unsigned int *_mint, unsigned int *_mfrac,
  134. unsigned int *_n)
  135. {
  136. unsigned int n;
  137. unsigned int foutiic;
  138. unsigned int fvcoiic;
  139. unsigned int mint;
  140. unsigned long long mfrac;
  141. n = 2550000000U / fout;
  142. if ((n & 1) && (n > 5))
  143. n -= 1;
  144. foutiic = fout - (fout / 10000);
  145. fvcoiic = foutiic * n;
  146. mint = fvcoiic / 114285000;
  147. if ((mint < 17) || (mint > 63))
  148. printf("ics8n3qv01_calc_parameters: cannot determine mint\n");
  149. mfrac = ((unsigned long long)fvcoiic % 114285000LL) * 262144LL
  150. / 114285000LL;
  151. *_mint = mint;
  152. *_mfrac = mfrac;
  153. *_n = n;
  154. }
  155. static void ics8n3qv01_set(unsigned screen, unsigned int fout)
  156. {
  157. unsigned int n;
  158. unsigned int mint;
  159. unsigned int mfrac;
  160. u8 reg0, reg4, reg8, reg12, reg18, reg20;
  161. ics8n3qv01_calc_parameters(fout, &mint, &mfrac, &n);
  162. reg0 = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 0) & 0xc0;
  163. reg0 |= (mint & 0x1f) << 1;
  164. reg0 |= (mfrac >> 17) & 0x01;
  165. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 0, reg0);
  166. reg4 = mfrac >> 9;
  167. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 4, reg4);
  168. reg8 = mfrac >> 1;
  169. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 8, reg8);
  170. reg12 = mfrac << 7;
  171. reg12 |= n & 0x7f;
  172. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 12, reg12);
  173. reg18 = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 18) & 0x03;
  174. reg18 |= 0x20;
  175. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 18, reg18);
  176. reg20 = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 20) & 0x1f;
  177. reg20 |= mint & (1 << 5);
  178. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 20, reg20);
  179. }
  180. #endif
  181. static int osd_write_videomem(unsigned screen, unsigned offset,
  182. u16 *data, size_t charcount)
  183. {
  184. ihs_fpga_t *fpga =
  185. (ihs_fpga_t *) CONFIG_SYS_FPGA_BASE(screen);
  186. unsigned int k;
  187. for (k = 0; k < charcount; ++k) {
  188. if (offset + k >= BUFSIZE)
  189. return -1;
  190. out_le16(&fpga->videomem + offset + k, data[k]);
  191. }
  192. return charcount;
  193. }
  194. static int osd_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  195. {
  196. unsigned screen;
  197. for (screen = 0; screen < CONFIG_SYS_OSD_SCREENS; ++screen) {
  198. unsigned x;
  199. unsigned y;
  200. unsigned charcount;
  201. unsigned len;
  202. u8 color;
  203. unsigned int k;
  204. u16 buf[BUFSIZE];
  205. char *text;
  206. int res;
  207. if (argc < 5) {
  208. cmd_usage(cmdtp);
  209. return 1;
  210. }
  211. x = simple_strtoul(argv[1], NULL, 16);
  212. y = simple_strtoul(argv[2], NULL, 16);
  213. color = simple_strtoul(argv[3], NULL, 16);
  214. text = argv[4];
  215. charcount = strlen(text);
  216. len = (charcount > BUFSIZE) ? BUFSIZE : charcount;
  217. for (k = 0; k < len; ++k)
  218. buf[k] = (text[k] << 8) | color;
  219. res = osd_write_videomem(screen, y * BASE_WIDTH + x, buf, len);
  220. if (res < 0)
  221. return res;
  222. }
  223. return 0;
  224. }
  225. int osd_probe(unsigned screen)
  226. {
  227. ihs_fpga_t *fpga = (ihs_fpga_t *) CONFIG_SYS_FPGA_BASE(screen);
  228. ihs_osd_t *osd = &fpga->osd;
  229. u16 version = in_le16(&osd->version);
  230. u16 features = in_le16(&osd->features);
  231. unsigned width;
  232. unsigned height;
  233. u8 value;
  234. width = ((features & 0x3f00) >> 8) + 1;
  235. height = (features & 0x001f) + 1;
  236. printf("OSD%d: Digital-OSD version %01d.%02d, %d" "x%d characters\n",
  237. screen, version/100, version%100, width, height);
  238. #ifdef CONFIG_SYS_CH7301
  239. value = i2c_reg_read(CH7301_I2C_ADDR, CH7301_DID);
  240. if (value != 0x17) {
  241. printf(" Probing CH7301 failed, DID %02x\n", value);
  242. return -1;
  243. }
  244. i2c_reg_write(CH7301_I2C_ADDR, CH7301_TPCP, 0x08);
  245. i2c_reg_write(CH7301_I2C_ADDR, CH7301_TPD, 0x16);
  246. i2c_reg_write(CH7301_I2C_ADDR, CH7301_TPF, 0x60);
  247. i2c_reg_write(CH7301_I2C_ADDR, CH7301_DC, 0x09);
  248. i2c_reg_write(CH7301_I2C_ADDR, CH7301_PM, 0xc0);
  249. #endif
  250. #ifdef CONFIG_SYS_MPC92469AC
  251. mpc92469ac_set(screen, PIXCLK_640_480_60);
  252. #endif
  253. #ifdef CONFIG_SYS_ICS8N3QV01
  254. ics8n3qv01_set(screen, PIXCLK_640_480_60);
  255. #endif
  256. #ifdef CONFIG_SYS_SIL1178
  257. value = fpga_iic_read(screen, SIL1178_SLAVE_I2C_ADDRESS, 0x02);
  258. if (value != 0x06) {
  259. printf(" Probing CH7301 SIL1178, DEV_IDL %02x\n", value);
  260. return -1;
  261. }
  262. /* magic initialization sequence adapted from datasheet */
  263. fpga_iic_write(screen, SIL1178_SLAVE_I2C_ADDRESS, 0x08, 0x36);
  264. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0f, 0x44);
  265. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0f, 0x4c);
  266. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0e, 0x10);
  267. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0a, 0x80);
  268. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x09, 0x30);
  269. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0c, 0x89);
  270. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0d, 0x60);
  271. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x08, 0x36);
  272. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x08, 0x37);
  273. #endif
  274. out_le16(&fpga->videocontrol, 0x0002);
  275. out_le16(&osd->control, 0x0049);
  276. out_le16(&osd->xy_size, ((32 - 1) << 8) | (16 - 1));
  277. return 0;
  278. }
  279. int osd_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  280. {
  281. unsigned screen;
  282. for (screen = 0; screen < CONFIG_SYS_OSD_SCREENS; ++screen) {
  283. unsigned x;
  284. unsigned y;
  285. unsigned k;
  286. u16 buffer[BASE_WIDTH];
  287. char *rp;
  288. u16 *wp = buffer;
  289. unsigned count = (argc > 4) ?
  290. simple_strtoul(argv[4], NULL, 16) : 1;
  291. if ((argc < 4) || (strlen(argv[3]) % 4)) {
  292. cmd_usage(cmdtp);
  293. return 1;
  294. }
  295. x = simple_strtoul(argv[1], NULL, 16);
  296. y = simple_strtoul(argv[2], NULL, 16);
  297. rp = argv[3];
  298. while (*rp) {
  299. char substr[5];
  300. memcpy(substr, rp, 4);
  301. substr[4] = 0;
  302. *wp = simple_strtoul(substr, NULL, 16);
  303. rp += 4;
  304. wp++;
  305. if (wp - buffer > BASE_WIDTH)
  306. break;
  307. }
  308. for (k = 0; k < count; ++k) {
  309. unsigned offset =
  310. y * BASE_WIDTH + x + k * (wp - buffer);
  311. osd_write_videomem(screen, offset, buffer,
  312. wp - buffer);
  313. }
  314. }
  315. return 0;
  316. }
  317. U_BOOT_CMD(
  318. osdw, 5, 0, osd_write,
  319. "write 16-bit hex encoded buffer to osd memory",
  320. "pos_x pos_y buffer count\n"
  321. );
  322. U_BOOT_CMD(
  323. osdp, 5, 0, osd_print,
  324. "write ASCII buffer to osd memory",
  325. "pos_x pos_y color text\n"
  326. );