tx4938ide: Check minimum cycle time and SHWT range (v2)
[pandora-kernel.git] / drivers / ide / tx4938ide.c
1 /*
2  * TX4938 internal IDE driver
3  * Based on tx4939ide.c.
4  *
5  * This file is subject to the terms and conditions of the GNU General Public
6  * License.  See the file "COPYING" in the main directory of this archive
7  * for more details.
8  *
9  * (C) Copyright TOSHIBA CORPORATION 2005-2007
10  */
11
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/ide.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
17 #include <linux/io.h>
18 #include <asm/txx9/tx4938.h>
19
20 static void tx4938ide_tune_ebusc(unsigned int ebus_ch,
21                                  unsigned int gbus_clock,
22                                  u8 pio)
23 {
24         struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
25         u64 cr = __raw_readq(&tx4938_ebuscptr->cr[ebus_ch]);
26         unsigned int sp = (cr >> 4) & 3;
27         unsigned int clock = gbus_clock / (4 - sp);
28         unsigned int cycle = 1000000000 / clock;
29         unsigned int wt, shwt;
30
31         /* Minimum DIOx- active time */
32         wt = DIV_ROUND_UP(t->act8b, cycle) - 2;
33         /* IORDY setup time: 35ns */
34         wt = max(wt, DIV_ROUND_UP(35, cycle));
35         /* actual wait-cycle is max(wt & ~1, 1) */
36         if (wt > 2 && (wt & 1))
37                 wt++;
38         wt &= ~1;
39         /* Address-valid to DIOR/DIOW setup */
40         shwt = DIV_ROUND_UP(t->setup, cycle);
41
42         /* -DIOx recovery time (SHWT * 4) and cycle time requirement */
43         while ((shwt * 4 + wt + (wt ? 2 : 3)) * cycle < t->cycle)
44                 shwt++;
45         if (shwt > 7) {
46                 pr_warning("tx4938ide: SHWT violation (%d)\n", shwt);
47                 shwt = 7;
48         }
49         pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
50                  ebus_ch, cycle, wt, shwt);
51
52         __raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt,
53                      &tx4938_ebuscptr->cr[ebus_ch]);
54 }
55
56 static void tx4938ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
57 {
58         ide_hwif_t *hwif = drive->hwif;
59         struct tx4938ide_platform_info *pdata = hwif->dev->platform_data;
60         u8 safe = pio;
61         ide_drive_t *pair;
62
63         pair = ide_get_pair_dev(drive);
64         if (pair)
65                 safe = min(safe, ide_get_best_pio_mode(pair, 255, 5));
66         tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, safe);
67 }
68
69 #ifdef __BIG_ENDIAN
70
71 /* custom iops (independent from SWAP_IO_SPACE) */
72 static u8 tx4938ide_inb(unsigned long port)
73 {
74         return __raw_readb((void __iomem *)port);
75 }
76
77 static void tx4938ide_outb(u8 value, unsigned long port)
78 {
79         __raw_writeb(value, (void __iomem *)port);
80 }
81
82 static void tx4938ide_tf_load(ide_drive_t *drive, ide_task_t *task)
83 {
84         ide_hwif_t *hwif = drive->hwif;
85         struct ide_io_ports *io_ports = &hwif->io_ports;
86         struct ide_taskfile *tf = &task->tf;
87         u8 HIHI = task->tf_flags & IDE_TFLAG_LBA48 ? 0xE0 : 0xEF;
88
89         if (task->tf_flags & IDE_TFLAG_FLAGGED)
90                 HIHI = 0xFF;
91
92         if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
93                 u16 data = (tf->hob_data << 8) | tf->data;
94
95                 /* no endian swap */
96                 __raw_writew(data, (void __iomem *)io_ports->data_addr);
97         }
98
99         if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
100                 tx4938ide_outb(tf->hob_feature, io_ports->feature_addr);
101         if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
102                 tx4938ide_outb(tf->hob_nsect, io_ports->nsect_addr);
103         if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
104                 tx4938ide_outb(tf->hob_lbal, io_ports->lbal_addr);
105         if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
106                 tx4938ide_outb(tf->hob_lbam, io_ports->lbam_addr);
107         if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
108                 tx4938ide_outb(tf->hob_lbah, io_ports->lbah_addr);
109
110         if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
111                 tx4938ide_outb(tf->feature, io_ports->feature_addr);
112         if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
113                 tx4938ide_outb(tf->nsect, io_ports->nsect_addr);
114         if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
115                 tx4938ide_outb(tf->lbal, io_ports->lbal_addr);
116         if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
117                 tx4938ide_outb(tf->lbam, io_ports->lbam_addr);
118         if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
119                 tx4938ide_outb(tf->lbah, io_ports->lbah_addr);
120
121         if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
122                 tx4938ide_outb((tf->device & HIHI) | drive->select,
123                                io_ports->device_addr);
124 }
125
126 static void tx4938ide_tf_read(ide_drive_t *drive, ide_task_t *task)
127 {
128         ide_hwif_t *hwif = drive->hwif;
129         struct ide_io_ports *io_ports = &hwif->io_ports;
130         struct ide_taskfile *tf = &task->tf;
131
132         if (task->tf_flags & IDE_TFLAG_IN_DATA) {
133                 u16 data;
134
135                 /* no endian swap */
136                 data = __raw_readw((void __iomem *)io_ports->data_addr);
137                 tf->data = data & 0xff;
138                 tf->hob_data = (data >> 8) & 0xff;
139         }
140
141         /* be sure we're looking at the low order bits */
142         tx4938ide_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
143
144         if (task->tf_flags & IDE_TFLAG_IN_FEATURE)
145                 tf->feature = tx4938ide_inb(io_ports->feature_addr);
146         if (task->tf_flags & IDE_TFLAG_IN_NSECT)
147                 tf->nsect  = tx4938ide_inb(io_ports->nsect_addr);
148         if (task->tf_flags & IDE_TFLAG_IN_LBAL)
149                 tf->lbal   = tx4938ide_inb(io_ports->lbal_addr);
150         if (task->tf_flags & IDE_TFLAG_IN_LBAM)
151                 tf->lbam   = tx4938ide_inb(io_ports->lbam_addr);
152         if (task->tf_flags & IDE_TFLAG_IN_LBAH)
153                 tf->lbah   = tx4938ide_inb(io_ports->lbah_addr);
154         if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
155                 tf->device = tx4938ide_inb(io_ports->device_addr);
156
157         if (task->tf_flags & IDE_TFLAG_LBA48) {
158                 tx4938ide_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
159
160                 if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
161                         tf->hob_feature =
162                                 tx4938ide_inb(io_ports->feature_addr);
163                 if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
164                         tf->hob_nsect   = tx4938ide_inb(io_ports->nsect_addr);
165                 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
166                         tf->hob_lbal    = tx4938ide_inb(io_ports->lbal_addr);
167                 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
168                         tf->hob_lbam    = tx4938ide_inb(io_ports->lbam_addr);
169                 if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
170                         tf->hob_lbah    = tx4938ide_inb(io_ports->lbah_addr);
171         }
172 }
173
174 static void tx4938ide_input_data_swap(ide_drive_t *drive, struct request *rq,
175                                 void *buf, unsigned int len)
176 {
177         unsigned long port = drive->hwif->io_ports.data_addr;
178         unsigned short *ptr = buf;
179         unsigned int count = (len + 1) / 2;
180
181         while (count--)
182                 *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
183         __ide_flush_dcache_range((unsigned long)buf, count * 2);
184 }
185
186 static void tx4938ide_output_data_swap(ide_drive_t *drive, struct request *rq,
187                                 void *buf, unsigned int len)
188 {
189         unsigned long port = drive->hwif->io_ports.data_addr;
190         unsigned short *ptr = buf;
191         unsigned int count = (len + 1) / 2;
192
193         while (count--) {
194                 __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
195                 ptr++;
196         }
197         __ide_flush_dcache_range((unsigned long)buf, count * 2);
198 }
199
200 static const struct ide_tp_ops tx4938ide_tp_ops = {
201         .exec_command           = ide_exec_command,
202         .read_status            = ide_read_status,
203         .read_altstatus         = ide_read_altstatus,
204         .read_sff_dma_status    = ide_read_sff_dma_status,
205
206         .set_irq                = ide_set_irq,
207
208         .tf_load                = tx4938ide_tf_load,
209         .tf_read                = tx4938ide_tf_read,
210
211         .input_data             = tx4938ide_input_data_swap,
212         .output_data            = tx4938ide_output_data_swap,
213 };
214
215 #endif  /* __BIG_ENDIAN */
216
217 static const struct ide_port_ops tx4938ide_port_ops = {
218         .set_pio_mode = tx4938ide_set_pio_mode,
219 };
220
221 static const struct ide_port_info tx4938ide_port_info __initdata = {
222         .port_ops = &tx4938ide_port_ops,
223 #ifdef __BIG_ENDIAN
224         .tp_ops = &tx4938ide_tp_ops,
225 #endif
226         .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
227         .pio_mask = ATA_PIO5,
228 };
229
230 static int __init tx4938ide_probe(struct platform_device *pdev)
231 {
232         hw_regs_t hw;
233         hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
234         struct ide_host *host;
235         struct resource *res;
236         struct tx4938ide_platform_info *pdata = pdev->dev.platform_data;
237         int irq, ret, i;
238         unsigned long mapbase;
239         struct ide_port_info d = tx4938ide_port_info;
240
241         irq = platform_get_irq(pdev, 0);
242         if (irq < 0)
243                 return -ENODEV;
244         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
245         if (!res)
246                 return -ENODEV;
247
248         if (!devm_request_mem_region(&pdev->dev, res->start,
249                                      res->end - res->start + 1, "tx4938ide"))
250                 return -EBUSY;
251         mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
252                                               res->end - res->start + 1);
253         if (!mapbase)
254                 return -EBUSY;
255
256         memset(&hw, 0, sizeof(hw));
257         if (pdata->ioport_shift) {
258                 unsigned long port = mapbase;
259
260                 hw.io_ports_array[0] = port;
261 #ifdef __BIG_ENDIAN
262                 port++;
263 #endif
264                 for (i = 1; i <= 7; i++)
265                         hw.io_ports_array[i] =
266                                 port + (i << pdata->ioport_shift);
267                 hw.io_ports.ctl_addr =
268                         port + 0x10000 + (6 << pdata->ioport_shift);
269         } else
270                 ide_std_init_ports(&hw, mapbase, mapbase + 0x10006);
271         hw.irq = irq;
272         hw.dev = &pdev->dev;
273
274         pr_info("TX4938 IDE interface (base %#lx, irq %d)\n", mapbase, hw.irq);
275         if (pdata->gbus_clock)
276                 tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
277         else
278                 d.port_ops = NULL;
279         ret = ide_host_add(&d, hws, &host);
280         if (ret)
281                 return ret;
282         platform_set_drvdata(pdev, host);
283         return 0;
284 }
285
286 static int __exit tx4938ide_remove(struct platform_device *pdev)
287 {
288         struct ide_host *host = platform_get_drvdata(pdev);
289
290         ide_host_remove(host);
291         return 0;
292 }
293
294 static struct platform_driver tx4938ide_driver = {
295         .driver         = {
296                 .name   = "tx4938ide",
297                 .owner  = THIS_MODULE,
298         },
299         .remove = __exit_p(tx4938ide_remove),
300 };
301
302 static int __init tx4938ide_init(void)
303 {
304         return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe);
305 }
306
307 static void __exit tx4938ide_exit(void)
308 {
309         platform_driver_unregister(&tx4938ide_driver);
310 }
311
312 module_init(tx4938ide_init);
313 module_exit(tx4938ide_exit);
314
315 MODULE_DESCRIPTION("TX4938 internal IDE driver");
316 MODULE_LICENSE("GPL");
317 MODULE_ALIAS("platform:tx4938ide");