]>
Commit | Line | Data |
---|---|---|
e3093091 WD |
1 | /* |
2 | * (C) Copyright 2002 | |
3 | * Gerald Van Baren, Custom IDEAS, [email protected] | |
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> | |
e3093091 | 31 | |
eb9401e3 WD |
32 | /*----------------------------------------------------------------------- |
33 | * Definitions | |
34 | */ | |
35 | ||
36 | #ifndef MAX_SPI_BYTES | |
37 | # define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */ | |
38 | #endif | |
e3093091 | 39 | |
d255bb0e HS |
40 | #ifndef CONFIG_DEFAULT_SPI_BUS |
41 | # define CONFIG_DEFAULT_SPI_BUS 0 | |
42 | #endif | |
43 | #ifndef CONFIG_DEFAULT_SPI_MODE | |
44 | # define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0 | |
45 | #endif | |
e3093091 WD |
46 | |
47 | /* | |
48 | * Values from last command. | |
49 | */ | |
d255bb0e HS |
50 | static unsigned int device; |
51 | static int bitlen; | |
52 | static uchar dout[MAX_SPI_BYTES]; | |
53 | static uchar din[MAX_SPI_BYTES]; | |
e3093091 WD |
54 | |
55 | /* | |
56 | * SPI read/write | |
57 | * | |
58 | * Syntax: | |
59 | * spi {dev} {num_bits} {dout} | |
60 | * {dev} is the device number for controlling chip select (see TBD) | |
61 | * {num_bits} is the number of bits to send & receive (base 10) | |
62 | * {dout} is a hexadecimal string of data to send | |
63 | * The command prints out the hexadecimal string received via SPI. | |
64 | */ | |
65 | ||
eb9401e3 | 66 | int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
e3093091 | 67 | { |
d255bb0e | 68 | struct spi_slave *slave; |
e3093091 WD |
69 | char *cp = 0; |
70 | uchar tmp; | |
71 | int j; | |
72 | int rcode = 0; | |
73 | ||
74 | /* | |
75 | * We use the last specified parameters, unless new ones are | |
76 | * entered. | |
77 | */ | |
78 | ||
79 | if ((flag & CMD_FLAG_REPEAT) == 0) | |
80 | { | |
81 | if (argc >= 2) | |
82 | device = simple_strtoul(argv[1], NULL, 10); | |
83 | if (argc >= 3) | |
84 | bitlen = simple_strtoul(argv[2], NULL, 10); | |
eb9401e3 WD |
85 | if (argc >= 4) { |
86 | cp = argv[3]; | |
87 | for(j = 0; *cp; j++, cp++) { | |
88 | tmp = *cp - '0'; | |
89 | if(tmp > 9) | |
90 | tmp -= ('A' - '0') - 10; | |
91 | if(tmp > 15) | |
92 | tmp -= ('a' - 'A'); | |
93 | if(tmp > 15) { | |
94 | printf("Hex conversion error on %c, giving up.\n", *cp); | |
95 | return 1; | |
96 | } | |
97 | if((j % 2) == 0) | |
98 | dout[j / 2] = (tmp << 4); | |
99 | else | |
100 | dout[j / 2] |= tmp; | |
e3093091 | 101 | } |
e3093091 WD |
102 | } |
103 | } | |
104 | ||
eb9401e3 WD |
105 | if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) { |
106 | printf("Invalid bitlen %d, giving up.\n", bitlen); | |
107 | return 1; | |
8bde7f77 | 108 | } |
eb9401e3 | 109 | |
d255bb0e HS |
110 | /* FIXME: Make these parameters run-time configurable */ |
111 | slave = spi_setup_slave(CONFIG_DEFAULT_SPI_BUS, device, 1000000, | |
112 | CONFIG_DEFAULT_SPI_MODE); | |
113 | if (!slave) { | |
114 | printf("Invalid device %d, giving up.\n", device); | |
115 | return 1; | |
116 | } | |
117 | ||
118 | debug ("spi chipsel = %08X\n", device); | |
eb9401e3 | 119 | |
d255bb0e HS |
120 | spi_claim_bus(slave); |
121 | if(spi_xfer(slave, bitlen, dout, din, | |
122 | SPI_XFER_BEGIN | SPI_XFER_END) != 0) { | |
e3093091 WD |
123 | printf("Error with the SPI transaction.\n"); |
124 | rcode = 1; | |
125 | } else { | |
e3093091 | 126 | for(j = 0; j < ((bitlen + 7) / 8); j++) { |
c46980f6 | 127 | printf("%02X", din[j]); |
e3093091 WD |
128 | } |
129 | printf("\n"); | |
130 | } | |
d255bb0e HS |
131 | spi_release_bus(slave); |
132 | spi_free_slave(slave); | |
e3093091 WD |
133 | |
134 | return rcode; | |
135 | } | |
136 | ||
8bde7f77 WD |
137 | /***************************************************/ |
138 | ||
0d498393 WD |
139 | U_BOOT_CMD( |
140 | sspi, 5, 1, do_spi, | |
2fb2604d | 141 | "SPI utility commands", |
8bde7f77 WD |
142 | "<device> <bit_len> <dout> - Send <bit_len> bits from <dout> out the SPI\n" |
143 | "<device> - Identifies the chip select of the device\n" | |
144 | "<bit_len> - Number of bits to send (base 10)\n" | |
145 | "<dout> - Hexadecimal string that gets sent\n" | |
146 | ); |