blob: 4d3ba63bba79ad6ee53cb1c49d1d1cfd23944be8 [file] [log] [blame]
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +02001/*
2 * (C) Copyright IBM Deutschland Entwicklung GmbH 2006
3 *
4 * Author: Maxim Shchetynin <maxim@de.ibm.com>
5 *
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.
10 *
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.
15 *
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.
20 *
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.
24 */
25
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 <asm/of_device.h>
46#include <asm/of_platform.h>
47#include <asm/page.h>
48#include <asm/prom.h>
49
50#define AXON_RAM_MODULE_NAME "axonram"
51#define AXON_RAM_DEVICE_NAME "axonram"
52#define AXON_RAM_MINORS_PER_DISK 16
53#define AXON_RAM_BLOCK_SHIFT PAGE_SHIFT
54#define AXON_RAM_BLOCK_SIZE 1 << AXON_RAM_BLOCK_SHIFT
55#define AXON_RAM_SECTOR_SHIFT 9
56#define AXON_RAM_SECTOR_SIZE 1 << AXON_RAM_SECTOR_SHIFT
57#define AXON_RAM_IRQ_FLAGS IRQF_SHARED | IRQF_TRIGGER_RISING
58
59struct axon_ram_bank {
60 struct of_device *device;
61 struct gendisk *disk;
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +100062 unsigned int irq_id;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020063 unsigned long ph_addr;
64 unsigned long io_addr;
65 unsigned long size;
66 unsigned long ecc_counter;
67};
68
69static ssize_t
70axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf)
71{
72 struct of_device *device = to_of_device(dev);
73 struct axon_ram_bank *bank = device->dev.platform_data;
74
75 BUG_ON(!bank);
76
77 return sprintf(buf, "%ld\n", bank->ecc_counter);
78}
79
80static DEVICE_ATTR(ecc, S_IRUGO, axon_ram_sysfs_ecc, NULL);
81
82/**
83 * axon_ram_irq_handler - interrupt handler for Axon RAM ECC
84 * @irq: interrupt ID
85 * @dev: pointer to of_device
86 */
87static irqreturn_t
88axon_ram_irq_handler(int irq, void *dev)
89{
90 struct of_device *device = dev;
91 struct axon_ram_bank *bank = device->dev.platform_data;
92
93 BUG_ON(!bank);
94
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +100095 dev_err(&device->dev, "Correctable memory error occured\n");
96 bank->ecc_counter++;
97 return IRQ_HANDLED;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020098}
99
100/**
101 * axon_ram_make_request - make_request() method for block device
102 * @queue, @bio: see blk_queue_make_request()
103 */
104static int
105axon_ram_make_request(struct request_queue *queue, struct bio *bio)
106{
107 struct axon_ram_bank *bank = bio->bi_bdev->bd_disk->private_data;
108 unsigned long phys_mem, phys_end;
109 void *user_mem;
110 struct bio_vec *vec;
111 unsigned int transfered;
112 unsigned short idx;
113 int rc = 0;
114
115 phys_mem = bank->io_addr + (bio->bi_sector << AXON_RAM_SECTOR_SHIFT);
116 phys_end = bank->io_addr + bank->size;
117 transfered = 0;
118 bio_for_each_segment(vec, bio, idx) {
119 if (unlikely(phys_mem + vec->bv_len > phys_end)) {
120 bio_io_error(bio, bio->bi_size);
121 rc = -ERANGE;
122 break;
123 }
124
125 user_mem = page_address(vec->bv_page) + vec->bv_offset;
126 if (bio_data_dir(bio) == READ)
127 memcpy(user_mem, (void *) phys_mem, vec->bv_len);
128 else
129 memcpy((void *) phys_mem, user_mem, vec->bv_len);
130
131 phys_mem += vec->bv_len;
132 transfered += vec->bv_len;
133 }
134 bio_endio(bio, transfered, 0);
135
136 return rc;
137}
138
139/**
140 * axon_ram_direct_access - direct_access() method for block device
141 * @device, @sector, @data: see block_device_operations method
142 */
143static int
144axon_ram_direct_access(struct block_device *device, sector_t sector,
145 unsigned long *data)
146{
147 struct axon_ram_bank *bank = device->bd_disk->private_data;
148 loff_t offset;
149
150 offset = sector << AXON_RAM_SECTOR_SHIFT;
151 if (offset >= bank->size) {
152 dev_err(&bank->device->dev, "Access outside of address space\n");
153 return -ERANGE;
154 }
155
156 *data = bank->ph_addr + offset;
157
158 return 0;
159}
160
161static struct block_device_operations axon_ram_devops = {
162 .owner = THIS_MODULE,
163 .direct_access = axon_ram_direct_access
164};
165
166/**
167 * axon_ram_probe - probe() method for platform driver
168 * @device, @device_id: see of_platform_driver method
169 */
170static int
171axon_ram_probe(struct of_device *device, const struct of_device_id *device_id)
172{
173 static int axon_ram_bank_id = -1;
174 struct axon_ram_bank *bank;
175 struct resource resource;
176 int rc = 0;
177
178 axon_ram_bank_id++;
179
180 dev_info(&device->dev, "Found memory controller on %s\n",
181 device->node->full_name);
182
183 bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL);
184 if (bank == NULL) {
185 dev_err(&device->dev, "Out of memory\n");
186 rc = -ENOMEM;
187 goto failed;
188 }
189
190 device->dev.platform_data = bank;
191
192 bank->device = device;
193
194 if (of_address_to_resource(device->node, 0, &resource) != 0) {
195 dev_err(&device->dev, "Cannot access device tree\n");
196 rc = -EFAULT;
197 goto failed;
198 }
199
200 bank->size = resource.end - resource.start + 1;
201
202 if (bank->size == 0) {
203 dev_err(&device->dev, "No DDR2 memory found for %s%d\n",
204 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
205 rc = -ENODEV;
206 goto failed;
207 }
208
209 dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n",
210 AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20);
211
212 bank->ph_addr = resource.start;
213 bank->io_addr = (unsigned long) ioremap_flags(
214 bank->ph_addr, bank->size, _PAGE_NO_CACHE);
215 if (bank->io_addr == 0) {
216 dev_err(&device->dev, "ioremap() failed\n");
217 rc = -EFAULT;
218 goto failed;
219 }
220
221 bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK);
222 if (bank->disk == NULL) {
223 dev_err(&device->dev, "Cannot register disk\n");
224 rc = -EFAULT;
225 goto failed;
226 }
227
228 bank->disk->first_minor = 0;
229 bank->disk->fops = &axon_ram_devops;
230 bank->disk->private_data = bank;
231 bank->disk->driverfs_dev = &device->dev;
232
233 sprintf(bank->disk->disk_name, "%s%d",
234 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
235 bank->disk->major = register_blkdev(0, bank->disk->disk_name);
236 if (bank->disk->major < 0) {
237 dev_err(&device->dev, "Cannot register block device\n");
238 rc = -EFAULT;
239 goto failed;
240 }
241
242 bank->disk->queue = blk_alloc_queue(GFP_KERNEL);
243 if (bank->disk->queue == NULL) {
244 dev_err(&device->dev, "Cannot register disk queue\n");
245 rc = -EFAULT;
246 goto failed;
247 }
248
249 set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT);
250 blk_queue_make_request(bank->disk->queue, axon_ram_make_request);
251 blk_queue_hardsect_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE);
252 add_disk(bank->disk);
253
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000254 bank->irq_id = irq_of_parse_and_map(device->node, 0);
255 if (bank->irq_id == NO_IRQ) {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200256 dev_err(&device->dev, "Cannot access ECC interrupt ID\n");
257 rc = -EFAULT;
258 goto failed;
259 }
260
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000261 rc = request_irq(bank->irq_id, axon_ram_irq_handler,
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200262 AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device);
263 if (rc != 0) {
264 dev_err(&device->dev, "Cannot register ECC interrupt handler\n");
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000265 bank->irq_id = NO_IRQ;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200266 rc = -EFAULT;
267 goto failed;
268 }
269
270 rc = device_create_file(&device->dev, &dev_attr_ecc);
271 if (rc != 0) {
272 dev_err(&device->dev, "Cannot create sysfs file\n");
273 rc = -EFAULT;
274 goto failed;
275 }
276
277 return 0;
278
279failed:
280 if (bank != NULL) {
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000281 if (bank->irq_id != NO_IRQ)
282 free_irq(bank->irq_id, device);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200283 if (bank->disk != NULL) {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200284 if (bank->disk->major > 0)
285 unregister_blkdev(bank->disk->major,
286 bank->disk->disk_name);
287 del_gendisk(bank->disk);
288 }
289 device->dev.platform_data = NULL;
290 if (bank->io_addr != 0)
291 iounmap((void __iomem *) bank->io_addr);
292 kfree(bank);
293 }
294
295 return rc;
296}
297
298/**
299 * axon_ram_remove - remove() method for platform driver
300 * @device: see of_platform_driver method
301 */
302static int
303axon_ram_remove(struct of_device *device)
304{
305 struct axon_ram_bank *bank = device->dev.platform_data;
306
307 BUG_ON(!bank || !bank->disk);
308
309 device_remove_file(&device->dev, &dev_attr_ecc);
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000310 free_irq(bank->irq_id, device);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200311 unregister_blkdev(bank->disk->major, bank->disk->disk_name);
312 del_gendisk(bank->disk);
313 iounmap((void __iomem *) bank->io_addr);
314 kfree(bank);
315
316 return 0;
317}
318
319static struct of_device_id axon_ram_device_id[] = {
320 {
321 .type = "dma-memory"
322 },
323 {}
324};
325
326static struct of_platform_driver axon_ram_driver = {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200327 .match_table = axon_ram_device_id,
328 .probe = axon_ram_probe,
Stephen Rothwell84dd4672007-10-11 15:19:03 +1000329 .remove = axon_ram_remove,
330 .driver = {
331 .owner = THIS_MODULE,
332 .name = AXON_RAM_MODULE_NAME,
333 },
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200334};
335
336/**
337 * axon_ram_init
338 */
339static int __init
340axon_ram_init(void)
341{
342 return of_register_platform_driver(&axon_ram_driver);
343}
344
345/**
346 * axon_ram_exit
347 */
348static void __exit
349axon_ram_exit(void)
350{
351 of_unregister_platform_driver(&axon_ram_driver);
352}
353
354module_init(axon_ram_init);
355module_exit(axon_ram_exit);
356
357MODULE_LICENSE("GPL");
358MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>");
359MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE");