blob: 657a61890b1caef03d6ae1202fc888a16e624ff7 [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>
Bartlomiej Zolnierkiewicz15a453a2009-03-27 12:46:26 +010018
19#include <asm/ide.h>
Atsushi Nemoto28502842008-10-23 23:22:08 +020020#include <asm/txx9/tx4938.h>
21
22static void tx4938ide_tune_ebusc(unsigned int ebus_ch,
23 unsigned int gbus_clock,
24 u8 pio)
25{
26 struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
27 u64 cr = __raw_readq(&tx4938_ebuscptr->cr[ebus_ch]);
28 unsigned int sp = (cr >> 4) & 3;
29 unsigned int clock = gbus_clock / (4 - sp);
30 unsigned int cycle = 1000000000 / clock;
Atsushi Nemoto7afa0532008-11-02 21:40:10 +010031 unsigned int shwt;
32 int wt;
Atsushi Nemoto28502842008-10-23 23:22:08 +020033
34 /* Minimum DIOx- active time */
35 wt = DIV_ROUND_UP(t->act8b, cycle) - 2;
36 /* IORDY setup time: 35ns */
Atsushi Nemoto7afa0532008-11-02 21:40:10 +010037 wt = max_t(int, wt, DIV_ROUND_UP(35, cycle));
Atsushi Nemoto28502842008-10-23 23:22:08 +020038 /* actual wait-cycle is max(wt & ~1, 1) */
39 if (wt > 2 && (wt & 1))
40 wt++;
41 wt &= ~1;
42 /* Address-valid to DIOR/DIOW setup */
43 shwt = DIV_ROUND_UP(t->setup, cycle);
44
Atsushi Nemoto630a8b22008-11-02 21:40:09 +010045 /* -DIOx recovery time (SHWT * 4) and cycle time requirement */
46 while ((shwt * 4 + wt + (wt ? 2 : 3)) * cycle < t->cycle)
47 shwt++;
48 if (shwt > 7) {
49 pr_warning("tx4938ide: SHWT violation (%d)\n", shwt);
50 shwt = 7;
51 }
Atsushi Nemoto28502842008-10-23 23:22:08 +020052 pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
53 ebus_ch, cycle, wt, shwt);
54
Atsushi Nemoto630a8b22008-11-02 21:40:09 +010055 __raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt,
Atsushi Nemoto28502842008-10-23 23:22:08 +020056 &tx4938_ebuscptr->cr[ebus_ch]);
57}
58
59static void tx4938ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
60{
61 ide_hwif_t *hwif = drive->hwif;
62 struct tx4938ide_platform_info *pdata = hwif->dev->platform_data;
63 u8 safe = pio;
64 ide_drive_t *pair;
65
66 pair = ide_get_pair_dev(drive);
67 if (pair)
68 safe = min(safe, ide_get_best_pio_mode(pair, 255, 5));
69 tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, safe);
70}
71
72#ifdef __BIG_ENDIAN
73
74/* custom iops (independent from SWAP_IO_SPACE) */
75static u8 tx4938ide_inb(unsigned long port)
76{
77 return __raw_readb((void __iomem *)port);
78}
79
80static void tx4938ide_outb(u8 value, unsigned long port)
81{
82 __raw_writeb(value, (void __iomem *)port);
83}
84
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010085static void tx4938ide_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
Atsushi Nemoto28502842008-10-23 23:22:08 +020086{
87 ide_hwif_t *hwif = drive->hwif;
88 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010089 struct ide_taskfile *tf = &cmd->tf;
90 u8 HIHI = cmd->tf_flags & IDE_TFLAG_LBA48 ? 0xE0 : 0xEF;
Atsushi Nemoto28502842008-10-23 23:22:08 +020091
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010092 if (cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
Atsushi Nemoto28502842008-10-23 23:22:08 +020093 HIHI = 0xFF;
94
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +010095 if (cmd->ftf_flags & IDE_FTFLAG_OUT_DATA) {
Atsushi Nemoto28502842008-10-23 23:22:08 +020096 u16 data = (tf->hob_data << 8) | tf->data;
97
98 /* no endian swap */
99 __raw_writew(data, (void __iomem *)io_ports->data_addr);
100 }
101
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100102 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200103 tx4938ide_outb(tf->hob_feature, io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100104 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200105 tx4938ide_outb(tf->hob_nsect, io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100106 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200107 tx4938ide_outb(tf->hob_lbal, io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100108 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200109 tx4938ide_outb(tf->hob_lbam, io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100110 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200111 tx4938ide_outb(tf->hob_lbah, io_ports->lbah_addr);
112
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100113 if (cmd->tf_flags & IDE_TFLAG_OUT_FEATURE)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200114 tx4938ide_outb(tf->feature, io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100115 if (cmd->tf_flags & IDE_TFLAG_OUT_NSECT)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200116 tx4938ide_outb(tf->nsect, io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100117 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAL)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200118 tx4938ide_outb(tf->lbal, io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100119 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAM)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200120 tx4938ide_outb(tf->lbam, io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100121 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAH)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200122 tx4938ide_outb(tf->lbah, io_ports->lbah_addr);
123
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100124 if (cmd->tf_flags & IDE_TFLAG_OUT_DEVICE)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200125 tx4938ide_outb((tf->device & HIHI) | drive->select,
126 io_ports->device_addr);
127}
128
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100129static void tx4938ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200130{
131 ide_hwif_t *hwif = drive->hwif;
132 struct ide_io_ports *io_ports = &hwif->io_ports;
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100133 struct ide_taskfile *tf = &cmd->tf;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200134
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100135 if (cmd->ftf_flags & IDE_FTFLAG_IN_DATA) {
Atsushi Nemoto28502842008-10-23 23:22:08 +0200136 u16 data;
137
138 /* no endian swap */
139 data = __raw_readw((void __iomem *)io_ports->data_addr);
140 tf->data = data & 0xff;
141 tf->hob_data = (data >> 8) & 0xff;
142 }
143
144 /* be sure we're looking at the low order bits */
145 tx4938ide_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
146
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100147 if (cmd->tf_flags & IDE_TFLAG_IN_FEATURE)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200148 tf->feature = tx4938ide_inb(io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100149 if (cmd->tf_flags & IDE_TFLAG_IN_NSECT)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200150 tf->nsect = tx4938ide_inb(io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100151 if (cmd->tf_flags & IDE_TFLAG_IN_LBAL)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200152 tf->lbal = tx4938ide_inb(io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100153 if (cmd->tf_flags & IDE_TFLAG_IN_LBAM)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200154 tf->lbam = tx4938ide_inb(io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100155 if (cmd->tf_flags & IDE_TFLAG_IN_LBAH)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200156 tf->lbah = tx4938ide_inb(io_ports->lbah_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100157 if (cmd->tf_flags & IDE_TFLAG_IN_DEVICE)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200158 tf->device = tx4938ide_inb(io_ports->device_addr);
159
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100160 if (cmd->tf_flags & IDE_TFLAG_LBA48) {
Atsushi Nemoto28502842008-10-23 23:22:08 +0200161 tx4938ide_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
162
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100163 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200164 tf->hob_feature =
165 tx4938ide_inb(io_ports->feature_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100166 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200167 tf->hob_nsect = tx4938ide_inb(io_ports->nsect_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100168 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200169 tf->hob_lbal = tx4938ide_inb(io_ports->lbal_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100170 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200171 tf->hob_lbam = tx4938ide_inb(io_ports->lbam_addr);
Bartlomiej Zolnierkiewicz22aa4b32009-03-27 12:46:37 +0100172 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200173 tf->hob_lbah = tx4938ide_inb(io_ports->lbah_addr);
174 }
175}
176
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100177static void tx4938ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200178 void *buf, unsigned int len)
179{
180 unsigned long port = drive->hwif->io_ports.data_addr;
181 unsigned short *ptr = buf;
182 unsigned int count = (len + 1) / 2;
183
184 while (count--)
185 *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
Atsushi Nemotof26f6ce2008-12-29 20:27:29 +0100186 __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
Atsushi Nemoto28502842008-10-23 23:22:08 +0200187}
188
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +0100189static void tx4938ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200190 void *buf, unsigned int len)
191{
192 unsigned long port = drive->hwif->io_ports.data_addr;
193 unsigned short *ptr = buf;
194 unsigned int count = (len + 1) / 2;
195
196 while (count--) {
197 __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
198 ptr++;
199 }
Atsushi Nemotof26f6ce2008-12-29 20:27:29 +0100200 __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
Atsushi Nemoto28502842008-10-23 23:22:08 +0200201}
202
203static const struct ide_tp_ops tx4938ide_tp_ops = {
204 .exec_command = ide_exec_command,
205 .read_status = ide_read_status,
206 .read_altstatus = ide_read_altstatus,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200207
208 .set_irq = ide_set_irq,
209
210 .tf_load = tx4938ide_tf_load,
211 .tf_read = tx4938ide_tf_read,
212
213 .input_data = tx4938ide_input_data_swap,
214 .output_data = tx4938ide_output_data_swap,
215};
216
217#endif /* __BIG_ENDIAN */
218
219static const struct ide_port_ops tx4938ide_port_ops = {
Bartlomiej Zolnierkiewicz3ee86dc2009-01-02 16:12:46 +0100220 .set_pio_mode = tx4938ide_set_pio_mode,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200221};
222
223static const struct ide_port_info tx4938ide_port_info __initdata = {
Bartlomiej Zolnierkiewicz3ee86dc2009-01-02 16:12:46 +0100224 .port_ops = &tx4938ide_port_ops,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200225#ifdef __BIG_ENDIAN
Bartlomiej Zolnierkiewicz3ee86dc2009-01-02 16:12:46 +0100226 .tp_ops = &tx4938ide_tp_ops,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200227#endif
Bartlomiej Zolnierkiewicz3ee86dc2009-01-02 16:12:46 +0100228 .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
229 .pio_mask = ATA_PIO5,
Bartlomiej Zolnierkiewiczb1d249e2009-01-02 16:12:47 +0100230 .chipset = ide_generic,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200231};
232
233static int __init tx4938ide_probe(struct platform_device *pdev)
234{
235 hw_regs_t hw;
236 hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
237 struct ide_host *host;
238 struct resource *res;
239 struct tx4938ide_platform_info *pdata = pdev->dev.platform_data;
240 int irq, ret, i;
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100241 unsigned long mapbase, mapctl;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200242 struct ide_port_info d = tx4938ide_port_info;
243
244 irq = platform_get_irq(pdev, 0);
245 if (irq < 0)
246 return -ENODEV;
247 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
248 if (!res)
249 return -ENODEV;
250
251 if (!devm_request_mem_region(&pdev->dev, res->start,
252 res->end - res->start + 1, "tx4938ide"))
253 return -EBUSY;
254 mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100255 8 << pdata->ioport_shift);
256 mapctl = (unsigned long)devm_ioremap(&pdev->dev,
257 res->start + 0x10000 +
258 (6 << pdata->ioport_shift),
259 1 << pdata->ioport_shift);
260 if (!mapbase || !mapctl)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200261 return -EBUSY;
262
263 memset(&hw, 0, sizeof(hw));
264 if (pdata->ioport_shift) {
265 unsigned long port = mapbase;
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100266 unsigned long ctl = mapctl;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200267
268 hw.io_ports_array[0] = port;
269#ifdef __BIG_ENDIAN
270 port++;
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100271 ctl++;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200272#endif
273 for (i = 1; i <= 7; i++)
274 hw.io_ports_array[i] =
275 port + (i << pdata->ioport_shift);
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100276 hw.io_ports.ctl_addr = ctl;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200277 } else
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100278 ide_std_init_ports(&hw, mapbase, mapctl);
Atsushi Nemoto28502842008-10-23 23:22:08 +0200279 hw.irq = irq;
280 hw.dev = &pdev->dev;
281
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100282 pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
283 mapbase, mapctl, hw.irq);
Atsushi Nemoto28502842008-10-23 23:22:08 +0200284 if (pdata->gbus_clock)
285 tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
286 else
287 d.port_ops = NULL;
288 ret = ide_host_add(&d, hws, &host);
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100289 if (!ret)
290 platform_set_drvdata(pdev, host);
291 return ret;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200292}
293
294static int __exit tx4938ide_remove(struct platform_device *pdev)
295{
296 struct ide_host *host = platform_get_drvdata(pdev);
297
298 ide_host_remove(host);
299 return 0;
300}
301
302static struct platform_driver tx4938ide_driver = {
303 .driver = {
304 .name = "tx4938ide",
305 .owner = THIS_MODULE,
306 },
307 .remove = __exit_p(tx4938ide_remove),
308};
309
310static int __init tx4938ide_init(void)
311{
312 return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe);
313}
314
315static void __exit tx4938ide_exit(void)
316{
317 platform_driver_unregister(&tx4938ide_driver);
318}
319
320module_init(tx4938ide_init);
321module_exit(tx4938ide_exit);
322
323MODULE_DESCRIPTION("TX4938 internal IDE driver");
324MODULE_LICENSE("GPL");
325MODULE_ALIAS("platform:tx4938ide");