2 * pata_cmd640.c - CMD640 PCI PATA for new ATA layer
6 * linux/drivers/ide/pci/cmd640.c Version 1.02 Sep 01, 1996
8 * Copyright (C) 1995-1996 Linus Torvalds & authors (see driver)
10 * This drives only the PCI version of the controller. If you have a
11 * VLB one then we have enough docs to support it but you can write
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
18 #include <linux/blkdev.h>
19 #include <linux/delay.h>
20 #include <linux/gfp.h>
21 #include <scsi/scsi_host.h>
22 #include <linux/libata.h>
24 #define DRV_NAME "pata_cmd640"
25 #define DRV_VERSION "0.0.5"
29 u8 reg58[ATA_MAX_DEVICES];
44 * cmd640_set_piomode - set initial PIO mode data
48 * Called to do the PIO mode setup.
51 static void cmd640_set_piomode(struct ata_port *ap, struct ata_device *adev)
53 struct cmd640_reg *timing = ap->private_data;
54 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
56 const unsigned long T = 1000000 / 33;
57 const u8 setup_data[] = { 0x40, 0x40, 0x40, 0x80, 0x00 };
59 int arttim = ARTIM0 + 2 * adev->devno;
60 struct ata_device *pair = ata_dev_pair(adev);
62 if (ata_timing_compute(adev, adev->pio_mode, &t, T, 0) < 0) {
63 printk(KERN_ERR DRV_NAME ": mode computation failed.\n");
67 /* The second channel has shared timings and the setup timing is
68 messy to switch to merge it for worst case */
69 if (ap->port_no && pair) {
71 ata_timing_compute(pair, pair->pio_mode, &p, T, 1);
72 ata_timing_merge(&p, &t, &t, ATA_TIMING_SETUP);
75 /* Make the timings fit */
77 t.active += t.recover - 16;
83 /* Now convert the clocks into values we can actually stuff into
87 t.recover--; /* 640B only */
94 t.setup = setup_data[t.setup];
96 if (ap->port_no == 0) {
97 t.active &= 0x0F; /* 0 = 16 */
99 /* Load setup timing */
100 pci_read_config_byte(pdev, arttim, ®);
103 pci_write_config_byte(pdev, arttim, reg);
105 /* Load active/recovery */
106 pci_write_config_byte(pdev, arttim + 1, (t.active << 4) | t.recover);
108 /* Save the shared timings for channel, they will be loaded
109 by qc_issue. Reloading the setup time is expensive so we
110 keep a merged one loaded */
111 pci_read_config_byte(pdev, ARTIM23, ®);
114 pci_write_config_byte(pdev, ARTIM23, reg);
115 timing->reg58[adev->devno] = (t.active << 4) | t.recover;
121 * cmd640_qc_issue - command preparation hook
122 * @qc: Command to be issued
124 * Channel 1 has shared timings. We must reprogram the
125 * clock each drive 2/3 switch we do.
128 static unsigned int cmd640_qc_issue(struct ata_queued_cmd *qc)
130 struct ata_port *ap = qc->ap;
131 struct ata_device *adev = qc->dev;
132 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
133 struct cmd640_reg *timing = ap->private_data;
135 if (ap->port_no != 0 && adev->devno != timing->last) {
136 pci_write_config_byte(pdev, DRWTIM23, timing->reg58[adev->devno]);
137 timing->last = adev->devno;
139 return ata_sff_qc_issue(qc);
143 * cmd640_port_start - port setup
144 * @ap: ATA port being set up
146 * The CMD640 needs to maintain private data structures so we
147 * allocate space here.
150 static int cmd640_port_start(struct ata_port *ap)
152 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
153 struct cmd640_reg *timing;
155 timing = devm_kzalloc(&pdev->dev, sizeof(struct cmd640_reg), GFP_KERNEL);
158 timing->last = -1; /* Force a load */
159 ap->private_data = timing;
163 static bool cmd640_sff_irq_check(struct ata_port *ap)
165 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
166 int irq_reg = ap->port_no ? ARTIM23 : CFR;
167 u8 irq_stat, irq_mask = ap->port_no ? 0x10 : 0x04;
169 pci_read_config_byte(pdev, irq_reg, &irq_stat);
171 return irq_stat & irq_mask;
174 static struct scsi_host_template cmd640_sht = {
175 ATA_PIO_SHT(DRV_NAME),
178 static struct ata_port_operations cmd640_port_ops = {
179 .inherits = &ata_sff_port_ops,
180 /* In theory xfer_noirq is not needed once we kill the prefetcher */
181 .sff_data_xfer = ata_sff_data_xfer_noirq,
182 .sff_irq_check = cmd640_sff_irq_check,
183 .qc_issue = cmd640_qc_issue,
184 .cable_detect = ata_cable_40wire,
185 .set_piomode = cmd640_set_piomode,
186 .port_start = cmd640_port_start,
189 static void cmd640_hardware_init(struct pci_dev *pdev)
193 /* CMD640 detected, commiserations */
194 pci_write_config_byte(pdev, 0x5B, 0x00);
195 /* PIO0 command cycles */
196 pci_write_config_byte(pdev, CMDTIM, 0);
197 /* 512 byte bursts (sector) */
198 pci_write_config_byte(pdev, BRST, 0x40);
200 * A reporter a long time ago
201 * Had problems with the data fifo
202 * So don't run the risk
203 * Of putting crap on the disk
204 * For its better just to go slow
207 pci_read_config_byte(pdev, CNTRL, &ctrl);
208 pci_write_config_byte(pdev, CNTRL, ctrl | 0xC0);
209 /* Ditto for channel 1 */
210 pci_read_config_byte(pdev, ARTIM23, &ctrl);
212 pci_write_config_byte(pdev, ARTIM23, ctrl);
215 static int cmd640_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
217 static const struct ata_port_info info = {
218 .flags = ATA_FLAG_SLAVE_POSS,
219 .pio_mask = ATA_PIO4,
220 .port_ops = &cmd640_port_ops
222 const struct ata_port_info *ppi[] = { &info, NULL };
225 rc = pcim_enable_device(pdev);
229 cmd640_hardware_init(pdev);
231 return ata_pci_sff_init_one(pdev, ppi, &cmd640_sht, NULL, 0);
234 #ifdef CONFIG_PM_SLEEP
235 static int cmd640_reinit_one(struct pci_dev *pdev)
237 struct ata_host *host = pci_get_drvdata(pdev);
240 rc = ata_pci_device_do_resume(pdev);
243 cmd640_hardware_init(pdev);
244 ata_host_resume(host);
249 static const struct pci_device_id cmd640[] = {
250 { PCI_VDEVICE(CMD, 0x640), 0 },
254 static struct pci_driver cmd640_pci_driver = {
257 .probe = cmd640_init_one,
258 .remove = ata_pci_remove_one,
259 #ifdef CONFIG_PM_SLEEP
260 .suspend = ata_pci_device_suspend,
261 .resume = cmd640_reinit_one,
265 module_pci_driver(cmd640_pci_driver);
267 MODULE_AUTHOR("Alan Cox");
268 MODULE_DESCRIPTION("low-level driver for CMD640 PATA controllers");
269 MODULE_LICENSE("GPL");
270 MODULE_DEVICE_TABLE(pci, cmd640);
271 MODULE_VERSION(DRV_VERSION);