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