lcd.c 8.6 KB

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