chrp_nvram.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * c 2001 PPC 64 Team, IBM Corp
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * /dev/nvram driver for PPC
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/prom.h>
  18. #include <asm/machdep.h>
  19. static unsigned int nvram_size;
  20. static unsigned char nvram_buf[4];
  21. static DEFINE_SPINLOCK(nvram_lock);
  22. static unsigned char chrp_nvram_read(int addr)
  23. {
  24. unsigned long done, flags;
  25. unsigned char ret;
  26. if (addr >= nvram_size) {
  27. printk(KERN_DEBUG "%s: read addr %d > nvram_size %u\n",
  28. current->comm, addr, nvram_size);
  29. return 0xff;
  30. }
  31. spin_lock_irqsave(&nvram_lock, flags);
  32. if ((call_rtas("nvram-fetch", 3, 2, &done, addr, __pa(nvram_buf), 1) != 0) || 1 != done)
  33. ret = 0xff;
  34. else
  35. ret = nvram_buf[0];
  36. spin_unlock_irqrestore(&nvram_lock, flags);
  37. return ret;
  38. }
  39. static void chrp_nvram_write(int addr, unsigned char val)
  40. {
  41. unsigned long done, flags;
  42. if (addr >= nvram_size) {
  43. printk(KERN_DEBUG "%s: write addr %d > nvram_size %u\n",
  44. current->comm, addr, nvram_size);
  45. return;
  46. }
  47. spin_lock_irqsave(&nvram_lock, flags);
  48. nvram_buf[0] = val;
  49. if ((call_rtas("nvram-store", 3, 2, &done, addr, __pa(nvram_buf), 1) != 0) || 1 != done)
  50. printk(KERN_DEBUG "rtas IO error storing 0x%02x at %d", val, addr);
  51. spin_unlock_irqrestore(&nvram_lock, flags);
  52. }
  53. void __init chrp_nvram_init(void)
  54. {
  55. struct device_node *nvram;
  56. unsigned int *nbytes_p, proplen;
  57. nvram = of_find_node_by_type(NULL, "nvram");
  58. if (nvram == NULL)
  59. return;
  60. nbytes_p = (unsigned int *)get_property(nvram, "#bytes", &proplen);
  61. if (nbytes_p == NULL || proplen != sizeof(unsigned int))
  62. return;
  63. nvram_size = *nbytes_p;
  64. printk(KERN_INFO "CHRP nvram contains %u bytes\n", nvram_size);
  65. of_node_put(nvram);
  66. ppc_md.nvram_read_val = chrp_nvram_read;
  67. ppc_md.nvram_write_val = chrp_nvram_write;
  68. return;
  69. }