osd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 114285000
  30. #define ICS8N3QV01_FREF_LL 114285000LL
  31. #define ICS8N3QV01_F_DEFAULT_0 156250000LL
  32. #define ICS8N3QV01_F_DEFAULT_1 125000000LL
  33. #define ICS8N3QV01_F_DEFAULT_2 100000000LL
  34. #define ICS8N3QV01_F_DEFAULT_3 25175000LL
  35. #define SIL1178_MASTER_I2C_ADDRESS 0x38
  36. #define SIL1178_SLAVE_I2C_ADDRESS 0x39
  37. #define PIXCLK_640_480_60 25180000
  38. #define BASE_WIDTH 32
  39. #define BASE_HEIGHT 16
  40. #define BUFSIZE (BASE_WIDTH * BASE_HEIGHT)
  41. enum {
  42. CH7301_CM = 0x1c, /* Clock Mode Register */
  43. CH7301_IC = 0x1d, /* Input Clock Register */
  44. CH7301_GPIO = 0x1e, /* GPIO Control Register */
  45. CH7301_IDF = 0x1f, /* Input Data Format Register */
  46. CH7301_CD = 0x20, /* Connection Detect Register */
  47. CH7301_DC = 0x21, /* DAC Control Register */
  48. CH7301_HPD = 0x23, /* Hot Plug Detection Register */
  49. CH7301_TCTL = 0x31, /* DVI Control Input Register */
  50. CH7301_TPCP = 0x33, /* DVI PLL Charge Pump Ctrl Register */
  51. CH7301_TPD = 0x34, /* DVI PLL Divide Register */
  52. CH7301_TPVT = 0x35, /* DVI PLL Supply Control Register */
  53. CH7301_TPF = 0x36, /* DVI PLL Filter Register */
  54. CH7301_TCT = 0x37, /* DVI Clock Test Register */
  55. CH7301_TSTP = 0x48, /* Test Pattern Register */
  56. CH7301_PM = 0x49, /* Power Management register */
  57. CH7301_VID = 0x4a, /* Version ID Register */
  58. CH7301_DID = 0x4b, /* Device ID Register */
  59. CH7301_DSP = 0x56, /* DVI Sync polarity Register */
  60. };
  61. #if defined(CONFIG_SYS_ICS8N3QV01) || defined(CONFIG_SYS_SIL1178)
  62. static void fpga_iic_write(unsigned screen, u8 slave, u8 reg, u8 data)
  63. {
  64. struct ihs_fpga *fpga = (struct ihs_fpga *)CONFIG_SYS_FPGA_BASE(screen);
  65. struct ihs_i2c *i2c = &fpga->i2c;
  66. while (in_le16(&fpga->extended_interrupt) & (1 << 12))
  67. ;
  68. out_le16(&i2c->write_mailbox_ext, reg | (data << 8));
  69. out_le16(&i2c->write_mailbox, 0xc400 | (slave << 1));
  70. }
  71. static u8 fpga_iic_read(unsigned screen, u8 slave, u8 reg)
  72. {
  73. struct ihs_fpga *fpga = (struct ihs_fpga *)CONFIG_SYS_FPGA_BASE(screen);
  74. struct ihs_i2c *i2c = &fpga->i2c;
  75. unsigned int ctr = 0;
  76. while (in_le16(&fpga->extended_interrupt) & (1 << 12))
  77. ;
  78. out_le16(&fpga->extended_interrupt, 1 << 14);
  79. out_le16(&i2c->write_mailbox_ext, reg);
  80. out_le16(&i2c->write_mailbox, 0xc000 | (slave << 1));
  81. while (!(in_le16(&fpga->extended_interrupt) & (1 << 14))) {
  82. udelay(100000);
  83. if (ctr++ > 5) {
  84. printf("iic receive timeout\n");
  85. break;
  86. }
  87. }
  88. return in_le16(&i2c->read_mailbox_ext) >> 8;
  89. }
  90. #endif
  91. #ifdef CONFIG_SYS_MPC92469AC
  92. static void mpc92469ac_calc_parameters(unsigned int fout,
  93. unsigned int *post_div, unsigned int *feedback_div)
  94. {
  95. unsigned int n = *post_div;
  96. unsigned int m = *feedback_div;
  97. unsigned int a;
  98. unsigned int b = 14745600 / 16;
  99. if (fout < 50169600)
  100. n = 8;
  101. else if (fout < 100339199)
  102. n = 4;
  103. else if (fout < 200678399)
  104. n = 2;
  105. else
  106. n = 1;
  107. a = fout * n + (b / 2); /* add b/2 for proper rounding */
  108. m = a / b;
  109. *post_div = n;
  110. *feedback_div = m;
  111. }
  112. static void mpc92469ac_set(unsigned screen, unsigned int fout)
  113. {
  114. struct ihs_fpga *fpga = (struct ihs_fpga *)CONFIG_SYS_FPGA_BASE(screen);
  115. unsigned int n;
  116. unsigned int m;
  117. unsigned int bitval = 0;
  118. mpc92469ac_calc_parameters(fout, &n, &m);
  119. switch (n) {
  120. case 1:
  121. bitval = 0x00;
  122. break;
  123. case 2:
  124. bitval = 0x01;
  125. break;
  126. case 4:
  127. bitval = 0x02;
  128. break;
  129. case 8:
  130. bitval = 0x03;
  131. break;
  132. }
  133. out_le16(&fpga->mpc3w_control, (bitval << 9) | m);
  134. }
  135. #endif
  136. #ifdef CONFIG_SYS_ICS8N3QV01
  137. static unsigned int ics8n3qv01_get_fout_calc(unsigned screen, unsigned index)
  138. {
  139. unsigned long long n;
  140. unsigned long long mint;
  141. unsigned long long mfrac;
  142. u8 reg_a, reg_b, reg_c, reg_d, reg_f;
  143. unsigned long long fout_calc;
  144. if (index > 3)
  145. return 0;
  146. reg_a = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 0 + index);
  147. reg_b = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 4 + index);
  148. reg_c = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 8 + index);
  149. reg_d = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 12 + index);
  150. reg_f = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 20 + index);
  151. mint = ((reg_a >> 1) & 0x1f) | (reg_f & 0x20);
  152. mfrac = ((reg_a & 0x01) << 17) | (reg_b << 9) | (reg_c << 1)
  153. | (reg_d >> 7);
  154. n = reg_d & 0x7f;
  155. fout_calc = (mint * ICS8N3QV01_FREF_LL
  156. + mfrac * ICS8N3QV01_FREF_LL / 262144LL
  157. + ICS8N3QV01_FREF_LL / 524288LL
  158. + n / 2)
  159. / n
  160. * 1000000
  161. / (1000000 - 100);
  162. return fout_calc;
  163. }
  164. static void ics8n3qv01_calc_parameters(unsigned int fout,
  165. unsigned int *_mint, unsigned int *_mfrac,
  166. unsigned int *_n)
  167. {
  168. unsigned int n;
  169. unsigned int foutiic;
  170. unsigned int fvcoiic;
  171. unsigned int mint;
  172. unsigned long long mfrac;
  173. n = (2215000000U + fout / 2) / fout;
  174. if ((n & 1) && (n > 5))
  175. n -= 1;
  176. foutiic = fout - (fout / 10000);
  177. fvcoiic = foutiic * n;
  178. mint = fvcoiic / 114285000;
  179. if ((mint < 17) || (mint > 63))
  180. printf("ics8n3qv01_calc_parameters: cannot determine mint\n");
  181. mfrac = ((unsigned long long)fvcoiic % 114285000LL) * 262144LL
  182. / 114285000LL;
  183. *_mint = mint;
  184. *_mfrac = mfrac;
  185. *_n = n;
  186. }
  187. static void ics8n3qv01_set(unsigned screen, unsigned int fout)
  188. {
  189. unsigned int n;
  190. unsigned int mint;
  191. unsigned int mfrac;
  192. unsigned int fout_calc;
  193. unsigned long long fout_prog;
  194. long long off_ppm;
  195. u8 reg0, reg4, reg8, reg12, reg18, reg20;
  196. fout_calc = ics8n3qv01_get_fout_calc(screen, 1);
  197. off_ppm = (fout_calc - ICS8N3QV01_F_DEFAULT_1) * 1000000
  198. / ICS8N3QV01_F_DEFAULT_1;
  199. printf(" PLL is off by %lld ppm\n", off_ppm);
  200. fout_prog = (unsigned long long)fout * (unsigned long long)fout_calc
  201. / ICS8N3QV01_F_DEFAULT_1;
  202. ics8n3qv01_calc_parameters(fout_prog, &mint, &mfrac, &n);
  203. reg0 = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 0) & 0xc0;
  204. reg0 |= (mint & 0x1f) << 1;
  205. reg0 |= (mfrac >> 17) & 0x01;
  206. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 0, reg0);
  207. reg4 = mfrac >> 9;
  208. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 4, reg4);
  209. reg8 = mfrac >> 1;
  210. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 8, reg8);
  211. reg12 = mfrac << 7;
  212. reg12 |= n & 0x7f;
  213. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 12, reg12);
  214. reg18 = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 18) & 0x03;
  215. reg18 |= 0x20;
  216. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 18, reg18);
  217. reg20 = fpga_iic_read(screen, ICS8N3QV01_I2C_ADDR, 20) & 0x1f;
  218. reg20 |= mint & (1 << 5);
  219. fpga_iic_write(screen, ICS8N3QV01_I2C_ADDR, 20, reg20);
  220. }
  221. #endif
  222. static int osd_write_videomem(unsigned screen, unsigned offset,
  223. u16 *data, size_t charcount)
  224. {
  225. struct ihs_fpga *fpga =
  226. (struct ihs_fpga *) CONFIG_SYS_FPGA_BASE(screen);
  227. unsigned int k;
  228. for (k = 0; k < charcount; ++k) {
  229. if (offset + k >= BUFSIZE)
  230. return -1;
  231. out_le16(&fpga->videomem + offset + k, data[k]);
  232. }
  233. return charcount;
  234. }
  235. static int osd_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  236. {
  237. unsigned screen;
  238. for (screen = 0; screen < CONFIG_SYS_OSD_SCREENS; ++screen) {
  239. unsigned x;
  240. unsigned y;
  241. unsigned charcount;
  242. unsigned len;
  243. u8 color;
  244. unsigned int k;
  245. u16 buf[BUFSIZE];
  246. char *text;
  247. int res;
  248. if (argc < 5) {
  249. cmd_usage(cmdtp);
  250. return 1;
  251. }
  252. x = simple_strtoul(argv[1], NULL, 16);
  253. y = simple_strtoul(argv[2], NULL, 16);
  254. color = simple_strtoul(argv[3], NULL, 16);
  255. text = argv[4];
  256. charcount = strlen(text);
  257. len = (charcount > BUFSIZE) ? BUFSIZE : charcount;
  258. for (k = 0; k < len; ++k)
  259. buf[k] = (text[k] << 8) | color;
  260. res = osd_write_videomem(screen, y * BASE_WIDTH + x, buf, len);
  261. if (res < 0)
  262. return res;
  263. }
  264. return 0;
  265. }
  266. int osd_probe(unsigned screen)
  267. {
  268. struct ihs_fpga *fpga = (struct ihs_fpga *)CONFIG_SYS_FPGA_BASE(screen);
  269. struct ihs_osd *osd = &fpga->osd;
  270. u16 version = in_le16(&osd->version);
  271. u16 features = in_le16(&osd->features);
  272. unsigned width;
  273. unsigned height;
  274. u8 value;
  275. width = ((features & 0x3f00) >> 8) + 1;
  276. height = (features & 0x001f) + 1;
  277. printf("OSD%d: Digital-OSD version %01d.%02d, %d" "x%d characters\n",
  278. screen, version/100, version%100, width, height);
  279. #ifdef CONFIG_SYS_CH7301
  280. value = i2c_reg_read(CH7301_I2C_ADDR, CH7301_DID);
  281. if (value != 0x17) {
  282. printf(" Probing CH7301 failed, DID %02x\n", value);
  283. return -1;
  284. }
  285. i2c_reg_write(CH7301_I2C_ADDR, CH7301_TPCP, 0x08);
  286. i2c_reg_write(CH7301_I2C_ADDR, CH7301_TPD, 0x16);
  287. i2c_reg_write(CH7301_I2C_ADDR, CH7301_TPF, 0x60);
  288. i2c_reg_write(CH7301_I2C_ADDR, CH7301_DC, 0x09);
  289. i2c_reg_write(CH7301_I2C_ADDR, CH7301_PM, 0xc0);
  290. #endif
  291. #ifdef CONFIG_SYS_MPC92469AC
  292. mpc92469ac_set(screen, PIXCLK_640_480_60);
  293. #endif
  294. #ifdef CONFIG_SYS_ICS8N3QV01
  295. ics8n3qv01_set(screen, PIXCLK_640_480_60);
  296. #endif
  297. #ifdef CONFIG_SYS_SIL1178
  298. value = fpga_iic_read(screen, SIL1178_SLAVE_I2C_ADDRESS, 0x02);
  299. if (value != 0x06) {
  300. printf(" Probing CH7301 SIL1178, DEV_IDL %02x\n", value);
  301. return -1;
  302. }
  303. /* magic initialization sequence adapted from datasheet */
  304. fpga_iic_write(screen, SIL1178_SLAVE_I2C_ADDRESS, 0x08, 0x36);
  305. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0f, 0x44);
  306. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0f, 0x4c);
  307. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0e, 0x10);
  308. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0a, 0x80);
  309. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x09, 0x30);
  310. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0c, 0x89);
  311. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x0d, 0x60);
  312. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x08, 0x36);
  313. fpga_iic_write(screen, SIL1178_MASTER_I2C_ADDRESS, 0x08, 0x37);
  314. #endif
  315. out_le16(&fpga->videocontrol, 0x0002);
  316. out_le16(&osd->control, 0x0049);
  317. out_le16(&osd->xy_size, ((32 - 1) << 8) | (16 - 1));
  318. out_le16(&osd->x_pos, 0x007f);
  319. out_le16(&osd->y_pos, 0x005f);
  320. return 0;
  321. }
  322. int osd_write(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  323. {
  324. unsigned screen;
  325. for (screen = 0; screen < CONFIG_SYS_OSD_SCREENS; ++screen) {
  326. unsigned x;
  327. unsigned y;
  328. unsigned k;
  329. u16 buffer[BASE_WIDTH];
  330. char *rp;
  331. u16 *wp = buffer;
  332. unsigned count = (argc > 4) ?
  333. simple_strtoul(argv[4], NULL, 16) : 1;
  334. if ((argc < 4) || (strlen(argv[3]) % 4)) {
  335. cmd_usage(cmdtp);
  336. return 1;
  337. }
  338. x = simple_strtoul(argv[1], NULL, 16);
  339. y = simple_strtoul(argv[2], NULL, 16);
  340. rp = argv[3];
  341. while (*rp) {
  342. char substr[5];
  343. memcpy(substr, rp, 4);
  344. substr[4] = 0;
  345. *wp = simple_strtoul(substr, NULL, 16);
  346. rp += 4;
  347. wp++;
  348. if (wp - buffer > BASE_WIDTH)
  349. break;
  350. }
  351. for (k = 0; k < count; ++k) {
  352. unsigned offset =
  353. y * BASE_WIDTH + x + k * (wp - buffer);
  354. osd_write_videomem(screen, offset, buffer,
  355. wp - buffer);
  356. }
  357. }
  358. return 0;
  359. }
  360. U_BOOT_CMD(
  361. osdw, 5, 0, osd_write,
  362. "write 16-bit hex encoded buffer to osd memory",
  363. "pos_x pos_y buffer count\n"
  364. );
  365. U_BOOT_CMD(
  366. osdp, 5, 0, osd_print,
  367. "write ASCII buffer to osd memory",
  368. "pos_x pos_y color text\n"
  369. );