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