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
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>
18 #include <asm/txx9/tx4938.h>
20 static void tx4938ide_tune_ebusc(unsigned int ebus_ch
,
21 unsigned int gbus_clock
,
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
;
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))
39 /* Address-valid to DIOR/DIOW setup */
40 shwt
= DIV_ROUND_UP(t
->setup
, cycle
);
42 /* -DIOx recovery time (SHWT * 4) and cycle time requirement */
43 while ((shwt
* 4 + wt
+ (wt
? 2 : 3)) * cycle
< t
->cycle
)
46 pr_warning("tx4938ide: SHWT violation (%d)\n", shwt
);
49 pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
50 ebus_ch
, cycle
, wt
, shwt
);
52 __raw_writeq((cr
& ~0x3f007ull
) | (wt
<< 12) | shwt
,
53 &tx4938_ebuscptr
->cr
[ebus_ch
]);
56 static void tx4938ide_set_pio_mode(ide_drive_t
*drive
, const u8 pio
)
58 ide_hwif_t
*hwif
= drive
->hwif
;
59 struct tx4938ide_platform_info
*pdata
= hwif
->dev
->platform_data
;
63 pair
= ide_get_pair_dev(drive
);
65 safe
= min(safe
, ide_get_best_pio_mode(pair
, 255, 5));
66 tx4938ide_tune_ebusc(pdata
->ebus_ch
, pdata
->gbus_clock
, safe
);
71 /* custom iops (independent from SWAP_IO_SPACE) */
72 static u8
tx4938ide_inb(unsigned long port
)
74 return __raw_readb((void __iomem
*)port
);
77 static void tx4938ide_outb(u8 value
, unsigned long port
)
79 __raw_writeb(value
, (void __iomem
*)port
);
82 static void tx4938ide_tf_load(ide_drive_t
*drive
, ide_task_t
*task
)
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;
89 if (task
->tf_flags
& IDE_TFLAG_FLAGGED
)
92 if (task
->tf_flags
& IDE_TFLAG_OUT_DATA
) {
93 u16 data
= (tf
->hob_data
<< 8) | tf
->data
;
96 __raw_writew(data
, (void __iomem
*)io_ports
->data_addr
);
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
);
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
);
121 if (task
->tf_flags
& IDE_TFLAG_OUT_DEVICE
)
122 tx4938ide_outb((tf
->device
& HIHI
) | drive
->select
,
123 io_ports
->device_addr
);
126 static void tx4938ide_tf_read(ide_drive_t
*drive
, ide_task_t
*task
)
128 ide_hwif_t
*hwif
= drive
->hwif
;
129 struct ide_io_ports
*io_ports
= &hwif
->io_ports
;
130 struct ide_taskfile
*tf
= &task
->tf
;
132 if (task
->tf_flags
& IDE_TFLAG_IN_DATA
) {
136 data
= __raw_readw((void __iomem
*)io_ports
->data_addr
);
137 tf
->data
= data
& 0xff;
138 tf
->hob_data
= (data
>> 8) & 0xff;
141 /* be sure we're looking at the low order bits */
142 tx4938ide_outb(ATA_DEVCTL_OBS
& ~0x80, io_ports
->ctl_addr
);
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
);
157 if (task
->tf_flags
& IDE_TFLAG_LBA48
) {
158 tx4938ide_outb(ATA_DEVCTL_OBS
| 0x80, io_ports
->ctl_addr
);
160 if (task
->tf_flags
& IDE_TFLAG_IN_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
);
174 static void tx4938ide_input_data_swap(ide_drive_t
*drive
, struct request
*rq
,
175 void *buf
, unsigned int len
)
177 unsigned long port
= drive
->hwif
->io_ports
.data_addr
;
178 unsigned short *ptr
= buf
;
179 unsigned int count
= (len
+ 1) / 2;
182 *ptr
++ = cpu_to_le16(__raw_readw((void __iomem
*)port
));
183 __ide_flush_dcache_range((unsigned long)buf
, count
* 2);
186 static void tx4938ide_output_data_swap(ide_drive_t
*drive
, struct request
*rq
,
187 void *buf
, unsigned int len
)
189 unsigned long port
= drive
->hwif
->io_ports
.data_addr
;
190 unsigned short *ptr
= buf
;
191 unsigned int count
= (len
+ 1) / 2;
194 __raw_writew(le16_to_cpu(*ptr
), (void __iomem
*)port
);
197 __ide_flush_dcache_range((unsigned long)buf
, count
* 2);
200 static 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
,
206 .set_irq
= ide_set_irq
,
208 .tf_load
= tx4938ide_tf_load
,
209 .tf_read
= tx4938ide_tf_read
,
211 .input_data
= tx4938ide_input_data_swap
,
212 .output_data
= tx4938ide_output_data_swap
,
215 #endif /* __BIG_ENDIAN */
217 static const struct ide_port_ops tx4938ide_port_ops
= {
218 .set_pio_mode
= tx4938ide_set_pio_mode
,
221 static const struct ide_port_info tx4938ide_port_info __initdata
= {
222 .port_ops
= &tx4938ide_port_ops
,
224 .tp_ops
= &tx4938ide_tp_ops
,
226 .host_flags
= IDE_HFLAG_MMIO
| IDE_HFLAG_NO_DMA
,
227 .pio_mask
= ATA_PIO5
,
230 static int __init
tx4938ide_probe(struct platform_device
*pdev
)
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
;
238 unsigned long mapbase
, mapctl
;
239 struct ide_port_info d
= tx4938ide_port_info
;
241 irq
= platform_get_irq(pdev
, 0);
244 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
248 if (!devm_request_mem_region(&pdev
->dev
, res
->start
,
249 res
->end
- res
->start
+ 1, "tx4938ide"))
251 mapbase
= (unsigned long)devm_ioremap(&pdev
->dev
, res
->start
,
252 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
)
260 memset(&hw
, 0, sizeof(hw
));
261 if (pdata
->ioport_shift
) {
262 unsigned long port
= mapbase
;
263 unsigned long ctl
= mapctl
;
265 hw
.io_ports_array
[0] = port
;
270 for (i
= 1; i
<= 7; i
++)
271 hw
.io_ports_array
[i
] =
272 port
+ (i
<< pdata
->ioport_shift
);
273 hw
.io_ports
.ctl_addr
= ctl
;
275 ide_std_init_ports(&hw
, mapbase
, mapctl
);
279 pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
280 mapbase
, mapctl
, hw
.irq
);
281 if (pdata
->gbus_clock
)
282 tx4938ide_tune_ebusc(pdata
->ebus_ch
, pdata
->gbus_clock
, 0);
285 ret
= ide_host_add(&d
, hws
, &host
);
287 platform_set_drvdata(pdev
, host
);
291 static int __exit
tx4938ide_remove(struct platform_device
*pdev
)
293 struct ide_host
*host
= platform_get_drvdata(pdev
);
295 ide_host_remove(host
);
299 static struct platform_driver tx4938ide_driver
= {
302 .owner
= THIS_MODULE
,
304 .remove
= __exit_p(tx4938ide_remove
),
307 static int __init
tx4938ide_init(void)
309 return platform_driver_probe(&tx4938ide_driver
, tx4938ide_probe
);
312 static void __exit
tx4938ide_exit(void)
314 platform_driver_unregister(&tx4938ide_driver
);
317 module_init(tx4938ide_init
);
318 module_exit(tx4938ide_exit
);
320 MODULE_DESCRIPTION("TX4938 internal IDE driver");
321 MODULE_LICENSE("GPL");
322 MODULE_ALIAS("platform:tx4938ide");