1 /* 2 * Driver for the Octeon bootbus compact flash. 3 * 4 * This file is subject to the terms and conditions of the GNU General Public 5 * License. See the file "COPYING" in the main directory of this archive 6 * for more details. 7 * 8 * Copyright (C) 2005 - 2012 Cavium Inc. 9 * Copyright (C) 2008 Wind River Systems 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/libata.h> 15 #include <linux/hrtimer.h> 16 #include <linux/slab.h> 17 #include <linux/irq.h> 18 #include <linux/of.h> 19 #include <linux/of_platform.h> 20 #include <linux/platform_device.h> 21 #include <scsi/scsi_host.h> 22 #include <trace/events/libata.h> 23 #include <asm/byteorder.h> 24 #include <asm/octeon/octeon.h> 25 26 /* 27 * The Octeon bootbus compact flash interface is connected in at least 28 * 3 different configurations on various evaluation boards: 29 * 30 * -- 8 bits no irq, no DMA 31 * -- 16 bits no irq, no DMA 32 * -- 16 bits True IDE mode with DMA, but no irq. 33 * 34 * In the last case the DMA engine can generate an interrupt when the 35 * transfer is complete. For the first two cases only PIO is supported. 36 * 37 */ 38 39 #define DRV_NAME "pata_octeon_cf" 40 #define DRV_VERSION "2.2" 41 42 /* Poll interval in nS. */ 43 #define OCTEON_CF_BUSY_POLL_INTERVAL 500000 44 45 #define DMA_CFG 0 46 #define DMA_TIM 0x20 47 #define DMA_INT 0x38 48 #define DMA_INT_EN 0x50 49 50 struct octeon_cf_port { 51 struct hrtimer delayed_finish; 52 struct ata_port *ap; 53 int dma_finished; 54 void *c0; 55 unsigned int cs0; 56 unsigned int cs1; 57 bool is_true_ide; 58 u64 dma_base; 59 }; 60 61 static struct scsi_host_template octeon_cf_sht = { 62 ATA_PIO_SHT(DRV_NAME), 63 }; 64 65 static int enable_dma; 66 module_param(enable_dma, int, 0444); 67 MODULE_PARM_DESC(enable_dma, 68 "Enable use of DMA on interfaces that support it (0=no dma [default], 1=use dma)"); 69 70 /* 71 * Convert nanosecond based time to setting used in the 72 * boot bus timing register, based on timing multiple 73 */ 74 static unsigned int ns_to_tim_reg(unsigned int tim_mult, unsigned int nsecs) 75 { 76 /* 77 * Compute # of eclock periods to get desired duration in 78 * nanoseconds. 79 */ 80 return DIV_ROUND_UP(nsecs * (octeon_get_io_clock_rate() / 1000000), 81 1000 * tim_mult); 82 } 83 84 static void octeon_cf_set_boot_reg_cfg(int cs, unsigned int multiplier) 85 { 86 union cvmx_mio_boot_reg_cfgx reg_cfg; 87 unsigned int tim_mult; 88 89 switch (multiplier) { 90 case 8: 91 tim_mult = 3; 92 break; 93 case 4: 94 tim_mult = 0; 95 break; 96 case 2: 97 tim_mult = 2; 98 break; 99 default: 100 tim_mult = 1; 101 break; 102 } 103 104 reg_cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs)); 105 reg_cfg.s.dmack = 0; /* Don't assert DMACK on access */ 106 reg_cfg.s.tim_mult = tim_mult; /* Timing mutiplier */ 107 reg_cfg.s.rd_dly = 0; /* Sample on falling edge of BOOT_OE */ 108 reg_cfg.s.sam = 0; /* Don't combine write and output enable */ 109 reg_cfg.s.we_ext = 0; /* No write enable extension */ 110 reg_cfg.s.oe_ext = 0; /* No read enable extension */ 111 reg_cfg.s.en = 1; /* Enable this region */ 112 reg_cfg.s.orbit = 0; /* Don't combine with previous region */ 113 reg_cfg.s.ale = 0; /* Don't do address multiplexing */ 114 cvmx_write_csr(CVMX_MIO_BOOT_REG_CFGX(cs), reg_cfg.u64); 115 } 116 117 /* 118 * Called after libata determines the needed PIO mode. This 119 * function programs the Octeon bootbus regions to support the 120 * timing requirements of the PIO mode. 121 * 122 * @ap: ATA port information 123 * @dev: ATA device 124 */ 125 static void octeon_cf_set_piomode(struct ata_port *ap, struct ata_device *dev) 126 { 127 struct octeon_cf_port *cf_port = ap->private_data; 128 union cvmx_mio_boot_reg_timx reg_tim; 129 int T; 130 struct ata_timing timing; 131 132 unsigned int div; 133 int use_iordy; 134 int trh; 135 int pause; 136 /* These names are timing parameters from the ATA spec */ 137 int t2; 138 139 /* 140 * A divisor value of four will overflow the timing fields at 141 * clock rates greater than 800MHz 142 */ 143 if (octeon_get_io_clock_rate() <= 800000000) 144 div = 4; 145 else 146 div = 8; 147 T = (int)((1000000000000LL * div) / octeon_get_io_clock_rate()); 148 149 BUG_ON(ata_timing_compute(dev, dev->pio_mode, &timing, T, T)); 150 151 t2 = timing.active; 152 if (t2) 153 t2--; 154 155 trh = ns_to_tim_reg(div, 20); 156 if (trh) 157 trh--; 158 159 pause = (int)timing.cycle - (int)timing.active - 160 (int)timing.setup - trh; 161 if (pause < 0) 162 pause = 0; 163 if (pause) 164 pause--; 165 166 octeon_cf_set_boot_reg_cfg(cf_port->cs0, div); 167 if (cf_port->is_true_ide) 168 /* True IDE mode, program both chip selects. */ 169 octeon_cf_set_boot_reg_cfg(cf_port->cs1, div); 170 171 172 use_iordy = ata_pio_need_iordy(dev); 173 174 reg_tim.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_TIMX(cf_port->cs0)); 175 /* Disable page mode */ 176 reg_tim.s.pagem = 0; 177 /* Enable dynamic timing */ 178 reg_tim.s.waitm = use_iordy; 179 /* Pages are disabled */ 180 reg_tim.s.pages = 0; 181 /* We don't use multiplexed address mode */ 182 reg_tim.s.ale = 0; 183 /* Not used */ 184 reg_tim.s.page = 0; 185 /* Time after IORDY to coninue to assert the data */ 186 reg_tim.s.wait = 0; 187 /* Time to wait to complete the cycle. */ 188 reg_tim.s.pause = pause; 189 /* How long to hold after a write to de-assert CE. */ 190 reg_tim.s.wr_hld = trh; 191 /* How long to wait after a read to de-assert CE. */ 192 reg_tim.s.rd_hld = trh; 193 /* How long write enable is asserted */ 194 reg_tim.s.we = t2; 195 /* How long read enable is asserted */ 196 reg_tim.s.oe = t2; 197 /* Time after CE that read/write starts */ 198 reg_tim.s.ce = ns_to_tim_reg(div, 5); 199 /* Time before CE that address is valid */ 200 reg_tim.s.adr = 0; 201 202 /* Program the bootbus region timing for the data port chip select. */ 203 cvmx_write_csr(CVMX_MIO_BOOT_REG_TIMX(cf_port->cs0), reg_tim.u64); 204 if (cf_port->is_true_ide) 205 /* True IDE mode, program both chip selects. */ 206 cvmx_write_csr(CVMX_MIO_BOOT_REG_TIMX(cf_port->cs1), 207 reg_tim.u64); 208 } 209 210 static void octeon_cf_set_dmamode(struct ata_port *ap, struct ata_device *dev) 211 { 212 struct octeon_cf_port *cf_port = ap->private_data; 213 union cvmx_mio_boot_pin_defs pin_defs; 214 union cvmx_mio_boot_dma_timx dma_tim; 215 unsigned int oe_a; 216 unsigned int oe_n; 217 unsigned int dma_ackh; 218 unsigned int dma_arq; 219 unsigned int pause; 220 unsigned int T0, Tkr, Td; 221 unsigned int tim_mult; 222 int c; 223 224 const struct ata_timing *timing; 225 226 timing = ata_timing_find_mode(dev->dma_mode); 227 T0 = timing->cycle; 228 Td = timing->active; 229 Tkr = timing->recover; 230 dma_ackh = timing->dmack_hold; 231 232 dma_tim.u64 = 0; 233 /* dma_tim.s.tim_mult = 0 --> 4x */ 234 tim_mult = 4; 235 236 /* not spec'ed, value in eclocks, not affected by tim_mult */ 237 dma_arq = 8; 238 pause = 25 - dma_arq * 1000 / 239 (octeon_get_io_clock_rate() / 1000000); /* Tz */ 240 241 oe_a = Td; 242 /* Tkr from cf spec, lengthened to meet T0 */ 243 oe_n = max(T0 - oe_a, Tkr); 244 245 pin_defs.u64 = cvmx_read_csr(CVMX_MIO_BOOT_PIN_DEFS); 246 247 /* DMA channel number. */ 248 c = (cf_port->dma_base & 8) >> 3; 249 250 /* Invert the polarity if the default is 0*/ 251 dma_tim.s.dmack_pi = (pin_defs.u64 & (1ull << (11 + c))) ? 0 : 1; 252 253 dma_tim.s.oe_n = ns_to_tim_reg(tim_mult, oe_n); 254 dma_tim.s.oe_a = ns_to_tim_reg(tim_mult, oe_a); 255 256 /* 257 * This is tI, C.F. spec. says 0, but Sony CF card requires 258 * more, we use 20 nS. 259 */ 260 dma_tim.s.dmack_s = ns_to_tim_reg(tim_mult, 20); 261 dma_tim.s.dmack_h = ns_to_tim_reg(tim_mult, dma_ackh); 262 263 dma_tim.s.dmarq = dma_arq; 264 dma_tim.s.pause = ns_to_tim_reg(tim_mult, pause); 265 266 dma_tim.s.rd_dly = 0; /* Sample right on edge */ 267 268 /* writes only */ 269 dma_tim.s.we_n = ns_to_tim_reg(tim_mult, oe_n); 270 dma_tim.s.we_a = ns_to_tim_reg(tim_mult, oe_a); 271 272 ata_dev_dbg(dev, "ns to ticks (mult %d) of %d is: %d\n", tim_mult, 60, 273 ns_to_tim_reg(tim_mult, 60)); 274 ata_dev_dbg(dev, "oe_n: %d, oe_a: %d, dmack_s: %d, dmack_h: %d, dmarq: %d, pause: %d\n", 275 dma_tim.s.oe_n, dma_tim.s.oe_a, dma_tim.s.dmack_s, 276 dma_tim.s.dmack_h, dma_tim.s.dmarq, dma_tim.s.pause); 277 278 cvmx_write_csr(cf_port->dma_base + DMA_TIM, dma_tim.u64); 279 } 280 281 /* 282 * Handle an 8 bit I/O request. 283 * 284 * @qc: Queued command 285 * @buffer: Data buffer 286 * @buflen: Length of the buffer. 287 * @rw: True to write. 288 */ 289 static unsigned int octeon_cf_data_xfer8(struct ata_queued_cmd *qc, 290 unsigned char *buffer, 291 unsigned int buflen, 292 int rw) 293 { 294 struct ata_port *ap = qc->dev->link->ap; 295 void __iomem *data_addr = ap->ioaddr.data_addr; 296 unsigned long words; 297 int count; 298 299 words = buflen; 300 if (rw) { 301 count = 16; 302 while (words--) { 303 iowrite8(*buffer, data_addr); 304 buffer++; 305 /* 306 * Every 16 writes do a read so the bootbus 307 * FIFO doesn't fill up. 308 */ 309 if (--count == 0) { 310 ioread8(ap->ioaddr.altstatus_addr); 311 count = 16; 312 } 313 } 314 } else { 315 ioread8_rep(data_addr, buffer, words); 316 } 317 return buflen; 318 } 319 320 /* 321 * Handle a 16 bit I/O request. 322 * 323 * @qc: Queued command 324 * @buffer: Data buffer 325 * @buflen: Length of the buffer. 326 * @rw: True to write. 327 */ 328 static unsigned int octeon_cf_data_xfer16(struct ata_queued_cmd *qc, 329 unsigned char *buffer, 330 unsigned int buflen, 331 int rw) 332 { 333 struct ata_port *ap = qc->dev->link->ap; 334 void __iomem *data_addr = ap->ioaddr.data_addr; 335 unsigned long words; 336 int count; 337 338 words = buflen / 2; 339 if (rw) { 340 count = 16; 341 while (words--) { 342 iowrite16(*(uint16_t *)buffer, data_addr); 343 buffer += sizeof(uint16_t); 344 /* 345 * Every 16 writes do a read so the bootbus 346 * FIFO doesn't fill up. 347 */ 348 if (--count == 0) { 349 ioread8(ap->ioaddr.altstatus_addr); 350 count = 16; 351 } 352 } 353 } else { 354 while (words--) { 355 *(uint16_t *)buffer = ioread16(data_addr); 356 buffer += sizeof(uint16_t); 357 } 358 } 359 /* Transfer trailing 1 byte, if any. */ 360 if (unlikely(buflen & 0x01)) { 361 __le16 align_buf[1] = { 0 }; 362 363 if (rw == READ) { 364 align_buf[0] = cpu_to_le16(ioread16(data_addr)); 365 memcpy(buffer, align_buf, 1); 366 } else { 367 memcpy(align_buf, buffer, 1); 368 iowrite16(le16_to_cpu(align_buf[0]), data_addr); 369 } 370 words++; 371 } 372 return buflen; 373 } 374 375 /* 376 * Read the taskfile for 16bit non-True IDE only. 377 */ 378 static void octeon_cf_tf_read16(struct ata_port *ap, struct ata_taskfile *tf) 379 { 380 u16 blob; 381 /* The base of the registers is at ioaddr.data_addr. */ 382 void __iomem *base = ap->ioaddr.data_addr; 383 384 blob = __raw_readw(base + 0xc); 385 tf->error = blob >> 8; 386 387 blob = __raw_readw(base + 2); 388 tf->nsect = blob & 0xff; 389 tf->lbal = blob >> 8; 390 391 blob = __raw_readw(base + 4); 392 tf->lbam = blob & 0xff; 393 tf->lbah = blob >> 8; 394 395 blob = __raw_readw(base + 6); 396 tf->device = blob & 0xff; 397 tf->status = blob >> 8; 398 399 if (tf->flags & ATA_TFLAG_LBA48) { 400 if (likely(ap->ioaddr.ctl_addr)) { 401 iowrite8(tf->ctl | ATA_HOB, ap->ioaddr.ctl_addr); 402 403 blob = __raw_readw(base + 0xc); 404 tf->hob_feature = blob >> 8; 405 406 blob = __raw_readw(base + 2); 407 tf->hob_nsect = blob & 0xff; 408 tf->hob_lbal = blob >> 8; 409 410 blob = __raw_readw(base + 4); 411 tf->hob_lbam = blob & 0xff; 412 tf->hob_lbah = blob >> 8; 413 414 iowrite8(tf->ctl, ap->ioaddr.ctl_addr); 415 ap->last_ctl = tf->ctl; 416 } else { 417 WARN_ON(1); 418 } 419 } 420 } 421 422 static u8 octeon_cf_check_status16(struct ata_port *ap) 423 { 424 u16 blob; 425 void __iomem *base = ap->ioaddr.data_addr; 426 427 blob = __raw_readw(base + 6); 428 return blob >> 8; 429 } 430 431 static int octeon_cf_softreset16(struct ata_link *link, unsigned int *classes, 432 unsigned long deadline) 433 { 434 struct ata_port *ap = link->ap; 435 void __iomem *base = ap->ioaddr.data_addr; 436 int rc; 437 u8 err; 438 439 __raw_writew(ap->ctl, base + 0xe); 440 udelay(20); 441 __raw_writew(ap->ctl | ATA_SRST, base + 0xe); 442 udelay(20); 443 __raw_writew(ap->ctl, base + 0xe); 444 445 rc = ata_sff_wait_after_reset(link, 1, deadline); 446 if (rc) { 447 ata_link_err(link, "SRST failed (errno=%d)\n", rc); 448 return rc; 449 } 450 451 /* determine by signature whether we have ATA or ATAPI devices */ 452 classes[0] = ata_sff_dev_classify(&link->device[0], 1, &err); 453 return 0; 454 } 455 456 /* 457 * Load the taskfile for 16bit non-True IDE only. The device_addr is 458 * not loaded, we do this as part of octeon_cf_exec_command16. 459 */ 460 static void octeon_cf_tf_load16(struct ata_port *ap, 461 const struct ata_taskfile *tf) 462 { 463 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR; 464 /* The base of the registers is at ioaddr.data_addr. */ 465 void __iomem *base = ap->ioaddr.data_addr; 466 467 if (tf->ctl != ap->last_ctl) { 468 iowrite8(tf->ctl, ap->ioaddr.ctl_addr); 469 ap->last_ctl = tf->ctl; 470 ata_wait_idle(ap); 471 } 472 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) { 473 __raw_writew(tf->hob_feature << 8, base + 0xc); 474 __raw_writew(tf->hob_nsect | tf->hob_lbal << 8, base + 2); 475 __raw_writew(tf->hob_lbam | tf->hob_lbah << 8, base + 4); 476 } 477 if (is_addr) { 478 __raw_writew(tf->feature << 8, base + 0xc); 479 __raw_writew(tf->nsect | tf->lbal << 8, base + 2); 480 __raw_writew(tf->lbam | tf->lbah << 8, base + 4); 481 } 482 ata_wait_idle(ap); 483 } 484 485 486 static void octeon_cf_dev_select(struct ata_port *ap, unsigned int device) 487 { 488 /* There is only one device, do nothing. */ 489 return; 490 } 491 492 /* 493 * Issue ATA command to host controller. The device_addr is also sent 494 * as it must be written in a combined write with the command. 495 */ 496 static void octeon_cf_exec_command16(struct ata_port *ap, 497 const struct ata_taskfile *tf) 498 { 499 /* The base of the registers is at ioaddr.data_addr. */ 500 void __iomem *base = ap->ioaddr.data_addr; 501 u16 blob = 0; 502 503 if (tf->flags & ATA_TFLAG_DEVICE) 504 blob = tf->device; 505 506 blob |= (tf->command << 8); 507 __raw_writew(blob, base + 6); 508 509 ata_wait_idle(ap); 510 } 511 512 static void octeon_cf_ata_port_noaction(struct ata_port *ap) 513 { 514 } 515 516 static void octeon_cf_dma_setup(struct ata_queued_cmd *qc) 517 { 518 struct ata_port *ap = qc->ap; 519 struct octeon_cf_port *cf_port; 520 521 cf_port = ap->private_data; 522 /* issue r/w command */ 523 qc->cursg = qc->sg; 524 cf_port->dma_finished = 0; 525 ap->ops->sff_exec_command(ap, &qc->tf); 526 } 527 528 /* 529 * Start a DMA transfer that was already setup 530 * 531 * @qc: Information about the DMA 532 */ 533 static void octeon_cf_dma_start(struct ata_queued_cmd *qc) 534 { 535 struct octeon_cf_port *cf_port = qc->ap->private_data; 536 union cvmx_mio_boot_dma_cfgx mio_boot_dma_cfg; 537 union cvmx_mio_boot_dma_intx mio_boot_dma_int; 538 struct scatterlist *sg; 539 540 /* Get the scatter list entry we need to DMA into */ 541 sg = qc->cursg; 542 BUG_ON(!sg); 543 544 /* 545 * Clear the DMA complete status. 546 */ 547 mio_boot_dma_int.u64 = 0; 548 mio_boot_dma_int.s.done = 1; 549 cvmx_write_csr(cf_port->dma_base + DMA_INT, mio_boot_dma_int.u64); 550 551 /* Enable the interrupt. */ 552 cvmx_write_csr(cf_port->dma_base + DMA_INT_EN, mio_boot_dma_int.u64); 553 554 /* Set the direction of the DMA */ 555 mio_boot_dma_cfg.u64 = 0; 556 #ifdef __LITTLE_ENDIAN 557 mio_boot_dma_cfg.s.endian = 1; 558 #endif 559 mio_boot_dma_cfg.s.en = 1; 560 mio_boot_dma_cfg.s.rw = ((qc->tf.flags & ATA_TFLAG_WRITE) != 0); 561 562 /* 563 * Don't stop the DMA if the device deasserts DMARQ. Many 564 * compact flashes deassert DMARQ for a short time between 565 * sectors. Instead of stopping and restarting the DMA, we'll 566 * let the hardware do it. If the DMA is really stopped early 567 * due to an error condition, a later timeout will force us to 568 * stop. 569 */ 570 mio_boot_dma_cfg.s.clr = 0; 571 572 /* Size is specified in 16bit words and minus one notation */ 573 mio_boot_dma_cfg.s.size = sg_dma_len(sg) / 2 - 1; 574 575 /* We need to swap the high and low bytes of every 16 bits */ 576 mio_boot_dma_cfg.s.swap8 = 1; 577 578 mio_boot_dma_cfg.s.adr = sg_dma_address(sg); 579 580 cvmx_write_csr(cf_port->dma_base + DMA_CFG, mio_boot_dma_cfg.u64); 581 } 582 583 /* 584 * 585 * LOCKING: 586 * spin_lock_irqsave(host lock) 587 * 588 */ 589 static unsigned int octeon_cf_dma_finished(struct ata_port *ap, 590 struct ata_queued_cmd *qc) 591 { 592 struct ata_eh_info *ehi = &ap->link.eh_info; 593 struct octeon_cf_port *cf_port = ap->private_data; 594 union cvmx_mio_boot_dma_cfgx dma_cfg; 595 union cvmx_mio_boot_dma_intx dma_int; 596 u8 status; 597 598 trace_ata_bmdma_stop(ap, &qc->tf, qc->tag); 599 600 if (ap->hsm_task_state != HSM_ST_LAST) 601 return 0; 602 603 dma_cfg.u64 = cvmx_read_csr(cf_port->dma_base + DMA_CFG); 604 if (dma_cfg.s.size != 0xfffff) { 605 /* Error, the transfer was not complete. */ 606 qc->err_mask |= AC_ERR_HOST_BUS; 607 ap->hsm_task_state = HSM_ST_ERR; 608 } 609 610 /* Stop and clear the dma engine. */ 611 dma_cfg.u64 = 0; 612 dma_cfg.s.size = -1; 613 cvmx_write_csr(cf_port->dma_base + DMA_CFG, dma_cfg.u64); 614 615 /* Disable the interrupt. */ 616 dma_int.u64 = 0; 617 cvmx_write_csr(cf_port->dma_base + DMA_INT_EN, dma_int.u64); 618 619 /* Clear the DMA complete status */ 620 dma_int.s.done = 1; 621 cvmx_write_csr(cf_port->dma_base + DMA_INT, dma_int.u64); 622 623 status = ap->ops->sff_check_status(ap); 624 625 ata_sff_hsm_move(ap, qc, status, 0); 626 627 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA)) 628 ata_ehi_push_desc(ehi, "DMA stat 0x%x", status); 629 630 return 1; 631 } 632 633 /* 634 * Check if any queued commands have more DMAs, if so start the next 635 * transfer, else do end of transfer handling. 636 */ 637 static irqreturn_t octeon_cf_interrupt(int irq, void *dev_instance) 638 { 639 struct ata_host *host = dev_instance; 640 struct octeon_cf_port *cf_port; 641 int i; 642 unsigned int handled = 0; 643 unsigned long flags; 644 645 spin_lock_irqsave(&host->lock, flags); 646 647 for (i = 0; i < host->n_ports; i++) { 648 u8 status; 649 struct ata_port *ap; 650 struct ata_queued_cmd *qc; 651 union cvmx_mio_boot_dma_intx dma_int; 652 union cvmx_mio_boot_dma_cfgx dma_cfg; 653 654 ap = host->ports[i]; 655 cf_port = ap->private_data; 656 657 dma_int.u64 = cvmx_read_csr(cf_port->dma_base + DMA_INT); 658 dma_cfg.u64 = cvmx_read_csr(cf_port->dma_base + DMA_CFG); 659 660 qc = ata_qc_from_tag(ap, ap->link.active_tag); 661 662 if (!qc || (qc->tf.flags & ATA_TFLAG_POLLING)) 663 continue; 664 665 if (dma_int.s.done && !dma_cfg.s.en) { 666 if (!sg_is_last(qc->cursg)) { 667 qc->cursg = sg_next(qc->cursg); 668 handled = 1; 669 trace_ata_bmdma_start(ap, &qc->tf, qc->tag); 670 octeon_cf_dma_start(qc); 671 continue; 672 } else { 673 cf_port->dma_finished = 1; 674 } 675 } 676 if (!cf_port->dma_finished) 677 continue; 678 status = ioread8(ap->ioaddr.altstatus_addr); 679 if (status & (ATA_BUSY | ATA_DRQ)) { 680 /* 681 * We are busy, try to handle it later. This 682 * is the DMA finished interrupt, and it could 683 * take a little while for the card to be 684 * ready for more commands. 685 */ 686 /* Clear DMA irq. */ 687 dma_int.u64 = 0; 688 dma_int.s.done = 1; 689 cvmx_write_csr(cf_port->dma_base + DMA_INT, 690 dma_int.u64); 691 hrtimer_start_range_ns(&cf_port->delayed_finish, 692 ns_to_ktime(OCTEON_CF_BUSY_POLL_INTERVAL), 693 OCTEON_CF_BUSY_POLL_INTERVAL / 5, 694 HRTIMER_MODE_REL); 695 handled = 1; 696 } else { 697 handled |= octeon_cf_dma_finished(ap, qc); 698 } 699 } 700 spin_unlock_irqrestore(&host->lock, flags); 701 return IRQ_RETVAL(handled); 702 } 703 704 static enum hrtimer_restart octeon_cf_delayed_finish(struct hrtimer *hrt) 705 { 706 struct octeon_cf_port *cf_port = container_of(hrt, 707 struct octeon_cf_port, 708 delayed_finish); 709 struct ata_port *ap = cf_port->ap; 710 struct ata_host *host = ap->host; 711 struct ata_queued_cmd *qc; 712 unsigned long flags; 713 u8 status; 714 enum hrtimer_restart rv = HRTIMER_NORESTART; 715 716 spin_lock_irqsave(&host->lock, flags); 717 718 /* 719 * If the port is not waiting for completion, it must have 720 * handled it previously. The hsm_task_state is 721 * protected by host->lock. 722 */ 723 if (ap->hsm_task_state != HSM_ST_LAST || !cf_port->dma_finished) 724 goto out; 725 726 status = ioread8(ap->ioaddr.altstatus_addr); 727 if (status & (ATA_BUSY | ATA_DRQ)) { 728 /* Still busy, try again. */ 729 hrtimer_forward_now(hrt, 730 ns_to_ktime(OCTEON_CF_BUSY_POLL_INTERVAL)); 731 rv = HRTIMER_RESTART; 732 goto out; 733 } 734 qc = ata_qc_from_tag(ap, ap->link.active_tag); 735 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING))) 736 octeon_cf_dma_finished(ap, qc); 737 out: 738 spin_unlock_irqrestore(&host->lock, flags); 739 return rv; 740 } 741 742 static void octeon_cf_dev_config(struct ata_device *dev) 743 { 744 /* 745 * A maximum of 2^20 - 1 16 bit transfers are possible with 746 * the bootbus DMA. So we need to throttle max_sectors to 747 * (2^12 - 1 == 4095) to assure that this can never happen. 748 */ 749 dev->max_sectors = min(dev->max_sectors, 4095U); 750 } 751 752 /* 753 * We don't do ATAPI DMA so return 0. 754 */ 755 static int octeon_cf_check_atapi_dma(struct ata_queued_cmd *qc) 756 { 757 return 0; 758 } 759 760 static unsigned int octeon_cf_qc_issue(struct ata_queued_cmd *qc) 761 { 762 struct ata_port *ap = qc->ap; 763 764 switch (qc->tf.protocol) { 765 case ATA_PROT_DMA: 766 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING); 767 768 trace_ata_tf_load(ap, &qc->tf); 769 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */ 770 trace_ata_bmdma_setup(ap, &qc->tf, qc->tag); 771 octeon_cf_dma_setup(qc); /* set up dma */ 772 trace_ata_bmdma_start(ap, &qc->tf, qc->tag); 773 octeon_cf_dma_start(qc); /* initiate dma */ 774 ap->hsm_task_state = HSM_ST_LAST; 775 break; 776 777 case ATAPI_PROT_DMA: 778 dev_err(ap->dev, "Error, ATAPI not supported\n"); 779 BUG(); 780 781 default: 782 return ata_sff_qc_issue(qc); 783 } 784 785 return 0; 786 } 787 788 static struct ata_port_operations octeon_cf_ops = { 789 .inherits = &ata_sff_port_ops, 790 .check_atapi_dma = octeon_cf_check_atapi_dma, 791 .qc_prep = ata_noop_qc_prep, 792 .qc_issue = octeon_cf_qc_issue, 793 .sff_dev_select = octeon_cf_dev_select, 794 .sff_irq_on = octeon_cf_ata_port_noaction, 795 .sff_irq_clear = octeon_cf_ata_port_noaction, 796 .cable_detect = ata_cable_40wire, 797 .set_piomode = octeon_cf_set_piomode, 798 .set_dmamode = octeon_cf_set_dmamode, 799 .dev_config = octeon_cf_dev_config, 800 }; 801 802 static int octeon_cf_probe(struct platform_device *pdev) 803 { 804 struct resource *res_cs0, *res_cs1; 805 806 bool is_16bit; 807 const __be32 *cs_num; 808 struct property *reg_prop; 809 int n_addr, n_size, reg_len; 810 struct device_node *node; 811 void __iomem *cs0; 812 void __iomem *cs1 = NULL; 813 struct ata_host *host; 814 struct ata_port *ap; 815 int irq = 0; 816 irq_handler_t irq_handler = NULL; 817 void __iomem *base; 818 struct octeon_cf_port *cf_port; 819 int rv = -ENOMEM; 820 u32 bus_width; 821 822 node = pdev->dev.of_node; 823 if (node == NULL) 824 return -EINVAL; 825 826 cf_port = devm_kzalloc(&pdev->dev, sizeof(*cf_port), GFP_KERNEL); 827 if (!cf_port) 828 return -ENOMEM; 829 830 cf_port->is_true_ide = of_property_read_bool(node, "cavium,true-ide"); 831 832 if (of_property_read_u32(node, "cavium,bus-width", &bus_width) == 0) 833 is_16bit = (bus_width == 16); 834 else 835 is_16bit = false; 836 837 n_addr = of_n_addr_cells(node); 838 n_size = of_n_size_cells(node); 839 840 reg_prop = of_find_property(node, "reg", ®_len); 841 if (!reg_prop || reg_len < sizeof(__be32)) 842 return -EINVAL; 843 844 cs_num = reg_prop->value; 845 cf_port->cs0 = be32_to_cpup(cs_num); 846 847 if (cf_port->is_true_ide) { 848 struct device_node *dma_node; 849 dma_node = of_parse_phandle(node, 850 "cavium,dma-engine-handle", 0); 851 if (dma_node) { 852 struct platform_device *dma_dev; 853 dma_dev = of_find_device_by_node(dma_node); 854 if (dma_dev) { 855 struct resource *res_dma; 856 int i; 857 res_dma = platform_get_resource(dma_dev, IORESOURCE_MEM, 0); 858 if (!res_dma) { 859 put_device(&dma_dev->dev); 860 of_node_put(dma_node); 861 return -EINVAL; 862 } 863 cf_port->dma_base = (u64)devm_ioremap(&pdev->dev, res_dma->start, 864 resource_size(res_dma)); 865 if (!cf_port->dma_base) { 866 put_device(&dma_dev->dev); 867 of_node_put(dma_node); 868 return -EINVAL; 869 } 870 871 i = platform_get_irq(dma_dev, 0); 872 if (i > 0) { 873 irq = i; 874 irq_handler = octeon_cf_interrupt; 875 } 876 put_device(&dma_dev->dev); 877 } 878 of_node_put(dma_node); 879 } 880 res_cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1); 881 if (!res_cs1) 882 return -EINVAL; 883 884 cs1 = devm_ioremap(&pdev->dev, res_cs1->start, 885 resource_size(res_cs1)); 886 if (!cs1) 887 return rv; 888 889 if (reg_len < (n_addr + n_size + 1) * sizeof(__be32)) 890 return -EINVAL; 891 892 cs_num += n_addr + n_size; 893 cf_port->cs1 = be32_to_cpup(cs_num); 894 } 895 896 res_cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0); 897 if (!res_cs0) 898 return -EINVAL; 899 900 cs0 = devm_ioremap(&pdev->dev, res_cs0->start, 901 resource_size(res_cs0)); 902 if (!cs0) 903 return rv; 904 905 /* allocate host */ 906 host = ata_host_alloc(&pdev->dev, 1); 907 if (!host) 908 return rv; 909 910 ap = host->ports[0]; 911 ap->private_data = cf_port; 912 pdev->dev.platform_data = cf_port; 913 cf_port->ap = ap; 914 ap->ops = &octeon_cf_ops; 915 ap->pio_mask = ATA_PIO6; 916 ap->flags |= ATA_FLAG_NO_ATAPI | ATA_FLAG_PIO_POLLING; 917 918 if (!is_16bit) { 919 base = cs0 + 0x800; 920 ap->ioaddr.cmd_addr = base; 921 ata_sff_std_ports(&ap->ioaddr); 922 923 ap->ioaddr.altstatus_addr = base + 0xe; 924 ap->ioaddr.ctl_addr = base + 0xe; 925 octeon_cf_ops.sff_data_xfer = octeon_cf_data_xfer8; 926 } else if (cf_port->is_true_ide) { 927 base = cs0; 928 ap->ioaddr.cmd_addr = base + (ATA_REG_CMD << 1) + 1; 929 ap->ioaddr.data_addr = base + (ATA_REG_DATA << 1); 930 ap->ioaddr.error_addr = base + (ATA_REG_ERR << 1) + 1; 931 ap->ioaddr.feature_addr = base + (ATA_REG_FEATURE << 1) + 1; 932 ap->ioaddr.nsect_addr = base + (ATA_REG_NSECT << 1) + 1; 933 ap->ioaddr.lbal_addr = base + (ATA_REG_LBAL << 1) + 1; 934 ap->ioaddr.lbam_addr = base + (ATA_REG_LBAM << 1) + 1; 935 ap->ioaddr.lbah_addr = base + (ATA_REG_LBAH << 1) + 1; 936 ap->ioaddr.device_addr = base + (ATA_REG_DEVICE << 1) + 1; 937 ap->ioaddr.status_addr = base + (ATA_REG_STATUS << 1) + 1; 938 ap->ioaddr.command_addr = base + (ATA_REG_CMD << 1) + 1; 939 ap->ioaddr.altstatus_addr = cs1 + (6 << 1) + 1; 940 ap->ioaddr.ctl_addr = cs1 + (6 << 1) + 1; 941 octeon_cf_ops.sff_data_xfer = octeon_cf_data_xfer16; 942 943 ap->mwdma_mask = enable_dma ? ATA_MWDMA4 : 0; 944 945 /* True IDE mode needs a timer to poll for not-busy. */ 946 hrtimer_init(&cf_port->delayed_finish, CLOCK_MONOTONIC, 947 HRTIMER_MODE_REL); 948 cf_port->delayed_finish.function = octeon_cf_delayed_finish; 949 } else { 950 /* 16 bit but not True IDE */ 951 base = cs0 + 0x800; 952 octeon_cf_ops.sff_data_xfer = octeon_cf_data_xfer16; 953 octeon_cf_ops.softreset = octeon_cf_softreset16; 954 octeon_cf_ops.sff_check_status = octeon_cf_check_status16; 955 octeon_cf_ops.sff_tf_read = octeon_cf_tf_read16; 956 octeon_cf_ops.sff_tf_load = octeon_cf_tf_load16; 957 octeon_cf_ops.sff_exec_command = octeon_cf_exec_command16; 958 959 ap->ioaddr.data_addr = base + ATA_REG_DATA; 960 ap->ioaddr.nsect_addr = base + ATA_REG_NSECT; 961 ap->ioaddr.lbal_addr = base + ATA_REG_LBAL; 962 ap->ioaddr.ctl_addr = base + 0xe; 963 ap->ioaddr.altstatus_addr = base + 0xe; 964 } 965 cf_port->c0 = ap->ioaddr.ctl_addr; 966 967 rv = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)); 968 if (rv) 969 return rv; 970 971 ata_port_desc(ap, "cmd %p ctl %p", base, ap->ioaddr.ctl_addr); 972 973 dev_info(&pdev->dev, "version " DRV_VERSION" %d bit%s.\n", 974 is_16bit ? 16 : 8, 975 cf_port->is_true_ide ? ", True IDE" : ""); 976 977 return ata_host_activate(host, irq, irq_handler, 978 IRQF_SHARED, &octeon_cf_sht); 979 } 980 981 static void octeon_cf_shutdown(struct device *dev) 982 { 983 union cvmx_mio_boot_dma_cfgx dma_cfg; 984 union cvmx_mio_boot_dma_intx dma_int; 985 986 struct octeon_cf_port *cf_port = dev_get_platdata(dev); 987 988 if (cf_port->dma_base) { 989 /* Stop and clear the dma engine. */ 990 dma_cfg.u64 = 0; 991 dma_cfg.s.size = -1; 992 cvmx_write_csr(cf_port->dma_base + DMA_CFG, dma_cfg.u64); 993 994 /* Disable the interrupt. */ 995 dma_int.u64 = 0; 996 cvmx_write_csr(cf_port->dma_base + DMA_INT_EN, dma_int.u64); 997 998 /* Clear the DMA complete status */ 999 dma_int.s.done = 1; 1000 cvmx_write_csr(cf_port->dma_base + DMA_INT, dma_int.u64); 1001 1002 __raw_writeb(0, cf_port->c0); 1003 udelay(20); 1004 __raw_writeb(ATA_SRST, cf_port->c0); 1005 udelay(20); 1006 __raw_writeb(0, cf_port->c0); 1007 mdelay(100); 1008 } 1009 } 1010 1011 static const struct of_device_id octeon_cf_match[] = { 1012 { .compatible = "cavium,ebt3000-compact-flash", }, 1013 { /* sentinel */ } 1014 }; 1015 MODULE_DEVICE_TABLE(of, octeon_cf_match); 1016 1017 static struct platform_driver octeon_cf_driver = { 1018 .probe = octeon_cf_probe, 1019 .driver = { 1020 .name = DRV_NAME, 1021 .of_match_table = octeon_cf_match, 1022 .shutdown = octeon_cf_shutdown 1023 }, 1024 }; 1025 1026 static int __init octeon_cf_init(void) 1027 { 1028 return platform_driver_register(&octeon_cf_driver); 1029 } 1030 1031 1032 MODULE_AUTHOR("David Daney <ddaney@caviumnetworks.com>"); 1033 MODULE_DESCRIPTION("low-level driver for Cavium OCTEON Compact Flash PATA"); 1034 MODULE_LICENSE("GPL"); 1035 MODULE_VERSION(DRV_VERSION); 1036 MODULE_ALIAS("platform:" DRV_NAME); 1037 1038 module_init(octeon_cf_init); 1039