Import 2.1.118
[davej-history.git] / drivers / block / z2ram.c
blob193208d0d8649b23c309eb4cc35ebebf27e0cc86
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
18 ** Permission to use, copy, modify, and distribute this software and its
19 ** documentation for any purpose and without fee is hereby granted, provided
20 ** that the above copyright notice appear in all copies and that both that
21 ** copyright notice and this permission notice appear in supporting
22 ** documentation. This software is provided "as is" without express or
23 ** implied warranty.
26 #define MAJOR_NR Z2RAM_MAJOR
28 #include <linux/major.h>
29 #include <linux/malloc.h>
30 #include <linux/blk.h>
31 #include <linux/init.h>
33 #if defined(MODULE)
34 #include <linux/module.h>
35 #endif
37 #include <asm/setup.h>
38 #include <asm/bitops.h>
39 #include <asm/amigahw.h>
40 #include <linux/zorro.h>
42 #define TRUE (1)
43 #define FALSE (0)
45 #define Z2MINOR_COMBINED (0)
46 #define Z2MINOR_Z2ONLY (1)
47 #define Z2MINOR_CHIPONLY (2)
49 #define Z2RAM_CHUNK1024 ( Z2RAM_CHUNKSIZE >> 10 )
51 static u_long *z2ram_map = NULL;
52 static u_long z2ram_size = 0;
53 static int z2_blocksizes[3] = { 1024, 1024, 1024 };
54 static int z2_sizes[3] = { 0, 0, 0 };
55 static int z2_count = 0;
56 static int chip_count = 0;
57 static int current_device = -1;
59 static void
60 do_z2_request( void )
62 u_long start, len, addr, size;
64 while ( TRUE )
66 INIT_REQUEST;
68 start = CURRENT->sector << 9;
69 len = CURRENT->current_nr_sectors << 9;
71 if ( ( start + len ) > z2ram_size )
73 printk( KERN_ERR DEVICE_NAME ": bad access: block=%ld, count=%ld\n",
74 CURRENT->sector,
75 CURRENT->current_nr_sectors);
76 end_request( FALSE );
77 continue;
80 if ( ( CURRENT->cmd != READ ) && ( CURRENT->cmd != WRITE ) )
82 printk( KERN_ERR DEVICE_NAME ": bad command: %d\n", CURRENT->cmd );
83 end_request( FALSE );
84 continue;
87 while ( len )
89 addr = start & Z2RAM_CHUNKMASK;
90 size = Z2RAM_CHUNKSIZE - addr;
91 if ( len < size )
92 size = len;
94 addr += z2ram_map[ start >> Z2RAM_CHUNKSHIFT ];
96 if ( CURRENT->cmd == READ )
97 memcpy( CURRENT->buffer, (char *)addr, size );
98 else
99 memcpy( (char *)addr, CURRENT->buffer, size );
101 start += size;
102 len -= size;
105 end_request( TRUE );
109 static void
110 get_z2ram( void )
112 int i;
114 for ( i = 0; i < Z2RAM_SIZE / Z2RAM_CHUNKSIZE; i++ )
116 if ( test_bit( i, zorro_unused_z2ram ) )
118 z2_count++;
119 z2ram_map[ z2ram_size++ ] =
120 ZTWO_VADDR( Z2RAM_START ) + ( i << Z2RAM_CHUNKSHIFT );
121 clear_bit( i, zorro_unused_z2ram );
125 return;
128 static void
129 get_chipram( void )
132 while ( amiga_chip_avail() > ( Z2RAM_CHUNKSIZE * 4 ) )
134 chip_count++;
135 z2ram_map[ z2ram_size ] =
136 (u_long)amiga_chip_alloc( Z2RAM_CHUNKSIZE );
138 if ( z2ram_map[ z2ram_size ] == 0 )
140 break;
143 z2ram_size++;
146 return;
149 static int
150 z2_open( struct inode *inode, struct file *filp )
152 int device;
153 int max_z2_map = ( Z2RAM_SIZE / Z2RAM_CHUNKSIZE ) *
154 sizeof( z2ram_map[0] );
155 int max_chip_map = ( amiga_chip_size / Z2RAM_CHUNKSIZE ) *
156 sizeof( z2ram_map[0] );
158 device = DEVICE_NR( inode->i_rdev );
160 if ( current_device != -1 && current_device != device )
162 return -EBUSY;
165 if ( current_device == -1 )
167 z2_count = 0;
168 chip_count = 0;
169 z2ram_size = 0;
171 switch ( device )
173 case Z2MINOR_COMBINED:
175 z2ram_map = kmalloc( max_z2_map + max_chip_map, GFP_KERNEL );
176 if ( z2ram_map == NULL )
178 printk( KERN_ERR DEVICE_NAME
179 ": cannot get mem for z2ram_map\n" );
180 return -ENOMEM;
183 get_z2ram();
184 get_chipram();
186 if ( z2ram_size != 0 )
187 printk( KERN_INFO DEVICE_NAME
188 ": using %iK Zorro II RAM and %iK Chip RAM (Total %dK)\n",
189 z2_count * Z2RAM_CHUNK1024,
190 chip_count * Z2RAM_CHUNK1024,
191 ( z2_count + chip_count ) * Z2RAM_CHUNK1024 );
193 break;
195 case Z2MINOR_Z2ONLY:
196 z2ram_map = kmalloc( max_z2_map, GFP_KERNEL );
197 if ( z2ram_map == NULL )
199 printk( KERN_ERR DEVICE_NAME
200 ": cannot get mem for z2ram_map\n" );
201 return -ENOMEM;
204 get_z2ram();
206 if ( z2ram_size != 0 )
207 printk( KERN_INFO DEVICE_NAME
208 ": using %iK of Zorro II RAM\n",
209 z2_count * Z2RAM_CHUNK1024 );
211 break;
213 case Z2MINOR_CHIPONLY:
214 z2ram_map = kmalloc( max_chip_map, GFP_KERNEL );
215 if ( z2ram_map == NULL )
217 printk( KERN_ERR DEVICE_NAME
218 ": cannot get mem for z2ram_map\n" );
219 return -ENOMEM;
222 get_chipram();
224 if ( z2ram_size != 0 )
225 printk( KERN_INFO DEVICE_NAME
226 ": using %iK Chip RAM\n",
227 chip_count * Z2RAM_CHUNK1024 );
229 break;
231 default:
232 return -ENODEV;
235 if ( z2ram_size == 0 )
237 kfree( z2ram_map );
238 printk( KERN_NOTICE DEVICE_NAME
239 ": no unused ZII/Chip RAM found\n" );
240 return -ENOMEM;
243 current_device = device;
244 z2ram_size <<= Z2RAM_CHUNKSHIFT;
245 z2_sizes[ device ] = z2ram_size >> 10;
246 blk_size[ MAJOR_NR ] = z2_sizes;
249 #if defined(MODULE)
250 MOD_INC_USE_COUNT;
251 #endif
253 return 0;
256 static void
257 z2_release( struct inode *inode, struct file *filp )
260 if ( current_device == -1 )
261 return;
263 sync_dev( inode->i_rdev );
265 #if defined(MODULE)
266 MOD_DEC_USE_COUNT;
267 #endif
269 return;
272 static struct file_operations z2_fops =
274 NULL, /* lseek - default */
275 block_read, /* read - general block-dev read */
276 block_write, /* write - general block-dev write */
277 NULL, /* readdir - bad */
278 NULL, /* poll */
279 NULL, /* ioctl */
280 NULL, /* mmap */
281 z2_open, /* open */
282 NULL, /* flush */
283 z2_release, /* release */
284 block_fsync /* fsync */
287 __initfunc(int
288 z2_init( void ))
291 if ( !MACH_IS_AMIGA )
292 return -ENXIO;
294 if ( register_blkdev( MAJOR_NR, DEVICE_NAME, &z2_fops ) )
296 printk( KERN_ERR DEVICE_NAME ": Unable to get major %d\n",
297 MAJOR_NR );
298 return -EBUSY;
301 blk_dev[ MAJOR_NR ].request_fn = DEVICE_REQUEST;
302 blksize_size[ MAJOR_NR ] = z2_blocksizes;
303 blk_size[ MAJOR_NR ] = z2_sizes;
305 return 0;
308 #if defined(MODULE)
310 init_module( void )
312 int error;
314 error = z2_init();
315 if ( error == 0 )
317 printk( KERN_INFO DEVICE_NAME ": loaded as module\n" );
320 return error;
323 void
324 cleanup_module( void )
326 int i, j;
328 if ( unregister_blkdev( MAJOR_NR, DEVICE_NAME ) != 0 )
329 printk( KERN_ERR DEVICE_NAME ": unregister of device failed\n");
331 if ( current_device != -1 )
333 i = 0;
335 for ( j = 0 ; j < z2_count; j++ )
337 set_bit( i++, zorro_unused_z2ram );
340 for ( j = 0 ; j < chip_count; j++ )
342 if ( z2ram_map[ i ] )
344 amiga_chip_free( (void *) z2ram_map[ i++ ] );
348 if ( z2ram_map != NULL )
350 kfree( z2ram_map );
354 return;
356 #endif