1 From f54ba59d355f59e4c01b5b9c7dbc2d27b47302d2 Mon Sep 17 00:00:00 2001
2 From: Alison Wang <b18965@freescale.com>
3 Date: Thu, 4 Aug 2011 09:59:41 +0800
4 Subject: [PATCH 10/52] Add SRAM char device driver support for MCF5445x
6 Created "/dev/sram" device file and implemented the sram mmap() operation to
7 provide interface for userspace access.
9 Signed-off-by: Alison Wang <b18965@freescale.com>
11 drivers/char/Kconfig | 11 ++++
12 drivers/char/Makefile | 2 +
13 drivers/char/sram.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++++
14 3 files changed, 164 insertions(+), 0 deletions(-)
15 create mode 100644 drivers/char/sram.c
17 --- a/drivers/char/Kconfig
18 +++ b/drivers/char/Kconfig
19 @@ -97,6 +97,17 @@ config DEVKMEM
20 kind of kernel debugging operations.
21 When in doubt, say "N".
24 + tristate "/dev/sram virtual device support"
28 + Say Y here if you want to suppot the SRAM char device. When in
31 + It implements mmap system call to provide interface for
35 tristate "Blackfin JTAG Communication"
37 --- a/drivers/char/Makefile
38 +++ b/drivers/char/Makefile
39 @@ -87,3 +87,5 @@ obj-$(CONFIG_RAMOOPS) += ramoops.o
41 obj-$(CONFIG_JS_RTC) += js-rtc.o
44 +obj-$(CONFIG_DEVSRAM) += sram.o
46 +++ b/drivers/char/sram.c
49 + * linux/drivers/char/sram.c
51 + * Copyright (C) 2009-2011 Freescale Semiconductor, Inc. All Rights Reserved.
52 + * Author: Lanttor.Guo@freescale.com
54 + * SRAM char device driver, implements mmap() system call to provide
55 + * interface for user space access.
57 + * This program is free software; you can redistribute it and/or modify
58 + * it under the terms of the GNU General Public License as published by
59 + * the Free Software Foundation; either version 2 of the License, or
60 + * (at your option) any later version.
63 +#include <linux/module.h>
64 +#include <linux/moduleparam.h>
65 +#include <linux/init.h>
66 +#include <linux/kernel.h>
67 +#include <linux/slab.h>
68 +#include <linux/fs.h>
69 +#include <linux/errno.h>
70 +#include <linux/types.h>
71 +#include <linux/mm.h>
72 +#include <linux/kdev_t.h>
73 +#include <asm/page.h>
74 +#include <linux/cdev.h>
75 +#include <linux/device.h>
77 +MODULE_LICENSE("GPL");
79 +static struct class *sram_class;
80 +static int sram_major;
81 +static int sram_minor;
82 +static struct cdev sram_cdev;
85 + * Set up the cdev structure for sram.
87 +static void sram_setup_cdev(struct cdev *dev, int minor,
88 + const struct file_operations *fops)
90 + int err, devno = MKDEV(sram_major, minor);
92 + cdev_init(dev, fops);
93 + dev->owner = THIS_MODULE;
95 + err = cdev_add(dev, devno, 1);
96 + /* Fail gracefully if need be */
98 + printk(KERN_NOTICE "Error %d adding sram%d", err, minor);
101 +static int sram_open(struct inode *inode, struct file *filp)
103 + filp->f_mapping->backing_dev_info = &directly_mappable_cdev_bdi;
107 +static int sram_release(struct inode *inode, struct file *filp)
112 +void sram_vma_open(struct vm_area_struct *vma)
114 + printk(KERN_DEBUG "Sram VMA open, virt %lx, phys %lx\n",
116 + CONFIG_SRAM_BASE + (vma->vm_pgoff << PAGE_SHIFT));
119 +void sram_vma_close(struct vm_area_struct *vma)
121 + printk(KERN_DEBUG "Sram VMA close.\n");
125 +static struct vm_operations_struct sram_remap_vm_ops = {
126 + .open = sram_vma_open,
127 + .close = sram_vma_close,
130 +static int sram_remap_mmap(struct file *filp, struct vm_area_struct *vma)
132 + size_t size = vma->vm_end - vma->vm_start;
134 + if (PAGE_ALIGN(size) > CONFIG_SRAM_SIZE) {
135 + printk(KERN_ERR "required length exceed the size "
136 + "of physical sram (%x)\n", CONFIG_SRAM_SIZE);
140 + if ((CONFIG_SRAM_BASE + (vma->vm_pgoff << PAGE_SHIFT) + size)
141 + > (CONFIG_SRAM_BASE + CONFIG_SRAM_SIZE)) {
142 + printk(KERN_ERR "required sram range exceed the size "
143 + "of phisical sram\n");
147 + if (remap_pfn_range(vma, vma->vm_start,
148 + (CONFIG_SRAM_BASE >> PAGE_SHIFT) + vma->vm_pgoff,
150 + vma->vm_page_prot)) {
151 + printk(KERN_ERR "remap_pfn_range faile at %s()\n", __func__);
155 + vma->vm_ops = &sram_remap_vm_ops;
159 +static const struct file_operations sram_ops = {
160 + .owner = THIS_MODULE,
162 + .release = sram_release,
163 + .mmap = sram_remap_mmap,
166 +static int __init sram_chrdev_init(void)
168 + int minor_devs = 1;
173 + result = alloc_chrdev_region(&dev, sram_minor, minor_devs, "sram");
174 + sram_major = MAJOR(dev);
176 + printk(KERN_WARNING "sram: can't get major %d\n", sram_major);
180 + sram_setup_cdev(&sram_cdev, 0, &sram_ops);
182 + sram_class = class_create(THIS_MODULE, "sram");
183 + device_create(sram_class, NULL, MKDEV(sram_major, sram_minor),
189 +static void sram_chrdev_cleanup(void)
191 + cdev_del(&sram_cdev);
192 + device_destroy(sram_class, MKDEV(sram_major, sram_minor));
193 + class_destroy(sram_class);
194 + unregister_chrdev_region(MKDEV(sram_major, 0), 1);
197 +module_init(sram_chrdev_init);
198 +module_exit(sram_chrdev_cleanup);