lcd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * LCD, LED and Button interface for Cobalt
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 1996, 1997 by Andrew Bose
  9. *
  10. * Linux kernel version history:
  11. * March 2001: Ported from 2.0.34 by Liam Davies
  12. *
  13. */
  14. #define RTC_IO_EXTENT 0x10 /*Only really two ports, but... */
  15. #include <linux/config.h>
  16. #include <linux/types.h>
  17. #include <linux/errno.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/slab.h>
  20. #include <linux/ioport.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/mc146818rtc.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/sched.h>
  25. #include <linux/delay.h>
  26. #include <asm/io.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/system.h>
  29. #include <linux/delay.h>
  30. #include "lcd.h"
  31. static DEFINE_SPINLOCK(lcd_lock);
  32. static int lcd_ioctl(struct inode *inode, struct file *file,
  33. unsigned int cmd, unsigned long arg);
  34. static unsigned int lcd_present = 1;
  35. /* used in arch/mips/cobalt/reset.c */
  36. int led_state = 0;
  37. #if defined(CONFIG_TULIP) && 0
  38. #define MAX_INTERFACES 8
  39. static linkcheck_func_t linkcheck_callbacks[MAX_INTERFACES];
  40. static void *linkcheck_cookies[MAX_INTERFACES];
  41. int lcd_register_linkcheck_func(int iface_num, void *func, void *cookie)
  42. {
  43. if (iface_num < 0 ||
  44. iface_num >= MAX_INTERFACES ||
  45. linkcheck_callbacks[iface_num] != NULL)
  46. return -1;
  47. linkcheck_callbacks[iface_num] = (linkcheck_func_t) func;
  48. linkcheck_cookies[iface_num] = cookie;
  49. return 0;
  50. }
  51. #endif
  52. static int lcd_ioctl(struct inode *inode, struct file *file,
  53. unsigned int cmd, unsigned long arg)
  54. {
  55. struct lcd_display button_display;
  56. unsigned long address, a;
  57. switch (cmd) {
  58. case LCD_On:
  59. udelay(150);
  60. BusyCheck();
  61. LCDWriteInst(0x0F);
  62. break;
  63. case LCD_Off:
  64. udelay(150);
  65. BusyCheck();
  66. LCDWriteInst(0x08);
  67. break;
  68. case LCD_Reset:
  69. udelay(150);
  70. LCDWriteInst(0x3F);
  71. udelay(150);
  72. LCDWriteInst(0x3F);
  73. udelay(150);
  74. LCDWriteInst(0x3F);
  75. udelay(150);
  76. LCDWriteInst(0x3F);
  77. udelay(150);
  78. LCDWriteInst(0x01);
  79. udelay(150);
  80. LCDWriteInst(0x06);
  81. break;
  82. case LCD_Clear:
  83. udelay(150);
  84. BusyCheck();
  85. LCDWriteInst(0x01);
  86. break;
  87. case LCD_Cursor_Left:
  88. udelay(150);
  89. BusyCheck();
  90. LCDWriteInst(0x10);
  91. break;
  92. case LCD_Cursor_Right:
  93. udelay(150);
  94. BusyCheck();
  95. LCDWriteInst(0x14);
  96. break;
  97. case LCD_Cursor_Off:
  98. udelay(150);
  99. BusyCheck();
  100. LCDWriteInst(0x0C);
  101. break;
  102. case LCD_Cursor_On:
  103. udelay(150);
  104. BusyCheck();
  105. LCDWriteInst(0x0F);
  106. break;
  107. case LCD_Blink_Off:
  108. udelay(150);
  109. BusyCheck();
  110. LCDWriteInst(0x0E);
  111. break;
  112. case LCD_Get_Cursor_Pos:{
  113. struct lcd_display display;
  114. udelay(150);
  115. BusyCheck();
  116. display.cursor_address = (LCDReadInst);
  117. display.cursor_address =
  118. (display.cursor_address & 0x07F);
  119. if (copy_to_user
  120. ((struct lcd_display *) arg, &display,
  121. sizeof(struct lcd_display)))
  122. return -EFAULT;
  123. break;
  124. }
  125. case LCD_Set_Cursor_Pos:{
  126. struct lcd_display display;
  127. if (copy_from_user
  128. (&display, (struct lcd_display *) arg,
  129. sizeof(struct lcd_display)))
  130. return -EFAULT;
  131. a = (display.cursor_address | kLCD_Addr);
  132. udelay(150);
  133. BusyCheck();
  134. LCDWriteInst(a);
  135. break;
  136. }
  137. case LCD_Get_Cursor:{
  138. struct lcd_display display;
  139. udelay(150);
  140. BusyCheck();
  141. display.character = LCDReadData;
  142. if (copy_to_user
  143. ((struct lcd_display *) arg, &display,
  144. sizeof(struct lcd_display)))
  145. return -EFAULT;
  146. udelay(150);
  147. BusyCheck();
  148. LCDWriteInst(0x10);
  149. break;
  150. }
  151. case LCD_Set_Cursor:{
  152. struct lcd_display display;
  153. if (copy_from_user
  154. (&display, (struct lcd_display *) arg,
  155. sizeof(struct lcd_display)))
  156. return -EFAULT;
  157. udelay(150);
  158. BusyCheck();
  159. LCDWriteData(display.character);
  160. udelay(150);
  161. BusyCheck();
  162. LCDWriteInst(0x10);
  163. break;
  164. }
  165. case LCD_Disp_Left:
  166. udelay(150);
  167. BusyCheck();
  168. LCDWriteInst(0x18);
  169. break;
  170. case LCD_Disp_Right:
  171. udelay(150);
  172. BusyCheck();
  173. LCDWriteInst(0x1C);
  174. break;
  175. case LCD_Home:
  176. udelay(150);
  177. BusyCheck();
  178. LCDWriteInst(0x02);
  179. break;
  180. case LCD_Write:{
  181. struct lcd_display display;
  182. unsigned int index;
  183. if (copy_from_user
  184. (&display, (struct lcd_display *) arg,
  185. sizeof(struct lcd_display)))
  186. return -EFAULT;
  187. udelay(150);
  188. BusyCheck();
  189. LCDWriteInst(0x80);
  190. udelay(150);
  191. BusyCheck();
  192. for (index = 0; index < (display.size1); index++) {
  193. udelay(150);
  194. BusyCheck();
  195. LCDWriteData(display.line1[index]);
  196. BusyCheck();
  197. }
  198. udelay(150);
  199. BusyCheck();
  200. LCDWriteInst(0xC0);
  201. udelay(150);
  202. BusyCheck();
  203. for (index = 0; index < (display.size2); index++) {
  204. udelay(150);
  205. BusyCheck();
  206. LCDWriteData(display.line2[index]);
  207. }
  208. break;
  209. }
  210. case LCD_Read:{
  211. struct lcd_display display;
  212. BusyCheck();
  213. for (address = kDD_R00; address <= kDD_R01;
  214. address++) {
  215. a = (address | kLCD_Addr);
  216. udelay(150);
  217. BusyCheck();
  218. LCDWriteInst(a);
  219. udelay(150);
  220. BusyCheck();
  221. display.line1[address] = LCDReadData;
  222. }
  223. display.line1[0x27] = '\0';
  224. for (address = kDD_R10; address <= kDD_R11;
  225. address++) {
  226. a = (address | kLCD_Addr);
  227. udelay(150);
  228. BusyCheck();
  229. LCDWriteInst(a);
  230. udelay(150);
  231. BusyCheck();
  232. display.line2[address - 0x40] =
  233. LCDReadData;
  234. }
  235. display.line2[0x27] = '\0';
  236. if (copy_to_user
  237. ((struct lcd_display *) arg, &display,
  238. sizeof(struct lcd_display)))
  239. return -EFAULT;
  240. break;
  241. }
  242. // set all GPIO leds to led_display.leds
  243. case LED_Set:{
  244. struct lcd_display led_display;
  245. if (copy_from_user
  246. (&led_display, (struct lcd_display *) arg,
  247. sizeof(struct lcd_display)))
  248. return -EFAULT;
  249. led_state = led_display.leds;
  250. LEDSet(led_state);
  251. break;
  252. }
  253. // set only bit led_display.leds
  254. case LED_Bit_Set:{
  255. unsigned int i;
  256. int bit = 1;
  257. struct lcd_display led_display;
  258. if (copy_from_user
  259. (&led_display, (struct lcd_display *) arg,
  260. sizeof(struct lcd_display)))
  261. return -EFAULT;
  262. for (i = 0; i < (int) led_display.leds; i++) {
  263. bit = 2 * bit;
  264. }
  265. led_state = led_state | bit;
  266. LEDSet(led_state);
  267. break;
  268. }
  269. // clear only bit led_display.leds
  270. case LED_Bit_Clear:{
  271. unsigned int i;
  272. int bit = 1;
  273. struct lcd_display led_display;
  274. if (copy_from_user
  275. (&led_display, (struct lcd_display *) arg,
  276. sizeof(struct lcd_display)))
  277. return -EFAULT;
  278. for (i = 0; i < (int) led_display.leds; i++) {
  279. bit = 2 * bit;
  280. }
  281. led_state = led_state & ~bit;
  282. LEDSet(led_state);
  283. break;
  284. }
  285. case BUTTON_Read:{
  286. button_display.buttons = GPIRead;
  287. if (copy_to_user
  288. ((struct lcd_display *) arg, &button_display,
  289. sizeof(struct lcd_display)))
  290. return -EFAULT;
  291. break;
  292. }
  293. case LINK_Check:{
  294. button_display.buttons =
  295. *((volatile unsigned long *) (0xB0100060));
  296. if (copy_to_user
  297. ((struct lcd_display *) arg, &button_display,
  298. sizeof(struct lcd_display)))
  299. return -EFAULT;
  300. break;
  301. }
  302. case LINK_Check_2:{
  303. int iface_num;
  304. /* panel-utils should pass in the desired interface status is wanted for
  305. * in "buttons" of the structure. We will set this to non-zero if the
  306. * link is in fact up for the requested interface. --DaveM
  307. */
  308. if (copy_from_user
  309. (&button_display, (struct lcd_display *) arg,
  310. sizeof(button_display)))
  311. return -EFAULT;
  312. iface_num = button_display.buttons;
  313. #if defined(CONFIG_TULIP) && 0
  314. if (iface_num >= 0 &&
  315. iface_num < MAX_INTERFACES &&
  316. linkcheck_callbacks[iface_num] != NULL) {
  317. button_display.buttons =
  318. linkcheck_callbacks[iface_num]
  319. (linkcheck_cookies[iface_num]);
  320. } else
  321. #endif
  322. button_display.buttons = 0;
  323. if (__copy_to_user
  324. ((struct lcd_display *) arg, &button_display,
  325. sizeof(struct lcd_display)))
  326. return -EFAULT;
  327. break;
  328. }
  329. // Erase the flash
  330. case FLASH_Erase:{
  331. int ctr = 0;
  332. if ( !capable(CAP_SYS_ADMIN) ) return -EPERM;
  333. pr_info(LCD "Erasing Flash\n");
  334. // Chip Erase Sequence
  335. WRITE_FLASH(kFlash_Addr1, kFlash_Data1);
  336. WRITE_FLASH(kFlash_Addr2, kFlash_Data2);
  337. WRITE_FLASH(kFlash_Addr1, kFlash_Erase3);
  338. WRITE_FLASH(kFlash_Addr1, kFlash_Data1);
  339. WRITE_FLASH(kFlash_Addr2, kFlash_Data2);
  340. WRITE_FLASH(kFlash_Addr1, kFlash_Erase6);
  341. while ((!dqpoll(0x00000000, 0xFF))
  342. && (!timeout(0x00000000))) {
  343. ctr++;
  344. }
  345. if (READ_FLASH(0x07FFF0) == 0xFF) {
  346. pr_info(LCD "Erase Successful\n");
  347. } else if (timeout) {
  348. pr_info(LCD "Erase Timed Out\n");
  349. }
  350. break;
  351. }
  352. // burn the flash
  353. case FLASH_Burn:{
  354. volatile unsigned long burn_addr;
  355. unsigned long flags;
  356. unsigned int i, index;
  357. unsigned char *rom;
  358. struct lcd_display display;
  359. if ( !capable(CAP_SYS_ADMIN) ) return -EPERM;
  360. if (copy_from_user
  361. (&display, (struct lcd_display *) arg,
  362. sizeof(struct lcd_display)))
  363. return -EFAULT;
  364. rom = (unsigned char *) kmalloc((128), GFP_ATOMIC);
  365. if (rom == NULL) {
  366. printk(KERN_ERR LCD "kmalloc() failed in %s\n",
  367. __FUNCTION__);
  368. return -ENOMEM;
  369. }
  370. pr_info(LCD "Starting Flash burn\n");
  371. for (i = 0; i < FLASH_SIZE; i = i + 128) {
  372. if (copy_from_user
  373. (rom, display.RomImage + i, 128)) {
  374. kfree(rom);
  375. return -EFAULT;
  376. }
  377. burn_addr = kFlashBase + i;
  378. spin_lock_irqsave(&lcd_lock, flags);
  379. for (index = 0; index < (128); index++) {
  380. WRITE_FLASH(kFlash_Addr1,
  381. kFlash_Data1);
  382. WRITE_FLASH(kFlash_Addr2,
  383. kFlash_Data2);
  384. WRITE_FLASH(kFlash_Addr1,
  385. kFlash_Prog);
  386. *((volatile unsigned char *)burn_addr) =
  387. (volatile unsigned char) rom[index];
  388. while ((!dqpoll (burn_addr,
  389. (volatile unsigned char)
  390. rom[index])) &&
  391. (!timeout(burn_addr))) { }
  392. burn_addr++;
  393. }
  394. spin_unlock_irqrestore(&lcd_lock, flags);
  395. if (* ((volatile unsigned char *)
  396. (burn_addr - 1)) ==
  397. (volatile unsigned char)
  398. rom[index - 1]) {
  399. } else if (timeout) {
  400. pr_info(LCD "Flash burn timed out\n");
  401. }
  402. }
  403. kfree(rom);
  404. pr_info(LCD "Flash successfully burned\n");
  405. break;
  406. }
  407. // read the flash all at once
  408. case FLASH_Read:{
  409. unsigned char *user_bytes;
  410. volatile unsigned long read_addr;
  411. unsigned int i;
  412. user_bytes =
  413. &(((struct lcd_display *) arg)->RomImage[0]);
  414. if (!access_ok
  415. (VERIFY_WRITE, user_bytes, FLASH_SIZE))
  416. return -EFAULT;
  417. pr_info(LCD "Reading Flash");
  418. for (i = 0; i < FLASH_SIZE; i++) {
  419. unsigned char tmp_byte;
  420. read_addr = kFlashBase + i;
  421. tmp_byte =
  422. *((volatile unsigned char *)
  423. read_addr);
  424. if (__put_user(tmp_byte, &user_bytes[i]))
  425. return -EFAULT;
  426. }
  427. break;
  428. }
  429. default:
  430. return -EINVAL;
  431. }
  432. return 0;
  433. }
  434. static int lcd_open(struct inode *inode, struct file *file)
  435. {
  436. if (!lcd_present)
  437. return -ENXIO;
  438. else
  439. return 0;
  440. }
  441. /* Only RESET or NEXT counts as button pressed */
  442. static inline int button_pressed(void)
  443. {
  444. unsigned long buttons = GPIRead;
  445. if ((buttons == BUTTON_Next) || (buttons == BUTTON_Next_B)
  446. || (buttons == BUTTON_Reset_B))
  447. return buttons;
  448. return 0;
  449. }
  450. /* LED daemon sits on this and we wake him up once a key is pressed. */
  451. static int lcd_waiters = 0;
  452. static ssize_t lcd_read(struct file *file, char *buf,
  453. size_t count, loff_t *ofs)
  454. {
  455. long buttons_now;
  456. if (lcd_waiters > 0)
  457. return -EINVAL;
  458. lcd_waiters++;
  459. while (((buttons_now = (long) button_pressed()) == 0) &&
  460. !(signal_pending(current))) {
  461. msleep_interruptible(2000);
  462. }
  463. lcd_waiters--;
  464. if (signal_pending(current))
  465. return -ERESTARTSYS;
  466. return buttons_now;
  467. }
  468. /*
  469. * The various file operations we support.
  470. */
  471. static struct file_operations lcd_fops = {
  472. .read = lcd_read,
  473. .ioctl = lcd_ioctl,
  474. .open = lcd_open,
  475. };
  476. static struct miscdevice lcd_dev = {
  477. MISC_DYNAMIC_MINOR,
  478. "lcd",
  479. &lcd_fops
  480. };
  481. static int lcd_init(void)
  482. {
  483. int ret;
  484. unsigned long data;
  485. pr_info("%s\n", LCD_DRIVER);
  486. ret = misc_register(&lcd_dev);
  487. if (ret) {
  488. printk(KERN_WARNING LCD "Unable to register misc device.\n");
  489. return ret;
  490. }
  491. /* Check region? Naaah! Just snarf it up. */
  492. /* request_region(RTC_PORT(0), RTC_IO_EXTENT, "lcd");*/
  493. udelay(150);
  494. data = LCDReadData;
  495. if ((data & 0x000000FF) == (0x00)) {
  496. lcd_present = 0;
  497. pr_info(LCD "LCD Not Present\n");
  498. } else {
  499. lcd_present = 1;
  500. WRITE_GAL(kGal_DevBank2PReg, kGal_DevBank2Cfg);
  501. WRITE_GAL(kGal_DevBank3PReg, kGal_DevBank3Cfg);
  502. }
  503. return 0;
  504. }
  505. static void __exit lcd_exit(void)
  506. {
  507. misc_deregister(&lcd_dev);
  508. }
  509. //
  510. // Function: dqpoll
  511. //
  512. // Description: Polls the data lines to see if the flash is busy
  513. //
  514. // In: address, byte data
  515. //
  516. // Out: 0 = busy, 1 = write or erase complete
  517. //
  518. //
  519. static int dqpoll(volatile unsigned long address, volatile unsigned char data)
  520. {
  521. volatile unsigned char dq7;
  522. dq7 = data & 0x80;
  523. return ((READ_FLASH(address) & 0x80) == dq7);
  524. }
  525. //
  526. // Function: timeout
  527. //
  528. // Description: Checks to see if erase or write has timed out
  529. // By polling dq5
  530. //
  531. // In: address
  532. //
  533. //
  534. // Out: 0 = not timed out, 1 = timed out
  535. static int timeout(volatile unsigned long address)
  536. {
  537. return (READ_FLASH(address) & 0x20) == 0x20;
  538. }
  539. module_init(lcd_init);
  540. module_exit(lcd_exit);
  541. MODULE_AUTHOR("Andrew Bose");
  542. MODULE_LICENSE("GPL");