xref: /openbsd-src/sys/dev/fdt/ahci_fdt.c (revision d59bb9942320b767f2a19aaa7690c8c6e30b724c)
1 /* $OpenBSD: ahci_fdt.c,v 1.2 2017/02/24 17:12:31 patrick 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, "snps,dwc-ahci");
57 }
58 
59 void
60 ahci_fdt_attach(struct device *parent, struct device *self, void *aux)
61 {
62 	struct ahci_softc *sc = (struct ahci_softc *) self;
63 	struct fdt_attach_args *faa = aux;
64 
65 	if (faa->fa_nreg < 1)
66 		return;
67 
68 	sc->sc_iot = faa->fa_iot;
69 	sc->sc_ios = faa->fa_reg[0].size;
70 	sc->sc_dmat = faa->fa_dmat;
71 
72 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
73 	    faa->fa_reg[0].size, 0, &sc->sc_ioh))
74 		panic("ahci_fdt_attach: bus_space_map failed!");
75 
76 	sc->sc_ih = arm_intr_establish_fdt(faa->fa_node, IPL_BIO,
77 	    ahci_intr, sc, sc->sc_dev.dv_xname);
78 	if (sc->sc_ih == NULL) {
79 		printf(": unable to establish interrupt\n");
80 		goto unmap;
81 	}
82 
83 	printf(":");
84 
85 	if (ahci_attach(sc) != 0) {
86 		/* error printed by ahci_attach */
87 		goto irq;
88 	}
89 
90 	return;
91 irq:
92 	arm_intr_disestablish_fdt(sc->sc_ih);
93 unmap:
94 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
95 }
96 
97 int
98 ahci_fdt_detach(struct device *self, int flags)
99 {
100 	struct ahci_softc *sc = (struct ahci_softc *) self;
101 
102 	ahci_detach(sc, flags);
103 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
104 	return 0;
105 }
106 
107 int
108 ahci_fdt_activate(struct device *self, int act)
109 {
110 	struct ahci_softc *sc = (struct ahci_softc *) self;
111 
112 	return ahci_activate((struct device *)sc, act);
113 }
114