Subversion Repositories HelenOS

Rev

Rev 4529 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4507 svoboda 1
/*
2
 * Copyright (c) 2009 Jiri Svoboda
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup bd
30
 * @{
31
 */
32
 
33
/**
34
 * @file
35
 * @brief ATA disk driver
36
 *
37
 * This driver currently works only with CHS addressing and uses PIO.
38
 * Currently based on the (now obsolete) ANSI X3.221-1994 (ATA-1) standard.
39
 * At this point only reading is possible, not writing.
40
 */
41
 
42
#include <stdio.h>
43
#include <libarch/ddi.h>
44
#include <ddi.h>
45
#include <ipc/ipc.h>
46
#include <ipc/bd.h>
47
#include <async.h>
48
#include <as.h>
49
#include <futex.h>
50
#include <devmap.h>
51
#include <sys/types.h>
52
#include <errno.h>
53
 
54
#define NAME "ata_bd"
55
 
56
enum {
57
    CTL_READ_START  = 0,
58
    CTL_WRITE_START = 1,
59
};
60
 
61
enum {
62
    STATUS_FAILURE  = 0
63
};
64
 
65
enum {
66
    MAX_DISKS   = 2
67
};
68
 
69
typedef union {
70
    /* Read */
71
    struct {
72
        uint8_t data_port;
73
        uint8_t error;
74
        uint8_t sector_count;
75
        uint8_t sector_number;
76
        uint8_t cylinder_low;
77
        uint8_t cylinder_high;
78
        uint8_t drive_head;
79
        uint8_t status;
80
    };
81
 
82
    /* Write */
83
    struct {
84
        uint8_t pad0[7];
85
        uint8_t command;
86
    };
87
} ata_cmd_t;
88
 
89
typedef union {
90
    /* Read */
91
    struct {
92
        uint8_t pad0[6];
93
        uint8_t alt_status;
94
        uint8_t drive_address;
95
    };
96
 
97
    /* Write */
98
    struct {
99
        uint8_t pad1[6];
100
        uint8_t device_control;
101
        uint8_t pad2;
102
    };
103
} ata_ctl_t;
104
 
105
enum devctl_bits {
106
    DCR_SRST    = 0x04, /**< Software Reset */
107
    DCR_nIEN    = 0x02  /**< Interrupt Enable (negated) */
108
};
109
 
110
enum status_bits {
111
    SR_BSY      = 0x80, /**< Busy */
112
    SR_DRDY     = 0x40, /**< Drive Ready */
113
    SR_DWF      = 0x20, /**< Drive Write Fault */
114
    SR_DSC      = 0x10, /**< Drive Seek Complete */
115
    SR_DRQ      = 0x08, /**< Data Request */
116
    SR_CORR     = 0x04, /**< Corrected Data */
117
    SR_IDX      = 0x02, /**< Index */
118
    SR_ERR      = 0x01  /**< Error */
119
};
120
 
121
enum drive_head_bits {
122
    DHR_DRV     = 0x10
123
};
124
 
125
enum error_bits {
126
    ER_BBK      = 0x80, /**< Bad Block Detected */
127
    ER_UNC      = 0x40, /**< Uncorrectable Data Error */
128
    ER_MC       = 0x20, /**< Media Changed */
129
    ER_IDNF     = 0x10, /**< ID Not Found */
130
    ER_MCR      = 0x08, /**< Media Change Request */
131
    ER_ABRT     = 0x04, /**< Aborted Command */
132
    ER_TK0NF    = 0x02, /**< Track 0 Not Found */
133
    ER_AMNF     = 0x01  /**< Address Mark Not Found */
134
};
135
 
136
static const size_t block_size = 512;
137
static size_t comm_size;
138
 
139
static uintptr_t cmd_physical = 0x1f0;
140
static uintptr_t ctl_physical = 0x170;
141
static ata_cmd_t *cmd;
142
static ata_ctl_t *ctl;
143
 
144
static dev_handle_t dev_handle[MAX_DISKS];
145
 
146
static atomic_t dev_futex = FUTEX_INITIALIZER;
147
 
148
static unsigned heads, cylinders, sectors;
149
static uint64_t disk_blocks;
150
 
151
static int ata_bd_init(void);
152
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
153
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t offset, off_t size,
154
    void *buf);
155
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
156
    void *buf);
