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