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 pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
43 ebus_ch
, cycle
, wt
, shwt
);
45 __raw_writeq((cr
& ~(0x3f007ull
)) | (wt
<< 12) | shwt
,
46 &tx4938_ebuscptr
->cr
[ebus_ch
]);
49 static void tx4938ide_set_pio_mode(ide_drive_t
*drive
, const u8 pio
)
51 ide_hwif_t
*hwif
= drive
->hwif
;
52 struct tx4938ide_platform_info
*pdata
= hwif
->dev
->platform_data
;
56 pair
= ide_get_pair_dev(drive
);
58 safe
= min(safe
, ide_get_best_pio_mode(pair
, 255, 5));
59 tx4938ide_tune_ebusc(pdata
->ebus_ch
, pdata
->gbus_clock
, safe
);
64 /* custom iops (independent from SWAP_IO_SPACE) */
65 static u8
tx4938ide_inb(unsigned long port
)
67 return __raw_readb((void __iomem
*)port
);
70 static void tx4938ide_outb(u8 value
, unsigned long port
)
72 __raw_writeb(value
, (void __iomem
*)port
);
75 static void tx4938ide_tf_load(ide_drive_t
*drive
, ide_task_t
*task
)
77 ide_hwif_t
*hwif
= drive
->hwif
;
78 struct ide_io_ports
*io_ports
= &hwif
->io_ports
;
79 struct ide_taskfile
*tf
= &task
->tf
;
80 u8 HIHI
= task
->tf_flags
& IDE_TFLAG_LBA48
? 0xE0 : 0xEF;
82 if (task
->tf_flags
& IDE_TFLAG_FLAGGED
)
85 if (task
->tf_flags
& IDE_TFLAG_OUT_DATA
) {
86 u16 data
= (tf
->hob_data
<< 8) | tf
->data
;
89 __raw_writew(data
, (void __iomem
*)io_ports
->data_addr
);
92 if (task
->tf_flags
& IDE_TFLAG_OUT_HOB_FEATURE
)
93 tx4938ide_outb(tf
->hob_feature
, io_ports
->feature_addr
);
94 if (task
->tf_flags
& IDE_TFLAG_OUT_HOB_NSECT
)
95 tx4938ide_outb(tf
->hob_nsect
, io_ports
->nsect_addr
);
96 if (task
->tf_flags
& IDE_TFLAG_OUT_HOB_LBAL
)
97 tx4938ide_outb(tf
->hob_lbal
, io_ports
->lbal_addr
);
98 if (task
->tf_flags
& IDE_TFLAG_OUT_HOB_LBAM
)
99 tx4938ide_outb(tf
->hob_lbam
, io_ports
->lbam_addr
);
100 if (task
->tf_flags
& IDE_TFLAG_OUT_HOB_LBAH
)
101 tx4938ide_outb(tf
->hob_lbah
, io_ports
->lbah_addr
);
103 if (task
->tf_flags
& IDE_TFLAG_OUT_FEATURE
)
104 tx4938ide_outb(tf
->feature
, io_ports
->feature_addr
);
105 if (task
->tf_flags
& IDE_TFLAG_OUT_NSECT
)
106 tx4938ide_outb(tf
->nsect
, io_ports
->nsect_addr
);
107 if (task
->tf_flags
& IDE_TFLAG_OUT_LBAL
)
108 tx4938ide_outb(tf
->lbal
, io_ports
->lbal_addr
);
109 if (task
->tf_flags
& IDE_TFLAG_OUT_LBAM
)
110 tx4938ide_outb(tf
->lbam
, io_ports
->lbam_addr
);
111 if (task
->tf_flags
& IDE_TFLAG_OUT_LBAH
)
112 tx4938ide_outb(tf
->lbah
, io_ports
->lbah_addr
);
114 if (task
->tf_flags
& IDE_TFLAG_OUT_DEVICE
)
115 tx4938ide_outb((tf
->device
& HIHI
) | drive
->select
,
116 io_ports
->device_addr
);
119 static void tx4938ide_tf_read(ide_drive_t
*drive
, ide_task_t
*task
)
121 ide_hwif_t
*hwif
= drive
->hwif
;
122 struct ide_io_ports
*io_ports
= &hwif
->io_ports
;
123 struct ide_taskfile
*tf
= &task
->tf
;
125 if (task
->tf_flags
& IDE_TFLAG_IN_DATA
) {
129 data
= __raw_readw((void __iomem
*)io_ports
->data_addr
);
130 tf
->data
= data
& 0xff;
131 tf
->hob_data
= (data
>> 8) & 0xff;
134 /* be sure we're looking at the low order bits */
135 tx4938ide_outb(ATA_DEVCTL_OBS
& ~0x80, io_ports
->ctl_addr
);
137 if (task
->tf_flags
& IDE_TFLAG_IN_FEATURE
)
138 tf
->feature
= tx4938ide_inb(io_ports
->feature_addr
);
139 if (task
->tf_flags
& IDE_TFLAG_IN_NSECT
)
140 tf
->nsect
= tx4938ide_inb(io_ports
->nsect_addr
);
141 if (task
->tf_flags
& IDE_TFLAG_IN_LBAL
)
142 tf
->lbal
= tx4938ide_inb(io_ports
->lbal_addr
);
143 if (task
->tf_flags
& IDE_TFLAG_IN_LBAM
)
144 tf
->lbam
= tx4938ide_inb(io_ports
->lbam_addr
);
145 if (task
->tf_flags
& IDE_TFLAG_IN_LBAH
)
146 tf
->lbah
= tx4938ide_inb(io_ports
->lbah_addr
);
147 if (task
->tf_flags
& IDE_TFLAG_IN_DEVICE
)
148 tf
->device
= tx4938ide_inb(io_ports
->device_addr
);
150 if (task
->tf_flags
& IDE_TFLAG_LBA48
) {
151 tx4938ide_outb(ATA_DEVCTL_OBS
| 0x80, io_ports
->ctl_addr
);
153 if (task
->tf_flags
& IDE_TFLAG_IN_HOB_FEATURE
)
155 tx4938ide_inb(io_ports
->feature_addr
);
156 if (task
->tf_flags
& IDE_TFLAG_IN_HOB_NSECT
)
157 tf
->hob_nsect
= tx4938ide_inb(io_ports
->nsect_addr
);
158 if (task
->tf_flags
& IDE_TFLAG_IN_HOB_LBAL
)
159 tf
->hob_lbal
= tx4938ide_inb(io_ports
->lbal_addr
);
160 if (task
->tf_flags
& IDE_TFLAG_IN_HOB_LBAM
)
161 tf
->hob_lbam
= tx4938ide_inb(io_ports
->lbam_addr
);
162 if (task
->tf_flags
& IDE_TFLAG_IN_HOB_LBAH
)
163 tf
->hob_lbah
= tx4938ide_inb(io_ports
->lbah_addr
);
167 static void tx4938ide_input_data_swap(ide_drive_t
*drive
, struct request
*rq
,
168 void *buf
, unsigned int len
)
170 unsigned long port
= drive
->hwif
->io_ports
.data_addr
;
171 unsigned short *ptr
= buf
;
172 unsigned int count
= (len
+ 1) / 2;
175 *ptr
++ = cpu_to_le16(__raw_readw((void __iomem
*)port
));
176 __ide_flush_dcache_range((unsigned long)buf
, count
* 2);
179 static void tx4938ide_output_data_swap(ide_drive_t
*drive
, struct request
*rq
,
180 void *buf
, unsigned int len
)
182 unsigned long port
= drive
->hwif
->io_ports
.data_addr
;
183 unsigned short *ptr
= buf
;
184 unsigned int count
= (len
+ 1) / 2;
187 __raw_writew(le16_to_cpu(*ptr
), (void __iomem
*)port
);
190 __ide_flush_dcache_range((unsigned long)buf
, count
* 2);
193 static const struct ide_tp_ops tx4938ide_tp_ops
= {
194 .exec_command
= ide_exec_command
,
195 .read_status
= ide_read_status
,
196 .read_altstatus
= ide_read_altstatus
,
197 .read_sff_dma_status
= ide_read_sff_dma_status
,
199 .set_irq
= ide_set_irq
,
201 .tf_load
= tx4938ide_tf_load
,
202 .tf_read
= tx4938ide_tf_read
,
204 .input_data
= tx4938ide_input_data_swap
,
205 .output_data
= tx4938ide_output_data_swap
,
208 #endif /* __BIG_ENDIAN */
210 static const struct ide_port_ops tx4938ide_port_ops
= {
211 .set_pio_mode
= tx4938ide_set_pio_mode
,
214 static const struct ide_port_info tx4938ide_port_info __initdata
= {
215 .port_ops
= &tx4938ide_port_ops
,
217 .tp_ops
= &tx4938ide_tp_ops
,
219 .host_flags
= IDE_HFLAG_MMIO
| IDE_HFLAG_NO_DMA
,
220 .pio_mask
= ATA_PIO5
,
223 static int __init
tx4938ide_probe(struct platform_device
*pdev
)
226 hw_regs_t
*hws
[] = { &hw
, NULL
, NULL
, NULL
};
227 struct ide_host
*host
;
228 struct resource
*res
;
229 struct tx4938ide_platform_info
*pdata
= pdev
->dev
.platform_data
;
231 unsigned long mapbase
;
232 struct ide_port_info d
= tx4938ide_port_info
;
234 irq
= platform_get_irq(pdev
, 0);
237 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
241 if (!devm_request_mem_region(&pdev
->dev
, res
->start
,
242 res
->end
- res
->start
+ 1, "tx4938ide"))
244 mapbase
= (unsigned long)devm_ioremap(&pdev
->dev
, res
->start
,
245 res
->end
- res
->start
+ 1);
249 memset(&hw
, 0, sizeof(hw
));
250 if (pdata
->ioport_shift
) {
251 unsigned long port
= mapbase
;
253 hw
.io_ports_array
[0] = port
;
257 for (i
= 1; i
<= 7; i
++)
258 hw
.io_ports_array
[i
] =
259 port
+ (i
<< pdata
->ioport_shift
);
260 hw
.io_ports
.ctl_addr
=
261 port
+ 0x10000 + (6 << pdata
->ioport_shift
);
263 ide_std_init_ports(&hw
, mapbase
, mapbase
+ 0x10006);
267 pr_info("TX4938 IDE interface (base %#lx, irq %d)\n", mapbase
, hw
.irq
);
268 if (pdata
->gbus_clock
)
269 tx4938ide_tune_ebusc(pdata
->ebus_ch
, pdata
->gbus_clock
, 0);
272 ret
= ide_host_add(&d
, hws
, &host
);
275 platform_set_drvdata(pdev
, host
);
279 static int __exit
tx4938ide_remove(struct platform_device
*pdev
)
281 struct ide_host
*host
= platform_get_drvdata(pdev
);
283 ide_host_remove(host
);
287 static struct platform_driver tx4938ide_driver
= {
290 .owner
= THIS_MODULE
,
292 .remove
= __exit_p(tx4938ide_remove
),
295 static int __init
tx4938ide_init(void)
297 return platform_driver_probe(&tx4938ide_driver
, tx4938ide_probe
);
300 static void __exit
tx4938ide_exit(void)
302 platform_driver_unregister(&tx4938ide_driver
);
305 module_init(tx4938ide_init
);
306 module_exit(tx4938ide_exit
);
308 MODULE_DESCRIPTION("TX4938 internal IDE driver");
309 MODULE_LICENSE("GPL");
310 MODULE_ALIAS("platform:tx4938ide");