nfc_rb.c revision 282430
1282430Sjhibbits/*-
2282430Sjhibbits * Copyright (C) 2015 Justin Hibbits
3282430Sjhibbits * All rights reserved.
4282430Sjhibbits *
5282430Sjhibbits * Redistribution and use in source and binary forms, with or without
6282430Sjhibbits * modification, are permitted provided that the following conditions
7282430Sjhibbits * are met:
8282430Sjhibbits * 1. Redistributions of source code must retain the above copyright
9282430Sjhibbits *    notice, this list of conditions and the following disclaimer.
10282430Sjhibbits * 2. Redistributions in binary form must reproduce the above copyright
11282430Sjhibbits *    notice, this list of conditions and the following disclaimer in the
12282430Sjhibbits *    documentation and/or other materials provided with the distribution.
13282430Sjhibbits *
14282430Sjhibbits * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15282430Sjhibbits * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16282430Sjhibbits * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17282430Sjhibbits * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18282430Sjhibbits * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19282430Sjhibbits * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20282430Sjhibbits * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21282430Sjhibbits * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22282430Sjhibbits * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23282430Sjhibbits * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24282430Sjhibbits * SUCH DAMAGE.
25282430Sjhibbits */
26282430Sjhibbits
27282430Sjhibbits/* RouterBoard 600/800 NAND controller driver. */
28282430Sjhibbits
29282430Sjhibbits#include <sys/cdefs.h>
30282430Sjhibbits__FBSDID("$FreeBSD: head/sys/dev/nand/nfc_rb.c 282430 2015-05-04 20:36:00Z jhibbits $");
31282430Sjhibbits
32282430Sjhibbits#include <sys/param.h>
33282430Sjhibbits#include <sys/systm.h>
34282430Sjhibbits#include <sys/proc.h>
35282430Sjhibbits#include <sys/bus.h>
36282430Sjhibbits#include <sys/conf.h>
37282430Sjhibbits#include <sys/kernel.h>
38282430Sjhibbits#include <sys/module.h>
39282430Sjhibbits#include <sys/malloc.h>
40282430Sjhibbits#include <sys/rman.h>
41282430Sjhibbits#include <sys/lock.h>
42282430Sjhibbits#include <sys/mutex.h>
43282430Sjhibbits#include <sys/time.h>
44282430Sjhibbits
45282430Sjhibbits#include <machine/bus.h>
46282430Sjhibbits
47282430Sjhibbits#include <dev/ofw/ofw_bus.h>
48282430Sjhibbits#include <dev/ofw/ofw_bus_subr.h>
49282430Sjhibbits
50282430Sjhibbits#include <dev/nand/nand.h>
51282430Sjhibbits#include <dev/nand/nandbus.h>
52282430Sjhibbits#include "nfc_if.h"
53282430Sjhibbits#include "gpio_if.h"
54282430Sjhibbits
55282430Sjhibbits#define RB_NAND_DATA	(0x00)
56282430Sjhibbits
57282430Sjhibbitsstruct rb_nand_softc {
58282430Sjhibbits	struct nand_softc 	nand_dev;
59282430Sjhibbits	struct resource		*sc_mem;
60282430Sjhibbits	int			rid;
61282430Sjhibbits	device_t		sc_gpio;
62282430Sjhibbits	uint32_t		sc_rdy_pin;
63282430Sjhibbits	uint32_t		sc_nce_pin;
64282430Sjhibbits	uint32_t		sc_cle_pin;
65282430Sjhibbits	uint32_t		sc_ale_pin;
66282430Sjhibbits};
67282430Sjhibbits
68282430Sjhibbitsstatic int	rb_nand_attach(device_t);
69282430Sjhibbitsstatic int	rb_nand_probe(device_t);
70282430Sjhibbitsstatic int	rb_nand_send_command(device_t, uint8_t);
71282430Sjhibbitsstatic int	rb_nand_send_address(device_t, uint8_t);
72282430Sjhibbitsstatic uint8_t	rb_nand_read_byte(device_t);
73282430Sjhibbitsstatic void	rb_nand_read_buf(device_t, void *, uint32_t);
74282430Sjhibbitsstatic void	rb_nand_write_buf(device_t, void *, uint32_t);
75282430Sjhibbitsstatic int	rb_nand_select_cs(device_t, uint8_t);
76282430Sjhibbitsstatic int	rb_nand_read_rnb(device_t);
77282430Sjhibbits
78282430Sjhibbitsstatic device_method_t rb_nand_methods[] = {
79282430Sjhibbits	DEVMETHOD(device_probe,		rb_nand_probe),
80282430Sjhibbits	DEVMETHOD(device_attach,	rb_nand_attach),
81282430Sjhibbits
82282430Sjhibbits	DEVMETHOD(nfc_send_command,	rb_nand_send_command),
83282430Sjhibbits	DEVMETHOD(nfc_send_address,	rb_nand_send_address),
84282430Sjhibbits	DEVMETHOD(nfc_read_byte,	rb_nand_read_byte),
85282430Sjhibbits	DEVMETHOD(nfc_read_buf,		rb_nand_read_buf),
86282430Sjhibbits	DEVMETHOD(nfc_write_buf,	rb_nand_write_buf),
87282430Sjhibbits	DEVMETHOD(nfc_select_cs,	rb_nand_select_cs),
88282430Sjhibbits	DEVMETHOD(nfc_read_rnb,		rb_nand_read_rnb),
89282430Sjhibbits
90282430Sjhibbits	{ 0, 0 },
91282430Sjhibbits};
92282430Sjhibbits
93282430Sjhibbitsstatic driver_t rb_nand_driver = {
94282430Sjhibbits	"nand",
95282430Sjhibbits	rb_nand_methods,
96282430Sjhibbits	sizeof(struct rb_nand_softc),
97282430Sjhibbits};
98282430Sjhibbits
99282430Sjhibbitsstatic devclass_t rb_nand_devclass;
100282430SjhibbitsDRIVER_MODULE(rb_nand, ofwbus, rb_nand_driver, rb_nand_devclass, 0, 0);
101282430Sjhibbits
102282430Sjhibbits#if 0
103282430Sjhibbitsstatic const struct nand_ecc_data rb_ecc = {
104282430Sjhibbits	.eccsize = 6,
105282430Sjhibbits	.eccmode = NAND_ECC_SOFT,
106282430Sjhibbits	.eccbytes = 6,
107282430Sjhibbits	.eccpositions = { 8, 9, 10, 13, 14, 15 },
108282430Sjhibbits};
109282430Sjhibbits#endif
110282430Sjhibbits
111282430Sjhibbitsstatic int
112282430Sjhibbitsrb_nand_probe(device_t dev)
113282430Sjhibbits{
114282430Sjhibbits	const char *device_type;
115282430Sjhibbits
116282430Sjhibbits	device_type = ofw_bus_get_type(dev);
117282430Sjhibbits
118282430Sjhibbits	if (!device_type || strcmp(device_type, "rb,nand"))
119282430Sjhibbits		return (ENXIO);
120282430Sjhibbits
121282430Sjhibbits	device_set_desc(dev, "RouterBoard 333/600/800 NAND controller");
122282430Sjhibbits	return (BUS_PROBE_DEFAULT);
123282430Sjhibbits}
124282430Sjhibbits
125282430Sjhibbitsstatic int
126282430Sjhibbitsrb_nand_attach(device_t dev)
127282430Sjhibbits{
128282430Sjhibbits	struct rb_nand_softc *sc;
129282430Sjhibbits	phandle_t node;
130282430Sjhibbits	uint32_t ale[2],cle[2],nce[2],rdy[2];
131282430Sjhibbits	int err;
132282430Sjhibbits
133282430Sjhibbits	sc = device_get_softc(dev);
134282430Sjhibbits	node = ofw_bus_get_node(dev);
135282430Sjhibbits
136282430Sjhibbits	if (OF_getprop(node, "ale", ale, sizeof(ale)) <= 0) {
137282430Sjhibbits		return (ENXIO);
138282430Sjhibbits	}
139282430Sjhibbits	if (OF_getprop(node, "cle", cle, sizeof(cle)) <= 0) {
140282430Sjhibbits		return (ENXIO);
141282430Sjhibbits	}
142282430Sjhibbits	if (OF_getprop(node, "nce", nce, sizeof(nce)) <= 0) {
143282430Sjhibbits		return (ENXIO);
144282430Sjhibbits	}
145282430Sjhibbits	if (OF_getprop(node, "rdy", rdy, sizeof(rdy)) <= 0) {
146282430Sjhibbits		return (ENXIO);
147282430Sjhibbits	}
148282430Sjhibbits
149282430Sjhibbits	if (ale[0] != cle[0] || ale[0] != nce[0] || ale[0] != rdy[0]) {
150282430Sjhibbits		device_printf(dev, "GPIO handles for signals must match.\n");
151282430Sjhibbits		return (ENXIO);
152282430Sjhibbits	}
153282430Sjhibbits	sc->sc_ale_pin = ale[1];
154282430Sjhibbits	sc->sc_cle_pin = cle[1];
155282430Sjhibbits	sc->sc_nce_pin = nce[1];
156282430Sjhibbits	sc->sc_rdy_pin = rdy[1];
157282430Sjhibbits
158282430Sjhibbits	sc->sc_gpio = OF_device_from_xref(ale[0]);
159282430Sjhibbits	if (sc->sc_gpio == NULL) {
160282430Sjhibbits		device_printf(dev, "No GPIO resource found!\n");
161282430Sjhibbits		return (ENXIO);
162282430Sjhibbits	}
163282430Sjhibbits
164282430Sjhibbits	sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
165282430Sjhibbits	    RF_ACTIVE);
166282430Sjhibbits	if (sc->sc_mem == NULL) {
167282430Sjhibbits		device_printf(dev, "could not allocate resources!\n");
168282430Sjhibbits		return (ENXIO);
169282430Sjhibbits	}
170282430Sjhibbits
171282430Sjhibbits	nand_init(&sc->nand_dev, dev, NAND_ECC_SOFT, 0, 0, NULL, NULL);
172282430Sjhibbits
173282430Sjhibbits	err = nandbus_create(dev);
174282430Sjhibbits
175282430Sjhibbits	return (err);
176282430Sjhibbits}
177282430Sjhibbits
178282430Sjhibbitsstatic int
179282430Sjhibbitsrb_nand_send_command(device_t dev, uint8_t command)
180282430Sjhibbits{
181282430Sjhibbits	struct rb_nand_softc *sc;
182282430Sjhibbits
183282430Sjhibbits	nand_debug(NDBG_DRV,"rb_nand: send command %x", command);
184282430Sjhibbits
185282430Sjhibbits	sc = device_get_softc(dev);
186282430Sjhibbits	GPIO_PIN_SET(sc->sc_gpio, sc->sc_cle_pin, 1);
187282430Sjhibbits	GPIO_PIN_SET(sc->sc_gpio, sc->sc_ale_pin, 0);
188282430Sjhibbits	GPIO_PIN_SET(sc->sc_gpio, sc->sc_nce_pin, 0);
189282430Sjhibbits	bus_write_1(sc->sc_mem, RB_NAND_DATA, command);
190282430Sjhibbits	GPIO_PIN_SET(sc->sc_gpio, sc->sc_cle_pin, 0);
191282430Sjhibbits	return (0);
192282430Sjhibbits}
193282430Sjhibbits
194282430Sjhibbitsstatic int
195282430Sjhibbitsrb_nand_send_address(device_t dev, uint8_t addr)
196282430Sjhibbits{
197282430Sjhibbits	struct rb_nand_softc *sc;
198282430Sjhibbits
199282430Sjhibbits	nand_debug(NDBG_DRV,"rb_nand: send address %x", addr);
200282430Sjhibbits
201282430Sjhibbits	sc = device_get_softc(dev);
202282430Sjhibbits	GPIO_PIN_SET(sc->sc_gpio, sc->sc_cle_pin, 0);
203282430Sjhibbits	GPIO_PIN_SET(sc->sc_gpio, sc->sc_ale_pin, 1);
204282430Sjhibbits	GPIO_PIN_SET(sc->sc_gpio, sc->sc_nce_pin, 0);
205282430Sjhibbits	bus_write_1(sc->sc_mem, RB_NAND_DATA, addr);
206282430Sjhibbits	GPIO_PIN_SET(sc->sc_gpio, sc->sc_ale_pin, 0);
207282430Sjhibbits	return (0);
208282430Sjhibbits}
209282430Sjhibbits
210282430Sjhibbitsstatic uint8_t
211282430Sjhibbitsrb_nand_read_byte(device_t dev)
212282430Sjhibbits{
213282430Sjhibbits	struct rb_nand_softc *sc;
214282430Sjhibbits	uint8_t data;
215282430Sjhibbits
216282430Sjhibbits	sc = device_get_softc(dev);
217282430Sjhibbits	data = bus_read_1(sc->sc_mem, RB_NAND_DATA);
218282430Sjhibbits
219282430Sjhibbits	nand_debug(NDBG_DRV,"rb_nand: read %x", data);
220282430Sjhibbits
221282430Sjhibbits	return (data);
222282430Sjhibbits}
223282430Sjhibbits
224282430Sjhibbitsstatic void
225282430Sjhibbitsrb_nand_read_buf(device_t dev, void* buf, uint32_t len)
226282430Sjhibbits{
227282430Sjhibbits	struct rb_nand_softc *sc;
228282430Sjhibbits
229282430Sjhibbits	sc = device_get_softc(dev);
230282430Sjhibbits
231282430Sjhibbits	bus_read_region_1(sc->sc_mem, RB_NAND_DATA, buf, len);
232282430Sjhibbits}
233282430Sjhibbits
234282430Sjhibbitsstatic void
235282430Sjhibbitsrb_nand_write_buf(device_t dev, void* buf, uint32_t len)
236282430Sjhibbits{
237282430Sjhibbits	struct rb_nand_softc *sc;
238282430Sjhibbits	int i;
239282430Sjhibbits	uint8_t *b = (uint8_t*)buf;
240282430Sjhibbits
241282430Sjhibbits	sc = device_get_softc(dev);
242282430Sjhibbits
243282430Sjhibbits	for (i = 0; i < len; i++) {
244282430Sjhibbits#ifdef NAND_DEBUG
245282430Sjhibbits		if (!(i % 16))
246282430Sjhibbits			printf("%s", i == 0 ? "rb_nand:\n" : "\n");
247282430Sjhibbits		printf(" %x", b[i]);
248282430Sjhibbits		if (i == len - 1)
249282430Sjhibbits			printf("\n");
250282430Sjhibbits#endif
251282430Sjhibbits		bus_write_1(sc->sc_mem, RB_NAND_DATA, b[i]);
252282430Sjhibbits	}
253282430Sjhibbits}
254282430Sjhibbits
255282430Sjhibbitsstatic int
256282430Sjhibbitsrb_nand_select_cs(device_t dev, uint8_t cs)
257282430Sjhibbits{
258282430Sjhibbits
259282430Sjhibbits	if (cs > 0)
260282430Sjhibbits		return (ENODEV);
261282430Sjhibbits
262282430Sjhibbits	return (0);
263282430Sjhibbits}
264282430Sjhibbits
265282430Sjhibbitsstatic int
266282430Sjhibbitsrb_nand_read_rnb(device_t dev)
267282430Sjhibbits{
268282430Sjhibbits	struct rb_nand_softc *sc;
269282430Sjhibbits	uint32_t rdy_bit;
270282430Sjhibbits
271282430Sjhibbits	sc = device_get_softc(dev);
272282430Sjhibbits	GPIO_PIN_GET(sc->sc_gpio, sc->sc_rdy_pin, &rdy_bit);
273282430Sjhibbits
274282430Sjhibbits	return (rdy_bit); /* ready */
275282430Sjhibbits}
276