1 /*	$OpenBSD: flash.c,v 1.3 2003/06/02 18:40:59 jason Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 Jason L. Wright (jason@thought.net)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Driver for the flash memory found on FORCE CPU-5V boards.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/errno.h>
37 #include <sys/ioctl.h>
38 #include <sys/syslog.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41 
42 #include <machine/autoconf.h>
43 #include <sparc/cpu.h>
44 #include <sparc/sparc/cpuvar.h>
45 
46 int	flashmatch(struct device *, void *, void *);
47 void	flashattach(struct device *, struct device *, void *);
48 
49 int	flashopen(dev_t, int, int, struct proc *p);
50 int	flashclose(dev_t, int, int, struct proc *p);
51 int	flashread(dev_t, struct uio *, int);
52 int	flashwrite(dev_t, struct uio *, int);
53 int	flashrw(dev_t, struct uio *, int);
54 int	flashioctl(dev_t, u_long, caddr_t, int, struct proc *);
55 
56 /*
57  * We see the flash-memory in 512k windows.  The current window is
58  * changed in the sysconfig registers (FMPCR1), see scf.c.
59  */
60 #define	FLASH_REGS_SIZE		0x80000
61 
62 struct flash_regs {
63 	u_int8_t		regs[0x80000];
64 };
65 
66 struct flash_softc {
67 	struct device		sc_dv;
68 	struct flash_regs	*sc_regs;
69 	int			sc_node;
70 	int			sc_open;
71 };
72 
73 struct cfattach flash_ca = {
74 	sizeof (struct flash_softc), flashmatch, flashattach
75 };
76 
77 struct cfdriver flash_cd = {
78 	NULL, "flash", DV_IFNET
79 };
80 
81 int
flashmatch(parent,vcf,aux)82 flashmatch(parent, vcf, aux)
83 	struct device *parent;
84 	void *vcf, *aux;
85 {
86 	struct confargs *ca = aux;
87 	register struct romaux *ra = &ca->ca_ra;
88 
89 	if (strcmp("flash-memory", ra->ra_name))
90 		return (0);
91 	return (1);
92 }
93 
94 void
flashattach(parent,self,aux)95 flashattach(parent, self, aux)
96 	struct device *parent, *self;
97 	void *aux;
98 {
99 	struct confargs *ca = aux;
100 	struct flash_softc *sc = (struct flash_softc *)self;
101 
102 	/* map registers */
103 	if (ca->ca_ra.ra_nreg != 1) {
104 		printf(": expected 1 register, got %d\n", ca->ca_ra.ra_nreg);
105 		return;
106 	}
107 	sc->sc_regs = mapiodev(&(ca->ca_ra.ra_reg[0]), 0,
108 			ca->ca_ra.ra_reg[0].rr_len);
109 
110 	sc->sc_node = ca->ca_ra.ra_node;
111 
112 	printf(": window 0x%x\n", ca->ca_ra.ra_reg[0].rr_len);
113 }
114 
115 int
flashopen(dev,flags,mode,p)116 flashopen(dev, flags, mode, p)
117 	dev_t dev;
118 	int flags;
119 	int mode;
120 	struct proc *p;
121 {
122 	struct flash_softc *sc;
123 	int card = 0;
124 
125 	if (card >= flash_cd.cd_ndevs)
126 		return (ENXIO);
127 	sc = flash_cd.cd_devs[card];
128 	if (sc->sc_open)
129 		return (EBUSY);
130 	sc->sc_open = 1;
131 	return (0);
132 }
133 
134 int
flashclose(dev,flags,mode,p)135 flashclose(dev, flags, mode, p)
136 	dev_t dev;
137 	int flags;
138 	int mode;
139 	struct proc *p;
140 {
141 	struct flash_softc *sc = flash_cd.cd_devs[0];
142 	sc->sc_open = 0;
143 	return (0);
144 }
145 
146 int
flashwrite(dev,uio,flags)147 flashwrite(dev, uio, flags)
148 	dev_t dev;
149 	struct uio *uio;
150 	int flags;
151 {
152 	return (flashrw(dev, uio, flags));
153 }
154 
155 int
flashread(dev,uio,flags)156 flashread(dev, uio, flags)
157 	dev_t dev;
158 	struct uio *uio;
159 	int flags;
160 {
161 	return (flashrw(dev, uio, flags));
162 }
163 
164 int
flashrw(dev,uio,flags)165 flashrw(dev, uio, flags)
166 	dev_t dev;
167 	struct uio *uio;
168 	int flags;
169 {
170 	struct flash_softc *sc = flash_cd.cd_devs[0];
171 	u_int cnt;
172 	int off;
173 
174 	off = uio->uio_offset;
175 	if (off >= FLASH_REGS_SIZE)
176 		return (EFAULT);
177 
178 	cnt = uio->uio_resid;
179 	if (cnt > (FLASH_REGS_SIZE - off))
180 		cnt = FLASH_REGS_SIZE - off;
181 
182 	return (uiomove(&sc->sc_regs->regs[0] + off, cnt, uio));
183 }
184 
185 int
flashioctl(dev,cmd,data,flags,p)186 flashioctl(dev, cmd, data, flags, p)
187 	dev_t dev;
188 	u_long cmd;
189 	caddr_t data;
190 	int flags;
191 	struct proc *p;
192 {
193 	return (EINVAL);
194 }
195