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