Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 2 | /* |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 3 | * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw> |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 4 | * Copyright (C) 2011 Missing Link Electronics |
| 5 | * Joachim Foerster <joachim@missinglinkelectronics.com> |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <malloc.h> |
| 11 | #include <fdtdec.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 13 | #include <asm/io.h> |
| 14 | #include <asm/gpio.h> |
| 15 | |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 17 | |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 18 | struct altera_pio_regs { |
| 19 | u32 data; /* Data register */ |
| 20 | u32 direction; /* Direction register */ |
| 21 | }; |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 22 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 23 | struct altera_pio_plat { |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 24 | struct altera_pio_regs *regs; |
| 25 | int gpio_count; |
| 26 | const char *bank_name; |
| 27 | }; |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 28 | |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 29 | static int altera_pio_direction_input(struct udevice *dev, unsigned pin) |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 30 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 31 | struct altera_pio_plat *plat = dev_get_plat(dev); |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 32 | struct altera_pio_regs *const regs = plat->regs; |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 33 | |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 34 | clrbits_le32(®s->direction, 1 << pin); |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 35 | |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 36 | return 0; |
| 37 | } |
| 38 | |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 39 | static int altera_pio_direction_output(struct udevice *dev, unsigned pin, |
| 40 | int val) |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 41 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 42 | struct altera_pio_plat *plat = dev_get_plat(dev); |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 43 | struct altera_pio_regs *const regs = plat->regs; |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 44 | |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 45 | if (val) |
| 46 | setbits_le32(®s->data, 1 << pin); |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 47 | else |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 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); |
| 51 | |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 55 | static int altera_pio_get_value(struct udevice *dev, unsigned pin) |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 56 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 57 | struct altera_pio_plat *plat = dev_get_plat(dev); |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 58 | struct altera_pio_regs *const regs = plat->regs; |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 59 | |
Julien BĂ©raud | 97b2627 | 2019-01-07 09:17:46 +0000 | [diff] [blame] | 60 | return !!(readl(®s->data) & (1 << pin)); |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 63 | |
| 64 | static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val) |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 65 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 66 | struct altera_pio_plat *plat = dev_get_plat(dev); |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 67 | struct altera_pio_regs *const regs = plat->regs; |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 68 | |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 69 | if (val) |
| 70 | setbits_le32(®s->data, 1 << pin); |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 71 | else |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 72 | clrbits_le32(®s->data, 1 << pin); |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 73 | |
Joachim Foerster | 03d67e1 | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 74 | return 0; |
| 75 | } |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 76 | |
| 77 | static int altera_pio_probe(struct udevice *dev) |
| 78 | { |
| 79 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 80 | struct altera_pio_plat *plat = dev_get_plat(dev); |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 81 | |
| 82 | uc_priv->gpio_count = plat->gpio_count; |
| 83 | uc_priv->bank_name = plat->bank_name; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 88 | static int altera_pio_of_to_plat(struct udevice *dev) |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 89 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 90 | struct altera_pio_plat *plat = dev_get_plat(dev); |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 91 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 92 | plat->regs = map_physmem(dev_read_addr(dev), |
Thomas Chou | 079bfc5 | 2015-11-14 11:26:51 +0800 | [diff] [blame] | 93 | sizeof(struct altera_pio_regs), |
| 94 | MAP_NOCACHE); |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 95 | plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 96 | "altr,gpio-bank-width", 32); |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 97 | plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 98 | "gpio-bank-name", NULL); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 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, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 120 | .of_to_plat = altera_pio_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 121 | .plat_auto = sizeof(struct altera_pio_plat), |
Thomas Chou | 88d5ecf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 122 | .probe = altera_pio_probe, |
| 123 | }; |