Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / block / z2ram.c
1 /*
2 ** z2ram - Amiga pseudo-driver to access 16bit-RAM in ZorroII space
3 **         as a block device, to be used as a RAM disk or swap space
4 ** 
5 ** Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
6 **
7 ** ++Geert: support for zorro_unused_z2ram, better range checking
8 ** ++roman: translate accesses via an array
9 ** ++Milan: support for ChipRAM usage
10 ** ++yambo: converted to 2.0 kernel
11 ** ++yambo: modularized and support added for 3 minor devices including:
12 **          MAJOR  MINOR  DESCRIPTION
13 **          -----  -----  ----------------------------------------------
14 **          37     0       Use Zorro II and Chip ram
15 **          37     1       Use only Zorro II ram
16 **          37     2       Use only Chip ram
17 **          37     4-7     Use memory list entry 1-4 (first is 0)
18 ** ++jskov: support for 1-4th memory list entry.
19 **
20 ** Permission to use, copy, modify, and distribute this software and its
21 ** documentation for any purpose and without fee is hereby granted, provided
22 ** that the above copyright notice appear in all copies and that both that
23 ** copyright notice and this permission notice appear in supporting
24 ** documentation.  This software is provided "as is" without express or
25 ** implied warranty.
26 */
27
28 #define MAJOR_NR    Z2RAM_MAJOR
29
30 #include <linux/major.h>
31 #include <linux/malloc.h>
32 #include <linux/vmalloc.h>
33 #include <linux/blk.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
36
37 #include <asm/setup.h>
38 #include <asm/bitops.h>
39 #include <asm/amigahw.h>
40 #include <asm/pgtable.h>
41 #include <asm/io.h>
42 #include <linux/zorro.h>
43
44
45 extern int m68k_realnum_memory;
46 extern struct mem_info m68k_memory[NUM_MEMINFO];
47
48 #define TRUE                  (1)
49 #define FALSE                 (0)
50
51 #define Z2MINOR_COMBINED      (0)
52 #define Z2MINOR_Z2ONLY        (1)
53 #define Z2MINOR_CHIPONLY      (2)
54 #define Z2MINOR_MEMLIST1      (4)
55 #define Z2MINOR_MEMLIST2      (5)
56 #define Z2MINOR_MEMLIST3      (6)
57 #define Z2MINOR_MEMLIST4      (7)
58 #define Z2MINOR_COUNT         (8) /* Move this down when adding a new minor */
59
60 #define Z2RAM_CHUNK1024       ( Z2RAM_CHUNKSIZE >> 10 )
61
62 static u_long *z2ram_map    = NULL;
63 static u_long z2ram_size    = 0;
64 static int z2_blocksizes[Z2MINOR_COUNT];
65 static int z2_sizes[Z2MINOR_COUNT];
66 static int z2_count         = 0;
67 static int chip_count       = 0;
68 static int list_count       = 0;
69 static int current_device   = -1;
70
71 static void
72 do_z2_request( request_queue_t * q )
73 {
74     u_long start, len, addr, size;
75
76     while ( TRUE )
77     {
78         INIT_REQUEST;
79
80         start = CURRENT->sector << 9;
81         len  = CURRENT->current_nr_sectors << 9;
82
83         if ( ( start + len ) > z2ram_size )
84         {
85             printk( KERN_ERR DEVICE_NAME ": bad access: block=%ld, count=%ld\n",
86                 CURRENT->sector,
87                 CURRENT->current_nr_sectors);
88             end_request( FALSE );
89             continue;
90         }
91
92         if ( ( CURRENT->cmd != READ ) && ( CURRENT->cmd != WRITE ) )
93         {
94             printk( KERN_ERR DEVICE_NAME ": bad command: %d\n", CURRENT->cmd );
95             end_request( FALSE );
96             continue;
97         }
98
99         while ( len ) 
100         {
101             addr = start & Z2RAM_CHUNKMASK;
102             size = Z2RAM_CHUNKSIZE - addr;
103             if ( len < size )
104                 size = len;
105
106             addr += z2ram_map[ start >> Z2RAM_CHUNKSHIFT ];
107
108             if ( CURRENT->cmd == READ )
109                 memcpy( CURRENT->buffer, (char *)addr, size );
110             else
111                 memcpy( (char *)addr, CURRENT->buffer, size );
112
113             start += size;
114             len -= size;
115         }
116
117         end_request( TRUE );
118     }
119 }
120
121 static void
122 get_z2ram( void )
123 {
124     int i;
125
126     for ( i = 0; i < Z2RAM_SIZE / Z2RAM_CHUNKSIZE; i++ )
127     {
128         if ( test_bit( i, zorro_unused_z2ram ) )
129         {
130             z2_count++;
131             z2ram_map[ z2ram_size++ ] = 
132                 ZTWO_VADDR( Z2RAM_START ) + ( i << Z2RAM_CHUNKSHIFT );
133             clear_bit( i, zorro_unused_z2ram );
134         }
135     }
136
137     return;
138 }
139
140 static void
141 get_chipram( void )
142 {
143
144     while ( amiga_chip_avail() > ( Z2RAM_CHUNKSIZE * 4 ) )
145     {
146         chip_count++;
147         z2ram_map[ z2ram_size ] =
148             (u_long)amiga_chip_alloc( Z2RAM_CHUNKSIZE, "z2ram" );
149
150         if ( z2ram_map[ z2ram_size ] == 0 )
151         {
152             break;
153         }
154
155         z2ram_size++;
156     }
157         
158     return;
159 }
160
161 static int
162 z2_open( struct inode *inode, struct file *filp )
163 {
164     int device;
165     int max_z2_map = ( Z2RAM_SIZE / Z2RAM_CHUNKSIZE ) *
166         sizeof( z2ram_map[0] );
167     int max_chip_map = ( amiga_chip_size / Z2RAM_CHUNKSIZE ) *
168         sizeof( z2ram_map[0] );
169     int rc = -ENOMEM;
170
171     MOD_INC_USE_COUNT;
172
173     device = DEVICE_NR( inode->i_rdev );
174
175     if ( current_device != -1 && current_device != device )
176     {
177         rc = -EBUSY;
178         goto err_out;
179     }
180
181     if ( current_device == -1 )
182     {
183         z2_count   = 0;
184         chip_count = 0;
185         list_count = 0;
186         z2ram_size = 0;
187
188         /* Use a specific list entry. */
189         if (device >= Z2MINOR_MEMLIST1 && device <= Z2MINOR_MEMLIST4) {
190                 int index = device - Z2MINOR_MEMLIST1 + 1;
191                 unsigned long size, paddr, vaddr;
192
193                 if (index >= m68k_realnum_memory) {
194                         printk( KERN_ERR DEVICE_NAME
195                                 ": no such entry in z2ram_map\n" );
196                         goto err_out;
197                 }
198
199                 paddr = m68k_memory[index].addr;
200                 size = m68k_memory[index].size & ~(Z2RAM_CHUNKSIZE-1);
201
202 #ifdef __powerpc__
203                 /* FIXME: ioremap doesn't build correct memory tables. */
204                 {
205                         vfree(vmalloc (size));
206                 }
207
208                 vaddr = (unsigned long) __ioremap (paddr, size, 
209                                                    _PAGE_WRITETHRU);
210
211 #else
212                 vaddr = (unsigned long)ioremap(paddr, size);
213 #endif
214                 z2ram_map = 
215                         kmalloc((size/Z2RAM_CHUNKSIZE)*sizeof(z2ram_map[0]),
216                                 GFP_KERNEL);
217                 if ( z2ram_map == NULL )
218                 {
219                     printk( KERN_ERR DEVICE_NAME
220                         ": cannot get mem for z2ram_map\n" );
221                     goto err_out;
222                 }
223
224                 while (size) {
225                         z2ram_map[ z2ram_size++ ] = vaddr;
226                         size -= Z2RAM_CHUNKSIZE;
227                         vaddr += Z2RAM_CHUNKSIZE;
228                         list_count++;
229                 }
230
231                 if ( z2ram_size != 0 )
232                     printk( KERN_INFO DEVICE_NAME
233                         ": using %iK List Entry %d Memory\n",
234                         list_count * Z2RAM_CHUNK1024, index );
235         } else
236
237         switch ( device )
238         {
239             case Z2MINOR_COMBINED:
240
241                 z2ram_map = kmalloc( max_z2_map + max_chip_map, GFP_KERNEL );
242                 if ( z2ram_map == NULL )
243                 {
244                     printk( KERN_ERR DEVICE_NAME
245                         ": cannot get mem for z2ram_map\n" );
246                     goto err_out;
247                 }
248
249                 get_z2ram();
250                 get_chipram();
251
252                 if ( z2ram_size != 0 )
253                     printk( KERN_INFO DEVICE_NAME 
254                         ": using %iK Zorro II RAM and %iK Chip RAM (Total %dK)\n",
255                         z2_count * Z2RAM_CHUNK1024,
256                         chip_count * Z2RAM_CHUNK1024,
257                         ( z2_count + chip_count ) * Z2RAM_CHUNK1024 );
258
259             break;
260
261             case Z2MINOR_Z2ONLY:
262                 z2ram_map = kmalloc( max_z2_map, GFP_KERNEL );
263                 if ( z2ram_map == NULL )
264                 {
265                     printk( KERN_ERR DEVICE_NAME
266                         ": cannot get mem for z2ram_map\n" );
267                     goto err_out;
268                 }
269
270                 get_z2ram();
271
272                 if ( z2ram_size != 0 )
273                     printk( KERN_INFO DEVICE_NAME 
274                         ": using %iK of Zorro II RAM\n",
275                         z2_count * Z2RAM_CHUNK1024 );
276
277             break;
278
279             case Z2MINOR_CHIPONLY:
280                 z2ram_map = kmalloc( max_chip_map, GFP_KERNEL );
281                 if ( z2ram_map == NULL )
282                 {
283                     printk( KERN_ERR DEVICE_NAME
284                         ": cannot get mem for z2ram_map\n" );
285                     goto err_out;
286                 }
287
288                 get_chipram();
289
290                 if ( z2ram_size != 0 )
291                     printk( KERN_INFO DEVICE_NAME 
292                         ": using %iK Chip RAM\n",
293                         chip_count * Z2RAM_CHUNK1024 );
294                     
295             break;
296
297             default:
298                 rc = -ENODEV;
299                 goto err_out;
300         
301             break;
302         }
303
304         if ( z2ram_size == 0 )
305         {
306             printk( KERN_NOTICE DEVICE_NAME
307                 ": no unused ZII/Chip RAM found\n" );
308             goto err_out_kfree;
309         }
310
311         current_device = device;
312         z2ram_size <<= Z2RAM_CHUNKSHIFT;
313         z2_sizes[ device ] = z2ram_size >> 10;
314         blk_size[ MAJOR_NR ] = z2_sizes;
315     }
316
317     return 0;
318
319 err_out_kfree:
320     kfree( z2ram_map );
321 err_out:
322     MOD_DEC_USE_COUNT;
323     return rc;
324 }
325
326 static int
327 z2_release( struct inode *inode, struct file *filp )
328 {
329     if ( current_device == -1 )
330         return 0;     
331
332     /*
333      * FIXME: unmap memory
334      */
335
336     MOD_DEC_USE_COUNT;
337
338     return 0;
339 }
340
341 static struct block_device_operations z2_fops =
342 {
343         open:           z2_open,
344         release:        z2_release,
345 };
346
347 int __init 
348 z2_init( void )
349 {
350
351     if ( !MACH_IS_AMIGA )
352         return -ENXIO;
353
354     if ( register_blkdev( MAJOR_NR, DEVICE_NAME, &z2_fops ) )
355     {
356         printk( KERN_ERR DEVICE_NAME ": Unable to get major %d\n",
357             MAJOR_NR );
358         return -EBUSY;
359     }
360
361     {
362             /* Initialize size arrays. */
363             int i;
364
365             for (i = 0; i < Z2MINOR_COUNT; i++) {
366                     z2_blocksizes[ i ] = 1024;
367                     z2_sizes[ i ] = 0;
368             }
369     }    
370    
371     blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
372     blksize_size[ MAJOR_NR ] = z2_blocksizes;
373     blk_size[ MAJOR_NR ] = z2_sizes;
374
375     return 0;
376 }
377
378 #if defined(MODULE)
379 int
380 init_module( void )
381 {
382     int error;
383     
384     error = z2_init();
385     if ( error == 0 )
386     {
387         printk( KERN_INFO DEVICE_NAME ": loaded as module\n" );
388     }
389     
390     return error;
391 }
392
393 void
394 cleanup_module( void )
395 {
396     int i, j;
397
398     if ( unregister_blkdev( MAJOR_NR, DEVICE_NAME ) != 0 )
399         printk( KERN_ERR DEVICE_NAME ": unregister of device failed\n");
400
401     blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
402
403     if ( current_device != -1 )
404     {
405         i = 0;
406
407         for ( j = 0 ; j < z2_count; j++ )
408         {
409             set_bit( i++, zorro_unused_z2ram ); 
410         }
411
412         for ( j = 0 ; j < chip_count; j++ )
413         {
414             if ( z2ram_map[ i ] )
415             {
416                 amiga_chip_free( (void *) z2ram_map[ i++ ] );
417             }
418         }
419
420         if ( z2ram_map != NULL )
421         {
422             kfree( z2ram_map );
423         }
424     }
425
426     return;
427
428 #endif