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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
/*-
* Copyright (C) 2009-2012 Semihalf
* All rights reserved.
*
* 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 AUTHOR 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 AUTHOR 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.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/proc.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <dev/nand/nand.h>
#include <dev/nand/nandbus.h>
#include "nand_if.h"
#include "nandbus_if.h"
#include "nfc_if.h"
#define NAND_NCS 4
static int nandbus_probe(device_t dev);
static int nandbus_attach(device_t dev);
static int nandbus_detach(device_t dev);
static int nandbus_child_location_str(device_t, device_t, char *, size_t);
static int nandbus_child_pnpinfo_str(device_t, device_t, char *, size_t);
static int nandbus_get_status(device_t, uint8_t *);
static void nandbus_read_buffer(device_t, void *, uint32_t);
static int nandbus_select_cs(device_t, uint8_t);
static int nandbus_send_command(device_t, uint8_t);
static int nandbus_send_address(device_t, uint8_t);
static int nandbus_start_command(device_t);
static int nandbus_wait_ready(device_t, uint8_t *);
static void nandbus_write_buffer(device_t, void *, uint32_t);
static int nandbus_get_ecc(device_t, void *, uint32_t, void *, int *);
static int nandbus_correct_ecc(device_t, void *, int, void *, void *);
static void nandbus_lock(device_t);
static void nandbus_unlock(device_t);
static int nand_readid(device_t, uint8_t *, uint8_t *);
static int nand_probe_onfi(device_t, uint8_t *);
static int nand_reset(device_t);
struct nandbus_softc {
device_t dev;
struct cv nandbus_cv;
struct mtx nandbus_mtx;
uint8_t busy;
};
static device_method_t nandbus_methods[] = {
/* device interface */
DEVMETHOD(device_probe, nandbus_probe),
DEVMETHOD(device_attach, nandbus_attach),
DEVMETHOD(device_detach, nandbus_detach),
DEVMETHOD(device_shutdown, bus_generic_shutdown),
/* bus interface */
DEVMETHOD(bus_print_child, bus_generic_print_child),
DEVMETHOD(bus_driver_added, bus_generic_driver_added),
DEVMETHOD(bus_child_pnpinfo_str, nandbus_child_pnpinfo_str),
DEVMETHOD(bus_child_location_str, nandbus_child_location_str),
/* nandbus interface */
DEVMETHOD(nandbus_get_status, nandbus_get_status),
DEVMETHOD(nandbus_read_buffer, nandbus_read_buffer),
DEVMETHOD(nandbus_select_cs, nandbus_select_cs),
DEVMETHOD(nandbus_send_command, nandbus_send_command),
DEVMETHOD(nandbus_send_address, nandbus_send_address),
DEVMETHOD(nandbus_start_command,nandbus_start_command),
DEVMETHOD(nandbus_wait_ready, nandbus_wait_ready),
DEVMETHOD(nandbus_write_buffer, nandbus_write_buffer),
DEVMETHOD(nandbus_get_ecc, nandbus_get_ecc),
DEVMETHOD(nandbus_correct_ecc, nandbus_correct_ecc),
DEVMETHOD(nandbus_lock, nandbus_lock),
DEVMETHOD(nandbus_unlock, nandbus_unlock),
{ 0, 0 }
};
devclass_t nandbus_devclass;
driver_t nandbus_driver = {
"nandbus",
nandbus_methods,
sizeof(struct nandbus_softc)
};
DRIVER_MODULE(nandbus, nand, nandbus_driver, nandbus_devclass, 0, 0);
int
nandbus_create(device_t nfc)
{
device_t child;
child = device_add_child(nfc, "nandbus", -1);
if (!child)
return (ENODEV);
bus_generic_attach(nfc);
return(0);
}
void
nandbus_destroy(device_t nfc)
{
device_t *children;
int nchildren, i;
mtx_lock(&Giant);
/* Detach & delete all children */
if (!device_get_children(nfc, &children, &nchildren)) {
for (i = 0; i < nchildren; i++)
device_delete_child(nfc, children[i]);
free(children, M_TEMP);
}
mtx_unlock(&Giant);
}
static int
nandbus_probe(device_t dev)
{
device_set_desc(dev, "NAND bus");
return (0);
}
static int
nandbus_attach(device_t dev)
{
device_t child, nfc;
struct nand_id chip_id;
struct nandbus_softc *sc;
struct nandbus_ivar *ivar;
struct nand_softc *nfc_sc;
struct nand_params *chip_params;
uint8_t cs, onfi;
sc = device_get_softc(dev);
sc->dev = dev;
nfc = device_get_parent(dev);
nfc_sc = device_get_softc(nfc);
mtx_init(&sc->nandbus_mtx, "nandbus lock", NULL, MTX_DEF);
cv_init(&sc->nandbus_cv, "nandbus cv");
/* Check each possible CS for existing nand devices */
for (cs = 0; cs < NAND_NCS; cs++) {
nand_debug(NDBG_BUS,"probe chip select %x", cs);
/* Select & reset chip */
if (nandbus_select_cs(dev, cs))
break;
if (nand_reset(dev))
continue;
/* Read manufacturer and device id */
if (nand_readid(dev, &chip_id.man_id, &chip_id.dev_id))
continue;
if (chip_id.man_id == 0xff)
continue;
/*
* First try to get info from the table. If that fails, see if
* the chip can provide ONFI info. We check the table first to
* allow table entries to override info from chips that are
* known to provide bad ONFI data.
*/
onfi = 0;
chip_params = nand_get_params(&chip_id);
if (chip_params == NULL) {
nand_probe_onfi(dev, &onfi);
}
/*
* At this point it appears there is a chip at this chipselect,
* so if we can't work with it, whine about it.
*/
if (chip_params == NULL && onfi == 0) {
if (bootverbose || (nand_debug_flag & NDBG_BUS))
printf("Chip params not found, chipsel: %d "
"(manuf: 0x%0x, chipid: 0x%0x, onfi: %d)\n",
cs, chip_id.man_id, chip_id.dev_id, onfi);
continue;
}
ivar = malloc(sizeof(struct nandbus_ivar),
M_NAND, M_WAITOK);
if (onfi == 1) {
ivar->cs = cs;
ivar->cols = 0;
ivar->rows = 0;
ivar->params = NULL;
ivar->man_id = chip_id.man_id;
ivar->dev_id = chip_id.dev_id;
ivar->is_onfi = onfi;
ivar->chip_cdev_name = nfc_sc->chip_cdev_name;
child = device_add_child(dev, NULL, -1);
device_set_ivars(child, ivar);
continue;
}
ivar->cs = cs;
ivar->cols = 1;
ivar->rows = 2;
ivar->params = chip_params;
ivar->man_id = chip_id.man_id;
ivar->dev_id = chip_id.dev_id;
ivar->is_onfi = onfi;
ivar->chip_cdev_name = nfc_sc->chip_cdev_name;
/*
* Check what type of device we have.
* devices bigger than 32MiB have on more row (3)
*/
if (chip_params->chip_size > 32)
ivar->rows++;
/* Large page devices have one more col (2) */
if (chip_params->chip_size >= 128 &&
chip_params->page_size > 512)
ivar->cols++;
child = device_add_child(dev, NULL, -1);
device_set_ivars(child, ivar);
}
bus_generic_attach(dev);
return (0);
}
static int
nandbus_detach(device_t dev)
{
struct nandbus_softc *sc;
sc = device_get_softc(dev);
bus_generic_detach(dev);
mtx_destroy(&sc->nandbus_mtx);
cv_destroy(&sc->nandbus_cv);
return (0);
}
static int
nandbus_child_location_str(device_t bus, device_t child, char *buf,
size_t buflen)
{
struct nandbus_ivar *ivar = device_get_ivars(child);
snprintf(buf, buflen, "at cs#%d", ivar->cs);
return (0);
}
static int
nandbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
size_t buflen)
{
// XXX man id, model id ????
*buf = '\0';
return (0);
}
static int
nand_readid(device_t bus, uint8_t *man_id, uint8_t *dev_id)
{
device_t nfc;
if (!bus || !man_id || !dev_id)
return (EINVAL);
nand_debug(NDBG_BUS,"read id");
nfc = device_get_parent(bus);
if (NFC_SEND_COMMAND(nfc, NAND_CMD_READ_ID)) {
nand_debug(NDBG_BUS,"Error : could not send READ ID command");
return (ENXIO);
}
if (NFC_SEND_ADDRESS(nfc, 0)) {
nand_debug(NDBG_BUS,"Error : could not sent address to chip");
return (ENXIO);
}
if (NFC_START_COMMAND(nfc) != 0) {
nand_debug(NDBG_BUS,"Error : could not start command");
return (ENXIO);
}
DELAY(25);
*man_id = NFC_READ_BYTE(nfc);
*dev_id = NFC_READ_BYTE(nfc);
nand_debug(NDBG_BUS,"manufacturer id: %x chip id: %x", *man_id,
*dev_id);
return (0);
}
static int
nand_probe_onfi(device_t bus, uint8_t *onfi_compliant)
{
device_t nfc;
char onfi_id[] = {'O', 'N', 'F', 'I', '\0'};
int i;
nand_debug(NDBG_BUS,"probing ONFI");
nfc = device_get_parent(bus);
if (NFC_SEND_COMMAND(nfc, NAND_CMD_READ_ID)) {
nand_debug(NDBG_BUS,"Error : could not sent READ ID command");
return (ENXIO);
}
if (NFC_SEND_ADDRESS(nfc, ONFI_SIG_ADDR)) {
nand_debug(NDBG_BUS,"Error : could not sent address to chip");
return (ENXIO);
}
if (NFC_START_COMMAND(nfc) != 0) {
nand_debug(NDBG_BUS,"Error : could not start command");
return (ENXIO);
}
for (i = 0; onfi_id[i] != '\0'; i++)
if (NFC_READ_BYTE(nfc) != onfi_id[i]) {
nand_debug(NDBG_BUS,"ONFI non-compliant");
*onfi_compliant = 0;
return (0);
}
nand_debug(NDBG_BUS,"ONFI compliant");
*onfi_compliant = 1;
return (0);
}
static int
nand_reset(device_t bus)
{
device_t nfc;
nand_debug(NDBG_BUS,"resetting...");
nfc = device_get_parent(bus);
if (NFC_SEND_COMMAND(nfc, NAND_CMD_RESET) != 0) {
nand_debug(NDBG_BUS,"Error : could not sent RESET command");
return (ENXIO);
}
if (NFC_START_COMMAND(nfc) != 0) {
nand_debug(NDBG_BUS,"Error : could not start RESET command");
return (ENXIO);
}
DELAY(1000);
return (0);
}
void
nandbus_lock(device_t dev)
{
struct nandbus_softc *sc;
sc = device_get_softc(dev);
mtx_lock(&sc->nandbus_mtx);
if (sc->busy)
cv_wait(&sc->nandbus_cv, &sc->nandbus_mtx);
sc->busy = 1;
mtx_unlock(&sc->nandbus_mtx);
}
void
nandbus_unlock(device_t dev)
{
struct nandbus_softc *sc;
sc = device_get_softc(dev);
mtx_lock(&sc->nandbus_mtx);
sc->busy = 0;
cv_signal(&sc->nandbus_cv);
mtx_unlock(&sc->nandbus_mtx);
}
int
nandbus_select_cs(device_t dev, uint8_t cs)
{
return (NFC_SELECT_CS(device_get_parent(dev), cs));
}
int
nandbus_send_command(device_t dev, uint8_t command)
{
int err;
if ((err = NFC_SEND_COMMAND(device_get_parent(dev), command)))
nand_debug(NDBG_BUS,"Err: Could not send command %x, err %x",
command, err);
return (err);
}
int
nandbus_send_address(device_t dev, uint8_t address)
{
int err;
if ((err = NFC_SEND_ADDRESS(device_get_parent(dev), address)))
nand_debug(NDBG_BUS,"Err: Could not send address %x, err %x",
address, err);
return (err);
}
int
nandbus_start_command(device_t dev)
{
int err;
if ((err = NFC_START_COMMAND(device_get_parent(dev))))
nand_debug(NDBG_BUS,"Err: Could not start command, err %x",
err);
return (err);
}
void
nandbus_read_buffer(device_t dev, void *buf, uint32_t len)
{
NFC_READ_BUF(device_get_parent(dev), buf, len);
}
void
nandbus_write_buffer(device_t dev, void *buf, uint32_t len)
{
NFC_WRITE_BUF(device_get_parent(dev), buf, len);
}
int
nandbus_get_status(device_t dev, uint8_t *status)
{
int err;
if ((err = NANDBUS_SEND_COMMAND(dev, NAND_CMD_STATUS)))
return (err);
if ((err = NANDBUS_START_COMMAND(dev)))
return (err);
*status = NFC_READ_BYTE(device_get_parent(dev));
return (0);
}
int
nandbus_wait_ready(device_t dev, uint8_t *status)
{
struct timeval tv, tv2;
tv2.tv_sec = 0;
tv2.tv_usec = 50 * 5000; /* 250ms */
getmicrotime(&tv);
timevaladd(&tv, &tv2);
do {
if (NANDBUS_GET_STATUS(dev, status))
return (ENXIO);
if (*status & NAND_STATUS_RDY)
return (0);
getmicrotime(&tv2);
} while (timevalcmp(&tv2, &tv, <=));
return (EBUSY);
}
int
nandbus_get_ecc(device_t dev, void *buf, uint32_t pagesize, void *ecc,
int *needwrite)
{
return (NFC_GET_ECC(device_get_parent(dev), buf, pagesize, ecc, needwrite));
}
int
nandbus_correct_ecc(device_t dev, void *buf, int pagesize, void *readecc,
void *calcecc)
{
return (NFC_CORRECT_ECC(device_get_parent(dev), buf, pagesize,
readecc, calcecc));
}
|