switch to 2.6.38
[openwrt/staging/yousong.git] / target / linux / coldfire / patches / 010-Add-SRAM-char-device-driver-support-for-MCF5445x.patch
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
5
6 Created "/dev/sram" device file and implemented the sram mmap() operation to
7 provide interface for userspace access.
8
9 Signed-off-by: Alison Wang <b18965@freescale.com>
10 ---
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
16
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".
22
23 +config DEVSRAM
24 + tristate "/dev/sram virtual device support"
25 + depends on M5445X
26 + default m
27 + help
28 + Say Y here if you want to suppot the SRAM char device. When in
29 + doubt, say "N".
30 +
31 + It implements mmap system call to provide interface for
32 + user space access.
33 +
34 config BFIN_JTAG_COMM
35 tristate "Blackfin JTAG Communication"
36 depends on BLACKFIN
37 --- a/drivers/char/Makefile
38 +++ b/drivers/char/Makefile
39 @@ -87,3 +87,5 @@ obj-$(CONFIG_RAMOOPS) += ramoops.o
40
41 obj-$(CONFIG_JS_RTC) += js-rtc.o
42 js-rtc-y = rtc.o
43 +
44 +obj-$(CONFIG_DEVSRAM) += sram.o
45 --- /dev/null
46 +++ b/drivers/char/sram.c
47 @@ -0,0 +1,151 @@
48 +/*
49 + * linux/drivers/char/sram.c
50 + *
51 + * Copyright (C) 2009-2011 Freescale Semiconductor, Inc. All Rights Reserved.
52 + * Author: Lanttor.Guo@freescale.com
53 + *
54 + * SRAM char device driver, implements mmap() system call to provide
55 + * interface for user space access.
56 + *
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.
61 + */
62 +
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>
76 +
77 +MODULE_LICENSE("GPL");
78 +
79 +static struct class *sram_class;
80 +static int sram_major;
81 +static int sram_minor;
82 +static struct cdev sram_cdev;
83 +
84 +/*
85 + * Set up the cdev structure for sram.
86 + */
87 +static void sram_setup_cdev(struct cdev *dev, int minor,
88 + const struct file_operations *fops)
89 +{
90 + int err, devno = MKDEV(sram_major, minor);
91 +
92 + cdev_init(dev, fops);
93 + dev->owner = THIS_MODULE;
94 + dev->ops = fops;
95 + err = cdev_add(dev, devno, 1);
96 + /* Fail gracefully if need be */
97 + if (err)
98 + printk(KERN_NOTICE "Error %d adding sram%d", err, minor);
99 +}
100 +
101 +static int sram_open(struct inode *inode, struct file *filp)
102 +{
103 + filp->f_mapping->backing_dev_info = &directly_mappable_cdev_bdi;
104 + return 0;
105 +}
106 +
107 +static int sram_release(struct inode *inode, struct file *filp)
108 +{
109 + return 0;
110 +}
111 +
112 +void sram_vma_open(struct vm_area_struct *vma)
113 +{
114 + printk(KERN_DEBUG "Sram VMA open, virt %lx, phys %lx\n",
115 + vma->vm_start,
116 + CONFIG_SRAM_BASE + (vma->vm_pgoff << PAGE_SHIFT));
117 +}
118 +
119 +void sram_vma_close(struct vm_area_struct *vma)
120 +{
121 + printk(KERN_DEBUG "Sram VMA close.\n");
122 +}
123 +
124 +
125 +static struct vm_operations_struct sram_remap_vm_ops = {
126 + .open = sram_vma_open,
127 + .close = sram_vma_close,
128 +};
129 +
130 +static int sram_remap_mmap(struct file *filp, struct vm_area_struct *vma)
131 +{
132 + size_t size = vma->vm_end - vma->vm_start;
133 +
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);
137 + return -EAGAIN;
138 + }
139 +
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");
144 + return -EAGAIN;
145 + }
146 +
147 + if (remap_pfn_range(vma, vma->vm_start,
148 + (CONFIG_SRAM_BASE >> PAGE_SHIFT) + vma->vm_pgoff,
149 + size,
150 + vma->vm_page_prot)) {
151 + printk(KERN_ERR "remap_pfn_range faile at %s()\n", __func__);
152 + return -EAGAIN;
153 + }
154 +
155 + vma->vm_ops = &sram_remap_vm_ops;
156 + return 0;
157 +}
158 +
159 +static const struct file_operations sram_ops = {
160 + .owner = THIS_MODULE,
161 + .open = sram_open,
162 + .release = sram_release,
163 + .mmap = sram_remap_mmap,
164 +};
165 +
166 +static int __init sram_chrdev_init(void)
167 +{
168 + int minor_devs = 1;
169 + int result;
170 + dev_t dev = 0;
171 + sram_minor = 0;
172 +
173 + result = alloc_chrdev_region(&dev, sram_minor, minor_devs, "sram");
174 + sram_major = MAJOR(dev);
175 + if (result < 0) {
176 + printk(KERN_WARNING "sram: can't get major %d\n", sram_major);
177 + return result;
178 + }
179 +
180 + sram_setup_cdev(&sram_cdev, 0, &sram_ops);
181 +
182 + sram_class = class_create(THIS_MODULE, "sram");
183 + device_create(sram_class, NULL, MKDEV(sram_major, sram_minor),
184 + NULL, "sram");
185 +
186 + return 0;
187 +}
188 +
189 +static void sram_chrdev_cleanup(void)
190 +{
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);
195 +}
196 +
197 +module_init(sram_chrdev_init);
198 +module_exit(sram_chrdev_cleanup);