]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nand/nfc_rb.c
MFV r336955: 9236 nuke spa_dbgmsg
[FreeBSD/FreeBSD.git] / sys / dev / nand / nfc_rb.c
1 /*-
2  * Copyright (C) 2015 Justin Hibbits
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 /* RouterBoard 600/800 NAND controller driver. */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/rman.h>
39 #include <sys/slicer.h>
40
41 #include <geom/geom_disk.h>
42
43 #include <machine/bus.h>
44
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47
48 #include <dev/nand/nand.h>
49 #include <dev/nand/nandbus.h>
50
51 #include <powerpc/mpc85xx/mpc85xx.h>
52
53 #include "nfc_if.h"
54 #include "gpio_if.h"
55
56 #define RB_NAND_DATA    (0x00)
57
58 struct rb_nand_softc {
59         struct nand_softc       nand_dev;
60         struct resource         *sc_mem;
61         int                     rid;
62         device_t                sc_gpio;
63         uint32_t                sc_rdy_pin;
64         uint32_t                sc_nce_pin;
65         uint32_t                sc_cle_pin;
66         uint32_t                sc_ale_pin;
67 };
68
69 static int      rb_nand_attach(device_t);
70 static int      rb_nand_probe(device_t);
71 static int      rb_nand_send_command(device_t, uint8_t);
72 static int      rb_nand_send_address(device_t, uint8_t);
73 static uint8_t  rb_nand_read_byte(device_t);
74 static void     rb_nand_read_buf(device_t, void *, uint32_t);
75 static void     rb_nand_write_buf(device_t, void *, uint32_t);
76 static int      rb_nand_select_cs(device_t, uint8_t);
77 static int      rb_nand_read_rnb(device_t);
78
79 static device_method_t rb_nand_methods[] = {
80         DEVMETHOD(device_probe,         rb_nand_probe),
81         DEVMETHOD(device_attach,        rb_nand_attach),
82
83         DEVMETHOD(nfc_send_command,     rb_nand_send_command),
84         DEVMETHOD(nfc_send_address,     rb_nand_send_address),
85         DEVMETHOD(nfc_read_byte,        rb_nand_read_byte),
86         DEVMETHOD(nfc_read_buf,         rb_nand_read_buf),
87         DEVMETHOD(nfc_write_buf,        rb_nand_write_buf),
88         DEVMETHOD(nfc_select_cs,        rb_nand_select_cs),
89         DEVMETHOD(nfc_read_rnb,         rb_nand_read_rnb),
90
91         { 0, 0 },
92 };
93
94 static driver_t rb_nand_driver = {
95         "nand",
96         rb_nand_methods,
97         sizeof(struct rb_nand_softc),
98 };
99
100 static devclass_t rb_nand_devclass;
101 DRIVER_MODULE(rb_nand, ofwbus, rb_nand_driver, rb_nand_devclass, 0, 0);
102
103 #if 0
104 static const struct nand_ecc_data rb_ecc = {
105         .eccsize = 6,
106         .eccmode = NAND_ECC_SOFT,
107         .eccbytes = 6,
108         .eccpositions = { 8, 9, 10, 13, 14, 15 },
109 };
110 #endif
111
112 /* Slicer operates on the NAND controller, so we have to find the chip. */
113 static int
114 rb_nand_slicer(device_t dev, const char *provider __unused,
115     struct flash_slice *slices, int *nslices)
116 {
117         struct nand_chip *chip;
118         device_t *children;
119         int n;
120
121         if (device_get_children(dev, &children, &n) != 0) {
122                 panic("Slicer called on controller with no child!");
123         }
124         dev = children[0];
125         free(children, M_TEMP);
126
127         if (device_get_children(dev, &children, &n) != 0) {
128                 panic("Slicer called on controller with nandbus but no child!");
129         }
130         dev = children[0];
131         free(children, M_TEMP);
132
133         chip = device_get_softc(dev);
134         *nslices = 2;
135         slices[0].base = 0;
136         slices[0].size = 4 * 1024 * 1024;
137         slices[0].label = "boot";
138
139         slices[1].base = 4 * 1024 * 1024;
140         slices[1].size = chip->ndisk->d_mediasize - slices[0].size;
141         slices[1].label = "rootfs";
142
143         return (0);
144 }
145
146 static int
147 rb_nand_probe(device_t dev)
148 {
149         const char *device_type;
150
151         device_type = ofw_bus_get_type(dev);
152
153         if (!device_type || strcmp(device_type, "rb,nand"))
154                 return (ENXIO);
155
156         device_set_desc(dev, "RouterBoard 333/600/800 NAND controller");
157         return (BUS_PROBE_DEFAULT);
158 }
159
160 static int
161 rb_nand_attach(device_t dev)
162 {
163         struct rb_nand_softc *sc;
164         phandle_t node;
165         uint32_t ale[2],cle[2],nce[2],rdy[2];
166         u_long size,start;
167         int err;
168
169         sc = device_get_softc(dev);
170         node = ofw_bus_get_node(dev);
171
172         if (OF_getprop(node, "ale", ale, sizeof(ale)) <= 0) {
173                 return (ENXIO);
174         }
175         if (OF_getprop(node, "cle", cle, sizeof(cle)) <= 0) {
176                 return (ENXIO);
177         }
178         if (OF_getprop(node, "nce", nce, sizeof(nce)) <= 0) {
179                 return (ENXIO);
180         }
181         if (OF_getprop(node, "rdy", rdy, sizeof(rdy)) <= 0) {
182                 return (ENXIO);
183         }
184
185         if (ale[0] != cle[0] || ale[0] != nce[0] || ale[0] != rdy[0]) {
186                 device_printf(dev, "GPIO handles for signals must match.\n");
187                 return (ENXIO);
188         }
189         sc->sc_ale_pin = ale[1];
190         sc->sc_cle_pin = cle[1];
191         sc->sc_nce_pin = nce[1];
192         sc->sc_rdy_pin = rdy[1];
193
194         sc->sc_gpio = OF_device_from_xref(ale[0]);
195         if (sc->sc_gpio == NULL) {
196                 device_printf(dev, "No GPIO resource found!\n");
197                 return (ENXIO);
198         }
199
200         sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
201             RF_ACTIVE);
202         if (sc->sc_mem == NULL) {
203                 device_printf(dev, "could not allocate resources!\n");
204                 return (ENXIO);
205         }
206
207         start = rman_get_start(sc->sc_mem);
208         size = rman_get_size(sc->sc_mem);
209         if (law_enable(OCP85XX_TGTIF_LBC, start, size) != 0) {
210                 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->sc_mem);
211                 device_printf(dev, "could not allocate local address window.\n");
212                 return (ENXIO);
213         }
214
215         flash_register_slicer(rb_nand_slicer, FLASH_SLICES_TYPE_NAND, TRUE);
216
217         nand_init(&sc->nand_dev, dev, NAND_ECC_SOFT, 0, 0, NULL, NULL);
218
219         err = nandbus_create(dev);
220
221         return (err);
222 }
223
224 static int
225 rb_nand_send_command(device_t dev, uint8_t command)
226 {
227         struct rb_nand_softc *sc;
228
229         nand_debug(NDBG_DRV,"rb_nand: send command %x", command);
230
231         sc = device_get_softc(dev);
232         GPIO_PIN_SET(sc->sc_gpio, sc->sc_cle_pin, 1);
233         GPIO_PIN_SET(sc->sc_gpio, sc->sc_ale_pin, 0);
234         GPIO_PIN_SET(sc->sc_gpio, sc->sc_nce_pin, 0);
235         bus_write_1(sc->sc_mem, RB_NAND_DATA, command);
236         GPIO_PIN_SET(sc->sc_gpio, sc->sc_cle_pin, 0);
237         return (0);
238 }
239
240 static int
241 rb_nand_send_address(device_t dev, uint8_t addr)
242 {
243         struct rb_nand_softc *sc;
244
245         nand_debug(NDBG_DRV,"rb_nand: send address %x", addr);
246
247         sc = device_get_softc(dev);
248         GPIO_PIN_SET(sc->sc_gpio, sc->sc_cle_pin, 0);
249         GPIO_PIN_SET(sc->sc_gpio, sc->sc_ale_pin, 1);
250         GPIO_PIN_SET(sc->sc_gpio, sc->sc_nce_pin, 0);
251         bus_write_1(sc->sc_mem, RB_NAND_DATA, addr);
252         GPIO_PIN_SET(sc->sc_gpio, sc->sc_ale_pin, 0);
253         return (0);
254 }
255
256 static uint8_t
257 rb_nand_read_byte(device_t dev)
258 {
259         struct rb_nand_softc *sc;
260         uint8_t data;
261
262         sc = device_get_softc(dev);
263         data = bus_read_1(sc->sc_mem, RB_NAND_DATA);
264
265         nand_debug(NDBG_DRV,"rb_nand: read %x", data);
266
267         return (data);
268 }
269
270 static void
271 rb_nand_read_buf(device_t dev, void* buf, uint32_t len)
272 {
273         struct rb_nand_softc *sc;
274
275         sc = device_get_softc(dev);
276
277         bus_read_region_1(sc->sc_mem, RB_NAND_DATA, buf, len);
278 }
279
280 static void
281 rb_nand_write_buf(device_t dev, void* buf, uint32_t len)
282 {
283         struct rb_nand_softc *sc;
284         int i;
285         uint8_t *b = (uint8_t*)buf;
286
287         sc = device_get_softc(dev);
288
289         for (i = 0; i < len; i++) {
290 #ifdef NAND_DEBUG
291                 if (!(i % 16))
292                         printf("%s", i == 0 ? "rb_nand:\n" : "\n");
293                 printf(" %x", b[i]);
294                 if (i == len - 1)
295                         printf("\n");
296 #endif
297                 bus_write_1(sc->sc_mem, RB_NAND_DATA, b[i]);
298         }
299 }
300
301 static int
302 rb_nand_select_cs(device_t dev, uint8_t cs)
303 {
304
305         if (cs > 0)
306                 return (ENODEV);
307
308         return (0);
309 }
310
311 static int
312 rb_nand_read_rnb(device_t dev)
313 {
314         struct rb_nand_softc *sc;
315         uint32_t rdy_bit;
316
317         sc = device_get_softc(dev);
318         GPIO_PIN_GET(sc->sc_gpio, sc->sc_rdy_pin, &rdy_bit);
319
320         return (rdy_bit); /* ready */
321 }