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