blob: 796289cf97e0fb5234f0eb49b582a644069f144e [file] [log] [blame]
Atsushi Nemoto28502842008-10-23 23:22:08 +02001/*
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
20static 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
Atsushi Nemoto630a8b22008-11-02 21:40:09 +010042 /* -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 }
Atsushi Nemoto28502842008-10-23 23:22:08 +020049 pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
50 ebus_ch, cycle, wt, shwt);
51
Atsushi Nemoto630a8b22008-11-02 21:40:09 +010052 __raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt,
Atsushi Nemoto28502842008-10-23 23:22:08 +020053 &tx4938_ebuscptr->cr[ebus_ch]);
54}
55
56static 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) */
72static u8 tx4938ide_inb(unsigned long port)
73{
74 return __raw_readb((void __iomem *)port);
75}
76
77static void tx4938ide_outb(u8 value, unsigned long port)
78{
79 __raw_writeb(value, (void __iomem *)port);
80}
81
82static 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
126static 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
174static 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
186static 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
200static 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
217static const struct ide_port_ops tx4938ide_port_ops = {
218 .set_pio_mode = tx4938ide_set_pio_mode,
219};
220
221static 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
230static 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;
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100238 unsigned long mapbase, mapctl;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200239 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,
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100252 8 << pdata->ioport_shift);
253 mapctl = (unsigned long)devm_ioremap(&pdev->dev,
254 res->start + 0x10000 +
255 (6 << pdata->ioport_shift),
256 1 << pdata->ioport_shift);
257 if (!mapbase || !mapctl)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200258 return -EBUSY;
259
260 memset(&hw, 0, sizeof(hw));
261 if (pdata->ioport_shift) {
262 unsigned long port = mapbase;
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100263 unsigned long ctl = mapctl;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200264
265 hw.io_ports_array[0] = port;
266#ifdef __BIG_ENDIAN
267 port++;
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100268 ctl++;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200269#endif
270 for (i = 1; i <= 7; i++)
271 hw.io_ports_array[i] =
272 port + (i << pdata->ioport_shift);
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100273 hw.io_ports.ctl_addr = ctl;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200274 } else
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100275 ide_std_init_ports(&hw, mapbase, mapctl);
Atsushi Nemoto28502842008-10-23 23:22:08 +0200276 hw.irq = irq;
277 hw.dev = &pdev->dev;
278
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100279 pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
280 mapbase, mapctl, hw.irq);
Atsushi Nemoto28502842008-10-23 23:22:08 +0200281 if (pdata->gbus_clock)
282 tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
283 else
284 d.port_ops = NULL;
285 ret = ide_host_add(&d, hws, &host);
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100286 if (!ret)
287 platform_set_drvdata(pdev, host);
288 return ret;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200289}
290
291static int __exit tx4938ide_remove(struct platform_device *pdev)
292{
293 struct ide_host *host = platform_get_drvdata(pdev);
294
295 ide_host_remove(host);
296 return 0;
297}
298
299static struct platform_driver tx4938ide_driver = {
300 .driver = {
301 .name = "tx4938ide",
302 .owner = THIS_MODULE,
303 },
304 .remove = __exit_p(tx4938ide_remove),
305};
306
307static int __init tx4938ide_init(void)
308{
309 return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe);
310}
311
312static void __exit tx4938ide_exit(void)
313{
314 platform_driver_unregister(&tx4938ide_driver);
315}
316
317module_init(tx4938ide_init);
318module_exit(tx4938ide_exit);
319
320MODULE_DESCRIPTION("TX4938 internal IDE driver");
321MODULE_LICENSE("GPL");
322MODULE_ALIAS("platform:tx4938ide");