Merge tag 'dm-pull-6feb20' of https://gitlab.denx.de/u-boot/custodians/u-boot-dm
[oweals/u-boot.git] / drivers / pinctrl / intel / pinctrl.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2017 Intel Corp.
4  * Copyright 2019 Google LLC
5  *
6  * Taken partly from coreboot gpio.c
7  *
8  * Pinctrl is modelled as a separate device-tree node and device for each
9  * 'community' (basically a set of GPIOs). The separate devices work together
10  * and many functions permit any PINCTRL device to be provided as a parameter,
11  * since the pad numbering is unique across all devices.
12  *
13  * Each pinctrl has a single child GPIO device to handle GPIO access and
14  * therefore there is a simple GPIO driver included in this file.
15  */
16
17 #define LOG_CATEGORY UCLASS_GPIO
18
19 #include <common.h>
20 #include <dm.h>
21 #include <irq.h>
22 #include <malloc.h>
23 #include <p2sb.h>
24 #include <spl.h>
25 #include <asm-generic/gpio.h>
26 #include <asm/intel_pinctrl.h>
27 #include <asm/intel_pinctrl_defs.h>
28 #include <asm/arch/gpio.h>
29 #include <asm/itss.h>
30 #include <dm/device-internal.h>
31 #include <dt-bindings/gpio/gpio.h>
32 #include <linux/err.h>
33
34 #define GPIO_DW_SIZE(x)                 (sizeof(u32) * (x))
35 #define PAD_CFG_OFFSET(x, dw_num)       ((x) + GPIO_DW_SIZE(dw_num))
36 #define PAD_CFG0_OFFSET(x)              PAD_CFG_OFFSET(x, 0)
37 #define PAD_CFG1_OFFSET(x)              PAD_CFG_OFFSET(x, 1)
38
39 #define MISCCFG_GPE0_DW0_SHIFT 8
40 #define MISCCFG_GPE0_DW0_MASK (0xf << MISCCFG_GPE0_DW0_SHIFT)
41 #define MISCCFG_GPE0_DW1_SHIFT 12
42 #define MISCCFG_GPE0_DW1_MASK (0xf << MISCCFG_GPE0_DW1_SHIFT)
43 #define MISCCFG_GPE0_DW2_SHIFT 16
44 #define MISCCFG_GPE0_DW2_MASK (0xf << MISCCFG_GPE0_DW2_SHIFT)
45
46 #define GPI_SMI_STS_OFFSET(comm, group) ((comm)->gpi_smi_sts_reg_0 +    \
47                                 ((group) * sizeof(u32)))
48 #define GPI_SMI_EN_OFFSET(comm, group) ((comm)->gpi_smi_en_reg_0 +      \
49                                 ((group) * sizeof(u32)))
50 #define GPI_IS_OFFSET(comm, group) ((comm)->gpi_int_sts_reg_0 + \
51                                 ((group) * sizeof(uint32_t)))
52 #define GPI_IE_OFFSET(comm, group) ((comm)->gpi_int_en_reg_0 +  \
53                                 ((group) * sizeof(uint32_t)))
54
55 /**
56  * relative_pad_in_comm() - Get the relative position of a GPIO
57  *
58  * This finds the position of a GPIO within a community
59  *
60  * @comm: Community to search
61  * @gpio: Pad number to look up (assumed to be valid)
62  * @return offset, 0 for first GPIO in community
63  */
64 static size_t relative_pad_in_comm(const struct pad_community *comm,
65                                    uint gpio)
66 {
67         return gpio - comm->first_pad;
68 }
69
70 /**
71  * pinctrl_group_index() - Find group for a a pad
72  *
73  * Find the group within the community that the pad is a part of
74  *
75  * @comm: Community to search
76  * @relative_pad: Pad to look up
77  * @return group number if found (see community_n_groups, etc.), or
78  *      -ESPIPE if no groups, or -ENOENT if not found
79  */
80 static int pinctrl_group_index(const struct pad_community *comm,
81                                uint relative_pad)
82 {
83         int i;
84
85         if (!comm->groups)
86                 return -ESPIPE;
87
88         /* find the base pad number for this pad's group */
89         for (i = 0; i < comm->num_groups; i++) {
90                 if (relative_pad >= comm->groups[i].first_pad &&
91                     relative_pad < comm->groups[i].first_pad +
92                     comm->groups[i].size)
93                         return i;
94         }
95
96         return -ENOENT;
97 }
98
99 static int pinctrl_group_index_scaled(const struct pad_community *comm,
100                                       uint relative_pad, size_t scale)
101 {
102         int ret;
103
104         ret = pinctrl_group_index(comm, relative_pad);
105         if (ret < 0)
106                 return ret;
107
108         return ret * scale;
109 }
110
111 static int pinctrl_within_group(const struct pad_community *comm,
112                                 uint relative_pad)
113 {
114         int ret;
115
116         ret = pinctrl_group_index(comm, relative_pad);
117         if (ret < 0)
118                 return ret;
119
120         return relative_pad - comm->groups[ret].first_pad;
121 }
122
123 static u32 pinctrl_bitmask_within_group(const struct pad_community *comm,
124                                         uint relative_pad)
125 {
126         return 1U << pinctrl_within_group(comm, relative_pad);
127 }
128
129 /**
130  * pinctrl_get_device() - Find the device for a particular pad
131  *
132  * Each pinctr, device is attached to one community and this supports a number
133  * of pads. This function finds the device which controls a particular pad.
134  *
135  * @pad: Pad to check
136  * @devp: Returns the device for that pad
137  * @return 0 if OK, -ENOTBLK if no device was found for the given pin
138  */
139 static int pinctrl_get_device(uint pad, struct udevice **devp)
140 {
141         struct udevice *dev;
142
143         /*
144          * We have to probe each one of these since the community link is only
145          * attached in intel_pinctrl_ofdata_to_platdata().
146          */
147         uclass_foreach_dev_probe(UCLASS_PINCTRL, dev) {
148                 struct intel_pinctrl_priv *priv = dev_get_priv(dev);
149                 const struct pad_community *comm = priv->comm;
150
151                 if (pad >= comm->first_pad && pad <= comm->last_pad) {
152                         *devp = dev;
153                         return 0;
154                 }
155         }
156         printf("pad %d not found\n", pad);
157
158         return -ENOTBLK;
159 }
160
161 int intel_pinctrl_get_pad(uint pad, struct udevice **devp, uint *offsetp)
162 {
163         const struct pad_community *comm;
164         struct intel_pinctrl_priv *priv;
165         struct udevice *dev;
166         int ret;
167
168         ret = pinctrl_get_device(pad, &dev);
169         if (ret)
170                 return log_msg_ret("pad", ret);
171         priv = dev_get_priv(dev);
172         comm = priv->comm;
173         *devp = dev;
174         *offsetp = relative_pad_in_comm(comm, pad);
175
176         return 0;
177 }
178
179 static int pinctrl_configure_owner(struct udevice *dev,
180                                    const struct pad_config *cfg,
181                                    const struct pad_community *comm)
182 {
183         u32 hostsw_own;
184         u16 hostsw_own_offset;
185         int pin;
186         int ret;
187
188         pin = relative_pad_in_comm(comm, cfg->pad);
189
190         /*
191          * Based on the gpio pin number configure the corresponding bit in
192          * HOSTSW_OWN register. Value of 0x1 indicates GPIO Driver onwership.
193          */
194         hostsw_own_offset = comm->host_own_reg_0;
195         ret = pinctrl_group_index_scaled(comm, pin, sizeof(u32));
196         if (ret < 0)
197                 return ret;
198         hostsw_own_offset += ret;
199
200         hostsw_own = pcr_read32(dev, hostsw_own_offset);
201
202         /*
203          *The 4th bit in pad_config 1 (RO) is used to indicate if the pad
204          * needs GPIO driver ownership.  Set the bit if GPIO driver ownership
205          * requested, otherwise clear the bit.
206          */
207         if (cfg->pad_config[1] & PAD_CFG1_GPIO_DRIVER)
208                 hostsw_own |= pinctrl_bitmask_within_group(comm, pin);
209         else
210                 hostsw_own &= ~pinctrl_bitmask_within_group(comm, pin);
211
212         pcr_write32(dev, hostsw_own_offset, hostsw_own);
213
214         return 0;
215 }
216
217 static int gpi_enable_smi(struct udevice *dev, const struct pad_config *cfg,
218                           const struct pad_community *comm)
219 {
220         u32 value;
221         u16 sts_reg;
222         u16 en_reg;
223         int group;
224         int pin;
225         int ret;
226
227         if ((cfg->pad_config[0] & PAD_CFG0_ROUTE_SMI) != PAD_CFG0_ROUTE_SMI)
228                 return 0;
229
230         pin = relative_pad_in_comm(comm, cfg->pad);
231         ret = pinctrl_group_index(comm, pin);
232         if (ret < 0)
233                 return ret;
234         group = ret;
235
236         sts_reg = GPI_SMI_STS_OFFSET(comm, group);
237         value = pcr_read32(dev, sts_reg);
238         /* Write back 1 to reset the sts bits */
239         pcr_write32(dev, sts_reg, value);
240
241         /* Set enable bits */
242         en_reg = GPI_SMI_EN_OFFSET(comm, group);
243         pcr_setbits32(dev, en_reg, pinctrl_bitmask_within_group(comm, pin));
244
245         return 0;
246 }
247
248 static int pinctrl_configure_itss(struct udevice *dev,
249                                   const struct pad_config *cfg,
250                                   uint pad_cfg_offset)
251 {
252         struct intel_pinctrl_priv *priv = dev_get_priv(dev);
253
254         if (!priv->itss_pol_cfg)
255                 return -ENOSYS;
256
257         int irq;
258
259         /*
260          * Set up ITSS polarity if pad is routed to APIC.
261          *
262          * The ITSS takes only active high interrupt signals. Therefore,
263          * if the pad configuration indicates an inversion assume the
264          * intent is for the ITSS polarity. Before forwarding on the
265          * request to the APIC there's an inversion setting for how the
266          * signal is forwarded to the APIC. Honor the inversion setting
267          * in the GPIO pad configuration so that a hardware active low
268          * signal looks that way to the APIC (double inversion).
269          */
270         if (!(cfg->pad_config[0] & PAD_CFG0_ROUTE_IOAPIC))
271                 return 0;
272
273         irq = pcr_read32(dev, PAD_CFG1_OFFSET(pad_cfg_offset));
274         irq &= PAD_CFG1_IRQ_MASK;
275         if (!irq) {
276                 log_err("GPIO %u doesn't support APIC routing\n", cfg->pad);
277
278                 return -EPROTONOSUPPORT;
279         }
280         irq_set_polarity(priv->itss, irq,
281                          cfg->pad_config[0] & PAD_CFG0_RX_POL_INVERT);
282
283         return 0;
284 }
285
286 /* Number of DWx config registers can be different for different SOCs */
287 static uint pad_config_offset(struct intel_pinctrl_priv *priv, uint pad)
288 {
289         const struct pad_community *comm = priv->comm;
290         size_t offset;
291
292         offset = relative_pad_in_comm(comm, pad);
293         offset *= GPIO_DW_SIZE(priv->num_cfgs);
294
295         return offset + comm->pad_cfg_base;
296 }
297
298 static int pinctrl_pad_reset_config_override(const struct pad_community *comm,
299                                              u32 config_value)
300 {
301         const struct reset_mapping *rst_map = comm->reset_map;
302         int i;
303
304         /* Logical reset values equal chipset values */
305         if (!rst_map || !comm->num_reset_vals)
306                 return config_value;
307
308         for (i = 0; i < comm->num_reset_vals; i++, rst_map++) {
309                 if ((config_value & PAD_CFG0_RESET_MASK) == rst_map->logical) {
310                         config_value &= ~PAD_CFG0_RESET_MASK;
311                         config_value |= rst_map->chipset;
312
313                         return config_value;
314                 }
315         }
316         log_err("Logical-to-Chipset mapping not found\n");
317
318         return -ENOENT;
319 }
320
321 static const int mask[4] = {
322         PAD_CFG0_TX_STATE |
323         PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE | PAD_CFG0_MODE_MASK |
324         PAD_CFG0_ROUTE_MASK | PAD_CFG0_RXTENCFG_MASK |
325         PAD_CFG0_RXINV_MASK | PAD_CFG0_PREGFRXSEL |
326         PAD_CFG0_TRIG_MASK | PAD_CFG0_RXRAW1_MASK |
327         PAD_CFG0_RXPADSTSEL_MASK | PAD_CFG0_RESET_MASK,
328
329 #ifdef CONFIG_INTEL_PINCTRL_IOSTANDBY
330         PAD_CFG1_IOSTERM_MASK | PAD_CFG1_PULL_MASK | PAD_CFG1_IOSSTATE_MASK,
331 #else
332         PAD_CFG1_IOSTERM_MASK | PAD_CFG1_PULL_MASK,
333 #endif
334
335         PAD_CFG2_DEBOUNCE_MASK,
336
337         0,
338 };
339
340 /**
341  * pinctrl_configure_pad() - Configure a pad
342  *
343  * @dev: Pinctrl device containing the pad (see pinctrl_get_device())
344  * @cfg: Configuration to apply
345  * @return 0 if OK, -ve on error
346  */
347 static int pinctrl_configure_pad(struct udevice *dev,
348                                  const struct pad_config *cfg)
349 {
350         struct intel_pinctrl_priv *priv = dev_get_priv(dev);
351         const struct pad_community *comm = priv->comm;
352         uint config_offset;
353         u32 pad_conf, soc_pad_conf;
354         int ret;
355         int i;
356
357         if (IS_ERR(comm))
358                 return PTR_ERR(comm);
359         config_offset = pad_config_offset(priv, cfg->pad);
360         for (i = 0; i < priv->num_cfgs; i++) {
361                 pad_conf = pcr_read32(dev, PAD_CFG_OFFSET(config_offset, i));
362
363                 soc_pad_conf = cfg->pad_config[i];
364                 if (i == 0) {
365                         ret = pinctrl_pad_reset_config_override(comm,
366                                                                 soc_pad_conf);
367                         if (ret < 0)
368                                 return ret;
369                         soc_pad_conf = ret;
370                 }
371                 soc_pad_conf &= mask[i];
372                 soc_pad_conf |= pad_conf & ~mask[i];
373
374                 log_debug("pinctrl_padcfg [0x%02x, %02zd] DW%d [0x%08x : 0x%08x : 0x%08x]\n",
375                           comm->port, relative_pad_in_comm(comm, cfg->pad), i,
376                           pad_conf,/* old value */
377                           /* value passed from pinctrl table */
378                           cfg->pad_config[i],
379                           soc_pad_conf); /*new value*/
380                 pcr_write32(dev, PAD_CFG_OFFSET(config_offset, i),
381                             soc_pad_conf);
382         }
383         ret = pinctrl_configure_itss(dev, cfg, config_offset);
384         if (ret && ret != -ENOSYS)
385                 return log_msg_ret("itss config failed", ret);
386         ret = pinctrl_configure_owner(dev, cfg, comm);
387         if (ret)
388                 return ret;
389         ret = gpi_enable_smi(dev, cfg, comm);
390         if (ret)
391                 return ret;
392
393         return 0;
394 }
395
396 u32 intel_pinctrl_get_config_reg_addr(struct udevice *dev, uint offset)
397 {
398         struct intel_pinctrl_priv *priv = dev_get_priv(dev);
399         const struct pad_community *comm = priv->comm;
400         uint config_offset;
401
402         assert(device_get_uclass_id(dev) == UCLASS_PINCTRL);
403         config_offset = comm->pad_cfg_base + offset *
404                  GPIO_DW_SIZE(priv->num_cfgs);
405
406         return config_offset;
407 }
408
409 u32 intel_pinctrl_get_config_reg(struct udevice *dev, uint offset)
410 {
411         uint config_offset = intel_pinctrl_get_config_reg_addr(dev, offset);
412
413         return pcr_read32(dev, config_offset);
414 }
415
416 int intel_pinctrl_get_acpi_pin(struct udevice *dev, uint offset)
417 {
418         struct intel_pinctrl_priv *priv = dev_get_priv(dev);
419         const struct pad_community *comm = priv->comm;
420         int group;
421
422         group = pinctrl_group_index(comm, offset);
423
424         /* If pad base is not set then use GPIO number as ACPI pin number */
425         if (comm->groups[group].acpi_pad_base == PAD_BASE_NONE)
426                 return comm->first_pad + offset;
427
428         /*
429          * If this group has a non-zero pad base then compute the ACPI pin
430          * number from the pad base and the relative pad in the group.
431          */
432         return comm->groups[group].acpi_pad_base +
433                  pinctrl_within_group(comm, offset);
434 }
435
436 int pinctrl_route_gpe(struct udevice *itss, uint gpe0b, uint gpe0c, uint gpe0d)
437 {
438         struct udevice *pinctrl_dev;
439         u32 misccfg_value;
440         u32 misccfg_clr;
441         int ret;
442
443         /*
444          * Get the group here for community specific MISCCFG register.
445          * If any of these returns -1 then there is some error in devicetree
446          * where the group is probably hardcoded and does not comply with the
447          * PMC group defines. So we return from here and MISCFG is set to
448          * default.
449          */
450         ret = irq_route_pmc_gpio_gpe(itss, gpe0b);
451         if (ret)
452                 return ret;
453         gpe0b = ret;
454
455         ret = irq_route_pmc_gpio_gpe(itss, gpe0c);
456         if (ret)
457                 return ret;
458         gpe0c = ret;
459
460         ret = irq_route_pmc_gpio_gpe(itss, gpe0d);
461         if (ret)
462                 return ret;
463         gpe0d = ret;
464
465         misccfg_value = gpe0b << MISCCFG_GPE0_DW0_SHIFT;
466         misccfg_value |= gpe0c << MISCCFG_GPE0_DW1_SHIFT;
467         misccfg_value |= gpe0d << MISCCFG_GPE0_DW2_SHIFT;
468
469         /* Program GPIO_MISCCFG */
470         misccfg_clr = MISCCFG_GPE0_DW2_MASK | MISCCFG_GPE0_DW1_MASK |
471                 MISCCFG_GPE0_DW0_MASK;
472
473         log_debug("misccfg_clr:%x misccfg_value:%x\n", misccfg_clr,
474                   misccfg_value);
475         uclass_foreach_dev_probe(UCLASS_PINCTRL, pinctrl_dev) {
476                 pcr_clrsetbits32(pinctrl_dev, GPIO_MISCCFG, misccfg_clr,
477                                  misccfg_value);
478         }
479
480         return 0;
481 }
482
483 int pinctrl_gpi_clear_int_cfg(void)
484 {
485         struct udevice *dev;
486         struct uclass *uc;
487         int ret;
488
489         ret = uclass_get(UCLASS_PINCTRL, &uc);
490         if (ret)
491                 return log_msg_ret("pinctrl uc", ret);
492         uclass_foreach_dev(dev, uc) {
493                 struct intel_pinctrl_priv *priv = dev_get_priv(dev);
494                 const struct pad_community *comm = priv->comm;
495                 uint sts_value;
496                 int group;
497
498                 for (group = 0; group < comm->num_gpi_regs; group++) {
499                         /* Clear the enable register */
500                         pcr_write32(dev, GPI_IE_OFFSET(comm, group), 0);
501
502                         /* Read and clear the set status register bits*/
503                         sts_value = pcr_read32(dev,
504                                                GPI_IS_OFFSET(comm, group));
505                         pcr_write32(dev, GPI_IS_OFFSET(comm, group), sts_value);
506                 }
507         }
508
509         return 0;
510 }
511
512 int pinctrl_config_pads(struct udevice *dev, u32 *pads, int pads_count)
513 {
514         struct intel_pinctrl_priv *priv = dev_get_priv(dev);
515         const u32 *ptr;
516         int i;
517
518         log_debug("%s: pads_count=%d\n", __func__, pads_count);
519         for (ptr = pads, i = 0; i < pads_count;
520              ptr += 1 + priv->num_cfgs, i++) {
521                 struct udevice *pad_dev = NULL;
522                 struct pad_config *cfg;
523                 int ret;
524
525                 cfg = (struct pad_config *)ptr;
526                 ret = pinctrl_get_device(cfg->pad, &pad_dev);
527                 if (ret)
528                         return ret;
529                 ret = pinctrl_configure_pad(pad_dev, cfg);
530                 if (ret)
531                         return ret;
532         }
533
534         return 0;
535 }
536
537 int pinctrl_read_pads(struct udevice *dev, ofnode node, const char *prop,
538                       u32 **padsp, int *pad_countp)
539 {
540         struct intel_pinctrl_priv *priv = dev_get_priv(dev);
541         u32 *pads;
542         int size;
543         int ret;
544
545         *padsp = NULL;
546         *pad_countp = 0;
547         size = ofnode_read_size(node, prop);
548         if (size < 0)
549                 return 0;
550
551         pads = malloc(size);
552         if (!pads)
553                 return -ENOMEM;
554         size /= sizeof(fdt32_t);
555         ret = ofnode_read_u32_array(node, prop, pads, size);
556         if (ret) {
557                 free(pads);
558                 return ret;
559         }
560         *pad_countp = size / (1 + priv->num_cfgs);
561         *padsp = pads;
562
563         return 0;
564 }
565
566 int pinctrl_count_pads(struct udevice *dev, u32 *pads, int size)
567 {
568         struct intel_pinctrl_priv *priv = dev_get_priv(dev);
569         int count = 0;
570         int i;
571
572         for (i = 0; i < size;) {
573                 u32 val;
574                 int j;
575
576                 for (val = j = 0; j < priv->num_cfgs + 1; j++)
577                         val |= pads[i + j];
578                 if (!val)
579                         break;
580                 count++;
581                 i += priv->num_cfgs + 1;
582         }
583
584         return count;
585 }
586
587 int pinctrl_config_pads_for_node(struct udevice *dev, ofnode node)
588 {
589         int pads_count;
590         u32 *pads;
591         int ret;
592
593         if (device_get_uclass_id(dev) != UCLASS_PINCTRL)
594                 return log_msg_ret("uclass", -EPROTONOSUPPORT);
595         ret = pinctrl_read_pads(dev, node, "pads", &pads, &pads_count);
596         if (ret)
597                 return log_msg_ret("no pads", ret);
598         ret = pinctrl_config_pads(dev, pads, pads_count);
599         free(pads);
600         if (ret)
601                 return log_msg_ret("pad config", ret);
602
603         return 0;
604 }
605
606 int intel_pinctrl_ofdata_to_platdata(struct udevice *dev,
607                                      const struct pad_community *comm,
608                                      int num_cfgs)
609 {
610         struct p2sb_child_platdata *pplat = dev_get_parent_platdata(dev);
611         struct intel_pinctrl_priv *priv = dev_get_priv(dev);
612         int ret;
613
614         if (!comm) {
615                 log_err("Cannot find community for pid %d\n", pplat->pid);
616                 return -EDOM;
617         }
618         ret = irq_first_device_type(X86_IRQT_ITSS, &priv->itss);
619         if (ret)
620                 return log_msg_ret("Cannot find ITSS", ret);
621         priv->comm = comm;
622         priv->num_cfgs = num_cfgs;
623
624         return 0;
625 }
626
627 int intel_pinctrl_probe(struct udevice *dev)
628 {
629         struct intel_pinctrl_priv *priv = dev_get_priv(dev);
630
631         priv->itss_pol_cfg = true;
632
633         return 0;
634 }
635
636 const struct pinctrl_ops intel_pinctrl_ops = {
637         /* No operations are supported, but DM expects this to be present */
638 };