blob: 584bc3126403d58955a915db2117fcd1d16de5a2 [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>
34#include <linux/blkdev.h>
35#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>
39#include <linux/workqueue.h>
40#include <linux/platform_device.h>
41#include <scsi/scsi.h>
42#include <asm/io.h>
43#include <asm/dma.h>
44#include <asm/delay.h>
Paul Mundt0764bff2008-07-29 22:10:01 +090045#include <mach/dma.h>
46#include <mach/sysasic.h>
Adrian McMenamin74ee1a72008-01-21 18:52:48 +090047
48#define GDROM_DEV_NAME "gdrom"
49#define GD_SESSION_OFFSET 150
50
51/* GD Rom commands */
52#define GDROM_COM_SOFTRESET 0x08
53#define GDROM_COM_EXECDIAG 0x90
54#define GDROM_COM_PACKET 0xA0
55#define GDROM_COM_IDDEV 0xA1
56
57/* GD Rom registers */
58#define GDROM_BASE_REG 0xA05F7000
59#define GDROM_ALTSTATUS_REG (GDROM_BASE_REG + 0x18)
60#define GDROM_DATA_REG (GDROM_BASE_REG + 0x80)
61#define GDROM_ERROR_REG (GDROM_BASE_REG + 0x84)
62#define GDROM_INTSEC_REG (GDROM_BASE_REG + 0x88)
63#define GDROM_SECNUM_REG (GDROM_BASE_REG + 0x8C)
64#define GDROM_BCL_REG (GDROM_BASE_REG + 0x90)
65#define GDROM_BCH_REG (GDROM_BASE_REG + 0x94)
66#define GDROM_DSEL_REG (GDROM_BASE_REG + 0x98)
67#define GDROM_STATUSCOMMAND_REG (GDROM_BASE_REG + 0x9C)
68#define GDROM_RESET_REG (GDROM_BASE_REG + 0x4E4)
69
70#define GDROM_DMA_STARTADDR_REG (GDROM_BASE_REG + 0x404)
71#define GDROM_DMA_LENGTH_REG (GDROM_BASE_REG + 0x408)
72#define GDROM_DMA_DIRECTION_REG (GDROM_BASE_REG + 0x40C)
73#define GDROM_DMA_ENABLE_REG (GDROM_BASE_REG + 0x414)
74#define GDROM_DMA_STATUS_REG (GDROM_BASE_REG + 0x418)
75#define GDROM_DMA_WAIT_REG (GDROM_BASE_REG + 0x4A0)
76#define GDROM_DMA_ACCESS_CTRL_REG (GDROM_BASE_REG + 0x4B8)
77
78#define GDROM_HARD_SECTOR 2048
79#define BLOCK_LAYER_SECTOR 512
80#define GD_TO_BLK 4
81
82#define GDROM_DEFAULT_TIMEOUT (HZ * 7)
83
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020084static DEFINE_MUTEX(gdrom_mutex);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +090085static const struct {
86 int sense_key;
87 const char * const text;
88} sense_texts[] = {
89 {NO_SENSE, "OK"},
90 {RECOVERED_ERROR, "Recovered from error"},
91 {NOT_READY, "Device not ready"},
92 {MEDIUM_ERROR, "Disk not ready"},
93 {HARDWARE_ERROR, "Hardware error"},
94 {ILLEGAL_REQUEST, "Command has failed"},
95 {UNIT_ATTENTION, "Device needs attention - disk may have been changed"},
96 {DATA_PROTECT, "Data protection error"},
97 {ABORTED_COMMAND, "Command aborted"},
98};
99
100static struct platform_device *pd;
101static int gdrom_major;
102static DECLARE_WAIT_QUEUE_HEAD(command_queue);
103static DECLARE_WAIT_QUEUE_HEAD(request_queue);
104
105static DEFINE_SPINLOCK(gdrom_lock);
106static void gdrom_readdisk_dma(struct work_struct *work);
107static DECLARE_WORK(work, gdrom_readdisk_dma);
108static LIST_HEAD(gdrom_deferred);
109
110struct gdromtoc {
111 unsigned int entry[99];
112 unsigned int first, last;
113 unsigned int leadout;
114};
115
116static struct gdrom_unit {
117 struct gendisk *disk;
118 struct cdrom_device_info *cd_info;
119 int status;
120 int pending;
121 int transfer;
122 char disk_type;
123 struct gdromtoc *toc;
124 struct request_queue *gdrom_rq;
125} gd;
126
127struct gdrom_id {
128 char mid;
129 char modid;
130 char verid;
131 char padA[13];
132 char mname[16];
133 char modname[16];
134 char firmver[16];
135 char padB[16];
136};
137
138static int gdrom_getsense(short *bufstring);
139static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
140 struct packet_command *command);
141static int gdrom_hardreset(struct cdrom_device_info *cd_info);
142
143static bool gdrom_is_busy(void)
144{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900145 return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) != 0;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900146}
147
148static bool gdrom_data_request(void)
149{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900150 return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x88) == 8;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900151}
152
153static bool gdrom_wait_clrbusy(void)
154{
155 unsigned long timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900156 while ((__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) &&
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900157 (time_before(jiffies, timeout)))
158 cpu_relax();
159 return time_before(jiffies, timeout + 1);
160}
161
162static bool gdrom_wait_busy_sleeps(void)
163{
164 unsigned long timeout;
165 /* Wait to get busy first */
166 timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
167 while (!gdrom_is_busy() && time_before(jiffies, timeout))
168 cpu_relax();
169 /* Now wait for busy to clear */
170 return gdrom_wait_clrbusy();
171}
172
173static void gdrom_identifydevice(void *buf)
174{
175 int c;
176 short *data = buf;
177 /* If the device won't clear it has probably
178 * been hit by a serious failure - but we'll
179 * try to return a sense key even so */
180 if (!gdrom_wait_clrbusy()) {
181 gdrom_getsense(NULL);
182 return;
183 }
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900184 __raw_writeb(GDROM_COM_IDDEV, GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900185 if (!gdrom_wait_busy_sleeps()) {
186 gdrom_getsense(NULL);
187 return;
188 }
189 /* now read in the data */
190 for (c = 0; c < 40; c++)
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900191 data[c] = __raw_readw(GDROM_DATA_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900192}
193
194static void gdrom_spicommand(void *spi_string, int buflen)
195{
196 short *cmd = spi_string;
197 unsigned long timeout;
198
199 /* ensure IRQ_WAIT is set */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900200 __raw_writeb(0x08, GDROM_ALTSTATUS_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900201 /* specify how many bytes we expect back */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900202 __raw_writeb(buflen & 0xFF, GDROM_BCL_REG);
203 __raw_writeb((buflen >> 8) & 0xFF, GDROM_BCH_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900204 /* other parameters */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900205 __raw_writeb(0, GDROM_INTSEC_REG);
206 __raw_writeb(0, GDROM_SECNUM_REG);
207 __raw_writeb(0, GDROM_ERROR_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900208 /* Wait until we can go */
209 if (!gdrom_wait_clrbusy()) {
210 gdrom_getsense(NULL);
211 return;
212 }
213 timeout = jiffies + GDROM_DEFAULT_TIMEOUT;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900214 __raw_writeb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900215 while (!gdrom_data_request() && time_before(jiffies, timeout))
216 cpu_relax();
217 if (!time_before(jiffies, timeout + 1)) {
218 gdrom_getsense(NULL);
219 return;
220 }
Paul Mundt54d51022009-10-13 12:30:40 +0900221 outsw(GDROM_DATA_REG, cmd, 6);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900222}
223
224
225/* gdrom_command_executediagnostic:
226 * Used to probe for presence of working GDROM
227 * Restarts GDROM device and then applies standard ATA 3
228 * Execute Diagnostic Command: a return of '1' indicates device 0
229 * present and device 1 absent
230 */
231static char gdrom_execute_diagnostic(void)
232{
233 gdrom_hardreset(gd.cd_info);
234 if (!gdrom_wait_clrbusy())
235 return 0;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900236 __raw_writeb(GDROM_COM_EXECDIAG, GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900237 if (!gdrom_wait_busy_sleeps())
238 return 0;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900239 return __raw_readb(GDROM_ERROR_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900240}
241
242/*
243 * Prepare disk command
244 * byte 0 = 0x70
245 * byte 1 = 0x1f
246 */
247static int gdrom_preparedisk_cmd(void)
248{
249 struct packet_command *spin_command;
250 spin_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
251 if (!spin_command)
252 return -ENOMEM;
253 spin_command->cmd[0] = 0x70;
254 spin_command->cmd[2] = 0x1f;
255 spin_command->buflen = 0;
256 gd.pending = 1;
257 gdrom_packetcommand(gd.cd_info, spin_command);
258 /* 60 second timeout */
259 wait_event_interruptible_timeout(command_queue, gd.pending == 0,
260 GDROM_DEFAULT_TIMEOUT);
261 gd.pending = 0;
262 kfree(spin_command);
263 if (gd.status & 0x01) {
264 /* log an error */
265 gdrom_getsense(NULL);
266 return -EIO;
267 }
268 return 0;
269}
270
271/*
272 * Read TOC command
273 * byte 0 = 0x14
274 * byte 1 = session
275 * byte 3 = sizeof TOC >> 8 ie upper byte
276 * byte 4 = sizeof TOC & 0xff ie lower byte
277 */
278static int gdrom_readtoc_cmd(struct gdromtoc *toc, int session)
279{
280 int tocsize;
281 struct packet_command *toc_command;
282 int err = 0;
283
284 toc_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
285 if (!toc_command)
286 return -ENOMEM;
287 tocsize = sizeof(struct gdromtoc);
288 toc_command->cmd[0] = 0x14;
289 toc_command->cmd[1] = session;
290 toc_command->cmd[3] = tocsize >> 8;
291 toc_command->cmd[4] = tocsize & 0xff;
292 toc_command->buflen = tocsize;
293 if (gd.pending) {
294 err = -EBUSY;
295 goto cleanup_readtoc_final;
296 }
297 gd.pending = 1;
298 gdrom_packetcommand(gd.cd_info, toc_command);
299 wait_event_interruptible_timeout(command_queue, gd.pending == 0,
300 GDROM_DEFAULT_TIMEOUT);
301 if (gd.pending) {
302 err = -EINVAL;
303 goto cleanup_readtoc;
304 }
Paul Mundt54d51022009-10-13 12:30:40 +0900305 insw(GDROM_DATA_REG, toc, tocsize/2);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900306 if (gd.status & 0x01)
307 err = -EINVAL;
308
309cleanup_readtoc:
310 gd.pending = 0;
311cleanup_readtoc_final:
312 kfree(toc_command);
313 return err;
314}
315
316/* TOC helpers */
317static int get_entry_lba(int track)
318{
319 return (cpu_to_be32(track & 0xffffff00) - GD_SESSION_OFFSET);
320}
321
322static int get_entry_q_ctrl(int track)
323{
324 return (track & 0x000000f0) >> 4;
325}
326
327static int get_entry_track(int track)
328{
329 return (track & 0x0000ff00) >> 8;
330}
331
332static int gdrom_get_last_session(struct cdrom_device_info *cd_info,
333 struct cdrom_multisession *ms_info)
334{
335 int fentry, lentry, track, data, tocuse, err;
336 if (!gd.toc)
337 return -ENOMEM;
338 tocuse = 1;
339 /* Check if GD-ROM */
340 err = gdrom_readtoc_cmd(gd.toc, 1);
341 /* Not a GD-ROM so check if standard CD-ROM */
342 if (err) {
343 tocuse = 0;
344 err = gdrom_readtoc_cmd(gd.toc, 0);
345 if (err) {
Joe Perchese597cd02010-07-01 08:24:32 +0200346 pr_info("Could not get CD table of contents\n");
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900347 return -ENXIO;
348 }
349 }
350
351 fentry = get_entry_track(gd.toc->first);
352 lentry = get_entry_track(gd.toc->last);
353 /* Find the first data track */
354 track = get_entry_track(gd.toc->last);
355 do {
356 data = gd.toc->entry[track - 1];
357 if (get_entry_q_ctrl(data))
358 break; /* ie a real data track */
359 track--;
360 } while (track >= fentry);
361
362 if ((track > 100) || (track < get_entry_track(gd.toc->first))) {
Joe Perchese597cd02010-07-01 08:24:32 +0200363 pr_info("No data on the last session of the CD\n");
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900364 gdrom_getsense(NULL);
365 return -ENXIO;
366 }
367
368 ms_info->addr_format = CDROM_LBA;
369 ms_info->addr.lba = get_entry_lba(data);
370 ms_info->xa_flag = 1;
371 return 0;
372}
373
374static int gdrom_open(struct cdrom_device_info *cd_info, int purpose)
375{
376 /* spin up the disk */
377 return gdrom_preparedisk_cmd();
378}
379
380/* this function is required even if empty */
381static void gdrom_release(struct cdrom_device_info *cd_info)
382{
383}
384
385static int gdrom_drivestatus(struct cdrom_device_info *cd_info, int ignore)
386{
387 /* read the sense key */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900388 char sense = __raw_readb(GDROM_ERROR_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900389 sense &= 0xF0;
390 if (sense == 0)
391 return CDS_DISC_OK;
392 if (sense == 0x20)
393 return CDS_DRIVE_NOT_READY;
394 /* default */
395 return CDS_NO_INFO;
396}
397
Tejun Heo1c270302011-03-09 19:54:28 +0100398static unsigned int gdrom_check_events(struct cdrom_device_info *cd_info,
399 unsigned int clearing, int ignore)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900400{
401 /* check the sense key */
Tejun Heo1c270302011-03-09 19:54:28 +0100402 return (__raw_readb(GDROM_ERROR_REG) & 0xF0) == 0x60 ?
403 DISK_EVENT_MEDIA_CHANGE : 0;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900404}
405
406/* reset the G1 bus */
407static int gdrom_hardreset(struct cdrom_device_info *cd_info)
408{
409 int count;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900410 __raw_writel(0x1fffff, GDROM_RESET_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900411 for (count = 0xa0000000; count < 0xa0200000; count += 4)
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900412 __raw_readl(count);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900413 return 0;
414}
415
416/* keep the function looking like the universal
417 * CD Rom specification - returning int */
418static int gdrom_packetcommand(struct cdrom_device_info *cd_info,
419 struct packet_command *command)
420{
421 gdrom_spicommand(&command->cmd, command->buflen);
422 return 0;
423}
424
425/* Get Sense SPI command
426 * From Marcus Comstedt
427 * cmd = 0x13
428 * cmd + 4 = length of returned buffer
429 * Returns 5 16 bit words
430 */
431static int gdrom_getsense(short *bufstring)
432{
433 struct packet_command *sense_command;
434 short sense[5];
435 int sense_key;
436 int err = -EIO;
437
438 sense_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
439 if (!sense_command)
440 return -ENOMEM;
441 sense_command->cmd[0] = 0x13;
442 sense_command->cmd[4] = 10;
443 sense_command->buflen = 10;
444 /* even if something is pending try to get
445 * the sense key if possible */
446 if (gd.pending && !gdrom_wait_clrbusy()) {
447 err = -EBUSY;
448 goto cleanup_sense_final;
449 }
450 gd.pending = 1;
451 gdrom_packetcommand(gd.cd_info, sense_command);
452 wait_event_interruptible_timeout(command_queue, gd.pending == 0,
453 GDROM_DEFAULT_TIMEOUT);
454 if (gd.pending)
455 goto cleanup_sense;
Paul Mundt54d51022009-10-13 12:30:40 +0900456 insw(GDROM_DATA_REG, &sense, sense_command->buflen/2);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900457 if (sense[1] & 40) {
Joe Perchese597cd02010-07-01 08:24:32 +0200458 pr_info("Drive not ready - command aborted\n");
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900459 goto cleanup_sense;
460 }
461 sense_key = sense[1] & 0x0F;
462 if (sense_key < ARRAY_SIZE(sense_texts))
Joe Perchese597cd02010-07-01 08:24:32 +0200463 pr_info("%s\n", sense_texts[sense_key].text);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900464 else
Joe Perchese597cd02010-07-01 08:24:32 +0200465 pr_err("Unknown sense key: %d\n", sense_key);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900466 if (bufstring) /* return addional sense data */
467 memcpy(bufstring, &sense[4], 2);
468 if (sense_key < 2)
469 err = 0;
470
471cleanup_sense:
472 gd.pending = 0;
473cleanup_sense_final:
474 kfree(sense_command);
475 return err;
476}
477
Borislav Petkovd15cad52008-08-18 21:40:04 +0200478static int gdrom_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,
479 void *arg)
480{
481 return -EINVAL;
482}
483
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900484static struct cdrom_device_ops gdrom_ops = {
485 .open = gdrom_open,
486 .release = gdrom_release,
487 .drive_status = gdrom_drivestatus,
Tejun Heo1c270302011-03-09 19:54:28 +0100488 .check_events = gdrom_check_events,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900489 .get_last_session = gdrom_get_last_session,
490 .reset = gdrom_hardreset,
Borislav Petkovd15cad52008-08-18 21:40:04 +0200491 .audio_ioctl = gdrom_audio_ioctl,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900492 .capability = CDC_MULTI_SESSION | CDC_MEDIA_CHANGED |
493 CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R,
494 .n_minors = 1,
495};
496
Al Viro8389feb2008-03-02 10:25:18 -0500497static int gdrom_bdops_open(struct block_device *bdev, fmode_t mode)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900498{
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200499 int ret;
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200500 mutex_lock(&gdrom_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200501 ret = cdrom_open(gd.cd_info, bdev, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200502 mutex_unlock(&gdrom_mutex);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +0200503 return ret;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900504}
505
Al Virodb2a1442013-05-05 21:52:57 -0400506static void gdrom_bdops_release(struct gendisk *disk, fmode_t mode)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900507{
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200508 mutex_lock(&gdrom_mutex);
Nobuhiro Iwamatsu545727f2008-10-27 11:32:27 +0900509 cdrom_release(gd.cd_info, mode);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200510 mutex_unlock(&gdrom_mutex);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900511}
512
Tejun Heo1c270302011-03-09 19:54:28 +0100513static unsigned int gdrom_bdops_check_events(struct gendisk *disk,
514 unsigned int clearing)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900515{
Tejun Heo1c270302011-03-09 19:54:28 +0100516 return cdrom_check_events(gd.cd_info, clearing);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900517}
518
Al Viro8389feb2008-03-02 10:25:18 -0500519static int gdrom_bdops_ioctl(struct block_device *bdev, fmode_t mode,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900520 unsigned cmd, unsigned long arg)
521{
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200522 int ret;
523
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200524 mutex_lock(&gdrom_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200525 ret = cdrom_ioctl(gd.cd_info, bdev, mode, cmd, arg);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200526 mutex_unlock(&gdrom_mutex);
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200527
528 return ret;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900529}
530
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700531static const struct block_device_operations gdrom_bdops = {
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900532 .owner = THIS_MODULE,
Al Viro8389feb2008-03-02 10:25:18 -0500533 .open = gdrom_bdops_open,
534 .release = gdrom_bdops_release,
Tejun Heo1c270302011-03-09 19:54:28 +0100535 .check_events = gdrom_bdops_check_events,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200536 .ioctl = gdrom_bdops_ioctl,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900537};
538
539static irqreturn_t gdrom_command_interrupt(int irq, void *dev_id)
540{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900541 gd.status = __raw_readb(GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900542 if (gd.pending != 1)
543 return IRQ_HANDLED;
544 gd.pending = 0;
545 wake_up_interruptible(&command_queue);
546 return IRQ_HANDLED;
547}
548
549static irqreturn_t gdrom_dma_interrupt(int irq, void *dev_id)
550{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900551 gd.status = __raw_readb(GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900552 if (gd.transfer != 1)
553 return IRQ_HANDLED;
554 gd.transfer = 0;
555 wake_up_interruptible(&request_queue);
556 return IRQ_HANDLED;
557}
558
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800559static int gdrom_set_interrupt_handlers(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900560{
561 int err;
562
563 err = request_irq(HW_EVENT_GDROM_CMD, gdrom_command_interrupt,
Michael Opdenacker644ff182013-12-03 19:22:17 -0700564 0, "gdrom_command", &gd);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900565 if (err)
566 return err;
567 err = request_irq(HW_EVENT_GDROM_DMA, gdrom_dma_interrupt,
Michael Opdenacker644ff182013-12-03 19:22:17 -0700568 0, "gdrom_dma", &gd);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900569 if (err)
570 free_irq(HW_EVENT_GDROM_CMD, &gd);
571 return err;
572}
573
574/* Implement DMA read using SPI command
575 * 0 -> 0x30
576 * 1 -> mode
577 * 2 -> block >> 16
578 * 3 -> block >> 8
579 * 4 -> block
580 * 8 -> sectors >> 16
581 * 9 -> sectors >> 8
582 * 10 -> sectors
583 */
584static void gdrom_readdisk_dma(struct work_struct *work)
585{
586 int err, block, block_cnt;
587 struct packet_command *read_command;
588 struct list_head *elem, *next;
589 struct request *req;
590 unsigned long timeout;
591
592 if (list_empty(&gdrom_deferred))
593 return;
594 read_command = kzalloc(sizeof(struct packet_command), GFP_KERNEL);
595 if (!read_command)
596 return; /* get more memory later? */
597 read_command->cmd[0] = 0x30;
598 read_command->cmd[1] = 0x20;
599 spin_lock(&gdrom_lock);
600 list_for_each_safe(elem, next, &gdrom_deferred) {
601 req = list_entry(elem, struct request, queuelist);
602 spin_unlock(&gdrom_lock);
Tejun Heo83096eb2009-05-07 22:24:39 +0900603 block = blk_rq_pos(req)/GD_TO_BLK + GD_SESSION_OFFSET;
604 block_cnt = blk_rq_sectors(req)/GD_TO_BLK;
Jens Axboe8e1a6282014-04-16 08:26:20 -0600605 __raw_writel(virt_to_phys(bio_data(req->bio)), GDROM_DMA_STARTADDR_REG);
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900606 __raw_writel(block_cnt * GDROM_HARD_SECTOR, GDROM_DMA_LENGTH_REG);
607 __raw_writel(1, GDROM_DMA_DIRECTION_REG);
608 __raw_writel(1, GDROM_DMA_ENABLE_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900609 read_command->cmd[2] = (block >> 16) & 0xFF;
610 read_command->cmd[3] = (block >> 8) & 0xFF;
611 read_command->cmd[4] = block & 0xFF;
612 read_command->cmd[8] = (block_cnt >> 16) & 0xFF;
613 read_command->cmd[9] = (block_cnt >> 8) & 0xFF;
614 read_command->cmd[10] = block_cnt & 0xFF;
615 /* set for DMA */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900616 __raw_writeb(1, GDROM_ERROR_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900617 /* other registers */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900618 __raw_writeb(0, GDROM_SECNUM_REG);
619 __raw_writeb(0, GDROM_BCL_REG);
620 __raw_writeb(0, GDROM_BCH_REG);
621 __raw_writeb(0, GDROM_DSEL_REG);
622 __raw_writeb(0, GDROM_INTSEC_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900623 /* Wait for registers to reset after any previous activity */
624 timeout = jiffies + HZ / 2;
625 while (gdrom_is_busy() && time_before(jiffies, timeout))
626 cpu_relax();
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900627 __raw_writeb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900628 timeout = jiffies + HZ / 2;
629 /* Wait for packet command to finish */
630 while (gdrom_is_busy() && time_before(jiffies, timeout))
631 cpu_relax();
632 gd.pending = 1;
633 gd.transfer = 1;
Paul Mundt54d51022009-10-13 12:30:40 +0900634 outsw(GDROM_DATA_REG, &read_command->cmd, 6);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900635 timeout = jiffies + HZ / 2;
636 /* Wait for any pending DMA to finish */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900637 while (__raw_readb(GDROM_DMA_STATUS_REG) &&
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900638 time_before(jiffies, timeout))
639 cpu_relax();
640 /* start transfer */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900641 __raw_writeb(1, GDROM_DMA_STATUS_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900642 wait_event_interruptible_timeout(request_queue,
643 gd.transfer == 0, GDROM_DEFAULT_TIMEOUT);
Kiyoshi Ueda7afb3a62008-10-01 10:13:02 -0400644 err = gd.transfer ? -EIO : 0;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900645 gd.transfer = 0;
646 gd.pending = 0;
647 /* now seek to take the request spinlock
648 * before handling ending the request */
649 spin_lock(&gdrom_lock);
650 list_del_init(&req->queuelist);
Tejun Heo40cbbb72009-04-23 11:05:19 +0900651 __blk_end_request_all(req, err);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900652 }
653 spin_unlock(&gdrom_lock);
654 kfree(read_command);
655}
656
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900657static void gdrom_request(struct request_queue *rq)
658{
659 struct request *req;
660
Tejun Heo9934c8c2009-05-08 11:54:16 +0900661 while ((req = blk_fetch_request(rq)) != NULL) {
Christoph Hellwig33659eb2010-08-07 18:17:56 +0200662 if (req->cmd_type != REQ_TYPE_FS) {
Joe Perchese597cd02010-07-01 08:24:32 +0200663 printk(KERN_DEBUG "gdrom: Non-fs request ignored\n");
Tejun Heo23430462009-05-08 11:54:14 +0900664 __blk_end_request_all(req, -EIO);
665 continue;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900666 }
667 if (rq_data_dir(req) != READ) {
Joe Perchese597cd02010-07-01 08:24:32 +0200668 pr_notice("Read only device - write request ignored\n");
Tejun Heo23430462009-05-08 11:54:14 +0900669 __blk_end_request_all(req, -EIO);
670 continue;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900671 }
Tejun Heo23430462009-05-08 11:54:14 +0900672
673 /*
674 * Add to list of deferred work and then schedule
675 * workqueue.
676 */
677 list_add_tail(&req->queuelist, &gdrom_deferred);
678 schedule_work(&work);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900679 }
680}
681
682/* Print string identifying GD ROM device */
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800683static int gdrom_outputversion(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900684{
685 struct gdrom_id *id;
686 char *model_name, *manuf_name, *firmw_ver;
687 int err = -ENOMEM;
688
689 /* query device ID */
690 id = kzalloc(sizeof(struct gdrom_id), GFP_KERNEL);
691 if (!id)
692 return err;
693 gdrom_identifydevice(id);
694 model_name = kstrndup(id->modname, 16, GFP_KERNEL);
695 if (!model_name)
696 goto free_id;
697 manuf_name = kstrndup(id->mname, 16, GFP_KERNEL);
698 if (!manuf_name)
699 goto free_model_name;
700 firmw_ver = kstrndup(id->firmver, 16, GFP_KERNEL);
701 if (!firmw_ver)
702 goto free_manuf_name;
Joe Perchese597cd02010-07-01 08:24:32 +0200703 pr_info("%s from %s with firmware %s\n",
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900704 model_name, manuf_name, firmw_ver);
705 err = 0;
706 kfree(firmw_ver);
707free_manuf_name:
708 kfree(manuf_name);
709free_model_name:
710 kfree(model_name);
711free_id:
712 kfree(id);
713 return err;
714}
715
716/* set the default mode for DMA transfer */
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800717static int gdrom_init_dma_mode(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900718{
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900719 __raw_writeb(0x13, GDROM_ERROR_REG);
720 __raw_writeb(0x22, GDROM_INTSEC_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900721 if (!gdrom_wait_clrbusy())
722 return -EBUSY;
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900723 __raw_writeb(0xEF, GDROM_STATUSCOMMAND_REG);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900724 if (!gdrom_wait_busy_sleeps())
725 return -EBUSY;
726 /* Memory protection setting for GDROM DMA
727 * Bits 31 - 16 security: 0x8843
728 * Bits 15 and 7 reserved (0)
729 * Bits 14 - 8 start of transfer range in 1 MB blocks OR'ed with 0x80
730 * Bits 6 - 0 end of transfer range in 1 MB blocks OR'ed with 0x80
731 * (0x40 | 0x80) = start range at 0x0C000000
732 * (0x7F | 0x80) = end range at 0x0FFFFFFF */
Paul Mundt9eb79bb2010-10-27 14:33:39 +0900733 __raw_writel(0x8843407F, GDROM_DMA_ACCESS_CTRL_REG);
734 __raw_writel(9, GDROM_DMA_WAIT_REG); /* DMA word setting */
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900735 return 0;
736}
737
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800738static void probe_gdrom_setupcd(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900739{
740 gd.cd_info->ops = &gdrom_ops;
741 gd.cd_info->capacity = 1;
742 strcpy(gd.cd_info->name, GDROM_DEV_NAME);
743 gd.cd_info->mask = CDC_CLOSE_TRAY|CDC_OPEN_TRAY|CDC_LOCK|
744 CDC_SELECT_DISC;
745}
746
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800747static void probe_gdrom_setupdisk(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900748{
749 gd.disk->major = gdrom_major;
750 gd.disk->first_minor = 1;
751 gd.disk->minors = 1;
752 strcpy(gd.disk->disk_name, GDROM_DEV_NAME);
753}
754
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800755static int probe_gdrom_setupqueue(void)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900756{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400757 blk_queue_logical_block_size(gd.gdrom_rq, GDROM_HARD_SECTOR);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900758 /* using DMA so memory will need to be contiguous */
Martin K. Petersen8a783622010-02-26 00:20:39 -0500759 blk_queue_max_segments(gd.gdrom_rq, 1);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900760 /* set a large max size to get most from DMA */
761 blk_queue_max_segment_size(gd.gdrom_rq, 0x40000);
762 gd.disk->queue = gd.gdrom_rq;
763 return gdrom_init_dma_mode();
764}
765
766/*
767 * register this as a block device and as compliant with the
768 * universal CD Rom driver interface
769 */
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800770static int probe_gdrom(struct platform_device *devptr)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900771{
772 int err;
773 /* Start the device */
774 if (gdrom_execute_diagnostic() != 1) {
Joe Perchese597cd02010-07-01 08:24:32 +0200775 pr_warning("ATA Probe for GDROM failed\n");
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900776 return -ENODEV;
777 }
778 /* Print out firmware ID */
779 if (gdrom_outputversion())
780 return -ENOMEM;
781 /* Register GDROM */
782 gdrom_major = register_blkdev(0, GDROM_DEV_NAME);
783 if (gdrom_major <= 0)
784 return gdrom_major;
Joe Perchese597cd02010-07-01 08:24:32 +0200785 pr_info("Registered with major number %d\n",
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900786 gdrom_major);
787 /* Specify basic properties of drive */
788 gd.cd_info = kzalloc(sizeof(struct cdrom_device_info), GFP_KERNEL);
789 if (!gd.cd_info) {
790 err = -ENOMEM;
791 goto probe_fail_no_mem;
792 }
793 probe_gdrom_setupcd();
794 gd.disk = alloc_disk(1);
795 if (!gd.disk) {
796 err = -ENODEV;
797 goto probe_fail_no_disk;
798 }
799 probe_gdrom_setupdisk();
800 if (register_cdrom(gd.cd_info)) {
801 err = -ENODEV;
802 goto probe_fail_cdrom_register;
803 }
804 gd.disk->fops = &gdrom_bdops;
805 /* latch on to the interrupt */
806 err = gdrom_set_interrupt_handlers();
807 if (err)
808 goto probe_fail_cmdirq_register;
809 gd.gdrom_rq = blk_init_queue(gdrom_request, &gdrom_lock);
810 if (!gd.gdrom_rq)
811 goto probe_fail_requestq;
812
813 err = probe_gdrom_setupqueue();
814 if (err)
815 goto probe_fail_toc;
816
817 gd.toc = kzalloc(sizeof(struct gdromtoc), GFP_KERNEL);
818 if (!gd.toc)
819 goto probe_fail_toc;
820 add_disk(gd.disk);
821 return 0;
822
823probe_fail_toc:
824 blk_cleanup_queue(gd.gdrom_rq);
825probe_fail_requestq:
826 free_irq(HW_EVENT_GDROM_DMA, &gd);
827 free_irq(HW_EVENT_GDROM_CMD, &gd);
828probe_fail_cmdirq_register:
829probe_fail_cdrom_register:
830 del_gendisk(gd.disk);
831probe_fail_no_disk:
832 kfree(gd.cd_info);
Libo Chen31bd8fb2013-07-03 15:01:11 -0700833probe_fail_no_mem:
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900834 unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
835 gdrom_major = 0;
Joe Perchese597cd02010-07-01 08:24:32 +0200836 pr_warning("Probe failed - error is 0x%X\n", err);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900837 return err;
838}
839
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800840static int remove_gdrom(struct platform_device *devptr)
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900841{
Tejun Heo43829732012-08-20 14:51:24 -0700842 flush_work(&work);
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900843 blk_cleanup_queue(gd.gdrom_rq);
844 free_irq(HW_EVENT_GDROM_CMD, &gd);
845 free_irq(HW_EVENT_GDROM_DMA, &gd);
846 del_gendisk(gd.disk);
847 if (gdrom_major)
848 unregister_blkdev(gdrom_major, GDROM_DEV_NAME);
Akinobu Mita0a0c4112008-03-26 12:09:02 +0100849 unregister_cdrom(gd.cd_info);
850
851 return 0;
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900852}
853
854static struct platform_driver gdrom_driver = {
855 .probe = probe_gdrom,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800856 .remove = remove_gdrom,
Adrian McMenamin74ee1a72008-01-21 18:52:48 +0900857 .driver = {
858 .name = GDROM_DEV_NAME,
859 },
860};
861
862static int __init init_gdrom(void)
863{
864 int rc;
865 gd.toc = NULL;
866 rc = platform_driver_register(&gdrom_driver);
867 if (rc)
868 return rc;
869 pd = platform_device_register_simple(GDROM_DEV_NAME, -1, NULL, 0);
870 if (IS_ERR(pd)) {
871 platform_driver_unregister(&gdrom_driver);
872 return PTR_ERR(pd);
873 }
874 return 0;
875}
876
877static void __exit exit_gdrom(void)
878{
879 platform_device_unregister(pd);
880 platform_driver_unregister(&gdrom_driver);
881 kfree(gd.toc);
882}
883
884module_init(init_gdrom);
885module_exit(exit_gdrom);
886MODULE_AUTHOR("Adrian McMenamin <adrian@mcmen.demon.co.uk>");
887MODULE_DESCRIPTION("SEGA Dreamcast GD-ROM Driver");
888MODULE_LICENSE("GPL");