1/* $NetBSD: ahcisata_fdt.c,v 1.3 2021/01/27 03:10:21 thorpej Exp $ */
2
3/*-
4 * Copyright (c) 2018 Jared McNeill <jmcneill@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: ahcisata_fdt.c,v 1.3 2021/01/27 03:10:21 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
44static const struct device_compatible_entry compat_data[] = {
45	{ .compat = "snps,dwc-ahci" },
46	{ .compat = "generic-ahci" },
47	DEVICE_COMPAT_EOL
48};
49
50static int
51ahcisata_fdt_match(device_t parent, cfdata_t cf, void *aux)
52{
53	struct fdt_attach_args * const faa = aux;
54
55	return of_compatible_match(faa->faa_phandle, compat_data);
56}
57
58static void
59ahcisata_fdt_attach(device_t parent, device_t self, void *aux)
60{
61	struct ahci_softc * const sc = device_private(self);
62	struct fdt_attach_args * const faa = aux;
63	const int phandle = faa->faa_phandle;
64	struct fdtbus_reset *rst;
65	struct clk *clk;
66	char intrstr[128];
67	bus_addr_t addr;
68	bus_size_t size;
69	int i;
70
71	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
72		aprint_error(": couldn't get registers\n");
73		return;
74	}
75
76	sc->sc_atac.atac_dev = self;
77	sc->sc_dmat = faa->faa_dmat;
78	sc->sc_ahcit = faa->faa_bst;
79	sc->sc_ahcis = size;
80	if (bus_space_map(sc->sc_ahcit, addr, size, 0, &sc->sc_ahcih) != 0) {
81		aprint_error(": couldn't map registers\n");
82		return;
83	}
84	if (of_getprop_uint32(phandle, "ports-implemented", &sc->sc_ahci_ports) != 0)
85		sc->sc_save_init_data = true;
86
87	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
88		aprint_error(": failed to decode interrupt\n");
89		return;
90	}
91
92	for (i = 0; (clk = fdtbus_clock_get_index(phandle, i)) != NULL; i++)
93		if (clk_enable(clk) != 0) {
94			aprint_error(": couldn't enable clock #%d\n", i);
95			return;
96		}
97	for (i = 0; (rst = fdtbus_reset_get_index(phandle, i)) != NULL; i++)
98		if (fdtbus_reset_deassert(rst) != 0) {
99			aprint_error(": couldn't de-assert reset #%d\n", i);
100			return;
101		}
102
103	aprint_naive("\n");
104	aprint_normal(": AHCI SATA controller\n");
105
106	if (fdtbus_intr_establish_xname(phandle, 0, IPL_BIO, 0,
107	    ahci_intr, sc, device_xname(self)) == NULL) {
108		aprint_error_dev(self,
109		    "failed to establish interrupt on %s\n", intrstr);
110		return;
111	}
112	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
113
114	ahci_attach(sc);
115}
116
117CFATTACH_DECL_NEW(ahcisata_fdt, sizeof(struct ahci_softc),
118	ahcisata_fdt_match, ahcisata_fdt_attach, NULL, NULL);
119