]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
e3093091 WD |
2 | /* |
3 | * (C) Copyright 2002 | |
4 | * Gerald Van Baren, Custom IDEAS, [email protected] | |
e3093091 WD |
5 | */ |
6 | ||
7 | /* | |
8 | * SPI Read/Write Utilities | |
9 | */ | |
10 | ||
e3093091 | 11 | #include <command.h> |
1157a161 | 12 | #include <dm.h> |
df3b23ae | 13 | #include <errno.h> |
e3093091 | 14 | #include <spi.h> |
e3093091 | 15 | |
eb9401e3 WD |
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 | |
e3093091 | 23 | |
e3093091 WD |
24 | /* |
25 | * Values from last command. | |
26 | */ | |
21032b35 RM |
27 | static unsigned int bus; |
28 | static unsigned int cs; | |
29 | static unsigned int mode; | |
6954756a | 30 | static unsigned int freq; |
0cf207ec WD |
31 | static int bitlen; |
32 | static uchar dout[MAX_SPI_BYTES]; | |
33 | static uchar din[MAX_SPI_BYTES]; | |
e3093091 | 34 | |
df3b23ae SG |
35 | static int do_spi_xfer(int bus, int cs) |
36 | { | |
37 | struct spi_slave *slave; | |
1157a161 | 38 | int ret = 0; |
df3b23ae | 39 | |
56c40460 | 40 | #if CONFIG_IS_ENABLED(DM_SPI) |
1157a161 SG |
41 | char name[30], *str; |
42 | struct udevice *dev; | |
43 | ||
44 | snprintf(name, sizeof(name), "generic_%d:%d", bus, cs); | |
45 | str = strdup(name); | |
9caeb26c PF |
46 | if (!str) |
47 | return -ENOMEM; | |
61708bb0 PC |
48 | ret = _spi_get_bus_and_cs(bus, cs, freq, mode, "spi_generic_drv", |
49 | str, &dev, &slave); | |
1157a161 SG |
50 | if (ret) |
51 | return ret; | |
52 | #else | |
6954756a | 53 | slave = spi_setup_slave(bus, cs, freq, mode); |
df3b23ae SG |
54 | if (!slave) { |
55 | printf("Invalid device %d:%d\n", bus, cs); | |
56 | return -EINVAL; | |
57 | } | |
1157a161 | 58 | #endif |
df3b23ae | 59 | |
1157a161 SG |
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); | |
56c40460 | 65 | #if !CONFIG_IS_ENABLED(DM_SPI) |
1157a161 SG |
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); | |
df3b23ae SG |
72 | } else { |
73 | int j; | |
74 | ||
75 | for (j = 0; j < ((bitlen + 7) / 8); j++) | |
76 | printf("%02X", din[j]); | |
77 | printf("\n"); | |
78 | } | |
1157a161 | 79 | done: |
df3b23ae | 80 | spi_release_bus(slave); |
56c40460 | 81 | #if !CONFIG_IS_ENABLED(DM_SPI) |
df3b23ae | 82 | spi_free_slave(slave); |
1157a161 | 83 | #endif |
df3b23ae | 84 | |
1157a161 | 85 | return ret; |
df3b23ae SG |
86 | } |
87 | ||
e3093091 WD |
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 | ||
09140113 | 99 | int do_spi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
e3093091 WD |
100 | { |
101 | char *cp = 0; | |
102 | uchar tmp; | |
103 | int j; | |
e3093091 WD |
104 | |
105 | /* | |
106 | * We use the last specified parameters, unless new ones are | |
107 | * entered. | |
108 | */ | |
6954756a MV |
109 | if (freq == 0) |
110 | freq = 1000000; | |
e3093091 WD |
111 | |
112 | if ((flag & CMD_FLAG_REPEAT) == 0) | |
113 | { | |
c40e021b | 114 | if (argc < 2) |
115 | return CMD_RET_USAGE; | |
116 | ||
21032b35 RM |
117 | if (argc >= 2) { |
118 | mode = CONFIG_DEFAULT_SPI_MODE; | |
0b1284eb | 119 | bus = dectoul(argv[1], &cp); |
21032b35 | 120 | if (*cp == ':') { |
0b1284eb | 121 | cs = dectoul(cp + 1, &cp); |
21032b35 RM |
122 | } else { |
123 | cs = bus; | |
124 | bus = CONFIG_DEFAULT_SPI_BUS; | |
125 | } | |
2d5e7c7a | 126 | if (*cp == '.') |
0b1284eb | 127 | mode = dectoul(cp + 1, &cp); |
6954756a | 128 | if (*cp == '@') |
0b1284eb | 129 | freq = dectoul(cp + 1, &cp); |
21032b35 | 130 | } |
e3093091 | 131 | if (argc >= 3) |
0b1284eb | 132 | bitlen = dectoul(argv[2], NULL); |
eb9401e3 WD |
133 | if (argc >= 4) { |
134 | cp = argv[3]; | |
135 | for(j = 0; *cp; j++, cp++) { | |
136 | tmp = *cp - '0'; | |
137 | if(tmp > 9) | |
138 | tmp -= ('A' - '0') - 10; | |
139 | if(tmp > 15) | |
140 | tmp -= ('a' - 'A'); | |
141 | if(tmp > 15) { | |
21032b35 | 142 | printf("Hex conversion error on %c\n", *cp); |
eb9401e3 WD |
143 | return 1; |
144 | } | |
145 | if((j % 2) == 0) | |
146 | dout[j / 2] = (tmp << 4); | |
147 | else | |
148 | dout[j / 2] |= tmp; | |
e3093091 | 149 | } |
e3093091 WD |
150 | } |
151 | } | |
152 | ||
eb9401e3 | 153 | if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) { |
21032b35 | 154 | printf("Invalid bitlen %d\n", bitlen); |
eb9401e3 | 155 | return 1; |
8bde7f77 | 156 | } |
eb9401e3 | 157 | |
df3b23ae | 158 | if (do_spi_xfer(bus, cs)) |
d255bb0e | 159 | return 1; |
e3093091 | 160 | |
df3b23ae | 161 | return 0; |
e3093091 WD |
162 | } |
163 | ||
8bde7f77 WD |
164 | /***************************************************/ |
165 | ||
0d498393 WD |
166 | U_BOOT_CMD( |
167 | sspi, 5, 1, do_spi, | |
21032b35 | 168 | "SPI utility command", |
6954756a | 169 | "[<bus>:]<cs>[.<mode>][@<freq>] <bit_len> <dout> - Send and receive bits\n" |
21032b35 RM |
170 | "<bus> - Identifies the SPI bus\n" |
171 | "<cs> - Identifies the chip select\n" | |
172 | "<mode> - Identifies the SPI mode to use\n" | |
6954756a | 173 | "<freq> - Identifies the SPI bus frequency in Hz\n" |
8bde7f77 | 174 | "<bit_len> - Number of bits to send (base 10)\n" |
a89c33db | 175 | "<dout> - Hexadecimal string that gets sent" |
8bde7f77 | 176 | ); |