| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Atmel MultiMedia Card Interface driver |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2004-2008 Atmel Corporation |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | | - * it under the terms of the GNU General Public License version 2 as |
|---|
| 8 | | - * published by the Free Software Foundation. |
|---|
| 9 | 6 | */ |
|---|
| 10 | 7 | #include <linux/blkdev.h> |
|---|
| 11 | 8 | #include <linux/clk.h> |
|---|
| .. | .. |
|---|
| 172 | 169 | #define atmci_writel(port, reg, value) \ |
|---|
| 173 | 170 | __raw_writel((value), (port)->regs + reg) |
|---|
| 174 | 171 | |
|---|
| 172 | +#define ATMCI_CMD_TIMEOUT_MS 2000 |
|---|
| 175 | 173 | #define AUTOSUSPEND_DELAY 50 |
|---|
| 176 | 174 | |
|---|
| 177 | 175 | #define ATMCI_DATA_ERROR_FLAGS (ATMCI_DCRCE | ATMCI_DTOE | ATMCI_OVRE | ATMCI_UNRE) |
|---|
| .. | .. |
|---|
| 227 | 225 | * @lock: Spinlock protecting the queue and associated data. |
|---|
| 228 | 226 | * @regs: Pointer to MMIO registers. |
|---|
| 229 | 227 | * @sg: Scatterlist entry currently being processed by PIO or PDC code. |
|---|
| 228 | + * @sg_len: Size of the scatterlist |
|---|
| 230 | 229 | * @pio_offset: Offset into the current scatterlist entry. |
|---|
| 231 | 230 | * @buffer: Buffer used if we don't have the r/w proof capability. We |
|---|
| 232 | 231 | * don't have the time to switch pdc buffers so we have to use only |
|---|
| 233 | 232 | * one buffer for the full transaction. |
|---|
| 234 | 233 | * @buf_size: size of the buffer. |
|---|
| 235 | | - * @phys_buf_addr: buffer address needed for pdc. |
|---|
| 234 | + * @buf_phys_addr: buffer address needed for pdc. |
|---|
| 236 | 235 | * @cur_slot: The slot which is currently using the controller. |
|---|
| 237 | 236 | * @mrq: The request currently being processed on @cur_slot, |
|---|
| 238 | 237 | * or NULL if the controller is idle. |
|---|
| .. | .. |
|---|
| 242 | 241 | * @data_size: just data->blocks * data->blksz. |
|---|
| 243 | 242 | * @dma: DMA client state. |
|---|
| 244 | 243 | * @data_chan: DMA channel being used for the current data transfer. |
|---|
| 244 | + * @dma_conf: Configuration for the DMA slave |
|---|
| 245 | 245 | * @cmd_status: Snapshot of SR taken upon completion of the current |
|---|
| 246 | 246 | * command. Only valid when EVENT_CMD_COMPLETE is pending. |
|---|
| 247 | 247 | * @data_status: Snapshot of SR taken upon completion of the current |
|---|
| .. | .. |
|---|
| 446 | 446 | return 0; |
|---|
| 447 | 447 | } |
|---|
| 448 | 448 | |
|---|
| 449 | | -static int atmci_req_open(struct inode *inode, struct file *file) |
|---|
| 450 | | -{ |
|---|
| 451 | | - return single_open(file, atmci_req_show, inode->i_private); |
|---|
| 452 | | -} |
|---|
| 453 | | - |
|---|
| 454 | | -static const struct file_operations atmci_req_fops = { |
|---|
| 455 | | - .owner = THIS_MODULE, |
|---|
| 456 | | - .open = atmci_req_open, |
|---|
| 457 | | - .read = seq_read, |
|---|
| 458 | | - .llseek = seq_lseek, |
|---|
| 459 | | - .release = single_release, |
|---|
| 460 | | -}; |
|---|
| 449 | +DEFINE_SHOW_ATTRIBUTE(atmci_req); |
|---|
| 461 | 450 | |
|---|
| 462 | 451 | static void atmci_show_status_reg(struct seq_file *s, |
|---|
| 463 | 452 | const char *regname, u32 value) |
|---|
| .. | .. |
|---|
| 583 | 572 | return ret; |
|---|
| 584 | 573 | } |
|---|
| 585 | 574 | |
|---|
| 586 | | -static int atmci_regs_open(struct inode *inode, struct file *file) |
|---|
| 587 | | -{ |
|---|
| 588 | | - return single_open(file, atmci_regs_show, inode->i_private); |
|---|
| 589 | | -} |
|---|
| 590 | | - |
|---|
| 591 | | -static const struct file_operations atmci_regs_fops = { |
|---|
| 592 | | - .owner = THIS_MODULE, |
|---|
| 593 | | - .open = atmci_regs_open, |
|---|
| 594 | | - .read = seq_read, |
|---|
| 595 | | - .llseek = seq_lseek, |
|---|
| 596 | | - .release = single_release, |
|---|
| 597 | | -}; |
|---|
| 575 | +DEFINE_SHOW_ATTRIBUTE(atmci_regs); |
|---|
| 598 | 576 | |
|---|
| 599 | 577 | static void atmci_init_debugfs(struct atmel_mci_slot *slot) |
|---|
| 600 | 578 | { |
|---|
| 601 | 579 | struct mmc_host *mmc = slot->mmc; |
|---|
| 602 | 580 | struct atmel_mci *host = slot->host; |
|---|
| 603 | 581 | struct dentry *root; |
|---|
| 604 | | - struct dentry *node; |
|---|
| 605 | 582 | |
|---|
| 606 | 583 | root = mmc->debugfs_root; |
|---|
| 607 | 584 | if (!root) |
|---|
| 608 | 585 | return; |
|---|
| 609 | 586 | |
|---|
| 610 | | - node = debugfs_create_file("regs", S_IRUSR, root, host, |
|---|
| 611 | | - &atmci_regs_fops); |
|---|
| 612 | | - if (IS_ERR(node)) |
|---|
| 613 | | - return; |
|---|
| 614 | | - if (!node) |
|---|
| 615 | | - goto err; |
|---|
| 616 | | - |
|---|
| 617 | | - node = debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops); |
|---|
| 618 | | - if (!node) |
|---|
| 619 | | - goto err; |
|---|
| 620 | | - |
|---|
| 621 | | - node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state); |
|---|
| 622 | | - if (!node) |
|---|
| 623 | | - goto err; |
|---|
| 624 | | - |
|---|
| 625 | | - node = debugfs_create_x32("pending_events", S_IRUSR, root, |
|---|
| 626 | | - (u32 *)&host->pending_events); |
|---|
| 627 | | - if (!node) |
|---|
| 628 | | - goto err; |
|---|
| 629 | | - |
|---|
| 630 | | - node = debugfs_create_x32("completed_events", S_IRUSR, root, |
|---|
| 631 | | - (u32 *)&host->completed_events); |
|---|
| 632 | | - if (!node) |
|---|
| 633 | | - goto err; |
|---|
| 634 | | - |
|---|
| 635 | | - return; |
|---|
| 636 | | - |
|---|
| 637 | | -err: |
|---|
| 638 | | - dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n"); |
|---|
| 587 | + debugfs_create_file("regs", S_IRUSR, root, host, &atmci_regs_fops); |
|---|
| 588 | + debugfs_create_file("req", S_IRUSR, root, slot, &atmci_req_fops); |
|---|
| 589 | + debugfs_create_u32("state", S_IRUSR, root, &host->state); |
|---|
| 590 | + debugfs_create_xul("pending_events", S_IRUSR, root, |
|---|
| 591 | + &host->pending_events); |
|---|
| 592 | + debugfs_create_xul("completed_events", S_IRUSR, root, |
|---|
| 593 | + &host->completed_events); |
|---|
| 639 | 594 | } |
|---|
| 640 | 595 | |
|---|
| 641 | 596 | #if defined(CONFIG_OF) |
|---|
| .. | .. |
|---|
| 856 | 811 | static void atmci_send_command(struct atmel_mci *host, |
|---|
| 857 | 812 | struct mmc_command *cmd, u32 cmd_flags) |
|---|
| 858 | 813 | { |
|---|
| 814 | + unsigned int timeout_ms = cmd->busy_timeout ? cmd->busy_timeout : |
|---|
| 815 | + ATMCI_CMD_TIMEOUT_MS; |
|---|
| 816 | + |
|---|
| 859 | 817 | WARN_ON(host->cmd); |
|---|
| 860 | 818 | host->cmd = cmd; |
|---|
| 861 | 819 | |
|---|
| .. | .. |
|---|
| 865 | 823 | |
|---|
| 866 | 824 | atmci_writel(host, ATMCI_ARGR, cmd->arg); |
|---|
| 867 | 825 | atmci_writel(host, ATMCI_CMDR, cmd_flags); |
|---|
| 826 | + |
|---|
| 827 | + mod_timer(&host->timer, jiffies + msecs_to_jiffies(timeout_ms)); |
|---|
| 868 | 828 | } |
|---|
| 869 | 829 | |
|---|
| 870 | 830 | static void atmci_send_stop_cmd(struct atmel_mci *host, struct mmc_data *data) |
|---|
| .. | .. |
|---|
| 1362 | 1322 | * prepared yet.) |
|---|
| 1363 | 1323 | */ |
|---|
| 1364 | 1324 | atmci_writel(host, ATMCI_IER, iflags); |
|---|
| 1365 | | - |
|---|
| 1366 | | - mod_timer(&host->timer, jiffies + msecs_to_jiffies(2000)); |
|---|
| 1367 | 1325 | } |
|---|
| 1368 | 1326 | |
|---|
| 1369 | 1327 | static void atmci_queue_request(struct atmel_mci *host, |
|---|
| .. | .. |
|---|
| 1430 | 1388 | break; |
|---|
| 1431 | 1389 | case MMC_BUS_WIDTH_4: |
|---|
| 1432 | 1390 | slot->sdc_reg |= ATMCI_SDCBUS_4BIT; |
|---|
| 1391 | + break; |
|---|
| 1392 | + case MMC_BUS_WIDTH_8: |
|---|
| 1393 | + slot->sdc_reg |= ATMCI_SDCBUS_8BIT; |
|---|
| 1433 | 1394 | break; |
|---|
| 1434 | 1395 | } |
|---|
| 1435 | 1396 | |
|---|
| .. | .. |
|---|
| 1602 | 1563 | |
|---|
| 1603 | 1564 | WARN_ON(host->cmd || host->data); |
|---|
| 1604 | 1565 | |
|---|
| 1566 | + del_timer(&host->timer); |
|---|
| 1567 | + |
|---|
| 1605 | 1568 | /* |
|---|
| 1606 | 1569 | * Update the MMC clock rate if necessary. This may be |
|---|
| 1607 | 1570 | * necessary if set_ios() is called when a different slot is |
|---|
| .. | .. |
|---|
| 1627 | 1590 | dev_vdbg(&host->pdev->dev, "list empty\n"); |
|---|
| 1628 | 1591 | host->state = STATE_IDLE; |
|---|
| 1629 | 1592 | } |
|---|
| 1630 | | - |
|---|
| 1631 | | - del_timer(&host->timer); |
|---|
| 1632 | 1593 | |
|---|
| 1633 | 1594 | spin_unlock(&host->lock); |
|---|
| 1634 | 1595 | mmc_request_done(prev_mmc, mrq); |
|---|
| .. | .. |
|---|
| 1857 | 1818 | atmci_writel(host, ATMCI_IER, ATMCI_NOTBUSY); |
|---|
| 1858 | 1819 | state = STATE_WAITING_NOTBUSY; |
|---|
| 1859 | 1820 | } else if (host->mrq->stop) { |
|---|
| 1860 | | - atmci_writel(host, ATMCI_IER, ATMCI_CMDRDY); |
|---|
| 1861 | 1821 | atmci_send_stop_cmd(host, data); |
|---|
| 1862 | 1822 | state = STATE_SENDING_STOP; |
|---|
| 1863 | 1823 | } else { |
|---|
| .. | .. |
|---|
| 1890 | 1850 | * command to send. |
|---|
| 1891 | 1851 | */ |
|---|
| 1892 | 1852 | if (host->mrq->stop) { |
|---|
| 1893 | | - atmci_writel(host, ATMCI_IER, |
|---|
| 1894 | | - ATMCI_CMDRDY); |
|---|
| 1895 | 1853 | atmci_send_stop_cmd(host, data); |
|---|
| 1896 | 1854 | state = STATE_SENDING_STOP; |
|---|
| 1897 | 1855 | } else { |
|---|
| .. | .. |
|---|
| 2262 | 2220 | { |
|---|
| 2263 | 2221 | struct mmc_host *mmc; |
|---|
| 2264 | 2222 | struct atmel_mci_slot *slot; |
|---|
| 2223 | + int ret; |
|---|
| 2265 | 2224 | |
|---|
| 2266 | 2225 | mmc = mmc_alloc_host(sizeof(struct atmel_mci_slot), &host->pdev->dev); |
|---|
| 2267 | 2226 | if (!mmc) |
|---|
| .. | .. |
|---|
| 2296 | 2255 | * use only one bit for data to prevent fifo underruns and overruns |
|---|
| 2297 | 2256 | * which will corrupt data. |
|---|
| 2298 | 2257 | */ |
|---|
| 2299 | | - if ((slot_data->bus_width >= 4) && host->caps.has_rwproof) |
|---|
| 2258 | + if ((slot_data->bus_width >= 4) && host->caps.has_rwproof) { |
|---|
| 2300 | 2259 | mmc->caps |= MMC_CAP_4_BIT_DATA; |
|---|
| 2260 | + if (slot_data->bus_width >= 8) |
|---|
| 2261 | + mmc->caps |= MMC_CAP_8_BIT_DATA; |
|---|
| 2262 | + } |
|---|
| 2301 | 2263 | |
|---|
| 2302 | 2264 | if (atmci_get_version(host) < 0x200) { |
|---|
| 2303 | 2265 | mmc->max_segs = 256; |
|---|
| .. | .. |
|---|
| 2342 | 2304 | |
|---|
| 2343 | 2305 | host->slot[id] = slot; |
|---|
| 2344 | 2306 | mmc_regulator_get_supply(mmc); |
|---|
| 2345 | | - mmc_add_host(mmc); |
|---|
| 2307 | + ret = mmc_add_host(mmc); |
|---|
| 2308 | + if (ret) { |
|---|
| 2309 | + mmc_free_host(mmc); |
|---|
| 2310 | + return ret; |
|---|
| 2311 | + } |
|---|
| 2346 | 2312 | |
|---|
| 2347 | 2313 | if (gpio_is_valid(slot->detect_pin)) { |
|---|
| 2348 | | - int ret; |
|---|
| 2349 | | - |
|---|
| 2350 | 2314 | timer_setup(&slot->detect_timer, atmci_detect_change, 0); |
|---|
| 2351 | 2315 | |
|---|
| 2352 | 2316 | ret = request_irq(gpio_to_irq(slot->detect_pin), |
|---|
| .. | .. |
|---|
| 2389 | 2353 | |
|---|
| 2390 | 2354 | static int atmci_configure_dma(struct atmel_mci *host) |
|---|
| 2391 | 2355 | { |
|---|
| 2392 | | - host->dma.chan = dma_request_slave_channel_reason(&host->pdev->dev, |
|---|
| 2393 | | - "rxtx"); |
|---|
| 2356 | + host->dma.chan = dma_request_chan(&host->pdev->dev, "rxtx"); |
|---|
| 2394 | 2357 | |
|---|
| 2395 | 2358 | if (PTR_ERR(host->dma.chan) == -ENODEV) { |
|---|
| 2396 | 2359 | struct mci_platform_data *pdata = host->pdev->dev.platform_data; |
|---|
| .. | .. |
|---|
| 2455 | 2418 | case 0x600: |
|---|
| 2456 | 2419 | case 0x500: |
|---|
| 2457 | 2420 | host->caps.has_odd_clk_div = 1; |
|---|
| 2421 | + fallthrough; |
|---|
| 2458 | 2422 | case 0x400: |
|---|
| 2459 | 2423 | case 0x300: |
|---|
| 2460 | 2424 | host->caps.has_dma_conf_reg = 1; |
|---|
| .. | .. |
|---|
| 2462 | 2426 | host->caps.has_cfg_reg = 1; |
|---|
| 2463 | 2427 | host->caps.has_cstor_reg = 1; |
|---|
| 2464 | 2428 | host->caps.has_highspeed = 1; |
|---|
| 2429 | + fallthrough; |
|---|
| 2465 | 2430 | case 0x200: |
|---|
| 2466 | 2431 | host->caps.has_rwproof = 1; |
|---|
| 2467 | 2432 | host->caps.need_blksz_mul_4 = 0; |
|---|
| 2468 | 2433 | host->caps.need_notbusy_for_read_ops = 1; |
|---|
| 2434 | + fallthrough; |
|---|
| 2469 | 2435 | case 0x100: |
|---|
| 2470 | 2436 | host->caps.has_bad_data_ordering = 0; |
|---|
| 2471 | 2437 | host->caps.need_reset_after_xfer = 0; |
|---|
| 2438 | + fallthrough; |
|---|
| 2472 | 2439 | case 0x0: |
|---|
| 2473 | 2440 | break; |
|---|
| 2474 | 2441 | default: |
|---|
| .. | .. |
|---|
| 2684 | 2651 | { |
|---|
| 2685 | 2652 | struct atmel_mci *host = dev_get_drvdata(dev); |
|---|
| 2686 | 2653 | |
|---|
| 2687 | | - pinctrl_pm_select_default_state(dev); |
|---|
| 2654 | + pinctrl_select_default_state(dev); |
|---|
| 2688 | 2655 | |
|---|
| 2689 | 2656 | return clk_prepare_enable(host->mck); |
|---|
| 2690 | 2657 | } |
|---|