lcd.c 13 KB

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