pq3cfi.c revision 1.6 1 /* $NetBSD: pq3cfi.c,v 1.6 2020/07/06 09:34:16 rin Exp $ */
2 /*-
3 * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Cliff Neighbors.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 * NOR CFI driver support for booke
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: pq3cfi.c,v 1.6 2020/07/06 09:34:16 rin Exp $");
37
38 #include "locators.h"
39
40 #ifdef _KERNEL_OPT
41 #include "opt_flash.h"
42 #endif
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/cdefs.h>
47 #include <sys/device.h>
48 #include <sys/endian.h>
49
50 #include <sys/bus.h>
51
52 #include <powerpc/booke/cpuvar.h>
53
54 #include <dev/nor/nor.h>
55 #include <dev/nor/cfi.h>
56
57
58 static int pq3cfi_match(device_t, cfdata_t, void *);
59 static void pq3cfi_attach(device_t, device_t, void *);
60 static int pq3cfi_detach(device_t, int);
61
62 struct pq3cfi_softc {
63 device_t sc_dev;
64 device_t sc_nordev;
65 struct cfi sc_cfi;
66 bus_addr_t sc_addr;
67 bus_size_t sc_size;
68 struct nor_interface sc_nor_if;
69 };
70
71 CFATTACH_DECL_NEW(pq3cfi, sizeof(struct pq3cfi_softc), pq3cfi_match,
72 pq3cfi_attach, pq3cfi_detach, NULL);
73
74 static int
75 pq3cfi_match(device_t parent, cfdata_t match, void *aux)
76 {
77 struct generic_attach_args *ga = aux;
78 bus_size_t tmpsize = CFI_QRY_MIN_MAP_SIZE;
79 bus_addr_t addr;
80 struct cfi cfi;
81 int rv;
82
83 KASSERT(ga->ga_bst != NULL);
84
85 addr = ga->ga_addr;
86 if (addr == OBIOCF_ADDR_DEFAULT) {
87 aprint_error("%s: no base address\n", __func__);
88 return 0;
89 }
90
91 cfi.cfi_bst = ga->ga_bst;
92 int error = bus_space_map(cfi.cfi_bst, addr, tmpsize, 0, &cfi.cfi_bsh);
93 if (error != 0) {
94 aprint_error("%s: cannot map %d at offset %#x, error %d\n", __func__, tmpsize, addr, error);
95 return false;
96 }
97
98 if (! cfi_probe(&cfi)) {
99 aprint_debug("%s: probe addr %#x, CFI not found\n",
100 __func__, addr);
101 rv = 0;
102 } else {
103 rv = 1;
104 }
105
106 bus_space_unmap(cfi.cfi_bst, cfi.cfi_bsh, tmpsize);
107
108 return rv;
109 }
110
111 static void
112 pq3cfi_attach(device_t parent, device_t self, void *aux)
113 {
114 struct pq3cfi_softc *sc = device_private(self);
115 struct generic_attach_args *ga = aux;
116 struct cfi_query_data * const qryp = &sc->sc_cfi.cfi_qry_data;
117 const bus_size_t tmpsize = CFI_QRY_MIN_MAP_SIZE;
118 bool found;
119 int error;
120
121 aprint_normal("\n");
122
123 sc->sc_dev = self;
124 sc->sc_cfi.cfi_bst = ga->ga_bst;
125 sc->sc_addr = ga->ga_addr;
126
127 /* map enough to identify, remap later when size is known */
128 error = bus_space_map(sc->sc_cfi.cfi_bst, sc->sc_addr, tmpsize,
129 0, &sc->sc_cfi.cfi_bsh);
130 if (error != 0) {
131 aprint_error_dev(self, "could not map error %d\n", error);
132 return;
133 }
134
135 found = cfi_identify(&sc->sc_cfi);
136
137 bus_space_unmap(sc->sc_cfi.cfi_bst, sc->sc_cfi.cfi_bsh, tmpsize);
138
139 if (! found) {
140 /* should not happen, we already probed OK in match */
141 aprint_error_dev(self, "could not map error %d\n", error);
142 return;
143 }
144
145 sc->sc_size = 1 << qryp->device_size;
146
147 sc->sc_nor_if = nor_interface_cfi;
148 sc->sc_nor_if.private = &sc->sc_cfi;
149 sc->sc_nor_if.access_width = (1 << sc->sc_cfi.cfi_portwidth);
150
151 cfi_print(self, &sc->sc_cfi);
152
153 error = bus_space_map(sc->sc_cfi.cfi_bst, sc->sc_addr, sc->sc_size,
154 BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_cfi.cfi_bsh);
155 if (error != 0) {
156 aprint_error_dev(self, "could not map error %d\n", error);
157 return;
158 }
159
160 if (! pmf_device_register1(self, NULL, NULL, NULL))
161 aprint_error_dev(self, "couldn't establish power handler\n");
162
163 sc->sc_nordev = nor_attach_mi(&sc->sc_nor_if, self);
164
165 }
166
167 static int
168 pq3cfi_detach(device_t self, int flags)
169 {
170 struct pq3cfi_softc *sc = device_private(self);
171 int rv = 0;
172
173 pmf_device_deregister(self);
174
175 if (sc->sc_nordev != NULL)
176 rv = config_detach(sc->sc_nordev, flags);
177
178 bus_space_unmap(sc->sc_cfi.cfi_bst, sc->sc_cfi.cfi_bsh, sc->sc_size);
179
180 return rv;
181 }
182