Lines Matching refs:host

48 #include <linux/mmc/host.h>
60 #define SDHSTS 0x20 /* SD host status - 11 R/W */
192 static void bcm2835_dumpcmd(struct bcm2835_host *host, struct mmc_command *cmd,
195 struct device *dev = &host->pdev->dev;
201 (cmd == host->cmd) ? '>' : ' ',
207 static void bcm2835_dumpregs(struct bcm2835_host *host)
209 struct mmc_request *mrq = host->mrq;
210 struct device *dev = &host->pdev->dev;
213 bcm2835_dumpcmd(host, mrq->sbc, "sbc");
214 bcm2835_dumpcmd(host, mrq->cmd, "cmd");
221 bcm2835_dumpcmd(host, mrq->stop, "stop");
225 dev_dbg(dev, "SDCMD 0x%08x\n", readl(host->ioaddr + SDCMD));
226 dev_dbg(dev, "SDARG 0x%08x\n", readl(host->ioaddr + SDARG));
227 dev_dbg(dev, "SDTOUT 0x%08x\n", readl(host->ioaddr + SDTOUT));
228 dev_dbg(dev, "SDCDIV 0x%08x\n", readl(host->ioaddr + SDCDIV));
229 dev_dbg(dev, "SDRSP0 0x%08x\n", readl(host->ioaddr + SDRSP0));
230 dev_dbg(dev, "SDRSP1 0x%08x\n", readl(host->ioaddr + SDRSP1));
231 dev_dbg(dev, "SDRSP2 0x%08x\n", readl(host->ioaddr + SDRSP2));
232 dev_dbg(dev, "SDRSP3 0x%08x\n", readl(host->ioaddr + SDRSP3));
233 dev_dbg(dev, "SDHSTS 0x%08x\n", readl(host->ioaddr + SDHSTS));
234 dev_dbg(dev, "SDVDD 0x%08x\n", readl(host->ioaddr + SDVDD));
235 dev_dbg(dev, "SDEDM 0x%08x\n", readl(host->ioaddr + SDEDM));
236 dev_dbg(dev, "SDHCFG 0x%08x\n", readl(host->ioaddr + SDHCFG));
237 dev_dbg(dev, "SDHBCT 0x%08x\n", readl(host->ioaddr + SDHBCT));
238 dev_dbg(dev, "SDHBLC 0x%08x\n", readl(host->ioaddr + SDHBLC));
242 static void bcm2835_reset_internal(struct bcm2835_host *host)
246 writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
247 writel(0, host->ioaddr + SDCMD);
248 writel(0, host->ioaddr + SDARG);
249 writel(0xf00000, host->ioaddr + SDTOUT);
250 writel(0, host->ioaddr + SDCDIV);
251 writel(0x7f8, host->ioaddr + SDHSTS); /* Write 1s to clear */
252 writel(0, host->ioaddr + SDHCFG);
253 writel(0, host->ioaddr + SDHBCT);
254 writel(0, host->ioaddr + SDHBLC);
257 temp = readl(host->ioaddr + SDEDM);
262 writel(temp, host->ioaddr + SDEDM);
264 writel(SDVDD_POWER_ON, host->ioaddr + SDVDD);
266 host->clock = 0;
267 writel(host->hcfg, host->ioaddr + SDHCFG);
268 writel(host->cdiv, host->ioaddr + SDCDIV);
273 struct bcm2835_host *host = mmc_priv(mmc);
275 if (host->dma_chan)
276 dmaengine_terminate_sync(host->dma_chan);
277 host->dma_chan = NULL;
278 bcm2835_reset_internal(host);
281 static void bcm2835_finish_command(struct bcm2835_host *host);
283 static void bcm2835_wait_transfer_complete(struct bcm2835_host *host)
288 alternate_idle = (host->mrq->data->flags & MMC_DATA_READ) ?
296 edm = readl(host->ioaddr + SDEDM);
304 host->ioaddr + SDEDM);
310 dev_err(&host->pdev->dev,
313 bcm2835_dumpregs(host);
314 host->mrq->data->error = -ETIMEDOUT;
323 struct bcm2835_host *host = param;
325 schedule_work(&host->dma_work);
328 static void bcm2835_transfer_block_pio(struct bcm2835_host *host, bool is_read)
333 blksize = host->data->blksz;
343 if (!sg_miter_next(&host->sg_miter)) {
344 host->data->error = -EINVAL;
348 len = min(host->sg_miter.length, blksize);
350 host->data->error = -EINVAL;
355 host->sg_miter.consumed = len;
357 buf = (u32 *)host->sg_miter.addr;
366 edm = readl(host->ioaddr + SDEDM);
374 struct device *dev = &host->pdev->dev;
384 hsts = readl(host->ioaddr + SDHSTS);
399 host->ns_per_fifo_word);
409 *(buf++) = readl(host->ioaddr + SDDATA);
411 writel(*(buf++), host->ioaddr + SDDATA);
420 sg_miter_stop(&host->sg_miter);
423 static void bcm2835_transfer_pio(struct bcm2835_host *host)
425 struct device *dev = &host->pdev->dev;
429 is_read = (host->data->flags & MMC_DATA_READ) != 0;
430 bcm2835_transfer_block_pio(host, is_read);
432 sdhsts = readl(host->ioaddr + SDHSTS);
438 host->data->error = -EILSEQ;
443 host->data->error = -ETIMEDOUT;
448 void bcm2835_prepare_dma(struct bcm2835_host *host, struct mmc_data *data)
454 dma_chan = host->dma_chan_rxtx;
470 host->drain_words = 0;
483 host->drain_page = sg_page(sg);
484 host->drain_offset = sg->offset + sg->length;
487 host->drain_words = len / 4;
493 &host->dma_cfg_rx :
494 &host->dma_cfg_tx);
510 desc->callback_param = host;
511 host->dma_desc = desc;
512 host->dma_chan = dma_chan;
513 host->dma_dir = dir_data;
516 static void bcm2835_start_dma(struct bcm2835_host *host)
518 dmaengine_submit(host->dma_desc);
519 dma_async_issue_pending(host->dma_chan);
522 static void bcm2835_set_transfer_irqs(struct bcm2835_host *host)
527 if (host->dma_desc) {
528 host->hcfg = (host->hcfg & ~all_irqs) |
531 host->hcfg = (host->hcfg & ~all_irqs) |
536 writel(host->hcfg, host->ioaddr + SDHCFG);
540 void bcm2835_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
544 WARN_ON(host->data);
546 host->data = data;
550 host->data_complete = false;
551 host->data->bytes_xfered = 0;
553 if (!host->dma_desc) {
561 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
562 host->blocks = data->blocks;
565 bcm2835_set_transfer_irqs(host);
567 writel(data->blksz, host->ioaddr + SDHBCT);
568 writel(data->blocks, host->ioaddr + SDHBLC);
571 static u32 bcm2835_read_wait_sdcmd(struct bcm2835_host *host, u32 max_ms)
573 struct device *dev = &host->pdev->dev;
577 ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
581 ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
590 static void bcm2835_finish_request(struct bcm2835_host *host)
595 cancel_delayed_work(&host->timeout_work);
597 mrq = host->mrq;
599 host->mrq = NULL;
600 host->cmd = NULL;
601 host->data = NULL;
603 host->dma_desc = NULL;
604 terminate_chan = host->dma_chan;
605 host->dma_chan = NULL;
611 dev_err(&host->pdev->dev,
615 mmc_request_done(mmc_from_priv(host), mrq);
619 bool bcm2835_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
621 struct device *dev = &host->pdev->dev;
625 WARN_ON(host->cmd);
627 sdcmd = bcm2835_read_wait_sdcmd(host, 100);
630 bcm2835_dumpregs(host);
632 bcm2835_finish_request(host);
640 schedule_delayed_work(&host->timeout_work, timeout);
642 host->cmd = cmd;
645 sdhsts = readl(host->ioaddr + SDHSTS);
647 writel(sdhsts, host->ioaddr + SDHSTS);
652 bcm2835_finish_request(host);
656 bcm2835_prepare_data(host, cmd);
658 writel(cmd->arg, host->ioaddr + SDARG);
662 host->use_busy = false;
670 host->use_busy = true;
681 writel(sdcmd | SDCMD_NEW_FLAG, host->ioaddr + SDCMD);
686 static void bcm2835_transfer_complete(struct bcm2835_host *host)
690 WARN_ON(!host->data_complete);
692 data = host->data;
693 host->data = NULL;
699 if (host->mrq->stop && (data->error || !host->use_sbc)) {
700 if (bcm2835_send_command(host, host->mrq->stop)) {
702 if (!host->use_busy)
703 bcm2835_finish_command(host);
706 bcm2835_wait_transfer_complete(host);
707 bcm2835_finish_request(host);
711 static void bcm2835_finish_data(struct bcm2835_host *host)
713 struct device *dev = &host->pdev->dev;
716 data = host->data;
718 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
719 writel(host->hcfg, host->ioaddr + SDHCFG);
723 host->data_complete = true;
725 if (host->cmd) {
731 readl(host->ioaddr + SDHSTS));
733 bcm2835_transfer_complete(host);
737 static void bcm2835_finish_command(struct bcm2835_host *host)
739 struct device *dev = &host->pdev->dev;
740 struct mmc_command *cmd = host->cmd;
743 sdcmd = bcm2835_read_wait_sdcmd(host, 100);
748 bcm2835_dumpregs(host);
749 host->cmd->error = -EIO;
750 bcm2835_finish_request(host);
753 u32 sdhsts = readl(host->ioaddr + SDHSTS);
756 writel(SDHSTS_ERROR_MASK, host->ioaddr + SDHSTS);
759 (host->cmd->opcode != MMC_SEND_OP_COND)) {
763 host->cmd->error = -ETIMEDOUT;
766 host->cmd->opcode);
767 bcm2835_dumpregs(host);
768 host->cmd->error = -EILSEQ;
770 edm = readl(host->ioaddr + SDEDM);
776 host->ioaddr + SDEDM);
777 bcm2835_finish_request(host);
788 readl(host->ioaddr + SDRSP0 + i * 4);
791 cmd->resp[0] = readl(host->ioaddr + SDRSP0);
795 if (cmd == host->mrq->sbc) {
797 host->cmd = NULL;
798 if (bcm2835_send_command(host, host->mrq->cmd)) {
799 if (host->data && host->dma_desc)
803 bcm2835_start_dma(host);
805 if (!host->use_busy)
806 bcm2835_finish_command(host);
808 } else if (cmd == host->mrq->stop) {
810 bcm2835_finish_request(host);
813 host->cmd = NULL;
814 if (!host->data)
815 bcm2835_finish_request(host);
816 else if (host->data_complete)
817 bcm2835_transfer_complete(host);
824 struct bcm2835_host *host =
826 struct device *dev = &host->pdev->dev;
828 mutex_lock(&host->mutex);
830 if (host->mrq) {
832 bcm2835_dumpregs(host);
834 bcm2835_reset(mmc_from_priv(host));
836 if (host->data) {
837 host->data->error = -ETIMEDOUT;
838 bcm2835_finish_data(host);
840 if (host->cmd)
841 host->cmd->error = -ETIMEDOUT;
843 host->mrq->cmd->error = -ETIMEDOUT;
845 bcm2835_finish_request(host);
849 mutex_unlock(&host->mutex);
852 static bool bcm2835_check_cmd_error(struct bcm2835_host *host, u32 intmask)
854 struct device *dev = &host->pdev->dev;
859 if (!host->cmd)
864 host->cmd->error = -EILSEQ;
867 if (host->mrq->data)
868 host->mrq->data->error = -EILSEQ;
870 host->cmd->error = -EILSEQ;
872 if (host->mrq->data)
873 host->mrq->data->error = -ETIMEDOUT;
875 host->cmd->error = -ETIMEDOUT;
877 host->cmd->error = -ETIMEDOUT;
879 bcm2835_dumpregs(host);
883 static void bcm2835_check_data_error(struct bcm2835_host *host, u32 intmask)
885 if (!host->data)
888 host->data->error = -EILSEQ;
890 host->data->error = -ETIMEDOUT;
893 static void bcm2835_busy_irq(struct bcm2835_host *host)
895 if (WARN_ON(!host->cmd)) {
896 bcm2835_dumpregs(host);
900 if (WARN_ON(!host->use_busy)) {
901 bcm2835_dumpregs(host);
904 host->use_busy = false;
906 bcm2835_finish_command(host);
909 static void bcm2835_data_irq(struct bcm2835_host *host, u32 intmask)
917 if (!host->data)
920 bcm2835_check_data_error(host, intmask);
921 if (host->data->error)
924 if (host->data->flags & MMC_DATA_WRITE) {
926 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
927 host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
928 writel(host->hcfg, host->ioaddr + SDHCFG);
929 bcm2835_transfer_pio(host);
931 bcm2835_transfer_pio(host);
932 host->blocks--;
933 if ((host->blocks == 0) || host->data->error)
939 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
940 writel(host->hcfg, host->ioaddr + SDHCFG);
943 static void bcm2835_data_threaded_irq(struct bcm2835_host *host)
945 if (!host->data)
947 if ((host->blocks == 0) || host->data->error)
948 bcm2835_finish_data(host);
951 static void bcm2835_block_irq(struct bcm2835_host *host)
953 if (WARN_ON(!host->data)) {
954 bcm2835_dumpregs(host);
958 if (!host->dma_desc) {
959 WARN_ON(!host->blocks);
960 if (host->data->error || (--host->blocks == 0))
961 bcm2835_finish_data(host);
963 bcm2835_transfer_pio(host);
964 } else if (host->data->flags & MMC_DATA_WRITE) {
965 bcm2835_finish_data(host);
972 struct bcm2835_host *host = dev_id;
975 spin_lock(&host->lock);
977 intmask = readl(host->ioaddr + SDHSTS);
983 host->ioaddr + SDHSTS);
986 bcm2835_check_data_error(host, intmask);
987 host->irq_block = true;
992 if (!bcm2835_check_cmd_error(host, intmask)) {
993 host->irq_busy = true;
1005 (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
1006 bcm2835_data_irq(host, intmask);
1007 host->irq_data = true;
1011 spin_unlock(&host->lock);
1018 struct bcm2835_host *host = dev_id;
1022 spin_lock_irqsave(&host->lock, flags);
1024 block = host->irq_block;
1025 busy = host->irq_busy;
1026 data = host->irq_data;
1027 host->irq_block = false;
1028 host->irq_busy = false;
1029 host->irq_data = false;
1031 spin_unlock_irqrestore(&host->lock, flags);
1033 mutex_lock(&host->mutex);
1036 bcm2835_block_irq(host);
1038 bcm2835_busy_irq(host);
1040 bcm2835_data_threaded_irq(host);
1042 mutex_unlock(&host->mutex);
1049 struct bcm2835_host *host =
1053 mutex_lock(&host->mutex);
1055 data = host->data;
1057 if (host->dma_chan) {
1058 dma_unmap_sg(host->dma_chan->device->dev,
1060 host->dma_dir);
1062 host->dma_chan = NULL;
1065 if (host->drain_words) {
1069 if (host->drain_offset & PAGE_MASK) {
1070 host->drain_page += host->drain_offset >> PAGE_SHIFT;
1071 host->drain_offset &= ~PAGE_MASK;
1073 page = kmap_local_page(host->drain_page);
1074 buf = page + host->drain_offset;
1076 while (host->drain_words) {
1077 u32 edm = readl(host->ioaddr + SDEDM);
1080 *(buf++) = readl(host->ioaddr + SDDATA);
1081 host->drain_words--;
1087 bcm2835_finish_data(host);
1089 mutex_unlock(&host->mutex);
1092 static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock)
1094 struct mmc_host *mmc = mmc_from_priv(host);
1122 host->cdiv = SDCDIV_MAX_CDIV;
1123 writel(host->cdiv, host->ioaddr + SDCDIV);
1127 div = host->max_clk / clock;
1130 if ((host->max_clk / div) > clock)
1137 clock = host->max_clk / (div + 2);
1142 host->ns_per_fifo_word = (1000000000 / clock) *
1145 host->cdiv = div;
1146 writel(host->cdiv, host->ioaddr + SDCDIV);
1149 writel(mmc->actual_clock / 2, host->ioaddr + SDTOUT);
1154 struct bcm2835_host *host = mmc_priv(mmc);
1155 struct device *dev = &host->pdev->dev;
1179 mutex_lock(&host->mutex);
1181 WARN_ON(host->mrq);
1182 host->mrq = mrq;
1184 edm = readl(host->ioaddr + SDEDM);
1190 readl(host->ioaddr + SDCMD) & SDCMD_CMD_MASK,
1192 bcm2835_dumpregs(host);
1197 bcm2835_finish_request(host);
1198 mutex_unlock(&host->mutex);
1202 if (host->use_dma && mrq->data && (mrq->data->blocks > PIO_THRESHOLD))
1203 bcm2835_prepare_dma(host, mrq->data);
1205 host->use_sbc = !!mrq->sbc && host->mrq->data &&
1206 (host->mrq->data->flags & MMC_DATA_READ);
1207 if (host->use_sbc) {
1208 if (bcm2835_send_command(host, mrq->sbc)) {
1209 if (!host->use_busy)
1210 bcm2835_finish_command(host);
1212 } else if (mrq->cmd && bcm2835_send_command(host, mrq->cmd)) {
1213 if (host->data && host->dma_desc) {
1215 bcm2835_start_dma(host);
1218 if (!host->use_busy)
1219 bcm2835_finish_command(host);
1222 mutex_unlock(&host->mutex);
1227 struct bcm2835_host *host = mmc_priv(mmc);
1229 mutex_lock(&host->mutex);
1231 if (!ios->clock || ios->clock != host->clock) {
1232 bcm2835_set_clock(host, ios->clock);
1233 host->clock = ios->clock;
1237 host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
1239 host->hcfg |= SDHCFG_WIDE_EXT_BUS;
1241 host->hcfg |= SDHCFG_WIDE_INT_BUS;
1244 host->hcfg |= SDHCFG_SLOW_CARD;
1246 writel(host->hcfg, host->ioaddr + SDHCFG);
1248 mutex_unlock(&host->mutex);
1257 static int bcm2835_add_host(struct bcm2835_host *host)
1259 struct mmc_host *mmc = mmc_from_priv(host);
1260 struct device *dev = &host->pdev->dev;
1264 if (!mmc->f_max || mmc->f_max > host->max_clk)
1265 mmc->f_max = host->max_clk;
1266 mmc->f_min = host->max_clk / SDCDIV_MAX_CDIV;
1273 /* host controller capabilities */
1277 spin_lock_init(&host->lock);
1278 mutex_init(&host->mutex);
1280 if (!host->dma_chan_rxtx) {
1282 host->use_dma = false;
1284 host->use_dma = true;
1286 host->dma_cfg_tx.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1287 host->dma_cfg_tx.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1288 host->dma_cfg_tx.direction = DMA_MEM_TO_DEV;
1289 host->dma_cfg_tx.src_addr = 0;
1290 host->dma_cfg_tx.dst_addr = host->phys_addr + SDDATA;
1292 host->dma_cfg_rx.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1293 host->dma_cfg_rx.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1294 host->dma_cfg_rx.direction = DMA_DEV_TO_MEM;
1295 host->dma_cfg_rx.src_addr = host->phys_addr + SDDATA;
1296 host->dma_cfg_rx.dst_addr = 0;
1298 if (dmaengine_slave_config(host->dma_chan_rxtx,
1299 &host->dma_cfg_tx) != 0 ||
1300 dmaengine_slave_config(host->dma_chan_rxtx,
1301 &host->dma_cfg_rx) != 0)
1302 host->use_dma = false;
1314 INIT_WORK(&host->dma_work, bcm2835_dma_complete_work);
1315 INIT_DELAYED_WORK(&host->timeout_work, bcm2835_timeout);
1318 host->hcfg = SDHCFG_BUSY_IRPT_EN;
1320 bcm2835_reset_internal(host);
1322 ret = request_threaded_irq(host->irq, bcm2835_irq,
1324 0, mmc_hostname(mmc), host);
1326 dev_err(dev, "failed to request IRQ %d: %d\n", host->irq, ret);
1332 free_irq(host->irq, host);
1337 if (host->use_dma && (PIO_THRESHOLD > 0))
1340 host->use_dma ? "enabled" : "disabled", pio_limit_string);
1349 struct bcm2835_host *host;
1355 mmc = mmc_alloc_host(sizeof(*host), dev);
1360 host = mmc_priv(mmc);
1361 host->pdev = pdev;
1362 spin_lock_init(&host->lock);
1364 host->ioaddr = devm_platform_ioremap_resource(pdev, 0);
1365 if (IS_ERR(host->ioaddr)) {
1366 ret = PTR_ERR(host->ioaddr);
1380 host->phys_addr = be32_to_cpup(regaddr_p);
1382 host->dma_chan = NULL;
1383 host->dma_desc = NULL;
1385 host->dma_chan_rxtx = dma_request_chan(dev, "rx-tx");
1386 if (IS_ERR(host->dma_chan_rxtx)) {
1387 ret = PTR_ERR(host->dma_chan_rxtx);
1388 host->dma_chan_rxtx = NULL;
1403 host->max_clk = clk_get_rate(clk);
1405 host->irq = platform_get_irq(pdev, 0);
1406 if (host->irq < 0) {
1407 ret = host->irq;
1415 ret = bcm2835_add_host(host);
1419 platform_set_drvdata(pdev, host);
1427 if (host->dma_chan_rxtx)
1428 dma_release_channel(host->dma_chan_rxtx);
1436 struct bcm2835_host *host = platform_get_drvdata(pdev);
1437 struct mmc_host *mmc = mmc_from_priv(host);
1441 writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
1443 free_irq(host->irq, host);
1445 cancel_work_sync(&host->dma_work);
1446 cancel_delayed_work_sync(&host->timeout_work);
1448 if (host->dma_chan_rxtx)
1449 dma_release_channel(host->dma_chan_rxtx);