libata: implement and use SHT initializers
[firefly-linux-kernel-4.4.55.git] / drivers / ata / pata_winbond.c
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  */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
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"
19 #define DRV_VERSION "0.0.3"
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
38 static DEFINE_SPINLOCK(winbond_lock);
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);
71
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);
82
83         /* Load the setup timing */
84
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 unsigned int winbond_data_xfer(struct ata_device *dev,
96                         unsigned char *buf, unsigned int buflen, int rw)
97 {
98         struct ata_port *ap = dev->link->ap;
99         int slop = buflen & 3;
100
101         if (ata_id_has_dword_io(dev->id)) {
102                 if (rw == READ)
103                         ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
104                 else
105                         iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
106
107                 if (unlikely(slop)) {
108                         u32 pad;
109                         if (rw == READ) {
110                                 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
111                                 memcpy(buf + buflen - slop, &pad, slop);
112                         } else {
113                                 memcpy(&pad, buf + buflen - slop, slop);
114                                 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
115                         }
116                         buflen += 4 - slop;
117                 }
118         } else
119                 buflen = ata_data_xfer(dev, buf, buflen, rw);
120
121         return buflen;
122 }
123
124 static struct scsi_host_template winbond_sht = {
125         ATA_PIO_SHT(DRV_NAME),
126 };
127
128 static struct ata_port_operations winbond_port_ops = {
129         .set_piomode    = winbond_set_piomode,
130
131         .tf_load        = ata_tf_load,
132         .tf_read        = ata_tf_read,
133         .check_status   = ata_check_status,
134         .exec_command   = ata_exec_command,
135         .dev_select     = ata_std_dev_select,
136
137         .freeze         = ata_bmdma_freeze,
138         .thaw           = ata_bmdma_thaw,
139         .error_handler  = ata_bmdma_error_handler,
140         .post_internal_cmd = ata_bmdma_post_internal_cmd,
141         .cable_detect   = ata_cable_40wire,
142
143         .qc_prep        = ata_qc_prep,
144         .qc_issue       = ata_qc_issue_prot,
145
146         .data_xfer      = winbond_data_xfer,
147
148         .irq_clear      = ata_noop_irq_clear,
149         .irq_on         = ata_irq_on,
150
151         .port_start     = ata_sff_port_start,
152 };
153
154 /**
155  *      winbond_init_one                -       attach a winbond interface
156  *      @type: Type to display
157  *      @io: I/O port start
158  *      @irq: interrupt line
159  *      @fast: True if on a > 33Mhz VLB
160  *
161  *      Register a VLB bus IDE interface. Such interfaces are PIO and we
162  *      assume do not support IRQ sharing.
163  */
164
165 static __init int winbond_init_one(unsigned long port)
166 {
167         struct platform_device *pdev;
168         u8 reg;
169         int i, rc;
170
171         reg = winbond_readcfg(port, 0x81);
172         reg |= 0x80;    /* jumpered mode off */
173         winbond_writecfg(port, 0x81, reg);
174         reg = winbond_readcfg(port, 0x83);
175         reg |= 0xF0;    /* local control */
176         winbond_writecfg(port, 0x83, reg);
177         reg = winbond_readcfg(port, 0x85);
178         reg |= 0xF0;    /* programmable timing */
179         winbond_writecfg(port, 0x85, reg);
180
181         reg = winbond_readcfg(port, 0x81);
182
183         if (!(reg & 0x03))              /* Disabled */
184                 return -ENODEV;
185
186         for (i = 0; i < 2 ; i ++) {
187                 unsigned long cmd_port = 0x1F0 - (0x80 * i);
188                 unsigned long ctl_port = cmd_port + 0x206;
189                 struct ata_host *host;
190                 struct ata_port *ap;
191                 void __iomem *cmd_addr, *ctl_addr;
192
193                 if (!(reg & (1 << i)))
194                         continue;
195
196                 pdev = platform_device_register_simple(DRV_NAME, nr_winbond_host, NULL, 0);
197                 if (IS_ERR(pdev))
198                         return PTR_ERR(pdev);
199
200                 rc = -ENOMEM;
201                 host = ata_host_alloc(&pdev->dev, 1);
202                 if (!host)
203                         goto err_unregister;
204                 ap = host->ports[0];
205
206                 rc = -ENOMEM;
207                 cmd_addr = devm_ioport_map(&pdev->dev, cmd_port, 8);
208                 ctl_addr = devm_ioport_map(&pdev->dev, ctl_port, 1);
209                 if (!cmd_addr || !ctl_addr)
210                         goto err_unregister;
211
212                 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", cmd_port, ctl_port);
213
214                 ap->ops = &winbond_port_ops;
215                 ap->pio_mask = 0x1F;
216                 ap->flags |= ATA_FLAG_SLAVE_POSS;
217                 ap->ioaddr.cmd_addr = cmd_addr;
218                 ap->ioaddr.altstatus_addr = ctl_addr;
219                 ap->ioaddr.ctl_addr = ctl_addr;
220                 ata_std_ports(&ap->ioaddr);
221
222                 /* hook in a private data structure per channel */
223                 host->private_data = &winbond_data[nr_winbond_host];
224                 winbond_data[nr_winbond_host].config = port;
225                 winbond_data[nr_winbond_host].platform_dev = pdev;
226
227                 /* activate */
228                 rc = ata_host_activate(host, 14 + i, ata_interrupt, 0,
229                                        &winbond_sht);
230                 if (rc)
231                         goto err_unregister;
232
233                 winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev);
234         }
235
236         return 0;
237
238  err_unregister:
239         platform_device_unregister(pdev);
240         return rc;
241 }
242
243 /**
244  *      winbond_init            -       attach winbond interfaces
245  *
246  *      Attach winbond IDE interfaces by scanning the ports it may occupy.
247  */
248
249 static __init int winbond_init(void)
250 {
251         static const unsigned long config[2] = { 0x130, 0x1B0 };
252
253         int ct = 0;
254         int i;
255
256         if (probe_winbond == 0)
257                 return -ENODEV;
258
259         /*
260          *      Check both base addresses
261          */
262
263         for (i = 0; i < 2; i++) {
264                 if (probe_winbond & (1<<i)) {
265                         int ret = 0;
266                         unsigned long port = config[i];
267
268                         if (request_region(port, 2, "pata_winbond")) {
269                                 ret = winbond_init_one(port);
270                                 if (ret <= 0)
271                                         release_region(port, 2);
272                                 else ct+= ret;
273                         }
274                 }
275         }
276         if (ct != 0)
277                 return 0;
278         return -ENODEV;
279 }
280
281 static __exit void winbond_exit(void)
282 {
283         int i;
284
285         for (i = 0; i < nr_winbond_host; i++) {
286                 ata_host_detach(winbond_host[i]);
287                 release_region(winbond_data[i].config, 2);
288                 platform_device_unregister(winbond_data[i].platform_dev);
289         }
290 }
291
292 MODULE_AUTHOR("Alan Cox");
293 MODULE_DESCRIPTION("low-level driver for Winbond VL ATA");
294 MODULE_LICENSE("GPL");
295 MODULE_VERSION(DRV_VERSION);
296
297 module_init(winbond_init);
298 module_exit(winbond_exit);
299
300 module_param(probe_winbond, int, 0);
301