/***************************************************************************
 * Copyright (C) 2006-2007 Richard C. Edelman II., All rights Reserved.    *
 *                                                                         *
 * The contents of this file are subject to the QuokOS Public License      *
 * ("QPL") Version 1.0, and You may not copy or use this file in either    *
 * source code or executable form, except in compliance with the terms and *
 * conditions of the QPL.                                                  *
 *                                                                         *
 * You may obtain a copy of the QPL (the "License") from the QuokOS        *
 * Website at http://quokos.quokworld.com.                                 *
 *                                                                         *
 * All software distributed under the Licenses is provided strictly on an  *
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, *
 * AND LICENSOR HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT    *
 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR *
 * PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the Licenses for     *
 * specific language governing rights and limitations under the Licenses.  *
 ***************************************************************************/

#ifndef __KERNEL_DEV_DISK_FLOPPY_FLOPPY_H__
#define __KERNEL_DEV_DISK_FLOPPY_FLOPPY_H__

#include <kernel/fs/devfs/devfs.h>

/** /def floppy I/O base addresses */
#define FLOPPY_PRIMARY_BASE				0x3F0
#define FLOPPY_SECONDARY_BASE			0x370

/* register addresses - add these to the base address! */
/** /def Status Register A - read only */
#define FLOPPY_REG_STATUS_A				0x00
/** /def  Status Register B - read only */
#define FLOPPY_REG_STATUS_B				0x01
/** /def  Digital Output Register - read/wrte */
#define	FLOPPY_REG_DOR					0x02
/** /def  Tape Drive Register - read/write */
#define FLOPPY_REG_TDR					0x03
/* Main Status Register - read only */
#define FLOPPY_REG_MSR					0x04
/* Data Rate Select Regster - write only */
#define FLOPPY_REG_DRS					0x04
/* Data Register (FIFO)- read/write */
#define FLOPPY_REG_DATA					0x05
/* Reserved Register */
#define FLOPPY_REG_RESERVED				0x06
/* Config Control Register - write only */
#define FLOPPY_REG_CCR					0x07
/* Digital Input Register - read only */
#define FLOPPY_REG_DIR					0x07

/* floppy commands - not all are here */
#define FLOPPY_CMD_SUCCESS				0x00
#define FLOPPY_CMD_READ_TRACK			0x02
#define FLOPPY_CMD_SPECIFY				0x03
#define FLOPPY_CMD_SENSE_DRIVE			0x04
#define FLOPPY_CMD_WRITE_DATA			0x45
#define FLOPPY_CMD_READ_DATA			0x46
#define FLOPPY_CMD_RECALIBRATE			0x07
#define FLOPPY_CMD_SENSE_INTERRUPT		0x08
#define FLOPPY_CMD_WRITE_DELETED		0x09
#define FLOPPY_CMD_READ_ID				0x0A
#define FLOPPY_CMD_READ_DELETED			0x0C
#define FLOPPY_CMD_FORMAT_TRACK			0x0D
#define FLOPPY_CMD_DUMPREG				0x0E
#define FLOPPY_CMD_SEEK					0x0F
#define FLOPPY_CMD_VERSION				0x10
#define FLOPPY_CMD_SCAN_EQUAL			0x11
#define FLOPPY_CMD_PERPENDICULAR		0x12
#define FLOPPY_CMD_CONFIGURE			0x13
#define FLOPPY_CMD_LOCK					0x14
#define FLOPPY_CMD_VERIFY				0x16
#define FLOPPY_CMD_SCAN_LOW_OR_EQL		0x19
#define FLOPPY_CMD_SCAN_HIGH_OR_EQL		0x1D
#define FLOPPY_CMD_INVALID				0x80

/* Data Rates */
#define FLOPPY_DATA_RATE_500KB			0x00
#define FLOPPY_DATA_RATE_300KB			0x01
#define FLOPPY_DATA_RATE_250KB			0x02
#define FLOPPY_DATA_RATE_1MB			0x03

/* Precomp Delays */
#define FLOPPY_PRECOMP_DELAY_DEFAULT	0x00
#define FLOPPY_PRECOMP_DELAY_41NS		0x01
#define FLOPPY_PRECOMP_DELAY_83NS		0x02
#define FLOPPY_PRECOMP_DELAY_125NS		0x03
#define FLOPPY_PRECOMP_DELAY_166NS		0x04
#define FLOPPY_PRECOMP_DELAY_208NS		0x05
#define FLOPPY_PRECOMP_DELAY_250NS		0x06
#define FLOPPY_PRECOMP_DELAY_DISABLED	0x07

/* MISC */
#define FLOPPY_MAX_RETRIES				0x03
#define FLOPPY_DMA_CHANNEL				0x02
#define FLOPPY_TIMEOUT					0x14
#define FLOPPY_READ						0x01
#define FLOPPY_WRITE					0x02
#define MAX_FLOPPIES					0x04

#define FLOPPY_TOTAL_BLOCKS				2880

#define FLOPPY_MOTOR_OFF				0x00
#define FLOPPY_MOTOR_ON					0x01
#define FLOPPY_MOTOR_WAITING			0x02


struct floppy_st0_s
{
	union
	{
		struct
		{
			unsigned int ds0:1;				/* drive select 0 */
			unsigned int ds1:1;				/* drive select 1 */
			unsigned int ha:1;				/* head address */
			unsigned int :1;				/* unused - always 0 */
			unsigned int ec:1;				/* equipment check */
			unsigned int se:1;				/* seek end */
			unsigned int ic:2;				/* interrupt code */
		} bits;

		unsigned char data;
	};

} __attribute__ ((packed));

