blob: 4969811f3b28dbe451d4cd3b989f1cb01d3cd917 [file] [log] [blame]
wdenkef893942004-02-23 16:11:30 +00001/*
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 */
51static unsigned ace_readw(unsigned offset)
52{
53 return readw(CFG_SYSTEMACE_BASE+offset);
54}
55
wdenk2cbc8732004-04-25 16:40:11 +000056static void ace_writew(unsigned val, unsigned offset)
wdenkef893942004-02-23 16:11:30 +000057{
58 writew(val, CFG_SYSTEMACE_BASE+offset);
59}
60
61/* */
62
63static unsigned long systemace_read(int dev,
64 unsigned long start,
65 unsigned long blkcnt,
66 unsigned long *buffer);
67
68static block_dev_desc_t systemace_dev = {0};
69
70static 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
90static void release_cf_lock(void)
91{
92 /* CONTROLREG = none */
93 ace_writew(0x0000, 0x18);
94}
95
96block_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 */
117static unsigned long systemace_read(int dev,
118 unsigned long start,
119 unsigned long blkcnt,
120 unsigned long *buffer)
121{
wdenkef893942004-02-23 16:11:30 +0000122 int retry;
wdenk372f0302004-02-27 08:21:54 +0000123 unsigned blk_countdown;
wdenkef893942004-02-23 16:11:30 +0000124 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
wdenk372f0302004-02-27 08:21:54 +0000139#ifdef DEBUG_SYSTEMACE
140 printf("... systemace read %lu sectors at %lu\n", blkcnt, start);
141#endif
142
wdenkef893942004-02-23 16:11:30 +0000143 retry = 2000;
144 for (;;) {
145 unsigned val = ace_readw(0x04);
146
147 /* If CFDETECT is false, card is missing. */
148 if (! (val & 0x0010)) {
149 printf("**** ACE CompactFlash not found.\n");
150 release_cf_lock();
151 return 0;
152 }
153
154 /* If RDYFORCMD, then we are ready to go. */
155 if (val & 0x0100)
156 break;
157
158 if (retry < 0) {
159 printf("**** SystemACE not ready.\n");
160 release_cf_lock();
161 return 0;
162 }
163
164 udelay(1000);
165 retry -= 1;
166 }
167
wdenk372f0302004-02-27 08:21:54 +0000168 /* The SystemACE can only transfer 256 sectors at a time, so
169 limit the current chunk of sectors. The blk_countdown
170 variable is the number of sectors left to transfer. */
wdenkef893942004-02-23 16:11:30 +0000171
wdenk372f0302004-02-27 08:21:54 +0000172 blk_countdown = blkcnt;
173 while (blk_countdown > 0) {
174 unsigned trans = blk_countdown;
wdenkef893942004-02-23 16:11:30 +0000175
wdenk372f0302004-02-27 08:21:54 +0000176 if (trans > 256) trans = 256;
wdenkef893942004-02-23 16:11:30 +0000177
wdenk372f0302004-02-27 08:21:54 +0000178#ifdef DEBUG_SYSTEMACE
179 printf("... transfer %lu sector in a chunk\n", trans);
180#endif
181 /* Write LBA block address */
182 ace_writew((start>> 0) & 0xffff, 0x10);
183 ace_writew((start>>16) & 0x00ff, 0x12);
wdenkef893942004-02-23 16:11:30 +0000184
wdenk372f0302004-02-27 08:21:54 +0000185 /* NOTE: in the Write Sector count below, a count of 0
186 causes a transfer of 256, so &0xff gives the right
187 value for whatever transfer count we want. */
188
189 /* Write sector count | ReadMemCardData. */
190 ace_writew((trans&0xff) | 0x0300, 0x14);
191
wdenk372f0302004-02-27 08:21:54 +0000192 retry = trans * 16;
193 while (retry > 0) {
194 int idx;
195
196 /* Wait for buffer to become ready. */
197 while (! (ace_readw(0x04) & 0x0020)) {
wdenkeec9a3d2004-03-23 23:20:24 +0000198 udelay(100);
wdenk372f0302004-02-27 08:21:54 +0000199 }
200
201 /* Read 16 words of 2bytes from the sector buffer. */
202 for (idx = 0 ; idx < 16 ; idx += 1) {
203 unsigned short val = ace_readw(0x40);
204 *dp++ = val & 0xff;
205 *dp++ = (val>>8) & 0xff;
206 }
207
208 retry -= 1;
wdenkef893942004-02-23 16:11:30 +0000209 }
210
wdenk372f0302004-02-27 08:21:54 +0000211 /* Count the blocks we transfer this time. */
212 start += trans;
213 blk_countdown -= trans;
wdenkef893942004-02-23 16:11:30 +0000214 }
215
216 release_cf_lock();
217
218 return blkcnt;
219}
wdenk2cbc8732004-04-25 16:40:11 +0000220#endif /* CONFIG_SYSTEMACE */