blob: 76044221416c9fbd194828b6f4c6bd35fdd461fd [file] [log] [blame]
wdenke3093092002-10-01 01:07:28 +00001/*
2 * (C) Copyright 2002
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * SPI Read/Write Utilities
26 */
27
28#include <common.h>
29#include <command.h>
30#include <spi.h>
wdenke3093092002-10-01 01:07:28 +000031
wdenkeb9401e2002-11-11 02:11:37 +000032/*-----------------------------------------------------------------------
33 * Definitions
34 */
35
36#ifndef MAX_SPI_BYTES
37# define MAX_SPI_BYTES 32 /* Maximum number of bytes we can handle */
38#endif
wdenke3093092002-10-01 01:07:28 +000039
40/*
41 * External table of chip select functions (see the appropriate board
42 * support for the actual definition of the table).
43 */
44extern spi_chipsel_type spi_chipsel[];
wdenkeb9401e2002-11-11 02:11:37 +000045extern int spi_chipsel_cnt;
wdenke3093092002-10-01 01:07:28 +000046
47/*
48 * Values from last command.
49 */
50static int device;
51static int bitlen;
52static uchar dout[MAX_SPI_BYTES];
53static uchar din[MAX_SPI_BYTES];
54
55/*
56 * SPI read/write
57 *
58 * Syntax:
59 * spi {dev} {num_bits} {dout}
60 * {dev} is the device number for controlling chip select (see TBD)
61 * {num_bits} is the number of bits to send & receive (base 10)
62 * {dout} is a hexadecimal string of data to send
63 * The command prints out the hexadecimal string received via SPI.
64 */
65
wdenkeb9401e2002-11-11 02:11:37 +000066int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenke3093092002-10-01 01:07:28 +000067{
68 char *cp = 0;
69 uchar tmp;
70 int j;
71 int rcode = 0;
72
73 /*
74 * We use the last specified parameters, unless new ones are
75 * entered.
76 */
77
78 if ((flag & CMD_FLAG_REPEAT) == 0)
79 {
80 if (argc >= 2)
81 device = simple_strtoul(argv[1], NULL, 10);
82 if (argc >= 3)
83 bitlen = simple_strtoul(argv[2], NULL, 10);
wdenkeb9401e2002-11-11 02:11:37 +000084 if (argc >= 4) {
85 cp = argv[3];
86 for(j = 0; *cp; j++, cp++) {
87 tmp = *cp - '0';
88 if(tmp > 9)
89 tmp -= ('A' - '0') - 10;
90 if(tmp > 15)
91 tmp -= ('a' - 'A');
92 if(tmp > 15) {
93 printf("Hex conversion error on %c, giving up.\n", *cp);
94 return 1;
95 }
96 if((j % 2) == 0)
97 dout[j / 2] = (tmp << 4);
98 else
99 dout[j / 2] |= tmp;
wdenke3093092002-10-01 01:07:28 +0000100 }
wdenke3093092002-10-01 01:07:28 +0000101 }
102 }
103
wdenkeb9401e2002-11-11 02:11:37 +0000104 if ((device < 0) || (device >= spi_chipsel_cnt)) {
105 printf("Invalid device %d, giving up.\n", device);
106 return 1;
wdenk8bde7f72003-06-27 21:31:46 +0000107 }
wdenkeb9401e2002-11-11 02:11:37 +0000108 if ((bitlen < 0) || (bitlen > (MAX_SPI_BYTES * 8))) {
109 printf("Invalid bitlen %d, giving up.\n", bitlen);
110 return 1;
wdenk8bde7f72003-06-27 21:31:46 +0000111 }
wdenkeb9401e2002-11-11 02:11:37 +0000112
113 debug ("spi_chipsel[%d] = %08X\n",
114 device, (uint)spi_chipsel[device]);
115
wdenke3093092002-10-01 01:07:28 +0000116 if(spi_xfer(spi_chipsel[device], bitlen, dout, din) != 0) {
117 printf("Error with the SPI transaction.\n");
118 rcode = 1;
119 } else {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200120 cp = (char *)din;
wdenke3093092002-10-01 01:07:28 +0000121 for(j = 0; j < ((bitlen + 7) / 8); j++) {
122 printf("%02X", *cp++);
123 }
124 printf("\n");
125 }
126
127 return rcode;
128}
129
wdenk8bde7f72003-06-27 21:31:46 +0000130/***************************************************/
131
wdenk0d498392003-07-01 21:06:45 +0000132U_BOOT_CMD(
133 sspi, 5, 1, do_spi,
wdenk206c60c2003-09-18 10:02:25 +0000134 "sspi - SPI utility commands\n",
wdenk8bde7f72003-06-27 21:31:46 +0000135 "<device> <bit_len> <dout> - Send <bit_len> bits from <dout> out the SPI\n"
136 "<device> - Identifies the chip select of the device\n"
137 "<bit_len> - Number of bits to send (base 10)\n"
138 "<dout> - Hexadecimal string that gets sent\n"
139);