Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* $NetBSD: athflash.c,v 1.12 2021/01/04 17:42:29 thorpej Exp $ */

/*
 * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
 * Copyright (c) 2006 Garrett D'Amore.
 * All rights reserved.
 *
 * Portions of this code were written by Garrett D'Amore for the
 * Champaign-Urbana Community Wireless Network Project.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgements:
 *      This product includes software developed by the Urbana-Champaign
 *      Independent Media Center.
 *	This product includes software developed by Garrett D'Amore.
 * 4. Urbana-Champaign Independent Media Center's name and Garrett
 *    D'Amore's name may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
 * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
 * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * Copyright (c) 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Naoto Shimazaki of YOKOGAWA Electric Corporation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Flash Memory Driver
 *
 * XXX This primitive flash driver does *not* support boot sectored devices,
 * XXX and only supports a fairly limited set of devices, that we are likely to
 * XXX to find in an AP30.
 * XXX
 * XXX We also are only supporting flash widths of 16 _for the moment_, and
 * XXX we are only supporting flash devices that use the AMD command sets.
 * XXX All this should be reviewed and improved to be much more generic.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: athflash.c,v 1.12 2021/01/04 17:42:29 thorpej Exp $");

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/systm.h>

#include <sys/bus.h>

#include <mips/atheros/include/arbusvar.h>

#ifdef FLASH_DEBUG
int	flash_debug = 0;
#define DPRINTF(x)	if (flash_debug) printf x
#else
#define DPRINTF(x)
#endif

struct flash_softc {
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_ioh;
	size_t			sc_size;
	size_t			sc_sector_size;
	int			sc_status;
	u_int8_t		*sc_buf;
};

#define	FLASH_ST_BUSY	0x1

static int flash_probe(device_t, cfdata_t, void *);
static void flash_attach(device_t, device_t, void *);

static int is_block_same(struct flash_softc *, bus_size_t, const void *);
static int toggle_bit_wait(struct flash_softc *, bus_size_t, int, int, int);

static int flash_sector_erase(struct flash_softc *, bus_size_t);
static int flash_sector_write(struct flash_softc *, bus_size_t);

extern struct cfdriver athflash_cd;

CFATTACH_DECL_NEW(athflash, sizeof(struct flash_softc),
	      flash_probe, flash_attach, NULL, NULL);

dev_type_open(flashopen);
dev_type_close(flashclose);
dev_type_read(flashread);
dev_type_write(flashwrite);

const struct cdevsw athflash_cdevsw = {
	.d_open = flashopen,
	.d_close = flashclose,
	.d_read = flashread,
	.d_write = flashwrite,
	.d_ioctl = noioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = nopoll,
	.d_mmap = nommap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = 0
};

static struct {
	uint16_t		vendor_id;
	uint16_t		device_id;
	const char		*name;
	int			sector_size;
	int			flash_size;
} flash_ids[] = {
	{ 0x00bf, 0x2780, "SST 39VF400", 0x01000, 0x080000 },	/* 512KB */
	{ 0x00bf, 0x2782, "SST 39VF160", 0x01000, 0x200000 },	/* 2MB */
	{ 0xffff, 0xffff, NULL, 0, 0 }				/* end list */
};

static int
flash_probe(device_t parent, cfdata_t cf, void *aux)
{
	struct arbus_attach_args	*aa = aux;
	bus_space_handle_t		ioh;
	int				rv = 0, i;
	uint16_t			venid, devid;

	if (strcmp(aa->aa_name, cf->cf_name) != 0)
		return 0;

	DPRINTF(("trying to map address %x\n", (unsigned)aa->aa_addr));
	if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0, &ioh))
		return 0;

	/* issue JEDEC query */
	DPRINTF(("issuing JEDEC query\n"));
	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0xAAAA);
	bus_space_write_2(aa->aa_bst, ioh, (0x2AAA << 1), 0x5555);
	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0x9090);

	delay(100);
	venid = bus_space_read_2(aa->aa_bst, ioh, 0);
	devid = bus_space_read_2(aa->aa_bst, ioh, 2);

	/* issue software exit */
	bus_space_write_2(aa->aa_bst, ioh, 0x0, 0xF0F0);

	for (i = 0; flash_ids[i].name != NULL; i++) {
		if ((venid == flash_ids[i].vendor_id) &&
		    (devid == flash_ids[i].device_id)) {
			rv = 1;
			break;
		}
	}

	bus_space_unmap(aa->aa_bst, ioh, aa->aa_size);
	return rv;
}

