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 */
38 static struct ps3_storage_device
*ps3flash_dev
;
40 static ssize_t
ps3flash_read_write_sectors(struct ps3_storage_device
*dev
,
41 u64 lpar
, u64 start_sector
,
42 u64 sectors
, int write
)
44 u64 res
= ps3stor_read_write_sectors(dev
, lpar
, start_sector
, sectors
,
47 dev_err(&dev
->sbd
.core
, "%s:%u: %s failed 0x%llx\n", __func__
,
48 __LINE__
, write
? "write" : "read", res
);
54 static ssize_t
ps3flash_read_sectors(struct ps3_storage_device
*dev
,
55 u64 start_sector
, u64 sectors
,
56 unsigned int sector_offset
)
58 u64 max_sectors
, lpar
;
60 max_sectors
= dev
->bounce_size
/ dev
->blk_size
;
61 if (sectors
> max_sectors
) {
62 dev_dbg(&dev
->sbd
.core
, "%s:%u Limiting sectors to %llu\n",
63 __func__
, __LINE__
, max_sectors
);
64 sectors
= max_sectors
;
67 lpar
= dev
->bounce_lpar
+ sector_offset
* dev
->blk_size
;
68 return ps3flash_read_write_sectors(dev
, lpar
, start_sector
, sectors
,
72 static ssize_t
ps3flash_write_chunk(struct ps3_storage_device
*dev
,
75 u64 sectors
= dev
->bounce_size
/ dev
->blk_size
;
76 return ps3flash_read_write_sectors(dev
, dev
->bounce_lpar
, start_sector
,
80 static loff_t
ps3flash_llseek(struct file
*file
, loff_t offset
, int origin
)
82 struct ps3_storage_device
*dev
= ps3flash_dev
;
85 mutex_lock(&file
->f_mapping
->host
->i_mutex
);
88 offset
+= file
->f_pos
;
91 offset
+= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
103 mutex_unlock(&file
->f_mapping
->host
->i_mutex
);
107 static ssize_t
ps3flash_read(char __user
*userbuf
, void *kernelbuf
,
108 size_t count
, loff_t
*pos
)
110 struct ps3_storage_device
*dev
= ps3flash_dev
;
111 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
112 u64 size
, start_sector
, end_sector
, offset
;
113 ssize_t sectors_read
;
117 dev_dbg(&dev
->sbd
.core
,
118 "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n",
119 __func__
, __LINE__
, count
, *pos
, userbuf
, kernelbuf
);
121 size
= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
122 if (*pos
>= size
|| !count
)
125 if (*pos
+ count
> size
) {
126 dev_dbg(&dev
->sbd
.core
,
127 "%s:%u Truncating count from %zu to %llu\n", __func__
,
128 __LINE__
, count
, size
- *pos
);
132 start_sector
= *pos
/ dev
->blk_size
;
133 offset
= *pos
% dev
->blk_size
;
134 end_sector
= DIV_ROUND_UP(*pos
+ count
, dev
->blk_size
);
138 mutex_lock(&priv
->mutex
);
140 sectors_read
= ps3flash_read_sectors(dev
, start_sector
,
141 end_sector
-start_sector
,
143 if (sectors_read
< 0) {
144 mutex_unlock(&priv
->mutex
);
148 n
= min_t(u64
, remaining
, sectors_read
*dev
->blk_size
-offset
);
149 src
= dev
->bounce_buf
+offset
;
150 dev_dbg(&dev
->sbd
.core
,
151 "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n",
152 __func__
, __LINE__
, n
, src
, userbuf
, kernelbuf
);
154 if (copy_to_user(userbuf
, src
, n
)) {
155 mutex_unlock(&priv
->mutex
);
156 sectors_read
= -EFAULT
;
162 memcpy(kernelbuf
, src
, n
);
166 mutex_unlock(&priv
->mutex
);
170 start_sector
+= sectors_read
;
172 } while (remaining
> 0);
180 static ssize_t
ps3flash_write(const char __user
*userbuf
,
181 const void *kernelbuf
, size_t count
, loff_t
*pos
)
183 struct ps3_storage_device
*dev
= ps3flash_dev
;
184 struct ps3flash_private
*priv
= ps3_system_bus_get_drvdata(&dev
->sbd
);
185 u64 size
, chunk_sectors
, start_write_sector
, end_write_sector
,
186 end_read_sector
, start_read_sector
, head
, tail
, offset
;
189 unsigned int sec_off
;
192 dev_dbg(&dev
->sbd
.core
,
193 "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n",
194 __func__
, __LINE__
, count
, *pos
, userbuf
, kernelbuf
);
196 size
= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
197 if (*pos
>= size
|| !count
)
200 if (*pos
+ count
> size
) {
201 dev_dbg(&dev
->sbd
.core
,
202 "%s:%u Truncating count from %zu to %llu\n", __func__
,
203 __LINE__
, count
, size
- *pos
);
207 chunk_sectors
= dev
->bounce_size
/ dev
->blk_size
;
209 start_write_sector
= *pos
/ dev
->bounce_size
* chunk_sectors
;
210 offset
= *pos
% dev
->bounce_size
;
211 end_write_sector
= DIV_ROUND_UP(*pos
+ count
, dev
->bounce_size
) *
214 end_read_sector
= DIV_ROUND_UP(*pos
, dev
->blk_size
);
215 start_read_sector
= (*pos
+ count
) / dev
->blk_size
;
218 * As we have to write in 256 KiB chunks, while we can read in blk_size
219 * (usually 512 bytes) chunks, we perform the following steps:
220 * 1. Read from start_write_sector to end_read_sector ("head")
221 * 2. Read from start_read_sector to end_write_sector ("tail")
222 * 3. Copy data to buffer
223 * 4. Write from start_write_sector to end_write_sector
224 * All of this is complicated by using only one 256 KiB bounce buffer.
227 head
= end_read_sector
- start_write_sector
;
228 tail
= end_write_sector
- start_read_sector
;
232 mutex_lock(&priv
->mutex
);
234 if (end_read_sector
>= start_read_sector
) {
235 /* Merge head and tail */
236 dev_dbg(&dev
->sbd
.core
,
237 "Merged head and tail: %llu sectors at %llu\n",
238 chunk_sectors
, start_write_sector
);
239 res
= ps3flash_read_sectors(dev
, start_write_sector
,
246 dev_dbg(&dev
->sbd
.core
,
247 "head: %llu sectors at %llu\n", head
,
249 res
= ps3flash_read_sectors(dev
,
255 if (start_read_sector
<
256 start_write_sector
+chunk_sectors
) {
258 dev_dbg(&dev
->sbd
.core
,
259 "tail: %llu sectors at %llu\n", tail
,
261 sec_off
= start_read_sector
-start_write_sector
;
262 res
= ps3flash_read_sectors(dev
,
270 n
= min_t(u64
, remaining
, dev
->bounce_size
-offset
);
271 dst
= dev
->bounce_buf
+offset
;
272 dev_dbg(&dev
->sbd
.core
,
273 "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n",
274 __func__
, __LINE__
, n
, userbuf
, kernelbuf
, dst
);
276 if (copy_from_user(dst
, userbuf
, n
)) {
283 memcpy(dst
, kernelbuf
, n
);
287 res
= ps3flash_write_chunk(dev
, start_write_sector
);
291 mutex_unlock(&priv
->mutex
);
295 start_write_sector
+= chunk_sectors
;
298 } while (remaining
> 0);
303 mutex_unlock(&priv
->mutex
);
307 static ssize_t
ps3flash_user_read(struct file
*file
, char __user
*buf
,
308 size_t count
, loff_t
*pos
)
310 return ps3flash_read(buf
, NULL
, count
, pos
);
313 static ssize_t
ps3flash_user_write(struct file
*file
, const char __user
*buf
,
314 size_t count
, loff_t
*pos
)
316 return ps3flash_write(buf
, NULL
, count
, pos
);
319 static ssize_t
ps3flash_kernel_read(void *buf
, size_t count
, loff_t pos
)
321 return ps3flash_read(NULL
, buf
, count
, &pos
);
324 static ssize_t
ps3flash_kernel_write(const void *buf
, size_t count
,
327 return ps3flash_write(NULL
, buf
, count
, &pos
);
331 static irqreturn_t
ps3flash_interrupt(int irq
, void *data
)
333 struct ps3_storage_device
*dev
= data
;
337 res
= lv1_storage_get_async_status(dev
->sbd
.dev_id
, &tag
, &status
);
340 dev_err(&dev
->sbd
.core
,
341 "%s:%u: tag mismatch, got %llx, expected %llx\n",
342 __func__
, __LINE__
, tag
, dev
->tag
);
345 dev_err(&dev
->sbd
.core
, "%s:%u: res=%d status=0x%llx\n",
346 __func__
, __LINE__
, res
, status
);
348 dev
->lv1_status
= status
;
349 complete(&dev
->done
);
354 static const struct file_operations ps3flash_fops
= {
355 .owner
= THIS_MODULE
,
356 .llseek
= ps3flash_llseek
,
357 .read
= ps3flash_user_read
,
358 .write
= ps3flash_user_write
,
361 static const struct ps3_os_area_flash_ops ps3flash_kernel_ops
= {
362 .read
= ps3flash_kernel_read
,
363 .write
= ps3flash_kernel_write
,
366 static struct miscdevice ps3flash_misc
= {
367 .minor
= MISC_DYNAMIC_MINOR
,
369 .fops
= &ps3flash_fops
,
372 static int __devinit
ps3flash_probe(struct ps3_system_bus_device
*_dev
)
374 struct ps3_storage_device
*dev
= to_ps3_storage_device(&_dev
->core
);
375 struct ps3flash_private
*priv
;
379 tmp
= dev
->regions
[dev
->region_idx
].start
*dev
->blk_size
;
380 if (tmp
% FLASH_BLOCK_SIZE
) {
381 dev_err(&dev
->sbd
.core
,
382 "%s:%u region start %lu is not aligned\n", __func__
,
386 tmp
= dev
->regions
[dev
->region_idx
].size
*dev
->blk_size
;
387 if (tmp
% FLASH_BLOCK_SIZE
) {
388 dev_err(&dev
->sbd
.core
,
389 "%s:%u region size %lu is not aligned\n", __func__
,
394 /* use static buffer, kmalloc cannot allocate 256 KiB */
395 if (!ps3flash_bounce_buffer
.address
)
399 dev_err(&dev
->sbd
.core
,
400 "Only one FLASH device is supported\n");
406 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
412 ps3_system_bus_set_drvdata(&dev
->sbd
, priv
);
413 mutex_init(&priv
->mutex
);
415 dev
->bounce_size
= ps3flash_bounce_buffer
.size
;
416 dev
->bounce_buf
= ps3flash_bounce_buffer
.address
;
418 error
= ps3stor_setup(dev
, ps3flash_interrupt
);
422 ps3flash_misc
.parent
= &dev
->sbd
.core
;
423 error
= misc_register(&ps3flash_misc
);
425 dev_err(&dev
->sbd
.core
, "%s:%u: misc_register failed %d\n",
426 __func__
, __LINE__
, error
);
430 dev_info(&dev
->sbd
.core
, "%s:%u: registered misc device %d\n",
431 __func__
, __LINE__
, ps3flash_misc
.minor
);
433 ps3_os_area_flash_register(&ps3flash_kernel_ops
);
437 ps3stor_teardown(dev
);
440 ps3_system_bus_set_drvdata(&dev
->sbd
, NULL
);
446 static int ps3flash_remove(struct ps3_system_bus_device
*_dev
)
448 struct ps3_storage_device
*dev
= to_ps3_storage_device(&_dev
->core
);
450 ps3_os_area_flash_register(NULL
);
451 misc_deregister(&ps3flash_misc
);
452 ps3stor_teardown(dev
);
453 kfree(ps3_system_bus_get_drvdata(&dev
->sbd
));
454 ps3_system_bus_set_drvdata(&dev
->sbd
, NULL
);
460 static struct ps3_system_bus_driver ps3flash
= {
461 .match_id
= PS3_MATCH_ID_STOR_FLASH
,
462 .core
.name
= DEVICE_NAME
,
463 .core
.owner
= THIS_MODULE
,
464 .probe
= ps3flash_probe
,
465 .remove
= ps3flash_remove
,
466 .shutdown
= ps3flash_remove
,
470 static int __init
ps3flash_init(void)
472 return ps3_system_bus_driver_register(&ps3flash
);
475 static void __exit
ps3flash_exit(void)
477 ps3_system_bus_driver_unregister(&ps3flash
);
480 module_init(ps3flash_init
);
481 module_exit(ps3flash_exit
);
483 MODULE_LICENSE("GPL");
484 MODULE_DESCRIPTION("PS3 FLASH ROM Storage Driver");
485 MODULE_AUTHOR("Sony Corporation");
486 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_FLASH
);