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