Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * pata_winbond.c - Winbond VLB ATA controllers |
| 3 | * (C) 2006 Red Hat <alan@redhat.com> |
| 4 | * |
| 5 | * Support for the Winbond 83759A when operating in advanced mode. |
| 6 | * Multichip mode is not currently supported. |
| 7 | */ |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 8 | |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/blkdev.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <scsi/scsi_host.h> |
| 15 | #include <linux/libata.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | |
| 18 | #define DRV_NAME "pata_winbond" |
Jeff Garzik | 8bc3fc4 | 2007-05-21 20:26:38 -0400 | [diff] [blame] | 19 | #define DRV_VERSION "0.0.3" |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 20 | |
| 21 | #define NR_HOST 4 /* Two winbond controllers, two channels each */ |
| 22 | |
| 23 | struct winbond_data { |
| 24 | unsigned long config; |
| 25 | struct platform_device *platform_dev; |
| 26 | }; |
| 27 | |
| 28 | static struct ata_host *winbond_host[NR_HOST]; |
| 29 | static struct winbond_data winbond_data[NR_HOST]; |
| 30 | static int nr_winbond_host; |
| 31 | |
| 32 | #ifdef MODULE |
| 33 | static int probe_winbond = 1; |
| 34 | #else |
| 35 | static int probe_winbond; |
| 36 | #endif |
| 37 | |
Milind Arun Choudhary | 2250363 | 2007-04-26 00:19:27 -0700 | [diff] [blame] | 38 | static DEFINE_SPINLOCK(winbond_lock); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 39 | |
| 40 | static void winbond_writecfg(unsigned long port, u8 reg, u8 val) |
| 41 | { |
| 42 | unsigned long flags; |
| 43 | spin_lock_irqsave(&winbond_lock, flags); |
| 44 | outb(reg, port + 0x01); |
| 45 | outb(val, port + 0x02); |
| 46 | spin_unlock_irqrestore(&winbond_lock, flags); |
| 47 | } |
| 48 | |
| 49 | static u8 winbond_readcfg(unsigned long port, u8 reg) |
| 50 | { |
| 51 | u8 val; |
| 52 | |
| 53 | unsigned long flags; |
| 54 | spin_lock_irqsave(&winbond_lock, flags); |
| 55 | outb(reg, port + 0x01); |
| 56 | val = inb(port + 0x02); |
| 57 | spin_unlock_irqrestore(&winbond_lock, flags); |
| 58 | |
| 59 | return val; |
| 60 | } |
| 61 | |
| 62 | static void winbond_set_piomode(struct ata_port *ap, struct ata_device *adev) |
| 63 | { |
| 64 | struct ata_timing t; |
| 65 | struct winbond_data *winbond = ap->host->private_data; |
| 66 | int active, recovery; |
| 67 | u8 reg; |
| 68 | int timing = 0x88 + (ap->port_no * 4) + (adev->devno * 2); |
| 69 | |
| 70 | reg = winbond_readcfg(winbond->config, 0x81); |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 71 | |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 72 | /* Get the timing data in cycles */ |
| 73 | if (reg & 0x40) /* Fast VLB bus, assume 50MHz */ |
| 74 | ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000); |
| 75 | else |
| 76 | ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000); |
| 77 | |
| 78 | active = (FIT(t.active, 3, 17) - 1) & 0x0F; |
| 79 | recovery = (FIT(t.recover, 1, 15) + 1) & 0x0F; |
| 80 | timing = (active << 4) | recovery; |
| 81 | winbond_writecfg(winbond->config, timing, reg); |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 82 | |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 83 | /* Load the setup timing */ |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 84 | |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 85 | reg = 0x35; |
| 86 | if (adev->class != ATA_DEV_ATA) |
| 87 | reg |= 0x08; /* FIFO off */ |
| 88 | if (!ata_pio_need_iordy(adev)) |
| 89 | reg |= 0x02; /* IORDY off */ |
| 90 | reg |= (FIT(t.setup, 0, 3) << 6); |
| 91 | winbond_writecfg(winbond->config, timing + 1, reg); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static void winbond_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data) |
| 96 | { |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 97 | struct ata_port *ap = adev->link->ap; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 98 | int slop = buflen & 3; |
| 99 | |
| 100 | if (ata_id_has_dword_io(adev->id)) { |
| 101 | if (write_data) |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 102 | iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 103 | else |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 104 | ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 105 | |
| 106 | if (unlikely(slop)) { |
| 107 | u32 pad; |
| 108 | if (write_data) { |
| 109 | memcpy(&pad, buf + buflen - slop, slop); |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 110 | pad = le32_to_cpu(pad); |
| 111 | iowrite32(pad, ap->ioaddr.data_addr); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 112 | } else { |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 113 | pad = ioread32(ap->ioaddr.data_addr); |
| 114 | pad = cpu_to_le16(pad); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 115 | memcpy(buf + buflen - slop, &pad, slop); |
| 116 | } |
| 117 | } |
| 118 | } else |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 119 | ata_data_xfer(adev, buf, buflen, write_data); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static struct scsi_host_template winbond_sht = { |
| 123 | .module = THIS_MODULE, |
| 124 | .name = DRV_NAME, |
| 125 | .ioctl = ata_scsi_ioctl, |
| 126 | .queuecommand = ata_scsi_queuecmd, |
| 127 | .can_queue = ATA_DEF_QUEUE, |
| 128 | .this_id = ATA_SHT_THIS_ID, |
| 129 | .sg_tablesize = LIBATA_MAX_PRD, |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 130 | .cmd_per_lun = ATA_SHT_CMD_PER_LUN, |
| 131 | .emulated = ATA_SHT_EMULATED, |
| 132 | .use_clustering = ATA_SHT_USE_CLUSTERING, |
| 133 | .proc_name = DRV_NAME, |
| 134 | .dma_boundary = ATA_DMA_BOUNDARY, |
| 135 | .slave_configure = ata_scsi_slave_config, |
Tejun Heo | c972b60 | 2006-11-29 12:10:46 +0900 | [diff] [blame] | 136 | .slave_destroy = ata_scsi_slave_destroy, |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 137 | .bios_param = ata_std_bios_param, |
| 138 | }; |
| 139 | |
| 140 | static struct ata_port_operations winbond_port_ops = { |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 141 | .set_piomode = winbond_set_piomode, |
| 142 | |
| 143 | .tf_load = ata_tf_load, |
| 144 | .tf_read = ata_tf_read, |
| 145 | .check_status = ata_check_status, |
| 146 | .exec_command = ata_exec_command, |
| 147 | .dev_select = ata_std_dev_select, |
| 148 | |
| 149 | .freeze = ata_bmdma_freeze, |
| 150 | .thaw = ata_bmdma_thaw, |
| 151 | .error_handler = ata_bmdma_error_handler, |
| 152 | .post_internal_cmd = ata_bmdma_post_internal_cmd, |
Alan Cox | 2f5344b | 2007-03-26 21:43:42 -0800 | [diff] [blame] | 153 | .cable_detect = ata_cable_40wire, |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 154 | |
| 155 | .qc_prep = ata_qc_prep, |
| 156 | .qc_issue = ata_qc_issue_prot, |
| 157 | |
| 158 | .data_xfer = winbond_data_xfer, |
| 159 | |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 160 | .irq_clear = ata_bmdma_irq_clear, |
Akira Iguchi | 246ce3b | 2007-01-26 16:27:58 +0900 | [diff] [blame] | 161 | .irq_on = ata_irq_on, |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 162 | |
| 163 | .port_start = ata_port_start, |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /** |
| 167 | * winbond_init_one - attach a winbond interface |
| 168 | * @type: Type to display |
| 169 | * @io: I/O port start |
| 170 | * @irq: interrupt line |
| 171 | * @fast: True if on a > 33Mhz VLB |
| 172 | * |
| 173 | * Register a VLB bus IDE interface. Such interfaces are PIO and we |
| 174 | * assume do not support IRQ sharing. |
| 175 | */ |
| 176 | |
| 177 | static __init int winbond_init_one(unsigned long port) |
| 178 | { |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 179 | struct platform_device *pdev; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 180 | u8 reg; |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 181 | int i, rc; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 182 | |
| 183 | reg = winbond_readcfg(port, 0x81); |
| 184 | reg |= 0x80; /* jumpered mode off */ |
| 185 | winbond_writecfg(port, 0x81, reg); |
| 186 | reg = winbond_readcfg(port, 0x83); |
| 187 | reg |= 0xF0; /* local control */ |
| 188 | winbond_writecfg(port, 0x83, reg); |
| 189 | reg = winbond_readcfg(port, 0x85); |
| 190 | reg |= 0xF0; /* programmable timing */ |
| 191 | winbond_writecfg(port, 0x85, reg); |
| 192 | |
| 193 | reg = winbond_readcfg(port, 0x81); |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 194 | |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 195 | if (!(reg & 0x03)) /* Disabled */ |
| 196 | return 0; |
| 197 | |
| 198 | for (i = 0; i < 2 ; i ++) { |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 199 | unsigned long cmd_port = 0x1F0 - (0x80 * i); |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 200 | struct ata_host *host; |
| 201 | struct ata_port *ap; |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 202 | void __iomem *cmd_addr, *ctl_addr; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 203 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 204 | if (!(reg & (1 << i))) |
| 205 | continue; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 206 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 207 | pdev = platform_device_register_simple(DRV_NAME, nr_winbond_host, NULL, 0); |
| 208 | if (IS_ERR(pdev)) |
| 209 | return PTR_ERR(pdev); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 210 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 211 | rc = -ENOMEM; |
| 212 | host = ata_host_alloc(&pdev->dev, 1); |
| 213 | if (!host) |
| 214 | goto err_unregister; |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 215 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 216 | rc = -ENOMEM; |
| 217 | cmd_addr = devm_ioport_map(&pdev->dev, cmd_port, 8); |
| 218 | ctl_addr = devm_ioport_map(&pdev->dev, cmd_port + 0x0206, 1); |
| 219 | if (!cmd_addr || !ctl_addr) |
| 220 | goto err_unregister; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 221 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 222 | ap = host->ports[0]; |
| 223 | ap->ops = &winbond_port_ops; |
| 224 | ap->pio_mask = 0x1F; |
| 225 | ap->flags |= ATA_FLAG_SLAVE_POSS; |
| 226 | ap->ioaddr.cmd_addr = cmd_addr; |
| 227 | ap->ioaddr.altstatus_addr = ctl_addr; |
| 228 | ap->ioaddr.ctl_addr = ctl_addr; |
| 229 | ata_std_ports(&ap->ioaddr); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 230 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 231 | /* hook in a private data structure per channel */ |
| 232 | host->private_data = &winbond_data[nr_winbond_host]; |
| 233 | winbond_data[nr_winbond_host].config = port; |
| 234 | winbond_data[nr_winbond_host].platform_dev = pdev; |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 235 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 236 | /* activate */ |
| 237 | rc = ata_host_activate(host, 14 + i, ata_interrupt, 0, |
| 238 | &winbond_sht); |
| 239 | if (rc) |
| 240 | goto err_unregister; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 241 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 242 | winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return 0; |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 246 | |
| 247 | err_unregister: |
| 248 | platform_device_unregister(pdev); |
| 249 | return rc; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | /** |
| 253 | * winbond_init - attach winbond interfaces |
| 254 | * |
| 255 | * Attach winbond IDE interfaces by scanning the ports it may occupy. |
| 256 | */ |
| 257 | |
| 258 | static __init int winbond_init(void) |
| 259 | { |
| 260 | static const unsigned long config[2] = { 0x130, 0x1B0 }; |
| 261 | |
| 262 | int ct = 0; |
| 263 | int i; |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 264 | |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 265 | if (probe_winbond == 0) |
| 266 | return -ENODEV; |
| 267 | |
| 268 | /* |
| 269 | * Check both base addresses |
| 270 | */ |
| 271 | |
| 272 | for (i = 0; i < 2; i++) { |
| 273 | if (probe_winbond & (1<<i)) { |
| 274 | int ret = 0; |
| 275 | unsigned long port = config[i]; |
| 276 | |
| 277 | if (request_region(port, 2, "pata_winbond")) { |
| 278 | ret = winbond_init_one(port); |
| 279 | if(ret <= 0) |
| 280 | release_region(port, 2); |
| 281 | else ct+= ret; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | if (ct != 0) |
| 286 | return 0; |
| 287 | return -ENODEV; |
| 288 | } |
| 289 | |
| 290 | static __exit void winbond_exit(void) |
| 291 | { |
| 292 | int i; |
| 293 | |
| 294 | for (i = 0; i < nr_winbond_host; i++) { |
Tejun Heo | 24dc5f3 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 295 | ata_host_detach(winbond_host[i]); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 296 | release_region(winbond_data[i].config, 2); |
| 297 | platform_device_unregister(winbond_data[i].platform_dev); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | MODULE_AUTHOR("Alan Cox"); |
| 302 | MODULE_DESCRIPTION("low-level driver for Winbond VL ATA"); |
| 303 | MODULE_LICENSE("GPL"); |
| 304 | MODULE_VERSION(DRV_VERSION); |
| 305 | |
| 306 | module_init(winbond_init); |
| 307 | module_exit(winbond_exit); |
| 308 | |
| 309 | module_param(probe_winbond, int, 0); |
| 310 | |