blob: 7a399b4d60a03a5a83d091113cfb257c45f92b39 [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>
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020028#include <linux/device.h>
29#include <linux/errno.h>
30#include <linux/fs.h>
31#include <linux/genhd.h>
32#include <linux/interrupt.h>
33#include <linux/io.h>
34#include <linux/ioport.h>
35#include <linux/irq.h>
36#include <linux/irqreturn.h>
37#include <linux/kernel.h>
38#include <linux/mm.h>
39#include <linux/mod_devicetable.h>
40#include <linux/module.h>
41#include <linux/slab.h>
42#include <linux/string.h>
43#include <linux/types.h>
Jon Loeligereb8f2762007-11-14 04:13:09 +110044#include <linux/of_device.h>
45#include <linux/of_platform.h>
46
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020047#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
Maxim Shchetynin9a234092008-07-16 05:51:41 +100059static int azfs_major, azfs_minor;
60
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020061struct axon_ram_bank {
Grant Likelya454dc52010-07-22 15:52:34 -060062 struct platform_device *device;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020063 struct gendisk *disk;
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +100064 unsigned int irq_id;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020065 unsigned long ph_addr;
66 unsigned long io_addr;
67 unsigned long size;
68 unsigned long ecc_counter;
69};
70
71static ssize_t
72axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf)
73{
Grant Likelya454dc52010-07-22 15:52:34 -060074 struct platform_device *device = to_platform_device(dev);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020075 struct axon_ram_bank *bank = device->dev.platform_data;
76
77 BUG_ON(!bank);
78
79 return sprintf(buf, "%ld\n", bank->ecc_counter);
80}
81
82static DEVICE_ATTR(ecc, S_IRUGO, axon_ram_sysfs_ecc, NULL);
83
84/**
85 * axon_ram_irq_handler - interrupt handler for Axon RAM ECC
86 * @irq: interrupt ID
87 * @dev: pointer to of_device
88 */
89static irqreturn_t
90axon_ram_irq_handler(int irq, void *dev)
91{
Grant Likelya454dc52010-07-22 15:52:34 -060092 struct platform_device *device = dev;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020093 struct axon_ram_bank *bank = device->dev.platform_data;
94
95 BUG_ON(!bank);
96
Lucas De Marchi25985ed2011-03-30 22:57:33 -030097 dev_err(&device->dev, "Correctable memory error occurred\n");
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +100098 bank->ecc_counter++;
99 return IRQ_HANDLED;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200100}
101
102/**
103 * axon_ram_make_request - make_request() method for block device
104 * @queue, @bio: see blk_queue_make_request()
105 */
Jens Axboedece1632015-11-05 10:41:16 -0700106static blk_qc_t
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200107axon_ram_make_request(struct request_queue *queue, struct bio *bio)
108{
109 struct axon_ram_bank *bank = bio->bi_bdev->bd_disk->private_data;
110 unsigned long phys_mem, phys_end;
111 void *user_mem;
Kent Overstreet79886132013-11-23 17:19:00 -0800112 struct bio_vec vec;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200113 unsigned int transfered;
Kent Overstreet79886132013-11-23 17:19:00 -0800114 struct bvec_iter iter;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200115
Kent Overstreet4f024f32013-10-11 15:44:27 -0700116 phys_mem = bank->io_addr + (bio->bi_iter.bi_sector <<
117 AXON_RAM_SECTOR_SHIFT);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200118 phys_end = bank->io_addr + bank->size;
119 transfered = 0;
Kent Overstreet79886132013-11-23 17:19:00 -0800120 bio_for_each_segment(vec, bio, iter) {
121 if (unlikely(phys_mem + vec.bv_len > phys_end)) {
Al Virod81fec02007-10-12 07:00:19 +0100122 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700123 return BLK_QC_T_NONE;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200124 }
125
Kent Overstreet79886132013-11-23 17:19:00 -0800126 user_mem = page_address(vec.bv_page) + vec.bv_offset;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200127 if (bio_data_dir(bio) == READ)
Kent Overstreet79886132013-11-23 17:19:00 -0800128 memcpy(user_mem, (void *) phys_mem, vec.bv_len);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200129 else
Kent Overstreet79886132013-11-23 17:19:00 -0800130 memcpy((void *) phys_mem, user_mem, vec.bv_len);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200131
Kent Overstreet79886132013-11-23 17:19:00 -0800132 phys_mem += vec.bv_len;
133 transfered += vec.bv_len;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200134 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200135 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700136 return BLK_QC_T_NONE;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200137}
138
139/**
140 * axon_ram_direct_access - direct_access() method for block device
141 * @device, @sector, @data: see block_device_operations method
142 */
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200143static long
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200144axon_ram_direct_access(struct block_device *device, sector_t sector,
Dan Williamscb389b92015-08-07 17:41:00 -0400145 void __pmem **kaddr, unsigned long *pfn)
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200146{
147 struct axon_ram_bank *bank = device->bd_disk->private_data;
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200148 loff_t offset = (loff_t)sector << AXON_RAM_SECTOR_SHIFT;
Ross Zwislere2e05392015-08-18 13:55:41 -0600149 void *addr = (void *)(bank->ph_addr + offset);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200150
Ross Zwislere2e05392015-08-18 13:55:41 -0600151 *kaddr = (void __pmem *)addr;
152 *pfn = virt_to_phys(addr) >> PAGE_SHIFT;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200153
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200154 return bank->size - offset;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200155}
156
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700157static const struct block_device_operations axon_ram_devops = {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200158 .owner = THIS_MODULE,
159 .direct_access = axon_ram_direct_access
160};
161
162/**
163 * axon_ram_probe - probe() method for platform driver
Grant Likely00006122011-02-22 19:59:54 -0700164 * @device: see platform_driver method
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200165 */
Grant Likely00006122011-02-22 19:59:54 -0700166static int axon_ram_probe(struct platform_device *device)
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200167{
168 static int axon_ram_bank_id = -1;
169 struct axon_ram_bank *bank;
170 struct resource resource;
171 int rc = 0;
172
173 axon_ram_bank_id++;
174
175 dev_info(&device->dev, "Found memory controller on %s\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700176 device->dev.of_node->full_name);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200177
178 bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL);
179 if (bank == NULL) {
180 dev_err(&device->dev, "Out of memory\n");
181 rc = -ENOMEM;
182 goto failed;
183 }
184
185 device->dev.platform_data = bank;
186
187 bank->device = device;
188
Grant Likely61c7a082010-04-13 16:12:29 -0700189 if (of_address_to_resource(device->dev.of_node, 0, &resource) != 0) {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200190 dev_err(&device->dev, "Cannot access device tree\n");
191 rc = -EFAULT;
192 goto failed;
193 }
194
Joe Perches28f65c112011-06-09 09:13:32 -0700195 bank->size = resource_size(&resource);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200196
197 if (bank->size == 0) {
198 dev_err(&device->dev, "No DDR2 memory found for %s%d\n",
199 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
200 rc = -ENODEV;
201 goto failed;
202 }
203
204 dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n",
205 AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20);
206
207 bank->ph_addr = resource.start;
Anton Blanchard40f1ce72011-05-08 21:43:47 +0000208 bank->io_addr = (unsigned long) ioremap_prot(
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200209 bank->ph_addr, bank->size, _PAGE_NO_CACHE);
210 if (bank->io_addr == 0) {
211 dev_err(&device->dev, "ioremap() failed\n");
212 rc = -EFAULT;
213 goto failed;
214 }
215
216 bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK);
217 if (bank->disk == NULL) {
218 dev_err(&device->dev, "Cannot register disk\n");
219 rc = -EFAULT;
220 goto failed;
221 }
222
Maxim Shchetynin9a234092008-07-16 05:51:41 +1000223 bank->disk->major = azfs_major;
224 bank->disk->first_minor = azfs_minor;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200225 bank->disk->fops = &axon_ram_devops;
226 bank->disk->private_data = bank;
227 bank->disk->driverfs_dev = &device->dev;
228
229 sprintf(bank->disk->disk_name, "%s%d",
230 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200231
232 bank->disk->queue = blk_alloc_queue(GFP_KERNEL);
233 if (bank->disk->queue == NULL) {
234 dev_err(&device->dev, "Cannot register disk queue\n");
235 rc = -EFAULT;
236 goto failed;
237 }
238
239 set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT);
240 blk_queue_make_request(bank->disk->queue, axon_ram_make_request);
Martin K. Petersene1defc42009-05-22 17:17:49 -0400241 blk_queue_logical_block_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200242 add_disk(bank->disk);
243
Grant Likely61c7a082010-04-13 16:12:29 -0700244 bank->irq_id = irq_of_parse_and_map(device->dev.of_node, 0);
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000245 if (bank->irq_id == NO_IRQ) {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200246 dev_err(&device->dev, "Cannot access ECC interrupt ID\n");
247 rc = -EFAULT;
248 goto failed;
249 }
250
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000251 rc = request_irq(bank->irq_id, axon_ram_irq_handler,
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200252 AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device);
253 if (rc != 0) {
254 dev_err(&device->dev, "Cannot register ECC interrupt handler\n");
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000255 bank->irq_id = NO_IRQ;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200256 rc = -EFAULT;
257 goto failed;
258 }
259
260 rc = device_create_file(&device->dev, &dev_attr_ecc);
261 if (rc != 0) {
262 dev_err(&device->dev, "Cannot create sysfs file\n");
263 rc = -EFAULT;
264 goto failed;
265 }
266
Maxim Shchetynin9a234092008-07-16 05:51:41 +1000267 azfs_minor += bank->disk->minors;
268
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200269 return 0;
270
271failed:
272 if (bank != NULL) {
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000273 if (bank->irq_id != NO_IRQ)
274 free_irq(bank->irq_id, device);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200275 if (bank->disk != NULL) {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200276 if (bank->disk->major > 0)
277 unregister_blkdev(bank->disk->major,
278 bank->disk->disk_name);
279 del_gendisk(bank->disk);
280 }
281 device->dev.platform_data = NULL;
282 if (bank->io_addr != 0)
283 iounmap((void __iomem *) bank->io_addr);
284 kfree(bank);
285 }
286
287 return rc;
288}
289
290/**
291 * axon_ram_remove - remove() method for platform driver
292 * @device: see of_platform_driver method
293 */
294static int
Grant Likelya454dc52010-07-22 15:52:34 -0600295axon_ram_remove(struct platform_device *device)
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200296{
297 struct axon_ram_bank *bank = device->dev.platform_data;
298
299 BUG_ON(!bank || !bank->disk);
300
301 device_remove_file(&device->dev, &dev_attr_ecc);
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000302 free_irq(bank->irq_id, device);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200303 del_gendisk(bank->disk);
304 iounmap((void __iomem *) bank->io_addr);
305 kfree(bank);
306
307 return 0;
308}
309
Uwe Kleine-Königce6d73c2014-09-10 21:56:38 +0200310static const struct of_device_id axon_ram_device_id[] = {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200311 {
312 .type = "dma-memory"
313 },
314 {}
315};
316
Grant Likely00006122011-02-22 19:59:54 -0700317static struct platform_driver axon_ram_driver = {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200318 .probe = axon_ram_probe,
Stephen Rothwell84dd4672007-10-11 15:19:03 +1000319 .remove = axon_ram_remove,
Grant Likely40182942010-04-13 16:13:02 -0700320 .driver = {
321 .name = AXON_RAM_MODULE_NAME,
Grant Likely40182942010-04-13 16:13:02 -0700322 .of_match_table = axon_ram_device_id,
Stephen Rothwell84dd4672007-10-11 15:19:03 +1000323 },
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200324};
325
326/**
327 * axon_ram_init
328 */
329static int __init
330axon_ram_init(void)
331{
Maxim Shchetynin9a234092008-07-16 05:51:41 +1000332 azfs_major = register_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
333 if (azfs_major < 0) {
334 printk(KERN_ERR "%s cannot become block device major number\n",
335 AXON_RAM_MODULE_NAME);
336 return -EFAULT;
337 }
338 azfs_minor = 0;
339
Grant Likely00006122011-02-22 19:59:54 -0700340 return platform_driver_register(&axon_ram_driver);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200341}
342
343/**
344 * axon_ram_exit
345 */
346static void __exit
347axon_ram_exit(void)
348{
Grant Likely00006122011-02-22 19:59:54 -0700349 platform_driver_unregister(&axon_ram_driver);
Maxim Shchetynin9a234092008-07-16 05:51:41 +1000350 unregister_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200351}
352
353module_init(axon_ram_init);
354module_exit(axon_ram_exit);
355
356MODULE_LICENSE("GPL");
357MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>");
358MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE");