Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 1 | /* |
| 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> |
Jon Loeliger | eb8f276 | 2007-11-14 04:13:09 +1100 | [diff] [blame] | 45 | #include <linux/of_device.h> |
| 46 | #include <linux/of_platform.h> |
| 47 | |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 48 | #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 Shchetynin | 9a23409 | 2008-07-16 05:51:41 +1000 | [diff] [blame] | 60 | static int azfs_major, azfs_minor; |
| 61 | |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 62 | struct axon_ram_bank { |
Grant Likely | a454dc5 | 2010-07-22 15:52:34 -0600 | [diff] [blame] | 63 | struct platform_device *device; |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 64 | struct gendisk *disk; |
Maxim Shchetynin | fedcd2c | 2007-08-23 03:01:27 +1000 | [diff] [blame] | 65 | unsigned int irq_id; |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 66 | unsigned long ph_addr; |
| 67 | unsigned long io_addr; |
| 68 | unsigned long size; |
| 69 | unsigned long ecc_counter; |
| 70 | }; |
| 71 | |
| 72 | static ssize_t |
| 73 | axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf) |
| 74 | { |
Grant Likely | a454dc5 | 2010-07-22 15:52:34 -0600 | [diff] [blame] | 75 | struct platform_device *device = to_platform_device(dev); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 76 | 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 | |
| 83 | static 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 | */ |
| 90 | static irqreturn_t |
| 91 | axon_ram_irq_handler(int irq, void *dev) |
| 92 | { |
Grant Likely | a454dc5 | 2010-07-22 15:52:34 -0600 | [diff] [blame] | 93 | struct platform_device *device = dev; |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 94 | struct axon_ram_bank *bank = device->dev.platform_data; |
| 95 | |
| 96 | BUG_ON(!bank); |
| 97 | |
Maxim Shchetynin | fedcd2c | 2007-08-23 03:01:27 +1000 | [diff] [blame] | 98 | dev_err(&device->dev, "Correctable memory error occured\n"); |
| 99 | bank->ecc_counter++; |
| 100 | return IRQ_HANDLED; |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * axon_ram_make_request - make_request() method for block device |
| 105 | * @queue, @bio: see blk_queue_make_request() |
| 106 | */ |
| 107 | static int |
| 108 | axon_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; |
| 113 | struct bio_vec *vec; |
| 114 | unsigned int transfered; |
| 115 | unsigned short idx; |
| 116 | int rc = 0; |
| 117 | |
| 118 | phys_mem = bank->io_addr + (bio->bi_sector << AXON_RAM_SECTOR_SHIFT); |
| 119 | phys_end = bank->io_addr + bank->size; |
| 120 | transfered = 0; |
| 121 | bio_for_each_segment(vec, bio, idx) { |
| 122 | if (unlikely(phys_mem + vec->bv_len > phys_end)) { |
Al Viro | d81fec0 | 2007-10-12 07:00:19 +0100 | [diff] [blame] | 123 | bio_io_error(bio); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 124 | rc = -ERANGE; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | user_mem = page_address(vec->bv_page) + vec->bv_offset; |
| 129 | if (bio_data_dir(bio) == READ) |
| 130 | memcpy(user_mem, (void *) phys_mem, vec->bv_len); |
| 131 | else |
| 132 | memcpy((void *) phys_mem, user_mem, vec->bv_len); |
| 133 | |
| 134 | phys_mem += vec->bv_len; |
| 135 | transfered += vec->bv_len; |
| 136 | } |
Al Viro | d81fec0 | 2007-10-12 07:00:19 +0100 | [diff] [blame] | 137 | bio_endio(bio, 0); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 138 | |
| 139 | return rc; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * axon_ram_direct_access - direct_access() method for block device |
| 144 | * @device, @sector, @data: see block_device_operations method |
| 145 | */ |
| 146 | static int |
| 147 | axon_ram_direct_access(struct block_device *device, sector_t sector, |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame] | 148 | void **kaddr, unsigned long *pfn) |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 149 | { |
| 150 | struct axon_ram_bank *bank = device->bd_disk->private_data; |
| 151 | loff_t offset; |
| 152 | |
Maxim Shchetynin | 8204cba | 2008-07-16 05:51:42 +1000 | [diff] [blame] | 153 | offset = sector; |
| 154 | if (device->bd_part != NULL) |
| 155 | offset += device->bd_part->start_sect; |
| 156 | offset <<= AXON_RAM_SECTOR_SHIFT; |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 157 | if (offset >= bank->size) { |
| 158 | dev_err(&bank->device->dev, "Access outside of address space\n"); |
| 159 | return -ERANGE; |
| 160 | } |
| 161 | |
Jared Hulbert | 30afcb4 | 2008-04-28 02:13:02 -0700 | [diff] [blame] | 162 | *kaddr = (void *)(bank->ph_addr + offset); |
| 163 | *pfn = virt_to_phys(kaddr) >> PAGE_SHIFT; |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 168 | static const struct block_device_operations axon_ram_devops = { |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 169 | .owner = THIS_MODULE, |
| 170 | .direct_access = axon_ram_direct_access |
| 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * axon_ram_probe - probe() method for platform driver |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame^] | 175 | * @device: see platform_driver method |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 176 | */ |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame^] | 177 | static int axon_ram_probe(struct platform_device *device) |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 178 | { |
| 179 | static int axon_ram_bank_id = -1; |
| 180 | struct axon_ram_bank *bank; |
| 181 | struct resource resource; |
| 182 | int rc = 0; |
| 183 | |
| 184 | axon_ram_bank_id++; |
| 185 | |
| 186 | dev_info(&device->dev, "Found memory controller on %s\n", |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 187 | device->dev.of_node->full_name); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 188 | |
| 189 | bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL); |
| 190 | if (bank == NULL) { |
| 191 | dev_err(&device->dev, "Out of memory\n"); |
| 192 | rc = -ENOMEM; |
| 193 | goto failed; |
| 194 | } |
| 195 | |
| 196 | device->dev.platform_data = bank; |
| 197 | |
| 198 | bank->device = device; |
| 199 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 200 | if (of_address_to_resource(device->dev.of_node, 0, &resource) != 0) { |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 201 | dev_err(&device->dev, "Cannot access device tree\n"); |
| 202 | rc = -EFAULT; |
| 203 | goto failed; |
| 204 | } |
| 205 | |
| 206 | bank->size = resource.end - resource.start + 1; |
| 207 | |
| 208 | if (bank->size == 0) { |
| 209 | dev_err(&device->dev, "No DDR2 memory found for %s%d\n", |
| 210 | AXON_RAM_DEVICE_NAME, axon_ram_bank_id); |
| 211 | rc = -ENODEV; |
| 212 | goto failed; |
| 213 | } |
| 214 | |
| 215 | dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n", |
| 216 | AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20); |
| 217 | |
| 218 | bank->ph_addr = resource.start; |
| 219 | bank->io_addr = (unsigned long) ioremap_flags( |
| 220 | bank->ph_addr, bank->size, _PAGE_NO_CACHE); |
| 221 | if (bank->io_addr == 0) { |
| 222 | dev_err(&device->dev, "ioremap() failed\n"); |
| 223 | rc = -EFAULT; |
| 224 | goto failed; |
| 225 | } |
| 226 | |
| 227 | bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK); |
| 228 | if (bank->disk == NULL) { |
| 229 | dev_err(&device->dev, "Cannot register disk\n"); |
| 230 | rc = -EFAULT; |
| 231 | goto failed; |
| 232 | } |
| 233 | |
Maxim Shchetynin | 9a23409 | 2008-07-16 05:51:41 +1000 | [diff] [blame] | 234 | bank->disk->major = azfs_major; |
| 235 | bank->disk->first_minor = azfs_minor; |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 236 | bank->disk->fops = &axon_ram_devops; |
| 237 | bank->disk->private_data = bank; |
| 238 | bank->disk->driverfs_dev = &device->dev; |
| 239 | |
| 240 | sprintf(bank->disk->disk_name, "%s%d", |
| 241 | AXON_RAM_DEVICE_NAME, axon_ram_bank_id); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 242 | |
| 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; |
| 248 | } |
| 249 | |
| 250 | set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT); |
| 251 | blk_queue_make_request(bank->disk->queue, axon_ram_make_request); |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 252 | blk_queue_logical_block_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 253 | add_disk(bank->disk); |
| 254 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 255 | bank->irq_id = irq_of_parse_and_map(device->dev.of_node, 0); |
Maxim Shchetynin | fedcd2c | 2007-08-23 03:01:27 +1000 | [diff] [blame] | 256 | if (bank->irq_id == NO_IRQ) { |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 257 | dev_err(&device->dev, "Cannot access ECC interrupt ID\n"); |
| 258 | rc = -EFAULT; |
| 259 | goto failed; |
| 260 | } |
| 261 | |
Maxim Shchetynin | fedcd2c | 2007-08-23 03:01:27 +1000 | [diff] [blame] | 262 | rc = request_irq(bank->irq_id, axon_ram_irq_handler, |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 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"); |
Maxim Shchetynin | fedcd2c | 2007-08-23 03:01:27 +1000 | [diff] [blame] | 266 | bank->irq_id = NO_IRQ; |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 267 | rc = -EFAULT; |
| 268 | goto failed; |
| 269 | } |
| 270 | |
| 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; |
| 276 | } |
| 277 | |
Maxim Shchetynin | 9a23409 | 2008-07-16 05:51:41 +1000 | [diff] [blame] | 278 | azfs_minor += bank->disk->minors; |
| 279 | |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 280 | return 0; |
| 281 | |
| 282 | failed: |
| 283 | if (bank != NULL) { |
Maxim Shchetynin | fedcd2c | 2007-08-23 03:01:27 +1000 | [diff] [blame] | 284 | if (bank->irq_id != NO_IRQ) |
| 285 | free_irq(bank->irq_id, device); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 286 | if (bank->disk != NULL) { |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 287 | if (bank->disk->major > 0) |
| 288 | unregister_blkdev(bank->disk->major, |
| 289 | bank->disk->disk_name); |
| 290 | del_gendisk(bank->disk); |
| 291 | } |
| 292 | device->dev.platform_data = NULL; |
| 293 | if (bank->io_addr != 0) |
| 294 | iounmap((void __iomem *) bank->io_addr); |
| 295 | kfree(bank); |
| 296 | } |
| 297 | |
| 298 | return rc; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * axon_ram_remove - remove() method for platform driver |
| 303 | * @device: see of_platform_driver method |
| 304 | */ |
| 305 | static int |
Grant Likely | a454dc5 | 2010-07-22 15:52:34 -0600 | [diff] [blame] | 306 | axon_ram_remove(struct platform_device *device) |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 307 | { |
| 308 | struct axon_ram_bank *bank = device->dev.platform_data; |
| 309 | |
| 310 | BUG_ON(!bank || !bank->disk); |
| 311 | |
| 312 | device_remove_file(&device->dev, &dev_attr_ecc); |
Maxim Shchetynin | fedcd2c | 2007-08-23 03:01:27 +1000 | [diff] [blame] | 313 | free_irq(bank->irq_id, device); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 314 | del_gendisk(bank->disk); |
| 315 | iounmap((void __iomem *) bank->io_addr); |
| 316 | kfree(bank); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static struct of_device_id axon_ram_device_id[] = { |
| 322 | { |
| 323 | .type = "dma-memory" |
| 324 | }, |
| 325 | {} |
| 326 | }; |
| 327 | |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame^] | 328 | static struct platform_driver axon_ram_driver = { |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 329 | .probe = axon_ram_probe, |
Stephen Rothwell | 84dd467 | 2007-10-11 15:19:03 +1000 | [diff] [blame] | 330 | .remove = axon_ram_remove, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 331 | .driver = { |
| 332 | .name = AXON_RAM_MODULE_NAME, |
| 333 | .owner = THIS_MODULE, |
| 334 | .of_match_table = axon_ram_device_id, |
Stephen Rothwell | 84dd467 | 2007-10-11 15:19:03 +1000 | [diff] [blame] | 335 | }, |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | /** |
| 339 | * axon_ram_init |
| 340 | */ |
| 341 | static int __init |
| 342 | axon_ram_init(void) |
| 343 | { |
Maxim Shchetynin | 9a23409 | 2008-07-16 05:51:41 +1000 | [diff] [blame] | 344 | azfs_major = register_blkdev(azfs_major, AXON_RAM_DEVICE_NAME); |
| 345 | if (azfs_major < 0) { |
| 346 | printk(KERN_ERR "%s cannot become block device major number\n", |
| 347 | AXON_RAM_MODULE_NAME); |
| 348 | return -EFAULT; |
| 349 | } |
| 350 | azfs_minor = 0; |
| 351 | |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame^] | 352 | return platform_driver_register(&axon_ram_driver); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
| 356 | * axon_ram_exit |
| 357 | */ |
| 358 | static void __exit |
| 359 | axon_ram_exit(void) |
| 360 | { |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame^] | 361 | platform_driver_unregister(&axon_ram_driver); |
Maxim Shchetynin | 9a23409 | 2008-07-16 05:51:41 +1000 | [diff] [blame] | 362 | unregister_blkdev(azfs_major, AXON_RAM_DEVICE_NAME); |
Maxim Shchetynin | dbdf04c | 2007-07-20 21:39:26 +0200 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | module_init(axon_ram_init); |
| 366 | module_exit(axon_ram_exit); |
| 367 | |
| 368 | MODULE_LICENSE("GPL"); |
| 369 | MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>"); |
| 370 | MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE"); |