blob: a401d8f82b50cde6f3d1c58b023df31d33436817 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _IDE_TIMING_H
2#define _IDE_TIMING_H
3
4/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (c) 1999-2001 Vojtech Pavlik
6 */
7
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 2 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, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Should you need to contact me, the author, you can do so either by
24 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
25 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
26 */
27
Clemens Buchacher2665b892005-09-10 00:27:00 -070028#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/hdreg.h>
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
32 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
33 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
34 * for PIO 5, which is a nonstandard extension and UDMA6, which
35 * is currently supported only by Maxtor drives.
36 */
37
38static struct ide_timing ide_timing[] = {
39
40 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
41 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
42 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
43 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
44
45 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
46 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
47 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
50 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
51 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Bartlomiej Zolnierkiewiczf0ffc982008-07-16 20:33:36 +020052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
54 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
55 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
56
57 { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 },
58 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
59 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
60
61 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
62 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
63 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
64
65 { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 },
66
Bartlomiej Zolnierkiewicz71d51612008-07-16 20:33:36 +020067 { 0xff }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Clemens Buchacher2665b892005-09-10 00:27:00 -070070#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
71#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static void ide_timing_quantize(struct ide_timing *t, struct ide_timing *q, int T, int UT)
74{
75 q->setup = EZ(t->setup * 1000, T);
76 q->act8b = EZ(t->act8b * 1000, T);
77 q->rec8b = EZ(t->rec8b * 1000, T);
78 q->cyc8b = EZ(t->cyc8b * 1000, T);
79 q->active = EZ(t->active * 1000, T);
80 q->recover = EZ(t->recover * 1000, T);
81 q->cycle = EZ(t->cycle * 1000, T);
82 q->udma = EZ(t->udma * 1000, UT);
83}
84
85static void ide_timing_merge(struct ide_timing *a, struct ide_timing *b, struct ide_timing *m, unsigned int what)
86{
Clemens Buchacher2665b892005-09-10 00:27:00 -070087 if (what & IDE_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
88 if (what & IDE_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
89 if (what & IDE_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
90 if (what & IDE_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
91 if (what & IDE_TIMING_ACTIVE ) m->active = max(a->active, b->active);
92 if (what & IDE_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
93 if (what & IDE_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
94 if (what & IDE_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Bartlomiej Zolnierkiewicz71d51612008-07-16 20:33:36 +020097static struct ide_timing *ide_timing_find_mode(u8 speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct ide_timing *t;
100
101 for (t = ide_timing; t->mode != speed; t++)
Bartlomiej Zolnierkiewicz71d51612008-07-16 20:33:36 +0200102 if (t->mode == 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return NULL;
104 return t;
105}
106
Bartlomiej Zolnierkiewicz71d51612008-07-16 20:33:36 +0200107static int ide_timing_compute(ide_drive_t *drive, u8 speed,
108 struct ide_timing *t, int T, int UT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct hd_driveid *id = drive->id;
111 struct ide_timing *s, p;
112
113/*
114 * Find the mode.
115 */
116
117 if (!(s = ide_timing_find_mode(speed)))
118 return -EINVAL;
119
120/*
Sergei Shtylyov17c10332006-06-26 00:26:15 -0700121 * Copy the timing from the table.
122 */
123
124 *t = *s;
125
126/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 * If the drive is an EIDE drive, it can tell us it needs extended
128 * PIO/MWDMA cycle timing.
129 */
130
131 if (id && id->field_valid & 2) { /* EIDE drive */
132
133 memset(&p, 0, sizeof(p));
134
Bartlomiej Zolnierkiewiczbd887f72008-07-16 20:33:36 +0200135 if (speed <= XFER_PIO_2)
136 p.cycle = p.cyc8b = id->eide_pio;
137 else if (speed <= XFER_PIO_5)
138 p.cycle = p.cyc8b = id->eide_pio_iordy;
139 else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
140 p.cycle = id->eide_dma_min;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 ide_timing_merge(&p, t, t, IDE_TIMING_CYCLE | IDE_TIMING_CYC8B);
143 }
144
145/*
146 * Convert the timing to bus clock counts.
147 */
148
Sergei Shtylyov17c10332006-06-26 00:26:15 -0700149 ide_timing_quantize(t, t, T, UT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/*
152 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
153 * and some other commands. We have to ensure that the DMA cycle timing is
154 * slower/equal than the fastest PIO timing.
155 */
156
Bartlomiej Zolnierkiewiczbd887f72008-07-16 20:33:36 +0200157 if (speed >= XFER_SW_DMA_0) {
Bartlomiej Zolnierkiewicz6a824c92007-07-20 01:11:58 +0200158 u8 pio = ide_get_best_pio_mode(drive, 255, 5);
159 ide_timing_compute(drive, XFER_PIO_0 + pio, &p, T, UT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 ide_timing_merge(&p, t, t, IDE_TIMING_ALL);
161 }
162
163/*
Paulius Zaleckasefad798b2008-02-03 15:42:53 +0200164 * Lengthen active & recovery time so that cycle time is correct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
166
167 if (t->act8b + t->rec8b < t->cyc8b) {
168 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
169 t->rec8b = t->cyc8b - t->act8b;
170 }
171
172 if (t->active + t->recover < t->cycle) {
173 t->active += (t->cycle - (t->active + t->recover)) / 2;
174 t->recover = t->cycle - t->active;
175 }
176
177 return 0;
178}
179
180#endif