Home | History | Annotate | Line # | Download | only in dev
pq3nandfcm.c revision 1.2
      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 #include "opt_flash.h"
     31  1.1    matt #define LBC_PRIVATE
     32  1.1    matt 
     33  1.1    matt #include <sys/cdefs.h>
     34  1.1    matt 
     35  1.2  dyoung __KERNEL_RCSID(0, "$NetBSD: pq3nandfcm.c,v 1.2 2011/07/17 23:08:56 dyoung Exp $");
     36  1.1    matt 
     37  1.1    matt #include <sys/param.h>
     38  1.1    matt #include <sys/systm.h>
     39  1.1    matt #include <sys/device.h>
     40  1.1    matt #include <sys/cpu.h>
     41  1.1    matt 
     42  1.2  dyoung #include <sys/bus.h>
     43  1.1    matt 
     44  1.1    matt #include <powerpc/booke/cpuvar.h>
     45  1.1    matt #include <powerpc/booke/e500reg.h>
     46  1.1    matt #include <powerpc/booke/obiovar.h>
     47  1.1    matt 
     48  1.1    matt #include <dev/nand/nand.h>
     49  1.1    matt #include <dev/nand/onfi.h>
     50  1.1    matt 
     51  1.1    matt static int  pq3nandfcm_match(device_t, cfdata_t, void *);
     52  1.1    matt static void pq3nandfcm_attach(device_t, device_t, void *);
     53  1.1    matt static int  pq3nandfcm_detach(device_t, int);
     54  1.1    matt 
     55  1.1    matt static void pq3nandfcm_select(device_t, bool);
     56  1.1    matt static void pq3nandfcm_command(device_t, uint8_t);
     57  1.1    matt static void pq3nandfcm_address(device_t, uint8_t);
     58  1.1    matt static void pq3nandfcm_busy(device_t);
     59  1.1    matt static void pq3nandfcm_read_byte(device_t, uint8_t *);
     60  1.1    matt static void pq3nandfcm_write_byte(device_t, uint8_t);
     61  1.1    matt static void pq3nandfcm_read_buf(device_t, void *, size_t);
     62  1.1    matt static void pq3nandfcm_write_buf(device_t, const void *, size_t);
     63  1.1    matt 
     64  1.1    matt struct pq3nandfcm_softc {
     65  1.1    matt 	device_t sc_dev;
     66  1.1    matt 	bus_space_tag_t sc_window_bst;
     67  1.1    matt 	bus_space_handle_t sc_window_bsh;
     68  1.1    matt 	bus_size_t sc_window_size;
     69  1.1    matt 
     70  1.1    matt 	struct nand_interface sc_nandif;
     71  1.1    matt 	device_t sc_nanddev;
     72  1.1    matt 
     73  1.1    matt 	struct pq3obio_softc *sc_obio;
     74  1.1    matt 	struct pq3lbc_softc *sc_lbc;
     75  1.1    matt 
     76  1.1    matt 	u_int	sc_cs;
     77  1.1    matt 
     78  1.1    matt };
     79  1.1    matt 
     80  1.1    matt CFATTACH_DECL_NEW(pq3nandfcm, sizeof(struct pq3nandfcm_softc),
     81  1.1    matt      pq3nandfcm_match, pq3nandfcm_attach, pq3nandfcm_detach, NULL);
     82  1.1    matt 
     83  1.1    matt int
     84  1.1    matt pq3nandfcm_match(device_t parent, cfdata_t cf, void *aux)
     85  1.1    matt {
     86  1.1    matt 	struct generic_attach_args * const ga = aux;
     87  1.1    matt 	struct pq3obio_softc * const psc = device_private(parent);
     88  1.1    matt 	struct pq3lbc_softc * const lbc = &psc->sc_lbcs[ga->ga_cs];
     89  1.1    matt 
     90  1.1    matt 	if ((lbc->lbc_br & BR_V) == 0)
     91  1.1    matt 		return 0;
     92  1.1    matt 
     93  1.1    matt 	if (__SHIFTOUT(lbc->lbc_br,BR_MSEL) != BR_MSEL_FCM)
     94  1.1    matt 		return 0;
     95  1.1    matt 
     96  1.1    matt 	return 1;
     97  1.1    matt }
     98  1.1    matt 
     99  1.1    matt void
    100  1.1    matt pq3nandfcm_attach(device_t parent, device_t self, void *aux)
    101  1.1    matt {
    102  1.1    matt 	struct generic_attach_args * const ga = aux;
    103  1.1    matt 	struct pq3nandfcm_softc * const sc = device_private(self);
    104  1.1    matt 	struct pq3obio_softc * const psc = device_private(parent);
    105  1.1    matt 	struct pq3lbc_softc * const lbc = &psc->sc_lbcs[ga->ga_cs];
    106  1.1    matt 
    107  1.1    matt 	sc->sc_dev = self;
    108  1.1    matt 	sc->sc_obio = psc;
    109  1.1    matt 	sc->sc_lbc = lbc;
    110  1.1    matt }
    111  1.1    matt 
    112  1.1    matt int
    113  1.1    matt pq3nandfcm_detach(device_t self, int flags)
    114  1.1    matt {
    115  1.1    matt 	struct pq3nandfcm_softc * const sc = device_private(self);
    116  1.1    matt 	int rv = 0;
    117  1.1    matt 
    118  1.1    matt 	pmf_device_deregister(self);
    119  1.1    matt 
    120  1.1    matt 	if (sc->sc_nanddev != NULL)
    121  1.1    matt 		rv = config_detach(sc->sc_nanddev, flags);
    122  1.1    matt 
    123  1.1    matt 	bus_space_unmap(sc->sc_window_bst, sc->sc_window_bsh,
    124  1.1    matt 	    sc->sc_window_size);
    125  1.1    matt 	return rv;
    126  1.1    matt }
    127  1.1    matt void
    128  1.1    matt pq3nandfcm_command(device_t self, uint8_t command)
    129  1.1    matt {
    130  1.1    matt 	struct pq3nandfcm_softc * const sc = device_private(self);
    131  1.1    matt 
    132  1.1    matt 	lbc_lock(sc->sc_obio);
    133  1.1    matt 	lbc_write_4(sc->sc_obio, FCR, __SHIFTIN(command, FCR_CMD0));
    134  1.1    matt 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_CM0, FIR_OP0));
    135  1.1    matt 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
    136  1.1    matt 	lbc_unlock(sc->sc_obio);
    137  1.1    matt 
    138  1.1    matt }
    139  1.1    matt 
    140  1.1    matt void
    141  1.1    matt pq3nandfcm_address(device_t self, uint8_t address)
    142  1.1    matt {
    143  1.1    matt 	struct pq3nandfcm_softc * const sc = device_private(self);
    144  1.1    matt 
    145  1.1    matt 	lbc_lock(sc->sc_obio);
    146  1.1    matt 	lbc_write_4(sc->sc_obio, MDR, __SHIFTIN(address, MDR_AS0));
    147  1.1    matt 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_UA, FIR_OP0));
    148  1.1    matt 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
    149  1.1    matt 	lbc_unlock(sc->sc_obio);
    150  1.1    matt }
    151  1.1    matt 
    152  1.1    matt void
    153  1.1    matt pq3nandfcm_busy(device_t self)
    154  1.1    matt {
    155  1.1    matt 	struct pq3nandfcm_softc * const sc = device_private(self);
    156  1.1    matt 
    157  1.1    matt 	lbc_lock(sc->sc_obio);
    158  1.1    matt 	for (;;) {
    159  1.1    matt 		uint32_t v = lbc_read_4(sc->sc_obio, LTESR);
    160  1.1    matt 		if ((v & LTESR_CC) == 0) {
    161  1.1    matt 			/*
    162  1.1    matt 			 * The command is done but the device might not
    163  1.1    matt 			 * be ready since the CC doesn't check for that.
    164  1.1    matt 			 */
    165  1.1    matt 			break;
    166  1.1    matt 		}
    167  1.1    matt 		DELAY(1);
    168  1.1    matt 	}
    169  1.1    matt 	lbc_unlock(sc->sc_obio);
    170  1.1    matt }
    171  1.1    matt 
    172  1.1    matt void
    173  1.1    matt pq3nandfcm_read_byte(device_t self, uint8_t *valp)
    174  1.1    matt {
    175  1.1    matt 	struct pq3nandfcm_softc * const sc = device_private(self);
    176  1.1    matt 
    177  1.1    matt 	lbc_lock(sc->sc_obio);
    178  1.1    matt 	/*
    179  1.1    matt 	 * Make sure the device is ready before reading the byte.
    180  1.1    matt 	 */
    181  1.1    matt 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_RSW, FIR_OP0));
    182  1.1    matt 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
    183  1.1    matt 	uint32_t v = lbc_read_4(sc->sc_obio, MDR);
    184  1.1    matt 	lbc_unlock(sc->sc_obio);
    185  1.1    matt 
    186  1.1    matt 	*valp = (uint8_t) v;
    187  1.1    matt }
    188  1.1    matt 
    189  1.1    matt void
    190  1.1    matt pq3nandfcm_write_byte(device_t self, uint8_t val)
    191  1.1    matt {
    192  1.1    matt 	struct pq3nandfcm_softc * const sc = device_private(self);
    193  1.1    matt 
    194  1.1    matt 	lbc_lock(sc->sc_obio);
    195  1.1    matt 	lbc_write_4(sc->sc_obio, MDR, val);
    196  1.1    matt 	/*
    197  1.1    matt 	 * Make sure the device is ready before writing the byte.
    198  1.1    matt 	 */
    199  1.1    matt 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_WS, FIR_OP0));
    200  1.1    matt 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
    201  1.1    matt 	lbc_unlock(sc->sc_obio);
    202  1.1    matt }
    203  1.1    matt 
    204  1.1    matt void
    205  1.1    matt pq3nandfcm_read_buf(device_t self, void *buf, size_t len)
    206  1.1    matt {
    207  1.1    matt 	struct pq3nandfcm_softc * const sc = device_private(self);
    208  1.1    matt 	bus_size_t offset = 0;
    209  1.1    matt 	uint32_t *dp32 = buf;
    210  1.1    matt 
    211  1.1    matt 	KASSERT(len < 4096);
    212  1.1    matt 	KASSERT((len & 3) == 0);
    213  1.1    matt 	KASSERT(((uintptr_t)dp32 & 3) == 0);
    214  1.1    matt 
    215  1.1    matt 	lbc_lock(sc->sc_obio);
    216  1.1    matt 	lbc_write_4(sc->sc_obio, FCR, len);
    217  1.1    matt 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_RBW, FIR_OP0));
    218  1.1    matt 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
    219  1.1    matt 
    220  1.1    matt 	while (lbc_read_4(sc->sc_obio, LTESR) & LTESR_CC) {
    221  1.1    matt 		DELAY(1);
    222  1.1    matt 	}
    223  1.1    matt 	for (offset = 0; len >= 4; offset += 4, len -= 4) {
    224  1.1    matt 		*dp32++ = fcm_buf_read(sc, offset);
    225  1.1    matt 	}
    226  1.1    matt 	if (len) {
    227  1.1    matt 		const uint32_t mask = ~0 >> (8 * len);
    228  1.1    matt 		const uint32_t data = fcm_buf_read(sc, offset);
    229  1.1    matt 		*dp32 = (data & ~mask) | (*dp32 & mask);
    230  1.1    matt 	}
    231  1.1    matt 	lbc_unlock(sc->sc_obio);
    232  1.1    matt }
    233  1.1    matt 
    234  1.1    matt void
    235  1.1    matt pq3nandfcm_write_buf(device_t self, const void *buf, size_t len)
    236  1.1    matt {
    237  1.1    matt 	struct pq3nandfcm_softc * const sc = device_private(self);
    238  1.1    matt 	bus_size_t offset = 0;
    239  1.1    matt 	const uint32_t *dp32 = buf;
    240  1.1    matt 
    241  1.1    matt 	KASSERT(len < 4096);
    242  1.1    matt 	KASSERT((len & 3) == 0);
    243  1.1    matt 	KASSERT(((uintptr_t)dp32 & 3) == 0);
    244  1.1    matt 
    245  1.1    matt 	lbc_lock(sc->sc_obio);
    246  1.1    matt 	lbc_write_4(sc->sc_obio, FCR, len);
    247  1.1    matt 
    248  1.1    matt 	/*
    249  1.1    matt 	 * First we need to copy to the FCM buffer.  There will be a few extra
    250  1.1    matt 	 * bytes at the end but we don't care.
    251  1.1    matt 	 */
    252  1.1    matt 	for (len = roundup2(len, 4); offset < len; offset += 4, dp32++) {
    253  1.1    matt 		fcm_buf_write(sc, offset, *dp32);
    254  1.1    matt 	}
    255  1.1    matt 
    256  1.1    matt 	/*
    257  1.1    matt 	 * W
    258  1.1    matt 	 */
    259  1.1    matt 	lbc_write_4(sc->sc_obio, FIR, __SHIFTIN(FIR_OP_WB, FIR_OP0));
    260  1.1    matt 	lbc_write_4(sc->sc_obio, LSOR, sc->sc_cs);
    261  1.1    matt 	while (lbc_read_4(sc->sc_obio, LTESR) & LTESR_CC) {
    262  1.1    matt 		DELAY(1);
    263  1.1    matt 	}
    264  1.1    matt 	lbc_unlock(sc->sc_obio);
    265  1.1    matt }
    266