2019-05-19 15:51:31 +02:00
// SPDX-License-Identifier: GPL-2.0-or-later
2008-01-21 18:52:48 +09:00
/* 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
*/
2010-07-01 08:24:32 +02:00
# define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2008-01-21 18:52:48 +09:00
# 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>
2018-10-15 09:01:40 -06:00
# include <linux/blk-mq.h>
2008-01-21 18:52:48 +09:00
# include <linux/interrupt.h>
# include <linux/device.h>
2010-06-02 14:28:52 +02:00
# include <linux/mutex.h>
2008-01-21 18:52:48 +09:00
# 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>
2008-07-29 22:10:01 +09:00
# include <mach/dma.h>
# include <mach/sysasic.h>
2008-01-21 18:52:48 +09:00
# 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)
2010-06-02 14:28:52 +02:00
static DEFINE_MUTEX ( gdrom_mutex ) ;
2008-01-21 18:52:48 +09:00
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 ;
2018-10-15 09:01:40 -06:00
struct blk_mq_tag_set tag_set ;
2008-01-21 18:52:48 +09:00
} 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 )
{
2010-10-27 14:33:39 +09:00
return ( __raw_readb ( GDROM_ALTSTATUS_REG ) & 0x80 ) ! = 0 ;
2008-01-21 18:52:48 +09:00
}
static bool gdrom_data_request ( void )
{
2010-10-27 14:33:39 +09:00
return ( __raw_readb ( GDROM_ALTSTATUS_REG ) & 0x88 ) = = 8 ;
2008-01-21 18:52:48 +09:00
}
static bool gdrom_wait_clrbusy ( void )
{
unsigned long timeout = jiffies + GDROM_DEFAULT_TIMEOUT ;
2010-10-27 14:33:39 +09:00
while ( ( __raw_readb ( GDROM_ALTSTATUS_REG ) & 0x80 ) & &
2008-01-21 18:52:48 +09:00
( 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 ;
}
2010-10-27 14:33:39 +09:00
__raw_writeb ( GDROM_COM_IDDEV , GDROM_STATUSCOMMAND_REG ) ;
2008-01-21 18:52:48 +09:00
if ( ! gdrom_wait_busy_sleeps ( ) ) {
gdrom_getsense ( NULL ) ;
return ;
}
/* now read in the data */
for ( c = 0 ; c < 40 ; c + + )
2010-10-27 14:33:39 +09:00
data [ c ] = __raw_readw ( GDROM_DATA_REG ) ;
2008-01-21 18:52:48 +09:00
}
static void gdrom_spicommand ( void * spi_string , int buflen )
{
short * cmd = spi_string ;
unsigned long timeout ;
/* ensure IRQ_WAIT is set */
2010-10-27 14:33:39 +09:00
__raw_writeb ( 0x08 , GDROM_ALTSTATUS_REG ) ;
2008-01-21 18:52:48 +09:00
/* specify how many bytes we expect back */
2010-10-27 14:33:39 +09:00
__raw_writeb ( buflen & 0xFF , GDROM_BCL_REG ) ;
__raw_writeb ( ( buflen > > 8 ) & 0xFF , GDROM_BCH_REG ) ;
2008-01-21 18:52:48 +09:00
/* other parameters */
2010-10-27 14:33:39 +09:00
__raw_writeb ( 0 , GDROM_INTSEC_REG ) ;
__raw_writeb ( 0 , GDROM_SECNUM_REG ) ;
__raw_writeb ( 0 , GDROM_ERROR_REG ) ;
2008-01-21 18:52:48 +09:00
/* Wait until we can go */
if ( ! gdrom_wait_clrbusy ( ) ) {
gdrom_getsense ( NULL ) ;
return ;
}
timeout = jiffies + GDROM_DEFAULT_TIMEOUT ;
2010-10-27 14:33:39 +09:00
__raw_writeb ( GDROM_COM_PACKET , GDROM_STATUSCOMMAND_REG ) ;
2008-01-21 18:52:48 +09:00
while ( ! gdrom_data_request ( ) & & time_before ( jiffies , timeout ) )
cpu_relax ( ) ;
if ( ! time_before ( jiffies , timeout + 1 ) ) {
gdrom_getsense ( NULL ) ;
return ;
}
2009-10-13 12:30:40 +09:00
outsw ( GDROM_DATA_REG , cmd , 6 ) ;
2008-01-21 18:52:48 +09:00
}
/* 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 ;
2010-10-27 14:33:39 +09:00
__raw_writeb ( GDROM_COM_EXECDIAG , GDROM_STATUSCOMMAND_REG ) ;
2008-01-21 18:52:48 +09:00
if ( ! gdrom_wait_busy_sleeps ( ) )
return 0 ;
2010-10-27 14:33:39 +09:00
return __raw_readb ( GDROM_ERROR_REG ) ;
2008-01-21 18:52:48 +09:00
}
/*
* Prepare disk command
* byte 0 = 0x70
* byte 1 = 0x1f
*/
static int gdrom_preparedisk_cmd ( void )
{
struct packet_command * spin_command ;
spin_command = kzalloc ( sizeof ( struct packet_command ) , GFP_KERNEL ) ;
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 ( sizeof ( struct packet_command ) , GFP_KERNEL ) ;
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 ;
}
2009-10-13 12:30:40 +09:00
insw ( GDROM_DATA_REG , toc , tocsize / 2 ) ;
2008-01-21 18:52:48 +09:00
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 )
{
2018-10-23 20:06:37 +08:00
int fentry , lentry , track , data , err ;
2008-01-21 18:52:48 +09:00
if ( ! gd . toc )
return - ENOMEM ;
2018-10-23 20:06:37 +08:00
2008-01-21 18:52:48 +09:00
/* 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 ) {
2010-07-01 08:24:32 +02:00
pr_info ( " Could not get CD table of contents \n " ) ;
2008-01-21 18:52:48 +09:00
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 ) ) ) {
2010-07-01 08:24:32 +02:00
pr_info ( " No data on the last session of the CD \n " ) ;
2008-01-21 18:52:48 +09:00
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 */
2010-10-27 14:33:39 +09:00
char sense = __raw_readb ( GDROM_ERROR_REG ) ;
2008-01-21 18:52:48 +09:00
sense & = 0xF0 ;
if ( sense = = 0 )
return CDS_DISC_OK ;
if ( sense = = 0x20 )
return CDS_DRIVE_NOT_READY ;
/* default */
return CDS_NO_INFO ;
}
2011-03-09 19:54:28 +01:00
static unsigned int gdrom_check_events ( struct cdrom_device_info * cd_info ,
unsigned int clearing , int ignore )
2008-01-21 18:52:48 +09:00
{
/* check the sense key */
2011-03-09 19:54:28 +01:00
return ( __raw_readb ( GDROM_ERROR_REG ) & 0xF0 ) = = 0x60 ?
DISK_EVENT_MEDIA_CHANGE : 0 ;
2008-01-21 18:52:48 +09:00
}
/* reset the G1 bus */
static int gdrom_hardreset ( struct cdrom_device_info * cd_info )
{
int count ;
2010-10-27 14:33:39 +09:00
__raw_writel ( 0x1fffff , GDROM_RESET_REG ) ;
2008-01-21 18:52:48 +09:00
for ( count = 0xa0000000 ; count < 0xa0200000 ; count + = 4 )
2010-10-27 14:33:39 +09:00
__raw_readl ( count ) ;
2008-01-21 18:52:48 +09:00
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 ( sizeof ( struct packet_command ) , GFP_KERNEL ) ;
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 ;
2009-10-13 12:30:40 +09:00
insw ( GDROM_DATA_REG , & sense , sense_command - > buflen / 2 ) ;
2008-01-21 18:52:48 +09:00
if ( sense [ 1 ] & 40 ) {
2010-07-01 08:24:32 +02:00
pr_info ( " Drive not ready - command aborted \n " ) ;
2008-01-21 18:52:48 +09:00
goto cleanup_sense ;
}
sense_key = sense [ 1 ] & 0x0F ;
if ( sense_key < ARRAY_SIZE ( sense_texts ) )
2010-07-01 08:24:32 +02:00
pr_info ( " %s \n " , sense_texts [ sense_key ] . text ) ;
2008-01-21 18:52:48 +09:00
else
2010-07-01 08:24:32 +02:00
pr_err ( " Unknown sense key: %d \n " , sense_key ) ;
2008-01-21 18:52:48 +09:00
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 ;
}
2008-08-18 21:40:04 +02:00
static int gdrom_audio_ioctl ( struct cdrom_device_info * cdi , unsigned int cmd ,
void * arg )
{
return - EINVAL ;
}
2017-02-13 16:25:26 -08:00
static const struct cdrom_device_ops gdrom_ops = {
2008-01-21 18:52:48 +09:00
. open = gdrom_open ,
. release = gdrom_release ,
. drive_status = gdrom_drivestatus ,
2011-03-09 19:54:28 +01:00
. check_events = gdrom_check_events ,
2008-01-21 18:52:48 +09:00
. get_last_session = gdrom_get_last_session ,
. reset = gdrom_hardreset ,
2008-08-18 21:40:04 +02:00
. audio_ioctl = gdrom_audio_ioctl ,
2017-02-13 16:25:26 -08:00
. generic_packet = cdrom_dummy_generic_packet ,
2008-01-21 18:52:48 +09:00
. capability = CDC_MULTI_SESSION | CDC_MEDIA_CHANGED |
CDC_RESET | CDC_DRIVE_STATUS | CDC_CD_R ,
} ;
2008-03-02 10:25:18 -05:00
static int gdrom_bdops_open ( struct block_device * bdev , fmode_t mode )
2008-01-21 18:52:48 +09:00
{
2010-08-07 18:25:34 +02:00
int ret ;
2018-03-09 13:59:06 +01:00
2020-09-08 16:53:39 +02:00
bdev_check_media_change ( bdev ) ;
2018-03-09 13:59:06 +01:00
2010-06-02 14:28:52 +02:00
mutex_lock ( & gdrom_mutex ) ;
2010-08-07 18:25:34 +02:00
ret = cdrom_open ( gd . cd_info , bdev , mode ) ;
2010-06-02 14:28:52 +02:00
mutex_unlock ( & gdrom_mutex ) ;
2010-08-07 18:25:34 +02:00
return ret ;
2008-01-21 18:52:48 +09:00
}
2013-05-05 21:52:57 -04:00
static void gdrom_bdops_release ( struct gendisk * disk , fmode_t mode )
2008-01-21 18:52:48 +09:00
{
2010-06-02 14:28:52 +02:00
mutex_lock ( & gdrom_mutex ) ;
2008-10-27 11:32:27 +09:00
cdrom_release ( gd . cd_info , mode ) ;
2010-06-02 14:28:52 +02:00
mutex_unlock ( & gdrom_mutex ) ;
2008-01-21 18:52:48 +09:00
}
2011-03-09 19:54:28 +01:00
static unsigned int gdrom_bdops_check_events ( struct gendisk * disk ,
unsigned int clearing )
2008-01-21 18:52:48 +09:00
{
2011-03-09 19:54:28 +01:00
return cdrom_check_events ( gd . cd_info , clearing ) ;
2008-01-21 18:52:48 +09:00
}
2008-03-02 10:25:18 -05:00
static int gdrom_bdops_ioctl ( struct block_device * bdev , fmode_t mode ,
2008-01-21 18:52:48 +09:00
unsigned cmd , unsigned long arg )
{
2010-07-08 10:18:46 +02:00
int ret ;
2010-06-02 14:28:52 +02:00
mutex_lock ( & gdrom_mutex ) ;
2010-07-08 10:18:46 +02:00
ret = cdrom_ioctl ( gd . cd_info , bdev , mode , cmd , arg ) ;
2010-06-02 14:28:52 +02:00
mutex_unlock ( & gdrom_mutex ) ;
2010-07-08 10:18:46 +02:00
return ret ;
2008-01-21 18:52:48 +09:00
}
2009-09-21 17:01:13 -07:00
static const struct block_device_operations gdrom_bdops = {
2008-01-21 18:52:48 +09:00
. owner = THIS_MODULE ,
2008-03-02 10:25:18 -05:00
. open = gdrom_bdops_open ,
. release = gdrom_bdops_release ,
2011-03-09 19:54:28 +01:00
. check_events = gdrom_bdops_check_events ,
2010-07-08 10:18:46 +02:00
. ioctl = gdrom_bdops_ioctl ,
2019-11-28 15:55:17 +01:00
# ifdef CONFIG_COMPAT
2020-02-19 17:50:07 +01:00
. compat_ioctl = blkdev_compat_ptr_ioctl ,
2019-11-28 15:55:17 +01:00
# endif
2008-01-21 18:52:48 +09:00
} ;
static irqreturn_t gdrom_command_interrupt ( int irq , void * dev_id )
{
2010-10-27 14:33:39 +09:00
gd . status = __raw_readb ( GDROM_STATUSCOMMAND_REG ) ;
2008-01-21 18:52:48 +09:00
if ( gd . pending ! = 1 )
return IRQ_HANDLED ;
gd . pending = 0 ;
wake_up_interruptible ( & command_queue ) ;
return IRQ_HANDLED ;
}
static irqreturn_t gdrom_dma_interrupt ( int irq , void * dev_id )
{
2010-10-27 14:33:39 +09:00
gd . status = __raw_readb ( GDROM_STATUSCOMMAND_REG ) ;
2008-01-21 18:52:48 +09:00
if ( gd . transfer ! = 1 )
return IRQ_HANDLED ;
gd . transfer = 0 ;
wake_up_interruptible ( & request_queue ) ;
return IRQ_HANDLED ;
}
2012-12-21 15:14:44 -08:00
static int gdrom_set_interrupt_handlers ( void )
2008-01-21 18:52:48 +09:00
{
int err ;
err = request_irq ( HW_EVENT_GDROM_CMD , gdrom_command_interrupt ,
2013-12-03 19:22:17 -07:00
0 , " gdrom_command " , & gd ) ;
2008-01-21 18:52:48 +09:00
if ( err )
return err ;
err = request_irq ( HW_EVENT_GDROM_DMA , gdrom_dma_interrupt ,
2013-12-03 19:22:17 -07:00
0 , " gdrom_dma " , & gd ) ;
2008-01-21 18:52:48 +09:00
if ( err )
free_irq ( HW_EVENT_GDROM_CMD , & gd ) ;
return err ;
}
/* Implement DMA read using SPI command
* 0 - > 0x30
* 1 - > mode
* 2 - > block > > 16
* 3 - > block > > 8
* 4 - > block
* 8 - > sectors > > 16
* 9 - > sectors > > 8
* 10 - > sectors
*/
2018-10-15 09:01:40 -06:00
static blk_status_t gdrom_readdisk_dma ( struct request * req )
2008-01-21 18:52:48 +09:00
{
2017-06-03 09:38:04 +02:00
int block , block_cnt ;
blk_status_t err ;
2008-01-21 18:52:48 +09:00
struct packet_command * read_command ;
unsigned long timeout ;
read_command = kzalloc ( sizeof ( struct packet_command ) , GFP_KERNEL ) ;
if ( ! read_command )
2018-10-15 09:01:40 -06:00
return BLK_STS_RESOURCE ;
2008-01-21 18:52:48 +09:00
read_command - > cmd [ 0 ] = 0x30 ;
read_command - > cmd [ 1 ] = 0x20 ;
2018-10-15 09:01:40 -06:00
block = blk_rq_pos ( req ) / GD_TO_BLK + GD_SESSION_OFFSET ;
block_cnt = blk_rq_sectors ( req ) / GD_TO_BLK ;
2021-04-11 15:43:30 -07:00
__raw_writel ( page_to_phys ( bio_page ( req - > bio ) ) + bio_offset ( req - > bio ) ,
2021-04-06 08:16:48 +02:00
GDROM_DMA_STARTADDR_REG ) ;
2018-10-15 09:01:40 -06:00
__raw_writel ( block_cnt * GDROM_HARD_SECTOR , GDROM_DMA_LENGTH_REG ) ;
__raw_writel ( 1 , GDROM_DMA_DIRECTION_REG ) ;
__raw_writel ( 1 , GDROM_DMA_ENABLE_REG ) ;
read_command - > cmd [ 2 ] = ( block > > 16 ) & 0xFF ;
read_command - > cmd [ 3 ] = ( block > > 8 ) & 0xFF ;
read_command - > cmd [ 4 ] = block & 0xFF ;
read_command - > cmd [ 8 ] = ( block_cnt > > 16 ) & 0xFF ;
read_command - > cmd [ 9 ] = ( block_cnt > > 8 ) & 0xFF ;
read_command - > cmd [ 10 ] = block_cnt & 0xFF ;
/* set for DMA */
__raw_writeb ( 1 , GDROM_ERROR_REG ) ;
/* other registers */
__raw_writeb ( 0 , GDROM_SECNUM_REG ) ;
__raw_writeb ( 0 , GDROM_BCL_REG ) ;
__raw_writeb ( 0 , GDROM_BCH_REG ) ;
__raw_writeb ( 0 , GDROM_DSEL_REG ) ;
__raw_writeb ( 0 , GDROM_INTSEC_REG ) ;
/* Wait for registers to reset after any previous activity */
timeout = jiffies + HZ / 2 ;
while ( gdrom_is_busy ( ) & & time_before ( jiffies , timeout ) )
cpu_relax ( ) ;
__raw_writeb ( GDROM_COM_PACKET , GDROM_STATUSCOMMAND_REG ) ;
timeout = jiffies + HZ / 2 ;
/* Wait for packet command to finish */
while ( gdrom_is_busy ( ) & & time_before ( jiffies , timeout ) )
cpu_relax ( ) ;
gd . pending = 1 ;
gd . transfer = 1 ;
outsw ( GDROM_DATA_REG , & read_command - > cmd , 6 ) ;
timeout = jiffies + HZ / 2 ;
/* Wait for any pending DMA to finish */
while ( __raw_readb ( GDROM_DMA_STATUS_REG ) & &
time_before ( jiffies , timeout ) )
cpu_relax ( ) ;
/* start transfer */
__raw_writeb ( 1 , GDROM_DMA_STATUS_REG ) ;
wait_event_interruptible_timeout ( request_queue ,
gd . transfer = = 0 , GDROM_DEFAULT_TIMEOUT ) ;
err = gd . transfer ? BLK_STS_IOERR : BLK_STS_OK ;
gd . transfer = 0 ;
gd . pending = 0 ;
blk_mq_end_request ( req , err ) ;
2008-01-21 18:52:48 +09:00
kfree ( read_command ) ;
2018-10-15 09:01:40 -06:00
return BLK_STS_OK ;
2008-01-21 18:52:48 +09:00
}
2018-10-15 09:01:40 -06:00
static blk_status_t gdrom_queue_rq ( struct blk_mq_hw_ctx * hctx ,
const struct blk_mq_queue_data * bd )
{
blk_mq_start_request ( bd - > rq ) ;
switch ( req_op ( bd - > rq ) ) {
case REQ_OP_READ :
return gdrom_readdisk_dma ( bd - > rq ) ;
case REQ_OP_WRITE :
pr_notice ( " Read only device - write request ignored \n " ) ;
return BLK_STS_IOERR ;
default :
printk ( KERN_DEBUG " gdrom: Non-fs request ignored \n " ) ;
return BLK_STS_IOERR ;
2008-01-21 18:52:48 +09:00
}
}
/* Print string identifying GD ROM device */
2012-12-21 15:14:44 -08:00
static int gdrom_outputversion ( void )
2008-01-21 18:52:48 +09:00
{
struct gdrom_id * id ;
char * model_name , * manuf_name , * firmw_ver ;
int err = - ENOMEM ;
/* query device ID */
id = kzalloc ( sizeof ( struct gdrom_id ) , GFP_KERNEL ) ;
if ( ! id )
return err ;
gdrom_identifydevice ( id ) ;
model_name = kstrndup ( id - > modname , 16 , GFP_KERNEL ) ;
if ( ! model_name )
goto free_id ;
manuf_name = kstrndup ( id - > mname , 16 , GFP_KERNEL ) ;
if ( ! manuf_name )
goto free_model_name ;
firmw_ver = kstrndup ( id - > firmver , 16 , GFP_KERNEL ) ;
if ( ! firmw_ver )
goto free_manuf_name ;
2010-07-01 08:24:32 +02:00
pr_info ( " %s from %s with firmware %s \n " ,
2008-01-21 18:52:48 +09:00
model_name , manuf_name , firmw_ver ) ;
err = 0 ;
kfree ( firmw_ver ) ;
free_manuf_name :
kfree ( manuf_name ) ;
free_model_name :
kfree ( model_name ) ;
free_id :
kfree ( id ) ;
return err ;
}
/* set the default mode for DMA transfer */
2012-12-21 15:14:44 -08:00
static int gdrom_init_dma_mode ( void )
2008-01-21 18:52:48 +09:00
{
2010-10-27 14:33:39 +09:00
__raw_writeb ( 0x13 , GDROM_ERROR_REG ) ;
__raw_writeb ( 0x22 , GDROM_INTSEC_REG ) ;
2008-01-21 18:52:48 +09:00
if ( ! gdrom_wait_clrbusy ( ) )
return - EBUSY ;
2010-10-27 14:33:39 +09:00
__raw_writeb ( 0xEF , GDROM_STATUSCOMMAND_REG ) ;
2008-01-21 18:52:48 +09:00
if ( ! gdrom_wait_busy_sleeps ( ) )
return - EBUSY ;
/* Memory protection setting for GDROM DMA
* Bits 31 - 16 security : 0x8843
* Bits 15 and 7 reserved ( 0 )
* Bits 14 - 8 start of transfer range in 1 MB blocks OR ' ed with 0x80
* Bits 6 - 0 end of transfer range in 1 MB blocks OR ' ed with 0x80
* ( 0x40 | 0x80 ) = start range at 0x0C000000
* ( 0x7F | 0x80 ) = end range at 0x0FFFFFFF */
2010-10-27 14:33:39 +09:00
__raw_writel ( 0x8843407F , GDROM_DMA_ACCESS_CTRL_REG ) ;
__raw_writel ( 9 , GDROM_DMA_WAIT_REG ) ; /* DMA word setting */
2008-01-21 18:52:48 +09:00
return 0 ;
}
2012-12-21 15:14:44 -08:00
static void probe_gdrom_setupcd ( void )
2008-01-21 18:52:48 +09:00
{
gd . cd_info - > ops = & gdrom_ops ;
gd . cd_info - > capacity = 1 ;
strcpy ( gd . cd_info - > name , GDROM_DEV_NAME ) ;
gd . cd_info - > mask = CDC_CLOSE_TRAY | CDC_OPEN_TRAY | CDC_LOCK |
CDC_SELECT_DISC ;
}
2012-12-21 15:14:44 -08:00
static void probe_gdrom_setupdisk ( void )
2008-01-21 18:52:48 +09:00
{
gd . disk - > major = gdrom_major ;
gd . disk - > first_minor = 1 ;
gd . disk - > minors = 1 ;
2021-11-22 14:06:22 +01:00
gd . disk - > flags | = GENHD_FL_NO_PART ;
2008-01-21 18:52:48 +09:00
strcpy ( gd . disk - > disk_name , GDROM_DEV_NAME ) ;
}
2012-12-21 15:14:44 -08:00
static int probe_gdrom_setupqueue ( void )
2008-01-21 18:52:48 +09:00
{
2009-05-22 17:17:49 -04:00
blk_queue_logical_block_size ( gd . gdrom_rq , GDROM_HARD_SECTOR ) ;
2008-01-21 18:52:48 +09:00
/* using DMA so memory will need to be contiguous */
2010-02-26 00:20:39 -05:00
blk_queue_max_segments ( gd . gdrom_rq , 1 ) ;
2008-01-21 18:52:48 +09:00
/* set a large max size to get most from DMA */
blk_queue_max_segment_size ( gd . gdrom_rq , 0x40000 ) ;
gd . disk - > queue = gd . gdrom_rq ;
return gdrom_init_dma_mode ( ) ;
}
2018-10-15 09:01:40 -06:00
static const struct blk_mq_ops gdrom_mq_ops = {
. queue_rq = gdrom_queue_rq ,
} ;
2008-01-21 18:52:48 +09:00
/*
* register this as a block device and as compliant with the
* universal CD Rom driver interface
*/
2012-12-21 15:14:44 -08:00
static int probe_gdrom ( struct platform_device * devptr )
2008-01-21 18:52:48 +09:00
{
int err ;
2021-05-06 16:00:47 +02:00
/*
* Ensure our " one " device is initialized properly in case of previous
* usages of it
*/
memset ( & gd , 0 , sizeof ( gd ) ) ;
2008-01-21 18:52:48 +09:00
/* Start the device */
if ( gdrom_execute_diagnostic ( ) ! = 1 ) {
2019-10-18 11:18:27 +08:00
pr_warn ( " ATA Probe for GDROM failed \n " ) ;
2008-01-21 18:52:48 +09:00
return - ENODEV ;
}
/* Print out firmware ID */
if ( gdrom_outputversion ( ) )
return - ENOMEM ;
/* Register GDROM */
gdrom_major = register_blkdev ( 0 , GDROM_DEV_NAME ) ;
if ( gdrom_major < = 0 )
return gdrom_major ;
2010-07-01 08:24:32 +02:00
pr_info ( " Registered with major number %d \n " ,
2008-01-21 18:52:48 +09:00
gdrom_major ) ;
/* Specify basic properties of drive */
gd . cd_info = kzalloc ( sizeof ( struct cdrom_device_info ) , GFP_KERNEL ) ;
if ( ! gd . cd_info ) {
err = - ENOMEM ;
goto probe_fail_no_mem ;
}
probe_gdrom_setupcd ( ) ;
2021-06-02 09:53:29 +03:00
err = blk_mq_alloc_sq_tag_set ( & gd . tag_set , & gdrom_mq_ops , 1 ,
BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING ) ;
if ( err )
goto probe_fail_free_cd_info ;
gd . disk = blk_mq_alloc_disk ( & gd . tag_set , NULL ) ;
if ( IS_ERR ( gd . disk ) ) {
err = PTR_ERR ( gd . disk ) ;
goto probe_fail_free_tag_set ;
2008-01-21 18:52:48 +09:00
}
2021-06-02 09:53:29 +03:00
gd . gdrom_rq = gd . disk - > queue ;
2008-01-21 18:52:48 +09:00
probe_gdrom_setupdisk ( ) ;
2020-04-25 09:57:00 +02:00
if ( register_cdrom ( gd . disk , gd . cd_info ) ) {
2008-01-21 18:52:48 +09:00
err = - ENODEV ;
2021-06-02 09:53:29 +03:00
goto probe_fail_cleanup_disk ;
2008-01-21 18:52:48 +09:00
}
gd . disk - > fops = & gdrom_bdops ;
2019-03-27 14:51:04 +01:00
gd . disk - > events = DISK_EVENT_MEDIA_CHANGE ;
2008-01-21 18:52:48 +09:00
/* latch on to the interrupt */
err = gdrom_set_interrupt_handlers ( ) ;
if ( err )
2021-06-02 09:53:29 +03:00
goto probe_fail_cleanup_disk ;
2018-10-15 09:01:40 -06:00
2008-01-21 18:52:48 +09:00
err = probe_gdrom_setupqueue ( ) ;
if ( err )
2021-06-02 09:53:29 +03:00
goto probe_fail_free_irqs ;
2008-01-21 18:52:48 +09:00
gd . toc = kzalloc ( sizeof ( struct gdromtoc ) , GFP_KERNEL ) ;
2017-02-06 23:00:13 +01:00
if ( ! gd . toc ) {
err = - ENOMEM ;
2021-06-02 09:53:29 +03:00
goto probe_fail_free_irqs ;
2017-02-06 23:00:13 +01:00
}
2021-09-27 15:02:27 -07:00
err = add_disk ( gd . disk ) ;
if ( err )
goto probe_fail_add_disk ;
2008-01-21 18:52:48 +09:00
return 0 ;
2021-09-27 15:02:27 -07:00
probe_fail_add_disk :
kfree ( gd . toc ) ;
2021-06-02 09:53:29 +03:00
probe_fail_free_irqs :
2008-01-21 18:52:48 +09:00
free_irq ( HW_EVENT_GDROM_DMA , & gd ) ;
free_irq ( HW_EVENT_GDROM_CMD , & gd ) ;
2021-06-02 09:53:29 +03:00
probe_fail_cleanup_disk :
2022-06-19 08:05:52 +02:00
put_disk ( gd . disk ) ;
2021-06-02 09:53:29 +03:00
probe_fail_free_tag_set :
blk_mq_free_tag_set ( & gd . tag_set ) ;
probe_fail_free_cd_info :
2008-01-21 18:52:48 +09:00
kfree ( gd . cd_info ) ;
2013-07-03 15:01:11 -07:00
probe_fail_no_mem :
2008-01-21 18:52:48 +09:00
unregister_blkdev ( gdrom_major , GDROM_DEV_NAME ) ;
gdrom_major = 0 ;
2019-10-18 11:18:27 +08:00
pr_warn ( " Probe failed - error is 0x%X \n " , err ) ;
2008-01-21 18:52:48 +09:00
return err ;
}
2012-12-21 15:14:44 -08:00
static int remove_gdrom ( struct platform_device * devptr )
2008-01-21 18:52:48 +09:00
{
2018-10-15 09:01:40 -06:00
blk_mq_free_tag_set ( & gd . tag_set ) ;
2008-01-21 18:52:48 +09:00
free_irq ( HW_EVENT_GDROM_CMD , & gd ) ;
free_irq ( HW_EVENT_GDROM_DMA , & gd ) ;
del_gendisk ( gd . disk ) ;
if ( gdrom_major )
unregister_blkdev ( gdrom_major , GDROM_DEV_NAME ) ;
2008-03-26 12:09:02 +01:00
unregister_cdrom ( gd . cd_info ) ;
2021-05-03 13:56:54 +02:00
kfree ( gd . cd_info ) ;
kfree ( gd . toc ) ;
2008-03-26 12:09:02 +01:00
return 0 ;
2008-01-21 18:52:48 +09:00
}
static struct platform_driver gdrom_driver = {
. probe = probe_gdrom ,
2012-12-21 15:14:44 -08:00
. remove = remove_gdrom ,
2008-01-21 18:52:48 +09:00
. driver = {
. name = GDROM_DEV_NAME ,
} ,
} ;
static int __init init_gdrom ( void )
{
int rc ;
2021-05-06 16:00:47 +02:00
2008-01-21 18:52:48 +09:00
rc = platform_driver_register ( & gdrom_driver ) ;
if ( rc )
return rc ;
pd = platform_device_register_simple ( GDROM_DEV_NAME , - 1 , NULL , 0 ) ;
if ( IS_ERR ( pd ) ) {
platform_driver_unregister ( & gdrom_driver ) ;
return PTR_ERR ( pd ) ;
}
return 0 ;
}
static void __exit exit_gdrom ( void )
{
platform_device_unregister ( pd ) ;
platform_driver_unregister ( & gdrom_driver ) ;
}
module_init ( init_gdrom ) ;
module_exit ( exit_gdrom ) ;
MODULE_AUTHOR ( " Adrian McMenamin <adrian@mcmen.demon.co.uk> " ) ;
MODULE_DESCRIPTION ( " SEGA Dreamcast GD-ROM Driver " ) ;
MODULE_LICENSE ( " GPL " ) ;