2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2009-2012 Semihalf
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
44 #include <dev/nand/nand.h>
45 #include <dev/nand/nandbus.h>
47 #include "nandbus_if.h"
52 static int nandbus_probe(device_t dev);
53 static int nandbus_attach(device_t dev);
54 static int nandbus_detach(device_t dev);
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);
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);
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);
76 struct nandbus_softc {
79 struct mtx nandbus_mtx;
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),
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),
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),
112 devclass_t nandbus_devclass;
114 driver_t nandbus_driver = {
117 sizeof(struct nandbus_softc)
120 DRIVER_MODULE(nandbus, nand, nandbus_driver, nandbus_devclass, 0, 0);
123 nandbus_create(device_t nfc)
127 child = device_add_child(nfc, "nandbus", -1);
131 bus_generic_attach(nfc);
137 nandbus_destroy(device_t nfc)
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]);
148 free(children, M_TEMP);
154 nandbus_probe(device_t dev)
157 device_set_desc(dev, "NAND bus");
163 nandbus_attach(device_t dev)
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;
173 sc = device_get_softc(dev);
176 nfc = device_get_parent(dev);
177 nfc_sc = device_get_softc(nfc);
179 mtx_init(&sc->nandbus_mtx, "nandbus lock", NULL, MTX_DEF);
180 cv_init(&sc->nandbus_cv, "nandbus cv");
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);
186 /* Select & reset chip */
187 if (nandbus_select_cs(dev, cs))
193 /* Read manufacturer and device id */
194 if (nand_readid(dev, &chip_id.man_id, &chip_id.dev_id))
197 if (chip_id.man_id == 0xff)
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.
207 chip_params = nand_get_params(&chip_id);
208 if (chip_params == NULL) {
209 nand_probe_onfi(dev, &onfi);
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.
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);
224 ivar = malloc(sizeof(struct nandbus_ivar),
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;
237 child = device_add_child(dev, NULL, -1);
238 device_set_ivars(child, ivar);
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;
252 * Check what type of device we have.
253 * devices bigger than 32MiB have on more row (3)
255 if (chip_params->chip_size > 32)
257 /* Large page devices have one more col (2) */
258 if (chip_params->chip_size >= 128 &&
259 chip_params->page_size > 512)
262 child = device_add_child(dev, NULL, -1);
263 device_set_ivars(child, ivar);
266 bus_generic_attach(dev);
271 nandbus_detach(device_t dev)
273 struct nandbus_softc *sc;
275 sc = device_get_softc(dev);
277 bus_generic_detach(dev);
279 mtx_destroy(&sc->nandbus_mtx);
280 cv_destroy(&sc->nandbus_cv);
286 nandbus_child_location_str(device_t bus, device_t child, char *buf,
289 struct nandbus_ivar *ivar = device_get_ivars(child);
291 snprintf(buf, buflen, "at cs#%d", ivar->cs);
296 nandbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
299 // XXX man id, model id ????
305 nand_readid(device_t bus, uint8_t *man_id, uint8_t *dev_id)
309 if (!bus || !man_id || !dev_id)
312 nand_debug(NDBG_BUS,"read id");
314 nfc = device_get_parent(bus);
316 if (NFC_SEND_COMMAND(nfc, NAND_CMD_READ_ID)) {
317 nand_debug(NDBG_BUS,"Error : could not send READ ID command");
321 if (NFC_SEND_ADDRESS(nfc, 0)) {
322 nand_debug(NDBG_BUS,"Error : could not sent address to chip");
326 if (NFC_START_COMMAND(nfc) != 0) {
327 nand_debug(NDBG_BUS,"Error : could not start command");
333 *man_id = NFC_READ_BYTE(nfc);
334 *dev_id = NFC_READ_BYTE(nfc);
336 nand_debug(NDBG_BUS,"manufacturer id: %x chip id: %x", *man_id,
343 nand_probe_onfi(device_t bus, uint8_t *onfi_compliant)
346 char onfi_id[] = {'O', 'N', 'F', 'I', '\0'};
349 nand_debug(NDBG_BUS,"probing ONFI");
351 nfc = device_get_parent(bus);
353 if (NFC_SEND_COMMAND(nfc, NAND_CMD_READ_ID)) {
354 nand_debug(NDBG_BUS,"Error : could not sent READ ID command");
358 if (NFC_SEND_ADDRESS(nfc, ONFI_SIG_ADDR)) {
359 nand_debug(NDBG_BUS,"Error : could not sent address to chip");
363 if (NFC_START_COMMAND(nfc) != 0) {
364 nand_debug(NDBG_BUS,"Error : could not start command");
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");
374 nand_debug(NDBG_BUS,"ONFI compliant");
381 nand_reset(device_t bus)
384 nand_debug(NDBG_BUS,"resetting...");
386 nfc = device_get_parent(bus);
388 if (NFC_SEND_COMMAND(nfc, NAND_CMD_RESET) != 0) {
389 nand_debug(NDBG_BUS,"Error : could not sent RESET command");
393 if (NFC_START_COMMAND(nfc) != 0) {
394 nand_debug(NDBG_BUS,"Error : could not start RESET command");
404 nandbus_lock(device_t dev)
406 struct nandbus_softc *sc;
408 sc = device_get_softc(dev);
410 mtx_lock(&sc->nandbus_mtx);
412 cv_wait(&sc->nandbus_cv, &sc->nandbus_mtx);
414 mtx_unlock(&sc->nandbus_mtx);
418 nandbus_unlock(device_t dev)
420 struct nandbus_softc *sc;
422 sc = device_get_softc(dev);
424 mtx_lock(&sc->nandbus_mtx);
426 cv_signal(&sc->nandbus_cv);
427 mtx_unlock(&sc->nandbus_mtx);
431 nandbus_select_cs(device_t dev, uint8_t cs)
434 return (NFC_SELECT_CS(device_get_parent(dev), cs));
438 nandbus_send_command(device_t dev, uint8_t command)
442 if ((err = NFC_SEND_COMMAND(device_get_parent(dev), command)))
443 nand_debug(NDBG_BUS,"Err: Could not send command %x, err %x",
450 nandbus_send_address(device_t dev, uint8_t address)
454 if ((err = NFC_SEND_ADDRESS(device_get_parent(dev), address)))
455 nand_debug(NDBG_BUS,"Err: Could not send address %x, err %x",
462 nandbus_start_command(device_t dev)
466 if ((err = NFC_START_COMMAND(device_get_parent(dev))))
467 nand_debug(NDBG_BUS,"Err: Could not start command, err %x",
474 nandbus_read_buffer(device_t dev, void *buf, uint32_t len)
477 NFC_READ_BUF(device_get_parent(dev), buf, len);
481 nandbus_write_buffer(device_t dev, void *buf, uint32_t len)
484 NFC_WRITE_BUF(device_get_parent(dev), buf, len);
488 nandbus_get_status(device_t dev, uint8_t *status)
492 if ((err = NANDBUS_SEND_COMMAND(dev, NAND_CMD_STATUS)))
494 if ((err = NANDBUS_START_COMMAND(dev)))
497 *status = NFC_READ_BYTE(device_get_parent(dev));
503 nandbus_wait_ready(device_t dev, uint8_t *status)
505 struct timeval tv, tv2;
508 tv2.tv_usec = 50 * 5000; /* 250ms */
511 timevaladd(&tv, &tv2);
514 if (NANDBUS_GET_STATUS(dev, status))
517 if (*status & NAND_STATUS_RDY)
521 } while (timevalcmp(&tv2, &tv, <=));
527 nandbus_get_ecc(device_t dev, void *buf, uint32_t pagesize, void *ecc,
531 return (NFC_GET_ECC(device_get_parent(dev), buf, pagesize, ecc, needwrite));
535 nandbus_correct_ecc(device_t dev, void *buf, int pagesize, void *readecc,
539 return (NFC_CORRECT_ECC(device_get_parent(dev), buf, pagesize,