summaryrefslogtreecommitdiff
path: root/drivers/mtd/nand/gpmi-nfc/gpmi-nfc-rom-v0.c
blob: 35321cc25546e90115dab6d3c5c9338343bd71fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Freescale GPMI NFC NAND Flash Driver
 *
 * Copyright (C) 2010 Freescale Semiconductor, Inc.
 * Copyright (C) 2008 Embedded Alley Solutions, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "gpmi-nfc.h"

/*
 * Useful variables for Boot ROM Helper version 0.
 */

static const char  *fingerprint = "STMP";

/**
 * set_geometry() - Sets geometry for the Boot ROM Helper.
 *
 * @this:  Per-device data.
 */
static int set_geometry(struct gpmi_nfc_data *this)
{
	struct gpmi_nfc_platform_data  *pdata    =  this->pdata;
	struct physical_geometry       *physical = &this->physical_geometry;
	struct boot_rom_geometry       *geometry = &this->rom_geometry;
	int                             error;

	/* Version-independent geometry. */

	error = gpmi_nfc_rom_helper_set_geometry(this);

	if (error)
		return error;

	/*
	 * Check if the platform data indicates we are to protect the boot area.
	 */

	if (!pdata->boot_area_size_in_bytes) {
		geometry->boot_area_count         = 0;
		geometry->boot_area_size_in_bytes = 0;
		return 0;
	}

	/*
	 * If control arrives here, we are supposed to set up partitions to
	 * protect the boot areas. In this version of the ROM, the number of
	 * boot areas and their size depends on the number of chips.
	 */

	if (physical->chip_count == 1) {
		geometry->boot_area_count = 1;
		geometry->boot_area_size_in_bytes =
					pdata->boot_area_size_in_bytes * 2;
	} else {
		geometry->boot_area_count = 2;
		geometry->boot_area_size_in_bytes =
					pdata->boot_area_size_in_bytes;
	}

	/* Return success. */

	return 0;

}

/**
 * check_transcription_stamp() - Checks for a transcription stamp.
 *
 * Returns 0 if a stamp is not found.
 *
 * @this:  Per-device data.
 */
static int check_transcription_stamp(struct gpmi_nfc_data *this)
{
	struct physical_geometry  *physical = &this->physical_geometry;
	struct boot_rom_geometry  *rom_geo  = &this->rom_geometry;
	struct mil                *mil      = &this->mil;
	struct mtd_info           *mtd      = &mil->mtd;
	struct nand_chip          *nand     = &mil->nand;
	unsigned int              search_area_size_in_strides;
	unsigned int              stride;
	unsigned int              page;
	loff_t                    byte;
	uint8_t                   *buffer = nand->buffers->databuf;
	int                       saved_chip_number;
	int                       found_an_ncb_fingerprint = false;

	/* Compute the number of strides in a search area. */

	search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;

	/* Select chip 0. */

	saved_chip_number = mil->current_chip;
	nand->select_chip(mtd, 0);

	/*
	 * Loop through the first search area, looking for the NCB fingerprint.
	 */

	pr_info("Scanning for an NCB fingerprint...\n");

	for (stride = 0; stride < search_area_size_in_strides; stride++) {

		/* Compute the page and byte addresses. */

		page = stride * rom_geo->stride_size_in_pages;
		byte = page   * physical->page_data_size_in_bytes;

		pr_info("  Looking for a fingerprint in page 0x%x\n", page);

		/*
		 * Read the NCB fingerprint. The fingerprint is four bytes long
		 * and starts in the 12th byte of the page.
		 */

		nand->cmdfunc(mtd, NAND_CMD_READ0, 12, page);
		nand->read_buf(mtd, buffer, strlen(fingerprint));

		/* Look for the fingerprint. */

		if (!memcmp(buffer, fingerprint,
					strlen(fingerprint))) {
			found_an_ncb_fingerprint = true;
			break;
		}

	}

	/* Deselect chip 0. */

	nand->select_chip(mtd, saved_chip_number);

	/* Return. */

	if (found_an_ncb_fingerprint)
		pr_info("  Found a fingerprint\n");
	else
		pr_info("  No fingerprint found\n");

	return found_an_ncb_fingerprint;

}

/**
 * write_transcription_stamp() - Writes a transcription stamp.
 *
 * @this:  Per-device data.
 */
