mmio_nvram.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ssize_t mmio_nvram_write(char *buf, size_t count, loff_t *index)
  47. {
  48. unsigned long flags;
  49. if (*index >= mmio_nvram_len)
  50. return 0;
  51. if (*index + count > mmio_nvram_len)
  52. count = mmio_nvram_len - *index;
  53. spin_lock_irqsave(&mmio_nvram_lock, flags);
  54. memcpy_toio(mmio_nvram_start + *index, buf, count);
  55. spin_unlock_irqrestore(&mmio_nvram_lock, flags);
  56. *index += count;
  57. return count;
  58. }
  59. static ssize_t mmio_nvram_get_size(void)
  60. {
  61. return mmio_nvram_len;
  62. }
  63. int __init mmio_nvram_init(void)
  64. {
  65. struct device_node *nvram_node;
  66. unsigned long nvram_addr;
  67. struct resource r;
  68. int ret;
  69. nvram_node = of_find_node_by_type(NULL, "nvram");
  70. if (!nvram_node) {
  71. printk(KERN_WARNING "nvram: no node found in device-tree\n");
  72. return -ENODEV;
  73. }
  74. ret = of_address_to_resource(nvram_node, 0, &r);
  75. if (ret) {
  76. printk(KERN_WARNING "nvram: failed to get address (err %d)\n",
  77. ret);
  78. goto out;
  79. }
  80. nvram_addr = r.start;
  81. mmio_nvram_len = r.end - r.start + 1;
  82. if ( (!mmio_nvram_len) || (!nvram_addr) ) {
  83. printk(KERN_WARNING "nvram: address or length is 0\n");
  84. ret = -EIO;
  85. goto out;
  86. }
  87. mmio_nvram_start = ioremap(nvram_addr, mmio_nvram_len);
  88. if (!mmio_nvram_start) {
  89. printk(KERN_WARNING "nvram: failed to ioremap\n");
  90. ret = -ENOMEM;
  91. goto out;
  92. }
  93. printk(KERN_INFO "mmio NVRAM, %luk at 0x%lx mapped to %p\n",
  94. mmio_nvram_len >> 10, nvram_addr, mmio_nvram_start);
  95. ppc_md.nvram_read = mmio_nvram_read;
  96. ppc_md.nvram_write = mmio_nvram_write;
  97. ppc_md.nvram_size = mmio_nvram_get_size;
  98. out:
  99. of_node_put(nvram_node);
  100. return ret;
  101. }