1 // SPDX-License-Identifier: GPL-2.0-only
3 * PS3 FLASH ROM Storage Driver
5 * Copyright (C) 2007 Sony Computer Entertainment Inc.
6 * Copyright 2007 Sony Corp.
10 #include <linux/miscdevice.h>
11 #include <linux/slab.h>
12 #include <linux/uaccess.h>
13 #include <linux/module.h>
15 #include <asm/lv1call.h>
16 #include <asm/ps3stor.h>
19 #define DEVICE_NAME "ps3flash"
21 #define FLASH_BLOCK_SIZE (256*1024)
24 struct ps3flash_private {
25 struct mutex mutex; /* Bounce buffer mutex */
27 int tag; /* Start sector of buffer, -1 if invalid */
31 static struct ps3_storage_device *ps3flash_dev;
33 static int ps3flash_read_write_sectors(struct ps3_storage_device *dev,
34 u64 start_sector, int write)
36 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
37 u64 res = ps3stor_read_write_sectors(dev, dev->bounce_lpar,
38 start_sector, priv->chunk_sectors,
41 dev_err(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
42 __LINE__, write ? "write" : "read", res);
48 static int ps3flash_writeback(struct ps3_storage_device *dev)
50 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
53 if (!priv->dirty || priv->tag < 0)
56 res = ps3flash_read_write_sectors(dev, priv->tag, 1);
64 static int ps3flash_fetch(struct ps3_storage_device *dev, u64 start_sector)
66 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
69 if (start_sector == priv->tag)
72 res = ps3flash_writeback(dev);
78 res = ps3flash_read_write_sectors(dev, start_sector, 0);
82 priv->tag = start_sector;
86 static loff_t ps3flash_llseek(struct file *file, loff_t offset, int origin)
88 struct ps3_storage_device *dev = ps3flash_dev;
89 return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE,
90 dev->regions[dev->region_idx].size*dev->blk_size);
93 static ssize_t ps3flash_read(char __user *userbuf, void *kernelbuf,
94 size_t count, loff_t *pos)
96 struct ps3_storage_device *dev = ps3flash_dev;
97 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
98 u64 size, sector, offset;
103 dev_dbg(&dev->sbd.core,
104 "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n",
105 __func__, __LINE__, count, *pos, userbuf, kernelbuf);
107 size = dev->regions[dev->region_idx].size*dev->blk_size;
108 if (*pos >= size || !count)
111 if (*pos + count > size) {
112 dev_dbg(&dev->sbd.core,
113 "%s:%u Truncating count from %zu to %llu\n", __func__,
114 __LINE__, count, size - *pos);
118 sector = *pos / dev->bounce_size * priv->chunk_sectors;
119 offset = *pos % dev->bounce_size;
123 n = min_t(u64, remaining, dev->bounce_size - offset);
124 src = dev->bounce_buf + offset;
126 mutex_lock(&priv->mutex);
128 res = ps3flash_fetch(dev, sector);
132 dev_dbg(&dev->sbd.core,
133 "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n",
134 __func__, __LINE__, n, src, userbuf, kernelbuf);
136 if (copy_to_user(userbuf, src, n)) {
143 memcpy(kernelbuf, src, n);
147 mutex_unlock(&priv->mutex);
151 sector += priv->chunk_sectors;
153 } while (remaining > 0);
158 mutex_unlock(&priv->mutex);
162 static ssize_t ps3flash_write(const char __user *userbuf,
163 const void *kernelbuf, size_t count, loff_t *pos)
165 struct ps3_storage_device *dev = ps3flash_dev;
166 struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
167 u64 size, sector, offset;
172 dev_dbg(&dev->sbd.core,
173 "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n",
174 __func__, __LINE__, count, *pos, userbuf, kernelbuf);
176 size = dev->regions[dev->region_idx].size*dev->blk_size;
177 if (*pos >= size || !count)
180 if (*pos + count > size) {
181 dev_dbg(&dev->sbd.core,
182 "%s:%u Truncating count from %zu to %llu\n", __func__,
183 __LINE__, count, size - *pos);
187 sector = *pos / dev->bounce_size * priv->chunk_sectors;
188 offset = *pos % dev->bounce_size;
192 n = min_t(u64, remaining, dev->bounce_size - offset);
193 dst = dev->bounce_buf + offset;
195 mutex_lock(&priv->mutex);
197 if (n != dev->bounce_size)
198 res = ps3flash_fetch(dev, sector);
199 else if (sector != priv->tag)
200 res = ps3flash_writeback(dev);
204 dev_dbg(&dev->sbd.core,
205 "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n",
206 __func__, __LINE__, n, userbuf, kernelbuf, dst);
208 if (copy_from_user(dst, userbuf, n)) {
215 memcpy(dst, kernelbuf, n);
222 mutex_unlock(&priv->mutex);
226 sector += priv->chunk_sectors;
228 } while (remaining > 0);
233 mutex_unlock(&priv->mutex);
237 static ssize_t ps3flash_user_read(struct file *file, char __user *buf,
238 size_t count, loff_t *pos)
240 return ps3flash_read(buf, NULL, count, pos);
243 static ssize_t ps3flash_user_write(struct file *file, const char __user *buf,
244 size_t count, loff_t *pos)
246 return ps3flash_write(buf, NULL, count, pos);
249 static ssize_t ps3flash_kernel_read(void *buf, size_t count, loff_t pos)
251 return ps3flash_read(NULL, buf, count, &pos);
254 static ssize_t ps3flash_kernel_write(const void *buf, size_t count,
260 res = ps3flash_write(NULL, buf, count, &pos);
264 /* Make kernel writes synchronous */
265 wb = ps3flash_writeback(ps3flash_dev);
272 static int ps3flash_flush(struct file *file, fl_owner_t id)
274 return ps3flash_writeback(ps3flash_dev);
277 static int ps3flash_fsync(struct file *file, loff_t start, loff_t end, int datasync)
279 struct inode *inode = file_inode(file);
282 err = ps3flash_writeback(ps3flash_dev);
287 static irqreturn_t ps3flash_interrupt(int irq, void *data)
289 struct ps3_storage_device *dev = data;
293 res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
296 dev_err(&dev->sbd.core,
297 "%s:%u: tag mismatch, got %llx, expected %llx\n",
298 __func__, __LINE__, tag, dev->tag);
301 dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
302 __func__, __LINE__, res, status);
304 dev->lv1_status = status;
305 complete(&dev->done);
310 static const struct file_operations ps3flash_fops = {
311 .owner = THIS_MODULE,
312 .llseek = ps3flash_llseek,
313 .read = ps3flash_user_read,
314 .write = ps3flash_user_write,
315 .flush = ps3flash_flush,
316 .fsync = ps3flash_fsync,
319 static const struct ps3_os_area_flash_ops ps3flash_kernel_ops = {
320 .read = ps3flash_kernel_read,
321 .write = ps3flash_kernel_write,
324 static struct miscdevice ps3flash_misc = {
325 .minor = MISC_DYNAMIC_MINOR,
327 .fops = &ps3flash_fops,
330 static int ps3flash_probe(struct ps3_system_bus_device *_dev)
332 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
333 struct ps3flash_private *priv;
337 tmp = dev->regions[dev->region_idx].start*dev->blk_size;
338 if (tmp % FLASH_BLOCK_SIZE) {
339 dev_err(&dev->sbd.core,
340 "%s:%u region start %lu is not aligned\n", __func__,
344 tmp = dev->regions[dev->region_idx].size*dev->blk_size;
345 if (tmp % FLASH_BLOCK_SIZE) {
346 dev_err(&dev->sbd.core,
347 "%s:%u region size %lu is not aligned\n", __func__,
352 /* use static buffer, kmalloc cannot allocate 256 KiB */
353 if (!ps3flash_bounce_buffer.address)
357 dev_err(&dev->sbd.core,
358 "Only one FLASH device is supported\n");
364 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
370 ps3_system_bus_set_drvdata(&dev->sbd, priv);
371 mutex_init(&priv->mutex);
374 dev->bounce_size = ps3flash_bounce_buffer.size;
375 dev->bounce_buf = ps3flash_bounce_buffer.address;
376 priv->chunk_sectors = dev->bounce_size / dev->blk_size;
378 error = ps3stor_setup(dev, ps3flash_interrupt);
382 ps3flash_misc.parent = &dev->sbd.core;
383 error = misc_register(&ps3flash_misc);
385 dev_err(&dev->sbd.core, "%s:%u: misc_register failed %d\n",
386 __func__, __LINE__, error);
390 dev_info(&dev->sbd.core, "%s:%u: registered misc device %d\n",
391 __func__, __LINE__, ps3flash_misc.minor);
393 ps3_os_area_flash_register(&ps3flash_kernel_ops);
397 ps3stor_teardown(dev);
400 ps3_system_bus_set_drvdata(&dev->sbd, NULL);
406 static void ps3flash_remove(struct ps3_system_bus_device *_dev)
408 struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
410 ps3_os_area_flash_register(NULL);
411 misc_deregister(&ps3flash_misc);
412 ps3stor_teardown(dev);
413 kfree(ps3_system_bus_get_drvdata(&dev->sbd));
414 ps3_system_bus_set_drvdata(&dev->sbd, NULL);
419 static struct ps3_system_bus_driver ps3flash = {
420 .match_id = PS3_MATCH_ID_STOR_FLASH,
421 .core.name = DEVICE_NAME,
422 .core.owner = THIS_MODULE,
423 .probe = ps3flash_probe,
424 .remove = ps3flash_remove,
425 .shutdown = ps3flash_remove,
429 static int __init ps3flash_init(void)
431 return ps3_system_bus_driver_register(&ps3flash);
434 static void __exit ps3flash_exit(void)
436 ps3_system_bus_driver_unregister(&ps3flash);
439 module_init(ps3flash_init);
440 module_exit(ps3flash_exit);
442 MODULE_LICENSE("GPL");
443 MODULE_DESCRIPTION("PS3 FLASH ROM Storage Driver");
444 MODULE_AUTHOR("Sony Corporation");
445 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_FLASH);