source: rtems/bsps/powerpc/gen5200/include/bsp/ata.h @ e560ee85

Last change on this file since e560ee85 was e560ee85, checked in by Joel Sherrill <joel@…>, on 03/01/22 at 21:38:55

bsps/powerpc/: Scripted embedded brains header file clean up

Updates #4625.

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*
2 * Copyright (c) 2010-2013 embedded brains GmbH.  All rights reserved.
3 *
4 * The license and distribution terms for this file may be
5 * found in the file LICENSE in this distribution or at
6 * http://www.rtems.org/license/LICENSE.
7 */
8
9#ifndef GEN5200_ATA_H
10#define GEN5200_ATA_H
11
12#include "bestcomm.h"
13
14#include <assert.h>
15
16#include <rtems.h>
17#include <rtems/diskdevs.h>
18#include <rtems/bdbuf.h>
19
20#include <libchip/ata_internal.h>
21#include <libchip/ide_ctrl_io.h>
22#include <libchip/ide_ctrl_cfg.h>
23
24#include <libcpu/powerpc-utility.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif /* __cplusplus */
29
30#define DCTRL_SRST BSP_BBIT8(5)
31#define DCTRL_NIEN BSP_BBIT8(6)
32
33#define DAST_BSY BSP_BBIT8(0)
34#define DAST_DRDY BSP_BBIT8(1)
35#define DAST_DRQ BSP_BBIT8(4)
36#define DAST_ERR BSP_BBIT8(7)
37
38#define DST_BSY BSP_BBIT16(0)
39#define DST_DRDY BSP_BBIT16(1)
40#define DST_DRQ BSP_BBIT16(4)
41#define DST_ERR BSP_BBIT16(7)
42
43#define DDMA_HUT BSP_BBIT8(1)
44#define DDMA_FR BSP_BBIT8(2)
45#define DDMA_FE BSP_BBIT8(3)
46#define DDMA_IE BSP_BBIT8(4)
47#define DDMA_UDMA BSP_BBIT8(5)
48#define DDMA_READ BSP_BBIT8(6)
49#define DDMA_WRITE BSP_BBIT8(7)
50
51#define ATA_SECTOR_SHIFT 9
52
53#define ATA_PER_TRANSFER_SECTOR_COUNT_MAX 256
54
55typedef union {
56  struct {
57    uint8_t alternate_status;
58    uint8_t reserved_0[3];
59    uint16_t data;
60    uint8_t reserved_1[2];
61    uint8_t error;
62    uint8_t reserved_2[3];
63    uint8_t sector_count;
64    uint8_t reserved_3[3];
65    uint8_t sector;
66    uint8_t reserved_4[3];
67    uint8_t cylinder_low;
68    uint8_t reserved_5[3];
69    uint8_t cylinder_high;
70    uint8_t reserved_6[3];
71    uint8_t head;
72    uint8_t reserved_7[3];
73    uint16_t status;
74    uint8_t reserved_8[2];
75  } read;
76
77  struct {
78    uint8_t control;
79    uint8_t reserved_0[3];
80    uint16_t data;
81    uint8_t reserved_1[2];
82    uint8_t feature;
83    uint8_t reserved_2[3];
84    uint8_t sector_count;
85    uint8_t reserved_3[3];
86    uint8_t sector;
87    uint8_t reserved_4[3];
88    uint8_t cylinder_low;
89    uint8_t reserved_5[3];
90    uint8_t cylinder_high;
91    uint8_t reserved_6[3];
92    uint8_t head;
93    uint8_t reserved_7[3];
94    uint8_t command;
95    uint8_t dma_control;
96    uint8_t reserved_8[2];
97  } write;
98} ata_drive_registers;
99
100#define ATA ((volatile ata_drive_registers *) 0xf0003a5c)
101
102static inline bool ata_is_data_request(void)
103{
104  return (ATA->read.alternate_status & DAST_DRQ) != 0;
105}
106
107static inline bool ata_is_drive_ready_for_selection(void)
108{
109  return (ATA->read.alternate_status & (DAST_BSY | DAST_DRQ)) == 0;
110}
111
112static inline void ata_wait_400_nano_seconds(void)
113{
114  ATA->read.alternate_status;
115}
116
117static inline void ata_wait_for_drive_ready(void)
118{
119  while ((ATA->read.alternate_status & (DAST_BSY | DAST_DRQ | DAST_DRDY)) != DAST_DRDY) {
120    /* Wait */
121  }
122}
123
124static inline void ata_wait_for_not_busy(void)
125{
126  ata_wait_400_nano_seconds();
127
128  while ((ATA->read.alternate_status & DAST_BSY) != 0) {
129    /* Wait */
130  }
131}
132
133static inline bool ata_wait_for_data_request(void)
134{
135  ata_wait_400_nano_seconds();
136
137  uint8_t alternate_status;
138  do {
139    alternate_status = ATA->read.alternate_status;
140  } while ((alternate_status & DAST_BSY) == DAST_BSY);
141
142  return (alternate_status & (DAST_ERR | DAST_DRQ)) == DAST_DRQ;
143}
144
145static inline bool ata_check_status(void)
146{
147  return (ATA->read.status & (DST_BSY | DST_ERR)) == 0;
148}
149
150static inline void ata_clear_interrupts(void)
151{
152  ATA->read.status;
153}
154
155static inline uint8_t ata_read_or_write_sectors_command(bool read)
156{
157  return read ? 0x20 : 0x30;
158}
159
160static inline rtems_blkdev_bnum ata_max_transfer_count(rtems_blkdev_bnum sector_count)
161{
162  return sector_count > ATA_PER_TRANSFER_SECTOR_COUNT_MAX ?
163    ATA_PER_TRANSFER_SECTOR_COUNT_MAX
164      : sector_count;
165}
166
167static inline void ata_flush_sector(uint16_t *begin)
168{
169  /* XXX: The dcbi operation does not work properly */
170  rtems_cache_flush_multiple_data_lines(begin, ATA_SECTOR_SIZE);
171}
172
173void ata_reset_device(void);
174
175bool ata_set_transfer_mode(uint8_t mode);
176
177bool ata_execute_io_command(uint8_t command, uint32_t lba, uint32_t sector_count);
178
179static inline bool ata_execute_io_command_with_sg(uint8_t command, const rtems_blkdev_sg_buffer *sg)
180{
181  uint32_t lba = sg->block;
182  uint32_t sector_count = sg->length / ATA_SECTOR_SIZE;
183  return ata_execute_io_command(command, lba, sector_count);
184}
185
186typedef struct {
187  const rtems_blkdev_sg_buffer *sg;
188
189  size_t sg_count;
190
191  rtems_blkdev_bnum sg_buffer_offset_mask;
192
193  int sg_index_shift;
194} ata_sg_context;
195
196static inline void ata_sg_reset(ata_sg_context *self, const rtems_blkdev_sg_buffer *sg, size_t sg_count)
197{
198  self->sg = sg;
199  self->sg_count = sg_count;
200  uint32_t sectors_per_buffer = self->sg[0].length >> ATA_SECTOR_SHIFT;
201  self->sg_buffer_offset_mask = sectors_per_buffer - 1;
202  self->sg_index_shift = __builtin_ffs((int) sectors_per_buffer) - 1;
203}
204
205static inline void ata_sg_create_default(ata_sg_context *self)
206{
207  ata_sg_reset(self, NULL, 0);
208}
209
210static inline void ata_sg_create(ata_sg_context *self, const rtems_blkdev_sg_buffer *sg, size_t sg_count)
211{
212  ata_sg_reset(self, sg, sg_count);
213}
214
215static inline rtems_blkdev_bnum ata_sg_get_start_sector(const ata_sg_context *self)
216{
217  return self->sg[0].block;
218}
219
220static inline rtems_blkdev_bnum ata_sg_get_sector_count(const ata_sg_context *self)
221{
222  return (self->sg_buffer_offset_mask + 1) * self->sg_count;
223}
224
225static inline uint16_t *ata_sg_get_sector_data_begin(const ata_sg_context *self, rtems_blkdev_bnum relative_sector)
226{
227  uint16_t *begin = (uint16_t *)(self->sg[relative_sector >> self->sg_index_shift].buffer);
228
229  return begin + ((relative_sector & self->sg_buffer_offset_mask) << (ATA_SECTOR_SHIFT - 1));
230}
231
232static inline uint16_t *ata_sg_get_sector_data_end(const ata_sg_context *self, uint16_t *begin)
233{
234  return begin + ATA_SECTOR_SIZE / 2;
235}
236
237typedef struct {
238  rtems_id lock;
239
240  bool card_present;
241} ata_driver;
242
243void ata_driver_create(ata_driver *self, const char *device_file_path, rtems_block_device_ioctl io_control);
244
245void ata_driver_destroy(ata_driver *self);
246
247static inline void ata_driver_lock(const ata_driver *self)
248{
249  rtems_status_code sc = rtems_semaphore_obtain(self->lock, RTEMS_WAIT, RTEMS_NO_TIMEOUT);
250  assert(sc == RTEMS_SUCCESSFUL);
251  (void) sc;
252}
253
254static inline void ata_driver_unlock(const ata_driver *self)
255{
256  rtems_status_code sc = rtems_semaphore_release(self->lock);
257  assert(sc == RTEMS_SUCCESSFUL);
258  (void) sc;
259}
260
261static inline bool ata_driver_is_card_present(const ata_driver *self)
262{
263  return self->card_present;
264}
265
266static inline void ata_driver_io_request(
267  ata_driver *self,
268  rtems_blkdev_request *request,
269  bool (*transfer)(ata_driver *, bool, rtems_blkdev_sg_buffer *, size_t)
270)
271{
272  assert(request->req == RTEMS_BLKDEV_REQ_READ || request->req == RTEMS_BLKDEV_REQ_WRITE);
273  bool read = request->req != RTEMS_BLKDEV_REQ_WRITE;
274  rtems_blkdev_sg_buffer *sg = &request->bufs[0];
275  uint32_t sg_count = request->bufnum;
276  ata_driver_lock(self);
277  bool ok = (*transfer)(self, read, sg, sg_count);
278  ata_driver_unlock(self);
279  rtems_status_code sc = ok ? RTEMS_SUCCESSFUL : RTEMS_IO_ERROR;
280  rtems_blkdev_request_done(request, sc);
281}
282
283static inline int ata_driver_io_control(
284  rtems_disk_device *dd,
285  uint32_t cmd,
286  void *arg,
287  bool (*transfer)(ata_driver *, bool, rtems_blkdev_sg_buffer *, size_t)
288)
289{
290  ata_driver *self = (ata_driver *) rtems_disk_get_driver_data(dd);
291
292  switch (cmd) {
293    case RTEMS_BLKIO_REQUEST:
294      ata_driver_io_request(self, (rtems_blkdev_request *) arg, transfer);
295      return 0;
296    case RTEMS_BLKIO_CAPABILITIES:
297      *(uint32_t *) arg = RTEMS_BLKDEV_CAP_MULTISECTOR_CONT;
298      return 0;
299    default:
300      return rtems_blkdev_ioctl(dd, cmd, arg);
301  }
302}
303
304int ata_driver_io_control_pio_polled(
305  rtems_disk_device *dd,
306  uint32_t cmd,
307  void *arg
308);
309
310typedef struct {
311  ata_driver super;
312
313  bestcomm_task task;
314
315  bool read;
316
317  ata_sg_context sg_context;
318
319  rtems_blkdev_bnum transfer_current;
320
321  rtems_blkdev_bnum transfer_end;
322} ata_driver_dma_pio_single;
323
324void ata_driver_dma_pio_single_create(
325  ata_driver_dma_pio_single *self,
326  const char *device_file_path,
327  TaskId task_index
328);
329
330#ifdef __cplusplus
331}
332#endif /* __cplusplus */
333
334#endif /* GEN5200_ATA_H */
Note: See TracBrowser for help on using the repository browser.