Commit | Line | Data |
---|---|---|
dbdf04c4 MS |
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> | |
dbdf04c4 MS |
28 | #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> | |
eb8f2763 JL |
44 | #include <linux/of_device.h> |
45 | #include <linux/of_platform.h> | |
46 | ||
dbdf04c4 MS |
47 | #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 | ||
9a23409b MS |
59 | static int azfs_major, azfs_minor; |
60 | ||
dbdf04c4 | 61 | struct axon_ram_bank { |
a454dc50 | 62 | struct platform_device *device; |
dbdf04c4 | 63 | struct gendisk *disk; |
fedcd2c5 | 64 | unsigned int irq_id; |
dbdf04c4 MS |
65 | unsigned long ph_addr; |
66 | unsigned long io_addr; | |
67 | unsigned long size; | |
68 | unsigned long ecc_counter; | |
69 | }; | |
70 | ||
71 | static ssize_t | |
72 | axon_ram_sysfs_ecc(struct device *dev, struct device_attribute *attr, char *buf) | |
73 | { | |
a454dc50 | 74 | struct platform_device *device = to_platform_device(dev); |
dbdf04c4 MS |
75 | 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 | ||
82 | static 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 | */ | |
89 | static irqreturn_t | |
90 | axon_ram_irq_handler(int irq, void *dev) | |
91 | { | |
a454dc50 | 92 | struct platform_device *device = dev; |
dbdf04c4 MS |
93 | struct axon_ram_bank *bank = device->dev.platform_data; |
94 | ||
95 | BUG_ON(!bank); | |
96 | ||
25985edc | 97 | dev_err(&device->dev, "Correctable memory error occurred\n"); |
fedcd2c5 MS |
98 | bank->ecc_counter++; |
99 | return IRQ_HANDLED; | |
dbdf04c4 MS |
100 | } |
101 | ||
102 | /** | |
103 | * axon_ram_make_request - make_request() method for block device | |
104 | * @queue, @bio: see blk_queue_make_request() | |
105 | */ | |
5a7bbad2 | 106 | static void |
dbdf04c4 MS |
107 | axon_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; | |
7988613b | 112 | struct bio_vec vec; |
dbdf04c4 | 113 | unsigned int transfered; |
7988613b | 114 | struct bvec_iter iter; |
dbdf04c4 | 115 | |
4f024f37 KO |
116 | phys_mem = bank->io_addr + (bio->bi_iter.bi_sector << |
117 | AXON_RAM_SECTOR_SHIFT); | |
dbdf04c4 MS |
118 | phys_end = bank->io_addr + bank->size; |
119 | transfered = 0; | |
7988613b KO |
120 | bio_for_each_segment(vec, bio, iter) { |
121 | if (unlikely(phys_mem + vec.bv_len > phys_end)) { | |
d81fec0f | 122 | bio_io_error(bio); |
5a7bbad2 | 123 | return; |
dbdf04c4 MS |
124 | } |
125 | ||
7988613b | 126 | user_mem = page_address(vec.bv_page) + vec.bv_offset; |
dbdf04c4 | 127 | if (bio_data_dir(bio) == READ) |
7988613b | 128 | memcpy(user_mem, (void *) phys_mem, vec.bv_len); |
dbdf04c4 | 129 | else |
7988613b | 130 | memcpy((void *) phys_mem, user_mem, vec.bv_len); |
dbdf04c4 | 131 | |
7988613b KO |
132 | phys_mem += vec.bv_len; |
133 | transfered += vec.bv_len; | |
dbdf04c4 | 134 | } |
d81fec0f | 135 | bio_endio(bio, 0); |
dbdf04c4 MS |
136 | } |
137 | ||
138 | /** | |
139 | * axon_ram_direct_access - direct_access() method for block device | |
140 | * @device, @sector, @data: see block_device_operations method | |
141 | */ | |
142 | static int | |
143 | axon_ram_direct_access(struct block_device *device, sector_t sector, | |
30afcb4b | 144 | void **kaddr, unsigned long *pfn) |
dbdf04c4 MS |
145 | { |
146 | struct axon_ram_bank *bank = device->bd_disk->private_data; | |
147 | loff_t offset; | |
148 | ||
8204cba7 MS |
149 | offset = sector; |
150 | if (device->bd_part != NULL) | |
151 | offset += device->bd_part->start_sect; | |
152 | offset <<= AXON_RAM_SECTOR_SHIFT; | |
dbdf04c4 MS |
153 | if (offset >= bank->size) { |
154 | dev_err(&bank->device->dev, "Access outside of address space\n"); | |
155 | return -ERANGE; | |
156 | } | |
157 | ||
30afcb4b JH |
158 | *kaddr = (void *)(bank->ph_addr + offset); |
159 | *pfn = virt_to_phys(kaddr) >> PAGE_SHIFT; | |
dbdf04c4 MS |
160 | |
161 | return 0; | |
162 | } | |
163 | ||
83d5cde4 | 164 | static const struct block_device_operations axon_ram_devops = { |
dbdf04c4 MS |
165 | .owner = THIS_MODULE, |
166 | .direct_access = axon_ram_direct_access | |
167 | }; | |
168 | ||
169 | /** | |
170 | * axon_ram_probe - probe() method for platform driver | |
00006124 | 171 | * @device: see platform_driver method |
dbdf04c4 | 172 | */ |
00006124 | 173 | static int axon_ram_probe(struct platform_device *device) |
dbdf04c4 MS |
174 | { |
175 | static int axon_ram_bank_id = -1; | |
176 | struct axon_ram_bank *bank; | |
177 | struct resource resource; | |
178 | int rc = 0; | |
179 | ||
180 | axon_ram_bank_id++; | |
181 | ||
182 | dev_info(&device->dev, "Found memory controller on %s\n", | |
61c7a080 | 183 | device->dev.of_node->full_name); |
dbdf04c4 MS |
184 | |
185 | bank = kzalloc(sizeof(struct axon_ram_bank), GFP_KERNEL); | |
186 | if (bank == NULL) { | |
187 | dev_err(&device->dev, "Out of memory\n"); | |
188 | rc = -ENOMEM; | |
189 | goto failed; | |
190 | } | |
191 | ||
192 | device->dev.platform_data = bank; | |
193 | ||
194 | bank->device = device; | |
195 | ||
61c7a080 | 196 | if (of_address_to_resource(device->dev.of_node, 0, &resource) != 0) { |
dbdf04c4 MS |
197 | dev_err(&device->dev, "Cannot access device tree\n"); |
198 | rc = -EFAULT; | |
199 | goto failed; | |
200 | } | |
201 | ||
28f65c11 | 202 | bank->size = resource_size(&resource); |
dbdf04c4 MS |
203 | |
204 | if (bank->size == 0) { | |
205 | dev_err(&device->dev, "No DDR2 memory found for %s%d\n", | |
206 | AXON_RAM_DEVICE_NAME, axon_ram_bank_id); | |
207 | rc = -ENODEV; | |
208 | goto failed; | |
209 | } | |
210 | ||
211 | dev_info(&device->dev, "Register DDR2 memory device %s%d with %luMB\n", | |
212 | AXON_RAM_DEVICE_NAME, axon_ram_bank_id, bank->size >> 20); | |
213 | ||
214 | bank->ph_addr = resource.start; | |
40f1ce7f | 215 | bank->io_addr = (unsigned long) ioremap_prot( |
dbdf04c4 MS |
216 | bank->ph_addr, bank->size, _PAGE_NO_CACHE); |
217 | if (bank->io_addr == 0) { | |
218 | dev_err(&device->dev, "ioremap() failed\n"); | |
219 | rc = -EFAULT; | |
220 | goto failed; | |
221 | } | |
222 | ||
223 | bank->disk = alloc_disk(AXON_RAM_MINORS_PER_DISK); | |
224 | if (bank->disk == NULL) { | |
225 | dev_err(&device->dev, "Cannot register disk\n"); | |
226 | rc = -EFAULT; | |
227 | goto failed; | |
228 | } | |
229 | ||
9a23409b MS |
230 | bank->disk->major = azfs_major; |
231 | bank->disk->first_minor = azfs_minor; | |
dbdf04c4 MS |
232 | bank->disk->fops = &axon_ram_devops; |
233 | bank->disk->private_data = bank; | |
234 | bank->disk->driverfs_dev = &device->dev; | |
235 | ||
236 | sprintf(bank->disk->disk_name, "%s%d", | |
237 | AXON_RAM_DEVICE_NAME, axon_ram_bank_id); | |
dbdf04c4 MS |
238 | |
239 | bank->disk->queue = blk_alloc_queue(GFP_KERNEL); | |
240 | if (bank->disk->queue == NULL) { | |
241 | dev_err(&device->dev, "Cannot register disk queue\n"); | |
242 | rc = -EFAULT; | |
243 | goto failed; | |
244 | } | |
245 | ||
246 | set_capacity(bank->disk, bank->size >> AXON_RAM_SECTOR_SHIFT); | |
247 | blk_queue_make_request(bank->disk->queue, axon_ram_make_request); | |
e1defc4f | 248 | blk_queue_logical_block_size(bank->disk->queue, AXON_RAM_SECTOR_SIZE); |
dbdf04c4 MS |
249 | add_disk(bank->disk); |
250 | ||
61c7a080 | 251 | bank->irq_id = irq_of_parse_and_map(device->dev.of_node, 0); |
fedcd2c5 | 252 | if (bank->irq_id == NO_IRQ) { |
dbdf04c4 MS |
253 | dev_err(&device->dev, "Cannot access ECC interrupt ID\n"); |
254 | rc = -EFAULT; | |
255 | goto failed; | |
256 | } | |
257 | ||
fedcd2c5 | 258 | rc = request_irq(bank->irq_id, axon_ram_irq_handler, |
dbdf04c4 MS |
259 | AXON_RAM_IRQ_FLAGS, bank->disk->disk_name, device); |
260 | if (rc != 0) { | |
261 | dev_err(&device->dev, "Cannot register ECC interrupt handler\n"); | |
fedcd2c5 | 262 | bank->irq_id = NO_IRQ; |
dbdf04c4 MS |
263 | rc = -EFAULT; |
264 | goto failed; | |
265 | } | |
266 | ||
267 | rc = device_create_file(&device->dev, &dev_attr_ecc); | |
268 | if (rc != 0) { | |
269 | dev_err(&device->dev, "Cannot create sysfs file\n"); | |
270 | rc = -EFAULT; | |
271 | goto failed; | |
272 | } | |
273 | ||
9a23409b MS |
274 | azfs_minor += bank->disk->minors; |
275 | ||
dbdf04c4 MS |
276 | return 0; |
277 | ||
278 | failed: | |
279 | if (bank != NULL) { | |
fedcd2c5 MS |
280 | if (bank->irq_id != NO_IRQ) |
281 | free_irq(bank->irq_id, device); | |
dbdf04c4 | 282 | if (bank->disk != NULL) { |
dbdf04c4 MS |
283 | if (bank->disk->major > 0) |
284 | unregister_blkdev(bank->disk->major, | |
285 | bank->disk->disk_name); | |
286 | del_gendisk(bank->disk); | |
287 | } | |
288 | device->dev.platform_data = NULL; | |
289 | if (bank->io_addr != 0) | |
290 | iounmap((void __iomem *) bank->io_addr); | |
291 | kfree(bank); | |
292 | } | |
293 | ||
294 | return rc; | |
295 | } | |
296 | ||
297 | /** | |
298 | * axon_ram_remove - remove() method for platform driver | |
299 | * @device: see of_platform_driver method | |
300 | */ | |
301 | static int | |
a454dc50 | 302 | axon_ram_remove(struct platform_device *device) |
dbdf04c4 MS |
303 | { |
304 | struct axon_ram_bank *bank = device->dev.platform_data; | |
305 | ||
306 | BUG_ON(!bank || !bank->disk); | |
307 | ||
308 | device_remove_file(&device->dev, &dev_attr_ecc); | |
fedcd2c5 | 309 | free_irq(bank->irq_id, device); |
dbdf04c4 MS |
310 | del_gendisk(bank->disk); |
311 | iounmap((void __iomem *) bank->io_addr); | |
312 | kfree(bank); | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
ce6d73c9 | 317 | static const struct of_device_id axon_ram_device_id[] = { |
dbdf04c4 MS |
318 | { |
319 | .type = "dma-memory" | |
320 | }, | |
321 | {} | |
322 | }; | |
323 | ||
00006124 | 324 | static struct platform_driver axon_ram_driver = { |
dbdf04c4 | 325 | .probe = axon_ram_probe, |
84dd4676 | 326 | .remove = axon_ram_remove, |
4018294b GL |
327 | .driver = { |
328 | .name = AXON_RAM_MODULE_NAME, | |
4018294b | 329 | .of_match_table = axon_ram_device_id, |
84dd4676 | 330 | }, |
dbdf04c4 MS |
331 | }; |
332 | ||
333 | /** | |
334 | * axon_ram_init | |
335 | */ | |
336 | static int __init | |
337 | axon_ram_init(void) | |
338 | { | |
9a23409b MS |
339 | azfs_major = register_blkdev(azfs_major, AXON_RAM_DEVICE_NAME); |
340 | if (azfs_major < 0) { | |
341 | printk(KERN_ERR "%s cannot become block device major number\n", | |
342 | AXON_RAM_MODULE_NAME); | |
343 | return -EFAULT; | |
344 | } | |
345 | azfs_minor = 0; | |
346 | ||
00006124 | 347 | return platform_driver_register(&axon_ram_driver); |
dbdf04c4 MS |
348 | } |
349 | ||
350 | /** | |
351 | * axon_ram_exit | |
352 | */ | |
353 | static void __exit | |
354 | axon_ram_exit(void) | |
355 | { | |
00006124 | 356 | platform_driver_unregister(&axon_ram_driver); |
9a23409b | 357 | unregister_blkdev(azfs_major, AXON_RAM_DEVICE_NAME); |
dbdf04c4 MS |
358 | } |
359 | ||
360 | module_init(axon_ram_init); | |
361 | module_exit(axon_ram_exit); | |
362 | ||
363 | MODULE_LICENSE("GPL"); | |
364 | MODULE_AUTHOR("Maxim Shchetynin <maxim@de.ibm.com>"); | |
365 | MODULE_DESCRIPTION("Axon DDR2 RAM device driver for IBM Cell BE"); |