blob: 0b412c2e0c8f04f854fc48e235c2d39e7f8d1c83 [file] [log] [blame]
Nobuhiro Iwamatsud764c502012-06-21 11:26:38 +09001/*
2 * Pinmuxed GPIO support for SuperH.
3 * Copy from linux kernel driver/sh/pfc.c
4 *
5 * Copyright (C) 2008 Magnus Damm
6 *
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
9 * for more details.
10 */
11
12#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <malloc.h>
Nobuhiro Iwamatsud764c502012-06-21 11:26:38 +090015#include <asm/bitops.h>
16#include <asm/io.h>
17#include <sh_pfc.h>
18
19static struct pinmux_info *gpioc;
20
21#define pfc_phys_to_virt(p, a) ((void *)a)
22
23static int enum_in_range(pinmux_enum_t enum_id, struct pinmux_range *r)
24{
25 if (enum_id < r->begin)
26 return 0;
27
28 if (enum_id > r->end)
29 return 0;
30
31 return 1;
32}
33
34static unsigned long gpio_read_raw_reg(void *mapped_reg,
35 unsigned long reg_width)
36{
37 switch (reg_width) {
38
39 case 8:
40 return readb(mapped_reg);
41 case 16:
42 return readw(mapped_reg);
43 case 32:
44 return readl(mapped_reg);
45 }
46
47 BUG();
48 return 0;
49}
50
51static void gpio_write_raw_reg(void *mapped_reg,
52 unsigned long reg_width,
53 unsigned long data)
54{
55 switch (reg_width) {
56 case 8:
57 writeb(data, mapped_reg);
58 return;
59 case 16:
60 writew(data, mapped_reg);
61 return;
62 case 32:
63 writel(data, mapped_reg);
64 return;
65 }
66
67 BUG();
68}
69
70static int gpio_read_bit(struct pinmux_data_reg *dr,
Kouei Abe1815c292017-05-13 15:48:04 +020071 unsigned long offset,
Nobuhiro Iwamatsud764c502012-06-21 11:26:38 +090072 unsigned long in_pos)
73{
74 unsigned long pos;
75
76 pos = dr->reg_width - (in_pos + 1);
77
Kouei Abe1815c292017-05-13 15:48:04 +020078 debug("read_bit: addr = %lx, pos = %ld, r_width = %ld\n",
79 dr->reg + offset, pos, dr->reg_width);
Nobuhiro Iwamatsud764c502012-06-21 11:26:38 +090080
Kouei Abe1815c292017-05-13 15:48:04 +020081 return (gpio_read_raw_reg(dr->mapped_reg + offset,
82 dr->reg_width) >> pos) & 1;
Nobuhiro Iwamatsud764c502012-06-21 11:26:38 +090083}
84
85static void gpio_write_bit(struct pinmux_data_reg *dr,
86 unsigned long in_pos, unsigned long value)
87{
88 unsigned long pos;
89
90 pos = dr->reg_width - (in_pos + 1);
91
92 debug("write_bit addr = %lx, value = %d, pos = %ld, "
93 "r_width = %ld\n",
94 dr->reg, !!value, pos, dr->reg_width);
95
96 if (value)
97 __set_bit(pos, &dr->reg_shadow);
98 else
99 __clear_bit(pos, &dr->reg_shadow);
100
101 gpio_write_raw_reg(dr->mapped_reg, dr->reg_width, dr->reg_shadow);
102}
103
104static void config_reg_helper(struct pinmux_info *gpioc,
105 struct pinmux_cfg_reg *crp,
106 unsigned long in_pos,
107#if 0
108 void __iomem **mapped_regp,
109#else
110 void **mapped_regp,
111#endif
112 unsigned long *maskp,
113 unsigned long *posp)
114{
115 int k;
116
117 *mapped_regp = pfc_phys_to_virt(gpioc, crp->reg);
118
119 if (crp->field_width) {
120 *maskp = (1 << crp->field_width) - 1;
121 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
122 } else {
123 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
124 *posp = crp->reg_width;
125 for (k = 0; k <= in_pos; k++)
126 *posp -= crp->var_field_width[k];
127 }
128}
129
130static int read_config_reg(struct pinmux_info *gpioc,
131 struct pinmux_cfg_reg *crp,
132 unsigned long field)
133{
134 void *mapped_reg;
135
136 unsigned long mask, pos;
137
138 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
139
140 debug("read_reg: addr = %lx, field = %ld, "
141 "r_width = %ld, f_width = %ld\n",
142 crp->reg, field, crp->reg_width, crp->field_width);
143
144 return (gpio_read_raw_reg(mapped_reg, crp->reg_width) >> pos) & mask;
145}
146
147static void write_config_reg(struct pinmux_info *gpioc,
148 struct pinmux_cfg_reg *crp,
149 unsigned long field, unsigned long value)
150{
151 void *mapped_reg;
152 unsigned long mask, pos, data;
153
154 config_reg_helper(gpioc, crp, field, &mapped_reg, &mask, &pos);
155
156 debug("write_reg addr = %lx, value = %ld, field = %ld, "
157 "r_width = %ld, f_width = %ld\n",
158 crp->reg, value, field, crp->reg_width, crp->field_width);
159
160 mask = ~(mask << pos);
161 value = value << pos;
162
163 data = gpio_read_raw_reg(mapped_reg, crp->reg_width);
164 data &= mask;
165 data |= value;
166
167 if (gpioc->unlock_reg)
168 gpio_write_raw_reg(pfc_phys_to_virt(gpioc, gpioc->unlock_reg),
169 32, ~data);
170
171 gpio_write_raw_reg(mapped_reg, crp->reg_width, data);
172}
173
174static int setup_data_reg(struct pinmux_info *gpioc, unsigned gpio)
175{
176 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
177 struct pinmux_data_reg *data_reg;
178 int k, n;
179
180 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
181 return -1;
182
183 k = 0;
184 while (1) {
185 data_reg = gpioc->data_regs + k;
186
187 if (!data_reg->reg_width)
188 break;
189
190 data_reg->mapped_reg = pfc_phys_to_virt(gpioc, data_reg->reg);
191
192 for (n = 0; n < data_reg->reg_width; n++) {
193 if (data_reg->enum_ids[n] == gpiop->enum_id) {
194 gpiop->flags &= ~PINMUX_FLAG_DREG;
195 gpiop->flags |= (k << PINMUX_FLAG_DREG_SHIFT);
196 gpiop->flags &= ~PINMUX_FLAG_DBIT;
197 gpiop->flags |= (n << PINMUX_FLAG_DBIT_SHIFT);
198 return 0;
199 }
200 }
201 k++;
202 }
203
204 BUG();
205
206 return -1;
207}
208
209static void setup_data_regs(struct pinmux_info *gpioc)
210{
211 struct pinmux_data_reg *drp;
212 int k;
213
214 for (k = gpioc->first_gpio; k <= gpioc->last_gpio; k++)
215 setup_data_reg(gpioc, k);
216
217 k = 0;
218 while (1) {
219 drp = gpioc->data_regs + k;
220
221 if (!drp->reg_width)
222 break;
223
224 drp->reg_shadow = gpio_read_raw_reg(drp->mapped_reg,
225 drp->reg_width);
226 k++;
227 }
228}
229
230static int get_data_reg(struct pinmux_info *gpioc, unsigned gpio,
231 struct pinmux_data_reg **drp, int *bitp)
232{
233 struct pinmux_gpio *gpiop = &gpioc->gpios[gpio];
234 int k, n;
235
236 if (!enum_in_range(gpiop->enum_id, &gpioc->data))
237 return -1;
238
239 k = (gpiop->flags & PINMUX_FLAG_DREG) >> PINMUX_FLAG_DREG_SHIFT;
240 n = (gpiop->flags & PINMUX_FLAG_DBIT) >> PINMUX_FLAG_DBIT_SHIFT;
241 *drp = gpioc->data_regs + k;
242 *bitp = n;
243 return 0;
244}
245
246static int get_config_reg(struct pinmux_info *gpioc, pinmux_enum_t enum_id,
247 struct pinmux_cfg_reg **crp,
248 int *fieldp, int *valuep,
249 unsigned long **cntp)
250{
251 struct pinmux_cfg_reg *config_reg;
252 unsigned long r_width, f_width, curr_width, ncomb;
253 int k, m, n, pos, bit_pos;
254
255 k = 0;
256 while (1) {
257 config_reg = gpioc->cfg_regs + k;
258
259 r_width = config_reg->reg_width;
260 f_width = config_reg->field_width;
261
262 if (!r_width)
263 break;
264
265 pos = 0;
266 m = 0;
267 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
268 if (f_width)
269 curr_width = f_width;
270 else
271 curr_width = config_reg->var_field_width[m];
272
273 ncomb = 1 << curr_width;
274 for (n = 0; n < ncomb; n++) {
275 if (config_reg->enum_ids[pos + n] == enum_id) {
276 *crp = config_reg;
277 *fieldp = m;
278 *valuep = n;
279 *cntp = &config_reg->cnt[m];
280 return 0;
281 }
282 }
283 pos += ncomb;
284 m++;
285 }
286 k++;
287 }
288
289 return -1;
290}
291
292static int get_gpio_enum_id(struct pinmux_info *gpioc, unsigned gpio,
293 int pos, pinmux_enum_t *enum_idp)
294{
295 pinmux_enum_t enum_id = gpioc->gpios[gpio].enum_id;
296 pinmux_enum_t *data = gpioc->gpio_data;
297 int k;
298
299 if (!enum_in_range(enum_id, &gpioc->data)) {
300 if (!enum_in_range(enum_id, &gpioc->mark)) {
301 debug("non data/mark enum_id for gpio %d\n", gpio);
302 return -1;
303 }
304 }
305
306 if (pos) {
307 *enum_idp = data[pos + 1];
308 return pos + 1;
309 }
310
311 for (k = 0; k < gpioc->gpio_data_size; k++) {
312 if (data[k] == enum_id) {
313 *enum_idp = data[k + 1];
314 return k + 1;
315 }
316 }
317
318 debug("cannot locate data/mark enum_id for gpio %d\n", gpio);
319 return -1;
320}
321
322enum { GPIO_CFG_DRYRUN, GPIO_CFG_REQ, GPIO_CFG_FREE };
323
324static int pinmux_config_gpio(struct pinmux_info *gpioc, unsigned gpio,
325 int pinmux_type, int cfg_mode)
326{
327 struct pinmux_cfg_reg *cr = NULL;
328 pinmux_enum_t enum_id;
329 struct pinmux_range *range;
330 int in_range, pos, field, value;
331 unsigned long *cntp;
332
333 switch (pinmux_type) {
334
335 case PINMUX_TYPE_FUNCTION:
336 range = NULL;
337 break;
338
339 case PINMUX_TYPE_OUTPUT:
340 range = &gpioc->output;
341 break;
342
343 case PINMUX_TYPE_INPUT:
344 range = &gpioc->input;
345 break;
346
347 case PINMUX_TYPE_INPUT_PULLUP:
348 range = &gpioc->input_pu;
349 break;
350
351 case PINMUX_TYPE_INPUT_PULLDOWN:
352 range = &gpioc->input_pd;
353 break;
354
355 default:
356 goto out_err;
357 }
358
359 pos = 0;
360 enum_id = 0;
361 field = 0;
362 value = 0;
363 while (1) {
364 pos = get_gpio_enum_id(gpioc, gpio, pos, &enum_id);
365 if (pos <= 0)
366 goto out_err;
367
368 if (!enum_id)
369 break;
370
371 /* first check if this is a function enum */
372 in_range = enum_in_range(enum_id, &gpioc->function);
373 if (!in_range) {
374 /* not a function enum */
375 if (range) {
376 /*
377 * other range exists, so this pin is
378 * a regular GPIO pin that now is being
379 * bound to a specific direction.
380 *
381 * for this case we only allow function enums
382 * and the enums that match the other range.
383 */
384 in_range = enum_in_range(enum_id, range);
385
386 /*
387 * special case pass through for fixed
388 * input-only or output-only pins without
389 * function enum register association.
390 */
391 if (in_range && enum_id == range->force)
392 continue;
393 } else {
394 /*
395 * no other range exists, so this pin
396 * must then be of the function type.
397 *
398 * allow function type pins to select
399 * any combination of function/in/out
400 * in their MARK lists.
401 */
402 in_range = 1;
403 }
404 }
405
406 if (!in_range)
407 continue;
408
409 if (get_config_reg(gpioc, enum_id, &cr,
410 &field, &value, &cntp) != 0)
411 goto out_err;
412
413 switch (cfg_mode) {
414 case GPIO_CFG_DRYRUN:
415 if (!*cntp ||
416 (read_config_reg(gpioc, cr, field) != value))
417 continue;
418 break;
419
420 case GPIO_CFG_REQ:
421 write_config_reg(gpioc, cr, field, value);
422 *cntp = *cntp + 1;
423 break;
424
425 case GPIO_CFG_FREE:
426 *cntp = *cntp - 1;
427 break;
428 }
429 }
430
431 return 0;
432 out_err:
433 return -1;
434}
435
436#if 0
437static DEFINE_SPINLOCK(gpio_lock);
438static struct pinmux_info *chip_to_pinmux(struct gpio_chip *chip)
439{
440 return container_of(chip, struct pinmux_info, chip);
441}
442#endif
443
444static int sh_gpio_request(unsigned offset)
445{
446 struct pinmux_data_reg *dummy;
447 int i, ret, pinmux_type;
448
449 ret = -1;
450
451 if (!gpioc)
452 goto err_out;
453
454 if ((gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE) != PINMUX_TYPE_NONE)
455 goto err_out;
456
457 /* setup pin function here if no data is associated with pin */
458
459 if (get_data_reg(gpioc, offset, &dummy, &i) != 0)
460 pinmux_type = PINMUX_TYPE_FUNCTION;
461 else
462 pinmux_type = PINMUX_TYPE_GPIO;
463
464 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
465 if (pinmux_config_gpio(gpioc, offset,
466 pinmux_type,
467 GPIO_CFG_DRYRUN) != 0)
468 goto err_out;
469
470 if (pinmux_config_gpio(gpioc, offset,
471 pinmux_type,
472 GPIO_CFG_REQ) != 0)
473 BUG();
474 }
475
476 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
477 gpioc->gpios[offset].flags |= pinmux_type;
478
479 ret = 0;
480err_out:
481 return ret;
482}
483
484static void sh_gpio_free(unsigned offset)
485{
486 int pinmux_type;
487
488 if (!gpioc)
489 return;
490
491 pinmux_type = gpioc->gpios[offset].flags & PINMUX_FLAG_TYPE;
492 pinmux_config_gpio(gpioc, offset, pinmux_type, GPIO_CFG_FREE);
493 gpioc->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
494 gpioc->gpios[offset].flags |= PINMUX_TYPE_NONE;
495}
496
497static int pinmux_direction(struct pinmux_info *gpioc,
498 unsigned gpio, int new_pinmux_type)
499{
500 int pinmux_type;
501 int ret = -1;
502
503 if (!gpioc)
504 goto err_out;
505
506 pinmux_type = gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE;
507
508 switch (pinmux_type) {
509 case PINMUX_TYPE_GPIO:
510 break;
511 case PINMUX_TYPE_OUTPUT:
512 case PINMUX_TYPE_INPUT:
513 case PINMUX_TYPE_INPUT_PULLUP:
514 case PINMUX_TYPE_INPUT_PULLDOWN:
515 pinmux_config_gpio(gpioc, gpio, pinmux_type, GPIO_CFG_FREE);
516 break;
517 default:
518 goto err_out;
519 }
520
521 if (pinmux_config_gpio(gpioc, gpio,
522 new_pinmux_type,
523 GPIO_CFG_DRYRUN) != 0)
524 goto err_out;
525
526 if (pinmux_config_gpio(gpioc, gpio,
527 new_pinmux_type,
528 GPIO_CFG_REQ) != 0)
529 BUG();
530
531 gpioc->gpios[gpio].flags &= ~PINMUX_FLAG_TYPE;
532 gpioc->gpios[gpio].flags |= new_pinmux_type;
533
534 ret = 0;
535 err_out:
536 return ret;
537}
538
539static int sh_gpio_direction_input(unsigned offset)
540{
541 return pinmux_direction(gpioc, offset, PINMUX_TYPE_INPUT);
542}
543
544static void sh_gpio_set_value(struct pinmux_info *gpioc,
545 unsigned gpio, int value)
546{
547 struct pinmux_data_reg *dr = NULL;
548 int bit = 0;
549
550 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
551 BUG();
552 else
553 gpio_write_bit(dr, bit, value);
554}
555
556static int sh_gpio_direction_output(unsigned offset, int value)
557{
558 sh_gpio_set_value(gpioc, offset, value);
559 return pinmux_direction(gpioc, offset, PINMUX_TYPE_OUTPUT);
560}
561
562static int sh_gpio_get_value(struct pinmux_info *gpioc, unsigned gpio)
563{
564 struct pinmux_data_reg *dr = NULL;
Kouei Abe1815c292017-05-13 15:48:04 +0200565 int bit = 0, offset = 0;
Nobuhiro Iwamatsud764c502012-06-21 11:26:38 +0900566
567 if (!gpioc || get_data_reg(gpioc, gpio, &dr, &bit) != 0)
568 return -1;
Kouei Abe1815c292017-05-13 15:48:04 +0200569#if defined(CONFIG_RCAR_GEN3)
570 if ((gpioc->gpios[gpio].flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_INPUT)
571 offset += 4;
572#endif
Nobuhiro Iwamatsud764c502012-06-21 11:26:38 +0900573
Kouei Abe1815c292017-05-13 15:48:04 +0200574 return gpio_read_bit(dr, offset, bit);
Nobuhiro Iwamatsud764c502012-06-21 11:26:38 +0900575}
576
577static int sh_gpio_get(unsigned offset)
578{
579 return sh_gpio_get_value(gpioc, offset);
580}
581
582static void sh_gpio_set(unsigned offset, int value)
583{
584 sh_gpio_set_value(gpioc, offset, value);
585}
586
587int register_pinmux(struct pinmux_info *pip)
588{
589 if (pip != NULL) {
590 gpioc = pip;
591 debug("%s deregistering\n", pip->name);
592 setup_data_regs(gpioc);
593 }
594 return 0;
595}
596
597int unregister_pinmux(struct pinmux_info *pip)
598{
599 debug("%s deregistering\n", pip->name);
600 if (gpioc != pip)
601 return -1;
602
603 gpioc = NULL;
604 return 0;
605}
606
607int gpio_request(unsigned gpio, const char *label)
608{
609 sh_gpio_request(gpio);
610 return 0;
611}
612
613int gpio_free(unsigned gpio)
614{
615 sh_gpio_free(gpio);
616 return 0;
617}
618
619int gpio_direction_input(unsigned gpio)
620{
621 return sh_gpio_direction_input(gpio);
622}
623
624int gpio_direction_output(unsigned gpio, int value)
625{
626 return sh_gpio_direction_output(gpio, value);
627}
628
629void gpio_set_value(unsigned gpio, int value)
630{
631 sh_gpio_set(gpio, value);
632}
633
634int gpio_get_value(unsigned gpio)
635{
636 return sh_gpio_get(gpio);
637}