]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_spi.c
ccf9cc2879deeb46d912ade20bd79b91b390bb07
[karo-tx-uboot.git] / common / cmd_spi.c
1 /*
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>
31 #include <cmd_spi.h>
32
33 #if (CONFIG_COMMANDS & CFG_CMD_SPI)
34
35 #define MAX_SPI_BYTES   32      /* max number of bytes we can handle */
36
37 /*
38  * External table of chip select functions (see the appropriate board
39  * support for the actual definition of the table).
40  */
41 extern spi_chipsel_type spi_chipsel[];
42
43
44 /*
45  * Values from last command.
46  */
47 static int   device;
48 static int   bitlen;
49 static uchar dout[MAX_SPI_BYTES];
50 static uchar din[MAX_SPI_BYTES];
51
52 /*
53  * SPI read/write
54  *
55  * Syntax:
56  *   spi {dev} {num_bits} {dout}
57  *     {dev} is the device number for controlling chip select (see TBD)
58  *     {num_bits} is the number of bits to send & receive (base 10)
59  *     {dout} is a hexadecimal string of data to send
60  * The command prints out the hexadecimal string received via SPI.
61  */
62
63 int do_spi (cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
64 {
65         char  *cp = 0;
66         uchar tmp;
67         int   j;
68         int   rcode = 0;
69
70         /*
71          * We use the last specified parameters, unless new ones are
72          * entered.
73          */
74
75         if ((flag & CMD_FLAG_REPEAT) == 0)
76         {
77                 if (argc >= 2)
78                         device = simple_strtoul(argv[1], NULL, 10);
79                 if (argc >= 3)
80                         bitlen = simple_strtoul(argv[2], NULL, 10);
81                 if (argc >= 4)
82                 cp = argv[3];
83                 for(j = 0; *cp; j++, cp++) {
84                         tmp = *cp - '0';
85                         if(tmp > 9)
86                                 tmp -= ('A' - '0') - 10;
87                         if(tmp > 15)
88                                 tmp -= ('a' - 'A');
89                         if(tmp > 15) {
90                                 printf("Conversion error on %c, bailing out.\n", *cp);
91                                 break;
92                         }
93                         if((j % 2) == 0)
94                                 dout[j / 2] = (tmp << 4);
95                         else
96                                 dout[j / 2] |= tmp;
97                 }
98         }
99
100 printf("spi_chipsel[%d] = %08X\n", device, (uint)spi_chipsel[device]);
101         if(spi_xfer(spi_chipsel[device], bitlen, dout, din) != 0) {
102                 printf("Error with the SPI transaction.\n");
103                 rcode = 1;
104         } else {
105                 cp = din;
106                 for(j = 0; j < ((bitlen + 7) / 8); j++) {
107                         printf("%02X", *cp++);
108                 }
109                 printf("\n");
110         }
111
112         return rcode;
113 }
114
115 #endif  /* CFG_CMD_SPI */
116