blob: e72e45e3fb4eed057e3d1bc01d4354e458281cce [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutad43cd32017-07-21 23:15:21 +02002/*
3 * ULCB board CPLD access support
4 *
5 * Copyright (C) 2017 Renesas Electronics Corporation
6 * Copyright (C) 2017 Cogent Embedded, Inc.
Marek Vasutad43cd32017-07-21 23:15:21 +02007 */
8
Simon Glassed38aef2020-05-10 11:40:03 -06009#include <command.h>
Marek Vasutad43cd32017-07-21 23:15:21 +020010#include <asm/gpio.h>
Marek Vasut901897b2017-11-26 20:32:44 +010011#include <asm/io.h>
12#include <dm.h>
13#include <errno.h>
14#include <linux/err.h>
15#include <sysreset.h>
Marek Vasutad43cd32017-07-21 23:15:21 +020016
17#define CPLD_ADDR_MODE 0x00 /* RW */
18#define CPLD_ADDR_MUX 0x02 /* RW */
19#define CPLD_ADDR_DIPSW6 0x08 /* R */
20#define CPLD_ADDR_RESET 0x80 /* RW */
21#define CPLD_ADDR_VERSION 0xFF /* R */
22
Marek Vasut901897b2017-11-26 20:32:44 +010023struct renesas_ulcb_sysreset_priv {
24 struct gpio_desc miso;
25 struct gpio_desc mosi;
26 struct gpio_desc sck;
27 struct gpio_desc sstbz;
28};
Marek Vasutad43cd32017-07-21 23:15:21 +020029
Marek Vasut901897b2017-11-26 20:32:44 +010030static u32 cpld_read(struct udevice *dev, u8 addr)
Marek Vasutad43cd32017-07-21 23:15:21 +020031{
Marek Vasut901897b2017-11-26 20:32:44 +010032 struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev);
Marek Vasutad43cd32017-07-21 23:15:21 +020033 u32 data = 0;
Marek Vasut901897b2017-11-26 20:32:44 +010034 int i;
Marek Vasutad43cd32017-07-21 23:15:21 +020035
Marek Vasut901897b2017-11-26 20:32:44 +010036 for (i = 0; i < 8; i++) {
37 dm_gpio_set_value(&priv->mosi, !!(addr & 0x80)); /* MSB first */
38 dm_gpio_set_value(&priv->sck, 1);
39 addr <<= 1;
40 dm_gpio_set_value(&priv->sck, 0);
41 }
Marek Vasutad43cd32017-07-21 23:15:21 +020042
Marek Vasut901897b2017-11-26 20:32:44 +010043 dm_gpio_set_value(&priv->mosi, 0); /* READ */
44 dm_gpio_set_value(&priv->sstbz, 0);
45 dm_gpio_set_value(&priv->sck, 1);
46 dm_gpio_set_value(&priv->sck, 0);
47 dm_gpio_set_value(&priv->sstbz, 1);
Marek Vasutad43cd32017-07-21 23:15:21 +020048
Marek Vasut901897b2017-11-26 20:32:44 +010049 for (i = 0; i < 32; i++) {
50 dm_gpio_set_value(&priv->sck, 1);
51 data <<= 1;
52 data |= dm_gpio_get_value(&priv->miso); /* MSB first */
53 dm_gpio_set_value(&priv->sck, 0);
54 }
Marek Vasutad43cd32017-07-21 23:15:21 +020055
Marek Vasut901897b2017-11-26 20:32:44 +010056 return data;
Marek Vasutad43cd32017-07-21 23:15:21 +020057}
58
Marek Vasut901897b2017-11-26 20:32:44 +010059static void cpld_write(struct udevice *dev, u8 addr, u32 data)
Marek Vasutad43cd32017-07-21 23:15:21 +020060{
Marek Vasut901897b2017-11-26 20:32:44 +010061 struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev);
62 int i;
Marek Vasutad43cd32017-07-21 23:15:21 +020063
Marek Vasut901897b2017-11-26 20:32:44 +010064 for (i = 0; i < 32; i++) {
65 dm_gpio_set_value(&priv->mosi, data & (1 << 31)); /* MSB first */
66 dm_gpio_set_value(&priv->sck, 1);
67 data <<= 1;
68 dm_gpio_set_value(&priv->sck, 0);
69 }
Marek Vasutad43cd32017-07-21 23:15:21 +020070
Marek Vasut901897b2017-11-26 20:32:44 +010071 for (i = 0; i < 8; i++) {
72 dm_gpio_set_value(&priv->mosi, addr & 0x80); /* MSB first */
73 dm_gpio_set_value(&priv->sck, 1);
74 addr <<= 1;
75 dm_gpio_set_value(&priv->sck, 0);
76 }
Marek Vasutad43cd32017-07-21 23:15:21 +020077
Marek Vasut901897b2017-11-26 20:32:44 +010078 dm_gpio_set_value(&priv->mosi, 1); /* WRITE */
79 dm_gpio_set_value(&priv->sstbz, 0);
80 dm_gpio_set_value(&priv->sck, 1);
81 dm_gpio_set_value(&priv->sck, 0);
82 dm_gpio_set_value(&priv->sstbz, 1);
Marek Vasutad43cd32017-07-21 23:15:21 +020083}
84
Simon Glassed38aef2020-05-10 11:40:03 -060085static int do_cpld(struct cmd_tbl *cmdtp, int flag, int argc,
86 char *const argv[])
Marek Vasutad43cd32017-07-21 23:15:21 +020087{
Marek Vasut901897b2017-11-26 20:32:44 +010088 struct udevice *dev;
Marek Vasutad43cd32017-07-21 23:15:21 +020089 u32 addr, val;
Marek Vasut901897b2017-11-26 20:32:44 +010090 int ret;
Marek Vasutad43cd32017-07-21 23:15:21 +020091
Marek Vasut901897b2017-11-26 20:32:44 +010092 ret = uclass_get_device_by_driver(UCLASS_SYSRESET,
Simon Glass65130cd2020-12-28 20:34:56 -070093 DM_DRIVER_GET(sysreset_renesas_ulcb),
Marek Vasut901897b2017-11-26 20:32:44 +010094 &dev);
95 if (ret)
96 return ret;
Marek Vasutad43cd32017-07-21 23:15:21 +020097
98 if (argc == 2 && strcmp(argv[1], "info") == 0) {
99 printf("CPLD version:\t\t\t0x%08x\n",
Marek Vasut901897b2017-11-26 20:32:44 +0100100 cpld_read(dev, CPLD_ADDR_VERSION));
Marek Vasutad43cd32017-07-21 23:15:21 +0200101 printf("H3 Mode setting (MD0..28):\t0x%08x\n",
Marek Vasut901897b2017-11-26 20:32:44 +0100102 cpld_read(dev, CPLD_ADDR_MODE));
Marek Vasutad43cd32017-07-21 23:15:21 +0200103 printf("Multiplexer settings:\t\t0x%08x\n",
Marek Vasut901897b2017-11-26 20:32:44 +0100104 cpld_read(dev, CPLD_ADDR_MUX));
Marek Vasutad43cd32017-07-21 23:15:21 +0200105 printf("DIPSW (SW6):\t\t\t0x%08x\n",
Marek Vasut901897b2017-11-26 20:32:44 +0100106 cpld_read(dev, CPLD_ADDR_DIPSW6));
Marek Vasutad43cd32017-07-21 23:15:21 +0200107 return 0;
108 }
109
110 if (argc < 3)
111 return CMD_RET_USAGE;
112
Simon Glass3ff49ec2021-07-24 09:03:29 -0600113 addr = hextoul(argv[2], NULL);
Marek Vasutad43cd32017-07-21 23:15:21 +0200114 if (!(addr == CPLD_ADDR_VERSION || addr == CPLD_ADDR_MODE ||
115 addr == CPLD_ADDR_MUX || addr == CPLD_ADDR_DIPSW6 ||
116 addr == CPLD_ADDR_RESET)) {
117 printf("Invalid CPLD register address\n");
118 return CMD_RET_USAGE;
119 }
120
121 if (argc == 3 && strcmp(argv[1], "read") == 0) {
Marek Vasut901897b2017-11-26 20:32:44 +0100122 printf("0x%x\n", cpld_read(dev, addr));
Marek Vasutad43cd32017-07-21 23:15:21 +0200123 } else if (argc == 4 && strcmp(argv[1], "write") == 0) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600124 val = hextoul(argv[3], NULL);
Marek Vasut901897b2017-11-26 20:32:44 +0100125 cpld_write(dev, addr, val);
Marek Vasutad43cd32017-07-21 23:15:21 +0200126 }
127
128 return 0;
129}
130
131U_BOOT_CMD(
132 cpld, 4, 1, do_cpld,
133 "CPLD access",
134 "info\n"
135 "cpld read addr\n"
136 "cpld write addr val\n"
137);
138
Marek Vasut901897b2017-11-26 20:32:44 +0100139static int renesas_ulcb_sysreset_request(struct udevice *dev, enum sysreset_t type)
140{
141 cpld_write(dev, CPLD_ADDR_RESET, 1);
142
143 return -EINPROGRESS;
144}
145
146static int renesas_ulcb_sysreset_probe(struct udevice *dev)
Marek Vasutad43cd32017-07-21 23:15:21 +0200147{
Marek Vasut901897b2017-11-26 20:32:44 +0100148 struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev);
149
150 if (gpio_request_by_name(dev, "gpio-miso", 0, &priv->miso,
151 GPIOD_IS_IN))
152 return -EINVAL;
153
154 if (gpio_request_by_name(dev, "gpio-sck", 0, &priv->sck,
155 GPIOD_IS_OUT))
156 return -EINVAL;
157
158 if (gpio_request_by_name(dev, "gpio-sstbz", 0, &priv->sstbz,
159 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE))
160 return -EINVAL;
161
162 if (gpio_request_by_name(dev, "gpio-mosi", 0, &priv->mosi,
163 GPIOD_IS_OUT))
164 return -EINVAL;
165
166 /* PULL-UP on MISO line */
167 setbits_le32(PFC_PUEN5, PUEN_SSI_SDATA4);
168
169 /* Dummy read */
170 cpld_read(dev, CPLD_ADDR_VERSION);
171
172 return 0;
Marek Vasutad43cd32017-07-21 23:15:21 +0200173}
Marek Vasut901897b2017-11-26 20:32:44 +0100174
175static struct sysreset_ops renesas_ulcb_sysreset = {
176 .request = renesas_ulcb_sysreset_request,
177};
178
179static const struct udevice_id renesas_ulcb_sysreset_ids[] = {
180 { .compatible = "renesas,ulcb-cpld" },
181 { }
182};
183
184U_BOOT_DRIVER(sysreset_renesas_ulcb) = {
185 .name = "renesas_ulcb_sysreset",
186 .id = UCLASS_SYSRESET,
187 .ops = &renesas_ulcb_sysreset,
188 .probe = renesas_ulcb_sysreset_probe,
189 .of_match = renesas_ulcb_sysreset_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700190 .priv_auto = sizeof(struct renesas_ulcb_sysreset_priv),
Marek Vasut901897b2017-11-26 20:32:44 +0100191};