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