| 1 | /* $Id: atalib.h 76 2015-12-28 01:37:00Z bird $ */
 | 
|---|
| 2 | /** @file
 | 
|---|
| 3 |  * Does a little write test.
 | 
|---|
| 4 |  */
 | 
|---|
| 5 | 
 | 
|---|
| 6 | /*
 | 
|---|
| 7 |  * Copyright (c) 2015 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  * This program is free software; you can redistribute it and/or modify
 | 
|---|
| 10 |  * it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  * the Free Software Foundation; either version 3 of the License, or
 | 
|---|
| 12 |  * (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  * This program is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  * GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  * You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  * along with This program.  If not, see <http://www.gnu.org/licenses/>
 | 
|---|
| 21 |  *
 | 
|---|
| 22 |  */
 | 
|---|
| 23 | 
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #ifndef ___atalib_h___
 | 
|---|
| 26 | #define ___atalib_h___
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include <stdint.h>
 | 
|---|
| 29 | 
 | 
|---|
| 30 | /* The necessary I/O ports, indexed by "bus". */
 | 
|---|
| 31 | #define ATA_PORT_SHIFT      (g_cPortShift) /* For XT-CF trick */
 | 
|---|
| 32 | #define ATA_REG_DATA(a_uBasePort)           (a_uBasePort)
 | 
|---|
| 33 | #define ATA_REG_FEATURES(a_uBasePort)       ((a_uBasePort) + (1 << ATA_PORT_SHIFT))
 | 
|---|
| 34 | #define ATA_REG_SECTOR_COUNT(a_uBasePort)   ((a_uBasePort) + (2 << ATA_PORT_SHIFT))
 | 
|---|
| 35 | 
 | 
|---|
| 36 | #define ATA_REG_SECTOR_NUMBER(a_uBasePort)  ((a_uBasePort) + (3 << ATA_PORT_SHIFT))
 | 
|---|
| 37 | #define ATA_REG_CYLINDER_LOW(a_uBasePort)   ((a_uBasePort) + (4 << ATA_PORT_SHIFT))
 | 
|---|
| 38 | #define ATA_REG_CYLINDER_HIGH(a_uBasePort)  ((a_uBasePort) + (5 << ATA_PORT_SHIFT))
 | 
|---|
| 39 | #define ATA_REG_HEAD(a_uBasePort)           ((a_uBasePort) + (6 << ATA_PORT_SHIFT))
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #define ATA_REG_LBA_0_7(a_uBasePort)        ((a_uBasePort) + (3 << ATA_PORT_SHIFT))
 | 
|---|
| 42 | #define ATA_REG_LBA_8_15(a_uBasePort)       ((a_uBasePort) + (4 << ATA_PORT_SHIFT))
 | 
|---|
| 43 | #define ATA_REG_LBA_16_23(a_uBasePort)      ((a_uBasePort) + (5 << ATA_PORT_SHIFT))
 | 
|---|
| 44 | #define ATA_REG_LBA_24_27_MODE(a_uBasePort) ((a_uBasePort) + (6 << ATA_PORT_SHIFT))
 | 
|---|
| 45 | #define ATA_LBA_MODE                        UINT8_C(0x40) /**< Selects LBA mode in ATA_REG_LBA_24_27_MODE. */
 | 
|---|
| 46 | 
 | 
|---|
| 47 | #define ATA_REG_DEVICE_SELECT(a_uBasePort)  ((a_uBasePort) + (6 << ATA_PORT_SHIFT))
 | 
|---|
| 48 | #define ATA_REG_COMMAND(a_uBasePort)        ((a_uBasePort) + (7 << ATA_PORT_SHIFT))
 | 
|---|
| 49 | 
 | 
|---|
| 50 | 
 | 
|---|
| 51 | #define ATA_REG_STATUS(a_uBasePort)         ATA_REG_COMMAND(a_uBasePort)
 | 
|---|
| 52 | #define ATA_REG_ALT_STATUS(a_uCtrlPort)     ATA_REG_CONTROL(a_uCtrlPort)
 | 
|---|
| 53 | #define ATA_STS_BUSY                        UINT8_C(0x80)
 | 
|---|
| 54 | #define ATA_STS_DRDY                        UINT8_C(0x40)
 | 
|---|
| 55 | #define ATA_STS_DF                          UINT8_C(0x20)
 | 
|---|
| 56 | #define ATA_STS_DSC                         UINT8_C(0x10)
 | 
|---|
| 57 | #define ATA_STS_DRQ                         UINT8_C(0x08)
 | 
|---|
| 58 | #define ATA_STS_CORR                        UINT8_C(0x04)
 | 
|---|
| 59 | #define ATA_STS_IDX                         UINT8_C(0x02)
 | 
|---|
| 60 | #define ATA_STS_ERR                         UINT8_C(0x01)
 | 
|---|
| 61 | 
 | 
