xref: /netbsd-src/sys/arch/powerpc/booke/dev/pq3nandfcm.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*-
2  * Copyright (c) 2011 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Matt Thomas of 3am Software Foundry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #define LBC_PRIVATE
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: pq3nandfcm.c,v 1.4 2020/07/06 10:22:44 rin Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/cpu.h>
39 
40 #include <sys/bus.h>
41 
42 #include <powerpc/booke/cpuvar.h>
43 #include <powerpc/booke/e500reg.h>
44 #include <powerpc/booke/obiovar.h>
45 
46 #include <dev/nand/nand.h>
47 #include <dev/nand/onfi.h>
48 
49 static int  pq3nandfcm_match(device_t, cfdata_t, void *);
50 static void pq3nandfcm_attach(device_t, device_t, void *);
51 static int  pq3nandfcm_detach(device_t, int);
52 
53 static void pq3nandfcm_select(device_t, bool);
54 static void pq3nandfcm_command(device_t, uint8_t);
55 static void pq3nandfcm_address(device_t, uint8_t);
56 static void pq3nandfcm_busy(device_t);
57 static void pq3nandfcm_read_byte(device_t, uint8_t *);
58 static void pq3nandfcm_write_byte(device_t, uint8_t);
59 static void pq3nandfcm_read_buf(device_t, void *, size_t);
60 static void pq3nandfcm_write_buf(device_t, const void *, size_t);
61 
62 struct pq3nandfcm_softc {
63 	device_t sc_dev;
64 	bus_space_tag_t sc_window_bst;
65 	bus_space_handle_t sc_window_bsh;
66 	bus_size_t sc_window_size;
67 
68 	struct nand_interface sc_nandif;
69 	device_t sc_nanddev;
70 
71 	struct pq3obio_softc *sc_obio;
72 	struct pq3lbc_softc *sc_lbc;
73 
74 	u_int	sc_cs;
75 
76 };
77 
78 CFATTACH_DECL_NEW(pq3nandfcm, sizeof(struct pq3nandfcm_softc),
79      pq3nandfcm_match, pq3nandfcm_attach, pq3nandfcm_detach, NULL);
80 
81 int
82 pq3nandfcm_match(device_t parent, cfdata_t cf, void *aux)
83 {
84 	struct generic_attach_args * const ga = aux;
85 	struct pq3obio_softc * const psc = device_private(parent);
86 	struct pq3lbc_softc * const lbc = &psc->sc_lbcs[ga->ga_cs];
87 
88 	if ((lbc->lbc_br & BR_V) == 0)
89 		return 0;
90 
91 	if (__SHIFTOUT(lbc->lbc_br,BR_MSEL) != BR_MSEL_FCM)
92 		return 0;
93 
94 	return 1;
95 }
96 
97 void
98 pq3nandfcm_attach(device_t parent, device_t self, void *aux)
99 {
100 	struct generic_attach_args * const ga = aux;
101 	struct pq3nandfcm_softc * const sc = device_private(self);
102 	struct pq3obio_softc * const psc = device_private(parent);
103 	struct pq3lbc_softc * const lbc = &psc->sc_lbcs[ga->ga_cs];
104 
105 	sc->sc_dev = self;
106 	sc->sc_obio = psc;
107 	sc->sc_lbc = lbc;
108 }
109 
110 int
111 pq3nandfcm_detach(device_t self, int flags)
112 {
113 	struct pq3nandfcm_softc * const sc = device_private(self);
114 	int rv = 0;
115 
116 	pmf_device_deregister(self);
117 
118 	if (sc->sc_nanddev != NULL)
119 		rv = config_detach(sc->sc_nanddev, flags);
120 
121 	bus_space_unmap(sc->sc_window_bst, sc->sc_window_bsh,
122 	    sc->sc_window_size);
123 	return rv;
124 }
125 void
126 pq3nandfcm_command(device_t self, uint8_t command)
127 {
128 	struct pq3nandfcm_softc * const sc = device_private(self);
129 
130 	lbc_lock(sc->sc_obio);
131 	lbc_write_4(sc->sc_obio, FCR, __SHIFTIN(command, FCR_CMD0));
132 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_CM0, FIR_OP0));
133 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
134 	lbc_unlock(sc->sc_obio);
135 
136 }
137 
138 void
139 pq3nandfcm_address(device_t self, uint8_t address)
140 {
141 	struct pq3nandfcm_softc * const sc = device_private(self);
142 
143 	lbc_lock(sc->sc_obio);
144 	lbc_write_4(sc->sc_obio, MDR, __SHIFTIN(address, MDR_AS0));
145 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_UA, FIR_OP0));
146 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
147 	lbc_unlock(sc->sc_obio);
148 }
149 
150 void
151 pq3nandfcm_busy(device_t self)
152 {
153 	struct pq3nandfcm_softc * const sc = device_private(self);
154 
155 	lbc_lock(sc->sc_obio);
156 	for (;;) {
157 		uint32_t v = lbc_read_4(sc->sc_obio, LTESR);
158 		if ((v & LTESR_CC) == 0) {
159 			/*
160 			 * The command is done but the device might not
161 			 * be ready since the CC doesn't check for that.
162 			 */
163 			break;
164 		}
165 		DELAY(1);
166 	}
167 	lbc_unlock(sc->sc_obio);
168 }
169 
170 void
171 pq3nandfcm_read_byte(device_t self, uint8_t *valp)
172 {
173 	struct pq3nandfcm_softc * const sc = device_private(self);
174 
175 	lbc_lock(sc->sc_obio);
176 	/*
177 	 * Make sure the device is ready before reading the byte.
178 	 */
179 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_RSW, FIR_OP0));
180 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
181 	uint32_t v = lbc_read_4(sc->sc_obio, MDR);
182 	lbc_unlock(sc->sc_obio);
183 
184 	*valp = (uint8_t) v;
185 }
186 
187 void
188 pq3nandfcm_write_byte(device_t self, uint8_t val)
189 {
190 	struct pq3nandfcm_softc * const sc = device_private(self);
191 
192 	lbc_lock(sc->sc_obio);
193 	lbc_write_4(sc->sc_obio, MDR, val);
194 	/*
195 	 * Make sure the device is ready before writing the byte.
196 	 */
197 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_WS, FIR_OP0));
198 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
199 	lbc_unlock(sc->sc_obio);
200 }
201 
202 void
203 pq3nandfcm_read_buf(device_t self, void *buf, size_t len)
204 {
205 	struct pq3nandfcm_softc * const sc = device_private(self);
206 	bus_size_t offset = 0;
207 	uint32_t *dp32 = buf;
208 
209 	KASSERT(len < 4096);
210 	KASSERT((len & 3) == 0);
211 	KASSERT(((uintptr_t)dp32 & 3) == 0);
212 
213 	lbc_lock(sc->sc_obio);
214 	lbc_write_4(sc->sc_obio, FCR, len);
215 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_RBW, FIR_OP0));
216 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
217 
218 	while (lbc_read_4(sc->sc_obio, LTESR) & LTESR_CC) {
219 		DELAY(1);
220 	}
221 	for (offset = 0; len >= 4; offset += 4, len -= 4) {
222 		*dp32++ = fcm_buf_read(sc, offset);
223 	}
224 	if (len) {
225 		const uint32_t mask = ~0 >> (8 * len);
226 		const uint32_t data = fcm_buf_read(sc, offset);
227 		*dp32 = (data & ~mask) | (*dp32 & mask);
228 	}
229 	lbc_unlock(sc->sc_obio);
230 }
231 
232 void
233 pq3nandfcm_write_buf(device_t self, const void *buf, size_t len)
234 {
235 	struct pq3nandfcm_softc * const sc = device_private(self);
236 	bus_size_t offset = 0;
237 	const uint32_t *dp32 = buf;
238 
239 	KASSERT(len < 4096);
240 	KASSERT((len & 3) == 0);
241 	KASSERT(((uintptr_t)dp32 & 3) == 0);
242 
243 	lbc_lock(sc->sc_obio);
244 	lbc_write_4(sc->sc_obio, FCR, len);
245 
246 	/*
247 	 * First we need to copy to the FCM buffer.  There will be a few extra
248 	 * bytes at the end but we don't care.
249 	 */
250 	for (len = roundup2(len, 4); offset < len; offset += 4, dp32++) {
251 		fcm_buf_write(sc, offset, *dp32);
252 	}
253 
254 	/*
255 	 * W
256 	 */
257 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_WB, FIR_OP0));
258 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
259 	while (lbc_read_4(sc->sc_obio, LTESR) & LTESR_CC) {
260 		DELAY(1);
261 	}
262 	lbc_unlock(sc->sc_obio);
263 }
264