pq3ddrc.c revision 1.2 1 1.2 rin /* $NetBSD: pq3ddrc.c,v 1.2 2020/07/06 09:34:16 rin Exp $ */
2 1.1 matt /*-
3 1.1 matt * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 1.1 matt * All rights reserved.
5 1.1 matt *
6 1.1 matt * This code is derived from software contributed to The NetBSD Foundation
7 1.1 matt * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8 1.1 matt * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9 1.1 matt *
10 1.1 matt * This material is based upon work supported by the Defense Advanced Research
11 1.1 matt * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12 1.1 matt * Contract No. N66001-09-C-2073.
13 1.1 matt * Approved for Public Release, Distribution Unlimited
14 1.1 matt *
15 1.1 matt * Redistribution and use in source and binary forms, with or without
16 1.1 matt * modification, are permitted provided that the following conditions
17 1.1 matt * are met:
18 1.1 matt * 1. Redistributions of source code must retain the above copyright
19 1.1 matt * notice, this list of conditions and the following disclaimer.
20 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
21 1.1 matt * notice, this list of conditions and the following disclaimer in the
22 1.1 matt * documentation and/or other materials provided with the distribution.
23 1.1 matt *
24 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 1.1 matt * POSSIBILITY OF SUCH DAMAGE.
35 1.1 matt */
36 1.2 rin
37 1.1 matt #define DDRC_PRIVATE
38 1.1 matt
39 1.1 matt #include <sys/cdefs.h>
40 1.2 rin __KERNEL_RCSID(0, "$NetBSD: pq3ddrc.c,v 1.2 2020/07/06 09:34:16 rin Exp $");
41 1.1 matt
42 1.1 matt #include "ioconf.h"
43 1.1 matt
44 1.1 matt #include <sys/param.h>
45 1.1 matt #include <sys/bus.h>
46 1.1 matt #include <sys/cpu.h>
47 1.1 matt #include <sys/device.h>
48 1.1 matt #include <sys/intr.h>
49 1.1 matt
50 1.1 matt #include <powerpc/booke/cpuvar.h>
51 1.1 matt #include <powerpc/booke/e500var.h>
52 1.1 matt #include <powerpc/booke/e500reg.h>
53 1.1 matt
54 1.1 matt struct pq3ddrc_softc {
55 1.1 matt device_t sc_dev;
56 1.1 matt bus_space_tag_t sc_memt;
57 1.1 matt bus_space_handle_t sc_memh;
58 1.1 matt void *sc_ih;
59 1.1 matt struct evcnt sc_ev_sbe;
60 1.1 matt };
61 1.1 matt
62 1.1 matt static int pq3ddrc_match(device_t, cfdata_t, void *);
63 1.1 matt static void pq3ddrc_attach(device_t, device_t, void *);
64 1.1 matt
65 1.1 matt CFATTACH_DECL_NEW(pq3ddrc, sizeof(struct pq3ddrc_softc),
66 1.1 matt pq3ddrc_match, pq3ddrc_attach, NULL, NULL);
67 1.1 matt
68 1.1 matt static int
69 1.1 matt pq3ddrc_match(device_t parent, cfdata_t cf, void *aux)
70 1.1 matt {
71 1.1 matt if (!e500_cpunode_submatch(parent, cf, cf->cf_name, aux))
72 1.1 matt return 0;
73 1.1 matt
74 1.1 matt return 1;
75 1.1 matt }
76 1.1 matt
77 1.1 matt static int
78 1.1 matt pq3ddrc_intr(void *arg)
79 1.1 matt {
80 1.1 matt struct pq3ddrc_softc * const sc = arg;
81 1.1 matt uint32_t v;
82 1.1 matt
83 1.1 matt v = bus_space_read_4(sc->sc_memt, sc->sc_memh, ERR_DETECT);
84 1.1 matt bus_space_write_4(sc->sc_memt, sc->sc_memh, ERR_DETECT, v);
85 1.1 matt
86 1.1 matt if (v & ERR_SBEE) {
87 1.1 matt v = bus_space_read_4(sc->sc_memt, sc->sc_memh, ERR_SBE);
88 1.1 matt sc->sc_ev_sbe.ev_count += __SHIFTIN(v, ERR_SBE_SBEC);
89 1.1 matt v &= ~ERR_SBE_SBEC;
90 1.1 matt bus_space_write_4(sc->sc_memt, sc->sc_memh, ERR_SBE, v);
91 1.1 matt }
92 1.1 matt
93 1.1 matt return 1;
94 1.1 matt }
95 1.1 matt
96 1.1 matt static void
97 1.1 matt pq3ddrc_attach(device_t parent, device_t self, void *aux)
98 1.1 matt {
99 1.1 matt struct cpunode_softc * const psc = device_private(parent);
100 1.1 matt struct pq3ddrc_softc * const sc = device_private(self);
101 1.1 matt struct cpunode_attach_args * const cna = aux;
102 1.1 matt struct cpunode_locators * const cnl = &cna->cna_locs;
103 1.1 matt uint32_t v;
104 1.1 matt
105 1.1 matt psc->sc_children |= cna->cna_childmask;
106 1.1 matt sc->sc_dev = self;
107 1.1 matt sc->sc_memt = cna->cna_memt;
108 1.1 matt
109 1.1 matt int error = bus_space_map(cna->cna_memt, cnl->cnl_addr, cnl->cnl_size,
110 1.1 matt 0, &sc->sc_memh);
111 1.1 matt if (error) {
112 1.1 matt aprint_error(": failed to map registers: %d\n", error);
113 1.1 matt return;
114 1.1 matt }
115 1.1 matt
116 1.1 matt v = bus_space_read_4(sc->sc_memt, sc->sc_memh, DDR_SDRAM_CFG);
117 1.1 matt if ((v & SDRAM_CFG_ECC_EN) == 0) {
118 1.1 matt aprint_normal(": ECC disabled\n");
119 1.1 matt return;
120 1.1 matt }
121 1.1 matt
122 1.1 matt evcnt_attach_dynamic(&sc->sc_ev_sbe, EVCNT_TYPE_MISC, NULL,
123 1.1 matt device_xname(self), "single-bit ecc errors");
124 1.1 matt
125 1.1 matt /*
126 1.1 matt * Clear errors.
127 1.1 matt */
128 1.1 matt v = bus_space_read_4(sc->sc_memt, sc->sc_memh, ERR_DETECT);
129 1.1 matt bus_space_write_4(sc->sc_memt, sc->sc_memh, ERR_DETECT, v);
130 1.1 matt
131 1.1 matt /*
132 1.1 matt * Make sure ECC errors are not disabled.
133 1.1 matt */
134 1.1 matt v = bus_space_read_4(sc->sc_memt, sc->sc_memh, ERR_DISABLE);
135 1.1 matt v &= ~(ERR_MBEE|ERR_SBEE);
136 1.1 matt bus_space_write_4(sc->sc_memt, sc->sc_memh, ERR_DISABLE, v);
137 1.1 matt
138 1.1 matt /*
139 1.1 matt * Make sure ECC errors generate interrupts
140 1.1 matt */
141 1.1 matt v = bus_space_read_4(sc->sc_memt, sc->sc_memh, ERR_INT_EN);
142 1.1 matt v |= ERR_MBEE|ERR_SBEE;
143 1.1 matt bus_space_write_4(sc->sc_memt, sc->sc_memh, ERR_INT_EN, v);
144 1.1 matt
145 1.1 matt sc->sc_ih = intr_establish(cnl->cnl_intrs[0], IPL_VM,
146 1.1 matt IST_ONCHIP, pq3ddrc_intr, sc);
147 1.1 matt if (sc->sc_ih == NULL) {
148 1.1 matt aprint_error_dev(self, "failed to establish interrupt %d\n",
149 1.1 matt cnl->cnl_intrs[0]);
150 1.1 matt } else {
151 1.1 matt aprint_normal_dev(self, "interrupting on irq %d\n",
152 1.1 matt cnl->cnl_intrs[0]);
153 1.1 matt }
154 1.1 matt }
155