xref: /openbmc/linux/arch/m68k/mm/hwtest.c (revision 24cae793)
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;
31e4dc601bSGeert Uytterhoeven 	unsigned long flags;
321da177e4SLinus Torvalds 	long save_sp, save_vbr;
331da177e4SLinus Torvalds 	long tmp_vectors[3];
341da177e4SLinus Torvalds 
35e4dc601bSGeert Uytterhoeven 	local_irq_save(flags);
3624cae793SGeert Uytterhoeven 	__asm__ __volatile__ (
3724cae793SGeert Uytterhoeven 		"movec %/vbr,%2\n\t"
381da177e4SLinus Torvalds 		"movel #Lberr1,%4@(8)\n\t"
391da177e4SLinus Torvalds 		"movec %4,%/vbr\n\t"
401da177e4SLinus Torvalds 		"movel %/sp,%1\n\t"
411da177e4SLinus Torvalds 		"moveq #0,%0\n\t"
421da177e4SLinus Torvalds 		"tstb %3@\n\t"
431da177e4SLinus Torvalds 		"nop\n\t"
441da177e4SLinus Torvalds 		"moveq #1,%0\n"
451da177e4SLinus Torvalds 	"Lberr1:\n\t"
461da177e4SLinus Torvalds 		"movel %1,%/sp\n\t"
471da177e4SLinus Torvalds 		"movec %2,%/vbr"
481da177e4SLinus Torvalds 		: "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
491da177e4SLinus Torvalds 		: "a" (regp), "a" (tmp_vectors)
501da177e4SLinus Torvalds 	);
51e4dc601bSGeert Uytterhoeven 	local_irq_restore(flags);
521da177e4SLinus Torvalds 
5324cae793SGeert Uytterhoeven 	return ret;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds EXPORT_SYMBOL(hwreg_present);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /* Basically the same, but writes a value into a word register, protected
581da177e4SLinus Torvalds  * by a bus error handler. Returns 1 if successful, 0 otherwise.
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds int hwreg_write(volatile void *regp, unsigned short val)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	int ret;
64e4dc601bSGeert Uytterhoeven 	unsigned long flags;
651da177e4SLinus Torvalds 	long save_sp, save_vbr;
661da177e4SLinus Torvalds 	long tmp_vectors[3];
671da177e4SLinus Torvalds 
68e4dc601bSGeert Uytterhoeven 	local_irq_save(flags);
6924cae793SGeert Uytterhoeven 	__asm__ __volatile__ (
7024cae793SGeert Uytterhoeven 		"movec %/vbr,%2\n\t"
711da177e4SLinus Torvalds 		"movel #Lberr2,%4@(8)\n\t"
721da177e4SLinus Torvalds 		"movec %4,%/vbr\n\t"
731da177e4SLinus Torvalds 		"movel %/sp,%1\n\t"
741da177e4SLinus Torvalds 		"moveq #0,%0\n\t"
751da177e4SLinus Torvalds 		"movew %5,%3@\n\t"
7624cae793SGeert Uytterhoeven 		"nop\n\t"
7724cae793SGeert Uytterhoeven 		/*
7824cae793SGeert Uytterhoeven 		 * If this nop isn't present, 'ret' may already be loaded
7924cae793SGeert Uytterhoeven 		 * with 1 at the time the bus error happens!
8024cae793SGeert Uytterhoeven 		 */
811da177e4SLinus Torvalds 		"moveq #1,%0\n"
821da177e4SLinus Torvalds 	"Lberr2:\n\t"
831da177e4SLinus Torvalds 		"movel %1,%/sp\n\t"
841da177e4SLinus Torvalds 		"movec %2,%/vbr"
851da177e4SLinus Torvalds 		: "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
861da177e4SLinus Torvalds 		: "a" (regp), "a" (tmp_vectors), "g" (val)
871da177e4SLinus Torvalds 	);
88e4dc601bSGeert Uytterhoeven 	local_irq_restore(flags);
891da177e4SLinus Torvalds 
9024cae793SGeert Uytterhoeven 	return ret;
911da177e4SLinus Torvalds }
921da177e4SLinus Torvalds EXPORT_SYMBOL(hwreg_write);
931da177e4SLinus Torvalds 
94