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
5 ** Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
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
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>
34 #include <linux/module.h>
37 #include <asm/setup.h>
38 #include <asm/bitops.h>
39 #include <asm/amigahw.h>
40 #include <linux/zorro.h>
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;
62 u_long start
, len
, addr
, size
;
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",
75 CURRENT
->current_nr_sectors
);
80 if ( ( CURRENT
->cmd
!= READ
) && ( CURRENT
->cmd
!= WRITE
) )
82 printk( KERN_ERR DEVICE_NAME
": bad command: %d\n", CURRENT
->cmd
);
89 addr
= start
& Z2RAM_CHUNKMASK
;
90 size
= Z2RAM_CHUNKSIZE
- addr
;
94 addr
+= z2ram_map
[ start
>> Z2RAM_CHUNKSHIFT
];
96 if ( CURRENT
->cmd
== READ
)
97 memcpy( CURRENT
->buffer
, (char *)addr
, size
);
99 memcpy( (char *)addr
, CURRENT
->buffer
, size
);
114 for ( i
= 0; i
< Z2RAM_SIZE
/ Z2RAM_CHUNKSIZE
; i
++ )
116 if ( test_bit( i
, zorro_unused_z2ram
) )
119 z2ram_map
[ z2ram_size
++ ] =
120 ZTWO_VADDR( Z2RAM_START
) + ( i
<< Z2RAM_CHUNKSHIFT
);
121 clear_bit( i
, zorro_unused_z2ram
);
132 while ( amiga_chip_avail() > ( Z2RAM_CHUNKSIZE
* 4 ) )
135 z2ram_map
[ z2ram_size
] =
136 (u_long
)amiga_chip_alloc( Z2RAM_CHUNKSIZE
);
138 if ( z2ram_map
[ z2ram_size
] == 0 )
150 z2_open( struct inode
*inode
, struct file
*filp
)
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
)
165 if ( current_device
== -1 )
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" );
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
);
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" );
206 if ( z2ram_size
!= 0 )
207 printk( KERN_INFO DEVICE_NAME
208 ": using %iK of Zorro II RAM\n",
209 z2_count
* Z2RAM_CHUNK1024
);
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" );
224 if ( z2ram_size
!= 0 )
225 printk( KERN_INFO DEVICE_NAME
226 ": using %iK Chip RAM\n",
227 chip_count
* Z2RAM_CHUNK1024
);
235 if ( z2ram_size
== 0 )
238 printk( KERN_NOTICE DEVICE_NAME
239 ": no unused ZII/Chip RAM found\n" );
243 current_device
= device
;
244 z2ram_size
<<= Z2RAM_CHUNKSHIFT
;
245 z2_sizes
[ device
] = z2ram_size
>> 10;
246 blk_size
[ MAJOR_NR
] = z2_sizes
;
257 z2_release( struct inode
*inode
, struct file
*filp
)
260 if ( current_device
== -1 )
263 sync_dev( inode
->i_rdev
);
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 */
283 z2_release
, /* release */
284 block_fsync
/* fsync */
291 if ( !MACH_IS_AMIGA
)
294 if ( register_blkdev( MAJOR_NR
, DEVICE_NAME
, &z2_fops
) )
296 printk( KERN_ERR DEVICE_NAME
": Unable to get major %d\n",
301 blk_dev
[ MAJOR_NR
].request_fn
= DEVICE_REQUEST
;
302 blksize_size
[ MAJOR_NR
] = z2_blocksizes
;
303 blk_size
[ MAJOR_NR
] = z2_sizes
;
317 printk( KERN_INFO DEVICE_NAME
": loaded as module\n" );
324 cleanup_module( void )
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 )
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
)