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