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 PPC64 10 * 11 * This perhaps should live in drivers/char 12 */ 13 14#include <linux/module.h> 15 16#include <linux/types.h> 17#include <linux/errno.h> 18#include <linux/fs.h> 19#include <linux/miscdevice.h> 20#include <linux/fcntl.h> 21#include <linux/nvram.h> 22#include <linux/init.h> 23#include <asm/uaccess.h> 24#include <asm/nvram.h> 25#include <asm/rtas.h> 26#include <asm/prom.h> 27 28static unsigned int rtas_nvram_size; 29static unsigned int nvram_fetch, nvram_store; 30static char nvram_buf[4]; /* assume this is in the first 4GB */ 31 32static loff_t nvram_llseek(struct file *file, loff_t offset, int origin) 33{ 34 switch (origin) { 35 case 1: 36 offset += file->f_pos; 37 break; 38 case 2: 39 offset += rtas_nvram_size; 40 break; 41 } 42 if (offset < 0) 43 return -EINVAL; 44 file->f_pos = offset; 45 return file->f_pos; 46} 47 48 49static ssize_t read_nvram(struct file *file, char *buf, 50 size_t count, loff_t *ppos) 51{ 52 unsigned int i; 53 unsigned long len; 54 char *p = buf; 55 56 if (verify_area(VERIFY_WRITE, buf, count)) 57 return -EFAULT; 58 if (*ppos >= rtas_nvram_size) 59 return 0; 60 for (i = *ppos; count > 0 && i < rtas_nvram_size; ++i, ++p, --count) { 61 if ((rtas_call(nvram_fetch, 3, 2, &len, i, __pa(nvram_buf), 1) != 0) || 62 len != 1) 63 return -EIO; 64 if (__put_user(nvram_buf[0], p)) 65 return -EFAULT; 66 } 67 *ppos = i; 68 return p - buf; 69} 70 71static ssize_t write_nvram(struct file *file, const char *buf, 72 size_t count, loff_t *ppos) 73{ 74 unsigned int i; 75 unsigned long len; 76 const char *p = buf; 77 char c; 78 79 if (verify_area(VERIFY_READ, buf, count)) 80 return -EFAULT; 81 if (*ppos >= rtas_nvram_size) 82 return 0; 83 for (i = *ppos; count > 0 && i < rtas_nvram_size; ++i, ++p, --count) { 84 if (__get_user(c, p)) 85 return -EFAULT; 86 nvram_buf[0] = c; 87 if ((rtas_call(nvram_store, 3, 2, &len, i, __pa(nvram_buf), 1) != 0) || 88 len != 1) 89 return -EIO; 90 } 91 *ppos = i; 92 return p - buf; 93} 94 95static int nvram_ioctl(struct inode *inode, struct file *file, 96 unsigned int cmd, unsigned long arg) 97{ 98 return -EINVAL; 99} 100 101struct file_operations nvram_fops = { 102 owner: THIS_MODULE, 103 llseek: nvram_llseek, 104 read: read_nvram, 105 write: write_nvram, 106 ioctl: nvram_ioctl, 107}; 108 109static struct miscdevice nvram_dev = { 110 NVRAM_MINOR, 111 "nvram", 112 &nvram_fops 113}; 114 115int __init nvram_init(void) 116{ 117 struct device_node *nvram; 118 unsigned int *nbytes_p, proplen; 119 if ((nvram = find_type_devices("nvram")) != NULL) { 120 nbytes_p = (unsigned int *)get_property(nvram, "#bytes", &proplen); 121 if (nbytes_p && proplen == sizeof(unsigned int)) { 122 rtas_nvram_size = *nbytes_p; 123 } 124 } 125 nvram_fetch = rtas_token("nvram-fetch"); 126 nvram_store = rtas_token("nvram-store"); 127 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", rtas_nvram_size); 128 129 misc_register(&nvram_dev); 130 return 0; 131} 132 133void __exit nvram_cleanup(void) 134{ 135 misc_deregister( &nvram_dev ); 136} 137 138module_init(nvram_init); 139module_exit(nvram_cleanup); 140MODULE_LICENSE("GPL"); 141