cs5535_gpio.c 5.8 KB

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