18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * TX4938 internal IDE driver
38c2ecf20Sopenharmony_ci * Based on tx4939ide.c.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
68c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
78c2ecf20Sopenharmony_ci * for more details.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * (C) Copyright TOSHIBA CORPORATION 2005-2007
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <linux/ide.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
178c2ecf20Sopenharmony_ci#include <linux/io.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <asm/ide.h>
208c2ecf20Sopenharmony_ci#include <asm/txx9/tx4938.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic void tx4938ide_tune_ebusc(unsigned int ebus_ch,
238c2ecf20Sopenharmony_ci				 unsigned int gbus_clock,
248c2ecf20Sopenharmony_ci				 u8 pio)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
278c2ecf20Sopenharmony_ci	u64 cr = __raw_readq(&tx4938_ebuscptr->cr[ebus_ch]);
288c2ecf20Sopenharmony_ci	unsigned int sp = (cr >> 4) & 3;
298c2ecf20Sopenharmony_ci	unsigned int clock = gbus_clock / (4 - sp);
308c2ecf20Sopenharmony_ci	unsigned int cycle = 1000000000 / clock;
318c2ecf20Sopenharmony_ci	unsigned int shwt;
328c2ecf20Sopenharmony_ci	int wt;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	/* Minimum DIOx- active time */
358c2ecf20Sopenharmony_ci	wt = DIV_ROUND_UP(t->act8b, cycle) - 2;
368c2ecf20Sopenharmony_ci	/* IORDY setup time: 35ns */
378c2ecf20Sopenharmony_ci	wt = max_t(int, wt, DIV_ROUND_UP(35, cycle));
388c2ecf20Sopenharmony_ci	/* actual wait-cycle is max(wt & ~1, 1) */
398c2ecf20Sopenharmony_ci	if (wt > 2 && (wt & 1))
408c2ecf20Sopenharmony_ci		wt++;
418c2ecf20Sopenharmony_ci	wt &= ~1;
428c2ecf20Sopenharmony_ci	/* Address-valid to DIOR/DIOW setup */
438c2ecf20Sopenharmony_ci	shwt = DIV_ROUND_UP(t->setup, cycle);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	/* -DIOx recovery time (SHWT * 4) and cycle time requirement */
468c2ecf20Sopenharmony_ci	while ((shwt * 4 + wt + (wt ? 2 : 3)) * cycle < t->cycle)
478c2ecf20Sopenharmony_ci		shwt++;
488c2ecf20Sopenharmony_ci	if (shwt > 7) {
498c2ecf20Sopenharmony_ci		pr_warn("tx4938ide: SHWT violation (%d)\n", shwt);
508c2ecf20Sopenharmony_ci		shwt = 7;
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci	pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
538c2ecf20Sopenharmony_ci		 ebus_ch, cycle, wt, shwt);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	__raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt,
568c2ecf20Sopenharmony_ci		     &tx4938_ebuscptr->cr[ebus_ch]);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic void tx4938ide_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct tx4938ide_platform_info *pdata = dev_get_platdata(hwif->dev);
628c2ecf20Sopenharmony_ci	u8 safe = drive->pio_mode - XFER_PIO_0;
638c2ecf20Sopenharmony_ci	ide_drive_t *pair;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	pair = ide_get_pair_dev(drive);
668c2ecf20Sopenharmony_ci	if (pair)
678c2ecf20Sopenharmony_ci		safe = min_t(u8, safe, pair->pio_mode - XFER_PIO_0);
688c2ecf20Sopenharmony_ci	tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, safe);
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/* custom iops (independent from SWAP_IO_SPACE) */
748c2ecf20Sopenharmony_cistatic void tx4938ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
758c2ecf20Sopenharmony_ci				void *buf, unsigned int len)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	unsigned long port = drive->hwif->io_ports.data_addr;
788c2ecf20Sopenharmony_ci	unsigned short *ptr = buf;
798c2ecf20Sopenharmony_ci	unsigned int count = (len + 1) / 2;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	while (count--)
828c2ecf20Sopenharmony_ci		*ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
838c2ecf20Sopenharmony_ci	__ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic void tx4938ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
878c2ecf20Sopenharmony_ci				void *buf, unsigned int len)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	unsigned long port = drive->hwif->io_ports.data_addr;
908c2ecf20Sopenharmony_ci	unsigned short *ptr = buf;
918c2ecf20Sopenharmony_ci	unsigned int count = (len + 1) / 2;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	while (count--) {
948c2ecf20Sopenharmony_ci		__raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
958c2ecf20Sopenharmony_ci		ptr++;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci	__ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic const struct ide_tp_ops tx4938ide_tp_ops = {
1018c2ecf20Sopenharmony_ci	.exec_command		= ide_exec_command,
1028c2ecf20Sopenharmony_ci	.read_status		= ide_read_status,
1038c2ecf20Sopenharmony_ci	.read_altstatus		= ide_read_altstatus,
1048c2ecf20Sopenharmony_ci	.write_devctl		= ide_write_devctl,
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	.dev_select		= ide_dev_select,
1078c2ecf20Sopenharmony_ci	.tf_load		= ide_tf_load,
1088c2ecf20Sopenharmony_ci	.tf_read		= ide_tf_read,
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	.input_data		= tx4938ide_input_data_swap,
1118c2ecf20Sopenharmony_ci	.output_data		= tx4938ide_output_data_swap,
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#endif	/* __BIG_ENDIAN */
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic const struct ide_port_ops tx4938ide_port_ops = {
1178c2ecf20Sopenharmony_ci	.set_pio_mode		= tx4938ide_set_pio_mode,
1188c2ecf20Sopenharmony_ci};
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic const struct ide_port_info tx4938ide_port_info __initconst = {
1218c2ecf20Sopenharmony_ci	.port_ops		= &tx4938ide_port_ops,
1228c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN
1238c2ecf20Sopenharmony_ci	.tp_ops			= &tx4938ide_tp_ops,
1248c2ecf20Sopenharmony_ci#endif
1258c2ecf20Sopenharmony_ci	.host_flags		= IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
1268c2ecf20Sopenharmony_ci	.pio_mask		= ATA_PIO5,
1278c2ecf20Sopenharmony_ci	.chipset		= ide_generic,
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic int __init tx4938ide_probe(struct platform_device *pdev)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	struct ide_hw hw, *hws[] = { &hw };
1338c2ecf20Sopenharmony_ci	struct ide_host *host;
1348c2ecf20Sopenharmony_ci	struct resource *res;
1358c2ecf20Sopenharmony_ci	struct tx4938ide_platform_info *pdata = dev_get_platdata(&pdev->dev);
1368c2ecf20Sopenharmony_ci	int irq, ret, i;
1378c2ecf20Sopenharmony_ci	unsigned long mapbase, mapctl;
1388c2ecf20Sopenharmony_ci	struct ide_port_info d = tx4938ide_port_info;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
1418c2ecf20Sopenharmony_ci	if (irq < 0)
1428c2ecf20Sopenharmony_ci		return -ENODEV;
1438c2ecf20Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1448c2ecf20Sopenharmony_ci	if (!res)
1458c2ecf20Sopenharmony_ci		return -ENODEV;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (!devm_request_mem_region(&pdev->dev, res->start,
1488c2ecf20Sopenharmony_ci				     resource_size(res), "tx4938ide"))
1498c2ecf20Sopenharmony_ci		return -EBUSY;
1508c2ecf20Sopenharmony_ci	mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
1518c2ecf20Sopenharmony_ci					      8 << pdata->ioport_shift);
1528c2ecf20Sopenharmony_ci	mapctl = (unsigned long)devm_ioremap(&pdev->dev,
1538c2ecf20Sopenharmony_ci					     res->start + 0x10000 +
1548c2ecf20Sopenharmony_ci					     (6 << pdata->ioport_shift),
1558c2ecf20Sopenharmony_ci					     1 << pdata->ioport_shift);
1568c2ecf20Sopenharmony_ci	if (!mapbase || !mapctl)
1578c2ecf20Sopenharmony_ci		return -EBUSY;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	memset(&hw, 0, sizeof(hw));
1608c2ecf20Sopenharmony_ci	if (pdata->ioport_shift) {
1618c2ecf20Sopenharmony_ci		unsigned long port = mapbase;
1628c2ecf20Sopenharmony_ci		unsigned long ctl = mapctl;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		hw.io_ports_array[0] = port;
1658c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN
1668c2ecf20Sopenharmony_ci		port++;
1678c2ecf20Sopenharmony_ci		ctl++;
1688c2ecf20Sopenharmony_ci#endif
1698c2ecf20Sopenharmony_ci		for (i = 1; i <= 7; i++)
1708c2ecf20Sopenharmony_ci			hw.io_ports_array[i] =
1718c2ecf20Sopenharmony_ci				port + (i << pdata->ioport_shift);
1728c2ecf20Sopenharmony_ci		hw.io_ports.ctl_addr = ctl;
1738c2ecf20Sopenharmony_ci	} else
1748c2ecf20Sopenharmony_ci		ide_std_init_ports(&hw, mapbase, mapctl);
1758c2ecf20Sopenharmony_ci	hw.irq = irq;
1768c2ecf20Sopenharmony_ci	hw.dev = &pdev->dev;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
1798c2ecf20Sopenharmony_ci		mapbase, mapctl, hw.irq);
1808c2ecf20Sopenharmony_ci	if (pdata->gbus_clock)
1818c2ecf20Sopenharmony_ci		tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
1828c2ecf20Sopenharmony_ci	else
1838c2ecf20Sopenharmony_ci		d.port_ops = NULL;
1848c2ecf20Sopenharmony_ci	ret = ide_host_add(&d, hws, 1, &host);
1858c2ecf20Sopenharmony_ci	if (!ret)
1868c2ecf20Sopenharmony_ci		platform_set_drvdata(pdev, host);
1878c2ecf20Sopenharmony_ci	return ret;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic int __exit tx4938ide_remove(struct platform_device *pdev)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	struct ide_host *host = platform_get_drvdata(pdev);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	ide_host_remove(host);
1958c2ecf20Sopenharmony_ci	return 0;
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic struct platform_driver tx4938ide_driver = {
1998c2ecf20Sopenharmony_ci	.driver		= {
2008c2ecf20Sopenharmony_ci		.name	= "tx4938ide",
2018c2ecf20Sopenharmony_ci	},
2028c2ecf20Sopenharmony_ci	.remove = __exit_p(tx4938ide_remove),
2038c2ecf20Sopenharmony_ci};
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cimodule_platform_driver_probe(tx4938ide_driver, tx4938ide_probe);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TX4938 internal IDE driver");
2088c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2098c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:tx4938ide");
210