lcd.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * (C) Copyright 2007
  3. * Stefan Roese, DENX Software Engineering, sr@denx.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 <config.h>
  24. #include <common.h>
  25. #include <command.h>
  26. #include <i2c.h>
  27. #include <miiphy.h>
  28. #ifdef CONFIG_TAISHAN
  29. #define LCD_DELAY_NORMAL_US 100
  30. #define LCD_DELAY_NORMAL_MS 2
  31. #define LCD_CMD_ADDR ((volatile char *)(CONFIG_SYS_EBC2_LCM_BASE))
  32. #define LCD_DATA_ADDR ((volatile char *)(CONFIG_SYS_EBC2_LCM_BASE+1))
  33. #define LCD_BLK_CTRL ((volatile char *)(CONFIG_SYS_EBC1_FPGA_BASE+0x2))
  34. #define mdelay(t) ({unsigned long msec=(t); while (msec--) { udelay(1000);}})
  35. static int g_lcd_init_b = 0;
  36. static char *amcc_logo = " AMCC TAISHAN 440GX EvalBoard";
  37. static char addr_flag = 0x80;
  38. static void lcd_bl_ctrl(char val)
  39. {
  40. char cpld_val;
  41. cpld_val = *LCD_BLK_CTRL;
  42. *LCD_BLK_CTRL = val | cpld_val;
  43. }
  44. static void lcd_putc(char val)
  45. {
  46. int i = 100;
  47. char addr;
  48. while (i--) {
  49. if ((*LCD_CMD_ADDR & 0x80) != 0x80) { /*BF = 1 ? */
  50. udelay(LCD_DELAY_NORMAL_US);
  51. break;
  52. }
  53. udelay(LCD_DELAY_NORMAL_US);
  54. }
  55. if (*LCD_CMD_ADDR & 0x80) {
  56. printf("LCD is busy\n");
  57. return;
  58. }
  59. addr = *LCD_CMD_ADDR;
  60. udelay(LCD_DELAY_NORMAL_US);
  61. if ((addr != 0) && (addr % 0x10 == 0)) {
  62. addr_flag ^= 0x40;
  63. *LCD_CMD_ADDR = addr_flag;
  64. }
  65. udelay(LCD_DELAY_NORMAL_US);
  66. *LCD_DATA_ADDR = val;
  67. udelay(LCD_DELAY_NORMAL_US);
  68. }
  69. static void lcd_puts(char *s)
  70. {
  71. char *p = s;
  72. int i = 100;
  73. while (i--) {
  74. if ((*LCD_CMD_ADDR & 0x80) != 0x80) { /*BF = 1 ? */
  75. udelay(LCD_DELAY_NORMAL_US);
  76. break;
  77. }
  78. udelay(LCD_DELAY_NORMAL_US);
  79. }
  80. if (*LCD_CMD_ADDR & 0x80) {
  81. printf("LCD is busy\n");
  82. return;
  83. }
  84. while (*p)
  85. lcd_putc(*p++);
  86. }
  87. static void lcd_put_logo(void)
  88. {
  89. int i = 100;
  90. char *p = amcc_logo;
  91. while (i--) {
  92. if ((*LCD_CMD_ADDR & 0x80) != 0x80) { /*BF = 1 ? */
  93. udelay(LCD_DELAY_NORMAL_US);
  94. break;
  95. }
  96. udelay(LCD_DELAY_NORMAL_US);
  97. }
  98. if (*LCD_CMD_ADDR & 0x80) {
  99. printf("LCD is busy\n");
  100. return;
  101. }
  102. *LCD_CMD_ADDR = 0x80;
  103. while (*p)
  104. lcd_putc(*p++);
  105. }
  106. int lcd_init(void)
  107. {
  108. if (g_lcd_init_b == 0) {
  109. puts("LCD: ");
  110. mdelay(100); /* Waiting for the LCD initialize */
  111. *LCD_CMD_ADDR = 0x38; /*set function:8-bit,2-line,5x7 font type */
  112. udelay(LCD_DELAY_NORMAL_US);
  113. *LCD_CMD_ADDR = 0x0f; /*set display on,cursor on,blink on */
  114. udelay(LCD_DELAY_NORMAL_US);
  115. *LCD_CMD_ADDR = 0x01; /*display clear */
  116. mdelay(LCD_DELAY_NORMAL_MS);
  117. *LCD_CMD_ADDR = 0x06; /*set entry */
  118. udelay(LCD_DELAY_NORMAL_US);
  119. lcd_bl_ctrl(0x02);
  120. lcd_put_logo();
  121. puts(" ready\n");
  122. g_lcd_init_b = 1;
  123. }
  124. return 0;
  125. }
  126. static int do_lcd_test(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  127. {
  128. lcd_init();
  129. return 0;
  130. }
  131. static int do_lcd_clear(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  132. {
  133. *LCD_CMD_ADDR = 0x01;
  134. mdelay(LCD_DELAY_NORMAL_MS);
  135. return 0;
  136. }
  137. static int do_lcd_puts(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  138. {
  139. if (argc < 2) {
  140. cmd_usage(cmdtp);
  141. return 1;
  142. }
  143. lcd_puts(argv[1]);
  144. return 0;
  145. }
  146. static int do_lcd_putc(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  147. {
  148. if (argc < 2) {
  149. cmd_usage(cmdtp);
  150. return 1;
  151. }
  152. lcd_putc((char)argv[1][0]);
  153. return 0;
  154. }
  155. static int do_lcd_cur(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  156. {
  157. ulong count;
  158. ulong dir;
  159. char cur_addr;
  160. if (argc < 3) {
  161. cmd_usage(cmdtp);
  162. return 1;
  163. }
  164. count = simple_strtoul(argv[1], NULL, 16);
  165. if (count > 31) {
  166. printf("unable to shift > 0x20\n");
  167. count = 0;
  168. }
  169. dir = simple_strtoul(argv[2], NULL, 16);
  170. cur_addr = *LCD_CMD_ADDR;
  171. udelay(LCD_DELAY_NORMAL_US);
  172. if (dir == 0x0) {
  173. if (addr_flag == 0x80) {
  174. if (count >= (cur_addr & 0xf)) {
  175. *LCD_CMD_ADDR = 0x80;
  176. udelay(LCD_DELAY_NORMAL_US);
  177. count = 0;
  178. }
  179. } else {
  180. if (count >= ((cur_addr & 0x0f) + 0x0f)) {
  181. *LCD_CMD_ADDR = 0x80;
  182. addr_flag = 0x80;
  183. udelay(LCD_DELAY_NORMAL_US);
  184. count = 0x0;
  185. } else if (count >= (cur_addr & 0xf)) {
  186. count -= cur_addr & 0xf;
  187. *LCD_CMD_ADDR = 0x80 | 0xf;
  188. addr_flag = 0x80;
  189. udelay(LCD_DELAY_NORMAL_US);
  190. }
  191. }
  192. } else {
  193. if (addr_flag == 0x80) {
  194. if (count >= (0x1f - (cur_addr & 0xf))) {
  195. count = 0x0;
  196. addr_flag = 0xc0;
  197. *LCD_CMD_ADDR = 0xc0 | 0xf;
  198. udelay(LCD_DELAY_NORMAL_US);
  199. } else if ((count + (cur_addr & 0xf)) >= 0x0f) {
  200. count = count + (cur_addr & 0xf) - 0x0f;
  201. addr_flag = 0xc0;
  202. *LCD_CMD_ADDR = 0xc0;
  203. udelay(LCD_DELAY_NORMAL_US);
  204. }
  205. } else if ((count + (cur_addr & 0xf)) >= 0x0f) {
  206. count = 0x0;
  207. *LCD_CMD_ADDR = 0xc0 | 0xf;
  208. udelay(LCD_DELAY_NORMAL_US);
  209. }
  210. }
  211. while (count--) {
  212. if (dir == 0) {
  213. *LCD_CMD_ADDR = 0x10;
  214. } else {
  215. *LCD_CMD_ADDR = 0x14;
  216. }
  217. udelay(LCD_DELAY_NORMAL_US);
  218. }
  219. return 0;
  220. }
  221. U_BOOT_CMD(lcd_test, 1, 1, do_lcd_test, "lcd test display", "");
  222. U_BOOT_CMD(lcd_cls, 1, 1, do_lcd_clear, "lcd clear display", "");
  223. U_BOOT_CMD(lcd_puts, 2, 1, do_lcd_puts,
  224. "display string on lcd",
  225. "<string> - <string> to be displayed");
  226. U_BOOT_CMD(lcd_putc, 2, 1, do_lcd_putc,
  227. "display char on lcd",
  228. "<char> - <char> to be displayed");
  229. U_BOOT_CMD(lcd_cur, 3, 1, do_lcd_cur,
  230. "shift cursor on lcd",
  231. "<count> <dir>- shift cursor on lcd <count> times, direction is <dir> \n"
  232. " <count> - 0~31\n" " <dir> - 0,backward; 1, forward");
  233. #if 0 /* test-only */
  234. void set_phy_loopback_mode(void)
  235. {
  236. char devemac2[32];
  237. char devemac3[32];
  238. sprintf(devemac2, "%s2", CONFIG_EMAC_DEV_NAME);
  239. sprintf(devemac3, "%s3", CONFIG_EMAC_DEV_NAME);
  240. #if 0
  241. unsigned short reg_short;
  242. miiphy_read(devemac2, 0x1, 1, &reg_short);
  243. if (reg_short & 0x04) {
  244. /*
  245. * printf("EMAC2 link up,do nothing\n");
  246. */
  247. } else {
  248. udelay(1000);
  249. miiphy_write(devemac2, 0x1, 0, 0x6000);
  250. udelay(1000);
  251. miiphy_read(devemac2, 0x1, 0, &reg_short);
  252. if (reg_short != 0x6000) {
  253. printf
  254. ("\nEMAC2 error set LOOPBACK mode error,reg2[0]=%x\n",
  255. reg_short);
  256. }
  257. }
  258. miiphy_read(devemac3, 0x3, 1, &reg_short);
  259. if (reg_short & 0x04) {
  260. /*
  261. * printf("EMAC3 link up,do nothing\n");
  262. */
  263. } else {
  264. udelay(1000);
  265. miiphy_write(devemac3, 0x3, 0, 0x6000);
  266. udelay(1000);
  267. miiphy_read(devemac3, 0x3, 0, &reg_short);
  268. if (reg_short != 0x6000) {
  269. printf
  270. ("\nEMAC3 error set LOOPBACK mode error,reg2[0]=%x\n",
  271. reg_short);
  272. }
  273. }
  274. #else
  275. /* Set PHY as LOOPBACK MODE, for Linux emac initializing */
  276. miiphy_write(devemac2, CONFIG_PHY2_ADDR, 0, 0x6000);
  277. udelay(1000);
  278. miiphy_write(devemac3, CONFIG_PHY3_ADDR, 0, 0x6000);
  279. udelay(1000);
  280. #endif /* 0 */
  281. }
  282. void set_phy_normal_mode(void)
  283. {
  284. char devemac2[32];
  285. char devemac3[32];
  286. unsigned short reg_short;
  287. sprintf(devemac2, "%s2", CONFIG_EMAC_DEV_NAME);
  288. sprintf(devemac3, "%s3", CONFIG_EMAC_DEV_NAME);
  289. /* Set phy of EMAC2 */
  290. miiphy_read(devemac2, CONFIG_PHY2_ADDR, 0x16, &reg_short);
  291. reg_short &= ~(0x7);
  292. reg_short |= 0x6; /* RGMII DLL Delay */
  293. miiphy_write(devemac2, CONFIG_PHY2_ADDR, 0x16, reg_short);
  294. miiphy_read(devemac2, CONFIG_PHY2_ADDR, 0x17, &reg_short);
  295. reg_short &= ~(0x40);
  296. miiphy_write(devemac2, CONFIG_PHY2_ADDR, 0x17, reg_short);
  297. miiphy_write(devemac2, CONFIG_PHY2_ADDR, 0x1c, 0x74f0);
  298. /* Set phy of EMAC3 */
  299. miiphy_read(devemac3, CONFIG_PHY3_ADDR, 0x16, &reg_short);
  300. reg_short &= ~(0x7);
  301. reg_short |= 0x6; /* RGMII DLL Delay */
  302. miiphy_write(devemac3, CONFIG_PHY3_ADDR, 0x16, reg_short);
  303. miiphy_read(devemac3, CONFIG_PHY3_ADDR, 0x17, &reg_short);
  304. reg_short &= ~(0x40);
  305. miiphy_write(devemac3, CONFIG_PHY3_ADDR, 0x17, reg_short);
  306. miiphy_write(devemac3, CONFIG_PHY3_ADDR, 0x1c, 0x74f0);
  307. }
  308. #endif /* 0 - test only */
  309. static int do_led_test_off(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  310. {
  311. volatile unsigned int *GpioOr =
  312. (volatile unsigned int *)(CONFIG_SYS_PERIPHERAL_BASE + 0x700);
  313. *GpioOr |= 0x00300000;
  314. return 0;
  315. }
  316. static int do_led_test_on(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  317. {
  318. volatile unsigned int *GpioOr =
  319. (volatile unsigned int *)(CONFIG_SYS_PERIPHERAL_BASE + 0x700);
  320. *GpioOr &= ~0x00300000;
  321. return 0;
  322. }
  323. U_BOOT_CMD(ledon, 1, 1, do_led_test_on,
  324. "led test light on", "");
  325. U_BOOT_CMD(ledoff, 1, 1, do_led_test_off,
  326. "led test light off", "");
  327. #endif