2 * SuperH Pin Function Controller support.
4 * Copyright (C) 2008 Magnus Damm
5 * Copyright (C) 2009 - 2012 Paul Mundt
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
11 #define pr_fmt(fmt) "sh_pfc " KBUILD_MODNAME ": " fmt
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/sh_pfc.h>
16 #include <linux/module.h>
17 #include <linux/err.h>
19 #include <linux/bitops.h>
20 #include <linux/slab.h>
21 #include <linux/ioport.h>
22 #include <linux/pinctrl/machine.h>
24 static struct sh_pfc sh_pfc __read_mostly
;
26 static void pfc_iounmap(struct sh_pfc
*pfc
)
30 for (k
= 0; k
< pfc
->pdata
->num_resources
; k
++)
31 if (pfc
->window
[k
].virt
)
32 iounmap(pfc
->window
[k
].virt
);
38 static int pfc_ioremap(struct sh_pfc
*pfc
)
43 if (!pfc
->pdata
->num_resources
)
46 pfc
->window
= kzalloc(pfc
->pdata
->num_resources
* sizeof(*pfc
->window
),
51 for (k
= 0; k
< pfc
->pdata
->num_resources
; k
++) {
52 res
= pfc
->pdata
->resource
+ k
;
53 WARN_ON(resource_type(res
) != IORESOURCE_MEM
);
54 pfc
->window
[k
].phys
= res
->start
;
55 pfc
->window
[k
].size
= resource_size(res
);
56 pfc
->window
[k
].virt
= ioremap_nocache(res
->start
,
58 if (!pfc
->window
[k
].virt
)
70 static void __iomem
*pfc_phys_to_virt(struct sh_pfc
*pfc
,
71 unsigned long address
)
73 struct pfc_window
*window
;
76 /* scan through physical windows and convert address */
77 for (k
= 0; k
< pfc
->pdata
->num_resources
; k
++) {
78 window
= pfc
->window
+ k
;
80 if (address
< window
->phys
)
83 if (address
>= (window
->phys
+ window
->size
))
86 return window
->virt
+ (address
- window
->phys
);
89 /* no windows defined, register must be 1:1 mapped virt:phys */
90 return (void __iomem
*)address
;
93 static int enum_in_range(pinmux_enum_t enum_id
, struct pinmux_range
*r
)
95 if (enum_id
< r
->begin
)
104 static unsigned long gpio_read_raw_reg(void __iomem
*mapped_reg
,
105 unsigned long reg_width
)
109 return ioread8(mapped_reg
);
111 return ioread16(mapped_reg
);
113 return ioread32(mapped_reg
);
120 static void gpio_write_raw_reg(void __iomem
*mapped_reg
,
121 unsigned long reg_width
,
126 iowrite8(data
, mapped_reg
);
129 iowrite16(data
, mapped_reg
);
132 iowrite32(data
, mapped_reg
);
139 int sh_pfc_read_bit(struct pinmux_data_reg
*dr
, unsigned long in_pos
)
143 pos
= dr
->reg_width
- (in_pos
+ 1);
145 pr_debug("read_bit: addr = %lx, pos = %ld, "
146 "r_width = %ld\n", dr
->reg
, pos
, dr
->reg_width
);
148 return (gpio_read_raw_reg(dr
->mapped_reg
, dr
->reg_width
) >> pos
) & 1;
150 EXPORT_SYMBOL_GPL(sh_pfc_read_bit
);
152 void sh_pfc_write_bit(struct pinmux_data_reg
*dr
, unsigned long in_pos
,
157 pos
= dr
->reg_width
- (in_pos
+ 1);
159 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
161 dr
->reg
, !!value
, pos
, dr
->reg_width
);
164 set_bit(pos
, &dr
->reg_shadow
);
166 clear_bit(pos
, &dr
->reg_shadow
);
168 gpio_write_raw_reg(dr
->mapped_reg
, dr
->reg_width
, dr
->reg_shadow
);
170 EXPORT_SYMBOL_GPL(sh_pfc_write_bit
);
172 static void config_reg_helper(struct sh_pfc
*pfc
,
173 struct pinmux_cfg_reg
*crp
,
174 unsigned long in_pos
,
175 void __iomem
**mapped_regp
,
176 unsigned long *maskp
,
181 *mapped_regp
= pfc_phys_to_virt(pfc
, crp
->reg
);
183 if (crp
->field_width
) {
184 *maskp
= (1 << crp
->field_width
) - 1;
185 *posp
= crp
->reg_width
- ((in_pos
+ 1) * crp
->field_width
);
187 *maskp
= (1 << crp
->var_field_width
[in_pos
]) - 1;
188 *posp
= crp
->reg_width
;
189 for (k
= 0; k
<= in_pos
; k
++)
190 *posp
-= crp
->var_field_width
[k
];
194 static int read_config_reg(struct sh_pfc
*pfc
,
195 struct pinmux_cfg_reg
*crp
,
198 void __iomem
*mapped_reg
;
199 unsigned long mask
, pos
;
201 config_reg_helper(pfc
, crp
, field
, &mapped_reg
, &mask
, &pos
);
203 pr_debug("read_reg: addr = %lx, field = %ld, "
204 "r_width = %ld, f_width = %ld\n",
205 crp
->reg
, field
, crp
->reg_width
, crp
->field_width
);
207 return (gpio_read_raw_reg(mapped_reg
, crp
->reg_width
) >> pos
) & mask
;
210 static void write_config_reg(struct sh_pfc
*pfc
,
211 struct pinmux_cfg_reg
*crp
,
212 unsigned long field
, unsigned long value
)
214 void __iomem
*mapped_reg
;
215 unsigned long mask
, pos
, data
;
217 config_reg_helper(pfc
, crp
, field
, &mapped_reg
, &mask
, &pos
);
219 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
220 "r_width = %ld, f_width = %ld\n",
221 crp
->reg
, value
, field
, crp
->reg_width
, crp
->field_width
);
223 mask
= ~(mask
<< pos
);
224 value
= value
<< pos
;
226 data
= gpio_read_raw_reg(mapped_reg
, crp
->reg_width
);
230 if (pfc
->pdata
->unlock_reg
)
231 gpio_write_raw_reg(pfc_phys_to_virt(pfc
, pfc
->pdata
->unlock_reg
),
234 gpio_write_raw_reg(mapped_reg
, crp
->reg_width
, data
);
237 static int setup_data_reg(struct sh_pfc
*pfc
, unsigned gpio
)
239 struct pinmux_gpio
*gpiop
= &pfc
->pdata
->gpios
[gpio
];
240 struct pinmux_data_reg
*data_reg
;
243 if (!enum_in_range(gpiop
->enum_id
, &pfc
->pdata
->data
))
248 data_reg
= pfc
->pdata
->data_regs
+ k
;
250 if (!data_reg
->reg_width
)
253 data_reg
->mapped_reg
= pfc_phys_to_virt(pfc
, data_reg
->reg
);
255 for (n
= 0; n
< data_reg
->reg_width
; n
++) {
256 if (data_reg
->enum_ids
[n
] == gpiop
->enum_id
) {
257 gpiop
->flags
&= ~PINMUX_FLAG_DREG
;
258 gpiop
->flags
|= (k
<< PINMUX_FLAG_DREG_SHIFT
);
259 gpiop
->flags
&= ~PINMUX_FLAG_DBIT
;
260 gpiop
->flags
|= (n
<< PINMUX_FLAG_DBIT_SHIFT
);
272 static void setup_data_regs(struct sh_pfc
*pfc
)
274 struct pinmux_data_reg
*drp
;
277 for (k
= pfc
->pdata
->first_gpio
; k
<= pfc
->pdata
->last_gpio
; k
++)
278 setup_data_reg(pfc
, k
);
282 drp
= pfc
->pdata
->data_regs
+ k
;
287 drp
->reg_shadow
= gpio_read_raw_reg(drp
->mapped_reg
,
293 int sh_pfc_get_data_reg(struct sh_pfc
*pfc
, unsigned gpio
,
294 struct pinmux_data_reg
**drp
, int *bitp
)
296 struct pinmux_gpio
*gpiop
= &pfc
->pdata
->gpios
[gpio
];
299 if (!enum_in_range(gpiop
->enum_id
, &pfc
->pdata
->data
))
302 k
= (gpiop
->flags
& PINMUX_FLAG_DREG
) >> PINMUX_FLAG_DREG_SHIFT
;
303 n
= (gpiop
->flags
& PINMUX_FLAG_DBIT
) >> PINMUX_FLAG_DBIT_SHIFT
;
304 *drp
= pfc
->pdata
->data_regs
+ k
;
308 EXPORT_SYMBOL_GPL(sh_pfc_get_data_reg
);
310 static int get_config_reg(struct sh_pfc
*pfc
, pinmux_enum_t enum_id
,
311 struct pinmux_cfg_reg
**crp
,
312 int *fieldp
, int *valuep
,
313 unsigned long **cntp
)
315 struct pinmux_cfg_reg
*config_reg
;
316 unsigned long r_width
, f_width
, curr_width
, ncomb
;
317 int k
, m
, n
, pos
, bit_pos
;
321 config_reg
= pfc
->pdata
->cfg_regs
+ k
;
323 r_width
= config_reg
->reg_width
;
324 f_width
= config_reg
->field_width
;
331 for (bit_pos
= 0; bit_pos
< r_width
; bit_pos
+= curr_width
) {
333 curr_width
= f_width
;
335 curr_width
= config_reg
->var_field_width
[m
];
337 ncomb
= 1 << curr_width
;
338 for (n
= 0; n
< ncomb
; n
++) {
339 if (config_reg
->enum_ids
[pos
+ n
] == enum_id
) {
343 *cntp
= &config_reg
->cnt
[m
];
356 int sh_pfc_gpio_to_enum(struct sh_pfc
*pfc
, unsigned gpio
, int pos
,
357 pinmux_enum_t
*enum_idp
)
359 pinmux_enum_t enum_id
= pfc
->pdata
->gpios
[gpio
].enum_id
;
360 pinmux_enum_t
*data
= pfc
->pdata
->gpio_data
;
363 if (!enum_in_range(enum_id
, &pfc
->pdata
->data
)) {
364 if (!enum_in_range(enum_id
, &pfc
->pdata
->mark
)) {
365 pr_err("non data/mark enum_id for gpio %d\n", gpio
);
371 *enum_idp
= data
[pos
+ 1];
375 for (k
= 0; k
< pfc
->pdata
->gpio_data_size
; k
++) {
376 if (data
[k
] == enum_id
) {
377 *enum_idp
= data
[k
+ 1];
382 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio
);
385 EXPORT_SYMBOL_GPL(sh_pfc_gpio_to_enum
);
387 int sh_pfc_config_gpio(struct sh_pfc
*pfc
, unsigned gpio
, int pinmux_type
,
390 struct pinmux_cfg_reg
*cr
= NULL
;
391 pinmux_enum_t enum_id
;
392 struct pinmux_range
*range
;
393 int in_range
, pos
, field
, value
;
396 switch (pinmux_type
) {
398 case PINMUX_TYPE_FUNCTION
:
402 case PINMUX_TYPE_OUTPUT
:
403 range
= &pfc
->pdata
->output
;
406 case PINMUX_TYPE_INPUT
:
407 range
= &pfc
->pdata
->input
;
410 case PINMUX_TYPE_INPUT_PULLUP
:
411 range
= &pfc
->pdata
->input_pu
;
414 case PINMUX_TYPE_INPUT_PULLDOWN
:
415 range
= &pfc
->pdata
->input_pd
;
427 pos
= sh_pfc_gpio_to_enum(pfc
, gpio
, pos
, &enum_id
);
434 /* first check if this is a function enum */
435 in_range
= enum_in_range(enum_id
, &pfc
->pdata
->function
);
437 /* not a function enum */
440 * other range exists, so this pin is
441 * a regular GPIO pin that now is being
442 * bound to a specific direction.
444 * for this case we only allow function enums
445 * and the enums that match the other range.
447 in_range
= enum_in_range(enum_id
, range
);
450 * special case pass through for fixed
451 * input-only or output-only pins without
452 * function enum register association.
454 if (in_range
&& enum_id
== range
->force
)
458 * no other range exists, so this pin
459 * must then be of the function type.
461 * allow function type pins to select
462 * any combination of function/in/out
463 * in their MARK lists.
472 if (get_config_reg(pfc
, enum_id
, &cr
,
473 &field
, &value
, &cntp
) != 0)
477 case GPIO_CFG_DRYRUN
:
479 (read_config_reg(pfc
, cr
, field
) != value
))
484 write_config_reg(pfc
, cr
, field
, value
);
498 EXPORT_SYMBOL_GPL(sh_pfc_config_gpio
);
500 int register_sh_pfc(struct sh_pfc_platform_data
*pdata
)
502 int (*initroutine
)(struct sh_pfc
*) = NULL
;
506 * Ensure that the type encoding fits
508 BUILD_BUG_ON(PINMUX_FLAG_TYPE
> ((1 << PINMUX_FLAG_DBIT_SHIFT
) - 1));
513 sh_pfc
.pdata
= pdata
;
515 ret
= pfc_ioremap(&sh_pfc
);
516 if (unlikely(ret
< 0)) {
521 spin_lock_init(&sh_pfc
.lock
);
523 pinctrl_provide_dummies();
524 setup_data_regs(&sh_pfc
);
527 * Initialize pinctrl bindings first
529 initroutine
= symbol_request(sh_pfc_register_pinctrl
);
531 ret
= (*initroutine
)(&sh_pfc
);
532 symbol_put_addr(initroutine
);
534 if (unlikely(ret
!= 0))
537 pr_err("failed to initialize pinctrl bindings\n");
544 initroutine
= symbol_request(sh_pfc_register_gpiochip
);
546 ret
= (*initroutine
)(&sh_pfc
);
547 symbol_put_addr(initroutine
);
550 * If the GPIO chip fails to come up we still leave the
551 * PFC state as it is, given that there are already
552 * extant users of it that have succeeded by this point.
554 if (unlikely(ret
!= 0)) {
555 pr_notice("failed to init GPIO chip, ignoring...\n");
560 pr_info("%s support registered\n", sh_pfc
.pdata
->name
);
565 pfc_iounmap(&sh_pfc
);