blob: 5cf3bade0d576e6f7c89fec78c9f7f4d70d4ef91 [file] [log] [blame]
Adrian McMenamin74ee1a72008-01-21 18:52:48 +09001/* GD ROM driver for the SEGA Dreamcast
2 * copyright Adrian McMenamin, 2007
3 * With thanks to Marcus Comstedt and Nathan Keynes
4 * for work in reversing PIO and DMA
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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
Joe Perchese597cd02010-07-01 08:24:32 +020022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Adrian McMenamin74ee1a72008-01-21 18:52:48 +090024#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/fs.h>
27#include <linux/kernel.h>
28#include <linux/list.h>
29#include <linux/slab.h>
30#include <linux/dma-mapping.h>
31#include <linux/cdrom.h>
32#include <linux/genhd.h>
33#include <linux/bio.h>
Jens Axboead5fc6b2018-10-15 09:01:40 -060034#include <linux/blk-mq.h>
Adrian McMenamin74ee1a72008-01-21 18:52:48 +090035#include <linux/interrupt.h>
36#include <linux/device.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020037#include <linux/mutex.h>
Adrian McMenamin74ee1a72008-01-21 18:52:48 +090038#include <linux/wait.h>
Adrian McMenamin74ee1a72008-01-21 18:52:48 +090039#include <linux/platform_device.h>
40#include <scsi/scsi.h>
41#include <asm/io.h>
42#include <asm/dma.h>
43#include <asm/delay.h>
Paul Mundt0764bff2008-07-29 22:10:01 +090044#include <mach/dma.h>
45#include <mach/sysasic.h>
Adrian McMenamin74ee1a72008-01-21 18:52:48 +090046
47#define GDROM_DEV_NAME "gdrom"
48#define GD_SESSION_OFFSET 150
49
50/* GD Rom commands */
51#define GDROM_COM_SOFTRESET 0x08
52#define GDROM_COM_EXECDIAG 0x90
53#define GDROM_COM_PACKET 0xA0
54#define GDROM_COM_IDDEV 0xA1
55
56/* GD Rom registers */
57#define GDROM_BASE_REG 0xA05F7000
58#define GDROM_ALTSTATUS_REG (GDROM_BASE_REG + 0x18)
59#define GDROM_DATA_REG (GDROM_BASE_REG + 0x80)
60#define GDROM_ERROR_REG (GDROM_BASE_REG + 0x84)
61#define GDROM_INTSEC_REG (GDROM_BASE_REG + 0x88)
62#define GDROM_SECNUM_REG (GDROM_BASE_REG + 0x8C)
63#define GDROM_BCL_REG (GDROM_BASE_REG + 0x90)
64#define GDROM_BCH_REG (GDROM_BASE_REG + 0x94)
65#define GDROM_DSEL_REG (GDROM_BASE_REG + 0x98)
66#define GDROM_STATUSCOMMAND_REG (GDROM_BASE_REG + 0x9C)
67#define GDROM_RESET_REG (GDROM_BASE_REG + 0x4E4)
68
69#define GDROM_DMA_STARTADDR_REG (GDROM_BASE_REG + 0x404)
70#define GDROM_DMA_LENGTH_REG (GDROM_BASE_REG + 0x408)
71#define GDROM_DMA_DIRECTION_REG (GDROM_BASE_REG + 0x40C)
72#define GDROM_DMA_ENABLE_REG (GDROM_BASE_REG + 0x414)
73#define GDROM_DMA_STATUS_REG (GDROM_BASE_REG + 0x418)
74#define GDROM_DMA_WAIT_REG (GDROM_BASE_REG + 0x4A0)
75#define GDROM_DMA_ACCESS_CTRL_REG (GDROM_BASE_REG + 0x4B8)
76
77#define GDROM_HARD_SECTOR 2048
78#define BLOCK_LAYER_SECTOR 512
79#define GD_TO_BLK 4
80
81#define GDROM_DEFAULT_TIMEOUT (HZ * 7)
82
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020083static DEFINE_MUTEX(gdrom_mutex);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +090084static const struct {
85 int sense_key;
86 const char * const text;
87} sense_texts[] = {
88 {NO_SENSE, "OK"},
89 {RECOVERED_ERROR, "Recovered from error"},
90 {NOT_READY, "Device not ready"},
91 {MEDIUM_ERROR, "Disk not ready"},
92 {HARDWARE_ERROR, "Hardware error"},
93 {ILLEGAL_REQUEST, "Command has failed"},
94 {UNIT_ATTENTION, "Device needs attention - disk may have been changed"},
95 {DATA_PROTECT, "Data protection error"},
96 {ABORTED_COMMAND, "Command aborted"},
97};
98
99static struct platform_device *pd;
100static int gdrom_major;
101static DECLARE_WAIT_QUEUE_HEAD(command_queue);
102static DECLARE_WAIT_QUEUE_HEAD(request_queue);
103
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900104struct gdromtoc {
105 unsigned int entry[99];
106 unsigned int first, last;
107 unsigned int leadout;
108};
109
110static struct gdrom_unit {
111 struct gendisk *disk;
112 struct cdrom_device_info *cd_info;
113 int status;
114 int pending;
115 int transfer;
116 char disk_type;
117 struct gdromtoc *toc;
118 struct request_queue *gdrom_rq;
Jens Axboead5fc6b2018-10-15 09:01:40 -0600119 struct blk_mq_tag_set tag_set;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900120} gd;
121
122struct gdrom_id {
123 char mid;
124 char modid;
125 char verid;
126 char padA[13];
127 char mname[16];
128 char modname[16];
129 char firmver[16];
130 char padB[16];
131};
132
133static int gdrom_getsense(short *bufstring);
134static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
135 struct packet_command *command);
136static int gdrom_hardreset(struct cdrom_device_info *cd_info);
137
138static bool gdrom_is_busy(void)
139{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900140 return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) != 0;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900141}
142
143static bool gdrom_data_request(void)
144{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900145 return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x88) == 8;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900146}
147
148static bool gdrom_wait_clrbusy(void)
149{
150 unsigned long timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900151 while ((__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) &&
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900152 (time_before(jiffies, timeout)))
153 cpu_relax();
154 return time_before(jiffies, timeout + 1);
155}
156
157static bool gdrom_wait_busy_sleeps(void)
158{
159 unsigned long timeout;
160 /* Wait to get busy first */
161 timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
162 while (!gdrom_is_busy() && time_before(jiffies, timeout))
163 cpu_relax();
164 /* Now wait for busy to clear */
165 return gdrom_wait_clrbusy();
166}
167
168static void gdrom_identifydevice(void *buf)
169{
170 int c;
171 short *data = buf;
172 /* If the device won't clear it has probably
173 * been hit by a serious failure - but we'll
174 * try to return a sense key even so */
175 if (!gdrom_wait_clrbusy()) {
176 gdrom_getsense(NULL);
177 return;
178 }
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900179 __raw_writeb(GDROM_COM_IDDEV, GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900180 if (!gdrom_wait_busy_sleeps()) {
181 gdrom_getsense(NULL);
182 return;
183 }
184 /* now read in the data */
185 for (c = 0; c < 40; c++)
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900186 data[c] = __raw_readw(GDROM_DATA_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900187}
188
189static void gdrom_spicommand(void *spi_string, int buflen)
190{
191 short *cmd = spi_string;
192 unsigned long timeout;
193
194 /* ensure IRQ_WAIT is set */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900195 __raw_writeb(0x08, GDROM_ALTSTATUS_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900196 /* specify how many bytes we expect back */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900197 __raw_writeb(buflen & 0xFF, GDROM_BCL_REG);
198 __raw_writeb((buflen >> 8) & 0xFF, GDROM_BCH_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900199 /* other parameters */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900200 __raw_writeb(0, GDROM_INTSEC_REG);
201 __raw_writeb(0, GDROM_SECNUM_REG);
202 __raw_writeb(0, GDROM_ERROR_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900203 /* Wait until we can go */
204 if (!gdrom_wait_clrbusy()) {
205 gdrom_getsense(NULL);
206 return;
207 }
208 timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900209 __raw_writeb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900210 while (!gdrom_data_request() && time_before(jiffies, timeout))
211 cpu_relax();
212 if (!time_before(jiffies, timeout + 1)) {
213 gdrom_getsense(NULL);
214 return;
215 }
Paul Mundt54d51022009-10-13 12:30:40 +0900216 outsw(GDROM_DATA_REG, cmd, 6);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900217}
218
219
220/* gdrom_command_executediagnostic:
221 * Used to probe for presence of working GDROM
222 * Restarts GDROM device and then applies standard ATA 3
223 * Execute Diagnostic Command: a return of '1' indicates device 0
224 * present and device 1 absent
225 */
226static char gdrom_execute_diagnostic(void)
227{
228 gdrom_hardreset(gd.cd_info);
229 if (!gdrom_wait_clrbusy())
230 return 0;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900231 __raw_writeb(GDROM_COM_EXECDIAG, GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900232 if (!gdrom_wait_busy_sleeps())
233 return 0;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900234 return __raw_readb(GDROM_ERROR_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900235}
236
237/*
238 * Prepare disk command
239 * byte 0 = 0x70
240 * byte 1 = 0x1f
241 */
242static int gdrom_preparedisk_cmd(void)
243{
244 struct packet_command *spin_command;
245 spin_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
246 if (!spin_command)
247 return -ENOMEM;
248 spin_command->cmd[0] = 0x70;
249 spin_command->cmd[2] = 0x1f;
250 spin_command->buflen = 0;
251 gd.pending = 1;
252 gdrom_packetcommand(gd.cd_info, spin_command);
253 /* 60 second timeout */
254 wait_event_interruptible_timeout(command_queue, gd.pending == 0,
255 GDROM_DEFAULT_TIMEOUT);
256 gd.pending = 0;
257 kfree(spin_command);
258 if (gd.status & 0x01) {
259 /* log an error */
260 gdrom_getsense(NULL);
261 return -EIO;
262 }
263 return 0;
264}
265
266/*
267 * Read TOC command
268 * byte 0 = 0x14
269 * byte 1 = session
270 * byte 3 = sizeof TOC >> 8 ie upper byte
271 * byte 4 = sizeof TOC & 0xff ie lower byte
272 */
273static int gdrom_readtoc_cmd(struct gdromtoc *toc, int session)
274{
275 int tocsize;
276 struct packet_command *toc_command;
277 int err = 0;
278
279 toc_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
280 if (!toc_command)
281 return -ENOMEM;
282 tocsize = sizeof(struct gdromtoc);
283 toc_command->cmd[0] = 0x14;
284 toc_command->cmd[1] = session;
285 toc_command->cmd[3] = tocsize >> 8;
286 toc_command->cmd[4] = tocsize & 0xff;
287 toc_command->buflen = tocsize;
288 if (gd.pending) {
289 err = -EBUSY;
290 goto cleanup_readtoc_final;
291 }
292 gd.pending = 1;
293 gdrom_packetcommand(gd.cd_info, toc_command);
294 wait_event_interruptible_timeout(command_queue, gd.pending == 0,
295 GDROM_DEFAULT_TIMEOUT);
296 if (gd.pending) {
297 err = -EINVAL;
298 goto cleanup_readtoc;
299 }
Paul Mundt54d51022009-10-13 12:30:40 +0900300 insw(GDROM_DATA_REG, toc, tocsize/2);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900301 if (gd.status & 0x01)
302 err = -EINVAL;
303
304cleanup_readtoc:
305 gd.pending = 0;
306cleanup_readtoc_final:
307 kfree(toc_command);
308 return err;
309}
310
311/* TOC helpers */
312static int get_entry_lba(int track)
313{
314 return (cpu_to_be32(track & 0xffffff00) - GD_SESSION_OFFSET);
315}
316
317static int get_entry_q_ctrl(int track)
318{
319 return (track & 0x000000f0) >> 4;
320}
321
322static int get_entry_track(int track)
323{
324 return (track & 0x0000ff00) >> 8;
325}
326
327static int gdrom_get_last_session(struct cdrom_device_info *cd_info,
328 struct cdrom_multisession *ms_info)
329{
zhong jiangcdffab72018-10-23 20:06:37 +0800330 int fentry, lentry, track, data, err;
331
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900332 if (!gd.toc)
333 return -ENOMEM;
zhong jiangcdffab72018-10-23 20:06:37 +0800334
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900335 /* Check if GD-ROM */
336 err = gdrom_readtoc_cmd(gd.toc, 1);
337 /* Not a GD-ROM so check if standard CD-ROM */
338 if (err) {
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900339 err = gdrom_readtoc_cmd(gd.toc, 0);
340 if (err) {
Joe Perchese597cd02010-07-01 08:24:32 +0200341 pr_info("Could not get CD table of contents\n");
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900342 return -ENXIO;
343 }
344 }
345
346 fentry = get_entry_track(gd.toc->first);
347 lentry = get_entry_track(gd.toc->last);
348 /* Find the first data track */
349 track = get_entry_track(gd.toc->last);
350 do {
351 data = gd.toc->entry[track - 1];
352 if (get_entry_q_ctrl(data))
353 break; /* ie a real data track */
354 track--;
355 } while (track >= fentry);
356
357 if ((track > 100) || (track < get_entry_track(gd.toc->first))) {
Joe Perchese597cd02010-07-01 08:24:32 +0200358 pr_info("No data on the last session of the CD\n");
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900359 gdrom_getsense(NULL);
360 return -ENXIO;
361 }
362
363 ms_info->addr_format = CDROM_LBA;
364 ms_info->addr.lba = get_entry_lba(data);
365 ms_info->xa_flag = 1;
366 return 0;
367}
368
369static int gdrom_open(struct cdrom_device_info *cd_info, int purpose)
370{
371 /* spin up the disk */
372 return gdrom_preparedisk_cmd();
373}
374
375/* this function is required even if empty */
376static void gdrom_release(struct cdrom_device_info *cd_info)
377{
378}
379
380static int gdrom_drivestatus(struct cdrom_device_info *cd_info, int ignore)
381{
382 /* read the sense key */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900383 char sense = __raw_readb(GDROM_ERROR_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900384 sense &= 0xF0;
385 if (sense == 0)
386 return CDS_DISC_OK;
387 if (sense == 0x20)
388 return CDS_DRIVE_NOT_READY;
389 /* default */
390 return CDS_NO_INFO;
391}
392
Tejun Heo1c270302011-03-09 19:54:28 +0100393static unsigned int gdrom_check_events(struct cdrom_device_info *cd_info,
394 unsigned int clearing, int ignore)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900395{
396 /* check the sense key */
Tejun Heo1c270302011-03-09 19:54:28 +0100397 return (__raw_readb(GDROM_ERROR_REG) & 0xF0) == 0x60 ?
398 DISK_EVENT_MEDIA_CHANGE : 0;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900399}
400
401/* reset the G1 bus */
402static int gdrom_hardreset(struct cdrom_device_info *cd_info)
403{
404 int count;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900405 __raw_writel(0x1fffff, GDROM_RESET_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900406 for (count = 0xa0000000; count < 0xa0200000; count += 4)
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900407 __raw_readl(count);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900408 return 0;
409}
410
411/* keep the function looking like the universal
412 * CD Rom specification - returning int */
413static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
414 struct packet_command *command)
415{
416 gdrom_spicommand(&command->cmd, command->buflen);
417 return 0;
418}
419
420/* Get Sense SPI command
421 * From Marcus Comstedt
422 * cmd = 0x13
423 * cmd + 4 = length of returned buffer
424 * Returns 5 16 bit words
425 */
426static int gdrom_getsense(short *bufstring)
427{
428 struct packet_command *sense_command;
429 short sense[5];
430 int sense_key;
431 int err = -EIO;
432
433 sense_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
434 if (!sense_command)
435 return -ENOMEM;
436 sense_command->cmd[0] = 0x13;
437 sense_command->cmd[4] = 10;
438 sense_command->buflen = 10;
439 /* even if something is pending try to get
440 * the sense key if possible */
441 if (gd.pending && !gdrom_wait_clrbusy()) {
442 err = -EBUSY;
443 goto cleanup_sense_final;
444 }
445 gd.pending = 1;
446 gdrom_packetcommand(gd.cd_info, sense_command);
447 wait_event_interruptible_timeout(command_queue, gd.pending == 0,
448 GDROM_DEFAULT_TIMEOUT);
449 if (gd.pending)
450 goto cleanup_sense;
Paul Mundt54d51022009-10-13 12:30:40 +0900451 insw(GDROM_DATA_REG, &sense, sense_command->buflen/2);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900452 if (sense[1] & 40) {
Joe Perchese597cd02010-07-01 08:24:32 +0200453 pr_info("Drive not ready - command aborted\n");
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900454 goto cleanup_sense;
455 }
456 sense_key = sense[1] & 0x0F;
457 if (sense_key < ARRAY_SIZE(sense_texts))
Joe Perchese597cd02010-07-01 08:24:32 +0200458 pr_info("%s\n", sense_texts[sense_key].text);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900459 else
Joe Perchese597cd02010-07-01 08:24:32 +0200460 pr_err("Unknown sense key: %d\n", sense_key);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900461 if (bufstring) /* return addional sense data */
462 memcpy(bufstring, &sense[4], 2);
463 if (sense_key < 2)
464 err = 0;
465
466cleanup_sense:
467 gd.pending = 0;
468cleanup_sense_final:
469 kfree(sense_command);
470 return err;
471}
472
Borislav Petkovd15cad52008-08-18 21:40:04 +0200473static int gdrom_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
474 void *arg)
475{
476 return -EINVAL;
477}
478
Kees Cook853fe1b2017-02-13 16:25:26 -0800479static const struct cdrom_device_ops gdrom_ops = {
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900480 .open = gdrom_open,
481 .release = gdrom_release,
482 .drive_status = gdrom_drivestatus,
Tejun Heo1c270302011-03-09 19:54:28 +0100483 .check_events = gdrom_check_events,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900484 .get_last_session = gdrom_get_last_session,
485 .reset = gdrom_hardreset,
Borislav Petkovd15cad52008-08-18 21:40:04 +0200486 .audio_ioctl = gdrom_audio_ioctl,
Kees Cook853fe1b2017-02-13 16:25:26 -0800487 .generic_packet = cdrom_dummy_generic_packet,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900488 .capability = CDC_MULTI_SESSION | CDC_MEDIA_CHANGED |
489 CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900490};
491
Al Viro8389feb2008-03-02 10:25:18 -0500492static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900493{
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200494 int ret;
Maurizio Lombardi2bbea6e2018-03-09 13:59:06 +0100495
496 check_disk_change(bdev);
497
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200498 mutex_lock(&gdrom_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200499 ret = cdrom_open(gd.cd_info, bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200500 mutex_unlock(&gdrom_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200501 return ret;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900502}
503
Al Virodb2a1442013-05-05 21:52:57 -0400504static void gdrom_bdops_release(struct gendisk *disk, fmode_t mode)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900505{
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200506 mutex_lock(&gdrom_mutex);
Nobuhiro Iwamatsu545727f2008-10-27 11:32:27 +0900507 cdrom_release(gd.cd_info, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200508 mutex_unlock(&gdrom_mutex);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900509}
510
Tejun Heo1c270302011-03-09 19:54:28 +0100511static unsigned int gdrom_bdops_check_events(struct gendisk *disk,
512 unsigned int clearing)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900513{
Tejun Heo1c270302011-03-09 19:54:28 +0100514 return cdrom_check_events(gd.cd_info, clearing);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900515}
516
Al Viro8389feb2008-03-02 10:25:18 -0500517static int gdrom_bdops_ioctl(struct block_device *bdev, fmode_t mode,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900518 unsigned cmd, unsigned long arg)
519{
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200520 int ret;
521
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200522 mutex_lock(&gdrom_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200523 ret = cdrom_ioctl(gd.cd_info, bdev, mode, cmd, arg);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200524 mutex_unlock(&gdrom_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200525
526 return ret;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900527}
528
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700529static const struct block_device_operations gdrom_bdops = {
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900530 .owner = THIS_MODULE,
Al Viro8389feb2008-03-02 10:25:18 -0500531 .open = gdrom_bdops_open,
532 .release = gdrom_bdops_release,
Tejun Heo1c270302011-03-09 19:54:28 +0100533 .check_events = gdrom_bdops_check_events,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200534 .ioctl = gdrom_bdops_ioctl,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900535};
536
537static irqreturn_t gdrom_command_interrupt(int irq, void *dev_id)
538{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900539 gd.status = __raw_readb(GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900540 if (gd.pending != 1)
541 return IRQ_HANDLED;
542 gd.pending = 0;
543 wake_up_interruptible(&command_queue);
544 return IRQ_HANDLED;
545}
546
547static irqreturn_t gdrom_dma_interrupt(int irq, void *dev_id)
548{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900549 gd.status = __raw_readb(GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900550 if (gd.transfer != 1)
551 return IRQ_HANDLED;
552 gd.transfer = 0;
553 wake_up_interruptible(&request_queue);
554 return IRQ_HANDLED;
555}
556
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800557static int gdrom_set_interrupt_handlers(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900558{
559 int err;
560
561 err = request_irq(HW_EVENT_GDROM_CMD, gdrom_command_interrupt,
Michael Opdenacker644ff182013-12-03 19:22:17 -0700562 0, "gdrom_command", &gd);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900563 if (err)
564 return err;
565 err = request_irq(HW_EVENT_GDROM_DMA, gdrom_dma_interrupt,
Michael Opdenacker644ff182013-12-03 19:22:17 -0700566 0, "gdrom_dma", &gd);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900567 if (err)
568 free_irq(HW_EVENT_GDROM_CMD, &gd);
569 return err;
570}
571
572/* Implement DMA read using SPI command
573 * 0 -> 0x30
574 * 1 -> mode
575 * 2 -> block >> 16
576 * 3 -> block >> 8
577 * 4 -> block
578 * 8 -> sectors >> 16
579 * 9 -> sectors >> 8
580 * 10 -> sectors
581 */
Jens Axboead5fc6b2018-10-15 09:01:40 -0600582static blk_status_t gdrom_readdisk_dma(struct request *req)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900583{
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200584 int block, block_cnt;
585 blk_status_t err;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900586 struct packet_command *read_command;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900587 unsigned long timeout;
588
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900589 read_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
590 if (!read_command)
Jens Axboead5fc6b2018-10-15 09:01:40 -0600591 return BLK_STS_RESOURCE;
592
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900593 read_command->cmd[0] = 0x30;
594 read_command->cmd[1] = 0x20;
Jens Axboead5fc6b2018-10-15 09:01:40 -0600595 block = blk_rq_pos(req)/GD_TO_BLK + GD_SESSION_OFFSET;
596 block_cnt = blk_rq_sectors(req)/GD_TO_BLK;
597 __raw_writel(virt_to_phys(bio_data(req->bio)), GDROM_DMA_STARTADDR_REG);
598 __raw_writel(block_cnt * GDROM_HARD_SECTOR, GDROM_DMA_LENGTH_REG);
599 __raw_writel(1, GDROM_DMA_DIRECTION_REG);
600 __raw_writel(1, GDROM_DMA_ENABLE_REG);
601 read_command->cmd[2] = (block >> 16) & 0xFF;
602 read_command->cmd[3] = (block >> 8) & 0xFF;
603 read_command->cmd[4] = block & 0xFF;
604 read_command->cmd[8] = (block_cnt >> 16) & 0xFF;
605 read_command->cmd[9] = (block_cnt >> 8) & 0xFF;
606 read_command->cmd[10] = block_cnt & 0xFF;
607 /* set for DMA */
608 __raw_writeb(1, GDROM_ERROR_REG);
609 /* other registers */
610 __raw_writeb(0, GDROM_SECNUM_REG);
611 __raw_writeb(0, GDROM_BCL_REG);
612 __raw_writeb(0, GDROM_BCH_REG);
613 __raw_writeb(0, GDROM_DSEL_REG);
614 __raw_writeb(0, GDROM_INTSEC_REG);
615 /* Wait for registers to reset after any previous activity */
616 timeout = jiffies + HZ / 2;
617 while (gdrom_is_busy() && time_before(jiffies, timeout))
618 cpu_relax();
619 __raw_writeb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
620 timeout = jiffies + HZ / 2;
621 /* Wait for packet command to finish */
622 while (gdrom_is_busy() && time_before(jiffies, timeout))
623 cpu_relax();
624 gd.pending = 1;
625 gd.transfer = 1;
626 outsw(GDROM_DATA_REG, &read_command->cmd, 6);
627 timeout = jiffies + HZ / 2;
628 /* Wait for any pending DMA to finish */
629 while (__raw_readb(GDROM_DMA_STATUS_REG) &&
630 time_before(jiffies, timeout))
631 cpu_relax();
632 /* start transfer */
633 __raw_writeb(1, GDROM_DMA_STATUS_REG);
634 wait_event_interruptible_timeout(request_queue,
635 gd.transfer == 0, GDROM_DEFAULT_TIMEOUT);
636 err = gd.transfer ? BLK_STS_IOERR : BLK_STS_OK;
637 gd.transfer = 0;
638 gd.pending = 0;
639
640 blk_mq_end_request(req, err);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900641 kfree(read_command);
Jens Axboead5fc6b2018-10-15 09:01:40 -0600642 return BLK_STS_OK;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900643}
644
Jens Axboead5fc6b2018-10-15 09:01:40 -0600645static blk_status_t gdrom_queue_rq(struct blk_mq_hw_ctx *hctx,
646 const struct blk_mq_queue_data *bd)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900647{
Jens Axboead5fc6b2018-10-15 09:01:40 -0600648 blk_mq_start_request(bd->rq);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900649
Jens Axboead5fc6b2018-10-15 09:01:40 -0600650 switch (req_op(bd->rq)) {
651 case REQ_OP_READ:
652 return gdrom_readdisk_dma(bd->rq);
653 case REQ_OP_WRITE:
654 pr_notice("Read only device - write request ignored\n");
655 return BLK_STS_IOERR;
656 default:
657 printk(KERN_DEBUG "gdrom: Non-fs request ignored\n");
658 return BLK_STS_IOERR;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900659 }
660}
661
662/* Print string identifying GD ROM device */
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800663static int gdrom_outputversion(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900664{
665 struct gdrom_id *id;
666 char *model_name, *manuf_name, *firmw_ver;
667 int err = -ENOMEM;
668
669 /* query device ID */
670 id = kzalloc(sizeof(struct gdrom_id), GFP_KERNEL);
671 if (!id)
672 return err;
673 gdrom_identifydevice(id);
674 model_name = kstrndup(id->modname, 16, GFP_KERNEL);
675 if (!model_name)
676 goto free_id;
677 manuf_name = kstrndup(id->mname, 16, GFP_KERNEL);
678 if (!manuf_name)
679 goto free_model_name;
680 firmw_ver = kstrndup(id->firmver, 16, GFP_KERNEL);
681 if (!firmw_ver)
682 goto free_manuf_name;
Joe Perchese597cd02010-07-01 08:24:32 +0200683 pr_info("%s from %s with firmware %s\n",
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900684 model_name, manuf_name, firmw_ver);
685 err = 0;
686 kfree(firmw_ver);
687free_manuf_name:
688 kfree(manuf_name);
689free_model_name:
690 kfree(model_name);
691free_id:
692 kfree(id);
693 return err;
694}
695
696/* set the default mode for DMA transfer */
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800697static int gdrom_init_dma_mode(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900698{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900699 __raw_writeb(0x13, GDROM_ERROR_REG);
700 __raw_writeb(0x22, GDROM_INTSEC_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900701 if (!gdrom_wait_clrbusy())
702 return -EBUSY;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900703 __raw_writeb(0xEF, GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900704 if (!gdrom_wait_busy_sleeps())
705 return -EBUSY;
706 /* Memory protection setting for GDROM DMA
707 * Bits 31 - 16 security: 0x8843
708 * Bits 15 and 7 reserved (0)
709 * Bits 14 - 8 start of transfer range in 1 MB blocks OR'ed with 0x80
710 * Bits 6 - 0 end of transfer range in 1 MB blocks OR'ed with 0x80
711 * (0x40 | 0x80) = start range at 0x0C000000
712 * (0x7F | 0x80) = end range at 0x0FFFFFFF */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900713 __raw_writel(0x8843407F, GDROM_DMA_ACCESS_CTRL_REG);
714 __raw_writel(9, GDROM_DMA_WAIT_REG); /* DMA word setting */
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900715 return 0;
716}
717
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800718static void probe_gdrom_setupcd(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900719{
720 gd.cd_info->ops = &gdrom_ops;
721 gd.cd_info->capacity = 1;
722 strcpy(gd.cd_info->name, GDROM_DEV_NAME);
723 gd.cd_info->mask = CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|
724 CDC_SELECT_DISC;
725}
726
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800727static void probe_gdrom_setupdisk(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900728{
729 gd.disk->major = gdrom_major;
730 gd.disk->first_minor = 1;
731 gd.disk->minors = 1;
732 strcpy(gd.disk->disk_name, GDROM_DEV_NAME);
733}
734
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800735static int probe_gdrom_setupqueue(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900736{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400737 blk_queue_logical_block_size(gd.gdrom_rq, GDROM_HARD_SECTOR);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900738 /* using DMA so memory will need to be contiguous */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500739 blk_queue_max_segments(gd.gdrom_rq, 1);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900740 /* set a large max size to get most from DMA */
741 blk_queue_max_segment_size(gd.gdrom_rq, 0x40000);
742 gd.disk->queue = gd.gdrom_rq;
743 return gdrom_init_dma_mode();
744}
745
Jens Axboead5fc6b2018-10-15 09:01:40 -0600746static const struct blk_mq_ops gdrom_mq_ops = {
747 .queue_rq = gdrom_queue_rq,
748};
749
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900750/*
751 * register this as a block device and as compliant with the
752 * universal CD Rom driver interface
753 */
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800754static int probe_gdrom(struct platform_device *devptr)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900755{
756 int err;
757 /* Start the device */
758 if (gdrom_execute_diagnostic() != 1) {
Joe Perchese597cd02010-07-01 08:24:32 +0200759 pr_warning("ATA Probe for GDROM failed\n");
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900760 return -ENODEV;
761 }
762 /* Print out firmware ID */
763 if (gdrom_outputversion())
764 return -ENOMEM;
765 /* Register GDROM */
766 gdrom_major = register_blkdev(0, GDROM_DEV_NAME);
767 if (gdrom_major <= 0)
768 return gdrom_major;
Joe Perchese597cd02010-07-01 08:24:32 +0200769 pr_info("Registered with major number %d\n",
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900770 gdrom_major);
771 /* Specify basic properties of drive */
772 gd.cd_info = kzalloc(sizeof(struct cdrom_device_info), GFP_KERNEL);
773 if (!gd.cd_info) {
774 err = -ENOMEM;
775 goto probe_fail_no_mem;
776 }
777 probe_gdrom_setupcd();
778 gd.disk = alloc_disk(1);
779 if (!gd.disk) {
780 err = -ENODEV;
781 goto probe_fail_no_disk;
782 }
783 probe_gdrom_setupdisk();
784 if (register_cdrom(gd.cd_info)) {
785 err = -ENODEV;
786 goto probe_fail_cdrom_register;
787 }
788 gd.disk->fops = &gdrom_bdops;
Martin Wilck773008f2019-03-27 14:51:04 +0100789 gd.disk->events = DISK_EVENT_MEDIA_CHANGE;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900790 /* latch on to the interrupt */
791 err = gdrom_set_interrupt_handlers();
792 if (err)
793 goto probe_fail_cmdirq_register;
Jens Axboead5fc6b2018-10-15 09:01:40 -0600794
795 gd.gdrom_rq = blk_mq_init_sq_queue(&gd.tag_set, &gdrom_mq_ops, 1,
796 BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING);
797 if (IS_ERR(gd.gdrom_rq)) {
Jens Axboe0a40a932018-10-25 08:15:30 -0600798 err = PTR_ERR(gd.gdrom_rq);
Jens Axboead5fc6b2018-10-15 09:01:40 -0600799 gd.gdrom_rq = NULL;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900800 goto probe_fail_requestq;
Christophe JAILLET605cdf02017-02-06 23:00:13 +0100801 }
Jens Axboead5fc6b2018-10-15 09:01:40 -0600802
Christoph Hellwig8fc45042017-06-19 09:26:26 +0200803 blk_queue_bounce_limit(gd.gdrom_rq, BLK_BOUNCE_HIGH);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900804
805 err = probe_gdrom_setupqueue();
806 if (err)
807 goto probe_fail_toc;
808
809 gd.toc = kzalloc(sizeof(struct gdromtoc), GFP_KERNEL);
Christophe JAILLET605cdf02017-02-06 23:00:13 +0100810 if (!gd.toc) {
811 err = -ENOMEM;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900812 goto probe_fail_toc;
Christophe JAILLET605cdf02017-02-06 23:00:13 +0100813 }
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900814 add_disk(gd.disk);
815 return 0;
816
817probe_fail_toc:
818 blk_cleanup_queue(gd.gdrom_rq);
Jens Axboead5fc6b2018-10-15 09:01:40 -0600819 blk_mq_free_tag_set(&gd.tag_set);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900820probe_fail_requestq:
821 free_irq(HW_EVENT_GDROM_DMA, &gd);
822 free_irq(HW_EVENT_GDROM_CMD, &gd);
823probe_fail_cmdirq_register:
824probe_fail_cdrom_register:
825 del_gendisk(gd.disk);
826probe_fail_no_disk:
827 kfree(gd.cd_info);
Libo Chen31bd8fb2013-07-03 15:01:11 -0700828probe_fail_no_mem:
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900829 unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
830 gdrom_major = 0;
Joe Perchese597cd02010-07-01 08:24:32 +0200831 pr_warning("Probe failed - error is 0x%X\n", err);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900832 return err;
833}
834
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800835static int remove_gdrom(struct platform_device *devptr)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900836{
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900837 blk_cleanup_queue(gd.gdrom_rq);
Jens Axboead5fc6b2018-10-15 09:01:40 -0600838 blk_mq_free_tag_set(&gd.tag_set);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900839 free_irq(HW_EVENT_GDROM_CMD, &gd);
840 free_irq(HW_EVENT_GDROM_DMA, &gd);
841 del_gendisk(gd.disk);
842 if (gdrom_major)
843 unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
Akinobu Mita0a0c4112008-03-26 12:09:02 +0100844 unregister_cdrom(gd.cd_info);
845
846 return 0;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900847}
848
849static struct platform_driver gdrom_driver = {
850 .probe = probe_gdrom,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800851 .remove = remove_gdrom,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900852 .driver = {
853 .name = GDROM_DEV_NAME,
854 },
855};
856
857static int __init init_gdrom(void)
858{
859 int rc;
860 gd.toc = NULL;
861 rc = platform_driver_register(&gdrom_driver);
862 if (rc)
863 return rc;
864 pd = platform_device_register_simple(GDROM_DEV_NAME, -1, NULL, 0);
865 if (IS_ERR(pd)) {
866 platform_driver_unregister(&gdrom_driver);
867 return PTR_ERR(pd);
868 }
869 return 0;
870}
871
872static void __exit exit_gdrom(void)
873{
874 platform_device_unregister(pd);
875 platform_driver_unregister(&gdrom_driver);
876 kfree(gd.toc);
Wenwen Wang093c4822018-12-26 20:15:13 -0600877 kfree(gd.cd_info);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900878}
879
880module_init(init_gdrom);
881module_exit(exit_gdrom);
882MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
883MODULE_DESCRIPTION("SEGA Dreamcast GD-ROM Driver");
884MODULE_LICENSE("GPL");