wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * SPI Read/Write Utilities |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <command.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 | |
wdenk | eb9401e | 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 | |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 25 | #ifndef CONFIG_DEFAULT_SPI_BUS |
| 26 | # define CONFIG_DEFAULT_SPI_BUS 0 |
| 27 | #endif |
| 28 | #ifndef CONFIG_DEFAULT_SPI_MODE |
| 29 | # define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0 |
| 30 | #endif |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * Values from last command. |
| 34 | */ |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 35 | static unsigned int bus; |
| 36 | static unsigned int cs; |
| 37 | static unsigned int mode; |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 38 | static int bitlen; |
| 39 | static uchar dout[MAX_SPI_BYTES]; |
| 40 | static uchar din[MAX_SPI_BYTES]; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 41 | |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 42 | static int do_spi_xfer(int bus, int cs) |
| 43 | { |
| 44 | struct spi_slave *slave; |
| 45 | int rcode = 0; |
| 46 | |
| 47 | slave = spi_setup_slave(bus, cs, 1000000, mode); |
| 48 | if (!slave) { |
| 49 | printf("Invalid device %d:%d\n", bus, cs); |
| 50 | return -EINVAL; |
| 51 | } |
| 52 | |
| 53 | spi_claim_bus(slave); |
| 54 | if (spi_xfer(slave, bitlen, dout, din, |
| 55 | SPI_XFER_BEGIN | SPI_XFER_END) != 0) { |
| 56 | printf("Error during SPI transaction\n"); |
| 57 | rcode = -EIO; |
| 58 | } else { |
| 59 | int j; |
| 60 | |
| 61 | for (j = 0; j < ((bitlen + 7) / 8); j++) |
| 62 | printf("%02X", din[j]); |
| 63 | printf("\n"); |
| 64 | } |
| 65 | spi_release_bus(slave); |
| 66 | spi_free_slave(slave); |
| 67 | |
| 68 | return rcode; |
| 69 | } |
| 70 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 71 | /* |
| 72 | * SPI read/write |
| 73 | * |
| 74 | * Syntax: |
| 75 | * spi {dev} {num_bits} {dout} |
| 76 | * {dev} is the device number for controlling chip select (see TBD) |
| 77 | * {num_bits} is the number of bits to send & receive (base 10) |
| 78 | * {dout} is a hexadecimal string of data to send |
| 79 | * The command prints out the hexadecimal string received via SPI. |
| 80 | */ |
| 81 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 82 | 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] | 83 | { |
| 84 | char *cp = 0; |
| 85 | uchar tmp; |
| 86 | int j; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * We use the last specified parameters, unless new ones are |
| 90 | * entered. |
| 91 | */ |
| 92 | |
| 93 | if ((flag & CMD_FLAG_REPEAT) == 0) |
| 94 | { |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 95 | if (argc >= 2) { |
| 96 | mode = CONFIG_DEFAULT_SPI_MODE; |
| 97 | bus = simple_strtoul(argv[1], &cp, 10); |
| 98 | if (*cp == ':') { |
| 99 | cs = simple_strtoul(cp+1, &cp, 10); |
| 100 | } else { |
| 101 | cs = bus; |
| 102 | bus = CONFIG_DEFAULT_SPI_BUS; |
| 103 | } |
Marek Vasut | 2d5e7c7 | 2012-08-01 15:12:15 +0200 | [diff] [blame] | 104 | if (*cp == '.') |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 105 | mode = simple_strtoul(cp+1, NULL, 10); |
| 106 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 107 | if (argc >= 3) |
| 108 | bitlen = simple_strtoul(argv[2], NULL, 10); |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 109 | if (argc >= 4) { |
| 110 | cp = argv[3]; |
| 111 | for(j = 0; *cp; j++, cp++) { |
| 112 | tmp = *cp - '0'; |
| 113 | if(tmp > 9) |
| 114 | tmp -= ('A' - '0') - 10; |
| 115 | if(tmp > 15) |
| 116 | tmp -= ('a' - 'A'); |
| 117 | if(tmp > 15) { |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 118 | printf("Hex conversion error on %c\n", *cp); |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 119 | return 1; |
| 120 | } |
| 121 | if((j % 2) == 0) |
| 122 | dout[j / 2] = (tmp << 4); |
| 123 | else |
| 124 | dout[j / 2] |= tmp; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 125 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 129 | if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) { |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 130 | printf("Invalid bitlen %d\n", bitlen); |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 131 | return 1; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | } |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 133 | |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 134 | if (do_spi_xfer(bus, cs)) |
Haavard Skinnemoen | d255bb0 | 2008-05-16 11:10:31 +0200 | [diff] [blame] | 135 | return 1; |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 136 | |
Simon Glass | df3b23a | 2014-09-15 06:33:22 -0600 | [diff] [blame] | 137 | return 0; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 138 | } |
| 139 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 140 | /***************************************************/ |
| 141 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 142 | U_BOOT_CMD( |
| 143 | sspi, 5, 1, do_spi, |
Reinhard Meyer | 21032b3 | 2010-08-26 10:57:27 +0200 | [diff] [blame] | 144 | "SPI utility command", |
| 145 | "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n" |
| 146 | "<bus> - Identifies the SPI bus\n" |
| 147 | "<cs> - Identifies the chip select\n" |
| 148 | "<mode> - Identifies the SPI mode to use\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 149 | "<bit_len> - Number of bits to send (base 10)\n" |
Wolfgang Denk | a89c33d | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 150 | "<dout> - Hexadecimal string that gets sent" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 151 | ); |