tb0219.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Driver for TANBAC TB0219 base board.
  3. *
  4. * Copyright (C) 2005 Yoichi Yuasa <yuasa@linux-mips.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/platform_device.h>
  21. #include <linux/fs.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/uaccess.h>
  25. #include <asm/io.h>
  26. #include <asm/reboot.h>
  27. #include <asm/vr41xx/giu.h>
  28. #include <asm/vr41xx/tb0219.h>
  29. MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
  30. MODULE_DESCRIPTION("TANBAC TB0219 base board driver");
  31. MODULE_LICENSE("GPL");
  32. static int major; /* default is dynamic major device number */
  33. module_param(major, int, 0);
  34. MODULE_PARM_DESC(major, "Major device number");
  35. static void (*old_machine_restart)(char *command);
  36. static void __iomem *tb0219_base;
  37. static DEFINE_SPINLOCK(tb0219_lock);
  38. #define tb0219_read(offset) readw(tb0219_base + (offset))
  39. #define tb0219_write(offset, value) writew((value), tb0219_base + (offset))
  40. #define TB0219_START 0x0a000000UL
  41. #define TB0219_SIZE 0x20UL
  42. #define TB0219_LED 0x00
  43. #define TB0219_GPIO_INPUT 0x02
  44. #define TB0219_GPIO_OUTPUT 0x04
  45. #define TB0219_DIP_SWITCH 0x06
  46. #define TB0219_MISC 0x08
  47. #define TB0219_RESET 0x0e
  48. #define TB0219_PCI_SLOT1_IRQ_STATUS 0x10
  49. #define TB0219_PCI_SLOT2_IRQ_STATUS 0x12
  50. #define TB0219_PCI_SLOT3_IRQ_STATUS 0x14
  51. typedef enum {
  52. TYPE_LED,
  53. TYPE_GPIO_OUTPUT,
  54. } tb0219_type_t;
  55. /*
  56. * Minor device number
  57. * 0 = 7 segment LED
  58. *
  59. * 16 = GPIO IN 0
  60. * 17 = GPIO IN 1
  61. * 18 = GPIO IN 2
  62. * 19 = GPIO IN 3
  63. * 20 = GPIO IN 4
  64. * 21 = GPIO IN 5
  65. * 22 = GPIO IN 6
  66. * 23 = GPIO IN 7
  67. *
  68. * 32 = GPIO OUT 0
  69. * 33 = GPIO OUT 1
  70. * 34 = GPIO OUT 2
  71. * 35 = GPIO OUT 3
  72. * 36 = GPIO OUT 4
  73. * 37 = GPIO OUT 5
  74. * 38 = GPIO OUT 6
  75. * 39 = GPIO OUT 7
  76. *
  77. * 48 = DIP switch 1
  78. * 49 = DIP switch 2
  79. * 50 = DIP switch 3
  80. * 51 = DIP switch 4
  81. * 52 = DIP switch 5
  82. * 53 = DIP switch 6
  83. * 54 = DIP switch 7
  84. * 55 = DIP switch 8
  85. */
  86. static inline char get_led(void)
  87. {
  88. return (char)tb0219_read(TB0219_LED);
  89. }
  90. static inline char get_gpio_input_pin(unsigned int pin)
  91. {
  92. uint16_t values;
  93. values = tb0219_read(TB0219_GPIO_INPUT);
  94. if (values & (1 << pin))
  95. return '1';
  96. return '0';
  97. }
  98. static inline char get_gpio_output_pin(unsigned int pin)
  99. {
  100. uint16_t values;
  101. values = tb0219_read(TB0219_GPIO_OUTPUT);
  102. if (values & (1 << pin))
  103. return '1';
  104. return '0';
  105. }
  106. static inline char get_dip_switch(unsigned int pin)
  107. {
  108. uint16_t values;
  109. values = tb0219_read(TB0219_DIP_SWITCH);
  110. if (values & (1 << pin))
  111. return '1';
  112. return '0';
  113. }
  114. static inline int set_led(char command)
  115. {
  116. tb0219_write(TB0219_LED, command);
  117. return 0;
  118. }
  119. static inline int set_gpio_output_pin(unsigned int pin, char command)
  120. {
  121. unsigned long flags;
  122. uint16_t value;
  123. if (command != '0' && command != '1')
  124. return -EINVAL;
  125. spin_lock_irqsave(&tb0219_lock, flags);
  126. value = tb0219_read(TB0219_GPIO_OUTPUT);
  127. if (command == '0')
  128. value &= ~(1 << pin);
  129. else
  130. value |= 1 << pin;
  131. tb0219_write(TB0219_GPIO_OUTPUT, value);
  132. spin_unlock_irqrestore(&tb0219_lock, flags);
  133. return 0;
  134. }
  135. static ssize_t tanbac_tb0219_read(struct file *file, char __user *buf, size_t len,
  136. loff_t *ppos)
  137. {
  138. unsigned int minor;
  139. char value;
  140. minor = iminor(file_inode(file));
  141. switch (minor) {
  142. case 0:
  143. value = get_led();
  144. break;
  145. case 16 ... 23:
  146. value = get_gpio_input_pin(minor - 16);
  147. break;
  148. case 32 ... 39:
  149. value = get_gpio_output_pin(minor - 32);
  150. break;
  151. case 48 ... 55:
  152. value = get_dip_switch(minor - 48);
  153. break;
  154. default:
  155. return -EBADF;
  156. }
  157. if (len <= 0)
  158. return -EFAULT;
  159. if (put_user(value, buf))
  160. return -EFAULT;
  161. return 1;
  162. }
  163. static ssize_t tanbac_tb0219_write(struct file *file, const char __user *data,
  164. size_t len, loff_t *ppos)
  165. {
  166. unsigned int minor;
  167. tb0219_type_t type;
  168. size_t i;
  169. int retval = 0;
  170. char c;
  171. minor = iminor(file_inode(file));
  172. switch (minor) {
  173. case 0:
  174. type = TYPE_LED;
  175. break;
  176. case 32 ... 39:
  177. type = TYPE_GPIO_OUTPUT;
  178. break;
  179. default:
  180. return -EBADF;
  181. }
  182. for (i = 0; i < len; i++) {
  183. if (get_user(c, data + i))
  184. return -EFAULT;
  185. switch (type) {
  186. case TYPE_LED:
  187. retval = set_led(c);
  188. break;
  189. case TYPE_GPIO_OUTPUT:
  190. retval = set_gpio_output_pin(minor - 32, c);
  191. break;
  192. }
  193. if (retval < 0)
  194. break;
  195. }
  196. return i;
  197. }
  198. static int tanbac_tb0219_open(struct inode *inode, struct file *file)
  199. {
  200. unsigned int minor;
  201. minor = iminor(inode);
  202. switch (minor) {
  203. case 0:
  204. case 16 ... 23:
  205. case 32 ... 39:
  206. case 48 ... 55:
  207. return nonseekable_open(inode, file);
  208. default:
  209. break;
  210. }
  211. return -EBADF;
  212. }
  213. static int tanbac_tb0219_release(struct inode *inode, struct file *file)
  214. {
  215. return 0;
  216. }
  217. static const struct file_operations tb0219_fops = {
  218. .owner = THIS_MODULE,
  219. .read = tanbac_tb0219_read,
  220. .write = tanbac_tb0219_write,
  221. .open = tanbac_tb0219_open,
  222. .release = tanbac_tb0219_release,
  223. .llseek = no_llseek,
  224. };
  225. static void tb0219_restart(char *command)
  226. {
  227. tb0219_write(TB0219_RESET, 0);
  228. }
  229. static void tb0219_pci_irq_init(void)
  230. {
  231. /* PCI Slot 1 */
  232. vr41xx_set_irq_trigger(TB0219_PCI_SLOT1_PIN, IRQ_TRIGGER_LEVEL, IRQ_SIGNAL_THROUGH);
  233. vr41xx_set_irq_level(TB0219_PCI_SLOT1_PIN, IRQ_LEVEL_LOW);
  234. /* PCI Slot 2 */
  235. vr41xx_set_irq_trigger(TB0219_PCI_SLOT2_PIN, IRQ_TRIGGER_LEVEL, IRQ_SIGNAL_THROUGH);
  236. vr41xx_set_irq_level(TB0219_PCI_SLOT2_PIN, IRQ_LEVEL_LOW);
  237. /* PCI Slot 3 */
  238. vr41xx_set_irq_trigger(TB0219_PCI_SLOT3_PIN, IRQ_TRIGGER_LEVEL, IRQ_SIGNAL_THROUGH);
  239. vr41xx_set_irq_level(TB0219_PCI_SLOT3_PIN, IRQ_LEVEL_LOW);
  240. }
  241. static int tb0219_probe(struct platform_device *dev)
  242. {
  243. int retval;
  244. if (request_mem_region(TB0219_START, TB0219_SIZE, "TB0219") == NULL)
  245. return -EBUSY;
  246. tb0219_base = ioremap(TB0219_START, TB0219_SIZE);
  247. if (tb0219_base == NULL) {
  248. release_mem_region(TB0219_START, TB0219_SIZE);
  249. return -ENOMEM;
  250. }
  251. retval = register_chrdev(major, "TB0219", &tb0219_fops);
  252. if (retval < 0) {
  253. iounmap(tb0219_base);
  254. tb0219_base = NULL;
  255. release_mem_region(TB0219_START, TB0219_SIZE);
  256. return retval;
  257. }
  258. old_machine_restart = _machine_restart;
  259. _machine_restart = tb0219_restart;
  260. tb0219_pci_irq_init();
  261. if (major == 0) {
  262. major = retval;
  263. printk(KERN_INFO "TB0219: major number %d\n", major);
  264. }
  265. return 0;
  266. }
  267. static int tb0219_remove(struct platform_device *dev)
  268. {
  269. _machine_restart = old_machine_restart;
  270. iounmap(tb0219_base);
  271. tb0219_base = NULL;
  272. release_mem_region(TB0219_START, TB0219_SIZE);
  273. return 0;
  274. }
  275. static struct platform_device *tb0219_platform_device;
  276. static struct platform_driver tb0219_device_driver = {
  277. .probe = tb0219_probe,
  278. .remove = tb0219_remove,
  279. .driver = {
  280. .name = "TB0219",
  281. },
  282. };
  283. static int __init tanbac_tb0219_init(void)
  284. {
  285. int retval;
  286. tb0219_platform_device = platform_device_alloc("TB0219", -1);
  287. if (!tb0219_platform_device)
  288. return -ENOMEM;
  289. retval = platform_device_add(tb0219_platform_device);
  290. if (retval < 0) {
  291. platform_device_put(tb0219_platform_device);
  292. return retval;
  293. }
  294. retval = platform_driver_register(&tb0219_device_driver);
  295. if (retval < 0)
  296. platform_device_unregister(tb0219_platform_device);
  297. return retval;
  298. }
  299. static void __exit tanbac_tb0219_exit(void)
  300. {
  301. platform_driver_unregister(&tb0219_device_driver);
  302. platform_device_unregister(tb0219_platform_device);
  303. }
  304. module_init(tanbac_tb0219_init);
  305. module_exit(tanbac_tb0219_exit);