1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Tests for presence or absence of hardware registers.
3*4882a593Smuzhiyun * This code was originally in atari/config.c, but I noticed
4*4882a593Smuzhiyun * that it was also in drivers/nubus/nubus.c and I wanted to
5*4882a593Smuzhiyun * use it in hp300/config.c, so it seemed sensible to pull it
6*4882a593Smuzhiyun * out into its own file.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * The test is for use when trying to read a hardware register
9*4882a593Smuzhiyun * that isn't present would cause a bus error. We set up a
10*4882a593Smuzhiyun * temporary handler so that this doesn't kill the kernel.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * There is a test-by-reading and a test-by-writing; I present
13*4882a593Smuzhiyun * them here complete with the comments from the original atari
14*4882a593Smuzhiyun * config.c...
15*4882a593Smuzhiyun * -- PMM <pmaydell@chiark.greenend.org.uk>, 05/1998
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /* This function tests for the presence of an address, specially a
19*4882a593Smuzhiyun * hardware register address. It is called very early in the kernel
20*4882a593Smuzhiyun * initialization process, when the VBR register isn't set up yet. On
21*4882a593Smuzhiyun * an Atari, it still points to address 0, which is unmapped. So a bus
22*4882a593Smuzhiyun * error would cause another bus error while fetching the exception
23*4882a593Smuzhiyun * vector, and the CPU would do nothing at all. So we needed to set up
24*4882a593Smuzhiyun * a temporary VBR and a vector table for the duration of the test.
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include <linux/module.h>
28*4882a593Smuzhiyun
hwreg_present(volatile void * regp)29*4882a593Smuzhiyun int hwreg_present(volatile void *regp)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun int ret = 0;
32*4882a593Smuzhiyun unsigned long flags;
33*4882a593Smuzhiyun long save_sp, save_vbr;
34*4882a593Smuzhiyun long tmp_vectors[3];
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun local_irq_save(flags);
37*4882a593Smuzhiyun __asm__ __volatile__ (
38*4882a593Smuzhiyun "movec %/vbr,%2\n\t"
39*4882a593Smuzhiyun "movel #Lberr1,%4@(8)\n\t"
40*4882a593Smuzhiyun "movec %4,%/vbr\n\t"
41*4882a593Smuzhiyun "movel %/sp,%1\n\t"
42*4882a593Smuzhiyun "moveq #0,%0\n\t"
43*4882a593Smuzhiyun "tstb %3@\n\t"
44*4882a593Smuzhiyun "nop\n\t"
45*4882a593Smuzhiyun "moveq #1,%0\n"
46*4882a593Smuzhiyun "Lberr1:\n\t"
47*4882a593Smuzhiyun "movel %1,%/sp\n\t"
48*4882a593Smuzhiyun "movec %2,%/vbr"
49*4882a593Smuzhiyun : "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
50*4882a593Smuzhiyun : "a" (regp), "a" (tmp_vectors)
51*4882a593Smuzhiyun );
52*4882a593Smuzhiyun local_irq_restore(flags);
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun return ret;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun EXPORT_SYMBOL(hwreg_present);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /* Basically the same, but writes a value into a word register, protected
59*4882a593Smuzhiyun * by a bus error handler. Returns 1 if successful, 0 otherwise.
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun
hwreg_write(volatile void * regp,unsigned short val)62*4882a593Smuzhiyun int hwreg_write(volatile void *regp, unsigned short val)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun int ret;
65*4882a593Smuzhiyun unsigned long flags;
66*4882a593Smuzhiyun long save_sp, save_vbr;
67*4882a593Smuzhiyun long tmp_vectors[3];
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun local_irq_save(flags);
70*4882a593Smuzhiyun __asm__ __volatile__ (
71*4882a593Smuzhiyun "movec %/vbr,%2\n\t"
72*4882a593Smuzhiyun "movel #Lberr2,%4@(8)\n\t"
73*4882a593Smuzhiyun "movec %4,%/vbr\n\t"
74*4882a593Smuzhiyun "movel %/sp,%1\n\t"
75*4882a593Smuzhiyun "moveq #0,%0\n\t"
76*4882a593Smuzhiyun "movew %5,%3@\n\t"
77*4882a593Smuzhiyun "nop\n\t"
78*4882a593Smuzhiyun /*
79*4882a593Smuzhiyun * If this nop isn't present, 'ret' may already be loaded
80*4882a593Smuzhiyun * with 1 at the time the bus error happens!
81*4882a593Smuzhiyun */
82*4882a593Smuzhiyun "moveq #1,%0\n"
83*4882a593Smuzhiyun "Lberr2:\n\t"
84*4882a593Smuzhiyun "movel %1,%/sp\n\t"
85*4882a593Smuzhiyun "movec %2,%/vbr"
86*4882a593Smuzhiyun : "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
87*4882a593Smuzhiyun : "a" (regp), "a" (tmp_vectors), "g" (val)
88*4882a593Smuzhiyun );
89*4882a593Smuzhiyun local_irq_restore(flags);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun return ret;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun EXPORT_SYMBOL(hwreg_write);
94*4882a593Smuzhiyun
95