tb0219.c 7.8 KB

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