blob: ea89fddeed9122fa002f1149ffb0364a7f579100 [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) */
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010075static void tx4938ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
Atsushi Nemoto28502842008-10-23 23:22:08 +020076 void *buf, unsigned int len)
77{
78 unsigned long port = drive->hwif->io_ports.data_addr;
79 unsigned short *ptr = buf;
80 unsigned int count = (len + 1) / 2;
81
82 while (count--)
83 *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
Atsushi Nemotof26f6ce2008-12-29 20:27:29 +010084 __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
Atsushi Nemoto28502842008-10-23 23:22:08 +020085}
86
Bartlomiej Zolnierkiewiczadb1af92009-03-27 12:46:38 +010087static void tx4938ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
Atsushi Nemoto28502842008-10-23 23:22:08 +020088 void *buf, unsigned int len)
89{
90 unsigned long port = drive->hwif->io_ports.data_addr;
91 unsigned short *ptr = buf;
92 unsigned int count = (len + 1) / 2;
93
94 while (count--) {
95 __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
96 ptr++;
97 }
Atsushi Nemotof26f6ce2008-12-29 20:27:29 +010098 __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
Atsushi Nemoto28502842008-10-23 23:22:08 +020099}
100
101static const struct ide_tp_ops tx4938ide_tp_ops = {
102 .exec_command = ide_exec_command,
103 .read_status = ide_read_status,
104 .read_altstatus = ide_read_altstatus,
Sergei Shtylyovecf3a312009-03-31 20:15:30 +0200105 .write_devctl = ide_write_devctl,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200106
Sergei Shtylyovabb596b2009-03-31 20:15:32 +0200107 .dev_select = ide_dev_select,
Atsushi Nemotod68bab52009-04-08 14:12:52 +0200108 .tf_load = ide_tf_load,
109 .tf_read = ide_tf_read,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200110
111 .input_data = tx4938ide_input_data_swap,
112 .output_data = tx4938ide_output_data_swap,
113};
114
115#endif /* __BIG_ENDIAN */
116
117static const struct ide_port_ops tx4938ide_port_ops = {
Bartlomiej Zolnierkiewicz3ee86dc2009-01-02 16:12:46 +0100118 .set_pio_mode = tx4938ide_set_pio_mode,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200119};
120
121static const struct ide_port_info tx4938ide_port_info __initdata = {
Bartlomiej Zolnierkiewicz3ee86dc2009-01-02 16:12:46 +0100122 .port_ops = &tx4938ide_port_ops,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200123#ifdef __BIG_ENDIAN
Bartlomiej Zolnierkiewicz3ee86dc2009-01-02 16:12:46 +0100124 .tp_ops = &tx4938ide_tp_ops,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200125#endif
Bartlomiej Zolnierkiewicz3ee86dc2009-01-02 16:12:46 +0100126 .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
127 .pio_mask = ATA_PIO5,
Bartlomiej Zolnierkiewiczb1d249e2009-01-02 16:12:47 +0100128 .chipset = ide_generic,
Atsushi Nemoto28502842008-10-23 23:22:08 +0200129};
130
131static int __init tx4938ide_probe(struct platform_device *pdev)
132{
Bartlomiej Zolnierkiewicz9f36d312009-05-17 19:12:25 +0200133 struct ide_hw hw, *hws[] = { &hw };
Atsushi Nemoto28502842008-10-23 23:22:08 +0200134 struct ide_host *host;
135 struct resource *res;
136 struct tx4938ide_platform_info *pdata = pdev->dev.platform_data;
137 int irq, ret, i;
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100138 unsigned long mapbase, mapctl;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200139 struct ide_port_info d = tx4938ide_port_info;
140
141 irq = platform_get_irq(pdev, 0);
142 if (irq < 0)
143 return -ENODEV;
144 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
145 if (!res)
146 return -ENODEV;
147
148 if (!devm_request_mem_region(&pdev->dev, res->start,
149 res->end - res->start + 1, "tx4938ide"))
150 return -EBUSY;
151 mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100152 8 << pdata->ioport_shift);
153 mapctl = (unsigned long)devm_ioremap(&pdev->dev,
154 res->start + 0x10000 +
155 (6 << pdata->ioport_shift),
156 1 << pdata->ioport_shift);
157 if (!mapbase || !mapctl)
Atsushi Nemoto28502842008-10-23 23:22:08 +0200158 return -EBUSY;
159
160 memset(&hw, 0, sizeof(hw));
161 if (pdata->ioport_shift) {
162 unsigned long port = mapbase;
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100163 unsigned long ctl = mapctl;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200164
165 hw.io_ports_array[0] = port;
166#ifdef __BIG_ENDIAN
167 port++;
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100168 ctl++;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200169#endif
170 for (i = 1; i <= 7; i++)
171 hw.io_ports_array[i] =
172 port + (i << pdata->ioport_shift);
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100173 hw.io_ports.ctl_addr = ctl;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200174 } else
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100175 ide_std_init_ports(&hw, mapbase, mapctl);
Atsushi Nemoto28502842008-10-23 23:22:08 +0200176 hw.irq = irq;
177 hw.dev = &pdev->dev;
178
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100179 pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
180 mapbase, mapctl, hw.irq);
Atsushi Nemoto28502842008-10-23 23:22:08 +0200181 if (pdata->gbus_clock)
182 tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
183 else
184 d.port_ops = NULL;
Bartlomiej Zolnierkiewiczdca39832009-05-17 19:12:24 +0200185 ret = ide_host_add(&d, hws, 1, &host);
Atsushi Nemoto9d4eb0a2008-11-02 21:40:09 +0100186 if (!ret)
187 platform_set_drvdata(pdev, host);
188 return ret;
Atsushi Nemoto28502842008-10-23 23:22:08 +0200189}
190
191static int __exit tx4938ide_remove(struct platform_device *pdev)
192{
193 struct ide_host *host = platform_get_drvdata(pdev);
194
195 ide_host_remove(host);
196 return 0;
197}
198
199static struct platform_driver tx4938ide_driver = {
200 .driver = {
201 .name = "tx4938ide",
202 .owner = THIS_MODULE,
203 },
204 .remove = __exit_p(tx4938ide_remove),
205};
206
207static int __init tx4938ide_init(void)
208{
209 return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe);
210}
211
212static void __exit tx4938ide_exit(void)
213{
214 platform_driver_unregister(&tx4938ide_driver);
215}
216
217module_init(tx4938ide_init);
218module_exit(tx4938ide_exit);
219
220MODULE_DESCRIPTION("TX4938 internal IDE driver");
221MODULE_LICENSE("GPL");
222MODULE_ALIAS("platform:tx4938ide");