blob: 9a2edcff65cafaaaaaa19ad42d6b3bd1ff563a79 [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
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020025#ifndef CONFIG_DEFAULT_SPI_BUS
26# define CONFIG_DEFAULT_SPI_BUS 0
27#endif
28#ifndef CONFIG_DEFAULT_SPI_MODE
29# define CONFIG_DEFAULT_SPI_MODE SPI_MODE_0
30#endif
wdenke3093092002-10-01 01:07:28 +000031
32/*
33 * Values from last command.
34 */
Reinhard Meyer7d398e92010-08-26 10:57:27 +020035static unsigned int bus;
36static unsigned int cs;
37static unsigned int mode;
Haavard Skinnemoend74084a2008-05-16 11:10:31 +020038static int bitlen;
39static uchar dout[MAX_SPI_BYTES];
40static uchar din[MAX_SPI_BYTES];
wdenke3093092002-10-01 01:07:28 +000041
Simon Glass6a0b0192014-09-15 06:33:22 -060042static int do_spi_xfer(int bus, int cs)
43{
44 struct spi_slave *slave;
Simon Glass3cc94b12014-10-13 23:41:56 -060045 int ret = 0;
Simon Glass6a0b0192014-09-15 06:33:22 -060046
Simon Glass3cc94b12014-10-13 23:41:56 -060047#ifdef CONFIG_DM_SPI
48 char name[30], *str;
49 struct udevice *dev;
50
51 snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
52 str = strdup(name);
Peng Fane349f942016-03-20 21:21:36 +080053 if (!str)
54 return -ENOMEM;
Simon Glass3cc94b12014-10-13 23:41:56 -060055 ret = spi_get_bus_and_cs(bus, cs, 1000000, mode, "spi_generic_drv",
56 str, &dev, &slave);
57 if (ret)
58 return ret;
59#else
Simon Glass6a0b0192014-09-15 06:33:22 -060060 slave = spi_setup_slave(bus, cs, 1000000, mode);
61 if (!slave) {
62 printf("Invalid device %d:%d\n", bus, cs);
63 return -EINVAL;
64 }
Simon Glass3cc94b12014-10-13 23:41:56 -060065#endif
Simon Glass6a0b0192014-09-15 06:33:22 -060066
Simon Glass3cc94b12014-10-13 23:41:56 -060067 ret = spi_claim_bus(slave);
68 if (ret)
69 goto done;
70 ret = spi_xfer(slave, bitlen, dout, din,
71 SPI_XFER_BEGIN | SPI_XFER_END);
72#ifndef CONFIG_DM_SPI
73 /* We don't get an error code in this case */
74 if (ret)
75 ret = -EIO;
76#endif
77 if (ret) {
78 printf("Error %d during SPI transaction\n", ret);
Simon Glass6a0b0192014-09-15 06:33:22 -060079 } else {
80 int j;
81
82 for (j = 0; j < ((bitlen + 7) / 8); j++)
83 printf("%02X", din[j]);
84 printf("\n");
85 }
Simon Glass3cc94b12014-10-13 23:41:56 -060086done:
Simon Glass6a0b0192014-09-15 06:33:22 -060087 spi_release_bus(slave);
Simon Glass3cc94b12014-10-13 23:41:56 -060088#ifndef CONFIG_DM_SPI
Simon Glass6a0b0192014-09-15 06:33:22 -060089 spi_free_slave(slave);
Simon Glass3cc94b12014-10-13 23:41:56 -060090#endif
Simon Glass6a0b0192014-09-15 06:33:22 -060091
Simon Glass3cc94b12014-10-13 23:41:56 -060092 return ret;
Simon Glass6a0b0192014-09-15 06:33:22 -060093}
94
wdenke3093092002-10-01 01:07:28 +000095/*
96 * SPI read/write
97 *
98 * Syntax:
99 * spi {dev} {num_bits} {dout}
100 * {dev} is the device number for controlling chip select (see TBD)
101 * {num_bits} is the number of bits to send & receive (base 10)
102 * {dout} is a hexadecimal string of data to send
103 * The command prints out the hexadecimal string received via SPI.
104 */
105
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200106int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke3093092002-10-01 01:07:28 +0000107{
108 char *cp = 0;
109 uchar tmp;
110 int j;
wdenke3093092002-10-01 01:07:28 +0000111
112 /*
113 * We use the last specified parameters, unless new ones are
114 * entered.
115 */
116
117 if ((flag & CMD_FLAG_REPEAT) == 0)
118 {
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200119 if (argc >= 2) {
120 mode = CONFIG_DEFAULT_SPI_MODE;
121 bus = simple_strtoul(argv[1], &cp, 10);
122 if (*cp == ':') {
123 cs = simple_strtoul(cp+1, &cp, 10);
124 } else {
125 cs = bus;
126 bus = CONFIG_DEFAULT_SPI_BUS;
127 }
Marek Vasut8af6a6a2012-08-01 15:12:15 +0200128 if (*cp == '.')
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200129 mode = simple_strtoul(cp+1, NULL, 10);
130 }
wdenke3093092002-10-01 01:07:28 +0000131 if (argc >= 3)
132 bitlen = simple_strtoul(argv[2], NULL, 10);
wdenkc217f6d2002-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 Meyer7d398e92010-08-26 10:57:27 +0200142 printf("Hex conversion error on %c\n", *cp);
wdenkc217f6d2002-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
wdenkc217f6d2002-11-11 02:11:37 +0000153 if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200154 printf("Invalid bitlen %d\n", bitlen);
wdenkc217f6d2002-11-11 02:11:37 +0000155 return 1;
wdenk57b2d802003-06-27 21:31:46 +0000156 }
wdenkc217f6d2002-11-11 02:11:37 +0000157
Simon Glass6a0b0192014-09-15 06:33:22 -0600158 if (do_spi_xfer(bus, cs))
Haavard Skinnemoend74084a2008-05-16 11:10:31 +0200159 return 1;
wdenke3093092002-10-01 01:07:28 +0000160
Simon Glass6a0b0192014-09-15 06:33:22 -0600161 return 0;
wdenke3093092002-10-01 01:07:28 +0000162}
163
wdenk57b2d802003-06-27 21:31:46 +0000164/***************************************************/
165
wdenkf287a242003-07-01 21:06:45 +0000166U_BOOT_CMD(
167 sspi, 5, 1, do_spi,
Reinhard Meyer7d398e92010-08-26 10:57:27 +0200168 "SPI utility command",
169 "[<bus>:]<cs>[.<mode>] <bit_len> <dout> - Send and receive bits\n"
170 "<bus> - Identifies the SPI bus\n"
171 "<cs> - Identifies the chip select\n"
172 "<mode> - Identifies the SPI mode to use\n"
wdenk57b2d802003-06-27 21:31:46 +0000173 "<bit_len> - Number of bits to send (base 10)\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200174 "<dout> - Hexadecimal string that gets sent"
wdenk57b2d802003-06-27 21:31:46 +0000175);