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 /* Integrated NAND controller driver */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/malloc.h>
42 #include <sys/mutex.h>
45 #include <machine/bus.h>
46 #include <machine/fdt.h>
47 #include <arm/mv/mvvar.h>
48 #include <arm/mv/mvwin.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
53 #include <dev/nand/nand.h>
54 #include <dev/nand/nandbus.h>
57 #define MV_NAND_DATA (0x00)
58 #define MV_NAND_COMMAND (0x01)
59 #define MV_NAND_ADDRESS (0x02)
61 struct mv_nand_softc {
62 struct nand_softc nand_dev;
63 bus_space_handle_t sc_handle;
64 bus_space_tag_t sc_tag;
69 static int mv_nand_attach(device_t);
70 static int mv_nand_probe(device_t);
71 static int mv_nand_send_command(device_t, uint8_t);
72 static int mv_nand_send_address(device_t, uint8_t);
73 static uint8_t mv_nand_read_byte(device_t);
74 static void mv_nand_read_buf(device_t, void *, uint32_t);
75 static void mv_nand_write_buf(device_t, void *, uint32_t);
76 static int mv_nand_select_cs(device_t, uint8_t);
77 static int mv_nand_read_rnb(device_t);
79 static device_method_t mv_nand_methods[] = {
80 DEVMETHOD(device_probe, mv_nand_probe),
81 DEVMETHOD(device_attach, mv_nand_attach),
83 DEVMETHOD(nfc_send_command, mv_nand_send_command),
84 DEVMETHOD(nfc_send_address, mv_nand_send_address),
85 DEVMETHOD(nfc_read_byte, mv_nand_read_byte),
86 DEVMETHOD(nfc_read_buf, mv_nand_read_buf),
87 DEVMETHOD(nfc_write_buf, mv_nand_write_buf),
88 DEVMETHOD(nfc_select_cs, mv_nand_select_cs),
89 DEVMETHOD(nfc_read_rnb, mv_nand_read_rnb),
94 static driver_t mv_nand_driver = {
97 sizeof(struct mv_nand_softc),
100 static devclass_t mv_nand_devclass;
101 DRIVER_MODULE(mv_nand, localbus, mv_nand_driver, mv_nand_devclass, 0, 0);
104 mv_nand_probe(device_t dev)
107 if (!ofw_bus_is_compatible(dev, "mrvl,nfc"))
110 device_set_desc(dev, "Marvell NAND controller");
111 return (BUS_PROBE_DEFAULT);
115 mv_nand_attach(device_t dev)
117 struct mv_nand_softc *sc;
120 sc = device_get_softc(dev);
121 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
123 if (sc->res == NULL) {
124 device_printf(dev, "could not allocate resources!\n");
128 sc->sc_tag = rman_get_bustag(sc->res);
129 sc->sc_handle = rman_get_bushandle(sc->res);
131 nand_init(&sc->nand_dev, dev, NAND_ECC_SOFT, 0, 0, NULL, NULL);
133 err = nandbus_create(dev);
139 mv_nand_send_command(device_t dev, uint8_t command)
141 struct mv_nand_softc *sc;
143 nand_debug(NDBG_DRV,"mv_nand: send command %x", command);
145 sc = device_get_softc(dev);
146 bus_space_write_1(sc->sc_tag, sc->sc_handle, MV_NAND_COMMAND, command);
151 mv_nand_send_address(device_t dev, uint8_t addr)
153 struct mv_nand_softc *sc;
155 nand_debug(NDBG_DRV,"mv_nand: send address %x", addr);
157 sc = device_get_softc(dev);
158 bus_space_write_1(sc->sc_tag, sc->sc_handle, MV_NAND_ADDRESS, addr);
163 mv_nand_read_byte(device_t dev)
165 struct mv_nand_softc *sc;
168 sc = device_get_softc(dev);
169 data = bus_space_read_1(sc->sc_tag, sc->sc_handle, MV_NAND_DATA);
171 nand_debug(NDBG_DRV,"mv_nand: read %x", data);
177 mv_nand_read_buf(device_t dev, void* buf, uint32_t len)
179 struct mv_nand_softc *sc;
181 uint8_t *b = (uint8_t*)buf;
183 sc = device_get_softc(dev);
185 for (i = 0; i < len; i++) {
186 b[i] = bus_space_read_1(sc->sc_tag, sc->sc_handle,
190 printf("%s", i == 0 ? "mv_nand:\n" : "\n");
199 mv_nand_write_buf(device_t dev, void* buf, uint32_t len)
201 struct mv_nand_softc *sc;
203 uint8_t *b = (uint8_t*)buf;
205 sc = device_get_softc(dev);
207 for (i = 0; i < len; i++) {
210 printf("%s", i == 0 ? "mv_nand:\n" : "\n");
215 bus_space_write_1(sc->sc_tag, sc->sc_handle, MV_NAND_DATA,
221 mv_nand_select_cs(device_t dev, uint8_t cs)
231 mv_nand_read_rnb(device_t dev)
235 return (0); /* ready */