2 * Copyright 2015 Alexander Kabaev <kan@FreeBSD.org>
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
28 * Ingenic JZ4780 NAND and External Memory Controller (NEMC) driver.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/resource.h>
46 #include <machine/bus.h>
48 #include <dev/extres/clk/clk.h>
50 #include <dev/fdt/fdt_common.h>
51 #include <dev/fdt/simplebus.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include <mips/ingenic/jz4780_regs.h>
57 struct jz4780_nemc_devinfo {
58 struct simplebus_devinfo sinfo;
62 struct jz4780_nemc_softc {
63 struct simplebus_softc simplebus_sc;
65 struct resource *res[1];
67 uint32_t clock_tick_psecs;
71 static struct resource_spec jz4780_nemc_spec[] = {
72 { SYS_RES_MEMORY, 0, RF_ACTIVE },
76 #define CSR_WRITE_4(sc, reg, val) bus_write_4((sc)->res[0], reg, (val))
77 #define CSR_READ_4(sc, reg) bus_read_4((sc)->res[0], reg)
79 static int jz4780_nemc_probe(device_t dev);
80 static int jz4780_nemc_attach(device_t dev);
81 static int jz4780_nemc_detach(device_t dev);
84 jz4780_nemc_probe(device_t dev)
87 if (!ofw_bus_status_okay(dev))
90 if (!ofw_bus_is_compatible(dev, "ingenic,jz4780-nemc"))
93 device_set_desc(dev, "Ingenic JZ4780 NEMC");
95 return (BUS_PROBE_DEFAULT);
98 #define JZ4780_NEMC_NS_TO_TICKS(sc, val) howmany((val) * 1000, (sc)->clock_tick_psecs)
100 /* Use table from JZ4780 programmers manual to convert ticks to tBP/tAW register values */
101 static const uint8_t ticks_to_tBP_tAW[32] = {
102 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, /* 1:1 mapping */
103 11, 11, /* 12 cycles */
104 12, 12, 12, /* 15 cycles */
105 13, 13, 13, 13, 13, /* 20 cycles */
106 14, 14, 14, 14, 14, /* 25 cycles */
107 15, 15, 15, 15, 15, 15 /* 31 cycles */
111 jz4780_nemc_configure_bank(struct jz4780_nemc_softc *sc,
112 device_t dev, u_int bank)
114 uint32_t smcr, cycles;
118 /* Check if bank is configured already */
119 if (sc->banks & (1 << bank))
122 smcr = CSR_READ_4(sc, JZ_NEMC_SMCR(bank));
124 smcr &= ~JZ_NEMC_SMCR_SMT_MASK;
125 smcr |= JZ_NEMC_SMCR_SMT_NORMAL << JZ_NEMC_SMCR_SMT_SHIFT;
127 node = ofw_bus_get_node(dev);
128 if (OF_getencprop(node, "ingenic,nemc-tAS", &val, sizeof(val)) > 0) {
129 cycles = JZ4780_NEMC_NS_TO_TICKS(sc, val);
131 device_printf(sc->dev,
132 "invalid value of %s %u (%u cycles), maximum %u cycles supported\n",
133 "ingenic,nemc-tAS", val, cycles, 15);
136 smcr &= ~JZ_NEMC_SMCR_TAS_MASK;
137 smcr |= cycles << JZ_NEMC_SMCR_TAS_SHIFT;
140 if (OF_getencprop(node, "ingenic,nemc-tAH", &val, sizeof(val)) > 0) {
141 cycles = JZ4780_NEMC_NS_TO_TICKS(sc, val);
143 device_printf(sc->dev,
144 "invalid value of %s %u (%u cycles), maximum %u cycles supported\n",
145 "ingenic,nemc-tAH", val, cycles, 15);
148 smcr &= ~JZ_NEMC_SMCR_TAH_MASK;
149 smcr |= cycles << JZ_NEMC_SMCR_TAH_SHIFT;
152 if (OF_getencprop(node, "ingenic,nemc-tBP", &val, sizeof(val)) > 0) {
153 cycles = JZ4780_NEMC_NS_TO_TICKS(sc, val);
155 device_printf(sc->dev,
156 "invalid value of %s %u (%u cycles), maximum %u cycles supported\n",
157 "ingenic,nemc-tBP", val, cycles, 15);
160 smcr &= ~JZ_NEMC_SMCR_TBP_MASK;
161 smcr |= ticks_to_tBP_tAW[cycles] << JZ_NEMC_SMCR_TBP_SHIFT;
164 if (OF_getencprop(node, "ingenic,nemc-tAW", &val, sizeof(val)) > 0) {
165 cycles = JZ4780_NEMC_NS_TO_TICKS(sc, val);
167 device_printf(sc->dev,
168 "invalid value of %s %u (%u cycles), maximum %u cycles supported\n",
169 "ingenic,nemc-tAW", val, cycles, 15);
172 smcr &= ~JZ_NEMC_SMCR_TAW_MASK;
173 smcr |= ticks_to_tBP_tAW[cycles] << JZ_NEMC_SMCR_TAW_SHIFT;
176 if (OF_getencprop(node, "ingenic,nemc-tSTRV", &val, sizeof(val)) > 0) {
177 cycles = JZ4780_NEMC_NS_TO_TICKS(sc, val);
179 device_printf(sc->dev,
180 "invalid value of %s %u (%u cycles), maximum %u cycles supported\n",
181 "ingenic,nemc-tSTRV", val, cycles, 15);
184 smcr &= ~JZ_NEMC_SMCR_STRV_MASK;
185 smcr |= cycles << JZ_NEMC_SMCR_STRV_SHIFT;
187 CSR_WRITE_4(sc, JZ_NEMC_SMCR(bank), smcr);
188 sc->banks |= (1 << bank);
192 /* Wholesale copy of simplebus routine */
194 jz4780_nemc_fill_ranges(phandle_t node, struct simplebus_softc *sc)
196 int host_address_cells;
198 ssize_t nbase_ranges;
202 err = OF_searchencprop(OF_parent(node), "#address-cells",
203 &host_address_cells, sizeof(host_address_cells));
207 nbase_ranges = OF_getproplen(node, "ranges");
208 if (nbase_ranges < 0)
210 sc->nranges = nbase_ranges / sizeof(cell_t) /
211 (sc->acells + host_address_cells + sc->scells);
212 if (sc->nranges == 0)
215 sc->ranges = malloc(sc->nranges * sizeof(sc->ranges[0]),
217 base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
218 OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
220 for (i = 0, j = 0; i < sc->nranges; i++) {
221 sc->ranges[i].bus = 0;
222 for (k = 0; k < sc->acells; k++) {
223 sc->ranges[i].bus <<= 32;
224 sc->ranges[i].bus |= base_ranges[j++];
226 sc->ranges[i].host = 0;
227 for (k = 0; k < host_address_cells; k++) {
228 sc->ranges[i].host <<= 32;
229 sc->ranges[i].host |= base_ranges[j++];
231 sc->ranges[i].size = 0;
232 for (k = 0; k < sc->scells; k++) {
233 sc->ranges[i].size <<= 32;
234 sc->ranges[i].size |= base_ranges[j++];
238 free(base_ranges, M_DEVBUF);
239 return (sc->nranges);
243 jz4780_nemc_attach(device_t dev)
245 struct jz4780_nemc_softc *sc = device_get_softc(dev);
251 if (bus_alloc_resources(dev, jz4780_nemc_spec, sc->res)) {
252 device_printf(dev, "could not allocate resources for device\n");
256 node = ofw_bus_get_node(dev);
258 /* Initialize simplebus and enumerate resources */
259 simplebus_init(dev, node);
261 if (jz4780_nemc_fill_ranges(node, &sc->simplebus_sc) < 0)
264 /* Figure our underlying clock rate. */
265 if (clk_get_by_ofw_index(dev, 0, 0, &sc->clk) != 0) {
266 device_printf(dev, "could not lookup device clock\n");
269 if (clk_enable(sc->clk) != 0) {
270 device_printf(dev, "could not enable device clock\n");
273 if (clk_get_freq(sc->clk, &freq) != 0) {
274 device_printf(dev, "could not determine clock speed\n");
278 /* Convert clock frequency to picoseconds-per-tick value. */
279 sc->clock_tick_psecs = (uint32_t)(1000000000000ULL / freq);
282 * Allow devices to identify.
284 bus_generic_probe(dev);
287 * Now walk the tree and attach top level devices
289 for (node = OF_child(node); node > 0; node = OF_peer(node))
290 simplebus_add_device(dev, node, 0, NULL, -1, NULL);
292 return (bus_generic_attach(dev));
294 jz4780_nemc_detach(dev);
299 jz4780_nemc_detach(device_t dev)
301 struct jz4780_nemc_softc *sc = device_get_softc(dev);
303 bus_generic_detach(dev);
305 clk_release(sc->clk);
306 bus_release_resources(dev, jz4780_nemc_spec, sc->res);
311 jz4780_nemc_decode_bank(struct simplebus_softc *sc, struct resource *r,
314 rman_res_t start, end;
317 start = rman_get_start(r);
318 end = rman_get_end(r);
320 /* Remap through ranges property */
321 for (i = 0; i < sc->nranges; i++) {
322 if (start >= sc->ranges[i].host && end <
323 sc->ranges[i].host + sc->ranges[i].size) {
324 *bank = (sc->ranges[i].bus >> 32);
332 jz4780_nemc_activate_resource(device_t bus, device_t child, int type, int rid,
335 struct jz4780_nemc_softc *sc;
339 if (type == SYS_RES_MEMORY) {
340 sc = device_get_softc(bus);
342 /* Figure out on what bank device is residing */
343 err = jz4780_nemc_decode_bank(&sc->simplebus_sc, r, &bank);
345 /* Attempt to configure the bank if not done already */
346 err = jz4780_nemc_configure_bank(sc, child, bank);
352 /* Call default implementation to finish the work */
353 return (bus_generic_activate_resource(bus, child,
357 static device_method_t jz4780_nemc_methods[] = {
358 /* Device interface */
359 DEVMETHOD(device_probe, jz4780_nemc_probe),
360 DEVMETHOD(device_attach, jz4780_nemc_attach),
361 DEVMETHOD(device_detach, jz4780_nemc_detach),
363 /* Overrides to configure bank on resource activation */
364 DEVMETHOD(bus_activate_resource, jz4780_nemc_activate_resource),
369 static devclass_t jz4780_nemc_devclass;
370 DEFINE_CLASS_1(nemc, jz4780_nemc_driver, jz4780_nemc_methods,
371 sizeof(struct jz4780_nemc_softc), simplebus_driver);
372 DRIVER_MODULE(jz4780_nemc, simplebus, jz4780_nemc_driver,
373 jz4780_nemc_devclass, 0, 0);