blob: fe99aa99987e9c38f153d54ad2b1cc512016fc8a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* Tests for presence or absence of hardware registers.
3 * This code was originally in atari/config.c, but I noticed
4 * that it was also in drivers/nubus/nubus.c and I wanted to
5 * use it in hp300/config.c, so it seemed sensible to pull it
6 * out into its own file.
7 *
8 * The test is for use when trying to read a hardware register
9 * that isn't present would cause a bus error. We set up a
10 * temporary handler so that this doesn't kill the kernel.
11 *
12 * There is a test-by-reading and a test-by-writing; I present
13 * them here complete with the comments from the original atari
14 * config.c...
15 * -- PMM <pmaydell@chiark.greenend.org.uk>, 05/1998
16 */
17
18/* This function tests for the presence of an address, specially a
19 * hardware register address. It is called very early in the kernel
20 * initialization process, when the VBR register isn't set up yet. On
21 * an Atari, it still points to address 0, which is unmapped. So a bus
22 * error would cause another bus error while fetching the exception
23 * vector, and the CPU would do nothing at all. So we needed to set up
24 * a temporary VBR and a vector table for the duration of the test.
25 */
26
27#include <linux/module.h>
28
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020029int hwreg_present(volatile void *regp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020031 int ret = 0;
32 unsigned long flags;
33 long save_sp, save_vbr;
34 long tmp_vectors[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020036 local_irq_save(flags);
37 __asm__ __volatile__ (
38 "movec %/vbr,%2\n\t"
39 "movel #Lberr1,%4@(8)\n\t"
40 "movec %4,%/vbr\n\t"
41 "movel %/sp,%1\n\t"
42 "moveq #0,%0\n\t"
43 "tstb %3@\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 "nop\n\t"
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020045 "moveq #1,%0\n"
46 "Lberr1:\n\t"
47 "movel %1,%/sp\n\t"
48 "movec %2,%/vbr"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 : "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
50 : "a" (regp), "a" (tmp_vectors)
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020051 );
52 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020054 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56EXPORT_SYMBOL(hwreg_present);
57
58/* Basically the same, but writes a value into a word register, protected
59 * by a bus error handler. Returns 1 if successful, 0 otherwise.
60 */
61
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020062int hwreg_write(volatile void *regp, unsigned short val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020064 int ret;
Geert Uytterhoevene4dc6012014-09-28 10:50:06 +020065 unsigned long flags;
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020066 long save_sp, save_vbr;
67 long tmp_vectors[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Geert Uytterhoevene4dc6012014-09-28 10:50:06 +020069 local_irq_save(flags);
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020070 __asm__ __volatile__ (
71 "movec %/vbr,%2\n\t"
72 "movel #Lberr2,%4@(8)\n\t"
73 "movec %4,%/vbr\n\t"
74 "movel %/sp,%1\n\t"
75 "moveq #0,%0\n\t"
76 "movew %5,%3@\n\t"
77 "nop\n\t"
78 /*
79 * If this nop isn't present, 'ret' may already be loaded
80 * with 1 at the time the bus error happens!
81 */
82 "moveq #1,%0\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 "Lberr2:\n\t"
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020084 "movel %1,%/sp\n\t"
85 "movec %2,%/vbr"
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 : "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
87 : "a" (regp), "a" (tmp_vectors), "g" (val)
88 );
Geert Uytterhoevene4dc6012014-09-28 10:50:06 +020089 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Geert Uytterhoeven24cae792014-09-28 11:18:49 +020091 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93EXPORT_SYMBOL(hwreg_write);
94