1/*	$NetBSD: nand_micron.c,v 1.5 2011/06/28 07:16:11 ahoka Exp $	*/
2
3/*-
4 * Copyright (c) 2011 Department of Software Engineering,
5 *		      University of Szeged, Hungary
6 * Copyright (c) 2011 Adam Hoka <ahoka@NetBSD.org>
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by the Department of Software Engineering, University of Szeged, Hungary
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/*
35 * Device specific functions for legacy Micron NAND chips
36 *
37 * Currently supported:
38 * MT29F2G08AACWP, MT29F4G08BACWP, MT29F8G08FACWP
39 */
40
41#include <sys/cdefs.h>
42__KERNEL_RCSID(0, "$NetBSD: nand_micron.c,v 1.5 2011/06/28 07:16:11 ahoka Exp $");
43
44#include "nand.h"
45#include "onfi.h"
46
47#define MT29F2G08AAC	0xda
48#define MT29F2G08ABC	0xaa
49#define MT29F2G16AAC	0xca
50#define MT29F2G16ABC	0xba
51#define MT29F4G08BAC	0xdc
52#define MT29F8G08FAC	0xdc		/* each 4GB section */
53
54#define MT29FxG_PARAM_WIDTH(p)		(((p) >> 1) & __BIT(0))
55#define MT29FxG_PAGESIZE		(2 * 1024)
56#define MT29FxG_BLOCK_PAGES		64		/* pages per block */
57#define MT29FxG_BLOCKSIZE		(128 * 1024)	/* not including spares */
58#define MT29FxG_SPARESIZE		64
59
60struct nand_micron_devices {
61	const char *name;
62	uint8_t	id;
63	uint8_t width;			/* bus width */
64	u_int lun_blocks;		/* number of blocks per LUN */
65	u_int num_luns;			/* number LUNs */
66};
67
68static const struct nand_micron_devices nand_micron_devices[] = {
69	{ "MT29F2G08AAC", MT29F2G08AAC,  8,  2048, 1 },
70	{ "MT29F2G08ABC", MT29F2G08ABC,  8,  2048, 1 },
71	{ "MT29F2G16AAC", MT29F2G16AAC, 16,  2048, 1 },
72	{ "MT29F2G16ABC", MT29F2G16ABC, 16,  2048, 1 },
73	{ "MT29F4G08BAC", MT29F4G08BAC,  8,  4096, 1 },
74#ifdef NOTYET
75	/* how do we recognize/match this? */
76	{ "MT29F8G08FAC", MT29F8G08FAC,  8,  4096, 2 },
77#endif
78};
79
80static int mt29fxgx_parameters(device_t, struct nand_chip *, u_int8_t, uint8_t);
81
82static const struct nand_micron_devices *
83nand_micron_device_lookup(u_int8_t id)
84{
85	for (int i=0; i < __arraycount(nand_micron_devices); i++)
86		if (nand_micron_devices[i].id == id)
87			return &nand_micron_devices[i];
88	return NULL;
89}
90
91int
92nand_read_parameters_micron(device_t self, struct nand_chip * const chip)
93{
94	uint8_t mfgrid;
95	uint8_t devid;
96	uint8_t dontcare;
97	uint8_t params;
98
99	KASSERT(chip->nc_manf_id == NAND_MFR_MICRON);
100	switch (chip->nc_manf_id) {
101	case NAND_MFR_MICRON:
102		break;
103	default:
104		return 1;
105	}
106
107	nand_select(self, true);
108	nand_command(self, ONFI_READ_ID);
109	nand_address(self, 0x00);
110	nand_read_1(self, &mfgrid);
111	nand_read_1(self, &devid);
112	nand_read_1(self, &dontcare);
113	nand_read_1(self, &params);
114	nand_select(self, false);
115
116	KASSERT(chip->nc_manf_id == mfgrid);
117
118	switch(devid) {
119	case MT29F2G08AAC:
120	case MT29F2G08ABC:
121	case MT29F2G16AAC:
122	case MT29F2G16ABC:
123	case MT29F4G08BAC:
124		return mt29fxgx_parameters(self, chip, devid, params);
125	default:
126		aprint_error_dev(self, "unsupported device id %#x\n", devid);
127		return 1;
128	}
129}
130
131static int
132mt29fxgx_parameters(device_t self, struct nand_chip * const chip,
133	u_int8_t devid, uint8_t params)
134{
135	const struct nand_micron_devices *dp;
136	const char *vendor = "Micron";
137
138	dp = nand_micron_device_lookup(devid);
139	if (dp == NULL) {
140		aprint_error_dev(self, "unknown device id %#x\n", devid);
141		return 1;
142	}
143
144	/*
145	 * MT29FxGx params across models are the same
146	 * except for luns, blocks per lun, and bus width
147	 * (and voltage)
148	 */
149	chip->nc_addr_cycles_column = 2;	/* XXX */
150	chip->nc_addr_cycles_row = 3;		/* XXX */
151	if (dp->width == 16)
152		chip->nc_flags |= NC_BUSWIDTH_16;
153	chip->nc_page_size = MT29FxG_PAGESIZE;
154	chip->nc_block_pages = MT29FxG_BLOCK_PAGES;
155	chip->nc_block_size = MT29FxG_BLOCK_PAGES * MT29FxG_PAGESIZE;
156	chip->nc_spare_size = MT29FxG_SPARESIZE;
157	chip->nc_lun_blocks = dp->lun_blocks;
158	chip->nc_num_luns = dp->num_luns;
159	chip->nc_size = MT29FxG_PAGESIZE * MT29FxG_BLOCK_PAGES *
160		dp->lun_blocks * dp->num_luns;
161
162	aprint_normal_dev(self, "%s %s, size %zuMB\n",
163		vendor, dp->name, chip->nc_size >> 20);
164
165	return 0;
166}
167