gpio.c 3.6 KB

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