157
 
158
int main(int argc, char **argv)
159
{
160
    uint8_t status;
161
    uint16_t data;
162
    int i;
163
 
164
    printf(NAME ": ATA disk driver\n");
165
 
166
    printf("cmd_physical = 0x%x\n", cmd_physical);
167
    printf("ctl_physical = 0x%x\n", ctl_physical);
168
 
169
    if (ata_bd_init() != EOK)
170
        return -1;
171
 
172
    /* Put drives to reset, disable interrupts. */
173
    printf("Reset drives...\n");
174
    pio_write_8(&ctl->device_control, DCR_SRST);
175
/*  printf("wait for busy\n");
176
    do {
177
        status = pio_read_8(&cmd->status);
178
    } while ((status & SR_BSY) == 0);
179
*/
180
    async_usleep(100);
181
    pio_write_8(&ctl->device_control, 0);
182
 
183
    do {
184
        status = pio_read_8(&cmd->status);
185
    } while ((status & SR_BSY) != 0);
186
    printf("Done\n");
187
 
188
    printf("Status = 0x%x\n", pio_read_8(&cmd->status));
189
 
190
    printf("Issue drive_identify command\n");
191
    pio_write_8(&cmd->drive_head, 0x00);
192
    async_usleep(100);
193
    pio_write_8(&cmd->command, 0xEC);
194
 
195
    printf("Status = 0x%x\n", pio_read_8(&cmd->status));
196
 
197
    for (i = 0; i < 256; i++) {
198
        do {
199
            status = pio_read_8(&cmd->status);
200
        } while ((status & SR_DRDY) == 0);
201
 
202
        data = pio_read_16(&cmd->data_port);
203
 
204
        switch (i) {
205
        case 1: cylinders = data; break;
206
        case 3: heads = data; break;
207
        case 6: sectors = data; break;
208
        }
209
    }
210
 
211
    printf("\n\nStatus = 0x%x\n", pio_read_8(&cmd->status));
212
 
213
    printf("Geometry: %u cylinders, %u heads, %u sectors\n",
214
        cylinders, heads, sectors);
215
    disk_blocks = cylinders * heads * sectors;
216
 
217
    printf(NAME ": Accepting connections\n");
218
    async_manager();
219
 
220
    /* Not reached */
221
    return 0;
222
}
223
 
224
static int ata_bd_init(void)
225
{
226
    void *vaddr;
227
    int rc, i;
228
    char name[16];
229
 
230
    rc = devmap_driver_register(NAME, ata_bd_connection);
231
    if (rc < 0) {
232
        printf(NAME ": Unable to register driver.\n");
233
        return rc;
234
    }
235
 
236
    rc = pio_enable((void *) cmd_physical, sizeof(ata_cmd_t), &vaddr);
237
    if (rc != EOK) {
238
        printf(NAME ": Could not initialize device I/O space.\n");
239
        return rc;
240
    }
241
 
242
    cmd = vaddr;
243
 
244
    rc = pio_enable((void *) ctl_physical, sizeof(ata_ctl_t), &vaddr);
245
    if (rc != EOK) {
246
        printf(NAME ": Could not initialize device I/O space.\n");
247
        return rc;
248
    }
249
 
250
    ctl = vaddr;
251
 
252
    for (i = 0; i < MAX_DISKS; i++) {
253
        snprintf(name, 16, "disk%d", i);
254
        rc = devmap_device_register(name, &dev_handle[i]);
255
        if (rc != EOK) {
256
            devmap_hangup_phone(DEVMAP_DRIVER);
257
            printf(NAME ": Unable to register device %s.\n",
258
                name);
259
            return rc;
260
        }
261
    }
262
 
263
    return EOK;
264
}
265
 
266
static void ata_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
267
{
268
    void *fs_va = NULL;
269
    ipc_callid_t callid;
270
    ipc_call_t call;
271
    ipcarg_t method;
272
    dev_handle_t dh;
273
    int flags;
274
    int retval;
275
    off_t idx;
276
    off_t size;
277
    int disk_id, i;
278
 
279
    /* Get the device handle. */
280
    dh = IPC_GET_ARG1(*icall);
281
 
282
    /* Determine which disk device is the client connecting to. */
283
    disk_id = -1;
284
    for (i = 0; i < MAX_DISKS; i++)
285
        if (dev_handle[i] == dh)
286
            disk_id = i;
287
 
288
    if (disk_id < 0) {
289
        ipc_answer_0(iid, EINVAL);
290
        return;
291
    }
292
 
293
    /* Answer the IPC_M_CONNECT_ME_TO call. */
294
    ipc_answer_0(iid, EOK);
295
 
296
    if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
297
        ipc_answer_0(callid, EHANGUP);
298
        return;
299
    }
