hwtest.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /* This function tests for the presence of an address, specially a
  17. * hardware register address. It is called very early in the kernel
  18. * initialization process, when the VBR register isn't set up yet. On
  19. * an Atari, it still points to address 0, which is unmapped. So a bus
  20. * error would cause another bus error while fetching the exception
  21. * vector, and the CPU would do nothing at all. So we needed to set up
  22. * a temporary VBR and a vector table for the duration of the test.
  23. */
  24. #include <linux/module.h>
  25. int hwreg_present( volatile void *regp )
  26. {
  27. int ret = 0;
  28. long save_sp, save_vbr;
  29. long tmp_vectors[3];
  30. __asm__ __volatile__
  31. ( "movec %/vbr,%2\n\t"
  32. "movel #Lberr1,%4@(8)\n\t"
  33. "movec %4,%/vbr\n\t"
  34. "movel %/sp,%1\n\t"
  35. "moveq #0,%0\n\t"
  36. "tstb %3@\n\t"
  37. "nop\n\t"
  38. "moveq #1,%0\n"
  39. "Lberr1:\n\t"
  40. "movel %1,%/sp\n\t"
  41. "movec %2,%/vbr"
  42. : "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
  43. : "a" (regp), "a" (tmp_vectors)
  44. );
  45. return( ret );
  46. }
  47. EXPORT_SYMBOL(hwreg_present);
  48. /* Basically the same, but writes a value into a word register, protected
  49. * by a bus error handler. Returns 1 if successful, 0 otherwise.
  50. */
  51. int hwreg_write( volatile void *regp, unsigned short val )
  52. {
  53. int ret;
  54. long save_sp, save_vbr;
  55. long tmp_vectors[3];
  56. __asm__ __volatile__
  57. ( "movec %/vbr,%2\n\t"
  58. "movel #Lberr2,%4@(8)\n\t"
  59. "movec %4,%/vbr\n\t"
  60. "movel %/sp,%1\n\t"
  61. "moveq #0,%0\n\t"
  62. "movew %5,%3@\n\t"
  63. "nop \n\t" /* If this nop isn't present, 'ret' may already be
  64. * loaded with 1 at the time the bus error
  65. * happens! */
  66. "moveq #1,%0\n"
  67. "Lberr2:\n\t"
  68. "movel %1,%/sp\n\t"
  69. "movec %2,%/vbr"
  70. : "=&d" (ret), "=&r" (save_sp), "=&r" (save_vbr)
  71. : "a" (regp), "a" (tmp_vectors), "g" (val)
  72. );
  73. return( ret );
  74. }
  75. EXPORT_SYMBOL(hwreg_write);