109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b2248dacSAlan Cox /*
3b2248dacSAlan Cox * pata_cmd640.c - CMD640 PCI PATA for new ATA layer
4b2248dacSAlan Cox * (C) 2007 Red Hat Inc
5b2248dacSAlan Cox *
6b2248dacSAlan Cox * Based upon
7b2248dacSAlan Cox * linux/drivers/ide/pci/cmd640.c Version 1.02 Sep 01, 1996
8b2248dacSAlan Cox *
9b2248dacSAlan Cox * Copyright (C) 1995-1996 Linus Torvalds & authors (see driver)
10b2248dacSAlan Cox *
11b2248dacSAlan Cox * This drives only the PCI version of the controller. If you have a
12b2248dacSAlan Cox * VLB one then we have enough docs to support it but you can write
13b2248dacSAlan Cox * your own code.
14b2248dacSAlan Cox */
15b2248dacSAlan Cox
16b2248dacSAlan Cox #include <linux/kernel.h>
17b2248dacSAlan Cox #include <linux/module.h>
18b2248dacSAlan Cox #include <linux/pci.h>
19b2248dacSAlan Cox #include <linux/blkdev.h>
20b2248dacSAlan Cox #include <linux/delay.h>
215a0e3ad6STejun Heo #include <linux/gfp.h>
22b2248dacSAlan Cox #include <scsi/scsi_host.h>
23b2248dacSAlan Cox #include <linux/libata.h>
24b2248dacSAlan Cox
25b2248dacSAlan Cox #define DRV_NAME "pata_cmd640"
267938a72dSAlan Cox #define DRV_VERSION "0.0.5"
27b2248dacSAlan Cox
28b2248dacSAlan Cox struct cmd640_reg {
29b2248dacSAlan Cox int last;
30b2248dacSAlan Cox u8 reg58[ATA_MAX_DEVICES];
31b2248dacSAlan Cox };
32b2248dacSAlan Cox
33b2248dacSAlan Cox enum {
34b2248dacSAlan Cox CFR = 0x50,
35b2248dacSAlan Cox CNTRL = 0x51,
36b2248dacSAlan Cox CMDTIM = 0x52,
37b2248dacSAlan Cox ARTIM0 = 0x53,
38b2248dacSAlan Cox DRWTIM0 = 0x54,
39b2248dacSAlan Cox ARTIM23 = 0x57,
40b2248dacSAlan Cox DRWTIM23 = 0x58,
41b2248dacSAlan Cox BRST = 0x59
42b2248dacSAlan Cox };
43b2248dacSAlan Cox
44b2248dacSAlan Cox /**
45b2248dacSAlan Cox * cmd640_set_piomode - set initial PIO mode data
467938a72dSAlan Cox * @ap: ATA port
47b2248dacSAlan Cox * @adev: ATA device
48b2248dacSAlan Cox *
49b2248dacSAlan Cox * Called to do the PIO mode setup.
50b2248dacSAlan Cox */
51b2248dacSAlan Cox
cmd640_set_piomode(struct ata_port * ap,struct ata_device * adev)52b2248dacSAlan Cox static void cmd640_set_piomode(struct ata_port *ap, struct ata_device *adev)
53b2248dacSAlan Cox {
54b2248dacSAlan Cox struct cmd640_reg *timing = ap->private_data;
55b2248dacSAlan Cox struct pci_dev *pdev = to_pci_dev(ap->host->dev);
56b2248dacSAlan Cox struct ata_timing t;
57b2248dacSAlan Cox const unsigned long T = 1000000 / 33;
58b2248dacSAlan Cox const u8 setup_data[] = { 0x40, 0x40, 0x40, 0x80, 0x00 };
59b2248dacSAlan Cox u8 reg;
60b2248dacSAlan Cox int arttim = ARTIM0 + 2 * adev->devno;
61b2248dacSAlan Cox struct ata_device *pair = ata_dev_pair(adev);
62b2248dacSAlan Cox
63b2248dacSAlan Cox if (ata_timing_compute(adev, adev->pio_mode, &t, T, 0) < 0) {
640f1c1294SHannes Reinecke ata_dev_err(adev, DRV_NAME ": mode computation failed.\n");
65b2248dacSAlan Cox return;
66b2248dacSAlan Cox }
67b2248dacSAlan Cox
68b2248dacSAlan Cox /* The second channel has shared timings and the setup timing is
69b2248dacSAlan Cox messy to switch to merge it for worst case */
70b2248dacSAlan Cox if (ap->port_no && pair) {
71b2248dacSAlan Cox struct ata_timing p;
72b2248dacSAlan Cox ata_timing_compute(pair, pair->pio_mode, &p, T, 1);
73b2248dacSAlan Cox ata_timing_merge(&p, &t, &t, ATA_TIMING_SETUP);
74b2248dacSAlan Cox }
75b2248dacSAlan Cox
76b2248dacSAlan Cox /* Make the timings fit */
77b2248dacSAlan Cox if (t.recover > 16) {
78b2248dacSAlan Cox t.active += t.recover - 16;
79b2248dacSAlan Cox t.recover = 16;
80b2248dacSAlan Cox }
81b2248dacSAlan Cox if (t.active > 16)
82b2248dacSAlan Cox t.active = 16;
83b2248dacSAlan Cox
84b2248dacSAlan Cox /* Now convert the clocks into values we can actually stuff into
85b2248dacSAlan Cox the chip */
86b2248dacSAlan Cox
87b2248dacSAlan Cox if (t.recover > 1)
88b2248dacSAlan Cox t.recover--; /* 640B only */
89b2248dacSAlan Cox else
90b2248dacSAlan Cox t.recover = 15;
91b2248dacSAlan Cox
92b2248dacSAlan Cox if (t.setup > 4)
93b2248dacSAlan Cox t.setup = 0xC0;
94b2248dacSAlan Cox else
95b2248dacSAlan Cox t.setup = setup_data[t.setup];
96b2248dacSAlan Cox
97b2248dacSAlan Cox if (ap->port_no == 0) {
98b2248dacSAlan Cox t.active &= 0x0F; /* 0 = 16 */
99b2248dacSAlan Cox
100b2248dacSAlan Cox /* Load setup timing */
101b2248dacSAlan Cox pci_read_config_byte(pdev, arttim, ®);
102b2248dacSAlan Cox reg &= 0x3F;
103b2248dacSAlan Cox reg |= t.setup;
104b2248dacSAlan Cox pci_write_config_byte(pdev, arttim, reg);
105b2248dacSAlan Cox
106b2248dacSAlan Cox /* Load active/recovery */
107b2248dacSAlan Cox pci_write_config_byte(pdev, arttim + 1, (t.active << 4) | t.recover);
108b2248dacSAlan Cox } else {
109b2248dacSAlan Cox /* Save the shared timings for channel, they will be loaded
1109363c382STejun Heo by qc_issue. Reloading the setup time is expensive so we
1119363c382STejun Heo keep a merged one loaded */
112b2248dacSAlan Cox pci_read_config_byte(pdev, ARTIM23, ®);
113b2248dacSAlan Cox reg &= 0x3F;
114b2248dacSAlan Cox reg |= t.setup;
115b2248dacSAlan Cox pci_write_config_byte(pdev, ARTIM23, reg);
116b2248dacSAlan Cox timing->reg58[adev->devno] = (t.active << 4) | t.recover;
117b2248dacSAlan Cox }
118b2248dacSAlan Cox }
119b2248dacSAlan Cox
120b2248dacSAlan Cox
121b2248dacSAlan Cox /**
1229363c382STejun Heo * cmd640_qc_issue - command preparation hook
123b2248dacSAlan Cox * @qc: Command to be issued
124b2248dacSAlan Cox *
125b2248dacSAlan Cox * Channel 1 has shared timings. We must reprogram the
126b2248dacSAlan Cox * clock each drive 2/3 switch we do.
127b2248dacSAlan Cox */
128b2248dacSAlan Cox
cmd640_qc_issue(struct ata_queued_cmd * qc)1299363c382STejun Heo static unsigned int cmd640_qc_issue(struct ata_queued_cmd *qc)
130b2248dacSAlan Cox {
131b2248dacSAlan Cox struct ata_port *ap = qc->ap;
132b2248dacSAlan Cox struct ata_device *adev = qc->dev;
133b2248dacSAlan Cox struct pci_dev *pdev = to_pci_dev(ap->host->dev);
134b2248dacSAlan Cox struct cmd640_reg *timing = ap->private_data;
135b2248dacSAlan Cox
136b2248dacSAlan Cox if (ap->port_no != 0 && adev->devno != timing->last) {
137b2248dacSAlan Cox pci_write_config_byte(pdev, DRWTIM23, timing->reg58[adev->devno]);
138b2248dacSAlan Cox timing->last = adev->devno;
139b2248dacSAlan Cox }
1409363c382STejun Heo return ata_sff_qc_issue(qc);
141b2248dacSAlan Cox }
142b2248dacSAlan Cox
143b2248dacSAlan Cox /**
144b2248dacSAlan Cox * cmd640_port_start - port setup
145b2248dacSAlan Cox * @ap: ATA port being set up
146b2248dacSAlan Cox *
147b2248dacSAlan Cox * The CMD640 needs to maintain private data structures so we
148b2248dacSAlan Cox * allocate space here.
149b2248dacSAlan Cox */
150b2248dacSAlan Cox
cmd640_port_start(struct ata_port * ap)151b2248dacSAlan Cox static int cmd640_port_start(struct ata_port *ap)
152b2248dacSAlan Cox {
153b2248dacSAlan Cox struct pci_dev *pdev = to_pci_dev(ap->host->dev);
154b2248dacSAlan Cox struct cmd640_reg *timing;
155b2248dacSAlan Cox
156b2248dacSAlan Cox timing = devm_kzalloc(&pdev->dev, sizeof(struct cmd640_reg), GFP_KERNEL);
157b2248dacSAlan Cox if (timing == NULL)
158b2248dacSAlan Cox return -ENOMEM;
159b2248dacSAlan Cox timing->last = -1; /* Force a load */
160b2248dacSAlan Cox ap->private_data = timing;
161c7087652STejun Heo return 0;
162b2248dacSAlan Cox }
163b2248dacSAlan Cox
cmd640_sff_irq_check(struct ata_port * ap)164c1ce90f2SSergei Shtylyov static bool cmd640_sff_irq_check(struct ata_port *ap)
165c1ce90f2SSergei Shtylyov {
166c1ce90f2SSergei Shtylyov struct pci_dev *pdev = to_pci_dev(ap->host->dev);
167c1ce90f2SSergei Shtylyov int irq_reg = ap->port_no ? ARTIM23 : CFR;
168c1ce90f2SSergei Shtylyov u8 irq_stat, irq_mask = ap->port_no ? 0x10 : 0x04;
169c1ce90f2SSergei Shtylyov
170c1ce90f2SSergei Shtylyov pci_read_config_byte(pdev, irq_reg, &irq_stat);
171c1ce90f2SSergei Shtylyov
172c1ce90f2SSergei Shtylyov return irq_stat & irq_mask;
173c1ce90f2SSergei Shtylyov }
174c1ce90f2SSergei Shtylyov
175*25df73d9SBart Van Assche static const struct scsi_host_template cmd640_sht = {
1768930ff25STejun Heo ATA_PIO_SHT(DRV_NAME),
177b2248dacSAlan Cox };
178b2248dacSAlan Cox
179b2248dacSAlan Cox static struct ata_port_operations cmd640_port_ops = {
1808930ff25STejun Heo .inherits = &ata_sff_port_ops,
181029cfd6bSTejun Heo /* In theory xfer_noirq is not needed once we kill the prefetcher */
18223ebda2fSSebastian Andrzej Siewior .sff_data_xfer = ata_sff_data_xfer32,
183c1ce90f2SSergei Shtylyov .sff_irq_check = cmd640_sff_irq_check,
1849363c382STejun Heo .qc_issue = cmd640_qc_issue,
185029cfd6bSTejun Heo .cable_detect = ata_cable_40wire,
186029cfd6bSTejun Heo .set_piomode = cmd640_set_piomode,
187b2248dacSAlan Cox .port_start = cmd640_port_start,
188b2248dacSAlan Cox };
189b2248dacSAlan Cox
cmd640_hardware_init(struct pci_dev * pdev)1907938a72dSAlan Cox static void cmd640_hardware_init(struct pci_dev *pdev)
191b2248dacSAlan Cox {
192b2248dacSAlan Cox u8 ctrl;
193b2248dacSAlan Cox
194b2248dacSAlan Cox /* CMD640 detected, commiserations */
1957938a72dSAlan Cox pci_write_config_byte(pdev, 0x5B, 0x00);
196b2248dacSAlan Cox /* PIO0 command cycles */
197b2248dacSAlan Cox pci_write_config_byte(pdev, CMDTIM, 0);
198b2248dacSAlan Cox /* 512 byte bursts (sector) */
199b2248dacSAlan Cox pci_write_config_byte(pdev, BRST, 0x40);
200b2248dacSAlan Cox /*
201b2248dacSAlan Cox * A reporter a long time ago
202b2248dacSAlan Cox * Had problems with the data fifo
203b2248dacSAlan Cox * So don't run the risk
204b2248dacSAlan Cox * Of putting crap on the disk
205b2248dacSAlan Cox * For its better just to go slow
206b2248dacSAlan Cox */
207b2248dacSAlan Cox /* Do channel 0 */
208b2248dacSAlan Cox pci_read_config_byte(pdev, CNTRL, &ctrl);
209b2248dacSAlan Cox pci_write_config_byte(pdev, CNTRL, ctrl | 0xC0);
210b2248dacSAlan Cox /* Ditto for channel 1 */
211b2248dacSAlan Cox pci_read_config_byte(pdev, ARTIM23, &ctrl);
212b2248dacSAlan Cox ctrl |= 0x0C;
213b2248dacSAlan Cox pci_write_config_byte(pdev, ARTIM23, ctrl);
2147938a72dSAlan Cox }
215b2248dacSAlan Cox
cmd640_init_one(struct pci_dev * pdev,const struct pci_device_id * id)2167938a72dSAlan Cox static int cmd640_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
2177938a72dSAlan Cox {
2181626aeb8STejun Heo static const struct ata_port_info info = {
2191d2808fdSJeff Garzik .flags = ATA_FLAG_SLAVE_POSS,
22014bdef98SErik Inge Bolsø .pio_mask = ATA_PIO4,
2217938a72dSAlan Cox .port_ops = &cmd640_port_ops
2227938a72dSAlan Cox };
2231626aeb8STejun Heo const struct ata_port_info *ppi[] = { &info, NULL };
224f08048e9STejun Heo int rc;
225f08048e9STejun Heo
226f08048e9STejun Heo rc = pcim_enable_device(pdev);
227f08048e9STejun Heo if (rc)
228f08048e9STejun Heo return rc;
2297938a72dSAlan Cox
2307938a72dSAlan Cox cmd640_hardware_init(pdev);
231f08048e9STejun Heo
23216ea0fc9SAlan Cox return ata_pci_sff_init_one(pdev, ppi, &cmd640_sht, NULL, 0);
233b2248dacSAlan Cox }
234b2248dacSAlan Cox
23558eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
cmd640_reinit_one(struct pci_dev * pdev)236b2248dacSAlan Cox static int cmd640_reinit_one(struct pci_dev *pdev)
237b2248dacSAlan Cox {
2380a86e1c8SJingoo Han struct ata_host *host = pci_get_drvdata(pdev);
239f08048e9STejun Heo int rc;
240f08048e9STejun Heo
241f08048e9STejun Heo rc = ata_pci_device_do_resume(pdev);
242f08048e9STejun Heo if (rc)
243f08048e9STejun Heo return rc;
2447938a72dSAlan Cox cmd640_hardware_init(pdev);
245f08048e9STejun Heo ata_host_resume(host);
2464b22afd7SAndrew Morton return 0;
247b2248dacSAlan Cox }
248f08048e9STejun Heo #endif
249b2248dacSAlan Cox
250b2248dacSAlan Cox static const struct pci_device_id cmd640[] = {
251b2248dacSAlan Cox { PCI_VDEVICE(CMD, 0x640), 0 },
252b2248dacSAlan Cox { },
253b2248dacSAlan Cox };
254b2248dacSAlan Cox
255b2248dacSAlan Cox static struct pci_driver cmd640_pci_driver = {
256b2248dacSAlan Cox .name = DRV_NAME,
257b2248dacSAlan Cox .id_table = cmd640,
258b2248dacSAlan Cox .probe = cmd640_init_one,
259b2248dacSAlan Cox .remove = ata_pci_remove_one,
26058eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
261b2248dacSAlan Cox .suspend = ata_pci_device_suspend,
262b2248dacSAlan Cox .resume = cmd640_reinit_one,
263f08048e9STejun Heo #endif
264b2248dacSAlan Cox };
265b2248dacSAlan Cox
2662fc75da0SAxel Lin module_pci_driver(cmd640_pci_driver);
267b2248dacSAlan Cox
268b2248dacSAlan Cox MODULE_AUTHOR("Alan Cox");
269b2248dacSAlan Cox MODULE_DESCRIPTION("low-level driver for CMD640 PATA controllers");
270b2248dacSAlan Cox MODULE_LICENSE("GPL");
271b2248dacSAlan Cox MODULE_DEVICE_TABLE(pci, cmd640);
272b2248dacSAlan Cox MODULE_VERSION(DRV_VERSION);
273