ans-lcd.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * /dev/lcd driver for Apple Network Servers.
  4. */
  5. #include <linux/types.h>
  6. #include <linux/errno.h>
  7. #include <linux/kernel.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/fcntl.h>
  10. #include <linux/module.h>
  11. #include <linux/delay.h>
  12. #include <linux/fs.h>
  13. #include <linux/uaccess.h>
  14. #include <asm/sections.h>
  15. #include <asm/prom.h>
  16. #include <asm/io.h>
  17. #include "ans-lcd.h"
  18. #define ANSLCD_ADDR 0xf301c000
  19. #define ANSLCD_CTRL_IX 0x00
  20. #define ANSLCD_DATA_IX 0x10
  21. static unsigned long anslcd_short_delay = 80;
  22. static unsigned long anslcd_long_delay = 3280;
  23. static volatile unsigned char __iomem *anslcd_ptr;
  24. static DEFINE_MUTEX(anslcd_mutex);
  25. #undef DEBUG
  26. static void
  27. anslcd_write_byte_ctrl ( unsigned char c )
  28. {
  29. #ifdef DEBUG
  30. printk(KERN_DEBUG "LCD: CTRL byte: %02x\n",c);
  31. #endif
  32. out_8(anslcd_ptr + ANSLCD_CTRL_IX, c);
  33. switch(c) {
  34. case 1:
  35. case 2:
  36. case 3:
  37. udelay(anslcd_long_delay); break;
  38. default: udelay(anslcd_short_delay);
  39. }
  40. }
  41. static void
  42. anslcd_write_byte_data ( unsigned char c )
  43. {
  44. out_8(anslcd_ptr + ANSLCD_DATA_IX, c);
  45. udelay(anslcd_short_delay);
  46. }
  47. static ssize_t
  48. anslcd_write( struct file * file, const char __user * buf,
  49. size_t count, loff_t *ppos )
  50. {
  51. const char __user *p = buf;
  52. int i;
  53. #ifdef DEBUG
  54. printk(KERN_DEBUG "LCD: write\n");
  55. #endif
  56. if (!access_ok(buf, count))
  57. return -EFAULT;
  58. mutex_lock(&anslcd_mutex);
  59. for ( i = *ppos; count > 0; ++i, ++p, --count )
  60. {
  61. char c;
  62. __get_user(c, p);
  63. anslcd_write_byte_data( c );
  64. }
  65. mutex_unlock(&anslcd_mutex);
  66. *ppos = i;
  67. return p - buf;
  68. }
  69. static long
  70. anslcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  71. {
  72. char ch, __user *temp;
  73. long ret = 0;
  74. #ifdef DEBUG
  75. printk(KERN_DEBUG "LCD: ioctl(%d,%d)\n",cmd,arg);
  76. #endif
  77. mutex_lock(&anslcd_mutex);
  78. switch ( cmd )
  79. {
  80. case ANSLCD_CLEAR:
  81. anslcd_write_byte_ctrl ( 0x38 );
  82. anslcd_write_byte_ctrl ( 0x0f );
  83. anslcd_write_byte_ctrl ( 0x06 );
  84. anslcd_write_byte_ctrl ( 0x01 );
  85. anslcd_write_byte_ctrl ( 0x02 );
  86. break;
  87. case ANSLCD_SENDCTRL:
  88. temp = (char __user *) arg;
  89. __get_user(ch, temp);
  90. for (; ch; temp++) { /* FIXME: This is ugly, but should work, as a \0 byte is not a valid command code */
  91. anslcd_write_byte_ctrl ( ch );
  92. __get_user(ch, temp);
  93. }
  94. break;
  95. case ANSLCD_SETSHORTDELAY:
  96. if (!capable(CAP_SYS_ADMIN))
  97. ret =-EACCES;
  98. else
  99. anslcd_short_delay=arg;
  100. break;
  101. case ANSLCD_SETLONGDELAY:
  102. if (!capable(CAP_SYS_ADMIN))
  103. ret = -EACCES;
  104. else
  105. anslcd_long_delay=arg;
  106. break;
  107. default:
  108. ret = -EINVAL;
  109. }
  110. mutex_unlock(&anslcd_mutex);
  111. return ret;
  112. }
  113. static int
  114. anslcd_open( struct inode * inode, struct file * file )
  115. {
  116. return 0;
  117. }
  118. const struct file_operations anslcd_fops = {
  119. .write = anslcd_write,
  120. .unlocked_ioctl = anslcd_ioctl,
  121. .open = anslcd_open,
  122. .llseek = default_llseek,
  123. };
  124. static struct miscdevice anslcd_dev = {
  125. LCD_MINOR,
  126. "anslcd",
  127. &anslcd_fops
  128. };
  129. static const char anslcd_logo[] __initconst =
  130. "********************" /* Line #1 */
  131. "* LINUX! *" /* Line #3 */
  132. "* Welcome to *" /* Line #2 */
  133. "********************"; /* Line #4 */
  134. static int __init
  135. anslcd_init(void)
  136. {
  137. int a;
  138. int retval;
  139. struct device_node* node;
  140. node = of_find_node_by_name(NULL, "lcd");
  141. if (!node || !of_node_name_eq(node->parent, "gc")) {
  142. of_node_put(node);
  143. return -ENODEV;
  144. }
  145. of_node_put(node);
  146. anslcd_ptr = ioremap(ANSLCD_ADDR, 0x20);
  147. retval = misc_register(&anslcd_dev);
  148. if(retval < 0){
  149. printk(KERN_INFO "LCD: misc_register failed\n");
  150. iounmap(anslcd_ptr);
  151. return retval;
  152. }
  153. #ifdef DEBUG
  154. printk(KERN_DEBUG "LCD: init\n");
  155. #endif
  156. mutex_lock(&anslcd_mutex);
  157. anslcd_write_byte_ctrl ( 0x38 );
  158. anslcd_write_byte_ctrl ( 0x0c );
  159. anslcd_write_byte_ctrl ( 0x06 );
  160. anslcd_write_byte_ctrl ( 0x01 );
  161. anslcd_write_byte_ctrl ( 0x02 );
  162. for(a=0;a<80;a++) {
  163. anslcd_write_byte_data(anslcd_logo[a]);
  164. }
  165. mutex_unlock(&anslcd_mutex);
  166. return 0;
  167. }
  168. static void __exit
  169. anslcd_exit(void)
  170. {
  171. misc_deregister(&anslcd_dev);
  172. iounmap(anslcd_ptr);
  173. }
  174. module_init(anslcd_init);
  175. module_exit(anslcd_exit);
  176. MODULE_LICENSE("GPL v2");