2 * Copyright (C) 2009-2012 Semihalf
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/socket.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
42 #include <dev/nand/nand.h>
43 #include <dev/nand/nandbus.h>
45 #include "nandbus_if.h"
50 static int nandbus_probe(device_t dev);
51 static int nandbus_attach(device_t dev);
52 static int nandbus_detach(device_t dev);
54 static int nandbus_child_location_str(device_t, device_t, char *, size_t);
55 static int nandbus_child_pnpinfo_str(device_t, device_t, char *, size_t);
57 static int nandbus_get_status(device_t, uint8_t *);
58 static void nandbus_read_buffer(device_t, void *, uint32_t);
59 static int nandbus_select_cs(device_t, uint8_t);
60 static int nandbus_send_command(device_t, uint8_t);
61 static int nandbus_send_address(device_t, uint8_t);
62 static int nandbus_start_command(device_t);
63 static int nandbus_wait_ready(device_t, uint8_t *);
64 static void nandbus_write_buffer(device_t, void *, uint32_t);
65 static int nandbus_get_ecc(device_t, void *, uint32_t, void *, int *);
66 static int nandbus_correct_ecc(device_t, void *, int, void *, void *);
67 static void nandbus_lock(device_t);
68 static void nandbus_unlock(device_t);
70 static int nand_readid(device_t, uint8_t *, uint8_t *);
71 static int nand_probe_onfi(device_t, uint8_t *);
72 static int nand_reset(device_t);
74 struct nandbus_softc {
77 struct mtx nandbus_mtx;
81 static device_method_t nandbus_methods[] = {
82 /* device interface */
83 DEVMETHOD(device_probe, nandbus_probe),
84 DEVMETHOD(device_attach, nandbus_attach),
85 DEVMETHOD(device_detach, nandbus_detach),
86 DEVMETHOD(device_shutdown, bus_generic_shutdown),
89 DEVMETHOD(bus_print_child, bus_generic_print_child),
90 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
91 DEVMETHOD(bus_child_pnpinfo_str, nandbus_child_pnpinfo_str),
92 DEVMETHOD(bus_child_location_str, nandbus_child_location_str),
94 /* nandbus interface */
95 DEVMETHOD(nandbus_get_status, nandbus_get_status),
96 DEVMETHOD(nandbus_read_buffer, nandbus_read_buffer),
97 DEVMETHOD(nandbus_select_cs, nandbus_select_cs),
98 DEVMETHOD(nandbus_send_command, nandbus_send_command),
99 DEVMETHOD(nandbus_send_address, nandbus_send_address),
100 DEVMETHOD(nandbus_start_command,nandbus_start_command),
101 DEVMETHOD(nandbus_wait_ready, nandbus_wait_ready),
102 DEVMETHOD(nandbus_write_buffer, nandbus_write_buffer),
103 DEVMETHOD(nandbus_get_ecc, nandbus_get_ecc),
104 DEVMETHOD(nandbus_correct_ecc, nandbus_correct_ecc),
105 DEVMETHOD(nandbus_lock, nandbus_lock),
106 DEVMETHOD(nandbus_unlock, nandbus_unlock),
110 devclass_t nandbus_devclass;
112 driver_t nandbus_driver = {
115 sizeof(struct nandbus_softc)
118 DRIVER_MODULE(nandbus, nand, nandbus_driver, nandbus_devclass, 0, 0);
121 nandbus_create(device_t nfc)
125 child = device_add_child(nfc, "nandbus", -1);
129 bus_generic_attach(nfc);
135 nandbus_destroy(device_t nfc)
141 /* Detach & delete all children */
142 if (!device_get_children(nfc, &children, &nchildren)) {
143 for (i = 0; i < nchildren; i++)
144 device_delete_child(nfc, children[i]);
146 free(children, M_TEMP);
152 nandbus_probe(device_t dev)
155 device_set_desc(dev, "NAND bus");
161 nandbus_attach(device_t dev)
164 struct nand_id chip_id;
165 struct nandbus_softc *sc;
166 struct nandbus_ivar *ivar;
167 struct nand_softc *nfc_sc;
168 struct nand_params *chip_params;
171 sc = device_get_softc(dev);
174 nfc = device_get_parent(dev);
175 nfc_sc = device_get_softc(nfc);
177 mtx_init(&sc->nandbus_mtx, "nandbus lock", MTX_DEF, 0);
178 cv_init(&sc->nandbus_cv, "nandbus cv");
180 /* Check each possible CS for existing nand devices */
181 for (cs = 0; cs < NAND_NCS; cs++) {
182 nand_debug(NDBG_BUS,"probe chip select %x", cs);
184 /* Select & reset chip */
185 if (nandbus_select_cs(dev, cs))
191 /* Read manufacturer and device id */
192 if (nand_readid(dev, &chip_id.man_id, &chip_id.dev_id))
195 if (chip_id.man_id == 0xff)
198 /* Check if chip is ONFI compliant */
199 if (nand_probe_onfi(dev, &onfi) != 0) {
203 ivar = malloc(sizeof(struct nandbus_ivar),
211 ivar->man_id = chip_id.man_id;
212 ivar->dev_id = chip_id.dev_id;
213 ivar->is_onfi = onfi;
214 ivar->chip_cdev_name = nfc_sc->chip_cdev_name;
216 child = device_add_child(dev, NULL, -1);
217 device_set_ivars(child, ivar);
221 chip_params = nand_get_params(&chip_id);
222 if (chip_params == NULL) {
223 nand_debug(NDBG_BUS,"Chip description not found! "
224 "(manuf: 0x%0x, chipid: 0x%0x)\n",
225 chip_id.man_id, chip_id.dev_id);
233 ivar->params = chip_params;
234 ivar->man_id = chip_id.man_id;
235 ivar->dev_id = chip_id.dev_id;
236 ivar->is_onfi = onfi;
237 ivar->chip_cdev_name = nfc_sc->chip_cdev_name;
240 * Check what type of device we have.
241 * devices bigger than 32MiB have on more row (3)
243 if (chip_params->chip_size > 32)
245 /* Large page devices have one more col (2) */
246 if (chip_params->chip_size >= 128 &&
247 chip_params->page_size > 512)
250 child = device_add_child(dev, NULL, -1);
251 device_set_ivars(child, ivar);
254 bus_generic_attach(dev);
259 nandbus_detach(device_t dev)
261 struct nandbus_softc *sc;
263 sc = device_get_softc(dev);
265 bus_generic_detach(dev);
267 mtx_destroy(&sc->nandbus_mtx);
268 cv_destroy(&sc->nandbus_cv);
274 nandbus_child_location_str(device_t bus, device_t child, char *buf,
277 struct nandbus_ivar *ivar = device_get_ivars(child);
279 snprintf(buf, buflen, "at cs#%d", ivar->cs);
284 nandbus_child_pnpinfo_str(device_t bus, device_t child, char *buf,
287 // XXX man id, model id ????
293 nand_readid(device_t bus, uint8_t *man_id, uint8_t *dev_id)
297 if (!bus || !man_id || !dev_id)
300 nand_debug(NDBG_BUS,"read id");
302 nfc = device_get_parent(bus);
304 if (NFC_SEND_COMMAND(nfc, NAND_CMD_READ_ID)) {
305 nand_debug(NDBG_BUS,"Error : could not send READ ID command");
309 if (NFC_SEND_ADDRESS(nfc, 0)) {
310 nand_debug(NDBG_BUS,"Error : could not sent address to chip");
314 if (NFC_START_COMMAND(nfc) != 0) {
315 nand_debug(NDBG_BUS,"Error : could not start command");
321 *man_id = NFC_READ_BYTE(nfc);
322 *dev_id = NFC_READ_BYTE(nfc);
324 nand_debug(NDBG_BUS,"manufacturer id: %x chip id: %x", *man_id,
331 nand_probe_onfi(device_t bus, uint8_t *onfi_compliant)
334 char onfi_id[] = {'O', 'N', 'F', 'I', '\0'};
337 nand_debug(NDBG_BUS,"probing ONFI");
339 nfc = device_get_parent(bus);
341 if (NFC_SEND_COMMAND(nfc, NAND_CMD_READ_ID)) {
342 nand_debug(NDBG_BUS,"Error : could not sent READ ID command");
346 if (NFC_SEND_ADDRESS(nfc, ONFI_SIG_ADDR)) {
347 nand_debug(NDBG_BUS,"Error : could not sent address to chip");
351 if (NFC_START_COMMAND(nfc) != 0) {
352 nand_debug(NDBG_BUS,"Error : could not start command");
355 for (i = 0; onfi_id[i] != '\0'; i++)
356 if (NFC_READ_BYTE(nfc) != onfi_id[i]) {
357 nand_debug(NDBG_BUS,"ONFI non-compliant");
362 nand_debug(NDBG_BUS,"ONFI compliant");
369 nand_reset(device_t bus)
372 nand_debug(NDBG_BUS,"resetting...");
374 nfc = device_get_parent(bus);
376 if (NFC_SEND_COMMAND(nfc, NAND_CMD_RESET) != 0) {
377 nand_debug(NDBG_BUS,"Error : could not sent RESET command");
381 if (NFC_START_COMMAND(nfc) != 0) {
382 nand_debug(NDBG_BUS,"Error : could not start RESET command");
392 nandbus_lock(device_t dev)
394 struct nandbus_softc *sc;
396 sc = device_get_softc(dev);
398 mtx_lock(&sc->nandbus_mtx);
400 cv_wait(&sc->nandbus_cv, &sc->nandbus_mtx);
402 mtx_unlock(&sc->nandbus_mtx);
406 nandbus_unlock(device_t dev)
408 struct nandbus_softc *sc;
410 sc = device_get_softc(dev);
412 mtx_lock(&sc->nandbus_mtx);
414 cv_signal(&sc->nandbus_cv);
415 mtx_unlock(&sc->nandbus_mtx);
419 nandbus_select_cs(device_t dev, uint8_t cs)
422 return (NFC_SELECT_CS(device_get_parent(dev), cs));
426 nandbus_send_command(device_t dev, uint8_t command)
430 if ((err = NFC_SEND_COMMAND(device_get_parent(dev), command)))
431 nand_debug(NDBG_BUS,"Err: Could not send command %x, err %x",
438 nandbus_send_address(device_t dev, uint8_t address)
442 if ((err = NFC_SEND_ADDRESS(device_get_parent(dev), address)))
443 nand_debug(NDBG_BUS,"Err: Could not send address %x, err %x",
450 nandbus_start_command(device_t dev)
454 if ((err = NFC_START_COMMAND(device_get_parent(dev))))
455 nand_debug(NDBG_BUS,"Err: Could not start command, err %x",
462 nandbus_read_buffer(device_t dev, void *buf, uint32_t len)
465 NFC_READ_BUF(device_get_parent(dev), buf, len);
469 nandbus_write_buffer(device_t dev, void *buf, uint32_t len)
472 NFC_WRITE_BUF(device_get_parent(dev), buf, len);
476 nandbus_get_status(device_t dev, uint8_t *status)
480 if ((err = NANDBUS_SEND_COMMAND(dev, NAND_CMD_STATUS)))
482 if ((err = NANDBUS_START_COMMAND(dev)))
485 *status = NFC_READ_BYTE(device_get_parent(dev));
491 nandbus_wait_ready(device_t dev, uint8_t *status)
493 struct timeval tv, tv2;
496 tv2.tv_usec = 50 * 5000; /* 10ms */
499 timevaladd(&tv, &tv2);
502 if (NANDBUS_GET_STATUS(dev, status))
505 if (*status & NAND_STATUS_RDY)
509 } while (timevalcmp(&tv2, &tv, <=));
515 nandbus_get_ecc(device_t dev, void *buf, uint32_t pagesize, void *ecc,
519 return (NFC_GET_ECC(device_get_parent(dev), buf, pagesize, ecc, needwrite));
523 nandbus_correct_ecc(device_t dev, void *buf, int pagesize, void *readecc,
527 return (NFC_CORRECT_ECC(device_get_parent(dev), buf, pagesize,