145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2669a5db4SJeff Garzik /*
3ab771630SAlan Cox * New ATA layer SC1200 driver Alan Cox <alan@lxorguk.ukuu.org.uk>
4669a5db4SJeff Garzik *
5669a5db4SJeff Garzik * TODO: Mode selection filtering
6669a5db4SJeff Garzik * TODO: Needs custom DMA cleanup code
7669a5db4SJeff Garzik *
8669a5db4SJeff Garzik * Based very heavily on
9669a5db4SJeff Garzik *
10669a5db4SJeff Garzik * linux/drivers/ide/pci/sc1200.c Version 0.91 28-Jan-2003
11669a5db4SJeff Garzik *
12669a5db4SJeff Garzik * Copyright (C) 2000-2002 Mark Lord <mlord@pobox.com>
13669a5db4SJeff Garzik * May be copied or modified under the terms of the GNU General Public License
14669a5db4SJeff Garzik *
15669a5db4SJeff Garzik * Development of this chipset driver was funded
16669a5db4SJeff Garzik * by the nice folks at National Semiconductor.
17669a5db4SJeff Garzik */
18669a5db4SJeff Garzik
19669a5db4SJeff Garzik #include <linux/kernel.h>
20669a5db4SJeff Garzik #include <linux/module.h>
21669a5db4SJeff Garzik #include <linux/pci.h>
22669a5db4SJeff Garzik #include <linux/blkdev.h>
23669a5db4SJeff Garzik #include <linux/delay.h>
24669a5db4SJeff Garzik #include <scsi/scsi_host.h>
25669a5db4SJeff Garzik #include <linux/libata.h>
26669a5db4SJeff Garzik
2770f301a6SBartlomiej Zolnierkiewicz #define DRV_NAME "pata_sc1200"
282a3103ceSJeff Garzik #define DRV_VERSION "0.2.6"
29669a5db4SJeff Garzik
30669a5db4SJeff Garzik #define SC1200_REV_A 0x00
31669a5db4SJeff Garzik #define SC1200_REV_B1 0x01
32669a5db4SJeff Garzik #define SC1200_REV_B3 0x02
33669a5db4SJeff Garzik #define SC1200_REV_C1 0x03
34669a5db4SJeff Garzik #define SC1200_REV_D1 0x04
35669a5db4SJeff Garzik
36669a5db4SJeff Garzik /**
37669a5db4SJeff Garzik * sc1200_clock - PCI clock
38669a5db4SJeff Garzik *
39669a5db4SJeff Garzik * Return the PCI bus clocking for the SC1200 chipset configuration
40669a5db4SJeff Garzik * in use. We return 0 for 33MHz 1 for 48MHz and 2 for 66Mhz
41669a5db4SJeff Garzik */
42669a5db4SJeff Garzik
sc1200_clock(void)43669a5db4SJeff Garzik static int sc1200_clock(void)
44669a5db4SJeff Garzik {
45669a5db4SJeff Garzik /* Magic registers that give us the chipset data */
46669a5db4SJeff Garzik u8 chip_id = inb(0x903C);
47669a5db4SJeff Garzik u8 silicon_rev = inb(0x903D);
48669a5db4SJeff Garzik u16 pci_clock;
49669a5db4SJeff Garzik
50669a5db4SJeff Garzik if (chip_id == 0x04 && silicon_rev < SC1200_REV_B1)
51669a5db4SJeff Garzik return 0; /* 33 MHz mode */
52669a5db4SJeff Garzik
53669a5db4SJeff Garzik /* Clock generator configuration 0x901E its 8/9 are the PCI clocking
54669a5db4SJeff Garzik 0/3 is 33Mhz 1 is 48 2 is 66 */
55669a5db4SJeff Garzik
56669a5db4SJeff Garzik pci_clock = inw(0x901E);
57669a5db4SJeff Garzik pci_clock >>= 8;
58669a5db4SJeff Garzik pci_clock &= 0x03;
59669a5db4SJeff Garzik if (pci_clock == 3)
60669a5db4SJeff Garzik pci_clock = 0;
61669a5db4SJeff Garzik return pci_clock;
62669a5db4SJeff Garzik }
63669a5db4SJeff Garzik
64669a5db4SJeff Garzik /**
65669a5db4SJeff Garzik * sc1200_set_piomode - PIO setup
66669a5db4SJeff Garzik * @ap: ATA interface
67669a5db4SJeff Garzik * @adev: device on the interface
68669a5db4SJeff Garzik *
69669a5db4SJeff Garzik * Set our PIO requirements. This is fairly simple on the SC1200
70669a5db4SJeff Garzik */
71669a5db4SJeff Garzik
sc1200_set_piomode(struct ata_port * ap,struct ata_device * adev)72669a5db4SJeff Garzik static void sc1200_set_piomode(struct ata_port *ap, struct ata_device *adev)
73669a5db4SJeff Garzik {
74669a5db4SJeff Garzik static const u32 pio_timings[4][5] = {
75d0dd4a01SBartlomiej Zolnierkiewicz /* format0, 33Mhz */
76d0dd4a01SBartlomiej Zolnierkiewicz { 0x00009172, 0x00012171, 0x00020080, 0x00032010, 0x00040010 },
77d0dd4a01SBartlomiej Zolnierkiewicz /* format1, 33Mhz */
78d0dd4a01SBartlomiej Zolnierkiewicz { 0xd1329172, 0x71212171, 0x30200080, 0x20102010, 0x00100010 },
79d0dd4a01SBartlomiej Zolnierkiewicz /* format1, 48Mhz */
80d0dd4a01SBartlomiej Zolnierkiewicz { 0xfaa3f4f3, 0xc23232b2, 0x513101c1, 0x31213121, 0x10211021 },
81d0dd4a01SBartlomiej Zolnierkiewicz /* format1, 66Mhz */
82d0dd4a01SBartlomiej Zolnierkiewicz { 0xfff4fff4, 0xf35353d3, 0x814102f1, 0x42314231, 0x11311131 }
83669a5db4SJeff Garzik };
84669a5db4SJeff Garzik
85669a5db4SJeff Garzik struct pci_dev *pdev = to_pci_dev(ap->host->dev);
86669a5db4SJeff Garzik u32 format;
87669a5db4SJeff Garzik unsigned int reg = 0x40 + 0x10 * ap->port_no;
88669a5db4SJeff Garzik int mode = adev->pio_mode - XFER_PIO_0;
89669a5db4SJeff Garzik
90669a5db4SJeff Garzik pci_read_config_dword(pdev, reg + 4, &format);
91669a5db4SJeff Garzik format >>= 31;
92669a5db4SJeff Garzik format += sc1200_clock();
93669a5db4SJeff Garzik pci_write_config_dword(pdev, reg + 8 * adev->devno,
94669a5db4SJeff Garzik pio_timings[format][mode]);
95669a5db4SJeff Garzik }
96669a5db4SJeff Garzik
97669a5db4SJeff Garzik /**
98669a5db4SJeff Garzik * sc1200_set_dmamode - DMA timing setup
99669a5db4SJeff Garzik * @ap: ATA interface
100669a5db4SJeff Garzik * @adev: Device being configured
101669a5db4SJeff Garzik *
102669a5db4SJeff Garzik * We cannot mix MWDMA and UDMA without reloading timings each switch
103669a5db4SJeff Garzik * master to slave.
104669a5db4SJeff Garzik */
105669a5db4SJeff Garzik
sc1200_set_dmamode(struct ata_port * ap,struct ata_device * adev)106669a5db4SJeff Garzik static void sc1200_set_dmamode(struct ata_port *ap, struct ata_device *adev)
107669a5db4SJeff Garzik {
108669a5db4SJeff Garzik static const u32 udma_timing[3][3] = {
109669a5db4SJeff Garzik { 0x00921250, 0x00911140, 0x00911030 },
110669a5db4SJeff Garzik { 0x00932470, 0x00922260, 0x00922140 },
111669a5db4SJeff Garzik { 0x009436A1, 0x00933481, 0x00923261 }
112669a5db4SJeff Garzik };
113669a5db4SJeff Garzik
114669a5db4SJeff Garzik static const u32 mwdma_timing[3][3] = {
115669a5db4SJeff Garzik { 0x00077771, 0x00012121, 0x00002020 },
116669a5db4SJeff Garzik { 0x000BBBB2, 0x00024241, 0x00013131 },
117669a5db4SJeff Garzik { 0x000FFFF3, 0x00035352, 0x00015151 }
118669a5db4SJeff Garzik };
119669a5db4SJeff Garzik
120669a5db4SJeff Garzik int clock = sc1200_clock();
121669a5db4SJeff Garzik struct pci_dev *pdev = to_pci_dev(ap->host->dev);
122669a5db4SJeff Garzik unsigned int reg = 0x40 + 0x10 * ap->port_no;
123669a5db4SJeff Garzik int mode = adev->dma_mode;
124669a5db4SJeff Garzik u32 format;
125669a5db4SJeff Garzik
126669a5db4SJeff Garzik if (mode >= XFER_UDMA_0)
127669a5db4SJeff Garzik format = udma_timing[clock][mode - XFER_UDMA_0];
128669a5db4SJeff Garzik else
129669a5db4SJeff Garzik format = mwdma_timing[clock][mode - XFER_MW_DMA_0];
130669a5db4SJeff Garzik
131669a5db4SJeff Garzik if (adev->devno == 0) {
132669a5db4SJeff Garzik u32 timings;
133669a5db4SJeff Garzik
134669a5db4SJeff Garzik pci_read_config_dword(pdev, reg + 4, &timings);
135669a5db4SJeff Garzik timings &= 0x80000000UL;
136669a5db4SJeff Garzik timings |= format;
137669a5db4SJeff Garzik pci_write_config_dword(pdev, reg + 4, timings);
138669a5db4SJeff Garzik } else
139669a5db4SJeff Garzik pci_write_config_dword(pdev, reg + 12, format);
140669a5db4SJeff Garzik }
141669a5db4SJeff Garzik
142669a5db4SJeff Garzik /**
1439363c382STejun Heo * sc1200_qc_issue - command issue
144669a5db4SJeff Garzik * @qc: command pending
145669a5db4SJeff Garzik *
146669a5db4SJeff Garzik * Called when the libata layer is about to issue a command. We wrap
147669a5db4SJeff Garzik * this interface so that we can load the correct ATA timings if
1483a4fa0a2SRobert P. J. Day * necessary. Specifically we have a problem that there is only
149669a5db4SJeff Garzik * one MWDMA/UDMA bit.
150669a5db4SJeff Garzik */
151669a5db4SJeff Garzik
sc1200_qc_issue(struct ata_queued_cmd * qc)1529363c382STejun Heo static unsigned int sc1200_qc_issue(struct ata_queued_cmd *qc)
153669a5db4SJeff Garzik {
154669a5db4SJeff Garzik struct ata_port *ap = qc->ap;
155669a5db4SJeff Garzik struct ata_device *adev = qc->dev;
156669a5db4SJeff Garzik struct ata_device *prev = ap->private_data;
157669a5db4SJeff Garzik
158669a5db4SJeff Garzik /* See if the DMA settings could be wrong */
159b15b3ebaSAlan Cox if (ata_dma_enabled(adev) && adev != prev && prev != NULL) {
160669a5db4SJeff Garzik /* Maybe, but do the channels match MWDMA/UDMA ? */
161b15b3ebaSAlan Cox if ((ata_using_udma(adev) && !ata_using_udma(prev)) ||
162b15b3ebaSAlan Cox (ata_using_udma(prev) && !ata_using_udma(adev)))
163669a5db4SJeff Garzik /* Switch the mode bits */
164669a5db4SJeff Garzik sc1200_set_dmamode(ap, adev);
165669a5db4SJeff Garzik }
166669a5db4SJeff Garzik
167360ff783STejun Heo return ata_bmdma_qc_issue(qc);
168669a5db4SJeff Garzik }
169669a5db4SJeff Garzik
170c0f2ee34SAlan Cox /**
171c0f2ee34SAlan Cox * sc1200_qc_defer - implement serialization
172c0f2ee34SAlan Cox * @qc: command
173c0f2ee34SAlan Cox *
174c0f2ee34SAlan Cox * Serialize command issue on this controller.
175c0f2ee34SAlan Cox */
176c0f2ee34SAlan Cox
sc1200_qc_defer(struct ata_queued_cmd * qc)177c0f2ee34SAlan Cox static int sc1200_qc_defer(struct ata_queued_cmd *qc)
178c0f2ee34SAlan Cox {
179c0f2ee34SAlan Cox struct ata_host *host = qc->ap->host;
180c0f2ee34SAlan Cox struct ata_port *alt = host->ports[1 ^ qc->ap->port_no];
181c0f2ee34SAlan Cox int rc;
182c0f2ee34SAlan Cox
183c0f2ee34SAlan Cox /* First apply the usual rules */
184c0f2ee34SAlan Cox rc = ata_std_qc_defer(qc);
185c0f2ee34SAlan Cox if (rc != 0)
186c0f2ee34SAlan Cox return rc;
187c0f2ee34SAlan Cox
188c0f2ee34SAlan Cox /* Now apply serialization rules. Only allow a command if the
189c0f2ee34SAlan Cox other channel state machine is idle */
190c0f2ee34SAlan Cox if (alt && alt->qc_active)
191c0f2ee34SAlan Cox return ATA_DEFER_PORT;
192c0f2ee34SAlan Cox return 0;
193c0f2ee34SAlan Cox }
194c0f2ee34SAlan Cox
195*25df73d9SBart Van Assche static const struct scsi_host_template sc1200_sht = {
196160be1bcSLee Jones ATA_BASE_SHT(DRV_NAME),
197d26fc955SAlan Cox .sg_tablesize = LIBATA_DUMB_MAX_PRD,
198160be1bcSLee Jones .dma_boundary = ATA_DMA_BOUNDARY,
199669a5db4SJeff Garzik };
200669a5db4SJeff Garzik
201669a5db4SJeff Garzik static struct ata_port_operations sc1200_port_ops = {
202029cfd6bSTejun Heo .inherits = &ata_bmdma_port_ops,
203f47451c4STejun Heo .qc_prep = ata_bmdma_dumb_qc_prep,
2049363c382STejun Heo .qc_issue = sc1200_qc_issue,
205c0f2ee34SAlan Cox .qc_defer = sc1200_qc_defer,
206029cfd6bSTejun Heo .cable_detect = ata_cable_40wire,
207029cfd6bSTejun Heo .set_piomode = sc1200_set_piomode,
208029cfd6bSTejun Heo .set_dmamode = sc1200_set_dmamode,
209669a5db4SJeff Garzik };
210669a5db4SJeff Garzik
211669a5db4SJeff Garzik /**
212669a5db4SJeff Garzik * sc1200_init_one - Initialise an SC1200
213669a5db4SJeff Garzik * @dev: PCI device
214669a5db4SJeff Garzik * @id: Entry in match table
215669a5db4SJeff Garzik *
216669a5db4SJeff Garzik * Just throw the needed data at the libata helper and it does all
217669a5db4SJeff Garzik * our work.
218669a5db4SJeff Garzik */
219669a5db4SJeff Garzik
sc1200_init_one(struct pci_dev * dev,const struct pci_device_id * id)220669a5db4SJeff Garzik static int sc1200_init_one(struct pci_dev *dev, const struct pci_device_id *id)
221669a5db4SJeff Garzik {
2221626aeb8STejun Heo static const struct ata_port_info info = {
2231d2808fdSJeff Garzik .flags = ATA_FLAG_SLAVE_POSS,
22414bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4,
22514bdef98SErik Inge Bolsø .mwdma_mask = ATA_MWDMA2,
22614bdef98SErik Inge Bolsø .udma_mask = ATA_UDMA2,
227669a5db4SJeff Garzik .port_ops = &sc1200_port_ops
228669a5db4SJeff Garzik };
2296d4f950eSAlan Cox const struct ata_port_info *ppi[] = { &info, NULL };
2301626aeb8STejun Heo
2311c5afdf7STejun Heo return ata_pci_bmdma_init_one(dev, ppi, &sc1200_sht, NULL, 0);
232669a5db4SJeff Garzik }
233669a5db4SJeff Garzik
2342d2744fcSJeff Garzik static const struct pci_device_id sc1200[] = {
2352d2744fcSJeff Garzik { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_SCx200_IDE), },
2362d2744fcSJeff Garzik
2372d2744fcSJeff Garzik { },
238669a5db4SJeff Garzik };
239669a5db4SJeff Garzik
240669a5db4SJeff Garzik static struct pci_driver sc1200_pci_driver = {
241669a5db4SJeff Garzik .name = DRV_NAME,
242669a5db4SJeff Garzik .id_table = sc1200,
243669a5db4SJeff Garzik .probe = sc1200_init_one,
24430ced0f0SAlan .remove = ata_pci_remove_one,
24558eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
24630ced0f0SAlan .suspend = ata_pci_device_suspend,
24730ced0f0SAlan .resume = ata_pci_device_resume,
248438ac6d5STejun Heo #endif
249669a5db4SJeff Garzik };
250669a5db4SJeff Garzik
2512fc75da0SAxel Lin module_pci_driver(sc1200_pci_driver);
252669a5db4SJeff Garzik
253669a5db4SJeff Garzik MODULE_AUTHOR("Alan Cox, Mark Lord");
254669a5db4SJeff Garzik MODULE_DESCRIPTION("low-level driver for the NS/AMD SC1200");
255669a5db4SJeff Garzik MODULE_LICENSE("GPL");
256669a5db4SJeff Garzik MODULE_DEVICE_TABLE(pci, sc1200);
257669a5db4SJeff Garzik MODULE_VERSION(DRV_VERSION);
258