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