Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / powerpc / sysdev / axonram.c
blobd359d6e929752f5cd7258a127b8845f13e3a2791
1 /*
2 * (C) Copyright IBM Deutschland Entwicklung GmbH 2006
4 * Author: Maxim Shchetynin <maxim@de.ibm.com>
6 * Axon DDR2 device driver.
7 * It registers one block device per Axon's DDR2 memory bank found on a system.
8 * Block devices are called axonram?, their major and minor numbers are
9 * available in /proc/devices, /proc/partitions or in /sys/block/axonram?/dev.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/bio.h>
27 #include <linux/blkdev.h>
28 #include <linux/buffer_head.h>
29 #include <linux/device.h>
30 #include <linux/errno.h>
31 #include <linux/fs.h>
32 #include <linux/genhd.h>
33 #include <linux/interrupt.h>
34 #include <linux/io.h>
35 #include <linux/ioport.h>
36 #include <linux/irq.h>
37 #include <linux/irqreturn.h>
38 #include <linux/kernel.h>
39 #include <linux/mm.h>
40 #include <linux/mod_devicetable.h>
41 #include <linux/module.h>
42 #include <linux/slab.h>
43 #include <linux/string.h>
44 #include <linux/types.h>
45 #include <linux/of_device.h>
46 #include <linux/of_platform.h>
48 #include <asm/page.h>
49 #include <asm/prom.h>
51 #define AXON_RAM_MODULE_NAME "axonram"
52 #define AXON_RAM_DEVICE_NAME "axonram"
53 #define AXON_RAM_MINORS_PER_DISK 16
54 #define AXON_RAM_BLOCK_SHIFT PAGE_SHIFT
55 #define AXON_RAM_BLOCK_SIZE 1 << AXON_RAM_BLOCK_SHIFT
56 #define AXON_RAM_SECTOR_SHIFT 9
57 #define AXON_RAM_SECTOR_SIZE 1 << AXON_RAM_SECTOR_SHIFT
58 #define AXON_RAM_IRQ_FLAGS IRQF_SHARED | IRQF_TRIGGER_RISING
60 struct axon_ram_bank {
61 struct of_device *device;
62 struct gendisk *disk;
63 unsigned int irq_id;
64 unsigned long ph_addr;
65 unsigned long io_addr;
66 unsigned long size;
67 unsigned long ecc_counter;
70 static ssize_t
71 axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf)
73 struct of_device *device = to_of_device(dev);
74 struct axon_ram_bank *bank = device->dev.platform_data;
76 BUG_ON(!bank);
78 return sprintf(buf, "%ld\n", bank->ecc_counter);
81 static DEVICE_ATTR(ecc, S_IRUGO, axon_ram_sysfs_ecc, NULL);
83 /**
84 * axon_ram_irq_handler - interrupt handler for Axon RAM ECC
85 * @irq: interrupt ID
86 * @dev: pointer to of_device
88 static irqreturn_t
89 axon_ram_irq_handler(int irq, void *dev)
91 struct of_device *device = dev;
92 struct axon_ram_bank *bank = device->dev.platform_data;
94 BUG_ON(!bank);
96 dev_err(&device->dev, "Correctable memory error occured\n");
97 bank->ecc_counter++;
98 return IRQ_HANDLED;
102 * axon_ram_make_request - make_request() method for block device
103 * @queue, @bio: see blk_queue_make_request()
105 static int
106 axon_ram_make_request(struct request_queue *queue, struct bio *bio)
108 struct axon_ram_bank *bank = bio->bi_bdev->bd_disk->private_data;
109 unsigned long phys_mem, phys_end;
110 void *user_mem;
111 struct bio_vec *vec;
112 unsigned int transfered;
113 unsigned short idx;
114 int rc = 0;
116 phys_mem = bank->io_addr + (bio->bi_sector << AXON_RAM_SECTOR_SHIFT);
117 phys_end = bank->io_addr + bank->size;
118 transfered = 0;
119 bio_for_each_segment(vec, bio, idx) {
120 if (unlikely(phys_mem + vec->bv_len > phys_end)) {
121 bio_io_error(bio);
122 rc = -ERANGE;
123 break;
126 user_mem = page_address(vec->bv_page) + vec->bv_offset;
127 if (bio_data_dir(bio) == READ)
128 memcpy(user_mem, (void *) phys_mem, vec->bv_len);
129 else
130 memcpy((void *) phys_mem, user_mem, vec->bv_len);
132 phys_mem += vec->bv_len;
133 transfered += vec->bv_len;
135 bio_endio(bio, 0);
137 return rc;
141 * axon_ram_direct_access - direct_access() method for block device
142 * @device, @sector, @data: see block_device_operations method
144 static int
145 axon_ram_direct_access(struct block_device *device, sector_t sector,
146 unsigned long *data)
148 struct axon_ram_bank *bank = device->bd_disk->private_data;
149 loff_t offset;
151 offset = sector << AXON_RAM_SECTOR_SHIFT;
152 if (offset >= bank->size) {
153 dev_err(&bank->device->dev, "Access outside of address space\n");
154 return -ERANGE;
157 *data = bank->ph_addr + offset;
159 return 0;
162 static struct block_device_operations axon_ram_devops = {
163 .owner = THIS_MODULE,
164 .direct_access = axon_ram_direct_access
168 * axon_ram_probe - probe() method for platform driver
169 * @device, @device_id: see of_platform_driver method
171 static int
172 axon_ram_probe(struct of_device *device, const struct of_device_id *device_id)
174 static int axon_ram_bank_id = -1;
175 struct axon_ram_bank *bank;
176 struct resource resource;
177 int rc = 0;
179 axon_ram_bank_id++;
181 dev_info(&device->dev, "Found memory controller on %s\n",
182 device->node->full_name);
184 bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL);
185 if (bank == NULL) {
186 dev_err(&device->dev, "Out of memory\n");
187 rc = -ENOMEM;
188 goto failed;
191 device->dev.platform_data = bank;
193 bank->device = device;
195 if (of_address_to_resource(device->node, 0, &resource) != 0) {
196 dev_err(&device->dev, "Cannot access device tree\n");
197 rc = -EFAULT;
198 goto failed;
201 bank->size = resource.end - resource.start + 1;
203 if (bank->size == 0) {
204 dev_err(&device->dev, "No DDR2 memory found for %s%d\n",
205 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
206 rc = -ENODEV;
207 goto failed;
210 dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n",
211 AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20);
213 bank->ph_addr = resource.start;
214 bank->io_addr = (unsigned long) ioremap_flags(
215 bank->ph_addr, bank->size, _PAGE_NO_CACHE);
216 if (bank->io_addr == 0) {
217 dev_err(&device->dev, "ioremap() failed\n");
218 rc = -EFAULT;
219 goto failed;
222 bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK);
223 if (bank->disk == NULL) {
224 dev_err(&device->dev, "Cannot register disk\n");
225 rc = -EFAULT;
226 goto failed;
229 bank->disk->first_minor = 0;
230 bank->disk->fops = &axon_ram_devops;
231 bank->disk->private_data = bank;
232 bank->disk->driverfs_dev = &device->dev;
234 sprintf(bank->disk->disk_name, "%s%d",
235 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
236 bank->disk->major = register_blkdev(0, bank->disk->disk_name);
237 if (bank->disk->major < 0) {
238 dev_err(&device->dev, "Cannot register block device\n");
239 rc = -EFAULT;
240 goto failed;
243 bank->disk->queue = blk_alloc_queue(GFP_KERNEL);
244 if (bank->disk->queue == NULL) {
245 dev_err(&device->dev, "Cannot register disk queue\n");
246 rc = -EFAULT;
247 goto failed;
250 set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT);
251 blk_queue_make_request(bank->disk->queue, axon_ram_make_request);
252 blk_queue_hardsect_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE);
253 add_disk(bank->disk);
255 bank->irq_id = irq_of_parse_and_map(device->node, 0);
256 if (bank->irq_id == NO_IRQ) {
257 dev_err(&device->dev, "Cannot access ECC interrupt ID\n");
258 rc = -EFAULT;
259 goto failed;
262 rc = request_irq(bank->irq_id, axon_ram_irq_handler,
263 AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device);
264 if (rc != 0) {
265 dev_err(&device->dev, "Cannot register ECC interrupt handler\n");
266 bank->irq_id = NO_IRQ;
267 rc = -EFAULT;
268 goto failed;
271 rc = device_create_file(&device->dev, &dev_attr_ecc);
272 if (rc != 0) {
273 dev_err(&device->dev, "Cannot create sysfs file\n");
274 rc = -EFAULT;
275 goto failed;
278 return 0;
280 failed:
281 if (bank != NULL) {
282 if (bank->irq_id != NO_IRQ)
283 free_irq(bank->irq_id, device);
284 if (bank->disk != NULL) {
285 if (bank->disk->major > 0)
286 unregister_blkdev(bank->disk->major,
287 bank->disk->disk_name);
288 del_gendisk(bank->disk);
290 device->dev.platform_data = NULL;
291 if (bank->io_addr != 0)
292 iounmap((void __iomem *) bank->io_addr);
293 kfree(bank);
296 return rc;
300 * axon_ram_remove - remove() method for platform driver
301 * @device: see of_platform_driver method
303 static int
304 axon_ram_remove(struct of_device *device)
306 struct axon_ram_bank *bank = device->dev.platform_data;
308 BUG_ON(!bank || !bank->disk);
310 device_remove_file(&device->dev, &dev_attr_ecc);
311 free_irq(bank->irq_id, device);
312 unregister_blkdev(bank->disk->major, bank->disk->disk_name);
313 del_gendisk(bank->disk);
314 iounmap((void __iomem *) bank->io_addr);
315 kfree(bank);
317 return 0;
320 static struct of_device_id axon_ram_device_id[] = {
322 .type = "dma-memory"
327 static struct of_platform_driver axon_ram_driver = {
328 .match_table = axon_ram_device_id,
329 .probe = axon_ram_probe,
330 .remove = axon_ram_remove,
331 .driver = {
332 .owner = THIS_MODULE,
333 .name = AXON_RAM_MODULE_NAME,
338 * axon_ram_init
340 static int __init
341 axon_ram_init(void)
343 return of_register_platform_driver(&axon_ram_driver);
347 * axon_ram_exit
349 static void __exit
350 axon_ram_exit(void)
352 of_unregister_platform_driver(&axon_ram_driver);
355 module_init(axon_ram_init);
356 module_exit(axon_ram_exit);
358 MODULE_LICENSE("GPL");
359 MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>");
360 MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE");