Home | History | Annotate | Line # | Download | only in nor
cfi.c revision 1.5
      1 /*	$NetBSD: cfi.c,v 1.5 2011/07/23 07:17:34 cliff Exp $	*/
      2 /*-
      3  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Cliff Neighbors.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "opt_flash.h"
     32 #include "opt_nor.h"
     33 #include "opt_cfi.h"
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: cfi.c,v 1.5 2011/07/23 07:17:34 cliff Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/cdefs.h>
     41 #include <sys/device.h>
     42 #include <sys/endian.h>
     43 
     44 #include <sys/bus.h>
     45 
     46 #include <dev/nor/nor.h>
     47 #include <dev/nor/cfi.h>
     48 #include <dev/nor/cfi_0002.h>
     49 
     50 
     51 static bool cfi_chip_query(struct cfi * const);
     52 static int  cfi_scan_media(device_t self, struct nor_chip *chip);
     53 static void cfi_init(device_t);
     54 static void cfi_select(device_t, bool);
     55 static void cfi_read_1(device_t, flash_off_t, uint8_t *);
     56 static void cfi_read_2(device_t, flash_off_t, uint16_t *);
     57 static void cfi_read_4(device_t, flash_off_t, uint32_t *);
     58 static void cfi_read_buf_1(device_t, flash_off_t, uint8_t *, size_t);
     59 static void cfi_read_buf_2(device_t, flash_off_t, uint16_t *, size_t);
     60 static void cfi_read_buf_4(device_t, flash_off_t, uint32_t *, size_t);
     61 static void cfi_write_1(device_t, flash_off_t, uint8_t);
     62 static void cfi_write_2(device_t, flash_off_t, uint16_t);
     63 static void cfi_write_4(device_t, flash_off_t, uint32_t);
     64 static void cfi_write_buf_1(device_t, flash_off_t, const uint8_t *, size_t);
     65 static void cfi_write_buf_2(device_t, flash_off_t, const uint16_t *, size_t);
     66 static void cfi_write_buf_4(device_t, flash_off_t, const uint32_t *, size_t);
     67 static void cfi_jedec_id_1(struct cfi * const );
     68 static void cfi_jedec_id_2(struct cfi * const );
     69 static void cfi_jedec_id_4(struct cfi * const );
     70 static bool cfi_jedec_id(struct cfi * const);
     71 static bool cfi_emulate(struct cfi * const);
     72 static const struct cfi_jedec_tab * cfi_jedec_search(struct cfi *);
     73 static void cfi_jedec_fill(struct cfi * const,
     74 	const struct cfi_jedec_tab *);
     75 #if defined(CFI_DEBUG_JEDEC) || defined(CFI_DEBUG_QRY)
     76 static void cfi_hexdump(flash_off_t, void * const, u_int, u_int);
     77 #endif
     78 
     79 
     80 
     81 /*
     82  * NOTE these opmode tables are informed by "Table 1. CFI Query Read"
     83  * in Intel "Common Flash Interface (CFI) and Command Sets"
     84  * Application Note 646, April 2000
     85  *
     86  * Assume the byte order of the flash (and of the signature there)
     87  * is the same as host byte order. The Intel App. Note describes the
     88  * little endian variant.
     89  *
     90  * XXX down-sized, interleaved & multi-chip opmodes not yet supported
     91  */
     92 
     93 #if BYTE_ORDER == BIG_ENDIAN
     94 /* BIG ENDIAN host */
     95 /* 1-byte access */
     96 static const struct cfi_opmodes cfi_opmodes_1[] = {
     97 	{ 0, 0, 0, 0x10,  3, "QRY", "x8 device operating in 8-bit mode" },
     98 };
     99 
    100 /* 2-byte access */
    101 static const struct cfi_opmodes cfi_opmodes_2[] = {
    102 	{ 1, 1, 0, 0x20,  6, "\0Q\0R\0Y",
    103 		"x16 device operating in 16-bit mode" },
    104 };
    105 
    106 /* 4-byte access */
    107 static const struct cfi_opmodes cfi_opmodes_4[] = {
    108 	{ 2, 2, 0, 0x40, 12, "\0\0\0Q\0\0\0R\0\0\0Y",
    109 		"x32 device operating in 32-bit mode" },
    110 };
    111 #else
    112 /* LITTLE ENDIAN host */
    113 /* 1-byte access */
    114 static const struct cfi_opmodes cfi_opmodes_1[] = {
    115 	{ 0, 0, 0, 0x10,  3, "QRY", "x8 device operating in 8-bit mode" },
    116 };
    117 
    118 /* 2-byte access */
    119 static const struct cfi_opmodes cfi_opmodes_2[] = {
    120 	{ 1, 1, 0, 0x20,  6, "Q\0R\0Y\0",
    121 		"x16 device operating in 16-bit mode" },
    122 };
    123 
    124 /* 4-byte access */
    125 static const struct cfi_opmodes cfi_opmodes_4[] = {
    126 	{ 2, 2, 0, 0x40, 12, "Q\0\0\0R\0\0\0Y\0\0\0",
    127 		"x32 device operating in 32-bit mode" },
    128 };
    129 #endif
    130 
    131 
    132 #define LOG2_64K	16
    133 #define LOG2_128K	17
    134 #define LOG2_256K	18
    135 #define LOG2_512K	19
    136 #define LOG2_1M		20
    137 #define LOG2_2M		21
    138 #define LOG2_4M		22
    139 #define LOG2_8M		23
    140 #define LOG2_16M	24
    141 #define LOG2_32M	25
    142 #define LOG2_64M	26
    143 #define LOG2_128M	27
    144 #define LOG2_256M	28
    145 #define LOG2_512M	29
    146 #define LOG2_1G		30
    147 #define LOG2_2G		31
    148 const struct cfi_jedec_tab cfi_jedec_tab[] = {
    149 	{
    150 		.jt_name = "Pm39LV512",
    151 		.jt_mid = 0x9d,
    152 		.jt_did = 0x1b,
    153 		.jt_id_pri = 0,				/* XXX */
    154 		.jt_id_alt = 0,				/* XXX */
    155 		.jt_device_size = LOG2_64K,
    156 		.jt_interface_code_desc = CFI_IFCODE_X8,
    157 		.jt_erase_blk_regions = 1,
    158 		.jt_erase_blk_info = {
    159 			{ 4096/256, (64/4)-1 },
    160 		},
    161 		.jt_write_word_time_typ = 40,
    162 		.jt_write_nbyte_time_typ = 0,
    163 		.jt_erase_blk_time_typ = 55,
    164 		.jt_erase_chip_time_typ = 55,
    165 		.jt_write_word_time_max = 1,
    166 		.jt_write_nbyte_time_max = 0,
    167 		.jt_erase_blk_time_max = 1,
    168 		.jt_erase_chip_time_max = 1,
    169 		.jt_opmode = &cfi_opmodes_1[0],
    170 	},
    171 	{
    172 		.jt_name = "Pm39LV010",
    173 		.jt_mid = 0x9d,
    174 		.jt_did = 0x1c,
    175 		.jt_id_pri = 0,				/* XXX */
    176 		.jt_id_alt = 0,				/* XXX */
    177 		.jt_device_size = LOG2_128K,
    178 		.jt_interface_code_desc = CFI_IFCODE_X8,
    179 		.jt_erase_blk_regions = 1,
    180 		.jt_erase_blk_info = {
    181 			{ 4096/256, (128/4)-1 },
    182 		},
    183 		.jt_write_word_time_typ = 40,
    184 		.jt_write_nbyte_time_typ = 0,
    185 		.jt_erase_blk_time_typ = 55,
    186 		.jt_erase_chip_time_typ = 55,
    187 		.jt_write_word_time_max = 1,
    188 		.jt_write_nbyte_time_max = 0,
    189 		.jt_erase_blk_time_max = 1,
    190 		.jt_erase_chip_time_max = 1,
    191 		.jt_opmode = &cfi_opmodes_1[0],
    192 	},
    193 };
    194 
    195 
    196 const struct nor_interface nor_interface_cfi = {
    197 	.scan_media = cfi_scan_media,
    198 	.init = cfi_init,
    199 	.select = cfi_select,
    200 	.read_1 = cfi_read_1,
    201 	.read_2 = cfi_read_2,
    202 	.read_4 = cfi_read_4,
    203 	.read_buf_1 = cfi_read_buf_1,
    204 	.read_buf_2 = cfi_read_buf_2,
    205 	.read_buf_4 = cfi_read_buf_4,
    206 	.write_1 = cfi_write_1,
    207 	.write_2 = cfi_write_2,
    208 	.write_4 = cfi_write_4,
    209 	.write_buf_1 = cfi_write_buf_1,
    210 	.write_buf_2 = cfi_write_buf_2,
    211 	.write_buf_4 = cfi_write_buf_4,
    212 	.read_page = NULL,			/* cmdset */
    213 	.program_page = NULL,			/* cmdset */
    214 	.busy = NULL,
    215 	.private = NULL,
    216 	.access_width = -1,
    217 	.part_info = NULL,
    218 	.part_num = -1,
    219 };
    220 
    221 
    222 /* only data[7..0] are used regardless of chip width */
    223 #define cfi_unpack_1(n)			((n) & 0xff)
    224 
    225 /* construct (arbitrarily big endian) uint16_t */
    226 #define cfi_unpack_2(b0, b1)						\
    227 	((cfi_unpack_1(b1) << 8) | cfi_unpack_1(b0))
    228 
    229 /* construct (arbitrarily) big endian uint32_t */
    230 #define cfi_unpack_4(b0, b1, b2, b3)					\
    231 	((cfi_unpack_1(b3) << 24) |					\
    232 	 (cfi_unpack_1(b2) << 16) |					\
    233 	 (cfi_unpack_1(b1) <<  8) |					\
    234 	 (cfi_unpack_1(b0)))
    235 
    236 #define cfi_unpack_qry(qryp, data)					\
    237     do {								\
    238 	(qryp)->qry[0] = cfi_unpack_1(data[0x10]);			\
    239 	(qryp)->qry[1] = cfi_unpack_1(data[0x11]);			\
    240 	(qryp)->qry[2] = cfi_unpack_1(data[0x12]);			\
    241 	(qryp)->id_pri = be16toh(cfi_unpack_2(data[0x13], data[0x14]));	\
    242 	(qryp)->addr_pri =						\
    243 		be16toh(cfi_unpack_2(data[0x15], data[0x16]));		\
    244 	(qryp)->id_alt = be16toh(cfi_unpack_2(data[0x17], data[0x18]));	\
    245 	(qryp)->addr_alt =						\
    246 		be16toh(cfi_unpack_2(data[0x19], data[0x1a]));		\
    247 	(qryp)->vcc_min = cfi_unpack_1(data[0x1b]);			\
    248 	(qryp)->vcc_max = cfi_unpack_1(data[0x1c]);			\
    249 	(qryp)->vpp_min = cfi_unpack_1(data[0x1d]);			\
    250 	(qryp)->vpp_max = cfi_unpack_1(data[0x1e]);			\
    251 	(qryp)->write_word_time_typ = cfi_unpack_1(data[0x1f]);		\
    252 	(qryp)->write_nbyte_time_typ = cfi_unpack_1(data[0x20]);	\
    253 	(qryp)->erase_blk_time_typ = cfi_unpack_1(data[0x21]);		\
    254 	(qryp)->erase_chip_time_typ = cfi_unpack_1(data[0x22]);		\
    255 	(qryp)->write_word_time_max = cfi_unpack_1(data[0x23]);		\
    256 	(qryp)->write_nbyte_time_max = cfi_unpack_1(data[0x24]);	\
    257 	(qryp)->erase_blk_time_max = cfi_unpack_1(data[0x25]);		\
    258 	(qryp)->erase_chip_time_max = cfi_unpack_1(data[0x26]);		\
    259 	(qryp)->device_size = cfi_unpack_1(data[0x27]);			\
    260 	(qryp)->interface_code_desc =					\
    261 		be16toh(cfi_unpack_2(data[0x28], data[0x29]));		\
    262 	(qryp)->write_nbyte_size_max = 					\
    263 		be16toh(cfi_unpack_2(data[0x2a], data[0x2b]));		\
    264 	(qryp)->erase_blk_regions = cfi_unpack_1(data[0x2c]);		\
    265 	u_int _i = 0x2d;						\
    266 	const u_int _n = (qryp)->erase_blk_regions;			\
    267 	KASSERT(_n <= 4);						\
    268 	for (u_int _r = 0; _r < _n; _r++, _i+=4) {			\
    269 		(qryp)->erase_blk_info[_r].y =				\
    270 			be32toh(cfi_unpack_2(data[_i+0], data[_i+1]));	\
    271 		(qryp)->erase_blk_info[_r].z =				\
    272 			be32toh(cfi_unpack_2(data[_i+2], data[_i+3]));	\
    273 	}								\
    274     } while (0)
    275 
    276 #define cfi_unpack_pri_0002(qryp, data)					\
    277     do {								\
    278 	(qryp)->pri.cmd_0002.pri[0] = cfi_unpack_1(data[0x00]);		\
    279 	(qryp)->pri.cmd_0002.pri[1] = cfi_unpack_1(data[0x01]);		\
    280 	(qryp)->pri.cmd_0002.pri[2] = cfi_unpack_1(data[0x02]);		\
    281 	(qryp)->pri.cmd_0002.version_maj = cfi_unpack_1(data[0x03]);	\
    282 	(qryp)->pri.cmd_0002.version_min = cfi_unpack_1(data[0x04]);	\
    283 	(qryp)->pri.cmd_0002.asupt = cfi_unpack_1(data[0x05]);		\
    284 	(qryp)->pri.cmd_0002.erase_susp = cfi_unpack_1(data[0x06]);	\
    285 	(qryp)->pri.cmd_0002.sector_prot = cfi_unpack_1(data[0x07]);	\
    286 	(qryp)->pri.cmd_0002.tmp_sector_unprot =			\
    287 		cfi_unpack_1(data[0x08]);				\
    288 	(qryp)->pri.cmd_0002.sector_prot_scheme =			\
    289 		cfi_unpack_1(data[0x09]);				\
    290 	(qryp)->pri.cmd_0002.simul_op = cfi_unpack_1(data[0x0a]);	\
    291 	(qryp)->pri.cmd_0002.burst_mode_type = cfi_unpack_1(data[0x0b]);\
    292 	(qryp)->pri.cmd_0002.page_mode_type = cfi_unpack_1(data[0x0c]);	\
    293 	(qryp)->pri.cmd_0002.acc_min = cfi_unpack_1(data[0x0d]);	\
    294 	(qryp)->pri.cmd_0002.acc_max = cfi_unpack_1(data[0x0e]);	\
    295 	(qryp)->pri.cmd_0002.wp_prot = cfi_unpack_1(data[0x0f]);	\
    296 	/* XXX 1.3 stops here */					\
    297 	(qryp)->pri.cmd_0002.prog_susp = cfi_unpack_1(data[0x10]);	\
    298 	(qryp)->pri.cmd_0002.unlock_bypass = cfi_unpack_1(data[0x11]);	\
    299 	(qryp)->pri.cmd_0002.sss_size = cfi_unpack_1(data[0x12]);	\
    300 	(qryp)->pri.cmd_0002.soft_feat = cfi_unpack_1(data[0x13]);	\
    301 	(qryp)->pri.cmd_0002.page_size = cfi_unpack_1(data[0x14]);	\
    302 	(qryp)->pri.cmd_0002.erase_susp_time_max =			\
    303 		cfi_unpack_1(data[0x15]);				\
    304 	(qryp)->pri.cmd_0002.prog_susp_time_max =			\
    305 		cfi_unpack_1(data[0x16]);				\
    306 	(qryp)->pri.cmd_0002.embhwrst_time_max =			\
    307 		cfi_unpack_1(data[0x38]);				\
    308 	(qryp)->pri.cmd_0002.hwrst_time_max =				\
    309 		cfi_unpack_1(data[0x39]);				\
    310     } while (0)
    311 
    312 #define CFI_QRY_UNPACK_COMMON(cfi, data, type, found)			\
    313     do {								\
    314 	struct cfi_query_data * const qryp = &cfi->cfi_qry_data;	\
    315 									\
    316 	memset(qryp, 0, sizeof(*qryp));					\
    317 	cfi_unpack_qry(qryp, data);					\
    318 									\
    319 	switch (qryp->id_pri) {						\
    320 	case 0x0002:							\
    321 		if ((cfi_unpack_1(data[qryp->addr_pri + 0]) == 'P') &&	\
    322 		    (cfi_unpack_1(data[qryp->addr_pri + 1]) == 'R') &&	\
    323 		    (cfi_unpack_1(data[qryp->addr_pri + 2]) == 'I')) {	\
    324 			type *pri_data = &data[qryp->addr_pri];		\
    325 			cfi_unpack_pri_0002(qryp, pri_data);		\
    326 			found = true;					\
    327 			break;						\
    328 		}							\
    329 	default:							\
    330 		printf("%s: unsupported id_pri=%#x\n",			\
    331 			__func__, qryp->id_pri);			\
    332 		break;	/* unknown command set */			\
    333 	}								\
    334     } while (0)
    335 
    336 #ifdef CFI_DEBUG_QRY
    337 # define CFI_DUMP_QRY(off, p, sz, stride)				\
    338     do {								\
    339 	printf("%s: QRY data\n", __func__);				\
    340 	cfi_hexdump(off, p, sz, stride);				\
    341     } while (0)
    342 #else
    343 # define CFI_DUMP_QRY(off, p, sz, stride)
    344 #endif
    345 
    346 #ifdef CFI_DEBUG_JEDEC
    347 # define CFI_DUMP_JEDEC(off, p, sz, stride)				\
    348     do {								\
    349 	printf("%s: JEDEC data\n", __func__);				\
    350 	cfi_hexdump(off, p, sz, stride);				\
    351     } while (0)
    352 #else
    353 # define CFI_DUMP_JEDEC(off, p, sz, stride)
    354 #endif
    355 
    356 
    357 /*
    358  * cfi_chip_query_opmode - determine operational mode based on QRY signature
    359  */
    360 static bool
    361 cfi_chip_query_opmode(struct cfi *cfi, uint8_t *data,
    362     const struct cfi_opmodes *tab, u_int nentries)
    363 {
    364 	for (u_int i=0; i < nentries; i++) {
    365 		if (memcmp(&data[tab[i].qsa], tab[i].sig, tab[i].len) == 0) {
    366 			cfi->cfi_opmode = &tab[i];
    367 			return true;
    368 		}
    369 	}
    370 	return false;
    371 }
    372 
    373 static bool
    374 cfi_chip_query_1(struct cfi * const cfi)
    375 {
    376 	uint8_t data[0x80];
    377 
    378 	bus_space_read_region_1(cfi->cfi_bst, cfi->cfi_bsh, 0, data,
    379 		__arraycount(data));
    380 
    381 	CFI_DUMP_QRY(0, data, sizeof(data), 1);
    382 
    383 	bool found = cfi_chip_query_opmode(cfi, data, cfi_opmodes_1,
    384 		__arraycount(cfi_opmodes_1));
    385 
    386 	if (found) {
    387 		CFI_QRY_UNPACK_COMMON(cfi, data, uint8_t, found);
    388 	}
    389 
    390 	return found;
    391 }
    392 
    393 static bool
    394 cfi_chip_query_2(struct cfi * const cfi)
    395 {
    396 	uint16_t data[0x80];
    397 
    398 	bus_space_read_region_2(cfi->cfi_bst, cfi->cfi_bsh, 0, data,
    399 		__arraycount(data));
    400 
    401 	CFI_DUMP_QRY(0, data, sizeof(data), 2);
    402 
    403 	bool found = cfi_chip_query_opmode(cfi, (uint8_t *)data,
    404 		cfi_opmodes_2, __arraycount(cfi_opmodes_2));
    405 
    406 	if (found) {
    407 		CFI_QRY_UNPACK_COMMON(cfi, data, uint16_t, found);
    408 	}
    409 
    410 	return found;
    411 }
    412 
    413 static bool
    414 cfi_chip_query_4(struct cfi * const cfi)
    415 {
    416 	uint32_t data[0x80];
    417 
    418 	bus_space_read_region_4(cfi->cfi_bst, cfi->cfi_bsh, 0, data,
    419 		__arraycount(data));
    420 
    421 	CFI_DUMP_QRY(0, data, sizeof(data), 4);
    422 
    423 	bool found = cfi_chip_query_opmode(cfi, (uint8_t *)data,
    424 		cfi_opmodes_4, __arraycount(cfi_opmodes_4));
    425 
    426 	if (found) {
    427 		CFI_QRY_UNPACK_COMMON(cfi, data, uint32_t, found);
    428 	}
    429 
    430 	return found;
    431 }
    432 
    433 static bool
    434 cfi_chip_query_8(struct cfi * const cfi)
    435 {
    436 #ifdef NOTYET
    437 	uint64_t data[0x80];
    438 
    439 	bus_space_read_region_8(cfi->cfi_bst, cfi->cfi_bsh, 0, data,
    440 		__arraycount(data));
    441 
    442 	CFI_DUMP_QRY(0, data, sizeof(data), 8);
    443 
    444 	bool found = cfi_chip_query_opmode(cfi, (uint8_t *)data,
    445 		cfi_opmodes_8, __arraycount(cfi_opmodes_8));
    446 
    447 	if (found) {
    448 		CFI_QRY_UNPACK_COMMON(cfi, data, uint64_t, found);
    449 	}
    450 
    451 	return found;
    452 #else
    453 	return false;
    454 #endif
    455 }
    456 
    457 /*
    458  * cfi_chip_query - detect a CFI chip
    459  *
    460  * fill in the struct cfi as we discover what's there
    461  */
    462 static bool
    463 cfi_chip_query(struct cfi * const cfi)
    464 {
    465 	bool found = false;
    466 	const bus_size_t cfi_query_offset[] = {
    467 		CFI_QUERY_MODE_ADDRESS,
    468 		CFI_QUERY_MODE_ALT_ADDRESS
    469 	};
    470 
    471 	KASSERT(cfi != NULL);
    472 	KASSERT(cfi->cfi_bst != NULL);
    473 
    474 	for (int j=0; !found && j < __arraycount(cfi_query_offset); j++) {
    475 
    476 		cfi_reset_default(cfi);
    477 		cfi_cmd(cfi, cfi_query_offset[j], CFI_QUERY_DATA);
    478 
    479 		switch(cfi->cfi_portwidth) {
    480 		case 0:
    481 			found = cfi_chip_query_1(cfi);
    482 			break;
    483 		case 1:
    484 			found = cfi_chip_query_2(cfi);
    485 			break;
    486 		case 2:
    487 			found = cfi_chip_query_4(cfi);
    488 			break;
    489 		case 3:
    490 			found = cfi_chip_query_8(cfi);
    491 			break;
    492 		default:
    493 			panic("%s: bad portwidth %d\n",
    494 				__func__, cfi->cfi_portwidth);
    495 		}
    496 	}
    497 
    498 	if (found)
    499 		cfi->cfi_emulated = false;
    500 
    501 	return found;
    502 }
    503 
    504 /*
    505  * cfi_probe - search for a CFI NOR trying various port & chip widths
    506  *
    507  * - gather CFI QRY and PRI data
    508  * - gather JEDEC ID data
    509  * - if cfi_chip_query() fails, emulate CFI using table data if possible,
    510  *   otherwise fail.
    511  *
    512  * NOTE:
    513  *   striped NOR chips design not supported yet,
    514  *   so force portwidth=chipwidth for now
    515  *   eventually permute portwidth seperately
    516  */
    517 bool
    518 cfi_probe(struct cfi * const cfi)
    519 {
    520 	bool found;
    521 
    522 	KASSERT(cfi != NULL);
    523 
    524 	for (u_int cw = 0; cw < 3; cw++) {
    525 		cfi->cfi_portwidth = 		/* XXX */
    526 		cfi->cfi_chipwidth = cw;
    527 		found = cfi_chip_query(cfi);
    528 		cfi_jedec_id(cfi);
    529 		if (! found)
    530 			found = cfi_emulate(cfi);
    531 		if (found)
    532 			break;
    533 	}
    534 
    535 	cfi_reset_default(cfi);		/* exit QRY mode */
    536 	return found;
    537 }
    538 
    539 bool
    540 cfi_identify(struct cfi * const cfi)
    541 {
    542 	const bus_space_tag_t bst = cfi->cfi_bst;
    543 	const bus_space_handle_t bsh = cfi->cfi_bsh;
    544 	bool found;
    545 
    546 	KASSERT(cfi != NULL);
    547 	KASSERT(bst != NULL);
    548 
    549 	memset(cfi, 0, sizeof(struct cfi));	/* XXX clean slate */
    550 	cfi->cfi_bst = bst;		/* restore bus space */
    551 	cfi->cfi_bsh = bsh;		/*  "       "   "    */
    552 
    553 	found = cfi_probe(cfi);
    554 
    555 	cfi_reset_default(cfi);	/* exit QRY mode */
    556 
    557 	return found;
    558 }
    559 
    560 static int
    561 cfi_scan_media(device_t self, struct nor_chip *chip)
    562 {
    563 	struct nor_softc *sc = device_private(self);
    564 	KASSERT(sc != NULL);
    565 	KASSERT(sc->sc_nor_if != NULL);
    566 	struct cfi * const cfi = (struct cfi * const)sc->sc_nor_if->private;
    567 	KASSERT(cfi != NULL);
    568 
    569 	sc->sc_nor_if->access_width = cfi->cfi_portwidth;
    570 
    571 	chip->nc_manf_id = cfi->cfi_id_data.id_mid;
    572 	chip->nc_dev_id = cfi->cfi_id_data.id_did[0]; /* XXX 3 words */
    573 	chip->nc_size = 1 << cfi->cfi_qry_data.device_size;
    574 
    575 	/* size of line for Read Buf command */
    576 	chip->nc_line_size = 1 << cfi->cfi_qry_data.pri.cmd_0002.page_size;
    577 
    578 	/*
    579 	 * size of erase block
    580 	 * XXX depends on erase region
    581 	 */
    582 	chip->nc_num_luns = 1;
    583 	chip->nc_lun_blocks = cfi->cfi_qry_data.erase_blk_info[0].y + 1;
    584 	chip->nc_block_size = cfi->cfi_qry_data.erase_blk_info[0].z * 256;
    585 
    586 	switch (cfi->cfi_qry_data.id_pri) {
    587 	case 0x0002:
    588 		cfi_0002_init(sc, cfi, chip);
    589 		break;
    590 	default:
    591 		aprint_error_dev(self, "unsupported CFI cmdset %#04x\n",
    592 			cfi->cfi_qry_data.id_pri);
    593 		return -1;
    594 	}
    595 
    596 	return 0;
    597 }
    598 
    599 void
    600 cfi_init(device_t self)
    601 {
    602 	/* nothing */
    603 }
    604 
    605 static void
    606 cfi_select(device_t self, bool select)
    607 {
    608 	/* nothing */
    609 }
    610 
    611 static void
    612 cfi_read_1(device_t self, flash_off_t offset, uint8_t *datap)
    613 {
    614 }
    615 
    616 static void
    617 cfi_read_2(device_t self, flash_off_t offset, uint16_t *datap)
    618 {
    619 }
    620 
    621 static void
    622 cfi_read_4(device_t self, flash_off_t offset, uint32_t *datap)
    623 {
    624 }
    625 
    626 static void
    627 cfi_read_buf_1(device_t self, flash_off_t offset, uint8_t *datap, size_t size)
    628 {
    629 }
    630 
    631 static void
    632 cfi_read_buf_2(device_t self, flash_off_t offset, uint16_t *datap, size_t size)
    633 {
    634 }
    635 
    636 static void
    637 cfi_read_buf_4(device_t self, flash_off_t offset, uint32_t *datap, size_t size)
    638 {
    639 }
    640 
    641 static void
    642 cfi_write_1(device_t self, flash_off_t offset, uint8_t data)
    643 {
    644 }
    645 
    646 static void
    647 cfi_write_2(device_t self, flash_off_t offset, uint16_t data)
    648 {
    649 }
    650 
    651 static void
    652 cfi_write_4(device_t self, flash_off_t offset, uint32_t data)
    653 {
    654 }
    655 
    656 static void
    657 cfi_write_buf_1(device_t self, flash_off_t offset, const uint8_t *datap,
    658     size_t size)
    659 {
    660 }
    661 
    662 static void
    663 cfi_write_buf_2(device_t self, flash_off_t offset, const uint16_t *datap,
    664     size_t size)
    665 {
    666 }
    667 
    668 static void
    669 cfi_write_buf_4(device_t self, flash_off_t offset, const uint32_t *datap,
    670     size_t size)
    671 {
    672 }
    673 
    674 void
    675 cfi_cmd(struct cfi * const cfi, bus_size_t off, uint32_t val)
    676 {
    677 	const bus_space_tag_t bst = cfi->cfi_bst;
    678 	bus_space_handle_t bsh = cfi->cfi_bsh;
    679 
    680 	off <<= cfi->cfi_portwidth;
    681 
    682 	DPRINTF(("%s: %p %x %x %x\n", __func__, bst, bsh, off, val));
    683 
    684 	switch(cfi->cfi_portwidth) {
    685 	case 0:
    686 		bus_space_write_1(bst, bsh, off, (uint8_t)val);
    687 		break;
    688 	case 1:
    689 		bus_space_write_2(bst, bsh, off, val);
    690 		break;
    691 	case 2:
    692 		bus_space_write_4(bst, bsh, off, (uint32_t)val);
    693 		break;
    694 #ifdef NOTYET
    695 	case 3:
    696 		bus_space_write_4(bst, bsh, off, (uint64_t)val);
    697 		break;
    698 #endif
    699 	default:
    700 		panic("%s: bad portwidth %d bytes\n",
    701 			__func__, 1 << cfi->cfi_portwidth);
    702 	}
    703 }
    704 
    705 /*
    706  * cfi_reset_default - when we don't know which command will work, use both
    707  */
    708 void
    709 cfi_reset_default(struct cfi * const cfi)
    710 {
    711 	cfi_cmd(cfi, CFI_ADDRESS_ANY, CFI_RESET_DATA);
    712 	cfi_cmd(cfi, CFI_ADDRESS_ANY, CFI_ALT_RESET_DATA);
    713 }
    714 
    715 /*
    716  * cfi_reset_std - use standard reset command
    717  */
    718 void
    719 cfi_reset_std(struct cfi * const cfi)
    720 {
    721 	cfi_cmd(cfi, CFI_ADDRESS_ANY, CFI_RESET_DATA);
    722 }
    723 
    724 /*
    725  * cfi_reset_alt - use "alternate" reset command
    726  */
    727 void
    728 cfi_reset_alt(struct cfi * const cfi)
    729 {
    730 	cfi_cmd(cfi, CFI_ADDRESS_ANY, CFI_ALT_RESET_DATA);
    731 }
    732 
    733 static void
    734 cfi_jedec_id_1(struct cfi * const cfi)
    735 {
    736 	struct cfi_jedec_id_data *idp = &cfi->cfi_id_data;
    737 	uint8_t data[0x10];
    738 
    739 	bus_space_read_region_1(cfi->cfi_bst, cfi->cfi_bsh, 0, data,
    740 		__arraycount(data));
    741 
    742 	CFI_DUMP_JEDEC(0, data, sizeof(data), 1);
    743 
    744 	idp->id_mid = (uint16_t)data[0];
    745 	idp->id_did[0] = (uint16_t)data[1];
    746 	idp->id_did[1] = (uint16_t)data[0xe];
    747 	idp->id_did[2] = (uint16_t)data[0xf];
    748 	idp->id_prot_state = (uint16_t)data[2];
    749 	idp->id_indicators = (uint16_t)data[3];
    750 
    751 	/* software bits, upper and lower */
    752 	idp->id_swb_lo = data[0xc];
    753 	idp->id_swb_hi = data[0xd];
    754 
    755 }
    756 
    757 static void
    758 cfi_jedec_id_2(struct cfi * const cfi)
    759 {
    760 	struct cfi_jedec_id_data *idp = &cfi->cfi_id_data;
    761 	uint16_t data[0x10];
    762 
    763 	bus_space_read_region_2(cfi->cfi_bst, cfi->cfi_bsh, 0, data,
    764 		__arraycount(data));
    765 
    766 	CFI_DUMP_JEDEC(0, data, sizeof(data), 1);
    767 
    768 	idp->id_mid = data[0];
    769 	idp->id_did[0] = data[1];
    770 	idp->id_did[1] = data[0xe];
    771 	idp->id_did[2] = data[0xf];
    772 	idp->id_prot_state = data[2];
    773 	idp->id_indicators = data[3];
    774 
    775 	/* software bits, upper and lower
    776 	 * - undefined on S29GL-P
    777 	 * - defined   on S29GL-S
    778 	 */
    779 	idp->id_swb_lo = data[0xc];
    780 	idp->id_swb_hi = data[0xd];
    781 
    782 }
    783 
    784 static void
    785 cfi_jedec_id_4(struct cfi * const cfi)
    786 {
    787 	struct cfi_jedec_id_data *idp = &cfi->cfi_id_data;
    788 	uint32_t data[0x10];
    789 
    790 	bus_space_read_region_4(cfi->cfi_bst, cfi->cfi_bsh, 0, data,
    791 		__arraycount(data));
    792 
    793 	CFI_DUMP_JEDEC(0, data, sizeof(data), 1);
    794 
    795 	idp->id_mid = data[0] & 0xffff;
    796 	idp->id_did[0] = data[1] & 0xffff;
    797 	idp->id_did[1] = data[0xe] & 0xffff;
    798 	idp->id_did[2] = data[0xf] & 0xffff;
    799 	idp->id_prot_state = data[2] & 0xffff;
    800 	idp->id_indicators = data[3] & 0xffff;
    801 
    802 	/* software bits, upper and lower
    803 	 * - undefined on S29GL-P
    804 	 * - defined   on S29GL-S
    805 	 */
    806 	idp->id_swb_lo = data[0xc] & 0xffff;
    807 	idp->id_swb_hi = data[0xd] & 0xffff;
    808 
    809 }
    810 
    811 /*
    812  * cfi_jedec_id - get JEDEC ID info
    813  */
    814 static bool
    815 cfi_jedec_id(struct cfi * const cfi)
    816 {
    817 
    818 	DPRINTF(("%s\n", __func__));
    819 
    820 	cfi_cmd(cfi, 0x555, 0xaa);
    821 	cfi_cmd(cfi, 0x2aa, 0x55);
    822 	cfi_cmd(cfi, 0x555, 0x90);
    823 
    824 	switch(cfi->cfi_portwidth) {
    825 	case 0:
    826 		cfi_jedec_id_1(cfi);
    827 		break;
    828 	case 1:
    829 		cfi_jedec_id_2(cfi);
    830 		break;
    831 	case 2:
    832 		cfi_jedec_id_4(cfi);
    833 		break;
    834 #ifdef NOTYET
    835 	case 3:
    836 		cfi_jedec_id_8(cfi);
    837 		break;
    838 #endif
    839 	default:
    840 		panic("%s: bad portwidth %d bytes\n",
    841 			__func__, 1 << cfi->cfi_portwidth);
    842 	}
    843 
    844 	return true;
    845 }
    846 
    847 static bool
    848 cfi_emulate(struct cfi * const cfi)
    849 {
    850 	bool found = false;
    851 	const struct cfi_jedec_tab *jt = cfi_jedec_search(cfi);
    852 	if (jt != NULL) {
    853 		found = true;
    854 		cfi->cfi_emulated = true;
    855 		cfi_jedec_fill(cfi, jt);
    856 	}
    857 	return found;
    858 }
    859 
    860 /*
    861  * cfi_jedec_search - search cfi_jedec_tab[] for entry matching given JEDEC IDs
    862  */
    863 static const struct cfi_jedec_tab *
    864 cfi_jedec_search(struct cfi *cfi)
    865 {
    866 	struct cfi_jedec_id_data *idp = &cfi->cfi_id_data;
    867 
    868 	for (u_int i=0; i < __arraycount(cfi_jedec_tab); i++) {
    869 		const struct cfi_jedec_tab *jt = &cfi_jedec_tab[i];
    870 		if ((jt->jt_mid == idp->id_mid) &&
    871 		    (jt->jt_did == idp->id_did[0])) {
    872 			return jt;
    873 		}
    874 	}
    875 	return NULL;
    876 }
    877 
    878 /*
    879  * cfi_jedec_fill - fill in cfi with info from table entry
    880  */
    881 static void
    882 cfi_jedec_fill(struct cfi *cfi, const struct cfi_jedec_tab *jt)
    883 {
    884 
    885 	cfi->cfi_name = jt->jt_name;
    886 	cfi->cfi_opmode = jt->jt_opmode;
    887 
    888 	struct cfi_query_data *qryp = &cfi->cfi_qry_data;
    889 	memset(&qryp, 0, sizeof(*qryp));
    890 	qryp->id_pri = jt->jt_id_pri;
    891 	qryp->id_alt = jt->jt_id_alt;
    892 	qryp->interface_code_desc = jt->jt_interface_code_desc;
    893 	qryp->write_word_time_typ = jt->jt_write_word_time_typ;
    894 	qryp->write_nbyte_time_typ = jt->jt_write_nbyte_time_typ;
    895 	qryp->erase_blk_time_typ = jt->jt_erase_blk_time_typ;
    896 	qryp->erase_chip_time_typ = jt->jt_erase_chip_time_typ;
    897 	qryp->write_word_time_max = jt->jt_write_word_time_max;
    898 	qryp->write_nbyte_time_max = jt->jt_write_nbyte_time_max;
    899 	qryp->erase_blk_time_max = jt->jt_erase_blk_time_max;
    900 	qryp->erase_chip_time_max = jt->jt_erase_chip_time_max;
    901 	qryp->device_size = jt->jt_device_size;
    902 	qryp->interface_code_desc = jt->jt_interface_code_desc;
    903 	qryp->write_nbyte_size_max = jt->jt_write_nbyte_size_max;
    904 	qryp->erase_blk_regions = jt->jt_erase_blk_regions;
    905 	for (u_int i=0; i < 4; i++)
    906 		qryp->erase_blk_info[i] = jt->jt_erase_blk_info[i];
    907 
    908 }
    909 
    910 void
    911 cfi_print(device_t self, struct cfi * const cfi)
    912 {
    913 	char pbuf[sizeof("XXXX MB")];
    914 	struct cfi_query_data * const qryp = &cfi->cfi_qry_data;
    915 
    916 	format_bytes(pbuf, sizeof(pbuf), 1 << qryp->device_size);
    917 	if (cfi->cfi_emulated) {
    918 		aprint_normal_dev(self, "%s NOR flash %s %s\n",
    919 			cfi->cfi_name, pbuf,
    920 			cfi_interface_desc_str(qryp->interface_code_desc));
    921 	} else {
    922 		aprint_normal_dev(self, "CFI NOR flash %s %s\n", pbuf,
    923 			cfi_interface_desc_str(qryp->interface_code_desc));
    924 	}
    925 #ifdef NOR_VERBOSE
    926 	aprint_normal_dev(self, "manufacturer id %#x, device id %#x %#x %#x\n",
    927 		cfi->cfi_id_data.id_mid,
    928 		cfi->cfi_id_data.id_did[0],
    929 		cfi->cfi_id_data.id_did[1],
    930 		cfi->cfi_id_data.id_did[2]);
    931 	aprint_normal_dev(self, "%s\n", cfi->cfi_opmode->str);
    932 	aprint_normal_dev(self, "sw bits lo=%#x hi=%#x\n",
    933 		cfi->cfi_id_data.id_swb_lo,
    934 		cfi->cfi_id_data.id_swb_hi);
    935 	aprint_normal_dev(self, "max multibyte write size %d\n",
    936 		1 << qryp->write_nbyte_size_max);
    937 	aprint_normal_dev(self, "%d Erase Block Region(s)\n",
    938 		qryp->erase_blk_regions);
    939 	for (u_int r=0; r < qryp->erase_blk_regions; r++) {
    940 		size_t sz = qryp->erase_blk_info[r].z * 256;
    941 		format_bytes(pbuf, sizeof(pbuf), sz);
    942 		aprint_normal("    %d: %d blocks, size %s\n", r,
    943 			qryp->erase_blk_info[r].y + 1, pbuf);
    944 	}
    945 #endif
    946 
    947 	switch (cfi->cfi_qry_data.id_pri) {
    948 	case 0x0002:
    949 		cfi_0002_print(self, cfi);
    950 		break;
    951 	}
    952 }
    953 
    954 #if defined(CFI_DEBUG_JEDEC) || defined(CFI_DEBUG_QRY)
    955 void
    956 cfi_hexdump(flash_off_t offset, void * const v, u_int count, u_int stride)
    957 {
    958 	uint8_t * const data = v;
    959 	for(int n=0; n < count; n+=16) {
    960 		int i;
    961 		printf("%08llx: ", (offset + n) / stride);
    962 		for(i=n; i < n+16; i++)
    963 			printf("%02x ", data[i]);
    964 		printf("\t");
    965 		for(i=n; i < n+16; i++) {
    966 			u_int c = (int)data[i];
    967 			if (c >= 0x20 && c < 0x7f)
    968 				printf("%c", c);
    969 			else
    970 				printf("%c", '.');
    971 		}
    972 		printf("\n");
    973 	}
    974 }
    975 #endif
    976