source: rtems/c/src/libchip/ide/ata_internal.h @ 81f64200

4.115
Last change on this file since 81f64200 was 81f64200, checked in by Sebastian Huber <sebastian.huber@…>, on 08/17/10 at 14:15:47

2010-08-17 Sebastian Huber <sebastian.huber@…>

  • libchip/ide/ata_internal.h: C++ compatibility.
  • Property mode set to 100644
File size: 11.7 KB
Line 
1/*
2 * ata_internal.h
3 *
4 * ATA RTEMS driver internal header file
5 *
6 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
7 * Authors: Eugeny S. Mints     <Eugeny.Mints@oktet.ru>
8 *          Alexandra Kossovsky <sasha@oktet.ru>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.com/license/LICENSE.
13 *
14 * $Id$
15 *
16 */
17#ifndef __ATA_INTERNAL_H__
18#define __ATA_INTERNAL_H__
19
20#include <rtems.h>
21#include <sys/types.h>
22#include <rtems/libio.h>
23#include <stdlib.h>
24
25#include <rtems/blkdev.h>
26#include <rtems/diskdevs.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/*
33 * Conversion from and to little-endian byte order. (no-op on i386/i486)
34 *
35 * Naming: Ca_b_c, where a: F = from, T = to, b: LE = little-endian,
36 * BE = big-endian, c: W = word (16 bits), L = longword (32 bits)
37 */
38#if (CPU_BIG_ENDIAN == TRUE)
39#    define CF_LE_W(v) CPU_swap_u16(v)
40#    define CF_LE_L(v) CPU_swap_u32(v)
41#    define CT_LE_W(v) CPU_swap_u16(v)
42#    define CT_LE_L(v) CPU_swap_u32(v)
43#else
44#    define CF_LE_W(v) (v)
45#    define CF_LE_L(v) (v)
46#    define CT_LE_W(v) (v)
47#    define CT_LE_L(v) (v)
48#endif
49
50#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
51#define ATA_UNDEFINED_VALUE   (-1)
52
53/* Sector size for all ATA devices */
54#define ATA_SECTOR_SIZE                 512
55
56
57#define ATA_MAX_CMD_REG_OFFSET          8
58
59
60/* ATA Commands */
61
62/* Types of ATA commands */
63#define ATA_COMMAND_TYPE_NON_DATA   0
64#define ATA_COMMAND_TYPE_PIO_IN     1
65#define ATA_COMMAND_TYPE_PIO_OUT    2
66#define ATA_COMMAND_TYPE_DMA        3
67
68/* ATA commands opcodes */
69/*
70 * Commands present in both ATA-2 and ATA-4 specs.
71 * Some commands have two values in ATA-2,
72 * in such case value from ATA-4 used.
73 * Some commands have slightly different names in these specifications,
74 * so names from ATA-4 are used.
75 */
76#define ATA_COMMAND_NOP                          0x00
77#define ATA_COMMAND_READ_SECTORS                 0x20
78#define ATA_COMMAND_WRITE_SECTORS                0x30
79#define ATA_COMMAND_READ_VERIFY_SECTORS          0x40
80#define ATA_COMMAND_SEEK                         0x70 /* or 0x7. */
81#define ATA_COMMAND_EXECUTE_DEVICE_DIAGNOSTIC    0x90
82#define ATA_COMMAND_INITIALIZE_DEVICE_PARAMETERS 0x91
83#define ATA_COMMAND_DOWNLOAD_MICROCODE           0x92
84#define ATA_COMMAND_READ_MULTIPLE                0xc4
85#define ATA_COMMAND_WRITE_MULTIPLE               0xc5
86#define ATA_COMMAND_SET_MULTIPLE_MODE            0xc6
87#define ATA_COMMAND_READ_DMA                     0xc8
88#define ATA_COMMAND_WRITE_DMA                    0xca
89#define ATA_COMMAND_STANDBY_IMMEDIATE            0xe0 /* or 0x94 */
90#define ATA_COMMAND_IDLE_IMMEDIATE               0xe1 /* or 0x95 */
91#define ATA_COMMAND_STANDBY                      0xe2 /* or 0x96 */
92#define ATA_COMMAND_IDLE                         0xe3 /* or 0x97 */
93#define ATA_COMMAND_READ_BUFFER                  0xe4
94#define ATA_COMMAND_CHECK_POWER_MODE             0xe5 /* or 0x98 in ATA-2 */
95#define ATA_COMMAND_SLEEP                        0xe6 /* or 0x99 */
96#define ATA_COMMAND_WRITE_BUFFER                 0xe8
97#define ATA_COMMAND_IDENTIFY_DEVICE              0xec
98#define ATA_COMMAND_SET_FEATURES                 0xef
99
100/* Commands present in both ATA-2 and ATA-4 specs: removable media */
101#define ATA_COMMAND_MEDIA_LOCK                   0xde
102#define ATA_COMMAND_MEDIA_UNLOCK                 0xdf
103#define ATA_COMMAND_MEDIA_EJECT                  0xed
104
105
106/* Commands present in ATA-2, but not in ATA-4 (not used) */
107#define ATA_COMMAND_RECALIBRATE                  0x10 /* or 0x1. */
108#define ATA_COMMAND_READ_SECTOR_NON_RETRY        0x21
109#define ATA_COMMAND_READ_LONG_RETRY              0x22
110#define ATA_COMMAND_READ_LONG_NON_RETRY          0x23
111#define ATA_COMMAND_WRITE_SECTOR_NON_RETRY       0x31
112#define ATA_COMMAND_WRITE_LONG_RETRY             0x32
113#define ATA_COMMAND_WRITE_LONG_NON_RETRY         0x33
114#define ATA_COMMAND_WRITE_VERIFY                 0x3c
115#define ATA_COMMAND_READ_VERIFY_SECTOR_NON_RETRY 0x41
116#define ATA_COMMAND_FORMAT_TRACK                 0x50
117#define ATA_COMMAND_READ_DMA_NON_RETRY           0xc9
118#define ATA_COMMAND_WRITE_DMA_NON_RETRY          0xcb
119#define ATA_COMMAND_ACKNOWLEGE_MEDIA_CHANGE      0xdb
120#define ATA_COMMAND_BOOT_POST_BOOT               0xdc
121#define ATA_COMMAND_BOOT_PRE_BOOT                0xdd
122#define ATA_COMMAND_WRITE_SAME                   0xe9
123
124/* Commands from ATA-4 specification: CFA feature set */
125#define ATA_COMMAND_CFA_REQUEST_EXTENDED_ERROR_CODE  0x03
126#define ATA_COMMAND_CFA_WRITE_SECTORS_WITHOUT_ERASE  0x38
127#define ATA_COMMAND_CFA_TRANSLATE_SECTOR             0x87
128#define ATA_COMMAND_CFA_ERASE_SECTORS                0xc0
129#define ATA_COMMAND_CFA_WRITE_MULTIPLE_WITHOUT_ERASE 0xcd
130
131/* Commands from ATA-4 specification: commands to use with PACKET command */
132#define ATA_COMMAND_DEVICE_RESET                 0x08
133#define ATA_COMMAND_PACKET                       0xa0
134#define ATA_COMMAND_IDENTIFY_PACKET_DEVICE       0xa1
135#define ATA_COMMAND_SERVICE                      0xa2
136
137/* Commands from ATA-4 specification: SECURITY commands */
138#define ATA_COMMAND_SECURITY_SET_PASSWORD        0xf1
139#define ATA_COMMAND_SECURITY_UNLOCK              0xf2
140#define ATA_COMMAND_SECURITY_ERASE_PREPARE       0xf3
141#define ATA_COMMAND_SECURITY_ERASE_UNIT          0xf4
142#define ATA_COMMAND_SECURITY_FREEZE_LOCK         0xf5
143#define ATA_COMMAND_SECURITY_DISABLE_PASSWORD    0xf6
144
145/* Commands from ATA-4 specification: other commands */
146#define ATA_COMMAND_SMART                        0xb0
147#define ATA_COMMAND_READ_DMA_QUEUED              0xc7
148#define ATA_COMMAND_WRITE_DMA_QUEUED             0xcc
149#define ATA_COMMAND_GET_MEDIA_STATUS             0xda
150#define ATA_COMMAND_FLUSH_CACHE                  0xe7
151#define ATA_COMMAND_READ_NATIVE_MAX_ADDRESS      0xf8
152#define ATA_COMMAND_SET_MAX_ADDRESS              0xf9
153
154#define ATA_REGISTERS_VALUE(reg)    (1 << (reg))
155
156/* ATA IDENTIFY DEVICE command words and bits */
157#define ATA_IDENT_WORD_RW_MULT                   47
158#define ATA_IDENT_WORD_CAPABILITIES              49
159#define ATA_IDENT_WORD_FIELD_VALIDITY            53
160#define ATA_IDENT_WORD_NUM_OF_CURR_LOG_CLNDS     54
161#define ATA_IDENT_WORD_NUM_OF_CURR_LOG_HEADS     55
162#define ATA_IDENT_WORD_NUM_OF_CURR_LOG_SECS      56
163#define ATA_IDENT_WORD_MULT_SECS                 59
164#define ATA_IDENT_WORD_NUM_OF_USR_SECS0          60
165#define ATA_IDENT_WORD_NUM_OF_USR_SECS1          61
166#define ATA_IDENT_WORD_PIO_SPPRTD                64
167
168#define ATA_IDENT_BIT_VALID                      0x02
169
170/*
171 * It is OR for all ATA_REGISTERS_VALUE(reg), where reg is neccessary
172 * for setting block position
173 */
174#define ATA_REGISTERS_POSITION       0xfc
175
176#define ATA_MINOR_NUM_RESERVED_PER_ATA_DEVICE     64
177
178#define ATA_MAX_RTEMS_INT_VEC_NUMBER              255
179
180#define ATA_MAX_NAME_LENGTH                       10
181
182/* diagnostic codes */
183#define ATA_DEV0_PASSED_DEV1_PASSED_OR_NOT_PRSNT  0x01
184#define ATA_DEV0_PASSED_DEV1_FAILED               0x81
185#define ATA_DEV1_PASSED_DEV0_FAILED               0x80
186
187/*
188 * Obtain ata device parameters by controller minor number and device number
189 */
190#define ATA_DEV_INFO(controller_minor, dev) \
191    ata_ide_ctrls[controller_minor].device[dev]
192
193/* ATA RTEMS driver internal data stuctures */
194
195/* Command block registers */
196typedef struct ata_registers_s {
197    uint16_t   regs[8];  /* command block registers */
198    uint16_t   to_read;  /* mask: which ata registers should be read */
199    uint16_t   to_write; /* mask: which ata registers should be written */
200} ata_registers_t;
201
202/* ATA request */
203typedef struct ata_req_s {
204    rtems_chain_node  link;   /* link in requests chain */
205    char              type;   /* request type */
206    ata_registers_t   regs;   /* ATA command */
207    uint32_t    cnt;    /* Number of sectors to be exchanged */
208    uint32_t    cbuf;   /* number of current buffer from breq in use */
209    uint32_t    pos;    /* current position in 'cbuf' */
210    rtems_blkdev_request *breq;   /* blkdev_request which corresponds to the
211                               * ata request
212                               */
213    rtems_id          sema;   /* semaphore which is used if synchronous
214                               * processing of the ata request is required
215                               */
216    rtems_status_code status; /* status of ata request processing */
217    int               info;  /* device info code */
218} ata_req_t;
219
220/* call callback provided by block device request if it is defined */
221#define ATA_EXEC_CALLBACK(areq, status) \
222    do {\
223        if (((areq)->breq != NULL) && ((areq)->breq->req_done != NULL)) \
224            (areq)->breq->req_done((areq)->breq->done_arg, status); \
225    } while (0)
226
227/* ATA RTEMS driver events types */
228typedef enum ata_msg_type_s {
229    ATA_MSG_GEN_EVT = 1,     /* general event */
230    ATA_MSG_SUCCESS_EVT,     /* success event */
231    ATA_MSG_ERROR_EVT,       /* error event */
232    ATA_MSG_PROCESS_NEXT_EVT /* process next request event */
233} ata_msg_type_t;
234
235/* ATA RTEMS driver message */
236typedef struct ata_queue_msg_s {
237    ata_msg_type_t            type;       /* message type */
238    rtems_device_minor_number ctrl_minor; /* IDE controller minor number */
239    int                       error;      /* error code */
240} ata_queue_msg_t;
241
242/* macros for messages processing */
243#define ATA_FILL_MSG(msg, evt_type, ctrl, err)\
244    do {\
245        msg.type = evt_type;\
246        msg.ctrl_minor = ctrl;\
247        msg.error = err;\
248    } while (0)
249
250#define ATA_SEND_EVT(msg, type, ctrl, err)\
251    do {\
252        rtems_status_code rc;\
253        ATA_FILL_MSG(msg, type, ctrl, err);\
254        rc = rtems_message_queue_send(ata_queue_id, &msg,\
255                                      sizeof(ata_queue_msg_t));\
256        if (rc != RTEMS_SUCCESSFUL)\
257            rtems_fatal_error_occurred(RTEMS_INTERNAL_ERROR);\
258    } while (0)
259
260/*
261 * Array of such structures is indexed by interrupt vecotrs and used for
262 * mapping of IDE controllers and interrupt vectors
263 */
264typedef struct ata_int_st_s {
265    rtems_chain_node          link;
266    rtems_device_minor_number ctrl_minor;
267} ata_int_st_t;
268
269/*
270 * Mapping of rtems ATA devices to the following pairs:
271 * (IDE controller number served the device, device number on the controller)
272 */
273typedef struct ata_ide_dev_s {
274    int ctrl_minor;/* minor number of IDE controller served rtems ATA device */
275    int device;    /* device number on IDE controller (0 or 1) */
276} ata_ide_dev_t;
277
278/*
279 * ATA device description
280 */
281typedef struct ata_dev_s {
282    int8_t      present;     /* 1 -- present, 0 -- not present, */
283                             /* -1 -- non-initialized */
284    uint16_t    cylinders;
285    uint16_t    heads;
286    uint16_t    sectors;
287    uint32_t    lba_sectors;  /* for small disk */
288                              /* == cylinders * heads * sectors */
289
290    uint8_t     lba_avaible;  /* 0 - CHS mode, 1 - LBA mode */
291
292    uint16_t  modes_available; /* OR of values for this modes */
293    uint16_t  mode_active;
294} ata_dev_t;
295
296/*
297 * This structure describes controller state, devices configuration on the
298 * controller and chain of ATA requests to the controller. Array of such
299 * structures is indexed by controller minor number
300 */
301typedef struct ata_ide_ctrl_s {
302    bool          present;   /* controller state */
303    ata_dev_t     device[2]; /* ata diveces description */
304    rtems_chain_control reqs; /* requests chain */
305} ata_ide_ctrl_t;
306
307/* Block device request with a single buffer provided */
308typedef struct blkdev_request1 {
309    rtems_blkdev_request   req;
310    rtems_blkdev_sg_buffer sg[1];
311} blkdev_request1;
312
313void ata_breq_init(blkdev_request1 *breq, uint16_t *sector_buffer);
314
315rtems_status_code ata_identify_device(
316  rtems_device_minor_number ctrl_minor,
317  int dev,
318  uint16_t *sector_buffer,
319  ata_dev_t *device_entry
320);
321
322void ata_process_request_on_init_phase(
323  rtems_device_minor_number ctrl_minor,
324  ata_req_t *areq
325);
326
327#ifdef __cplusplus
328}
329#endif
330
331#endif /* __ATA_INTERNAL_H__ */
Note: See TracBrowser for help on using the repository browser.