Linux 4.13.16
[linux/fpc-iii.git] / arch / powerpc / sysdev / axonram.c
blob2799706106c63a3f5e3bc377d10385f3149a065d
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/dax.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>
47 #include <linux/pfn_t.h>
48 #include <linux/uio.h>
50 #include <asm/page.h>
51 #include <asm/prom.h>
53 #define AXON_RAM_MODULE_NAME "axonram"
54 #define AXON_RAM_DEVICE_NAME "axonram"
55 #define AXON_RAM_MINORS_PER_DISK 16
56 #define AXON_RAM_BLOCK_SHIFT PAGE_SHIFT
57 #define AXON_RAM_BLOCK_SIZE 1 << AXON_RAM_BLOCK_SHIFT
58 #define AXON_RAM_SECTOR_SHIFT 9
59 #define AXON_RAM_SECTOR_SIZE 1 << AXON_RAM_SECTOR_SHIFT
60 #define AXON_RAM_IRQ_FLAGS IRQF_SHARED | IRQF_TRIGGER_RISING
62 static int azfs_major, azfs_minor;
64 struct axon_ram_bank {
65 struct platform_device *device;
66 struct gendisk *disk;
67 struct dax_device *dax_dev;
68 unsigned int irq_id;
69 unsigned long ph_addr;
70 unsigned long io_addr;
71 unsigned long size;
72 unsigned long ecc_counter;
75 static ssize_t
76 axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf)
78 struct platform_device *device = to_platform_device(dev);
79 struct axon_ram_bank *bank = device->dev.platform_data;
81 BUG_ON(!bank);
83 return sprintf(buf, "%ld\n", bank->ecc_counter);
86 static DEVICE_ATTR(ecc, S_IRUGO, axon_ram_sysfs_ecc, NULL);
88 /**
89 * axon_ram_irq_handler - interrupt handler for Axon RAM ECC
90 * @irq: interrupt ID
91 * @dev: pointer to of_device
93 static irqreturn_t
94 axon_ram_irq_handler(int irq, void *dev)
96 struct platform_device *device = dev;
97 struct axon_ram_bank *bank = device->dev.platform_data;
99 BUG_ON(!bank);
101 dev_err(&device->dev, "Correctable memory error occurred\n");
102 bank->ecc_counter++;
103 return IRQ_HANDLED;
107 * axon_ram_make_request - make_request() method for block device
108 * @queue, @bio: see blk_queue_make_request()
110 static blk_qc_t
111 axon_ram_make_request(struct request_queue *queue, struct bio *bio)
113 struct axon_ram_bank *bank = bio->bi_bdev->bd_disk->private_data;
114 unsigned long phys_mem, phys_end;
115 void *user_mem;
116 struct bio_vec vec;
117 unsigned int transfered;
118 struct bvec_iter iter;
120 phys_mem = bank->io_addr + (bio->bi_iter.bi_sector <<
121 AXON_RAM_SECTOR_SHIFT);
122 phys_end = bank->io_addr + bank->size;
123 transfered = 0;
124 bio_for_each_segment(vec, bio, iter) {
125 if (unlikely(phys_mem + vec.bv_len > phys_end)) {
126 bio_io_error(bio);
127 return BLK_QC_T_NONE;
130 user_mem = page_address(vec.bv_page) + vec.bv_offset;
131 if (bio_data_dir(bio) == READ)
132 memcpy(user_mem, (void *) phys_mem, vec.bv_len);
133 else
134 memcpy((void *) phys_mem, user_mem, vec.bv_len);
136 phys_mem += vec.bv_len;
137 transfered += vec.bv_len;
139 bio_endio(bio);
140 return BLK_QC_T_NONE;
143 static const struct block_device_operations axon_ram_devops = {
144 .owner = THIS_MODULE,
147 static long
148 __axon_ram_direct_access(struct axon_ram_bank *bank, pgoff_t pgoff, long nr_pages,
149 void **kaddr, pfn_t *pfn)
151 resource_size_t offset = pgoff * PAGE_SIZE;
153 *kaddr = (void *) bank->io_addr + offset;
154 *pfn = phys_to_pfn_t(bank->ph_addr + offset, PFN_DEV);
155 return (bank->size - offset) / PAGE_SIZE;
158 static long
159 axon_ram_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
160 void **kaddr, pfn_t *pfn)
162 struct axon_ram_bank *bank = dax_get_private(dax_dev);
164 return __axon_ram_direct_access(bank, pgoff, nr_pages, kaddr, pfn);
167 static size_t axon_ram_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff,
168 void *addr, size_t bytes, struct iov_iter *i)
170 return copy_from_iter(addr, bytes, i);
173 static const struct dax_operations axon_ram_dax_ops = {
174 .direct_access = axon_ram_dax_direct_access,
175 .copy_from_iter = axon_ram_copy_from_iter,
179 * axon_ram_probe - probe() method for platform driver
180 * @device: see platform_driver method
182 static int axon_ram_probe(struct platform_device *device)
184 static int axon_ram_bank_id = -1;
185 struct axon_ram_bank *bank;
186 struct resource resource;
187 int rc = 0;
189 axon_ram_bank_id++;
191 dev_info(&device->dev, "Found memory controller on %s\n",
192 device->dev.of_node->full_name);
194 bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL);
195 if (bank == NULL) {
196 dev_err(&device->dev, "Out of memory\n");
197 rc = -ENOMEM;
198 goto failed;
201 device->dev.platform_data = bank;
203 bank->device = device;
205 if (of_address_to_resource(device->dev.of_node, 0, &resource) != 0) {
206 dev_err(&device->dev, "Cannot access device tree\n");
207 rc = -EFAULT;
208 goto failed;
211 bank->size = resource_size(&resource);
213 if (bank->size == 0) {
214 dev_err(&device->dev, "No DDR2 memory found for %s%d\n",
215 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
216 rc = -ENODEV;
217 goto failed;
220 dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n",
221 AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20);
223 bank->ph_addr = resource.start;
224 bank->io_addr = (unsigned long) ioremap_prot(
225 bank->ph_addr, bank->size, _PAGE_NO_CACHE);
226 if (bank->io_addr == 0) {
227 dev_err(&device->dev, "ioremap() failed\n");
228 rc = -EFAULT;
229 goto failed;
232 bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK);
233 if (bank->disk == NULL) {
234 dev_err(&device->dev, "Cannot register disk\n");
235 rc = -EFAULT;
236 goto failed;
240 bank->disk->major = azfs_major;
241 bank->disk->first_minor = azfs_minor;
242 bank->disk->fops = &axon_ram_devops;
243 bank->disk->private_data = bank;
245 sprintf(bank->disk->disk_name, "%s%d",
246 AXON_RAM_DEVICE_NAME, axon_ram_bank_id);
248 bank->dax_dev = alloc_dax(bank, bank->disk->disk_name,
249 &axon_ram_dax_ops);
250 if (!bank->dax_dev) {
251 rc = -ENOMEM;
252 goto failed;
255 bank->disk->queue = blk_alloc_queue(GFP_KERNEL);
256 if (bank->disk->queue == NULL) {
257 dev_err(&device->dev, "Cannot register disk queue\n");
258 rc = -EFAULT;
259 goto failed;
262 set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT);
263 blk_queue_make_request(bank->disk->queue, axon_ram_make_request);
264 blk_queue_logical_block_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE);
265 device_add_disk(&device->dev, bank->disk);
267 bank->irq_id = irq_of_parse_and_map(device->dev.of_node, 0);
268 if (!bank->irq_id) {
269 dev_err(&device->dev, "Cannot access ECC interrupt ID\n");
270 rc = -EFAULT;
271 goto failed;
274 rc = request_irq(bank->irq_id, axon_ram_irq_handler,
275 AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device);
276 if (rc != 0) {
277 dev_err(&device->dev, "Cannot register ECC interrupt handler\n");
278 bank->irq_id = 0;
279 rc = -EFAULT;
280 goto failed;
283 rc = device_create_file(&device->dev, &dev_attr_ecc);
284 if (rc != 0) {
285 dev_err(&device->dev, "Cannot create sysfs file\n");
286 rc = -EFAULT;
287 goto failed;
290 azfs_minor += bank->disk->minors;
292 return 0;
294 failed:
295 if (bank != NULL) {
296 if (bank->irq_id)
297 free_irq(bank->irq_id, device);
298 if (bank->disk != NULL) {
299 if (bank->disk->major > 0)
300 unregister_blkdev(bank->disk->major,
301 bank->disk->disk_name);
302 if (bank->disk->flags & GENHD_FL_UP)
303 del_gendisk(bank->disk);
304 put_disk(bank->disk);
306 kill_dax(bank->dax_dev);
307 put_dax(bank->dax_dev);
308 device->dev.platform_data = NULL;
309 if (bank->io_addr != 0)
310 iounmap((void __iomem *) bank->io_addr);
311 kfree(bank);
314 return rc;
318 * axon_ram_remove - remove() method for platform driver
319 * @device: see of_platform_driver method
321 static int
322 axon_ram_remove(struct platform_device *device)
324 struct axon_ram_bank *bank = device->dev.platform_data;
326 BUG_ON(!bank || !bank->disk);
328 device_remove_file(&device->dev, &dev_attr_ecc);
329 free_irq(bank->irq_id, device);
330 kill_dax(bank->dax_dev);
331 put_dax(bank->dax_dev);
332 del_gendisk(bank->disk);
333 put_disk(bank->disk);
334 iounmap((void __iomem *) bank->io_addr);
335 kfree(bank);
337 return 0;
340 static const struct of_device_id axon_ram_device_id[] = {
342 .type = "dma-memory"
346 MODULE_DEVICE_TABLE(of, axon_ram_device_id);
348 static struct platform_driver axon_ram_driver = {
349 .probe = axon_ram_probe,
350 .remove = axon_ram_remove,
351 .driver = {
352 .name = AXON_RAM_MODULE_NAME,
353 .of_match_table = axon_ram_device_id,
358 * axon_ram_init
360 static int __init
361 axon_ram_init(void)
363 azfs_major = register_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
364 if (azfs_major < 0) {
365 printk(KERN_ERR "%s cannot become block device major number\n",
366 AXON_RAM_MODULE_NAME);
367 return -EFAULT;
369 azfs_minor = 0;
371 return platform_driver_register(&axon_ram_driver);
375 * axon_ram_exit
377 static void __exit
378 axon_ram_exit(void)
380 platform_driver_unregister(&axon_ram_driver);
381 unregister_blkdev(azfs_major, AXON_RAM_DEVICE_NAME);
384 module_init(axon_ram_init);
385 module_exit(axon_ram_exit);
387 MODULE_LICENSE("GPL");
388 MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>");
389 MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE");