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