00a841646c40241e3ef07196cf5b371868e69177
[dragonfly.git] / sys / contrib / dev / fla / fla.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD: src/sys/contrib/dev/fla/fla.c,v 1.16 1999/12/08 04:45:16 ken Exp $ 
10  * $DragonFly: src/sys/contrib/dev/fla/Attic/fla.c,v 1.2 2003/06/17 04:28:20 dillon Exp $ 
11  *
12  */
13
14 #include "fla.h"
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/sysctl.h>
18 #include <sys/kernel.h>
19 #include <sys/buf.h>
20 #include <sys/malloc.h>
21 #include <sys/conf.h>
22 #include <sys/disk.h>
23 #include <sys/devicestat.h>
24 #include <sys/module.h>
25 #include <machine/clock.h>
26 #include <machine/resource.h>
27
28 #include <vm/vm.h>
29 #include <vm/pmap.h>
30 #include <vm/vm_param.h>
31
32 #include <sys/bus.h>
33 #include <isa/isavar.h>
34
35 #ifdef SMP
36 #include <machine/smp.h>
37 #define LEAVE() rel_mplock();                   
38 #define ENTER() get_mplock();                   
39 #else
40 #define LEAVE()
41 #define ENTER()
42 #endif
43
44 #include <contrib/dev/fla/msysosak.h>
45
46 MALLOC_DEFINE(M_FLA, "fla driver", "fla driver storage");
47
48 static int fla_debug = 0;
49 SYSCTL_INT(_debug, OID_AUTO, fladebug, CTLFLAG_RW, &fla_debug, 0, "");
50
51 #define CDEV_MAJOR      102
52 #define BDEV_MAJOR      28
53
54 static d_strategy_t flastrategy;
55 static d_open_t flaopen;
56 static d_close_t flaclose;
57 static d_ioctl_t flaioctl;
58
59 static struct cdevsw fla_cdevsw = {
60         /* open */      flaopen,
61         /* close */     flaclose,
62         /* read */      physread,
63         /* write */     physwrite,
64         /* ioctl */     flaioctl,
65         /* poll */      nopoll,
66         /* mmap */      nommap,
67         /* strategy */  flastrategy,
68         /* name */      "fla",
69         /* maj */       CDEV_MAJOR,
70         /* dump */      nodump,
71         /* psize */     nopsize,
72         /* flags */     D_DISK | D_CANFREE,
73         /* bmaj */      BDEV_MAJOR
74 };
75 static struct cdevsw fladisk_cdevsw;
76
77 void *
78 doc2k_malloc(int bytes) 
79 {
80         return malloc(bytes, M_FLA, M_WAITOK);
81 }
82
83 void
84 doc2k_free(void *ptr)
85 {
86         free(ptr, M_FLA);
87 }
88
89 void
90 doc2k_delay(unsigned msec)
91 {
92         DELAY(1000 * msec);
93 }
94
95 void
96 doc2k_memcpy(void *dst, const void *src, unsigned len)
97 {
98         bcopy(src, dst, len);
99 }
100
101 int
102 doc2k_memcmp(const void *dst, const void *src, unsigned len)
103 {
104         return (bcmp(src, dst, len));
105 }
106
107 void
108 doc2k_memset(void *dst, int c, unsigned len)
109 {
110         u_char *p = dst;
111         while (len--)
112                 *p++ = c;
113 }
114
115 static struct fla_s {
116         int busy;
117         int unit;
118         unsigned nsect;
119         struct doc2k_stat ds;
120         struct buf_queue_head buf_queue;
121         struct devstat stats;
122         struct disk disk;
123         dev_t dev;
124 } softc[NFLA];
125
126 static int
127 flaopen(dev_t dev, int flag, int fmt, struct proc *p)
128 {
129         struct fla_s *sc;
130         int error;
131         struct disklabel *dl;
132
133         if (fla_debug)
134                 printf("flaopen(%s %x %x %p)\n",
135                         devtoname(dev), flag, fmt, p);
136
137         sc = dev->si_drv1;
138
139         error = doc2k_open(sc->unit);
140
141         if (error) {
142                 printf("doc2k_open(%d) -> err %d\n", sc->unit, error);
143                 return (EIO);
144         }
145
146         dl = &sc->disk.d_label;
147         bzero(dl, sizeof(*dl));
148         error = doc2k_size(sc->unit, &dl->d_secperunit,
149             &dl->d_ncylinders, &dl->d_ntracks, &dl->d_nsectors);
150         dl->d_secsize = DEV_BSIZE;
151         dl->d_secpercyl = dl->d_ntracks * dl->d_nsectors; /* XXX */
152
153         return (0);
154 }
155
156 static int
157 flaclose(dev_t dev, int flags, int fmt, struct proc *p)
158 {
159         int error;
160         struct fla_s *sc;
161
162         if (fla_debug)
163                 printf("flaclose(%s %x %x %p)\n",
164                         devtoname(dev), flags, fmt, p);
165
166         sc = dev->si_drv1;
167
168         error = doc2k_close(sc->unit);
169         if (error) {
170                 printf("doc2k_close(%d) -> err %d\n", sc->unit, error);
171                 return (EIO);
172         }
173         return (0);
174 }
175
176 static int
177 flaioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
178 {
179
180         if (fla_debug)
181                 printf("flaioctl(%s %lx %p %x %p)\n",
182                         devtoname(dev), cmd, addr, flags, p);
183
184         return (ENOIOCTL);
185 }
186
187 static void
188 flastrategy(struct buf *bp)
189 {
190         int unit, error;
191         int s;
192         struct fla_s *sc;
193         enum doc2k_work what;
194
195         if (fla_debug > 1)
196                 printf("flastrategy(%p) %s %lx, %d, %ld, %p)\n",
197                     bp, devtoname(bp->b_dev), bp->b_flags, bp->b_blkno, 
198                     bp->b_bcount / DEV_BSIZE, bp->b_data);
199
200         sc = bp->b_dev->si_drv1;
201
202         s = splbio();
203
204         bufqdisksort(&sc->buf_queue, bp);
205
206         if (sc->busy) {
207                 splx(s);
208                 return;
209         }
210
211         sc->busy++;
212         
213         while (1) {
214                 bp = bufq_first(&sc->buf_queue);
215                 if (bp)
216                         bufq_remove(&sc->buf_queue, bp);
217                 splx(s);
218                 if (!bp)
219                         break;
220
221                 devstat_start_transaction(&sc->stats);
222                 bp->b_resid = bp->b_bcount;
223                 unit = dkunit(bp->b_dev);
224
225                 if (bp->b_flags & B_FREEBUF)
226                         what = DOC2K_ERASE;
227                 else if (bp->b_flags & B_READ)
228                         what = DOC2K_READ;
229                 else 
230                         what = DOC2K_WRITE;
231
232                 LEAVE();
233
234                 error = doc2k_rwe( unit, what, bp->b_pblkno,
235                     bp->b_bcount / DEV_BSIZE, bp->b_data);
236
237                 ENTER();
238
239                 if (fla_debug > 1 || error) {
240                         printf("fla%d: %d = rwe(%p, %d, %d, %d, %ld, %p)\n",
241                             unit, error, bp, unit, what, bp->b_pblkno, 
242                             bp->b_bcount / DEV_BSIZE, bp->b_data);
243                 }
244                 if (error) {
245                         bp->b_error = EIO;
246                         bp->b_flags |= B_ERROR;
247                 } else {
248                         bp->b_resid = 0;
249                 }
250                 devstat_end_transaction_buf(&sc->stats, bp);
251                 biodone(bp);
252
253                 s = splbio();
254         }
255         sc->busy = 0;
256         return;
257 }
258
259 static int
260 flaprobe (device_t dev)
261 {
262         int unit;
263         struct fla_s *sc;
264         int i;
265
266         unit = device_get_unit(dev);
267         sc = &softc[unit];
268
269         /* This is slightly ugly */
270         i = doc2k_probe(unit, KERNBASE + 0xc0000, KERNBASE + 0xe0000);
271         if (i)
272                 return (ENXIO);
273
274         i = doc2k_info(unit, &sc->ds);
275         if (i)
276                 return (ENXIO);
277
278         bus_set_resource(dev, SYS_RES_MEMORY, 0, 
279                 sc->ds.window - KERNBASE, 8192);
280
281         return (0);
282 }
283
284 static int
285 flaattach (device_t dev)
286 {
287         int unit;
288         int i, j, k, l, m, error;
289         struct fla_s *sc;
290
291         unit = device_get_unit(dev);
292         sc = &softc[unit];
293
294         error = doc2k_open(unit);
295         if (error) {
296                 printf("doc2k_open(%d) -> err %d\n", unit, error);
297                 return (EIO);
298         }
299
300         error = doc2k_size(unit, &sc->nsect, &i, &j, &k );
301         if (error) {
302                 printf("doc2k_size(%d) -> err %d\n", unit, error);
303                 return (EIO);
304         }
305
306         printf("fla%d: <%s %s>\n", unit, sc->ds.product, sc->ds.model);
307
308         error = doc2k_close(unit);
309         if (error) {
310                 printf("doc2k_close(%d) -> err %d\n", unit, error);
311                 return (EIO);
312         }
313         
314         m = 1024L * 1024L / DEV_BSIZE;
315         l = (sc->nsect * 10 + m/2) / m;
316         printf("fla%d: %d.%01dMB (%u sectors),"
317             " %d cyls, %d heads, %d S/T, 512 B/S\n",
318             unit, l / 10, l % 10, sc->nsect, i, j, k);
319
320         if (bootverbose)
321                 printf("fla%d: JEDEC=0x%x unitsize=%ld mediasize=%ld"
322                        " chipsize=%ld interleave=%d window=%lx\n", 
323                     unit, sc->ds.type, sc->ds.unitSize, sc->ds.mediaSize, 
324                     sc->ds.chipSize, sc->ds.interleaving, sc->ds.window);
325
326         bufq_init(&sc->buf_queue);
327
328         devstat_add_entry(&softc[unit].stats, "fla", unit, DEV_BSIZE,
329                 DEVSTAT_NO_ORDERED_TAGS, 
330                 DEVSTAT_TYPE_DIRECT | DEVSTAT_TYPE_IF_OTHER,
331                 DEVSTAT_PRIORITY_DISK);
332
333         sc->dev = disk_create(unit, &sc->disk, 0, &fla_cdevsw, &fladisk_cdevsw);
334         sc->dev->si_drv1 = sc;
335         sc->unit = unit;
336
337         return (0);
338 }
339
340 static device_method_t fla_methods[] = {
341         /* Device interface */
342         DEVMETHOD(device_probe,         flaprobe),
343         DEVMETHOD(device_attach,        flaattach),
344         {0, 0}
345 };
346
347 static driver_t fladriver = {
348         "fla",
349         fla_methods,
350         sizeof(struct fla_s),
351 };
352
353 static devclass_t       fla_devclass;
354
355 DRIVER_MODULE(fla, isa, fladriver, fla_devclass, 0, 0);