Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 2 | /* |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 3 | * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw> |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 4 | * Copyright (C) 2011 Missing Link Electronics |
| 5 | * Joachim Foerster <joachim@missinglinkelectronics.com> |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
Thomas Chou | fb1a4bf | 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> |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <asm/gpio.h> |
| 14 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 16 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 17 | struct altera_pio_regs { |
| 18 | u32 data; /* Data register */ |
| 19 | u32 direction; /* Direction register */ |
| 20 | }; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 21 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 22 | struct altera_pio_platdata { |
| 23 | struct altera_pio_regs *regs; |
| 24 | int gpio_count; |
| 25 | const char *bank_name; |
| 26 | }; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 27 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 28 | static int altera_pio_direction_input(struct udevice *dev, unsigned pin) |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 29 | { |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 30 | struct altera_pio_platdata *plat = dev_get_platdata(dev); |
| 31 | struct altera_pio_regs *const regs = plat->regs; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 32 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 33 | clrbits_le32(®s->direction, 1 << pin); |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 34 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 35 | return 0; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 38 | static int altera_pio_direction_output(struct udevice *dev, unsigned pin, |
| 39 | int val) |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 40 | { |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 41 | struct altera_pio_platdata *plat = dev_get_platdata(dev); |
| 42 | struct altera_pio_regs *const regs = plat->regs; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 43 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 44 | if (val) |
| 45 | setbits_le32(®s->data, 1 << pin); |
| 46 | else |
| 47 | clrbits_le32(®s->data, 1 << pin); |
| 48 | /* change the data first, then the direction. to avoid glitch */ |
| 49 | setbits_le32(®s->direction, 1 << pin); |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 50 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 51 | return 0; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 52 | } |
| 53 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 54 | static int altera_pio_get_value(struct udevice *dev, unsigned pin) |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 55 | { |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 56 | struct altera_pio_platdata *plat = dev_get_platdata(dev); |
| 57 | struct altera_pio_regs *const regs = plat->regs; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 58 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 59 | return readl(®s->data) & (1 << pin); |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 63 | static int altera_pio_set_value(struct udevice *dev, unsigned pin, int val) |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 64 | { |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 65 | struct altera_pio_platdata *plat = dev_get_platdata(dev); |
| 66 | struct altera_pio_regs *const regs = plat->regs; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 67 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 68 | if (val) |
| 69 | setbits_le32(®s->data, 1 << pin); |
| 70 | else |
| 71 | clrbits_le32(®s->data, 1 << pin); |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 72 | |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 76 | static int altera_pio_probe(struct udevice *dev) |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 77 | { |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 78 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 79 | struct altera_pio_platdata *plat = dev_get_platdata(dev); |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 80 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 81 | uc_priv->gpio_count = plat->gpio_count; |
| 82 | uc_priv->bank_name = plat->bank_name; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 83 | |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 87 | static int altera_pio_ofdata_to_platdata(struct udevice *dev) |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 88 | { |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 89 | struct altera_pio_platdata *plat = dev_get_platdata(dev); |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 90 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 91 | plat->regs = map_physmem(devfdt_get_addr(dev), |
Thomas Chou | ee650e2 | 2015-11-14 11:26:51 +0800 | [diff] [blame] | 92 | sizeof(struct altera_pio_regs), |
| 93 | MAP_NOCACHE); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 94 | plat->gpio_count = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 95 | "altr,gpio-bank-width", 32); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 96 | plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 97 | "gpio-bank-name", NULL); |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 98 | |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 102 | static const struct dm_gpio_ops altera_pio_ops = { |
| 103 | .direction_input = altera_pio_direction_input, |
| 104 | .direction_output = altera_pio_direction_output, |
| 105 | .get_value = altera_pio_get_value, |
| 106 | .set_value = altera_pio_set_value, |
| 107 | }; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 108 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 109 | static const struct udevice_id altera_pio_ids[] = { |
| 110 | { .compatible = "altr,pio-1.0" }, |
| 111 | { } |
| 112 | }; |
Joachim Foerster | c932eed | 2011-10-21 15:48:50 +0200 | [diff] [blame] | 113 | |
Thomas Chou | fb1a4bf | 2015-10-21 21:33:45 +0800 | [diff] [blame] | 114 | U_BOOT_DRIVER(altera_pio) = { |
| 115 | .name = "altera_pio", |
| 116 | .id = UCLASS_GPIO, |
| 117 | .of_match = altera_pio_ids, |
| 118 | .ops = &altera_pio_ops, |
| 119 | .ofdata_to_platdata = altera_pio_ofdata_to_platdata, |
| 120 | .platdata_auto_alloc_size = sizeof(struct altera_pio_platdata), |
| 121 | .probe = altera_pio_probe, |
| 122 | }; |