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 /* Integrated NAND controller driver */
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/malloc.h>
44 #include <sys/mutex.h>
47 #include <machine/bus.h>
48 #include <machine/fdt.h>
49 #include <arm/mv/mvvar.h>
50 #include <arm/mv/mvwin.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/nand/nand.h>
56 #include <dev/nand/nandbus.h>
59 #define MV_NAND_DATA (0x00)
60 #define MV_NAND_COMMAND (0x01)
61 #define MV_NAND_ADDRESS (0x02)
63 struct mv_nand_softc {
64 struct nand_softc nand_dev;
65 bus_space_handle_t sc_handle;
66 bus_space_tag_t sc_tag;
71 static int mv_nand_attach(device_t);
72 static int mv_nand_probe(device_t);
73 static int mv_nand_send_command(device_t, uint8_t);
74 static int mv_nand_send_address(device_t, uint8_t);
75 static uint8_t mv_nand_read_byte(device_t);
76 static void mv_nand_read_buf(device_t, void *, uint32_t);
77 static void mv_nand_write_buf(device_t, void *, uint32_t);
78 static int mv_nand_select_cs(device_t, uint8_t);
79 static int mv_nand_read_rnb(device_t);
81 static device_method_t mv_nand_methods[] = {
82 DEVMETHOD(device_probe, mv_nand_probe),
83 DEVMETHOD(device_attach, mv_nand_attach),
85 DEVMETHOD(nfc_send_command, mv_nand_send_command),
86 DEVMETHOD(nfc_send_address, mv_nand_send_address),
87 DEVMETHOD(nfc_read_byte, mv_nand_read_byte),
88 DEVMETHOD(nfc_read_buf, mv_nand_read_buf),
89 DEVMETHOD(nfc_write_buf, mv_nand_write_buf),
90 DEVMETHOD(nfc_select_cs, mv_nand_select_cs),
91 DEVMETHOD(nfc_read_rnb, mv_nand_read_rnb),
96 static driver_t mv_nand_driver = {
99 sizeof(struct mv_nand_softc),
102 static devclass_t mv_nand_devclass;
103 DRIVER_MODULE(mv_nand, localbus, mv_nand_driver, mv_nand_devclass, 0, 0);
106 mv_nand_probe(device_t dev)
109 if (!ofw_bus_is_compatible(dev, "mrvl,nfc"))
112 device_set_desc(dev, "Marvell NAND controller");
113 return (BUS_PROBE_DEFAULT);
117 mv_nand_attach(device_t dev)
119 struct mv_nand_softc *sc;
122 sc = device_get_softc(dev);
123 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
125 if (sc->res == NULL) {
126 device_printf(dev, "could not allocate resources!\n");
130 sc->sc_tag = rman_get_bustag(sc->res);
131 sc->sc_handle = rman_get_bushandle(sc->res);
133 nand_init(&sc->nand_dev, dev, NAND_ECC_SOFT, 0, 0, NULL, NULL);
135 err = nandbus_create(dev);
141 mv_nand_send_command(device_t dev, uint8_t command)
143 struct mv_nand_softc *sc;
145 nand_debug(NDBG_DRV,"mv_nand: send command %x", command);
147 sc = device_get_softc(dev);
148 bus_space_write_1(sc->sc_tag, sc->sc_handle, MV_NAND_COMMAND, command);
153 mv_nand_send_address(device_t dev, uint8_t addr)
155 struct mv_nand_softc *sc;
157 nand_debug(NDBG_DRV,"mv_nand: send address %x", addr);
159 sc = device_get_softc(dev);
160 bus_space_write_1(sc->sc_tag, sc->sc_handle, MV_NAND_ADDRESS, addr);
165 mv_nand_read_byte(device_t dev)
167 struct mv_nand_softc *sc;
170 sc = device_get_softc(dev);
171 data = bus_space_read_1(sc->sc_tag, sc->sc_handle, MV_NAND_DATA);
173 nand_debug(NDBG_DRV,"mv_nand: read %x", data);
179 mv_nand_read_buf(device_t dev, void* buf, uint32_t len)
181 struct mv_nand_softc *sc;
183 uint8_t *b = (uint8_t*)buf;
185 sc = device_get_softc(dev);
187 for (i = 0; i < len; i++) {
188 b[i] = bus_space_read_1(sc->sc_tag, sc->sc_handle,
192 printf("%s", i == 0 ? "mv_nand:\n" : "\n");
201 mv_nand_write_buf(device_t dev, void* buf, uint32_t len)
203 struct mv_nand_softc *sc;
205 uint8_t *b = (uint8_t*)buf;
207 sc = device_get_softc(dev);
209 for (i = 0; i < len; i++) {
212 printf("%s", i == 0 ? "mv_nand:\n" : "\n");
217 bus_space_write_1(sc->sc_tag, sc->sc_handle, MV_NAND_DATA,
223 mv_nand_select_cs(device_t dev, uint8_t cs)
233 mv_nand_read_rnb(device_t dev)
237 return (0); /* ready */