pata_atp867x: clarifications in timings calculations and cable detection
[safe/jmp/linux-2.6] / drivers / ata / pata_atp867x.c
1 /*
2  * pata_atp867x.c - ARTOP 867X 64bit 4-channel UDMA133 ATA controller driver
3  *
4  *      (C) 2009 Google Inc. John(Jung-Ik) Lee <jilee@google.com>
5  *
6  * Per Atp867 data sheet rev 1.2, Acard.
7  * Based in part on early ide code from
8  *      2003-2004 by Eric Uhrhane, Google, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  *
25  * TODO:
26  *   1. RAID features [comparison, XOR, striping, mirroring, etc.]
27  */
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <linux/init.h>
33 #include <linux/blkdev.h>
34 #include <linux/delay.h>
35 #include <linux/device.h>
36 #include <scsi/scsi_host.h>
37 #include <linux/libata.h>
38
39 #define DRV_NAME        "pata_atp867x"
40 #define DRV_VERSION     "0.7.5"
41
42 /*
43  * IO Registers
44  * Note that all runtime hot priv ports are cached in ap private_data
45  */
46
47 enum {
48         ATP867X_IO_CHANNEL_OFFSET       = 0x10,
49
50         /*
51          * IO Register Bitfields
52          */
53
54         ATP867X_IO_PIOSPD_ACTIVE_SHIFT  = 4,
55         ATP867X_IO_PIOSPD_RECOVER_SHIFT = 0,
56
57         ATP867X_IO_DMAMODE_MSTR_SHIFT   = 0,
58         ATP867X_IO_DMAMODE_MSTR_MASK    = 0x07,
59         ATP867X_IO_DMAMODE_SLAVE_SHIFT  = 4,
60         ATP867X_IO_DMAMODE_SLAVE_MASK   = 0x70,
61
62         ATP867X_IO_DMAMODE_UDMA_6       = 0x07,
63         ATP867X_IO_DMAMODE_UDMA_5       = 0x06,
64         ATP867X_IO_DMAMODE_UDMA_4       = 0x05,
65         ATP867X_IO_DMAMODE_UDMA_3       = 0x04,
66         ATP867X_IO_DMAMODE_UDMA_2       = 0x03,
67         ATP867X_IO_DMAMODE_UDMA_1       = 0x02,
68         ATP867X_IO_DMAMODE_UDMA_0       = 0x01,
69         ATP867X_IO_DMAMODE_DISABLE      = 0x00,
70
71         ATP867X_IO_SYS_INFO_66MHZ       = 0x04,
72         ATP867X_IO_SYS_INFO_SLOW_UDMA5  = 0x02,
73         ATP867X_IO_SYS_MASK_RESERVED    = (~0xf1),
74
75         ATP867X_IO_PORTSPD_VAL          = 0x1143,
76         ATP867X_PREREAD_VAL             = 0x0200,
77
78         ATP867X_NUM_PORTS               = 4,
79         ATP867X_BAR_IOBASE              = 0,
80         ATP867X_BAR_ROMBASE             = 6,
81 };
82
83 #define ATP867X_IOBASE(ap)              ((ap)->host->iomap[0])
84 #define ATP867X_SYS_INFO(ap)            (0x3F + ATP867X_IOBASE(ap))
85
86 #define ATP867X_IO_PORTBASE(ap, port)   (0x00 + ATP867X_IOBASE(ap) + \
87                                         (port) * ATP867X_IO_CHANNEL_OFFSET)
88 #define ATP867X_IO_DMABASE(ap, port)    (0x40 + \
89                                         ATP867X_IO_PORTBASE((ap), (port)))
90
91 #define ATP867X_IO_STATUS(ap, port)     (0x07 + \
92                                         ATP867X_IO_PORTBASE((ap), (port)))
93 #define ATP867X_IO_ALTSTATUS(ap, port)  (0x0E + \
94                                         ATP867X_IO_PORTBASE((ap), (port)))
95
96 /*
97  * hot priv ports
98  */
99 #define ATP867X_IO_MSTRPIOSPD(ap, port) (0x08 + \
100                                         ATP867X_IO_DMABASE((ap), (port)))
101 #define ATP867X_IO_SLAVPIOSPD(ap, port) (0x09 + \
102                                         ATP867X_IO_DMABASE((ap), (port)))
103 #define ATP867X_IO_8BPIOSPD(ap, port)   (0x0A + \
104                                         ATP867X_IO_DMABASE((ap), (port)))
105 #define ATP867X_IO_DMAMODE(ap, port)    (0x0B + \
106                                         ATP867X_IO_DMABASE((ap), (port)))
107
108 #define ATP867X_IO_PORTSPD(ap, port)    (0x4A + \
109                                         ATP867X_IO_PORTBASE((ap), (port)))
110 #define ATP867X_IO_PREREAD(ap, port)    (0x4C + \
111                                         ATP867X_IO_PORTBASE((ap), (port)))
112
113 struct atp867x_priv {
114         void __iomem *dma_mode;
115         void __iomem *mstr_piospd;
116         void __iomem *slave_piospd;
117         void __iomem *eightb_piospd;
118         int             pci66mhz;
119 };
120
121 static void atp867x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
122 {
123         struct pci_dev *pdev    = to_pci_dev(ap->host->dev);
124         struct atp867x_priv *dp = ap->private_data;
125         u8 speed = adev->dma_mode;
126         u8 b;
127         u8 mode = speed - XFER_UDMA_0 + 1;
128
129         /*
130          * Doc 6.6.9: decrease the udma mode value by 1 for safer UDMA speed
131          * on 66MHz bus
132          *   rev-A: UDMA_1~4 (5, 6 no change)
133          *   rev-B: all UDMA modes
134          *   UDMA_0 stays not to disable UDMA
135          */
136         if (dp->pci66mhz && mode > ATP867X_IO_DMAMODE_UDMA_0  &&
137            (pdev->device == PCI_DEVICE_ID_ARTOP_ATP867B ||
138             mode < ATP867X_IO_DMAMODE_UDMA_5))
139                 mode--;
140
141         b = ioread8(dp->dma_mode);
142         if (adev->devno & 1) {
143                 b = (b & ~ATP867X_IO_DMAMODE_SLAVE_MASK) |
144                         (mode << ATP867X_IO_DMAMODE_SLAVE_SHIFT);
145         } else {
146                 b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK) |
147                         (mode << ATP867X_IO_DMAMODE_MSTR_SHIFT);
148         }
149         iowrite8(b, dp->dma_mode);
150 }
151
152 static int atp867x_get_active_clocks_shifted(struct ata_port *ap,
153         unsigned int clk)
154 {
155         struct atp867x_priv *dp = ap->private_data;
156         unsigned char clocks = clk;
157
158         switch (clocks) {
159         case 0:
160                 clocks = 1;
161                 break;
162         case 1 ... 7:
163                 break;
164         case 9 ... 12:
165                 clocks = 7;
166                 break;
167         default:
168                 printk(KERN_WARNING "ATP867X: active %dclk is invalid. "
169                         "Using default 8clk.\n", clk);
170         case 8: /* default 8 clk */
171                 clocks = 0;
172                 goto active_clock_shift_done;
173         }
174
175         /*
176          * Doc 6.6.9: increase the clock value by 1 for safer PIO speed
177          * on 66MHz bus
178          */
179         if (dp->pci66mhz && clocks < 7)
180                 clocks++;
181
182 active_clock_shift_done:
183         return clocks << ATP867X_IO_PIOSPD_ACTIVE_SHIFT;
184 }
185
186 static int atp867x_get_recover_clocks_shifted(unsigned int clk)
187 {
188         unsigned char clocks = clk;
189
190         switch (clocks) {
191         case 0:
192                 clocks = 1;
193                 break;
194         case 1 ... 11:
195                 break;
196         case 13: case 14:
197                 --clocks;       /* by the spec */
198                 break;
199         case 15:
200                 break;
201         default:
202                 printk(KERN_WARNING "ATP867X: recover %dclk is invalid. "
203                         "Using default 12clk.\n", clk);
204         case 12:        /* default 12 clk */
205                 clocks = 0;
206                 break;
207         }
208
209         return clocks << ATP867X_IO_PIOSPD_RECOVER_SHIFT;
210 }
211
212 static void atp867x_set_piomode(struct ata_port *ap, struct ata_device *adev)
213 {
214         struct ata_device *peer = ata_dev_pair(adev);
215         struct atp867x_priv *dp = ap->private_data;
216         u8 speed = adev->pio_mode;
217         struct ata_timing t, p;
218         int T, UT;
219         u8 b;
220
221         T = 1000000000 / 33333;
222         UT = T / 4;
223
224         ata_timing_compute(adev, speed, &t, T, UT);
225         if (peer && peer->pio_mode) {
226                 ata_timing_compute(peer, peer->pio_mode, &p, T, UT);
227                 ata_timing_merge(&p, &t, &t, ATA_TIMING_8BIT);
228         }
229
230         b = ioread8(dp->dma_mode);
231         if (adev->devno & 1)
232                 b = (b & ~ATP867X_IO_DMAMODE_SLAVE_MASK);
233         else
234                 b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK);
235         iowrite8(b, dp->dma_mode);
236
237         b = atp867x_get_active_clocks_shifted(ap, t.active) |
238                 atp867x_get_recover_clocks_shifted(t.recover);
239
240         if (adev->devno & 1)
241                 iowrite8(b, dp->slave_piospd);
242         else
243                 iowrite8(b, dp->mstr_piospd);
244
245         /*
246          * use the same value for comand timing as for PIO timimg
247          */
248         iowrite8(b, dp->eightb_piospd);
249 }
250
251 static int atp867x_cable_override(struct pci_dev *pdev)
252 {
253         if (pdev->subsystem_vendor == PCI_VENDOR_ID_ARTOP &&
254                 (pdev->subsystem_device == PCI_DEVICE_ID_ARTOP_ATP867A ||
255                  pdev->subsystem_device == PCI_DEVICE_ID_ARTOP_ATP867B)) {
256                 return 1;
257         }
258         return 0;
259 }
260
261 static int atp867x_cable_detect(struct ata_port *ap)
262 {
263         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
264
265         if (atp867x_cable_override(pdev))
266                 return ATA_CBL_PATA40_SHORT;
267
268         return ATA_CBL_PATA_UNK;
269 }
270
271 static struct scsi_host_template atp867x_sht = {
272         ATA_BMDMA_SHT(DRV_NAME),
273 };
274
275 static struct ata_port_operations atp867x_ops = {
276         .inherits               = &ata_bmdma_port_ops,
277         .cable_detect           = atp867x_cable_detect,
278         .set_piomode            = atp867x_set_piomode,
279         .set_dmamode            = atp867x_set_dmamode,
280 };
281
282
283 #ifdef  ATP867X_DEBUG
284 static void atp867x_check_res(struct pci_dev *pdev)
285 {
286         int i;
287         unsigned long start, len;
288
289         /* Check the PCI resources for this channel are enabled */
290         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
291                 start = pci_resource_start(pdev, i);
292                 len   = pci_resource_len(pdev, i);
293                 printk(KERN_DEBUG "ATP867X: resource start:len=%lx:%lx\n",
294                         start, len);
295         }
296 }
297
298 static void atp867x_check_ports(struct ata_port *ap, int port)
299 {
300         struct ata_ioports *ioaddr = &ap->ioaddr;
301         struct atp867x_priv *dp = ap->private_data;
302
303         printk(KERN_DEBUG "ATP867X: port[%d] addresses\n"
304                 "  cmd_addr     =0x%llx, 0x%llx\n"
305                 "  ctl_addr     =0x%llx, 0x%llx\n"
306                 "  bmdma_addr   =0x%llx, 0x%llx\n"
307                 "  data_addr    =0x%llx\n"
308                 "  error_addr   =0x%llx\n"
309                 "  feature_addr =0x%llx\n"
310                 "  nsect_addr   =0x%llx\n"
311                 "  lbal_addr    =0x%llx\n"
312                 "  lbam_addr    =0x%llx\n"
313                 "  lbah_addr    =0x%llx\n"
314                 "  device_addr  =0x%llx\n"
315                 "  status_addr  =0x%llx\n"
316                 "  command_addr =0x%llx\n"
317                 "  dp->dma_mode =0x%llx\n"
318                 "  dp->mstr_piospd      =0x%llx\n"
319                 "  dp->slave_piospd     =0x%llx\n"
320                 "  dp->eightb_piospd    =0x%llx\n"
321                 "  dp->pci66mhz         =0x%lx\n",
322                 port,
323                 (unsigned long long)ioaddr->cmd_addr,
324                 (unsigned long long)ATP867X_IO_PORTBASE(ap, port),
325                 (unsigned long long)ioaddr->ctl_addr,
326                 (unsigned long long)ATP867X_IO_ALTSTATUS(ap, port),
327                 (unsigned long long)ioaddr->bmdma_addr,
328                 (unsigned long long)ATP867X_IO_DMABASE(ap, port),
329                 (unsigned long long)ioaddr->data_addr,
330                 (unsigned long long)ioaddr->error_addr,
331                 (unsigned long long)ioaddr->feature_addr,
332                 (unsigned long long)ioaddr->nsect_addr,
333                 (unsigned long long)ioaddr->lbal_addr,
334                 (unsigned long long)ioaddr->lbam_addr,
335                 (unsigned long long)ioaddr->lbah_addr,
336                 (unsigned long long)ioaddr->device_addr,
337                 (unsigned long long)ioaddr->status_addr,
338                 (unsigned long long)ioaddr->command_addr,
339                 (unsigned long long)dp->dma_mode,
340                 (unsigned long long)dp->mstr_piospd,
341                 (unsigned long long)dp->slave_piospd,
342                 (unsigned long long)dp->eightb_piospd,
343                 (unsigned long)dp->pci66mhz);
344 }
345 #endif
346
347 static int atp867x_set_priv(struct ata_port *ap)
348 {
349         struct pci_dev *pdev = to_pci_dev(ap->host->dev);
350         struct atp867x_priv *dp;
351         int port = ap->port_no;
352
353         dp = ap->private_data =
354                 devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
355         if (dp == NULL)
356                 return -ENOMEM;
357
358         dp->dma_mode     = ATP867X_IO_DMAMODE(ap, port);
359         dp->mstr_piospd  = ATP867X_IO_MSTRPIOSPD(ap, port);
360         dp->slave_piospd = ATP867X_IO_SLAVPIOSPD(ap, port);
361         dp->eightb_piospd = ATP867X_IO_8BPIOSPD(ap, port);
362
363         dp->pci66mhz =
364                 ioread8(ATP867X_SYS_INFO(ap)) & ATP867X_IO_SYS_INFO_66MHZ;
365
366         return 0;
367 }
368
369 static void atp867x_fixup(struct ata_host *host)
370 {
371         struct pci_dev *pdev = to_pci_dev(host->dev);
372         struct ata_port *ap = host->ports[0];
373         int i;
374         u8 v;
375
376         /*
377          * Broken BIOS might not set latency high enough
378          */
379         pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &v);
380         if (v < 0x80) {
381                 v = 0x80;
382                 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, v);
383                 printk(KERN_DEBUG "ATP867X: set latency timer of device %s"
384                         " to %d\n", pci_name(pdev), v);
385         }
386
387         /*
388          * init 8bit io ports speed(0aaarrrr) to 43h and
389          * init udma modes of master/slave to 0/0(11h)
390          */
391         for (i = 0; i < ATP867X_NUM_PORTS; i++)
392                 iowrite16(ATP867X_IO_PORTSPD_VAL, ATP867X_IO_PORTSPD(ap, i));
393
394         /*
395          * init PreREAD counts
396          */
397         for (i = 0; i < ATP867X_NUM_PORTS; i++)
398                 iowrite16(ATP867X_PREREAD_VAL, ATP867X_IO_PREREAD(ap, i));
399
400         v = ioread8(ATP867X_IOBASE(ap) + 0x28);
401         v &= 0xcf;      /* Enable INTA#: bit4=0 means enable */
402         v |= 0xc0;      /* Enable PCI burst, MRM & not immediate interrupts */
403         iowrite8(v, ATP867X_IOBASE(ap) + 0x28);
404
405         /*
406          * Turn off the over clocked udma5 mode, only for Rev-B
407          */
408         v = ioread8(ATP867X_SYS_INFO(ap));
409         v &= ATP867X_IO_SYS_MASK_RESERVED;
410         if (pdev->device == PCI_DEVICE_ID_ARTOP_ATP867B)
411                 v |= ATP867X_IO_SYS_INFO_SLOW_UDMA5;
412         iowrite8(v, ATP867X_SYS_INFO(ap));
413 }
414
415 static int atp867x_ata_pci_sff_init_host(struct ata_host *host)
416 {
417         struct device *gdev = host->dev;
418         struct pci_dev *pdev = to_pci_dev(gdev);
419         unsigned int mask = 0;
420         int i, rc;
421
422         /*
423          * do not map rombase
424          */
425         rc = pcim_iomap_regions(pdev, 1 << ATP867X_BAR_IOBASE, DRV_NAME);
426         if (rc == -EBUSY)
427                 pcim_pin_device(pdev);
428         if (rc)
429                 return rc;
430         host->iomap = pcim_iomap_table(pdev);
431
432 #ifdef  ATP867X_DEBUG
433         atp867x_check_res(pdev);
434
435         for (i = 0; i < PCI_ROM_RESOURCE; i++)
436                 printk(KERN_DEBUG "ATP867X: iomap[%d]=0x%llx\n", i,
437                         (unsigned long long)(host->iomap[i]));
438 #endif
439
440         /*
441          * request, iomap BARs and init port addresses accordingly
442          */
443         for (i = 0; i < host->n_ports; i++) {
444                 struct ata_port *ap = host->ports[i];
445                 struct ata_ioports *ioaddr = &ap->ioaddr;
446
447                 ioaddr->cmd_addr = ATP867X_IO_PORTBASE(ap, i);
448                 ioaddr->ctl_addr = ioaddr->altstatus_addr
449                                  = ATP867X_IO_ALTSTATUS(ap, i);
450                 ioaddr->bmdma_addr = ATP867X_IO_DMABASE(ap, i);
451
452                 ata_sff_std_ports(ioaddr);
453                 rc = atp867x_set_priv(ap);
454                 if (rc)
455                         return rc;
456
457 #ifdef  ATP867X_DEBUG
458                 atp867x_check_ports(ap, i);
459 #endif
460                 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx",
461                         (unsigned long)ioaddr->cmd_addr,
462                         (unsigned long)ioaddr->ctl_addr);
463                 ata_port_desc(ap, "bmdma 0x%lx",
464                         (unsigned long)ioaddr->bmdma_addr);
465
466                 mask |= 1 << i;
467         }
468
469         if (!mask) {
470                 dev_printk(KERN_ERR, gdev, "no available native port\n");
471                 return -ENODEV;
472         }
473
474         atp867x_fixup(host);
475
476         rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
477         if (rc)
478                 return rc;
479
480         rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
481         return rc;
482 }
483
484 static int atp867x_init_one(struct pci_dev *pdev,
485         const struct pci_device_id *id)
486 {
487         static int printed_version;
488         static const struct ata_port_info info_867x = {
489                 .flags          = ATA_FLAG_SLAVE_POSS,
490                 .pio_mask       = ATA_PIO4,
491                 .udma_mask      = ATA_UDMA6,
492                 .port_ops       = &atp867x_ops,
493         };
494
495         struct ata_host *host;
496         const struct ata_port_info *ppi[] = { &info_867x, NULL };
497         int rc;
498
499         if (!printed_version++)
500                 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
501
502         rc = pcim_enable_device(pdev);
503         if (rc)
504                 return rc;
505
506         printk(KERN_INFO "ATP867X: ATP867 ATA UDMA133 controller (rev %02X)",
507                 pdev->device);
508
509         host = ata_host_alloc_pinfo(&pdev->dev, ppi, ATP867X_NUM_PORTS);
510         if (!host) {
511                 dev_printk(KERN_ERR, &pdev->dev,
512                         "failed to allocate ATA host\n");
513                 rc = -ENOMEM;
514                 goto err_out;
515         }
516
517         rc = atp867x_ata_pci_sff_init_host(host);
518         if (rc) {
519                 dev_printk(KERN_ERR, &pdev->dev, "failed to init host\n");
520                 goto err_out;
521         }
522
523         pci_set_master(pdev);
524
525         rc = ata_host_activate(host, pdev->irq, ata_sff_interrupt,
526                                 IRQF_SHARED, &atp867x_sht);
527         if (rc)
528                 dev_printk(KERN_ERR, &pdev->dev, "failed to activate host\n");
529
530 err_out:
531         return rc;
532 }
533
534 static struct pci_device_id atp867x_pci_tbl[] = {
535         { PCI_VDEVICE(ARTOP, PCI_DEVICE_ID_ARTOP_ATP867A),      0 },
536         { PCI_VDEVICE(ARTOP, PCI_DEVICE_ID_ARTOP_ATP867B),      0 },
537         { },
538 };
539
540 static struct pci_driver atp867x_driver = {
541         .name           = DRV_NAME,
542         .id_table       = atp867x_pci_tbl,
543         .probe          = atp867x_init_one,
544         .remove         = ata_pci_remove_one,
545 };
546
547 static int __init atp867x_init(void)
548 {
549         return pci_register_driver(&atp867x_driver);
550 }
551
552 static void __exit atp867x_exit(void)
553 {
554         pci_unregister_driver(&atp867x_driver);
555 }
556
557 MODULE_AUTHOR("John(Jung-Ik) Lee, Google Inc.");
558 MODULE_DESCRIPTION("low level driver for Artop/Acard 867x ATA controller");
559 MODULE_LICENSE("GPL");
560 MODULE_DEVICE_TABLE(pci, atp867x_pci_tbl);
561 MODULE_VERSION(DRV_VERSION);
562
563 module_init(atp867x_init);
564 module_exit(atp867x_exit);