^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * TX4938 internal IDE driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Based on tx4939ide.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * License. See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * (C) Copyright TOSHIBA CORPORATION 2005-2007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/ide.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/ide.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/txx9/tx4938.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static void tx4938ide_tune_ebusc(unsigned int ebus_ch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int gbus_clock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u8 pio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct ide_timing *t = ide_timing_find_mode(XFER_PIO_0 + pio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u64 cr = __raw_readq(&tx4938_ebuscptr->cr[ebus_ch]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int sp = (cr >> 4) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned int clock = gbus_clock / (4 - sp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned int cycle = 1000000000 / clock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int shwt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int wt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Minimum DIOx- active time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) wt = DIV_ROUND_UP(t->act8b, cycle) - 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* IORDY setup time: 35ns */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) wt = max_t(int, wt, DIV_ROUND_UP(35, cycle));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /* actual wait-cycle is max(wt & ~1, 1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (wt > 2 && (wt & 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) wt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) wt &= ~1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Address-valid to DIOR/DIOW setup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) shwt = DIV_ROUND_UP(t->setup, cycle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* -DIOx recovery time (SHWT * 4) and cycle time requirement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) while ((shwt * 4 + wt + (wt ? 2 : 3)) * cycle < t->cycle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) shwt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (shwt > 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) pr_warn("tx4938ide: SHWT violation (%d)\n", shwt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) shwt = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ebus_ch, cycle, wt, shwt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) __raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) &tx4938_ebuscptr->cr[ebus_ch]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void tx4938ide_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct tx4938ide_platform_info *pdata = dev_get_platdata(hwif->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) u8 safe = drive->pio_mode - XFER_PIO_0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ide_drive_t *pair;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pair = ide_get_pair_dev(drive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (pair)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) safe = min_t(u8, safe, pair->pio_mode - XFER_PIO_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, safe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* custom iops (independent from SWAP_IO_SPACE) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static void tx4938ide_input_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) void *buf, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned long port = drive->hwif->io_ports.data_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unsigned short *ptr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned int count = (len + 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) while (count--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static void tx4938ide_output_data_swap(ide_drive_t *drive, struct ide_cmd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) void *buf, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned long port = drive->hwif->io_ports.data_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unsigned short *ptr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) unsigned int count = (len + 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) while (count--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ptr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static const struct ide_tp_ops tx4938ide_tp_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .exec_command = ide_exec_command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .read_status = ide_read_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .read_altstatus = ide_read_altstatus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .write_devctl = ide_write_devctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .dev_select = ide_dev_select,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .tf_load = ide_tf_load,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .tf_read = ide_tf_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .input_data = tx4938ide_input_data_swap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) .output_data = tx4938ide_output_data_swap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #endif /* __BIG_ENDIAN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static const struct ide_port_ops tx4938ide_port_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .set_pio_mode = tx4938ide_set_pio_mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static const struct ide_port_info tx4938ide_port_info __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .port_ops = &tx4938ide_port_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .tp_ops = &tx4938ide_tp_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .pio_mask = ATA_PIO5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) .chipset = ide_generic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static int __init tx4938ide_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct ide_hw hw, *hws[] = { &hw };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct ide_host *host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct tx4938ide_platform_info *pdata = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int irq, ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unsigned long mapbase, mapctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct ide_port_info d = tx4938ide_port_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) irq = platform_get_irq(pdev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!devm_request_mem_region(&pdev->dev, res->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) resource_size(res), "tx4938ide"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 8 << pdata->ioport_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) mapctl = (unsigned long)devm_ioremap(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) res->start + 0x10000 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) (6 << pdata->ioport_shift),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 1 << pdata->ioport_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!mapbase || !mapctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) memset(&hw, 0, sizeof(hw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (pdata->ioport_shift) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) unsigned long port = mapbase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) unsigned long ctl = mapctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) hw.io_ports_array[0] = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) port++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ctl++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) for (i = 1; i <= 7; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) hw.io_ports_array[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) port + (i << pdata->ioport_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) hw.io_ports.ctl_addr = ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ide_std_init_ports(&hw, mapbase, mapctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) hw.irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) hw.dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) mapbase, mapctl, hw.irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (pdata->gbus_clock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) d.port_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ret = ide_host_add(&d, hws, 1, &host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) platform_set_drvdata(pdev, host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int __exit tx4938ide_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) struct ide_host *host = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ide_host_remove(host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static struct platform_driver tx4938ide_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) .name = "tx4938ide",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .remove = __exit_p(tx4938ide_remove),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) module_platform_driver_probe(tx4938ide_driver, tx4938ide_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) MODULE_DESCRIPTION("TX4938 internal IDE driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) MODULE_ALIAS("platform:tx4938ide");