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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * SPI Read/Write Utilities |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <command.h> |
| 30 | #include <spi.h> |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 31 | |
Jon Loeliger | fd9bcaa | 2007-07-08 18:05:39 -0500 | [diff] [blame] | 32 | #if defined(CONFIG_CMD_SPI) |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 33 | |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 34 | /*----------------------------------------------------------------------- |
| 35 | * Definitions |
| 36 | */ |
| 37 | |
| 38 | #ifndef MAX_SPI_BYTES |
| 39 | # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */ |
| 40 | #endif |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * External table of chip select functions (see the appropriate board |
| 44 | * support for the actual definition of the table). |
| 45 | */ |
| 46 | extern spi_chipsel_type spi_chipsel[]; |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 47 | extern int spi_chipsel_cnt; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 48 | |
| 49 | /* |
| 50 | * Values from last command. |
| 51 | */ |
| 52 | static int device; |
| 53 | static int bitlen; |
| 54 | static uchar dout[MAX_SPI_BYTES]; |
| 55 | static uchar din[MAX_SPI_BYTES]; |
| 56 | |
| 57 | /* |
| 58 | * SPI read/write |
| 59 | * |
| 60 | * Syntax: |
| 61 | * spi {dev} {num_bits} {dout} |
| 62 | * {dev} is the device number for controlling chip select (see TBD) |
| 63 | * {num_bits} is the number of bits to send & receive (base 10) |
| 64 | * {dout} is a hexadecimal string of data to send |
| 65 | * The command prints out the hexadecimal string received via SPI. |
| 66 | */ |
| 67 | |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 68 | int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 69 | { |
| 70 | char *cp = 0; |
| 71 | uchar tmp; |
| 72 | int j; |
| 73 | int rcode = 0; |
| 74 | |
| 75 | /* |
| 76 | * We use the last specified parameters, unless new ones are |
| 77 | * entered. |
| 78 | */ |
| 79 | |
| 80 | if ((flag & CMD_FLAG_REPEAT) == 0) |
| 81 | { |
| 82 | if (argc >= 2) |
| 83 | device = simple_strtoul(argv[1], NULL, 10); |
| 84 | if (argc >= 3) |
| 85 | bitlen = simple_strtoul(argv[2], NULL, 10); |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 86 | if (argc >= 4) { |
| 87 | cp = argv[3]; |
| 88 | for(j = 0; *cp; j++, cp++) { |
| 89 | tmp = *cp - '0'; |
| 90 | if(tmp > 9) |
| 91 | tmp -= ('A' - '0') - 10; |
| 92 | if(tmp > 15) |
| 93 | tmp -= ('a' - 'A'); |
| 94 | if(tmp > 15) { |
| 95 | printf("Hex conversion error on %c, giving up.\n", *cp); |
| 96 | return 1; |
| 97 | } |
| 98 | if((j % 2) == 0) |
| 99 | dout[j / 2] = (tmp << 4); |
| 100 | else |
| 101 | dout[j / 2] |= tmp; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 102 | } |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 106 | if ((device < 0) || (device >= spi_chipsel_cnt)) { |
| 107 | printf("Invalid device %d, giving up.\n", device); |
| 108 | return 1; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 109 | } |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 110 | if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) { |
| 111 | printf("Invalid bitlen %d, giving up.\n", bitlen); |
| 112 | return 1; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 113 | } |
wdenk | eb9401e | 2002-11-11 02:11:37 +0000 | [diff] [blame] | 114 | |
| 115 | debug ("spi_chipsel[%d] = %08X\n", |
| 116 | device, (uint)spi_chipsel[device]); |
| 117 | |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 118 | if(spi_xfer(spi_chipsel[device], bitlen, dout, din) != 0) { |
| 119 | printf("Error with the SPI transaction.\n"); |
| 120 | rcode = 1; |
| 121 | } else { |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 122 | cp = (char *)din; |
wdenk | e309309 | 2002-10-01 01:07:28 +0000 | [diff] [blame] | 123 | for(j = 0; j < ((bitlen + 7) / 8); j++) { |
| 124 | printf("%02X", *cp++); |
| 125 | } |
| 126 | printf("\n"); |
| 127 | } |
| 128 | |
| 129 | return rcode; |
| 130 | } |
| 131 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | /***************************************************/ |
| 133 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 134 | U_BOOT_CMD( |
| 135 | sspi, 5, 1, do_spi, |
wdenk | 206c60c | 2003-09-18 10:02:25 +0000 | [diff] [blame] | 136 | "sspi - SPI utility commands\n", |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 137 | "<device> <bit_len> <dout> - Send <bit_len> bits from <dout> out the SPI\n" |
| 138 | "<device> - Identifies the chip select of the device\n" |
| 139 | "<bit_len> - Number of bits to send (base 10)\n" |
| 140 | "<dout> - Hexadecimal string that gets sent\n" |
| 141 | ); |
| 142 | |
Jon Loeliger | 9025317 | 2007-07-10 11:02:44 -0500 | [diff] [blame] | 143 | #endif |