ide: destroy DMA mappings after ending DMA (v2)
[firefly-linux-kernel-4.4.55.git] / drivers / ide / scc_pata.c
1 /*
2  * Support for IDE interfaces on Celleb platform
3  *
4  * (C) Copyright 2006 TOSHIBA CORPORATION
5  *
6  * This code is based on drivers/ide/pci/siimage.c:
7  * Copyright (C) 2001-2002      Andre Hedrick <andre@linux-ide.org>
8  * Copyright (C) 2003           Red Hat
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 along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include <linux/types.h>
26 #include <linux/module.h>
27 #include <linux/pci.h>
28 #include <linux/delay.h>
29 #include <linux/ide.h>
30 #include <linux/init.h>
31
32 #define PCI_DEVICE_ID_TOSHIBA_SCC_ATA            0x01b4
33
34 #define SCC_PATA_NAME           "scc IDE"
35
36 #define TDVHSEL_MASTER          0x00000001
37 #define TDVHSEL_SLAVE           0x00000004
38
39 #define MODE_JCUSFEN            0x00000080
40
41 #define CCKCTRL_ATARESET        0x00040000
42 #define CCKCTRL_BUFCNT          0x00020000
43 #define CCKCTRL_CRST            0x00010000
44 #define CCKCTRL_OCLKEN          0x00000100
45 #define CCKCTRL_ATACLKOEN       0x00000002
46 #define CCKCTRL_LCLKEN          0x00000001
47
48 #define QCHCD_IOS_SS            0x00000001
49
50 #define QCHSD_STPDIAG           0x00020000
51
52 #define INTMASK_MSK             0xD1000012
53 #define INTSTS_SERROR           0x80000000
54 #define INTSTS_PRERR            0x40000000
55 #define INTSTS_RERR             0x10000000
56 #define INTSTS_ICERR            0x01000000
57 #define INTSTS_BMSINT           0x00000010
58 #define INTSTS_BMHE             0x00000008
59 #define INTSTS_IOIRQS           0x00000004
60 #define INTSTS_INTRQ            0x00000002
61 #define INTSTS_ACTEINT          0x00000001
62
63 #define ECMODE_VALUE 0x01
64
65 static struct scc_ports {
66         unsigned long ctl, dma;
67         struct ide_host *host;  /* for removing port from system */
68 } scc_ports[MAX_HWIFS];
69
70 /* PIO transfer mode  table */
71 /* JCHST */
72 static unsigned long JCHSTtbl[2][7] = {
73         {0x0E, 0x05, 0x02, 0x03, 0x02, 0x00, 0x00},   /* 100MHz */
74         {0x13, 0x07, 0x04, 0x04, 0x03, 0x00, 0x00}    /* 133MHz */
75 };
76
77 /* JCHHT */
78 static unsigned long JCHHTtbl[2][7] = {
79         {0x0E, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00},   /* 100MHz */
80         {0x13, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00}    /* 133MHz */
81 };
82
83 /* JCHCT */
84 static unsigned long JCHCTtbl[2][7] = {
85         {0x1D, 0x1D, 0x1C, 0x0B, 0x06, 0x00, 0x00},   /* 100MHz */
86         {0x27, 0x26, 0x26, 0x0E, 0x09, 0x00, 0x00}    /* 133MHz */
87 };
88
89
90 /* DMA transfer mode  table */
91 /* JCHDCTM/JCHDCTS */
92 static unsigned long JCHDCTxtbl[2][7] = {
93         {0x0A, 0x06, 0x04, 0x03, 0x01, 0x00, 0x00},   /* 100MHz */
94         {0x0E, 0x09, 0x06, 0x04, 0x02, 0x01, 0x00}    /* 133MHz */
95 };
96
97 /* JCSTWTM/JCSTWTS  */
98 static unsigned long JCSTWTxtbl[2][7] = {
99         {0x06, 0x04, 0x03, 0x02, 0x02, 0x02, 0x00},   /* 100MHz */
100         {0x09, 0x06, 0x04, 0x02, 0x02, 0x02, 0x02}    /* 133MHz */
101 };
102
103 /* JCTSS */
104 static unsigned long JCTSStbl[2][7] = {
105         {0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x00},   /* 100MHz */
106         {0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05}    /* 133MHz */
107 };
108
109 /* JCENVT */
110 static unsigned long JCENVTtbl[2][7] = {
111         {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00},   /* 100MHz */
112         {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02}    /* 133MHz */
113 };
114
115 /* JCACTSELS/JCACTSELM */
116 static unsigned long JCACTSELtbl[2][7] = {
117         {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00},   /* 100MHz */
118         {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}    /* 133MHz */
119 };
120
121
122 static u8 scc_ide_inb(unsigned long port)
123 {
124         u32 data = in_be32((void*)port);
125         return (u8)data;
126 }
127
128 static void scc_exec_command(ide_hwif_t *hwif, u8 cmd)
129 {
130         out_be32((void *)hwif->io_ports.command_addr, cmd);
131         eieio();
132         in_be32((void *)(hwif->dma_base + 0x01c));
133         eieio();
134 }
135
136 static u8 scc_read_status(ide_hwif_t *hwif)
137 {
138         return (u8)in_be32((void *)hwif->io_ports.status_addr);
139 }
140
141 static u8 scc_read_altstatus(ide_hwif_t *hwif)
142 {
143         return (u8)in_be32((void *)hwif->io_ports.ctl_addr);
144 }
145
146 static u8 scc_dma_sff_read_status(ide_hwif_t *hwif)
147 {
148         return (u8)in_be32((void *)(hwif->dma_base + 4));
149 }
150
151 static void scc_set_irq(ide_hwif_t *hwif, int on)
152 {
153         u8 ctl = ATA_DEVCTL_OBS;
154
155         if (on == 4) { /* hack for SRST */
156                 ctl |= 4;
157                 on &= ~4;
158         }
159
160         ctl |= on ? 0 : 2;
161
162         out_be32((void *)hwif->io_ports.ctl_addr, ctl);
163         eieio();
164         in_be32((void *)(hwif->dma_base + 0x01c));
165         eieio();
166 }
167
168 static void scc_ide_insw(unsigned long port, void *addr, u32 count)
169 {
170         u16 *ptr = (u16 *)addr;
171         while (count--) {
172                 *ptr++ = le16_to_cpu(in_be32((void*)port));
173         }
174 }
175
176 static void scc_ide_insl(unsigned long port, void *addr, u32 count)
177 {
178         u16 *ptr = (u16 *)addr;
179         while (count--) {
180                 *ptr++ = le16_to_cpu(in_be32((void*)port));
181                 *ptr++ = le16_to_cpu(in_be32((void*)port));
182         }
183 }
184
185 static void scc_ide_outb(u8 addr, unsigned long port)
186 {
187         out_be32((void*)port, addr);
188 }
189
190 static void
191 scc_ide_outsw(unsigned long port, void *addr, u32 count)
192 {
193         u16 *ptr = (u16 *)addr;
194         while (count--) {
195                 out_be32((void*)port, cpu_to_le16(*ptr++));
196         }
197 }
198
199 static void
200 scc_ide_outsl(unsigned long port, void *addr, u32 count)
201 {
202         u16 *ptr = (u16 *)addr;
203         while (count--) {
204                 out_be32((void*)port, cpu_to_le16(*ptr++));
205                 out_be32((void*)port, cpu_to_le16(*ptr++));
206         }
207 }
208
209 /**
210  *      scc_set_pio_mode        -       set host controller for PIO mode
211  *      @drive: drive
212  *      @pio: PIO mode number
213  *
214  *      Load the timing settings for this device mode into the
215  *      controller.
216  */
217
218 static void scc_set_pio_mode(ide_drive_t *drive, const u8 pio)
219 {
220         ide_hwif_t *hwif = drive->hwif;
221         struct scc_ports *ports = ide_get_hwifdata(hwif);
222         unsigned long ctl_base = ports->ctl;
223         unsigned long cckctrl_port = ctl_base + 0xff0;
224         unsigned long piosht_port = ctl_base + 0x000;
225         unsigned long pioct_port = ctl_base + 0x004;
226         unsigned long reg;
227         int offset;
228
229         reg = in_be32((void __iomem *)cckctrl_port);
230         if (reg & CCKCTRL_ATACLKOEN) {
231                 offset = 1; /* 133MHz */
232         } else {
233                 offset = 0; /* 100MHz */
234         }
235         reg = JCHSTtbl[offset][pio] << 16 | JCHHTtbl[offset][pio];
236         out_be32((void __iomem *)piosht_port, reg);
237         reg = JCHCTtbl[offset][pio];
238         out_be32((void __iomem *)pioct_port, reg);
239 }
240
241 /**
242  *      scc_set_dma_mode        -       set host controller for DMA mode
243  *      @drive: drive
244  *      @speed: DMA mode
245  *
246  *      Load the timing settings for this device mode into the
247  *      controller.
248  */
249
250 static void scc_set_dma_mode(ide_drive_t *drive, const u8 speed)
251 {
252         ide_hwif_t *hwif = drive->hwif;
253         struct scc_ports *ports = ide_get_hwifdata(hwif);
254         unsigned long ctl_base = ports->ctl;
255         unsigned long cckctrl_port = ctl_base + 0xff0;
256         unsigned long mdmact_port = ctl_base + 0x008;
257         unsigned long mcrcst_port = ctl_base + 0x00c;
258         unsigned long sdmact_port = ctl_base + 0x010;
259         unsigned long scrcst_port = ctl_base + 0x014;
260         unsigned long udenvt_port = ctl_base + 0x018;
261         unsigned long tdvhsel_port   = ctl_base + 0x020;
262         int is_slave = drive->dn & 1;
263         int offset, idx;
264         unsigned long reg;
265         unsigned long jcactsel;
266
267         reg = in_be32((void __iomem *)cckctrl_port);
268         if (reg & CCKCTRL_ATACLKOEN) {
269                 offset = 1; /* 133MHz */
270         } else {
271                 offset = 0; /* 100MHz */
272         }
273
274         idx = speed - XFER_UDMA_0;
275
276         jcactsel = JCACTSELtbl[offset][idx];
277         if (is_slave) {
278                 out_be32((void __iomem *)sdmact_port, JCHDCTxtbl[offset][idx]);
279                 out_be32((void __iomem *)scrcst_port, JCSTWTxtbl[offset][idx]);
280                 jcactsel = jcactsel << 2;
281                 out_be32((void __iomem *)tdvhsel_port, (in_be32((void __iomem *)tdvhsel_port) & ~TDVHSEL_SLAVE) | jcactsel);
282         } else {
283                 out_be32((void __iomem *)mdmact_port, JCHDCTxtbl[offset][idx]);
284                 out_be32((void __iomem *)mcrcst_port, JCSTWTxtbl[offset][idx]);
285                 out_be32((void __iomem *)tdvhsel_port, (in_be32((void __iomem *)tdvhsel_port) & ~TDVHSEL_MASTER) | jcactsel);
286         }
287         reg = JCTSStbl[offset][idx] << 16 | JCENVTtbl[offset][idx];
288         out_be32((void __iomem *)udenvt_port, reg);
289 }
290
291 static void scc_dma_host_set(ide_drive_t *drive, int on)
292 {
293         ide_hwif_t *hwif = drive->hwif;
294         u8 unit = drive->dn & 1;
295         u8 dma_stat = scc_dma_sff_read_status(hwif);
296
297         if (on)
298                 dma_stat |= (1 << (5 + unit));
299         else
300                 dma_stat &= ~(1 << (5 + unit));
301
302         scc_ide_outb(dma_stat, hwif->dma_base + 4);
303 }
304
305 /**
306  *      scc_dma_setup   -       begin a DMA phase
307  *      @drive: target device
308  *      @cmd: command
309  *
310  *      Build an IDE DMA PRD (IDE speak for scatter gather table)
311  *      and then set up the DMA transfer registers.
312  *
313  *      Returns 0 on success. If a PIO fallback is required then 1
314  *      is returned.
315  */
316
317 static int scc_dma_setup(ide_drive_t *drive, struct ide_cmd *cmd)
318 {
319         ide_hwif_t *hwif = drive->hwif;
320         u32 rw = (cmd->tf_flags & IDE_TFLAG_WRITE) ? 0 : ATA_DMA_WR;
321         u8 dma_stat;
322
323         /* fall back to pio! */
324         if (ide_build_dmatable(drive, cmd) == 0) {
325                 ide_map_sg(drive, cmd);
326                 return 1;
327         }
328
329         /* PRD table */
330         out_be32((void __iomem *)(hwif->dma_base + 8), hwif->dmatable_dma);
331
332         /* specify r/w */
333         out_be32((void __iomem *)hwif->dma_base, rw);
334
335         /* read DMA status for INTR & ERROR flags */
336         dma_stat = scc_dma_sff_read_status(hwif);
337
338         /* clear INTR & ERROR flags */
339         out_be32((void __iomem *)(hwif->dma_base + 4), dma_stat | 6);
340         drive->waiting_for_dma = 1;
341         return 0;
342 }
343
344 static void scc_dma_start(ide_drive_t *drive)
345 {
346         ide_hwif_t *hwif = drive->hwif;
347         u8 dma_cmd = scc_ide_inb(hwif->dma_base);
348
349         /* start DMA */
350         scc_ide_outb(dma_cmd | 1, hwif->dma_base);
351         wmb();
352 }
353
354 static int __scc_dma_end(ide_drive_t *drive)
355 {
356         ide_hwif_t *hwif = drive->hwif;
357         u8 dma_stat, dma_cmd;
358
359         drive->waiting_for_dma = 0;
360         /* get DMA command mode */
361         dma_cmd = scc_ide_inb(hwif->dma_base);
362         /* stop DMA */
363         scc_ide_outb(dma_cmd & ~1, hwif->dma_base);
364         /* get DMA status */
365         dma_stat = scc_dma_sff_read_status(hwif);
366         /* clear the INTR & ERROR bits */
367         scc_ide_outb(dma_stat | 6, hwif->dma_base + 4);
368         /* verify good DMA status */
369         wmb();
370         return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
371 }
372
373 /**
374  *      scc_dma_end     -       Stop DMA
375  *      @drive: IDE drive
376  *
377  *      Check and clear INT Status register.
378  *      Then call __scc_dma_end().
379  */
380
381 static int scc_dma_end(ide_drive_t *drive)
382 {
383         ide_hwif_t *hwif = drive->hwif;
384         void __iomem *dma_base = (void __iomem *)hwif->dma_base;
385         unsigned long intsts_port = hwif->dma_base + 0x014;
386         u32 reg;
387         int dma_stat, data_loss = 0;
388         static int retry = 0;
389
390         /* errata A308 workaround: Step5 (check data loss) */
391         /* We don't check non ide_disk because it is limited to UDMA4 */
392         if (!(in_be32((void __iomem *)hwif->io_ports.ctl_addr)
393               & ATA_ERR) &&
394             drive->media == ide_disk && drive->current_speed > XFER_UDMA_4) {
395                 reg = in_be32((void __iomem *)intsts_port);
396                 if (!(reg & INTSTS_ACTEINT)) {
397                         printk(KERN_WARNING "%s: operation failed (transfer data loss)\n",
398                                drive->name);
399                         data_loss = 1;
400                         if (retry++) {
401                                 struct request *rq = hwif->rq;
402                                 ide_drive_t *drive;
403                                 int i;
404
405                                 /* ERROR_RESET and drive->crc_count are needed
406                                  * to reduce DMA transfer mode in retry process.
407                                  */
408                                 if (rq)
409                                         rq->errors |= ERROR_RESET;
410
411                                 ide_port_for_each_dev(i, drive, hwif)
412                                         drive->crc_count++;
413                         }
414                 }
415         }
416
417         while (1) {
418                 reg = in_be32((void __iomem *)intsts_port);
419
420                 if (reg & INTSTS_SERROR) {
421                         printk(KERN_WARNING "%s: SERROR\n", SCC_PATA_NAME);
422                         out_be32((void __iomem *)intsts_port, INTSTS_SERROR|INTSTS_BMSINT);
423
424                         out_be32(dma_base, in_be32(dma_base) & ~QCHCD_IOS_SS);
425                         continue;
426                 }
427
428                 if (reg & INTSTS_PRERR) {
429                         u32 maea0, maec0;
430                         unsigned long ctl_base = hwif->config_data;
431
432                         maea0 = in_be32((void __iomem *)(ctl_base + 0xF50));
433                         maec0 = in_be32((void __iomem *)(ctl_base + 0xF54));
434
435                         printk(KERN_WARNING "%s: PRERR [addr:%x cmd:%x]\n", SCC_PATA_NAME, maea0, maec0);
436
437                         out_be32((void __iomem *)intsts_port, INTSTS_PRERR|INTSTS_BMSINT);
438
439                         out_be32(dma_base, in_be32(dma_base) & ~QCHCD_IOS_SS);
440                         continue;
441                 }
442
443                 if (reg & INTSTS_RERR) {
444                         printk(KERN_WARNING "%s: Response Error\n", SCC_PATA_NAME);
445                         out_be32((void __iomem *)intsts_port, INTSTS_RERR|INTSTS_BMSINT);
446
447                         out_be32(dma_base, in_be32(dma_base) & ~QCHCD_IOS_SS);
448                         continue;
449                 }
450
451                 if (reg & INTSTS_ICERR) {
452                         out_be32(dma_base, in_be32(dma_base) & ~QCHCD_IOS_SS);
453
454                         printk(KERN_WARNING "%s: Illegal Configuration\n", SCC_PATA_NAME);
455                         out_be32((void __iomem *)intsts_port, INTSTS_ICERR|INTSTS_BMSINT);
456                         continue;
457                 }
458
459                 if (reg & INTSTS_BMSINT) {
460                         printk(KERN_WARNING "%s: Internal Bus Error\n", SCC_PATA_NAME);
461                         out_be32((void __iomem *)intsts_port, INTSTS_BMSINT);
462
463                         ide_do_reset(drive);
464                         continue;
465                 }
466
467                 if (reg & INTSTS_BMHE) {
468                         out_be32((void __iomem *)intsts_port, INTSTS_BMHE);
469                         continue;
470                 }
471
472                 if (reg & INTSTS_ACTEINT) {
473                         out_be32((void __iomem *)intsts_port, INTSTS_ACTEINT);
474                         continue;
475                 }
476
477                 if (reg & INTSTS_IOIRQS) {
478                         out_be32((void __iomem *)intsts_port, INTSTS_IOIRQS);
479                         continue;
480                 }
481                 break;
482         }
483
484         dma_stat = __scc_dma_end(drive);
485         if (data_loss)
486                 dma_stat |= 2; /* emulate DMA error (to retry command) */
487         return dma_stat;
488 }
489
490 /* returns 1 if dma irq issued, 0 otherwise */
491 static int scc_dma_test_irq(ide_drive_t *drive)
492 {
493         ide_hwif_t *hwif = drive->hwif;
494         u32 int_stat = in_be32((void __iomem *)hwif->dma_base + 0x014);
495
496         /* SCC errata A252,A308 workaround: Step4 */
497         if ((in_be32((void __iomem *)hwif->io_ports.ctl_addr)
498              & ATA_ERR) &&
499             (int_stat & INTSTS_INTRQ))
500                 return 1;
501
502         /* SCC errata A308 workaround: Step5 (polling IOIRQS) */
503         if (int_stat & INTSTS_IOIRQS)
504                 return 1;
505
506         return 0;
507 }
508
509 static u8 scc_udma_filter(ide_drive_t *drive)
510 {
511         ide_hwif_t *hwif = drive->hwif;
512         u8 mask = hwif->ultra_mask;
513
514         /* errata A308 workaround: limit non ide_disk drive to UDMA4 */
515         if ((drive->media != ide_disk) && (mask & 0xE0)) {
516                 printk(KERN_INFO "%s: limit %s to UDMA4\n",
517                        SCC_PATA_NAME, drive->name);
518                 mask = ATA_UDMA4;
519         }
520
521         return mask;
522 }
523
524 /**
525  *      setup_mmio_scc  -       map CTRL/BMID region
526  *      @dev: PCI device we are configuring
527  *      @name: device name
528  *
529  */
530
531 static int setup_mmio_scc (struct pci_dev *dev, const char *name)
532 {
533         void __iomem *ctl_addr;
534         void __iomem *dma_addr;
535         int i, ret;
536
537         for (i = 0; i < MAX_HWIFS; i++) {
538                 if (scc_ports[i].ctl == 0)
539                         break;
540         }
541         if (i >= MAX_HWIFS)
542                 return -ENOMEM;
543
544         ret = pci_request_selected_regions(dev, (1 << 2) - 1, name);
545         if (ret < 0) {
546                 printk(KERN_ERR "%s: can't reserve resources\n", name);
547                 return ret;
548         }
549
550         ctl_addr = pci_ioremap_bar(dev, 0);
551         if (!ctl_addr)
552                 goto fail_0;
553
554         dma_addr = pci_ioremap_bar(dev, 1);
555         if (!dma_addr)
556                 goto fail_1;
557
558         pci_set_master(dev);
559         scc_ports[i].ctl = (unsigned long)ctl_addr;
560         scc_ports[i].dma = (unsigned long)dma_addr;
561         pci_set_drvdata(dev, (void *) &scc_ports[i]);
562
563         return 1;
564
565  fail_1:
566         iounmap(ctl_addr);
567  fail_0:
568         return -ENOMEM;
569 }
570
571 static int scc_ide_setup_pci_device(struct pci_dev *dev,
572                                     const struct ide_port_info *d)
573 {
574         struct scc_ports *ports = pci_get_drvdata(dev);
575         struct ide_host *host;
576         hw_regs_t hw, *hws[] = { &hw, NULL, NULL, NULL };
577         int i, rc;
578
579         memset(&hw, 0, sizeof(hw));
580         for (i = 0; i <= 8; i++)
581                 hw.io_ports_array[i] = ports->dma + 0x20 + i * 4;
582         hw.irq = dev->irq;
583         hw.dev = &dev->dev;
584         hw.chipset = ide_pci;
585
586         rc = ide_host_add(d, hws, &host);
587         if (rc)
588                 return rc;
589
590         ports->host = host;
591
592         return 0;
593 }
594
595 /**
596  *      init_setup_scc  -       set up an SCC PATA Controller
597  *      @dev: PCI device
598  *      @d: IDE port info
599  *
600  *      Perform the initial set up for this device.
601  */
602
603 static int __devinit init_setup_scc(struct pci_dev *dev,
604                                     const struct ide_port_info *d)
605 {
606         unsigned long ctl_base;
607         unsigned long dma_base;
608         unsigned long cckctrl_port;
609         unsigned long intmask_port;
610         unsigned long mode_port;
611         unsigned long ecmode_port;
612         u32 reg = 0;
613         struct scc_ports *ports;
614         int rc;
615
616         rc = pci_enable_device(dev);
617         if (rc)
618                 goto end;
619
620         rc = setup_mmio_scc(dev, d->name);
621         if (rc < 0)
622                 goto end;
623
624         ports = pci_get_drvdata(dev);
625         ctl_base = ports->ctl;
626         dma_base = ports->dma;
627         cckctrl_port = ctl_base + 0xff0;
628         intmask_port = dma_base + 0x010;
629         mode_port = ctl_base + 0x024;
630         ecmode_port = ctl_base + 0xf00;
631
632         /* controller initialization */
633         reg = 0;
634         out_be32((void*)cckctrl_port, reg);
635         reg |= CCKCTRL_ATACLKOEN;
636         out_be32((void*)cckctrl_port, reg);
637         reg |= CCKCTRL_LCLKEN | CCKCTRL_OCLKEN;
638         out_be32((void*)cckctrl_port, reg);
639         reg |= CCKCTRL_CRST;
640         out_be32((void*)cckctrl_port, reg);
641
642         for (;;) {
643                 reg = in_be32((void*)cckctrl_port);
644                 if (reg & CCKCTRL_CRST)
645                         break;
646                 udelay(5000);
647         }
648
649         reg |= CCKCTRL_ATARESET;
650         out_be32((void*)cckctrl_port, reg);
651
652         out_be32((void*)ecmode_port, ECMODE_VALUE);
653         out_be32((void*)mode_port, MODE_JCUSFEN);
654         out_be32((void*)intmask_port, INTMASK_MSK);
655
656         rc = scc_ide_setup_pci_device(dev, d);
657
658  end:
659         return rc;
660 }
661
662 static void scc_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
663 {
664         struct ide_io_ports *io_ports = &drive->hwif->io_ports;
665         struct ide_taskfile *tf = &cmd->tf;
666         u8 HIHI = (cmd->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
667
668         if (cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
669                 HIHI = 0xFF;
670
671         if (cmd->ftf_flags & IDE_FTFLAG_OUT_DATA)
672                 out_be32((void *)io_ports->data_addr,
673                          (tf->hob_data << 8) | tf->data);
674
675         if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
676                 scc_ide_outb(tf->hob_feature, io_ports->feature_addr);
677         if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
678                 scc_ide_outb(tf->hob_nsect, io_ports->nsect_addr);
679         if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
680                 scc_ide_outb(tf->hob_lbal, io_ports->lbal_addr);
681         if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
682                 scc_ide_outb(tf->hob_lbam, io_ports->lbam_addr);
683         if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
684                 scc_ide_outb(tf->hob_lbah, io_ports->lbah_addr);
685
686         if (cmd->tf_flags & IDE_TFLAG_OUT_FEATURE)
687                 scc_ide_outb(tf->feature, io_ports->feature_addr);
688         if (cmd->tf_flags & IDE_TFLAG_OUT_NSECT)
689                 scc_ide_outb(tf->nsect, io_ports->nsect_addr);
690         if (cmd->tf_flags & IDE_TFLAG_OUT_LBAL)
691                 scc_ide_outb(tf->lbal, io_ports->lbal_addr);
692         if (cmd->tf_flags & IDE_TFLAG_OUT_LBAM)
693                 scc_ide_outb(tf->lbam, io_ports->lbam_addr);
694         if (cmd->tf_flags & IDE_TFLAG_OUT_LBAH)
695                 scc_ide_outb(tf->lbah, io_ports->lbah_addr);
696
697         if (cmd->tf_flags & IDE_TFLAG_OUT_DEVICE)
698                 scc_ide_outb((tf->device & HIHI) | drive->select,
699                              io_ports->device_addr);
700 }
701
702 static void scc_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
703 {
704         struct ide_io_ports *io_ports = &drive->hwif->io_ports;
705         struct ide_taskfile *tf = &cmd->tf;
706
707         if (cmd->ftf_flags & IDE_FTFLAG_IN_DATA) {
708                 u16 data = (u16)in_be32((void *)io_ports->data_addr);
709
710                 tf->data = data & 0xff;
711                 tf->hob_data = (data >> 8) & 0xff;
712         }
713
714         /* be sure we're looking at the low order bits */
715         scc_ide_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
716
717         if (cmd->tf_flags & IDE_TFLAG_IN_FEATURE)
718                 tf->feature = scc_ide_inb(io_ports->feature_addr);
719         if (cmd->tf_flags & IDE_TFLAG_IN_NSECT)
720                 tf->nsect  = scc_ide_inb(io_ports->nsect_addr);
721         if (cmd->tf_flags & IDE_TFLAG_IN_LBAL)
722                 tf->lbal   = scc_ide_inb(io_ports->lbal_addr);
723         if (cmd->tf_flags & IDE_TFLAG_IN_LBAM)
724                 tf->lbam   = scc_ide_inb(io_ports->lbam_addr);
725         if (cmd->tf_flags & IDE_TFLAG_IN_LBAH)
726                 tf->lbah   = scc_ide_inb(io_ports->lbah_addr);
727         if (cmd->tf_flags & IDE_TFLAG_IN_DEVICE)
728                 tf->device = scc_ide_inb(io_ports->device_addr);
729
730         if (cmd->tf_flags & IDE_TFLAG_LBA48) {
731                 scc_ide_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
732
733                 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
734                         tf->hob_feature = scc_ide_inb(io_ports->feature_addr);
735                 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
736                         tf->hob_nsect   = scc_ide_inb(io_ports->nsect_addr);
737                 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
738                         tf->hob_lbal    = scc_ide_inb(io_ports->lbal_addr);
739                 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
740                         tf->hob_lbam    = scc_ide_inb(io_ports->lbam_addr);
741                 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
742                         tf->hob_lbah    = scc_ide_inb(io_ports->lbah_addr);
743         }
744 }
745
746 static void scc_input_data(ide_drive_t *drive, struct ide_cmd *cmd,
747                            void *buf, unsigned int len)
748 {
749         unsigned long data_addr = drive->hwif->io_ports.data_addr;
750
751         len++;
752
753         if (drive->io_32bit) {
754                 scc_ide_insl(data_addr, buf, len / 4);
755
756                 if ((len & 3) >= 2)
757                         scc_ide_insw(data_addr, (u8 *)buf + (len & ~3), 1);
758         } else
759                 scc_ide_insw(data_addr, buf, len / 2);
760 }
761
762 static void scc_output_data(ide_drive_t *drive,  struct ide_cmd *cmd,
763                             void *buf, unsigned int len)
764 {
765         unsigned long data_addr = drive->hwif->io_ports.data_addr;
766
767         len++;
768
769         if (drive->io_32bit) {
770                 scc_ide_outsl(data_addr, buf, len / 4);
771
772                 if ((len & 3) >= 2)
773                         scc_ide_outsw(data_addr, (u8 *)buf + (len & ~3), 1);
774         } else
775                 scc_ide_outsw(data_addr, buf, len / 2);
776 }
777
778 /**
779  *      init_mmio_iops_scc      -       set up the iops for MMIO
780  *      @hwif: interface to set up
781  *
782  */
783
784 static void __devinit init_mmio_iops_scc(ide_hwif_t *hwif)
785 {
786         struct pci_dev *dev = to_pci_dev(hwif->dev);
787         struct scc_ports *ports = pci_get_drvdata(dev);
788         unsigned long dma_base = ports->dma;
789
790         ide_set_hwifdata(hwif, ports);
791
792         hwif->dma_base = dma_base;
793         hwif->config_data = ports->ctl;
794 }
795
796 /**
797  *      init_iops_scc   -       set up iops
798  *      @hwif: interface to set up
799  *
800  *      Do the basic setup for the SCC hardware interface
801  *      and then do the MMIO setup.
802  */
803
804 static void __devinit init_iops_scc(ide_hwif_t *hwif)
805 {
806         struct pci_dev *dev = to_pci_dev(hwif->dev);
807
808         hwif->hwif_data = NULL;
809         if (pci_get_drvdata(dev) == NULL)
810                 return;
811         init_mmio_iops_scc(hwif);
812 }
813
814 static int __devinit scc_init_dma(ide_hwif_t *hwif,
815                                   const struct ide_port_info *d)
816 {
817         return ide_allocate_dma_engine(hwif);
818 }
819
820 static u8 scc_cable_detect(ide_hwif_t *hwif)
821 {
822         return ATA_CBL_PATA80;
823 }
824
825 /**
826  *      init_hwif_scc   -       set up hwif
827  *      @hwif: interface to set up
828  *
829  *      We do the basic set up of the interface structure. The SCC
830  *      requires several custom handlers so we override the default
831  *      ide DMA handlers appropriately.
832  */
833
834 static void __devinit init_hwif_scc(ide_hwif_t *hwif)
835 {
836         /* PTERADD */
837         out_be32((void __iomem *)(hwif->dma_base + 0x018), hwif->dmatable_dma);
838
839         if (in_be32((void __iomem *)(hwif->config_data + 0xff0)) & CCKCTRL_ATACLKOEN)
840                 hwif->ultra_mask = ATA_UDMA6; /* 133MHz */
841         else
842                 hwif->ultra_mask = ATA_UDMA5; /* 100MHz */
843 }
844
845 static const struct ide_tp_ops scc_tp_ops = {
846         .exec_command           = scc_exec_command,
847         .read_status            = scc_read_status,
848         .read_altstatus         = scc_read_altstatus,
849
850         .set_irq                = scc_set_irq,
851
852         .tf_load                = scc_tf_load,
853         .tf_read                = scc_tf_read,
854
855         .input_data             = scc_input_data,
856         .output_data            = scc_output_data,
857 };
858
859 static const struct ide_port_ops scc_port_ops = {
860         .set_pio_mode           = scc_set_pio_mode,
861         .set_dma_mode           = scc_set_dma_mode,
862         .udma_filter            = scc_udma_filter,
863         .cable_detect           = scc_cable_detect,
864 };
865
866 static const struct ide_dma_ops scc_dma_ops = {
867         .dma_host_set           = scc_dma_host_set,
868         .dma_setup              = scc_dma_setup,
869         .dma_start              = scc_dma_start,
870         .dma_end                = scc_dma_end,
871         .dma_test_irq           = scc_dma_test_irq,
872         .dma_lost_irq           = ide_dma_lost_irq,
873         .dma_timer_expiry       = ide_dma_sff_timer_expiry,
874         .dma_sff_read_status    = scc_dma_sff_read_status,
875 };
876
877 static const struct ide_port_info scc_chipset __devinitdata = {
878         .name           = "sccIDE",
879         .init_iops      = init_iops_scc,
880         .init_dma       = scc_init_dma,
881         .init_hwif      = init_hwif_scc,
882         .tp_ops         = &scc_tp_ops,
883         .port_ops       = &scc_port_ops,
884         .dma_ops        = &scc_dma_ops,
885         .host_flags     = IDE_HFLAG_SINGLE,
886         .irq_flags      = IRQF_SHARED,
887         .pio_mask       = ATA_PIO4,
888 };
889
890 /**
891  *      scc_init_one    -       pci layer discovery entry
892  *      @dev: PCI device
893  *      @id: ident table entry
894  *
895  *      Called by the PCI code when it finds an SCC PATA controller.
896  *      We then use the IDE PCI generic helper to do most of the work.
897  */
898
899 static int __devinit scc_init_one(struct pci_dev *dev, const struct pci_device_id *id)
900 {
901         return init_setup_scc(dev, &scc_chipset);
902 }
903
904 /**
905  *      scc_remove      -       pci layer remove entry
906  *      @dev: PCI device
907  *
908  *      Called by the PCI code when it removes an SCC PATA controller.
909  */
910
911 static void __devexit scc_remove(struct pci_dev *dev)
912 {
913         struct scc_ports *ports = pci_get_drvdata(dev);
914         struct ide_host *host = ports->host;
915
916         ide_host_remove(host);
917
918         iounmap((void*)ports->dma);
919         iounmap((void*)ports->ctl);
920         pci_release_selected_regions(dev, (1 << 2) - 1);
921         memset(ports, 0, sizeof(*ports));
922 }
923
924 static const struct pci_device_id scc_pci_tbl[] = {
925         { PCI_VDEVICE(TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SCC_ATA), 0 },
926         { 0, },
927 };
928 MODULE_DEVICE_TABLE(pci, scc_pci_tbl);
929
930 static struct pci_driver scc_pci_driver = {
931         .name = "SCC IDE",
932         .id_table = scc_pci_tbl,
933         .probe = scc_init_one,
934         .remove = __devexit_p(scc_remove),
935 };
936
937 static int scc_ide_init(void)
938 {
939         return ide_pci_register_driver(&scc_pci_driver);
940 }
941
942 module_init(scc_ide_init);
943 /* -- No exit code?
944 static void scc_ide_exit(void)
945 {
946         ide_pci_unregister_driver(&scc_pci_driver);
947 }
948 module_exit(scc_ide_exit);
949  */
950
951
952 MODULE_DESCRIPTION("PCI driver module for Toshiba SCC IDE");
953 MODULE_LICENSE("GPL");