2 * pata_cmd640.c - CMD640 PCI PATA for new ATA layer
4 * Alan Cox <alan@redhat.com>
7 * linux/drivers/ide/pci/cmd640.c Version 1.02 Sep 01, 1996
9 * Copyright (C) 1995-1996 Linus Torvalds & authors (see driver)
11 * This drives only the PCI version of the controller. If you have a
12 * VLB one then we have enough docs to support it but you can write
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/init.h>
20 #include <linux/blkdev.h>
21 #include <linux/delay.h>
22 #include <scsi/scsi_host.h>
23 #include <linux/libata.h>
25 #define DRV_NAME "pata_cmd640"
26 #define DRV_VERSION "0.0.5"
30 u8 reg58[ATA_MAX_DEVICES];
45 * cmd640_set_piomode - set initial PIO mode data
49 * Called to do the PIO mode setup.
52 static void cmd640_set_piomode(struct ata_port *ap, struct ata_device *adev)
54 struct cmd640_reg *timing = ap->private_data;
55 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
57 const unsigned long T = 1000000 / 33;
58 const u8 setup_data[] = { 0x40, 0x40, 0x40, 0x80, 0x00 };
60 int arttim = ARTIM0 + 2 * adev->devno;
61 struct ata_device *pair = ata_dev_pair(adev);
63 if (ata_timing_compute(adev, adev->pio_mode, &t, T, 0) < 0) {
64 printk(KERN_ERR DRV_NAME ": mode computation failed.\n");
68 /* The second channel has shared timings and the setup timing is
69 messy to switch to merge it for worst case */
70 if (ap->port_no && pair) {
72 ata_timing_compute(pair, pair->pio_mode, &p, T, 1);
73 ata_timing_merge(&p, &t, &t, ATA_TIMING_SETUP);
76 /* Make the timings fit */
78 t.active += t.recover - 16;
84 /* Now convert the clocks into values we can actually stuff into
88 t.recover--; /* 640B only */
95 t.setup = setup_data[t.setup];
97 if (ap->port_no == 0) {
98 t.active &= 0x0F; /* 0 = 16 */
100 /* Load setup timing */
101 pci_read_config_byte(pdev, arttim, ®);
104 pci_write_config_byte(pdev, arttim, reg);
106 /* Load active/recovery */
107 pci_write_config_byte(pdev, arttim + 1, (t.active << 4) | t.recover);
109 /* Save the shared timings for channel, they will be loaded
110 by qc_issue_prot. Reloading the setup time is expensive
111 so we keep a merged one loaded */
112 pci_read_config_byte(pdev, ARTIM23, ®);
115 pci_write_config_byte(pdev, ARTIM23, reg);
116 timing->reg58[adev->devno] = (t.active << 4) | t.recover;
122 * cmd640_qc_issue_prot - command preparation hook
123 * @qc: Command to be issued
125 * Channel 1 has shared timings. We must reprogram the
126 * clock each drive 2/3 switch we do.
129 static unsigned int cmd640_qc_issue_prot(struct ata_queued_cmd *qc)
131 struct ata_port *ap = qc->ap;
132 struct ata_device *adev = qc->dev;
133 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
134 struct cmd640_reg *timing = ap->private_data;
136 if (ap->port_no != 0 && adev->devno != timing->last) {
137 pci_write_config_byte(pdev, DRWTIM23, timing->reg58[adev->devno]);
138 timing->last = adev->devno;
140 return ata_qc_issue_prot(qc);
144 * cmd640_port_start - port setup
145 * @ap: ATA port being set up
147 * The CMD640 needs to maintain private data structures so we
148 * allocate space here.
151 static int cmd640_port_start(struct ata_port *ap)
153 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
154 struct cmd640_reg *timing;
156 int ret = ata_port_start(ap);
160 timing = devm_kzalloc(&pdev->dev, sizeof(struct cmd640_reg), GFP_KERNEL);
163 timing->last = -1; /* Force a load */
164 ap->private_data = timing;
168 static struct scsi_host_template cmd640_sht = {
169 .module = THIS_MODULE,
171 .ioctl = ata_scsi_ioctl,
172 .queuecommand = ata_scsi_queuecmd,
173 .can_queue = ATA_DEF_QUEUE,
174 .this_id = ATA_SHT_THIS_ID,
175 .sg_tablesize = LIBATA_MAX_PRD,
176 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
177 .emulated = ATA_SHT_EMULATED,
178 .use_clustering = ATA_SHT_USE_CLUSTERING,
179 .proc_name = DRV_NAME,
180 .dma_boundary = ATA_DMA_BOUNDARY,
181 .slave_configure = ata_scsi_slave_config,
182 .slave_destroy = ata_scsi_slave_destroy,
183 .bios_param = ata_std_bios_param,
186 static struct ata_port_operations cmd640_port_ops = {
187 .port_disable = ata_port_disable,
188 .set_piomode = cmd640_set_piomode,
189 .mode_filter = ata_pci_default_filter,
190 .tf_load = ata_tf_load,
191 .tf_read = ata_tf_read,
192 .check_status = ata_check_status,
193 .exec_command = ata_exec_command,
194 .dev_select = ata_std_dev_select,
196 .freeze = ata_bmdma_freeze,
197 .thaw = ata_bmdma_thaw,
198 .error_handler = ata_bmdma_error_handler,
199 .post_internal_cmd = ata_bmdma_post_internal_cmd,
200 .cable_detect = ata_cable_40wire,
202 .bmdma_setup = ata_bmdma_setup,
203 .bmdma_start = ata_bmdma_start,
204 .bmdma_stop = ata_bmdma_stop,
205 .bmdma_status = ata_bmdma_status,
207 .qc_prep = ata_qc_prep,
208 .qc_issue = cmd640_qc_issue_prot,
210 /* In theory this is not needed once we kill the prefetcher */
211 .data_xfer = ata_data_xfer_noirq,
213 .irq_handler = ata_interrupt,
214 .irq_clear = ata_bmdma_irq_clear,
215 .irq_on = ata_irq_on,
216 .irq_ack = ata_irq_ack,
218 .port_start = cmd640_port_start,
221 static void cmd640_hardware_init(struct pci_dev *pdev)
226 /* CMD640 detected, commiserations */
227 pci_write_config_byte(pdev, 0x5B, 0x00);
228 /* Get version info */
229 pci_read_config_byte(pdev, CFR, &r);
230 /* PIO0 command cycles */
231 pci_write_config_byte(pdev, CMDTIM, 0);
232 /* 512 byte bursts (sector) */
233 pci_write_config_byte(pdev, BRST, 0x40);
235 * A reporter a long time ago
236 * Had problems with the data fifo
237 * So don't run the risk
238 * Of putting crap on the disk
239 * For its better just to go slow
242 pci_read_config_byte(pdev, CNTRL, &ctrl);
243 pci_write_config_byte(pdev, CNTRL, ctrl | 0xC0);
244 /* Ditto for channel 1 */
245 pci_read_config_byte(pdev, ARTIM23, &ctrl);
247 pci_write_config_byte(pdev, ARTIM23, ctrl);
250 static int cmd640_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
252 static const struct ata_port_info info = {
254 .flags = ATA_FLAG_SLAVE_POSS,
256 .port_ops = &cmd640_port_ops
258 const struct ata_port_info *ppi[] = { &info, NULL };
260 cmd640_hardware_init(pdev);
261 return ata_pci_init_one(pdev, ppi);
264 static int cmd640_reinit_one(struct pci_dev *pdev)
266 cmd640_hardware_init(pdev);
268 return ata_pci_device_resume(pdev);
274 static const struct pci_device_id cmd640[] = {
275 { PCI_VDEVICE(CMD, 0x640), 0 },
279 static struct pci_driver cmd640_pci_driver = {
282 .probe = cmd640_init_one,
283 .remove = ata_pci_remove_one,
285 .suspend = ata_pci_device_suspend,
287 .resume = cmd640_reinit_one,
290 static int __init cmd640_init(void)
292 return pci_register_driver(&cmd640_pci_driver);
295 static void __exit cmd640_exit(void)
297 pci_unregister_driver(&cmd640_pci_driver);
300 MODULE_AUTHOR("Alan Cox");
301 MODULE_DESCRIPTION("low-level driver for CMD640 PATA controllers");
302 MODULE_LICENSE("GPL");
303 MODULE_DEVICE_TABLE(pci, cmd640);
304 MODULE_VERSION(DRV_VERSION);
306 module_init(cmd640_init);
307 module_exit(cmd640_exit);