blob: ff75d70f7285dd21bd42ba5e8b5b2b6b9e69ab70 [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>
Dan Williams34c0fd52016-01-15 16:56:14 -080046#include <linux/pfn_t.h>
Jon Loeligereb8f2762007-11-14 04:13:09 +110047
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020048#include <asm/page.h>
49#include <asm/prom.h>
50
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
59
Maxim Shchetynin9a234092008-07-16 05:51:41 +100060static int azfs_major, azfs_minor;
61
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020062struct axon_ram_bank {
Grant Likelya454dc52010-07-22 15:52:34 -060063 struct platform_device *device;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020064 struct gendisk *disk;
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +100065 unsigned int irq_id;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020066 unsigned long ph_addr;
67 unsigned long io_addr;
68 unsigned long size;
69 unsigned long ecc_counter;
70};
71
72static ssize_t
73axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf)
74{
Grant Likelya454dc52010-07-22 15:52:34 -060075 struct platform_device *device = to_platform_device(dev);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020076 struct axon_ram_bank *bank = device->dev.platform_data;
77
78 BUG_ON(!bank);
79
80 return sprintf(buf, "%ld\n", bank->ecc_counter);
81}
82
83static DEVICE_ATTR(ecc, S_IRUGO, axon_ram_sysfs_ecc, NULL);
84
85/**
86 * axon_ram_irq_handler - interrupt handler for Axon RAM ECC
87 * @irq: interrupt ID
88 * @dev: pointer to of_device
89 */
90static irqreturn_t
91axon_ram_irq_handler(int irq, void *dev)
92{
Grant Likelya454dc52010-07-22 15:52:34 -060093 struct platform_device *device = dev;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +020094 struct axon_ram_bank *bank = device->dev.platform_data;
95
96 BUG_ON(!bank);
97
Lucas De Marchi25985ed2011-03-30 22:57:33 -030098 dev_err(&device->dev, "Correctable memory error occurred\n");
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +100099 bank->ecc_counter++;
100 return IRQ_HANDLED;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200101}
102
103/**
104 * axon_ram_make_request - make_request() method for block device
105 * @queue, @bio: see blk_queue_make_request()
106 */
Jens Axboedece1632015-11-05 10:41:16 -0700107static blk_qc_t
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200108axon_ram_make_request(struct request_queue *queue, struct bio *bio)
109{
110 struct axon_ram_bank *bank = bio->bi_bdev->bd_disk->private_data;
111 unsigned long phys_mem, phys_end;
112 void *user_mem;
Kent Overstreet79886132013-11-23 17:19:00 -0800113 struct bio_vec vec;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200114 unsigned int transfered;
Kent Overstreet79886132013-11-23 17:19:00 -0800115 struct bvec_iter iter;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200116
Kent Overstreet4f024f32013-10-11 15:44:27 -0700117 phys_mem = bank->io_addr + (bio->bi_iter.bi_sector <<
118 AXON_RAM_SECTOR_SHIFT);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200119 phys_end = bank->io_addr + bank->size;
120 transfered = 0;
Kent Overstreet79886132013-11-23 17:19:00 -0800121 bio_for_each_segment(vec, bio, iter) {
122 if (unlikely(phys_mem + vec.bv_len > phys_end)) {
Al Virod81fec02007-10-12 07:00:19 +0100123 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700124 return BLK_QC_T_NONE;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200125 }
126
Kent Overstreet79886132013-11-23 17:19:00 -0800127 user_mem = page_address(vec.bv_page) + vec.bv_offset;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200128 if (bio_data_dir(bio) == READ)
Kent Overstreet79886132013-11-23 17:19:00 -0800129 memcpy(user_mem, (void *) phys_mem, vec.bv_len);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200130 else
Kent Overstreet79886132013-11-23 17:19:00 -0800131 memcpy((void *) phys_mem, user_mem, vec.bv_len);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200132
Kent Overstreet79886132013-11-23 17:19:00 -0800133 phys_mem += vec.bv_len;
134 transfered += vec.bv_len;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200135 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200136 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700137 return BLK_QC_T_NONE;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200138}
139
140/**
141 * axon_ram_direct_access - direct_access() method for block device
142 * @device, @sector, @data: see block_device_operations method
143 */
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200144static long
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200145axon_ram_direct_access(struct block_device *device, sector_t sector,
Dan Williams0a70bd42016-02-24 14:02:11 -0800146 void __pmem **kaddr, pfn_t *pfn, long size)
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200147{
148 struct axon_ram_bank *bank = device->bd_disk->private_data;
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200149 loff_t offset = (loff_t)sector << AXON_RAM_SECTOR_SHIFT;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200150
Dan Williams34c0fd52016-01-15 16:56:14 -0800151 *kaddr = (void __pmem __force *) bank->io_addr + offset;
152 *pfn = phys_to_pfn_t(bank->ph_addr + offset, PFN_DEV);
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200153 return bank->size - offset;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200154}
155
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700156static const struct block_device_operations axon_ram_devops = {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200157 .owner = THIS_MODULE,
158 .direct_access = axon_ram_direct_access
159};
160
161/**
162 * axon_ram_probe - probe() method for platform driver
Grant Likely00006122011-02-22 19:59:54 -0700163 * @device: see platform_driver method
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200164 */
Grant Likely00006122011-02-22 19:59:54 -0700165static int axon_ram_probe(struct platform_device *device)
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200166{
167 static int axon_ram_bank_id = -1;
168 struct axon_ram_bank *bank;
169 struct resource resource;
170 int rc = 0;
171
172 axon_ram_bank_id++;
173
174 dev_info(&device->dev, "Found memory controller on %s\n",
Grant Likely61c7a082010-04-13 16:12:29 -0700175 device->dev.of_node->full_name);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200176
177 bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL);
178 if (bank == NULL) {
179 dev_err(&device->dev, "Out of memory\n");
180 rc = -ENOMEM;
181 goto failed;
182 }
183
184 device->dev.platform_data = bank;
185
186 bank->device = device;
187
Grant Likely61c7a082010-04-13 16:12:29 -0700188 if (of_address_to_resource(device->dev.of_node, 0, &resource) != 0) {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200189 dev_err(&device->dev, "Cannot access device tree\n");
190 rc = -EFAULT;
191 goto failed;
192 }
193
Joe Perches28f65c112011-06-09 09:13:32 -0700194 bank->size = resource_size(&resource);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200195
196 if (bank->size == 0) {
197 dev_err(&device->dev, "No DDR2 memory found for %s%d\n",
198 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
199 rc = -ENODEV;
200 goto failed;
201 }
202
203 dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n",
204 AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20);
205
206 bank->ph_addr = resource.start;
Anton Blanchard40f1ce72011-05-08 21:43:47 +0000207 bank->io_addr = (unsigned long) ioremap_prot(
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200208 bank->ph_addr, bank->size, _PAGE_NO_CACHE);
209 if (bank->io_addr == 0) {
210 dev_err(&device->dev, "ioremap() failed\n");
211 rc = -EFAULT;
212 goto failed;
213 }
214
215 bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK);
216 if (bank->disk == NULL) {
217 dev_err(&device->dev, "Cannot register disk\n");
218 rc = -EFAULT;
219 goto failed;
220 }
221
Maxim Shchetynin9a234092008-07-16 05:51:41 +1000222 bank->disk->major = azfs_major;
223 bank->disk->first_minor = azfs_minor;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200224 bank->disk->fops = &axon_ram_devops;
225 bank->disk->private_data = bank;
226 bank->disk->driverfs_dev = &device->dev;
227
228 sprintf(bank->disk->disk_name, "%s%d",
229 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200230
231 bank->disk->queue = blk_alloc_queue(GFP_KERNEL);
232 if (bank->disk->queue == NULL) {
233 dev_err(&device->dev, "Cannot register disk queue\n");
234 rc = -EFAULT;
235 goto failed;
236 }
237
238 set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT);
239 blk_queue_make_request(bank->disk->queue, axon_ram_make_request);
Martin K. Petersene1defc42009-05-22 17:17:49 -0400240 blk_queue_logical_block_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200241 add_disk(bank->disk);
242
Grant Likely61c7a082010-04-13 16:12:29 -0700243 bank->irq_id = irq_of_parse_and_map(device->dev.of_node, 0);
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000244 if (bank->irq_id == NO_IRQ) {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200245 dev_err(&device->dev, "Cannot access ECC interrupt ID\n");
246 rc = -EFAULT;
247 goto failed;
248 }
249
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000250 rc = request_irq(bank->irq_id, axon_ram_irq_handler,
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200251 AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device);
252 if (rc != 0) {
253 dev_err(&device->dev, "Cannot register ECC interrupt handler\n");
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000254 bank->irq_id = NO_IRQ;
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200255 rc = -EFAULT;
256 goto failed;
257 }
258
259 rc = device_create_file(&device->dev, &dev_attr_ecc);
260 if (rc != 0) {
261 dev_err(&device->dev, "Cannot create sysfs file\n");
262 rc = -EFAULT;
263 goto failed;
264 }
265
Maxim Shchetynin9a234092008-07-16 05:51:41 +1000266 azfs_minor += bank->disk->minors;
267
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200268 return 0;
269
270failed:
271 if (bank != NULL) {
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000272 if (bank->irq_id != NO_IRQ)
273 free_irq(bank->irq_id, device);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200274 if (bank->disk != NULL) {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200275 if (bank->disk->major > 0)
276 unregister_blkdev(bank->disk->major,
277 bank->disk->disk_name);
278 del_gendisk(bank->disk);
279 }
280 device->dev.platform_data = NULL;
281 if (bank->io_addr != 0)
282 iounmap((void __iomem *) bank->io_addr);
283 kfree(bank);
284 }
285
286 return rc;
287}
288
289/**
290 * axon_ram_remove - remove() method for platform driver
291 * @device: see of_platform_driver method
292 */
293static int
Grant Likelya454dc52010-07-22 15:52:34 -0600294axon_ram_remove(struct platform_device *device)
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200295{
296 struct axon_ram_bank *bank = device->dev.platform_data;
297
298 BUG_ON(!bank || !bank->disk);
299
300 device_remove_file(&device->dev, &dev_attr_ecc);
Maxim Shchetyninfedcd2c2007-08-23 03:01:27 +1000301 free_irq(bank->irq_id, device);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200302 del_gendisk(bank->disk);
303 iounmap((void __iomem *) bank->io_addr);
304 kfree(bank);
305
306 return 0;
307}
308
Uwe Kleine-Königce6d73c2014-09-10 21:56:38 +0200309static const struct of_device_id axon_ram_device_id[] = {
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200310 {
311 .type = "dma-memory"
312 },
313 {}
314};
Luis de Bethencourtb4f81442015-10-20 16:04:13 +0100315MODULE_DEVICE_TABLE(of, axon_ram_device_id);
Maxim Shchetynindbdf04c2007-07-20 21:39:26 +0200316
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");