[75] | 1 | /* |
---|
[215] | 2 | * ioc_ata.c - ATA driver implementation |
---|
[75] | 3 | * |
---|
[215] | 4 | * Copyright (c) 2017 Maxime Villard |
---|
[75] | 5 | * |
---|
| 6 | * This file is part of ALMOS-MKH. |
---|
| 7 | * |
---|
[215] | 8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[75] | 9 | * under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 11 | * |
---|
[215] | 12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[75] | 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 15 | * General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
[215] | 18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[75] | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include <chdev.h> |
---|
| 23 | #include <dev_ioc.h> |
---|
[215] | 24 | #include <hal_drivers.h> |
---|
[75] | 25 | #include <thread.h> |
---|
| 26 | #include <spinlock.h> |
---|
| 27 | |
---|
[194] | 28 | #include <hal_internal.h> |
---|
| 29 | |
---|
| 30 | #define PIO_ATA_CBR_BASE 0x1F0 |
---|
| 31 | # define ATA_DATA 0x000 |
---|
| 32 | # define ATA_ERRFEAT 0x001 /* two regs */ |
---|
| 33 | # define ATA_SCR 0x002 |
---|
| 34 | # define ATA_SNR 0x003 /* lba0 */ |
---|
| 35 | # define ATA_CLR 0x004 /* lba1 */ |
---|
| 36 | # define ATA_CHR 0x005 /* lba2 */ |
---|
| 37 | # define ATA_DHR 0x006 /* drive | lba3 */ |
---|
| 38 | # define ATA_SR 0x007 |
---|
| 39 | # define ATA_SR_ERR 0x01 |
---|
| 40 | # define ATA_SR_IDX 0x02 |
---|
| 41 | # define ATA_SR_CORR 0x04 |
---|
| 42 | # define ATA_SR_DRQ 0x08 |
---|
| 43 | # define ATA_SR_DSC 0x10 |
---|
| 44 | # define ATA_SR_DF 0x20 |
---|
| 45 | # define ATA_SR_DRDY 0x40 |
---|
| 46 | # define ATA_SR_BSY 0x80 |
---|
| 47 | # define ATA_CR 0x007 /* two regs */ |
---|
| 48 | |
---|
| 49 | #define ATA_CMD_READ_SECTORS_RETRY 0x20 |
---|
| 50 | #define ATA_CMD_READ_SECTORS_NORETRY 0x21 |
---|
| 51 | #define ATA_CMD_WRITE_SECTORS_RETRY 0x30 |
---|
| 52 | #define ATA_CMD_WRITE_SECTORS_NORETRY 0x31 |
---|
| 53 | #define ATA_CMD_IDENTIFY 0xEC |
---|
| 54 | |
---|
| 55 | static inline uint16_t ata_data_read() |
---|
[75] | 56 | { |
---|
[194] | 57 | return in16(PIO_ATA_CBR_BASE + ATA_DATA); |
---|
| 58 | } |
---|
[75] | 59 | |
---|
[194] | 60 | static inline void ata_data_write(uint16_t val) |
---|
| 61 | { |
---|
| 62 | out16(PIO_ATA_CBR_BASE + ATA_DATA, val); |
---|
[76] | 63 | } |
---|
[75] | 64 | |
---|
[194] | 65 | static inline uint8_t ata_cbr_read(uint32_t reg) |
---|
| 66 | { |
---|
| 67 | return in8(PIO_ATA_CBR_BASE + reg); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | static inline void ata_cbr_write(uint32_t reg, uint8_t val) |
---|
| 71 | { |
---|
| 72 | out8(PIO_ATA_CBR_BASE + reg, val); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | static inline int ata_wait() |
---|
| 76 | { |
---|
| 77 | uint8_t status; |
---|
| 78 | |
---|
| 79 | while (!(ata_cbr_read(ATA_SR) & ATA_SR_DRQ)); |
---|
| 80 | status = ata_cbr_read(ATA_SR); |
---|
| 81 | return ((status & ATA_SR_ERR) ? -1 : 0); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | static void ata_prepare(uint8_t slave, uint32_t lba, uint8_t count) |
---|
| 85 | { |
---|
| 86 | ata_cbr_write(ATA_ERRFEAT, 0x00); /* NULL byte to port 0x1F1 */ |
---|
| 87 | ata_cbr_write(ATA_SCR, count); /* sector count */ |
---|
| 88 | |
---|
| 89 | /* set the lba */ |
---|
| 90 | ata_cbr_write(ATA_SNR, (lba >> 0) & 0xFF); |
---|
| 91 | ata_cbr_write(ATA_CLR, (lba >> 8) & 0xFF); |
---|
| 92 | ata_cbr_write(ATA_CHR, (lba >> 16)& 0xFF); |
---|
| 93 | |
---|
| 94 | /* set the drive and lba3 */ |
---|
| 95 | ata_cbr_write(ATA_DHR, 0xE0 | (slave << 4) | ((lba >> 24) & 0x0F)); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | static int ata_read(uint8_t count, char *buf) |
---|
| 99 | { |
---|
| 100 | uint16_t tmpword; |
---|
| 101 | int idx; |
---|
| 102 | |
---|
| 103 | /* wait for the drive to signal that it's ready */ |
---|
| 104 | if (ata_wait() == -1) |
---|
[210] | 105 | x86_panic("ata_wait"); |
---|
[194] | 106 | |
---|
| 107 | for (idx = 0; idx < 256 * count; idx++) { |
---|
| 108 | tmpword = ata_data_read(); |
---|
| 109 | buf[idx * 2] = (uint8_t)(tmpword & 0xFF); |
---|
| 110 | buf[idx * 2 + 1] = (uint8_t)(tmpword >> 8); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | return 0; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | static int ata_write(uint8_t count, char *buf) |
---|
| 117 | { |
---|
| 118 | uint16_t tmpword; |
---|
| 119 | int idx; |
---|
| 120 | |
---|
| 121 | /* wait for the drive to signal that it's ready */ |
---|
| 122 | if (ata_wait() == -1) |
---|
[210] | 123 | x86_panic("ata_wait"); |
---|
[194] | 124 | |
---|
| 125 | for (idx = 0; idx < 256 * count; idx++) { |
---|
| 126 | tmpword = (buf[idx * 2 + 1] << 8) | buf[idx * 2]; |
---|
| 127 | ata_data_write(tmpword); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | return 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | static uint16_t bswap16(uint16_t x) |
---|
| 134 | { |
---|
| 135 | return ((x << 8) & 0xFF00) | ((x >> 8) & 0x00FF); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | static void ata_init() |
---|
| 139 | { |
---|
| 140 | uint8_t id[512]; |
---|
| 141 | uint16_t tmpw, *p; |
---|
| 142 | size_t idx; |
---|
| 143 | char *model; |
---|
| 144 | uint8_t ncyl; |
---|
| 145 | int ret; |
---|
| 146 | |
---|
| 147 | ata_prepare(0, 0, 0); |
---|
| 148 | ata_cbr_write(ATA_CR, ATA_CMD_IDENTIFY); |
---|
| 149 | |
---|
| 150 | /* wait for the drive to signal that it's ready */ |
---|
| 151 | ret = ata_wait(); |
---|
| 152 | if (ret == -1) |
---|
| 153 | x86_printf("-> unable to identify ATA\n"); |
---|
| 154 | |
---|
| 155 | for (idx = 0; idx < 256; idx++) { |
---|
| 156 | tmpw = ata_data_read(); |
---|
| 157 | id[idx * 2] = (uint8_t)((tmpw >> 0) & 0xFF); |
---|
| 158 | id[idx * 2 + 1] = (uint8_t)((tmpw >> 8) & 0xFF); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | ncyl = id[0] & 0x1; |
---|
| 162 | x86_printf("-> cyl: %z\n", (uint64_t)ncyl); |
---|
| 163 | |
---|
| 164 | for (idx = 27*2; idx < 46*2; idx += 2) { |
---|
| 165 | p = (uint16_t *)(&id[idx]); |
---|
| 166 | *p = bswap16(*p); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | model = &id[27*2]; |
---|
| 170 | id[46*2] = '\0'; |
---|
| 171 | x86_printf("-> ATA model: '%s'\n", model); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /* -------------------------------------------------------------------------- */ |
---|
| 175 | |
---|
[215] | 176 | static void ioc_ata_cmd(xptr_t th_xp); |
---|
| 177 | static void ioc_ata_isr(chdev_t *chdev); |
---|
| 178 | |
---|
| 179 | void ioc_ata_init(chdev_t *chdev) |
---|
[194] | 180 | { |
---|
[215] | 181 | chdev->cmd = &ioc_ata_cmd; |
---|
| 182 | chdev->isr = &ioc_ata_isr; |
---|
[194] | 183 | ata_init(); |
---|
| 184 | } |
---|
| 185 | |
---|
[215] | 186 | static void ioc_ata_cmd(xptr_t th_xp) |
---|
[75] | 187 | { |
---|
[194] | 188 | uint32_t cmd_type; // IOC_READ / IOC_WRITE / IOC_SYNC_READ |
---|
| 189 | uint32_t lba; // command argument |
---|
| 190 | uint32_t count; // command argument |
---|
| 191 | xptr_t buf_xp; // command argument |
---|
| 192 | |
---|
| 193 | // get client thread cluster and local pointer |
---|
| 194 | cxy_t th_cxy = GET_CXY( th_xp ); |
---|
| 195 | thread_t * th_ptr = (thread_t *)GET_PTR( th_xp ); |
---|
| 196 | |
---|
| 197 | // get command arguments and extended pointer on IOC device |
---|
| 198 | cmd_type = hal_remote_lw ( XPTR( th_cxy , &th_ptr->command.ioc.type ) ); |
---|
| 199 | lba = hal_remote_lw ( XPTR( th_cxy , &th_ptr->command.ioc.lba ) ); |
---|
| 200 | count = hal_remote_lw ( XPTR( th_cxy , &th_ptr->command.ioc.count ) ); |
---|
| 201 | buf_xp = (xptr_t)hal_remote_lwd( XPTR( th_cxy , &th_ptr->command.ioc.buf_xp ) ); |
---|
| 202 | |
---|
| 203 | /* execute operation */ |
---|
| 204 | ata_prepare(0, lba, count); |
---|
| 205 | if (cmd_type == IOC_WRITE) { |
---|
| 206 | ata_cbr_write(ATA_CR, ATA_CMD_WRITE_SECTORS_RETRY); |
---|
| 207 | } else { |
---|
| 208 | ata_cbr_write(ATA_CR, ATA_CMD_READ_SECTORS_RETRY); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /* waiting policy depends on the command type */ |
---|
| 212 | if (cmd_type == IOC_SYNC_READ) { |
---|
| 213 | ata_read(count, (char *)buf_xp); |
---|
| 214 | } else { |
---|
| 215 | x86_panic("!IOC_SYNC_READ not supported"); |
---|
| 216 | } |
---|
[76] | 217 | } |
---|
[75] | 218 | |
---|
[215] | 219 | static void ioc_ata_isr(chdev_t *chdev) |
---|
[75] | 220 | { |
---|
[203] | 221 | x86_panic((char *)__func__); |
---|
[76] | 222 | } |
---|
[75] | 223 | |
---|