|---|
| 62 | #define ATA_REG_ERROR(a_uBasePort)          ATA_REG_FEATURES(a_uBasePort)
 | 
|---|
| 63 | #define ATA_ERR_RSVR                        UINT8_C(0x80)
 | 
|---|
| 64 | #define ATA_ERR_UNC                         UINT8_C(0x40)
 | 
|---|
| 65 | #define ATA_ERR_MC                          UINT8_C(0x20)
 | 
|---|
| 66 | #define ATA_ERR_IDNF                        UINT8_C(0x10)
 | 
|---|
| 67 | #define ATA_ERR_MCR                         UINT8_C(0x08)
 | 
|---|
| 68 | #define ATA_ERR_ABRT                        UINT8_C(0x04)
 | 
|---|
| 69 | #define ATA_ERR_TKNONF                      UINT8_C(0x02)
 | 
|---|
| 70 | #define ATA_ERR_AMNF                        UINT8_C(0x01)
 | 
|---|
| 71 | 
 | 
|---|
| 72 | #define ATA_REG_CONTROL(a_uCtrlPort)        ((a_uCtrlPort) + (6 << ATA_PORT_SHIFT))
 | 
|---|
| 73 | #define ATA_CTL_IEN                         UINT8_C(0x02) /**< Interrupt enable. */
 | 
|---|
| 74 | #define ATA_CTL_SRST                        UINT8_C(0x04) /**< software reset */
 | 
|---|
| 75 | 
 | 
|---|
| 76 | #define ATA_CMD_NOP                         UINT8_C(0x00)
 | 
|---|
| 77 | #define ATA_CMD_READ_SECTORS                UINT8_C(0x20)
 | 
|---|
| 78 | #define ATA_CMD_READ_SECTORS_NR             UINT8_C(0x21)
 | 
|---|
| 79 | #define ATA_CMD_READ_LONG                   UINT8_C(0x22)
 | 
|---|
| 80 | #define ATA_CMD_READ_LONG_NR                UINT8_C(0x23)
 | 
|---|
| 81 | #define ATA_CMD_READ_BUFFER                 UINT8_C(0xe4)
 | 
|---|
| 82 | #define ATA_CMD_WRITE_SECTORS               UINT8_C(0x30)
 | 
|---|
| 83 | #define ATA_CMD_WRITE_SECTORS_NR            UINT8_C(0x31)
 | 
|---|
| 84 | #define ATA_CMD_WRITE_LONG                  UINT8_C(0x32)
 | 
|---|
| 85 | #define ATA_CMD_WRITE_LONG_NR               UINT8_C(0x33)
 | 
|---|
| 86 | #define ATA_CMD_WRITE_BUFFER                UINT8_C(0xe8)
 | 
|---|
| 87 | #define ATA_CMD_INIT_DEVICE_PARAMS          UINT8_C(0x91)
 | 
|---|
| 88 | #define ATA_CMD_SET_FEATURES                UINT8_C(0xef)
 | 
|---|
| 89 | #define ATA_CMD_IDENTIFY_DEVICE             UINT8_C(0xec)
 | 
|---|
| 90 | 
 | 
|---|
| 91 | #define ATA_DEV_MASTER                      UINT8_C(0x00)  /**< Master device selection bit value. */
 | 
|---|
| 92 | #define ATA_DEV_SLAVE                       UINT8_C(0x10)  /**< Slave device selection bit value. */
 | 
|---|
| 93 | 
 | 
|---|
| 94 | #define ATA_FEATURE_EN_8BIT_DATA            UINT8_C(0x01)
 | 
|---|
| 95 | #define ATA_FEATURE_DI_8BIT_DATA            UINT8_C(0x81)
 | 
|---|
| 96 | #define ATA_FEATURE_EN_WRITE_CACHE          UINT8_C(0x02)
 | 
|---|
| 97 | #define ATA_FEATURE_DI_WRITE_CACHE          UINT8_C(0x82)
 | 
|---|
| 98 | #define ATA_FEATURE_SET_XFERMODE            UINT8_C(0x03)
 | 
|---|
| 99 | #define ATA_FV_XFERMODE_PIO_MODE_DEFAULT             UINT8_C(0x00)
 | 
|---|
| 100 | #define ATA_FV_XFERMODE_PIO_MODE_DEFAULT_NO_IORDY    UINT8_C(0x01)
 | 
|---|
| 101 | #define ATA_FV_XFERMODE_PIO_MODE_XXX_FLAG            UINT8_C(0x08)
 | 
|---|
| 102 | #define ATA_FV_XFERMODE_SWDMA_MODE_XXX_FLAG          UINT8_C(0x10)
 | 
|---|
| 103 | #define ATA_FV_XFERMODE_MWDMA_MODE_XXX_FLAG          UINT8_C(0x20)
 | 
|---|
| 104 | 
 | 
|---|
| 105 | 
 | 
