Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * PS3 FLASH ROM Storage Driver |
| 3 | * |
| 4 | * Copyright (C) 2007 Sony Computer Entertainment Inc. |
| 5 | * Copyright 2007 Sony Corp. |
| 6 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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. |
| 19 | */ |
| 20 | |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/miscdevice.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 24 | #include <linux/uaccess.h> |
| 25 | |
| 26 | #include <asm/lv1call.h> |
| 27 | #include <asm/ps3stor.h> |
| 28 | |
| 29 | |
| 30 | #define DEVICE_NAME "ps3flash" |
| 31 | |
| 32 | #define FLASH_BLOCK_SIZE (256*1024) |
| 33 | |
| 34 | |
| 35 | struct ps3flash_private { |
| 36 | struct mutex mutex; /* Bounce buffer mutex */ |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 37 | u64 chunk_sectors; |
| 38 | int tag; /* Start sector of buffer, -1 if invalid */ |
| 39 | bool dirty; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static struct ps3_storage_device *ps3flash_dev; |
| 43 | |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 44 | static int ps3flash_read_write_sectors(struct ps3_storage_device *dev, |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 45 | u64 start_sector, int write) |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 46 | { |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 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, |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 50 | write); |
| 51 | if (res) { |
Stephen Rothwell | 4c33d2d | 2009-01-13 20:06:02 +0000 | [diff] [blame] | 52 | dev_err(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__, |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 53 | __LINE__, write ? "write" : "read", res); |
| 54 | return -EIO; |
| 55 | } |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 56 | return 0; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 59 | static int ps3flash_writeback(struct ps3_storage_device *dev) |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 60 | { |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 61 | struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); |
| 62 | int res; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 63 | |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 64 | if (!priv->dirty || priv->tag < 0) |
| 65 | return 0; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 66 | |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 67 | res = ps3flash_read_write_sectors(dev, priv->tag, 1); |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 68 | if (res) |
| 69 | return res; |
| 70 | |
| 71 | priv->dirty = false; |
| 72 | return 0; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 75 | static int ps3flash_fetch(struct ps3_storage_device *dev, u64 start_sector) |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 76 | { |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 77 | struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 78 | int res; |
| 79 | |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 80 | if (start_sector == priv->tag) |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 81 | return 0; |
| 82 | |
| 83 | res = ps3flash_writeback(dev); |
| 84 | if (res) |
| 85 | return res; |
| 86 | |
| 87 | priv->tag = -1; |
| 88 | |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 89 | res = ps3flash_read_write_sectors(dev, start_sector, 0); |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 90 | if (res) |
| 91 | return res; |
| 92 | |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 93 | priv->tag = start_sector; |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 94 | return 0; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static loff_t ps3flash_llseek(struct file *file, loff_t offset, int origin) |
| 98 | { |
| 99 | struct ps3_storage_device *dev = ps3flash_dev; |
| 100 | loff_t res; |
| 101 | |
| 102 | mutex_lock(&file->f_mapping->host->i_mutex); |
| 103 | switch (origin) { |
Josef Bacik | 2273506 | 2011-07-18 13:21:39 -0400 | [diff] [blame^] | 104 | case 0: |
| 105 | break; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 106 | case 1: |
| 107 | offset += file->f_pos; |
| 108 | break; |
| 109 | case 2: |
| 110 | offset += dev->regions[dev->region_idx].size*dev->blk_size; |
| 111 | break; |
Josef Bacik | 2273506 | 2011-07-18 13:21:39 -0400 | [diff] [blame^] | 112 | default: |
| 113 | offset = -1; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 114 | } |
| 115 | if (offset < 0) { |
| 116 | res = -EINVAL; |
| 117 | goto out; |
| 118 | } |
| 119 | |
| 120 | file->f_pos = offset; |
| 121 | res = file->f_pos; |
| 122 | |
| 123 | out: |
| 124 | mutex_unlock(&file->f_mapping->host->i_mutex); |
| 125 | return res; |
| 126 | } |
| 127 | |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 128 | static ssize_t ps3flash_read(char __user *userbuf, void *kernelbuf, |
| 129 | size_t count, loff_t *pos) |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 130 | { |
| 131 | struct ps3_storage_device *dev = ps3flash_dev; |
Geert Uytterhoeven | 559dc87f5 | 2009-06-10 04:38:55 +0000 | [diff] [blame] | 132 | struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 133 | u64 size, sector, offset; |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 134 | int res; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 135 | size_t remaining, n; |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 136 | const void *src; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 137 | |
| 138 | dev_dbg(&dev->sbd.core, |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 139 | "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n", |
| 140 | __func__, __LINE__, count, *pos, userbuf, kernelbuf); |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 141 | |
| 142 | size = dev->regions[dev->region_idx].size*dev->blk_size; |
| 143 | if (*pos >= size || !count) |
| 144 | return 0; |
| 145 | |
| 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); |
| 150 | count = size - *pos; |
| 151 | } |
| 152 | |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 153 | sector = *pos / dev->bounce_size * priv->chunk_sectors; |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 154 | offset = *pos % dev->bounce_size; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 155 | |
| 156 | remaining = count; |
| 157 | do { |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 158 | n = min_t(u64, remaining, dev->bounce_size - offset); |
| 159 | src = dev->bounce_buf + offset; |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 160 | |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 161 | mutex_lock(&priv->mutex); |
| 162 | |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 163 | res = ps3flash_fetch(dev, sector); |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 164 | if (res) |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 165 | goto fail; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 166 | |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 167 | dev_dbg(&dev->sbd.core, |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 168 | "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n", |
| 169 | __func__, __LINE__, n, src, userbuf, kernelbuf); |
| 170 | if (userbuf) { |
| 171 | if (copy_to_user(userbuf, src, n)) { |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 172 | res = -EFAULT; |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 173 | goto fail; |
| 174 | } |
| 175 | userbuf += n; |
| 176 | } |
| 177 | if (kernelbuf) { |
| 178 | memcpy(kernelbuf, src, n); |
| 179 | kernelbuf += n; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | mutex_unlock(&priv->mutex); |
| 183 | |
| 184 | *pos += n; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 185 | remaining -= n; |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 186 | sector += priv->chunk_sectors; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 187 | offset = 0; |
| 188 | } while (remaining > 0); |
| 189 | |
| 190 | return count; |
| 191 | |
| 192 | fail: |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 193 | mutex_unlock(&priv->mutex); |
| 194 | return res; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 197 | static ssize_t ps3flash_write(const char __user *userbuf, |
| 198 | const void *kernelbuf, size_t count, loff_t *pos) |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 199 | { |
| 200 | struct ps3_storage_device *dev = ps3flash_dev; |
Geert Uytterhoeven | 559dc87f5 | 2009-06-10 04:38:55 +0000 | [diff] [blame] | 201 | struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd); |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 202 | u64 size, sector, offset; |
| 203 | int res = 0; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 204 | size_t remaining, n; |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 205 | void *dst; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 206 | |
| 207 | dev_dbg(&dev->sbd.core, |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 208 | "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n", |
| 209 | __func__, __LINE__, count, *pos, userbuf, kernelbuf); |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 210 | |
| 211 | size = dev->regions[dev->region_idx].size*dev->blk_size; |
| 212 | if (*pos >= size || !count) |
| 213 | return 0; |
| 214 | |
| 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); |
| 219 | count = size - *pos; |
| 220 | } |
| 221 | |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 222 | sector = *pos / dev->bounce_size * priv->chunk_sectors; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 223 | offset = *pos % dev->bounce_size; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 224 | |
| 225 | remaining = count; |
| 226 | do { |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 227 | n = min_t(u64, remaining, dev->bounce_size - offset); |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 228 | dst = dev->bounce_buf + offset; |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 229 | |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 230 | mutex_lock(&priv->mutex); |
| 231 | |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 232 | if (n != dev->bounce_size) |
Geert Uytterhoeven | 42e27bf | 2009-06-10 04:39:08 +0000 | [diff] [blame] | 233 | res = ps3flash_fetch(dev, sector); |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 234 | else if (sector != priv->tag) |
| 235 | res = ps3flash_writeback(dev); |
| 236 | if (res) |
| 237 | goto fail; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 238 | |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 239 | dev_dbg(&dev->sbd.core, |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 240 | "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n", |
| 241 | __func__, __LINE__, n, userbuf, kernelbuf, dst); |
| 242 | if (userbuf) { |
| 243 | if (copy_from_user(dst, userbuf, n)) { |
| 244 | res = -EFAULT; |
| 245 | goto fail; |
| 246 | } |
| 247 | userbuf += n; |
| 248 | } |
| 249 | if (kernelbuf) { |
| 250 | memcpy(dst, kernelbuf, n); |
| 251 | kernelbuf += n; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 254 | priv->tag = sector; |
| 255 | priv->dirty = true; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 256 | |
| 257 | mutex_unlock(&priv->mutex); |
| 258 | |
| 259 | *pos += n; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 260 | remaining -= n; |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 261 | sector += priv->chunk_sectors; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 262 | offset = 0; |
| 263 | } while (remaining > 0); |
| 264 | |
| 265 | return count; |
| 266 | |
| 267 | fail: |
| 268 | mutex_unlock(&priv->mutex); |
| 269 | return res; |
| 270 | } |
| 271 | |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 272 | static ssize_t ps3flash_user_read(struct file *file, char __user *buf, |
| 273 | size_t count, loff_t *pos) |
| 274 | { |
| 275 | return ps3flash_read(buf, NULL, count, pos); |
| 276 | } |
| 277 | |
| 278 | static ssize_t ps3flash_user_write(struct file *file, const char __user *buf, |
| 279 | size_t count, loff_t *pos) |
| 280 | { |
| 281 | return ps3flash_write(buf, NULL, count, pos); |
| 282 | } |
| 283 | |
| 284 | static ssize_t ps3flash_kernel_read(void *buf, size_t count, loff_t pos) |
| 285 | { |
| 286 | return ps3flash_read(NULL, buf, count, &pos); |
| 287 | } |
| 288 | |
| 289 | static ssize_t ps3flash_kernel_write(const void *buf, size_t count, |
| 290 | loff_t pos) |
| 291 | { |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 292 | ssize_t res; |
| 293 | int wb; |
| 294 | |
| 295 | res = ps3flash_write(NULL, buf, count, &pos); |
| 296 | if (res < 0) |
| 297 | return res; |
| 298 | |
| 299 | /* Make kernel writes synchronous */ |
| 300 | wb = ps3flash_writeback(ps3flash_dev); |
| 301 | if (wb) |
| 302 | return wb; |
| 303 | |
| 304 | return res; |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 307 | static int ps3flash_flush(struct file *file, fl_owner_t id) |
| 308 | { |
| 309 | return ps3flash_writeback(ps3flash_dev); |
| 310 | } |
| 311 | |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 312 | static int ps3flash_fsync(struct file *file, int datasync) |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 313 | { |
| 314 | return ps3flash_writeback(ps3flash_dev); |
| 315 | } |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 316 | |
| 317 | static irqreturn_t ps3flash_interrupt(int irq, void *data) |
| 318 | { |
| 319 | struct ps3_storage_device *dev = data; |
| 320 | int res; |
| 321 | u64 tag, status; |
| 322 | |
| 323 | res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status); |
| 324 | |
| 325 | if (tag != dev->tag) |
| 326 | dev_err(&dev->sbd.core, |
Stephen Rothwell | 4c33d2d | 2009-01-13 20:06:02 +0000 | [diff] [blame] | 327 | "%s:%u: tag mismatch, got %llx, expected %llx\n", |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 328 | __func__, __LINE__, tag, dev->tag); |
| 329 | |
| 330 | if (res) { |
Stephen Rothwell | 4c33d2d | 2009-01-13 20:06:02 +0000 | [diff] [blame] | 331 | dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n", |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 332 | __func__, __LINE__, res, status); |
| 333 | } else { |
| 334 | dev->lv1_status = status; |
| 335 | complete(&dev->done); |
| 336 | } |
| 337 | return IRQ_HANDLED; |
| 338 | } |
| 339 | |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 340 | static const struct file_operations ps3flash_fops = { |
| 341 | .owner = THIS_MODULE, |
| 342 | .llseek = ps3flash_llseek, |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 343 | .read = ps3flash_user_read, |
| 344 | .write = ps3flash_user_write, |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 345 | .flush = ps3flash_flush, |
| 346 | .fsync = ps3flash_fsync, |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
| 349 | static const struct ps3_os_area_flash_ops ps3flash_kernel_ops = { |
| 350 | .read = ps3flash_kernel_read, |
| 351 | .write = ps3flash_kernel_write, |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 352 | }; |
| 353 | |
| 354 | static struct miscdevice ps3flash_misc = { |
| 355 | .minor = MISC_DYNAMIC_MINOR, |
| 356 | .name = DEVICE_NAME, |
| 357 | .fops = &ps3flash_fops, |
| 358 | }; |
| 359 | |
| 360 | static int __devinit ps3flash_probe(struct ps3_system_bus_device *_dev) |
| 361 | { |
| 362 | struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core); |
| 363 | struct ps3flash_private *priv; |
| 364 | int error; |
| 365 | unsigned long tmp; |
| 366 | |
| 367 | tmp = dev->regions[dev->region_idx].start*dev->blk_size; |
| 368 | if (tmp % FLASH_BLOCK_SIZE) { |
| 369 | dev_err(&dev->sbd.core, |
| 370 | "%s:%u region start %lu is not aligned\n", __func__, |
| 371 | __LINE__, tmp); |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | tmp = dev->regions[dev->region_idx].size*dev->blk_size; |
| 375 | if (tmp % FLASH_BLOCK_SIZE) { |
| 376 | dev_err(&dev->sbd.core, |
| 377 | "%s:%u region size %lu is not aligned\n", __func__, |
| 378 | __LINE__, tmp); |
| 379 | return -EINVAL; |
| 380 | } |
| 381 | |
| 382 | /* use static buffer, kmalloc cannot allocate 256 KiB */ |
| 383 | if (!ps3flash_bounce_buffer.address) |
| 384 | return -ENODEV; |
| 385 | |
| 386 | if (ps3flash_dev) { |
| 387 | dev_err(&dev->sbd.core, |
| 388 | "Only one FLASH device is supported\n"); |
| 389 | return -EBUSY; |
| 390 | } |
| 391 | |
| 392 | ps3flash_dev = dev; |
| 393 | |
| 394 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 395 | if (!priv) { |
| 396 | error = -ENOMEM; |
| 397 | goto fail; |
| 398 | } |
| 399 | |
Geert Uytterhoeven | 559dc87f5 | 2009-06-10 04:38:55 +0000 | [diff] [blame] | 400 | ps3_system_bus_set_drvdata(&dev->sbd, priv); |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 401 | mutex_init(&priv->mutex); |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 402 | priv->tag = -1; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 403 | |
| 404 | dev->bounce_size = ps3flash_bounce_buffer.size; |
| 405 | dev->bounce_buf = ps3flash_bounce_buffer.address; |
Geert Uytterhoeven | 6bd57f2 | 2009-06-10 04:39:07 +0000 | [diff] [blame] | 406 | priv->chunk_sectors = dev->bounce_size / dev->blk_size; |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 407 | |
| 408 | error = ps3stor_setup(dev, ps3flash_interrupt); |
| 409 | if (error) |
| 410 | goto fail_free_priv; |
| 411 | |
| 412 | ps3flash_misc.parent = &dev->sbd.core; |
| 413 | error = misc_register(&ps3flash_misc); |
| 414 | if (error) { |
| 415 | dev_err(&dev->sbd.core, "%s:%u: misc_register failed %d\n", |
| 416 | __func__, __LINE__, error); |
| 417 | goto fail_teardown; |
| 418 | } |
| 419 | |
| 420 | dev_info(&dev->sbd.core, "%s:%u: registered misc device %d\n", |
| 421 | __func__, __LINE__, ps3flash_misc.minor); |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 422 | |
| 423 | ps3_os_area_flash_register(&ps3flash_kernel_ops); |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 424 | return 0; |
| 425 | |
| 426 | fail_teardown: |
| 427 | ps3stor_teardown(dev); |
| 428 | fail_free_priv: |
| 429 | kfree(priv); |
Geert Uytterhoeven | 559dc87f5 | 2009-06-10 04:38:55 +0000 | [diff] [blame] | 430 | ps3_system_bus_set_drvdata(&dev->sbd, NULL); |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 431 | fail: |
| 432 | ps3flash_dev = NULL; |
| 433 | return error; |
| 434 | } |
| 435 | |
| 436 | static int ps3flash_remove(struct ps3_system_bus_device *_dev) |
| 437 | { |
| 438 | struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core); |
| 439 | |
Geert Uytterhoeven | a4e623f | 2009-06-10 04:39:06 +0000 | [diff] [blame] | 440 | ps3_os_area_flash_register(NULL); |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 441 | misc_deregister(&ps3flash_misc); |
| 442 | ps3stor_teardown(dev); |
Geert Uytterhoeven | 559dc87f5 | 2009-06-10 04:38:55 +0000 | [diff] [blame] | 443 | kfree(ps3_system_bus_get_drvdata(&dev->sbd)); |
| 444 | ps3_system_bus_set_drvdata(&dev->sbd, NULL); |
Geert Uytterhoeven | f965263 | 2007-07-21 04:37:48 -0700 | [diff] [blame] | 445 | ps3flash_dev = NULL; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | static struct ps3_system_bus_driver ps3flash = { |
| 451 | .match_id = PS3_MATCH_ID_STOR_FLASH, |
| 452 | .core.name = DEVICE_NAME, |
| 453 | .core.owner = THIS_MODULE, |
| 454 | .probe = ps3flash_probe, |
| 455 | .remove = ps3flash_remove, |
| 456 | .shutdown = ps3flash_remove, |
| 457 | }; |
| 458 | |
| 459 | |
| 460 | static int __init ps3flash_init(void) |
| 461 | { |
| 462 | return ps3_system_bus_driver_register(&ps3flash); |
| 463 | } |
| 464 | |
| 465 | static void __exit ps3flash_exit(void) |
| 466 | { |
| 467 | ps3_system_bus_driver_unregister(&ps3flash); |
| 468 | } |
| 469 | |
| 470 | module_init(ps3flash_init); |
| 471 | module_exit(ps3flash_exit); |
| 472 | |
| 473 | MODULE_LICENSE("GPL"); |
| 474 | MODULE_DESCRIPTION("PS3 FLASH ROM Storage Driver"); |
| 475 | MODULE_AUTHOR("Sony Corporation"); |
| 476 | MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_FLASH); |