Thomas Gleixner | 1621633 | 2019-05-19 15:51:31 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 2 | /* GD ROM driver for the SEGA Dreamcast |
| 3 | * copyright Adrian McMenamin, 2007 |
| 4 | * With thanks to Marcus Comstedt and Nathan Keynes |
| 5 | * for work in reversing PIO and DMA |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
Joe Perches | e597cd0 | 2010-07-01 08:24:32 +0200 | [diff] [blame] | 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/dma-mapping.h> |
| 17 | #include <linux/cdrom.h> |
| 18 | #include <linux/genhd.h> |
| 19 | #include <linux/bio.h> |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 20 | #include <linux/blk-mq.h> |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/device.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 24 | #include <linux/wait.h> |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <scsi/scsi.h> |
| 27 | #include <asm/io.h> |
| 28 | #include <asm/dma.h> |
| 29 | #include <asm/delay.h> |
Paul Mundt | 0764bff | 2008-07-29 22:10:01 +0900 | [diff] [blame] | 30 | #include <mach/dma.h> |
| 31 | #include <mach/sysasic.h> |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 32 | |
| 33 | #define GDROM_DEV_NAME "gdrom" |
| 34 | #define GD_SESSION_OFFSET 150 |
| 35 | |
| 36 | /* GD Rom commands */ |
| 37 | #define GDROM_COM_SOFTRESET 0x08 |
| 38 | #define GDROM_COM_EXECDIAG 0x90 |
| 39 | #define GDROM_COM_PACKET 0xA0 |
| 40 | #define GDROM_COM_IDDEV 0xA1 |
| 41 | |
| 42 | /* GD Rom registers */ |
| 43 | #define GDROM_BASE_REG 0xA05F7000 |
| 44 | #define GDROM_ALTSTATUS_REG (GDROM_BASE_REG + 0x18) |
| 45 | #define GDROM_DATA_REG (GDROM_BASE_REG + 0x80) |
| 46 | #define GDROM_ERROR_REG (GDROM_BASE_REG + 0x84) |
| 47 | #define GDROM_INTSEC_REG (GDROM_BASE_REG + 0x88) |
| 48 | #define GDROM_SECNUM_REG (GDROM_BASE_REG + 0x8C) |
| 49 | #define GDROM_BCL_REG (GDROM_BASE_REG + 0x90) |
| 50 | #define GDROM_BCH_REG (GDROM_BASE_REG + 0x94) |
| 51 | #define GDROM_DSEL_REG (GDROM_BASE_REG + 0x98) |
| 52 | #define GDROM_STATUSCOMMAND_REG (GDROM_BASE_REG + 0x9C) |
| 53 | #define GDROM_RESET_REG (GDROM_BASE_REG + 0x4E4) |
| 54 | |
| 55 | #define GDROM_DMA_STARTADDR_REG (GDROM_BASE_REG + 0x404) |
| 56 | #define GDROM_DMA_LENGTH_REG (GDROM_BASE_REG + 0x408) |
| 57 | #define GDROM_DMA_DIRECTION_REG (GDROM_BASE_REG + 0x40C) |
| 58 | #define GDROM_DMA_ENABLE_REG (GDROM_BASE_REG + 0x414) |
| 59 | #define GDROM_DMA_STATUS_REG (GDROM_BASE_REG + 0x418) |
| 60 | #define GDROM_DMA_WAIT_REG (GDROM_BASE_REG + 0x4A0) |
| 61 | #define GDROM_DMA_ACCESS_CTRL_REG (GDROM_BASE_REG + 0x4B8) |
| 62 | |
| 63 | #define GDROM_HARD_SECTOR 2048 |
| 64 | #define BLOCK_LAYER_SECTOR 512 |
| 65 | #define GD_TO_BLK 4 |
| 66 | |
| 67 | #define GDROM_DEFAULT_TIMEOUT (HZ * 7) |
| 68 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 69 | static DEFINE_MUTEX(gdrom_mutex); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 70 | static const struct { |
| 71 | int sense_key; |
| 72 | const char * const text; |
| 73 | } sense_texts[] = { |
| 74 | {NO_SENSE, "OK"}, |
| 75 | {RECOVERED_ERROR, "Recovered from error"}, |
| 76 | {NOT_READY, "Device not ready"}, |
| 77 | {MEDIUM_ERROR, "Disk not ready"}, |
| 78 | {HARDWARE_ERROR, "Hardware error"}, |
| 79 | {ILLEGAL_REQUEST, "Command has failed"}, |
| 80 | {UNIT_ATTENTION, "Device needs attention - disk may have been changed"}, |
| 81 | {DATA_PROTECT, "Data protection error"}, |
| 82 | {ABORTED_COMMAND, "Command aborted"}, |
| 83 | }; |
| 84 | |
| 85 | static struct platform_device *pd; |
| 86 | static int gdrom_major; |
| 87 | static DECLARE_WAIT_QUEUE_HEAD(command_queue); |
| 88 | static DECLARE_WAIT_QUEUE_HEAD(request_queue); |
| 89 | |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 90 | struct gdromtoc { |
| 91 | unsigned int entry[99]; |
| 92 | unsigned int first, last; |
| 93 | unsigned int leadout; |
| 94 | }; |
| 95 | |
| 96 | static struct gdrom_unit { |
| 97 | struct gendisk *disk; |
| 98 | struct cdrom_device_info *cd_info; |
| 99 | int status; |
| 100 | int pending; |
| 101 | int transfer; |
| 102 | char disk_type; |
| 103 | struct gdromtoc *toc; |
| 104 | struct request_queue *gdrom_rq; |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 105 | struct blk_mq_tag_set tag_set; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 106 | } gd; |
| 107 | |
| 108 | struct gdrom_id { |
| 109 | char mid; |
| 110 | char modid; |
| 111 | char verid; |
| 112 | char padA[13]; |
| 113 | char mname[16]; |
| 114 | char modname[16]; |
| 115 | char firmver[16]; |
| 116 | char padB[16]; |
| 117 | }; |
| 118 | |
| 119 | static int gdrom_getsense(short *bufstring); |
| 120 | static int gdrom_packetcommand(struct cdrom_device_info *cd_info, |
| 121 | struct packet_command *command); |
| 122 | static int gdrom_hardreset(struct cdrom_device_info *cd_info); |
| 123 | |
| 124 | static bool gdrom_is_busy(void) |
| 125 | { |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 126 | return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) != 0; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static bool gdrom_data_request(void) |
| 130 | { |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 131 | return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x88) == 8; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static bool gdrom_wait_clrbusy(void) |
| 135 | { |
| 136 | unsigned long timeout = jiffies + GDROM_DEFAULT_TIMEOUT; |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 137 | while ((__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) && |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 138 | (time_before(jiffies, timeout))) |
| 139 | cpu_relax(); |
| 140 | return time_before(jiffies, timeout + 1); |
| 141 | } |
| 142 | |
| 143 | static bool gdrom_wait_busy_sleeps(void) |
| 144 | { |
| 145 | unsigned long timeout; |
| 146 | /* Wait to get busy first */ |
| 147 | timeout = jiffies + GDROM_DEFAULT_TIMEOUT; |
| 148 | while (!gdrom_is_busy() && time_before(jiffies, timeout)) |
| 149 | cpu_relax(); |
| 150 | /* Now wait for busy to clear */ |
| 151 | return gdrom_wait_clrbusy(); |
| 152 | } |
| 153 | |
| 154 | static void gdrom_identifydevice(void *buf) |
| 155 | { |
| 156 | int c; |
| 157 | short *data = buf; |
| 158 | /* If the device won't clear it has probably |
| 159 | * been hit by a serious failure - but we'll |
| 160 | * try to return a sense key even so */ |
| 161 | if (!gdrom_wait_clrbusy()) { |
| 162 | gdrom_getsense(NULL); |
| 163 | return; |
| 164 | } |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 165 | __raw_writeb(GDROM_COM_IDDEV, GDROM_STATUSCOMMAND_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 166 | if (!gdrom_wait_busy_sleeps()) { |
| 167 | gdrom_getsense(NULL); |
| 168 | return; |
| 169 | } |
| 170 | /* now read in the data */ |
| 171 | for (c = 0; c < 40; c++) |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 172 | data[c] = __raw_readw(GDROM_DATA_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void gdrom_spicommand(void *spi_string, int buflen) |
| 176 | { |
| 177 | short *cmd = spi_string; |
| 178 | unsigned long timeout; |
| 179 | |
| 180 | /* ensure IRQ_WAIT is set */ |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 181 | __raw_writeb(0x08, GDROM_ALTSTATUS_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 182 | /* specify how many bytes we expect back */ |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 183 | __raw_writeb(buflen & 0xFF, GDROM_BCL_REG); |
| 184 | __raw_writeb((buflen >> 8) & 0xFF, GDROM_BCH_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 185 | /* other parameters */ |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 186 | __raw_writeb(0, GDROM_INTSEC_REG); |
| 187 | __raw_writeb(0, GDROM_SECNUM_REG); |
| 188 | __raw_writeb(0, GDROM_ERROR_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 189 | /* Wait until we can go */ |
| 190 | if (!gdrom_wait_clrbusy()) { |
| 191 | gdrom_getsense(NULL); |
| 192 | return; |
| 193 | } |
| 194 | timeout = jiffies + GDROM_DEFAULT_TIMEOUT; |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 195 | __raw_writeb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 196 | while (!gdrom_data_request() && time_before(jiffies, timeout)) |
| 197 | cpu_relax(); |
| 198 | if (!time_before(jiffies, timeout + 1)) { |
| 199 | gdrom_getsense(NULL); |
| 200 | return; |
| 201 | } |
Paul Mundt | 54d5102 | 2009-10-13 12:30:40 +0900 | [diff] [blame] | 202 | outsw(GDROM_DATA_REG, cmd, 6); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
| 206 | /* gdrom_command_executediagnostic: |
| 207 | * Used to probe for presence of working GDROM |
| 208 | * Restarts GDROM device and then applies standard ATA 3 |
| 209 | * Execute Diagnostic Command: a return of '1' indicates device 0 |
| 210 | * present and device 1 absent |
| 211 | */ |
| 212 | static char gdrom_execute_diagnostic(void) |
| 213 | { |
| 214 | gdrom_hardreset(gd.cd_info); |
| 215 | if (!gdrom_wait_clrbusy()) |
| 216 | return 0; |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 217 | __raw_writeb(GDROM_COM_EXECDIAG, GDROM_STATUSCOMMAND_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 218 | if (!gdrom_wait_busy_sleeps()) |
| 219 | return 0; |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 220 | return __raw_readb(GDROM_ERROR_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Prepare disk command |
| 225 | * byte 0 = 0x70 |
| 226 | * byte 1 = 0x1f |
| 227 | */ |
| 228 | static int gdrom_preparedisk_cmd(void) |
| 229 | { |
| 230 | struct packet_command *spin_command; |
| 231 | spin_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL); |
| 232 | if (!spin_command) |
| 233 | return -ENOMEM; |
| 234 | spin_command->cmd[0] = 0x70; |
| 235 | spin_command->cmd[2] = 0x1f; |
| 236 | spin_command->buflen = 0; |
| 237 | gd.pending = 1; |
| 238 | gdrom_packetcommand(gd.cd_info, spin_command); |
| 239 | /* 60 second timeout */ |
| 240 | wait_event_interruptible_timeout(command_queue, gd.pending == 0, |
| 241 | GDROM_DEFAULT_TIMEOUT); |
| 242 | gd.pending = 0; |
| 243 | kfree(spin_command); |
| 244 | if (gd.status & 0x01) { |
| 245 | /* log an error */ |
| 246 | gdrom_getsense(NULL); |
| 247 | return -EIO; |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * Read TOC command |
| 254 | * byte 0 = 0x14 |
| 255 | * byte 1 = session |
| 256 | * byte 3 = sizeof TOC >> 8 ie upper byte |
| 257 | * byte 4 = sizeof TOC & 0xff ie lower byte |
| 258 | */ |
| 259 | static int gdrom_readtoc_cmd(struct gdromtoc *toc, int session) |
| 260 | { |
| 261 | int tocsize; |
| 262 | struct packet_command *toc_command; |
| 263 | int err = 0; |
| 264 | |
| 265 | toc_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL); |
| 266 | if (!toc_command) |
| 267 | return -ENOMEM; |
| 268 | tocsize = sizeof(struct gdromtoc); |
| 269 | toc_command->cmd[0] = 0x14; |
| 270 | toc_command->cmd[1] = session; |
| 271 | toc_command->cmd[3] = tocsize >> 8; |
| 272 | toc_command->cmd[4] = tocsize & 0xff; |
| 273 | toc_command->buflen = tocsize; |
| 274 | if (gd.pending) { |
| 275 | err = -EBUSY; |
| 276 | goto cleanup_readtoc_final; |
| 277 | } |
| 278 | gd.pending = 1; |
| 279 | gdrom_packetcommand(gd.cd_info, toc_command); |
| 280 | wait_event_interruptible_timeout(command_queue, gd.pending == 0, |
| 281 | GDROM_DEFAULT_TIMEOUT); |
| 282 | if (gd.pending) { |
| 283 | err = -EINVAL; |
| 284 | goto cleanup_readtoc; |
| 285 | } |
Paul Mundt | 54d5102 | 2009-10-13 12:30:40 +0900 | [diff] [blame] | 286 | insw(GDROM_DATA_REG, toc, tocsize/2); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 287 | if (gd.status & 0x01) |
| 288 | err = -EINVAL; |
| 289 | |
| 290 | cleanup_readtoc: |
| 291 | gd.pending = 0; |
| 292 | cleanup_readtoc_final: |
| 293 | kfree(toc_command); |
| 294 | return err; |
| 295 | } |
| 296 | |
| 297 | /* TOC helpers */ |
| 298 | static int get_entry_lba(int track) |
| 299 | { |
| 300 | return (cpu_to_be32(track & 0xffffff00) - GD_SESSION_OFFSET); |
| 301 | } |
| 302 | |
| 303 | static int get_entry_q_ctrl(int track) |
| 304 | { |
| 305 | return (track & 0x000000f0) >> 4; |
| 306 | } |
| 307 | |
| 308 | static int get_entry_track(int track) |
| 309 | { |
| 310 | return (track & 0x0000ff00) >> 8; |
| 311 | } |
| 312 | |
| 313 | static int gdrom_get_last_session(struct cdrom_device_info *cd_info, |
| 314 | struct cdrom_multisession *ms_info) |
| 315 | { |
zhong jiang | cdffab7 | 2018-10-23 20:06:37 +0800 | [diff] [blame] | 316 | int fentry, lentry, track, data, err; |
| 317 | |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 318 | if (!gd.toc) |
| 319 | return -ENOMEM; |
zhong jiang | cdffab7 | 2018-10-23 20:06:37 +0800 | [diff] [blame] | 320 | |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 321 | /* Check if GD-ROM */ |
| 322 | err = gdrom_readtoc_cmd(gd.toc, 1); |
| 323 | /* Not a GD-ROM so check if standard CD-ROM */ |
| 324 | if (err) { |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 325 | err = gdrom_readtoc_cmd(gd.toc, 0); |
| 326 | if (err) { |
Joe Perches | e597cd0 | 2010-07-01 08:24:32 +0200 | [diff] [blame] | 327 | pr_info("Could not get CD table of contents\n"); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 328 | return -ENXIO; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | fentry = get_entry_track(gd.toc->first); |
| 333 | lentry = get_entry_track(gd.toc->last); |
| 334 | /* Find the first data track */ |
| 335 | track = get_entry_track(gd.toc->last); |
| 336 | do { |
| 337 | data = gd.toc->entry[track - 1]; |
| 338 | if (get_entry_q_ctrl(data)) |
| 339 | break; /* ie a real data track */ |
| 340 | track--; |
| 341 | } while (track >= fentry); |
| 342 | |
| 343 | if ((track > 100) || (track < get_entry_track(gd.toc->first))) { |
Joe Perches | e597cd0 | 2010-07-01 08:24:32 +0200 | [diff] [blame] | 344 | pr_info("No data on the last session of the CD\n"); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 345 | gdrom_getsense(NULL); |
| 346 | return -ENXIO; |
| 347 | } |
| 348 | |
| 349 | ms_info->addr_format = CDROM_LBA; |
| 350 | ms_info->addr.lba = get_entry_lba(data); |
| 351 | ms_info->xa_flag = 1; |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static int gdrom_open(struct cdrom_device_info *cd_info, int purpose) |
| 356 | { |
| 357 | /* spin up the disk */ |
| 358 | return gdrom_preparedisk_cmd(); |
| 359 | } |
| 360 | |
| 361 | /* this function is required even if empty */ |
| 362 | static void gdrom_release(struct cdrom_device_info *cd_info) |
| 363 | { |
| 364 | } |
| 365 | |
| 366 | static int gdrom_drivestatus(struct cdrom_device_info *cd_info, int ignore) |
| 367 | { |
| 368 | /* read the sense key */ |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 369 | char sense = __raw_readb(GDROM_ERROR_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 370 | sense &= 0xF0; |
| 371 | if (sense == 0) |
| 372 | return CDS_DISC_OK; |
| 373 | if (sense == 0x20) |
| 374 | return CDS_DRIVE_NOT_READY; |
| 375 | /* default */ |
| 376 | return CDS_NO_INFO; |
| 377 | } |
| 378 | |
Tejun Heo | 1c27030 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 379 | static unsigned int gdrom_check_events(struct cdrom_device_info *cd_info, |
| 380 | unsigned int clearing, int ignore) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 381 | { |
| 382 | /* check the sense key */ |
Tejun Heo | 1c27030 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 383 | return (__raw_readb(GDROM_ERROR_REG) & 0xF0) == 0x60 ? |
| 384 | DISK_EVENT_MEDIA_CHANGE : 0; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* reset the G1 bus */ |
| 388 | static int gdrom_hardreset(struct cdrom_device_info *cd_info) |
| 389 | { |
| 390 | int count; |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 391 | __raw_writel(0x1fffff, GDROM_RESET_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 392 | for (count = 0xa0000000; count < 0xa0200000; count += 4) |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 393 | __raw_readl(count); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* keep the function looking like the universal |
| 398 | * CD Rom specification - returning int */ |
| 399 | static int gdrom_packetcommand(struct cdrom_device_info *cd_info, |
| 400 | struct packet_command *command) |
| 401 | { |
| 402 | gdrom_spicommand(&command->cmd, command->buflen); |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | /* Get Sense SPI command |
| 407 | * From Marcus Comstedt |
| 408 | * cmd = 0x13 |
| 409 | * cmd + 4 = length of returned buffer |
| 410 | * Returns 5 16 bit words |
| 411 | */ |
| 412 | static int gdrom_getsense(short *bufstring) |
| 413 | { |
| 414 | struct packet_command *sense_command; |
| 415 | short sense[5]; |
| 416 | int sense_key; |
| 417 | int err = -EIO; |
| 418 | |
| 419 | sense_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL); |
| 420 | if (!sense_command) |
| 421 | return -ENOMEM; |
| 422 | sense_command->cmd[0] = 0x13; |
| 423 | sense_command->cmd[4] = 10; |
| 424 | sense_command->buflen = 10; |
| 425 | /* even if something is pending try to get |
| 426 | * the sense key if possible */ |
| 427 | if (gd.pending && !gdrom_wait_clrbusy()) { |
| 428 | err = -EBUSY; |
| 429 | goto cleanup_sense_final; |
| 430 | } |
| 431 | gd.pending = 1; |
| 432 | gdrom_packetcommand(gd.cd_info, sense_command); |
| 433 | wait_event_interruptible_timeout(command_queue, gd.pending == 0, |
| 434 | GDROM_DEFAULT_TIMEOUT); |
| 435 | if (gd.pending) |
| 436 | goto cleanup_sense; |
Paul Mundt | 54d5102 | 2009-10-13 12:30:40 +0900 | [diff] [blame] | 437 | insw(GDROM_DATA_REG, &sense, sense_command->buflen/2); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 438 | if (sense[1] & 40) { |
Joe Perches | e597cd0 | 2010-07-01 08:24:32 +0200 | [diff] [blame] | 439 | pr_info("Drive not ready - command aborted\n"); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 440 | goto cleanup_sense; |
| 441 | } |
| 442 | sense_key = sense[1] & 0x0F; |
| 443 | if (sense_key < ARRAY_SIZE(sense_texts)) |
Joe Perches | e597cd0 | 2010-07-01 08:24:32 +0200 | [diff] [blame] | 444 | pr_info("%s\n", sense_texts[sense_key].text); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 445 | else |
Joe Perches | e597cd0 | 2010-07-01 08:24:32 +0200 | [diff] [blame] | 446 | pr_err("Unknown sense key: %d\n", sense_key); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 447 | if (bufstring) /* return addional sense data */ |
| 448 | memcpy(bufstring, &sense[4], 2); |
| 449 | if (sense_key < 2) |
| 450 | err = 0; |
| 451 | |
| 452 | cleanup_sense: |
| 453 | gd.pending = 0; |
| 454 | cleanup_sense_final: |
| 455 | kfree(sense_command); |
| 456 | return err; |
| 457 | } |
| 458 | |
Borislav Petkov | d15cad5 | 2008-08-18 21:40:04 +0200 | [diff] [blame] | 459 | static int gdrom_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd, |
| 460 | void *arg) |
| 461 | { |
| 462 | return -EINVAL; |
| 463 | } |
| 464 | |
Kees Cook | 853fe1b | 2017-02-13 16:25:26 -0800 | [diff] [blame] | 465 | static const struct cdrom_device_ops gdrom_ops = { |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 466 | .open = gdrom_open, |
| 467 | .release = gdrom_release, |
| 468 | .drive_status = gdrom_drivestatus, |
Tejun Heo | 1c27030 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 469 | .check_events = gdrom_check_events, |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 470 | .get_last_session = gdrom_get_last_session, |
| 471 | .reset = gdrom_hardreset, |
Borislav Petkov | d15cad5 | 2008-08-18 21:40:04 +0200 | [diff] [blame] | 472 | .audio_ioctl = gdrom_audio_ioctl, |
Kees Cook | 853fe1b | 2017-02-13 16:25:26 -0800 | [diff] [blame] | 473 | .generic_packet = cdrom_dummy_generic_packet, |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 474 | .capability = CDC_MULTI_SESSION | CDC_MEDIA_CHANGED | |
| 475 | CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R, |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 476 | }; |
| 477 | |
Al Viro | 8389feb | 2008-03-02 10:25:18 -0500 | [diff] [blame] | 478 | static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 479 | { |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 480 | int ret; |
Maurizio Lombardi | 2bbea6e | 2018-03-09 13:59:06 +0100 | [diff] [blame] | 481 | |
| 482 | check_disk_change(bdev); |
| 483 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 484 | mutex_lock(&gdrom_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 485 | ret = cdrom_open(gd.cd_info, bdev, mode); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 486 | mutex_unlock(&gdrom_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 487 | return ret; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 488 | } |
| 489 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 490 | static void gdrom_bdops_release(struct gendisk *disk, fmode_t mode) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 491 | { |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 492 | mutex_lock(&gdrom_mutex); |
Nobuhiro Iwamatsu | 545727f | 2008-10-27 11:32:27 +0900 | [diff] [blame] | 493 | cdrom_release(gd.cd_info, mode); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 494 | mutex_unlock(&gdrom_mutex); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 495 | } |
| 496 | |
Tejun Heo | 1c27030 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 497 | static unsigned int gdrom_bdops_check_events(struct gendisk *disk, |
| 498 | unsigned int clearing) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 499 | { |
Tejun Heo | 1c27030 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 500 | return cdrom_check_events(gd.cd_info, clearing); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 501 | } |
| 502 | |
Al Viro | 8389feb | 2008-03-02 10:25:18 -0500 | [diff] [blame] | 503 | static int gdrom_bdops_ioctl(struct block_device *bdev, fmode_t mode, |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 504 | unsigned cmd, unsigned long arg) |
| 505 | { |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 506 | int ret; |
| 507 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 508 | mutex_lock(&gdrom_mutex); |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 509 | ret = cdrom_ioctl(gd.cd_info, bdev, mode, cmd, arg); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 510 | mutex_unlock(&gdrom_mutex); |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 511 | |
| 512 | return ret; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 513 | } |
| 514 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 515 | static const struct block_device_operations gdrom_bdops = { |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 516 | .owner = THIS_MODULE, |
Al Viro | 8389feb | 2008-03-02 10:25:18 -0500 | [diff] [blame] | 517 | .open = gdrom_bdops_open, |
| 518 | .release = gdrom_bdops_release, |
Tejun Heo | 1c27030 | 2011-03-09 19:54:28 +0100 | [diff] [blame] | 519 | .check_events = gdrom_bdops_check_events, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 520 | .ioctl = gdrom_bdops_ioctl, |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 521 | }; |
| 522 | |
| 523 | static irqreturn_t gdrom_command_interrupt(int irq, void *dev_id) |
| 524 | { |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 525 | gd.status = __raw_readb(GDROM_STATUSCOMMAND_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 526 | if (gd.pending != 1) |
| 527 | return IRQ_HANDLED; |
| 528 | gd.pending = 0; |
| 529 | wake_up_interruptible(&command_queue); |
| 530 | return IRQ_HANDLED; |
| 531 | } |
| 532 | |
| 533 | static irqreturn_t gdrom_dma_interrupt(int irq, void *dev_id) |
| 534 | { |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 535 | gd.status = __raw_readb(GDROM_STATUSCOMMAND_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 536 | if (gd.transfer != 1) |
| 537 | return IRQ_HANDLED; |
| 538 | gd.transfer = 0; |
| 539 | wake_up_interruptible(&request_queue); |
| 540 | return IRQ_HANDLED; |
| 541 | } |
| 542 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 543 | static int gdrom_set_interrupt_handlers(void) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 544 | { |
| 545 | int err; |
| 546 | |
| 547 | err = request_irq(HW_EVENT_GDROM_CMD, gdrom_command_interrupt, |
Michael Opdenacker | 644ff18 | 2013-12-03 19:22:17 -0700 | [diff] [blame] | 548 | 0, "gdrom_command", &gd); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 549 | if (err) |
| 550 | return err; |
| 551 | err = request_irq(HW_EVENT_GDROM_DMA, gdrom_dma_interrupt, |
Michael Opdenacker | 644ff18 | 2013-12-03 19:22:17 -0700 | [diff] [blame] | 552 | 0, "gdrom_dma", &gd); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 553 | if (err) |
| 554 | free_irq(HW_EVENT_GDROM_CMD, &gd); |
| 555 | return err; |
| 556 | } |
| 557 | |
| 558 | /* Implement DMA read using SPI command |
| 559 | * 0 -> 0x30 |
| 560 | * 1 -> mode |
| 561 | * 2 -> block >> 16 |
| 562 | * 3 -> block >> 8 |
| 563 | * 4 -> block |
| 564 | * 8 -> sectors >> 16 |
| 565 | * 9 -> sectors >> 8 |
| 566 | * 10 -> sectors |
| 567 | */ |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 568 | static blk_status_t gdrom_readdisk_dma(struct request *req) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 569 | { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 570 | int block, block_cnt; |
| 571 | blk_status_t err; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 572 | struct packet_command *read_command; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 573 | unsigned long timeout; |
| 574 | |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 575 | read_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL); |
| 576 | if (!read_command) |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 577 | return BLK_STS_RESOURCE; |
| 578 | |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 579 | read_command->cmd[0] = 0x30; |
| 580 | read_command->cmd[1] = 0x20; |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 581 | block = blk_rq_pos(req)/GD_TO_BLK + GD_SESSION_OFFSET; |
| 582 | block_cnt = blk_rq_sectors(req)/GD_TO_BLK; |
| 583 | __raw_writel(virt_to_phys(bio_data(req->bio)), GDROM_DMA_STARTADDR_REG); |
| 584 | __raw_writel(block_cnt * GDROM_HARD_SECTOR, GDROM_DMA_LENGTH_REG); |
| 585 | __raw_writel(1, GDROM_DMA_DIRECTION_REG); |
| 586 | __raw_writel(1, GDROM_DMA_ENABLE_REG); |
| 587 | read_command->cmd[2] = (block >> 16) & 0xFF; |
| 588 | read_command->cmd[3] = (block >> 8) & 0xFF; |
| 589 | read_command->cmd[4] = block & 0xFF; |
| 590 | read_command->cmd[8] = (block_cnt >> 16) & 0xFF; |
| 591 | read_command->cmd[9] = (block_cnt >> 8) & 0xFF; |
| 592 | read_command->cmd[10] = block_cnt & 0xFF; |
| 593 | /* set for DMA */ |
| 594 | __raw_writeb(1, GDROM_ERROR_REG); |
| 595 | /* other registers */ |
| 596 | __raw_writeb(0, GDROM_SECNUM_REG); |
| 597 | __raw_writeb(0, GDROM_BCL_REG); |
| 598 | __raw_writeb(0, GDROM_BCH_REG); |
| 599 | __raw_writeb(0, GDROM_DSEL_REG); |
| 600 | __raw_writeb(0, GDROM_INTSEC_REG); |
| 601 | /* Wait for registers to reset after any previous activity */ |
| 602 | timeout = jiffies + HZ / 2; |
| 603 | while (gdrom_is_busy() && time_before(jiffies, timeout)) |
| 604 | cpu_relax(); |
| 605 | __raw_writeb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG); |
| 606 | timeout = jiffies + HZ / 2; |
| 607 | /* Wait for packet command to finish */ |
| 608 | while (gdrom_is_busy() && time_before(jiffies, timeout)) |
| 609 | cpu_relax(); |
| 610 | gd.pending = 1; |
| 611 | gd.transfer = 1; |
| 612 | outsw(GDROM_DATA_REG, &read_command->cmd, 6); |
| 613 | timeout = jiffies + HZ / 2; |
| 614 | /* Wait for any pending DMA to finish */ |
| 615 | while (__raw_readb(GDROM_DMA_STATUS_REG) && |
| 616 | time_before(jiffies, timeout)) |
| 617 | cpu_relax(); |
| 618 | /* start transfer */ |
| 619 | __raw_writeb(1, GDROM_DMA_STATUS_REG); |
| 620 | wait_event_interruptible_timeout(request_queue, |
| 621 | gd.transfer == 0, GDROM_DEFAULT_TIMEOUT); |
| 622 | err = gd.transfer ? BLK_STS_IOERR : BLK_STS_OK; |
| 623 | gd.transfer = 0; |
| 624 | gd.pending = 0; |
| 625 | |
| 626 | blk_mq_end_request(req, err); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 627 | kfree(read_command); |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 628 | return BLK_STS_OK; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 629 | } |
| 630 | |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 631 | static blk_status_t gdrom_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 632 | const struct blk_mq_queue_data *bd) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 633 | { |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 634 | blk_mq_start_request(bd->rq); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 635 | |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 636 | switch (req_op(bd->rq)) { |
| 637 | case REQ_OP_READ: |
| 638 | return gdrom_readdisk_dma(bd->rq); |
| 639 | case REQ_OP_WRITE: |
| 640 | pr_notice("Read only device - write request ignored\n"); |
| 641 | return BLK_STS_IOERR; |
| 642 | default: |
| 643 | printk(KERN_DEBUG "gdrom: Non-fs request ignored\n"); |
| 644 | return BLK_STS_IOERR; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
| 648 | /* Print string identifying GD ROM device */ |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 649 | static int gdrom_outputversion(void) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 650 | { |
| 651 | struct gdrom_id *id; |
| 652 | char *model_name, *manuf_name, *firmw_ver; |
| 653 | int err = -ENOMEM; |
| 654 | |
| 655 | /* query device ID */ |
| 656 | id = kzalloc(sizeof(struct gdrom_id), GFP_KERNEL); |
| 657 | if (!id) |
| 658 | return err; |
| 659 | gdrom_identifydevice(id); |
| 660 | model_name = kstrndup(id->modname, 16, GFP_KERNEL); |
| 661 | if (!model_name) |
| 662 | goto free_id; |
| 663 | manuf_name = kstrndup(id->mname, 16, GFP_KERNEL); |
| 664 | if (!manuf_name) |
| 665 | goto free_model_name; |
| 666 | firmw_ver = kstrndup(id->firmver, 16, GFP_KERNEL); |
| 667 | if (!firmw_ver) |
| 668 | goto free_manuf_name; |
Joe Perches | e597cd0 | 2010-07-01 08:24:32 +0200 | [diff] [blame] | 669 | pr_info("%s from %s with firmware %s\n", |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 670 | model_name, manuf_name, firmw_ver); |
| 671 | err = 0; |
| 672 | kfree(firmw_ver); |
| 673 | free_manuf_name: |
| 674 | kfree(manuf_name); |
| 675 | free_model_name: |
| 676 | kfree(model_name); |
| 677 | free_id: |
| 678 | kfree(id); |
| 679 | return err; |
| 680 | } |
| 681 | |
| 682 | /* set the default mode for DMA transfer */ |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 683 | static int gdrom_init_dma_mode(void) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 684 | { |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 685 | __raw_writeb(0x13, GDROM_ERROR_REG); |
| 686 | __raw_writeb(0x22, GDROM_INTSEC_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 687 | if (!gdrom_wait_clrbusy()) |
| 688 | return -EBUSY; |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 689 | __raw_writeb(0xEF, GDROM_STATUSCOMMAND_REG); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 690 | if (!gdrom_wait_busy_sleeps()) |
| 691 | return -EBUSY; |
| 692 | /* Memory protection setting for GDROM DMA |
| 693 | * Bits 31 - 16 security: 0x8843 |
| 694 | * Bits 15 and 7 reserved (0) |
| 695 | * Bits 14 - 8 start of transfer range in 1 MB blocks OR'ed with 0x80 |
| 696 | * Bits 6 - 0 end of transfer range in 1 MB blocks OR'ed with 0x80 |
| 697 | * (0x40 | 0x80) = start range at 0x0C000000 |
| 698 | * (0x7F | 0x80) = end range at 0x0FFFFFFF */ |
Paul Mundt | 9eb79bb | 2010-10-27 14:33:39 +0900 | [diff] [blame] | 699 | __raw_writel(0x8843407F, GDROM_DMA_ACCESS_CTRL_REG); |
| 700 | __raw_writel(9, GDROM_DMA_WAIT_REG); /* DMA word setting */ |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 701 | return 0; |
| 702 | } |
| 703 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 704 | static void probe_gdrom_setupcd(void) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 705 | { |
| 706 | gd.cd_info->ops = &gdrom_ops; |
| 707 | gd.cd_info->capacity = 1; |
| 708 | strcpy(gd.cd_info->name, GDROM_DEV_NAME); |
| 709 | gd.cd_info->mask = CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK| |
| 710 | CDC_SELECT_DISC; |
| 711 | } |
| 712 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 713 | static void probe_gdrom_setupdisk(void) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 714 | { |
| 715 | gd.disk->major = gdrom_major; |
| 716 | gd.disk->first_minor = 1; |
| 717 | gd.disk->minors = 1; |
| 718 | strcpy(gd.disk->disk_name, GDROM_DEV_NAME); |
| 719 | } |
| 720 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 721 | static int probe_gdrom_setupqueue(void) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 722 | { |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 723 | blk_queue_logical_block_size(gd.gdrom_rq, GDROM_HARD_SECTOR); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 724 | /* using DMA so memory will need to be contiguous */ |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 725 | blk_queue_max_segments(gd.gdrom_rq, 1); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 726 | /* set a large max size to get most from DMA */ |
| 727 | blk_queue_max_segment_size(gd.gdrom_rq, 0x40000); |
| 728 | gd.disk->queue = gd.gdrom_rq; |
| 729 | return gdrom_init_dma_mode(); |
| 730 | } |
| 731 | |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 732 | static const struct blk_mq_ops gdrom_mq_ops = { |
| 733 | .queue_rq = gdrom_queue_rq, |
| 734 | }; |
| 735 | |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 736 | /* |
| 737 | * register this as a block device and as compliant with the |
| 738 | * universal CD Rom driver interface |
| 739 | */ |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 740 | static int probe_gdrom(struct platform_device *devptr) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 741 | { |
| 742 | int err; |
| 743 | /* Start the device */ |
| 744 | if (gdrom_execute_diagnostic() != 1) { |
Kefeng Wang | f8b4899 | 2019-10-18 11:18:27 +0800 | [diff] [blame] | 745 | pr_warn("ATA Probe for GDROM failed\n"); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 746 | return -ENODEV; |
| 747 | } |
| 748 | /* Print out firmware ID */ |
| 749 | if (gdrom_outputversion()) |
| 750 | return -ENOMEM; |
| 751 | /* Register GDROM */ |
| 752 | gdrom_major = register_blkdev(0, GDROM_DEV_NAME); |
| 753 | if (gdrom_major <= 0) |
| 754 | return gdrom_major; |
Joe Perches | e597cd0 | 2010-07-01 08:24:32 +0200 | [diff] [blame] | 755 | pr_info("Registered with major number %d\n", |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 756 | gdrom_major); |
| 757 | /* Specify basic properties of drive */ |
| 758 | gd.cd_info = kzalloc(sizeof(struct cdrom_device_info), GFP_KERNEL); |
| 759 | if (!gd.cd_info) { |
| 760 | err = -ENOMEM; |
| 761 | goto probe_fail_no_mem; |
| 762 | } |
| 763 | probe_gdrom_setupcd(); |
| 764 | gd.disk = alloc_disk(1); |
| 765 | if (!gd.disk) { |
| 766 | err = -ENODEV; |
| 767 | goto probe_fail_no_disk; |
| 768 | } |
| 769 | probe_gdrom_setupdisk(); |
| 770 | if (register_cdrom(gd.cd_info)) { |
| 771 | err = -ENODEV; |
| 772 | goto probe_fail_cdrom_register; |
| 773 | } |
| 774 | gd.disk->fops = &gdrom_bdops; |
Martin Wilck | 773008f | 2019-03-27 14:51:04 +0100 | [diff] [blame] | 775 | gd.disk->events = DISK_EVENT_MEDIA_CHANGE; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 776 | /* latch on to the interrupt */ |
| 777 | err = gdrom_set_interrupt_handlers(); |
| 778 | if (err) |
| 779 | goto probe_fail_cmdirq_register; |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 780 | |
| 781 | gd.gdrom_rq = blk_mq_init_sq_queue(&gd.tag_set, &gdrom_mq_ops, 1, |
| 782 | BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING); |
| 783 | if (IS_ERR(gd.gdrom_rq)) { |
Jens Axboe | 0a40a93 | 2018-10-25 08:15:30 -0600 | [diff] [blame] | 784 | err = PTR_ERR(gd.gdrom_rq); |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 785 | gd.gdrom_rq = NULL; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 786 | goto probe_fail_requestq; |
Christophe JAILLET | 605cdf0 | 2017-02-06 23:00:13 +0100 | [diff] [blame] | 787 | } |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 788 | |
Christoph Hellwig | 8fc4504 | 2017-06-19 09:26:26 +0200 | [diff] [blame] | 789 | blk_queue_bounce_limit(gd.gdrom_rq, BLK_BOUNCE_HIGH); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 790 | |
| 791 | err = probe_gdrom_setupqueue(); |
| 792 | if (err) |
| 793 | goto probe_fail_toc; |
| 794 | |
| 795 | gd.toc = kzalloc(sizeof(struct gdromtoc), GFP_KERNEL); |
Christophe JAILLET | 605cdf0 | 2017-02-06 23:00:13 +0100 | [diff] [blame] | 796 | if (!gd.toc) { |
| 797 | err = -ENOMEM; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 798 | goto probe_fail_toc; |
Christophe JAILLET | 605cdf0 | 2017-02-06 23:00:13 +0100 | [diff] [blame] | 799 | } |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 800 | add_disk(gd.disk); |
| 801 | return 0; |
| 802 | |
| 803 | probe_fail_toc: |
| 804 | blk_cleanup_queue(gd.gdrom_rq); |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 805 | blk_mq_free_tag_set(&gd.tag_set); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 806 | probe_fail_requestq: |
| 807 | free_irq(HW_EVENT_GDROM_DMA, &gd); |
| 808 | free_irq(HW_EVENT_GDROM_CMD, &gd); |
| 809 | probe_fail_cmdirq_register: |
| 810 | probe_fail_cdrom_register: |
| 811 | del_gendisk(gd.disk); |
| 812 | probe_fail_no_disk: |
| 813 | kfree(gd.cd_info); |
Libo Chen | 31bd8fb | 2013-07-03 15:01:11 -0700 | [diff] [blame] | 814 | probe_fail_no_mem: |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 815 | unregister_blkdev(gdrom_major, GDROM_DEV_NAME); |
| 816 | gdrom_major = 0; |
Kefeng Wang | f8b4899 | 2019-10-18 11:18:27 +0800 | [diff] [blame] | 817 | pr_warn("Probe failed - error is 0x%X\n", err); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 818 | return err; |
| 819 | } |
| 820 | |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 821 | static int remove_gdrom(struct platform_device *devptr) |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 822 | { |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 823 | blk_cleanup_queue(gd.gdrom_rq); |
Jens Axboe | ad5fc6b | 2018-10-15 09:01:40 -0600 | [diff] [blame] | 824 | blk_mq_free_tag_set(&gd.tag_set); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 825 | free_irq(HW_EVENT_GDROM_CMD, &gd); |
| 826 | free_irq(HW_EVENT_GDROM_DMA, &gd); |
| 827 | del_gendisk(gd.disk); |
| 828 | if (gdrom_major) |
| 829 | unregister_blkdev(gdrom_major, GDROM_DEV_NAME); |
Akinobu Mita | 0a0c411 | 2008-03-26 12:09:02 +0100 | [diff] [blame] | 830 | unregister_cdrom(gd.cd_info); |
| 831 | |
| 832 | return 0; |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | static struct platform_driver gdrom_driver = { |
| 836 | .probe = probe_gdrom, |
Greg Kroah-Hartman | 0fe763c | 2012-12-21 15:14:44 -0800 | [diff] [blame] | 837 | .remove = remove_gdrom, |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 838 | .driver = { |
| 839 | .name = GDROM_DEV_NAME, |
| 840 | }, |
| 841 | }; |
| 842 | |
| 843 | static int __init init_gdrom(void) |
| 844 | { |
| 845 | int rc; |
| 846 | gd.toc = NULL; |
| 847 | rc = platform_driver_register(&gdrom_driver); |
| 848 | if (rc) |
| 849 | return rc; |
| 850 | pd = platform_device_register_simple(GDROM_DEV_NAME, -1, NULL, 0); |
| 851 | if (IS_ERR(pd)) { |
| 852 | platform_driver_unregister(&gdrom_driver); |
| 853 | return PTR_ERR(pd); |
| 854 | } |
| 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | static void __exit exit_gdrom(void) |
| 859 | { |
| 860 | platform_device_unregister(pd); |
| 861 | platform_driver_unregister(&gdrom_driver); |
| 862 | kfree(gd.toc); |
Wenwen Wang | 093c482 | 2018-12-26 20:15:13 -0600 | [diff] [blame] | 863 | kfree(gd.cd_info); |
Adrian McMenamin | 74ee1a7 | 2008-01-21 18:52:48 +0900 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | module_init(init_gdrom); |
| 867 | module_exit(exit_gdrom); |
| 868 | MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>"); |
| 869 | MODULE_DESCRIPTION("SEGA Dreamcast GD-ROM Driver"); |
| 870 | MODULE_LICENSE("GPL"); |