blob: 95dad340794ab972b60efa025b52bbb90cc5e050 [file] [log] [blame]
Magnus Dammfae43392009-11-27 07:38:01 +00001/*
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
11#ifndef __SH_PFC_H
12#define __SH_PFC_H
13
14#include <asm-generic/gpio.h>
15
16typedef unsigned short pinmux_enum_t;
17typedef unsigned short pinmux_flag_t;
18
19#define PINMUX_TYPE_NONE 0
20#define PINMUX_TYPE_FUNCTION 1
21#define PINMUX_TYPE_GPIO 2
22#define PINMUX_TYPE_OUTPUT 3
23#define PINMUX_TYPE_INPUT 4
24#define PINMUX_TYPE_INPUT_PULLUP 5
25#define PINMUX_TYPE_INPUT_PULLDOWN 6
26
27#define PINMUX_FLAG_TYPE (0x7)
28#define PINMUX_FLAG_WANT_PULLUP (1 << 3)
29#define PINMUX_FLAG_WANT_PULLDOWN (1 << 4)
30
31#define PINMUX_FLAG_DBIT_SHIFT 5
32#define PINMUX_FLAG_DBIT (0x1f << PINMUX_FLAG_DBIT_SHIFT)
33#define PINMUX_FLAG_DREG_SHIFT 10
34#define PINMUX_FLAG_DREG (0x3f << PINMUX_FLAG_DREG_SHIFT)
35
36struct pinmux_gpio {
37 pinmux_enum_t enum_id;
38 pinmux_flag_t flags;
39};
40
41#define PINMUX_GPIO(gpio, data_or_mark) [gpio] = { data_or_mark }
42#define PINMUX_DATA(data_or_mark, ids...) data_or_mark, ids, 0
43
44struct pinmux_cfg_reg {
45 unsigned long reg, reg_width, field_width;
46 unsigned long *cnt;
47 pinmux_enum_t *enum_ids;
Magnus Dammf78a26f2011-12-14 01:01:05 +090048 unsigned long *var_field_width;
Magnus Dammfae43392009-11-27 07:38:01 +000049};
50
51#define PINMUX_CFG_REG(name, r, r_width, f_width) \
52 .reg = r, .reg_width = r_width, .field_width = f_width, \
53 .cnt = (unsigned long [r_width / f_width]) {}, \
Magnus Dammf78a26f2011-12-14 01:01:05 +090054 .enum_ids = (pinmux_enum_t [(r_width / f_width) * (1 << f_width)])
55
56#define PINMUX_CFG_REG_VAR(name, r, r_width, var_fw0, var_fwn...) \
57 .reg = r, .reg_width = r_width, \
58 .cnt = (unsigned long [r_width]) {}, \
59 .var_field_width = (unsigned long [r_width]) { var_fw0, var_fwn, 0 }, \
60 .enum_ids = (pinmux_enum_t [])
Magnus Dammfae43392009-11-27 07:38:01 +000061
62struct pinmux_data_reg {
63 unsigned long reg, reg_width, reg_shadow;
64 pinmux_enum_t *enum_ids;
Magnus Dammb0e10212011-12-09 12:14:27 +090065 void __iomem *mapped_reg;
Magnus Dammfae43392009-11-27 07:38:01 +000066};
67
68#define PINMUX_DATA_REG(name, r, r_width) \
69 .reg = r, .reg_width = r_width, \
70 .enum_ids = (pinmux_enum_t [r_width]) \
71
Magnus Dammad2a8e72011-09-28 16:50:58 +090072struct pinmux_irq {
73 int irq;
74 pinmux_enum_t *enum_ids;
75};
76
77#define PINMUX_IRQ(irq_nr, ids...) \
78 { .irq = irq_nr, .enum_ids = (pinmux_enum_t []) { ids, 0 } } \
79
Magnus Dammfae43392009-11-27 07:38:01 +000080struct pinmux_range {
81 pinmux_enum_t begin;
82 pinmux_enum_t end;
83 pinmux_enum_t force;
84};
85
Magnus Dammb0e10212011-12-09 12:14:27 +090086struct pfc_window {
87 phys_addr_t phys;
88 void __iomem *virt;
89 unsigned long size;
90};
91
Paul Mundtb3c185a2012-06-20 17:29:04 +090092struct sh_pfc {
Magnus Dammfae43392009-11-27 07:38:01 +000093 char *name;
94 pinmux_enum_t reserved_id;
95 struct pinmux_range data;
96 struct pinmux_range input;
97 struct pinmux_range input_pd;
98 struct pinmux_range input_pu;
99 struct pinmux_range output;
100 struct pinmux_range mark;
101 struct pinmux_range function;
102
103 unsigned first_gpio, last_gpio;
104
105 struct pinmux_gpio *gpios;
106 struct pinmux_cfg_reg *cfg_regs;
107 struct pinmux_data_reg *data_regs;
108
109 pinmux_enum_t *gpio_data;
110 unsigned int gpio_data_size;
111
Magnus Dammad2a8e72011-09-28 16:50:58 +0900112 struct pinmux_irq *gpio_irq;
113 unsigned int gpio_irq_size;
114
Paul Mundtb3c185a2012-06-20 17:29:04 +0900115 spinlock_t lock;
116
Magnus Dammb0e10212011-12-09 12:14:27 +0900117 struct resource *resource;
118 unsigned int num_resources;
119 struct pfc_window *window;
120
Magnus Damme499ada2011-12-14 01:01:14 +0900121 unsigned long unlock_reg;
Magnus Dammfae43392009-11-27 07:38:01 +0000122};
123
Paul Mundtb3c185a2012-06-20 17:29:04 +0900124/* XXX compat for now */
125#define pinmux_info sh_pfc
126
127/* drivers/sh/pfc-gpio.c */
128int sh_pfc_register_gpiochip(struct sh_pfc *pfc);
129
130/* drivers/sh/pfc.c */
131int register_sh_pfc(struct sh_pfc *pfc);
132
133int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos);
134void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
135 unsigned long value);
136int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
137 struct pinmux_data_reg **drp, int *bitp);
138int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
139 pinmux_enum_t *enum_idp);
140int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
141 int cfg_mode);
142int sh_pfc_set_direction(struct sh_pfc *pfc, unsigned gpio,
143 int new_pinmux_type);
144
145/* xxx */
146static inline int register_pinmux(struct pinmux_info *pip)
147{
148 struct sh_pfc *pfc = pip;
149 return register_sh_pfc(pfc);
150}
151
152enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
Magnus Dammfae43392009-11-27 07:38:01 +0000153
Kuninori Morimoto972c3fb2011-11-10 18:45:33 -0800154/* helper macro for port */
155#define PORT_1(fn, pfx, sfx) fn(pfx, sfx)
156
157#define PORT_10(fn, pfx, sfx) \
158 PORT_1(fn, pfx##0, sfx), PORT_1(fn, pfx##1, sfx), \
159 PORT_1(fn, pfx##2, sfx), PORT_1(fn, pfx##3, sfx), \
160 PORT_1(fn, pfx##4, sfx), PORT_1(fn, pfx##5, sfx), \
161 PORT_1(fn, pfx##6, sfx), PORT_1(fn, pfx##7, sfx), \
162 PORT_1(fn, pfx##8, sfx), PORT_1(fn, pfx##9, sfx)
163
164#define PORT_90(fn, pfx, sfx) \
165 PORT_10(fn, pfx##1, sfx), PORT_10(fn, pfx##2, sfx), \
166 PORT_10(fn, pfx##3, sfx), PORT_10(fn, pfx##4, sfx), \
167 PORT_10(fn, pfx##5, sfx), PORT_10(fn, pfx##6, sfx), \
168 PORT_10(fn, pfx##7, sfx), PORT_10(fn, pfx##8, sfx), \
169 PORT_10(fn, pfx##9, sfx)
170
171#define _PORT_ALL(pfx, sfx) pfx##_##sfx
172#define _GPIO_PORT(pfx, sfx) PINMUX_GPIO(GPIO_PORT##pfx, PORT##pfx##_DATA)
173#define PORT_ALL(str) CPU_ALL_PORT(_PORT_ALL, PORT, str)
174#define GPIO_PORT_ALL() CPU_ALL_PORT(_GPIO_PORT, , unused)
175#define GPIO_FN(str) PINMUX_GPIO(GPIO_FN_##str, str##_MARK)
176
Kuninori Morimotobd8d0cba2011-11-10 18:45:23 -0800177/* helper macro for pinmux_enum_t */
178#define PORT_DATA_I(nr) \
179 PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_IN)
180
181#define PORT_DATA_I_PD(nr) \
182 PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, \
183 PORT##nr##_IN, PORT##nr##_IN_PD)
184
185#define PORT_DATA_I_PU(nr) \
186 PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, \
187 PORT##nr##_IN, PORT##nr##_IN_PU)
188
189#define PORT_DATA_I_PU_PD(nr) \
190 PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, \
191 PORT##nr##_IN, PORT##nr##_IN_PD, PORT##nr##_IN_PU)
192
193#define PORT_DATA_O(nr) \
194 PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT)
195
196#define PORT_DATA_IO(nr) \
197 PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT, \
198 PORT##nr##_IN)
199
200#define PORT_DATA_IO_PD(nr) \
201 PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT, \
202 PORT##nr##_IN, PORT##nr##_IN_PD)
203
204#define PORT_DATA_IO_PU(nr) \
205 PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT, \
206 PORT##nr##_IN, PORT##nr##_IN_PU)
207
208#define PORT_DATA_IO_PU_PD(nr) \
209 PINMUX_DATA(PORT##nr##_DATA, PORT##nr##_FN0, PORT##nr##_OUT, \
210 PORT##nr##_IN, PORT##nr##_IN_PD, PORT##nr##_IN_PU)
211
Kuninori Morimoto9b491392011-11-10 18:45:43 -0800212/* helper macro for top 4 bits in PORTnCR */
213#define _PCRH(in, in_pd, in_pu, out) \
214 0, (out), (in), 0, \
215 0, 0, 0, 0, \
216 0, 0, (in_pd), 0, \
217 0, 0, (in_pu), 0
218
219#define PORTCR(nr, reg) \
220 { \
221 PINMUX_CFG_REG("PORT" nr "CR", reg, 8, 4) { \
222 _PCRH(PORT##nr##_IN, PORT##nr##_IN_PD, \
223 PORT##nr##_IN_PU, PORT##nr##_OUT), \
224 PORT##nr##_FN0, PORT##nr##_FN1, \
225 PORT##nr##_FN2, PORT##nr##_FN3, \
226 PORT##nr##_FN4, PORT##nr##_FN5, \
227 PORT##nr##_FN6, PORT##nr##_FN7 } \
228 }
Kuninori Morimotobd8d0cba2011-11-10 18:45:23 -0800229
Magnus Dammfae43392009-11-27 07:38:01 +0000230#endif /* __SH_PFC_H */