]> Git Repo - J-u-boot.git/blame - cmd/spi.c
Azure CI: Save pytest output automatically
[J-u-boot.git] / cmd / spi.c
CommitLineData
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
11#include <common.h>
12#include <command.h>
1157a161 13#include <dm.h>
df3b23ae 14#include <errno.h>
e3093091 15#include <spi.h>
e3093091 16
eb9401e3
WD
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
e3093091 24
e3093091
WD
25/*
26 * Values from last command.
27 */
21032b35
RM
28static unsigned int bus;
29static unsigned int cs;
30static unsigned int mode;
6954756a 31static unsigned int freq;
0cf207ec
WD
32static int bitlen;
33static uchar dout[MAX_SPI_BYTES];
34static uchar din[MAX_SPI_BYTES];
e3093091 35
df3b23ae
SG
36static int do_spi_xfer(int bus, int cs)
37{
38 struct spi_slave *slave;
1157a161 39 int ret = 0;
df3b23ae 40
56c40460 41#if CONFIG_IS_ENABLED(DM_SPI)
1157a161
SG
42 char name[30], *str;
43 struct udevice *dev;
44
45 snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
46 str = strdup(name);
9caeb26c
PF
47 if (!str)
48 return -ENOMEM;
61708bb0
PC
49 ret = _spi_get_bus_and_cs(bus, cs, freq, mode, "spi_generic_drv",
50 str, &dev, &slave);
1157a161
SG
51 if (ret)
52 return ret;
53#else
6954756a 54 slave = spi_setup_slave(bus, cs, freq, mode);
df3b23ae
SG
55 if (!slave) {
56 printf("Invalid device %d:%d\n", bus, cs);
57 return -EINVAL;
58 }
1157a161 59#endif
df3b23ae 60
1157a161
SG
61 ret = spi_claim_bus(slave);
62 if (ret)
63 goto done;
64 ret = spi_xfer(slave, bitlen, dout, din,
65 SPI_XFER_BEGIN | SPI_XFER_END);
56c40460 66#if !CONFIG_IS_ENABLED(DM_SPI)
1157a161
SG
67 /* We don't get an error code in this case */
68 if (ret)
69 ret = -EIO;
70#endif
71 if (ret) {
72 printf("Error %d during SPI transaction\n", ret);
df3b23ae
SG
73 } else {
74 int j;
75
76 for (j = 0; j < ((bitlen + 7) / 8); j++)
77 printf("%02X", din[j]);
78 printf("\n");
79 }
1157a161 80done:
df3b23ae 81 spi_release_bus(slave);
56c40460 82#if !CONFIG_IS_ENABLED(DM_SPI)
df3b23ae 83 spi_free_slave(slave);
1157a161 84#endif
df3b23ae 85
1157a161 86 return ret;
df3b23ae
SG
87}
88
e3093091
WD
89/*
90 * SPI read/write
91 *
92 * Syntax:
93 * spi {dev} {num_bits} {dout}
94 * {dev} is the device number for controlling chip select (see TBD)
95 * {num_bits} is the number of bits to send & receive (base 10)
96 * {dout} is a hexadecimal string of data to send
97 * The command prints out the hexadecimal string received via SPI.
98 */
99
09140113 100int do_spi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
e3093091
WD
101{
102 char *cp = 0;
103 uchar tmp;
104 int j;
e3093091
WD
105
106 /*
107 * We use the last specified parameters, unless new ones are
108 * entered.
109 */
6954756a
MV
110 if (freq == 0)
111 freq = 1000000;
e3093091
WD
112
113 if ((flag & CMD_FLAG_REPEAT) == 0)
114 {
c40e021b 115 if (argc < 2)
116 return CMD_RET_USAGE;
117
21032b35
RM
118 if (argc >= 2) {
119 mode = CONFIG_DEFAULT_SPI_MODE;
0b1284eb 120 bus = dectoul(argv[1], &cp);
21032b35 121 if (*cp == ':') {
0b1284eb 122 cs = dectoul(cp + 1, &cp);
21032b35
RM
123 } else {
124 cs = bus;
125 bus = CONFIG_DEFAULT_SPI_BUS;
126 }
2d5e7c7a 127 if (*cp == '.')
0b1284eb 128 mode = dectoul(cp + 1, &cp);
6954756a 129 if (*cp == '@')
0b1284eb 130 freq = dectoul(cp + 1, &cp);
21032b35 131 }
e3093091 132 if (argc >= 3)
0b1284eb 133 bitlen = dectoul(argv[2], NULL);
eb9401e3
WD
134 if (argc >= 4) {
135 cp = argv[3];
136 for(j = 0; *cp; j++, cp++) {
137 tmp = *cp - '0';
138 if(tmp > 9)
139 tmp -= ('A' - '0') - 10;
140 if(tmp > 15)
141 tmp -= ('a' - 'A');
142 if(tmp > 15) {
21032b35 143 printf("Hex conversion error on %c\n", *cp);
eb9401e3
WD
144 return 1;
145 }
146 if((j % 2) == 0)
147 dout[j / 2] = (tmp << 4);
148 else
149 dout[j / 2] |= tmp;
e3093091 150 }
e3093091
WD
151 }
152 }
153
eb9401e3 154 if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
21032b35 155 printf("Invalid bitlen %d\n", bitlen);
eb9401e3 156 return 1;
8bde7f77 157 }
eb9401e3 158
df3b23ae 159 if (do_spi_xfer(bus, cs))
d255bb0e 160 return 1;
e3093091 161
df3b23ae 162 return 0;
e3093091
WD
163}
164
8bde7f77
WD
165/***************************************************/
166
0d498393
WD
167U_BOOT_CMD(
168 sspi, 5, 1, do_spi,
21032b35 169 "SPI utility command",
6954756a 170 "[<bus>:]<cs>[.<mode>][@<freq>] <bit_len> <dout> - Send and receive bits\n"
21032b35
RM
171 "<bus> - Identifies the SPI bus\n"
172 "<cs> - Identifies the chip select\n"
173 "<mode> - Identifies the SPI mode to use\n"
6954756a 174 "<freq> - Identifies the SPI bus frequency in Hz\n"
8bde7f77 175 "<bit_len> - Number of bits to send (base 10)\n"
a89c33db 176 "<dout> - Hexadecimal string that gets sent"
8bde7f77 177);
This page took 0.573769 seconds and 4 git commands to generate.