]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nand/nandbus.c
dts: Import DTS for arm64
[FreeBSD/FreeBSD.git] / sys / dev / nand / nandbus.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2009-2012 Semihalf
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/bus.h>
39 #include <sys/proc.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43
44 #include <dev/nand/nand.h>
45 #include <dev/nand/nandbus.h>
46 #include "nand_if.h"
47 #include "nandbus_if.h"
48 #include "nfc_if.h"
49
50 #define NAND_NCS 4
51
52 static int nandbus_probe(device_t dev);
53 static int nandbus_attach(device_t dev);
54 static int nandbus_detach(device_t dev);
55
56 static int nandbus_child_location_str(device_t, device_t, char *, size_t);
57 static int nandbus_child_pnpinfo_str(device_t, device_t, char *, size_t);
58
59 static int nandbus_get_status(device_t, uint8_t *);
60 static void nandbus_read_buffer(device_t, void *, uint32_t);
61 static int nandbus_select_cs(device_t, uint8_t);
62 static int nandbus_send_command(device_t, uint8_t);
63 static int nandbus_send_address(device_t, uint8_t);
64 static int nandbus_start_command(device_t);
65 static int nandbus_wait_ready(device_t, uint8_t *);
66 static void nandbus_write_buffer(device_t, void *, uint32_t);
67 static int nandbus_get_ecc(device_t, void *, uint32_t, void *, int *);
68 static int nandbus_correct_ecc(device_t, void *, int, void *, void *);
69 static void nandbus_lock(device_t);
70 static void nandbus_unlock(device_t);
71
72 static int nand_readid(device_t, uint8_t *, uint8_t *);
73 static int nand_probe_onfi(device_t, uint8_t *);
74 static int nand_reset(device_t);
75
76 struct nandbus_softc {
77         device_t dev;
78         struct cv nandbus_cv;
79         struct mtx nandbus_mtx;
80         uint8_t busy;
81 };
82
83 static device_method_t nandbus_methods[] = {
84         /* device interface */
85         DEVMETHOD(device_probe,         nandbus_probe),
86         DEVMETHOD(device_attach,        nandbus_attach),
87         DEVMETHOD(device_detach,        nandbus_detach),
88         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
89
90         /* bus interface */
91         DEVMETHOD(bus_print_child,      bus_generic_print_child),
92         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
93         DEVMETHOD(bus_child_pnpinfo_str, nandbus_child_pnpinfo_str),
94         DEVMETHOD(bus_child_location_str, nandbus_child_location_str),
95
96         /* nandbus interface */
97         DEVMETHOD(nandbus_get_status,   nandbus_get_status),
98         DEVMETHOD(nandbus_read_buffer,  nandbus_read_buffer),
99         DEVMETHOD(nandbus_select_cs,    nandbus_select_cs),
100         DEVMETHOD(nandbus_send_command, nandbus_send_command),
101         DEVMETHOD(nandbus_send_address, nandbus_send_address),
102         DEVMETHOD(nandbus_start_command,nandbus_start_command),
103         DEVMETHOD(nandbus_wait_ready,   nandbus_wait_ready),
104         DEVMETHOD(nandbus_write_buffer, nandbus_write_buffer),
105         DEVMETHOD(nandbus_get_ecc,      nandbus_get_ecc),
106         DEVMETHOD(nandbus_correct_ecc,  nandbus_correct_ecc),
107         DEVMETHOD(nandbus_lock,         nandbus_lock),
108         DEVMETHOD(nandbus_unlock,       nandbus_unlock),
109         { 0, 0 }
110 };
111
112 devclass_t nandbus_devclass;
113
114 driver_t nandbus_driver = {
115         "nandbus",
116         nandbus_methods,
117         sizeof(struct nandbus_softc)
118 };
119
120 DRIVER_MODULE(nandbus, nand, nandbus_driver, nandbus_devclass, 0, 0);
121
122 int
123 nandbus_create(device_t nfc)
124 {
125         device_t child;
126
127         child = device_add_child(nfc, "nandbus", -1);
128         if (!child)
129                 return (ENODEV);
130
131         bus_generic_attach(nfc);
132
133         return(0);
134 }
135
136 void
137 nandbus_destroy(device_t nfc)
138 {
139         device_t *children;
140         int nchildren, i;
141
142         mtx_lock(&Giant);
143         /* Detach & delete all children */
144         if (!device_get_children(nfc, &children, &nchildren)) {
145                 for (i = 0; i < nchildren; i++)
146                         device_delete_child(nfc, children[i]);
147
148                 free(children, M_TEMP);
149         }
150         mtx_unlock(&Giant);
151 }
152
153 static int
154 nandbus_probe(device_t dev)
155 {
156
157         device_set_desc(dev, "NAND bus");
158
159         return (0);
160 }
161
162 static int
163 nandbus_attach(device_t dev)
164 {
165         device_t child, nfc;
166         struct nand_id chip_id;
167         struct nandbus_softc *sc;
168         struct nandbus_ivar *ivar;
169         struct nand_softc *nfc_sc;
170         struct nand_params *chip_params;
171         uint8_t cs, onfi;
172
173         sc = device_get_softc(dev);
174         sc->dev = dev;
175
176         nfc = device_get_parent(dev);
177         nfc_sc = device_get_softc(nfc);
178
179         mtx_init(&sc->nandbus_mtx, "nandbus lock", NULL, MTX_DEF);
180         cv_init(&sc->nandbus_cv, "nandbus cv");
181
182         /* Check each possible CS for existing nand devices */
183         for (cs = 0; cs < NAND_NCS; cs++) {
184                 nand_debug(NDBG_BUS,"probe chip select %x", cs);
185
186                 /* Select & reset chip */
187                 if (nandbus_select_cs(dev, cs))
188                         break;
189
190                 if (nand_reset(dev))
191                         continue;
192
193                 /* Read manufacturer and device id */
194                 if (nand_readid(dev, &chip_id.man_id, &chip_id.dev_id))
195                         continue;
196
197                 if (chip_id.man_id == 0xff)
198                         continue;
199
200                 /*
201                  * First try to get info from the table.  If that fails, see if
202                  * the chip can provide ONFI info.  We check the table first to
203                  * allow table entries to override info from chips that are
204                  * known to provide bad ONFI data.
205                  */
206                 onfi = 0;
207                 chip_params = nand_get_params(&chip_id);
208                 if (chip_params == NULL) {
209                         nand_probe_onfi(dev, &onfi);
210                 }
211
212                 /*
213                  * At this point it appears there is a chip at this chipselect,
214                  * so if we can't work with it, whine about it.
215                  */
216                 if (chip_params == NULL && onfi == 0) {
217                         if (bootverbose || (nand_debug_flag & NDBG_BUS))
218                                 printf("Chip params not found, chipsel: %d "
219                                     "(manuf: 0x%0x, chipid: 0x%0x, onfi: %d)\n",
220                                     cs, chip_id.man_id, chip_id.dev_id, onfi);
221                         continue;
222                 }
223
224                 ivar = malloc(sizeof(struct nandbus_ivar),
225                     M_NAND, M_WAITOK);
226
227                 if (onfi == 1) {
228                         ivar->cs = cs;
229                         ivar->cols = 0;
230                         ivar->rows = 0;
231                         ivar->params = NULL;
232                         ivar->man_id = chip_id.man_id;
233                         ivar->dev_id = chip_id.dev_id;
234                         ivar->is_onfi = onfi;
235                         ivar->chip_cdev_name = nfc_sc->chip_cdev_name;
236
237                         child = device_add_child(dev, NULL, -1);
238                         device_set_ivars(child, ivar);
239                         continue;
240                 }
241
242                 ivar->cs = cs;
243                 ivar->cols = 1;
244                 ivar->rows = 2;
245                 ivar->params = chip_params;
246                 ivar->man_id = chip_id.man_id;
247                 ivar->dev_id = chip_id.dev_id;
248                 ivar->is_onfi = onfi;
249                 ivar->chip_cdev_name = nfc_sc->chip_cdev_name;
250
251                 /*
252                  * Check what type of device we have.
253                  * devices bigger than 32MiB have on more row (3)
254                  */
255                 if (chip_params->chip_size > 32)
256                         ivar->rows++;
257                 /* Large page devices have one more col (2) */
258                 if (chip_params->chip_size >= 128 &&
259                     chip_params->page_size > 512)
260                         ivar->cols++;
261
262                 child = device_add_child(dev, NULL, -1);
263                 device_set_ivars(child, ivar);
264         }
265
266         bus_generic_attach(dev);
267         return (0);
268 }
269
270 static int
271 nandbus_detach(device_t dev)
272 {
273         struct nandbus_softc *sc;
274
275         sc = device_get_softc(dev);
276
277         bus_generic_detach(dev);
278
279         mtx_destroy(&sc->nandbus_mtx);
280         cv_destroy(&sc->nandbus_cv);
281
282         return (0);
283 }
284
285 static int
286 nandbus_child_location_str(device_t bus, device_t child, char *buf,
287     size_t buflen)
288 {
289         struct nandbus_ivar *ivar = device_get_ivars(child);
290
291         snprintf(buf, buflen, "at cs#%d", ivar->cs);
292         return (0);
293 }
294
295 static int
296 nandbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
297     size_t buflen)
298 {
299         // XXX man id, model id ????
300         *buf = '\0';
301         return (0);
302 }
303
304 static int
305 nand_readid(device_t bus, uint8_t *man_id, uint8_t *dev_id)
306 {
307         device_t nfc;
308
309         if (!bus || !man_id || !dev_id)
310                 return (EINVAL);
311
312         nand_debug(NDBG_BUS,"read id");
313
314         nfc = device_get_parent(bus);
315
316         if (NFC_SEND_COMMAND(nfc, NAND_CMD_READ_ID)) {
317                 nand_debug(NDBG_BUS,"Error : could not send READ ID command");
318                 return (ENXIO);
319         }
320
321         if (NFC_SEND_ADDRESS(nfc, 0)) {
322                 nand_debug(NDBG_BUS,"Error : could not sent address to chip");
323                 return (ENXIO);
324         }
325
326         if (NFC_START_COMMAND(nfc) != 0) {
327                 nand_debug(NDBG_BUS,"Error : could not start command");
328                 return (ENXIO);
329         }
330
331         DELAY(25);
332
333         *man_id = NFC_READ_BYTE(nfc);
334         *dev_id = NFC_READ_BYTE(nfc);
335
336         nand_debug(NDBG_BUS,"manufacturer id: %x chip id: %x", *man_id,
337             *dev_id);
338
339         return (0);
340 }
341
342 static int
343 nand_probe_onfi(device_t bus, uint8_t *onfi_compliant)
344 {
345         device_t nfc;
346         char onfi_id[] = {'O', 'N', 'F', 'I', '\0'};
347         int i;
348
349         nand_debug(NDBG_BUS,"probing ONFI");
350
351         nfc = device_get_parent(bus);
352
353         if (NFC_SEND_COMMAND(nfc, NAND_CMD_READ_ID)) {
354                 nand_debug(NDBG_BUS,"Error : could not sent READ ID command");
355                 return (ENXIO);
356         }
357
358         if (NFC_SEND_ADDRESS(nfc, ONFI_SIG_ADDR)) {
359                 nand_debug(NDBG_BUS,"Error : could not sent address to chip");
360                 return (ENXIO);
361         }
362
363         if (NFC_START_COMMAND(nfc) != 0) {
364                 nand_debug(NDBG_BUS,"Error : could not start command");
365                 return (ENXIO);
366         }
367         for (i = 0; onfi_id[i] != '\0'; i++)
368                 if (NFC_READ_BYTE(nfc) != onfi_id[i]) {
369                         nand_debug(NDBG_BUS,"ONFI non-compliant");
370                         *onfi_compliant = 0;
371                         return (0);
372                 }
373
374         nand_debug(NDBG_BUS,"ONFI compliant");
375         *onfi_compliant = 1;
376
377         return (0);
378 }
379
380 static int
381 nand_reset(device_t bus)
382 {
383         device_t nfc;
384         nand_debug(NDBG_BUS,"resetting...");
385
386         nfc = device_get_parent(bus);
387
388         if (NFC_SEND_COMMAND(nfc, NAND_CMD_RESET) != 0) {
389                 nand_debug(NDBG_BUS,"Error : could not sent RESET command");
390                 return (ENXIO);
391         }
392
393         if (NFC_START_COMMAND(nfc) != 0) {
394                 nand_debug(NDBG_BUS,"Error : could not start RESET command");
395                 return (ENXIO);
396         }
397
398         DELAY(1000);
399
400         return (0);
401 }
402
403 void
404 nandbus_lock(device_t dev)
405 {
406         struct nandbus_softc *sc;
407
408         sc = device_get_softc(dev);
409
410         mtx_lock(&sc->nandbus_mtx);
411         if (sc->busy)
412                 cv_wait(&sc->nandbus_cv, &sc->nandbus_mtx);
413         sc->busy = 1;
414         mtx_unlock(&sc->nandbus_mtx);
415 }
416
417 void
418 nandbus_unlock(device_t dev)
419 {
420         struct nandbus_softc *sc;
421
422         sc = device_get_softc(dev);
423
424         mtx_lock(&sc->nandbus_mtx);
425         sc->busy = 0;
426         cv_signal(&sc->nandbus_cv);
427         mtx_unlock(&sc->nandbus_mtx);
428 }
429
430 int
431 nandbus_select_cs(device_t dev, uint8_t cs)
432 {
433
434         return (NFC_SELECT_CS(device_get_parent(dev), cs));
435 }
436
437 int
438 nandbus_send_command(device_t dev, uint8_t command)
439 {
440         int err;
441
442         if ((err = NFC_SEND_COMMAND(device_get_parent(dev), command)))
443                 nand_debug(NDBG_BUS,"Err: Could not send command %x, err %x",
444                     command, err);
445
446         return (err);
447 }
448
449 int
450 nandbus_send_address(device_t dev, uint8_t address)
451 {
452         int err;
453
454         if ((err = NFC_SEND_ADDRESS(device_get_parent(dev), address)))
455                 nand_debug(NDBG_BUS,"Err: Could not send address %x, err %x",
456                     address, err);
457
458         return (err);
459 }
460
461 int
462 nandbus_start_command(device_t dev)
463 {
464         int err;
465
466         if ((err = NFC_START_COMMAND(device_get_parent(dev))))
467                 nand_debug(NDBG_BUS,"Err: Could not start command, err %x",
468                     err);
469
470         return (err);
471 }
472
473 void
474 nandbus_read_buffer(device_t dev, void *buf, uint32_t len)
475 {
476
477         NFC_READ_BUF(device_get_parent(dev), buf, len);
478 }
479
480 void
481 nandbus_write_buffer(device_t dev, void *buf, uint32_t len)
482 {
483
484         NFC_WRITE_BUF(device_get_parent(dev), buf, len);
485 }
486
487 int
488 nandbus_get_status(device_t dev, uint8_t *status)
489 {
490         int err;
491
492         if ((err = NANDBUS_SEND_COMMAND(dev, NAND_CMD_STATUS)))
493                 return (err);
494         if ((err = NANDBUS_START_COMMAND(dev)))
495                 return (err);
496
497         *status = NFC_READ_BYTE(device_get_parent(dev));
498
499         return (0);
500 }
501
502 int
503 nandbus_wait_ready(device_t dev, uint8_t *status)
504 {
505         struct timeval tv, tv2;
506
507         tv2.tv_sec = 0;
508         tv2.tv_usec = 50 * 5000; /* 250ms */
509
510         getmicrotime(&tv);
511         timevaladd(&tv, &tv2);
512
513         do {
514                 if (NANDBUS_GET_STATUS(dev, status))
515                         return (ENXIO);
516
517                 if (*status & NAND_STATUS_RDY)
518                         return (0);
519
520                 getmicrotime(&tv2);
521         } while (timevalcmp(&tv2, &tv, <=));
522
523         return (EBUSY);
524 }
525
526 int
527 nandbus_get_ecc(device_t dev, void *buf, uint32_t pagesize, void *ecc,
528     int *needwrite)
529 {
530
531         return (NFC_GET_ECC(device_get_parent(dev), buf, pagesize, ecc, needwrite));
532 }
533
534 int
535 nandbus_correct_ecc(device_t dev, void *buf, int pagesize, void *readecc,
536     void *calcecc)
537 {
538
539         return (NFC_CORRECT_ECC(device_get_parent(dev), buf, pagesize,
540             readecc, calcecc));
541 }
542