Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 1 | /* |
| 2 | * TX4938 internal IDE driver |
| 3 | * Based on tx4939ide.c. |
| 4 | * |
| 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. |
| 8 | * |
| 9 | * (C) Copyright TOSHIBA CORPORATION 2005-2007 |
| 10 | */ |
| 11 | |
| 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> |
| 18 | #include <asm/txx9/tx4938.h> |
| 19 | |
| 20 | static void tx4938ide_tune_ebusc(unsigned int ebus_ch, |
| 21 | unsigned int gbus_clock, |
| 22 | u8 pio) |
| 23 | { |
| 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; |
Atsushi Nemoto | 7afa053 | 2008-11-02 21:40:10 +0100 | [diff] [blame] | 29 | unsigned int shwt; |
| 30 | int wt; |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 31 | |
| 32 | /* Minimum DIOx- active time */ |
| 33 | wt = DIV_ROUND_UP(t->act8b, cycle) - 2; |
| 34 | /* IORDY setup time: 35ns */ |
Atsushi Nemoto | 7afa053 | 2008-11-02 21:40:10 +0100 | [diff] [blame] | 35 | wt = max_t(int, wt, DIV_ROUND_UP(35, cycle)); |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 36 | /* actual wait-cycle is max(wt & ~1, 1) */ |
| 37 | if (wt > 2 && (wt & 1)) |
| 38 | wt++; |
| 39 | wt &= ~1; |
| 40 | /* Address-valid to DIOR/DIOW setup */ |
| 41 | shwt = DIV_ROUND_UP(t->setup, cycle); |
| 42 | |
Atsushi Nemoto | 630a8b2 | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 43 | /* -DIOx recovery time (SHWT * 4) and cycle time requirement */ |
| 44 | while ((shwt * 4 + wt + (wt ? 2 : 3)) * cycle < t->cycle) |
| 45 | shwt++; |
| 46 | if (shwt > 7) { |
| 47 | pr_warning("tx4938ide: SHWT violation (%d)\n", shwt); |
| 48 | shwt = 7; |
| 49 | } |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 50 | pr_debug("tx4938ide: ebus %d, bus cycle %dns, WT %d, SHWT %d\n", |
| 51 | ebus_ch, cycle, wt, shwt); |
| 52 | |
Atsushi Nemoto | 630a8b2 | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 53 | __raw_writeq((cr & ~0x3f007ull) | (wt << 12) | shwt, |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 54 | &tx4938_ebuscptr->cr[ebus_ch]); |
| 55 | } |
| 56 | |
| 57 | static void tx4938ide_set_pio_mode(ide_drive_t *drive, const u8 pio) |
| 58 | { |
| 59 | ide_hwif_t *hwif = drive->hwif; |
| 60 | struct tx4938ide_platform_info *pdata = hwif->dev->platform_data; |
| 61 | u8 safe = pio; |
| 62 | ide_drive_t *pair; |
| 63 | |
| 64 | pair = ide_get_pair_dev(drive); |
| 65 | if (pair) |
| 66 | safe = min(safe, ide_get_best_pio_mode(pair, 255, 5)); |
| 67 | tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, safe); |
| 68 | } |
| 69 | |
| 70 | #ifdef __BIG_ENDIAN |
| 71 | |
| 72 | /* custom iops (independent from SWAP_IO_SPACE) */ |
| 73 | static u8 tx4938ide_inb(unsigned long port) |
| 74 | { |
| 75 | return __raw_readb((void __iomem *)port); |
| 76 | } |
| 77 | |
| 78 | static void tx4938ide_outb(u8 value, unsigned long port) |
| 79 | { |
| 80 | __raw_writeb(value, (void __iomem *)port); |
| 81 | } |
| 82 | |
| 83 | static void tx4938ide_tf_load(ide_drive_t *drive, ide_task_t *task) |
| 84 | { |
| 85 | ide_hwif_t *hwif = drive->hwif; |
| 86 | struct ide_io_ports *io_ports = &hwif->io_ports; |
| 87 | struct ide_taskfile *tf = &task->tf; |
| 88 | u8 HIHI = task->tf_flags & IDE_TFLAG_LBA48 ? 0xE0 : 0xEF; |
| 89 | |
| 90 | if (task->tf_flags & IDE_TFLAG_FLAGGED) |
| 91 | HIHI = 0xFF; |
| 92 | |
| 93 | if (task->tf_flags & IDE_TFLAG_OUT_DATA) { |
| 94 | u16 data = (tf->hob_data << 8) | tf->data; |
| 95 | |
| 96 | /* no endian swap */ |
| 97 | __raw_writew(data, (void __iomem *)io_ports->data_addr); |
| 98 | } |
| 99 | |
| 100 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE) |
| 101 | tx4938ide_outb(tf->hob_feature, io_ports->feature_addr); |
| 102 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT) |
| 103 | tx4938ide_outb(tf->hob_nsect, io_ports->nsect_addr); |
| 104 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL) |
| 105 | tx4938ide_outb(tf->hob_lbal, io_ports->lbal_addr); |
| 106 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM) |
| 107 | tx4938ide_outb(tf->hob_lbam, io_ports->lbam_addr); |
| 108 | if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH) |
| 109 | tx4938ide_outb(tf->hob_lbah, io_ports->lbah_addr); |
| 110 | |
| 111 | if (task->tf_flags & IDE_TFLAG_OUT_FEATURE) |
| 112 | tx4938ide_outb(tf->feature, io_ports->feature_addr); |
| 113 | if (task->tf_flags & IDE_TFLAG_OUT_NSECT) |
| 114 | tx4938ide_outb(tf->nsect, io_ports->nsect_addr); |
| 115 | if (task->tf_flags & IDE_TFLAG_OUT_LBAL) |
| 116 | tx4938ide_outb(tf->lbal, io_ports->lbal_addr); |
| 117 | if (task->tf_flags & IDE_TFLAG_OUT_LBAM) |
| 118 | tx4938ide_outb(tf->lbam, io_ports->lbam_addr); |
| 119 | if (task->tf_flags & IDE_TFLAG_OUT_LBAH) |
| 120 | tx4938ide_outb(tf->lbah, io_ports->lbah_addr); |
| 121 | |
| 122 | if (task->tf_flags & IDE_TFLAG_OUT_DEVICE) |
| 123 | tx4938ide_outb((tf->device & HIHI) | drive->select, |
| 124 | io_ports->device_addr); |
| 125 | } |
| 126 | |
| 127 | static void tx4938ide_tf_read(ide_drive_t *drive, ide_task_t *task) |
| 128 | { |
| 129 | ide_hwif_t *hwif = drive->hwif; |
| 130 | struct ide_io_ports *io_ports = &hwif->io_ports; |
| 131 | struct ide_taskfile *tf = &task->tf; |
| 132 | |
| 133 | if (task->tf_flags & IDE_TFLAG_IN_DATA) { |
| 134 | u16 data; |
| 135 | |
| 136 | /* no endian swap */ |
| 137 | data = __raw_readw((void __iomem *)io_ports->data_addr); |
| 138 | tf->data = data & 0xff; |
| 139 | tf->hob_data = (data >> 8) & 0xff; |
| 140 | } |
| 141 | |
| 142 | /* be sure we're looking at the low order bits */ |
| 143 | tx4938ide_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr); |
| 144 | |
| 145 | if (task->tf_flags & IDE_TFLAG_IN_FEATURE) |
| 146 | tf->feature = tx4938ide_inb(io_ports->feature_addr); |
| 147 | if (task->tf_flags & IDE_TFLAG_IN_NSECT) |
| 148 | tf->nsect = tx4938ide_inb(io_ports->nsect_addr); |
| 149 | if (task->tf_flags & IDE_TFLAG_IN_LBAL) |
| 150 | tf->lbal = tx4938ide_inb(io_ports->lbal_addr); |
| 151 | if (task->tf_flags & IDE_TFLAG_IN_LBAM) |
| 152 | tf->lbam = tx4938ide_inb(io_ports->lbam_addr); |
| 153 | if (task->tf_flags & IDE_TFLAG_IN_LBAH) |
| 154 | tf->lbah = tx4938ide_inb(io_ports->lbah_addr); |
| 155 | if (task->tf_flags & IDE_TFLAG_IN_DEVICE) |
| 156 | tf->device = tx4938ide_inb(io_ports->device_addr); |
| 157 | |
| 158 | if (task->tf_flags & IDE_TFLAG_LBA48) { |
| 159 | tx4938ide_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr); |
| 160 | |
| 161 | if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE) |
| 162 | tf->hob_feature = |
| 163 | tx4938ide_inb(io_ports->feature_addr); |
| 164 | if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT) |
| 165 | tf->hob_nsect = tx4938ide_inb(io_ports->nsect_addr); |
| 166 | if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL) |
| 167 | tf->hob_lbal = tx4938ide_inb(io_ports->lbal_addr); |
| 168 | if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM) |
| 169 | tf->hob_lbam = tx4938ide_inb(io_ports->lbam_addr); |
| 170 | if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH) |
| 171 | tf->hob_lbah = tx4938ide_inb(io_ports->lbah_addr); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void tx4938ide_input_data_swap(ide_drive_t *drive, struct request *rq, |
| 176 | void *buf, unsigned int len) |
| 177 | { |
| 178 | unsigned long port = drive->hwif->io_ports.data_addr; |
| 179 | unsigned short *ptr = buf; |
| 180 | unsigned int count = (len + 1) / 2; |
| 181 | |
| 182 | while (count--) |
| 183 | *ptr++ = cpu_to_le16(__raw_readw((void __iomem *)port)); |
Atsushi Nemoto | f26f6ce | 2008-12-29 20:27:29 +0100 | [diff] [blame] | 184 | __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2)); |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void tx4938ide_output_data_swap(ide_drive_t *drive, struct request *rq, |
| 188 | void *buf, unsigned int len) |
| 189 | { |
| 190 | unsigned long port = drive->hwif->io_ports.data_addr; |
| 191 | unsigned short *ptr = buf; |
| 192 | unsigned int count = (len + 1) / 2; |
| 193 | |
| 194 | while (count--) { |
| 195 | __raw_writew(le16_to_cpu(*ptr), (void __iomem *)port); |
| 196 | ptr++; |
| 197 | } |
Atsushi Nemoto | f26f6ce | 2008-12-29 20:27:29 +0100 | [diff] [blame] | 198 | __ide_flush_dcache_range((unsigned long)buf, roundup(len, 2)); |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static const struct ide_tp_ops tx4938ide_tp_ops = { |
| 202 | .exec_command = ide_exec_command, |
| 203 | .read_status = ide_read_status, |
| 204 | .read_altstatus = ide_read_altstatus, |
| 205 | .read_sff_dma_status = ide_read_sff_dma_status, |
| 206 | |
| 207 | .set_irq = ide_set_irq, |
| 208 | |
| 209 | .tf_load = tx4938ide_tf_load, |
| 210 | .tf_read = tx4938ide_tf_read, |
| 211 | |
| 212 | .input_data = tx4938ide_input_data_swap, |
| 213 | .output_data = tx4938ide_output_data_swap, |
| 214 | }; |
| 215 | |
| 216 | #endif /* __BIG_ENDIAN */ |
| 217 | |
| 218 | static const struct ide_port_ops tx4938ide_port_ops = { |
Bartlomiej Zolnierkiewicz | 3ee86dc | 2009-01-02 16:12:46 +0100 | [diff] [blame] | 219 | .set_pio_mode = tx4938ide_set_pio_mode, |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | static const struct ide_port_info tx4938ide_port_info __initdata = { |
Bartlomiej Zolnierkiewicz | 3ee86dc | 2009-01-02 16:12:46 +0100 | [diff] [blame] | 223 | .port_ops = &tx4938ide_port_ops, |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 224 | #ifdef __BIG_ENDIAN |
Bartlomiej Zolnierkiewicz | 3ee86dc | 2009-01-02 16:12:46 +0100 | [diff] [blame] | 225 | .tp_ops = &tx4938ide_tp_ops, |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 226 | #endif |
Bartlomiej Zolnierkiewicz | 3ee86dc | 2009-01-02 16:12:46 +0100 | [diff] [blame] | 227 | .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA, |
| 228 | .pio_mask = ATA_PIO5, |
Bartlomiej Zolnierkiewicz | b1d249e | 2009-01-02 16:12:47 +0100 | [diff] [blame] | 229 | .chipset = ide_generic, |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | static int __init tx4938ide_probe(struct platform_device *pdev) |
| 233 | { |
| 234 | hw_regs_t hw; |
| 235 | hw_regs_t *hws[] = { &hw, NULL, NULL, NULL }; |
| 236 | struct ide_host *host; |
| 237 | struct resource *res; |
| 238 | struct tx4938ide_platform_info *pdata = pdev->dev.platform_data; |
| 239 | int irq, ret, i; |
Atsushi Nemoto | 9d4eb0a | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 240 | unsigned long mapbase, mapctl; |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 241 | struct ide_port_info d = tx4938ide_port_info; |
| 242 | |
| 243 | irq = platform_get_irq(pdev, 0); |
| 244 | if (irq < 0) |
| 245 | return -ENODEV; |
| 246 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 247 | if (!res) |
| 248 | return -ENODEV; |
| 249 | |
| 250 | if (!devm_request_mem_region(&pdev->dev, res->start, |
| 251 | res->end - res->start + 1, "tx4938ide")) |
| 252 | return -EBUSY; |
| 253 | mapbase = (unsigned long)devm_ioremap(&pdev->dev, res->start, |
Atsushi Nemoto | 9d4eb0a | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 254 | 8 << pdata->ioport_shift); |
| 255 | mapctl = (unsigned long)devm_ioremap(&pdev->dev, |
| 256 | res->start + 0x10000 + |
| 257 | (6 << pdata->ioport_shift), |
| 258 | 1 << pdata->ioport_shift); |
| 259 | if (!mapbase || !mapctl) |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 260 | return -EBUSY; |
| 261 | |
| 262 | memset(&hw, 0, sizeof(hw)); |
| 263 | if (pdata->ioport_shift) { |
| 264 | unsigned long port = mapbase; |
Atsushi Nemoto | 9d4eb0a | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 265 | unsigned long ctl = mapctl; |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 266 | |
| 267 | hw.io_ports_array[0] = port; |
| 268 | #ifdef __BIG_ENDIAN |
| 269 | port++; |
Atsushi Nemoto | 9d4eb0a | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 270 | ctl++; |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 271 | #endif |
| 272 | for (i = 1; i <= 7; i++) |
| 273 | hw.io_ports_array[i] = |
| 274 | port + (i << pdata->ioport_shift); |
Atsushi Nemoto | 9d4eb0a | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 275 | hw.io_ports.ctl_addr = ctl; |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 276 | } else |
Atsushi Nemoto | 9d4eb0a | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 277 | ide_std_init_ports(&hw, mapbase, mapctl); |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 278 | hw.irq = irq; |
| 279 | hw.dev = &pdev->dev; |
| 280 | |
Atsushi Nemoto | 9d4eb0a | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 281 | pr_info("TX4938 IDE interface (base %#lx, ctl %#lx, irq %d)\n", |
| 282 | mapbase, mapctl, hw.irq); |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 283 | if (pdata->gbus_clock) |
| 284 | tx4938ide_tune_ebusc(pdata->ebus_ch, pdata->gbus_clock, 0); |
| 285 | else |
| 286 | d.port_ops = NULL; |
| 287 | ret = ide_host_add(&d, hws, &host); |
Atsushi Nemoto | 9d4eb0a | 2008-11-02 21:40:09 +0100 | [diff] [blame] | 288 | if (!ret) |
| 289 | platform_set_drvdata(pdev, host); |
| 290 | return ret; |
Atsushi Nemoto | 2850284 | 2008-10-23 23:22:08 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static int __exit tx4938ide_remove(struct platform_device *pdev) |
| 294 | { |
| 295 | struct ide_host *host = platform_get_drvdata(pdev); |
| 296 | |
| 297 | ide_host_remove(host); |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static struct platform_driver tx4938ide_driver = { |
| 302 | .driver = { |
| 303 | .name = "tx4938ide", |
| 304 | .owner = THIS_MODULE, |
| 305 | }, |
| 306 | .remove = __exit_p(tx4938ide_remove), |
| 307 | }; |
| 308 | |
| 309 | static int __init tx4938ide_init(void) |
| 310 | { |
| 311 | return platform_driver_probe(&tx4938ide_driver, tx4938ide_probe); |
| 312 | } |
| 313 | |
| 314 | static void __exit tx4938ide_exit(void) |
| 315 | { |
| 316 | platform_driver_unregister(&tx4938ide_driver); |
| 317 | } |
| 318 | |
| 319 | module_init(tx4938ide_init); |
| 320 | module_exit(tx4938ide_exit); |
| 321 | |
| 322 | MODULE_DESCRIPTION("TX4938 internal IDE driver"); |
| 323 | MODULE_LICENSE("GPL"); |
| 324 | MODULE_ALIAS("platform:tx4938ide"); |