sh_pfc.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * SuperH Pin Function Controller Support
  3. *
  4. * Copyright (c) 2008 Magnus Damm
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #ifndef __SH_PFC_H
  11. #define __SH_PFC_H
  12. #include <asm-generic/gpio.h>
  13. typedef unsigned short pinmux_enum_t;
  14. typedef unsigned short pinmux_flag_t;
  15. #define PINMUX_TYPE_NONE 0
  16. #define PINMUX_TYPE_FUNCTION 1
  17. #define PINMUX_TYPE_GPIO 2
  18. #define PINMUX_TYPE_OUTPUT 3
  19. #define PINMUX_TYPE_INPUT 4
  20. #define PINMUX_TYPE_INPUT_PULLUP 5
  21. #define PINMUX_TYPE_INPUT_PULLDOWN 6
  22. #define PINMUX_FLAG_TYPE (0x7)
  23. #define PINMUX_FLAG_WANT_PULLUP (1 << 3)
  24. #define PINMUX_FLAG_WANT_PULLDOWN (1 << 4)
  25. #define PINMUX_FLAG_DBIT_SHIFT 5
  26. #define PINMUX_FLAG_DBIT (0x1f << PINMUX_FLAG_DBIT_SHIFT)
  27. #define PINMUX_FLAG_DREG_SHIFT 10
  28. #define PINMUX_FLAG_DREG (0x3f << PINMUX_FLAG_DREG_SHIFT)
  29. struct pinmux_gpio {
  30. pinmux_enum_t enum_id;
  31. pinmux_flag_t flags;
  32. };
  33. #define PINMUX_GPIO(gpio, data_or_mark) [gpio] = { data_or_mark }
  34. #define PINMUX_DATA(data_or_mark, ids...) data_or_mark, ids, 0
  35. struct pinmux_cfg_reg {
  36. unsigned long reg, reg_width, field_width;
  37. unsigned long *cnt;
  38. pinmux_enum_t *enum_ids;
  39. unsigned long *var_field_width;
  40. };
  41. #define PINMUX_CFG_REG(name, r, r_width, f_width) \
  42. .reg = r, .reg_width = r_width, .field_width = f_width, \
  43. .cnt = (unsigned long [r_width / f_width]) {}, \
  44. .enum_ids = (pinmux_enum_t [(r_width / f_width) * (1 << f_width)])
  45. #define PINMUX_CFG_REG_VAR(name, r, r_width, var_fw0, var_fwn...) \
  46. .reg = r, .reg_width = r_width, \
  47. .cnt = (unsigned long [r_width]) {}, \
  48. .var_field_width = (unsigned long [r_width]) { var_fw0, var_fwn, 0 }, \
  49. .enum_ids = (pinmux_enum_t [])
  50. struct pinmux_data_reg {
  51. unsigned long reg, reg_width, reg_shadow;
  52. pinmux_enum_t *enum_ids;
  53. void __iomem *mapped_reg;
  54. };
  55. #define PINMUX_DATA_REG(name, r, r_width) \
  56. .reg = r, .reg_width = r_width, \
  57. .enum_ids = (pinmux_enum_t [r_width]) \
  58. struct pinmux_irq {
  59. int irq;
  60. pinmux_enum_t *enum_ids;
  61. };
  62. #define PINMUX_IRQ(irq_nr, ids...) \
  63. { .irq = irq_nr, .enum_ids = (pinmux_enum_t []) { ids, 0 } } \
  64. struct pinmux_range {
  65. pinmux_enum_t begin;
  66. pinmux_enum_t end;
  67. pinmux_enum_t force;
  68. };
  69. struct pfc_window {
  70. phys_addr_t phys;
  71. void __iomem *virt;
  72. unsigned long size;
  73. };
  74. struct sh_pfc {
  75. char *name;
  76. pinmux_enum_t reserved_id;
  77. struct pinmux_range data;
  78. struct pinmux_range input;
  79. struct pinmux_range input_pd;
  80. struct pinmux_range input_pu;
  81. struct pinmux_range output;
  82. struct pinmux_range mark;
  83. struct pinmux_range function;
  84. unsigned first_gpio, last_gpio;
  85. struct pinmux_gpio *gpios;
  86. struct pinmux_cfg_reg *cfg_regs;
  87. struct pinmux_data_reg *data_regs;
  88. pinmux_enum_t *gpio_data;
  89. unsigned int gpio_data_size;
  90. struct pinmux_irq *gpio_irq;
  91. unsigned int gpio_irq_size;
  92. spinlock_t lock;
  93. struct resource *resource;
  94. unsigned int num_resources;
  95. struct pfc_window *window;
  96. unsigned long unlock_reg;
  97. };
  98. /* XXX compat for now */
  99. #define pinmux_info sh_pfc
  100. /* drivers/sh/pfc-gpio.c */
  101. int sh_pfc_register_gpiochip(struct sh_pfc *pfc);
  102. /* drivers/sh/pfc.c */
  103. int register_sh_pfc(struct sh_pfc *pfc);
  104. int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos);
  105. void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
  106. unsigned long value);
  107. int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
  108. struct pinmux_data_reg **drp, int *bitp);
  109. int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
  110. pinmux_enum_t *enum_idp);
  111. int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
  112. int cfg_mode);
  113. int sh_pfc_set_direction(struct sh_pfc *pfc, unsigned gpio,
  114. int new_pinmux_type);
  115. /* xxx */
  116. static inline int register_pinmux(struct pinmux_info *pip)
  117. {
  118. struct sh_pfc *pfc = pip;
  119. return register_sh_pfc(pfc);
  120. }
  121. enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
  122. /* helper macro for port */
  123. #define PORT_1(fn, pfx, sfx) fn(pfx, sfx)
  124. #define PORT_10(fn, pfx, sfx) \
  125. PORT_1(fn, pfx##0, sfx), PORT_1(fn, pfx##1, sfx), \
  126. PORT_1(fn, pfx##2, sfx), PORT_1(fn, pfx##3, sfx), \
  127. PORT_1(fn, pfx##4, sfx), PORT_1(fn, pfx##5, sfx), \
  128. PORT_1(fn, pfx##6, sfx), PORT_1(fn, pfx##7, sfx), \
  129. PORT_1(fn, pfx##8, sfx), PORT_1(fn, pfx##9, sfx)
  130. #define PORT_90(fn, pfx, sfx) \
  131. PORT_10(fn, pfx##1, sfx), PORT_10(fn, pfx##2, sfx), \
  132. PORT_10(fn, pfx##3, sfx), PORT_10(fn, pfx##4, sfx), \
  133. PORT_10(fn, pfx##5, sfx), PORT_10(fn, pfx##6, sfx), \
  134. PORT_10(fn, pfx##7, sfx), PORT_10(fn, pfx##8, sfx), \
  135. PORT_10(fn, pfx##9, sfx)
  136. #define _PORT_ALL(pfx, sfx) pfx##_##sfx
  137. #define _GPIO_PORT(pfx, sfx) PINMUX_GPIO(GPIO_PORT##pfx, PORT##pfx##_DATA)
  138. #define PORT_ALL(str) CPU_ALL_PORT(_PORT_ALL, PORT, str)
  139. #define GPIO_PORT_ALL() CPU_ALL_PORT(_GPIO_PORT, , unused)
  140. #define GPIO_FN(str) PINMUX_GPIO(GPIO_FN_##str, str##_MARK)
  141. /* helper macro for pinmux_enum_t */
  142. #define PORT_DATA_I(nr) \
  143. PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_IN)
  144. #define PORT_DATA_I_PD(nr) \
  145. PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, \
  146. PORT##nr##_IN, PORT##nr##_IN_PD)
  147. #define PORT_DATA_I_PU(nr) \
  148. PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, \
  149. PORT##nr##_IN, PORT##nr##_IN_PU)
  150. #define PORT_DATA_I_PU_PD(nr) \
  151. PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, \
  152. PORT##nr##_IN, PORT##nr##_IN_PD, PORT##nr##_IN_PU)
  153. #define PORT_DATA_O(nr) \
  154. PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT)
  155. #define PORT_DATA_IO(nr) \
  156. PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT, \
  157. PORT##nr##_IN)
  158. #define PORT_DATA_IO_PD(nr) \
  159. PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT, \
  160. PORT##nr##_IN, PORT##nr##_IN_PD)
  161. #define PORT_DATA_IO_PU(nr) \
  162. PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT, \
  163. PORT##nr##_IN, PORT##nr##_IN_PU)
  164. #define PORT_DATA_IO_PU_PD(nr) \
  165. PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT, \
  166. PORT##nr##_IN, PORT##nr##_IN_PD, PORT##nr##_IN_PU)
  167. /* helper macro for top 4 bits in PORTnCR */
  168. #define _PCRH(in, in_pd, in_pu, out) \
  169. 0, (out), (in), 0, \
  170. 0, 0, 0, 0, \
  171. 0, 0, (in_pd), 0, \
  172. 0, 0, (in_pu), 0
  173. #define PORTCR(nr, reg) \
  174. { \
  175. PINMUX_CFG_REG("PORT" nr "CR", reg, 8, 4) { \
  176. _PCRH(PORT##nr##_IN, PORT##nr##_IN_PD, \
  177. PORT##nr##_IN_PU, PORT##nr##_OUT), \
  178. PORT##nr##_FN0, PORT##nr##_FN1, \
  179. PORT##nr##_FN2, PORT##nr##_FN3, \
  180. PORT##nr##_FN4, PORT##nr##_FN5, \
  181. PORT##nr##_FN6, PORT##nr##_FN7 } \
  182. }
  183. #endif /* __SH_PFC_H */