gpio.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * linux/arch/h8300/kernel/gpio.c
  3. *
  4. * Yoshinori Sato <ysato@users.sourceforge.jp>
  5. *
  6. */
  7. /*
  8. * Internal I/O Port Management
  9. */
  10. #include <linux/stddef.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/kernel.h>
  14. #include <linux/string.h>
  15. #include <linux/fs.h>
  16. #include <linux/init.h>
  17. #define _(addr) (volatile unsigned char *)(addr)
  18. #if defined(CONFIG_H83007) || defined(CONFIG_H83068)
  19. #include <asm/regs306x.h>
  20. static volatile unsigned char *ddrs[] = {
  21. _(P1DDR),_(P2DDR),_(P3DDR),_(P4DDR),_(P5DDR),_(P6DDR),
  22. NULL, _(P8DDR),_(P9DDR),_(PADDR),_(PBDDR),
  23. };
  24. #define MAX_PORT 11
  25. #endif
  26. #if defined(CONFIG_H83002) || defined(CONFIG_H8048)
  27. /* Fix me!! */
  28. #include <asm/regs306x.h>
  29. static volatile unsigned char *ddrs[] = {
  30. _(P1DDR),_(P2DDR),_(P3DDR),_(P4DDR),_(P5DDR),_(P6DDR),
  31. NULL, _(P8DDR),_(P9DDR),_(PADDR),_(PBDDR),
  32. };
  33. #define MAX_PORT 11
  34. #endif
  35. #if defined(CONFIG_H8S2678)
  36. #include <asm/regs267x.h>
  37. static volatile unsigned char *ddrs[] = {
  38. _(P1DDR),_(P2DDR),_(P3DDR),NULL ,_(P5DDR),_(P6DDR),
  39. _(P7DDR),_(P8DDR),NULL, _(PADDR),_(PBDDR),_(PCDDR),
  40. _(PDDDR),_(PEDDR),_(PFDDR),_(PGDDR),_(PHDDR),
  41. _(PADDR),_(PBDDR),_(PCDDR),_(PDDDR),_(PEDDR),_(PFDDR),
  42. _(PGDDR),_(PHDDR)
  43. };
  44. #define MAX_PORT 17
  45. #endif
  46. #undef _
  47. #if !defined(P1DDR)
  48. #error Unsuppoted CPU Selection
  49. #endif
  50. static struct {
  51. unsigned char used;
  52. unsigned char ddr;
  53. } gpio_regs[MAX_PORT];
  54. extern char *_platform_gpio_table(int length);
  55. int h8300_reserved_gpio(int port, unsigned int bits)
  56. {
  57. unsigned char *used;
  58. if (port < 0 || port >= MAX_PORT)
  59. return -1;
  60. used = &(gpio_regs[port].used);
  61. if ((*used & bits) != 0)
  62. return 0;
  63. *used |= bits;
  64. return 1;
  65. }
  66. int h8300_free_gpio(int port, unsigned int bits)
  67. {
  68. unsigned char *used;
  69. if (port < 0 || port >= MAX_PORT)
  70. return -1;
  71. used = &(gpio_regs[port].used);
  72. if ((*used & bits) != bits)
  73. return 0;
  74. *used &= (~bits);
  75. return 1;
  76. }
  77. int h8300_set_gpio_dir(int port_bit,int dir)
  78. {
  79. int port = (port_bit >> 8) & 0xff;
  80. int bit = port_bit & 0xff;
  81. if (ddrs[port] == NULL)
  82. return 0;
  83. if (gpio_regs[port].used & bit) {
  84. if (dir)
  85. gpio_regs[port].ddr |= bit;
  86. else
  87. gpio_regs[port].ddr &= ~bit;
  88. *ddrs[port] = gpio_regs[port].ddr;
  89. return 1;
  90. } else
  91. return 0;
  92. }
  93. int h8300_get_gpio_dir(int port_bit)
  94. {
  95. int port = (port_bit >> 8) & 0xff;
  96. int bit = port_bit & 0xff;
  97. if (ddrs[port] == NULL)
  98. return 0;
  99. if (gpio_regs[port].used & bit) {
  100. return (gpio_regs[port].ddr & bit) != 0;
  101. } else
  102. return -1;
  103. }
  104. #if defined(CONFIG_PROC_FS)
  105. static char *port_status(int portno)
  106. {
  107. static char result[10];
  108. static const char io[2]={'I','O'};
  109. char *rp;
  110. int c;
  111. unsigned char used,ddr;
  112. used = gpio_regs[portno].used;
  113. ddr = gpio_regs[portno].ddr;
  114. result[8]='\0';
  115. rp = result + 7;
  116. for (c = 8; c > 0; c--,rp--,used >>= 1, ddr >>= 1)
  117. if (used & 0x01)
  118. *rp = io[ ddr & 0x01];
  119. else
  120. *rp = '-';
  121. return result;
  122. }
  123. static int gpio_proc_show(struct seq_file *m, void *v)
  124. {
  125. static const char port_name[]="123456789ABCDEFGH";
  126. int c;
  127. for (c = 0; c < MAX_PORT; c++) {
  128. if (ddrs[c] == NULL)
  129. continue;
  130. seq_printf(m, "P%c: %s\n", port_name[c], port_status(c));
  131. }
  132. return 0;
  133. }
  134. static int gpio_proc_open(struct inode *inode, struct file *file)
  135. {
  136. return single_open(file, gpio_proc_show, PDE_DATA(inode));
  137. }
  138. static const struct file_operations gpio_proc_fops = {
  139. .open = gpio_proc_open,
  140. .read = seq_read,
  141. .llseek = seq_lseek,
  142. .release = single_release,
  143. };
  144. static __init int register_proc(void)
  145. {
  146. return proc_create("gpio", S_IRUGO, NULL, &gpio_proc_fops) != NULL;
  147. }
  148. __initcall(register_proc);
  149. #endif
  150. void __init h8300_gpio_init(void)
  151. {
  152. memcpy(gpio_regs,_platform_gpio_table(sizeof(gpio_regs)),sizeof(gpio_regs));
  153. }