pq3cfi.c revision 1.3 1 /* $NetBSD: pq3cfi.c,v 1.3 2011/07/19 20:52:10 cliff 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 "opt_flash.h"
36 #include "locators.h"
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: pq3cfi.c,v 1.3 2011/07/19 20:52:10 cliff Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/cdefs.h>
44 #include <sys/device.h>
45 #include <sys/endian.h>
46
47 #include <sys/bus.h>
48
49 #include <powerpc/booke/cpuvar.h>
50
51 #include <dev/nor/nor.h>
52 #include <dev/nor/cfi.h>
53
54
55 static int pq3cfi_match(device_t, cfdata_t, void *);
56 static void pq3cfi_attach(device_t, device_t, void *);
57 static int pq3cfi_detach(device_t, int);
58
59 struct pq3cfi_softc {
60 device_t sc_dev;
61 device_t sc_nordev;
62 struct cfi sc_cfi;
63 bus_addr_t sc_addr;
64 bus_size_t sc_size;
65 struct nor_interface sc_nor_if;
66 };
67
68 CFATTACH_DECL_NEW(pq3cfi, sizeof(struct pq3cfi_softc), pq3cfi_match,
69 pq3cfi_attach, pq3cfi_detach, NULL);
70
71 /*
72 * pq3cfi_addr - return bus address for the CFI NOR flash
73 *
74 * if the chip select not specified, use address from attach args
75 * otherwise use address from the chip select.
76 */
77 static inline bus_addr_t
78 pq3cfi_addr(struct generic_attach_args *ga)
79 {
80 bus_addr_t addr_aa = ga->ga_addr;
81
82 if (ga->ga_cs != OBIOCF_CS_DEFAULT) {
83 #ifdef NOTYET
84 bus_addr_t addr_cs = get_addr_from_cs(ga->ga_cs);
85 if (addr_aa != addr_cs)
86 aprint_warn("%s: configured addr %#x, CS%d addr %#x\n",
87 __func__, addr_aa, ga->ga_cs, addr_cs);
88 return addr_cs;
89 #endif
90 }
91 return addr_aa;
92 }
93
94 static int
95 pq3cfi_match(device_t parent, cfdata_t match, void *aux)
96 {
97 struct generic_attach_args *ga = aux;
98 bus_size_t tmpsize = CFI_QRY_MIN_MAP_SIZE;
99 bus_addr_t addr;
100 struct cfi cfi;
101 int rv;
102
103 KASSERT(ga->ga_bst != NULL);
104
105 addr = pq3cfi_addr(ga);
106 if (addr == OBIOCF_ADDR_DEFAULT) {
107 aprint_error("%s: no base address\n", __func__);
108 return 0;
109 }
110
111 cfi.cfi_bst = ga->ga_bst;
112 int error = bus_space_map(cfi.cfi_bst, addr, tmpsize, 0, &cfi.cfi_bsh);
113 if (error != 0) {
114 aprint_error("%s: cannot map %d at offset %#x, error %d\n", __func__, tmpsize, addr, error);
115 return false;
116 }
117
118 if (! cfi_probe(&cfi)) {
119 aprint_debug("%s: probe addr %#x, CFI not found\n",
120 __func__, addr);
121 rv = 0;
122 } else {
123 rv = 1;
124 }
125
126 bus_space_unmap(cfi.cfi_bst, cfi.cfi_bsh, tmpsize);
127
128 return rv;
129 }
130
131 static void
132 pq3cfi_attach(device_t parent, device_t self, void *aux)
133 {
134 struct pq3cfi_softc *sc = device_private(self);
135 struct generic_attach_args *ga = aux;
136 struct cfi_query_data * const qryp = &sc->sc_cfi.cfi_qry_data;
137 const bus_size_t tmpsize = CFI_QRY_MIN_MAP_SIZE;
138 bool found;
139 int error;
140
141 aprint_normal("\n");
142
143 sc->sc_dev = self;
144 sc->sc_cfi.cfi_bst = ga->ga_bst;
145 sc->sc_addr = pq3cfi_addr(ga);
146
147 /* map enough to identify, remap later when size is known */
148 error = bus_space_map(sc->sc_cfi.cfi_bst, sc->sc_addr, tmpsize,
149 0, &sc->sc_cfi.cfi_bsh);
150 if (error != 0) {
151 aprint_error_dev(self, "could not map error %d\n", error);
152 return;
153 }
154
155 found = cfi_identify(&sc->sc_cfi);
156
157 bus_space_unmap(sc->sc_cfi.cfi_bst, sc->sc_cfi.cfi_bsh, tmpsize);
158
159 if (! found) {
160 /* should not happen, we already probed OK in match */
161 aprint_error_dev(self, "could not map error %d\n", error);
162 return;
163 }
164
165 sc->sc_size = 1 << qryp->device_size;
166
167 sc->sc_nor_if = nor_interface_cfi;
168 sc->sc_nor_if.private = &sc->sc_cfi;
169 sc->sc_nor_if.access_width = (1 << sc->sc_cfi.cfi_portwidth);
170
171 cfi_print(self, &sc->sc_cfi);
172
173 error = bus_space_map(sc->sc_cfi.cfi_bst, sc->sc_addr, sc->sc_size,
174 0, &sc->sc_cfi.cfi_bsh);
175 if (error != 0) {
176 aprint_error_dev(self, "could not map error %d\n", error);
177 return;
178 }
179
180 if (! pmf_device_register1(self, NULL, NULL, NULL))
181 aprint_error_dev(self, "couldn't establish power handler\n");
182
183 sc->sc_nordev = nor_attach_mi(&sc->sc_nor_if, self);
184
185 }
186
187 static int
188 pq3cfi_detach(device_t self, int flags)
189 {
190 struct pq3cfi_softc *sc = device_private(self);
191 int rv = 0;
192
193 pmf_device_deregister(self);
194
195 if (sc->sc_nordev != NULL)
196 rv = config_detach(sc->sc_nordev, flags);
197
198 bus_space_unmap(sc->sc_cfi.cfi_bst, sc->sc_cfi.cfi_bsh, sc->sc_size);
199
200 return rv;
201 }
202