blob: 454ebe37d7547a1ac38e6275bee47729904c0461 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenke3093092002-10-01 01:07:28 +00002/*
3 * (C) Copyright 2002
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
wdenke3093092002-10-01 01:07:28 +00005 */
6
7/*
8 * SPI Read/Write Utilities
9 */
10
11#include <common.h>
12#include <command.h>
Simon Glass3cc94b12014-10-13 23:41:56 -060013#include <dm.h>
Simon Glass6a0b0192014-09-15 06:33:22 -060014#include <errno.h>
wdenke3093092002-10-01 01:07:28 +000015#include <spi.h>
wdenke3093092002-10-01 01:07:28 +000016
wdenkc217f6d2002-11-11 02:11:37 +000017/*-----------------------------------------------------------------------
18 * Definitions
19 */
20
21#ifndef MAX_SPI_BYTES
22# define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
23#endif
wdenke3093092002-10-01 01:07:28 +000024
wdenke3093092002-10-01 01:07:28 +000025/*
26 * Values from last command.
27 */
Reinhard Meyer7d398e92010-08-26 10:57:27 +020028static unsigned int bus;
29static unsigned int cs;
30static unsigned int mode;
Marek Vasut67e20fe2019-12-20 12:44:57 +010031static unsigned int freq;
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020032static int bitlen;
33static uchar dout[MAX_SPI_BYTES];
34static uchar din[MAX_SPI_BYTES];
wdenke3093092002-10-01 01:07:28 +000035
Simon Glass6a0b0192014-09-15 06:33:22 -060036static int do_spi_xfer(int bus, int cs)
37{
38 struct spi_slave *slave;
Simon Glass3cc94b12014-10-13 23:41:56 -060039 int ret = 0;
Simon Glass6a0b0192014-09-15 06:33:22 -060040
Lukasz Majewski76f442982020-06-04 23:11:53 +080041#if CONFIG_IS_ENABLED(DM_SPI)
Simon Glass3cc94b12014-10-13 23:41:56 -060042 char name[30], *str;
43 struct udevice *dev;
44
45 snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
46 str = strdup(name);
Peng Fane349f942016-03-20 21:21:36 +080047 if (!str)
48 return -ENOMEM;
Patrice Chotard86e06ae2022-03-30 09:33:13 +020049 ret = _spi_get_bus_and_cs(bus, cs, freq, mode, "spi_generic_drv",
50 str, &dev, &slave);
Simon Glass3cc94b12014-10-13 23:41:56 -060051 if (ret)
52 return ret;
53#else
Marek Vasut67e20fe2019-12-20 12:44:57 +010054 slave = spi_setup_slave(bus, cs, freq, mode);
Simon Glass6a0b0192014-09-15 06:33:22 -060055 if (!slave) {
56 printf("Invalid device %d:%d\n", bus, cs);
57 return -EINVAL;
58 }
Simon Glass3cc94b12014-10-13 23:41:56 -060059#endif
Simon Glass6a0b0192014-09-15 06:33:22 -060060
Simon Glass3cc94b12014-10-13 23:41:56 -060061 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);
Lukasz Majewski76f442982020-06-04 23:11:53 +080066#if !CONFIG_IS_ENABLED(DM_SPI)
Simon Glass3cc94b12014-10-13 23:41:56 -060067 /* 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);
Simon Glass6a0b0192014-09-15 06:33:22 -060073 } else {
74 int j;
75
76 for (j = 0; j < ((bitlen + 7) / 8); j++)
77 printf("%02X", din[j]);
78 printf("\n");
79 }
Simon Glass3cc94b12014-10-13 23:41:56 -060080done:
Simon Glass6a0b0192014-09-15 06:33:22 -060081 spi_release_bus(slave);
Lukasz Majewski76f442982020-06-04 23:11:53 +080082#if !CONFIG_IS_ENABLED(DM_SPI)
Simon Glass6a0b0192014-09-15 06:33:22 -060083 spi_free_slave(slave);
Simon Glass3cc94b12014-10-13 23:41:56 -060084#endif
Simon Glass6a0b0192014-09-15 06:33:22 -060085
Simon Glass3cc94b12014-10-13 23:41:56 -060086 return ret;
Simon Glass6a0b0192014-09-15 06:33:22 -060087}
88
wdenke3093092002-10-01 01:07:28 +000089/*
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
Simon Glassed38aef2020-05-10 11:40:03 -0600100int do_spi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenke3093092002-10-01 01:07:28 +0000101{
102 char *cp = 0;
103 uchar tmp;
104 int j;
wdenke3093092002-10-01 01:07:28 +0000105
106 /*
107 * We use the last specified parameters, unless new ones are
108 * entered.
109 */
Marek Vasut67e20fe2019-12-20 12:44:57 +0100110 if (freq == 0)
111 freq = 1000000;
wdenke3093092002-10-01 01:07:28 +0000112
113 if ((flag & CMD_FLAG_REPEAT) == 0)
114 {
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200115 if (argc >= 2) {
116 mode = CONFIG_DEFAULT_SPI_MODE;
Simon Glassff9b9032021-07-24 09:03:30 -0600117 bus = dectoul(argv[1], &cp);
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200118 if (*cp == ':') {
Simon Glassff9b9032021-07-24 09:03:30 -0600119 cs = dectoul(cp + 1, &cp);
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200120 } else {
121 cs = bus;
122 bus = CONFIG_DEFAULT_SPI_BUS;
123 }
Marek Vasut8af6a6a2012-08-01 15:12:15 +0200124 if (*cp == '.')
Simon Glassff9b9032021-07-24 09:03:30 -0600125 mode = dectoul(cp + 1, &cp);
Marek Vasut67e20fe2019-12-20 12:44:57 +0100126 if (*cp == '@')
Simon Glassff9b9032021-07-24 09:03:30 -0600127 freq = dectoul(cp + 1, &cp);
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200128 }
wdenke3093092002-10-01 01:07:28 +0000129 if (argc >= 3)
Simon Glassff9b9032021-07-24 09:03:30 -0600130 bitlen = dectoul(argv[2], NULL);
wdenkc217f6d2002-11-11 02:11:37 +0000131 if (argc >= 4) {
132 cp = argv[3];
133 for(j = 0; *cp; j++, cp++) {
134 tmp = *cp - '0';
135 if(tmp > 9)
136 tmp -= ('A' - '0') - 10;
137 if(tmp > 15)
138 tmp -= ('a' - 'A');
139 if(tmp > 15) {
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200140 printf("Hex conversion error on %c\n", *cp);
wdenkc217f6d2002-11-11 02:11:37 +0000141 return 1;
142 }
143 if((j % 2) == 0)
144 dout[j / 2] = (tmp << 4);
145 else
146 dout[j / 2] |= tmp;
wdenke3093092002-10-01 01:07:28 +0000147 }
wdenke3093092002-10-01 01:07:28 +0000148 }
149 }
150
wdenkc217f6d2002-11-11 02:11:37 +0000151 if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200152 printf("Invalid bitlen %d\n", bitlen);
wdenkc217f6d2002-11-11 02:11:37 +0000153 return 1;
wdenk57b2d802003-06-27 21:31:46 +0000154 }
wdenkc217f6d2002-11-11 02:11:37 +0000155
Simon Glass6a0b0192014-09-15 06:33:22 -0600156 if (do_spi_xfer(bus, cs))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200157 return 1;
wdenke3093092002-10-01 01:07:28 +0000158
Simon Glass6a0b0192014-09-15 06:33:22 -0600159 return 0;
wdenke3093092002-10-01 01:07:28 +0000160}
161
wdenk57b2d802003-06-27 21:31:46 +0000162/***************************************************/
163
wdenkf287a242003-07-01 21:06:45 +0000164U_BOOT_CMD(
165 sspi, 5, 1, do_spi,
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200166 "SPI utility command",
Marek Vasut67e20fe2019-12-20 12:44:57 +0100167 "[<bus>:]<cs>[.<mode>][@<freq>] <bit_len> <dout> - Send and receive bits\n"
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200168 "<bus> - Identifies the SPI bus\n"
169 "<cs> - Identifies the chip select\n"
170 "<mode> - Identifies the SPI mode to use\n"
Marek Vasut67e20fe2019-12-20 12:44:57 +0100171 "<freq> - Identifies the SPI bus frequency in Hz\n"
wdenk57b2d802003-06-27 21:31:46 +0000172 "<bit_len> - Number of bits to send (base 10)\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200173 "<dout> - Hexadecimal string that gets sent"
wdenk57b2d802003-06-27 21:31:46 +0000174);