Tom Rini | 10e4779 | 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 | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 6a0b019 | 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 | |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 17 | /*----------------------------------------------------------------------- |
| 18 | * Definitions |
| 19 | */ |
| 20 | |
| 21 | #ifndef MAX_SPI_BYTES |
| 22 | # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */ |
| 23 | #endif |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 24 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 25 | /* |
| 26 | * Values from last command. |
| 27 | */ |
Reinhard Meyer | 7d398e9 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 28 | static unsigned int bus; |
| 29 | static unsigned int cs; |
| 30 | static unsigned int mode; |
Marek Vasut | 67e20fe | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 31 | static unsigned int freq; |
Wolfgang Denk | 62fb2b4 | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 32 | static int bitlen; |
| 33 | static uchar dout[MAX_SPI_BYTES]; |
| 34 | static uchar din[MAX_SPI_BYTES]; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 35 | |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 36 | static int do_spi_xfer(int bus, int cs) |
| 37 | { |
| 38 | struct spi_slave *slave; |
Simon Glass | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 39 | int ret = 0; |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 40 | |
Lukasz Majewski | 76f44298 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 41 | #if CONFIG_IS_ENABLED(DM_SPI) |
Simon Glass | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 42 | char name[30], *str; |
| 43 | struct udevice *dev; |
| 44 | |
| 45 | snprintf(name, sizeof(name), "generic_%d:%d", bus, cs); |
| 46 | str = strdup(name); |
Peng Fan | e349f94 | 2016-03-20 21:21:36 +0800 | [diff] [blame] | 47 | if (!str) |
| 48 | return -ENOMEM; |
Patrice Chotard | 86e06ae | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 49 | ret = _spi_get_bus_and_cs(bus, cs, freq, mode, "spi_generic_drv", |
| 50 | str, &dev, &slave); |
Simon Glass | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 51 | if (ret) |
| 52 | return ret; |
| 53 | #else |
Marek Vasut | 67e20fe | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 54 | slave = spi_setup_slave(bus, cs, freq, mode); |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 55 | if (!slave) { |
| 56 | printf("Invalid device %d:%d\n", bus, cs); |
| 57 | return -EINVAL; |
| 58 | } |
Simon Glass | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 59 | #endif |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 60 | |
Simon Glass | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 61 | ret = spi_claim_bus(slave); |
| 62 | if (ret) |
| 63 | goto done; |
| 64 | ret = spi_xfer(slave, bitlen, dout, din, |
| 65 | SPI_XFER_BEGIN | SPI_XFER_END); |
Lukasz Majewski | 76f44298 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 66 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Simon Glass | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 67 | /* We don't get an error code in this case */ |
| 68 | if (ret) |
| 69 | ret = -EIO; |
| 70 | #endif |
| 71 | if (ret) { |
| 72 | printf("Error %d during SPI transaction\n", ret); |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 73 | } else { |
| 74 | int j; |
| 75 | |
| 76 | for (j = 0; j < ((bitlen + 7) / 8); j++) |
| 77 | printf("%02X", din[j]); |
| 78 | printf("\n"); |
| 79 | } |
Simon Glass | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 80 | done: |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 81 | spi_release_bus(slave); |
Lukasz Majewski | 76f44298 | 2020-06-04 23:11:53 +0800 | [diff] [blame] | 82 | #if !CONFIG_IS_ENABLED(DM_SPI) |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 83 | spi_free_slave(slave); |
Simon Glass | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 84 | #endif |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 85 | |
Simon Glass | 3cc94b1 | 2014-10-13 23:41:56 -0600 | [diff] [blame] | 86 | return ret; |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 87 | } |
| 88 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 89 | /* |
| 90 | * SPI read/write |
| 91 | * |
| 92 | * Syntax: |
| 93 | * spi {dev} {num_bits} {dout} |
| 94 | * {dev} is the device number for controlling chip select (see TBD) |
| 95 | * {num_bits} is the number of bits to send & receive (base 10) |
| 96 | * {dout} is a hexadecimal string of data to send |
| 97 | * The command prints out the hexadecimal string received via SPI. |
| 98 | */ |
| 99 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 100 | 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] | 101 | { |
| 102 | char *cp = 0; |
| 103 | uchar tmp; |
| 104 | int j; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | * We use the last specified parameters, unless new ones are |
| 108 | * entered. |
| 109 | */ |
Marek Vasut | 67e20fe | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 110 | if (freq == 0) |
| 111 | freq = 1000000; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 112 | |
| 113 | if ((flag & CMD_FLAG_REPEAT) == 0) |
| 114 | { |
chenzhipeng | a3f5ff2 | 2022-12-06 17:24:38 +0800 | [diff] [blame] | 115 | if (argc < 2) |
| 116 | return CMD_RET_USAGE; |
| 117 | |
Reinhard Meyer | 7d398e9 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 118 | if (argc >= 2) { |
| 119 | mode = CONFIG_DEFAULT_SPI_MODE; |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 120 | bus = dectoul(argv[1], &cp); |
Reinhard Meyer | 7d398e9 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 121 | if (*cp == ':') { |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 122 | cs = dectoul(cp + 1, &cp); |
Reinhard Meyer | 7d398e9 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 123 | } else { |
| 124 | cs = bus; |
| 125 | bus = CONFIG_DEFAULT_SPI_BUS; |
| 126 | } |
Marek Vasut | 8af6a6a | 2012-08-01 15:12:15 +0200 | [diff] [blame] | 127 | if (*cp == '.') |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 128 | mode = dectoul(cp + 1, &cp); |
Marek Vasut | 67e20fe | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 129 | if (*cp == '@') |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 130 | freq = dectoul(cp + 1, &cp); |
Reinhard Meyer | 7d398e9 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 131 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 132 | if (argc >= 3) |
Simon Glass | ff9b903 | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 133 | bitlen = dectoul(argv[2], NULL); |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 134 | if (argc >= 4) { |
| 135 | cp = argv[3]; |
| 136 | for(j = 0; *cp; j++, cp++) { |
| 137 | tmp = *cp - '0'; |
| 138 | if(tmp > 9) |
| 139 | tmp -= ('A' - '0') - 10; |
| 140 | if(tmp > 15) |
| 141 | tmp -= ('a' - 'A'); |
| 142 | if(tmp > 15) { |
Reinhard Meyer | 7d398e9 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 143 | printf("Hex conversion error on %c\n", *cp); |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 144 | return 1; |
| 145 | } |
| 146 | if((j % 2) == 0) |
| 147 | dout[j / 2] = (tmp << 4); |
| 148 | else |
| 149 | dout[j / 2] |= tmp; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 150 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 154 | if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) { |
Reinhard Meyer | 7d398e9 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 155 | printf("Invalid bitlen %d\n", bitlen); |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 156 | return 1; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 157 | } |
wdenk | c217f6d | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 158 | |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 159 | if (do_spi_xfer(bus, cs)) |
Haavard Skinnemoen | d74084a | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 160 | return 1; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 161 | |
Simon Glass | 6a0b019 | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 162 | return 0; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 163 | } |
| 164 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 165 | /***************************************************/ |
| 166 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 167 | U_BOOT_CMD( |
| 168 | sspi, 5, 1, do_spi, |
Reinhard Meyer | 7d398e9 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 169 | "SPI utility command", |
Marek Vasut | 67e20fe | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 170 | "[<bus>:]<cs>[.<mode>][@<freq>] <bit_len> <dout> - Send and receive bits\n" |
Reinhard Meyer | 7d398e9 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 171 | "<bus> - Identifies the SPI bus\n" |
| 172 | "<cs> - Identifies the chip select\n" |
| 173 | "<mode> - Identifies the SPI mode to use\n" |
Marek Vasut | 67e20fe | 2019-12-20 12:44:57 +0100 | [diff] [blame] | 174 | "<freq> - Identifies the SPI bus frequency in Hz\n" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 175 | "<bit_len> - Number of bits to send (base 10)\n" |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 176 | "<dout> - Hexadecimal string that gets sent" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 177 | ); |