]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
03d67e12 | 2 | /* |
88d5ecf4 | 3 | * Copyright (C) 2015 Thomas Chou <[email protected]> |
03d67e12 JF |
4 | * Copyright (C) 2011 Missing Link Electronics |
5 | * Joachim Foerster <[email protected]> | |
03d67e12 JF |
6 | */ |
7 | #include <common.h> | |
88d5ecf4 TC |
8 | #include <dm.h> |
9 | #include <errno.h> | |
10 | #include <malloc.h> | |
11 | #include <fdtdec.h> | |
401d1c4f | 12 | #include <asm/global_data.h> |
03d67e12 JF |
13 | #include <asm/io.h> |
14 | #include <asm/gpio.h> | |
15 | ||
88d5ecf4 | 16 | DECLARE_GLOBAL_DATA_PTR; |
03d67e12 | 17 | |
88d5ecf4 TC |
18 | struct altera_pio_regs { |
19 | u32 data; /* Data register */ | |
20 | u32 direction; /* Direction register */ | |
21 | }; | |
03d67e12 | 22 | |
8a8d24bd | 23 | struct altera_pio_plat { |
88d5ecf4 TC |
24 | struct altera_pio_regs *regs; |
25 | int gpio_count; | |
26 | const char *bank_name; | |
27 | }; | |
03d67e12 | 28 | |
88d5ecf4 | 29 | static int altera_pio_direction_input(struct udevice *dev, unsigned pin) |
03d67e12 | 30 | { |
8a8d24bd | 31 | struct altera_pio_plat *plat = dev_get_plat(dev); |
88d5ecf4 | 32 | struct altera_pio_regs *const regs = plat->regs; |
03d67e12 | 33 | |
88d5ecf4 | 34 | clrbits_le32(®s->direction, 1 << pin); |
03d67e12 | 35 | |
88d5ecf4 | 36 | return 0; |
03d67e12 JF |
37 | } |
38 | ||
88d5ecf4 TC |
39 | static int altera_pio_direction_output(struct udevice *dev, unsigned pin, |
40 | int val) | |
03d67e12 | 41 | { |
8a8d24bd | 42 | struct altera_pio_plat *plat = dev_get_plat(dev); |
88d5ecf4 | 43 | struct altera_pio_regs *const regs = plat->regs; |
03d67e12 | 44 | |
88d5ecf4 TC |
45 | if (val) |
46 | setbits_le32(®s->data, 1 << pin); | |
47 | else | |
48 | clrbits_le32(®s->data, 1 << pin); | |
49 | /* change the data first, then the direction. to avoid glitch */ | |
50 | setbits_le32(®s->direction, 1 << pin); | |
03d67e12 | 51 | |
88d5ecf4 | 52 | return 0; |
03d67e12 JF |
53 | } |
54 | ||
88d5ecf4 | 55 | static int altera_pio_get_value(struct udevice *dev, unsigned pin) |
03d67e12 | 56 | { |
8a8d24bd | 57 | struct altera_pio_plat *plat = dev_get_plat(dev); |
88d5ecf4 | 58 | struct altera_pio_regs *const regs = plat->regs; |
03d67e12 | 59 | |
97b26275 | 60 | return !!(readl(®s->data) & (1 << pin)); |
03d67e12 JF |
61 | } |
62 | ||
63 | ||
88d5ecf4 | 64 | static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val) |
03d67e12 | 65 | { |
8a8d24bd | 66 | struct altera_pio_plat *plat = dev_get_plat(dev); |
88d5ecf4 | 67 | struct altera_pio_regs *const regs = plat->regs; |
03d67e12 | 68 | |
88d5ecf4 TC |
69 | if (val) |
70 | setbits_le32(®s->data, 1 << pin); | |
71 | else | |
72 | clrbits_le32(®s->data, 1 << pin); | |
03d67e12 | 73 | |
03d67e12 JF |
74 | return 0; |
75 | } | |
76 | ||
88d5ecf4 | 77 | static int altera_pio_probe(struct udevice *dev) |
03d67e12 | 78 | { |
88d5ecf4 | 79 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
8a8d24bd | 80 | struct altera_pio_plat *plat = dev_get_plat(dev); |
03d67e12 | 81 | |
88d5ecf4 TC |
82 | uc_priv->gpio_count = plat->gpio_count; |
83 | uc_priv->bank_name = plat->bank_name; | |
03d67e12 | 84 | |
03d67e12 JF |
85 | return 0; |
86 | } | |
87 | ||
d1998a9f | 88 | static int altera_pio_of_to_plat(struct udevice *dev) |
03d67e12 | 89 | { |
8a8d24bd | 90 | struct altera_pio_plat *plat = dev_get_plat(dev); |
03d67e12 | 91 | |
2548493a | 92 | plat->regs = map_physmem(dev_read_addr(dev), |
079bfc5d TC |
93 | sizeof(struct altera_pio_regs), |
94 | MAP_NOCACHE); | |
e160f7d4 | 95 | plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
88d5ecf4 | 96 | "altr,gpio-bank-width", 32); |
e160f7d4 | 97 | plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
88d5ecf4 | 98 | "gpio-bank-name", NULL); |
03d67e12 | 99 | |
03d67e12 JF |
100 | return 0; |
101 | } | |
102 | ||
88d5ecf4 TC |
103 | static const struct dm_gpio_ops altera_pio_ops = { |
104 | .direction_input = altera_pio_direction_input, | |
105 | .direction_output = altera_pio_direction_output, | |
106 | .get_value = altera_pio_get_value, | |
107 | .set_value = altera_pio_set_value, | |
108 | }; | |
109 | ||
110 | static const struct udevice_id altera_pio_ids[] = { | |
111 | { .compatible = "altr,pio-1.0" }, | |
112 | { } | |
113 | }; | |
114 | ||
115 | U_BOOT_DRIVER(altera_pio) = { | |
116 | .name = "altera_pio", | |
117 | .id = UCLASS_GPIO, | |
118 | .of_match = altera_pio_ids, | |
119 | .ops = &altera_pio_ops, | |
d1998a9f | 120 | .of_to_plat = altera_pio_of_to_plat, |
8a8d24bd | 121 | .plat_auto = sizeof(struct altera_pio_plat), |
88d5ecf4 TC |
122 | .probe = altera_pio_probe, |
123 | }; |