blob: 8aaffaf37b3dd1078153aab3defc607c74c60244 [file] [log] [blame]
T Karthik Reddy01719722021-10-29 13:11:43 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * ZynqMP GPIO modepin driver
4 *
5 * Copyright (C) 2021 Xilinx, Inc.
6 */
7
T Karthik Reddy01719722021-10-29 13:11:43 +02008#include <errno.h>
9#include <asm/io.h>
10#include <asm/gpio.h>
11#include <dm.h>
12#include <asm/arch/hardware.h>
13#include <zynqmp_firmware.h>
14
15#define OUTEN(pin) (BIT(0) << (pin))
16#define INVAL(pin) (BIT(4) << (pin))
17#define OUTVAL(pin) (BIT(8) << (pin))
18
19#define ZYNQMP_CRL_APB_BOOTPIN_CTRL_MASK 0xF0F
20#define ZYNQMP_CRL_APB_BOOT_PIN_CTRL (ZYNQMP_CRL_APB_BASEADDR + \
21 (0x250U))
22
23static int get_gpio_modepin(u32 *ret_payload)
24{
25 return xilinx_pm_request(PM_MMIO_READ, ZYNQMP_CRL_APB_BOOT_PIN_CTRL,
26 0, 0, 0, ret_payload);
27}
28
29static int set_gpio_modepin(int val)
30{
31 return xilinx_pm_request(PM_MMIO_WRITE, ZYNQMP_CRL_APB_BOOT_PIN_CTRL,
32 ZYNQMP_CRL_APB_BOOTPIN_CTRL_MASK,
33 val, 0, NULL);
34}
35
36static int modepin_gpio_direction_input(struct udevice *dev,
37 unsigned int offset)
38{
39 return 0;
40}
41
42static int modepin_gpio_set_value(struct udevice *dev, unsigned int offset,
43 int value)
44{
45 u32 ret_payload[PAYLOAD_ARG_CNT];
46 u32 out_val = 0;
47 int ret;
48
49 ret = get_gpio_modepin(ret_payload);
Algapally Santosh Sagarc75f1c42023-01-19 22:36:15 -070050 if (ret)
51 return ret;
52
T Karthik Reddy01719722021-10-29 13:11:43 +020053 if (value)
54 out_val = OUTVAL(offset) | ret_payload[1];
55 else
56 out_val = ~OUTVAL(offset) & ret_payload[1];
57
58 return set_gpio_modepin(out_val);
59}
60
61static int modepin_gpio_direction_output(struct udevice *dev,
62 unsigned int offset, int value)
63{
64 u32 ret_payload[PAYLOAD_ARG_CNT];
65 u32 out_en = 0;
66 int ret;
67
68 ret = get_gpio_modepin(ret_payload);
69 if (ret)
70 return ret;
71
72 if (value)
73 out_en = OUTEN(offset) | ret_payload[1];
74 else
75 out_en = ~OUTEN(offset) & ret_payload[1];
76
77 ret = set_gpio_modepin(out_en);
78 if (ret)
79 return ret;
80
81 return modepin_gpio_set_value(dev, offset, value);
82}
83
84static int modepin_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
85 struct ofnode_phandle_args *args)
86{
87 desc->offset = args->args[0];
88
89 return 0;
90}
91
92static int modepin_gpio_get_value(struct udevice *dev, unsigned int offset)
93{
94 u32 ret_payload[PAYLOAD_ARG_CNT];
95 int ret;
96
97 ret = get_gpio_modepin(ret_payload);
98 if (ret)
99 return ret;
100
101 return (INVAL(offset) & ret_payload[1]) ? 1 : 0;
102}
103
104static int modepin_gpio_get_function(struct udevice *dev, unsigned int offset)
105{
106 u32 ret_payload[PAYLOAD_ARG_CNT];
107 int ret;
108
109 ret = get_gpio_modepin(ret_payload);
110 if (ret)
111 return ret;
112
113 return (OUTEN(offset) & ret_payload[1]) ? GPIOF_OUTPUT : GPIOF_INPUT;
114}
115
116static const struct dm_gpio_ops modepin_gpio_ops = {
117 .direction_input = modepin_gpio_direction_input,
118 .direction_output = modepin_gpio_direction_output,
119 .get_value = modepin_gpio_get_value,
120 .set_value = modepin_gpio_set_value,
121 .get_function = modepin_gpio_get_function,
122 .xlate = modepin_gpio_xlate,
123};
124
125static int modepin_gpio_probe(struct udevice *dev)
126{
127 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
128 const void *label_ptr;
129
130 label_ptr = dev_read_prop(dev, "label", NULL);
131 if (label_ptr) {
132 uc_priv->bank_name = strdup(label_ptr);
133 if (!uc_priv->bank_name)
134 return -ENOMEM;
135 } else {
136 uc_priv->bank_name = dev->name;
137 }
138
139 uc_priv->gpio_count = 4;
140
141 return 0;
142}
143
144static const struct udevice_id modepin_gpio_ids[] = {
145 { .compatible = "xlnx,zynqmp-gpio-modepin",},
146 { }
147};
148
149U_BOOT_DRIVER(modepin_gpio) = {
150 .name = "modepin_gpio",
151 .id = UCLASS_GPIO,
152 .ops = &modepin_gpio_ops,
153 .of_match = modepin_gpio_ids,
154 .probe = modepin_gpio_probe,
155};