162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  Toshiba PCI Secure Digital Host Controller Interface driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (C) 2014 Ondrej Zary
662306a36Sopenharmony_ci *  Copyright (C) 2007 Richard Betts, All Rights Reserved.
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci *      Based on asic3_mmc.c Copyright (c) 2005 SDG Systems, LLC
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#define HCLK	33000000	/* 33 MHz (PCI clock) */
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#define SD_PCICFG_CLKSTOP	0x40	/* 0x1f = clock controller, 0 = stop */
1462306a36Sopenharmony_ci#define SD_PCICFG_GATEDCLK	0x41	/* Gated clock */
1562306a36Sopenharmony_ci#define SD_PCICFG_CLKMODE	0x42	/* Control clock of SD controller */
1662306a36Sopenharmony_ci#define SD_PCICFG_PINSTATUS	0x44	/* R/O: read status of SD pins */
1762306a36Sopenharmony_ci#define SD_PCICFG_POWER1	0x48
1862306a36Sopenharmony_ci#define SD_PCICFG_POWER2	0x49
1962306a36Sopenharmony_ci#define SD_PCICFG_POWER3	0x4a
2062306a36Sopenharmony_ci#define SD_PCICFG_CARDDETECT	0x4c
2162306a36Sopenharmony_ci#define SD_PCICFG_SLOTS		0x50	/* R/O: define support slot number */
2262306a36Sopenharmony_ci#define SD_PCICFG_EXTGATECLK1	0xf0	/* Could be used for gated clock */
2362306a36Sopenharmony_ci#define SD_PCICFG_EXTGATECLK2	0xf1	/* Could be used for gated clock */
2462306a36Sopenharmony_ci#define SD_PCICFG_EXTGATECLK3	0xf9	/* Bit 1: double buffer/single buffer */
2562306a36Sopenharmony_ci#define SD_PCICFG_SDLED_ENABLE1	0xfa
2662306a36Sopenharmony_ci#define SD_PCICFG_SDLED_ENABLE2	0xfe
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#define SD_PCICFG_CLKMODE_DIV_DISABLE	BIT(0)
2962306a36Sopenharmony_ci#define SD_PCICFG_CLKSTOP_ENABLE_ALL	0x1f
3062306a36Sopenharmony_ci#define SD_PCICFG_LED_ENABLE1_START	0x12
3162306a36Sopenharmony_ci#define SD_PCICFG_LED_ENABLE2_START	0x80
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define SD_PCICFG_PWR1_33V	0x08	/* Set for 3.3 volts */
3462306a36Sopenharmony_ci#define SD_PCICFG_PWR1_OFF	0x00	/* Turn off power */
3562306a36Sopenharmony_ci#define SD_PCICFG_PWR2_AUTO	0x02
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#define SD_CMD			0x00	/* also for SDIO */
3862306a36Sopenharmony_ci#define SD_ARG0			0x04	/* also for SDIO */
3962306a36Sopenharmony_ci#define SD_ARG1			0x06	/* also for SDIO */
4062306a36Sopenharmony_ci#define SD_STOPINTERNAL		0x08
4162306a36Sopenharmony_ci#define SD_BLOCKCOUNT		0x0a	/* also for SDIO */
4262306a36Sopenharmony_ci#define SD_RESPONSE0		0x0c	/* also for SDIO */
4362306a36Sopenharmony_ci#define SD_RESPONSE1		0x0e	/* also for SDIO */
4462306a36Sopenharmony_ci#define SD_RESPONSE2		0x10	/* also for SDIO */
4562306a36Sopenharmony_ci#define SD_RESPONSE3		0x12	/* also for SDIO */
4662306a36Sopenharmony_ci#define SD_RESPONSE4		0x14	/* also for SDIO */
4762306a36Sopenharmony_ci#define SD_RESPONSE5		0x16	/* also for SDIO */
4862306a36Sopenharmony_ci#define SD_RESPONSE6		0x18	/* also for SDIO */
4962306a36Sopenharmony_ci#define SD_RESPONSE7		0x1a	/* also for SDIO */
5062306a36Sopenharmony_ci#define SD_CARDSTATUS		0x1c	/* also for SDIO */
5162306a36Sopenharmony_ci#define SD_BUFFERCTRL		0x1e	/* also for SDIO */
5262306a36Sopenharmony_ci#define SD_INTMASKCARD		0x20	/* also for SDIO */
5362306a36Sopenharmony_ci#define SD_INTMASKBUFFER	0x22	/* also for SDIO */
5462306a36Sopenharmony_ci#define SD_CARDCLOCKCTRL	0x24
5562306a36Sopenharmony_ci#define SD_CARDXFERDATALEN	0x26	/* also for SDIO */
5662306a36Sopenharmony_ci#define SD_CARDOPTIONSETUP	0x28	/* also for SDIO */
5762306a36Sopenharmony_ci#define SD_ERRORSTATUS0		0x2c	/* also for SDIO */
5862306a36Sopenharmony_ci#define SD_ERRORSTATUS1		0x2e	/* also for SDIO */
5962306a36Sopenharmony_ci#define SD_DATAPORT		0x30	/* also for SDIO */
6062306a36Sopenharmony_ci#define SD_TRANSACTIONCTRL	0x34	/* also for SDIO */
6162306a36Sopenharmony_ci#define SD_SOFTWARERESET	0xe0	/* also for SDIO */
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/* registers above marked "also for SDIO" and all SDIO registers below can be
6462306a36Sopenharmony_ci * accessed at SDIO_BASE + reg address */
6562306a36Sopenharmony_ci#define SDIO_BASE	 0x100
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#define SDIO_CARDPORTSEL	0x02
6862306a36Sopenharmony_ci#define SDIO_CARDINTCTRL	0x36
6962306a36Sopenharmony_ci#define SDIO_CLOCKNWAITCTRL	0x38
7062306a36Sopenharmony_ci#define SDIO_HOSTINFORMATION	0x3a
7162306a36Sopenharmony_ci#define SDIO_ERRORCTRL		0x3c
7262306a36Sopenharmony_ci#define SDIO_LEDCTRL		0x3e
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define SD_TRANSCTL_SET		BIT(8)
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci#define SD_CARDCLK_DIV_DISABLE	BIT(15)
7762306a36Sopenharmony_ci#define SD_CARDCLK_ENABLE_CLOCK	BIT(8)
7862306a36Sopenharmony_ci#define SD_CARDCLK_CLK_DIV_512	BIT(7)
7962306a36Sopenharmony_ci#define SD_CARDCLK_CLK_DIV_256	BIT(6)
8062306a36Sopenharmony_ci#define SD_CARDCLK_CLK_DIV_128	BIT(5)
8162306a36Sopenharmony_ci#define SD_CARDCLK_CLK_DIV_64	BIT(4)
8262306a36Sopenharmony_ci#define SD_CARDCLK_CLK_DIV_32	BIT(3)
8362306a36Sopenharmony_ci#define SD_CARDCLK_CLK_DIV_16	BIT(2)
8462306a36Sopenharmony_ci#define SD_CARDCLK_CLK_DIV_8	BIT(1)
8562306a36Sopenharmony_ci#define SD_CARDCLK_CLK_DIV_4	BIT(0)
8662306a36Sopenharmony_ci#define SD_CARDCLK_CLK_DIV_2	0
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci#define SD_CARDOPT_REQUIRED		0x000e
8962306a36Sopenharmony_ci#define SD_CARDOPT_DATA_RESP_TIMEOUT(x)	(((x) & 0x0f) << 4) /* 4 bits */
9062306a36Sopenharmony_ci#define SD_CARDOPT_C2_MODULE_ABSENT	BIT(14)
9162306a36Sopenharmony_ci#define SD_CARDOPT_DATA_XFR_WIDTH_1	(1 << 15)
9262306a36Sopenharmony_ci#define SD_CARDOPT_DATA_XFR_WIDTH_4	(0 << 15)
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci#define SD_CMD_TYPE_CMD			(0 << 6)
9562306a36Sopenharmony_ci#define SD_CMD_TYPE_ACMD		(1 << 6)
9662306a36Sopenharmony_ci#define SD_CMD_TYPE_AUTHEN		(2 << 6)
9762306a36Sopenharmony_ci#define SD_CMD_RESP_TYPE_NONE		(3 << 8)
9862306a36Sopenharmony_ci#define SD_CMD_RESP_TYPE_EXT_R1		(4 << 8)
9962306a36Sopenharmony_ci#define SD_CMD_RESP_TYPE_EXT_R1B	(5 << 8)
10062306a36Sopenharmony_ci#define SD_CMD_RESP_TYPE_EXT_R2		(6 << 8)
10162306a36Sopenharmony_ci#define SD_CMD_RESP_TYPE_EXT_R3		(7 << 8)
10262306a36Sopenharmony_ci#define SD_CMD_RESP_TYPE_EXT_R6		(4 << 8)
10362306a36Sopenharmony_ci#define SD_CMD_RESP_TYPE_EXT_R7		(4 << 8)
10462306a36Sopenharmony_ci#define SD_CMD_DATA_PRESENT		BIT(11)
10562306a36Sopenharmony_ci#define SD_CMD_TRANSFER_READ		BIT(12)
10662306a36Sopenharmony_ci#define SD_CMD_MULTI_BLOCK		BIT(13)
10762306a36Sopenharmony_ci#define SD_CMD_SECURITY_CMD		BIT(14)
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci#define SD_STOPINT_ISSUE_CMD12		BIT(0)
11062306a36Sopenharmony_ci#define SD_STOPINT_AUTO_ISSUE_CMD12	BIT(8)
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci#define SD_CARD_RESP_END	BIT(0)
11362306a36Sopenharmony_ci#define SD_CARD_RW_END		BIT(2)
11462306a36Sopenharmony_ci#define SD_CARD_CARD_REMOVED_0	BIT(3)
11562306a36Sopenharmony_ci#define SD_CARD_CARD_INSERTED_0	BIT(4)
11662306a36Sopenharmony_ci#define SD_CARD_PRESENT_0	BIT(5)
11762306a36Sopenharmony_ci#define SD_CARD_UNK6		BIT(6)
11862306a36Sopenharmony_ci#define SD_CARD_WRITE_PROTECT	BIT(7)
11962306a36Sopenharmony_ci#define SD_CARD_CARD_REMOVED_3	BIT(8)
12062306a36Sopenharmony_ci#define SD_CARD_CARD_INSERTED_3	BIT(9)
12162306a36Sopenharmony_ci#define SD_CARD_PRESENT_3	BIT(10)
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#define SD_BUF_CMD_INDEX_ERR	BIT(16)
12462306a36Sopenharmony_ci#define SD_BUF_CRC_ERR		BIT(17)
12562306a36Sopenharmony_ci#define SD_BUF_STOP_BIT_END_ERR	BIT(18)
12662306a36Sopenharmony_ci#define SD_BUF_DATA_TIMEOUT	BIT(19)
12762306a36Sopenharmony_ci#define SD_BUF_OVERFLOW		BIT(20)
12862306a36Sopenharmony_ci#define SD_BUF_UNDERFLOW	BIT(21)
12962306a36Sopenharmony_ci#define SD_BUF_CMD_TIMEOUT	BIT(22)
13062306a36Sopenharmony_ci#define SD_BUF_UNK7		BIT(23)
13162306a36Sopenharmony_ci#define SD_BUF_READ_ENABLE	BIT(24)
13262306a36Sopenharmony_ci#define SD_BUF_WRITE_ENABLE	BIT(25)
13362306a36Sopenharmony_ci#define SD_BUF_ILLEGAL_FUNCTION	BIT(29)
13462306a36Sopenharmony_ci#define SD_BUF_CMD_BUSY		BIT(30)
13562306a36Sopenharmony_ci#define SD_BUF_ILLEGAL_ACCESS	BIT(31)
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci#define SD_ERR0_RESP_CMD_ERR			BIT(0)
13862306a36Sopenharmony_ci#define SD_ERR0_RESP_NON_CMD12_END_BIT_ERR	BIT(2)
13962306a36Sopenharmony_ci#define SD_ERR0_RESP_CMD12_END_BIT_ERR		BIT(3)
14062306a36Sopenharmony_ci#define SD_ERR0_READ_DATA_END_BIT_ERR		BIT(4)
14162306a36Sopenharmony_ci#define SD_ERR0_WRITE_CRC_STATUS_END_BIT_ERR	BIT(5)
14262306a36Sopenharmony_ci#define SD_ERR0_RESP_NON_CMD12_CRC_ERR		BIT(8)
14362306a36Sopenharmony_ci#define SD_ERR0_RESP_CMD12_CRC_ERR		BIT(9)
14462306a36Sopenharmony_ci#define SD_ERR0_READ_DATA_CRC_ERR		BIT(10)
14562306a36Sopenharmony_ci#define SD_ERR0_WRITE_CMD_CRC_ERR		BIT(11)
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci#define SD_ERR1_NO_CMD_RESP		BIT(16)
14862306a36Sopenharmony_ci#define SD_ERR1_TIMEOUT_READ_DATA	BIT(20)
14962306a36Sopenharmony_ci#define SD_ERR1_TIMEOUT_CRS_STATUS	BIT(21)
15062306a36Sopenharmony_ci#define SD_ERR1_TIMEOUT_CRC_BUSY	BIT(22)
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci#define IRQ_DONT_CARE_BITS (SD_CARD_PRESENT_3 \
15362306a36Sopenharmony_ci	| SD_CARD_WRITE_PROTECT \
15462306a36Sopenharmony_ci	| SD_CARD_UNK6 \
15562306a36Sopenharmony_ci	| SD_CARD_PRESENT_0 \
15662306a36Sopenharmony_ci	| SD_BUF_UNK7 \
15762306a36Sopenharmony_ci	| SD_BUF_CMD_BUSY)
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_cistruct toshsd_host {
16062306a36Sopenharmony_ci	struct pci_dev *pdev;
16162306a36Sopenharmony_ci	struct mmc_host *mmc;
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	spinlock_t lock;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	struct mmc_request *mrq;/* Current request */
16662306a36Sopenharmony_ci	struct mmc_command *cmd;/* Current command */
16762306a36Sopenharmony_ci	struct mmc_data *data;	/* Current data request */
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	struct sg_mapping_iter sg_miter; /* for PIO */
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	void __iomem *ioaddr; /* mapped address */
17262306a36Sopenharmony_ci};
173