a445cdba71302c5be9d6d01d7f681462e8388d73
[platform/kernel/u-boot.git] / drivers / pinctrl / renesas / pfc-r7s72100.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * R7S72100 processor support
4  *
5  * Copyright (C) 2019 Marek Vasut <marek.vasut@gmail.com>
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <asm/global_data.h>
11 #include <dm/lists.h>
12 #include <dm/pinctrl.h>
13 #include <linux/bitops.h>
14 #include <linux/io.h>
15 #include <linux/err.h>
16
17 #define P(bank)                 (0x0000 + (bank) * 4)
18 #define PSR(bank)               (0x0100 + (bank) * 4)
19 #define PPR(bank)               (0x0200 + (bank) * 4)
20 #define PM(bank)                (0x0300 + (bank) * 4)
21 #define PMC(bank)               (0x0400 + (bank) * 4)
22 #define PFC(bank)               (0x0500 + (bank) * 4)
23 #define PFCE(bank)              (0x0600 + (bank) * 4)
24 #define PNOT(bank)              (0x0700 + (bank) * 4)
25 #define PMSR(bank)              (0x0800 + (bank) * 4)
26 #define PMCSR(bank)             (0x0900 + (bank) * 4)
27 #define PFCAE(bank)             (0x0A00 + (bank) * 4)
28 #define PIBC(bank)              (0x4000 + (bank) * 4)
29 #define PBDC(bank)              (0x4100 + (bank) * 4)
30 #define PIPC(bank)              (0x4200 + (bank) * 4)
31
32 #define RZA1_PINS_PER_PORT      16
33
34 DECLARE_GLOBAL_DATA_PTR;
35
36 struct r7s72100_pfc_plat {
37         void __iomem    *base;
38 };
39
40 static void r7s72100_pfc_set_function(struct udevice *dev, u16 bank, u16 line,
41                                       u16 func, u16 inbuf, u16 bidir)
42 {
43         struct r7s72100_pfc_plat *plat = dev_get_plat(dev);
44
45         clrsetbits_le16(plat->base + PFCAE(bank), BIT(line),
46                         (func & BIT(2)) ? BIT(line) : 0);
47         clrsetbits_le16(plat->base + PFCE(bank), BIT(line),
48                         (func & BIT(1)) ? BIT(line) : 0);
49         clrsetbits_le16(plat->base + PFC(bank), BIT(line),
50                         (func & BIT(0)) ? BIT(line) : 0);
51
52         clrsetbits_le16(plat->base + PIBC(bank), BIT(line),
53                         inbuf ? BIT(line) : 0);
54         clrsetbits_le16(plat->base + PBDC(bank), BIT(line),
55                         bidir ? BIT(line) : 0);
56
57         setbits_le32(plat->base + PMCSR(bank), BIT(line + 16) | BIT(line));
58
59         setbits_le16(plat->base + PIPC(bank), BIT(line));
60 }
61
62 static int r7s72100_pfc_set_state(struct udevice *dev, struct udevice *config)
63 {
64         const void *blob = gd->fdt_blob;
65         int node = dev_of_offset(config);
66         u32 cells[32];
67         u16 bank, line, func;
68         int i, count, bidir;
69
70         count = fdtdec_get_int_array_count(blob, node, "pinmux",
71                                            cells, ARRAY_SIZE(cells));
72         if (count < 0) {
73                 printf("%s: bad pinmux array %d\n", __func__, count);
74                 return -EINVAL;
75         }
76
77         if (count > ARRAY_SIZE(cells)) {
78                 printf("%s: unsupported pinmux array count %d\n",
79                        __func__, count);
80                 return -EINVAL;
81         }
82
83         for (i = 0 ; i < count; i++) {
84                 func = (cells[i] >> 16) & 0xf;
85                 if (func == 0 || func > 8) {
86                         printf("Invalid cell %i in node %s!\n",
87                                count, ofnode_get_name(dev_ofnode(config)));
88                         continue;
89                 }
90
91                 func = (func - 1) & 0x7;
92
93                 bank = (cells[i] / RZA1_PINS_PER_PORT) & 0xff;
94                 line = cells[i] % RZA1_PINS_PER_PORT;
95
96                 bidir = 0;
97                 if (bank == 3 && line == 3 && func == 1)
98                         bidir = 1;
99
100                 r7s72100_pfc_set_function(dev, bank, line, func, 0, bidir);
101         }
102
103         return 0;
104 }
105
106 const struct pinctrl_ops r7s72100_pfc_ops  = {
107         .set_state = r7s72100_pfc_set_state,
108 };
109
110 static int r7s72100_pfc_probe(struct udevice *dev)
111 {
112         struct r7s72100_pfc_plat *plat = dev_get_plat(dev);
113         fdt_addr_t addr_base;
114         ofnode node;
115
116         addr_base = dev_read_addr(dev);
117         if (addr_base == FDT_ADDR_T_NONE)
118                 return -EINVAL;
119
120         plat->base = (void __iomem *)addr_base;
121
122         dev_for_each_subnode(node, dev) {
123                 struct udevice *cdev;
124
125                 if (!ofnode_read_bool(node, "gpio-controller"))
126                         continue;
127
128                 device_bind_driver_to_node(dev, "r7s72100-gpio",
129                                            ofnode_get_name(node),
130                                            node, &cdev);
131         }
132
133         return 0;
134 }
135
136 static const struct udevice_id r7s72100_pfc_match[] = {
137         { .compatible = "renesas,r7s72100-ports" },
138         {}
139 };
140
141 U_BOOT_DRIVER(r7s72100_pfc) = {
142         .name           = "r7s72100_pfc",
143         .id             = UCLASS_PINCTRL,
144         .of_match       = r7s72100_pfc_match,
145         .probe          = r7s72100_pfc_probe,
146         .plat_auto      = sizeof(struct r7s72100_pfc_plat),
147         .ops            = &r7s72100_pfc_ops,
148 };