Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | ad43cd3 | 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 | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 10 | #include <asm/gpio.h> |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <sysreset.h> |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 16 | |
| 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 Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 23 | struct 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 Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 29 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 30 | static u32 cpld_read(struct udevice *dev, u8 addr) |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 31 | { |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 32 | struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev); |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 33 | u32 data = 0; |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 34 | int i; |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 35 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 36 | 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 Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 42 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 43 | 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 Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 48 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 49 | 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 Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 55 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 56 | return data; |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 59 | static void cpld_write(struct udevice *dev, u8 addr, u32 data) |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 60 | { |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 61 | struct renesas_ulcb_sysreset_priv *priv = dev_get_priv(dev); |
| 62 | int i; |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 63 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 64 | 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 Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 70 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 71 | 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 Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 77 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 78 | 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 Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static int do_cpld(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 86 | { |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 87 | struct udevice *dev; |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 88 | u32 addr, val; |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 89 | int ret; |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 90 | |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 91 | ret = uclass_get_device_by_driver(UCLASS_SYSRESET, |
| 92 | DM_GET_DRIVER(sysreset_renesas_ulcb), |
| 93 | &dev); |
| 94 | if (ret) |
| 95 | return ret; |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 96 | |
| 97 | if (argc == 2 && strcmp(argv[1], "info") == 0) { |
| 98 | printf("CPLD version:\t\t\t0x%08x\n", |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 99 | cpld_read(dev, CPLD_ADDR_VERSION)); |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 100 | printf("H3 Mode setting (MD0..28):\t0x%08x\n", |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 101 | cpld_read(dev, CPLD_ADDR_MODE)); |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 102 | printf("Multiplexer settings:\t\t0x%08x\n", |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 103 | cpld_read(dev, CPLD_ADDR_MUX)); |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 104 | printf("DIPSW (SW6):\t\t\t0x%08x\n", |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 105 | cpld_read(dev, CPLD_ADDR_DIPSW6)); |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 106 | 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 Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 121 | printf("0x%x\n", cpld_read(dev, addr)); |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 122 | } else if (argc == 4 && strcmp(argv[1], "write") == 0) { |
| 123 | val = simple_strtoul(argv[3], NULL, 16); |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 124 | cpld_write(dev, addr, val); |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | U_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 Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 138 | static int renesas_ulcb_sysreset_request(struct udevice *dev, enum sysreset_t type) |
| 139 | { |
| 140 | cpld_write(dev, CPLD_ADDR_RESET, 1); |
| 141 | |
| 142 | return -EINPROGRESS; |
| 143 | } |
| 144 | |
| 145 | static int renesas_ulcb_sysreset_probe(struct udevice *dev) |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 146 | { |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 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; |
Marek Vasut | ad43cd3 | 2017-07-21 23:15:21 +0200 | [diff] [blame] | 172 | } |
Marek Vasut | 901897b | 2017-11-26 20:32:44 +0100 | [diff] [blame] | 173 | |
| 174 | static struct sysreset_ops renesas_ulcb_sysreset = { |
| 175 | .request = renesas_ulcb_sysreset_request, |
| 176 | }; |
| 177 | |
| 178 | static const struct udevice_id renesas_ulcb_sysreset_ids[] = { |
| 179 | { .compatible = "renesas,ulcb-cpld" }, |
| 180 | { } |
| 181 | }; |
| 182 | |
| 183 | U_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 | }; |