blob: 9fdaea37aa7473c9f80257c3decaf6a6a4f3526c [file] [log] [blame]
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001/*
2 * Driver for sunxi SD/MMC host controllers
3 * (C) Copyright 2007-2011 Reuuimlla Technology Co., Ltd.
4 * (C) Copyright 2007-2011 Aaron Maoye <leafy.myeh@reuuimllatech.com>
5 * (C) Copyright 2013-2014 O2S GmbH <www.o2s.ch>
Adam Borowski1907e382017-11-28 04:44:55 +01006 * (C) Copyright 2013-2014 David Lanzendörfer <david.lanzendoerfer@o2s.ch>
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02007 * (C) Copyright 2013-2014 Hans de Goede <hdegoede@redhat.com>
Maxime Ripard4fb3ce02017-01-27 22:38:38 +01008 * (C) Copyright 2017 Sootech SA
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 */
15
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +020016#include <linux/clk.h>
Chen-Yu Tsaiff39e7f2017-07-24 21:58:59 +080017#include <linux/clk/sunxi-ng.h>
Maxime Ripard743b8192018-04-16 16:22:59 +020018#include <linux/delay.h>
19#include <linux/device.h>
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +020020#include <linux/dma-mapping.h>
Maxime Ripard743b8192018-04-16 16:22:59 +020021#include <linux/err.h>
22#include <linux/gpio.h>
23#include <linux/interrupt.h>
24#include <linux/io.h>
25#include <linux/kernel.h>
26#include <linux/mmc/card.h>
27#include <linux/mmc/core.h>
28#include <linux/mmc/host.h>
29#include <linux/mmc/mmc.h>
30#include <linux/mmc/sd.h>
31#include <linux/mmc/sdio.h>
32#include <linux/mmc/slot-gpio.h>
33#include <linux/module.h>
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +020034#include <linux/of_address.h>
35#include <linux/of_gpio.h>
36#include <linux/of_platform.h>
Maxime Ripard743b8192018-04-16 16:22:59 +020037#include <linux/platform_device.h>
38#include <linux/regulator/consumer.h>
39#include <linux/reset.h>
40#include <linux/scatterlist.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +020043
44/* register offset definitions */
45#define SDXC_REG_GCTRL (0x00) /* SMC Global Control Register */
46#define SDXC_REG_CLKCR (0x04) /* SMC Clock Control Register */
47#define SDXC_REG_TMOUT (0x08) /* SMC Time Out Register */
48#define SDXC_REG_WIDTH (0x0C) /* SMC Bus Width Register */
49#define SDXC_REG_BLKSZ (0x10) /* SMC Block Size Register */
50#define SDXC_REG_BCNTR (0x14) /* SMC Byte Count Register */
51#define SDXC_REG_CMDR (0x18) /* SMC Command Register */
52#define SDXC_REG_CARG (0x1C) /* SMC Argument Register */
53#define SDXC_REG_RESP0 (0x20) /* SMC Response Register 0 */
54#define SDXC_REG_RESP1 (0x24) /* SMC Response Register 1 */
55#define SDXC_REG_RESP2 (0x28) /* SMC Response Register 2 */
56#define SDXC_REG_RESP3 (0x2C) /* SMC Response Register 3 */
57#define SDXC_REG_IMASK (0x30) /* SMC Interrupt Mask Register */
58#define SDXC_REG_MISTA (0x34) /* SMC Masked Interrupt Status Register */
59#define SDXC_REG_RINTR (0x38) /* SMC Raw Interrupt Status Register */
60#define SDXC_REG_STAS (0x3C) /* SMC Status Register */
61#define SDXC_REG_FTRGL (0x40) /* SMC FIFO Threshold Watermark Registe */
62#define SDXC_REG_FUNS (0x44) /* SMC Function Select Register */
63#define SDXC_REG_CBCR (0x48) /* SMC CIU Byte Count Register */
64#define SDXC_REG_BBCR (0x4C) /* SMC BIU Byte Count Register */
65#define SDXC_REG_DBGC (0x50) /* SMC Debug Enable Register */
66#define SDXC_REG_HWRST (0x78) /* SMC Card Hardware Reset for Register */
67#define SDXC_REG_DMAC (0x80) /* SMC IDMAC Control Register */
68#define SDXC_REG_DLBA (0x84) /* SMC IDMAC Descriptor List Base Addre */
69#define SDXC_REG_IDST (0x88) /* SMC IDMAC Status Register */
70#define SDXC_REG_IDIE (0x8C) /* SMC IDMAC Interrupt Enable Register */
71#define SDXC_REG_CHDA (0x90)
72#define SDXC_REG_CBDA (0x94)
73
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +020074/* New registers introduced in A64 */
75#define SDXC_REG_A12A 0x058 /* SMC Auto Command 12 Register */
76#define SDXC_REG_SD_NTSR 0x05C /* SMC New Timing Set Register */
77#define SDXC_REG_DRV_DL 0x140 /* Drive Delay Control Register */
78#define SDXC_REG_SAMP_DL_REG 0x144 /* SMC sample delay control */
79#define SDXC_REG_DS_DL_REG 0x148 /* SMC data strobe delay control */
80
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +020081#define mmc_readl(host, reg) \
82 readl((host)->reg_base + SDXC_##reg)
83#define mmc_writel(host, reg, value) \
84 writel((value), (host)->reg_base + SDXC_##reg)
85
86/* global control register bits */
87#define SDXC_SOFT_RESET BIT(0)
88#define SDXC_FIFO_RESET BIT(1)
89#define SDXC_DMA_RESET BIT(2)
90#define SDXC_INTERRUPT_ENABLE_BIT BIT(4)
91#define SDXC_DMA_ENABLE_BIT BIT(5)
92#define SDXC_DEBOUNCE_ENABLE_BIT BIT(8)
93#define SDXC_POSEDGE_LATCH_DATA BIT(9)
94#define SDXC_DDR_MODE BIT(10)
95#define SDXC_MEMORY_ACCESS_DONE BIT(29)
96#define SDXC_ACCESS_DONE_DIRECT BIT(30)
97#define SDXC_ACCESS_BY_AHB BIT(31)
98#define SDXC_ACCESS_BY_DMA (0 << 31)
99#define SDXC_HARDWARE_RESET \
100 (SDXC_SOFT_RESET | SDXC_FIFO_RESET | SDXC_DMA_RESET)
101
102/* clock control bits */
Maxime Ripard16e821e2017-01-27 22:38:37 +0100103#define SDXC_MASK_DATA0 BIT(31)
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200104#define SDXC_CARD_CLOCK_ON BIT(16)
105#define SDXC_LOW_POWER_ON BIT(17)
106
107/* bus width */
108#define SDXC_WIDTH1 0
109#define SDXC_WIDTH4 1
110#define SDXC_WIDTH8 2
111
112/* smc command bits */
113#define SDXC_RESP_EXPIRE BIT(6)
114#define SDXC_LONG_RESPONSE BIT(7)
115#define SDXC_CHECK_RESPONSE_CRC BIT(8)
116#define SDXC_DATA_EXPIRE BIT(9)
117#define SDXC_WRITE BIT(10)
118#define SDXC_SEQUENCE_MODE BIT(11)
119#define SDXC_SEND_AUTO_STOP BIT(12)
120#define SDXC_WAIT_PRE_OVER BIT(13)
121#define SDXC_STOP_ABORT_CMD BIT(14)
122#define SDXC_SEND_INIT_SEQUENCE BIT(15)
123#define SDXC_UPCLK_ONLY BIT(21)
124#define SDXC_READ_CEATA_DEV BIT(22)
125#define SDXC_CCS_EXPIRE BIT(23)
126#define SDXC_ENABLE_BIT_BOOT BIT(24)
127#define SDXC_ALT_BOOT_OPTIONS BIT(25)
128#define SDXC_BOOT_ACK_EXPIRE BIT(26)
129#define SDXC_BOOT_ABORT BIT(27)
130#define SDXC_VOLTAGE_SWITCH BIT(28)
131#define SDXC_USE_HOLD_REGISTER BIT(29)
132#define SDXC_START BIT(31)
133
134/* interrupt bits */
135#define SDXC_RESP_ERROR BIT(1)
136#define SDXC_COMMAND_DONE BIT(2)
137#define SDXC_DATA_OVER BIT(3)
138#define SDXC_TX_DATA_REQUEST BIT(4)
139#define SDXC_RX_DATA_REQUEST BIT(5)
140#define SDXC_RESP_CRC_ERROR BIT(6)
141#define SDXC_DATA_CRC_ERROR BIT(7)
142#define SDXC_RESP_TIMEOUT BIT(8)
143#define SDXC_DATA_TIMEOUT BIT(9)
144#define SDXC_VOLTAGE_CHANGE_DONE BIT(10)
145#define SDXC_FIFO_RUN_ERROR BIT(11)
146#define SDXC_HARD_WARE_LOCKED BIT(12)
147#define SDXC_START_BIT_ERROR BIT(13)
148#define SDXC_AUTO_COMMAND_DONE BIT(14)
149#define SDXC_END_BIT_ERROR BIT(15)
150#define SDXC_SDIO_INTERRUPT BIT(16)
151#define SDXC_CARD_INSERT BIT(30)
152#define SDXC_CARD_REMOVE BIT(31)
153#define SDXC_INTERRUPT_ERROR_BIT \
154 (SDXC_RESP_ERROR | SDXC_RESP_CRC_ERROR | SDXC_DATA_CRC_ERROR | \
155 SDXC_RESP_TIMEOUT | SDXC_DATA_TIMEOUT | SDXC_FIFO_RUN_ERROR | \
156 SDXC_HARD_WARE_LOCKED | SDXC_START_BIT_ERROR | SDXC_END_BIT_ERROR)
157#define SDXC_INTERRUPT_DONE_BIT \
158 (SDXC_AUTO_COMMAND_DONE | SDXC_DATA_OVER | \
159 SDXC_COMMAND_DONE | SDXC_VOLTAGE_CHANGE_DONE)
160
161/* status */
162#define SDXC_RXWL_FLAG BIT(0)
163#define SDXC_TXWL_FLAG BIT(1)
164#define SDXC_FIFO_EMPTY BIT(2)
165#define SDXC_FIFO_FULL BIT(3)
166#define SDXC_CARD_PRESENT BIT(8)
167#define SDXC_CARD_DATA_BUSY BIT(9)
168#define SDXC_DATA_FSM_BUSY BIT(10)
169#define SDXC_DMA_REQUEST BIT(31)
170#define SDXC_FIFO_SIZE 16
171
172/* Function select */
173#define SDXC_CEATA_ON (0xceaa << 16)
174#define SDXC_SEND_IRQ_RESPONSE BIT(0)
175#define SDXC_SDIO_READ_WAIT BIT(1)
176#define SDXC_ABORT_READ_DATA BIT(2)
177#define SDXC_SEND_CCSD BIT(8)
178#define SDXC_SEND_AUTO_STOPCCSD BIT(9)
179#define SDXC_CEATA_DEV_IRQ_ENABLE BIT(10)
180
181/* IDMA controller bus mod bit field */
182#define SDXC_IDMAC_SOFT_RESET BIT(0)
183#define SDXC_IDMAC_FIX_BURST BIT(1)
184#define SDXC_IDMAC_IDMA_ON BIT(7)
185#define SDXC_IDMAC_REFETCH_DES BIT(31)
186
187/* IDMA status bit field */
188#define SDXC_IDMAC_TRANSMIT_INTERRUPT BIT(0)
189#define SDXC_IDMAC_RECEIVE_INTERRUPT BIT(1)
190#define SDXC_IDMAC_FATAL_BUS_ERROR BIT(2)
191#define SDXC_IDMAC_DESTINATION_INVALID BIT(4)
192#define SDXC_IDMAC_CARD_ERROR_SUM BIT(5)
193#define SDXC_IDMAC_NORMAL_INTERRUPT_SUM BIT(8)
194#define SDXC_IDMAC_ABNORMAL_INTERRUPT_SUM BIT(9)
195#define SDXC_IDMAC_HOST_ABORT_INTERRUPT BIT(10)
196#define SDXC_IDMAC_IDLE (0 << 13)
197#define SDXC_IDMAC_SUSPEND (1 << 13)
198#define SDXC_IDMAC_DESC_READ (2 << 13)
199#define SDXC_IDMAC_DESC_CHECK (3 << 13)
200#define SDXC_IDMAC_READ_REQUEST_WAIT (4 << 13)
201#define SDXC_IDMAC_WRITE_REQUEST_WAIT (5 << 13)
202#define SDXC_IDMAC_READ (6 << 13)
203#define SDXC_IDMAC_WRITE (7 << 13)
204#define SDXC_IDMAC_DESC_CLOSE (8 << 13)
205
206/*
207* If the idma-des-size-bits of property is ie 13, bufsize bits are:
208* Bits 0-12: buf1 size
209* Bits 13-25: buf2 size
210* Bits 26-31: not used
211* Since we only ever set buf1 size, we can simply store it directly.
212*/
213#define SDXC_IDMAC_DES0_DIC BIT(1) /* disable interrupt on completion */
214#define SDXC_IDMAC_DES0_LD BIT(2) /* last descriptor */
215#define SDXC_IDMAC_DES0_FD BIT(3) /* first descriptor */
216#define SDXC_IDMAC_DES0_CH BIT(4) /* chain mode */
217#define SDXC_IDMAC_DES0_ER BIT(5) /* end of ring */
218#define SDXC_IDMAC_DES0_CES BIT(30) /* card error summary */
219#define SDXC_IDMAC_DES0_OWN BIT(31) /* 1-idma owns it, 0-host owns it */
220
Hans de Goede51424b22015-09-23 22:06:48 +0200221#define SDXC_CLK_400K 0
222#define SDXC_CLK_25M 1
223#define SDXC_CLK_50M 2
224#define SDXC_CLK_50M_DDR 3
Chen-Yu Tsai2a7aa632016-01-30 01:21:47 +0800225#define SDXC_CLK_50M_DDR_8BIT 4
Hans de Goede51424b22015-09-23 22:06:48 +0200226
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +0200227#define SDXC_2X_TIMING_MODE BIT(31)
228
229#define SDXC_CAL_START BIT(15)
230#define SDXC_CAL_DONE BIT(14)
231#define SDXC_CAL_DL_SHIFT 8
232#define SDXC_CAL_DL_SW_EN BIT(7)
233#define SDXC_CAL_DL_SW_SHIFT 0
234#define SDXC_CAL_DL_MASK 0x3f
235
236#define SDXC_CAL_TIMEOUT 3 /* in seconds, 3s is enough*/
237
Hans de Goede51424b22015-09-23 22:06:48 +0200238struct sunxi_mmc_clk_delay {
239 u32 output;
240 u32 sample;
241};
242
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200243struct sunxi_idma_des {
Michael Weiser2dd110b2016-08-22 18:42:18 +0200244 __le32 config;
245 __le32 buf_size;
246 __le32 buf_addr_ptr1;
247 __le32 buf_addr_ptr2;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200248};
249
Hans de Goede86a93312016-07-30 16:25:45 +0200250struct sunxi_mmc_cfg {
251 u32 idma_des_size_bits;
252 const struct sunxi_mmc_clk_delay *clk_delays;
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +0200253
254 /* does the IP block support autocalibration? */
255 bool can_calibrate;
Maxime Ripard9a37e532017-01-27 22:38:36 +0100256
Maxime Ripard16e821e2017-01-27 22:38:37 +0100257 /* Does DATA0 needs to be masked while the clock is updated */
258 bool mask_data0;
259
Chen-Yu Tsaiff39e7f2017-07-24 21:58:59 +0800260 /* hardware only supports new timing mode */
Maxime Ripard9a37e532017-01-27 22:38:36 +0100261 bool needs_new_timings;
Chen-Yu Tsaiff39e7f2017-07-24 21:58:59 +0800262
263 /* hardware can switch between old and new timing modes */
264 bool has_timings_switch;
Hans de Goede86a93312016-07-30 16:25:45 +0200265};
266
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200267struct sunxi_mmc_host {
Maxime Ripard774c0102018-03-08 15:52:51 +0100268 struct device *dev;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200269 struct mmc_host *mmc;
270 struct reset_control *reset;
Hans de Goede86a93312016-07-30 16:25:45 +0200271 const struct sunxi_mmc_cfg *cfg;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200272
273 /* IO mapping base */
274 void __iomem *reg_base;
275
276 /* clock management */
277 struct clk *clk_ahb;
278 struct clk *clk_mmc;
Maxime Ripard6c09bb82014-07-12 12:01:33 +0200279 struct clk *clk_sample;
280 struct clk *clk_output;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200281
282 /* irq */
283 spinlock_t lock;
284 int irq;
285 u32 int_sum;
286 u32 sdio_imask;
287
288 /* dma */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200289 dma_addr_t sg_dma;
290 void *sg_cpu;
291 bool wait_dma;
292
293 struct mmc_request *mrq;
294 struct mmc_request *manual_stop_mrq;
295 int ferror;
Chen-Yu Tsaif771f6e2016-01-21 13:26:31 +0800296
297 /* vqmmc */
298 bool vqmmc_enabled;
Chen-Yu Tsaiff39e7f2017-07-24 21:58:59 +0800299
300 /* timings */
301 bool use_new_timings;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200302};
303
304static int sunxi_mmc_reset_host(struct sunxi_mmc_host *host)
305{
306 unsigned long expire = jiffies + msecs_to_jiffies(250);
307 u32 rval;
308
David Lanzendörfer0f0fcd32014-12-16 15:11:10 +0100309 mmc_writel(host, REG_GCTRL, SDXC_HARDWARE_RESET);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200310 do {
311 rval = mmc_readl(host, REG_GCTRL);
312 } while (time_before(jiffies, expire) && (rval & SDXC_HARDWARE_RESET));
313
314 if (rval & SDXC_HARDWARE_RESET) {
315 dev_err(mmc_dev(host->mmc), "fatal err reset timeout\n");
316 return -EIO;
317 }
318
319 return 0;
320}
321
Maxime Ripard0fc4c612018-04-16 16:23:00 +0200322static int sunxi_mmc_init_host(struct sunxi_mmc_host *host)
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200323{
324 u32 rval;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200325
326 if (sunxi_mmc_reset_host(host))
327 return -EIO;
328
Chen-Yu Tsai0314cbd2016-01-21 13:26:28 +0800329 /*
330 * Burst 8 transfers, RX trigger level: 7, TX trigger level: 8
331 *
332 * TODO: sun9i has a larger FIFO and supports higher trigger values
333 */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200334 mmc_writel(host, REG_FTRGL, 0x20070008);
Chen-Yu Tsai0314cbd2016-01-21 13:26:28 +0800335 /* Maximum timeout value */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200336 mmc_writel(host, REG_TMOUT, 0xffffffff);
Chen-Yu Tsai0314cbd2016-01-21 13:26:28 +0800337 /* Unmask SDIO interrupt if needed */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200338 mmc_writel(host, REG_IMASK, host->sdio_imask);
Chen-Yu Tsai0314cbd2016-01-21 13:26:28 +0800339 /* Clear all pending interrupts */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200340 mmc_writel(host, REG_RINTR, 0xffffffff);
Chen-Yu Tsai0314cbd2016-01-21 13:26:28 +0800341 /* Debug register? undocumented */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200342 mmc_writel(host, REG_DBGC, 0xdeb);
Chen-Yu Tsai0314cbd2016-01-21 13:26:28 +0800343 /* Enable CEATA support */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200344 mmc_writel(host, REG_FUNS, SDXC_CEATA_ON);
Chen-Yu Tsai0314cbd2016-01-21 13:26:28 +0800345 /* Set DMA descriptor list base address */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200346 mmc_writel(host, REG_DLBA, host->sg_dma);
347
348 rval = mmc_readl(host, REG_GCTRL);
349 rval |= SDXC_INTERRUPT_ENABLE_BIT;
Chen-Yu Tsai0314cbd2016-01-21 13:26:28 +0800350 /* Undocumented, but found in Allwinner code */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200351 rval &= ~SDXC_ACCESS_DONE_DIRECT;
352 mmc_writel(host, REG_GCTRL, rval);
353
354 return 0;
355}
356
357static void sunxi_mmc_init_idma_des(struct sunxi_mmc_host *host,
358 struct mmc_data *data)
359{
360 struct sunxi_idma_des *pdes = (struct sunxi_idma_des *)host->sg_cpu;
Arnd Bergmannd34712d2015-02-24 10:47:27 +0100361 dma_addr_t next_desc = host->sg_dma;
Hans de Goede86a93312016-07-30 16:25:45 +0200362 int i, max_len = (1 << host->cfg->idma_des_size_bits);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200363
364 for (i = 0; i < data->sg_len; i++) {
Michael Weiser2dd110b2016-08-22 18:42:18 +0200365 pdes[i].config = cpu_to_le32(SDXC_IDMAC_DES0_CH |
366 SDXC_IDMAC_DES0_OWN |
367 SDXC_IDMAC_DES0_DIC);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200368
369 if (data->sg[i].length == max_len)
370 pdes[i].buf_size = 0; /* 0 == max_len */
371 else
Michael Weiser2dd110b2016-08-22 18:42:18 +0200372 pdes[i].buf_size = cpu_to_le32(data->sg[i].length);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200373
Arnd Bergmannd34712d2015-02-24 10:47:27 +0100374 next_desc += sizeof(struct sunxi_idma_des);
Michael Weiser2dd110b2016-08-22 18:42:18 +0200375 pdes[i].buf_addr_ptr1 =
376 cpu_to_le32(sg_dma_address(&data->sg[i]));
377 pdes[i].buf_addr_ptr2 = cpu_to_le32((u32)next_desc);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200378 }
379
Michael Weiser2dd110b2016-08-22 18:42:18 +0200380 pdes[0].config |= cpu_to_le32(SDXC_IDMAC_DES0_FD);
381 pdes[i - 1].config |= cpu_to_le32(SDXC_IDMAC_DES0_LD |
382 SDXC_IDMAC_DES0_ER);
383 pdes[i - 1].config &= cpu_to_le32(~SDXC_IDMAC_DES0_DIC);
Hans de Goedee8a59042014-12-16 15:10:59 +0100384 pdes[i - 1].buf_addr_ptr2 = 0;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200385
386 /*
387 * Avoid the io-store starting the idmac hitting io-mem before the
388 * descriptors hit the main-mem.
389 */
390 wmb();
391}
392
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200393static int sunxi_mmc_map_dma(struct sunxi_mmc_host *host,
394 struct mmc_data *data)
395{
396 u32 i, dma_len;
397 struct scatterlist *sg;
398
399 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200400 mmc_get_dma_dir(data));
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200401 if (dma_len == 0) {
402 dev_err(mmc_dev(host->mmc), "dma_map_sg failed\n");
403 return -ENOMEM;
404 }
405
406 for_each_sg(data->sg, sg, data->sg_len, i) {
407 if (sg->offset & 3 || sg->length & 3) {
408 dev_err(mmc_dev(host->mmc),
409 "unaligned scatterlist: os %x length %d\n",
410 sg->offset, sg->length);
411 return -EINVAL;
412 }
413 }
414
415 return 0;
416}
417
418static void sunxi_mmc_start_dma(struct sunxi_mmc_host *host,
419 struct mmc_data *data)
420{
421 u32 rval;
422
423 sunxi_mmc_init_idma_des(host, data);
424
425 rval = mmc_readl(host, REG_GCTRL);
426 rval |= SDXC_DMA_ENABLE_BIT;
427 mmc_writel(host, REG_GCTRL, rval);
428 rval |= SDXC_DMA_RESET;
429 mmc_writel(host, REG_GCTRL, rval);
430
431 mmc_writel(host, REG_DMAC, SDXC_IDMAC_SOFT_RESET);
432
433 if (!(data->flags & MMC_DATA_WRITE))
434 mmc_writel(host, REG_IDIE, SDXC_IDMAC_RECEIVE_INTERRUPT);
435
436 mmc_writel(host, REG_DMAC,
437 SDXC_IDMAC_FIX_BURST | SDXC_IDMAC_IDMA_ON);
438}
439
440static void sunxi_mmc_send_manual_stop(struct sunxi_mmc_host *host,
441 struct mmc_request *req)
442{
443 u32 arg, cmd_val, ri;
444 unsigned long expire = jiffies + msecs_to_jiffies(1000);
445
446 cmd_val = SDXC_START | SDXC_RESP_EXPIRE |
447 SDXC_STOP_ABORT_CMD | SDXC_CHECK_RESPONSE_CRC;
448
449 if (req->cmd->opcode == SD_IO_RW_EXTENDED) {
450 cmd_val |= SD_IO_RW_DIRECT;
451 arg = (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
452 ((req->cmd->arg >> 28) & 0x7);
453 } else {
454 cmd_val |= MMC_STOP_TRANSMISSION;
455 arg = 0;
456 }
457
458 mmc_writel(host, REG_CARG, arg);
459 mmc_writel(host, REG_CMDR, cmd_val);
460
461 do {
462 ri = mmc_readl(host, REG_RINTR);
463 } while (!(ri & (SDXC_COMMAND_DONE | SDXC_INTERRUPT_ERROR_BIT)) &&
464 time_before(jiffies, expire));
465
466 if (!(ri & SDXC_COMMAND_DONE) || (ri & SDXC_INTERRUPT_ERROR_BIT)) {
467 dev_err(mmc_dev(host->mmc), "send stop command failed\n");
468 if (req->stop)
469 req->stop->resp[0] = -ETIMEDOUT;
470 } else {
471 if (req->stop)
472 req->stop->resp[0] = mmc_readl(host, REG_RESP0);
473 }
474
475 mmc_writel(host, REG_RINTR, 0xffff);
476}
477
478static void sunxi_mmc_dump_errinfo(struct sunxi_mmc_host *host)
479{
480 struct mmc_command *cmd = host->mrq->cmd;
481 struct mmc_data *data = host->mrq->data;
482
483 /* For some cmds timeout is normal with sd/mmc cards */
484 if ((host->int_sum & SDXC_INTERRUPT_ERROR_BIT) ==
485 SDXC_RESP_TIMEOUT && (cmd->opcode == SD_IO_SEND_OP_COND ||
486 cmd->opcode == SD_IO_RW_DIRECT))
487 return;
488
Icenowy Zhengbd675692017-03-16 21:29:03 +0800489 dev_dbg(mmc_dev(host->mmc),
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200490 "smc %d err, cmd %d,%s%s%s%s%s%s%s%s%s%s !!\n",
491 host->mmc->index, cmd->opcode,
492 data ? (data->flags & MMC_DATA_WRITE ? " WR" : " RD") : "",
493 host->int_sum & SDXC_RESP_ERROR ? " RE" : "",
494 host->int_sum & SDXC_RESP_CRC_ERROR ? " RCE" : "",
495 host->int_sum & SDXC_DATA_CRC_ERROR ? " DCE" : "",
496 host->int_sum & SDXC_RESP_TIMEOUT ? " RTO" : "",
497 host->int_sum & SDXC_DATA_TIMEOUT ? " DTO" : "",
498 host->int_sum & SDXC_FIFO_RUN_ERROR ? " FE" : "",
499 host->int_sum & SDXC_HARD_WARE_LOCKED ? " HL" : "",
500 host->int_sum & SDXC_START_BIT_ERROR ? " SBE" : "",
501 host->int_sum & SDXC_END_BIT_ERROR ? " EBE" : ""
502 );
503}
504
505/* Called in interrupt context! */
506static irqreturn_t sunxi_mmc_finalize_request(struct sunxi_mmc_host *host)
507{
508 struct mmc_request *mrq = host->mrq;
509 struct mmc_data *data = mrq->data;
510 u32 rval;
511
512 mmc_writel(host, REG_IMASK, host->sdio_imask);
513 mmc_writel(host, REG_IDIE, 0);
514
515 if (host->int_sum & SDXC_INTERRUPT_ERROR_BIT) {
516 sunxi_mmc_dump_errinfo(host);
517 mrq->cmd->error = -ETIMEDOUT;
518
519 if (data) {
520 data->error = -ETIMEDOUT;
521 host->manual_stop_mrq = mrq;
522 }
523
524 if (mrq->stop)
525 mrq->stop->error = -ETIMEDOUT;
526 } else {
527 if (mrq->cmd->flags & MMC_RSP_136) {
528 mrq->cmd->resp[0] = mmc_readl(host, REG_RESP3);
529 mrq->cmd->resp[1] = mmc_readl(host, REG_RESP2);
530 mrq->cmd->resp[2] = mmc_readl(host, REG_RESP1);
531 mrq->cmd->resp[3] = mmc_readl(host, REG_RESP0);
532 } else {
533 mrq->cmd->resp[0] = mmc_readl(host, REG_RESP0);
534 }
535
536 if (data)
537 data->bytes_xfered = data->blocks * data->blksz;
538 }
539
540 if (data) {
541 mmc_writel(host, REG_IDST, 0x337);
542 mmc_writel(host, REG_DMAC, 0);
543 rval = mmc_readl(host, REG_GCTRL);
544 rval |= SDXC_DMA_RESET;
545 mmc_writel(host, REG_GCTRL, rval);
546 rval &= ~SDXC_DMA_ENABLE_BIT;
547 mmc_writel(host, REG_GCTRL, rval);
548 rval |= SDXC_FIFO_RESET;
549 mmc_writel(host, REG_GCTRL, rval);
550 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200551 mmc_get_dma_dir(data));
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200552 }
553
554 mmc_writel(host, REG_RINTR, 0xffff);
555
556 host->mrq = NULL;
557 host->int_sum = 0;
558 host->wait_dma = false;
559
560 return host->manual_stop_mrq ? IRQ_WAKE_THREAD : IRQ_HANDLED;
561}
562
563static irqreturn_t sunxi_mmc_irq(int irq, void *dev_id)
564{
565 struct sunxi_mmc_host *host = dev_id;
566 struct mmc_request *mrq;
567 u32 msk_int, idma_int;
568 bool finalize = false;
569 bool sdio_int = false;
570 irqreturn_t ret = IRQ_HANDLED;
571
572 spin_lock(&host->lock);
573
574 idma_int = mmc_readl(host, REG_IDST);
575 msk_int = mmc_readl(host, REG_MISTA);
576
577 dev_dbg(mmc_dev(host->mmc), "irq: rq %p mi %08x idi %08x\n",
578 host->mrq, msk_int, idma_int);
579
580 mrq = host->mrq;
581 if (mrq) {
582 if (idma_int & SDXC_IDMAC_RECEIVE_INTERRUPT)
583 host->wait_dma = false;
584
585 host->int_sum |= msk_int;
586
587 /* Wait for COMMAND_DONE on RESPONSE_TIMEOUT before finalize */
588 if ((host->int_sum & SDXC_RESP_TIMEOUT) &&
589 !(host->int_sum & SDXC_COMMAND_DONE))
590 mmc_writel(host, REG_IMASK,
591 host->sdio_imask | SDXC_COMMAND_DONE);
592 /* Don't wait for dma on error */
593 else if (host->int_sum & SDXC_INTERRUPT_ERROR_BIT)
594 finalize = true;
595 else if ((host->int_sum & SDXC_INTERRUPT_DONE_BIT) &&
596 !host->wait_dma)
597 finalize = true;
598 }
599
600 if (msk_int & SDXC_SDIO_INTERRUPT)
601 sdio_int = true;
602
603 mmc_writel(host, REG_RINTR, msk_int);
604 mmc_writel(host, REG_IDST, idma_int);
605
606 if (finalize)
607 ret = sunxi_mmc_finalize_request(host);
608
609 spin_unlock(&host->lock);
610
611 if (finalize && ret == IRQ_HANDLED)
612 mmc_request_done(host->mmc, mrq);
613
614 if (sdio_int)
615 mmc_signal_sdio_irq(host->mmc);
616
617 return ret;
618}
619
620static irqreturn_t sunxi_mmc_handle_manual_stop(int irq, void *dev_id)
621{
622 struct sunxi_mmc_host *host = dev_id;
623 struct mmc_request *mrq;
624 unsigned long iflags;
625
626 spin_lock_irqsave(&host->lock, iflags);
627 mrq = host->manual_stop_mrq;
628 spin_unlock_irqrestore(&host->lock, iflags);
629
630 if (!mrq) {
631 dev_err(mmc_dev(host->mmc), "no request for manual stop\n");
632 return IRQ_HANDLED;
633 }
634
635 dev_err(mmc_dev(host->mmc), "data error, sending stop command\n");
David Lanzendörferdd9b3802014-12-16 15:11:04 +0100636
637 /*
638 * We will never have more than one outstanding request,
639 * and we do not complete the request until after
640 * we've cleared host->manual_stop_mrq so we do not need to
641 * spin lock this function.
642 * Additionally we have wait states within this function
643 * so having it in a lock is a very bad idea.
644 */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200645 sunxi_mmc_send_manual_stop(host, mrq);
646
647 spin_lock_irqsave(&host->lock, iflags);
648 host->manual_stop_mrq = NULL;
649 spin_unlock_irqrestore(&host->lock, iflags);
650
651 mmc_request_done(host->mmc, mrq);
652
653 return IRQ_HANDLED;
654}
655
656static int sunxi_mmc_oclk_onoff(struct sunxi_mmc_host *host, u32 oclk_en)
657{
Michal Suchanek7bb9c242015-08-12 15:29:31 +0200658 unsigned long expire = jiffies + msecs_to_jiffies(750);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200659 u32 rval;
660
Maxime Ripard43c15e92017-01-27 22:38:39 +0100661 dev_dbg(mmc_dev(host->mmc), "%sabling the clock\n",
662 oclk_en ? "en" : "dis");
663
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200664 rval = mmc_readl(host, REG_CLKCR);
Maxime Ripard16e821e2017-01-27 22:38:37 +0100665 rval &= ~(SDXC_CARD_CLOCK_ON | SDXC_LOW_POWER_ON | SDXC_MASK_DATA0);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200666
667 if (oclk_en)
668 rval |= SDXC_CARD_CLOCK_ON;
Maxime Ripard16e821e2017-01-27 22:38:37 +0100669 if (host->cfg->mask_data0)
670 rval |= SDXC_MASK_DATA0;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200671
672 mmc_writel(host, REG_CLKCR, rval);
673
674 rval = SDXC_START | SDXC_UPCLK_ONLY | SDXC_WAIT_PRE_OVER;
675 mmc_writel(host, REG_CMDR, rval);
676
677 do {
678 rval = mmc_readl(host, REG_CMDR);
679 } while (time_before(jiffies, expire) && (rval & SDXC_START));
680
681 /* clear irq status bits set by the command */
682 mmc_writel(host, REG_RINTR,
683 mmc_readl(host, REG_RINTR) & ~SDXC_SDIO_INTERRUPT);
684
685 if (rval & SDXC_START) {
686 dev_err(mmc_dev(host->mmc), "fatal err update clk timeout\n");
687 return -EIO;
688 }
689
Maxime Ripard16e821e2017-01-27 22:38:37 +0100690 if (host->cfg->mask_data0) {
691 rval = mmc_readl(host, REG_CLKCR);
692 mmc_writel(host, REG_CLKCR, rval & ~SDXC_MASK_DATA0);
693 }
694
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200695 return 0;
696}
697
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +0200698static int sunxi_mmc_calibrate(struct sunxi_mmc_host *host, int reg_off)
699{
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +0200700 if (!host->cfg->can_calibrate)
701 return 0;
702
Maxime Ripard860fdf82017-01-27 22:38:35 +0100703 /*
704 * FIXME:
705 * This is not clear how the calibration is supposed to work
706 * yet. The best rate have been obtained by simply setting the
707 * delay to 0, as Allwinner does in its BSP.
708 *
709 * The only mode that doesn't have such a delay is HS400, that
710 * is in itself a TODO.
711 */
712 writel(SDXC_CAL_DL_SW_EN, host->reg_base + reg_off);
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +0200713
714 return 0;
715}
716
Hans de Goedef2cecb72016-07-30 16:25:46 +0200717static int sunxi_mmc_clk_set_phase(struct sunxi_mmc_host *host,
718 struct mmc_ios *ios, u32 rate)
719{
720 int index;
721
Chen-Yu Tsaia6461132017-08-08 15:02:44 +0800722 /* clk controller delays not used under new timings mode */
Chen-Yu Tsaiff39e7f2017-07-24 21:58:59 +0800723 if (host->use_new_timings)
Hans de Goedeb4656462016-07-30 16:25:47 +0200724 return 0;
725
Chen-Yu Tsaia6461132017-08-08 15:02:44 +0800726 /* some old controllers don't support delays */
727 if (!host->cfg->clk_delays)
728 return 0;
729
Hans de Goedef2cecb72016-07-30 16:25:46 +0200730 /* determine delays */
731 if (rate <= 400000) {
732 index = SDXC_CLK_400K;
733 } else if (rate <= 25000000) {
734 index = SDXC_CLK_25M;
735 } else if (rate <= 52000000) {
736 if (ios->timing != MMC_TIMING_UHS_DDR50 &&
737 ios->timing != MMC_TIMING_MMC_DDR52) {
738 index = SDXC_CLK_50M;
739 } else if (ios->bus_width == MMC_BUS_WIDTH_8) {
740 index = SDXC_CLK_50M_DDR_8BIT;
741 } else {
742 index = SDXC_CLK_50M_DDR;
743 }
744 } else {
Maxime Ripard43c15e92017-01-27 22:38:39 +0100745 dev_dbg(mmc_dev(host->mmc), "Invalid clock... returning\n");
Hans de Goedef2cecb72016-07-30 16:25:46 +0200746 return -EINVAL;
747 }
748
749 clk_set_phase(host->clk_sample, host->cfg->clk_delays[index].sample);
750 clk_set_phase(host->clk_output, host->cfg->clk_delays[index].output);
751
752 return 0;
753}
754
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200755static int sunxi_mmc_clk_set_rate(struct sunxi_mmc_host *host,
756 struct mmc_ios *ios)
757{
Maxime Ripard43c15e92017-01-27 22:38:39 +0100758 struct mmc_host *mmc = host->mmc;
Jean-Francois Moine63311be2016-08-23 10:51:04 +0200759 long rate;
Chen-Yu Tsaic903a2a2017-07-24 21:59:00 +0800760 u32 rval, clock = ios->clock, div = 1;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200761 int ret;
762
Maxime Ripard39cc2812017-01-27 22:38:33 +0100763 ret = sunxi_mmc_oclk_onoff(host, 0);
764 if (ret)
765 return ret;
766
Maxime Ripard43c15e92017-01-27 22:38:39 +0100767 /* Our clock is gated now */
768 mmc->actual_clock = 0;
769
Maxime Ripard94790742017-01-27 22:38:34 +0100770 if (!ios->clock)
771 return 0;
772
Chen-Yu Tsaic903a2a2017-07-24 21:59:00 +0800773 /*
774 * Under the old timing mode, 8 bit DDR requires the module
775 * clock to be double the card clock. Under the new timing
776 * mode, all DDR modes require a doubled module clock.
777 *
778 * We currently only support the standard MMC DDR52 mode.
779 * This block should be updated once support for other DDR
780 * modes is added.
781 */
Chen-Yu Tsai2a7aa632016-01-30 01:21:47 +0800782 if (ios->timing == MMC_TIMING_MMC_DDR52 &&
Chen-Yu Tsaic903a2a2017-07-24 21:59:00 +0800783 (host->use_new_timings ||
784 ios->bus_width == MMC_BUS_WIDTH_8)) {
785 div = 2;
Chen-Yu Tsai2a7aa632016-01-30 01:21:47 +0800786 clock <<= 1;
Chen-Yu Tsaic903a2a2017-07-24 21:59:00 +0800787 }
Chen-Yu Tsai2a7aa632016-01-30 01:21:47 +0800788
Icenowy Zhengb939e0b72017-08-08 15:09:03 +0800789 if (host->use_new_timings && host->cfg->has_timings_switch) {
Chen-Yu Tsaiff39e7f2017-07-24 21:58:59 +0800790 ret = sunxi_ccu_set_mmc_timing_mode(host->clk_mmc, true);
791 if (ret) {
792 dev_err(mmc_dev(mmc),
793 "error setting new timing mode\n");
794 return ret;
795 }
796 }
797
Chen-Yu Tsai2a7aa632016-01-30 01:21:47 +0800798 rate = clk_round_rate(host->clk_mmc, clock);
Jean-Francois Moine63311be2016-08-23 10:51:04 +0200799 if (rate < 0) {
Maxime Ripard43c15e92017-01-27 22:38:39 +0100800 dev_err(mmc_dev(mmc), "error rounding clk to %d: %ld\n",
Jean-Francois Moine63311be2016-08-23 10:51:04 +0200801 clock, rate);
802 return rate;
803 }
Maxime Ripard43c15e92017-01-27 22:38:39 +0100804 dev_dbg(mmc_dev(mmc), "setting clk to %d, rounded %ld\n",
Chen-Yu Tsai2a7aa632016-01-30 01:21:47 +0800805 clock, rate);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200806
807 /* setting clock rate */
808 ret = clk_set_rate(host->clk_mmc, rate);
809 if (ret) {
Maxime Ripard43c15e92017-01-27 22:38:39 +0100810 dev_err(mmc_dev(mmc), "error setting clk to %ld: %d\n",
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200811 rate, ret);
812 return ret;
813 }
814
Chen-Yu Tsaic903a2a2017-07-24 21:59:00 +0800815 /* set internal divider */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200816 rval = mmc_readl(host, REG_CLKCR);
817 rval &= ~0xff;
Chen-Yu Tsaic903a2a2017-07-24 21:59:00 +0800818 rval |= div - 1;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200819 mmc_writel(host, REG_CLKCR, rval);
820
Chen-Yu Tsai082bb852017-08-10 11:29:54 +0800821 /* update card clock rate to account for internal divider */
822 rate /= div;
823
Chen-Yu Tsaiff39e7f2017-07-24 21:58:59 +0800824 if (host->use_new_timings) {
Chen-Yu Tsai26cb2be2017-07-14 14:42:55 +0800825 /* Don't touch the delay bits */
826 rval = mmc_readl(host, REG_SD_NTSR);
827 rval |= SDXC_2X_TIMING_MODE;
828 mmc_writel(host, REG_SD_NTSR, rval);
829 }
Maxime Ripard9a37e532017-01-27 22:38:36 +0100830
Chen-Yu Tsai082bb852017-08-10 11:29:54 +0800831 /* sunxi_mmc_clk_set_phase expects the actual card clock rate */
Hans de Goedef2cecb72016-07-30 16:25:46 +0200832 ret = sunxi_mmc_clk_set_phase(host, ios, rate);
833 if (ret)
834 return ret;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200835
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +0200836 ret = sunxi_mmc_calibrate(host, SDXC_REG_SAMP_DL_REG);
837 if (ret)
838 return ret;
839
Maxime Ripard860fdf82017-01-27 22:38:35 +0100840 /*
841 * FIXME:
842 *
843 * In HS400 we'll also need to calibrate the data strobe
844 * signal. This should only happen on the MMC2 controller (at
845 * least on the A64).
846 */
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +0200847
Maxime Ripard43c15e92017-01-27 22:38:39 +0100848 ret = sunxi_mmc_oclk_onoff(host, 1);
849 if (ret)
850 return ret;
851
852 /* And we just enabled our clock back */
Chen-Yu Tsai082bb852017-08-10 11:29:54 +0800853 mmc->actual_clock = rate;
Maxime Ripard43c15e92017-01-27 22:38:39 +0100854
855 return 0;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200856}
857
858static void sunxi_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
859{
860 struct sunxi_mmc_host *host = mmc_priv(mmc);
861 u32 rval;
862
863 /* Set the power state */
864 switch (ios->power_mode) {
865 case MMC_POWER_ON:
866 break;
867
868 case MMC_POWER_UP:
Maxime Ripard424feb52016-10-19 15:33:04 +0200869 if (!IS_ERR(mmc->supply.vmmc)) {
870 host->ferror = mmc_regulator_set_ocr(mmc,
871 mmc->supply.vmmc,
872 ios->vdd);
873 if (host->ferror)
874 return;
875 }
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200876
Chen-Yu Tsaif771f6e2016-01-21 13:26:31 +0800877 if (!IS_ERR(mmc->supply.vqmmc)) {
878 host->ferror = regulator_enable(mmc->supply.vqmmc);
879 if (host->ferror) {
880 dev_err(mmc_dev(mmc),
881 "failed to enable vqmmc\n");
882 return;
883 }
884 host->vqmmc_enabled = true;
885 }
886
Maxime Ripard0fc4c612018-04-16 16:23:00 +0200887 host->ferror = sunxi_mmc_init_host(host);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200888 if (host->ferror)
889 return;
890
891 dev_dbg(mmc_dev(mmc), "power on!\n");
892 break;
893
894 case MMC_POWER_OFF:
895 dev_dbg(mmc_dev(mmc), "power off!\n");
896 sunxi_mmc_reset_host(host);
Maxime Ripard424feb52016-10-19 15:33:04 +0200897 if (!IS_ERR(mmc->supply.vmmc))
898 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
899
Chen-Yu Tsaif771f6e2016-01-21 13:26:31 +0800900 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled)
901 regulator_disable(mmc->supply.vqmmc);
902 host->vqmmc_enabled = false;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200903 break;
904 }
905
906 /* set bus width */
907 switch (ios->bus_width) {
908 case MMC_BUS_WIDTH_1:
909 mmc_writel(host, REG_WIDTH, SDXC_WIDTH1);
910 break;
911 case MMC_BUS_WIDTH_4:
912 mmc_writel(host, REG_WIDTH, SDXC_WIDTH4);
913 break;
914 case MMC_BUS_WIDTH_8:
915 mmc_writel(host, REG_WIDTH, SDXC_WIDTH8);
916 break;
917 }
918
919 /* set ddr mode */
920 rval = mmc_readl(host, REG_GCTRL);
Chen-Yu Tsai2dcb3052016-01-30 01:21:46 +0800921 if (ios->timing == MMC_TIMING_UHS_DDR50 ||
922 ios->timing == MMC_TIMING_MMC_DDR52)
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200923 rval |= SDXC_DDR_MODE;
924 else
925 rval &= ~SDXC_DDR_MODE;
926 mmc_writel(host, REG_GCTRL, rval);
927
928 /* set up clock */
Maxime Ripard94790742017-01-27 22:38:34 +0100929 if (ios->power_mode) {
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200930 host->ferror = sunxi_mmc_clk_set_rate(host, ios);
931 /* Android code had a usleep_range(50000, 55000); here */
932 }
933}
934
Chen-Yu Tsaif771f6e2016-01-21 13:26:31 +0800935static int sunxi_mmc_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
936{
937 /* vqmmc regulator is available */
938 if (!IS_ERR(mmc->supply.vqmmc))
939 return mmc_regulator_set_vqmmc(mmc, ios);
940
941 /* no vqmmc regulator, assume fixed regulator at 3/3.3V */
942 if (mmc->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330)
943 return 0;
944
945 return -EINVAL;
946}
947
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200948static void sunxi_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
949{
950 struct sunxi_mmc_host *host = mmc_priv(mmc);
951 unsigned long flags;
952 u32 imask;
953
954 spin_lock_irqsave(&host->lock, flags);
955
956 imask = mmc_readl(host, REG_IMASK);
957 if (enable) {
958 host->sdio_imask = SDXC_SDIO_INTERRUPT;
959 imask |= SDXC_SDIO_INTERRUPT;
960 } else {
961 host->sdio_imask = 0;
962 imask &= ~SDXC_SDIO_INTERRUPT;
963 }
964 mmc_writel(host, REG_IMASK, imask);
965 spin_unlock_irqrestore(&host->lock, flags);
966}
967
968static void sunxi_mmc_hw_reset(struct mmc_host *mmc)
969{
970 struct sunxi_mmc_host *host = mmc_priv(mmc);
971 mmc_writel(host, REG_HWRST, 0);
972 udelay(10);
973 mmc_writel(host, REG_HWRST, 1);
974 udelay(300);
975}
976
977static void sunxi_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
978{
979 struct sunxi_mmc_host *host = mmc_priv(mmc);
980 struct mmc_command *cmd = mrq->cmd;
981 struct mmc_data *data = mrq->data;
982 unsigned long iflags;
983 u32 imask = SDXC_INTERRUPT_ERROR_BIT;
984 u32 cmd_val = SDXC_START | (cmd->opcode & 0x3f);
David Lanzendörferdd9b3802014-12-16 15:11:04 +0100985 bool wait_dma = host->wait_dma;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +0200986 int ret;
987
988 /* Check for set_ios errors (should never happen) */
989 if (host->ferror) {
990 mrq->cmd->error = host->ferror;
991 mmc_request_done(mmc, mrq);
992 return;
993 }
994
995 if (data) {
996 ret = sunxi_mmc_map_dma(host, data);
997 if (ret < 0) {
998 dev_err(mmc_dev(mmc), "map DMA failed\n");
999 cmd->error = ret;
1000 data->error = ret;
1001 mmc_request_done(mmc, mrq);
1002 return;
1003 }
1004 }
1005
1006 if (cmd->opcode == MMC_GO_IDLE_STATE) {
1007 cmd_val |= SDXC_SEND_INIT_SEQUENCE;
1008 imask |= SDXC_COMMAND_DONE;
1009 }
1010
1011 if (cmd->flags & MMC_RSP_PRESENT) {
1012 cmd_val |= SDXC_RESP_EXPIRE;
1013 if (cmd->flags & MMC_RSP_136)
1014 cmd_val |= SDXC_LONG_RESPONSE;
1015 if (cmd->flags & MMC_RSP_CRC)
1016 cmd_val |= SDXC_CHECK_RESPONSE_CRC;
1017
1018 if ((cmd->flags & MMC_CMD_MASK) == MMC_CMD_ADTC) {
1019 cmd_val |= SDXC_DATA_EXPIRE | SDXC_WAIT_PRE_OVER;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001020
1021 if (cmd->data->stop) {
1022 imask |= SDXC_AUTO_COMMAND_DONE;
1023 cmd_val |= SDXC_SEND_AUTO_STOP;
1024 } else {
1025 imask |= SDXC_DATA_OVER;
1026 }
1027
1028 if (cmd->data->flags & MMC_DATA_WRITE)
1029 cmd_val |= SDXC_WRITE;
1030 else
David Lanzendörferdd9b3802014-12-16 15:11:04 +01001031 wait_dma = true;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001032 } else {
1033 imask |= SDXC_COMMAND_DONE;
1034 }
1035 } else {
1036 imask |= SDXC_COMMAND_DONE;
1037 }
1038
1039 dev_dbg(mmc_dev(mmc), "cmd %d(%08x) arg %x ie 0x%08x len %d\n",
1040 cmd_val & 0x3f, cmd_val, cmd->arg, imask,
1041 mrq->data ? mrq->data->blksz * mrq->data->blocks : 0);
1042
1043 spin_lock_irqsave(&host->lock, iflags);
1044
1045 if (host->mrq || host->manual_stop_mrq) {
1046 spin_unlock_irqrestore(&host->lock, iflags);
1047
1048 if (data)
1049 dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +02001050 mmc_get_dma_dir(data));
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001051
1052 dev_err(mmc_dev(mmc), "request already pending\n");
1053 mrq->cmd->error = -EBUSY;
1054 mmc_request_done(mmc, mrq);
1055 return;
1056 }
1057
1058 if (data) {
1059 mmc_writel(host, REG_BLKSZ, data->blksz);
1060 mmc_writel(host, REG_BCNTR, data->blksz * data->blocks);
1061 sunxi_mmc_start_dma(host, data);
1062 }
1063
1064 host->mrq = mrq;
David Lanzendörferdd9b3802014-12-16 15:11:04 +01001065 host->wait_dma = wait_dma;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001066 mmc_writel(host, REG_IMASK, host->sdio_imask | imask);
1067 mmc_writel(host, REG_CARG, cmd->arg);
1068 mmc_writel(host, REG_CMDR, cmd_val);
1069
1070 spin_unlock_irqrestore(&host->lock, iflags);
1071}
1072
Hans de Goedec1590dd2015-09-22 17:30:26 +02001073static int sunxi_mmc_card_busy(struct mmc_host *mmc)
1074{
1075 struct sunxi_mmc_host *host = mmc_priv(mmc);
1076
1077 return !!(mmc_readl(host, REG_STAS) & SDXC_CARD_DATA_BUSY);
1078}
1079
Julia Lawall1f8029c2017-07-29 07:59:38 +02001080static const struct mmc_host_ops sunxi_mmc_ops = {
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001081 .request = sunxi_mmc_request,
1082 .set_ios = sunxi_mmc_set_ios,
1083 .get_ro = mmc_gpio_get_ro,
1084 .get_cd = mmc_gpio_get_cd,
1085 .enable_sdio_irq = sunxi_mmc_enable_sdio_irq,
Chen-Yu Tsaif771f6e2016-01-21 13:26:31 +08001086 .start_signal_voltage_switch = sunxi_mmc_volt_switch,
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001087 .hw_reset = sunxi_mmc_hw_reset,
Hans de Goedec1590dd2015-09-22 17:30:26 +02001088 .card_busy = sunxi_mmc_card_busy,
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001089};
1090
Hans de Goede51424b22015-09-23 22:06:48 +02001091static const struct sunxi_mmc_clk_delay sunxi_mmc_clk_delays[] = {
1092 [SDXC_CLK_400K] = { .output = 180, .sample = 180 },
1093 [SDXC_CLK_25M] = { .output = 180, .sample = 75 },
1094 [SDXC_CLK_50M] = { .output = 90, .sample = 120 },
1095 [SDXC_CLK_50M_DDR] = { .output = 60, .sample = 120 },
Chen-Yu Tsai2a7aa632016-01-30 01:21:47 +08001096 /* Value from A83T "new timing mode". Works but might not be right. */
1097 [SDXC_CLK_50M_DDR_8BIT] = { .output = 90, .sample = 180 },
Hans de Goede51424b22015-09-23 22:06:48 +02001098};
1099
1100static const struct sunxi_mmc_clk_delay sun9i_mmc_clk_delays[] = {
1101 [SDXC_CLK_400K] = { .output = 180, .sample = 180 },
1102 [SDXC_CLK_25M] = { .output = 180, .sample = 75 },
1103 [SDXC_CLK_50M] = { .output = 150, .sample = 120 },
Chen-Yu Tsai01752492016-05-29 15:04:43 +08001104 [SDXC_CLK_50M_DDR] = { .output = 54, .sample = 36 },
1105 [SDXC_CLK_50M_DDR_8BIT] = { .output = 72, .sample = 72 },
Hans de Goede51424b22015-09-23 22:06:48 +02001106};
1107
Hans de Goede86a93312016-07-30 16:25:45 +02001108static const struct sunxi_mmc_cfg sun4i_a10_cfg = {
1109 .idma_des_size_bits = 13,
Hans de Goedeb4656462016-07-30 16:25:47 +02001110 .clk_delays = NULL,
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +02001111 .can_calibrate = false,
Hans de Goede86a93312016-07-30 16:25:45 +02001112};
1113
1114static const struct sunxi_mmc_cfg sun5i_a13_cfg = {
1115 .idma_des_size_bits = 16,
Hans de Goedeb4656462016-07-30 16:25:47 +02001116 .clk_delays = NULL,
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +02001117 .can_calibrate = false,
Hans de Goedeb4656462016-07-30 16:25:47 +02001118};
1119
1120static const struct sunxi_mmc_cfg sun7i_a20_cfg = {
1121 .idma_des_size_bits = 16,
Hans de Goede86a93312016-07-30 16:25:45 +02001122 .clk_delays = sunxi_mmc_clk_delays,
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +02001123 .can_calibrate = false,
Hans de Goede86a93312016-07-30 16:25:45 +02001124};
1125
Chen-Yu Tsaiac98cae2017-07-24 21:59:01 +08001126static const struct sunxi_mmc_cfg sun8i_a83t_emmc_cfg = {
1127 .idma_des_size_bits = 16,
1128 .clk_delays = sunxi_mmc_clk_delays,
1129 .can_calibrate = false,
1130 .has_timings_switch = true,
1131};
1132
Hans de Goede86a93312016-07-30 16:25:45 +02001133static const struct sunxi_mmc_cfg sun9i_a80_cfg = {
1134 .idma_des_size_bits = 16,
1135 .clk_delays = sun9i_mmc_clk_delays,
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +02001136 .can_calibrate = false,
1137};
1138
1139static const struct sunxi_mmc_cfg sun50i_a64_cfg = {
1140 .idma_des_size_bits = 16,
1141 .clk_delays = NULL,
1142 .can_calibrate = true,
Maxime Ripard16e821e2017-01-27 22:38:37 +01001143 .mask_data0 = true,
Maxime Ripard9a37e532017-01-27 22:38:36 +01001144 .needs_new_timings = true,
Hans de Goede86a93312016-07-30 16:25:45 +02001145};
1146
Maxime Ripard4fb3ce02017-01-27 22:38:38 +01001147static const struct sunxi_mmc_cfg sun50i_a64_emmc_cfg = {
1148 .idma_des_size_bits = 13,
1149 .clk_delays = NULL,
1150 .can_calibrate = true,
1151};
1152
Hans de Goede86a93312016-07-30 16:25:45 +02001153static const struct of_device_id sunxi_mmc_of_match[] = {
1154 { .compatible = "allwinner,sun4i-a10-mmc", .data = &sun4i_a10_cfg },
1155 { .compatible = "allwinner,sun5i-a13-mmc", .data = &sun5i_a13_cfg },
Hans de Goedeb4656462016-07-30 16:25:47 +02001156 { .compatible = "allwinner,sun7i-a20-mmc", .data = &sun7i_a20_cfg },
Chen-Yu Tsaiac98cae2017-07-24 21:59:01 +08001157 { .compatible = "allwinner,sun8i-a83t-emmc", .data = &sun8i_a83t_emmc_cfg },
Hans de Goede86a93312016-07-30 16:25:45 +02001158 { .compatible = "allwinner,sun9i-a80-mmc", .data = &sun9i_a80_cfg },
Icenowy Zhenge1b8dfd2016-08-05 04:57:15 +02001159 { .compatible = "allwinner,sun50i-a64-mmc", .data = &sun50i_a64_cfg },
Maxime Ripard4fb3ce02017-01-27 22:38:38 +01001160 { .compatible = "allwinner,sun50i-a64-emmc", .data = &sun50i_a64_emmc_cfg },
Hans de Goede86a93312016-07-30 16:25:45 +02001161 { /* sentinel */ }
1162};
1163MODULE_DEVICE_TABLE(of, sunxi_mmc_of_match);
1164
Maxime Ripard774c0102018-03-08 15:52:51 +01001165static int sunxi_mmc_enable(struct sunxi_mmc_host *host)
1166{
1167 int ret;
1168
Maxime Ripardd8181942018-03-08 15:52:52 +01001169 if (!IS_ERR(host->reset)) {
1170 ret = reset_control_reset(host->reset);
1171 if (ret) {
1172 dev_err(host->dev, "Couldn't reset the MMC controller (%d)\n",
1173 ret);
1174 return ret;
1175 }
1176 }
1177
Maxime Ripard774c0102018-03-08 15:52:51 +01001178 ret = clk_prepare_enable(host->clk_ahb);
1179 if (ret) {
Maxime Ripardd8181942018-03-08 15:52:52 +01001180 dev_err(host->dev, "Couldn't enable the bus clocks (%d)\n", ret);
1181 goto error_assert_reset;
Maxime Ripard774c0102018-03-08 15:52:51 +01001182 }
1183
1184 ret = clk_prepare_enable(host->clk_mmc);
1185 if (ret) {
1186 dev_err(host->dev, "Enable mmc clk err %d\n", ret);
1187 goto error_disable_clk_ahb;
1188 }
1189
1190 ret = clk_prepare_enable(host->clk_output);
1191 if (ret) {
1192 dev_err(host->dev, "Enable output clk err %d\n", ret);
1193 goto error_disable_clk_mmc;
1194 }
1195
1196 ret = clk_prepare_enable(host->clk_sample);
1197 if (ret) {
1198 dev_err(host->dev, "Enable sample clk err %d\n", ret);
1199 goto error_disable_clk_output;
1200 }
1201
Maxime Ripard774c0102018-03-08 15:52:51 +01001202 /*
1203 * Sometimes the controller asserts the irq on boot for some reason,
1204 * make sure the controller is in a sane state before enabling irqs.
1205 */
1206 ret = sunxi_mmc_reset_host(host);
1207 if (ret)
Maxime Ripardd8181942018-03-08 15:52:52 +01001208 goto error_disable_clk_sample;
Maxime Ripard774c0102018-03-08 15:52:51 +01001209
1210 return 0;
1211
Maxime Ripard774c0102018-03-08 15:52:51 +01001212error_disable_clk_sample:
1213 clk_disable_unprepare(host->clk_sample);
1214error_disable_clk_output:
1215 clk_disable_unprepare(host->clk_output);
1216error_disable_clk_mmc:
1217 clk_disable_unprepare(host->clk_mmc);
1218error_disable_clk_ahb:
1219 clk_disable_unprepare(host->clk_ahb);
Maxime Ripardd8181942018-03-08 15:52:52 +01001220error_assert_reset:
1221 if (!IS_ERR(host->reset))
1222 reset_control_assert(host->reset);
Maxime Ripard774c0102018-03-08 15:52:51 +01001223 return ret;
1224}
1225
1226static void sunxi_mmc_disable(struct sunxi_mmc_host *host)
1227{
1228 sunxi_mmc_reset_host(host);
1229
Maxime Ripard774c0102018-03-08 15:52:51 +01001230 clk_disable_unprepare(host->clk_sample);
1231 clk_disable_unprepare(host->clk_output);
1232 clk_disable_unprepare(host->clk_mmc);
1233 clk_disable_unprepare(host->clk_ahb);
Maxime Ripardd8181942018-03-08 15:52:52 +01001234
1235 if (!IS_ERR(host->reset))
1236 reset_control_assert(host->reset);
Maxime Ripard774c0102018-03-08 15:52:51 +01001237}
1238
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001239static int sunxi_mmc_resource_request(struct sunxi_mmc_host *host,
1240 struct platform_device *pdev)
1241{
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001242 int ret;
1243
Hans de Goede86a93312016-07-30 16:25:45 +02001244 host->cfg = of_device_get_match_data(&pdev->dev);
1245 if (!host->cfg)
1246 return -EINVAL;
Hans de Goede51424b22015-09-23 22:06:48 +02001247
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001248 ret = mmc_regulator_get_supply(host->mmc);
Wolfram Sangaaab3c42017-10-08 16:50:08 +02001249 if (ret)
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001250 return ret;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001251
1252 host->reg_base = devm_ioremap_resource(&pdev->dev,
1253 platform_get_resource(pdev, IORESOURCE_MEM, 0));
1254 if (IS_ERR(host->reg_base))
1255 return PTR_ERR(host->reg_base);
1256
1257 host->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
1258 if (IS_ERR(host->clk_ahb)) {
1259 dev_err(&pdev->dev, "Could not get ahb clock\n");
1260 return PTR_ERR(host->clk_ahb);
1261 }
1262
1263 host->clk_mmc = devm_clk_get(&pdev->dev, "mmc");
1264 if (IS_ERR(host->clk_mmc)) {
1265 dev_err(&pdev->dev, "Could not get mmc clock\n");
1266 return PTR_ERR(host->clk_mmc);
1267 }
1268
Hans de Goedeb4656462016-07-30 16:25:47 +02001269 if (host->cfg->clk_delays) {
1270 host->clk_output = devm_clk_get(&pdev->dev, "output");
1271 if (IS_ERR(host->clk_output)) {
1272 dev_err(&pdev->dev, "Could not get output clock\n");
1273 return PTR_ERR(host->clk_output);
1274 }
Maxime Ripard6c09bb82014-07-12 12:01:33 +02001275
Hans de Goedeb4656462016-07-30 16:25:47 +02001276 host->clk_sample = devm_clk_get(&pdev->dev, "sample");
1277 if (IS_ERR(host->clk_sample)) {
1278 dev_err(&pdev->dev, "Could not get sample clock\n");
1279 return PTR_ERR(host->clk_sample);
1280 }
Maxime Ripard6c09bb82014-07-12 12:01:33 +02001281 }
1282
Philipp Zabel5e40dda2017-07-19 17:25:44 +02001283 host->reset = devm_reset_control_get_optional_exclusive(&pdev->dev,
1284 "ahb");
Chen-Yu Tsai9e71c5892015-03-03 09:44:40 +08001285 if (PTR_ERR(host->reset) == -EPROBE_DEFER)
1286 return PTR_ERR(host->reset);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001287
Maxime Ripard774c0102018-03-08 15:52:51 +01001288 ret = sunxi_mmc_enable(host);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001289 if (ret)
Maxime Ripard774c0102018-03-08 15:52:51 +01001290 return ret;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001291
1292 host->irq = platform_get_irq(pdev, 0);
Arvind Yadav2408a082017-11-19 10:22:47 +05301293 if (host->irq <= 0) {
1294 ret = -EINVAL;
Maxime Ripard774c0102018-03-08 15:52:51 +01001295 goto error_disable_mmc;
Arvind Yadav2408a082017-11-19 10:22:47 +05301296 }
1297
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001298 return devm_request_threaded_irq(&pdev->dev, host->irq, sunxi_mmc_irq,
1299 sunxi_mmc_handle_manual_stop, 0, "sunxi-mmc", host);
1300
Maxime Ripard774c0102018-03-08 15:52:51 +01001301error_disable_mmc:
1302 sunxi_mmc_disable(host);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001303 return ret;
1304}
1305
1306static int sunxi_mmc_probe(struct platform_device *pdev)
1307{
1308 struct sunxi_mmc_host *host;
1309 struct mmc_host *mmc;
1310 int ret;
1311
1312 mmc = mmc_alloc_host(sizeof(struct sunxi_mmc_host), &pdev->dev);
1313 if (!mmc) {
1314 dev_err(&pdev->dev, "mmc alloc host failed\n");
1315 return -ENOMEM;
1316 }
Maxime Ripardcb1214d2018-03-08 15:52:53 +01001317 platform_set_drvdata(pdev, mmc);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001318
1319 host = mmc_priv(mmc);
Maxime Ripard774c0102018-03-08 15:52:51 +01001320 host->dev = &pdev->dev;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001321 host->mmc = mmc;
1322 spin_lock_init(&host->lock);
1323
1324 ret = sunxi_mmc_resource_request(host, pdev);
1325 if (ret)
1326 goto error_free_host;
1327
1328 host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
1329 &host->sg_dma, GFP_KERNEL);
1330 if (!host->sg_cpu) {
1331 dev_err(&pdev->dev, "Failed to allocate DMA descriptor mem\n");
1332 ret = -ENOMEM;
1333 goto error_free_host;
1334 }
1335
Chen-Yu Tsaiff39e7f2017-07-24 21:58:59 +08001336 if (host->cfg->has_timings_switch) {
1337 /*
1338 * Supports both old and new timing modes.
1339 * Try setting the clk to new timing mode.
1340 */
1341 sunxi_ccu_set_mmc_timing_mode(host->clk_mmc, true);
1342
1343 /* And check the result */
1344 ret = sunxi_ccu_get_mmc_timing_mode(host->clk_mmc);
1345 if (ret < 0) {
1346 /*
1347 * For whatever reason we were not able to get
1348 * the current active mode. Default to old mode.
1349 */
1350 dev_warn(&pdev->dev, "MMC clk timing mode unknown\n");
1351 host->use_new_timings = false;
1352 } else {
1353 host->use_new_timings = !!ret;
1354 }
1355 } else if (host->cfg->needs_new_timings) {
1356 /* Supports new timing mode only */
1357 host->use_new_timings = true;
1358 }
1359
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001360 mmc->ops = &sunxi_mmc_ops;
1361 mmc->max_blk_count = 8192;
1362 mmc->max_blk_size = 4096;
1363 mmc->max_segs = PAGE_SIZE / sizeof(struct sunxi_idma_des);
Hans de Goede86a93312016-07-30 16:25:45 +02001364 mmc->max_seg_size = (1 << host->cfg->idma_des_size_bits);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001365 mmc->max_req_size = mmc->max_seg_size * mmc->max_segs;
Chen-Yu Tsai2dcb3052016-01-30 01:21:46 +08001366 /* 400kHz ~ 52MHz */
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001367 mmc->f_min = 400000;
Chen-Yu Tsai2dcb3052016-01-30 01:21:46 +08001368 mmc->f_max = 52000000;
Chen-Yu Tsai3df01a92014-08-20 21:39:20 +08001369 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Hans de Goedea4101dc2015-03-10 16:36:36 +01001370 MMC_CAP_ERASE | MMC_CAP_SDIO_IRQ;
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001371
Chen-Yu Tsaic903a2a2017-07-24 21:59:00 +08001372 if (host->cfg->clk_delays || host->use_new_timings)
Hans de Goedeb4656462016-07-30 16:25:47 +02001373 mmc->caps |= MMC_CAP_1_8V_DDR;
1374
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001375 ret = mmc_of_parse(mmc);
1376 if (ret)
1377 goto error_free_dma;
1378
1379 ret = mmc_add_host(mmc);
1380 if (ret)
1381 goto error_free_dma;
1382
1383 dev_info(&pdev->dev, "base:0x%p irq:%u\n", host->reg_base, host->irq);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001384 return 0;
1385
1386error_free_dma:
1387 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
1388error_free_host:
1389 mmc_free_host(mmc);
1390 return ret;
1391}
1392
1393static int sunxi_mmc_remove(struct platform_device *pdev)
1394{
1395 struct mmc_host *mmc = platform_get_drvdata(pdev);
1396 struct sunxi_mmc_host *host = mmc_priv(mmc);
1397
1398 mmc_remove_host(mmc);
1399 disable_irq(host->irq);
Maxime Ripard774c0102018-03-08 15:52:51 +01001400 sunxi_mmc_disable(host);
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001401 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
1402 mmc_free_host(mmc);
1403
1404 return 0;
1405}
1406
1407static struct platform_driver sunxi_mmc_driver = {
1408 .driver = {
1409 .name = "sunxi-mmc",
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001410 .of_match_table = of_match_ptr(sunxi_mmc_of_match),
1411 },
1412 .probe = sunxi_mmc_probe,
1413 .remove = sunxi_mmc_remove,
1414};
1415module_platform_driver(sunxi_mmc_driver);
1416
1417MODULE_DESCRIPTION("Allwinner's SD/MMC Card Controller Driver");
1418MODULE_LICENSE("GPL v2");
Adam Borowski1907e382017-11-28 04:44:55 +01001419MODULE_AUTHOR("David Lanzendörfer <david.lanzendoerfer@o2s.ch>");
David Lanzendörfer3cbcb1602014-05-12 14:04:48 +02001420MODULE_ALIAS("platform:sunxi-mmc");