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/slab.h>
24 #include <linux/uaccess.h>
26 #include <asm/lv1call.h>
27 #include <asm/ps3stor.h>
30 #define DEVICE_NAME "ps3flash"
32 #define FLASH_BLOCK_SIZE (256*1024)
35 struct ps3flash_private
{
36 struct mutex mutex
; /* Bounce buffer mutex */
38 int tag
; /* Start sector of buffer, -1 if invalid */
42 static struct ps3_storage_device
*ps3flash_dev
;
44 static int ps3flash_read_write_sectors(struct ps3_storage_device
*dev
,
45 u64 start_sector
, int write
)
47 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
48 u64 res
= ps3stor_read_write_sectors(dev
, dev
->bounce_lpar
,
49 start_sector
, priv
->chunk_sectors
,
52 dev_err(&dev
->sbd
.core
, "%s:%u: %s failed 0x%llx\n", __func__
,
53 __LINE__
, write
? "write" : "read", res
);
59 static int ps3flash_writeback(struct ps3_storage_device
*dev
)
61 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
64 if (!priv
->dirty
|| priv
->tag
< 0)
67 res
= ps3flash_read_write_sectors(dev
, priv
->tag
, 1);
75 static int ps3flash_fetch(struct ps3_storage_device
*dev
, u64 start_sector
)
77 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
80 if (start_sector
== priv
->tag
)
83 res
= ps3flash_writeback(dev
);
89 res
= ps3flash_read_write_sectors(dev
, start_sector
, 0);
93 priv
->tag
= start_sector
;
97 static loff_t
ps3flash_llseek(struct file
*file
, loff_t offset
, int origin
)
99 struct ps3_storage_device
*dev
= ps3flash_dev
;
102 mutex_lock(&file
->f_mapping
->host
->i_mutex
);
107 offset
+= file
->f_pos
;
110 offset
+= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
120 file
->f_pos
= offset
;
124 mutex_unlock(&file
->f_mapping
->host
->i_mutex
);
128 static ssize_t
ps3flash_read(char __user
*userbuf
, void *kernelbuf
,
129 size_t count
, loff_t
*pos
)
131 struct ps3_storage_device
*dev
= ps3flash_dev
;
132 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
133 u64 size
, sector
, offset
;
138 dev_dbg(&dev
->sbd
.core
,
139 "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n",
140 __func__
, __LINE__
, count
, *pos
, userbuf
, kernelbuf
);
142 size
= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
143 if (*pos
>= size
|| !count
)
146 if (*pos
+ count
> size
) {
147 dev_dbg(&dev
->sbd
.core
,
148 "%s:%u Truncating count from %zu to %llu\n", __func__
,
149 __LINE__
, count
, size
- *pos
);
153 sector
= *pos
/ dev
->bounce_size
* priv
->chunk_sectors
;
154 offset
= *pos
% dev
->bounce_size
;
158 n
= min_t(u64
, remaining
, dev
->bounce_size
- offset
);
159 src
= dev
->bounce_buf
+ offset
;
161 mutex_lock(&priv
->mutex
);
163 res
= ps3flash_fetch(dev
, sector
);
167 dev_dbg(&dev
->sbd
.core
,
168 "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n",
169 __func__
, __LINE__
, n
, src
, userbuf
, kernelbuf
);
171 if (copy_to_user(userbuf
, src
, n
)) {
178 memcpy(kernelbuf
, src
, n
);
182 mutex_unlock(&priv
->mutex
);
186 sector
+= priv
->chunk_sectors
;
188 } while (remaining
> 0);
193 mutex_unlock(&priv
->mutex
);
197 static ssize_t
ps3flash_write(const char __user
*userbuf
,
198 const void *kernelbuf
, size_t count
, loff_t
*pos
)
200 struct ps3_storage_device
*dev
= ps3flash_dev
;
201 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
202 u64 size
, sector
, offset
;
207 dev_dbg(&dev
->sbd
.core
,
208 "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n",
209 __func__
, __LINE__
, count
, *pos
, userbuf
, kernelbuf
);
211 size
= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
212 if (*pos
>= size
|| !count
)
215 if (*pos
+ count
> size
) {
216 dev_dbg(&dev
->sbd
.core
,
217 "%s:%u Truncating count from %zu to %llu\n", __func__
,
218 __LINE__
, count
, size
- *pos
);
222 sector
= *pos
/ dev
->bounce_size
* priv
->chunk_sectors
;
223 offset
= *pos
% dev
->bounce_size
;
227 n
= min_t(u64
, remaining
, dev
->bounce_size
- offset
);
228 dst
= dev
->bounce_buf
+ offset
;
230 mutex_lock(&priv
->mutex
);
232 if (n
!= dev
->bounce_size
)
233 res
= ps3flash_fetch(dev
, sector
);
234 else if (sector
!= priv
->tag
)
235 res
= ps3flash_writeback(dev
);
239 dev_dbg(&dev
->sbd
.core
,
240 "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n",
241 __func__
, __LINE__
, n
, userbuf
, kernelbuf
, dst
);
243 if (copy_from_user(dst
, userbuf
, n
)) {
250 memcpy(dst
, kernelbuf
, n
);
257 mutex_unlock(&priv
->mutex
);
261 sector
+= priv
->chunk_sectors
;
263 } while (remaining
> 0);
268 mutex_unlock(&priv
->mutex
);
272 static ssize_t
ps3flash_user_read(struct file
*file
, char __user
*buf
,
273 size_t count
, loff_t
*pos
)
275 return ps3flash_read(buf
, NULL
, count
, pos
);
278 static ssize_t
ps3flash_user_write(struct file
*file
, const char __user
*buf
,
279 size_t count
, loff_t
*pos
)
281 return ps3flash_write(buf
, NULL
, count
, pos
);
284 static ssize_t
ps3flash_kernel_read(void *buf
, size_t count
, loff_t pos
)
286 return ps3flash_read(NULL
, buf
, count
, &pos
);
289 static ssize_t
ps3flash_kernel_write(const void *buf
, size_t count
,
295 res
= ps3flash_write(NULL
, buf
, count
, &pos
);
299 /* Make kernel writes synchronous */
300 wb
= ps3flash_writeback(ps3flash_dev
);
307 static int ps3flash_flush(struct file
*file
, fl_owner_t id
)
309 return ps3flash_writeback(ps3flash_dev
);
312 static int ps3flash_fsync(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
314 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
316 mutex_lock(&inode
->i_mutex
);
317 err
= ps3flash_writeback(ps3flash_dev
);
318 mutex_unlock(&inode
->i_mutex
);
322 static irqreturn_t
ps3flash_interrupt(int irq
, void *data
)
324 struct ps3_storage_device
*dev
= data
;
328 res
= lv1_storage_get_async_status(dev
->sbd
.dev_id
, &tag
, &status
);
331 dev_err(&dev
->sbd
.core
,
332 "%s:%u: tag mismatch, got %llx, expected %llx\n",
333 __func__
, __LINE__
, tag
, dev
->tag
);
336 dev_err(&dev
->sbd
.core
, "%s:%u: res=%d status=0x%llx\n",
337 __func__
, __LINE__
, res
, status
);
339 dev
->lv1_status
= status
;
340 complete(&dev
->done
);
345 static const struct file_operations ps3flash_fops
= {
346 .owner
= THIS_MODULE
,
347 .llseek
= ps3flash_llseek
,
348 .read
= ps3flash_user_read
,
349 .write
= ps3flash_user_write
,
350 .flush
= ps3flash_flush
,
351 .fsync
= ps3flash_fsync
,
354 static const struct ps3_os_area_flash_ops ps3flash_kernel_ops
= {
355 .read
= ps3flash_kernel_read
,
356 .write
= ps3flash_kernel_write
,
359 static struct miscdevice ps3flash_misc
= {
360 .minor
= MISC_DYNAMIC_MINOR
,
362 .fops
= &ps3flash_fops
,
365 static int __devinit
ps3flash_probe(struct ps3_system_bus_device
*_dev
)
367 struct ps3_storage_device
*dev
= to_ps3_storage_device(&_dev
->core
);
368 struct ps3flash_private
*priv
;
372 tmp
= dev
->regions
[dev
->region_idx
].start
*dev
->blk_size
;
373 if (tmp
% FLASH_BLOCK_SIZE
) {
374 dev_err(&dev
->sbd
.core
,
375 "%s:%u region start %lu is not aligned\n", __func__
,
379 tmp
= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
380 if (tmp
% FLASH_BLOCK_SIZE
) {
381 dev_err(&dev
->sbd
.core
,
382 "%s:%u region size %lu is not aligned\n", __func__
,
387 /* use static buffer, kmalloc cannot allocate 256 KiB */
388 if (!ps3flash_bounce_buffer
.address
)
392 dev_err(&dev
->sbd
.core
,
393 "Only one FLASH device is supported\n");
399 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
405 ps3_system_bus_set_drvdata(&dev
->sbd
, priv
);
406 mutex_init(&priv
->mutex
);
409 dev
->bounce_size
= ps3flash_bounce_buffer
.size
;
410 dev
->bounce_buf
= ps3flash_bounce_buffer
.address
;
411 priv
->chunk_sectors
= dev
->bounce_size
/ dev
->blk_size
;
413 error
= ps3stor_setup(dev
, ps3flash_interrupt
);
417 ps3flash_misc
.parent
= &dev
->sbd
.core
;
418 error
= misc_register(&ps3flash_misc
);
420 dev_err(&dev
->sbd
.core
, "%s:%u: misc_register failed %d\n",
421 __func__
, __LINE__
, error
);
425 dev_info(&dev
->sbd
.core
, "%s:%u: registered misc device %d\n",
426 __func__
, __LINE__
, ps3flash_misc
.minor
);
428 ps3_os_area_flash_register(&ps3flash_kernel_ops
);
432 ps3stor_teardown(dev
);
435 ps3_system_bus_set_drvdata(&dev
->sbd
, NULL
);
441 static int ps3flash_remove(struct ps3_system_bus_device
*_dev
)
443 struct ps3_storage_device
*dev
= to_ps3_storage_device(&_dev
->core
);
445 ps3_os_area_flash_register(NULL
);
446 misc_deregister(&ps3flash_misc
);
447 ps3stor_teardown(dev
);
448 kfree(ps3_system_bus_get_drvdata(&dev
->sbd
));
449 ps3_system_bus_set_drvdata(&dev
->sbd
, NULL
);
455 static struct ps3_system_bus_driver ps3flash
= {
456 .match_id
= PS3_MATCH_ID_STOR_FLASH
,
457 .core
.name
= DEVICE_NAME
,
458 .core
.owner
= THIS_MODULE
,
459 .probe
= ps3flash_probe
,
460 .remove
= ps3flash_remove
,
461 .shutdown
= ps3flash_remove
,
465 static int __init
ps3flash_init(void)
467 return ps3_system_bus_driver_register(&ps3flash
);
470 static void __exit
ps3flash_exit(void)
472 ps3_system_bus_driver_unregister(&ps3flash
);
475 module_init(ps3flash_init
);
476 module_exit(ps3flash_exit
);
478 MODULE_LICENSE("GPL");
479 MODULE_DESCRIPTION("PS3 FLASH ROM Storage Driver");
480 MODULE_AUTHOR("Sony Corporation");
481 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_FLASH
);