static int write_transcription_stamp(struct gpmi_nfc_data *this)
{
	struct device             *dev      =  this->dev;
	struct physical_geometry  *physical = &this->physical_geometry;
	struct boot_rom_geometry  *rom_geo  = &this->rom_geometry;
	struct mil                *mil      = &this->mil;
	struct mtd_info           *mtd      = &mil->mtd;
	struct nand_chip          *nand     = &mil->nand;
	unsigned int              block_size_in_pages;
	unsigned int              search_area_size_in_strides;
	unsigned int              search_area_size_in_pages;
	unsigned int              search_area_size_in_blocks;
	unsigned int              block;
	unsigned int              stride;
	unsigned int              page;
	loff_t                    byte;
	uint8_t                   *buffer = nand->buffers->databuf;
	int                       saved_chip_number;
	int                       status;

	/* Compute the search area geometry. */

	block_size_in_pages = physical->block_size_in_bytes >>
				(ffs(physical->page_data_size_in_bytes) - 1);

	search_area_size_in_strides = 1 << rom_geo->search_area_stride_exponent;

	search_area_size_in_pages = search_area_size_in_strides *
						rom_geo->stride_size_in_pages;

	search_area_size_in_blocks =
		  (search_area_size_in_pages + (block_size_in_pages - 1)) /
		/*-------------------------------------------------------*/
				    block_size_in_pages;

	#if defined(DETAILED_INFO)

	pr_info("--------------------\n");
	pr_info("Search Area Geometry\n");
	pr_info("--------------------\n");
	pr_info("Search Area Size in Blocks : %u", search_area_size_in_blocks);
	pr_info("Search Area Size in Strides: %u", search_area_size_in_strides);
	pr_info("Search Area Size in Pages  : %u", search_area_size_in_pages);

	#endif

	/* Select chip 0. */

	saved_chip_number = mil->current_chip;
	nand->select_chip(mtd, 0);

	/* Loop over blocks in the first search area, erasing them. */

	pr_info("Erasing the search area...\n");

	for (block = 0; block < search_area_size_in_blocks; block++) {

		/* Compute the page address. */

		page = block * block_size_in_pages;

		/* Erase this block. */

		pr_info("  Erasing block 0x%x\n", block);

		nand->cmdfunc(mtd, NAND_CMD_ERASE1, -1, page);
		nand->cmdfunc(mtd, NAND_CMD_ERASE2, -1, -1);

		/* Wait for the erase to finish. */

		status = nand->waitfunc(mtd, nand);

		if (status & NAND_STATUS_FAIL)
			dev_err(dev, "[%s] Erase failed.\n", __func__);

	}

	/* Write the NCB fingerprint into the page buffer. */

	memset(buffer, ~0, mtd->writesize);
	memset(nand->oob_poi, ~0, mtd->oobsize);

	memcpy(buffer + 12, fingerprint, strlen(fingerprint));

	/* Loop through the first search area, writing NCB fingerprints. */

	pr_info("Writing NCB fingerprints...\n");

	for (stride = 0; stride < search_area_size_in_strides; stride++) {

		/* Compute the page and byte addresses. */

		page = stride * rom_geo->stride_size_in_pages;
		byte = page   * physical->page_data_size_in_bytes;

		/* Write the first page of the current stride. */

		pr_info("  Writing an NCB fingerprint in page 0x%x\n", page);

		nand->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
		nand->ecc.write_page_raw(mtd, nand, buffer);
		nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);

		/* Wait for the write to finish. */

		status = nand->waitfunc(mtd, nand);

		if (status & NAND_STATUS_FAIL)
			dev_err(dev, "[%s] Write failed.\n", __func__);

	}

	/* Deselect chip 0. */

	nand->select_chip(mtd, saved_chip_number);

	/* Return success. */

	return 0;

}

/* This structure represents the Boot ROM Helper for this version. */

struct boot_rom_helper  gpmi_nfc_boot_rom_helper_v0 = {
	.version                   = 0,
	.description               = "Single/dual-chip boot area, "
						"no block mark swapping",
	.swap_block_mark           = false,
	.set_geometry              = set_geometry,
	.check_transcription_stamp = check_transcription_stamp,
	.write_transcription_stamp = write_transcription_stamp,
};