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> 45#include <linux/of_device.h> 46#include <linux/of_platform.h> 47 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 60static int azfs_major, azfs_minor; 61 62struct axon_ram_bank { 63 struct platform_device *device; 64 struct gendisk *disk; 65 unsigned int irq_id; 66 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{ 75 struct platform_device *device = to_platform_device(dev); 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 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{ 93 struct platform_device *device = dev; 94 struct axon_ram_bank *bank = device->dev.platform_data; 95 96 BUG_ON(!bank); 97 98 dev_err(&device->dev, "Correctable memory error occured\n"); 99 bank->ecc_counter++; 100 return IRQ_HANDLED; 101} 102 103/** 104 * axon_ram_make_request - make_request() method for block device 105 * @queue, @bio: see blk_queue_make_request() 106 */ 107static int 108axon_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)) { 123 bio_io_error(bio); 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 } 137 bio_endio(bio, 0); 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 */ 146static int 147axon_ram_direct_access(struct block_device *device, sector_t sector, 148 void **kaddr, unsigned long *pfn) 149{ 150 struct axon_ram_bank *bank = device->bd_disk->private_data; 151 loff_t offset; 152 153 offset = sector; 154 if (device->bd_part != NULL) 155 offset += device->bd_part->start_sect; 156 offset <<= AXON_RAM_SECTOR_SHIFT; 157 if (offset >= bank->size) { 158 dev_err(&bank->device->dev, "Access outside of address space\n"); 159 return -ERANGE; 160 } 161 162 *kaddr = (void *)(bank->ph_addr + offset); 163 *pfn = virt_to_phys(kaddr) >> PAGE_SHIFT; 164 165 return 0; 166} 167 168static const struct block_device_operations axon_ram_devops = { 169 .owner = THIS_MODULE, 170 .direct_access = axon_ram_direct_access 171}; 172 173/** 174 * axon_ram_probe - probe() method for platform driver 175 * @device, @device_id: see of_platform_driver method 176 */ 177static int axon_ram_probe(struct platform_device *device, 178 const struct of_device_id *device_id) 179{ 180 static int axon_ram_bank_id = -1; 181 struct axon_ram_bank *bank; 182 struct resource resource; 183 int rc = 0; 184 185 axon_ram_bank_id++; 186 187 dev_info(&device->dev, "Found memory controller on %s\n", 188 device->dev.of_node->full_name); 189 190 bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL); 191 if (bank == NULL) { 192 dev_err(&device->dev, "Out of memory\n"); 193 rc = -ENOMEM; 194 goto failed; 195 } 196 197 device->dev.platform_data = bank; 198 199 bank->device = device; 200 201 if (of_address_to_resource(device->dev.of_node, 0, &resource) != 0) { 202 dev_err(&device->dev, "Cannot access device tree\n"); 203 rc = -EFAULT; 204 goto failed; 205 } 206 207 bank->size = resource.end - resource.start + 1; 208 209 if (bank->size == 0) { 210 dev_err(&device->dev, "No DDR2 memory found for %s%d\n", 211 AXON_RAM_DEVICE_NAME, axon_ram_bank_id); 212 rc = -ENODEV; 213 goto failed; 214 } 215 216 dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n", 217 AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20); 218 219 bank->ph_addr = resource.start; 220 bank->io_addr = (unsigned long) ioremap_flags( 221 bank->ph_addr, bank->size, _PAGE_NO_CACHE); 222 if (bank->io_addr == 0) { 223 dev_err(&device->dev, "ioremap() failed\n"); 224 rc = -EFAULT; 225 goto failed; 226 } 227 228 bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK); 229 if (bank->disk == NULL) { 230 dev_err(&device->dev, "Cannot register disk\n"); 231 rc = -EFAULT; 232 goto failed; 233 } 234 235 bank->disk->major = azfs_major; 236 bank->disk->first_minor = azfs_minor; 237 bank->disk->fops = &axon_ram_devops; 238 bank->disk->private_data = bank; 239 bank->disk->driverfs_dev = &device->dev; 240 241 sprintf(bank->disk->disk_name, "%s%d", 242 AXON_RAM_DEVICE_NAME, axon_ram_bank_id); 243 244 bank->disk->queue = blk_alloc_queue(GFP_KERNEL); 245 if (bank->disk->queue == NULL) { 246 dev_err(&device->dev, "Cannot register disk queue\n"); 247 rc = -EFAULT; 248 goto failed; 249 } 250 251 set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT); 252 blk_queue_make_request(bank->disk->queue, axon_ram_make_request); 253 blk_queue_logical_block_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE); 254 add_disk(bank->disk); 255 256 bank->irq_id = irq_of_parse_and_map(device->dev.of_node, 0); 257 if (bank->irq_id == NO_IRQ) { 258 dev_err(&device->dev, "Cannot access ECC interrupt ID\n"); 259 rc = -EFAULT; 260 goto failed; 261 } 262 263 rc = request_irq(bank->irq_id, axon_ram_irq_handler, 264 AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device); 265 if (rc != 0) { 266 dev_err(&device->dev, "Cannot register ECC interrupt handler\n"); 267 bank->irq_id = NO_IRQ; 268 rc = -EFAULT; 269 goto failed; 270 } 271 272 rc = device_create_file(&device->dev, &dev_attr_ecc); 273 if (rc != 0) { 274 dev_err(&device->dev, "Cannot create sysfs file\n"); 275 rc = -EFAULT; 276 goto failed; 277 } 278 279 azfs_minor += bank->disk->minors; 280 281 return 0; 282 283failed: 284 if (bank != NULL) { 285 if (bank->irq_id != NO_IRQ) 286 free_irq(bank->irq_id, device); 287 if (bank->disk != NULL) { 288 if (bank->disk->major > 0) 289 unregister_blkdev(bank->disk->major, 290 bank->disk->disk_name); 291 del_gendisk(bank->disk); 292 } 293 device->dev.platform_data = NULL; 294 if (bank->io_addr != 0) 295 iounmap((void __iomem *) bank->io_addr); 296 kfree(bank); 297 } 298 299 return rc; 300} 301 302/** 303 * axon_ram_remove - remove() method for platform driver 304 * @device: see of_platform_driver method 305 */ 306static int 307axon_ram_remove(struct platform_device *device) 308{ 309 struct axon_ram_bank *bank = device->dev.platform_data; 310 311 BUG_ON(!bank || !bank->disk); 312 313 device_remove_file(&device->dev, &dev_attr_ecc); 314 free_irq(bank->irq_id, device); 315 del_gendisk(bank->disk); 316 iounmap((void __iomem *) bank->io_addr); 317 kfree(bank); 318 319 return 0; 320} 321 322static struct of_device_id axon_ram_device_id[] = { 323 { 324 .type = "dma-memory" 325 }, 326 {} 327}; 328 329static struct of_platform_driver axon_ram_driver = { 330 .probe = axon_ram_probe, 331 .remove = axon_ram_remove, 332 .driver = { 333 .name = AXON_RAM_MODULE_NAME, 334 .owner = THIS_MODULE, 335 .of_match_table = axon_ram_device_id, 336 }, 337}; 338 339/** 340 * axon_ram_init 341 */ 342static int __init 343axon_ram_init(void) 344{ 345 azfs_major = register_blkdev(azfs_major, AXON_RAM_DEVICE_NAME); 346 if (azfs_major < 0) { 347 printk(KERN_ERR "%s cannot become block device major number\n", 348 AXON_RAM_MODULE_NAME); 349 return -EFAULT; 350 } 351 azfs_minor = 0; 352 353 return of_register_platform_driver(&axon_ram_driver); 354} 355 356/** 357 * axon_ram_exit 358 */ 359static void __exit 360axon_ram_exit(void) 361{ 362 of_unregister_platform_driver(&axon_ram_driver); 363 unregister_blkdev(azfs_major, AXON_RAM_DEVICE_NAME); 364} 365 366module_init(axon_ram_init); 367module_exit(axon_ram_exit); 368 369MODULE_LICENSE("GPL"); 370MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>"); 371MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE"); 372