ahcisata_acpi.c revision 1.3.2.2 1 1.3.2.2 pgoyette /* $NetBSD: ahcisata_acpi.c,v 1.3.2.2 2018/10/20 06:58:30 pgoyette Exp $ */
2 1.3.2.2 pgoyette
3 1.3.2.2 pgoyette /*-
4 1.3.2.2 pgoyette * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.3.2.2 pgoyette * All rights reserved.
6 1.3.2.2 pgoyette *
7 1.3.2.2 pgoyette * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 pgoyette * by Jared McNeill <jmcneill (at) invisible.ca>.
9 1.3.2.2 pgoyette *
10 1.3.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 pgoyette * modification, are permitted provided that the following conditions
12 1.3.2.2 pgoyette * are met:
13 1.3.2.2 pgoyette * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 pgoyette * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 pgoyette * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 pgoyette * documentation and/or other materials provided with the distribution.
18 1.3.2.2 pgoyette *
19 1.3.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.2.2 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.2.2 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.2.2 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.2.2 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.2.2 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.2.2 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.2.2 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.2.2 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.2.2 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.2.2 pgoyette * POSSIBILITY OF SUCH DAMAGE.
30 1.3.2.2 pgoyette */
31 1.3.2.2 pgoyette
32 1.3.2.2 pgoyette #include <sys/cdefs.h>
33 1.3.2.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: ahcisata_acpi.c,v 1.3.2.2 2018/10/20 06:58:30 pgoyette Exp $");
34 1.3.2.2 pgoyette
35 1.3.2.2 pgoyette #include <sys/param.h>
36 1.3.2.2 pgoyette #include <sys/bus.h>
37 1.3.2.2 pgoyette #include <sys/cpu.h>
38 1.3.2.2 pgoyette #include <sys/device.h>
39 1.3.2.2 pgoyette #include <sys/termios.h>
40 1.3.2.2 pgoyette
41 1.3.2.2 pgoyette #include <dev/ata/atavar.h>
42 1.3.2.2 pgoyette #include <dev/ic/ahcisatavar.h>
43 1.3.2.2 pgoyette
44 1.3.2.2 pgoyette #include <dev/acpi/acpireg.h>
45 1.3.2.2 pgoyette #include <dev/acpi/acpivar.h>
46 1.3.2.2 pgoyette
47 1.3.2.2 pgoyette #include <dev/pci/pcireg.h>
48 1.3.2.2 pgoyette
49 1.3.2.2 pgoyette static int ahcisata_acpi_match(device_t, cfdata_t, void *);
50 1.3.2.2 pgoyette static void ahcisata_acpi_attach(device_t, device_t, void *);
51 1.3.2.2 pgoyette
52 1.3.2.2 pgoyette CFATTACH_DECL_NEW(ahcisata_acpi, sizeof(struct ahci_softc), ahcisata_acpi_match, ahcisata_acpi_attach, NULL, NULL);
53 1.3.2.2 pgoyette
54 1.3.2.2 pgoyette static int
55 1.3.2.2 pgoyette ahcisata_acpi_match(device_t parent, cfdata_t cf, void *aux)
56 1.3.2.2 pgoyette {
57 1.3.2.2 pgoyette struct acpi_attach_args *aa = aux;
58 1.3.2.2 pgoyette
59 1.3.2.2 pgoyette if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
60 1.3.2.2 pgoyette return 0;
61 1.3.2.2 pgoyette
62 1.3.2.2 pgoyette return acpi_match_class(aa->aa_node->ad_handle,
63 1.3.2.2 pgoyette PCI_CLASS_MASS_STORAGE,
64 1.3.2.2 pgoyette PCI_SUBCLASS_MASS_STORAGE_SATA,
65 1.3.2.2 pgoyette PCI_INTERFACE_SATA_AHCI10);
66 1.3.2.2 pgoyette }
67 1.3.2.2 pgoyette
68 1.3.2.2 pgoyette static void
69 1.3.2.2 pgoyette ahcisata_acpi_attach(device_t parent, device_t self, void *aux)
70 1.3.2.2 pgoyette {
71 1.3.2.2 pgoyette struct ahci_softc * const sc = device_private(self);
72 1.3.2.2 pgoyette struct acpi_attach_args *aa = aux;
73 1.3.2.2 pgoyette struct acpi_resources res;
74 1.3.2.2 pgoyette struct acpi_mem *mem;
75 1.3.2.2 pgoyette struct acpi_irq *irq;
76 1.3.2.2 pgoyette ACPI_STATUS rv;
77 1.3.2.2 pgoyette void *ih;
78 1.3.2.2 pgoyette
79 1.3.2.2 pgoyette rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
80 1.3.2.2 pgoyette &res, &acpi_resource_parse_ops_default);
81 1.3.2.2 pgoyette if (ACPI_FAILURE(rv))
82 1.3.2.2 pgoyette return;
83 1.3.2.2 pgoyette
84 1.3.2.2 pgoyette mem = acpi_res_mem(&res, 0);
85 1.3.2.2 pgoyette if (mem == NULL) {
86 1.3.2.2 pgoyette aprint_error(": couldn't find mem resource\n");
87 1.3.2.2 pgoyette goto done;
88 1.3.2.2 pgoyette }
89 1.3.2.2 pgoyette
90 1.3.2.2 pgoyette irq = acpi_res_irq(&res, 0);
91 1.3.2.2 pgoyette if (irq == NULL) {
92 1.3.2.2 pgoyette aprint_error(": couldn't find irq resource\n");
93 1.3.2.2 pgoyette goto done;
94 1.3.2.2 pgoyette }
95 1.3.2.2 pgoyette
96 1.3.2.2 pgoyette sc->sc_atac.atac_dev = self;
97 1.3.2.2 pgoyette sc->sc_dmat = aa->aa_dmat;
98 1.3.2.2 pgoyette sc->sc_ahcit = aa->aa_memt;
99 1.3.2.2 pgoyette sc->sc_ahcis = mem->ar_length;
100 1.3.2.2 pgoyette if (bus_space_map(aa->aa_memt, mem->ar_base, mem->ar_length, 0, &sc->sc_ahcih) != 0) {
101 1.3.2.2 pgoyette aprint_error(": couldn't map registers\n");
102 1.3.2.2 pgoyette goto done;
103 1.3.2.2 pgoyette }
104 1.3.2.2 pgoyette
105 1.3.2.2 pgoyette const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
106 1.3.2.2 pgoyette ih = intr_establish(irq->ar_irq, IPL_BIO, type, ahci_intr, sc);
107 1.3.2.2 pgoyette if (ih == NULL) {
108 1.3.2.2 pgoyette aprint_error_dev(self, "couldn't install interrupt handler\n");
109 1.3.2.2 pgoyette return;
110 1.3.2.2 pgoyette }
111 1.3.2.2 pgoyette
112 1.3.2.2 pgoyette ahci_attach(sc);
113 1.3.2.2 pgoyette
114 1.3.2.2 pgoyette done:
115 1.3.2.2 pgoyette acpi_resource_cleanup(&res);
116 1.3.2.2 pgoyette }
117