18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) 1999-2001 Vojtech Pavlik
48c2ecf20Sopenharmony_ci *  Copyright (c) 2007-2008 Bartlomiej Zolnierkiewicz
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Should you need to contact me, the author, you can do so either by
78c2ecf20Sopenharmony_ci * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
88c2ecf20Sopenharmony_ci * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/ide.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
178c2ecf20Sopenharmony_ci * These were taken from ATA/ATAPI-6 standard, rev 0a, except
188c2ecf20Sopenharmony_ci * for PIO 5, which is a nonstandard extension and UDMA6, which
198c2ecf20Sopenharmony_ci * is currently supported only by Maxtor drives.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic struct ide_timing ide_timing[] = {
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0,   0,  15 },
258c2ecf20Sopenharmony_ci	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0,   0,  20 },
268c2ecf20Sopenharmony_ci	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0,   0,  30 },
278c2ecf20Sopenharmony_ci	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0,   0,  45 },
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0,   0,  60 },
308c2ecf20Sopenharmony_ci	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0,   0,  80 },
318c2ecf20Sopenharmony_ci	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0,   0, 120 },
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20,  80,   0 },
348c2ecf20Sopenharmony_ci	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 100,   0 },
358c2ecf20Sopenharmony_ci	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 120,   0 },
368c2ecf20Sopenharmony_ci	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 150,   0 },
378c2ecf20Sopenharmony_ci	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 480,   0 },
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 240,   0 },
408c2ecf20Sopenharmony_ci	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 480,   0 },
418c2ecf20Sopenharmony_ci	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 960,   0 },
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20,  80,   0 },
448c2ecf20Sopenharmony_ci	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 100,   0 },
458c2ecf20Sopenharmony_ci	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 120,   0 },
468c2ecf20Sopenharmony_ci	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 180,   0 },
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 240,   0 },
498c2ecf20Sopenharmony_ci	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 383,   0 },
508c2ecf20Sopenharmony_ci	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 600,   0 },
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960,   0 },
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	{ 0xff }
558c2ecf20Sopenharmony_ci};
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct ide_timing *ide_timing_find_mode(u8 speed)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct ide_timing *t;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	for (t = ide_timing; t->mode != speed; t++)
628c2ecf20Sopenharmony_ci		if (t->mode == 0xff)
638c2ecf20Sopenharmony_ci			return NULL;
648c2ecf20Sopenharmony_ci	return t;
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ide_timing_find_mode);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciu16 ide_pio_cycle_time(ide_drive_t *drive, u8 pio)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	u16 *id = drive->id;
718c2ecf20Sopenharmony_ci	struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
728c2ecf20Sopenharmony_ci	u16 cycle = 0;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	if (id[ATA_ID_FIELD_VALID] & 2) {
758c2ecf20Sopenharmony_ci		if (ata_id_has_iordy(drive->id))
768c2ecf20Sopenharmony_ci			cycle = id[ATA_ID_EIDE_PIO_IORDY];
778c2ecf20Sopenharmony_ci		else
788c2ecf20Sopenharmony_ci			cycle = id[ATA_ID_EIDE_PIO];
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		/* conservative "downgrade" for all pre-ATA2 drives */
818c2ecf20Sopenharmony_ci		if (pio < 3 && cycle < t->cycle)
828c2ecf20Sopenharmony_ci			cycle = 0; /* use standard timing */
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci		/* Use the standard timing for the CF specific modes too */
858c2ecf20Sopenharmony_ci		if (pio > 4 && ata_id_is_cfa(id))
868c2ecf20Sopenharmony_ci			cycle = 0;
878c2ecf20Sopenharmony_ci	}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	return cycle ? cycle : t->cycle;
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ide_pio_cycle_time);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#define ENOUGH(v, unit)		(((v) - 1) / (unit) + 1)
948c2ecf20Sopenharmony_ci#define EZ(v, unit)		((v) ? ENOUGH((v) * 1000, unit) : 0)
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic void ide_timing_quantize(struct ide_timing *t, struct ide_timing *q,
978c2ecf20Sopenharmony_ci				int T, int UT)
988c2ecf20Sopenharmony_ci{
998c2ecf20Sopenharmony_ci	q->setup   = EZ(t->setup,   T);
1008c2ecf20Sopenharmony_ci	q->act8b   = EZ(t->act8b,   T);
1018c2ecf20Sopenharmony_ci	q->rec8b   = EZ(t->rec8b,   T);
1028c2ecf20Sopenharmony_ci	q->cyc8b   = EZ(t->cyc8b,   T);
1038c2ecf20Sopenharmony_ci	q->active  = EZ(t->active,  T);
1048c2ecf20Sopenharmony_ci	q->recover = EZ(t->recover, T);
1058c2ecf20Sopenharmony_ci	q->cycle   = EZ(t->cycle,   T);
1068c2ecf20Sopenharmony_ci	q->udma    = EZ(t->udma,    UT);
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_civoid ide_timing_merge(struct ide_timing *a, struct ide_timing *b,
1108c2ecf20Sopenharmony_ci		      struct ide_timing *m, unsigned int what)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	if (what & IDE_TIMING_SETUP)
1138c2ecf20Sopenharmony_ci		m->setup   = max(a->setup,   b->setup);
1148c2ecf20Sopenharmony_ci	if (what & IDE_TIMING_ACT8B)
1158c2ecf20Sopenharmony_ci		m->act8b   = max(a->act8b,   b->act8b);
1168c2ecf20Sopenharmony_ci	if (what & IDE_TIMING_REC8B)
1178c2ecf20Sopenharmony_ci		m->rec8b   = max(a->rec8b,   b->rec8b);
1188c2ecf20Sopenharmony_ci	if (what & IDE_TIMING_CYC8B)
1198c2ecf20Sopenharmony_ci		m->cyc8b   = max(a->cyc8b,   b->cyc8b);
1208c2ecf20Sopenharmony_ci	if (what & IDE_TIMING_ACTIVE)
1218c2ecf20Sopenharmony_ci		m->active  = max(a->active,  b->active);
1228c2ecf20Sopenharmony_ci	if (what & IDE_TIMING_RECOVER)
1238c2ecf20Sopenharmony_ci		m->recover = max(a->recover, b->recover);
1248c2ecf20Sopenharmony_ci	if (what & IDE_TIMING_CYCLE)
1258c2ecf20Sopenharmony_ci		m->cycle   = max(a->cycle,   b->cycle);
1268c2ecf20Sopenharmony_ci	if (what & IDE_TIMING_UDMA)
1278c2ecf20Sopenharmony_ci		m->udma    = max(a->udma,    b->udma);
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ide_timing_merge);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ciint ide_timing_compute(ide_drive_t *drive, u8 speed,
1328c2ecf20Sopenharmony_ci		       struct ide_timing *t, int T, int UT)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	u16 *id = drive->id;
1358c2ecf20Sopenharmony_ci	struct ide_timing *s, p;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	/*
1388c2ecf20Sopenharmony_ci	 * Find the mode.
1398c2ecf20Sopenharmony_ci	 */
1408c2ecf20Sopenharmony_ci	s = ide_timing_find_mode(speed);
1418c2ecf20Sopenharmony_ci	if (s == NULL)
1428c2ecf20Sopenharmony_ci		return -EINVAL;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/*
1458c2ecf20Sopenharmony_ci	 * Copy the timing from the table.
1468c2ecf20Sopenharmony_ci	 */
1478c2ecf20Sopenharmony_ci	*t = *s;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/*
1508c2ecf20Sopenharmony_ci	 * If the drive is an EIDE drive, it can tell us it needs extended
1518c2ecf20Sopenharmony_ci	 * PIO/MWDMA cycle timing.
1528c2ecf20Sopenharmony_ci	 */
1538c2ecf20Sopenharmony_ci	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
1548c2ecf20Sopenharmony_ci		memset(&p, 0, sizeof(p));
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci		if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
1578c2ecf20Sopenharmony_ci			if (speed <= XFER_PIO_2)
1588c2ecf20Sopenharmony_ci				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
1598c2ecf20Sopenharmony_ci			else if ((speed <= XFER_PIO_4) ||
1608c2ecf20Sopenharmony_ci				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
1618c2ecf20Sopenharmony_ci				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
1628c2ecf20Sopenharmony_ci		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
1638c2ecf20Sopenharmony_ci			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		ide_timing_merge(&p, t, t, IDE_TIMING_CYCLE | IDE_TIMING_CYC8B);
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/*
1698c2ecf20Sopenharmony_ci	 * Convert the timing to bus clock counts.
1708c2ecf20Sopenharmony_ci	 */
1718c2ecf20Sopenharmony_ci	ide_timing_quantize(t, t, T, UT);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/*
1748c2ecf20Sopenharmony_ci	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1758c2ecf20Sopenharmony_ci	 * S.M.A.R.T and some other commands. We have to ensure that the
1768c2ecf20Sopenharmony_ci	 * DMA cycle timing is slower/equal than the current PIO timing.
1778c2ecf20Sopenharmony_ci	 */
1788c2ecf20Sopenharmony_ci	if (speed >= XFER_SW_DMA_0) {
1798c2ecf20Sopenharmony_ci		ide_timing_compute(drive, drive->pio_mode, &p, T, UT);
1808c2ecf20Sopenharmony_ci		ide_timing_merge(&p, t, t, IDE_TIMING_ALL);
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/*
1848c2ecf20Sopenharmony_ci	 * Lengthen active & recovery time so that cycle time is correct.
1858c2ecf20Sopenharmony_ci	 */
1868c2ecf20Sopenharmony_ci	if (t->act8b + t->rec8b < t->cyc8b) {
1878c2ecf20Sopenharmony_ci		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1888c2ecf20Sopenharmony_ci		t->rec8b = t->cyc8b - t->act8b;
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (t->active + t->recover < t->cycle) {
1928c2ecf20Sopenharmony_ci		t->active += (t->cycle - (t->active + t->recover)) / 2;
1938c2ecf20Sopenharmony_ci		t->recover = t->cycle - t->active;
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	return 0;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ide_timing_compute);
199