Home | History | Annotate | Line # | Download | only in sunxi
sunxi_sata.c revision 1.1.6.1
      1  1.1.6.1  christos /* $NetBSD: sunxi_sata.c,v 1.1.6.1 2019/06/10 22:05:57 christos Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmcneill  * modification, are permitted provided that the following conditions
      9      1.1  jmcneill  * are met:
     10      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmcneill  *
     16      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill #include <sys/cdefs.h>
     30      1.1  jmcneill 
     31  1.1.6.1  christos __KERNEL_RCSID(0, "$NetBSD: sunxi_sata.c,v 1.1.6.1 2019/06/10 22:05:57 christos Exp $");
     32      1.1  jmcneill 
     33      1.1  jmcneill #include <sys/param.h>
     34      1.1  jmcneill #include <sys/bus.h>
     35      1.1  jmcneill #include <sys/device.h>
     36      1.1  jmcneill #include <sys/intr.h>
     37      1.1  jmcneill #include <sys/systm.h>
     38      1.1  jmcneill 
     39      1.1  jmcneill #include <dev/ata/atavar.h>
     40      1.1  jmcneill #include <dev/ic/ahcisatavar.h>
     41      1.1  jmcneill 
     42      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     43      1.1  jmcneill 
     44      1.1  jmcneill static const char * compatible[] = {
     45      1.1  jmcneill 	"allwinner,sun4i-a10-ahci",
     46      1.1  jmcneill 	NULL
     47      1.1  jmcneill };
     48      1.1  jmcneill 
     49      1.1  jmcneill #define	SUNXI_SATA_DMACR(port)	(0x170 + AHCI_P_OFFSET(port))
     50      1.1  jmcneill 
     51      1.1  jmcneill static void
     52      1.1  jmcneill sunxi_sata_channel_start(struct ahci_softc *sc, struct ata_channel *chp)
     53      1.1  jmcneill {
     54      1.1  jmcneill 	const bus_size_t dma_reg = SUNXI_SATA_DMACR(chp->ch_channel);
     55      1.1  jmcneill 	uint32_t val;
     56      1.1  jmcneill 
     57      1.1  jmcneill 	val = AHCI_READ(sc, dma_reg);
     58  1.1.6.1  christos 	val &= ~0xffff;
     59  1.1.6.1  christos 	val |= 0x4433;
     60      1.1  jmcneill 	AHCI_WRITE(sc, dma_reg, val);
     61      1.1  jmcneill }
     62      1.1  jmcneill 
     63      1.1  jmcneill static int
     64      1.1  jmcneill sunxi_sata_match(device_t parent, cfdata_t cf, void *aux)
     65      1.1  jmcneill {
     66      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     67      1.1  jmcneill 
     68      1.1  jmcneill 	return of_match_compatible(faa->faa_phandle, compatible);
     69      1.1  jmcneill }
     70      1.1  jmcneill 
     71      1.1  jmcneill static void
     72      1.1  jmcneill sunxi_sata_attach(device_t parent, device_t self, void *aux)
     73      1.1  jmcneill {
     74      1.1  jmcneill 	struct ahci_softc * const sc = device_private(self);
     75      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     76      1.1  jmcneill 	const int phandle = faa->faa_phandle;
     77      1.1  jmcneill 	char intrstr[128];
     78      1.1  jmcneill 	struct clk *clk;
     79      1.1  jmcneill 	bus_addr_t addr;
     80      1.1  jmcneill 	bus_size_t size;
     81      1.1  jmcneill 	int i;
     82      1.1  jmcneill 
     83      1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     84      1.1  jmcneill 		aprint_error(": couldn't get registers\n");
     85      1.1  jmcneill 		return;
     86      1.1  jmcneill 	}
     87      1.1  jmcneill 
     88      1.1  jmcneill 	sc->sc_atac.atac_dev = self;
     89      1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
     90      1.1  jmcneill 	sc->sc_ahcit = faa->faa_bst;
     91      1.1  jmcneill 	sc->sc_ahcis = size;
     92      1.1  jmcneill 	if (bus_space_map(sc->sc_ahcit, addr, size, 0, &sc->sc_ahcih) != 0) {
     93      1.1  jmcneill 		aprint_error(": couldn't map registers\n");
     94      1.1  jmcneill 		return;
     95      1.1  jmcneill 	}
     96      1.1  jmcneill 	sc->sc_ahci_ports = 1;
     97      1.1  jmcneill 	sc->sc_ahci_quirks = AHCI_QUIRK_BADPMP;
     98      1.1  jmcneill 	sc->sc_save_init_data = true;
     99      1.1  jmcneill 	sc->sc_channel_start = sunxi_sata_channel_start;
    100      1.1  jmcneill 
    101      1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    102      1.1  jmcneill 		aprint_error(": failed to decode interrupt\n");
    103      1.1  jmcneill 		return;
    104      1.1  jmcneill 	}
    105      1.1  jmcneill 
    106      1.1  jmcneill 	for (i = 0; (clk = fdtbus_clock_get_index(phandle, i)) != NULL; i++)
    107      1.1  jmcneill 		if (clk_enable(clk) != 0) {
    108      1.1  jmcneill 			aprint_error(": couldn't enable clock #%d\n", i);
    109      1.1  jmcneill 			return;
    110      1.1  jmcneill 		}
    111      1.1  jmcneill 
    112      1.1  jmcneill 	aprint_naive("\n");
    113      1.1  jmcneill 	aprint_normal(": SATA\n");
    114      1.1  jmcneill 
    115      1.1  jmcneill 	if (fdtbus_intr_establish(phandle, 0, IPL_BIO, 0, ahci_intr, sc) == NULL) {
    116      1.1  jmcneill 		aprint_error_dev(self, "failed to establish interrupt on %s\n", intrstr);
    117      1.1  jmcneill 		return;
    118      1.1  jmcneill 	}
    119      1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    120      1.1  jmcneill 
    121      1.1  jmcneill 	ahci_attach(sc);
    122      1.1  jmcneill }
    123      1.1  jmcneill 
    124      1.1  jmcneill CFATTACH_DECL_NEW(sunxi_sata, sizeof(struct ahci_softc),
    125      1.1  jmcneill 	sunxi_sata_match, sunxi_sata_attach, NULL, NULL);
    126