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