cs5535_gpio.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * AMD CS5535/CS5536 GPIO driver.
  3. * Allows a user space process to play with the GPIO pins.
  4. *
  5. * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the smems of the GNU General Public License as published by
  9. * the Free Software Foundation; version 2 of the License.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/module.h>
  13. #include <linux/errno.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/cdev.h>
  17. #include <linux/ioport.h>
  18. #include <linux/pci.h>
  19. #include <linux/smp_lock.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/io.h>
  22. #define NAME "cs5535_gpio"
  23. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  24. MODULE_DESCRIPTION("AMD CS5535/CS5536 GPIO Pin Driver");
  25. MODULE_LICENSE("GPL");
  26. static int major;
  27. module_param(major, int, 0);
  28. MODULE_PARM_DESC(major, "Major device number");
  29. static ulong mask;
  30. module_param(mask, ulong, 0);
  31. MODULE_PARM_DESC(mask, "GPIO channel mask");
  32. #define MSR_LBAR_GPIO 0x5140000C
  33. static u32 gpio_base;
  34. static struct pci_device_id divil_pci[] = {
  35. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_ISA) },
  36. { PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_CS5536_ISA) },
  37. { } /* NULL entry */
  38. };
  39. MODULE_DEVICE_TABLE(pci, divil_pci);
  40. static struct cdev cs5535_gpio_cdev;
  41. /* reserve 32 entries even though some aren't usable */
  42. #define CS5535_GPIO_COUNT 32
  43. /* IO block size */
  44. #define CS5535_GPIO_SIZE 256
  45. struct gpio_regmap {
  46. u32 rd_offset;
  47. u32 wr_offset;
  48. char on;
  49. char off;
  50. };
  51. static struct gpio_regmap rm[] =
  52. {
  53. { 0x30, 0x00, '1', '0' }, /* GPIOx_READ_BACK / GPIOx_OUT_VAL */
  54. { 0x20, 0x20, 'I', 'i' }, /* GPIOx_IN_EN */
  55. { 0x04, 0x04, 'O', 'o' }, /* GPIOx_OUT_EN */
  56. { 0x08, 0x08, 't', 'T' }, /* GPIOx_OUT_OD_EN */
  57. { 0x18, 0x18, 'P', 'p' }, /* GPIOx_OUT_PU_EN */
  58. { 0x1c, 0x1c, 'D', 'd' }, /* GPIOx_OUT_PD_EN */
  59. };
  60. /**
  61. * Gets the register offset for the GPIO bank.
  62. * Low (0-15) starts at 0x00, high (16-31) starts at 0x80
  63. */
  64. static inline u32 cs5535_lowhigh_base(int reg)
  65. {
  66. return (reg & 0x10) << 3;
  67. }
  68. static ssize_t cs5535_gpio_write(struct file *file, const char __user *data,
  69. size_t len, loff_t *ppos)
  70. {
  71. u32 m = iminor(file->f_path.dentry->d_inode);
  72. int i, j;
  73. u32 base = gpio_base + cs5535_lowhigh_base(m);
  74. u32 m0, m1;
  75. char c;
  76. /**
  77. * Creates the mask for atomic bit programming.
  78. * The high 16 bits and the low 16 bits are used to set the mask.
  79. * For example, GPIO 15 maps to 31,15: 0,1 => On; 1,0=> Off
  80. */
  81. m1 = 1 << (m & 0x0F);
  82. m0 = m1 << 16;
  83. for (i = 0; i < len; ++i) {
  84. if (get_user(c, data+i))
  85. return -EFAULT;
  86. for (j = 0; j < ARRAY_SIZE(rm); j++) {
  87. if (c == rm[j].on) {
  88. outl(m1, base + rm[j].wr_offset);
  89. /* If enabling output, turn off AUX 1 and AUX 2 */
  90. if (c == 'O') {
  91. outl(m0, base + 0x10);
  92. outl(m0, base + 0x14);
  93. }
  94. break;
  95. } else if (c == rm[j].off) {
  96. outl(m0, base + rm[j].wr_offset);
  97. break;
  98. }
  99. }
  100. }
  101. *ppos = 0;
  102. return len;
  103. }
  104. static ssize_t cs5535_gpio_read(struct file *file, char __user *buf,
  105. size_t len, loff_t *ppos)
  106. {
  107. u32 m = iminor(file->f_path.dentry->d_inode);
  108. u32 base = gpio_base + cs5535_lowhigh_base(m);
  109. int rd_bit = 1 << (m & 0x0f);
  110. int i;
  111. char ch;
  112. ssize_t count = 0;
  113. if (*ppos >= ARRAY_SIZE(rm))
  114. return 0;
  115. for (i = *ppos; (i < (*ppos + len)) && (i < ARRAY_SIZE(rm)); i++) {
  116. ch = (inl(base + rm[i].rd_offset) & rd_bit) ?
  117. rm[i].on : rm[i].off;
  118. if (put_user(ch, buf+count))
  119. return -EFAULT;
  120. count++;
  121. }
  122. /* add a line-feed if there is room */
  123. if ((i == ARRAY_SIZE(rm)) && (count < len)) {
  124. put_user('\n', buf + count);
  125. count++;
  126. }
  127. *ppos += count;
  128. return count;
  129. }
  130. static int cs5535_gpio_open(struct inode *inode, struct file *file)
  131. {
  132. u32 m = iminor(inode);
  133. cycle_kernel_lock();
  134. /* the mask says which pins are usable by this driver */
  135. if ((mask & (1 << m)) == 0)
  136. return -EINVAL;
  137. return nonseekable_open(inode, file);
  138. }
  139. static const struct file_operations cs5535_gpio_fops = {
  140. .owner = THIS_MODULE,
  141. .write = cs5535_gpio_write,
  142. .read = cs5535_gpio_read,
  143. .open = cs5535_gpio_open
  144. };
  145. static int __init cs5535_gpio_init(void)
  146. {
  147. dev_t dev_id;
  148. u32 low, hi;
  149. int retval;
  150. if (pci_dev_present(divil_pci) == 0) {
  151. printk(KERN_WARNING NAME ": DIVIL not found\n");
  152. return -ENODEV;
  153. }
  154. /* Grab the GPIO I/O range */
  155. rdmsr(MSR_LBAR_GPIO, low, hi);
  156. /* Check the mask and whether GPIO is enabled (sanity check) */
  157. if (hi != 0x0000f001) {
  158. printk(KERN_WARNING NAME ": GPIO not enabled\n");
  159. return -ENODEV;
  160. }
  161. /* Mask off the IO base address */
  162. gpio_base = low & 0x0000ff00;
  163. /**
  164. * Some GPIO pins
  165. * 31-29,23 : reserved (always mask out)
  166. * 28 : Power Button
  167. * 26 : PME#
  168. * 22-16 : LPC
  169. * 14,15 : SMBus
  170. * 9,8 : UART1
  171. * 7 : PCI INTB
  172. * 3,4 : UART2/DDC
  173. * 2 : IDE_IRQ0
  174. * 0 : PCI INTA
  175. *
  176. * If a mask was not specified, be conservative and only allow:
  177. * 1,2,5,6,10-13,24,25,27
  178. */
  179. if (mask != 0)
  180. mask &= 0x1f7fffff;
  181. else
  182. mask = 0x0b003c66;
  183. if (!request_region(gpio_base, CS5535_GPIO_SIZE, NAME)) {
  184. printk(KERN_ERR NAME ": can't allocate I/O for GPIO\n");
  185. return -ENODEV;
  186. }
  187. if (major) {
  188. dev_id = MKDEV(major, 0);
  189. retval = register_chrdev_region(dev_id, CS5535_GPIO_COUNT,
  190. NAME);
  191. } else {
  192. retval = alloc_chrdev_region(&dev_id, 0, CS5535_GPIO_COUNT,
  193. NAME);
  194. major = MAJOR(dev_id);
  195. }
  196. if (retval) {
  197. release_region(gpio_base, CS5535_GPIO_SIZE);
  198. return -1;
  199. }
  200. printk(KERN_DEBUG NAME ": base=%#x mask=%#lx major=%d\n",
  201. gpio_base, mask, major);
  202. cdev_init(&cs5535_gpio_cdev, &cs5535_gpio_fops);
  203. cdev_add(&cs5535_gpio_cdev, dev_id, CS5535_GPIO_COUNT);
  204. return 0;
  205. }
  206. static void __exit cs5535_gpio_cleanup(void)
  207. {
  208. dev_t dev_id = MKDEV(major, 0);
  209. cdev_del(&cs5535_gpio_cdev);
  210. unregister_chrdev_region(dev_id, CS5535_GPIO_COUNT);
  211. release_region(gpio_base, CS5535_GPIO_SIZE);
  212. }
  213. module_init(cs5535_gpio_init);
  214. module_exit(cs5535_gpio_cleanup);