1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  Generic IDE disk driver			File: dev_ide_common.c
5    *
6    *  This file contains common constants and structures for IDE
7    *  disks and CFE drivers for them.
8    *
9    *  Author:  Mitch Lichtenberg
10    *
11    *********************************************************************
12    *
13    *  Copyright 2000,2001,2002,2003
14    *  Broadcom Corporation. All rights reserved.
15    *
16    *  This software is furnished under license and may be used and
17    *  copied only in accordance with the following terms and
18    *  conditions.  Subject to these conditions, you may download,
19    *  copy, install, use, modify and distribute modified or unmodified
20    *  copies of this software in source and/or binary form.  No title
21    *  or ownership is transferred hereby.
22    *
23    *  1) Any source code used, modified or distributed must reproduce
24    *     and retain this copyright notice and list of conditions
25    *     as they appear in the source file.
26    *
27    *  2) No right is granted to use any trade name, trademark, or
28    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
29    *     name may not be used to endorse or promote products derived
30    *     from this software without the prior written permission of
31    *     Broadcom Corporation.
32    *
33    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
34    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
35    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
37    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
38    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
39    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
41    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
43    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
44    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
45    *     THE POSSIBILITY OF SUCH DAMAGE.
46    ********************************************************************* */
47
48
49/*  *********************************************************************
50    *  Constants
51    ********************************************************************* */
52
53#define IDE_REG_DATA	0x0
54#define IDE_REG_ERROR	0x1
55#define IDE_REG_FEATURE	0x1
56#define IDE_REG_PRECOMP	0x1
57#define IDE_REG_SECCNT	0x2
58#define IDE_REG_IR	0x2	/* ATAPI */
59#define IDE_REG_SECNUM	0x3
60#define IDE_REG_BCLSB	0x4	/* ATAPI */
61#define IDE_REG_BCMSB	0x5	/* ATAPI */
62#define IDE_REG_CYLLSB	0x4
63#define IDE_REG_CYLMSB	0x5
64#define IDE_REG_DRVHD	0x6
65#define IDE_REG_STATUS	0x7
66#define IDE_REG_COMMAND	0x7
67#define IDE_REG_ALTSTAT	0x6	/* Note: ALTSTAT is really 0x3F6, what do we do? */
68#define IDE_REG_DIGOUT	0x6
69
70#define IDE_IR_CD	0x01	/* 1 = command, 0 = data */
71#define IDE_IR_IO	0x02	/* 1 = from device, 0 = to device */
72#define IDE_IR_REL	0x04
73
74#define IDE_ERR_BBK	0x80	/* sector marked bad by host */
75#define IDE_ERR_UNC	0x40	/* uncorrectable error */
76#define IDE_ERR_MC	0x20	/* medium changed */
77#define IDE_ERR_NID	0x10	/* no ID mark found */
78#define IDE_ERR_MCR	0x08	/* medium change required */
79#define IDE_ERR_ABT	0x04	/* command aborted */
80#define IDE_ERR_NT0	0x02	/* track 0 not found */
81#define IDE_ERR_NDM	0x01	/* address mark not found */
82
83#define IDE_DRV_SLAVE	0x10
84#define IDE_DRV_LBA	0x40
85#define IDE_DRV_MBO	0xA0
86#define IDE_DRV_HDMASK	0x0F
87
88#define IDE_STS_BSY	0x80	/* drive is busy */
89#define IDE_STS_RDY	0x40	/* drive is ready */
90#define IDE_STS_WFT	0x20	/* write fault */
91#define IDE_STS_SKC	0x10	/* seek complete */
92#define IDE_STS_DRQ	0x08	/* data can be transferred */
93#define IDE_STS_CORR	0x04	/* correctable data error */
94#define IDE_STS_IDX	0x02	/* index mark just passed */
95#define IDE_STS_ERR	0x01	/* Error register contains info */
96
97#define IDE_CMD_RECAL		0x10
98#define IDE_CMD_READ		0x20
99#define IDE_CMD_READRETRY	0x21
100#define IDE_CMD_WRITE		0x30
101#define IDE_CMD_READVERIFY	0x40
102#define IDE_CMD_DIAGNOSTIC	0x90
103#define IDE_CMD_INITPARAMS	0x91
104#define IDE_CMD_SETMULTIPLE	0xC6
105#define IDE_CMD_POWER_MODE	0xE5
106#define IDE_CMD_DRIVE_INFO	0xEC
107
108#define IDE_CMD_ATAPI_SOFTRESET	0x08
109#define IDE_CMD_ATAPI_PACKET	0xA0
110#define IDE_CMD_ATAPI_IDENTIFY	0xA1
111#define IDE_CMD_ATAPI_SERVICE	0xA2
112#define IDE_CMD_SET_FEATURE     0xEF
113
114#define IDE_SUB_FEAT_DEV_SPINUP 0x07
115
116#define IDE_DOR_SRST		0x04
117#define IDE_DOR_IEN		0x02
118
119#define DISK_SECTORSIZE		512
120#define CDROM_SECTORSIZE	2048
121#define MAX_SECTORSIZE		2048
122
123#define ATAPI_SENSE_MASK	0xF0
124#define ATAPI_SENSE_NONE	0x00
125#define ATAPI_SENSE_RECOVERED	0x10
126#define ATAPI_SENSE_NOTREADY	0x20
127#define ATAPI_SENSE_MEDIUMERROR	0x30
128#define ATAPI_SENSE_HWERROR	0x40
129#define ATAPI_SENSE_ILLREQUEST	0x50
130#define ATAPI_SENSE_ATTENTION	0x60
131#define ATAPI_SENSE_PROTECT	0x70
132#define ATAPI_SENSE_BLANKCHECK	0x80
133#define ATAPI_SENSE_VSPECIFIC	0x90
134#define ATAPI_SENSE_COPYABORT	0xA0
135#define ATAPI_SENSE_CMDABORT	0xB0
136#define ATAPI_SENSE_EQUAL	0xC0
137#define ATAPI_SENSE_VOLOVERFLOW	0xD0
138#define ATAPI_SENSE_MISCOMPARE	0xE0
139#define ATAPI_SENSE_RESERVED	0xF0
140
141#define ATAPI_SIG_LSB		0x14
142#define ATAPI_SIG_MSB		0xEB
143
144#define CDB_CMD_READ		0x28
145#define CDB_CMD_WRITE		0x2A
146#define CDB_CMD_REQSENSE	0x03
147
148
149/*  *********************************************************************
150    *  Structures
151    ********************************************************************* */
152
153typedef struct idecommon_dispatch_s idecommon_dispatch_t;
154
155struct idecommon_dispatch_s {
156    void *ref;
157    uint32_t baseaddr;
158    uint8_t (*inb)(idecommon_dispatch_t *disp,uint32_t reg);
159    uint16_t (*inw)(idecommon_dispatch_t *disp,uint32_t reg);
160    void (*ins)(idecommon_dispatch_t *disp,uint32_t reg,hsaddr_t buf,int len);
161
162    void (*outb)(idecommon_dispatch_t *disp,uint32_t reg,uint8_t val);
163    void (*outw)(idecommon_dispatch_t *disp,uint32_t reg,uint16_t val);
164    void (*outs)(idecommon_dispatch_t *disp,uint32_t reg,hsaddr_t buf,int len);
165};
166
167#define IDEDISP_WRITEREG8(ide,reg,val)    (*((ide)->outb))(ide,reg,val)
168#define IDEDISP_WRITEREG16(ide,reg,val)   (*((ide)->outw))(ide,reg,val)
169#define IDEDISP_WRITEBUF(ide,reg,buf,len) (*((ide)->outs))(ide,reg,buf,len)
170#define IDEDISP_READREG8(ide,reg)         (*((ide)->inb))(ide,reg)
171#define IDEDISP_READREG16(ide,reg)        (*((ide)->inw))(ide,reg)
172#define IDEDISP_READBUF(ide,reg,buf,len)  (*((ide)->ins))(ide,reg,buf,len)
173
174typedef struct idecommon_s idecommon_t;
175
176struct idecommon_s {
177    idecommon_dispatch_t *idecommon_dispatch;
178    unsigned long idecommon_addr;	/* physical address */
179    int idecommon_unit;			/* 0 or 1 master/slave */
180    int idecommon_sectorsize;		/* size of each sector */
181    long long idecommon_ttlsect;	/* total sectors */
182    int idecommon_atapi;		/* 1 if ATAPI device */
183    int idecommon_devtype;		/* device type */
184    uint64_t idecommon_deferprobe;	/* Defer probe to open */
185    uint32_t idecommon_flags;		/* flags for underlying driver */
186    int (*idecommon_readfunc)(idecommon_t *ide,uint64_t lba,int numsec,hsaddr_t buffer);
187    int (*idecommon_writefunc)(idecommon_t *ide,uint64_t lba,int numsec,hsaddr_t buffer);
188
189    uint32_t timer;
190};
191
192
193
194/*  *********************************************************************
195    *  Prototypes
196    ********************************************************************* */
197
198extern void idecommon_init(idecommon_t *ide,int devtype);
199extern int idecommon_open(cfe_devctx_t *ctx);
200extern int idecommon_read(cfe_devctx_t *ctx,iocb_buffer_t *buffer);
201extern int idecommon_inpstat(cfe_devctx_t *ctx,iocb_inpstat_t *inpstat);
202extern int idecommon_write(cfe_devctx_t *ctx,iocb_buffer_t *buffer);
203extern int idecommon_ioctl(cfe_devctx_t *ctx,iocb_buffer_t *buffer);
204extern int idecommon_identify(idecommon_t *ide,unsigned char *buffer);
205extern int idecommon_close(cfe_devctx_t *ctx);
206extern int idecommon_devprobe(idecommon_t *ide,int noisy);
207void idecommon_attach(cfe_devdisp_t *disp);
208
209