Marek Vasut | 6d99d96 | 2019-05-04 16:00:17 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019 Marek Vasut <marek.vasut@gmail.com> |
| 4 | */ |
| 5 | |
Marek Vasut | 6d99d96 | 2019-05-04 16:00:17 +0200 | [diff] [blame] | 6 | #include <clk.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Marek Vasut | 6d99d96 | 2019-05-04 16:00:17 +0200 | [diff] [blame] | 10 | #include <asm/gpio.h> |
| 11 | #include <asm/io.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Marek Vasut | 6d99d96 | 2019-05-04 16:00:17 +0200 | [diff] [blame] | 13 | |
| 14 | #define P(bank) (0x0000 + (bank) * 4) |
| 15 | #define PSR(bank) (0x0100 + (bank) * 4) |
| 16 | #define PPR(bank) (0x0200 + (bank) * 4) |
| 17 | #define PM(bank) (0x0300 + (bank) * 4) |
| 18 | #define PMC(bank) (0x0400 + (bank) * 4) |
| 19 | #define PFC(bank) (0x0500 + (bank) * 4) |
| 20 | #define PFCE(bank) (0x0600 + (bank) * 4) |
| 21 | #define PNOT(bank) (0x0700 + (bank) * 4) |
| 22 | #define PMSR(bank) (0x0800 + (bank) * 4) |
| 23 | #define PMCSR(bank) (0x0900 + (bank) * 4) |
| 24 | #define PFCAE(bank) (0x0A00 + (bank) * 4) |
| 25 | #define PIBC(bank) (0x4000 + (bank) * 4) |
| 26 | #define PBDC(bank) (0x4100 + (bank) * 4) |
| 27 | #define PIPC(bank) (0x4200 + (bank) * 4) |
| 28 | |
| 29 | #define RZA1_MAX_GPIO_PER_BANK 16 |
| 30 | |
| 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
| 33 | struct r7s72100_gpio_priv { |
| 34 | void __iomem *regs; |
| 35 | int bank; |
| 36 | }; |
| 37 | |
| 38 | static int r7s72100_gpio_get_value(struct udevice *dev, unsigned offset) |
| 39 | { |
| 40 | struct r7s72100_gpio_priv *priv = dev_get_priv(dev); |
| 41 | |
| 42 | return !!(readw(priv->regs + PPR(priv->bank)) & BIT(offset)); |
| 43 | } |
| 44 | |
| 45 | static int r7s72100_gpio_set_value(struct udevice *dev, unsigned line, |
| 46 | int value) |
| 47 | { |
| 48 | struct r7s72100_gpio_priv *priv = dev_get_priv(dev); |
| 49 | |
| 50 | writel(BIT(line + 16) | (value ? BIT(line) : 0), |
| 51 | priv->regs + PSR(priv->bank)); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static void r7s72100_gpio_set_direction(struct udevice *dev, unsigned line, |
| 57 | bool output) |
| 58 | { |
| 59 | struct r7s72100_gpio_priv *priv = dev_get_priv(dev); |
| 60 | |
| 61 | writel(BIT(line + 16), priv->regs + PMCSR(priv->bank)); |
| 62 | writel(BIT(line + 16) | (output ? 0 : BIT(line)), |
| 63 | priv->regs + PMSR(priv->bank)); |
| 64 | |
| 65 | clrsetbits_le16(priv->regs + PIBC(priv->bank), BIT(line), |
| 66 | output ? 0 : BIT(line)); |
| 67 | } |
| 68 | |
| 69 | static int r7s72100_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 70 | { |
| 71 | r7s72100_gpio_set_direction(dev, offset, false); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int r7s72100_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 76 | int value) |
| 77 | { |
| 78 | /* write GPIO value to output before selecting output mode of pin */ |
| 79 | r7s72100_gpio_set_value(dev, offset, value); |
| 80 | r7s72100_gpio_set_direction(dev, offset, true); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int r7s72100_gpio_get_function(struct udevice *dev, unsigned offset) |
| 86 | { |
| 87 | struct r7s72100_gpio_priv *priv = dev_get_priv(dev); |
| 88 | |
| 89 | if (readw(priv->regs + PM(priv->bank)) & BIT(offset)) |
| 90 | return GPIOF_INPUT; |
| 91 | else |
| 92 | return GPIOF_OUTPUT; |
| 93 | } |
| 94 | |
| 95 | static const struct dm_gpio_ops r7s72100_gpio_ops = { |
| 96 | .direction_input = r7s72100_gpio_direction_input, |
| 97 | .direction_output = r7s72100_gpio_direction_output, |
| 98 | .get_value = r7s72100_gpio_get_value, |
| 99 | .set_value = r7s72100_gpio_set_value, |
| 100 | .get_function = r7s72100_gpio_get_function, |
| 101 | }; |
| 102 | |
| 103 | static int r7s72100_gpio_probe(struct udevice *dev) |
| 104 | { |
| 105 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 106 | struct r7s72100_gpio_priv *priv = dev_get_priv(dev); |
| 107 | struct fdtdec_phandle_args args; |
| 108 | int node = dev_of_offset(dev); |
| 109 | int ret; |
| 110 | |
| 111 | fdt_addr_t addr_base; |
| 112 | |
| 113 | uc_priv->bank_name = dev->name; |
| 114 | dev = dev_get_parent(dev); |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 115 | addr_base = dev_read_addr(dev); |
Marek Vasut | 6d99d96 | 2019-05-04 16:00:17 +0200 | [diff] [blame] | 116 | if (addr_base == FDT_ADDR_T_NONE) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | priv->regs = (void __iomem *)addr_base; |
| 120 | |
| 121 | ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, node, "gpio-ranges", |
| 122 | NULL, 3, 0, &args); |
| 123 | priv->bank = ret == 0 ? (args.args[1] / RZA1_MAX_GPIO_PER_BANK) : -1; |
| 124 | uc_priv->gpio_count = ret == 0 ? args.args[2] : RZA1_MAX_GPIO_PER_BANK; |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | U_BOOT_DRIVER(r7s72100_gpio) = { |
| 130 | .name = "r7s72100-gpio", |
| 131 | .id = UCLASS_GPIO, |
| 132 | .ops = &r7s72100_gpio_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 133 | .priv_auto = sizeof(struct r7s72100_gpio_priv), |
Marek Vasut | 6d99d96 | 2019-05-04 16:00:17 +0200 | [diff] [blame] | 134 | .probe = r7s72100_gpio_probe, |
| 135 | }; |