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