static void
flash_attach(device_t parent, device_t self, void *aux)
{
	char nbuf[32];
	struct flash_softc		*sc = device_private(self);
	struct arbus_attach_args	*aa = aux;
	int				i;
	bus_space_tag_t			iot = aa->aa_bst;
	bus_space_handle_t		ioh;
	uint16_t			venid, devid;

	if (bus_space_map(iot, aa->aa_addr, aa->aa_size, 0, &ioh)) {
		printf(": can't map i/o space\n");
                return;
  	}
	
	sc->sc_iot = iot;
	sc->sc_ioh = ioh;
	sc->sc_status = 0;

	/* issue JEDEC query */
	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0xAAAA);
	bus_space_write_2(aa->aa_bst, ioh, (0x2AAA << 1), 0x5555);
	bus_space_write_2(aa->aa_bst, ioh, (0x5555 << 1), 0x9090);

	delay(100);
	venid = bus_space_read_2(aa->aa_bst, ioh, 0);
	devid = bus_space_read_2(aa->aa_bst, ioh, 2);

	/* issue software exit */
	bus_space_write_2(aa->aa_bst, ioh, 0x0, 0xF0F0);

	for (i = 0; flash_ids[i].name != NULL; i++) {
		if ((venid == flash_ids[i].vendor_id) &&
		    (devid == flash_ids[i].device_id)) {
			break;
		}
	}

	KASSERT(flash_ids[i].name != NULL);
	printf(": %s", flash_ids[i].name);
	if (humanize_number(nbuf, sizeof(nbuf), flash_ids[i].flash_size, "B",
	    1024) > 0)
		printf(" (%s)", nbuf);

	/*
	 * determine size of the largest block
	 */
	sc->sc_size = flash_ids[i].flash_size;
	sc->sc_sector_size = flash_ids[i].sector_size;

	sc->sc_buf = kmem_alloc(sc->sc_sector_size, KM_SLEEP);

	printf("\n");
}

int
flashopen(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct flash_softc	*sc;

	sc = device_lookup_private(&athflash_cd, minor(dev));
	if (sc == NULL)
		return ENXIO;
	if (sc->sc_status & FLASH_ST_BUSY)
		return EBUSY;
	sc->sc_status |= FLASH_ST_BUSY;
	return 0;
}

int
flashclose(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct flash_softc	*sc;

	sc = device_lookup_private(&athflash_cd, minor(dev));
	sc->sc_status &= ~FLASH_ST_BUSY;
	return 0;
}

int
flashread(dev_t dev, struct uio *uio, int flag)
{
	struct flash_softc	*sc;
	bus_space_tag_t		iot;
	bus_space_handle_t	ioh;
	bus_size_t		off;
	int			total;
	int			count;
	int			error;

	sc = device_lookup_private(&athflash_cd, minor(dev));
	iot = sc->sc_iot;
	ioh = sc->sc_ioh;

	off = uio->uio_offset;
	total = uimin(sc->sc_size - off, uio->uio_resid);

	while (total > 0) {
		count = uimin(sc->sc_sector_size, uio->uio_resid);
		bus_space_read_region_1(iot, ioh, off, sc->sc_buf, count);
		if ((error = uiomove(sc->sc_buf, count, uio)) != 0)
			return error;
		off += count;
		total -= count;
	}
	return 0;
}


