wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 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 | { |
wdenk | 57bfdd4 | 2004-09-29 22:55:14 +0000 | [diff] [blame] | 53 | #if (CFG_SYSTEMACE_WIDTH == 8) |
| 54 | u16 temp; |
| 55 | |
| 56 | #if !defined(__BIG_ENDIAN) |
| 57 | temp =((u16)readb(CFG_SYSTEMACE_BASE+offset) << 8); |
| 58 | temp |= (u16)readb(CFG_SYSTEMACE_BASE+offset+1); |
| 59 | #else |
| 60 | temp = (u16)readb(CFG_SYSTEMACE_BASE+offset); |
| 61 | temp |=((u16)readb(CFG_SYSTEMACE_BASE+offset+1) << 8); |
| 62 | #endif |
| 63 | return temp; |
| 64 | #else |
| 65 | return readw(CFG_SYSTEMACE_BASE+offset); |
| 66 | #endif |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 67 | } |
| 68 | |
wdenk | 2cbc873 | 2004-04-25 16:40:11 +0000 | [diff] [blame] | 69 | static void ace_writew(unsigned val, unsigned offset) |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 70 | { |
wdenk | 57bfdd4 | 2004-09-29 22:55:14 +0000 | [diff] [blame] | 71 | #if (CFG_SYSTEMACE_WIDTH == 8) |
| 72 | #if !defined(__BIG_ENDIAN) |
| 73 | writeb((u8)(val>>8), CFG_SYSTEMACE_BASE+offset); |
| 74 | writeb((u8)val, CFG_SYSTEMACE_BASE+offset+1); |
| 75 | #else |
| 76 | writeb((u8)val, CFG_SYSTEMACE_BASE+offset); |
| 77 | writeb((u8)(val>>8), CFG_SYSTEMACE_BASE+offset+1); |
| 78 | #endif |
| 79 | #else |
| 80 | writew(val, CFG_SYSTEMACE_BASE+offset); |
| 81 | #endif |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* */ |
| 85 | |
| 86 | static unsigned long systemace_read(int dev, |
| 87 | unsigned long start, |
| 88 | unsigned long blkcnt, |
| 89 | unsigned long *buffer); |
| 90 | |
| 91 | static block_dev_desc_t systemace_dev = {0}; |
| 92 | |
| 93 | static int get_cf_lock(void) |
| 94 | { |
| 95 | int retry = 10; |
| 96 | |
| 97 | /* CONTROLREG = LOCKREG */ |
| 98 | ace_writew(0x0002, 0x18); |
| 99 | |
| 100 | /* Wait for MPULOCK in STATUSREG[15:0] */ |
| 101 | while (! (ace_readw(0x04) & 0x0002)) { |
| 102 | |
| 103 | if (retry < 0) |
| 104 | return -1; |
| 105 | |
| 106 | udelay(100000); |
| 107 | retry -= 1; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static void release_cf_lock(void) |
| 114 | { |
| 115 | /* CONTROLREG = none */ |
| 116 | ace_writew(0x0000, 0x18); |
| 117 | } |
| 118 | |
| 119 | block_dev_desc_t * systemace_get_dev(int dev) |
| 120 | { |
| 121 | /* The first time through this, the systemace_dev object is |
| 122 | not yet initialized. In that case, fill it in. */ |
| 123 | if (systemace_dev.blksz == 0) { |
| 124 | systemace_dev.if_type = IF_TYPE_UNKNOWN; |
| 125 | systemace_dev.part_type = PART_TYPE_UNKNOWN; |
| 126 | systemace_dev.type = DEV_TYPE_HARDDISK; |
| 127 | systemace_dev.blksz = 512; |
| 128 | systemace_dev.removable = 1; |
| 129 | systemace_dev.block_read = systemace_read; |
| 130 | } |
| 131 | |
| 132 | return &systemace_dev; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * This function is called (by dereferencing the block_read pointer in |
| 137 | * the dev_desc) to read blocks of data. The return value is the |
| 138 | * number of blocks read. A zero return indicates an error. |
| 139 | */ |
| 140 | static unsigned long systemace_read(int dev, |
| 141 | unsigned long start, |
| 142 | unsigned long blkcnt, |
| 143 | unsigned long *buffer) |
| 144 | { |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 145 | int retry; |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 146 | unsigned blk_countdown; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 147 | unsigned char*dp = (unsigned char*)buffer; |
| 148 | |
| 149 | if (get_cf_lock() < 0) { |
| 150 | unsigned status = ace_readw(0x04); |
| 151 | |
| 152 | /* If CFDETECT is false, card is missing. */ |
| 153 | if (! (status&0x0010)) { |
| 154 | printf("** CompactFlash card not present. **\n"); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | printf("**** ACE locked away from me (STATUSREG=%04x)\n", status); |
| 159 | return 0; |
| 160 | } |
| 161 | |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 162 | #ifdef DEBUG_SYSTEMACE |
| 163 | printf("... systemace read %lu sectors at %lu\n", blkcnt, start); |
| 164 | #endif |
| 165 | |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 166 | retry = 2000; |
| 167 | for (;;) { |
| 168 | unsigned val = ace_readw(0x04); |
| 169 | |
| 170 | /* If CFDETECT is false, card is missing. */ |
| 171 | if (! (val & 0x0010)) { |
| 172 | printf("**** ACE CompactFlash not found.\n"); |
| 173 | release_cf_lock(); |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | /* If RDYFORCMD, then we are ready to go. */ |
| 178 | if (val & 0x0100) |
| 179 | break; |
| 180 | |
| 181 | if (retry < 0) { |
| 182 | printf("**** SystemACE not ready.\n"); |
| 183 | release_cf_lock(); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | udelay(1000); |
| 188 | retry -= 1; |
| 189 | } |
| 190 | |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 191 | /* The SystemACE can only transfer 256 sectors at a time, so |
| 192 | limit the current chunk of sectors. The blk_countdown |
| 193 | variable is the number of sectors left to transfer. */ |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 194 | |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 195 | blk_countdown = blkcnt; |
| 196 | while (blk_countdown > 0) { |
| 197 | unsigned trans = blk_countdown; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 198 | |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 199 | if (trans > 256) trans = 256; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 200 | |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 201 | #ifdef DEBUG_SYSTEMACE |
| 202 | printf("... transfer %lu sector in a chunk\n", trans); |
| 203 | #endif |
| 204 | /* Write LBA block address */ |
| 205 | ace_writew((start>> 0) & 0xffff, 0x10); |
| 206 | ace_writew((start>>16) & 0x00ff, 0x12); |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 207 | |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 208 | /* NOTE: in the Write Sector count below, a count of 0 |
| 209 | causes a transfer of 256, so &0xff gives the right |
| 210 | value for whatever transfer count we want. */ |
| 211 | |
| 212 | /* Write sector count | ReadMemCardData. */ |
| 213 | ace_writew((trans&0xff) | 0x0300, 0x14); |
| 214 | |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 215 | retry = trans * 16; |
| 216 | while (retry > 0) { |
| 217 | int idx; |
| 218 | |
| 219 | /* Wait for buffer to become ready. */ |
| 220 | while (! (ace_readw(0x04) & 0x0020)) { |
wdenk | eec9a3d | 2004-03-23 23:20:24 +0000 | [diff] [blame] | 221 | udelay(100); |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* Read 16 words of 2bytes from the sector buffer. */ |
| 225 | for (idx = 0 ; idx < 16 ; idx += 1) { |
| 226 | unsigned short val = ace_readw(0x40); |
| 227 | *dp++ = val & 0xff; |
| 228 | *dp++ = (val>>8) & 0xff; |
| 229 | } |
| 230 | |
| 231 | retry -= 1; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 232 | } |
| 233 | |
wdenk | 372f030 | 2004-02-27 08:21:54 +0000 | [diff] [blame] | 234 | /* Count the blocks we transfer this time. */ |
| 235 | start += trans; |
| 236 | blk_countdown -= trans; |
wdenk | ef89394 | 2004-02-23 16:11:30 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | release_cf_lock(); |
| 240 | |
| 241 | return blkcnt; |
| 242 | } |
wdenk | 2cbc873 | 2004-04-25 16:40:11 +0000 | [diff] [blame] | 243 | #endif /* CONFIG_SYSTEMACE */ |