Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2002 |
| 4 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * SPI Read/Write Utilities |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 14 | #include <errno.h> |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 15 | #include <spi.h> |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 16 | |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 17 | #if CONFIG_IS_ENABLED(AMLOGIC_MODIFY) |
| 18 | #include <linux/compat.h> |
| 19 | #endif |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 20 | /*----------------------------------------------------------------------- |
| 21 | * Definitions |
| 22 | */ |
| 23 | |
| 24 | #ifndef MAX_SPI_BYTES |
| 25 | # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */ |
| 26 | #endif |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 27 | |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 28 | #if CONFIG_IS_ENABLED(AMLOGIC_MODIFY) |
| 29 | #ifndef CONFIG_DEFAULT_SPI_BUS |
| 30 | # define CONFIG_DEFAULT_SPI_BUS 0 |
| 31 | #endif |
| 32 | #ifndef CONFIG_DEFAULT_SPI_MODE |
| 33 | # define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0 |
| 34 | #endif |
| 35 | #endif |
| 36 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 37 | /* |
| 38 | * Values from last command. |
| 39 | */ |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 40 | static unsigned int bus; |
| 41 | static unsigned int cs; |
| 42 | static unsigned int mode; |
Marek Vasut | 6954756 | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 43 | static unsigned int freq; |
Wolfgang Denk | 0cf207e | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 44 | static int bitlen; |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 45 | |
| 46 | #if !CONFIG_IS_ENABLED(AMLOGIC_MODIFY) |
Wolfgang Denk | 0cf207e | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 47 | static uchar dout[MAX_SPI_BYTES]; |
| 48 | static uchar din[MAX_SPI_BYTES]; |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 49 | #else |
| 50 | static int wordlen = 8; |
| 51 | static unsigned int xfer_flags; |
| 52 | struct spi_slave *g_slave; |
| 53 | #define XFERS_MAX 8 |
| 54 | static uchar *buf_list[XFERS_MAX]; |
| 55 | static int buf_cnt; |
| 56 | static uchar *dout; |
| 57 | static uchar *din; |
| 58 | #endif |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 59 | |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 60 | static int do_spi_xfer(int bus, int cs) |
| 61 | { |
| 62 | struct spi_slave *slave; |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 63 | int ret = 0; |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 64 | #if CONFIG_IS_ENABLED(AMLOGIC_MODIFY) |
| 65 | if (g_slave && !(xfer_flags & SPI_XFER_BEGIN)) { |
| 66 | slave = g_slave; |
| 67 | goto next_xfer; |
| 68 | } |
| 69 | #endif |
Lukasz Majewski | 56c4046 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 70 | #if CONFIG_IS_ENABLED(DM_SPI) |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 71 | char name[30], *str; |
| 72 | struct udevice *dev; |
| 73 | |
| 74 | snprintf(name, sizeof(name), "generic_%d:%d", bus, cs); |
| 75 | str = strdup(name); |
Peng Fan | 9caeb26 | 2016-03-20 21:21:36 +0800 | [diff] [blame] | 76 | if (!str) |
| 77 | return -ENOMEM; |
Patrice Chotard | 61708bb | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 78 | ret = _spi_get_bus_and_cs(bus, cs, freq, mode, "spi_generic_drv", |
| 79 | str, &dev, &slave); |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 80 | if (ret) |
| 81 | return ret; |
| 82 | #else |
Marek Vasut | 6954756 | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 83 | slave = spi_setup_slave(bus, cs, freq, mode); |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 84 | if (!slave) { |
| 85 | printf("Invalid device %d:%d\n", bus, cs); |
| 86 | return -EINVAL; |
| 87 | } |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 88 | #endif |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 89 | |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 90 | #if CONFIG_IS_ENABLED(AMLOGIC_MODIFY) |
| 91 | g_slave = slave; |
| 92 | slave->wordlen = wordlen; |
| 93 | #endif |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 94 | ret = spi_claim_bus(slave); |
| 95 | if (ret) |
| 96 | goto done; |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 97 | #if !CONFIG_IS_ENABLED(AMLOGIC_MODIFY) |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 98 | ret = spi_xfer(slave, bitlen, dout, din, |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 99 | SPI_XFER_BEGIN | SPI_XFER_END); |
| 100 | #else |
| 101 | next_xfer: |
| 102 | ret = spi_xfer(slave, bitlen, dout, din, xfer_flags); |
| 103 | if (ret > 0) |
| 104 | return 0; |
| 105 | #endif |
Lukasz Majewski | 56c4046 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 106 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 107 | /* We don't get an error code in this case */ |
| 108 | if (ret) |
| 109 | ret = -EIO; |
| 110 | #endif |
| 111 | if (ret) { |
| 112 | printf("Error %d during SPI transaction\n", ret); |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 113 | } else { |
| 114 | int j; |
| 115 | |
| 116 | for (j = 0; j < ((bitlen + 7) / 8); j++) |
| 117 | printf("%02X", din[j]); |
| 118 | printf("\n"); |
| 119 | } |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 120 | done: |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 121 | #if CONFIG_IS_ENABLED(AMLOGIC_MODIFY) |
| 122 | g_slave = 0; |
| 123 | for (int i = 0; i < buf_cnt; i++) |
| 124 | kfree(buf_list[i]); |
| 125 | buf_cnt = 0; |
| 126 | #endif |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 127 | spi_release_bus(slave); |
Lukasz Majewski | 56c4046 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 128 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 129 | spi_free_slave(slave); |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 130 | #endif |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 131 | |
Simon Glass | 1157a16 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 132 | return ret; |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 133 | } |
| 134 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 135 | /* |
| 136 | * SPI read/write |
| 137 | * |
| 138 | * Syntax: |
| 139 | * spi {dev} {num_bits} {dout} |
| 140 | * {dev} is the device number for controlling chip select (see TBD) |
| 141 | * {num_bits} is the number of bits to send & receive (base 10) |
| 142 | * {dout} is a hexadecimal string of data to send |
| 143 | * The command prints out the hexadecimal string received via SPI. |
| 144 | */ |
| 145 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 146 | int do_spi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 147 | { |
| 148 | char *cp = 0; |
| 149 | uchar tmp; |
| 150 | int j; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * We use the last specified parameters, unless new ones are |
| 154 | * entered. |
| 155 | */ |
Marek Vasut | 6954756 | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 156 | if (freq == 0) |
| 157 | freq = 1000000; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 158 | |
| 159 | if ((flag & CMD_FLAG_REPEAT) == 0) |
| 160 | { |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 161 | if (argc >= 2) { |
| 162 | mode = CONFIG_DEFAULT_SPI_MODE; |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 163 | bus = dectoul(argv[1], &cp); |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 164 | if (*cp == ':') { |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 165 | cs = dectoul(cp + 1, &cp); |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 166 | } else { |
| 167 | cs = bus; |
| 168 | bus = CONFIG_DEFAULT_SPI_BUS; |
| 169 | } |
Marek Vasut | 2d5e7c7 | 2012-08-01 15:12:15 +0200 | [diff] [blame] | 170 | if (*cp == '.') |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 171 | mode = dectoul(cp + 1, &cp); |
Marek Vasut | 6954756 | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 172 | if (*cp == '@') |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 173 | freq = dectoul(cp + 1, &cp); |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 174 | #if CONFIG_IS_ENABLED(AMLOGIC_MODIFY) |
| 175 | if (*cp == '.') |
| 176 | wordlen = simple_strtoul(cp + 1, &cp, 10); |
| 177 | if (*cp == '.') |
| 178 | xfer_flags = simple_strtoul(cp + 1, &cp, 16); |
| 179 | else |
| 180 | xfer_flags = SPI_XFER_BEGIN | SPI_XFER_END; |
| 181 | #endif |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 182 | } |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 183 | #if !CONFIG_IS_ENABLED(AMLOGIC_MODIFY) |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 184 | if (argc >= 3) |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 185 | bitlen = dectoul(argv[2], NULL); |
Feng Chen | 2874819 | 2023-04-19 14:48:09 +0800 | [diff] [blame] | 186 | #else |
| 187 | if (argc >= 3) { |
| 188 | bitlen = dectoul(argv[2], NULL); |
| 189 | dout = kcalloc(ALIGN(bitlen / 8, 8), 2, GFP_KERNEL); |
| 190 | if (!dout) |
| 191 | return -ENOMEM; |
| 192 | din = dout + ALIGN(bitlen / 8, 8); |
| 193 | buf_list[buf_cnt] = dout; |
| 194 | if (++buf_cnt >= XFERS_MAX) |
| 195 | xfer_flags |= SPI_XFER_END; |
| 196 | } |
| 197 | #endif |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 198 | if (argc >= 4) { |
| 199 | cp = argv[3]; |
| 200 | for(j = 0; *cp; j++, cp++) { |
| 201 | tmp = *cp - '0'; |
| 202 | if(tmp > 9) |
| 203 | tmp -= ('A' - '0') - 10; |
| 204 | if(tmp > 15) |
| 205 | tmp -= ('a' - 'A'); |
| 206 | if(tmp > 15) { |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 207 | printf("Hex conversion error on %c\n", *cp); |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 208 | return 1; |
| 209 | } |
| 210 | if((j % 2) == 0) |
| 211 | dout[j / 2] = (tmp << 4); |
| 212 | else |
| 213 | dout[j / 2] |= tmp; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 214 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 218 | if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) { |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 219 | printf("Invalid bitlen %d\n", bitlen); |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 220 | return 1; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 221 | } |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 222 | |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 223 | if (do_spi_xfer(bus, cs)) |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 224 | return 1; |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 225 | |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 226 | return 0; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 227 | } |
| 228 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 229 | /***************************************************/ |
| 230 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 231 | U_BOOT_CMD( |
| 232 | sspi, 5, 1, do_spi, |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 233 | "SPI utility command", |
Marek Vasut | 6954756 | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 234 | "[<bus>:]<cs>[.<mode>][@<freq>] <bit_len> <dout> - Send and receive bits\n" |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 235 | "<bus> - Identifies the SPI bus\n" |
| 236 | "<cs> - Identifies the chip select\n" |
| 237 | "<mode> - Identifies the SPI mode to use\n" |
Marek Vasut | 6954756 | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 238 | "<freq> - Identifies the SPI bus frequency in Hz\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 239 | "<bit_len> - Number of bits to send (base 10)\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 240 | "<dout> - Hexadecimal string that gets sent" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 241 | ); |