881 lines · 21.9 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* GD ROM driver for the SEGA Dreamcast * copyright Adrian McMenamin, 2007 * With thanks to Marcus Comstedt and Nathan Keynes * for work in reversing PIO and DMA */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/init.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/list.h>#include <linux/slab.h>#include <linux/dma-mapping.h>#include <linux/cdrom.h>#include <linux/bio.h>#include <linux/blk-mq.h>#include <linux/interrupt.h>#include <linux/device.h>#include <linux/mutex.h>#include <linux/wait.h>#include <linux/platform_device.h>#include <scsi/scsi.h>#include <asm/io.h>#include <asm/dma.h>#include <asm/delay.h>#include <mach/dma.h>#include <mach/sysasic.h> #define GDROM_DEV_NAME "gdrom"#define GD_SESSION_OFFSET 150 /* GD Rom commands */#define GDROM_COM_SOFTRESET 0x08#define GDROM_COM_EXECDIAG 0x90#define GDROM_COM_PACKET 0xA0#define GDROM_COM_IDDEV 0xA1 /* GD Rom registers */#define GDROM_BASE_REG			0xA05F7000#define GDROM_ALTSTATUS_REG		(GDROM_BASE_REG + 0x18)#define GDROM_DATA_REG			(GDROM_BASE_REG + 0x80)#define GDROM_ERROR_REG		(GDROM_BASE_REG + 0x84)#define GDROM_INTSEC_REG		(GDROM_BASE_REG + 0x88)#define GDROM_SECNUM_REG		(GDROM_BASE_REG + 0x8C)#define GDROM_BCL_REG			(GDROM_BASE_REG + 0x90)#define GDROM_BCH_REG			(GDROM_BASE_REG + 0x94)#define GDROM_DSEL_REG			(GDROM_BASE_REG + 0x98)#define GDROM_STATUSCOMMAND_REG	(GDROM_BASE_REG + 0x9C)#define GDROM_RESET_REG		(GDROM_BASE_REG + 0x4E4) #define GDROM_DMA_STARTADDR_REG	(GDROM_BASE_REG + 0x404)#define GDROM_DMA_LENGTH_REG		(GDROM_BASE_REG + 0x408)#define GDROM_DMA_DIRECTION_REG	(GDROM_BASE_REG + 0x40C)#define GDROM_DMA_ENABLE_REG		(GDROM_BASE_REG + 0x414)#define GDROM_DMA_STATUS_REG		(GDROM_BASE_REG + 0x418)#define GDROM_DMA_WAIT_REG		(GDROM_BASE_REG + 0x4A0)#define GDROM_DMA_ACCESS_CTRL_REG	(GDROM_BASE_REG + 0x4B8) #define GDROM_HARD_SECTOR	2048#define BLOCK_LAYER_SECTOR	512#define GD_TO_BLK		4 #define GDROM_DEFAULT_TIMEOUT	(HZ * 7) static DEFINE_MUTEX(gdrom_mutex);static const struct {	int sense_key;	const char * const text;} sense_texts[] = {	{NO_SENSE, "OK"},	{RECOVERED_ERROR, "Recovered from error"},	{NOT_READY, "Device not ready"},	{MEDIUM_ERROR, "Disk not ready"},	{HARDWARE_ERROR, "Hardware error"},	{ILLEGAL_REQUEST, "Command has failed"},	{UNIT_ATTENTION, "Device needs attention - disk may have been changed"},	{DATA_PROTECT, "Data protection error"},	{ABORTED_COMMAND, "Command aborted"},}; static struct platform_device *pd;static int gdrom_major;static DECLARE_WAIT_QUEUE_HEAD(command_queue);static DECLARE_WAIT_QUEUE_HEAD(request_queue); struct gdromtoc {	unsigned int entry[99];	unsigned int first, last;	unsigned int leadout;}; static struct gdrom_unit {	struct gendisk *disk;	struct cdrom_device_info *cd_info;	int status;	int pending;	int transfer;	char disk_type;	struct gdromtoc *toc;	struct request_queue *gdrom_rq;	struct blk_mq_tag_set tag_set;} gd; struct gdrom_id {	char mid;	char modid;	char verid;	char padA[13];	char mname[16];	char modname[16];	char firmver[16];	char padB[16];}; static int gdrom_getsense(short *bufstring);static int gdrom_packetcommand(struct cdrom_device_info *cd_info,	struct packet_command *command);static int gdrom_hardreset(struct cdrom_device_info *cd_info); static bool gdrom_is_busy(void){	return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) != 0;} static bool gdrom_data_request(void){	return (__raw_readb(GDROM_ALTSTATUS_REG) & 0x88) == 8;} static bool gdrom_wait_clrbusy(void){	unsigned long timeout = jiffies + GDROM_DEFAULT_TIMEOUT;	while ((__raw_readb(GDROM_ALTSTATUS_REG) & 0x80) &&		(time_before(jiffies, timeout)))		cpu_relax();	return time_before(jiffies, timeout + 1);} static bool gdrom_wait_busy_sleeps(void){	unsigned long timeout;	/* Wait to get busy first */	timeout = jiffies + GDROM_DEFAULT_TIMEOUT;	while (!gdrom_is_busy() && time_before(jiffies, timeout))		cpu_relax();	/* Now wait for busy to clear */	return gdrom_wait_clrbusy();} static void gdrom_identifydevice(void *buf){	int c;	short *data = buf;	/* If the device won't clear it has probably	* been hit by a serious failure - but we'll	* try to return a sense key even so */	if (!gdrom_wait_clrbusy()) {		gdrom_getsense(NULL);		return;	}	__raw_writeb(GDROM_COM_IDDEV, GDROM_STATUSCOMMAND_REG);	if (!gdrom_wait_busy_sleeps()) {		gdrom_getsense(NULL);		return;	}	/* now read in the data */	for (c = 0; c < 40; c++)		data[c] = __raw_readw(GDROM_DATA_REG);} static void gdrom_spicommand(void *spi_string, int buflen){	short *cmd = spi_string;	unsigned long timeout; 	/* ensure IRQ_WAIT is set */	__raw_writeb(0x08, GDROM_ALTSTATUS_REG);	/* specify how many bytes we expect back */	__raw_writeb(buflen & 0xFF, GDROM_BCL_REG);	__raw_writeb((buflen >> 8) & 0xFF, GDROM_BCH_REG);	/* other parameters */	__raw_writeb(0, GDROM_INTSEC_REG);	__raw_writeb(0, GDROM_SECNUM_REG);	__raw_writeb(0, GDROM_ERROR_REG);	/* Wait until we can go */	if (!gdrom_wait_clrbusy()) {		gdrom_getsense(NULL);		return;	}	timeout = jiffies + GDROM_DEFAULT_TIMEOUT;	__raw_writeb(GDROM_COM_PACKET, GDROM_STATUSCOMMAND_REG);	while (!gdrom_data_request() && time_before(jiffies, timeout))		cpu_relax();	if (!time_before(jiffies, timeout + 1)) {		gdrom_getsense(NULL);		return;	}	outsw(GDROM_DATA_REG, cmd, 6);}  /* gdrom_command_executediagnostic: * Used to probe for presence of working GDROM * Restarts GDROM device and then applies standard ATA 3 * Execute Diagnostic Command: a return of '1' indicates device 0 * present and device 1 absent */static char gdrom_execute_diagnostic(void){	gdrom_hardreset(gd.cd_info);	if (!gdrom_wait_clrbusy())		return 0;	__raw_writeb(GDROM_COM_EXECDIAG, GDROM_STATUSCOMMAND_REG);	if (!gdrom_wait_busy_sleeps())		return 0;	return __raw_readb(GDROM_ERROR_REG);} /* * Prepare disk command * byte 0 = 0x70 * byte 1 = 0x1f */static int gdrom_preparedisk_cmd(void){	struct packet_command *spin_command;	spin_command = kzalloc_obj(struct packet_command);	if (!spin_command)		return -ENOMEM;	spin_command->cmd[0] = 0x70;	spin_command->cmd[2] = 0x1f;	spin_command->buflen = 0;	gd.pending = 1;	gdrom_packetcommand(gd.cd_info, spin_command);	/* 60 second timeout */	wait_event_interruptible_timeout(command_queue, gd.pending == 0,		GDROM_DEFAULT_TIMEOUT);	gd.pending = 0;	kfree(spin_command);	if (gd.status & 0x01) {		/* log an error */		gdrom_getsense(NULL);		return -EIO;	}	return 0;} /* * Read TOC command * byte 0 = 0x14 * byte 1 = session * byte 3 = sizeof TOC >> 8  ie upper byte * byte 4 = sizeof TOC & 0xff ie lower byte */static int gdrom_readtoc_cmd(struct gdromtoc *toc, int session){	int tocsize;	struct packet_command *toc_command;	int err = 0; 	toc_command = kzalloc_obj(struct packet_command);	if (!toc_command)		return -ENOMEM;	tocsize = sizeof(struct gdromtoc);	toc_command->cmd[0] = 0x14;	toc_command->cmd[1] = session;	toc_command->cmd[3] = tocsize >> 8;	toc_command->cmd[4] = tocsize & 0xff;	toc_command->buflen = tocsize;	if (gd.pending) {		err = -EBUSY;		goto cleanup_readtoc_final;	}	gd.pending = 1;	gdrom_packetcommand(gd.cd_info, toc_command);	wait_event_interruptible_timeout(command_queue, gd.pending == 0,		GDROM_DEFAULT_TIMEOUT);	if (gd.pending) {		err = -EINVAL;		goto cleanup_readtoc;	}	insw(GDROM_DATA_REG, toc, tocsize/2);	if (gd.status & 0x01)		err = -EINVAL; cleanup_readtoc:	gd.pending = 0;cleanup_readtoc_final:	kfree(toc_command);	return err;} /* TOC helpers */static int get_entry_lba(int track){	return (cpu_to_be32(track & 0xffffff00) - GD_SESSION_OFFSET);} static int get_entry_q_ctrl(int track){	return (track & 0x000000f0) >> 4;} static int get_entry_track(int track){	return (track & 0x0000ff00) >> 8;} static int gdrom_get_last_session(struct cdrom_device_info *cd_info,	struct cdrom_multisession *ms_info){	int fentry, lentry, track, data, err; 	if (!gd.toc)		return -ENOMEM; 	/* Check if GD-ROM */	err = gdrom_readtoc_cmd(gd.toc, 1);	/* Not a GD-ROM so check if standard CD-ROM */	if (err) {		err = gdrom_readtoc_cmd(gd.toc, 0);		if (err) {			pr_info("Could not get CD table of contents\n");			return -ENXIO;		}	} 	fentry = get_entry_track(gd.toc->first);	lentry = get_entry_track(gd.toc->last);	/* Find the first data track */	track = get_entry_track(gd.toc->last);	do {		data = gd.toc->entry[track - 1];		if (get_entry_q_ctrl(data))			break;	/* ie a real data track */		track--;	} while (track >= fentry); 	if ((track > 100) || (track < get_entry_track(gd.toc->first))) {		pr_info("No data on the last session of the CD\n");		gdrom_getsense(NULL);		return -ENXIO;	} 	ms_info->addr_format = CDROM_LBA;	ms_info->addr.lba = get_entry_lba(data);	ms_info->xa_flag = 1;	return 0;} static int gdrom_open(struct cdrom_device_info *cd_info, int purpose){	/* spin up the disk */	return gdrom_preparedisk_cmd();} /* this function is required even if empty */static void gdrom_release(struct cdrom_device_info *cd_info){} static int gdrom_drivestatus(struct cdrom_device_info *cd_info, int ignore){	/* read the sense key */	char sense = __raw_readb(GDROM_ERROR_REG);	sense &= 0xF0;	if (sense == 0)		return CDS_DISC_OK;	if (sense == 0x20)		return CDS_DRIVE_NOT_READY;	/* default */	return CDS_NO_INFO;} static unsigned int gdrom_check_events(struct cdrom_device_info *cd_info,				       unsigned int clearing, int ignore){	/* check the sense key */	return (__raw_readb(GDROM_ERROR_REG) & 0xF0) == 0x60 ?		DISK_EVENT_MEDIA_CHANGE : 0;} /* reset the G1 bus */static int gdrom_hardreset(struct cdrom_device_info *cd_info){	int count;	__raw_writel(0x1fffff, GDROM_RESET_REG);	for (count = 0xa0000000; count < 0xa0200000; count += 4)		__raw_readl(count);	return 0;} /* keep the function looking like the universal * CD Rom specification  - returning int */static int gdrom_packetcommand(struct cdrom_device_info *cd_info,	struct packet_command *command){	gdrom_spicommand(&command->cmd, command->buflen);	return 0;} /* Get Sense SPI command * From Marcus Comstedt * cmd = 0x13 * cmd + 4 = length of returned buffer * Returns 5 16 bit words */static int gdrom_getsense(short *bufstring){	struct packet_command *sense_command;	short sense[5];	int sense_key;	int err = -EIO; 	sense_command = kzalloc_obj(struct packet_command);	if (!sense_command)		return -ENOMEM;	sense_command->cmd[0] = 0x13;	sense_command->cmd[4] = 10;	sense_command->buflen = 10;	/* even if something is pending try to get	* the sense key if possible */	if (gd.pending && !gdrom_wait_clrbusy()) {		err = -EBUSY;		goto cleanup_sense_final;	}	gd.pending = 1;	gdrom_packetcommand(gd.cd_info, sense_command);	wait_event_interruptible_timeout(command_queue, gd.pending == 0,		GDROM_DEFAULT_TIMEOUT);	if (gd.pending)		goto cleanup_sense;	insw(GDROM_DATA_REG, &sense, sense_command->buflen/2);	if (sense[1] & 40) {		pr_info("Drive not ready - command aborted\n");		goto cleanup_sense;	}	sense_key = sense[1] & 0x0F;	if (sense_key < ARRAY_SIZE(sense_texts))		pr_info("%s\n", sense_texts[sense_key].text);	else		pr_err("Unknown sense key: %d\n", sense_key);	if (bufstring) /* return addional sense data */		memcpy(bufstring, &sense[4], 2);	if (sense_key < 2)		err = 0; cleanup_sense:	gd.pending = 0;cleanup_sense_final:	kfree(sense_command);	return err;} static int gdrom_audio_ioctl(struct cdrom_device_info *cdi, unsigned int cmd,			     void *arg){	return -EINVAL;} static const struct cdrom_device_ops gdrom_ops = {	.open			= gdrom_open,	.release		= gdrom_release,	.drive_status		= gdrom_drivestatus,	.check_events		= gdrom_check_events,	.get_last_session	= gdrom_get_last_session,	.reset			= gdrom_hardreset,	.audio_ioctl		= gdrom_audio_ioctl,	.generic_packet		= cdrom_dummy_generic_packet,	.capability		= CDC_MULTI_SESSION | CDC_MEDIA_CHANGED |				  CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R,}; static int gdrom_bdops_open(struct gendisk *disk, blk_mode_t mode){	int ret; 	disk_check_media_change(disk); 	mutex_lock(&gdrom_mutex);	ret = cdrom_open(gd.cd_info, mode);	mutex_unlock(&gdrom_mutex);	return ret;} static void gdrom_bdops_release(struct gendisk *disk){	mutex_lock(&gdrom_mutex);	cdrom_release(gd.cd_info);	mutex_unlock(&gdrom_mutex);} static unsigned int gdrom_bdops_check_events(struct gendisk *disk,					     unsigned int clearing){	return cdrom_check_events(gd.cd_info, clearing);}

Lines 1–500 of 881.