* CVS add missing files
[oweals/u-boot.git] / common / cmd_ace.c
1 /*
2  * Copyright (c) 2004 Picture Elements, Inc.
3  *    Stephen Williams (XXXXXXXXXXXXXXXX)
4  *
5  *    This source code is free software; you can redistribute it
6  *    and/or modify it in source code form under the terms of the GNU
7  *    General Public License as published by the Free Software
8  *    Foundation; either version 2 of the License, or (at your option)
9  *    any later version.
10  *
11  *    This program is distributed in the hope that it will be useful,
12  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *    GNU General Public License for more details.
15  *
16  *    You should have received a copy of the GNU General Public License
17  *    along with this program; if not, write to the Free Software
18  *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20 #ident "$Id:$"
21
22 /*
23  * The Xilinx SystemACE chip support is activated by defining
24  * CONFIG_SYSTEMACE to turn on support, and CFG_SYSTEMACE_BASE
25  * to set the base address of the device. This code currently
26  * assumes that the chip is connected via a byte-wide bus.
27  *
28  * The CONFIG_SYSTEMACE also adds to fat support the device class
29  * "ace" that allows the user to execute "fatls ace 0" and the
30  * like. This works by making the systemace_get_dev function
31  * available to cmd_fat.c:get_dev and filling in a block device
32  * description that has all the bits needed for FAT support to
33  * read sectors.
34  */
35
36 # include  <common.h>
37 # include  <command.h>
38 # include  <systemace.h>
39 # include  <part.h>
40 # include  <asm/io.h>
41
42 #ifdef CONFIG_SYSTEMACE
43
44 /*
45  * The ace_readw and writew functions read/write 16bit words, but the
46  * offset value is the BYTE offset as most used in the Xilinx
47  * datasheet for the SystemACE chip. The CFG_SYSTEMACE_BASE is defined
48  * to be the base address for the chip, usually in the local
49  * peripheral bus.
50  */
51 static unsigned ace_readw(unsigned offset)
52 {
53       return readw(CFG_SYSTEMACE_BASE+offset);
54 }
55
56 static unsigned ace_writew(unsigned val, unsigned offset)
57 {
58       writew(val, CFG_SYSTEMACE_BASE+offset);
59 }
60
61 /* */
62
63 static unsigned long systemace_read(int dev,
64                                     unsigned long start,
65                                     unsigned long blkcnt,
66                                     unsigned long *buffer);
67
68 static block_dev_desc_t systemace_dev = {0};
69
70 static int get_cf_lock(void)
71 {
72       int retry = 10;
73
74         /* CONTROLREG = LOCKREG */
75       ace_writew(0x0002, 0x18);
76
77         /* Wait for MPULOCK in STATUSREG[15:0] */
78       while (! (ace_readw(0x04) & 0x0002)) {
79
80             if (retry < 0)
81                   return -1;
82
83             udelay(100000);
84             retry -= 1;
85       }
86
87       return 0;
88 }
89
90 static void release_cf_lock(void)
91 {
92         /* CONTROLREG = none */
93       ace_writew(0x0000, 0x18);
94 }
95
96 block_dev_desc_t *  systemace_get_dev(int dev)
97 {
98         /* The first time through this, the systemace_dev object is
99            not yet initialized. In that case, fill it in. */
100       if (systemace_dev.blksz == 0) {
101             systemace_dev.if_type   = IF_TYPE_UNKNOWN;
102             systemace_dev.part_type = PART_TYPE_UNKNOWN;
103             systemace_dev.type      = DEV_TYPE_HARDDISK;
104             systemace_dev.blksz     = 512;
105             systemace_dev.removable = 1;
106             systemace_dev.block_read = systemace_read;
107       }
108
109       return &systemace_dev;
110 }
111
112 /*
113  * This function is called (by dereferencing the block_read pointer in
114  * the dev_desc) to read blocks of data. The return value is the
115  * number of blocks read. A zero return indicates an error.
116  */
117 static unsigned long systemace_read(int dev,
118                                     unsigned long start,
119                                     unsigned long blkcnt,
120                                     unsigned long *buffer)
121 {
122       unsigned val;
123       int retry;
124       unsigned char*dp = (unsigned char*)buffer;
125
126       if (get_cf_lock() < 0) {
127             unsigned status = ace_readw(0x04);
128
129               /* If CFDETECT is false, card is missing. */
130             if (! (status&0x0010)) {
131                   printf("** CompactFlash card not present. **\n");
132                   return 0;
133             }
134
135             printf("**** ACE locked away from me (STATUSREG=%04x)\n", status);
136             return 0;
137       }
138
139       retry = 2000;
140       for (;;) {
141             unsigned val = ace_readw(0x04);
142
143               /* If CFDETECT is false, card is missing. */
144             if (! (val & 0x0010)) {
145                   printf("**** ACE CompactFlash not found.\n");
146                   release_cf_lock();
147                   return 0;
148             }
149
150               /* If RDYFORCMD, then we are ready to go. */
151             if (val & 0x0100)
152                   break;
153
154             if (retry < 0) {
155                   printf("**** SystemACE not ready.\n");
156                   release_cf_lock();
157                   return 0;
158             }
159
160             udelay(1000);
161             retry -= 1;
162       }
163
164         /* Write LBA block address */
165       ace_writew(start & 0xffff, 0x10);
166       start >>= 16;
167       ace_writew(start & 0xff, 0x12);
168
169         /* Write sector count | ReadMemCardData. */
170       ace_writew(blkcnt | 0x0300, 0x14);
171
172         /* CONTROLREG = CFGRESET|LOCKREQ */
173       ace_writew(0x0082, 0x18);
174
175       retry = blkcnt * 16;
176       while (retry > 0) {
177             int idx;
178
179               /* Wait for buffer to become ready. */
180             while (! (ace_readw(0x04) & 0x0020)) {
181                   udelay(1000);
182             }
183
184               /* Read 16 words of 2bytes from the sector buffer. */
185             for (idx = 0 ;  idx < 16 ;  idx += 1) {
186                   unsigned short val = ace_readw(0x40);
187                   *dp++ = val & 0xff;
188                   *dp++ = (val>>8) & 0xff;
189             }
190
191             retry -= 1;
192       }
193
194       release_cf_lock();
195
196       return blkcnt;
197 }
198
199
200 #endif
201
202 /*
203  * $Log: $
204  */
205