Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * pata_cmd640.c - CMD640 PCI PATA for new ATA layer |
| 3 | * (C) 2007 Red Hat Inc |
| 4 | * Alan Cox <alan@redhat.com> |
| 5 | * |
| 6 | * Based upon |
| 7 | * linux/drivers/ide/pci/cmd640.c Version 1.02 Sep 01, 1996 |
| 8 | * |
| 9 | * Copyright (C) 1995-1996 Linus Torvalds & authors (see driver) |
| 10 | * |
| 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 |
| 13 | * your own code. |
| 14 | */ |
| 15 | |
| 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> |
| 24 | |
| 25 | #define DRV_NAME "pata_cmd640" |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 26 | #define DRV_VERSION "0.0.5" |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 27 | |
| 28 | struct cmd640_reg { |
| 29 | int last; |
| 30 | u8 reg58[ATA_MAX_DEVICES]; |
| 31 | }; |
| 32 | |
| 33 | enum { |
| 34 | CFR = 0x50, |
| 35 | CNTRL = 0x51, |
| 36 | CMDTIM = 0x52, |
| 37 | ARTIM0 = 0x53, |
| 38 | DRWTIM0 = 0x54, |
| 39 | ARTIM23 = 0x57, |
| 40 | DRWTIM23 = 0x58, |
| 41 | BRST = 0x59 |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * cmd640_set_piomode - set initial PIO mode data |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 46 | * @ap: ATA port |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 47 | * @adev: ATA device |
| 48 | * |
| 49 | * Called to do the PIO mode setup. |
| 50 | */ |
| 51 | |
| 52 | static void cmd640_set_piomode(struct ata_port *ap, struct ata_device *adev) |
| 53 | { |
| 54 | struct cmd640_reg *timing = ap->private_data; |
| 55 | struct pci_dev *pdev = to_pci_dev(ap->host->dev); |
| 56 | struct ata_timing t; |
| 57 | const unsigned long T = 1000000 / 33; |
| 58 | const u8 setup_data[] = { 0x40, 0x40, 0x40, 0x80, 0x00 }; |
| 59 | u8 reg; |
| 60 | int arttim = ARTIM0 + 2 * adev->devno; |
| 61 | struct ata_device *pair = ata_dev_pair(adev); |
| 62 | |
| 63 | if (ata_timing_compute(adev, adev->pio_mode, &t, T, 0) < 0) { |
| 64 | printk(KERN_ERR DRV_NAME ": mode computation failed.\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 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) { |
| 71 | struct ata_timing p; |
| 72 | ata_timing_compute(pair, pair->pio_mode, &p, T, 1); |
| 73 | ata_timing_merge(&p, &t, &t, ATA_TIMING_SETUP); |
| 74 | } |
| 75 | |
| 76 | /* Make the timings fit */ |
| 77 | if (t.recover > 16) { |
| 78 | t.active += t.recover - 16; |
| 79 | t.recover = 16; |
| 80 | } |
| 81 | if (t.active > 16) |
| 82 | t.active = 16; |
| 83 | |
| 84 | /* Now convert the clocks into values we can actually stuff into |
| 85 | the chip */ |
| 86 | |
| 87 | if (t.recover > 1) |
| 88 | t.recover--; /* 640B only */ |
| 89 | else |
| 90 | t.recover = 15; |
| 91 | |
| 92 | if (t.setup > 4) |
| 93 | t.setup = 0xC0; |
| 94 | else |
| 95 | t.setup = setup_data[t.setup]; |
| 96 | |
| 97 | if (ap->port_no == 0) { |
| 98 | t.active &= 0x0F; /* 0 = 16 */ |
| 99 | |
| 100 | /* Load setup timing */ |
| 101 | pci_read_config_byte(pdev, arttim, ®); |
| 102 | reg &= 0x3F; |
| 103 | reg |= t.setup; |
| 104 | pci_write_config_byte(pdev, arttim, reg); |
| 105 | |
| 106 | /* Load active/recovery */ |
| 107 | pci_write_config_byte(pdev, arttim + 1, (t.active << 4) | t.recover); |
| 108 | } else { |
| 109 | /* Save the shared timings for channel, they will be loaded |
Jeff Garzik | a617c09 | 2007-05-21 20:14:23 -0400 | [diff] [blame] | 110 | by qc_issue_prot. Reloading the setup time is expensive |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 111 | so we keep a merged one loaded */ |
| 112 | pci_read_config_byte(pdev, ARTIM23, ®); |
| 113 | reg &= 0x3F; |
| 114 | reg |= t.setup; |
| 115 | pci_write_config_byte(pdev, ARTIM23, reg); |
| 116 | timing->reg58[adev->devno] = (t.active << 4) | t.recover; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /** |
| 122 | * cmd640_qc_issue_prot - command preparation hook |
| 123 | * @qc: Command to be issued |
| 124 | * |
| 125 | * Channel 1 has shared timings. We must reprogram the |
| 126 | * clock each drive 2/3 switch we do. |
| 127 | */ |
| 128 | |
| 129 | static unsigned int cmd640_qc_issue_prot(struct ata_queued_cmd *qc) |
| 130 | { |
| 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; |
| 135 | |
| 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; |
| 139 | } |
| 140 | return ata_qc_issue_prot(qc); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * cmd640_port_start - port setup |
| 145 | * @ap: ATA port being set up |
| 146 | * |
| 147 | * The CMD640 needs to maintain private data structures so we |
| 148 | * allocate space here. |
| 149 | */ |
| 150 | |
| 151 | static int cmd640_port_start(struct ata_port *ap) |
| 152 | { |
| 153 | struct pci_dev *pdev = to_pci_dev(ap->host->dev); |
| 154 | struct cmd640_reg *timing; |
| 155 | |
Alan Cox | 81ad183 | 2007-08-22 22:55:41 +0100 | [diff] [blame] | 156 | int ret = ata_sff_port_start(ap); |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 157 | if (ret < 0) |
| 158 | return ret; |
| 159 | |
| 160 | timing = devm_kzalloc(&pdev->dev, sizeof(struct cmd640_reg), GFP_KERNEL); |
| 161 | if (timing == NULL) |
| 162 | return -ENOMEM; |
| 163 | timing->last = -1; /* Force a load */ |
| 164 | ap->private_data = timing; |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | static struct scsi_host_template cmd640_sht = { |
Tejun Heo | 68d1d07 | 2008-03-25 12:22:49 +0900 | [diff] [blame^] | 169 | ATA_BMDMA_SHT(DRV_NAME), |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | static struct ata_port_operations cmd640_port_ops = { |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 173 | .set_piomode = cmd640_set_piomode, |
| 174 | .mode_filter = ata_pci_default_filter, |
| 175 | .tf_load = ata_tf_load, |
| 176 | .tf_read = ata_tf_read, |
| 177 | .check_status = ata_check_status, |
| 178 | .exec_command = ata_exec_command, |
| 179 | .dev_select = ata_std_dev_select, |
| 180 | |
| 181 | .freeze = ata_bmdma_freeze, |
| 182 | .thaw = ata_bmdma_thaw, |
| 183 | .error_handler = ata_bmdma_error_handler, |
| 184 | .post_internal_cmd = ata_bmdma_post_internal_cmd, |
| 185 | .cable_detect = ata_cable_40wire, |
| 186 | |
| 187 | .bmdma_setup = ata_bmdma_setup, |
| 188 | .bmdma_start = ata_bmdma_start, |
| 189 | .bmdma_stop = ata_bmdma_stop, |
| 190 | .bmdma_status = ata_bmdma_status, |
| 191 | |
| 192 | .qc_prep = ata_qc_prep, |
| 193 | .qc_issue = cmd640_qc_issue_prot, |
| 194 | |
| 195 | /* In theory this is not needed once we kill the prefetcher */ |
| 196 | .data_xfer = ata_data_xfer_noirq, |
| 197 | |
| 198 | .irq_handler = ata_interrupt, |
| 199 | .irq_clear = ata_bmdma_irq_clear, |
| 200 | .irq_on = ata_irq_on, |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 201 | |
| 202 | .port_start = cmd640_port_start, |
| 203 | }; |
| 204 | |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 205 | static void cmd640_hardware_init(struct pci_dev *pdev) |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 206 | { |
| 207 | u8 r; |
| 208 | u8 ctrl; |
| 209 | |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 210 | /* CMD640 detected, commiserations */ |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 211 | pci_write_config_byte(pdev, 0x5B, 0x00); |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 212 | /* Get version info */ |
| 213 | pci_read_config_byte(pdev, CFR, &r); |
| 214 | /* PIO0 command cycles */ |
| 215 | pci_write_config_byte(pdev, CMDTIM, 0); |
| 216 | /* 512 byte bursts (sector) */ |
| 217 | pci_write_config_byte(pdev, BRST, 0x40); |
Jeff Garzik | a617c09 | 2007-05-21 20:14:23 -0400 | [diff] [blame] | 218 | /* |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 219 | * A reporter a long time ago |
| 220 | * Had problems with the data fifo |
| 221 | * So don't run the risk |
| 222 | * Of putting crap on the disk |
| 223 | * For its better just to go slow |
| 224 | */ |
| 225 | /* Do channel 0 */ |
| 226 | pci_read_config_byte(pdev, CNTRL, &ctrl); |
| 227 | pci_write_config_byte(pdev, CNTRL, ctrl | 0xC0); |
| 228 | /* Ditto for channel 1 */ |
| 229 | pci_read_config_byte(pdev, ARTIM23, &ctrl); |
| 230 | ctrl |= 0x0C; |
| 231 | pci_write_config_byte(pdev, ARTIM23, ctrl); |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 232 | } |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 233 | |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 234 | static int cmd640_init_one(struct pci_dev *pdev, const struct pci_device_id *id) |
| 235 | { |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 236 | static const struct ata_port_info info = { |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 237 | .sht = &cmd640_sht, |
Jeff Garzik | 1d2808f | 2007-05-28 06:59:48 -0400 | [diff] [blame] | 238 | .flags = ATA_FLAG_SLAVE_POSS, |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 239 | .pio_mask = 0x1f, |
| 240 | .port_ops = &cmd640_port_ops |
| 241 | }; |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 242 | const struct ata_port_info *ppi[] = { &info, NULL }; |
Tejun Heo | f08048e | 2008-03-25 12:22:47 +0900 | [diff] [blame] | 243 | int rc; |
| 244 | |
| 245 | rc = pcim_enable_device(pdev); |
| 246 | if (rc) |
| 247 | return rc; |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 248 | |
| 249 | cmd640_hardware_init(pdev); |
Tejun Heo | f08048e | 2008-03-25 12:22:47 +0900 | [diff] [blame] | 250 | |
Tejun Heo | 1626aeb | 2007-05-04 12:43:58 +0200 | [diff] [blame] | 251 | return ata_pci_init_one(pdev, ppi); |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Tejun Heo | f08048e | 2008-03-25 12:22:47 +0900 | [diff] [blame] | 254 | #ifdef CONFIG_PM |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 255 | static int cmd640_reinit_one(struct pci_dev *pdev) |
| 256 | { |
Tejun Heo | f08048e | 2008-03-25 12:22:47 +0900 | [diff] [blame] | 257 | struct ata_host *host = dev_get_drvdata(&pdev->dev); |
| 258 | int rc; |
| 259 | |
| 260 | rc = ata_pci_device_do_resume(pdev); |
| 261 | if (rc) |
| 262 | return rc; |
Alan Cox | 7938a72 | 2007-03-07 16:43:29 +0000 | [diff] [blame] | 263 | cmd640_hardware_init(pdev); |
Tejun Heo | f08048e | 2008-03-25 12:22:47 +0900 | [diff] [blame] | 264 | ata_host_resume(host); |
Andrew Morton | 4b22afd | 2007-04-26 00:19:26 -0700 | [diff] [blame] | 265 | return 0; |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 266 | } |
Tejun Heo | f08048e | 2008-03-25 12:22:47 +0900 | [diff] [blame] | 267 | #endif |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 268 | |
| 269 | static const struct pci_device_id cmd640[] = { |
| 270 | { PCI_VDEVICE(CMD, 0x640), 0 }, |
| 271 | { }, |
| 272 | }; |
| 273 | |
| 274 | static struct pci_driver cmd640_pci_driver = { |
| 275 | .name = DRV_NAME, |
| 276 | .id_table = cmd640, |
| 277 | .probe = cmd640_init_one, |
| 278 | .remove = ata_pci_remove_one, |
Andrew Morton | 4b22afd | 2007-04-26 00:19:26 -0700 | [diff] [blame] | 279 | #ifdef CONFIG_PM |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 280 | .suspend = ata_pci_device_suspend, |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 281 | .resume = cmd640_reinit_one, |
Tejun Heo | f08048e | 2008-03-25 12:22:47 +0900 | [diff] [blame] | 282 | #endif |
Alan Cox | b2248da | 2007-03-06 02:38:11 -0800 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | static int __init cmd640_init(void) |
| 286 | { |
| 287 | return pci_register_driver(&cmd640_pci_driver); |
| 288 | } |
| 289 | |
| 290 | static void __exit cmd640_exit(void) |
| 291 | { |
| 292 | pci_unregister_driver(&cmd640_pci_driver); |
| 293 | } |
| 294 | |
| 295 | MODULE_AUTHOR("Alan Cox"); |
| 296 | MODULE_DESCRIPTION("low-level driver for CMD640 PATA controllers"); |
| 297 | MODULE_LICENSE("GPL"); |
| 298 | MODULE_DEVICE_TABLE(pci, cmd640); |
| 299 | MODULE_VERSION(DRV_VERSION); |
| 300 | |
| 301 | module_init(cmd640_init); |
| 302 | module_exit(cmd640_exit); |