xref: /OK3568_Linux_fs/u-boot/drivers/block/systemace.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (c) 2004 Picture Elements, Inc.
3*4882a593Smuzhiyun  *    Stephen Williams (XXXXXXXXXXXXXXXX)
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  * The Xilinx SystemACE chip support is activated by defining
10*4882a593Smuzhiyun  * CONFIG_SYSTEMACE to turn on support, and CONFIG_SYS_SYSTEMACE_BASE
11*4882a593Smuzhiyun  * to set the base address of the device. This code currently
12*4882a593Smuzhiyun  * assumes that the chip is connected via a byte-wide bus.
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The CONFIG_SYSTEMACE also adds to fat support the device class
15*4882a593Smuzhiyun  * "ace" that allows the user to execute "fatls ace 0" and the
16*4882a593Smuzhiyun  * like. This works by making the systemace_get_dev function
17*4882a593Smuzhiyun  * available to cmd_fat.c:get_dev and filling in a block device
18*4882a593Smuzhiyun  * description that has all the bits needed for FAT support to
19*4882a593Smuzhiyun  * read sectors.
20*4882a593Smuzhiyun  *
21*4882a593Smuzhiyun  * According to Xilinx technical support, before accessing the
22*4882a593Smuzhiyun  * SystemACE CF you need to set the following control bits:
23*4882a593Smuzhiyun  *      FORCECFGMODE : 1
24*4882a593Smuzhiyun  *      CFGMODE : 0
25*4882a593Smuzhiyun  *      CFGSTART : 0
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include <common.h>
29*4882a593Smuzhiyun #include <command.h>
30*4882a593Smuzhiyun #include <dm.h>
31*4882a593Smuzhiyun #include <part.h>
32*4882a593Smuzhiyun #include <asm/io.h>
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /*
35*4882a593Smuzhiyun  * The ace_readw and writew functions read/write 16bit words, but the
36*4882a593Smuzhiyun  * offset value is the BYTE offset as most used in the Xilinx
37*4882a593Smuzhiyun  * datasheet for the SystemACE chip. The CONFIG_SYS_SYSTEMACE_BASE is defined
38*4882a593Smuzhiyun  * to be the base address for the chip, usually in the local
39*4882a593Smuzhiyun  * peripheral bus.
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun static u32 base = CONFIG_SYS_SYSTEMACE_BASE;
43*4882a593Smuzhiyun static u32 width = CONFIG_SYS_SYSTEMACE_WIDTH;
44*4882a593Smuzhiyun 
ace_writew(u16 val,unsigned off)45*4882a593Smuzhiyun static void ace_writew(u16 val, unsigned off)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	if (width == 8) {
48*4882a593Smuzhiyun #if !defined(__BIG_ENDIAN)
49*4882a593Smuzhiyun 		writeb(val >> 8, base + off);
50*4882a593Smuzhiyun 		writeb(val, base + off + 1);
51*4882a593Smuzhiyun #else
52*4882a593Smuzhiyun 		writeb(val, base + off);
53*4882a593Smuzhiyun 		writeb(val >> 8, base + off + 1);
54*4882a593Smuzhiyun #endif
55*4882a593Smuzhiyun 	} else
56*4882a593Smuzhiyun 		out16(base + off, val);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
ace_readw(unsigned off)59*4882a593Smuzhiyun static u16 ace_readw(unsigned off)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun 	if (width == 8) {
62*4882a593Smuzhiyun #if !defined(__BIG_ENDIAN)
63*4882a593Smuzhiyun 		return (readb(base + off) << 8) | readb(base + off + 1);
64*4882a593Smuzhiyun #else
65*4882a593Smuzhiyun 		return readb(base + off) | (readb(base + off + 1) << 8);
66*4882a593Smuzhiyun #endif
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	return in16(base + off);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun #ifndef CONFIG_BLK
73*4882a593Smuzhiyun static struct blk_desc systemace_dev = { 0 };
74*4882a593Smuzhiyun #endif
75*4882a593Smuzhiyun 
get_cf_lock(void)76*4882a593Smuzhiyun static int get_cf_lock(void)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	int retry = 10;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	/* CONTROLREG = LOCKREG */
81*4882a593Smuzhiyun 	unsigned val = ace_readw(0x18);
82*4882a593Smuzhiyun 	val |= 0x0002;
83*4882a593Smuzhiyun 	ace_writew((val & 0xffff), 0x18);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	/* Wait for MPULOCK in STATUSREG[15:0] */
86*4882a593Smuzhiyun 	while (!(ace_readw(0x04) & 0x0002)) {
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 		if (retry < 0)
89*4882a593Smuzhiyun 			return -1;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 		udelay(100000);
92*4882a593Smuzhiyun 		retry -= 1;
93*4882a593Smuzhiyun 	}
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	return 0;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
release_cf_lock(void)98*4882a593Smuzhiyun static void release_cf_lock(void)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	unsigned val = ace_readw(0x18);
101*4882a593Smuzhiyun 	val &= ~(0x0002);
102*4882a593Smuzhiyun 	ace_writew((val & 0xffff), 0x18);
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun  * This function is called (by dereferencing the block_read pointer in
107*4882a593Smuzhiyun  * the dev_desc) to read blocks of data. The return value is the
108*4882a593Smuzhiyun  * number of blocks read. A zero return indicates an error.
109*4882a593Smuzhiyun  */
110*4882a593Smuzhiyun #ifdef CONFIG_BLK
systemace_read(struct udevice * dev,unsigned long start,lbaint_t blkcnt,void * buffer)111*4882a593Smuzhiyun static unsigned long systemace_read(struct udevice *dev, unsigned long start,
112*4882a593Smuzhiyun 				    lbaint_t blkcnt, void *buffer)
113*4882a593Smuzhiyun #else
114*4882a593Smuzhiyun static unsigned long systemace_read(struct blk_desc *block_dev,
115*4882a593Smuzhiyun 				    unsigned long start, lbaint_t blkcnt,
116*4882a593Smuzhiyun 				    void *buffer)
117*4882a593Smuzhiyun #endif
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	int retry;
120*4882a593Smuzhiyun 	unsigned blk_countdown;
121*4882a593Smuzhiyun 	unsigned char *dp = buffer;
122*4882a593Smuzhiyun 	unsigned val;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	if (get_cf_lock() < 0) {
125*4882a593Smuzhiyun 		unsigned status = ace_readw(0x04);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		/* If CFDETECT is false, card is missing. */
128*4882a593Smuzhiyun 		if (!(status & 0x0010)) {
129*4882a593Smuzhiyun 			printf("** CompactFlash card not present. **\n");
130*4882a593Smuzhiyun 			return 0;
131*4882a593Smuzhiyun 		}
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 		printf("**** ACE locked away from me (STATUSREG=%04x)\n",
134*4882a593Smuzhiyun 		       status);
135*4882a593Smuzhiyun 		return 0;
136*4882a593Smuzhiyun 	}
137*4882a593Smuzhiyun #ifdef DEBUG_SYSTEMACE
138*4882a593Smuzhiyun 	printf("... systemace read %lu sectors at %lu\n", blkcnt, start);
139*4882a593Smuzhiyun #endif
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	retry = 2000;
142*4882a593Smuzhiyun 	for (;;) {
143*4882a593Smuzhiyun 		val = ace_readw(0x04);
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 		/* If CFDETECT is false, card is missing. */
146*4882a593Smuzhiyun 		if (!(val & 0x0010)) {
147*4882a593Smuzhiyun 			printf("**** ACE CompactFlash not found.\n");
148*4882a593Smuzhiyun 			release_cf_lock();
149*4882a593Smuzhiyun 			return 0;
150*4882a593Smuzhiyun 		}
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 		/* If RDYFORCMD, then we are ready to go. */
153*4882a593Smuzhiyun 		if (val & 0x0100)
154*4882a593Smuzhiyun 			break;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 		if (retry < 0) {
157*4882a593Smuzhiyun 			printf("**** SystemACE not ready.\n");
158*4882a593Smuzhiyun 			release_cf_lock();
159*4882a593Smuzhiyun 			return 0;
160*4882a593Smuzhiyun 		}
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 		udelay(1000);
163*4882a593Smuzhiyun 		retry -= 1;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	/* The SystemACE can only transfer 256 sectors at a time, so
167*4882a593Smuzhiyun 	   limit the current chunk of sectors. The blk_countdown
168*4882a593Smuzhiyun 	   variable is the number of sectors left to transfer. */
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	blk_countdown = blkcnt;
171*4882a593Smuzhiyun 	while (blk_countdown > 0) {
172*4882a593Smuzhiyun 		unsigned trans = blk_countdown;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 		if (trans > 256)
175*4882a593Smuzhiyun 			trans = 256;
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun #ifdef DEBUG_SYSTEMACE
178*4882a593Smuzhiyun 		printf("... transfer %lu sector in a chunk\n", trans);
179*4882a593Smuzhiyun #endif
180*4882a593Smuzhiyun 		/* Write LBA block address */
181*4882a593Smuzhiyun 		ace_writew((start >> 0) & 0xffff, 0x10);
182*4882a593Smuzhiyun 		ace_writew((start >> 16) & 0x0fff, 0x12);
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 		/* NOTE: in the Write Sector count below, a count of 0
185*4882a593Smuzhiyun 		   causes a transfer of 256, so &0xff gives the right
186*4882a593Smuzhiyun 		   value for whatever transfer count we want. */
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 		/* Write sector count | ReadMemCardData. */
189*4882a593Smuzhiyun 		ace_writew((trans & 0xff) | 0x0300, 0x14);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun  * For FPGA configuration via SystemACE is reset unacceptable
193*4882a593Smuzhiyun  * CFGDONE bit in STATUSREG is not set to 1.
194*4882a593Smuzhiyun  */
195*4882a593Smuzhiyun #ifndef SYSTEMACE_CONFIG_FPGA
196*4882a593Smuzhiyun 		/* Reset the configruation controller */
197*4882a593Smuzhiyun 		val = ace_readw(0x18);
198*4882a593Smuzhiyun 		val |= 0x0080;
199*4882a593Smuzhiyun 		ace_writew(val, 0x18);
200*4882a593Smuzhiyun #endif
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 		retry = trans * 16;
203*4882a593Smuzhiyun 		while (retry > 0) {
204*4882a593Smuzhiyun 			int idx;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 			/* Wait for buffer to become ready. */
207*4882a593Smuzhiyun 			while (!(ace_readw(0x04) & 0x0020)) {
208*4882a593Smuzhiyun 				udelay(100);
209*4882a593Smuzhiyun 			}
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 			/* Read 16 words of 2bytes from the sector buffer. */
212*4882a593Smuzhiyun 			for (idx = 0; idx < 16; idx += 1) {
213*4882a593Smuzhiyun 				unsigned short val = ace_readw(0x40);
214*4882a593Smuzhiyun 				*dp++ = val & 0xff;
215*4882a593Smuzhiyun 				*dp++ = (val >> 8) & 0xff;
216*4882a593Smuzhiyun 			}
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 			retry -= 1;
219*4882a593Smuzhiyun 		}
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 		/* Clear the configruation controller reset */
222*4882a593Smuzhiyun 		val = ace_readw(0x18);
223*4882a593Smuzhiyun 		val &= ~0x0080;
224*4882a593Smuzhiyun 		ace_writew(val, 0x18);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 		/* Count the blocks we transfer this time. */
227*4882a593Smuzhiyun 		start += trans;
228*4882a593Smuzhiyun 		blk_countdown -= trans;
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	release_cf_lock();
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	return blkcnt;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun #ifdef CONFIG_BLK
systemace_bind(struct udevice * dev)237*4882a593Smuzhiyun static int systemace_bind(struct udevice *dev)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	struct blk_desc *bdesc;
240*4882a593Smuzhiyun 	struct udevice *bdev;
241*4882a593Smuzhiyun 	int ret;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	ret = blk_create_devicef(dev, "systemace_blk", "blk", IF_TYPE_SYSTEMACE,
244*4882a593Smuzhiyun 				 -1, 512, 0, &bdev);
245*4882a593Smuzhiyun 	if (ret) {
246*4882a593Smuzhiyun 		debug("Cannot create block device\n");
247*4882a593Smuzhiyun 		return ret;
248*4882a593Smuzhiyun 	}
249*4882a593Smuzhiyun 	bdesc = dev_get_uclass_platdata(bdev);
250*4882a593Smuzhiyun 	bdesc->removable = 1;
251*4882a593Smuzhiyun 	bdesc->part_type = PART_TYPE_UNKNOWN;
252*4882a593Smuzhiyun 	bdesc->log2blksz = LOG2(bdesc->blksz);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	/* Ensure the correct bus mode (8/16 bits) gets enabled */
255*4882a593Smuzhiyun 	ace_writew(width == 8 ? 0 : 0x0001, 0);
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	return 0;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun static const struct blk_ops systemace_blk_ops = {
261*4882a593Smuzhiyun 	.read	= systemace_read,
262*4882a593Smuzhiyun };
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun U_BOOT_DRIVER(systemace_blk) = {
265*4882a593Smuzhiyun 	.name		= "systemace_blk",
266*4882a593Smuzhiyun 	.id		= UCLASS_BLK,
267*4882a593Smuzhiyun 	.ops		= &systemace_blk_ops,
268*4882a593Smuzhiyun 	.bind		= systemace_bind,
269*4882a593Smuzhiyun };
270*4882a593Smuzhiyun #else
systemace_get_dev(int dev,struct blk_desc ** descp)271*4882a593Smuzhiyun static int systemace_get_dev(int dev, struct blk_desc **descp)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	/* The first time through this, the systemace_dev object is
274*4882a593Smuzhiyun 	   not yet initialized. In that case, fill it in. */
275*4882a593Smuzhiyun 	if (systemace_dev.blksz == 0) {
276*4882a593Smuzhiyun 		systemace_dev.if_type = IF_TYPE_UNKNOWN;
277*4882a593Smuzhiyun 		systemace_dev.devnum = 0;
278*4882a593Smuzhiyun 		systemace_dev.part_type = PART_TYPE_UNKNOWN;
279*4882a593Smuzhiyun 		systemace_dev.type = DEV_TYPE_HARDDISK;
280*4882a593Smuzhiyun 		systemace_dev.blksz = 512;
281*4882a593Smuzhiyun 		systemace_dev.log2blksz = LOG2(systemace_dev.blksz);
282*4882a593Smuzhiyun 		systemace_dev.removable = 1;
283*4882a593Smuzhiyun 		systemace_dev.block_read = systemace_read;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 		/*
286*4882a593Smuzhiyun 		 * Ensure the correct bus mode (8/16 bits) gets enabled
287*4882a593Smuzhiyun 		 */
288*4882a593Smuzhiyun 		ace_writew(width == 8 ? 0 : 0x0001, 0);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 		part_init(&systemace_dev);
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 	*descp = &systemace_dev;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	return 0;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun U_BOOT_LEGACY_BLK(systemace) = {
298*4882a593Smuzhiyun 	.if_typename	= "ace",
299*4882a593Smuzhiyun 	.if_type	= IF_TYPE_SYSTEMACE,
300*4882a593Smuzhiyun 	.max_devs	= 1,
301*4882a593Smuzhiyun 	.get_dev	= systemace_get_dev,
302*4882a593Smuzhiyun };
303*4882a593Smuzhiyun #endif
304