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 inline bool sh_pfc_initialized(void)
31 static void pfc_iounmap(struct sh_pfc *pfc)
35 for (k = 0; k < pfc->num_resources; k++)
36 if (pfc->window[k].virt)
37 iounmap(pfc->window[k].virt);
43 static int pfc_ioremap(struct sh_pfc *pfc)
48 if (!pfc->num_resources)
51 pfc->window = kzalloc(pfc->num_resources * sizeof(*pfc->window),
56 for (k = 0; k < pfc->num_resources; k++) {
57 res = pfc->resource + k;
58 WARN_ON(resource_type(res) != IORESOURCE_MEM);
59 pfc->window[k].phys = res->start;
60 pfc->window[k].size = resource_size(res);
61 pfc->window[k].virt = ioremap_nocache(res->start,
63 if (!pfc->window[k].virt)
75 static void __iomem *pfc_phys_to_virt(struct sh_pfc *pfc,
76 unsigned long address)
78 struct pfc_window *window;
81 /* scan through physical windows and convert address */
82 for (k = 0; k < pfc->num_resources; k++) {
83 window = pfc->window + k;
85 if (address < window->phys)
88 if (address >= (window->phys + window->size))
91 return window->virt + (address - window->phys);
94 /* no windows defined, register must be 1:1 mapped virt:phys */
95 return (void __iomem *)address;
98 static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
100 if (enum_id < r->begin)
103 if (enum_id > r->end)
109 static unsigned long gpio_read_raw_reg(void __iomem *mapped_reg,
110 unsigned long reg_width)
114 return ioread8(mapped_reg);
116 return ioread16(mapped_reg);
118 return ioread32(mapped_reg);
125 static void gpio_write_raw_reg(void __iomem *mapped_reg,
126 unsigned long reg_width,
131 iowrite8(data, mapped_reg);
134 iowrite16(data, mapped_reg);
137 iowrite32(data, mapped_reg);
144 int sh_pfc_read_bit(struct pinmux_data_reg *dr, unsigned long in_pos)
148 pos = dr->reg_width - (in_pos + 1);
150 pr_debug("read_bit: addr = %lx, pos = %ld, "
151 "r_width = %ld\n", dr->reg, pos, dr->reg_width);
153 return (gpio_read_raw_reg(dr->mapped_reg, dr->reg_width) >> pos) & 1;
155 EXPORT_SYMBOL_GPL(sh_pfc_read_bit);
157 void sh_pfc_write_bit(struct pinmux_data_reg *dr, unsigned long in_pos,
162 pos = dr->reg_width - (in_pos + 1);
164 pr_debug("write_bit addr = %lx, value = %d, pos = %ld, "
166 dr->reg, !!value, pos, dr->reg_width);
169 set_bit(pos, &dr->reg_shadow);
171 clear_bit(pos, &dr->reg_shadow);
173 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
175 EXPORT_SYMBOL_GPL(sh_pfc_write_bit);
177 static void config_reg_helper(struct sh_pfc *pfc,
178 struct pinmux_cfg_reg *crp,
179 unsigned long in_pos,
180 void __iomem **mapped_regp,
181 unsigned long *maskp,
186 *mapped_regp = pfc_phys_to_virt(pfc, crp->reg);
188 if (crp->field_width) {
189 *maskp = (1 << crp->field_width) - 1;
190 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
192 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
193 *posp = crp->reg_width;
194 for (k = 0; k <= in_pos; k++)
195 *posp -= crp->var_field_width[k];
199 static int read_config_reg(struct sh_pfc *pfc,
200 struct pinmux_cfg_reg *crp,
203 void __iomem *mapped_reg;
204 unsigned long mask, pos;
206 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
208 pr_debug("read_reg: addr = %lx, field = %ld, "
209 "r_width = %ld, f_width = %ld\n",
210 crp->reg, field, crp->reg_width, crp->field_width);
212 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
215 static void write_config_reg(struct sh_pfc *pfc,
216 struct pinmux_cfg_reg *crp,
217 unsigned long field, unsigned long value)
219 void __iomem *mapped_reg;
220 unsigned long mask, pos, data;
222 config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
224 pr_debug("write_reg addr = %lx, value = %ld, field = %ld, "
225 "r_width = %ld, f_width = %ld\n",
226 crp->reg, value, field, crp->reg_width, crp->field_width);
228 mask = ~(mask << pos);
229 value = value << pos;
231 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
236 gpio_write_raw_reg(pfc_phys_to_virt(pfc, pfc->unlock_reg),
239 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
242 static int setup_data_reg(struct sh_pfc *pfc, unsigned gpio)
244 struct pinmux_gpio *gpiop = &pfc->gpios[gpio];
245 struct pinmux_data_reg *data_reg;
248 if (!enum_in_range(gpiop->enum_id, &pfc->data))
253 data_reg = pfc->data_regs + k;
255 if (!data_reg->reg_width)
258 data_reg->mapped_reg = pfc_phys_to_virt(pfc, data_reg->reg);
260 for (n = 0; n < data_reg->reg_width; n++) {
261 if (data_reg->enum_ids[n] == gpiop->enum_id) {
262 gpiop->flags &= ~PINMUX_FLAG_DREG;
263 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
264 gpiop->flags &= ~PINMUX_FLAG_DBIT;
265 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
277 static void setup_data_regs(struct sh_pfc *pfc)
279 struct pinmux_data_reg *drp;
282 for (k = pfc->first_gpio; k <= pfc->last_gpio; k++)
283 setup_data_reg(pfc, k);
287 drp = pfc->data_regs + k;
292 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
298 int sh_pfc_get_data_reg(struct sh_pfc *pfc, unsigned gpio,
299 struct pinmux_data_reg **drp, int *bitp)
301 struct pinmux_gpio *gpiop = &pfc->gpios[gpio];
304 if (!enum_in_range(gpiop->enum_id, &pfc->data))
307 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
308 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
309 *drp = pfc->data_regs + k;
313 EXPORT_SYMBOL_GPL(sh_pfc_get_data_reg);
315 static int get_config_reg(struct sh_pfc *pfc, pinmux_enum_t enum_id,
316 struct pinmux_cfg_reg **crp,
317 int *fieldp, int *valuep,
318 unsigned long **cntp)
320 struct pinmux_cfg_reg *config_reg;
321 unsigned long r_width, f_width, curr_width, ncomb;
322 int k, m, n, pos, bit_pos;
326 config_reg = pfc->cfg_regs + k;
328 r_width = config_reg->reg_width;
329 f_width = config_reg->field_width;
336 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
338 curr_width = f_width;
340 curr_width = config_reg->var_field_width[m];
342 ncomb = 1 << curr_width;
343 for (n = 0; n < ncomb; n++) {
344 if (config_reg->enum_ids[pos + n] == enum_id) {
348 *cntp = &config_reg->cnt[m];
361 int sh_pfc_gpio_to_enum(struct sh_pfc *pfc, unsigned gpio, int pos,
362 pinmux_enum_t *enum_idp)
364 pinmux_enum_t enum_id = pfc->gpios[gpio].enum_id;
365 pinmux_enum_t *data = pfc->gpio_data;
368 if (!enum_in_range(enum_id, &pfc->data)) {
369 if (!enum_in_range(enum_id, &pfc->mark)) {
370 pr_err("non data/mark enum_id for gpio %d\n", gpio);
376 *enum_idp = data[pos + 1];
380 for (k = 0; k < pfc->gpio_data_size; k++) {
381 if (data[k] == enum_id) {
382 *enum_idp = data[k + 1];
387 pr_err("cannot locate data/mark enum_id for gpio %d\n", gpio);
390 EXPORT_SYMBOL_GPL(sh_pfc_gpio_to_enum);
392 int sh_pfc_config_gpio(struct sh_pfc *pfc, unsigned gpio, int pinmux_type,
395 struct pinmux_cfg_reg *cr = NULL;
396 pinmux_enum_t enum_id;
397 struct pinmux_range *range;
398 int in_range, pos, field, value;
401 switch (pinmux_type) {
403 case PINMUX_TYPE_FUNCTION:
407 case PINMUX_TYPE_OUTPUT:
408 range = &pfc->output;
411 case PINMUX_TYPE_INPUT:
415 case PINMUX_TYPE_INPUT_PULLUP:
416 range = &pfc->input_pu;
419 case PINMUX_TYPE_INPUT_PULLDOWN:
420 range = &pfc->input_pd;
432 pos = sh_pfc_gpio_to_enum(pfc, gpio, pos, &enum_id);
439 /* first check if this is a function enum */
440 in_range = enum_in_range(enum_id, &pfc->function);
442 /* not a function enum */
445 * other range exists, so this pin is
446 * a regular GPIO pin that now is being
447 * bound to a specific direction.
449 * for this case we only allow function enums
450 * and the enums that match the other range.
452 in_range = enum_in_range(enum_id, range);
455 * special case pass through for fixed
456 * input-only or output-only pins without
457 * function enum register association.
459 if (in_range && enum_id == range->force)
463 * no other range exists, so this pin
464 * must then be of the function type.
466 * allow function type pins to select
467 * any combination of function/in/out
468 * in their MARK lists.
477 if (get_config_reg(pfc, enum_id, &cr,
478 &field, &value, &cntp) != 0)
482 case GPIO_CFG_DRYRUN:
484 (read_config_reg(pfc, cr, field) != value))
489 write_config_reg(pfc, cr, field, value);
503 EXPORT_SYMBOL_GPL(sh_pfc_config_gpio);
505 int register_sh_pfc(struct sh_pfc *pfc)
507 int (*initroutine)(struct sh_pfc *) = NULL;
511 * Ensure that the type encoding fits
513 BUILD_BUG_ON(PINMUX_FLAG_TYPE > ((1 << PINMUX_FLAG_DBIT_SHIFT) - 1));
518 ret = pfc_ioremap(pfc);
519 if (unlikely(ret < 0))
522 spin_lock_init(&pfc->lock);
524 pinctrl_provide_dummies();
525 setup_data_regs(pfc);
530 * Initialize pinctrl bindings first
532 initroutine = symbol_request(sh_pfc_register_pinctrl);
534 ret = (*initroutine)(pfc);
535 symbol_put_addr(initroutine);
537 if (unlikely(ret != 0))
540 pr_err("failed to initialize pinctrl bindings\n");
547 initroutine = symbol_request(sh_pfc_register_gpiochip);
549 ret = (*initroutine)(pfc);
550 symbol_put_addr(initroutine);
553 * If the GPIO chip fails to come up we still leave the
554 * PFC state as it is, given that there are already
555 * extant users of it that have succeeded by this point.
557 if (unlikely(ret != 0)) {
558 pr_notice("failed to init GPIO chip, ignoring...\n");
563 pr_info("%s support registered\n", pfc->name);