libata: separate out ata_host_alloc() and ata_host_register()
[powerpc.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.2"
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 spinlock_t winbond_lock = SPIN_LOCK_UNLOCKED;
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 void winbond_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
96 {
97         struct ata_port *ap = adev->ap;
98         int slop = buflen & 3;
99
100         if (ata_id_has_dword_io(adev->id)) {
101                 if (write_data)
102                         iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
103                 else
104                         ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
105
106                 if (unlikely(slop)) {
107                         u32 pad;
108                         if (write_data) {
109                                 memcpy(&pad, buf + buflen - slop, slop);
110                                 pad = le32_to_cpu(pad);
111                                 iowrite32(pad, ap->ioaddr.data_addr);
112                         } else {
113                                 pad = ioread32(ap->ioaddr.data_addr);
114                                 pad = cpu_to_le16(pad);
115                                 memcpy(buf + buflen - slop, &pad, slop);
116                         }
117                 }
118         } else
119                 ata_data_xfer(adev, buf, buflen, write_data);
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,
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,
136         .slave_destroy          = ata_scsi_slave_destroy,
137         .bios_param             = ata_std_bios_param,
138 };
139
140 static struct ata_port_operations winbond_port_ops = {
141         .port_disable   = ata_port_disable,
142         .set_piomode    = winbond_set_piomode,
143
144         .tf_load        = ata_tf_load,
145         .tf_read        = ata_tf_read,
146         .check_status   = ata_check_status,
147         .exec_command   = ata_exec_command,
148         .dev_select     = ata_std_dev_select,
149
150         .freeze         = ata_bmdma_freeze,
151         .thaw           = ata_bmdma_thaw,
152         .error_handler  = ata_bmdma_error_handler,
153         .post_internal_cmd = ata_bmdma_post_internal_cmd,
154         .cable_detect   = ata_cable_40wire,
155
156         .qc_prep        = ata_qc_prep,
157         .qc_issue       = ata_qc_issue_prot,
158
159         .data_xfer      = winbond_data_xfer,
160
161         .irq_handler    = ata_interrupt,
162         .irq_clear      = ata_bmdma_irq_clear,
163         .irq_on         = ata_irq_on,
164         .irq_ack        = ata_irq_ack,
165
166         .port_start     = ata_port_start,
167 };
168
169 /**
170  *      winbond_init_one                -       attach a winbond interface
171  *      @type: Type to display
172  *      @io: I/O port start
173  *      @irq: interrupt line
174  *      @fast: True if on a > 33Mhz VLB
175  *
176  *      Register a VLB bus IDE interface. Such interfaces are PIO and we
177  *      assume do not support IRQ sharing.
178  */
179
180 static __init int winbond_init_one(unsigned long port)
181 {
182         struct ata_probe_ent ae;
183         struct platform_device *pdev;
184         int ret;
185         u8 reg;
186         int i;
187
188         reg = winbond_readcfg(port, 0x81);
189         reg |= 0x80;    /* jumpered mode off */
190         winbond_writecfg(port, 0x81, reg);
191         reg = winbond_readcfg(port, 0x83);
192         reg |= 0xF0;    /* local control */
193         winbond_writecfg(port, 0x83, reg);
194         reg = winbond_readcfg(port, 0x85);
195         reg |= 0xF0;    /* programmable timing */
196         winbond_writecfg(port, 0x85, reg);
197
198         reg = winbond_readcfg(port, 0x81);
199
200         if (!(reg & 0x03))              /* Disabled */
201                 return 0;
202
203         for (i = 0; i < 2 ; i ++) {
204                 unsigned long cmd_port = 0x1F0 - (0x80 * i);
205                 void __iomem *cmd_addr, *ctl_addr;
206
207                 if (reg & (1 << i)) {
208                         /*
209                          *      Fill in a probe structure first of all
210                          */
211
212                         pdev = platform_device_register_simple(DRV_NAME, nr_winbond_host, NULL, 0);
213                         if (IS_ERR(pdev))
214                                 return PTR_ERR(pdev);
215
216                         cmd_addr = devm_ioport_map(&pdev->dev, cmd_port, 8);
217                         ctl_addr = devm_ioport_map(&pdev->dev, cmd_port + 0x0206, 1);
218                         if (!cmd_addr || !ctl_addr) {
219                                 platform_device_unregister(pdev);
220                                 return -ENOMEM;
221                         }
222
223                         memset(&ae, 0, sizeof(struct ata_probe_ent));
224                         INIT_LIST_HEAD(&ae.node);
225                         ae.dev = &pdev->dev;
226
227                         ae.port_ops = &winbond_port_ops;
228                         ae.pio_mask = 0x1F;
229
230                         ae.sht = &winbond_sht;
231
232                         ae.n_ports = 1;
233                         ae.irq = 14 + i;
234                         ae.irq_flags = 0;
235                         ae.port_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST;
236                         ae.port[0].cmd_addr = cmd_addr;
237                         ae.port[0].altstatus_addr = ctl_addr;
238                         ae.port[0].ctl_addr = ctl_addr;
239                         ata_std_ports(&ae.port[0]);
240                         /*
241                          *      Hook in a private data structure per channel
242                          */
243                         ae.private_data = &winbond_data[nr_winbond_host];
244                         winbond_data[nr_winbond_host].config = port;
245                         winbond_data[nr_winbond_host].platform_dev = pdev;
246
247                         ret = ata_device_add(&ae);
248                         if (ret == 0) {
249                                 platform_device_unregister(pdev);
250                                 return -ENODEV;
251                         }
252                         winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev);
253                 }
254         }
255
256         return 0;
257 }
258
259 /**
260  *      winbond_init            -       attach winbond interfaces
261  *
262  *      Attach winbond IDE interfaces by scanning the ports it may occupy.
263  */
264
265 static __init int winbond_init(void)
266 {
267         static const unsigned long config[2] = { 0x130, 0x1B0 };
268
269         int ct = 0;
270         int i;
271
272         if (probe_winbond == 0)
273                 return -ENODEV;
274
275         /*
276          *      Check both base addresses
277          */
278
279         for (i = 0; i < 2; i++) {
280                 if (probe_winbond & (1<<i)) {
281                         int ret = 0;
282                         unsigned long port = config[i];
283
284                         if (request_region(port, 2, "pata_winbond")) {
285                                 ret = winbond_init_one(port);
286                                 if(ret <= 0)
287                                         release_region(port, 2);
288                                 else ct+= ret;
289                         }
290                 }
291         }
292         if (ct != 0)
293                 return 0;
294         return -ENODEV;
295 }
296
297 static __exit void winbond_exit(void)
298 {
299         int i;
300
301         for (i = 0; i < nr_winbond_host; i++) {
302                 ata_host_detach(winbond_host[i]);
303                 release_region(winbond_data[i].config, 2);
304                 platform_device_unregister(winbond_data[i].platform_dev);
305         }
306 }
307
308 MODULE_AUTHOR("Alan Cox");
309 MODULE_DESCRIPTION("low-level driver for Winbond VL ATA");
310 MODULE_LICENSE("GPL");
311 MODULE_VERSION(DRV_VERSION);
312
313 module_init(winbond_init);
314 module_exit(winbond_exit);
315
316 module_param(probe_winbond, int, 0);
317