Valentine Barshak | 638bb69 | 2019-04-23 23:44:57 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * V3HSK board CPLD access support |
| 4 | * |
| 5 | * Copyright (C) 2019 Renesas Electronics Corporation |
| 6 | * Copyright (C) 2019 Cogent Embedded, Inc. |
| 7 | * |
| 8 | */ |
| 9 | |
Valentine Barshak | 638bb69 | 2019-04-23 23:44:57 +0300 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <i2c.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <sysreset.h> |
| 15 | #include <command.h> |
| 16 | |
| 17 | #define CPLD_ADDR_PRODUCT_0 0x0000 /* R */ |
| 18 | #define CPLD_ADDR_PRODUCT_1 0x0001 /* R */ |
| 19 | #define CPLD_ADDR_PRODUCT_2 0x0002 /* R */ |
| 20 | #define CPLD_ADDR_PRODUCT_3 0x0003 /* R */ |
| 21 | #define CPLD_ADDR_CPLD_VERSION_D 0x0004 /* R */ |
| 22 | #define CPLD_ADDR_CPLD_VERSION_M 0x0005 /* R */ |
| 23 | #define CPLD_ADDR_CPLD_VERSION_Y_0 0x0006 /* R */ |
| 24 | #define CPLD_ADDR_CPLD_VERSION_Y_1 0x0007 /* R */ |
| 25 | #define CPLD_ADDR_MODE_SET_0 0x0008 /* R */ |
| 26 | #define CPLD_ADDR_MODE_SET_1 0x0009 /* R */ |
| 27 | #define CPLD_ADDR_MODE_SET_2 0x000A /* R */ |
| 28 | #define CPLD_ADDR_MODE_SET_3 0x000B /* R */ |
| 29 | #define CPLD_ADDR_MODE_SET_4 0x000C /* R */ |
| 30 | #define CPLD_ADDR_MODE_LAST_0 0x0018 /* R */ |
| 31 | #define CPLD_ADDR_MODE_LAST_1 0x0019 /* R */ |
| 32 | #define CPLD_ADDR_MODE_LAST_2 0x001A /* R */ |
| 33 | #define CPLD_ADDR_MODE_LAST_3 0x001B /* R */ |
| 34 | #define CPLD_ADDR_MODE_LAST_4 0x001C /* R */ |
| 35 | #define CPLD_ADDR_DIPSW4 0x0020 /* R */ |
| 36 | #define CPLD_ADDR_DIPSW5 0x0021 /* R */ |
| 37 | #define CPLD_ADDR_RESET 0x0024 /* R/W */ |
| 38 | #define CPLD_ADDR_POWER_CFG 0x0025 /* R/W */ |
| 39 | #define CPLD_ADDR_PERI_CFG_0 0x0030 /* R/W */ |
| 40 | #define CPLD_ADDR_PERI_CFG_1 0x0031 /* R/W */ |
| 41 | #define CPLD_ADDR_PERI_CFG_2 0x0032 /* R/W */ |
| 42 | #define CPLD_ADDR_PERI_CFG_3 0x0033 /* R/W */ |
| 43 | #define CPLD_ADDR_LEDS 0x0034 /* R/W */ |
| 44 | #define CPLD_ADDR_LEDS_CFG 0x0035 /* R/W */ |
| 45 | #define CPLD_ADDR_UART_CFG 0x0036 /* R/W */ |
| 46 | #define CPLD_ADDR_UART_STATUS 0x0037 /* R */ |
| 47 | |
| 48 | #define CPLD_ADDR_PCB_VERSION_0 0x1000 /* R */ |
| 49 | #define CPLD_ADDR_PCB_VERSION_1 0x1001 /* R */ |
| 50 | #define CPLD_ADDR_SOC_VERSION_0 0x1002 /* R */ |
| 51 | #define CPLD_ADDR_SOC_VERSION_1 0x1003 /* R */ |
| 52 | #define CPLD_ADDR_PCB_SN_0 0x1004 /* R */ |
| 53 | #define CPLD_ADDR_PCB_SN_1 0x1005 /* R */ |
| 54 | |
| 55 | static u16 cpld_read(struct udevice *dev, u16 addr) |
| 56 | { |
| 57 | u8 data[2]; |
| 58 | |
| 59 | /* Random flash reads require 2 reads: first read is unreliable */ |
| 60 | if (addr >= CPLD_ADDR_PCB_VERSION_0) |
| 61 | dm_i2c_read(dev, addr, data, 2); |
| 62 | |
| 63 | /* Only the second byte read is valid */ |
| 64 | dm_i2c_read(dev, addr, data, 2); |
| 65 | return data[1]; |
| 66 | } |
| 67 | |
| 68 | static void cpld_write(struct udevice *dev, u16 addr, u8 data) |
| 69 | { |
| 70 | dm_i2c_write(dev, addr, &data, 1); |
| 71 | } |
| 72 | |
| 73 | static int do_cpld(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) |
| 74 | { |
| 75 | struct udevice *dev; |
| 76 | u16 addr, val; |
| 77 | int ret; |
| 78 | |
| 79 | ret = uclass_get_device_by_driver(UCLASS_SYSRESET, |
| 80 | DM_DRIVER_GET(sysreset_renesas_v3hsk), |
| 81 | &dev); |
| 82 | if (ret) |
| 83 | return ret; |
| 84 | |
| 85 | if (argc == 2 && strcmp(argv[1], "info") == 0) { |
| 86 | printf("Product: 0x%08x\n", |
| 87 | (cpld_read(dev, CPLD_ADDR_PRODUCT_3) << 24) | |
| 88 | (cpld_read(dev, CPLD_ADDR_PRODUCT_2) << 16) | |
| 89 | (cpld_read(dev, CPLD_ADDR_PRODUCT_1) << 8) | |
| 90 | cpld_read(dev, CPLD_ADDR_PRODUCT_0)); |
| 91 | printf("CPLD version: 0x%08x\n", |
| 92 | (cpld_read(dev, CPLD_ADDR_CPLD_VERSION_Y_1) << 24) | |
| 93 | (cpld_read(dev, CPLD_ADDR_CPLD_VERSION_Y_0) << 16) | |
| 94 | (cpld_read(dev, CPLD_ADDR_CPLD_VERSION_M) << 8) | |
| 95 | cpld_read(dev, CPLD_ADDR_CPLD_VERSION_D)); |
| 96 | printf("Mode setting (MD0..26): 0x%08x\n", |
| 97 | (cpld_read(dev, CPLD_ADDR_MODE_LAST_3) << 24) | |
| 98 | (cpld_read(dev, CPLD_ADDR_MODE_LAST_2) << 16) | |
| 99 | (cpld_read(dev, CPLD_ADDR_MODE_LAST_1) << 8) | |
| 100 | cpld_read(dev, CPLD_ADDR_MODE_LAST_0)); |
| 101 | printf("DIPSW (SW4, SW5): 0x%02x, 0x%x\n", |
| 102 | cpld_read(dev, CPLD_ADDR_DIPSW4) ^ 0xff, |
| 103 | (cpld_read(dev, CPLD_ADDR_DIPSW5) ^ 0xff) & 0xf); |
| 104 | printf("Power config: 0x%08x\n", |
| 105 | cpld_read(dev, CPLD_ADDR_POWER_CFG)); |
| 106 | printf("Periferals config: 0x%08x\n", |
| 107 | (cpld_read(dev, CPLD_ADDR_PERI_CFG_3) << 24) | |
| 108 | (cpld_read(dev, CPLD_ADDR_PERI_CFG_2) << 16) | |
| 109 | (cpld_read(dev, CPLD_ADDR_PERI_CFG_1) << 8) | |
| 110 | cpld_read(dev, CPLD_ADDR_PERI_CFG_0)); |
| 111 | printf("PCB version: %d.%d\n", |
| 112 | cpld_read(dev, CPLD_ADDR_PCB_VERSION_1), |
| 113 | cpld_read(dev, CPLD_ADDR_PCB_VERSION_0)); |
| 114 | printf("SOC version: %d.%d\n", |
| 115 | cpld_read(dev, CPLD_ADDR_SOC_VERSION_1), |
| 116 | cpld_read(dev, CPLD_ADDR_SOC_VERSION_0)); |
| 117 | printf("PCB S/N: %d\n", |
| 118 | (cpld_read(dev, CPLD_ADDR_PCB_SN_1) << 8) | |
| 119 | cpld_read(dev, CPLD_ADDR_PCB_SN_0)); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | if (argc < 3) |
| 124 | return CMD_RET_USAGE; |
| 125 | |
| 126 | addr = simple_strtoul(argv[2], NULL, 16); |
| 127 | if (!(addr >= CPLD_ADDR_PRODUCT_0 && addr <= CPLD_ADDR_UART_STATUS)) { |
| 128 | printf("cpld invalid addr\n"); |
| 129 | return CMD_RET_USAGE; |
| 130 | } |
| 131 | |
| 132 | if (argc == 3 && strcmp(argv[1], "read") == 0) { |
| 133 | printf("0x%x\n", cpld_read(dev, addr)); |
| 134 | } else if (argc == 4 && strcmp(argv[1], "write") == 0) { |
| 135 | val = simple_strtoul(argv[3], NULL, 16); |
| 136 | cpld_write(dev, addr, val); |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | U_BOOT_CMD(cpld, 4, 1, do_cpld, |
| 143 | "CPLD access", |
| 144 | "info\n" |
| 145 | "cpld read addr\n" |
| 146 | "cpld write addr val\n" |
| 147 | ); |
| 148 | |
| 149 | static int renesas_v3hsk_sysreset_request(struct udevice *dev, enum sysreset_t type) |
| 150 | { |
| 151 | cpld_write(dev, CPLD_ADDR_RESET, 1); |
| 152 | |
| 153 | return -EINPROGRESS; |
| 154 | } |
| 155 | |
| 156 | static int renesas_v3hsk_sysreset_probe(struct udevice *dev) |
| 157 | { |
| 158 | if (device_get_uclass_id(dev->parent) != UCLASS_I2C) |
| 159 | return -EPROTONOSUPPORT; |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static struct sysreset_ops renesas_v3hsk_sysreset = { |
| 165 | .request = renesas_v3hsk_sysreset_request, |
| 166 | }; |
| 167 | |
| 168 | static const struct udevice_id renesas_v3hsk_sysreset_ids[] = { |
| 169 | { .compatible = "renesas,v3hsk-cpld" }, |
| 170 | { /* sentinel */ } |
| 171 | }; |
| 172 | |
| 173 | U_BOOT_DRIVER(sysreset_renesas_v3hsk) = { |
| 174 | .name = "renesas_v3hsk_sysreset", |
| 175 | .id = UCLASS_SYSRESET, |
| 176 | .ops = &renesas_v3hsk_sysreset, |
| 177 | .probe = renesas_v3hsk_sysreset_probe, |
| 178 | .of_match = renesas_v3hsk_sysreset_ids, |
| 179 | }; |