int
flashwrite(dev_t dev, struct uio *uio, int flag)
{
	struct flash_softc	*sc;
	bus_size_t		off;
	int			stat;
	int			error;

	sc = device_lookup_private(&athflash_cd, minor(dev));

	if (sc->sc_size < uio->uio_offset + uio->uio_resid)
		return ENOSPC;
	if (uio->uio_offset % sc->sc_sector_size)
		return EINVAL;
	if (uio->uio_resid % sc->sc_sector_size)
		return EINVAL;

	for (off = uio->uio_offset;
	     uio->uio_resid > 0;
	     off += sc->sc_sector_size) {
		error = uiomove(sc->sc_buf, sc->sc_sector_size, uio);
		if (error != 0)
			return error;
		if (is_block_same(sc, off, sc->sc_buf))
			continue;
		if ((stat = flash_sector_erase(sc, off)) != 0) {
			printf("sector erase failed status = 0x%x\n", stat);
			return EIO;
		}
		if ((stat = flash_sector_write(sc, off)) != 0) {
			printf("sector write failed status = 0x%x\n", stat);
			return EIO;
		}
	}
	return 0;
}

static int
is_block_same(struct flash_softc *sc, bus_size_t offset, const void *bufp)
{
	bus_space_tag_t		iot = sc->sc_iot;
	bus_space_handle_t	ioh = sc->sc_ioh;
	const u_int8_t		*p = bufp;
	int			count = sc->sc_sector_size;

	while (count-- > 0) {
		if (bus_space_read_1(iot, ioh, offset++) != *p++)
			return 0;
	}
	return 1;
}

static int
toggle_bit_wait(struct flash_softc *sc, bus_size_t offset,
    int typtmo, int maxtmo, int spin)
{
	bus_space_tag_t		iot = sc->sc_iot;
	bus_space_handle_t	ioh = sc->sc_ioh;
	uint8_t			d1, d2;

	while (maxtmo > 0) {

		if (spin) {
			DELAY(typtmo);
		} else {
			tsleep(sc, PRIBIO, "blockerase",
			    (typtmo / hz) + 1);
		}

		d1 = bus_space_read_1(iot, ioh, offset);
		d2 = bus_space_read_2(iot, ioh, offset);

		/* watch for the toggle bit to stop toggling */
		if ((d1 & 0x40) == (d2 & 0x40)) {
			return 0;
		}

		maxtmo -= typtmo;
	}
	return (ETIMEDOUT);
}

static int
flash_sector_erase(struct flash_softc *sc, bus_size_t offset)
{
	bus_space_tag_t		iot = sc->sc_iot;
	bus_space_handle_t	ioh = sc->sc_ioh;

	DPRINTF(("flash_sector_erase offset = %08lx\n", offset));

	bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
	bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
	bus_space_write_2(iot, ioh, (0x5555 << 1), 0x8080);
	bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
	bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);

	bus_space_write_2(iot, ioh, offset, 0x3030);

	/*
	 * NB: with CFI, we could get more meaningful timeout data for
	 * now we just assign reasonable values - 10 msec typical, and
	 * up to 60 secs to erase the whole sector.
	 */

	return toggle_bit_wait(sc, offset, 10000, 60000000, 0);
}

static int
flash_sector_write(struct flash_softc *sc, bus_size_t offset)
{
	bus_space_tag_t		iot = sc->sc_iot;
	bus_space_handle_t	ioh = sc->sc_ioh;
	bus_size_t		fence;
	const u_int16_t		*p;

	p = (u_int16_t *) sc->sc_buf;
	fence = offset + sc->sc_sector_size;
	do {
		bus_space_write_2(iot, ioh, (0x5555 << 1), 0xAAAA);
		bus_space_write_2(iot, ioh, (0x2AAA << 1), 0x5555);
		bus_space_write_2(iot, ioh, (0xAAAA << 1), 0xA0A0);

		bus_space_write_2(iot, ioh, offset, *p);

		/* wait up to 1 msec, in 10 usec increments, no sleeping */
		if (toggle_bit_wait(sc, offset, 10, 1000, 1) != 0)
			return ETIMEDOUT;
		p++;
		offset += 2;
	} while (offset < fence);

	return 0;
}