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)
334 blksize = host->data->blksz;
346 if (!sg_miter_next(&host->sg_miter)) {
347 host->data->error = -EINVAL;
351 len = min(host->sg_miter.length, blksize);
353 host->data->error = -EINVAL;
358 host->sg_miter.consumed = len;
360 buf = (u32 *)host->sg_miter.addr;
369 edm = readl(host->ioaddr + SDEDM);
377 struct device *dev = &host->pdev->dev;
387 hsts = readl(host->ioaddr + SDHSTS);
402 host->ns_per_fifo_word);
412 *(buf++) = readl(host->ioaddr + SDDATA);
414 writel(*(buf++), host->ioaddr + SDDATA);
423 sg_miter_stop(&host->sg_miter);
428 static void bcm2835_transfer_pio(struct bcm2835_host *host)
430 struct device *dev = &host->pdev->dev;
434 is_read = (host->data->flags & MMC_DATA_READ) != 0;
435 bcm2835_transfer_block_pio(host, is_read);
437 sdhsts = readl(host->ioaddr + SDHSTS);
443 host->data->error = -EILSEQ;
448 host->data->error = -ETIMEDOUT;
453 void bcm2835_prepare_dma(struct bcm2835_host *host, struct mmc_data *data)
459 dma_chan = host->dma_chan_rxtx;
475 host->drain_words = 0;
488 host->drain_page = sg_page(sg);
489 host->drain_offset = sg->offset + sg->length;
492 host->drain_words = len / 4;
498 &host->dma_cfg_rx :
499 &host->dma_cfg_tx);
515 desc->callback_param = host;
516 host->dma_desc = desc;
517 host->dma_chan = dma_chan;
518 host->dma_dir = dir_data;
521 static void bcm2835_start_dma(struct bcm2835_host *host)
523 dmaengine_submit(host->dma_desc);
524 dma_async_issue_pending(host->dma_chan);
527 static void bcm2835_set_transfer_irqs(struct bcm2835_host *host)
532 if (host->dma_desc) {
533 host->hcfg = (host->hcfg & ~all_irqs) |
536 host->hcfg = (host->hcfg & ~all_irqs) |
541 writel(host->hcfg, host->ioaddr + SDHCFG);
545 void bcm2835_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
549 WARN_ON(host->data);
551 host->data = data;
555 host->data_complete = false;
556 host->data->bytes_xfered = 0;
558 if (!host->dma_desc) {
566 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
567 host->blocks = data->blocks;
570 bcm2835_set_transfer_irqs(host);
572 writel(data->blksz, host->ioaddr + SDHBCT);
573 writel(data->blocks, host->ioaddr + SDHBLC);
576 static u32 bcm2835_read_wait_sdcmd(struct bcm2835_host *host, u32 max_ms)
578 struct device *dev = &host->pdev->dev;
582 ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
586 ret = readl_poll_timeout(host->ioaddr + SDCMD, value,
595 static void bcm2835_finish_request(struct bcm2835_host *host)
600 cancel_delayed_work(&host->timeout_work);
602 mrq = host->mrq;
604 host->mrq = NULL;
605 host->cmd = NULL;
606 host->data = NULL;
608 host->dma_desc = NULL;
609 terminate_chan = host->dma_chan;
610 host->dma_chan = NULL;
616 dev_err(&host->pdev->dev,
620 mmc_request_done(mmc_from_priv(host), mrq);
624 bool bcm2835_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
626 struct device *dev = &host->pdev->dev;
630 WARN_ON(host->cmd);
632 sdcmd = bcm2835_read_wait_sdcmd(host, 100);
635 bcm2835_dumpregs(host);
637 bcm2835_finish_request(host);
645 schedule_delayed_work(&host->timeout_work, timeout);
647 host->cmd = cmd;
650 sdhsts = readl(host->ioaddr + SDHSTS);
652 writel(sdhsts, host->ioaddr + SDHSTS);
657 bcm2835_finish_request(host);
661 bcm2835_prepare_data(host, cmd);
663 writel(cmd->arg, host->ioaddr + SDARG);
667 host->use_busy = false;
675 host->use_busy = true;
686 writel(sdcmd | SDCMD_NEW_FLAG, host->ioaddr + SDCMD);
691 static void bcm2835_transfer_complete(struct bcm2835_host *host)
695 WARN_ON(!host->data_complete);
697 data = host->data;
698 host->data = NULL;
704 if (host->mrq->stop && (data->error || !host->use_sbc)) {
705 if (bcm2835_send_command(host, host->mrq->stop)) {
707 if (!host->use_busy)
708 bcm2835_finish_command(host);
711 bcm2835_wait_transfer_complete(host);
712 bcm2835_finish_request(host);
716 static void bcm2835_finish_data(struct bcm2835_host *host)
718 struct device *dev = &host->pdev->dev;
721 data = host->data;
723 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
724 writel(host->hcfg, host->ioaddr + SDHCFG);
728 host->data_complete = true;
730 if (host->cmd) {
736 readl(host->ioaddr + SDHSTS));
738 bcm2835_transfer_complete(host);
742 static void bcm2835_finish_command(struct bcm2835_host *host)
744 struct device *dev = &host->pdev->dev;
745 struct mmc_command *cmd = host->cmd;
748 sdcmd = bcm2835_read_wait_sdcmd(host, 100);
753 bcm2835_dumpregs(host);
754 host->cmd->error = -EIO;
755 bcm2835_finish_request(host);
758 u32 sdhsts = readl(host->ioaddr + SDHSTS);
761 writel(SDHSTS_ERROR_MASK, host->ioaddr + SDHSTS);
764 (host->cmd->opcode != MMC_SEND_OP_COND)) {
768 host->cmd->error = -ETIMEDOUT;
771 host->cmd->opcode);
772 bcm2835_dumpregs(host);
773 host->cmd->error = -EILSEQ;
775 edm = readl(host->ioaddr + SDEDM);
781 host->ioaddr + SDEDM);
782 bcm2835_finish_request(host);
793 readl(host->ioaddr + SDRSP0 + i * 4);
796 cmd->resp[0] = readl(host->ioaddr + SDRSP0);
800 if (cmd == host->mrq->sbc) {
802 host->cmd = NULL;
803 if (bcm2835_send_command(host, host->mrq->cmd)) {
804 if (host->data && host->dma_desc)
808 bcm2835_start_dma(host);
810 if (!host->use_busy)
811 bcm2835_finish_command(host);
813 } else if (cmd == host->mrq->stop) {
815 bcm2835_finish_request(host);
818 host->cmd = NULL;
819 if (!host->data)
820 bcm2835_finish_request(host);
821 else if (host->data_complete)
822 bcm2835_transfer_complete(host);
829 struct bcm2835_host *host =
831 struct device *dev = &host->pdev->dev;
833 mutex_lock(&host->mutex);
835 if (host->mrq) {
837 bcm2835_dumpregs(host);
839 bcm2835_reset(mmc_from_priv(host));
841 if (host->data) {
842 host->data->error = -ETIMEDOUT;
843 bcm2835_finish_data(host);
845 if (host->cmd)
846 host->cmd->error = -ETIMEDOUT;
848 host->mrq->cmd->error = -ETIMEDOUT;
850 bcm2835_finish_request(host);
854 mutex_unlock(&host->mutex);
857 static bool bcm2835_check_cmd_error(struct bcm2835_host *host, u32 intmask)
859 struct device *dev = &host->pdev->dev;
864 if (!host->cmd)
869 host->cmd->error = -EILSEQ;
872 if (host->mrq->data)
873 host->mrq->data->error = -EILSEQ;
875 host->cmd->error = -EILSEQ;
877 if (host->mrq->data)
878 host->mrq->data->error = -ETIMEDOUT;
880 host->cmd->error = -ETIMEDOUT;
882 host->cmd->error = -ETIMEDOUT;
884 bcm2835_dumpregs(host);
888 static void bcm2835_check_data_error(struct bcm2835_host *host, u32 intmask)
890 if (!host->data)
893 host->data->error = -EILSEQ;
895 host->data->error = -ETIMEDOUT;
898 static void bcm2835_busy_irq(struct bcm2835_host *host)
900 if (WARN_ON(!host->cmd)) {
901 bcm2835_dumpregs(host);
905 if (WARN_ON(!host->use_busy)) {
906 bcm2835_dumpregs(host);
909 host->use_busy = false;
911 bcm2835_finish_command(host);
914 static void bcm2835_data_irq(struct bcm2835_host *host, u32 intmask)
922 if (!host->data)
925 bcm2835_check_data_error(host, intmask);
926 if (host->data->error)
929 if (host->data->flags & MMC_DATA_WRITE) {
931 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN);
932 host->hcfg |= SDHCFG_BLOCK_IRPT_EN;
933 writel(host->hcfg, host->ioaddr + SDHCFG);
934 bcm2835_transfer_pio(host);
936 bcm2835_transfer_pio(host);
937 host->blocks--;
938 if ((host->blocks == 0) || host->data->error)
944 host->hcfg &= ~(SDHCFG_DATA_IRPT_EN | SDHCFG_BLOCK_IRPT_EN);
945 writel(host->hcfg, host->ioaddr + SDHCFG);
948 static void bcm2835_data_threaded_irq(struct bcm2835_host *host)
950 if (!host->data)
952 if ((host->blocks == 0) || host->data->error)
953 bcm2835_finish_data(host);
956 static void bcm2835_block_irq(struct bcm2835_host *host)
958 if (WARN_ON(!host->data)) {
959 bcm2835_dumpregs(host);
963 if (!host->dma_desc) {
964 WARN_ON(!host->blocks);
965 if (host->data->error || (--host->blocks == 0))
966 bcm2835_finish_data(host);
968 bcm2835_transfer_pio(host);
969 } else if (host->data->flags & MMC_DATA_WRITE) {
970 bcm2835_finish_data(host);
977 struct bcm2835_host *host = dev_id;
980 spin_lock(&host->lock);
982 intmask = readl(host->ioaddr + SDHSTS);
988 host->ioaddr + SDHSTS);
991 bcm2835_check_data_error(host, intmask);
992 host->irq_block = true;
997 if (!bcm2835_check_cmd_error(host, intmask)) {
998 host->irq_busy = true;
1010 (host->hcfg & SDHCFG_DATA_IRPT_EN)) {
1011 bcm2835_data_irq(host, intmask);
1012 host->irq_data = true;
1016 spin_unlock(&host->lock);
1023 struct bcm2835_host *host = dev_id;
1027 spin_lock_irqsave(&host->lock, flags);
1029 block = host->irq_block;
1030 busy = host->irq_busy;
1031 data = host->irq_data;
1032 host->irq_block = false;
1033 host->irq_busy = false;
1034 host->irq_data = false;
1036 spin_unlock_irqrestore(&host->lock, flags);
1038 mutex_lock(&host->mutex);
1041 bcm2835_block_irq(host);
1043 bcm2835_busy_irq(host);
1045 bcm2835_data_threaded_irq(host);
1047 mutex_unlock(&host->mutex);
1054 struct bcm2835_host *host =
1058 mutex_lock(&host->mutex);
1060 data = host->data;
1062 if (host->dma_chan) {
1063 dma_unmap_sg(host->dma_chan->device->dev,
1065 host->dma_dir);
1067 host->dma_chan = NULL;
1070 if (host->drain_words) {
1075 if (host->drain_offset & PAGE_MASK) {
1076 host->drain_page += host->drain_offset >> PAGE_SHIFT;
1077 host->drain_offset &= ~PAGE_MASK;
1080 page = kmap_atomic(host->drain_page);
1081 buf = page + host->drain_offset;
1083 while (host->drain_words) {
1084 u32 edm = readl(host->ioaddr + SDEDM);
1087 *(buf++) = readl(host->ioaddr + SDDATA);
1088 host->drain_words--;
1095 bcm2835_finish_data(host);
1097 mutex_unlock(&host->mutex);
1100 static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock)
1102 struct mmc_host *mmc = mmc_from_priv(host);
1130 host->cdiv = SDCDIV_MAX_CDIV;
1131 writel(host->cdiv, host->ioaddr + SDCDIV);
1135 div = host->max_clk / clock;
1138 if ((host->max_clk / div) > clock)
1145 clock = host->max_clk / (div + 2);
1150 host->ns_per_fifo_word = (1000000000 / clock) *
1153 host->cdiv = div;
1154 writel(host->cdiv, host->ioaddr + SDCDIV);
1157 writel(mmc->actual_clock / 2, host->ioaddr + SDTOUT);
1162 struct bcm2835_host *host = mmc_priv(mmc);
1163 struct device *dev = &host->pdev->dev;
1187 mutex_lock(&host->mutex);
1189 WARN_ON(host->mrq);
1190 host->mrq = mrq;
1192 edm = readl(host->ioaddr + SDEDM);
1198 readl(host->ioaddr + SDCMD) & SDCMD_CMD_MASK,
1200 bcm2835_dumpregs(host);
1205 bcm2835_finish_request(host);
1206 mutex_unlock(&host->mutex);
1210 if (host->use_dma && mrq->data && (mrq->data->blocks > PIO_THRESHOLD))
1211 bcm2835_prepare_dma(host, mrq->data);
1213 host->use_sbc = !!mrq->sbc && host->mrq->data &&
1214 (host->mrq->data->flags & MMC_DATA_READ);
1215 if (host->use_sbc) {
1216 if (bcm2835_send_command(host, mrq->sbc)) {
1217 if (!host->use_busy)
1218 bcm2835_finish_command(host);
1220 } else if (mrq->cmd && bcm2835_send_command(host, mrq->cmd)) {
1221 if (host->data && host->dma_desc) {
1223 bcm2835_start_dma(host);
1226 if (!host->use_busy)
1227 bcm2835_finish_command(host);
1230 mutex_unlock(&host->mutex);
1235 struct bcm2835_host *host = mmc_priv(mmc);
1237 mutex_lock(&host->mutex);
1239 if (!ios->clock || ios->clock != host->clock) {
1240 bcm2835_set_clock(host, ios->clock);
1241 host->clock = ios->clock;
1245 host->hcfg &= ~SDHCFG_WIDE_EXT_BUS;
1247 host->hcfg |= SDHCFG_WIDE_EXT_BUS;
1249 host->hcfg |= SDHCFG_WIDE_INT_BUS;
1252 host->hcfg |= SDHCFG_SLOW_CARD;
1254 writel(host->hcfg, host->ioaddr + SDHCFG);
1256 mutex_unlock(&host->mutex);
1265 static int bcm2835_add_host(struct bcm2835_host *host)
1267 struct mmc_host *mmc = mmc_from_priv(host);
1268 struct device *dev = &host->pdev->dev;
1272 if (!mmc->f_max || mmc->f_max > host->max_clk)
1273 mmc->f_max = host->max_clk;
1274 mmc->f_min = host->max_clk / SDCDIV_MAX_CDIV;
1281 /* host controller capabilities */
1285 spin_lock_init(&host->lock);
1286 mutex_init(&host->mutex);
1288 if (!host->dma_chan_rxtx) {
1290 host->use_dma = false;
1292 host->use_dma = true;
1294 host->dma_cfg_tx.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1295 host->dma_cfg_tx.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1296 host->dma_cfg_tx.slave_id = 13; /* DREQ channel */
1297 host->dma_cfg_tx.direction = DMA_MEM_TO_DEV;
1298 host->dma_cfg_tx.src_addr = 0;
1299 host->dma_cfg_tx.dst_addr = host->phys_addr + SDDATA;
1301 host->dma_cfg_rx.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1302 host->dma_cfg_rx.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1303 host->dma_cfg_rx.slave_id = 13; /* DREQ channel */
1304 host->dma_cfg_rx.direction = DMA_DEV_TO_MEM;
1305 host->dma_cfg_rx.src_addr = host->phys_addr + SDDATA;
1306 host->dma_cfg_rx.dst_addr = 0;
1308 if (dmaengine_slave_config(host->dma_chan_rxtx,
1309 &host->dma_cfg_tx) != 0 ||
1310 dmaengine_slave_config(host->dma_chan_rxtx,
1311 &host->dma_cfg_rx) != 0)
1312 host->use_dma = false;
1324 INIT_WORK(&host->dma_work, bcm2835_dma_complete_work);
1325 INIT_DELAYED_WORK(&host->timeout_work, bcm2835_timeout);
1328 host->hcfg = SDHCFG_BUSY_IRPT_EN;
1330 bcm2835_reset_internal(host);
1332 ret = request_threaded_irq(host->irq, bcm2835_irq,
1334 0, mmc_hostname(mmc), host);
1336 dev_err(dev, "failed to request IRQ %d: %d\n", host->irq, ret);
1342 free_irq(host->irq, host);
1347 if (host->use_dma && (PIO_THRESHOLD > 0))
1350 host->use_dma ? "enabled" : "disabled", pio_limit_string);
1359 struct bcm2835_host *host;
1365 mmc = mmc_alloc_host(sizeof(*host), dev);
1370 host = mmc_priv(mmc);
1371 host->pdev = pdev;
1372 spin_lock_init(&host->lock);
1374 host->ioaddr = devm_platform_ioremap_resource(pdev, 0);
1375 if (IS_ERR(host->ioaddr)) {
1376 ret = PTR_ERR(host->ioaddr);
1390 host->phys_addr = be32_to_cpup(regaddr_p);
1392 host->dma_chan = NULL;
1393 host->dma_desc = NULL;
1395 host->dma_chan_rxtx = dma_request_chan(dev, "rx-tx");
1396 if (IS_ERR(host->dma_chan_rxtx)) {
1397 ret = PTR_ERR(host->dma_chan_rxtx);
1398 host->dma_chan_rxtx = NULL;
1413 host->max_clk = clk_get_rate(clk);
1415 host->irq = platform_get_irq(pdev, 0);
1416 if (host->irq < 0) {
1417 ret = host->irq;
1425 ret = bcm2835_add_host(host);
1429 platform_set_drvdata(pdev, host);
1437 if (host->dma_chan_rxtx)
1438 dma_release_channel(host->dma_chan_rxtx);
1446 struct bcm2835_host *host = platform_get_drvdata(pdev);
1447 struct mmc_host *mmc = mmc_from_priv(host);
1451 writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD);
1453 free_irq(host->irq, host);
1455 cancel_work_sync(&host->dma_work);
1456 cancel_delayed_work_sync(&host->timeout_work);
1458 if (host->dma_chan_rxtx)
1459 dma_release_channel(host->dma_chan_rxtx);