blob: 7116a9e7a8b27ab9e887fdfe503a9ecd2f6963eb [file] [log] [blame]
Alan Cox7e45b0e2006-09-29 18:30:05 +01001/*
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 Garzikf20b16f2006-12-11 11:14:06 -05008
Alan Cox7e45b0e2006-09-29 18:30:05 +01009#include <linux/kernel.h>
10#include <linux/module.h>
Alan Cox7e45b0e2006-09-29 18:30:05 +010011#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 Garzik8bc3fc42007-05-21 20:26:38 -040019#define DRV_VERSION "0.0.3"
Alan Cox7e45b0e2006-09-29 18:30:05 +010020
21#define NR_HOST 4 /* Two winbond controllers, two channels each */
22
23struct winbond_data {
24 unsigned long config;
25 struct platform_device *platform_dev;
26};
27
28static struct ata_host *winbond_host[NR_HOST];
29static struct winbond_data winbond_data[NR_HOST];
30static int nr_winbond_host;
31
32#ifdef MODULE
33static int probe_winbond = 1;
34#else
35static int probe_winbond;
36#endif
37
Milind Arun Choudhary22503632007-04-26 00:19:27 -070038static DEFINE_SPINLOCK(winbond_lock);
Alan Cox7e45b0e2006-09-29 18:30:05 +010039
40static 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
49static 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
62static 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 Garzikf20b16f2006-12-11 11:14:06 -050071
Alan Cox7e45b0e2006-09-29 18:30:05 +010072 /* 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 Garzikf20b16f2006-12-11 11:14:06 -050082
Alan Cox7e45b0e2006-09-29 18:30:05 +010083 /* Load the setup timing */
Jeff Garzikf20b16f2006-12-11 11:14:06 -050084
Alan Cox7e45b0e2006-09-29 18:30:05 +010085 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
95static void winbond_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
96{
Tejun Heo9af5c9c2007-08-06 18:36:22 +090097 struct ata_port *ap = adev->link->ap;
Alan Cox7e45b0e2006-09-29 18:30:05 +010098 int slop = buflen & 3;
99
100 if (ata_id_has_dword_io(adev->id)) {
101 if (write_data)
Tejun Heo0d5ff562007-02-01 15:06:36 +0900102 iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
Alan Cox7e45b0e2006-09-29 18:30:05 +0100103 else
Tejun Heo0d5ff562007-02-01 15:06:36 +0900104 ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
Alan Cox7e45b0e2006-09-29 18:30:05 +0100105
106 if (unlikely(slop)) {
Al Virob50e56d2008-01-12 14:16:14 +0000107 __le32 pad = 0;
Alan Cox7e45b0e2006-09-29 18:30:05 +0100108 if (write_data) {
109 memcpy(&pad, buf + buflen - slop, slop);
Al Virob50e56d2008-01-12 14:16:14 +0000110 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
Alan Cox7e45b0e2006-09-29 18:30:05 +0100111 } else {
Al Virob50e56d2008-01-12 14:16:14 +0000112 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
Alan Cox7e45b0e2006-09-29 18:30:05 +0100113 memcpy(buf + buflen - slop, &pad, slop);
114 }
115 }
116 } else
Tejun Heo0d5ff562007-02-01 15:06:36 +0900117 ata_data_xfer(adev, buf, buflen, write_data);
Alan Cox7e45b0e2006-09-29 18:30:05 +0100118}
119
120static struct scsi_host_template winbond_sht = {
121 .module = THIS_MODULE,
122 .name = DRV_NAME,
123 .ioctl = ata_scsi_ioctl,
124 .queuecommand = ata_scsi_queuecmd,
125 .can_queue = ATA_DEF_QUEUE,
126 .this_id = ATA_SHT_THIS_ID,
127 .sg_tablesize = LIBATA_MAX_PRD,
Alan Cox7e45b0e2006-09-29 18:30:05 +0100128 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
129 .emulated = ATA_SHT_EMULATED,
130 .use_clustering = ATA_SHT_USE_CLUSTERING,
131 .proc_name = DRV_NAME,
132 .dma_boundary = ATA_DMA_BOUNDARY,
133 .slave_configure = ata_scsi_slave_config,
Tejun Heoc972b602006-11-29 12:10:46 +0900134 .slave_destroy = ata_scsi_slave_destroy,
Alan Cox7e45b0e2006-09-29 18:30:05 +0100135 .bios_param = ata_std_bios_param,
136};
137
138static struct ata_port_operations winbond_port_ops = {
Alan Cox7e45b0e2006-09-29 18:30:05 +0100139 .set_piomode = winbond_set_piomode,
140
141 .tf_load = ata_tf_load,
142 .tf_read = ata_tf_read,
143 .check_status = ata_check_status,
144 .exec_command = ata_exec_command,
145 .dev_select = ata_std_dev_select,
146
147 .freeze = ata_bmdma_freeze,
148 .thaw = ata_bmdma_thaw,
149 .error_handler = ata_bmdma_error_handler,
150 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Alan Cox2f5344b2007-03-26 21:43:42 -0800151 .cable_detect = ata_cable_40wire,
Alan Cox7e45b0e2006-09-29 18:30:05 +0100152
153 .qc_prep = ata_qc_prep,
154 .qc_issue = ata_qc_issue_prot,
155
156 .data_xfer = winbond_data_xfer,
157
Alan Cox7e45b0e2006-09-29 18:30:05 +0100158 .irq_clear = ata_bmdma_irq_clear,
Akira Iguchi246ce3b2007-01-26 16:27:58 +0900159 .irq_on = ata_irq_on,
Alan Cox7e45b0e2006-09-29 18:30:05 +0100160
Alan Cox81ad1832007-08-22 22:55:41 +0100161 .port_start = ata_sff_port_start,
Alan Cox7e45b0e2006-09-29 18:30:05 +0100162};
163
164/**
165 * winbond_init_one - attach a winbond interface
166 * @type: Type to display
167 * @io: I/O port start
168 * @irq: interrupt line
169 * @fast: True if on a > 33Mhz VLB
170 *
171 * Register a VLB bus IDE interface. Such interfaces are PIO and we
172 * assume do not support IRQ sharing.
173 */
174
175static __init int winbond_init_one(unsigned long port)
176{
Alan Cox7e45b0e2006-09-29 18:30:05 +0100177 struct platform_device *pdev;
Alan Cox7e45b0e2006-09-29 18:30:05 +0100178 u8 reg;
Tejun Heo5d7288242007-04-17 23:44:08 +0900179 int i, rc;
Alan Cox7e45b0e2006-09-29 18:30:05 +0100180
181 reg = winbond_readcfg(port, 0x81);
182 reg |= 0x80; /* jumpered mode off */
183 winbond_writecfg(port, 0x81, reg);
184 reg = winbond_readcfg(port, 0x83);
185 reg |= 0xF0; /* local control */
186 winbond_writecfg(port, 0x83, reg);
187 reg = winbond_readcfg(port, 0x85);
188 reg |= 0xF0; /* programmable timing */
189 winbond_writecfg(port, 0x85, reg);
190
191 reg = winbond_readcfg(port, 0x81);
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500192
Alan Cox7e45b0e2006-09-29 18:30:05 +0100193 if (!(reg & 0x03)) /* Disabled */
194 return 0;
195
196 for (i = 0; i < 2 ; i ++) {
Tejun Heo0d5ff562007-02-01 15:06:36 +0900197 unsigned long cmd_port = 0x1F0 - (0x80 * i);
Tejun Heocbcdd872007-08-18 13:14:55 +0900198 unsigned long ctl_port = cmd_port + 0x206;
Tejun Heo5d7288242007-04-17 23:44:08 +0900199 struct ata_host *host;
200 struct ata_port *ap;
Tejun Heo0d5ff562007-02-01 15:06:36 +0900201 void __iomem *cmd_addr, *ctl_addr;
Alan Cox7e45b0e2006-09-29 18:30:05 +0100202
Tejun Heo5d7288242007-04-17 23:44:08 +0900203 if (!(reg & (1 << i)))
204 continue;
Alan Cox7e45b0e2006-09-29 18:30:05 +0100205
Tejun Heo5d7288242007-04-17 23:44:08 +0900206 pdev = platform_device_register_simple(DRV_NAME, nr_winbond_host, NULL, 0);
207 if (IS_ERR(pdev))
208 return PTR_ERR(pdev);
Alan Cox7e45b0e2006-09-29 18:30:05 +0100209
Tejun Heo5d7288242007-04-17 23:44:08 +0900210 rc = -ENOMEM;
211 host = ata_host_alloc(&pdev->dev, 1);
212 if (!host)
213 goto err_unregister;
Tejun Heocbcdd872007-08-18 13:14:55 +0900214 ap = host->ports[0];
Tejun Heo0d5ff562007-02-01 15:06:36 +0900215
Tejun Heo5d7288242007-04-17 23:44:08 +0900216 rc = -ENOMEM;
217 cmd_addr = devm_ioport_map(&pdev->dev, cmd_port, 8);
Tejun Heocbcdd872007-08-18 13:14:55 +0900218 ctl_addr = devm_ioport_map(&pdev->dev, ctl_port, 1);
Tejun Heo5d7288242007-04-17 23:44:08 +0900219 if (!cmd_addr || !ctl_addr)
220 goto err_unregister;
Alan Cox7e45b0e2006-09-29 18:30:05 +0100221
Tejun Heocbcdd872007-08-18 13:14:55 +0900222 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", cmd_port, ctl_port);
223
Tejun Heo5d7288242007-04-17 23:44:08 +0900224 ap->ops = &winbond_port_ops;
225 ap->pio_mask = 0x1F;
226 ap->flags |= ATA_FLAG_SLAVE_POSS;
227 ap->ioaddr.cmd_addr = cmd_addr;
228 ap->ioaddr.altstatus_addr = ctl_addr;
229 ap->ioaddr.ctl_addr = ctl_addr;
230 ata_std_ports(&ap->ioaddr);
Alan Cox7e45b0e2006-09-29 18:30:05 +0100231
Tejun Heo5d7288242007-04-17 23:44:08 +0900232 /* hook in a private data structure per channel */
233 host->private_data = &winbond_data[nr_winbond_host];
234 winbond_data[nr_winbond_host].config = port;
235 winbond_data[nr_winbond_host].platform_dev = pdev;
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500236
Tejun Heo5d7288242007-04-17 23:44:08 +0900237 /* activate */
238 rc = ata_host_activate(host, 14 + i, ata_interrupt, 0,
239 &winbond_sht);
240 if (rc)
241 goto err_unregister;
Alan Cox7e45b0e2006-09-29 18:30:05 +0100242
Tejun Heo5d7288242007-04-17 23:44:08 +0900243 winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev);
Alan Cox7e45b0e2006-09-29 18:30:05 +0100244 }
245
246 return 0;
Tejun Heo5d7288242007-04-17 23:44:08 +0900247
248 err_unregister:
249 platform_device_unregister(pdev);
250 return rc;
Alan Cox7e45b0e2006-09-29 18:30:05 +0100251}
252
253/**
254 * winbond_init - attach winbond interfaces
255 *
256 * Attach winbond IDE interfaces by scanning the ports it may occupy.
257 */
258
259static __init int winbond_init(void)
260{
261 static const unsigned long config[2] = { 0x130, 0x1B0 };
262
263 int ct = 0;
264 int i;
Jeff Garzikf20b16f2006-12-11 11:14:06 -0500265
Alan Cox7e45b0e2006-09-29 18:30:05 +0100266 if (probe_winbond == 0)
267 return -ENODEV;
268
269 /*
270 * Check both base addresses
271 */
272
273 for (i = 0; i < 2; i++) {
274 if (probe_winbond & (1<<i)) {
275 int ret = 0;
276 unsigned long port = config[i];
277
278 if (request_region(port, 2, "pata_winbond")) {
279 ret = winbond_init_one(port);
Jeff Garzikb4479162007-10-25 20:47:30 -0400280 if (ret <= 0)
Alan Cox7e45b0e2006-09-29 18:30:05 +0100281 release_region(port, 2);
282 else ct+= ret;
283 }
284 }
285 }
286 if (ct != 0)
287 return 0;
288 return -ENODEV;
289}
290
291static __exit void winbond_exit(void)
292{
293 int i;
294
295 for (i = 0; i < nr_winbond_host; i++) {
Tejun Heo24dc5f32007-01-20 16:00:28 +0900296 ata_host_detach(winbond_host[i]);
Alan Cox7e45b0e2006-09-29 18:30:05 +0100297 release_region(winbond_data[i].config, 2);
298 platform_device_unregister(winbond_data[i].platform_dev);
299 }
300}
301
302MODULE_AUTHOR("Alan Cox");
303MODULE_DESCRIPTION("low-level driver for Winbond VL ATA");
304MODULE_LICENSE("GPL");
305MODULE_VERSION(DRV_VERSION);
306
307module_init(winbond_init);
308module_exit(winbond_exit);
309
310module_param(probe_winbond, int, 0);
311