15f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław/* 25f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * cb710/mmc.c 35f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * 45f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * Copyright by Michał Mirosław, 2008-2009 55f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * 65f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * This program is free software; you can redistribute it and/or modify 75f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * it under the terms of the GNU General Public License version 2 as 85f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * published by the Free Software Foundation. 95f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław */ 105f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#include <linux/kernel.h> 115f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#include <linux/module.h> 125f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#include <linux/pci.h> 135f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#include <linux/delay.h> 145f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#include "cb710-mmc.h" 155f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 165f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic const u8 cb710_clock_divider_log2[8] = { 175f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław/* 1, 2, 4, 8, 16, 32, 128, 512 */ 185f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 0, 1, 2, 3, 4, 5, 7, 9 195f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław}; 205f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#define CB710_MAX_DIVIDER_IDX \ 215f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław (ARRAY_SIZE(cb710_clock_divider_log2) - 1) 225f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 235f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic const u8 cb710_src_freq_mhz[16] = { 245f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 33, 10, 20, 25, 30, 35, 40, 45, 255f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 50, 55, 60, 65, 70, 75, 80, 85 265f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław}; 275f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2819d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosławstatic void cb710_mmc_select_clock_divider(struct mmc_host *mmc, int hz) 295f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 305f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_slot *slot = cb710_mmc_to_slot(mmc); 315f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct pci_dev *pdev = cb710_slot_to_chip(slot)->pdev; 325f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u32 src_freq_idx; 335f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u32 divider_idx; 345f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int src_hz; 355f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3619d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław /* on CB710 in HP nx9500: 3719d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław * src_freq_idx == 0 3819d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław * indexes 1-7 work as written in the table 3919d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław * indexes 0,8-15 give no clock output 4019d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław */ 415f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław pci_read_config_dword(pdev, 0x48, &src_freq_idx); 425f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław src_freq_idx = (src_freq_idx >> 16) & 0xF; 435f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław src_hz = cb710_src_freq_mhz[src_freq_idx] * 1000000; 445f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 455f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław for (divider_idx = 0; divider_idx < CB710_MAX_DIVIDER_IDX; ++divider_idx) { 465f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (hz >= src_hz >> cb710_clock_divider_log2[divider_idx]) 475f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław break; 485f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 495f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 505f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (src_freq_idx) 515f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław divider_idx |= 0x8; 5219d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław else if (divider_idx == 0) 5319d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław divider_idx = 1; 545f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 555f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_pci_update_config_reg(pdev, 0x40, ~0xF0000000, divider_idx << 28); 565f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 575f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), 5819d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław "clock set to %d Hz, wanted %d Hz; src_freq_idx = %d, divider_idx = %d|%d\n", 595f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław src_hz >> cb710_clock_divider_log2[divider_idx & 7], 6019d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław hz, src_freq_idx, divider_idx & 7, divider_idx & 8); 615f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 625f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 635f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void __cb710_mmc_enable_irq(struct cb710_slot *slot, 645f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław unsigned short enable, unsigned short mask) 655f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 665f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* clear global IE 675f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * - it gets set later if any interrupt sources are enabled */ 685f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mask |= CB710_MMC_IE_IRQ_ENABLE; 695f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 705f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* look like interrupt is fired whenever 715f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * WORD[0x0C] & WORD[0x10] != 0; 725f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * -> bit 15 port 0x0C seems to be global interrupt enable 735f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław */ 745f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 755f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław enable = (cb710_read_port_16(slot, CB710_MMC_IRQ_ENABLE_PORT) 765f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław & ~mask) | enable; 775f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 785f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (enable) 795f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław enable |= CB710_MMC_IE_IRQ_ENABLE; 805f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 815f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_16(slot, CB710_MMC_IRQ_ENABLE_PORT, enable); 825f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 835f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 845f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_mmc_enable_irq(struct cb710_slot *slot, 855f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław unsigned short enable, unsigned short mask) 865f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 875f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_mmc_reader *reader = mmc_priv(cb710_slot_to_mmc(slot)); 885f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław unsigned long flags; 895f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 905f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław spin_lock_irqsave(&reader->irq_lock, flags); 915f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* this is the only thing irq_lock protects */ 925f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław __cb710_mmc_enable_irq(slot, enable, mask); 935f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław spin_unlock_irqrestore(&reader->irq_lock, flags); 945f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 955f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 965f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_mmc_reset_events(struct cb710_slot *slot) 975f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 985f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, 0xFF); 995f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, 0xFF); 1005f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_8(slot, CB710_MMC_STATUS2_PORT, 0xFF); 1015f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 1025f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1035f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_mmc_enable_4bit_data(struct cb710_slot *slot, int enable) 1045f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 1055f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (enable) 1065f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 1075f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław CB710_MMC_C1_4BIT_DATA_BUS, 0); 1085f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław else 1095f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 1105f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 0, CB710_MMC_C1_4BIT_DATA_BUS); 1115f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 1125f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1135f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_check_event(struct cb710_slot *slot, u8 what) 1145f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 1155f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u16 status; 1165f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1175f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław status = cb710_read_port_16(slot, CB710_MMC_STATUS_PORT); 1185f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1195f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (status & CB710_MMC_S0_FIFO_UNDERFLOW) { 1205f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* it is just a guess, so log it */ 1215f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), 1225f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław "CHECK : ignoring bit 6 in status %04X\n", status); 1235f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, 1245f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław CB710_MMC_S0_FIFO_UNDERFLOW); 1255f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław status &= ~CB710_MMC_S0_FIFO_UNDERFLOW; 1265f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 1275f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1285f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (status & CB710_MMC_STATUS_ERROR_EVENTS) { 1295f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), 1305f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław "CHECK : returning EIO on status %04X\n", status); 1315f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, status & 0xFF); 1325f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, 1335f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław CB710_MMC_S1_RESET); 1345f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return -EIO; 1355f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 1365f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1375f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* 'what' is a bit in MMC_STATUS1 */ 1385f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if ((status >> 8) & what) { 1395f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, what); 1405f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return 1; 1415f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 1425f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1435f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return 0; 1445f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 1455f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1465f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_wait_for_event(struct cb710_slot *slot, u8 what) 1475f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 1485f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int err = 0; 1495f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław unsigned limit = 2000000; /* FIXME: real timeout */ 1505f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1515f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#ifdef CONFIG_CB710_DEBUG 1525f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u32 e, x; 1535f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław e = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT); 1545f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#endif 1555f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1565f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław while (!(err = cb710_check_event(slot, what))) { 1575f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!--limit) { 1585f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(cb710_slot_to_chip(slot), 1595f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław CB710_DUMP_REGS_MMC); 1605f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = -ETIMEDOUT; 1615f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław break; 1625f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 1635f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław udelay(1); 1645f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 1655f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1665f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#ifdef CONFIG_CB710_DEBUG 1675f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław x = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT); 1685f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1695f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław limit = 2000000 - limit; 1705f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (limit > 100) 1715f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), 1725f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław "WAIT10: waited %d loops, what %d, entry val %08X, exit val %08X\n", 1735f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław limit, what, e, x); 1745f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#endif 1755f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return err < 0 ? err : 0; 1765f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 1775f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1785f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1795f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_wait_while_busy(struct cb710_slot *slot, uint8_t mask) 1805f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 1815f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław unsigned limit = 500000; /* FIXME: real timeout */ 1825f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int err = 0; 1835f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1845f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#ifdef CONFIG_CB710_DEBUG 1855f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u32 e, x; 1865f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław e = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT); 1875f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#endif 1885f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1895f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław while (cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT) & mask) { 1905f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!--limit) { 1915f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(cb710_slot_to_chip(slot), 1925f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław CB710_DUMP_REGS_MMC); 1935f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = -ETIMEDOUT; 1945f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław break; 1955f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 1965f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław udelay(1); 1975f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 1985f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 1995f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#ifdef CONFIG_CB710_DEBUG 2005f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław x = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT); 2015f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2025f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław limit = 500000 - limit; 2035f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (limit > 100) 2045f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), 2055f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław "WAIT12: waited %d loops, mask %02X, entry val %08X, exit val %08X\n", 2065f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław limit, mask, e, x); 2075f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#endif 208b3bd1b5b0054f716e58a65d4e2e449ea0252e9ddChris Ball return err; 2095f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 2105f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2115f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_mmc_set_transfer_size(struct cb710_slot *slot, 2125f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław size_t count, size_t blocksize) 2135f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 2145f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20); 2155f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_32(slot, CB710_MMC_TRANSFER_SIZE_PORT, 2165f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław ((count - 1) << 16)|(blocksize - 1)); 2175f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 21809adfe454c87e3ec5dffbc42567cd7b4b4948522Pierre Ossman dev_vdbg(cb710_slot_dev(slot), "set up for %zu block%s of %zu bytes\n", 2195f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław count, count == 1 ? "" : "s", blocksize); 2205f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 2215f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2225f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_mmc_fifo_hack(struct cb710_slot *slot) 2235f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 2245f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* without this, received data is prepended with 8-bytes of zeroes */ 2255f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u32 r1, r2; 2265f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int ok = 0; 2275f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2285f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław r1 = cb710_read_port_32(slot, CB710_MMC_DATA_PORT); 2295f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław r2 = cb710_read_port_32(slot, CB710_MMC_DATA_PORT); 2305f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (cb710_read_port_8(slot, CB710_MMC_STATUS0_PORT) 2315f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław & CB710_MMC_S0_FIFO_UNDERFLOW) { 2325f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_8(slot, CB710_MMC_STATUS0_PORT, 2335f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław CB710_MMC_S0_FIFO_UNDERFLOW); 2345f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław ok = 1; 2355f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 2365f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2375f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), 2385f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław "FIFO-read-hack: expected STATUS0 bit was %s\n", 2395f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław ok ? "set." : "NOT SET!"); 2405f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), 2415f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław "FIFO-read-hack: dwords ignored: %08X %08X - %s\n", 2425f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław r1, r2, (r1|r2) ? "BAD (NOT ZERO)!" : "ok"); 2435f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 2445f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2455f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_receive_pio(struct cb710_slot *slot, 2465f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct sg_mapping_iter *miter, size_t dw_count) 2475f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 2485f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!(cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT) & CB710_MMC_S2_FIFO_READY)) { 2495f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int err = cb710_wait_for_event(slot, 2505f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław CB710_MMC_S1_PIO_TRANSFER_DONE); 2515f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (err) 2525f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return err; 2535f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 2545f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2555f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_sg_dwiter_write_from_io(miter, 2565f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław slot->iobase + CB710_MMC_DATA_PORT, dw_count); 2575f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2585f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return 0; 2595f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 2605f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2615f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic bool cb710_is_transfer_size_supported(struct mmc_data *data) 2625f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 2635f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return !(data->blksz & 15 && (data->blocks != 1 || data->blksz != 8)); 2645f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 2655f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2665f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_receive(struct cb710_slot *slot, struct mmc_data *data) 2675f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 2685f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct sg_mapping_iter miter; 2695f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław size_t len, blocks = data->blocks; 2705f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int err = 0; 2715f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2725f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* TODO: I don't know how/if the hardware handles non-16B-boundary blocks 2735f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * except single 8B block */ 2745f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (unlikely(data->blksz & 15 && (data->blocks != 1 || data->blksz != 8))) 2755f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return -EINVAL; 2765f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2774b2a108cd0d34880fe9d932258ca5b2ccebcd05eSebastian Andrzej Siewior sg_miter_start(&miter, data->sg, data->sg_len, SG_MITER_TO_SG); 2785f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2795f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT, 2805f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 15, CB710_MMC_C2_READ_PIO_SIZE_MASK); 2815f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2825f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_fifo_hack(slot); 2835f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2845f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław while (blocks-- > 0) { 2855f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław len = data->blksz; 2865f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2875f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław while (len >= 16) { 2885f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = cb710_mmc_receive_pio(slot, &miter, 4); 2895f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (err) 2905f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław goto out; 2915f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław len -= 16; 2925f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 2935f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2945f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!len) 2955f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław continue; 2965f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 2975f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT, 2985f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław len - 1, CB710_MMC_C2_READ_PIO_SIZE_MASK); 2995f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3005f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław len = (len >= 8) ? 4 : 2; 3015f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = cb710_mmc_receive_pio(slot, &miter, len); 3025f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (err) 3035f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław goto out; 3045f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 3055f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławout: 3064b2a108cd0d34880fe9d932258ca5b2ccebcd05eSebastian Andrzej Siewior sg_miter_stop(&miter); 3075f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return err; 3085f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 3095f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3105f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_send(struct cb710_slot *slot, struct mmc_data *data) 3115f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 3125f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct sg_mapping_iter miter; 3135f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław size_t len, blocks = data->blocks; 3145f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int err = 0; 3155f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3165f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* TODO: I don't know how/if the hardware handles multiple 3175f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * non-16B-boundary blocks */ 3185f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (unlikely(data->blocks > 1 && data->blksz & 15)) 3195f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return -EINVAL; 3205f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3214b2a108cd0d34880fe9d932258ca5b2ccebcd05eSebastian Andrzej Siewior sg_miter_start(&miter, data->sg, data->sg_len, SG_MITER_FROM_SG); 3225f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3235f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT, 3245f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 0, CB710_MMC_C2_READ_PIO_SIZE_MASK); 3255f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3265f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław while (blocks-- > 0) { 3275f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław len = (data->blksz + 15) >> 4; 3285f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław do { 3295f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!(cb710_read_port_8(slot, CB710_MMC_STATUS2_PORT) 3305f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław & CB710_MMC_S2_FIFO_EMPTY)) { 3315f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = cb710_wait_for_event(slot, 3325f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław CB710_MMC_S1_PIO_TRANSFER_DONE); 3335f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (err) 3345f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław goto out; 3355f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 3365f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_sg_dwiter_read_to_io(&miter, 3375f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław slot->iobase + CB710_MMC_DATA_PORT, 4); 3385f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } while (--len); 3395f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 3405f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławout: 3415f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław sg_miter_stop(&miter); 3425f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return err; 3435f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 3445f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3455f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic u16 cb710_encode_cmd_flags(struct cb710_mmc_reader *reader, 3465f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct mmc_command *cmd) 3475f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 3485f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław unsigned int flags = cmd->flags; 3495f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u16 cb_flags = 0; 3505f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3515f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* Windows driver returned 0 for commands for which no response 3525f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * is expected. It happened that there were only two such commands 3535f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * used: MMC_GO_IDLE_STATE and MMC_GO_INACTIVE_STATE so it might 3545f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * as well be a bug in that driver. 3555f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * 3565f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * Original driver set bit 14 for MMC/SD application 3575f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * commands. There's no difference 'on the wire' and 3585f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * it apparently works without it anyway. 3595f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław */ 3605f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3615f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław switch (flags & MMC_CMD_MASK) { 3625f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław case MMC_CMD_AC: cb_flags = CB710_MMC_CMD_AC; break; 3635f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław case MMC_CMD_ADTC: cb_flags = CB710_MMC_CMD_ADTC; break; 3645f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław case MMC_CMD_BC: cb_flags = CB710_MMC_CMD_BC; break; 3655f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław case MMC_CMD_BCR: cb_flags = CB710_MMC_CMD_BCR; break; 3665f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 3675f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3685f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (flags & MMC_RSP_BUSY) 3695f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb_flags |= CB710_MMC_RSP_BUSY; 3705f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3715f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb_flags |= cmd->opcode << CB710_MMC_CMD_CODE_SHIFT; 3725f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3735f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (cmd->data && (cmd->data->flags & MMC_DATA_READ)) 3745f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb_flags |= CB710_MMC_DATA_READ; 3755f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3765f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (flags & MMC_RSP_PRESENT) { 3775f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* Windows driver set 01 at bits 4,3 except for 3785f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * MMC_SET_BLOCKLEN where it set 10. Maybe the 3795f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * hardware can do something special about this 3805f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * command? The original driver looks buggy/incomplete 3815f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * anyway so we ignore this for now. 3825f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * 3835f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * I assume that 00 here means no response is expected. 3845f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław */ 3855f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb_flags |= CB710_MMC_RSP_PRESENT; 3865f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3875f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (flags & MMC_RSP_136) 3885f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb_flags |= CB710_MMC_RSP_136; 3895f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!(flags & MMC_RSP_CRC)) 3905f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb_flags |= CB710_MMC_RSP_NO_CRC; 3915f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 3925f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3935f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return cb_flags; 3945f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 3955f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 3965f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_receive_response(struct cb710_slot *slot, 3975f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct mmc_command *cmd) 3985f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 3995f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław unsigned rsp_opcode, wanted_opcode; 4005f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4015f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* Looks like final byte with CRC is always stripped (same as SDHCI) */ 4025f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (cmd->flags & MMC_RSP_136) { 4035f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u32 resp[4]; 4045f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4055f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław resp[0] = cb710_read_port_32(slot, CB710_MMC_RESPONSE3_PORT); 4065f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław resp[1] = cb710_read_port_32(slot, CB710_MMC_RESPONSE2_PORT); 4075f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław resp[2] = cb710_read_port_32(slot, CB710_MMC_RESPONSE1_PORT); 4085f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław resp[3] = cb710_read_port_32(slot, CB710_MMC_RESPONSE0_PORT); 4095f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław rsp_opcode = resp[0] >> 24; 4105f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4115f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cmd->resp[0] = (resp[0] << 8)|(resp[1] >> 24); 4125f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cmd->resp[1] = (resp[1] << 8)|(resp[2] >> 24); 4135f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cmd->resp[2] = (resp[2] << 8)|(resp[3] >> 24); 4145f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cmd->resp[3] = (resp[3] << 8); 4155f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } else { 4165f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław rsp_opcode = cb710_read_port_32(slot, CB710_MMC_RESPONSE1_PORT) & 0x3F; 4175f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cmd->resp[0] = cb710_read_port_32(slot, CB710_MMC_RESPONSE0_PORT); 4185f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 4195f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4205f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław wanted_opcode = (cmd->flags & MMC_RSP_OPCODE) ? cmd->opcode : 0x3F; 4215f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (rsp_opcode != wanted_opcode) 4225f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cmd->error = -EILSEQ; 4235f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 4245f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4255f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_transfer_data(struct cb710_slot *slot, 4265f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct mmc_data *data) 4275f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 4285f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int error, to; 4295f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4305f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (data->flags & MMC_DATA_READ) 4315f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław error = cb710_mmc_receive(slot, data); 4325f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław else 4335f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław error = cb710_mmc_send(slot, data); 4345f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4355f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław to = cb710_wait_for_event(slot, CB710_MMC_S1_DATA_TRANSFER_DONE); 4365f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!error) 4375f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław error = to; 4385f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4395f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!error) 4405f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław data->bytes_xfered = data->blksz * data->blocks; 4415f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return error; 4425f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 4435f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4445f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_command(struct mmc_host *mmc, struct mmc_command *cmd) 4455f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 4465f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_slot *slot = cb710_mmc_to_slot(mmc); 4475f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_mmc_reader *reader = mmc_priv(mmc); 4485f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct mmc_data *data = cmd->data; 4495f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4505f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u16 cb_cmd = cb710_encode_cmd_flags(reader, cmd); 4515f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "cmd request: 0x%04X\n", cb_cmd); 4525f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4535f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (data) { 4545f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!cb710_is_transfer_size_supported(data)) { 4555f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław data->error = -EINVAL; 4565f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return -1; 4575f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 4585f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_set_transfer_size(slot, data->blocks, data->blksz); 4595f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 4605f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4615f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20|CB710_MMC_S2_BUSY_10); 4625f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_16(slot, CB710_MMC_CMD_TYPE_PORT, cb_cmd); 4635f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20); 4645f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_32(slot, CB710_MMC_CMD_PARAM_PORT, cmd->arg); 4655f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_reset_events(slot); 4665f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20); 4675f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x01, 0); 4685f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4695f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cmd->error = cb710_wait_for_event(slot, CB710_MMC_S1_COMMAND_SENT); 4705f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (cmd->error) 4715f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return -1; 4725f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4735f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (cmd->flags & MMC_RSP_PRESENT) { 4745f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_receive_response(slot, cmd); 4755f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (cmd->error) 4765f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return -1; 4775f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 4785f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4795f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (data) 4805f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław data->error = cb710_mmc_transfer_data(slot, data); 4815f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return 0; 4825f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 4835f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4845f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq) 4855f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 4865f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_slot *slot = cb710_mmc_to_slot(mmc); 4875f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_mmc_reader *reader = mmc_priv(mmc); 4885f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4895f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław WARN_ON(reader->mrq != NULL); 4905f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4915f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław reader->mrq = mrq; 4925f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_enable_irq(slot, CB710_MMC_IE_TEST_MASK, 0); 4935f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4947fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław if (!cb710_mmc_command(mmc, mrq->cmd) && mrq->stop) 4957fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław cb710_mmc_command(mmc, mrq->stop); 4965f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 4975f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław tasklet_schedule(&reader->finish_req_tasklet); 4985f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 4995f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 5005f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_powerup(struct cb710_slot *slot) 5015f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 5025f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#ifdef CONFIG_CB710_DEBUG 5035f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_chip *chip = cb710_slot_to_chip(slot); 5045f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#endif 5055f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int err; 5065f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 50719d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław /* a lot of magic for now */ 5085f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "bus powerup\n"); 5095f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 5105f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20); 5115f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (unlikely(err)) 5125f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return err; 5135f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x80, 0); 5145f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0x80, 0); 5155f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 5165f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mdelay(1); 5175f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "after delay 1\n"); 5185f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 5195f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20); 5205f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (unlikely(err)) 5215f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return err; 5225f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x09, 0); 5235f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 5245f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mdelay(1); 5255f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "after delay 2\n"); 5265f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 5275f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20); 5285f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (unlikely(err)) 5295f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return err; 5305f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0, 0x08); 5315f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 5325f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mdelay(2); 5335f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "after delay 3\n"); 5345f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 5355f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x06, 0); 5365f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0x70, 0); 5375f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG2_PORT, 0x80, 0); 5385f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0x03, 0); 5395f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 5405f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = cb710_wait_while_busy(slot, CB710_MMC_S2_BUSY_20); 5415f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (unlikely(err)) 5425f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return err; 5435f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* This port behaves weird: quick byte reads of 0x08,0x09 return 5445f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * 0xFF,0x00 after writing 0xFFFF to 0x08; it works correctly when 5455f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * read/written from userspace... What am I missing here? 5465f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław * (it doesn't depend on write-to-read delay) */ 5475f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_16(slot, CB710_MMC_CONFIGB_PORT, 0xFFFF); 5485f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG0_PORT, 0x06, 0); 5495f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 5505f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "bus powerup finished\n"); 5515f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 5525f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return cb710_check_event(slot, 0); 5535f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 5545f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 5555f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_mmc_powerdown(struct cb710_slot *slot) 5565f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 5575f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG1_PORT, 0, 0x81); 5585f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_modify_port_8(slot, CB710_MMC_CONFIG3_PORT, 0, 0x80); 5595f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 5605f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 5615f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 5625f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 5635f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_slot *slot = cb710_mmc_to_slot(mmc); 5645f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_mmc_reader *reader = mmc_priv(mmc); 5655f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int err; 5665f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 56719d614a45ae4beb4712bb50ba3d816ae3fe0f1f2Michał Mirosław cb710_mmc_select_clock_divider(mmc, ios->clock); 5685f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 5695f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (ios->power_mode != reader->last_power_mode) 5705f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław switch (ios->power_mode) { 5715f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław case MMC_POWER_ON: 5725f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = cb710_mmc_powerup(slot); 5735f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (err) { 5745f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_warn(cb710_slot_dev(slot), 5755f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław "powerup failed (%d)- retrying\n", err); 5765f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_powerdown(slot); 5775f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław udelay(1); 5785f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = cb710_mmc_powerup(slot); 5795f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (err) 5805f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_warn(cb710_slot_dev(slot), 5815f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław "powerup retry failed (%d) - expect errors\n", 5825f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err); 5835f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 5845f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław reader->last_power_mode = MMC_POWER_ON; 5855f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław break; 5865f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław case MMC_POWER_OFF: 5875f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_powerdown(slot); 5885f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław reader->last_power_mode = MMC_POWER_OFF; 5895f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław break; 5905f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław case MMC_POWER_UP: 5915f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław default: 5925f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* ignore */; 5935f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 5945f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 5955f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_enable_4bit_data(slot, ios->bus_width != MMC_BUS_WIDTH_1); 5965f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 5975f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_enable_irq(slot, CB710_MMC_IE_TEST_MASK, 0); 5985f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 5995f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6005f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_get_ro(struct mmc_host *mmc) 6015f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 6025f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_slot *slot = cb710_mmc_to_slot(mmc); 6035f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6045f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return cb710_read_port_8(slot, CB710_MMC_STATUS3_PORT) 6055f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław & CB710_MMC_S3_WRITE_PROTECTED; 6065f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 6075f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6087fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosławstatic int cb710_mmc_get_cd(struct mmc_host *mmc) 6097fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław{ 6107fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław struct cb710_slot *slot = cb710_mmc_to_slot(mmc); 6117fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław 6127fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław return cb710_read_port_8(slot, CB710_MMC_STATUS3_PORT) 6137fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław & CB710_MMC_S3_CARD_DETECTED; 6147fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław} 6157fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław 6165f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_irq_handler(struct cb710_slot *slot) 6175f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 6185f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct mmc_host *mmc = cb710_slot_to_mmc(slot); 6195f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_mmc_reader *reader = mmc_priv(mmc); 6205f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u32 status, config1, config2, irqen; 6215f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6225f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław status = cb710_read_port_32(slot, CB710_MMC_STATUS_PORT); 6235f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław irqen = cb710_read_port_32(slot, CB710_MMC_IRQ_ENABLE_PORT); 6245f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław config2 = cb710_read_port_32(slot, CB710_MMC_CONFIGB_PORT); 6255f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław config1 = cb710_read_port_32(slot, CB710_MMC_CONFIG_PORT); 6265f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6275f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "interrupt; status: %08X, " 6285f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław "ie: %08X, c2: %08X, c1: %08X\n", 6295f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław status, irqen, config2, config1); 6305f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6315f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (status & (CB710_MMC_S1_CARD_CHANGED << 8)) { 6325f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* ack the event */ 6335f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_8(slot, CB710_MMC_STATUS1_PORT, 6345f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław CB710_MMC_S1_CARD_CHANGED); 6355f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if ((irqen & CB710_MMC_IE_CISTATUS_MASK) 6365f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław == CB710_MMC_IE_CISTATUS_MASK) 6375f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc_detect_change(mmc, HZ/5); 6385f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } else { 6395f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "unknown interrupt (test)\n"); 6405f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław spin_lock(&reader->irq_lock); 6415f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław __cb710_mmc_enable_irq(slot, 0, CB710_MMC_IE_TEST_MASK); 6425f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław spin_unlock(&reader->irq_lock); 6435f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław } 6445f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6455f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return 1; 6465f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 6475f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6485f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic void cb710_mmc_finish_request_tasklet(unsigned long data) 6495f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 6505f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct mmc_host *mmc = (void *)data; 6515f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_mmc_reader *reader = mmc_priv(mmc); 6525f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct mmc_request *mrq = reader->mrq; 6535f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6545f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław reader->mrq = NULL; 6555f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc_request_done(mmc, mrq); 6565f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 6575f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6585f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic const struct mmc_host_ops cb710_mmc_host = { 6595f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław .request = cb710_mmc_request, 6605f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław .set_ios = cb710_mmc_set_ios, 6617fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław .get_ro = cb710_mmc_get_ro, 6627fcc4ce37f0ccec846e32e1f5ff0647ded74fec9Michał Mirosław .get_cd = cb710_mmc_get_cd, 6635f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław}; 6645f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6655f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#ifdef CONFIG_PM 6665f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6675f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_suspend(struct platform_device *pdev, pm_message_t state) 6685f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 6695f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_slot *slot = cb710_pdev_to_slot(pdev); 6705f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6715f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_enable_irq(slot, 0, ~0); 6725f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return 0; 6735f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 6745f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6755f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic int cb710_mmc_resume(struct platform_device *pdev) 6765f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 6775f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_slot *slot = cb710_pdev_to_slot(pdev); 6785f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6795f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_enable_irq(slot, 0, ~0); 680d978a2b631506bd0501ceb185a21d23cd63b8790Ulf Hansson return 0; 6815f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 6825f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6835f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#endif /* CONFIG_PM */ 6845f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 685c3be1efd41a97f93be390240387d356a07b664c7Bill Pembertonstatic int cb710_mmc_init(struct platform_device *pdev) 6865f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 6875f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_slot *slot = cb710_pdev_to_slot(pdev); 6885f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_chip *chip = cb710_slot_to_chip(slot); 6895f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct mmc_host *mmc; 6905f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_mmc_reader *reader; 6915f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław int err; 6925f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław u32 val; 6935f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6945f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc = mmc_alloc_host(sizeof(*reader), cb710_slot_dev(slot)); 6955f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (!mmc) 6965f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return -ENOMEM; 6975f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 6986e2c0f3ffbb54547edcf1dd92a120ff37988a4d8Jingoo Han platform_set_drvdata(pdev, mmc); 6995f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7005f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* harmless (maybe) magic */ 7015f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław pci_read_config_dword(chip->pdev, 0x48, &val); 7025f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław val = cb710_src_freq_mhz[(val >> 16) & 0xF]; 7035f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "source frequency: %dMHz\n", val); 7045f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław val *= 1000000; 7055f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7065f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc->ops = &cb710_mmc_host; 7075f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc->f_max = val; 7085f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc->f_min = val >> cb710_clock_divider_log2[CB710_MAX_DIVIDER_IDX]; 7095f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34; 7105f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc->caps = MMC_CAP_4_BIT_DATA; 7115f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7125f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław reader = mmc_priv(mmc); 7135f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7145f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław tasklet_init(&reader->finish_req_tasklet, 7155f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_finish_request_tasklet, (unsigned long)mmc); 7165f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław spin_lock_init(&reader->irq_lock); 7175f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_dump_regs(chip, CB710_DUMP_REGS_MMC); 7185f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7195f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_enable_irq(slot, 0, ~0); 7205f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_set_irq_handler(slot, cb710_mmc_irq_handler); 7215f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7225f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław err = mmc_add_host(mmc); 7235f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław if (unlikely(err)) 7245f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław goto err_free_mmc; 7255f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7265f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "mmc_hostname is %s\n", 7275f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc_hostname(mmc)); 7285f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7295f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_enable_irq(slot, CB710_MMC_IE_CARD_INSERTION_STATUS, 0); 7305f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7315f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return 0; 7325f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7335f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławerr_free_mmc: 7345f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław dev_dbg(cb710_slot_dev(slot), "mmc_add_host() failed: %d\n", err); 7355f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 736e0780db9f8ccf2dbca3161dc153bbf54b53cb6afMichał Mirosław cb710_set_irq_handler(slot, NULL); 7375f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc_free_host(mmc); 7385f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return err; 7395f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 7405f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7416e0ee714fdab0568c3487455951dea2673e9557fBill Pembertonstatic int cb710_mmc_exit(struct platform_device *pdev) 7425f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław{ 7435f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_slot *slot = cb710_pdev_to_slot(pdev); 7445f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct mmc_host *mmc = cb710_slot_to_mmc(slot); 7455f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław struct cb710_mmc_reader *reader = mmc_priv(mmc); 7465f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7475f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_enable_irq(slot, 0, CB710_MMC_IE_CARD_INSERTION_STATUS); 7485f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7495f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc_remove_host(mmc); 7505f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7515f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* IRQs should be disabled now, but let's stay on the safe side */ 7525f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_mmc_enable_irq(slot, 0, ~0); 7535f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_set_irq_handler(slot, NULL); 7545f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7555f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław /* clear config ports - just in case */ 7565f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_32(slot, CB710_MMC_CONFIG_PORT, 0); 7575f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław cb710_write_port_16(slot, CB710_MMC_CONFIGB_PORT, 0); 7585f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7595f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław tasklet_kill(&reader->finish_req_tasklet); 7605f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7615f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław mmc_free_host(mmc); 7625f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław return 0; 7635f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław} 7645f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7655f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosławstatic struct platform_driver cb710_mmc_driver = { 7665f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław .driver.name = "cb710-mmc", 7675f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław .probe = cb710_mmc_init, 7680433c14356702e296f474f77ebd42f0a9d9a5487Bill Pemberton .remove = cb710_mmc_exit, 7695f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#ifdef CONFIG_PM 7705f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław .suspend = cb710_mmc_suspend, 7715f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław .resume = cb710_mmc_resume, 7725f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław#endif 7735f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław}; 7745f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 775d1f81a64a4250bdd776978be06ae2b8e13ec7471Axel Linmodule_platform_driver(cb710_mmc_driver); 7765f5bac8272be791b67c7b7b411e7c8c5847e598aMichał Mirosław 7775f5bac8272be791b67c7b7b411e7c8c5847e598aMichał MirosławMODULE_AUTHOR("Michał Mirosław <mirq-linux@rere.qmqm.pl>"); 7785f5bac8272be791b67c7b7b411e7c8c5847e598aMichał MirosławMODULE_DESCRIPTION("ENE CB710 memory card reader driver - MMC/SD part"); 7795f5bac8272be791b67c7b7b411e7c8c5847e598aMichał MirosławMODULE_LICENSE("GPL"); 7805f5bac8272be791b67c7b7b411e7c8c5847e598aMichał MirosławMODULE_ALIAS("platform:cb710-mmc"); 781