blob: 7990de925d2e296cd79d9d7abf4622713662bc09 [file] [log] [blame]
John(Jung-Ik) Leed15d6e62009-09-14 21:32:33 -07001/*
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
47enum {
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
113struct 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
121static inline u8 atp867x_speed_to_mode(u8 speed)
122{
123 return speed - XFER_UDMA_0 + 1;
124}
125
126static void atp867x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
127{
128 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
129 struct atp867x_priv *dp = ap->private_data;
130 u8 speed = adev->dma_mode;
131 u8 b;
132 u8 mode;
133
134 mode = atp867x_speed_to_mode(speed);
135
136 /*
137 * Doc 6.6.9: decrease the udma mode value by 1 for safer UDMA speed
138 * on 66MHz bus
139 * rev-A: UDMA_1~4 (5, 6 no change)
140 * rev-B: all UDMA modes
141 * UDMA_0 stays not to disable UDMA
142 */
143 if (dp->pci66mhz && mode > ATP867X_IO_DMAMODE_UDMA_0 &&
144 (pdev->device == PCI_DEVICE_ID_ARTOP_ATP867B ||
145 mode < ATP867X_IO_DMAMODE_UDMA_5))
146 mode--;
147
148 b = ioread8(dp->dma_mode);
149 if (adev->devno & 1) {
150 b = (b & ~ATP867X_IO_DMAMODE_SLAVE_MASK) |
151 (mode << ATP867X_IO_DMAMODE_SLAVE_SHIFT);
152 } else {
153 b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK) |
154 (mode << ATP867X_IO_DMAMODE_MSTR_SHIFT);
155 }
156 iowrite8(b, dp->dma_mode);
157}
158
159static int atp867x_get_active_clocks_shifted(unsigned int clk)
160{
161 unsigned char clocks = clk;
162
163 switch (clocks) {
164 case 0:
165 clocks = 1;
166 break;
167 case 1 ... 7:
168 break;
169 case 8 ... 12:
170 clocks = 7;
171 break;
172 default:
173 printk(KERN_WARNING "ATP867X: active %dclk is invalid. "
174 "Using default 8clk.\n", clk);
175 clocks = 0; /* 8 clk */
176 break;
177 }
178 return clocks << ATP867X_IO_PIOSPD_ACTIVE_SHIFT;
179}
180
181static int atp867x_get_recover_clocks_shifted(unsigned int clk)
182{
183 unsigned char clocks = clk;
184
185 switch (clocks) {
186 case 0:
187 clocks = 1;
188 break;
189 case 1 ... 11:
190 break;
191 case 12:
192 clocks = 0;
193 break;
194 case 13: case 14:
195 --clocks;
196 break;
197 case 15:
198 break;
199 default:
200 printk(KERN_WARNING "ATP867X: recover %dclk is invalid. "
201 "Using default 15clk.\n", clk);
202 clocks = 0; /* 12 clk */
203 break;
204 }
205 return clocks << ATP867X_IO_PIOSPD_RECOVER_SHIFT;
206}
207
208static void atp867x_set_piomode(struct ata_port *ap, struct ata_device *adev)
209{
210 struct ata_device *peer = ata_dev_pair(adev);
211 struct atp867x_priv *dp = ap->private_data;
212 u8 speed = adev->pio_mode;
213 struct ata_timing t, p;
214 int T, UT;
215 u8 b;
216
217 T = 1000000000 / 33333;
218 UT = T / 4;
219
220 ata_timing_compute(adev, speed, &t, T, UT);
221 if (peer && peer->pio_mode) {
222 ata_timing_compute(peer, peer->pio_mode, &p, T, UT);
223 ata_timing_merge(&p, &t, &t, ATA_TIMING_8BIT);
224 }
225
226 b = ioread8(dp->dma_mode);
227 if (adev->devno & 1)
228 b = (b & ~ATP867X_IO_DMAMODE_SLAVE_MASK);
229 else
230 b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK);
231 iowrite8(b, dp->dma_mode);
232
233 b = atp867x_get_active_clocks_shifted(t.active) |
234 atp867x_get_recover_clocks_shifted(t.recover);
235 if (dp->pci66mhz)
236 b += 0x10;
237
238 if (adev->devno & 1)
239 iowrite8(b, dp->slave_piospd);
240 else
241 iowrite8(b, dp->mstr_piospd);
242
243 /*
244 * use the same value for comand timing as for PIO timimg
245 */
246 iowrite8(b, dp->eightb_piospd);
247}
248
249static int atp867x_cable_detect(struct ata_port *ap)
250{
251 return ATA_CBL_PATA40_SHORT;
252}
253
254static struct scsi_host_template atp867x_sht = {
255 ATA_BMDMA_SHT(DRV_NAME),
256};
257
258static struct ata_port_operations atp867x_ops = {
259 .inherits = &ata_bmdma_port_ops,
260 .cable_detect = atp867x_cable_detect,
261 .set_piomode = atp867x_set_piomode,
262 .set_dmamode = atp867x_set_dmamode,
263};
264
265
266#ifdef ATP867X_DEBUG
267static void atp867x_check_res(struct pci_dev *pdev)
268{
269 int i;
270 unsigned long start, len;
271
272 /* Check the PCI resources for this channel are enabled */
273 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
274 start = pci_resource_start(pdev, i);
275 len = pci_resource_len(pdev, i);
276 printk(KERN_DEBUG "ATP867X: resource start:len=%lx:%lx\n",
277 start, len);
278 }
279}
280
281static void atp867x_check_ports(struct ata_port *ap, int port)
282{
283 struct ata_ioports *ioaddr = &ap->ioaddr;
284 struct atp867x_priv *dp = ap->private_data;
285
286 printk(KERN_DEBUG "ATP867X: port[%d] addresses\n"
287 " cmd_addr =0x%llx, 0x%llx\n"
288 " ctl_addr =0x%llx, 0x%llx\n"
289 " bmdma_addr =0x%llx, 0x%llx\n"
290 " data_addr =0x%llx\n"
291 " error_addr =0x%llx\n"
292 " feature_addr =0x%llx\n"
293 " nsect_addr =0x%llx\n"
294 " lbal_addr =0x%llx\n"
295 " lbam_addr =0x%llx\n"
296 " lbah_addr =0x%llx\n"
297 " device_addr =0x%llx\n"
298 " status_addr =0x%llx\n"
299 " command_addr =0x%llx\n"
300 " dp->dma_mode =0x%llx\n"
301 " dp->mstr_piospd =0x%llx\n"
302 " dp->slave_piospd =0x%llx\n"
303 " dp->eightb_piospd =0x%llx\n"
304 " dp->pci66mhz =0x%lx\n",
305 port,
306 (unsigned long long)ioaddr->cmd_addr,
307 (unsigned long long)ATP867X_IO_PORTBASE(ap, port),
308 (unsigned long long)ioaddr->ctl_addr,
309 (unsigned long long)ATP867X_IO_ALTSTATUS(ap, port),
310 (unsigned long long)ioaddr->bmdma_addr,
311 (unsigned long long)ATP867X_IO_DMABASE(ap, port),
312 (unsigned long long)ioaddr->data_addr,
313 (unsigned long long)ioaddr->error_addr,
314 (unsigned long long)ioaddr->feature_addr,
315 (unsigned long long)ioaddr->nsect_addr,
316 (unsigned long long)ioaddr->lbal_addr,
317 (unsigned long long)ioaddr->lbam_addr,
318 (unsigned long long)ioaddr->lbah_addr,
319 (unsigned long long)ioaddr->device_addr,
320 (unsigned long long)ioaddr->status_addr,
321 (unsigned long long)ioaddr->command_addr,
322 (unsigned long long)dp->dma_mode,
323 (unsigned long long)dp->mstr_piospd,
324 (unsigned long long)dp->slave_piospd,
325 (unsigned long long)dp->eightb_piospd,
326 (unsigned long)dp->pci66mhz);
327}
328#endif
329
330static int atp867x_set_priv(struct ata_port *ap)
331{
332 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
333 struct atp867x_priv *dp;
334 int port = ap->port_no;
335
336 dp = ap->private_data =
337 devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
338 if (dp == NULL)
339 return -ENOMEM;
340
341 dp->dma_mode = ATP867X_IO_DMAMODE(ap, port);
342 dp->mstr_piospd = ATP867X_IO_MSTRPIOSPD(ap, port);
343 dp->slave_piospd = ATP867X_IO_SLAVPIOSPD(ap, port);
344 dp->eightb_piospd = ATP867X_IO_8BPIOSPD(ap, port);
345
346 dp->pci66mhz =
347 ioread8(ATP867X_SYS_INFO(ap)) & ATP867X_IO_SYS_INFO_66MHZ;
348
349 return 0;
350}
351
352static void atp867x_fixup(struct ata_host *host)
353{
354 struct pci_dev *pdev = to_pci_dev(host->dev);
355 struct ata_port *ap = host->ports[0];
356 int i;
357 u8 v;
358
359 /*
360 * Broken BIOS might not set latency high enough
361 */
362 pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &v);
363 if (v < 0x80) {
364 v = 0x80;
365 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, v);
366 printk(KERN_DEBUG "ATP867X: set latency timer of device %s"
367 " to %d\n", pci_name(pdev), v);
368 }
369
370 /*
371 * init 8bit io ports speed(0aaarrrr) to 43h and
372 * init udma modes of master/slave to 0/0(11h)
373 */
374 for (i = 0; i < ATP867X_NUM_PORTS; i++)
375 iowrite16(ATP867X_IO_PORTSPD_VAL, ATP867X_IO_PORTSPD(ap, i));
376
377 /*
378 * init PreREAD counts
379 */
380 for (i = 0; i < ATP867X_NUM_PORTS; i++)
381 iowrite16(ATP867X_PREREAD_VAL, ATP867X_IO_PREREAD(ap, i));
382
383 v = ioread8(ATP867X_IOBASE(ap) + 0x28);
384 v &= 0xcf; /* Enable INTA#: bit4=0 means enable */
385 v |= 0xc0; /* Enable PCI burst, MRM & not immediate interrupts */
386 iowrite8(v, ATP867X_IOBASE(ap) + 0x28);
387
388 /*
389 * Turn off the over clocked udma5 mode, only for Rev-B
390 */
391 v = ioread8(ATP867X_SYS_INFO(ap));
392 v &= ATP867X_IO_SYS_MASK_RESERVED;
393 if (pdev->device == PCI_DEVICE_ID_ARTOP_ATP867B)
394 v |= ATP867X_IO_SYS_INFO_SLOW_UDMA5;
395 iowrite8(v, ATP867X_SYS_INFO(ap));
396}
397
398static int atp867x_ata_pci_sff_init_host(struct ata_host *host)
399{
400 struct device *gdev = host->dev;
401 struct pci_dev *pdev = to_pci_dev(gdev);
402 unsigned int mask = 0;
403 int i, rc;
404
405 /*
406 * do not map rombase
407 */
408 rc = pcim_iomap_regions(pdev, 1 << ATP867X_BAR_IOBASE, DRV_NAME);
409 if (rc == -EBUSY)
410 pcim_pin_device(pdev);
411 if (rc)
412 return rc;
413 host->iomap = pcim_iomap_table(pdev);
414
415#ifdef ATP867X_DEBUG
416 atp867x_check_res(pdev);
417
418 for (i = 0; i < PCI_ROM_RESOURCE; i++)
419 printk(KERN_DEBUG "ATP867X: iomap[%d]=0x%llx\n", i,
420 (unsigned long long)(host->iomap[i]));
421#endif
422
423 /*
424 * request, iomap BARs and init port addresses accordingly
425 */
426 for (i = 0; i < host->n_ports; i++) {
427 struct ata_port *ap = host->ports[i];
428 struct ata_ioports *ioaddr = &ap->ioaddr;
429
430 ioaddr->cmd_addr = ATP867X_IO_PORTBASE(ap, i);
431 ioaddr->ctl_addr = ioaddr->altstatus_addr
432 = ATP867X_IO_ALTSTATUS(ap, i);
433 ioaddr->bmdma_addr = ATP867X_IO_DMABASE(ap, i);
434
435 ata_sff_std_ports(ioaddr);
436 rc = atp867x_set_priv(ap);
437 if (rc)
438 return rc;
439
440#ifdef ATP867X_DEBUG
441 atp867x_check_ports(ap, i);
442#endif
443 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx",
444 (unsigned long)ioaddr->cmd_addr,
445 (unsigned long)ioaddr->ctl_addr);
446 ata_port_desc(ap, "bmdma 0x%lx",
447 (unsigned long)ioaddr->bmdma_addr);
448
449 mask |= 1 << i;
450 }
451
452 if (!mask) {
453 dev_printk(KERN_ERR, gdev, "no available native port\n");
454 return -ENODEV;
455 }
456
457 atp867x_fixup(host);
458
459 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
460 if (rc)
461 return rc;
462
463 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
464 return rc;
465}
466
467static int atp867x_init_one(struct pci_dev *pdev,
468 const struct pci_device_id *id)
469{
470 static int printed_version;
471 static const struct ata_port_info info_867x = {
472 .flags = ATA_FLAG_SLAVE_POSS,
473 .pio_mask = ATA_PIO4,
474 .mwdma_mask = ATA_MWDMA2,
475 .udma_mask = ATA_UDMA6,
476 .port_ops = &atp867x_ops,
477 };
478
479 struct ata_host *host;
480 const struct ata_port_info *ppi[] = { &info_867x, NULL };
481 int rc;
482
483 if (!printed_version++)
484 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
485
486 rc = pcim_enable_device(pdev);
487 if (rc)
488 return rc;
489
490 printk(KERN_INFO "ATP867X: ATP867 ATA UDMA133 controller (rev %02X)",
491 pdev->device);
492
493 host = ata_host_alloc_pinfo(&pdev->dev, ppi, ATP867X_NUM_PORTS);
494 if (!host) {
495 dev_printk(KERN_ERR, &pdev->dev,
496 "failed to allocate ATA host\n");
497 rc = -ENOMEM;
498 goto err_out;
499 }
500
501 rc = atp867x_ata_pci_sff_init_host(host);
502 if (rc) {
503 dev_printk(KERN_ERR, &pdev->dev, "failed to init host\n");
504 goto err_out;
505 }
506
507 pci_set_master(pdev);
508
509 rc = ata_host_activate(host, pdev->irq, ata_sff_interrupt,
510 IRQF_SHARED, &atp867x_sht);
511 if (rc)
512 dev_printk(KERN_ERR, &pdev->dev, "failed to activate host\n");
513
514err_out:
515 return rc;
516}
517
518static struct pci_device_id atp867x_pci_tbl[] = {
519 { PCI_VDEVICE(ARTOP, PCI_DEVICE_ID_ARTOP_ATP867A), 0 },
520 { PCI_VDEVICE(ARTOP, PCI_DEVICE_ID_ARTOP_ATP867B), 0 },
521 { },
522};
523
524static struct pci_driver atp867x_driver = {
525 .name = DRV_NAME,
526 .id_table = atp867x_pci_tbl,
527 .probe = atp867x_init_one,
528 .remove = ata_pci_remove_one,
529};
530
531static int __init atp867x_init(void)
532{
533 return pci_register_driver(&atp867x_driver);
534}
535
536static void __exit atp867x_exit(void)
537{
538 pci_unregister_driver(&atp867x_driver);
539}
540
541MODULE_AUTHOR("John(Jung-Ik) Lee, Google Inc.");
542MODULE_DESCRIPTION("low level driver for Artop/Acard 867x ATA controller");
543MODULE_LICENSE("GPL");
544MODULE_DEVICE_TABLE(pci, atp867x_pci_tbl);
545MODULE_VERSION(DRV_VERSION);
546
547module_init(atp867x_init);
548module_exit(atp867x_exit);