xref: /openbmc/linux/arch/m68k/mm/hwtest.c (revision 1da177e4)
11da177e4SLinus Torvalds /* Tests for presence or absence of hardware registers.
21da177e4SLinus Torvalds  * This code was originally in atari/config.c, but I noticed
31da177e4SLinus Torvalds  * that it was also in drivers/nubus/nubus.c and I wanted to
41da177e4SLinus Torvalds  * use it in hp300/config.c, so it seemed sensible to pull it
51da177e4SLinus Torvalds  * out into its own file.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * The test is for use when trying to read a hardware register
81da177e4SLinus Torvalds  * that isn't present would cause a bus error. We set up a
91da177e4SLinus Torvalds  * temporary handler so that this doesn't kill the kernel.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * There is a test-by-reading and a test-by-writing; I present
121da177e4SLinus Torvalds  * them here complete with the comments from the original atari
131da177e4SLinus Torvalds  * config.c...
141da177e4SLinus Torvalds  *                -- PMM <pmaydell@chiark.greenend.org.uk>, 05/1998
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds /* This function tests for the presence of an address, specially a
181da177e4SLinus Torvalds  * hardware register address. It is called very early in the kernel
191da177e4SLinus Torvalds  * initialization process, when the VBR register isn't set up yet. On
201da177e4SLinus Torvalds  * an Atari, it still points to address 0, which is unmapped. So a bus
211da177e4SLinus Torvalds  * error would cause another bus error while fetching the exception
221da177e4SLinus Torvalds  * vector, and the CPU would do nothing at all. So we needed to set up
231da177e4SLinus Torvalds  * a temporary VBR and a vector table for the duration of the test.
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #include <linux/module.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds int hwreg_present( volatile void *regp )
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds     int	ret = 0;
311da177e4SLinus Torvalds     long	save_sp, save_vbr;
321da177e4SLinus Torvalds     long	tmp_vectors[3];
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds     __asm__ __volatile__
351da177e4SLinus Torvalds 	(	"movec	%/vbr,%2\n\t"
361da177e4SLinus Torvalds 		"movel	#Lberr1,%4@(8)\n\t"
371da177e4SLinus Torvalds                 "movec	%4,%/vbr\n\t"
381da177e4SLinus Torvalds 		"movel	%/sp,%1\n\t"
391da177e4SLinus Torvalds 		"moveq	#0,%0\n\t"
401da177e4SLinus Torvalds 		"tstb	%3@\n\t"
411da177e4SLinus Torvalds 		"nop\n\t"
421da177e4SLinus Torvalds 		"moveq	#1,%0\n"
431da177e4SLinus Torvalds                 "Lberr1:\n\t"
441da177e4SLinus Torvalds 		"movel	%1,%/sp\n\t"
451da177e4SLinus Torvalds 		"movec	%2,%/vbr"
461da177e4SLinus Torvalds 		: "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
471da177e4SLinus Torvalds 		: "a" (regp), "a" (tmp_vectors)
481da177e4SLinus Torvalds                 );
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds     return( ret );
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds EXPORT_SYMBOL(hwreg_present);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /* Basically the same, but writes a value into a word register, protected
551da177e4SLinus Torvalds  * by a bus error handler. Returns 1 if successful, 0 otherwise.
561da177e4SLinus Torvalds  */
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds int hwreg_write( volatile void *regp, unsigned short val )
591da177e4SLinus Torvalds {
601da177e4SLinus Torvalds 	int		ret;
611da177e4SLinus Torvalds 	long	save_sp, save_vbr;
621da177e4SLinus Torvalds 	long	tmp_vectors[3];
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	__asm__ __volatile__
651da177e4SLinus Torvalds 	(	"movec	%/vbr,%2\n\t"
661da177e4SLinus Torvalds 		"movel	#Lberr2,%4@(8)\n\t"
671da177e4SLinus Torvalds 		"movec	%4,%/vbr\n\t"
681da177e4SLinus Torvalds 		"movel	%/sp,%1\n\t"
691da177e4SLinus Torvalds 		"moveq	#0,%0\n\t"
701da177e4SLinus Torvalds 		"movew	%5,%3@\n\t"
711da177e4SLinus Torvalds 		"nop	\n\t"	/* If this nop isn't present, 'ret' may already be
721da177e4SLinus Torvalds 				 * loaded with 1 at the time the bus error
731da177e4SLinus Torvalds 				 * happens! */
741da177e4SLinus Torvalds 		"moveq	#1,%0\n"
751da177e4SLinus Torvalds 	"Lberr2:\n\t"
761da177e4SLinus Torvalds 		"movel	%1,%/sp\n\t"
771da177e4SLinus Torvalds 		"movec	%2,%/vbr"
781da177e4SLinus Torvalds 		: "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
791da177e4SLinus Torvalds 		: "a" (regp), "a" (tmp_vectors), "g" (val)
801da177e4SLinus Torvalds 	);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	return( ret );
831da177e4SLinus Torvalds }
841da177e4SLinus Torvalds EXPORT_SYMBOL(hwreg_write);
851da177e4SLinus Torvalds 
86