18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Helper library for PATA timings 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2003-2004 Red Hat, Inc. All rights reserved. 68c2ecf20Sopenharmony_ci * Copyright 2003-2004 Jeff Garzik 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <linux/kernel.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/libata.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci/* 148c2ecf20Sopenharmony_ci * This mode timing computation functionality is ported over from 158c2ecf20Sopenharmony_ci * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_ci/* 188c2ecf20Sopenharmony_ci * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds). 198c2ecf20Sopenharmony_ci * These were taken from ATA/ATAPI-6 standard, rev 0a, except 208c2ecf20Sopenharmony_ci * for UDMA6, which is currently supported only by Maxtor drives. 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0. 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistatic const struct ata_timing ata_timing[] = { 268c2ecf20Sopenharmony_ci/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */ 278c2ecf20Sopenharmony_ci { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 }, 288c2ecf20Sopenharmony_ci { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 }, 298c2ecf20Sopenharmony_ci { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 }, 308c2ecf20Sopenharmony_ci { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 }, 318c2ecf20Sopenharmony_ci { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 }, 328c2ecf20Sopenharmony_ci { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 }, 338c2ecf20Sopenharmony_ci { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 }, 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 }, 368c2ecf20Sopenharmony_ci { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 }, 378c2ecf20Sopenharmony_ci { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 }, 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 }, 408c2ecf20Sopenharmony_ci { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 }, 418c2ecf20Sopenharmony_ci { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 }, 428c2ecf20Sopenharmony_ci { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 }, 438c2ecf20Sopenharmony_ci { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 }, 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */ 468c2ecf20Sopenharmony_ci { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 }, 478c2ecf20Sopenharmony_ci { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 }, 488c2ecf20Sopenharmony_ci { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 }, 498c2ecf20Sopenharmony_ci { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 }, 508c2ecf20Sopenharmony_ci { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 }, 518c2ecf20Sopenharmony_ci { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 }, 528c2ecf20Sopenharmony_ci { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 }, 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci { 0xFF } 558c2ecf20Sopenharmony_ci}; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci#define ENOUGH(v, unit) (((v)-1)/(unit)+1) 588c2ecf20Sopenharmony_ci#define EZ(v, unit) ((v)?ENOUGH(((v) * 1000), unit):0) 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic void ata_timing_quantize(const struct ata_timing *t, 618c2ecf20Sopenharmony_ci struct ata_timing *q, int T, int UT) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci q->setup = EZ(t->setup, T); 648c2ecf20Sopenharmony_ci q->act8b = EZ(t->act8b, T); 658c2ecf20Sopenharmony_ci q->rec8b = EZ(t->rec8b, T); 668c2ecf20Sopenharmony_ci q->cyc8b = EZ(t->cyc8b, T); 678c2ecf20Sopenharmony_ci q->active = EZ(t->active, T); 688c2ecf20Sopenharmony_ci q->recover = EZ(t->recover, T); 698c2ecf20Sopenharmony_ci q->dmack_hold = EZ(t->dmack_hold, T); 708c2ecf20Sopenharmony_ci q->cycle = EZ(t->cycle, T); 718c2ecf20Sopenharmony_ci q->udma = EZ(t->udma, UT); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_civoid ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b, 758c2ecf20Sopenharmony_ci struct ata_timing *m, unsigned int what) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci if (what & ATA_TIMING_SETUP) 788c2ecf20Sopenharmony_ci m->setup = max(a->setup, b->setup); 798c2ecf20Sopenharmony_ci if (what & ATA_TIMING_ACT8B) 808c2ecf20Sopenharmony_ci m->act8b = max(a->act8b, b->act8b); 818c2ecf20Sopenharmony_ci if (what & ATA_TIMING_REC8B) 828c2ecf20Sopenharmony_ci m->rec8b = max(a->rec8b, b->rec8b); 838c2ecf20Sopenharmony_ci if (what & ATA_TIMING_CYC8B) 848c2ecf20Sopenharmony_ci m->cyc8b = max(a->cyc8b, b->cyc8b); 858c2ecf20Sopenharmony_ci if (what & ATA_TIMING_ACTIVE) 868c2ecf20Sopenharmony_ci m->active = max(a->active, b->active); 878c2ecf20Sopenharmony_ci if (what & ATA_TIMING_RECOVER) 888c2ecf20Sopenharmony_ci m->recover = max(a->recover, b->recover); 898c2ecf20Sopenharmony_ci if (what & ATA_TIMING_DMACK_HOLD) 908c2ecf20Sopenharmony_ci m->dmack_hold = max(a->dmack_hold, b->dmack_hold); 918c2ecf20Sopenharmony_ci if (what & ATA_TIMING_CYCLE) 928c2ecf20Sopenharmony_ci m->cycle = max(a->cycle, b->cycle); 938c2ecf20Sopenharmony_ci if (what & ATA_TIMING_UDMA) 948c2ecf20Sopenharmony_ci m->udma = max(a->udma, b->udma); 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ata_timing_merge); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ciconst struct ata_timing *ata_timing_find_mode(u8 xfer_mode) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci const struct ata_timing *t = ata_timing; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci while (xfer_mode > t->mode) 1038c2ecf20Sopenharmony_ci t++; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci if (xfer_mode == t->mode) 1068c2ecf20Sopenharmony_ci return t; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n", 1098c2ecf20Sopenharmony_ci __func__, xfer_mode); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci return NULL; 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ata_timing_find_mode); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ciint ata_timing_compute(struct ata_device *adev, unsigned short speed, 1168c2ecf20Sopenharmony_ci struct ata_timing *t, int T, int UT) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci const u16 *id = adev->id; 1198c2ecf20Sopenharmony_ci const struct ata_timing *s; 1208c2ecf20Sopenharmony_ci struct ata_timing p; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci /* 1238c2ecf20Sopenharmony_ci * Find the mode. 1248c2ecf20Sopenharmony_ci */ 1258c2ecf20Sopenharmony_ci s = ata_timing_find_mode(speed); 1268c2ecf20Sopenharmony_ci if (!s) 1278c2ecf20Sopenharmony_ci return -EINVAL; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci memcpy(t, s, sizeof(*s)); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci /* 1328c2ecf20Sopenharmony_ci * If the drive is an EIDE drive, it can tell us it needs extended 1338c2ecf20Sopenharmony_ci * PIO/MW_DMA cycle timing. 1348c2ecf20Sopenharmony_ci */ 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */ 1378c2ecf20Sopenharmony_ci memset(&p, 0, sizeof(p)); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) { 1408c2ecf20Sopenharmony_ci if (speed <= XFER_PIO_2) 1418c2ecf20Sopenharmony_ci p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO]; 1428c2ecf20Sopenharmony_ci else if ((speed <= XFER_PIO_4) || 1438c2ecf20Sopenharmony_ci (speed == XFER_PIO_5 && !ata_id_is_cfa(id))) 1448c2ecf20Sopenharmony_ci p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY]; 1458c2ecf20Sopenharmony_ci } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) 1468c2ecf20Sopenharmony_ci p.cycle = id[ATA_ID_EIDE_DMA_MIN]; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B); 1498c2ecf20Sopenharmony_ci } 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* 1528c2ecf20Sopenharmony_ci * Convert the timing to bus clock counts. 1538c2ecf20Sopenharmony_ci */ 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci ata_timing_quantize(t, t, T, UT); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci /* 1588c2ecf20Sopenharmony_ci * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, 1598c2ecf20Sopenharmony_ci * S.M.A.R.T * and some other commands. We have to ensure that the 1608c2ecf20Sopenharmony_ci * DMA cycle timing is slower/equal than the fastest PIO timing. 1618c2ecf20Sopenharmony_ci */ 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci if (speed > XFER_PIO_6) { 1648c2ecf20Sopenharmony_ci ata_timing_compute(adev, adev->pio_mode, &p, T, UT); 1658c2ecf20Sopenharmony_ci ata_timing_merge(&p, t, t, ATA_TIMING_ALL); 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci /* 1698c2ecf20Sopenharmony_ci * Lengthen active & recovery time so that cycle time is correct. 1708c2ecf20Sopenharmony_ci */ 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci if (t->act8b + t->rec8b < t->cyc8b) { 1738c2ecf20Sopenharmony_ci t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2; 1748c2ecf20Sopenharmony_ci t->rec8b = t->cyc8b - t->act8b; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci if (t->active + t->recover < t->cycle) { 1788c2ecf20Sopenharmony_ci t->active += (t->cycle - (t->active + t->recover)) / 2; 1798c2ecf20Sopenharmony_ci t->recover = t->cycle - t->active; 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci /* 1838c2ecf20Sopenharmony_ci * In a few cases quantisation may produce enough errors to 1848c2ecf20Sopenharmony_ci * leave t->cycle too low for the sum of active and recovery 1858c2ecf20Sopenharmony_ci * if so we must correct this. 1868c2ecf20Sopenharmony_ci */ 1878c2ecf20Sopenharmony_ci if (t->active + t->recover > t->cycle) 1888c2ecf20Sopenharmony_ci t->cycle = t->active + t->recover; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci return 0; 1918c2ecf20Sopenharmony_ci} 1928c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ata_timing_compute); 193