xref: /openbsd-src/sys/dev/fdt/ahci_fdt.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /* $OpenBSD: ahci_fdt.c,v 1.5 2018/12/04 11:25:48 kettenis Exp $ */
2 /*
3  * Copyright (c) 2013,2017 Patrick Wildt <patrick@blueri.se>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/buf.h>
21 #include <sys/kernel.h>
22 #include <sys/malloc.h>
23 #include <sys/device.h>
24 #include <sys/queue.h>
25 
26 #include <machine/bus.h>
27 #include <machine/fdt.h>
28 
29 #include <dev/ic/ahcireg.h>
30 #include <dev/ic/ahcivar.h>
31 
32 #include <dev/ofw/openfirm.h>
33 #include <dev/ofw/fdt.h>
34 
35 int	ahci_fdt_match(struct device *, void *, void *);
36 void	ahci_fdt_attach(struct device *, struct device *, void *);
37 int	ahci_fdt_detach(struct device *, int);
38 int	ahci_fdt_activate(struct device *, int);
39 
40 extern int ahci_intr(void *);
41 
42 struct cfattach ahci_fdt_ca = {
43 	sizeof(struct ahci_softc),
44 	ahci_fdt_match,
45 	ahci_fdt_attach,
46 	ahci_fdt_detach,
47 	ahci_fdt_activate
48 };
49 
50 int
51 ahci_fdt_match(struct device *parent, void *match, void *aux)
52 {
53 	struct fdt_attach_args *faa = aux;
54 
55 	return OF_is_compatible(faa->fa_node, "generic-ahci") ||
56 	    OF_is_compatible(faa->fa_node, "cavium,octeon-7130-ahci") ||
57 	    OF_is_compatible(faa->fa_node, "snps,dwc-ahci");
58 }
59 
60 void
61 ahci_fdt_attach(struct device *parent, struct device *self, void *aux)
62 {
63 	struct ahci_softc *sc = (struct ahci_softc *) self;
64 	struct fdt_attach_args *faa = aux;
65 
66 	if (faa->fa_nreg < 1)
67 		return;
68 
69 	sc->sc_iot = faa->fa_iot;
70 	sc->sc_ios = faa->fa_reg[0].size;
71 	sc->sc_dmat = faa->fa_dmat;
72 
73 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
74 	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
75 		panic("ahci_fdt_attach: bus_space_map failed!");
76 
77 	sc->sc_ih = fdt_intr_establish(faa->fa_node, IPL_BIO,
78 	    ahci_intr, sc, sc->sc_dev.dv_xname);
79 	if (sc->sc_ih == NULL) {
80 		printf(": can't establish interrupt\n");
81 		goto unmap;
82 	}
83 
84 	printf(":");
85 
86 	if (ahci_attach(sc) != 0) {
87 		/* error printed by ahci_attach */
88 		goto irq;
89 	}
90 
91 	return;
92 irq:
93 	fdt_intr_disestablish(sc->sc_ih);
94 unmap:
95 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
96 }
97 
98 int
99 ahci_fdt_detach(struct device *self, int flags)
100 {
101 	struct ahci_softc *sc = (struct ahci_softc *) self;
102 
103 	ahci_detach(sc, flags);
104 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
105 	return 0;
106 }
107 
108 int
109 ahci_fdt_activate(struct device *self, int act)
110 {
111 	struct ahci_softc *sc = (struct ahci_softc *) self;
112 
113 	return ahci_activate((struct device *)sc, act);
114 }
115