PCI: Fix oops in pci_vpd_truncate
[linux-2.6/mini2440.git] / drivers / ide / tx4938ide.c
blob4cb79c4c2604e2920c435ff1c97ebe3fb236de0d
1 /*
2 * TX4938 internal IDE driver
3 * Based on tx4939ide.c.
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.
9 * (C) Copyright TOSHIBA CORPORATION 2005-2007
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>
19 #include <asm/ide.h>
20 #include <asm/txx9/tx4938.h>
22 static void tx4938ide_tune_ebusc(unsigned int ebus_ch,
23 unsigned int gbus_clock,
24 u8 pio)
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;
31 unsigned int shwt;
32 int wt;
34 /* Minimum DIOx- active time */
35 wt = DIV_ROUND_UP(t->act8b, cycle) - 2;
36 /* IORDY setup time: 35ns */
37 wt = max_t(int, wt, DIV_ROUND_UP(35, cycle));
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);
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;
52 pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
53 ebus_ch, cycle, wt, shwt);
55 __raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt,
56 &tx4938_ebuscptr->cr[ebus_ch]);
59 static void tx4938ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
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;
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);
72 #ifdef __BIG_ENDIAN
74 /* custom iops (independent from SWAP_IO_SPACE) */
75 static u8 tx4938ide_inb(unsigned long port)
77 return __raw_readb((void __iomem *)port);
80 static void tx4938ide_outb(u8 value, unsigned long port)
82 __raw_writeb(value, (void __iomem *)port);
85 static void tx4938ide_tf_load(ide_drive_t *drive, struct ide_cmd *cmd)
87 ide_hwif_t *hwif = drive->hwif;
88 struct ide_io_ports *io_ports = &hwif->io_ports;
89 struct ide_taskfile *tf = &cmd->tf;
90 u8 HIHI = cmd->tf_flags & IDE_TFLAG_LBA48 ? 0xE0 : 0xEF;
92 if (cmd->ftf_flags & IDE_FTFLAG_FLAGGED)
93 HIHI = 0xFF;
95 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
96 tx4938ide_outb(tf->hob_feature, io_ports->feature_addr);
97 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
98 tx4938ide_outb(tf->hob_nsect, io_ports->nsect_addr);
99 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
100 tx4938ide_outb(tf->hob_lbal, io_ports->lbal_addr);
101 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
102 tx4938ide_outb(tf->hob_lbam, io_ports->lbam_addr);
103 if (cmd->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
104 tx4938ide_outb(tf->hob_lbah, io_ports->lbah_addr);
106 if (cmd->tf_flags & IDE_TFLAG_OUT_FEATURE)
107 tx4938ide_outb(tf->feature, io_ports->feature_addr);
108 if (cmd->tf_flags & IDE_TFLAG_OUT_NSECT)
109 tx4938ide_outb(tf->nsect, io_ports->nsect_addr);
110 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAL)
111 tx4938ide_outb(tf->lbal, io_ports->lbal_addr);
112 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAM)
113 tx4938ide_outb(tf->lbam, io_ports->lbam_addr);
114 if (cmd->tf_flags & IDE_TFLAG_OUT_LBAH)
115 tx4938ide_outb(tf->lbah, io_ports->lbah_addr);
117 if (cmd->tf_flags & IDE_TFLAG_OUT_DEVICE)
118 tx4938ide_outb((tf->device & HIHI) | drive->select,
119 io_ports->device_addr);
122 static void tx4938ide_tf_read(ide_drive_t *drive, struct ide_cmd *cmd)
124 ide_hwif_t *hwif = drive->hwif;
125 struct ide_io_ports *io_ports = &hwif->io_ports;
126 struct ide_taskfile *tf = &cmd->tf;
128 /* be sure we're looking at the low order bits */
129 tx4938ide_outb(ATA_DEVCTL_OBS, io_ports->ctl_addr);
131 if (cmd->tf_flags & IDE_TFLAG_IN_ERROR)
132 tf->error = tx4938ide_inb(io_ports->feature_addr);
133 if (cmd->tf_flags & IDE_TFLAG_IN_NSECT)
134 tf->nsect = tx4938ide_inb(io_ports->nsect_addr);
135 if (cmd->tf_flags & IDE_TFLAG_IN_LBAL)
136 tf->lbal = tx4938ide_inb(io_ports->lbal_addr);
137 if (cmd->tf_flags & IDE_TFLAG_IN_LBAM)
138 tf->lbam = tx4938ide_inb(io_ports->lbam_addr);
139 if (cmd->tf_flags & IDE_TFLAG_IN_LBAH)
140 tf->lbah = tx4938ide_inb(io_ports->lbah_addr);
141 if (cmd->tf_flags & IDE_TFLAG_IN_DEVICE)
142 tf->device = tx4938ide_inb(io_ports->device_addr);
144 if (cmd->tf_flags & IDE_TFLAG_LBA48) {
145 tx4938ide_outb(ATA_HOB | ATA_DEVCTL_OBS, io_ports->ctl_addr);
147 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_ERROR)
148 tf->hob_error = tx4938ide_inb(io_ports->feature_addr);
149 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
150 tf->hob_nsect = tx4938ide_inb(io_ports->nsect_addr);
151 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
152 tf->hob_lbal = tx4938ide_inb(io_ports->lbal_addr);
153 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
154 tf->hob_lbam = tx4938ide_inb(io_ports->lbam_addr);
155 if (cmd->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
156 tf->hob_lbah = tx4938ide_inb(io_ports->lbah_addr);
160 static void tx4938ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
161 void *buf, unsigned int len)
163 unsigned long port = drive->hwif->io_ports.data_addr;
164 unsigned short *ptr = buf;
165 unsigned int count = (len + 1) / 2;
167 while (count--)
168 *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
169 __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
172 static void tx4938ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
173 void *buf, unsigned int len)
175 unsigned long port = drive->hwif->io_ports.data_addr;
176 unsigned short *ptr = buf;
177 unsigned int count = (len + 1) / 2;
179 while (count--) {
180 __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
181 ptr++;
183 __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
186 static const struct ide_tp_ops tx4938ide_tp_ops = {
187 .exec_command = ide_exec_command,
188 .read_status = ide_read_status,
189 .read_altstatus = ide_read_altstatus,
190 .write_devctl = ide_write_devctl,
192 .dev_select = ide_dev_select,
193 .tf_load = tx4938ide_tf_load,
194 .tf_read = tx4938ide_tf_read,
196 .input_data = tx4938ide_input_data_swap,
197 .output_data = tx4938ide_output_data_swap,
200 #endif /* __BIG_ENDIAN */
202 static const struct ide_port_ops tx4938ide_port_ops = {
203 .set_pio_mode = tx4938ide_set_pio_mode,
206 static const struct ide_port_info tx4938ide_port_info __initdata = {
207 .port_ops = &tx4938ide_port_ops,
208 #ifdef __BIG_ENDIAN
209 .tp_ops = &tx4938ide_tp_ops,
210 #endif
211 .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
212 .pio_mask = ATA_PIO5,
213 .chipset = ide_generic,
216 static int __init tx4938ide_probe(struct platform_device *pdev)
218 hw_regs_t hw;
219 hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
220 struct ide_host *host;
221 struct resource *res;
222 struct tx4938ide_platform_info *pdata = pdev->dev.platform_data;
223 int irq, ret, i;
224 unsigned long mapbase, mapctl;
225 struct ide_port_info d = tx4938ide_port_info;
227 irq = platform_get_irq(pdev, 0);
228 if (irq < 0)
229 return -ENODEV;
230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
231 if (!res)
232 return -ENODEV;
234 if (!devm_request_mem_region(&pdev->dev, res->start,
235 res->end - res->start + 1, "tx4938ide"))
236 return -EBUSY;
237 mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
238 8 << pdata->ioport_shift);
239 mapctl = (unsigned long)devm_ioremap(&pdev->dev,
240 res->start + 0x10000 +
241 (6 << pdata->ioport_shift),
242 1 << pdata->ioport_shift);
243 if (!mapbase || !mapctl)
244 return -EBUSY;
246 memset(&hw, 0, sizeof(hw));
247 if (pdata->ioport_shift) {
248 unsigned long port = mapbase;
249 unsigned long ctl = mapctl;
251 hw.io_ports_array[0] = port;
252 #ifdef __BIG_ENDIAN
253 port++;
254 ctl++;
255 #endif
256 for (i = 1; i <= 7; i++)
257 hw.io_ports_array[i] =
258 port + (i << pdata->ioport_shift);
259 hw.io_ports.ctl_addr = ctl;
260 } else
261 ide_std_init_ports(&hw, mapbase, mapctl);
262 hw.irq = irq;
263 hw.dev = &pdev->dev;
265 pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
266 mapbase, mapctl, hw.irq);
267 if (pdata->gbus_clock)
268 tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
269 else
270 d.port_ops = NULL;
271 ret = ide_host_add(&d, hws, &host);
272 if (!ret)
273 platform_set_drvdata(pdev, host);
274 return ret;
277 static int __exit tx4938ide_remove(struct platform_device *pdev)
279 struct ide_host *host = platform_get_drvdata(pdev);
281 ide_host_remove(host);
282 return 0;
285 static struct platform_driver tx4938ide_driver = {
286 .driver = {
287 .name = "tx4938ide",
288 .owner = THIS_MODULE,
290 .remove = __exit_p(tx4938ide_remove),
293 static int __init tx4938ide_init(void)
295 return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe);
298 static void __exit tx4938ide_exit(void)
300 platform_driver_unregister(&tx4938ide_driver);
303 module_init(tx4938ide_init);
304 module_exit(tx4938ide_exit);
306 MODULE_DESCRIPTION("TX4938 internal IDE driver");
307 MODULE_LICENSE("GPL");
308 MODULE_ALIAS("platform:tx4938ide");