sh_pfc.h 6.3 KB

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