blob: 528675651dbffc062808fef1e8dd44bde8d9dbd4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutbd390502017-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 Vasutbd390502017-07-21 23:15:21 +02007 */
8
9#include <common.h>
Marek Vasutbd390502017-07-21 23:15:21 +020010#include <asm/gpio.h>
Marek Vasutab61e172017-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 Vasutbd390502017-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 Vasutab61e172017-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 Vasutbd390502017-07-21 23:15:21 +020029
Marek Vasutab61e172017-11-26 20:32:44 +010030static u32 cpld_read(struct udevice *dev, u8 addr)
Marek Vasutbd390502017-07-21 23:15:21 +020031{
Marek Vasutab61e172017-11-26 20:32:44 +010032 struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev);
Marek Vasutbd390502017-07-21 23:15:21 +020033 u32 data = 0;
Marek Vasutab61e172017-11-26 20:32:44 +010034 int i;
Marek Vasutbd390502017-07-21 23:15:21 +020035
Marek Vasutab61e172017-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 Vasutbd390502017-07-21 23:15:21 +020042
Marek Vasutab61e172017-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 Vasutbd390502017-07-21 23:15:21 +020048
Marek Vasutab61e172017-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 Vasutbd390502017-07-21 23:15:21 +020055
Marek Vasutab61e172017-11-26 20:32:44 +010056 return data;
Marek Vasutbd390502017-07-21 23:15:21 +020057}
58
Marek Vasutab61e172017-11-26 20:32:44 +010059static void cpld_write(struct udevice *dev, u8 addr, u32 data)
Marek Vasutbd390502017-07-21 23:15:21 +020060{
Marek Vasutab61e172017-11-26 20:32:44 +010061 struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev);
62 int i;
Marek Vasutbd390502017-07-21 23:15:21 +020063
Marek Vasutab61e172017-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 Vasutbd390502017-07-21 23:15:21 +020070
Marek Vasutab61e172017-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 Vasutbd390502017-07-21 23:15:21 +020077
Marek Vasutab61e172017-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 Vasutbd390502017-07-21 23:15:21 +020083}
84
85static int do_cpld(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
86{
Marek Vasutab61e172017-11-26 20:32:44 +010087 struct udevice *dev;
Marek Vasutbd390502017-07-21 23:15:21 +020088 u32 addr, val;
Marek Vasutab61e172017-11-26 20:32:44 +010089 int ret;
Marek Vasutbd390502017-07-21 23:15:21 +020090
Marek Vasutab61e172017-11-26 20:32:44 +010091 ret = uclass_get_device_by_driver(UCLASS_SYSRESET,
92 DM_GET_DRIVER(sysreset_renesas_ulcb),
93 &dev);
94 if (ret)
95 return ret;
Marek Vasutbd390502017-07-21 23:15:21 +020096
97 if (argc == 2 && strcmp(argv[1], "info") == 0) {
98 printf("CPLD version:\t\t\t0x%08x\n",
Marek Vasutab61e172017-11-26 20:32:44 +010099 cpld_read(dev, CPLD_ADDR_VERSION));
Marek Vasutbd390502017-07-21 23:15:21 +0200100 printf("H3 Mode setting (MD0..28):\t0x%08x\n",
Marek Vasutab61e172017-11-26 20:32:44 +0100101 cpld_read(dev, CPLD_ADDR_MODE));
Marek Vasutbd390502017-07-21 23:15:21 +0200102 printf("Multiplexer settings:\t\t0x%08x\n",
Marek Vasutab61e172017-11-26 20:32:44 +0100103 cpld_read(dev, CPLD_ADDR_MUX));
Marek Vasutbd390502017-07-21 23:15:21 +0200104 printf("DIPSW (SW6):\t\t\t0x%08x\n",
Marek Vasutab61e172017-11-26 20:32:44 +0100105 cpld_read(dev, CPLD_ADDR_DIPSW6));
Marek Vasutbd390502017-07-21 23:15:21 +0200106 return 0;
107 }
108
109 if (argc < 3)
110 return CMD_RET_USAGE;
111
112 addr = simple_strtoul(argv[2], NULL, 16);
113 if (!(addr == CPLD_ADDR_VERSION || addr == CPLD_ADDR_MODE ||
114 addr == CPLD_ADDR_MUX || addr == CPLD_ADDR_DIPSW6 ||
115 addr == CPLD_ADDR_RESET)) {
116 printf("Invalid CPLD register address\n");
117 return CMD_RET_USAGE;
118 }
119
120 if (argc == 3 && strcmp(argv[1], "read") == 0) {
Marek Vasutab61e172017-11-26 20:32:44 +0100121 printf("0x%x\n", cpld_read(dev, addr));
Marek Vasutbd390502017-07-21 23:15:21 +0200122 } else if (argc == 4 && strcmp(argv[1], "write") == 0) {
123 val = simple_strtoul(argv[3], NULL, 16);
Marek Vasutab61e172017-11-26 20:32:44 +0100124 cpld_write(dev, addr, val);
Marek Vasutbd390502017-07-21 23:15:21 +0200125 }
126
127 return 0;
128}
129
130U_BOOT_CMD(
131 cpld, 4, 1, do_cpld,
132 "CPLD access",
133 "info\n"
134 "cpld read addr\n"
135 "cpld write addr val\n"
136);
137
Marek Vasutab61e172017-11-26 20:32:44 +0100138static int renesas_ulcb_sysreset_request(struct udevice *dev, enum sysreset_t type)
Marek Vasutbd390502017-07-21 23:15:21 +0200139{
Marek Vasutab61e172017-11-26 20:32:44 +0100140 cpld_write(dev, CPLD_ADDR_RESET, 1);
141
142 return -EINPROGRESS;
Marek Vasutbd390502017-07-21 23:15:21 +0200143}
Marek Vasutab61e172017-11-26 20:32:44 +0100144
145static int renesas_ulcb_sysreset_probe(struct udevice *dev)
146{
147 struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev);
148
149 if (gpio_request_by_name(dev, "gpio-miso", 0, &priv->miso,
150 GPIOD_IS_IN))
151 return -EINVAL;
152
153 if (gpio_request_by_name(dev, "gpio-sck", 0, &priv->sck,
154 GPIOD_IS_OUT))
155 return -EINVAL;
156
157 if (gpio_request_by_name(dev, "gpio-sstbz", 0, &priv->sstbz,
158 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE))
159 return -EINVAL;
160
161 if (gpio_request_by_name(dev, "gpio-mosi", 0, &priv->mosi,
162 GPIOD_IS_OUT))
163 return -EINVAL;
164
165 /* PULL-UP on MISO line */
166 setbits_le32(PFC_PUEN5, PUEN_SSI_SDATA4);
167
168 /* Dummy read */
169 cpld_read(dev, CPLD_ADDR_VERSION);
170
171 return 0;
172}
173
174static struct sysreset_ops renesas_ulcb_sysreset = {
175 .request = renesas_ulcb_sysreset_request,
176};
177
178static const struct udevice_id renesas_ulcb_sysreset_ids[] = {
179 { .compatible = "renesas,ulcb-cpld" },
180 { }
181};
182
183U_BOOT_DRIVER(sysreset_renesas_ulcb) = {
184 .name = "renesas_ulcb_sysreset",
185 .id = UCLASS_SYSRESET,
186 .ops = &renesas_ulcb_sysreset,
187 .probe = renesas_ulcb_sysreset_probe,
188 .of_match = renesas_ulcb_sysreset_ids,
189 .priv_auto_alloc_size = sizeof(struct renesas_ulcb_sysreset_priv),
190};