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