Lines Matching +full:mmc +full:- +full:host
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
6 * JZ4740 SD/MMC controller driver
13 #include <linux/dma-mapping.h>
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/slot-gpio.h>
131 * The MMC core allows to prepare a mmc_request while another mmc_request
132 * is in-flight. This is used via the pre_req/post_req hooks.
150 struct mmc_host *mmc; member
189 static void jz4740_mmc_write_irq_mask(struct jz4740_mmc_host *host, in jz4740_mmc_write_irq_mask() argument
192 if (host->version >= JZ_MMC_JZ4725B) in jz4740_mmc_write_irq_mask()
193 return writel(val, host->base + JZ_REG_MMC_IMASK); in jz4740_mmc_write_irq_mask()
195 return writew(val, host->base + JZ_REG_MMC_IMASK); in jz4740_mmc_write_irq_mask()
198 static void jz4740_mmc_write_irq_reg(struct jz4740_mmc_host *host, in jz4740_mmc_write_irq_reg() argument
201 if (host->version >= JZ_MMC_JZ4780) in jz4740_mmc_write_irq_reg()
202 writel(val, host->base + JZ_REG_MMC_IREG); in jz4740_mmc_write_irq_reg()
204 writew(val, host->base + JZ_REG_MMC_IREG); in jz4740_mmc_write_irq_reg()
207 static uint32_t jz4740_mmc_read_irq_reg(struct jz4740_mmc_host *host) in jz4740_mmc_read_irq_reg() argument
209 if (host->version >= JZ_MMC_JZ4780) in jz4740_mmc_read_irq_reg()
210 return readl(host->base + JZ_REG_MMC_IREG); in jz4740_mmc_read_irq_reg()
212 return readw(host->base + JZ_REG_MMC_IREG); in jz4740_mmc_read_irq_reg()
215 /*----------------------------------------------------------------------------*/
218 static void jz4740_mmc_release_dma_channels(struct jz4740_mmc_host *host) in jz4740_mmc_release_dma_channels() argument
220 if (!host->use_dma) in jz4740_mmc_release_dma_channels()
223 dma_release_channel(host->dma_tx); in jz4740_mmc_release_dma_channels()
224 if (host->dma_rx) in jz4740_mmc_release_dma_channels()
225 dma_release_channel(host->dma_rx); in jz4740_mmc_release_dma_channels()
228 static int jz4740_mmc_acquire_dma_channels(struct jz4740_mmc_host *host) in jz4740_mmc_acquire_dma_channels() argument
230 struct device *dev = mmc_dev(host->mmc); in jz4740_mmc_acquire_dma_channels()
232 host->dma_tx = dma_request_chan(dev, "tx-rx"); in jz4740_mmc_acquire_dma_channels()
233 if (!IS_ERR(host->dma_tx)) in jz4740_mmc_acquire_dma_channels()
236 if (PTR_ERR(host->dma_tx) != -ENODEV) { in jz4740_mmc_acquire_dma_channels()
237 dev_err(dev, "Failed to get dma tx-rx channel\n"); in jz4740_mmc_acquire_dma_channels()
238 return PTR_ERR(host->dma_tx); in jz4740_mmc_acquire_dma_channels()
241 host->dma_tx = dma_request_chan(mmc_dev(host->mmc), "tx"); in jz4740_mmc_acquire_dma_channels()
242 if (IS_ERR(host->dma_tx)) { in jz4740_mmc_acquire_dma_channels()
243 dev_err(mmc_dev(host->mmc), "Failed to get dma_tx channel\n"); in jz4740_mmc_acquire_dma_channels()
244 return PTR_ERR(host->dma_tx); in jz4740_mmc_acquire_dma_channels()
247 host->dma_rx = dma_request_chan(mmc_dev(host->mmc), "rx"); in jz4740_mmc_acquire_dma_channels()
248 if (IS_ERR(host->dma_rx)) { in jz4740_mmc_acquire_dma_channels()
249 dev_err(mmc_dev(host->mmc), "Failed to get dma_rx channel\n"); in jz4740_mmc_acquire_dma_channels()
250 dma_release_channel(host->dma_tx); in jz4740_mmc_acquire_dma_channels()
251 return PTR_ERR(host->dma_rx); in jz4740_mmc_acquire_dma_channels()
258 if (host->dma_tx) { in jz4740_mmc_acquire_dma_channels()
259 struct device *dev = host->dma_tx->device->dev; in jz4740_mmc_acquire_dma_channels()
262 if (max_seg_size < host->mmc->max_seg_size) in jz4740_mmc_acquire_dma_channels()
263 host->mmc->max_seg_size = max_seg_size; in jz4740_mmc_acquire_dma_channels()
266 if (host->dma_rx) { in jz4740_mmc_acquire_dma_channels()
267 struct device *dev = host->dma_rx->device->dev; in jz4740_mmc_acquire_dma_channels()
270 if (max_seg_size < host->mmc->max_seg_size) in jz4740_mmc_acquire_dma_channels()
271 host->mmc->max_seg_size = max_seg_size; in jz4740_mmc_acquire_dma_channels()
277 static inline struct dma_chan *jz4740_mmc_get_dma_chan(struct jz4740_mmc_host *host, in jz4740_mmc_get_dma_chan() argument
280 if ((data->flags & MMC_DATA_READ) && host->dma_rx) in jz4740_mmc_get_dma_chan()
281 return host->dma_rx; in jz4740_mmc_get_dma_chan()
283 return host->dma_tx; in jz4740_mmc_get_dma_chan()
286 static void jz4740_mmc_dma_unmap(struct jz4740_mmc_host *host, in jz4740_mmc_dma_unmap() argument
289 struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data); in jz4740_mmc_dma_unmap()
292 dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir); in jz4740_mmc_dma_unmap()
293 data->host_cookie = COOKIE_UNMAPPED; in jz4740_mmc_dma_unmap()
297 * A request can be in-flight when this is called.
299 static int jz4740_mmc_prepare_dma_data(struct jz4740_mmc_host *host, in jz4740_mmc_prepare_dma_data() argument
303 struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data); in jz4740_mmc_prepare_dma_data()
307 if (data->host_cookie == COOKIE_PREMAPPED) in jz4740_mmc_prepare_dma_data()
308 return data->sg_count; in jz4740_mmc_prepare_dma_data()
310 sg_count = dma_map_sg(chan->device->dev, in jz4740_mmc_prepare_dma_data()
311 data->sg, in jz4740_mmc_prepare_dma_data()
312 data->sg_len, in jz4740_mmc_prepare_dma_data()
316 dev_err(mmc_dev(host->mmc), in jz4740_mmc_prepare_dma_data()
318 return -EINVAL; in jz4740_mmc_prepare_dma_data()
321 data->sg_count = sg_count; in jz4740_mmc_prepare_dma_data()
322 data->host_cookie = cookie; in jz4740_mmc_prepare_dma_data()
324 return data->sg_count; in jz4740_mmc_prepare_dma_data()
327 static int jz4740_mmc_start_dma_transfer(struct jz4740_mmc_host *host, in jz4740_mmc_start_dma_transfer() argument
330 struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data); in jz4740_mmc_start_dma_transfer()
340 if (data->flags & MMC_DATA_WRITE) { in jz4740_mmc_start_dma_transfer()
342 conf.dst_addr = host->mem_res->start + JZ_REG_MMC_TXFIFO; in jz4740_mmc_start_dma_transfer()
345 conf.src_addr = host->mem_res->start + JZ_REG_MMC_RXFIFO; in jz4740_mmc_start_dma_transfer()
348 sg_count = jz4740_mmc_prepare_dma_data(host, data, COOKIE_MAPPED); in jz4740_mmc_start_dma_transfer()
353 desc = dmaengine_prep_slave_sg(chan, data->sg, sg_count, in jz4740_mmc_start_dma_transfer()
357 dev_err(mmc_dev(host->mmc), in jz4740_mmc_start_dma_transfer()
369 if (data->host_cookie == COOKIE_MAPPED) in jz4740_mmc_start_dma_transfer()
370 jz4740_mmc_dma_unmap(host, data); in jz4740_mmc_start_dma_transfer()
371 return -ENOMEM; in jz4740_mmc_start_dma_transfer()
374 static void jz4740_mmc_pre_request(struct mmc_host *mmc, in jz4740_mmc_pre_request() argument
377 struct jz4740_mmc_host *host = mmc_priv(mmc); in jz4740_mmc_pre_request() local
378 struct mmc_data *data = mrq->data; in jz4740_mmc_pre_request()
380 if (!host->use_dma) in jz4740_mmc_pre_request()
383 data->host_cookie = COOKIE_UNMAPPED; in jz4740_mmc_pre_request()
384 if (jz4740_mmc_prepare_dma_data(host, data, COOKIE_PREMAPPED) < 0) in jz4740_mmc_pre_request()
385 data->host_cookie = COOKIE_UNMAPPED; in jz4740_mmc_pre_request()
388 static void jz4740_mmc_post_request(struct mmc_host *mmc, in jz4740_mmc_post_request() argument
392 struct jz4740_mmc_host *host = mmc_priv(mmc); in jz4740_mmc_post_request() local
393 struct mmc_data *data = mrq->data; in jz4740_mmc_post_request()
395 if (data && data->host_cookie != COOKIE_UNMAPPED) in jz4740_mmc_post_request()
396 jz4740_mmc_dma_unmap(host, data); in jz4740_mmc_post_request()
399 struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data); in jz4740_mmc_post_request()
405 /*----------------------------------------------------------------------------*/
407 static void jz4740_mmc_set_irq_enabled(struct jz4740_mmc_host *host, in jz4740_mmc_set_irq_enabled() argument
412 spin_lock_irqsave(&host->lock, flags); in jz4740_mmc_set_irq_enabled()
414 host->irq_mask &= ~irq; in jz4740_mmc_set_irq_enabled()
416 host->irq_mask |= irq; in jz4740_mmc_set_irq_enabled()
418 jz4740_mmc_write_irq_mask(host, host->irq_mask); in jz4740_mmc_set_irq_enabled()
419 spin_unlock_irqrestore(&host->lock, flags); in jz4740_mmc_set_irq_enabled()
422 static void jz4740_mmc_clock_enable(struct jz4740_mmc_host *host, in jz4740_mmc_clock_enable() argument
430 writew(val, host->base + JZ_REG_MMC_STRPCL); in jz4740_mmc_clock_enable()
433 static void jz4740_mmc_clock_disable(struct jz4740_mmc_host *host) in jz4740_mmc_clock_disable() argument
438 writew(JZ_MMC_STRPCL_CLOCK_STOP, host->base + JZ_REG_MMC_STRPCL); in jz4740_mmc_clock_disable()
440 status = readl(host->base + JZ_REG_MMC_STATUS); in jz4740_mmc_clock_disable()
441 } while (status & JZ_MMC_STATUS_CLK_EN && --timeout); in jz4740_mmc_clock_disable()
444 static void jz4740_mmc_reset(struct jz4740_mmc_host *host) in jz4740_mmc_reset() argument
449 writew(JZ_MMC_STRPCL_RESET, host->base + JZ_REG_MMC_STRPCL); in jz4740_mmc_reset()
452 status = readl(host->base + JZ_REG_MMC_STATUS); in jz4740_mmc_reset()
453 } while (status & JZ_MMC_STATUS_IS_RESETTING && --timeout); in jz4740_mmc_reset()
456 static void jz4740_mmc_request_done(struct jz4740_mmc_host *host) in jz4740_mmc_request_done() argument
461 req = host->req; in jz4740_mmc_request_done()
462 data = req->data; in jz4740_mmc_request_done()
463 host->req = NULL; in jz4740_mmc_request_done()
465 if (data && data->host_cookie == COOKIE_MAPPED) in jz4740_mmc_request_done()
466 jz4740_mmc_dma_unmap(host, data); in jz4740_mmc_request_done()
467 mmc_request_done(host->mmc, req); in jz4740_mmc_request_done()
470 static unsigned int jz4740_mmc_poll_irq(struct jz4740_mmc_host *host, in jz4740_mmc_poll_irq() argument
477 status = jz4740_mmc_read_irq_reg(host); in jz4740_mmc_poll_irq()
478 } while (!(status & irq) && --timeout); in jz4740_mmc_poll_irq()
481 set_bit(0, &host->waiting); in jz4740_mmc_poll_irq()
482 mod_timer(&host->timeout_timer, in jz4740_mmc_poll_irq()
484 jz4740_mmc_set_irq_enabled(host, irq, true); in jz4740_mmc_poll_irq()
491 static void jz4740_mmc_transfer_check_state(struct jz4740_mmc_host *host, in jz4740_mmc_transfer_check_state() argument
496 status = readl(host->base + JZ_REG_MMC_STATUS); in jz4740_mmc_transfer_check_state()
499 host->req->cmd->error = -ETIMEDOUT; in jz4740_mmc_transfer_check_state()
500 data->error = -ETIMEDOUT; in jz4740_mmc_transfer_check_state()
502 host->req->cmd->error = -EIO; in jz4740_mmc_transfer_check_state()
503 data->error = -EIO; in jz4740_mmc_transfer_check_state()
507 host->req->cmd->error = -ETIMEDOUT; in jz4740_mmc_transfer_check_state()
508 data->error = -ETIMEDOUT; in jz4740_mmc_transfer_check_state()
510 host->req->cmd->error = -EIO; in jz4740_mmc_transfer_check_state()
511 data->error = -EIO; in jz4740_mmc_transfer_check_state()
516 static bool jz4740_mmc_write_data(struct jz4740_mmc_host *host, in jz4740_mmc_write_data() argument
519 struct sg_mapping_iter *miter = &host->miter; in jz4740_mmc_write_data()
520 void __iomem *fifo_addr = host->base + JZ_REG_MMC_TXFIFO; in jz4740_mmc_write_data()
526 buf = miter->addr; in jz4740_mmc_write_data()
527 i = miter->length / 4; in jz4740_mmc_write_data()
531 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ); in jz4740_mmc_write_data()
544 --j; in jz4740_mmc_write_data()
547 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ); in jz4740_mmc_write_data()
554 --i; in jz4740_mmc_write_data()
557 data->bytes_xfered += miter->length; in jz4740_mmc_write_data()
564 miter->consumed = (void *)buf - miter->addr; in jz4740_mmc_write_data()
565 data->bytes_xfered += miter->consumed; in jz4740_mmc_write_data()
571 static bool jz4740_mmc_read_data(struct jz4740_mmc_host *host, in jz4740_mmc_read_data() argument
574 struct sg_mapping_iter *miter = &host->miter; in jz4740_mmc_read_data()
575 void __iomem *fifo_addr = host->base + JZ_REG_MMC_RXFIFO; in jz4740_mmc_read_data()
583 buf = miter->addr; in jz4740_mmc_read_data()
584 i = miter->length; in jz4740_mmc_read_data()
588 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ); in jz4740_mmc_read_data()
602 --j; in jz4740_mmc_read_data()
606 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ); in jz4740_mmc_read_data()
612 i -= 4; in jz4740_mmc_read_data()
619 data->bytes_xfered += miter->length; in jz4740_mmc_read_data()
626 status = readl(host->base + JZ_REG_MMC_STATUS); in jz4740_mmc_read_data()
627 while (!(status & JZ_MMC_STATUS_DATA_FIFO_EMPTY) && --timeout) { in jz4740_mmc_read_data()
629 status = readl(host->base + JZ_REG_MMC_STATUS); in jz4740_mmc_read_data()
635 miter->consumed = (void *)buf - miter->addr; in jz4740_mmc_read_data()
636 data->bytes_xfered += miter->consumed; in jz4740_mmc_read_data()
644 struct jz4740_mmc_host *host = from_timer(host, t, timeout_timer); in jz4740_mmc_timeout() local
646 if (!test_and_clear_bit(0, &host->waiting)) in jz4740_mmc_timeout()
649 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, false); in jz4740_mmc_timeout()
651 host->req->cmd->error = -ETIMEDOUT; in jz4740_mmc_timeout()
652 jz4740_mmc_request_done(host); in jz4740_mmc_timeout()
655 static void jz4740_mmc_read_response(struct jz4740_mmc_host *host, in jz4740_mmc_read_response() argument
660 void __iomem *fifo_addr = host->base + JZ_REG_MMC_RESP_FIFO; in jz4740_mmc_read_response()
662 if (cmd->flags & MMC_RSP_136) { in jz4740_mmc_read_response()
665 cmd->resp[i] = tmp << 24; in jz4740_mmc_read_response()
667 cmd->resp[i] |= tmp << 8; in jz4740_mmc_read_response()
669 cmd->resp[i] |= tmp >> 8; in jz4740_mmc_read_response()
672 cmd->resp[0] = readw(fifo_addr) << 24; in jz4740_mmc_read_response()
673 cmd->resp[0] |= readw(fifo_addr) << 8; in jz4740_mmc_read_response()
674 cmd->resp[0] |= readw(fifo_addr) & 0xff; in jz4740_mmc_read_response()
678 static void jz4740_mmc_send_command(struct jz4740_mmc_host *host, in jz4740_mmc_send_command() argument
681 uint32_t cmdat = host->cmdat; in jz4740_mmc_send_command()
683 host->cmdat &= ~JZ_MMC_CMDAT_INIT; in jz4740_mmc_send_command()
684 jz4740_mmc_clock_disable(host); in jz4740_mmc_send_command()
686 host->cmd = cmd; in jz4740_mmc_send_command()
688 if (cmd->flags & MMC_RSP_BUSY) in jz4740_mmc_send_command()
706 if (cmd->data) { in jz4740_mmc_send_command()
708 if (cmd->data->flags & MMC_DATA_WRITE) in jz4740_mmc_send_command()
710 if (host->use_dma) { in jz4740_mmc_send_command()
712 * The JZ4780's MMC controller has integrated DMA ability in jz4740_mmc_send_command()
720 if (host->version >= JZ_MMC_JZ4780) { in jz4740_mmc_send_command()
722 host->base + JZ_REG_MMC_DMAC); in jz4740_mmc_send_command()
726 } else if (host->version >= JZ_MMC_JZ4780) { in jz4740_mmc_send_command()
727 writel(0, host->base + JZ_REG_MMC_DMAC); in jz4740_mmc_send_command()
730 writew(cmd->data->blksz, host->base + JZ_REG_MMC_BLKLEN); in jz4740_mmc_send_command()
731 writew(cmd->data->blocks, host->base + JZ_REG_MMC_NOB); in jz4740_mmc_send_command()
734 writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD); in jz4740_mmc_send_command()
735 writel(cmd->arg, host->base + JZ_REG_MMC_ARG); in jz4740_mmc_send_command()
736 writel(cmdat, host->base + JZ_REG_MMC_CMDAT); in jz4740_mmc_send_command()
738 jz4740_mmc_clock_enable(host, 1); in jz4740_mmc_send_command()
741 static void jz_mmc_prepare_data_transfer(struct jz4740_mmc_host *host) in jz_mmc_prepare_data_transfer() argument
743 struct mmc_command *cmd = host->req->cmd; in jz_mmc_prepare_data_transfer()
744 struct mmc_data *data = cmd->data; in jz_mmc_prepare_data_transfer()
747 if (data->flags & MMC_DATA_READ) in jz_mmc_prepare_data_transfer()
752 sg_miter_start(&host->miter, data->sg, data->sg_len, direction); in jz_mmc_prepare_data_transfer()
758 struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)devid; in jz_mmc_irq_worker() local
759 struct mmc_command *cmd = host->req->cmd; in jz_mmc_irq_worker()
760 struct mmc_request *req = host->req; in jz_mmc_irq_worker()
761 struct mmc_data *data = cmd->data; in jz_mmc_irq_worker()
764 if (cmd->error) in jz_mmc_irq_worker()
765 host->state = JZ4740_MMC_STATE_DONE; in jz_mmc_irq_worker()
767 switch (host->state) { in jz_mmc_irq_worker()
769 if (cmd->flags & MMC_RSP_PRESENT) in jz_mmc_irq_worker()
770 jz4740_mmc_read_response(host, cmd); in jz_mmc_irq_worker()
775 jz_mmc_prepare_data_transfer(host); in jz_mmc_irq_worker()
779 if (host->use_dma) { in jz_mmc_irq_worker()
786 timeout = jz4740_mmc_start_dma_transfer(host, data); in jz_mmc_irq_worker()
787 data->bytes_xfered = data->blocks * data->blksz; in jz_mmc_irq_worker()
788 } else if (data->flags & MMC_DATA_READ) in jz_mmc_irq_worker()
794 timeout = jz4740_mmc_read_data(host, data); in jz_mmc_irq_worker()
796 timeout = jz4740_mmc_write_data(host, data); in jz_mmc_irq_worker()
799 host->state = JZ4740_MMC_STATE_TRANSFER_DATA; in jz_mmc_irq_worker()
803 jz4740_mmc_transfer_check_state(host, data); in jz_mmc_irq_worker()
805 timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_DATA_TRAN_DONE); in jz_mmc_irq_worker()
807 host->state = JZ4740_MMC_STATE_SEND_STOP; in jz_mmc_irq_worker()
810 jz4740_mmc_write_irq_reg(host, JZ_MMC_IRQ_DATA_TRAN_DONE); in jz_mmc_irq_worker()
814 if (!req->stop) in jz_mmc_irq_worker()
817 jz4740_mmc_send_command(host, req->stop); in jz_mmc_irq_worker()
819 if (mmc_resp_type(req->stop) & MMC_RSP_BUSY) { in jz_mmc_irq_worker()
820 timeout = jz4740_mmc_poll_irq(host, in jz_mmc_irq_worker()
823 host->state = JZ4740_MMC_STATE_DONE; in jz_mmc_irq_worker()
834 jz4740_mmc_request_done(host); in jz_mmc_irq_worker()
841 struct jz4740_mmc_host *host = devid; in jz_mmc_irq() local
842 struct mmc_command *cmd = host->cmd; in jz_mmc_irq()
845 status = readl(host->base + JZ_REG_MMC_STATUS); in jz_mmc_irq()
846 irq_reg = jz4740_mmc_read_irq_reg(host); in jz_mmc_irq()
849 irq_reg &= ~host->irq_mask; in jz_mmc_irq()
855 jz4740_mmc_write_irq_reg(host, tmp & ~irq_reg); in jz_mmc_irq()
858 jz4740_mmc_write_irq_reg(host, JZ_MMC_IRQ_SDIO); in jz_mmc_irq()
859 mmc_signal_sdio_irq(host->mmc); in jz_mmc_irq()
863 if (host->req && cmd && irq_reg) { in jz_mmc_irq()
864 if (test_and_clear_bit(0, &host->waiting)) { in jz_mmc_irq()
865 del_timer(&host->timeout_timer); in jz_mmc_irq()
868 cmd->error = -ETIMEDOUT; in jz_mmc_irq()
870 cmd->error = -EIO; in jz_mmc_irq()
873 if (cmd->data) in jz_mmc_irq()
874 cmd->data->error = -EIO; in jz_mmc_irq()
875 cmd->error = -EIO; in jz_mmc_irq()
878 jz4740_mmc_set_irq_enabled(host, irq_reg, false); in jz_mmc_irq()
879 jz4740_mmc_write_irq_reg(host, irq_reg); in jz_mmc_irq()
888 static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host *host, int rate) in jz4740_mmc_set_clock_rate() argument
893 jz4740_mmc_clock_disable(host); in jz4740_mmc_set_clock_rate()
894 clk_set_rate(host->clk, host->mmc->f_max); in jz4740_mmc_set_clock_rate()
896 real_rate = clk_get_rate(host->clk); in jz4740_mmc_set_clock_rate()
903 writew(div, host->base + JZ_REG_MMC_CLKRT); in jz4740_mmc_set_clock_rate()
906 if (host->version >= JZ_MMC_JZ4780) { in jz4740_mmc_set_clock_rate()
910 host->base + JZ_REG_MMC_LPM); in jz4740_mmc_set_clock_rate()
911 } else if (host->version >= JZ_MMC_JZ4760) { in jz4740_mmc_set_clock_rate()
914 host->base + JZ_REG_MMC_LPM); in jz4740_mmc_set_clock_rate()
915 } else if (host->version >= JZ_MMC_JZ4725B) in jz4740_mmc_set_clock_rate()
917 host->base + JZ_REG_MMC_LPM); in jz4740_mmc_set_clock_rate()
923 static void jz4740_mmc_request(struct mmc_host *mmc, struct mmc_request *req) in jz4740_mmc_request() argument
925 struct jz4740_mmc_host *host = mmc_priv(mmc); in jz4740_mmc_request() local
927 host->req = req; in jz4740_mmc_request()
929 jz4740_mmc_write_irq_reg(host, ~0); in jz4740_mmc_request()
930 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, true); in jz4740_mmc_request()
932 host->state = JZ4740_MMC_STATE_READ_RESPONSE; in jz4740_mmc_request()
933 set_bit(0, &host->waiting); in jz4740_mmc_request()
934 mod_timer(&host->timeout_timer, in jz4740_mmc_request()
936 jz4740_mmc_send_command(host, req->cmd); in jz4740_mmc_request()
939 static void jz4740_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) in jz4740_mmc_set_ios() argument
941 struct jz4740_mmc_host *host = mmc_priv(mmc); in jz4740_mmc_set_ios() local
944 if (ios->clock) in jz4740_mmc_set_ios()
945 jz4740_mmc_set_clock_rate(host, ios->clock); in jz4740_mmc_set_ios()
947 switch (ios->power_mode) { in jz4740_mmc_set_ios()
949 jz4740_mmc_reset(host); in jz4740_mmc_set_ios()
950 if (!IS_ERR(mmc->supply.vmmc)) in jz4740_mmc_set_ios()
951 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd); in jz4740_mmc_set_ios()
952 host->cmdat |= JZ_MMC_CMDAT_INIT; in jz4740_mmc_set_ios()
953 clk_prepare_enable(host->clk); in jz4740_mmc_set_ios()
956 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) { in jz4740_mmc_set_ios()
957 ret = regulator_enable(mmc->supply.vqmmc); in jz4740_mmc_set_ios()
959 dev_err(&host->pdev->dev, "Failed to set vqmmc power!\n"); in jz4740_mmc_set_ios()
961 host->vqmmc_enabled = true; in jz4740_mmc_set_ios()
965 if (!IS_ERR(mmc->supply.vmmc)) in jz4740_mmc_set_ios()
966 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); in jz4740_mmc_set_ios()
967 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) { in jz4740_mmc_set_ios()
968 regulator_disable(mmc->supply.vqmmc); in jz4740_mmc_set_ios()
969 host->vqmmc_enabled = false; in jz4740_mmc_set_ios()
971 clk_disable_unprepare(host->clk); in jz4740_mmc_set_ios()
977 switch (ios->bus_width) { in jz4740_mmc_set_ios()
979 host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_MASK; in jz4740_mmc_set_ios()
982 host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_MASK; in jz4740_mmc_set_ios()
983 host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_4BIT; in jz4740_mmc_set_ios()
986 host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_MASK; in jz4740_mmc_set_ios()
987 host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_8BIT; in jz4740_mmc_set_ios()
994 static void jz4740_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable) in jz4740_mmc_enable_sdio_irq() argument
996 struct jz4740_mmc_host *host = mmc_priv(mmc); in jz4740_mmc_enable_sdio_irq() local
997 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_SDIO, enable); in jz4740_mmc_enable_sdio_irq()
1000 static int jz4740_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios) in jz4740_voltage_switch() argument
1005 if (!IS_ERR(mmc->supply.vqmmc)) { in jz4740_voltage_switch()
1006 ret = mmc_regulator_set_vqmmc(mmc, ios); in jz4740_voltage_switch()
1011 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) in jz4740_voltage_switch()
1014 return -EINVAL; in jz4740_voltage_switch()
1029 { .compatible = "ingenic,jz4740-mmc", .data = (void *) JZ_MMC_JZ4740 },
1030 { .compatible = "ingenic,jz4725b-mmc", .data = (void *)JZ_MMC_JZ4725B },
1031 { .compatible = "ingenic,jz4760-mmc", .data = (void *) JZ_MMC_JZ4760 },
1032 { .compatible = "ingenic,jz4775-mmc", .data = (void *) JZ_MMC_JZ4780 },
1033 { .compatible = "ingenic,jz4780-mmc", .data = (void *) JZ_MMC_JZ4780 },
1034 { .compatible = "ingenic,x1000-mmc", .data = (void *) JZ_MMC_X1000 },
1042 struct mmc_host *mmc; in jz4740_mmc_probe() local
1043 struct jz4740_mmc_host *host; in jz4740_mmc_probe() local
1045 mmc = mmc_alloc_host(sizeof(struct jz4740_mmc_host), &pdev->dev); in jz4740_mmc_probe()
1046 if (!mmc) { in jz4740_mmc_probe()
1047 dev_err(&pdev->dev, "Failed to alloc mmc host structure\n"); in jz4740_mmc_probe()
1048 return -ENOMEM; in jz4740_mmc_probe()
1051 host = mmc_priv(mmc); in jz4740_mmc_probe()
1054 host->version = (enum jz4740_mmc_version)device_get_match_data(&pdev->dev); in jz4740_mmc_probe()
1056 ret = mmc_of_parse(mmc); in jz4740_mmc_probe()
1058 dev_err_probe(&pdev->dev, ret, "could not parse device properties\n"); in jz4740_mmc_probe()
1062 mmc_regulator_get_supply(mmc); in jz4740_mmc_probe()
1064 host->irq = platform_get_irq(pdev, 0); in jz4740_mmc_probe()
1065 if (host->irq < 0) { in jz4740_mmc_probe()
1066 ret = host->irq; in jz4740_mmc_probe()
1070 host->clk = devm_clk_get(&pdev->dev, "mmc"); in jz4740_mmc_probe()
1071 if (IS_ERR(host->clk)) { in jz4740_mmc_probe()
1072 ret = PTR_ERR(host->clk); in jz4740_mmc_probe()
1073 dev_err(&pdev->dev, "Failed to get mmc clock\n"); in jz4740_mmc_probe()
1077 host->base = devm_platform_get_and_ioremap_resource(pdev, 0, &host->mem_res); in jz4740_mmc_probe()
1078 if (IS_ERR(host->base)) { in jz4740_mmc_probe()
1079 ret = PTR_ERR(host->base); in jz4740_mmc_probe()
1083 mmc->ops = &jz4740_mmc_ops; in jz4740_mmc_probe()
1084 if (!mmc->f_max) in jz4740_mmc_probe()
1085 mmc->f_max = JZ_MMC_CLK_RATE; in jz4740_mmc_probe()
1093 if (host->version == JZ_MMC_JZ4760 && mmc->f_max > JZ_MMC_CLK_RATE) in jz4740_mmc_probe()
1094 mmc->f_max = JZ_MMC_CLK_RATE; in jz4740_mmc_probe()
1096 mmc->f_min = mmc->f_max / 128; in jz4740_mmc_probe()
1097 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; in jz4740_mmc_probe()
1101 * future improvement should instead respect the cmd->busy_timeout. in jz4740_mmc_probe()
1103 mmc->max_busy_timeout = JZ_MMC_REQ_TIMEOUT_MS; in jz4740_mmc_probe()
1105 mmc->max_blk_size = (1 << 10) - 1; in jz4740_mmc_probe()
1106 mmc->max_blk_count = (1 << 15) - 1; in jz4740_mmc_probe()
1107 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count; in jz4740_mmc_probe()
1109 mmc->max_segs = 128; in jz4740_mmc_probe()
1110 mmc->max_seg_size = mmc->max_req_size; in jz4740_mmc_probe()
1112 host->mmc = mmc; in jz4740_mmc_probe()
1113 host->pdev = pdev; in jz4740_mmc_probe()
1114 spin_lock_init(&host->lock); in jz4740_mmc_probe()
1115 host->irq_mask = ~0; in jz4740_mmc_probe()
1117 jz4740_mmc_reset(host); in jz4740_mmc_probe()
1119 ret = request_threaded_irq(host->irq, jz_mmc_irq, jz_mmc_irq_worker, 0, in jz4740_mmc_probe()
1120 dev_name(&pdev->dev), host); in jz4740_mmc_probe()
1122 dev_err(&pdev->dev, "Failed to request irq: %d\n", ret); in jz4740_mmc_probe()
1126 jz4740_mmc_clock_disable(host); in jz4740_mmc_probe()
1127 timer_setup(&host->timeout_timer, jz4740_mmc_timeout, 0); in jz4740_mmc_probe()
1129 ret = jz4740_mmc_acquire_dma_channels(host); in jz4740_mmc_probe()
1130 if (ret == -EPROBE_DEFER) in jz4740_mmc_probe()
1132 host->use_dma = !ret; in jz4740_mmc_probe()
1134 platform_set_drvdata(pdev, host); in jz4740_mmc_probe()
1135 ret = mmc_add_host(mmc); in jz4740_mmc_probe()
1138 dev_err(&pdev->dev, "Failed to add mmc host: %d\n", ret); in jz4740_mmc_probe()
1141 dev_info(&pdev->dev, "Ingenic SD/MMC card driver registered\n"); in jz4740_mmc_probe()
1143 dev_info(&pdev->dev, "Using %s, %d-bit mode\n", in jz4740_mmc_probe()
1144 host->use_dma ? "DMA" : "PIO", in jz4740_mmc_probe()
1145 (mmc->caps & MMC_CAP_8_BIT_DATA) ? 8 : in jz4740_mmc_probe()
1146 ((mmc->caps & MMC_CAP_4_BIT_DATA) ? 4 : 1)); in jz4740_mmc_probe()
1151 if (host->use_dma) in jz4740_mmc_probe()
1152 jz4740_mmc_release_dma_channels(host); in jz4740_mmc_probe()
1154 free_irq(host->irq, host); in jz4740_mmc_probe()
1156 mmc_free_host(mmc); in jz4740_mmc_probe()
1163 struct jz4740_mmc_host *host = platform_get_drvdata(pdev); in jz4740_mmc_remove() local
1165 del_timer_sync(&host->timeout_timer); in jz4740_mmc_remove()
1166 jz4740_mmc_set_irq_enabled(host, 0xff, false); in jz4740_mmc_remove()
1167 jz4740_mmc_reset(host); in jz4740_mmc_remove()
1169 mmc_remove_host(host->mmc); in jz4740_mmc_remove()
1171 free_irq(host->irq, host); in jz4740_mmc_remove()
1173 if (host->use_dma) in jz4740_mmc_remove()
1174 jz4740_mmc_release_dma_channels(host); in jz4740_mmc_remove()
1176 mmc_free_host(host->mmc); in jz4740_mmc_remove()
1196 .name = "jz4740-mmc",
1205 MODULE_DESCRIPTION("JZ4740 SD/MMC controller driver");
1207 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");