* Patch by Travis Sawyer, 26 Feb 2004:
[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 blk_countdown;
125       unsigned char*dp = (unsigned char*)buffer;
126
127       if (get_cf_lock() < 0) {
128             unsigned status = ace_readw(0x04);
129
130               /* If CFDETECT is false, card is missing. */
131             if (! (status&0x0010)) {
132                   printf("** CompactFlash card not present. **\n");
133                   return 0;
134             }
135
136             printf("**** ACE locked away from me (STATUSREG=%04x)\n", status);
137             return 0;
138       }
139
140 #ifdef DEBUG_SYSTEMACE
141       printf("... systemace read %lu sectors at %lu\n", blkcnt, start);
142 #endif
143
144       retry = 2000;
145       for (;;) {
146             unsigned val = ace_readw(0x04);
147
148               /* If CFDETECT is false, card is missing. */
149             if (! (val & 0x0010)) {
150                   printf("**** ACE CompactFlash not found.\n");
151                   release_cf_lock();
152                   return 0;
153             }
154
155               /* If RDYFORCMD, then we are ready to go. */
156             if (val & 0x0100)
157                   break;
158
159             if (retry < 0) {
160                   printf("**** SystemACE not ready.\n");
161                   release_cf_lock();
162                   return 0;
163             }
164
165             udelay(1000);
166             retry -= 1;
167       }
168
169         /* The SystemACE can only transfer 256 sectors at a time, so
170            limit the current chunk of sectors. The blk_countdown
171            variable is the number of sectors left to transfer. */
172
173       blk_countdown = blkcnt;
174       while (blk_countdown > 0) {
175             unsigned trans = blk_countdown;
176
177             if (trans > 256) trans = 256;
178
179 #ifdef DEBUG_SYSTEMACE
180             printf("... transfer %lu sector in a chunk\n", trans);
181 #endif
182               /* Write LBA block address */
183             ace_writew((start>> 0) & 0xffff, 0x10);
184             ace_writew((start>>16) & 0x00ff, 0x12);
185
186               /* NOTE: in the Write Sector count below, a count of 0
187                  causes a transfer of 256, so &0xff gives the right
188                  value for whatever transfer count we want. */
189
190               /* Write sector count | ReadMemCardData. */
191             ace_writew((trans&0xff) | 0x0300, 0x14);
192
193               /* CONTROLREG = CFGRESET|LOCKREQ */
194             ace_writew(0x0082, 0x18);
195
196             retry = trans * 16;
197             while (retry > 0) {
198                   int idx;
199
200                     /* Wait for buffer to become ready. */
201                   while (! (ace_readw(0x04) & 0x0020)) {
202                         udelay(1000);
203                   }
204
205                     /* Read 16 words of 2bytes from the sector buffer. */
206                   for (idx = 0 ;  idx < 16 ;  idx += 1) {
207                         unsigned short val = ace_readw(0x40);
208                         *dp++ = val & 0xff;
209                         *dp++ = (val>>8) & 0xff;
210                   }
211
212                   retry -= 1;
213             }
214
215               /* Count the blocks we transfer this time. */
216             start += trans;
217             blk_countdown -= trans;
218       }
219
220       release_cf_lock();
221
222       return blkcnt;
223 }
224 #endif