mmio_nvram.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * memory mapped NVRAM
  3. *
  4. * (C) Copyright IBM Corp. 2005
  5. *
  6. * Authors : Utz Bacher <utz.bacher@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/fs.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/types.h>
  27. #include <asm/machdep.h>
  28. #include <asm/nvram.h>
  29. #include <asm/prom.h>
  30. static void __iomem *mmio_nvram_start;
  31. static long mmio_nvram_len;
  32. static DEFINE_SPINLOCK(mmio_nvram_lock);
  33. static ssize_t mmio_nvram_read(char *buf, size_t count, loff_t *index)
  34. {
  35. unsigned long flags;
  36. if (*index >= mmio_nvram_len)
  37. return 0;
  38. if (*index + count > mmio_nvram_len)
  39. count = mmio_nvram_len - *index;
  40. spin_lock_irqsave(&mmio_nvram_lock, flags);
  41. memcpy_fromio(buf, mmio_nvram_start + *index, count);
  42. spin_unlock_irqrestore(&mmio_nvram_lock, flags);
  43. *index += count;
  44. return count;
  45. }
  46. static unsigned char mmio_nvram_read_val(int addr)
  47. {
  48. unsigned long flags;
  49. unsigned char val;
  50. if (addr >= mmio_nvram_len)
  51. return 0xff;
  52. spin_lock_irqsave(&mmio_nvram_lock, flags);
  53. val = ioread8(mmio_nvram_start + addr);
  54. spin_unlock_irqrestore(&mmio_nvram_lock, flags);
  55. return val;
  56. }
  57. static ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index)
  58. {
  59. unsigned long flags;
  60. if (*index >= mmio_nvram_len)
  61. return 0;
  62. if (*index + count > mmio_nvram_len)
  63. count = mmio_nvram_len - *index;
  64. spin_lock_irqsave(&mmio_nvram_lock, flags);
  65. memcpy_toio(mmio_nvram_start + *index, buf, count);
  66. spin_unlock_irqrestore(&mmio_nvram_lock, flags);
  67. *index += count;
  68. return count;
  69. }
  70. static void mmio_nvram_write_val(int addr, unsigned char val)
  71. {
  72. unsigned long flags;
  73. if (addr < mmio_nvram_len) {
  74. spin_lock_irqsave(&mmio_nvram_lock, flags);
  75. iowrite8(val, mmio_nvram_start + addr);
  76. spin_unlock_irqrestore(&mmio_nvram_lock, flags);
  77. }
  78. }
  79. static ssize_t mmio_nvram_get_size(void)
  80. {
  81. return mmio_nvram_len;
  82. }
  83. int __init mmio_nvram_init(void)
  84. {
  85. struct device_node *nvram_node;
  86. unsigned long nvram_addr;
  87. struct resource r;
  88. int ret;
  89. nvram_node = of_find_node_by_type(NULL, "nvram");
  90. if (!nvram_node)
  91. nvram_node = of_find_compatible_node(NULL, NULL, "nvram");
  92. if (!nvram_node) {
  93. printk(KERN_WARNING "nvram: no node found in device-tree\n");
  94. return -ENODEV;
  95. }
  96. ret = of_address_to_resource(nvram_node, 0, &r);
  97. if (ret) {
  98. printk(KERN_WARNING "nvram: failed to get address (err %d)\n",
  99. ret);
  100. goto out;
  101. }
  102. nvram_addr = r.start;
  103. mmio_nvram_len = resource_size(&r);
  104. if ( (!mmio_nvram_len) || (!nvram_addr) ) {
  105. printk(KERN_WARNING "nvram: address or length is 0\n");
  106. ret = -EIO;
  107. goto out;
  108. }
  109. mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len);
  110. if (!mmio_nvram_start) {
  111. printk(KERN_WARNING "nvram: failed to ioremap\n");
  112. ret = -ENOMEM;
  113. goto out;
  114. }
  115. printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n",
  116. mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start);
  117. ppc_md.nvram_read_val = mmio_nvram_read_val;
  118. ppc_md.nvram_write_val = mmio_nvram_write_val;
  119. ppc_md.nvram_read = mmio_nvram_read;
  120. ppc_md.nvram_write = mmio_nvram_write;
  121. ppc_md.nvram_size = mmio_nvram_get_size;
  122. out:
  123. of_node_put(nvram_node);
  124. return ret;
  125. }