|---|
| 106 | #define ATA_PIT_FREQ                        UINT32_C(1193182)
 | 
|---|
| 107 | void AtaDelayPitTicks(uint8_t cTicks);
 | 
|---|
| 108 | #pragma aux AtaDelayPitTicks = \
 | 
|---|
| 109 |     "pushf" \
 | 
|---|
| 110 |     "cli" \
 | 
|---|
| 111 |     "in     al, 40h" \
 | 
|---|
| 112 |     "mov    ch, al" /* ch = prev */ \
 | 
|---|
| 113 |  "again:" \
 | 
|---|
| 114 |     "nop" \
 | 
|---|
| 115 |     "in     al, 40h" \
 | 
|---|
| 116 |     "sub    ch, al" /* ch = ticks elapsed */ \
 | 
|---|
| 117 |     "sub    cl, ch" /* check if ticks left is <= ticks elapsed; update ticks left. */ \
 | 
|---|
| 118 |     "mov    ch, al" /* ch = prev tick count */ \
 | 
|---|
| 119 |     "ja     again" \
 | 
|---|
| 120 |     "popf" \
 | 
|---|
| 121 |     parm [cl] \
 | 
|---|
| 122 |     modify exact [ax cx];
 | 
|---|
| 123 | 
 | 
|---|
| 124 | /** Delay a bit by reading PIC mask. Should take 4-5 bus cycles,
 | 
|---|
| 125 |  * and thus be more than the required 400ns delay on old computers. */
 | 
|---|
| 126 | #if 0
 | 
|---|
| 127 | # define ATA_DELAY_400NS()   do { inp(0x21); } while (0)
 | 
|---|
| 128 | # define ATA_DELAY_5US()     \
 | 
|---|
| 129 |     do { ATA_DELAY_400NS(); ATA_DELAY_400NS(); ATA_DELAY_400NS(); ATA_DELAY_400NS(); ATA_DELAY_400NS(); \
 | 
|---|
| 130 |          ATA_DELAY_400NS(); ATA_DELAY_400NS(); ATA_DELAY_400NS(); ATA_DELAY_400NS(); ATA_DELAY_400NS(); \
 | 
|---|
| 131 |          ATA_DELAY_400NS(); ATA_DELAY_400NS(); ATA_DELAY_400NS(); \
 | 
|---|
| 132 |     } while (0)
 | 
|---|
| 133 | #else
 | 
|---|
| 134 | # define ATA_DELAY_400NS()   AtaDelayPitTicks(1)
 | 
|---|
| 135 | # define ATA_DELAY_5US()     AtaDelayPitTicks(6)
 | 
|---|
| 136 | #endif
 | 
|---|
| 137 | 
 | 
|---|
| 138 | 
 | 
|---|
| 139 | extern uint16_t g_cHeads;
 | 
|---|
| 140 | extern uint8_t  g_cSectorsPerTrack;
 | 
|---|
| 141 | extern uint16_t g_cCylinders;
 | 
|---|
| 142 | extern uint16_t g_cSectorsPerCylinder;
 | 
|---|
| 143 | extern uint16_t g_awIdentify[256];
 | 
|---|
| 144 | 
 | 
|---|
| 145 | int  AtaInit(uint16_t uBasePort, uint16_t uCtrlPort, uint8_t uPortShift, uint8_t bDevice, uint8_t f8BitData, uint8_t fReset);
 | 
|---|
| 146 | int  AtaInitFromArgv(int iArg, int cArgs, char **papszArgs);
 | 
|---|
| 147 | int  AtaReInit(void);
 | 
|---|
| 148 | int  AtaReset(void);
 | 
|---|
| 149 | int  AtaIdentifyDevice(uint8_t bDevice, void *pvBuf);
 | 
|---|
| 150 | int  AtaInitDeviceParams(uint8_t bDevice, uint8_t cSectorsPerTrack, uint8_t cHeads);
 | 
|---|
| 151 | int  AtaSetFeature(uint8_t bDevice, uint8_t bFeature, uint8_t bValue);
 | 
|---|
| 152 | int  AtaWriteSector(uint32_t iSector, void const *pvBuf);
 | 
|---|
| 153 | int  AtaReadSector(uint32_t iSector, void *pvBuf);
 | 
|---|
| 154 | int  AtaWriteBuffer(void const *pvBuf, uint8_t fExtraChecks);
 | 
|---|
| 155 | int  AtaReadBuffer(void *pvBuf, uint8_t fExtraChecks);
 | 
|---|
| 156 | void AtaWriteData(void const *pvBuf, size_t cb, uint8_t f8BitData);
 | 
|---|
| 157 | void AtaReadData(void *pvBuf, size_t cb, uint8_t f8BitData);
 | 
|---|
| 158 | 
 | 
|---|
| 159 | #endif
 | 
|---|
| 160 | 
 | 
|---|