blob: ea30c854c2188974b2086502d94dd44950bd90a9 [file] [log] [blame]
Tom Rini83d290c2018-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
wdenke3093092002-10-01 01:07:28 +000011#include <command.h>
Simon Glass1157a162014-10-13 23:41:56 -060012#include <dm.h>
Simon Glassdf3b23a2014-09-15 06:33:22 -060013#include <errno.h>
wdenke3093092002-10-01 01:07:28 +000014#include <spi.h>
wdenke3093092002-10-01 01:07:28 +000015
wdenkeb9401e2002-11-11 02:11:37 +000016/*-----------------------------------------------------------------------
17 * Definitions
18 */
19
20#ifndef MAX_SPI_BYTES
21# define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
22#endif
wdenke3093092002-10-01 01:07:28 +000023
wdenke3093092002-10-01 01:07:28 +000024/*
25 * Values from last command.
26 */
Reinhard Meyer21032b32010-08-26 10:57:27 +020027static unsigned int bus;
28static unsigned int cs;
29static unsigned int mode;
Marek Vasut69547562019-12-20 12:44:57 +010030static unsigned int freq;
Wolfgang Denk0cf207e2021-09-27 17:42:39 +020031static int bitlen;
32static uchar dout[MAX_SPI_BYTES];
33static uchar din[MAX_SPI_BYTES];
wdenke3093092002-10-01 01:07:28 +000034
Simon Glassdf3b23a2014-09-15 06:33:22 -060035static int do_spi_xfer(int bus, int cs)
36{
37 struct spi_slave *slave;
Simon Glass1157a162014-10-13 23:41:56 -060038 int ret = 0;
Simon Glassdf3b23a2014-09-15 06:33:22 -060039
Lukasz Majewski56c40462020-06-04 23:11:53 +080040#if CONFIG_IS_ENABLED(DM_SPI)
Simon Glass1157a162014-10-13 23:41:56 -060041 char name[30], *str;
42 struct udevice *dev;
43
44 snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
45 str = strdup(name);
Peng Fan9caeb262016-03-20 21:21:36 +080046 if (!str)
47 return -ENOMEM;
Patrice Chotard61708bb2022-03-30 09:33:13 +020048 ret = _spi_get_bus_and_cs(bus, cs, freq, mode, "spi_generic_drv",
49 str, &dev, &slave);
Simon Glass1157a162014-10-13 23:41:56 -060050 if (ret)
51 return ret;
52#else
Marek Vasut69547562019-12-20 12:44:57 +010053 slave = spi_setup_slave(bus, cs, freq, mode);
Simon Glassdf3b23a2014-09-15 06:33:22 -060054 if (!slave) {
55 printf("Invalid device %d:%d\n", bus, cs);
56 return -EINVAL;
57 }
Simon Glass1157a162014-10-13 23:41:56 -060058#endif
Simon Glassdf3b23a2014-09-15 06:33:22 -060059
Simon Glass1157a162014-10-13 23:41:56 -060060 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);
Lukasz Majewski56c40462020-06-04 23:11:53 +080065#if !CONFIG_IS_ENABLED(DM_SPI)
Simon Glass1157a162014-10-13 23:41:56 -060066 /* 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);
Simon Glassdf3b23a2014-09-15 06:33:22 -060072 } else {
73 int j;
74
75 for (j = 0; j < ((bitlen + 7) / 8); j++)
76 printf("%02X", din[j]);
77 printf("\n");
78 }
Simon Glass1157a162014-10-13 23:41:56 -060079done:
Simon Glassdf3b23a2014-09-15 06:33:22 -060080 spi_release_bus(slave);
Lukasz Majewski56c40462020-06-04 23:11:53 +080081#if !CONFIG_IS_ENABLED(DM_SPI)
Simon Glassdf3b23a2014-09-15 06:33:22 -060082 spi_free_slave(slave);
Simon Glass1157a162014-10-13 23:41:56 -060083#endif
Simon Glassdf3b23a2014-09-15 06:33:22 -060084
Simon Glass1157a162014-10-13 23:41:56 -060085 return ret;
Simon Glassdf3b23a2014-09-15 06:33:22 -060086}
87
wdenke3093092002-10-01 01:07:28 +000088/*
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
Simon Glass09140112020-05-10 11:40:03 -060099int do_spi(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
wdenke3093092002-10-01 01:07:28 +0000100{
101 char *cp = 0;
102 uchar tmp;
103 int j;
wdenke3093092002-10-01 01:07:28 +0000104
105 /*
106 * We use the last specified parameters, unless new ones are
107 * entered.
108 */
Marek Vasut69547562019-12-20 12:44:57 +0100109 if (freq == 0)
110 freq = 1000000;
wdenke3093092002-10-01 01:07:28 +0000111
112 if ((flag & CMD_FLAG_REPEAT) == 0)
113 {
chenzhipengc40e0212022-12-06 17:24:38 +0800114 if (argc < 2)
115 return CMD_RET_USAGE;
116
Reinhard Meyer21032b32010-08-26 10:57:27 +0200117 if (argc >= 2) {
118 mode = CONFIG_DEFAULT_SPI_MODE;
Simon Glass0b1284e2021-07-24 09:03:30 -0600119 bus = dectoul(argv[1], &cp);
Reinhard Meyer21032b32010-08-26 10:57:27 +0200120 if (*cp == ':') {
Simon Glass0b1284e2021-07-24 09:03:30 -0600121 cs = dectoul(cp + 1, &cp);
Reinhard Meyer21032b32010-08-26 10:57:27 +0200122 } else {
123 cs = bus;
124 bus = CONFIG_DEFAULT_SPI_BUS;
125 }
Marek Vasut2d5e7c72012-08-01 15:12:15 +0200126 if (*cp == '.')
Simon Glass0b1284e2021-07-24 09:03:30 -0600127 mode = dectoul(cp + 1, &cp);
Marek Vasut69547562019-12-20 12:44:57 +0100128 if (*cp == '@')
Simon Glass0b1284e2021-07-24 09:03:30 -0600129 freq = dectoul(cp + 1, &cp);
Reinhard Meyer21032b32010-08-26 10:57:27 +0200130 }
wdenke3093092002-10-01 01:07:28 +0000131 if (argc >= 3)
Simon Glass0b1284e2021-07-24 09:03:30 -0600132 bitlen = dectoul(argv[2], NULL);
wdenkeb9401e2002-11-11 02:11:37 +0000133 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) {
Reinhard Meyer21032b32010-08-26 10:57:27 +0200142 printf("Hex conversion error on %c\n", *cp);
wdenkeb9401e2002-11-11 02:11:37 +0000143 return 1;
144 }
145 if((j % 2) == 0)
146 dout[j / 2] = (tmp << 4);
147 else
148 dout[j / 2] |= tmp;
wdenke3093092002-10-01 01:07:28 +0000149 }
wdenke3093092002-10-01 01:07:28 +0000150 }
151 }
152
wdenkeb9401e2002-11-11 02:11:37 +0000153 if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
Reinhard Meyer21032b32010-08-26 10:57:27 +0200154 printf("Invalid bitlen %d\n", bitlen);
wdenkeb9401e2002-11-11 02:11:37 +0000155 return 1;
wdenk8bde7f72003-06-27 21:31:46 +0000156 }
wdenkeb9401e2002-11-11 02:11:37 +0000157
Simon Glassdf3b23a2014-09-15 06:33:22 -0600158 if (do_spi_xfer(bus, cs))
Haavard Skinnemoend255bb02008-05-16 11:10:31 +0200159 return 1;
wdenkeb9401e2002-11-11 02:11:37 +0000160
Simon Glassdf3b23a2014-09-15 06:33:22 -0600161 return 0;
wdenke3093092002-10-01 01:07:28 +0000162}
163
wdenk8bde7f72003-06-27 21:31:46 +0000164/***************************************************/
165
wdenk0d498392003-07-01 21:06:45 +0000166U_BOOT_CMD(
167 sspi, 5, 1, do_spi,
Reinhard Meyer21032b32010-08-26 10:57:27 +0200168 "SPI utility command",
Marek Vasut69547562019-12-20 12:44:57 +0100169 "[<bus>:]<cs>[.<mode>][@<freq>] <bit_len> <dout> - Send and receive bits\n"
Reinhard Meyer21032b32010-08-26 10:57:27 +0200170 "<bus> - Identifies the SPI bus\n"
171 "<cs> - Identifies the chip select\n"
172 "<mode> - Identifies the SPI mode to use\n"
Marek Vasut69547562019-12-20 12:44:57 +0100173 "<freq> - Identifies the SPI bus frequency in Hz\n"
wdenk8bde7f72003-06-27 21:31:46 +0000174 "<bit_len> - Number of bits to send (base 10)\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200175 "<dout> - Hexadecimal string that gets sent"
wdenk8bde7f72003-06-27 21:31:46 +0000176);