struct floppy_st1_s
{
	union
	{
		struct
		{
			unsigned int ma:1;				/* missing address mark */
			unsigned int nw:1;				/* not writable */
			unsigned int nd:1;				/* no data */
			unsigned int :1;				/* unused - always 0 */
			unsigned int ou:1;				/* overrun/underrun */
			unsigned int de:1;				/* data error */
			unsigned int :1;				/* unused - always 0 */
			unsigned int en:1;				/* end of cylinder */
		} bits;

		unsigned char data;
	};

} __attribute__ ((packed));

struct floppy_st2_s
{
	union
	{
		struct
		{
			unsigned int md:1;				/* missing data address mark */
			unsigned int bc:1;				/* bad cylinder */
			unsigned int :1;				/* unused - always 0 */
			unsigned int :1;				/* unused - always 0 */
			unsigned int wc:1;				/* wrong cylinder */
			unsigned int dd:1;				/* data error in data field */
			unsigned int cm:1;				/* control mark */
			unsigned int :1;				/* unused - always 0 */
		} bits;

		unsigned char data;
	};

} __attribute__ ((packed));

struct floppy_st3_s
{
	union
	{
		struct
		{
			unsigned int ds0:1;				/* drive select 0 */
			unsigned int ds1:1;				/* drive select 1 */
			unsigned int hd:1;				/* head address */
			unsigned int :1;				/* unused - always 1 */
			unsigned int t0:1;				/* track zero */
			unsigned int :1;				/* unused - always 1 */
			unsigned int wp:1;				/* write protected */
			unsigned int :1;				/* unused - always 0 */
		} bits;

		unsigned char data;
	};

} __attribute__ ((packed));

struct floppy_drs_s
{
	union
	{
		struct
		{
			unsigned int drate_select:2;	/* data rate selection */
			unsigned int precomp:3;			/* precomp delay */
			unsigned int :1;				/* unused - always 0 */
			unsigned int power_down:1;		/* power down the controller */
			unsigned int sw_reset:1;		/* reset the controller */
		} bits;

		unsigned char data;
	};

} __attribute__ ((packed));

struct floppy_dor_s
{
	union
	{
		struct
		{
			unsigned int drive:2;
			unsigned int reset:1;
			unsigned int dma:1;
			unsigned int motor0:1;
			unsigned int motor1:1;
			unsigned int motor2:1;
			unsigned int motor3:1;
		} bits;

		unsigned char data;
	};

} __attribute__ ((packed));

struct floppy_msr_s
{
	union
	{
		struct
		{
			unsigned int drive_0_busy:1;
			unsigned int drive_1_busy:1;
			unsigned int drive_2_busy:1;
			unsigned int drive_3_busy:1;
			unsigned int cmd_busy:1;
			unsigned int non_dma:1;
			unsigned int dio:1;
			unsigned int rqm:1;
		} bits;

		unsigned char data;
	};

} __attribute__ ((packed));

struct floppy_dir_s
{
	union
	{
		struct
		{
			unsigned int unused:7;
			unsigned int disk_change:1;
		} bits;

		unsigned char data;
	};

} __attribute__ ((packed));

struct floppy_ccr_s
{
	union
	{
		struct
		{
			unsigned int drate_sel_0:1;
			unsigned int drate_sel_1:1;
			unsigned int bit2:1;
			unsigned int bit3:1;
			unsigned int bit4:1;
			unsigned int bit5:1;
			unsigned int bit6:1;
			unsigned int bit7:1;
		} bits;

		unsigned char data;
	};

} __attribute__ ((packed));

typedef struct
{
	unsigned int sectors;
	unsigned int heads;
	unsigned int cylinders;
	unsigned int blocksize;
} floppy_drive_geometry_t;

typedef struct
{
	char name[4];
	int drive_num; /* 0 and 1 are primary, 2 and 3 are secondary */
	unsigned base;
	unsigned char current_block;
	struct floppy_st0_s st0;
	int locked;
	floppy_drive_geometry_t *geometry;
	
	struct 
	{
		unsigned timeout;
		unsigned retries;
		unsigned write_prot;
	} params;
	
} floppy_drive_t;

extern int init_floppy_drive( void );
extern int floppy_service_routine( void );
extern void floppy_motor_on( floppy_drive_t *floppy );
extern void floppy_motor_off( floppy_drive_t *floppy );
extern int floppy_send_byte( floppy_drive_t *floppy, unsigned char byte );
extern int floppy_get_byte( floppy_drive_t *floppy, unsigned char *byte );
extern int floppy_wait_for_interrupt( floppy_drive_t *floppy, int issue_sis );
extern int floppy_recalibrate( floppy_drive_t *floppy );
extern int floppy_reset( floppy_drive_t *floppy );
extern int floppy_seek_cylinder( floppy_drive_t *floppy, int block );
extern void floppy_block_geometry( floppy_drive_t *floppy, int block, floppy_drive_geometry_t *geometry );
extern int floppy_read_write_block( floppy_drive_t *floppy, void *buffer, int mode );

extern int floppy_read_write( struct device_handle *handle, char *buffer, unsigned int size, int mode );
extern int floppy_open( struct device_handle *device, char *filename );
extern int floppy_close( struct device_handle *device );
extern int floppy_read( struct device_handle *device, char *buffer, unsigned int bytes );
extern int floppy_write( struct device_handle *device, char *buffer, unsigned int bytes );
extern int floppy_seek( struct device_handle *device, unsigned int offset, char origin );

#ifdef FLOPPY_DETECT_DISK_CHANGE
extern void floppy_sense_disk_change( floppy_drive_t *floppy );
#endif

#endif /* __KERNEL_DEV_DISK_FLOPPY_FLOPPY_H__ */
