1b2248dacSAlan Cox /* 2b2248dacSAlan Cox * pata_cmd640.c - CMD640 PCI PATA for new ATA layer 3b2248dacSAlan Cox * (C) 2007 Red Hat Inc 4b2248dacSAlan Cox * Alan Cox <alan@redhat.com> 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/init.h> 20b2248dacSAlan Cox #include <linux/blkdev.h> 21b2248dacSAlan Cox #include <linux/delay.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 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) { 64b2248dacSAlan Cox printk(KERN_ERR 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 110b2248dacSAlan Cox by qc_issue_prot. Reloading the setup time is expensive 111b2248dacSAlan Cox so we 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 /** 122b2248dacSAlan Cox * cmd640_qc_issue_prot - 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 129b2248dacSAlan Cox static unsigned int cmd640_qc_issue_prot(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 } 140b2248dacSAlan Cox return ata_qc_issue_prot(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 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 int ret = ata_port_start(ap); 157b2248dacSAlan Cox if (ret < 0) 158b2248dacSAlan Cox return ret; 159b2248dacSAlan Cox 160b2248dacSAlan Cox timing = devm_kzalloc(&pdev->dev, sizeof(struct cmd640_reg), GFP_KERNEL); 161b2248dacSAlan Cox if (timing == NULL) 162b2248dacSAlan Cox return -ENOMEM; 163b2248dacSAlan Cox timing->last = -1; /* Force a load */ 164b2248dacSAlan Cox ap->private_data = timing; 165b2248dacSAlan Cox return ret; 166b2248dacSAlan Cox } 167b2248dacSAlan Cox 168b2248dacSAlan Cox static struct scsi_host_template cmd640_sht = { 169b2248dacSAlan Cox .module = THIS_MODULE, 170b2248dacSAlan Cox .name = DRV_NAME, 171b2248dacSAlan Cox .ioctl = ata_scsi_ioctl, 172b2248dacSAlan Cox .queuecommand = ata_scsi_queuecmd, 173b2248dacSAlan Cox .can_queue = ATA_DEF_QUEUE, 174b2248dacSAlan Cox .this_id = ATA_SHT_THIS_ID, 175b2248dacSAlan Cox .sg_tablesize = LIBATA_MAX_PRD, 176b2248dacSAlan Cox .cmd_per_lun = ATA_SHT_CMD_PER_LUN, 177b2248dacSAlan Cox .emulated = ATA_SHT_EMULATED, 178b2248dacSAlan Cox .use_clustering = ATA_SHT_USE_CLUSTERING, 179b2248dacSAlan Cox .proc_name = DRV_NAME, 180b2248dacSAlan Cox .dma_boundary = ATA_DMA_BOUNDARY, 181b2248dacSAlan Cox .slave_configure = ata_scsi_slave_config, 182b2248dacSAlan Cox .slave_destroy = ata_scsi_slave_destroy, 183b2248dacSAlan Cox .bios_param = ata_std_bios_param, 184b2248dacSAlan Cox }; 185b2248dacSAlan Cox 186b2248dacSAlan Cox static struct ata_port_operations cmd640_port_ops = { 187b2248dacSAlan Cox .port_disable = ata_port_disable, 188b2248dacSAlan Cox .set_piomode = cmd640_set_piomode, 189b2248dacSAlan Cox .mode_filter = ata_pci_default_filter, 190b2248dacSAlan Cox .tf_load = ata_tf_load, 191b2248dacSAlan Cox .tf_read = ata_tf_read, 192b2248dacSAlan Cox .check_status = ata_check_status, 193b2248dacSAlan Cox .exec_command = ata_exec_command, 194b2248dacSAlan Cox .dev_select = ata_std_dev_select, 195b2248dacSAlan Cox 196b2248dacSAlan Cox .freeze = ata_bmdma_freeze, 197b2248dacSAlan Cox .thaw = ata_bmdma_thaw, 198b2248dacSAlan Cox .error_handler = ata_bmdma_error_handler, 199b2248dacSAlan Cox .post_internal_cmd = ata_bmdma_post_internal_cmd, 200b2248dacSAlan Cox .cable_detect = ata_cable_40wire, 201b2248dacSAlan Cox 202b2248dacSAlan Cox .bmdma_setup = ata_bmdma_setup, 203b2248dacSAlan Cox .bmdma_start = ata_bmdma_start, 204b2248dacSAlan Cox .bmdma_stop = ata_bmdma_stop, 205b2248dacSAlan Cox .bmdma_status = ata_bmdma_status, 206b2248dacSAlan Cox 207b2248dacSAlan Cox .qc_prep = ata_qc_prep, 208b2248dacSAlan Cox .qc_issue = cmd640_qc_issue_prot, 209b2248dacSAlan Cox 210b2248dacSAlan Cox /* In theory this is not needed once we kill the prefetcher */ 211b2248dacSAlan Cox .data_xfer = ata_data_xfer_noirq, 212b2248dacSAlan Cox 213b2248dacSAlan Cox .irq_handler = ata_interrupt, 214b2248dacSAlan Cox .irq_clear = ata_bmdma_irq_clear, 215b2248dacSAlan Cox .irq_on = ata_irq_on, 216b2248dacSAlan Cox .irq_ack = ata_irq_ack, 217b2248dacSAlan Cox 218b2248dacSAlan Cox .port_start = cmd640_port_start, 219b2248dacSAlan Cox }; 220b2248dacSAlan Cox 2217938a72dSAlan Cox static void cmd640_hardware_init(struct pci_dev *pdev) 222b2248dacSAlan Cox { 223b2248dacSAlan Cox u8 r; 224b2248dacSAlan Cox u8 ctrl; 225b2248dacSAlan Cox 226b2248dacSAlan Cox /* CMD640 detected, commiserations */ 2277938a72dSAlan Cox pci_write_config_byte(pdev, 0x5B, 0x00); 228b2248dacSAlan Cox /* Get version info */ 229b2248dacSAlan Cox pci_read_config_byte(pdev, CFR, &r); 230b2248dacSAlan Cox /* PIO0 command cycles */ 231b2248dacSAlan Cox pci_write_config_byte(pdev, CMDTIM, 0); 232b2248dacSAlan Cox /* 512 byte bursts (sector) */ 233b2248dacSAlan Cox pci_write_config_byte(pdev, BRST, 0x40); 234b2248dacSAlan Cox /* 235b2248dacSAlan Cox * A reporter a long time ago 236b2248dacSAlan Cox * Had problems with the data fifo 237b2248dacSAlan Cox * So don't run the risk 238b2248dacSAlan Cox * Of putting crap on the disk 239b2248dacSAlan Cox * For its better just to go slow 240b2248dacSAlan Cox */ 241b2248dacSAlan Cox /* Do channel 0 */ 242b2248dacSAlan Cox pci_read_config_byte(pdev, CNTRL, &ctrl); 243b2248dacSAlan Cox pci_write_config_byte(pdev, CNTRL, ctrl | 0xC0); 244b2248dacSAlan Cox /* Ditto for channel 1 */ 245b2248dacSAlan Cox pci_read_config_byte(pdev, ARTIM23, &ctrl); 246b2248dacSAlan Cox ctrl |= 0x0C; 247b2248dacSAlan Cox pci_write_config_byte(pdev, ARTIM23, ctrl); 2487938a72dSAlan Cox } 249b2248dacSAlan Cox 2507938a72dSAlan Cox static int cmd640_init_one(struct pci_dev *pdev, const struct pci_device_id *id) 2517938a72dSAlan Cox { 252*1626aeb8STejun Heo static const struct ata_port_info info = { 2537938a72dSAlan Cox .sht = &cmd640_sht, 2547938a72dSAlan Cox .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST, 2557938a72dSAlan Cox .pio_mask = 0x1f, 2567938a72dSAlan Cox .port_ops = &cmd640_port_ops 2577938a72dSAlan Cox }; 258*1626aeb8STejun Heo const struct ata_port_info *ppi[] = { &info, NULL }; 2597938a72dSAlan Cox 2607938a72dSAlan Cox cmd640_hardware_init(pdev); 261*1626aeb8STejun Heo return ata_pci_init_one(pdev, ppi); 262b2248dacSAlan Cox } 263b2248dacSAlan Cox 264b2248dacSAlan Cox static int cmd640_reinit_one(struct pci_dev *pdev) 265b2248dacSAlan Cox { 2667938a72dSAlan Cox cmd640_hardware_init(pdev); 2674b22afd7SAndrew Morton #ifdef CONFIG_PM 268b2248dacSAlan Cox return ata_pci_device_resume(pdev); 2694b22afd7SAndrew Morton #else 2704b22afd7SAndrew Morton return 0; 2714b22afd7SAndrew Morton #endif 272b2248dacSAlan Cox } 273b2248dacSAlan Cox 274b2248dacSAlan Cox static const struct pci_device_id cmd640[] = { 275b2248dacSAlan Cox { PCI_VDEVICE(CMD, 0x640), 0 }, 276b2248dacSAlan Cox { }, 277b2248dacSAlan Cox }; 278b2248dacSAlan Cox 279b2248dacSAlan Cox static struct pci_driver cmd640_pci_driver = { 280b2248dacSAlan Cox .name = DRV_NAME, 281b2248dacSAlan Cox .id_table = cmd640, 282b2248dacSAlan Cox .probe = cmd640_init_one, 283b2248dacSAlan Cox .remove = ata_pci_remove_one, 2844b22afd7SAndrew Morton #ifdef CONFIG_PM 285b2248dacSAlan Cox .suspend = ata_pci_device_suspend, 2864b22afd7SAndrew Morton #endif 287b2248dacSAlan Cox .resume = cmd640_reinit_one, 288b2248dacSAlan Cox }; 289b2248dacSAlan Cox 290b2248dacSAlan Cox static int __init cmd640_init(void) 291b2248dacSAlan Cox { 292b2248dacSAlan Cox return pci_register_driver(&cmd640_pci_driver); 293b2248dacSAlan Cox } 294b2248dacSAlan Cox 295b2248dacSAlan Cox static void __exit cmd640_exit(void) 296b2248dacSAlan Cox { 297b2248dacSAlan Cox pci_unregister_driver(&cmd640_pci_driver); 298b2248dacSAlan Cox } 299b2248dacSAlan Cox 300b2248dacSAlan Cox MODULE_AUTHOR("Alan Cox"); 301b2248dacSAlan Cox MODULE_DESCRIPTION("low-level driver for CMD640 PATA controllers"); 302b2248dacSAlan Cox MODULE_LICENSE("GPL"); 303b2248dacSAlan Cox MODULE_DEVICE_TABLE(pci, cmd640); 304b2248dacSAlan Cox MODULE_VERSION(DRV_VERSION); 305b2248dacSAlan Cox 306b2248dacSAlan Cox module_init(cmd640_init); 307b2248dacSAlan Cox module_exit(cmd640_exit); 308