1 /*-
2  * Copyright (c) 2012 Oleksandr Tymoshenko <[email protected]>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/ktr.h>
36 #include <sys/module.h>
37 #include <sys/rman.h>
38 #include <machine/bus.h>
39 #include <machine/intr.h>
40 
41 #include <dev/ofw/openfirm.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 
45 #ifdef  DEBUG
46 #define dprintf(fmt, args...) printf(fmt, ##args)
47 #else
48 #define dprintf(fmt, args...)
49 #endif
50 
51 #define	SIC_STATUS	0x00
52 #define	SIC_RAWSTAT	0x04
53 #define	SIC_ENABLE	0x08
54 #define	SIC_ENSET	0x08
55 #define	SIC_ENCLR	0x0C
56 #define	SIC_SOFTINTSET	0x10
57 #define	SIC_SOFTINTCLR	0x14
58 #define	SIC_PICENABLE	0x20
59 #define	SIC_PICENSET	0x20
60 #define	SIC_PICENCLR	0x24
61 
62 struct versatile_sic_softc {
63 	device_t		sc_dev;
64 	struct resource *	mem_res;
65 };
66 
67 #define	sic_read_4(sc, reg)			\
68     bus_read_4(sc->mem_res, (reg))
69 #define	sic_write_4(sc, reg, val)		\
70     bus_write_4(sc->mem_res, (reg), (val))
71 
72 static int
73 versatile_sic_probe(device_t dev)
74 {
75 
76 	if (!ofw_bus_status_okay(dev))
77 		return (ENXIO);
78 
79 	if (!ofw_bus_is_compatible(dev, "arm,versatile-sic"))
80 		return (ENXIO);
81 	device_set_desc(dev, "ARM Versatile SIC");
82 	return (BUS_PROBE_DEFAULT);
83 }
84 
85 static int
86 versatile_sic_attach(device_t dev)
87 {
88 	struct		versatile_sic_softc *sc = device_get_softc(dev);
89 	uint32_t	pass_irqs;
90 	int		rid;
91 
92 	sc->sc_dev = dev;
93 
94 	/* Request memory resources */
95 	rid = 0;
96 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
97 	    RF_ACTIVE);
98 	if (sc->mem_res == NULL) {
99 		device_printf(dev, "Error: could not allocate memory resources\n");
100 		return (ENXIO);
101 	}
102 
103 	/* Disable all interrupts on SIC */
104 	sic_write_4(sc, SIC_ENCLR, 0xffffffff);
105 
106 	/*
107 	 * XXX: Enable IRQ3 for KMI
108 	 * Should be replaced by proper interrupts cascading
109 	 */
110 	sic_write_4(sc, SIC_ENSET, (1 << 3));
111 
112 	/*
113 	 * Let PCI and Ethernet interrupts pass through
114 	 * IRQ25, IRQ27..IRQ31
115 	 */
116 	pass_irqs = (0x1f << 27) | (1 << 25);
117 	sic_write_4(sc, SIC_PICENSET, pass_irqs);
118 
119 	return (0);
120 }
121 
122 static device_method_t versatile_sic_methods[] = {
123 	DEVMETHOD(device_probe,		versatile_sic_probe),
124 	DEVMETHOD(device_attach,	versatile_sic_attach),
125 	{ 0, 0 }
126 };
127 
128 static driver_t versatile_sic_driver = {
129 	"sic",
130 	versatile_sic_methods,
131 	sizeof(struct versatile_sic_softc),
132 };
133 
134 static devclass_t versatile_sic_devclass;
135 
136 DRIVER_MODULE(sic, simplebus, versatile_sic_driver, versatile_sic_devclass, 0, 0);
137