2 * PS3 FLASH ROM Storage Driver
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2007 Sony Corp.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <linux/miscdevice.h>
23 #include <linux/uaccess.h>
25 #include <asm/lv1call.h>
26 #include <asm/ps3stor.h>
29 #define DEVICE_NAME "ps3flash"
31 #define FLASH_BLOCK_SIZE (256*1024)
34 struct ps3flash_private
{
35 struct mutex mutex
; /* Bounce buffer mutex */
37 int tag
; /* Start sector of buffer, -1 if invalid */
41 static struct ps3_storage_device
*ps3flash_dev
;
43 static int ps3flash_read_write_sectors(struct ps3_storage_device
*dev
,
44 u64 lpar
, u64 start_sector
, u64 sectors
,
47 u64 res
= ps3stor_read_write_sectors(dev
, lpar
, start_sector
, sectors
,
50 dev_err(&dev
->sbd
.core
, "%s:%u: %s failed 0x%llx\n", __func__
,
51 __LINE__
, write
? "write" : "read", res
);
57 static int ps3flash_writeback(struct ps3_storage_device
*dev
)
59 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
62 if (!priv
->dirty
|| priv
->tag
< 0)
65 res
= ps3flash_read_write_sectors(dev
, dev
->bounce_lpar
, priv
->tag
,
66 priv
->chunk_sectors
, 1);
74 static int ps3flash_fetch(struct ps3_storage_device
*dev
, u64 start_sector
,
77 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
78 unsigned int tag
, offset
;
82 offset
= start_sector
% priv
->chunk_sectors
;
83 tag
= start_sector
- offset
;
87 res
= ps3flash_writeback(dev
);
93 lpar
= dev
->bounce_lpar
+ offset
* dev
->blk_size
;
94 res
= ps3flash_read_write_sectors(dev
, lpar
, start_sector
, sectors
, 0);
98 /* We don't bother caching reads smaller than the chunk size */
99 if (sectors
== priv
->chunk_sectors
)
105 static loff_t
ps3flash_llseek(struct file
*file
, loff_t offset
, int origin
)
107 struct ps3_storage_device
*dev
= ps3flash_dev
;
110 mutex_lock(&file
->f_mapping
->host
->i_mutex
);
113 offset
+= file
->f_pos
;
116 offset
+= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
124 file
->f_pos
= offset
;
128 mutex_unlock(&file
->f_mapping
->host
->i_mutex
);
132 static ssize_t
ps3flash_read(char __user
*userbuf
, void *kernelbuf
,
133 size_t count
, loff_t
*pos
)
135 struct ps3_storage_device
*dev
= ps3flash_dev
;
136 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
137 u64 size
, start_sector
, end_sector
, offset
, sectors
;
142 dev_dbg(&dev
->sbd
.core
,
143 "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n",
144 __func__
, __LINE__
, count
, *pos
, userbuf
, kernelbuf
);
146 size
= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
147 if (*pos
>= size
|| !count
)
150 if (*pos
+ count
> size
) {
151 dev_dbg(&dev
->sbd
.core
,
152 "%s:%u Truncating count from %zu to %llu\n", __func__
,
153 __LINE__
, count
, size
- *pos
);
157 start_sector
= *pos
/ dev
->blk_size
;
158 offset
= *pos
% dev
->bounce_size
;
159 end_sector
= DIV_ROUND_UP(*pos
+ count
, dev
->blk_size
);
163 sectors
= min(end_sector
- start_sector
,
164 priv
->chunk_sectors
-
165 start_sector
% priv
->chunk_sectors
);
167 mutex_lock(&priv
->mutex
);
169 res
= ps3flash_fetch(dev
, start_sector
, sectors
);
173 n
= min_t(u64
, remaining
, dev
->bounce_size
- offset
);
174 src
= dev
->bounce_buf
+ offset
;
175 dev_dbg(&dev
->sbd
.core
,
176 "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n",
177 __func__
, __LINE__
, n
, src
, userbuf
, kernelbuf
);
179 if (copy_to_user(userbuf
, src
, n
)) {
186 memcpy(kernelbuf
, src
, n
);
190 mutex_unlock(&priv
->mutex
);
194 start_sector
+= sectors
;
196 } while (remaining
> 0);
201 mutex_unlock(&priv
->mutex
);
205 static ssize_t
ps3flash_write(const char __user
*userbuf
,
206 const void *kernelbuf
, size_t count
, loff_t
*pos
)
208 struct ps3_storage_device
*dev
= ps3flash_dev
;
209 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
210 u64 size
, sector
, offset
;
215 dev_dbg(&dev
->sbd
.core
,
216 "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n",
217 __func__
, __LINE__
, count
, *pos
, userbuf
, kernelbuf
);
219 size
= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
220 if (*pos
>= size
|| !count
)
223 if (*pos
+ count
> size
) {
224 dev_dbg(&dev
->sbd
.core
,
225 "%s:%u Truncating count from %zu to %llu\n", __func__
,
226 __LINE__
, count
, size
- *pos
);
230 sector
= *pos
/ dev
->bounce_size
* priv
->chunk_sectors
;
231 offset
= *pos
% dev
->bounce_size
;
235 n
= min_t(u64
, remaining
, dev
->bounce_size
- offset
);
237 mutex_lock(&priv
->mutex
);
239 if (n
!= dev
->bounce_size
)
240 res
= ps3flash_fetch(dev
, sector
, priv
->chunk_sectors
);
241 else if (sector
!= priv
->tag
)
242 res
= ps3flash_writeback(dev
);
246 dst
= dev
->bounce_buf
+ offset
;
247 dev_dbg(&dev
->sbd
.core
,
248 "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n",
249 __func__
, __LINE__
, n
, userbuf
, kernelbuf
, dst
);
251 if (copy_from_user(dst
, userbuf
, n
)) {
258 memcpy(dst
, kernelbuf
, n
);
265 mutex_unlock(&priv
->mutex
);
269 sector
+= priv
->chunk_sectors
;
271 } while (remaining
> 0);
276 mutex_unlock(&priv
->mutex
);
280 static ssize_t
ps3flash_user_read(struct file
*file
, char __user
*buf
,
281 size_t count
, loff_t
*pos
)
283 return ps3flash_read(buf
, NULL
, count
, pos
);
286 static ssize_t
ps3flash_user_write(struct file
*file
, const char __user
*buf
,
287 size_t count
, loff_t
*pos
)
289 return ps3flash_write(buf
, NULL
, count
, pos
);
292 static ssize_t
ps3flash_kernel_read(void *buf
, size_t count
, loff_t pos
)
294 return ps3flash_read(NULL
, buf
, count
, &pos
);
297 static ssize_t
ps3flash_kernel_write(const void *buf
, size_t count
,
303 res
= ps3flash_write(NULL
, buf
, count
, &pos
);
307 /* Make kernel writes synchronous */
308 wb
= ps3flash_writeback(ps3flash_dev
);
315 static int ps3flash_flush(struct file
*file
, fl_owner_t id
)
317 return ps3flash_writeback(ps3flash_dev
);
320 static int ps3flash_fsync(struct file
*file
, struct dentry
*dentry
,
323 return ps3flash_writeback(ps3flash_dev
);
326 static irqreturn_t
ps3flash_interrupt(int irq
, void *data
)
328 struct ps3_storage_device
*dev
= data
;
332 res
= lv1_storage_get_async_status(dev
->sbd
.dev_id
, &tag
, &status
);
335 dev_err(&dev
->sbd
.core
,
336 "%s:%u: tag mismatch, got %llx, expected %llx\n",
337 __func__
, __LINE__
, tag
, dev
->tag
);
340 dev_err(&dev
->sbd
.core
, "%s:%u: res=%d status=0x%llx\n",
341 __func__
, __LINE__
, res
, status
);
343 dev
->lv1_status
= status
;
344 complete(&dev
->done
);
349 static const struct file_operations ps3flash_fops
= {
350 .owner
= THIS_MODULE
,
351 .llseek
= ps3flash_llseek
,
352 .read
= ps3flash_user_read
,
353 .write
= ps3flash_user_write
,
354 .flush
= ps3flash_flush
,
355 .fsync
= ps3flash_fsync
,
358 static const struct ps3_os_area_flash_ops ps3flash_kernel_ops
= {
359 .read
= ps3flash_kernel_read
,
360 .write
= ps3flash_kernel_write
,
363 static struct miscdevice ps3flash_misc
= {
364 .minor
= MISC_DYNAMIC_MINOR
,
366 .fops
= &ps3flash_fops
,
369 static int __devinit
ps3flash_probe(struct ps3_system_bus_device
*_dev
)
371 struct ps3_storage_device
*dev
= to_ps3_storage_device(&_dev
->core
);
372 struct ps3flash_private
*priv
;
376 tmp
= dev
->regions
[dev
->region_idx
].start
*dev
->blk_size
;
377 if (tmp
% FLASH_BLOCK_SIZE
) {
378 dev_err(&dev
->sbd
.core
,
379 "%s:%u region start %lu is not aligned\n", __func__
,
383 tmp
= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
384 if (tmp
% FLASH_BLOCK_SIZE
) {
385 dev_err(&dev
->sbd
.core
,
386 "%s:%u region size %lu is not aligned\n", __func__
,
391 /* use static buffer, kmalloc cannot allocate 256 KiB */
392 if (!ps3flash_bounce_buffer
.address
)
396 dev_err(&dev
->sbd
.core
,
397 "Only one FLASH device is supported\n");
403 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
409 ps3_system_bus_set_drvdata(&dev
->sbd
, priv
);
410 mutex_init(&priv
->mutex
);
413 dev
->bounce_size
= ps3flash_bounce_buffer
.size
;
414 dev
->bounce_buf
= ps3flash_bounce_buffer
.address
;
415 priv
->chunk_sectors
= dev
->bounce_size
/ dev
->blk_size
;
417 error
= ps3stor_setup(dev
, ps3flash_interrupt
);
421 ps3flash_misc
.parent
= &dev
->sbd
.core
;
422 error
= misc_register(&ps3flash_misc
);
424 dev_err(&dev
->sbd
.core
, "%s:%u: misc_register failed %d\n",
425 __func__
, __LINE__
, error
);
429 dev_info(&dev
->sbd
.core
, "%s:%u: registered misc device %d\n",
430 __func__
, __LINE__
, ps3flash_misc
.minor
);
432 ps3_os_area_flash_register(&ps3flash_kernel_ops
);
436 ps3stor_teardown(dev
);
439 ps3_system_bus_set_drvdata(&dev
->sbd
, NULL
);
445 static int ps3flash_remove(struct ps3_system_bus_device
*_dev
)
447 struct ps3_storage_device
*dev
= to_ps3_storage_device(&_dev
->core
);
449 ps3_os_area_flash_register(NULL
);
450 misc_deregister(&ps3flash_misc
);
451 ps3stor_teardown(dev
);
452 kfree(ps3_system_bus_get_drvdata(&dev
->sbd
));
453 ps3_system_bus_set_drvdata(&dev
->sbd
, NULL
);
459 static struct ps3_system_bus_driver ps3flash
= {
460 .match_id
= PS3_MATCH_ID_STOR_FLASH
,
461 .core
.name
= DEVICE_NAME
,
462 .core
.owner
= THIS_MODULE
,
463 .probe
= ps3flash_probe
,
464 .remove
= ps3flash_remove
,
465 .shutdown
= ps3flash_remove
,
469 static int __init
ps3flash_init(void)
471 return ps3_system_bus_driver_register(&ps3flash
);
474 static void __exit
ps3flash_exit(void)
476 ps3_system_bus_driver_unregister(&ps3flash
);
479 module_init(ps3flash_init
);
480 module_exit(ps3flash_exit
);
482 MODULE_LICENSE("GPL");
483 MODULE_DESCRIPTION("PS3 FLASH ROM Storage Driver");
484 MODULE_AUTHOR("Sony Corporation");
485 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_FLASH
);