Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 1 | /* |
| 2 | * pata_winbond.c - Winbond VLB ATA controllers |
Alan Cox | ab77163 | 2008-10-27 15:09:10 +0000 | [diff] [blame] | 3 | * (C) 2006 Red Hat |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 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 | |
Harvey Harrison | 07633b5 | 2008-05-14 16:17:00 -0700 | [diff] [blame] | 78 | active = (clamp_val(t.active, 3, 17) - 1) & 0x0F; |
| 79 | recovery = (clamp_val(t.recover, 1, 15) + 1) & 0x0F; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 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 */ |
Harvey Harrison | 07633b5 | 2008-05-14 16:17:00 -0700 | [diff] [blame] | 90 | reg |= (clamp_val(t.setup, 0, 3) << 6); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 91 | winbond_writecfg(winbond->config, timing + 1, reg); |
| 92 | } |
| 93 | |
| 94 | |
Andrew Morton | 045eeb4 | 2008-01-10 14:33:04 -0800 | [diff] [blame] | 95 | static unsigned int winbond_data_xfer(struct ata_device *dev, |
| 96 | unsigned char *buf, unsigned int buflen, int rw) |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 97 | { |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 98 | struct ata_port *ap = dev->link->ap; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 99 | int slop = buflen & 3; |
| 100 | |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 101 | if (ata_id_has_dword_io(dev->id)) { |
| 102 | if (rw == READ) |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 103 | ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2); |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 104 | else |
| 105 | iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 106 | |
| 107 | if (unlikely(slop)) { |
Harvey Harrison | 6ad6740 | 2008-06-18 17:16:43 -0700 | [diff] [blame] | 108 | __le32 pad; |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 109 | if (rw == READ) { |
Al Viro | b50e56d | 2008-01-12 14:16:14 +0000 | [diff] [blame] | 110 | pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr)); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 111 | memcpy(buf + buflen - slop, &pad, slop); |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 112 | } else { |
| 113 | memcpy(&pad, buf + buflen - slop, slop); |
| 114 | iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 115 | } |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 116 | buflen += 4 - slop; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 117 | } |
| 118 | } else |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 119 | buflen = ata_sff_data_xfer(dev, buf, buflen, rw); |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 120 | |
| 121 | return buflen; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static struct scsi_host_template winbond_sht = { |
Tejun Heo | 68d1d07 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 125 | ATA_PIO_SHT(DRV_NAME), |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static struct ata_port_operations winbond_port_ops = { |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 129 | .inherits = &ata_sff_port_ops, |
Tejun Heo | 5682ed3 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 130 | .sff_data_xfer = winbond_data_xfer, |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 131 | .cable_detect = ata_cable_40wire, |
| 132 | .set_piomode = winbond_set_piomode, |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | /** |
| 136 | * winbond_init_one - attach a winbond interface |
| 137 | * @type: Type to display |
| 138 | * @io: I/O port start |
| 139 | * @irq: interrupt line |
| 140 | * @fast: True if on a > 33Mhz VLB |
| 141 | * |
| 142 | * Register a VLB bus IDE interface. Such interfaces are PIO and we |
| 143 | * assume do not support IRQ sharing. |
| 144 | */ |
| 145 | |
| 146 | static __init int winbond_init_one(unsigned long port) |
| 147 | { |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 148 | struct platform_device *pdev; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 149 | u8 reg; |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 150 | int i, rc; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 151 | |
| 152 | reg = winbond_readcfg(port, 0x81); |
| 153 | reg |= 0x80; /* jumpered mode off */ |
| 154 | winbond_writecfg(port, 0x81, reg); |
| 155 | reg = winbond_readcfg(port, 0x83); |
| 156 | reg |= 0xF0; /* local control */ |
| 157 | winbond_writecfg(port, 0x83, reg); |
| 158 | reg = winbond_readcfg(port, 0x85); |
| 159 | reg |= 0xF0; /* programmable timing */ |
| 160 | winbond_writecfg(port, 0x85, reg); |
| 161 | |
| 162 | reg = winbond_readcfg(port, 0x81); |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 163 | |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 164 | if (!(reg & 0x03)) /* Disabled */ |
Alan Cox | 3e9b902 | 2008-01-19 15:53:55 +0000 | [diff] [blame] | 165 | return -ENODEV; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 166 | |
| 167 | for (i = 0; i < 2 ; i ++) { |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 168 | unsigned long cmd_port = 0x1F0 - (0x80 * i); |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 169 | unsigned long ctl_port = cmd_port + 0x206; |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 170 | struct ata_host *host; |
| 171 | struct ata_port *ap; |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 172 | void __iomem *cmd_addr, *ctl_addr; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 173 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 174 | if (!(reg & (1 << i))) |
| 175 | continue; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 176 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 177 | pdev = platform_device_register_simple(DRV_NAME, nr_winbond_host, NULL, 0); |
| 178 | if (IS_ERR(pdev)) |
| 179 | return PTR_ERR(pdev); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 180 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 181 | rc = -ENOMEM; |
| 182 | host = ata_host_alloc(&pdev->dev, 1); |
| 183 | if (!host) |
| 184 | goto err_unregister; |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 185 | ap = host->ports[0]; |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 186 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 187 | rc = -ENOMEM; |
| 188 | cmd_addr = devm_ioport_map(&pdev->dev, cmd_port, 8); |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 189 | ctl_addr = devm_ioport_map(&pdev->dev, ctl_port, 1); |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 190 | if (!cmd_addr || !ctl_addr) |
| 191 | goto err_unregister; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 192 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 193 | ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", cmd_port, ctl_port); |
| 194 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 195 | ap->ops = &winbond_port_ops; |
Erik Inge Bolsø | 14bdef9 | 2009-03-14 21:38:24 +0100 | [diff] [blame] | 196 | ap->pio_mask = ATA_PIO4; |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 197 | ap->flags |= ATA_FLAG_SLAVE_POSS; |
| 198 | ap->ioaddr.cmd_addr = cmd_addr; |
| 199 | ap->ioaddr.altstatus_addr = ctl_addr; |
| 200 | ap->ioaddr.ctl_addr = ctl_addr; |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 201 | ata_sff_std_ports(&ap->ioaddr); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 202 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 203 | /* hook in a private data structure per channel */ |
| 204 | host->private_data = &winbond_data[nr_winbond_host]; |
| 205 | winbond_data[nr_winbond_host].config = port; |
| 206 | winbond_data[nr_winbond_host].platform_dev = pdev; |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 207 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 208 | /* activate */ |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 209 | rc = ata_host_activate(host, 14 + i, ata_sff_interrupt, 0, |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 210 | &winbond_sht); |
| 211 | if (rc) |
| 212 | goto err_unregister; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 213 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 214 | winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return 0; |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 218 | |
| 219 | err_unregister: |
| 220 | platform_device_unregister(pdev); |
| 221 | return rc; |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * winbond_init - attach winbond interfaces |
| 226 | * |
| 227 | * Attach winbond IDE interfaces by scanning the ports it may occupy. |
| 228 | */ |
| 229 | |
| 230 | static __init int winbond_init(void) |
| 231 | { |
| 232 | static const unsigned long config[2] = { 0x130, 0x1B0 }; |
| 233 | |
| 234 | int ct = 0; |
| 235 | int i; |
Jeff Garzik | f20b16f | 2006-12-11 11:14:06 -0500 | [diff] [blame] | 236 | |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 237 | if (probe_winbond == 0) |
| 238 | return -ENODEV; |
| 239 | |
| 240 | /* |
| 241 | * Check both base addresses |
| 242 | */ |
| 243 | |
| 244 | for (i = 0; i < 2; i++) { |
| 245 | if (probe_winbond & (1<<i)) { |
| 246 | int ret = 0; |
| 247 | unsigned long port = config[i]; |
| 248 | |
| 249 | if (request_region(port, 2, "pata_winbond")) { |
| 250 | ret = winbond_init_one(port); |
Jeff Garzik | b447916 | 2007-10-25 20:47:30 -0400 | [diff] [blame] | 251 | if (ret <= 0) |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 252 | release_region(port, 2); |
| 253 | else ct+= ret; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | if (ct != 0) |
| 258 | return 0; |
| 259 | return -ENODEV; |
| 260 | } |
| 261 | |
| 262 | static __exit void winbond_exit(void) |
| 263 | { |
| 264 | int i; |
| 265 | |
| 266 | for (i = 0; i < nr_winbond_host; i++) { |
Tejun Heo | 24dc5f3 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 267 | ata_host_detach(winbond_host[i]); |
Alan Cox | 7e45b0e | 2006-09-29 18:30:05 +0100 | [diff] [blame] | 268 | release_region(winbond_data[i].config, 2); |
| 269 | platform_device_unregister(winbond_data[i].platform_dev); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | MODULE_AUTHOR("Alan Cox"); |
| 274 | MODULE_DESCRIPTION("low-level driver for Winbond VL ATA"); |
| 275 | MODULE_LICENSE("GPL"); |
| 276 | MODULE_VERSION(DRV_VERSION); |
| 277 | |
| 278 | module_init(winbond_init); |
| 279 | module_exit(winbond_exit); |
| 280 | |
| 281 | module_param(probe_winbond, int, 0); |
| 282 | |