Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 2 | /* |
| 3 | * ULCB board CPLD access support |
| 4 | * |
| 5 | * Copyright (C) 2017 Renesas Electronics Corporation |
| 6 | * Copyright (C) 2017 Cogent Embedded, Inc. |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <command.h> |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 11 | #include <asm/gpio.h> |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <dm.h> |
| 14 | #include <errno.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <sysreset.h> |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 17 | |
| 18 | #define CPLD_ADDR_MODE 0x00 /* RW */ |
| 19 | #define CPLD_ADDR_MUX 0x02 /* RW */ |
| 20 | #define CPLD_ADDR_DIPSW6 0x08 /* R */ |
| 21 | #define CPLD_ADDR_RESET 0x80 /* RW */ |
| 22 | #define CPLD_ADDR_VERSION 0xFF /* R */ |
| 23 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 24 | struct renesas_ulcb_sysreset_priv { |
| 25 | struct gpio_desc miso; |
| 26 | struct gpio_desc mosi; |
| 27 | struct gpio_desc sck; |
| 28 | struct gpio_desc sstbz; |
| 29 | }; |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 30 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 31 | static u32 cpld_read(struct udevice *dev, u8 addr) |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 32 | { |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 33 | struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 34 | u32 data = 0; |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 35 | int i; |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 36 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 37 | for (i = 0; i < 8; i++) { |
| 38 | dm_gpio_set_value(&priv->mosi, !!(addr & 0x80)); /* MSB first */ |
| 39 | dm_gpio_set_value(&priv->sck, 1); |
| 40 | addr <<= 1; |
| 41 | dm_gpio_set_value(&priv->sck, 0); |
| 42 | } |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 43 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 44 | dm_gpio_set_value(&priv->mosi, 0); /* READ */ |
| 45 | dm_gpio_set_value(&priv->sstbz, 0); |
| 46 | dm_gpio_set_value(&priv->sck, 1); |
| 47 | dm_gpio_set_value(&priv->sck, 0); |
| 48 | dm_gpio_set_value(&priv->sstbz, 1); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 49 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 50 | for (i = 0; i < 32; i++) { |
| 51 | dm_gpio_set_value(&priv->sck, 1); |
| 52 | data <<= 1; |
| 53 | data |= dm_gpio_get_value(&priv->miso); /* MSB first */ |
| 54 | dm_gpio_set_value(&priv->sck, 0); |
| 55 | } |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 56 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 57 | return data; |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 60 | static void cpld_write(struct udevice *dev, u8 addr, u32 data) |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 61 | { |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 62 | struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev); |
| 63 | int i; |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 64 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 65 | for (i = 0; i < 32; i++) { |
| 66 | dm_gpio_set_value(&priv->mosi, data & (1 << 31)); /* MSB first */ |
| 67 | dm_gpio_set_value(&priv->sck, 1); |
| 68 | data <<= 1; |
| 69 | dm_gpio_set_value(&priv->sck, 0); |
| 70 | } |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 71 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 72 | for (i = 0; i < 8; i++) { |
| 73 | dm_gpio_set_value(&priv->mosi, addr & 0x80); /* MSB first */ |
| 74 | dm_gpio_set_value(&priv->sck, 1); |
| 75 | addr <<= 1; |
| 76 | dm_gpio_set_value(&priv->sck, 0); |
| 77 | } |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 78 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 79 | dm_gpio_set_value(&priv->mosi, 1); /* WRITE */ |
| 80 | dm_gpio_set_value(&priv->sstbz, 0); |
| 81 | dm_gpio_set_value(&priv->sck, 1); |
| 82 | dm_gpio_set_value(&priv->sck, 0); |
| 83 | dm_gpio_set_value(&priv->sstbz, 1); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 86 | static int do_cpld(struct cmd_tbl *cmdtp, int flag, int argc, |
| 87 | char *const argv[]) |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 88 | { |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 89 | struct udevice *dev; |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 90 | u32 addr, val; |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 91 | int ret; |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 92 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 93 | ret = uclass_get_device_by_driver(UCLASS_SYSRESET, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 94 | DM_DRIVER_GET(sysreset_renesas_ulcb), |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 95 | &dev); |
| 96 | if (ret) |
| 97 | return ret; |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 98 | |
| 99 | if (argc == 2 && strcmp(argv[1], "info") == 0) { |
| 100 | printf("CPLD version:\t\t\t0x%08x\n", |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 101 | cpld_read(dev, CPLD_ADDR_VERSION)); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 102 | printf("H3 Mode setting (MD0..28):\t0x%08x\n", |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 103 | cpld_read(dev, CPLD_ADDR_MODE)); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 104 | printf("Multiplexer settings:\t\t0x%08x\n", |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 105 | cpld_read(dev, CPLD_ADDR_MUX)); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 106 | printf("DIPSW (SW6):\t\t\t0x%08x\n", |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 107 | cpld_read(dev, CPLD_ADDR_DIPSW6)); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | if (argc < 3) |
| 112 | return CMD_RET_USAGE; |
| 113 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 114 | addr = hextoul(argv[2], NULL); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 115 | if (!(addr == CPLD_ADDR_VERSION || addr == CPLD_ADDR_MODE || |
| 116 | addr == CPLD_ADDR_MUX || addr == CPLD_ADDR_DIPSW6 || |
| 117 | addr == CPLD_ADDR_RESET)) { |
| 118 | printf("Invalid CPLD register address\n"); |
| 119 | return CMD_RET_USAGE; |
| 120 | } |
| 121 | |
| 122 | if (argc == 3 && strcmp(argv[1], "read") == 0) { |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 123 | printf("0x%x\n", cpld_read(dev, addr)); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 124 | } else if (argc == 4 && strcmp(argv[1], "write") == 0) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 125 | val = hextoul(argv[3], NULL); |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 126 | cpld_write(dev, addr, val); |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | U_BOOT_CMD( |
| 133 | cpld, 4, 1, do_cpld, |
| 134 | "CPLD access", |
| 135 | "info\n" |
| 136 | "cpld read addr\n" |
| 137 | "cpld write addr val\n" |
| 138 | ); |
| 139 | |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 140 | static int renesas_ulcb_sysreset_request(struct udevice *dev, enum sysreset_t type) |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 141 | { |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 142 | cpld_write(dev, CPLD_ADDR_RESET, 1); |
| 143 | |
| 144 | return -EINPROGRESS; |
Marek Vasut | bd39050 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 145 | } |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 146 | |
| 147 | static int renesas_ulcb_sysreset_probe(struct udevice *dev) |
| 148 | { |
| 149 | struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev); |
| 150 | |
| 151 | if (gpio_request_by_name(dev, "gpio-miso", 0, &priv->miso, |
| 152 | GPIOD_IS_IN)) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | if (gpio_request_by_name(dev, "gpio-sck", 0, &priv->sck, |
| 156 | GPIOD_IS_OUT)) |
| 157 | return -EINVAL; |
| 158 | |
| 159 | if (gpio_request_by_name(dev, "gpio-sstbz", 0, &priv->sstbz, |
| 160 | GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE)) |
| 161 | return -EINVAL; |
| 162 | |
| 163 | if (gpio_request_by_name(dev, "gpio-mosi", 0, &priv->mosi, |
| 164 | GPIOD_IS_OUT)) |
| 165 | return -EINVAL; |
| 166 | |
| 167 | /* PULL-UP on MISO line */ |
| 168 | setbits_le32(PFC_PUEN5, PUEN_SSI_SDATA4); |
| 169 | |
| 170 | /* Dummy read */ |
| 171 | cpld_read(dev, CPLD_ADDR_VERSION); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static struct sysreset_ops renesas_ulcb_sysreset = { |
| 177 | .request = renesas_ulcb_sysreset_request, |
| 178 | }; |
| 179 | |
| 180 | static const struct udevice_id renesas_ulcb_sysreset_ids[] = { |
| 181 | { .compatible = "renesas,ulcb-cpld" }, |
| 182 | { } |
| 183 | }; |
| 184 | |
| 185 | U_BOOT_DRIVER(sysreset_renesas_ulcb) = { |
| 186 | .name = "renesas_ulcb_sysreset", |
| 187 | .id = UCLASS_SYSRESET, |
| 188 | .ops = &renesas_ulcb_sysreset, |
| 189 | .probe = renesas_ulcb_sysreset_probe, |
| 190 | .of_match = renesas_ulcb_sysreset_ids, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 191 | .priv_auto = sizeof(struct renesas_ulcb_sysreset_priv), |
Marek Vasut | ab61e17 | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 192 | }; |