1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Barry Wardell
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 /* ATA stuff was taken from the iPod code */
27 #include "ata-target.h"
34 void ata_enable(bool on
)
36 /* TODO: Implement ata_enable() */
40 bool ata_is_coldstart()
43 /* TODO: Implement coldstart variable */
46 void ata_device_init()
49 CPU_INT_DIS
= (1<<IDE_IRQ
);
53 IDE_DMA_CONTROL
&= ~1;
58 /* From ipod-ide.c:ipod_ide_register() */
61 IDE0_CFG
|= (0x10000000); /* cpu > 65MHz */
63 IDE0_CFG
&=~(0x10000000); /* cpu < 65MHz */
67 IDE0_PRI_TIMING0
= 0x10;
68 IDE0_PRI_TIMING1
= 0x80002150;
71 /* These are PIO timings for 80 Mhz. At 24 Mhz,
72 the first value is 0 but the rest are the same.
73 They go in IDE0_PRI_TIMING0.
75 Rockbox used 0x10, and test_disk shows that leads to faster PIO.
76 If 0x10 is incorrect, these timings may be needed with some devices.
77 static const unsigned long pio80mhz[] = {
78 0xC293, 0x43A2, 0x11A1, 0x7232, 0x3131
83 /* Timings for multi-word and ultra DMA modes.
84 These go in IDE0_PRI_TIMING1
86 static const unsigned long tm_mwdma
[] = {
87 0xF9F92, 0x56562, 0x45451
90 static const unsigned long tm_udma
[] = {
91 0x800037C1, 0x80003491, 0x80003371,
93 0x80003271, 0x80003071
98 static bool dma_boosted
= false;
99 static bool dma_needs_boost
;
102 /* This function sets up registers for 80 Mhz.
103 Ultra DMA mode 2 works at 30 Mhz.
105 void ata_dma_set_mode(unsigned char mode
) {
108 (*(volatile unsigned long *)(0x600060C4)) = 0xC0000000; /* 80 Mhz */
109 IDE0_CFG
&= ~0x10000000;
113 if (mode
== 0x40 && modeidx
<= ATA_MAX_UDMA
) {
114 IDE0_PRI_TIMING1
= tm_udma
[modeidx
];
117 dma_needs_boost
= true;
119 dma_needs_boost
= false;
121 } else if (mode
== 0x20 && modeidx
<= ATA_MAX_MWDMA
)
122 IDE0_PRI_TIMING1
= tm_mwdma
[modeidx
];
124 IDE0_CFG
|= 0x20000000; /* >= 50 Mhz */
127 #define IDE_CFG_INTRQ 8
128 #define IDE_DMA_CONTROL_READ 8
130 /* This waits for an ATA interrupt using polling.
131 In ATA_CONTROL, CONTROL_nIEN must be cleared.
133 STATICIRAM ICODE_ATTR
int ata_wait_intrq(void)
135 long timeout
= current_tick
+ HZ
*10;
139 if (IDE0_CFG
& IDE_CFG_INTRQ
)
143 } while (TIME_BEFORE(current_tick
, timeout
));
145 return 0; /* timeout */
148 /* This function checks if parameters are appropriate for DMA,
149 and if they are, it sets up for DMA.
151 If return value is false, caller may use PIO for this transfer.
153 If return value is true, caller must issue a DMA ATA command
154 and then call ata_dma_finish().
156 bool ata_dma_setup(void *addr
, unsigned long bytes
, bool write
) {
157 /* Require cacheline alignment for reads to prevent interference. */
158 if (!write
&& ((unsigned long)addr
& 15))
161 /* Writes only need to be word-aligned, but by default DMA
162 * is not used for writing as it appears to be slower.
164 #ifdef ATA_DMA_WRITES
165 if (write
&& ((unsigned long)addr
& 3))
173 if (dma_needs_boost
&& !dma_boosted
) {
180 /* If unflushed, old data may be written to disk */
184 /* Invalidate cache because new data may be present in RAM */
185 cpucache_invalidate();
188 /* Clear pending interrupts so ata_dma_finish() can wait for an
189 interrupt from this transfer
191 IDE0_CFG
|= IDE_CFG_INTRQ
;
193 IDE_DMA_CONTROL
|= 2;
194 IDE_DMA_LENGTH
= bytes
- 4;
197 if ((unsigned long)addr
< DRAM_START
)
198 /* Rockbox remaps DRAM to start at 0 */
199 IDE_DMA_ADDR
= (unsigned long)addr
+ DRAM_START
;
202 IDE_DMA_ADDR
= (unsigned long)addr
;
205 IDE_DMA_CONTROL
&= ~IDE_DMA_CONTROL_READ
;
207 IDE_DMA_CONTROL
|= IDE_DMA_CONTROL_READ
;
214 /* This function waits for a DMA transfer to end.
215 It must be called to finish what ata_dma_setup started.
217 Return value is true if DMA completed before the timeout, and false
218 if a timeout happened.
220 bool ata_dma_finish(void) {
223 /* It may be okay to put this at the end of setup */
224 IDE_DMA_CONTROL
|= 1;
226 /* Wait for end of transfer.
227 Reading standard ATA status while DMA is in progress causes
228 failures and hangs. Because of that, another wait is used.
230 res
= ata_wait_intrq();
233 IDE_DMA_CONTROL
&= ~0x80000001;
245 #endif /* HAVE_ATA_DMA */