KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
g1ata.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 dc/g1ata.h
4 Copyright (C) 2013, 2014 Lawrence Sebald
5 Copyright (C) 2023, 2024, 2025 Ruslan Rostovtsev
6*/
7
8/** \file dc/g1ata.h
9 \brief G1 bus ATA interface.
10 \ingroup g1ata
11
12 This file provides support for accessing an ATA device on the G1 bus in the
13 Dreamcast. The G1 bus usually contains a few useful pieces of the system,
14 including the flashrom and the GD-ROM drive. The interesting piece here is
15 that the GD-ROM drive itself is actually an ATA device.
16
17 Luckily, Sega left everything in place to access both a master and slave
18 device on this ATA port. The GD-ROM drive should always be the master device
19 on the chain, but you can hook up a hard drive or some other device as a
20 slave. The functions herein are for accessing just such a slave device.
21
22 \note The functions herein do not provide for direct access to the GD-ROM
23 drive. There is not really any sort of compelling reason to access
24 the GD-ROM drive directly instead of using the system calls, so you
25 should continue to use the normal cdrom_* functions for accessing
26 the GD-ROM drive. Also, currently there is no locking done to
27 prevent you from doing "bad things" with concurrent access on the
28 bus, so be careful. ;)
29
30 \author Lawrence Sebald
31 \author Ruslan Rostovtsev
32*/
33
34#ifndef __DC_G1ATA_H
35#define __DC_G1ATA_H
36
37#include <sys/cdefs.h>
38__BEGIN_DECLS
39
40#include <stdint.h>
41#include <kos/blockdev.h>
42
43/** \defgroup g1ata G1 ATA
44 \brief Driver for Accessing an ATA device on the G1 Bus
45 \ingroup vfs_drivers
46*/
47
48/** \defgroup ata_devices Device Definitions
49 \brief ATA device definitions
50 \ingroup g1ata
51
52 The constants here represent the valid values that can be set as the active
53 device on the ATA bus. You should pass one of these values to the
54 g1_ata_select_device() function to select the appropriate device.
55
56 \note Many times, the value returned by the g1_ata_select_device()
57 function will have other bits set than the constants below. You
58 should AND the value returned from that function with 0x10 if you
59 really need to know what device is actually selected. If the value
60 returned is true when ANDed with 0x10, then the slave device was
61 selected, otherwise the master device was. The other bits are either
62 command-specific or are reserved for compatibility sake.
63
64 @{
65*/
66/** \brief ATA master device.
67 \ingroup g1ata
68
69 This constant selects the master device on the ATA bus. This is normally the
70 GD-ROM drive.
71
72 \note The GD-ROM really does not like the reserved bits being set in the
73 device select register, hence why this constant doesn't select them.
74 Some hard drives may require them, however. If you find one that
75 does, then you should use the \ref G1_ATA_MASTER_ALT constant to
76 access it if it is the master device on the bus.
77*/
78#define G1_ATA_MASTER 0x00
79
80/** \brief ATA master device (compatible with old drives).
81 \ingroup g1ata
82
83 This constant selects the master device on the ATA bus, with the old
84 reserved bits set to 1. If you have a drive that predates ATA-2, then this
85 will probably be the constant you want to access it as the master device.
86
87 \note Do not use this constant to access the GD-ROM. It will not work. Use
88 \ref G1_ATA_MASTER instead.
89*/
90#define G1_ATA_MASTER_ALT 0x90
91
92/** \brief ATA slave device.
93 \ingroup g1ata
94
95 This constant selects the slave device on the ATA bus. This is where you
96 would find a hard drive, if the user has an adapter installed.
97*/
98#define G1_ATA_SLAVE 0xB0
99
100/** \brief Select LBA addressing mode.
101 \ingroup g1ata
102
103 OR this constant with one of the device constants (\ref G1_ATA_MASTER or
104 \ref G1_ATA_SLAVE) to select LBA addressing mode. The various g1_ata_*
105 functions all do this as appropriate already, so you shouldn't have to worry
106 about this one at all. This bit is irrelevant for packet devices.
107*/
108#define G1_ATA_LBA_MODE 0x40
109/** @} */
110
111/** \defgroup g1ata_protect G1 ATA protection
112 \brief G1 Bus and DMA protection
113 \ingroup g1ata
114 @{
115*/
116/** \brief G1 ATA bus protection register.
117 \ingroup g1ata
118
119 Used to check BIOS contents in the Holly.
120*/
121#define G1_ATA_BUS_PROTECTION 0x005F74E4
122
123/** \brief G1 ATA bus protection status register.
124 \ingroup g1ata
125
126 Used to determine the status of the check in the Holly.
127*/
128#define G1_ATA_BUS_PROTECTION_STATUS 0x005F74EC
129
130/** \brief G1 ATA bus protection in progress state.
131 \ingroup g1ata
132*/
133#define G1_ATA_BUS_PROTECTION_STATUS_IN_PROGRESS 0x00
134
135/** \brief G1 ATA bus protection failed state.
136 \ingroup g1ata
137*/
138#define G1_ATA_BUS_PROTECTION_STATUS_FAILED 0x02
139
140/** \brief G1 ATA bus protection passed state.
141 \ingroup g1ata
142*/
143#define G1_ATA_BUS_PROTECTION_STATUS_PASSED 0x03
144
145/** \brief G1 ATA DMA protection register.
146 \ingroup g1ata
147*/
148#define G1_ATA_DMA_PROTECTION 0x005F74B8
149
150/** \brief G1 ATA DMA protection register code.
151 \ingroup g1ata
152*/
153#define G1_ATA_DMA_UNLOCK_CODE 0x8843
154
155/** \brief System memory DMA protection unlock value.
156 \ingroup g1ata
157*/
158#define G1_ATA_DMA_UNLOCK_SYSMEM (G1_ATA_DMA_UNLOCK_CODE << 16 | 0x407F)
159
160/** \brief All memory DMA protection unlock value.
161 \ingroup g1ata
162*/
163#define G1_ATA_DMA_UNLOCK_ALLMEM (G1_ATA_DMA_UNLOCK_CODE << 16 | 0x007F)
164/** @} */
165
166/** \brief Is there a G1 DMA in progress currently?
167 \ingroup g1ata
168
169 This function returns non-zero if a DMA is in progress. This can be used to
170 check on the completion of DMA transfers when non-blocking mode was selected
171 at transfer time.
172
173 \return 0 if no DMA is in progress, nonzero otherwise.
174*/
176
177/** \brief Lock the G1 ATA mutex.
178 \ingroup g1ata
179
180 This function locks the mutex that arbitrates access to the ATA bus. You
181 should never have to do this on your own unless you're accessing devices
182 manually yourself.
183
184 \return 0 on success, -1 on failure.
185 \note Failure conditions are exactly the same as the
186 \ref mutex_lock() function.
187*/
189
190/** \brief Unlock the G1 ATA mutex.
191 \ingroup g1ata
192
193 This function unlocks the mutex that arbitrates access to the ATA bus. You
194 should never have to do this on your own unless you're accessing devices
195 manually yourself.
196
197 \return 0 on success, -1 on failure.
198 \note Failure conditions are exactly the same as the
199 \ref mutex_unlock() function.
200*/
202
203/** \brief Set the active ATA device.
204 \ingroup g1ata
205
206 This function sets the device that any further ATA commands will go to. You
207 shouldn't have to ever call this yourself, as it should be done for you by
208 any of the access functions. This must be called with the ATA lock held.
209
210 \param dev The device to access (generally either
211 \ref G1_ATA_MASTER or \ref G1_ATA_SLAVE).
212 \return The previous active device (or 0x0F if the function
213 would block in an IRQ handler).
214
215 \note This function may block if there is a transfer
216 ongoing. If called in an IRQ handler and the call
217 would otherwise block, 0x0F is returned.
218*/
219uint8_t g1_ata_select_device(uint8_t dev);
220
221/** \brief Read one or more disk sectors with Cylinder-Head-Sector addressing.
222 \ingroup g1ata
223
224 This function reads one or more 512-byte disk blocks from the slave device
225 on the G1 ATA bus using Cylinder-Head-Sector addressing. This function uses
226 PIO and blocks until the data is read in.
227
228 \param c The cylinder to start reading from.
229 \param h The head to start reading from.
230 \param s The sector to start reading from.
231 \param count The number of disk sectors to read.
232 \param buf Storage for the read-in disk sectors. This should be
233 at least (count * 512) bytes in length, and must be
234 at least 16-bit aligned.
235 \return 0 on success. < 0 on failure, setting errno as
236 appropriate.
237
238 \note Unless you're accessing a really old hard drive, you
239 probably do not want to use this function to access
240 the disk. Use the g1_ata_read_lba() function instead
241 of this one, unless you get an error from that
242 function indicating that LBA addressing is not
243 supported.
244
245 \par Error Conditions:
246 \em EIO - an I/O error occurred in reading data \n
247 \em ENXIO - ATA support not initialized or no device attached \n
248 \em EOVERFLOW - one or more of the requested sectors is out of the
249 range of the disk
250*/
251int g1_ata_read_chs(uint16_t c, uint8_t h, uint8_t s, size_t count,
252 void *buf);
253
254/** \brief Write one or more disk sectors with Cylinder-Head-Sector addressing.
255 \ingroup g1ata
256
257 This function writes one or more 512-byte disk blocks to the slave device
258 on the G1 ATA bus using Cylinder-Head-Sector addressing. This function uses
259 PIO and blocks until the data is written.
260
261 \param c The cylinder to start writing to.
262 \param h The head to start writing to.
263 \param s The sector to start writing to.
264 \param count The number of disk sectors to write.
265 \param buf The data to write to the disk. This should be
266 (count * 512) bytes in length and must be at least
267 16-bit aligned.
268 \return 0 on success. < 0 on failure, setting errno as
269 appropriate.
270
271 \note Unless you're accessing a really old hard drive, you
272 probably do not want to use this function to access
273 the disk. Use the g1_ata_write_lba() function
274 instead of this one, unless you get an error from
275 that function indicating that LBA addressing is not
276 supported.
277
278 \par Error Conditions:
279 \em ENXIO - ATA support not initialized or no device attached \n
280 \em EOVERFLOW - one or more of the requested sectors is out of the
281 range of the disk
282*/
283int g1_ata_write_chs(uint16_t c, uint8_t h, uint8_t s, size_t count,
284 const void *buf);
285
286/** \brief Read one or more disk sectors with Linear Block Addressing (LBA).
287 \ingroup g1ata
288
289 This function reads one or more 512-byte disk blocks from the slave device
290 on the G1 ATA bus using LBA mode (either 28 or 48 bits, as appropriate).
291 This function uses PIO and blocks until the data is read.
292
293 \param sector The sector to start reading from.
294 \param count The number of disk sectors to read.
295 \param buf Storage for the read-in disk sectors. This should be
296 at least (count * 512) bytes in length, and must be
297 at least 16-bit aligned.
298 \return 0 on success. < 0 on failure, setting errno as
299 appropriate.
300
301 \note If errno is set to ENOTSUP after calling this
302 function, you must use the g1_ata_read_chs()
303 function instead.
304
305 \par Error Conditions:
306 \em EIO - an I/O error occurred in reading data \n
307 \em ENXIO - ATA support not initialized or no device attached \n
308 \em EOVERFLOW - one or more of the requested sectors is out of the
309 range of the disk \n
310 \em ENOTSUP - LBA mode not supported by the device
311*/
312int g1_ata_read_lba(uint64_t sector, size_t count, void *buf);
313
314/** \brief DMA read disk sectors with Linear Block Addressing (LBA).
315 \ingroup g1ata
316
317 This function reads one or more 512-byte disk blocks from the slave device
318 on the G1 ATA bus using LBA mode (either 28 or 48 bits, as appropriate).
319 This function uses DMA and optionally blocks until the data is read.
320
321 \param sector The sector to start reading from.
322 \param count The number of disk sectors to read.
323 \param buf Storage for the read-in disk sectors. This should be
324 at least (count * 512) bytes in length, and must be
325 at least 32-byte aligned.
326 \param block Non-zero to block until the transfer completes.
327 \return 0 on success. < 0 on failure, setting errno as
328 appropriate.
329
330 \note If errno is set to ENOTSUP after calling this
331 function, you must use a CHS addressed transfer
332 function instead, like g1_ata_read_chs().
333
334 \note If errno is set to EPERM after calling this
335 function, DMA mode is not supported. You should use
336 a PIO transfer function like g1_ata_read_lba()
337 instead.
338
339 \note If the buffer address points to the P2 memory area,
340 the caller function will be responsible for ensuring
341 memory coherency.
342
343 \par Error Conditions:
344 \em EIO - an I/O error occurred in reading data \n
345 \em ENXIO - ATA support not initialized or no device attached \n
346 \em EOVERFLOW - one or more of the requested sectors is out of the
347 range of the disk \n
348 \em ENOTSUP - LBA mode not supported by the device \n
349 \em EPERM - device does not support DMA
350*/
351int g1_ata_read_lba_dma(uint64_t sector, size_t count, void *buf,
352 int block);
353
354/** \brief Write one or more disk sectors with Linear Block Addressing (LBA).
355 \ingroup g1ata
356
357 This function writes one or more 512-byte disk blocks to the slave device
358 on the G1 ATA bus using LBA mode (either 28 or 48 bits, as appropriate).
359 This function uses PIO and blocks until the data is written.
360
361 \param sector The sector to start writing to.
362 \param count The number of disk sectors to write.
363 \param buf The data to write to the disk. This should be
364 (count * 512) bytes in length and must be at least
365 16-bit aligned.
366 \return 0 on success. < 0 on failure, setting errno as
367 appropriate.
368
369 \note If errno is set to ENOTSUP after calling this
370 function, you must use the g1_ata_write_chs()
371 function instead.
372
373 \par Error Conditions:
374 \em ENXIO - ATA support not initialized or no device attached \n
375 \em EOVERFLOW - one or more of the requested sectors is out of the
376 range of the disk \n
377 \em ENOTSUP - LBA mode not supported by the device
378*/
379int g1_ata_write_lba(uint64_t sector, size_t count, const void *buf);
380
381/** \brief DMA Write disk sectors with Linear Block Addressing (LBA).
382 \ingroup g1ata
383
384 This function writes one or more 512-byte disk blocks to the slave device
385 on the G1 ATA bus using LBA mode (either 28 or 48 bits, as appropriate).
386 This function uses DMA and optionally blocks until the data is written.
387
388 \param sector The sector to start writing to.
389 \param count The number of disk sectors to write.
390 \param buf The data to write to the disk. This should be
391 (count * 512) bytes in length and must be at least
392 32-byte aligned.
393 \param block Non-zero to block until the transfer completes.
394 \return 0 on success. < 0 on failure, setting errno as
395 appropriate.
396
397 \note If errno is set to ENOTSUP after calling this
398 function, you must use the g1_ata_write_chs()
399 function instead.
400
401 \note If errno is set to EPERM after calling this
402 function, DMA mode is not supported. You should use
403 a PIO transfer function like g1_ata_write_lba()
404 instead.
405
406 \note If the buffer address points to the P2 memory area,
407 the caller function will be responsible for ensuring
408 memory coherency.
409
410 \par Error Conditions:
411 \em ENXIO - ATA support not initialized or no device attached \n
412 \em EOVERFLOW - one or more of the requested sectors is out of the
413 range of the disk \n
414 \em ENOTSUP - LBA mode not supported by the device \n
415 \em EPERM - device does not support DMA
416*/
417int g1_ata_write_lba_dma(uint64_t sector, size_t count, const void *buf,
418 int block);
419
420/** \brief Flush the write cache on the attached disk.
421 \ingroup g1ata
422
423 This function flushes the write cache on the disk attached as the slave
424 device on the G1 ATA bus. This ensures that all writes that have previously
425 completed are fully persisted to the disk. You should do this before
426 unmounting any disks or exiting your program if you have called any of the
427 write functions in here.
428
429 \return 0 on success. <0 on error, setting errno as
430 appropriate.
431
432 \par Error Conditions:
433 \em ENXIO - ATA support not initialized or no device attached
434*/
435int g1_ata_flush(void);
436
437/** \brief Get LBA mode of the attached disk.
438 \ingroup g1ata
439
440 \return -1 on error, 0 - CHS, 28 - LBA28, 48 - LBA48
441
442 \par Error Conditions:
443 \em ENXIO - ATA support not initialized or no device attached
444*/
446
447/** \brief Get a block device for a given partition on the slave ATA device.
448 \ingroup g1ata
449
450 This function creates a block device descriptor for the given partition on
451 the attached ATA device. This block device is used to interface with various
452 filesystems on the device.
453
454 \param partition The partition number (0-3) to use.
455 \param dma Set to 1 to use DMA for reads/writes on the device,
456 if available.
457 \param rv Used to return the block device. Must be non-NULL.
458 \param partition_type Used to return the partition type. Must be non-NULL.
459 \retval 0 On success.
460 \retval -1 On error, errno will be set as appropriate.
461
462 \par Error Conditions:
463 \em ENXIO - ATA support not initialized or no device attached \n
464 \em EIO - an I/O error occurred in reading data \n
465 \em EINVAL - invalid partition number was given \n
466 \em EFAULT - rv or partition_type was NULL \n
467 \em ENOENT - no MBR found \n
468 \em ENOENT - no partition at the specified position \n
469 \em ENOMEM - out of memory
470
471 \note This interface currently only supports MBR-formatted disks. There
472 is currently no support for GPT partition tables.
473*/
474int g1_ata_blockdev_for_partition(int partition, int dma, kos_blockdev_t *rv,
475 uint8_t *partition_type);
476
477/** \brief Get a block device for the attached ATA device.
478 \ingroup g1ata
479
480 This function creates a block device descriptor for the attached ATA device.
481
482 \param dma Set to 1 to use DMA for reads/writes on the device,
483 if available.
484 \param rv Used to return the block device. Must be non-NULL.
485 \retval 0 On success.
486 \retval -1 On error, errno will be set as appropriate.
487
488 \par Error Conditions:
489 \em ENXIO - ATA support not initialized or no device attached \n
490 \em EFAULT - rv was NULL \n
491 \em ENOMEM - out of memory
492*/
494
495/** \brief Initialize G1 ATA support.
496 \ingroup g1ata
497
498 This function initializes the rest of this subsystem and completes a scan of
499 the G1 ATA bus for devices. This function may take a while to complete with
500 some devices. Currently only the slave device is scanned, as the master
501 device should always be the GD-ROM drive.
502
503 \return 0 on success, <0 on error or if no device is present
504*/
505int g1_ata_init(void);
506
507/** \brief Shut down G1 ATA support.
508 \ingroup g1ata
509
510 This function shuts down the rest of this subsystem, and attempts to flush
511 the write cache of any attached slave devices. Accessing any ATA devices
512 using this subsystem after this function is called may produce undefined
513 results.
514*/
516
517__END_DECLS
518
519#endif /* __DC_G1ATA_H */
Definitions for a simple block device interface.
int g1_ata_flush(void)
Flush the write cache on the attached disk.
int g1_ata_lba_mode(void)
Get LBA mode of the attached disk.
int g1_ata_blockdev_for_partition(int partition, int dma, kos_blockdev_t *rv, uint8_t *partition_type)
Get a block device for a given partition on the slave ATA device.
void g1_ata_shutdown(void)
Shut down G1 ATA support.
int g1_ata_read_lba_dma(uint64_t sector, size_t count, void *buf, int block)
DMA read disk sectors with Linear Block Addressing (LBA).
int g1_ata_write_lba(uint64_t sector, size_t count, const void *buf)
Write one or more disk sectors with Linear Block Addressing (LBA).
int g1_dma_in_progress(void)
Is there a G1 DMA in progress currently?
int g1_ata_mutex_unlock(void)
Unlock the G1 ATA mutex.
int g1_ata_write_lba_dma(uint64_t sector, size_t count, const void *buf, int block)
DMA Write disk sectors with Linear Block Addressing (LBA).
int g1_ata_blockdev_for_device(int dma, kos_blockdev_t *rv)
Get a block device for the attached ATA device.
int g1_ata_read_chs(uint16_t c, uint8_t h, uint8_t s, size_t count, void *buf)
Read one or more disk sectors with Cylinder-Head-Sector addressing.
uint8_t g1_ata_select_device(uint8_t dev)
Set the active ATA device.
int g1_ata_init(void)
Initialize G1 ATA support.
int g1_ata_mutex_lock(void)
Lock the G1 ATA mutex.
int g1_ata_read_lba(uint64_t sector, size_t count, void *buf)
Read one or more disk sectors with Linear Block Addressing (LBA).
int g1_ata_write_chs(uint16_t c, uint8_t h, uint8_t s, size_t count, const void *buf)
Write one or more disk sectors with Cylinder-Head-Sector addressing.
A simple block device.
Definition blockdev.h:54