300
 
301
    fs_va = as_get_mappable_page(comm_size);
302
    if (fs_va == NULL) {
303
        ipc_answer_0(callid, EHANGUP);
304
        return;
305
    }
306
 
307
    (void) ipc_share_out_finalize(callid, fs_va);
308
 
309
    while (1) {
310
        callid = async_get_call(&call);
311
        method = IPC_GET_METHOD(call);
312
        switch (method) {
313
        case IPC_M_PHONE_HUNGUP:
314
            /* The other side has hung up. */
315
            ipc_answer_0(callid, EOK);
316
            return;
317
        case BD_READ_BLOCK:
318
        case BD_WRITE_BLOCK:
319
            idx = IPC_GET_ARG1(call);
320
            size = IPC_GET_ARG2(call);
321
            if (size > comm_size) {
322
                retval = EINVAL;
323
                break;
324
            }
325
            retval = ata_bd_rdwr(disk_id, method, idx,
326
                size, fs_va);
327
            break;
328
        default:
329
            retval = EINVAL;
330
            break;
331
        }
332
        ipc_answer_0(callid, retval);
333
    }
334
}
335
 
336
static int ata_bd_rdwr(int disk_id, ipcarg_t method, off_t blk_idx, off_t size,
337
    void *buf)
338
{
339
    int rc;
340
    off_t now;
341
 
342
    while (size > 0) {
343
        now = size < block_size ? size : (off_t) block_size;
344
        if (now != block_size)
345
            return EINVAL;
346
 
347
        if (method == BD_READ_BLOCK)
348
            rc = ata_bd_read_block(disk_id, blk_idx, 1, buf);
349
        else
350
            rc = ENOTSUP;
351
 
352
        if (rc != EOK)
353
            return rc;
354
 
355
        buf += block_size;
356
        blk_idx++;
357
 
358
        if (size > block_size)
359
            size -= block_size;
360
        else
361
            size = 0;
362
    }
363
 
364
    return EOK;
365
}
366
 
367
 
368
static int ata_bd_read_block(int disk_id, uint64_t blk_idx, size_t blk_cnt,
369
    void *buf)
370
{
371
    size_t i;
372
    uint16_t data;
373
    uint8_t status;
374
    uint64_t c, h, s;
375
    uint64_t idx;
376
    uint8_t drv_head;
377
 
378
    /* Check device bounds. */
379
    if (blk_idx >= disk_blocks)
380
        return EINVAL;
381
 
382
    /* Compute CHS. */
383
    c = blk_idx / (heads * sectors);
384
    idx = blk_idx % (heads * sectors);
385
 
386
    h = idx / sectors;
387
    s = 1 + (idx % sectors);
388
 
389
    /* New value for Drive/Head register */
390
    drv_head =
391
        ((disk_id != 0) ? DHR_DRV : 0) |
392
        (h & 0x0f);
393
 
394
    futex_down(&dev_futex);
395
 
396
    /* Program a Read Sectors operation. */
397
 
398
    pio_write_8(&cmd->drive_head, drv_head);
399
    pio_write_8(&cmd->sector_count, 1);
400
    pio_write_8(&cmd->sector_number, s);
401
    pio_write_8(&cmd->cylinder_low, c & 0xff);
402
    pio_write_8(&cmd->cylinder_high, c >> 16);
403
    pio_write_8(&cmd->command, 0x20);
404
 
405
    /* Read data from the disk buffer. */
406
 
407
    for (i = 0; i < 256; i++) {
408
        do {
409
            status = pio_read_8(&cmd->status);
410
        } while ((status & SR_DRDY) == 0);
411
 
412
        data = pio_read_16(&cmd->data_port);
413
        ((uint16_t *) buf)[i] = data;
414
    }
415
 
416
    futex_up(&dev_futex);
417
    return EOK;
418
}
419
 
420
 
421
/**
422
 * @}
423
 */