ide: rename IDE_TFLAG_IN_[HOB_]FEATURE
[deliverable/linux.git] / drivers / ide / tx4938ide.c
CommitLineData
28502848
AN
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>
15a453a9
BZ
18
19#include <asm/ide.h>
28502848
AN
20#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;
7afa0535
AN
31 unsigned int shwt;
32 int wt;
28502848
AN
33
34 /* Minimum DIOx- active time */
35 wt = DIV_ROUND_UP(t->act8b, cycle) - 2;
36 /* IORDY setup time: 35ns */
7afa0535 37 wt = max_t(int, wt, DIV_ROUND_UP(35, cycle));
28502848
AN
38 /* 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
630a8b25
AN
45 /* -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 }
28502848
AN
52 pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
53 ebus_ch, cycle, wt, shwt);
54
630a8b25 55 __raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt,
28502848
AN
56 &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
22aa4b32 85static void tx4938ide_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
28502848
AN
86{
87 ide_hwif_t *hwif = drive->hwif;
88 struct ide_io_ports *io_ports = &hwif->io_ports;
22aa4b32
BZ
89 struct ide_taskfile *tf = &cmd->tf;
90 u8 HIHI = cmd->tf_flags & IDE_TFLAG_LBA48 ? 0xE0 : 0xEF;
28502848 91
22aa4b32 92 if (cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
28502848
AN
93 HIHI = 0xFF;
94
22aa4b32 95 if (cmd->ftf_flags & IDE_FTFLAG_OUT_DATA) {
28502848
AN
96 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
22aa4b32 102 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
28502848 103 tx4938ide_outb(tf->hob_feature, io_ports->feature_addr);
22aa4b32 104 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
28502848 105 tx4938ide_outb(tf->hob_nsect, io_ports->nsect_addr);
22aa4b32 106 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
28502848 107 tx4938ide_outb(tf->hob_lbal, io_ports->lbal_addr);
22aa4b32 108 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
28502848 109 tx4938ide_outb(tf->hob_lbam, io_ports->lbam_addr);
22aa4b32 110 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
28502848
AN
111 tx4938ide_outb(tf->hob_lbah, io_ports->lbah_addr);
112
22aa4b32 113 if (cmd->tf_flags & IDE_TFLAG_OUT_FEATURE)
28502848 114 tx4938ide_outb(tf->feature, io_ports->feature_addr);
22aa4b32 115 if (cmd->tf_flags & IDE_TFLAG_OUT_NSECT)
28502848 116 tx4938ide_outb(tf->nsect, io_ports->nsect_addr);
22aa4b32 117 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAL)
28502848 118 tx4938ide_outb(tf->lbal, io_ports->lbal_addr);
22aa4b32 119 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAM)
28502848 120 tx4938ide_outb(tf->lbam, io_ports->lbam_addr);
22aa4b32 121 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAH)
28502848
AN
122 tx4938ide_outb(tf->lbah, io_ports->lbah_addr);
123
22aa4b32 124 if (cmd->tf_flags & IDE_TFLAG_OUT_DEVICE)
28502848
AN
125 tx4938ide_outb((tf->device & HIHI) | drive->select,
126 io_ports->device_addr);
127}
128
22aa4b32 129static void tx4938ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
28502848
AN
130{
131 ide_hwif_t *hwif = drive->hwif;
132 struct ide_io_ports *io_ports = &hwif->io_ports;
22aa4b32 133 struct ide_taskfile *tf = &cmd->tf;
28502848 134
22aa4b32 135 if (cmd->ftf_flags & IDE_FTFLAG_IN_DATA) {
28502848
AN
136 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 */
4d74c3fc 145 tx4938ide_outb(ATA_DEVCTL_OBS, io_ports->ctl_addr);
28502848 146
67625119
SS
147 if (cmd->tf_flags & IDE_TFLAG_IN_ERROR)
148 tf->error = tx4938ide_inb(io_ports->feature_addr);
22aa4b32 149 if (cmd->tf_flags & IDE_TFLAG_IN_NSECT)
28502848 150 tf->nsect = tx4938ide_inb(io_ports->nsect_addr);
22aa4b32 151 if (cmd->tf_flags & IDE_TFLAG_IN_LBAL)
28502848 152 tf->lbal = tx4938ide_inb(io_ports->lbal_addr);
22aa4b32 153 if (cmd->tf_flags & IDE_TFLAG_IN_LBAM)
28502848 154 tf->lbam = tx4938ide_inb(io_ports->lbam_addr);
22aa4b32 155 if (cmd->tf_flags & IDE_TFLAG_IN_LBAH)
28502848 156 tf->lbah = tx4938ide_inb(io_ports->lbah_addr);
22aa4b32 157 if (cmd->tf_flags & IDE_TFLAG_IN_DEVICE)
28502848
AN
158 tf->device = tx4938ide_inb(io_ports->device_addr);
159
22aa4b32 160 if (cmd->tf_flags & IDE_TFLAG_LBA48) {
4d74c3fc 161 tx4938ide_outb(ATA_HOB | ATA_DEVCTL_OBS, io_ports->ctl_addr);
28502848 162
67625119
SS
163 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_ERROR)
164 tf->hob_error = tx4938ide_inb(io_ports->feature_addr);
22aa4b32 165 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
67625119 166 tf->hob_nsect = tx4938ide_inb(io_ports->nsect_addr);
22aa4b32 167 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
67625119 168 tf->hob_lbal = tx4938ide_inb(io_ports->lbal_addr);
22aa4b32 169 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
67625119 170 tf->hob_lbam = tx4938ide_inb(io_ports->lbam_addr);
22aa4b32 171 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
67625119 172 tf->hob_lbah = tx4938ide_inb(io_ports->lbah_addr);
28502848
AN
173 }
174}
175
adb1af98 176static void tx4938ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
28502848
AN
177 void *buf, unsigned int len)
178{
179 unsigned long port = drive->hwif->io_ports.data_addr;
180 unsigned short *ptr = buf;
181 unsigned int count = (len + 1) / 2;
182
183 while (count--)
184 *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
f26f6cea 185 __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
28502848
AN
186}
187
adb1af98 188static void tx4938ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
28502848
AN
189 void *buf, unsigned int len)
190{
191 unsigned long port = drive->hwif->io_ports.data_addr;
192 unsigned short *ptr = buf;
193 unsigned int count = (len + 1) / 2;
194
195 while (count--) {
196 __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
197 ptr++;
198 }
f26f6cea 199 __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
28502848
AN
200}
201
202static const struct ide_tp_ops tx4938ide_tp_ops = {
203 .exec_command = ide_exec_command,
204 .read_status = ide_read_status,
205 .read_altstatus = ide_read_altstatus,
ecf3a31d 206 .write_devctl = ide_write_devctl,
28502848
AN
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 = {
3ee86dcd 218 .set_pio_mode = tx4938ide_set_pio_mode,
28502848
AN
219};
220
221static const struct ide_port_info tx4938ide_port_info __initdata = {
3ee86dcd 222 .port_ops = &tx4938ide_port_ops,
28502848 223#ifdef __BIG_ENDIAN
3ee86dcd 224 .tp_ops = &tx4938ide_tp_ops,
28502848 225#endif
3ee86dcd
BZ
226 .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
227 .pio_mask = ATA_PIO5,
b1d249e8 228 .chipset = ide_generic,
28502848
AN
229};
230
231static int __init tx4938ide_probe(struct platform_device *pdev)
232{
233 hw_regs_t hw;
234 hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
235 struct ide_host *host;
236 struct resource *res;
237 struct tx4938ide_platform_info *pdata = pdev->dev.platform_data;
238 int irq, ret, i;
9d4eb0a3 239 unsigned long mapbase, mapctl;
28502848
AN
240 struct ide_port_info d = tx4938ide_port_info;
241
242 irq = platform_get_irq(pdev, 0);
243 if (irq < 0)
244 return -ENODEV;
245 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
246 if (!res)
247 return -ENODEV;
248
249 if (!devm_request_mem_region(&pdev->dev, res->start,
250 res->end - res->start + 1, "tx4938ide"))
251 return -EBUSY;
252 mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
9d4eb0a3
AN
253 8 << pdata->ioport_shift);
254 mapctl = (unsigned long)devm_ioremap(&pdev->dev,
255 res->start + 0x10000 +
256 (6 << pdata->ioport_shift),
257 1 << pdata->ioport_shift);
258 if (!mapbase || !mapctl)
28502848
AN
259 return -EBUSY;
260
261 memset(&hw, 0, sizeof(hw));
262 if (pdata->ioport_shift) {
263 unsigned long port = mapbase;
9d4eb0a3 264 unsigned long ctl = mapctl;
28502848
AN
265
266 hw.io_ports_array[0] = port;
267#ifdef __BIG_ENDIAN
268 port++;
9d4eb0a3 269 ctl++;
28502848
AN
270#endif
271 for (i = 1; i <= 7; i++)
272 hw.io_ports_array[i] =
273 port + (i << pdata->ioport_shift);
9d4eb0a3 274 hw.io_ports.ctl_addr = ctl;
28502848 275 } else
9d4eb0a3 276 ide_std_init_ports(&hw, mapbase, mapctl);
28502848
AN
277 hw.irq = irq;
278 hw.dev = &pdev->dev;
279
9d4eb0a3
AN
280 pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
281 mapbase, mapctl, hw.irq);
28502848
AN
282 if (pdata->gbus_clock)
283 tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
284 else
285 d.port_ops = NULL;
286 ret = ide_host_add(&d, hws, &host);
9d4eb0a3
AN
287 if (!ret)
288 platform_set_drvdata(pdev, host);
289 return ret;
28502848
AN
290}
291
292static int __exit tx4938ide_remove(struct platform_device *pdev)
293{
294 struct ide_host *host = platform_get_drvdata(pdev);
295
296 ide_host_remove(host);
297 return 0;
298}
299
300static struct platform_driver tx4938ide_driver = {
301 .driver = {
302 .name = "tx4938ide",
303 .owner = THIS_MODULE,
304 },
305 .remove = __exit_p(tx4938ide_remove),
306};
307
308static int __init tx4938ide_init(void)
309{
310 return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe);
311}
312
313static void __exit tx4938ide_exit(void)
314{
315 platform_driver_unregister(&tx4938ide_driver);
316}
317
318module_init(tx4938ide_init);
319module_exit(tx4938ide_exit);
320
321MODULE_DESCRIPTION("TX4938 internal IDE driver");
322MODULE_LICENSE("GPL");
323MODULE_ALIAS("platform:tx4938ide");
This page took 0.073947 seconds and 5 git commands to generate.