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