Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * MMCIF eMMC driver. |
| 3 | * |
| 4 | * Copyright (C) 2010 Renesas Solutions Corp. |
| 5 | * Yusuke Goda <yusuke.goda.sx@renesas.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License. |
| 10 | * |
| 11 | * |
| 12 | * TODO |
| 13 | * 1. DMA |
| 14 | * 2. Power management |
| 15 | * 3. Handle MMC errors better |
| 16 | * |
| 17 | */ |
| 18 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 19 | /* |
| 20 | * The MMCIF driver is now processing MMC requests asynchronously, according |
| 21 | * to the Linux MMC API requirement. |
| 22 | * |
| 23 | * The MMCIF driver processes MMC requests in up to 3 stages: command, optional |
| 24 | * data, and optional stop. To achieve asynchronous processing each of these |
| 25 | * stages is split into two halves: a top and a bottom half. The top half |
| 26 | * initialises the hardware, installs a timeout handler to handle completion |
| 27 | * timeouts, and returns. In case of the command stage this immediately returns |
| 28 | * control to the caller, leaving all further processing to run asynchronously. |
| 29 | * All further request processing is performed by the bottom halves. |
| 30 | * |
| 31 | * The bottom half further consists of a "hard" IRQ handler, an IRQ handler |
| 32 | * thread, a DMA completion callback, if DMA is used, a timeout work, and |
| 33 | * request- and stage-specific handler methods. |
| 34 | * |
| 35 | * Each bottom half run begins with either a hardware interrupt, a DMA callback |
| 36 | * invocation, or a timeout work run. In case of an error or a successful |
| 37 | * processing completion, the MMC core is informed and the request processing is |
| 38 | * finished. In case processing has to continue, i.e., if data has to be read |
| 39 | * from or written to the card, or if a stop command has to be sent, the next |
| 40 | * top half is called, which performs the necessary hardware handling and |
| 41 | * reschedules the timeout work. This returns the driver state machine into the |
| 42 | * bottom half waiting state. |
| 43 | */ |
| 44 | |
Guennadi Liakhovetski | 86df174 | 2011-11-23 15:52:30 +0100 | [diff] [blame] | 45 | #include <linux/bitops.h> |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 46 | #include <linux/clk.h> |
| 47 | #include <linux/completion.h> |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 48 | #include <linux/delay.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 49 | #include <linux/dma-mapping.h> |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 50 | #include <linux/dmaengine.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 51 | #include <linux/mmc/card.h> |
| 52 | #include <linux/mmc/core.h> |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 53 | #include <linux/mmc/host.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 54 | #include <linux/mmc/mmc.h> |
| 55 | #include <linux/mmc/sdio.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 56 | #include <linux/mmc/sh_mmcif.h> |
Guennadi Liakhovetski | e480606 | 2012-06-14 14:24:35 +0200 | [diff] [blame] | 57 | #include <linux/mmc/slot-gpio.h> |
Guennadi Liakhovetski | bf68a81 | 2012-05-01 18:18:16 +0200 | [diff] [blame] | 58 | #include <linux/mod_devicetable.h> |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 59 | #include <linux/mutex.h> |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 60 | #include <linux/pagemap.h> |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 61 | #include <linux/platform_device.h> |
Rafael J. Wysocki | efe6a8a | 2012-03-13 01:02:15 +0100 | [diff] [blame] | 62 | #include <linux/pm_qos.h> |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 63 | #include <linux/pm_runtime.h> |
Guennadi Liakhovetski | d00cada | 2013-08-02 14:48:02 +0200 | [diff] [blame] | 64 | #include <linux/sh_dma.h> |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 65 | #include <linux/spinlock.h> |
Paul Gortmaker | 88b4767 | 2011-07-03 15:15:51 -0400 | [diff] [blame] | 66 | #include <linux/module.h> |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 67 | |
| 68 | #define DRIVER_NAME "sh_mmcif" |
| 69 | #define DRIVER_VERSION "2010-04-28" |
| 70 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 71 | /* CE_CMD_SET */ |
| 72 | #define CMD_MASK 0x3f000000 |
| 73 | #define CMD_SET_RTYP_NO ((0 << 23) | (0 << 22)) |
| 74 | #define CMD_SET_RTYP_6B ((0 << 23) | (1 << 22)) /* R1/R1b/R3/R4/R5 */ |
| 75 | #define CMD_SET_RTYP_17B ((1 << 23) | (0 << 22)) /* R2 */ |
| 76 | #define CMD_SET_RBSY (1 << 21) /* R1b */ |
| 77 | #define CMD_SET_CCSEN (1 << 20) |
| 78 | #define CMD_SET_WDAT (1 << 19) /* 1: on data, 0: no data */ |
| 79 | #define CMD_SET_DWEN (1 << 18) /* 1: write, 0: read */ |
| 80 | #define CMD_SET_CMLTE (1 << 17) /* 1: multi block trans, 0: single */ |
| 81 | #define CMD_SET_CMD12EN (1 << 16) /* 1: CMD12 auto issue */ |
| 82 | #define CMD_SET_RIDXC_INDEX ((0 << 15) | (0 << 14)) /* index check */ |
| 83 | #define CMD_SET_RIDXC_BITS ((0 << 15) | (1 << 14)) /* check bits check */ |
| 84 | #define CMD_SET_RIDXC_NO ((1 << 15) | (0 << 14)) /* no check */ |
| 85 | #define CMD_SET_CRC7C ((0 << 13) | (0 << 12)) /* CRC7 check*/ |
| 86 | #define CMD_SET_CRC7C_BITS ((0 << 13) | (1 << 12)) /* check bits check*/ |
| 87 | #define CMD_SET_CRC7C_INTERNAL ((1 << 13) | (0 << 12)) /* internal CRC7 check*/ |
| 88 | #define CMD_SET_CRC16C (1 << 10) /* 0: CRC16 check*/ |
| 89 | #define CMD_SET_CRCSTE (1 << 8) /* 1: not receive CRC status */ |
| 90 | #define CMD_SET_TBIT (1 << 7) /* 1: tran mission bit "Low" */ |
| 91 | #define CMD_SET_OPDM (1 << 6) /* 1: open/drain */ |
| 92 | #define CMD_SET_CCSH (1 << 5) |
Teppei Kamijou | 555061f | 2012-12-12 15:38:08 +0100 | [diff] [blame] | 93 | #define CMD_SET_DARS (1 << 2) /* Dual Data Rate */ |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 94 | #define CMD_SET_DATW_1 ((0 << 1) | (0 << 0)) /* 1bit */ |
| 95 | #define CMD_SET_DATW_4 ((0 << 1) | (1 << 0)) /* 4bit */ |
| 96 | #define CMD_SET_DATW_8 ((1 << 1) | (0 << 0)) /* 8bit */ |
| 97 | |
| 98 | /* CE_CMD_CTRL */ |
| 99 | #define CMD_CTRL_BREAK (1 << 0) |
| 100 | |
| 101 | /* CE_BLOCK_SET */ |
| 102 | #define BLOCK_SIZE_MASK 0x0000ffff |
| 103 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 104 | /* CE_INT */ |
| 105 | #define INT_CCSDE (1 << 29) |
| 106 | #define INT_CMD12DRE (1 << 26) |
| 107 | #define INT_CMD12RBE (1 << 25) |
| 108 | #define INT_CMD12CRE (1 << 24) |
| 109 | #define INT_DTRANE (1 << 23) |
| 110 | #define INT_BUFRE (1 << 22) |
| 111 | #define INT_BUFWEN (1 << 21) |
| 112 | #define INT_BUFREN (1 << 20) |
| 113 | #define INT_CCSRCV (1 << 19) |
| 114 | #define INT_RBSYE (1 << 17) |
| 115 | #define INT_CRSPE (1 << 16) |
| 116 | #define INT_CMDVIO (1 << 15) |
| 117 | #define INT_BUFVIO (1 << 14) |
| 118 | #define INT_WDATERR (1 << 11) |
| 119 | #define INT_RDATERR (1 << 10) |
| 120 | #define INT_RIDXERR (1 << 9) |
| 121 | #define INT_RSPERR (1 << 8) |
| 122 | #define INT_CCSTO (1 << 5) |
| 123 | #define INT_CRCSTO (1 << 4) |
| 124 | #define INT_WDATTO (1 << 3) |
| 125 | #define INT_RDATTO (1 << 2) |
| 126 | #define INT_RBSYTO (1 << 1) |
| 127 | #define INT_RSPTO (1 << 0) |
| 128 | #define INT_ERR_STS (INT_CMDVIO | INT_BUFVIO | INT_WDATERR | \ |
| 129 | INT_RDATERR | INT_RIDXERR | INT_RSPERR | \ |
| 130 | INT_CCSTO | INT_CRCSTO | INT_WDATTO | \ |
| 131 | INT_RDATTO | INT_RBSYTO | INT_RSPTO) |
| 132 | |
Guennadi Liakhovetski | 8af5075 | 2012-12-12 15:45:14 +0100 | [diff] [blame] | 133 | #define INT_ALL (INT_RBSYE | INT_CRSPE | INT_BUFREN | \ |
| 134 | INT_BUFWEN | INT_CMD12DRE | INT_BUFRE | \ |
| 135 | INT_DTRANE | INT_CMD12RBE | INT_CMD12CRE) |
| 136 | |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 137 | #define INT_CCS (INT_CCSTO | INT_CCSRCV | INT_CCSDE) |
| 138 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 139 | /* CE_INT_MASK */ |
| 140 | #define MASK_ALL 0x00000000 |
| 141 | #define MASK_MCCSDE (1 << 29) |
| 142 | #define MASK_MCMD12DRE (1 << 26) |
| 143 | #define MASK_MCMD12RBE (1 << 25) |
| 144 | #define MASK_MCMD12CRE (1 << 24) |
| 145 | #define MASK_MDTRANE (1 << 23) |
| 146 | #define MASK_MBUFRE (1 << 22) |
| 147 | #define MASK_MBUFWEN (1 << 21) |
| 148 | #define MASK_MBUFREN (1 << 20) |
| 149 | #define MASK_MCCSRCV (1 << 19) |
| 150 | #define MASK_MRBSYE (1 << 17) |
| 151 | #define MASK_MCRSPE (1 << 16) |
| 152 | #define MASK_MCMDVIO (1 << 15) |
| 153 | #define MASK_MBUFVIO (1 << 14) |
| 154 | #define MASK_MWDATERR (1 << 11) |
| 155 | #define MASK_MRDATERR (1 << 10) |
| 156 | #define MASK_MRIDXERR (1 << 9) |
| 157 | #define MASK_MRSPERR (1 << 8) |
| 158 | #define MASK_MCCSTO (1 << 5) |
| 159 | #define MASK_MCRCSTO (1 << 4) |
| 160 | #define MASK_MWDATTO (1 << 3) |
| 161 | #define MASK_MRDATTO (1 << 2) |
| 162 | #define MASK_MRBSYTO (1 << 1) |
| 163 | #define MASK_MRSPTO (1 << 0) |
| 164 | |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 165 | #define MASK_START_CMD (MASK_MCMDVIO | MASK_MBUFVIO | MASK_MWDATERR | \ |
| 166 | MASK_MRDATERR | MASK_MRIDXERR | MASK_MRSPERR | \ |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 167 | MASK_MCRCSTO | MASK_MWDATTO | \ |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 168 | MASK_MRDATTO | MASK_MRBSYTO | MASK_MRSPTO) |
| 169 | |
Guennadi Liakhovetski | 8af5075 | 2012-12-12 15:45:14 +0100 | [diff] [blame] | 170 | #define MASK_CLEAN (INT_ERR_STS | MASK_MRBSYE | MASK_MCRSPE | \ |
| 171 | MASK_MBUFREN | MASK_MBUFWEN | \ |
| 172 | MASK_MCMD12DRE | MASK_MBUFRE | MASK_MDTRANE | \ |
| 173 | MASK_MCMD12RBE | MASK_MCMD12CRE) |
| 174 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 175 | /* CE_HOST_STS1 */ |
| 176 | #define STS1_CMDSEQ (1 << 31) |
| 177 | |
| 178 | /* CE_HOST_STS2 */ |
| 179 | #define STS2_CRCSTE (1 << 31) |
| 180 | #define STS2_CRC16E (1 << 30) |
| 181 | #define STS2_AC12CRCE (1 << 29) |
| 182 | #define STS2_RSPCRC7E (1 << 28) |
| 183 | #define STS2_CRCSTEBE (1 << 27) |
| 184 | #define STS2_RDATEBE (1 << 26) |
| 185 | #define STS2_AC12REBE (1 << 25) |
| 186 | #define STS2_RSPEBE (1 << 24) |
| 187 | #define STS2_AC12IDXE (1 << 23) |
| 188 | #define STS2_RSPIDXE (1 << 22) |
| 189 | #define STS2_CCSTO (1 << 15) |
| 190 | #define STS2_RDATTO (1 << 14) |
| 191 | #define STS2_DATBSYTO (1 << 13) |
| 192 | #define STS2_CRCSTTO (1 << 12) |
| 193 | #define STS2_AC12BSYTO (1 << 11) |
| 194 | #define STS2_RSPBSYTO (1 << 10) |
| 195 | #define STS2_AC12RSPTO (1 << 9) |
| 196 | #define STS2_RSPTO (1 << 8) |
| 197 | #define STS2_CRC_ERR (STS2_CRCSTE | STS2_CRC16E | \ |
| 198 | STS2_AC12CRCE | STS2_RSPCRC7E | STS2_CRCSTEBE) |
| 199 | #define STS2_TIMEOUT_ERR (STS2_CCSTO | STS2_RDATTO | \ |
| 200 | STS2_DATBSYTO | STS2_CRCSTTO | \ |
| 201 | STS2_AC12BSYTO | STS2_RSPBSYTO | \ |
| 202 | STS2_AC12RSPTO | STS2_RSPTO) |
| 203 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 204 | #define CLKDEV_EMMC_DATA 52000000 /* 52MHz */ |
| 205 | #define CLKDEV_MMC_DATA 20000000 /* 20MHz */ |
| 206 | #define CLKDEV_INIT 400000 /* 400 KHz */ |
| 207 | |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 208 | enum sh_mmcif_state { |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 209 | STATE_IDLE, |
| 210 | STATE_REQUEST, |
| 211 | STATE_IOS, |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 212 | STATE_TIMEOUT, |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 215 | enum sh_mmcif_wait_for { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 216 | MMCIF_WAIT_FOR_REQUEST, |
| 217 | MMCIF_WAIT_FOR_CMD, |
| 218 | MMCIF_WAIT_FOR_MREAD, |
| 219 | MMCIF_WAIT_FOR_MWRITE, |
| 220 | MMCIF_WAIT_FOR_READ, |
| 221 | MMCIF_WAIT_FOR_WRITE, |
| 222 | MMCIF_WAIT_FOR_READ_END, |
| 223 | MMCIF_WAIT_FOR_WRITE_END, |
| 224 | MMCIF_WAIT_FOR_STOP, |
| 225 | }; |
| 226 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 227 | struct sh_mmcif_host { |
| 228 | struct mmc_host *mmc; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 229 | struct mmc_request *mrq; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 230 | struct platform_device *pd; |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 231 | struct clk *clk; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 232 | int bus_width; |
Teppei Kamijou | 555061f | 2012-12-12 15:38:08 +0100 | [diff] [blame] | 233 | unsigned char timing; |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 234 | bool sd_error; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 235 | bool dying; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 236 | long timeout; |
| 237 | void __iomem *addr; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 238 | u32 *pio_ptr; |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 239 | spinlock_t lock; /* protect sh_mmcif_host::state */ |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 240 | enum sh_mmcif_state state; |
| 241 | enum sh_mmcif_wait_for wait_for; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 242 | struct delayed_work timeout_work; |
| 243 | size_t blocksize; |
| 244 | int sg_idx; |
| 245 | int sg_blkidx; |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 246 | bool power; |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 247 | bool card_present; |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 248 | bool ccs_enable; /* Command Completion Signal support */ |
Guennadi Liakhovetski | 6d6fd36 | 2013-07-10 21:21:13 +0200 | [diff] [blame] | 249 | bool clk_ctrl2_enable; |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 250 | struct mutex thread_lock; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 251 | |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 252 | /* DMA support */ |
| 253 | struct dma_chan *chan_rx; |
| 254 | struct dma_chan *chan_tx; |
| 255 | struct completion dma_complete; |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 256 | bool dma_active; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 257 | }; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 258 | |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 259 | static const struct of_device_id sh_mmcif_of_match[] = { |
Kuninori Morimoto | 70830b4 | 2015-04-23 08:13:25 +0000 | [diff] [blame] | 260 | { .compatible = "renesas,sh-mmcif" }, |
| 261 | { } |
| 262 | }; |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 263 | MODULE_DEVICE_TABLE(of, sh_mmcif_of_match); |
Kuninori Morimoto | 70830b4 | 2015-04-23 08:13:25 +0000 | [diff] [blame] | 264 | |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 265 | #define sh_mmcif_host_to_dev(host) (&host->pd->dev) |
| 266 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 267 | static inline void sh_mmcif_bitset(struct sh_mmcif_host *host, |
| 268 | unsigned int reg, u32 val) |
| 269 | { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 270 | writel(val | readl(host->addr + reg), host->addr + reg); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static inline void sh_mmcif_bitclr(struct sh_mmcif_host *host, |
| 274 | unsigned int reg, u32 val) |
| 275 | { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 276 | writel(~val & readl(host->addr + reg), host->addr + reg); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 279 | static void sh_mmcif_dma_complete(void *arg) |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 280 | { |
| 281 | struct sh_mmcif_host *host = arg; |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 282 | struct mmc_request *mrq = host->mrq; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 283 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 284 | |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 285 | dev_dbg(dev, "Command completed\n"); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 286 | |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 287 | if (WARN(!mrq || !mrq->data, "%s: NULL data in DMA completion!\n", |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 288 | dev_name(dev))) |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 289 | return; |
| 290 | |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 291 | complete(&host->dma_complete); |
| 292 | } |
| 293 | |
| 294 | static void sh_mmcif_start_dma_rx(struct sh_mmcif_host *host) |
| 295 | { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 296 | struct mmc_data *data = host->mrq->data; |
| 297 | struct scatterlist *sg = data->sg; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 298 | struct dma_async_tx_descriptor *desc = NULL; |
| 299 | struct dma_chan *chan = host->chan_rx; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 300 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 301 | dma_cookie_t cookie = -EINVAL; |
| 302 | int ret; |
| 303 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 304 | ret = dma_map_sg(chan->device->dev, sg, data->sg_len, |
Linus Walleij | 1ed828d | 2011-02-10 16:09:29 +0100 | [diff] [blame] | 305 | DMA_FROM_DEVICE); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 306 | if (ret > 0) { |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 307 | host->dma_active = true; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 308 | desc = dmaengine_prep_slave_sg(chan, sg, ret, |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 309 | DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (desc) { |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 313 | desc->callback = sh_mmcif_dma_complete; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 314 | desc->callback_param = host; |
Linus Walleij | a5ece7d | 2011-02-10 16:10:00 +0100 | [diff] [blame] | 315 | cookie = dmaengine_submit(desc); |
| 316 | sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN); |
| 317 | dma_async_issue_pending(chan); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 318 | } |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 319 | dev_dbg(dev, "%s(): mapped %d -> %d, cookie %d\n", |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 320 | __func__, data->sg_len, ret, cookie); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 321 | |
| 322 | if (!desc) { |
| 323 | /* DMA failed, fall back to PIO */ |
| 324 | if (ret >= 0) |
| 325 | ret = -EIO; |
| 326 | host->chan_rx = NULL; |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 327 | host->dma_active = false; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 328 | dma_release_channel(chan); |
| 329 | /* Free the Tx channel too */ |
| 330 | chan = host->chan_tx; |
| 331 | if (chan) { |
| 332 | host->chan_tx = NULL; |
| 333 | dma_release_channel(chan); |
| 334 | } |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 335 | dev_warn(dev, |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 336 | "DMA failed: %d, falling back to PIO\n", ret); |
| 337 | sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); |
| 338 | } |
| 339 | |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 340 | dev_dbg(dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__, |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 341 | desc, cookie, data->sg_len); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static void sh_mmcif_start_dma_tx(struct sh_mmcif_host *host) |
| 345 | { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 346 | struct mmc_data *data = host->mrq->data; |
| 347 | struct scatterlist *sg = data->sg; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 348 | struct dma_async_tx_descriptor *desc = NULL; |
| 349 | struct dma_chan *chan = host->chan_tx; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 350 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 351 | dma_cookie_t cookie = -EINVAL; |
| 352 | int ret; |
| 353 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 354 | ret = dma_map_sg(chan->device->dev, sg, data->sg_len, |
Linus Walleij | 1ed828d | 2011-02-10 16:09:29 +0100 | [diff] [blame] | 355 | DMA_TO_DEVICE); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 356 | if (ret > 0) { |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 357 | host->dma_active = true; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 358 | desc = dmaengine_prep_slave_sg(chan, sg, ret, |
Vinod Koul | 05f5799 | 2011-10-14 10:45:11 +0530 | [diff] [blame] | 359 | DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | if (desc) { |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 363 | desc->callback = sh_mmcif_dma_complete; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 364 | desc->callback_param = host; |
Linus Walleij | a5ece7d | 2011-02-10 16:10:00 +0100 | [diff] [blame] | 365 | cookie = dmaengine_submit(desc); |
| 366 | sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAWEN); |
| 367 | dma_async_issue_pending(chan); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 368 | } |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 369 | dev_dbg(dev, "%s(): mapped %d -> %d, cookie %d\n", |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 370 | __func__, data->sg_len, ret, cookie); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 371 | |
| 372 | if (!desc) { |
| 373 | /* DMA failed, fall back to PIO */ |
| 374 | if (ret >= 0) |
| 375 | ret = -EIO; |
| 376 | host->chan_tx = NULL; |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 377 | host->dma_active = false; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 378 | dma_release_channel(chan); |
| 379 | /* Free the Rx channel too */ |
| 380 | chan = host->chan_rx; |
| 381 | if (chan) { |
| 382 | host->chan_rx = NULL; |
| 383 | dma_release_channel(chan); |
| 384 | } |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 385 | dev_warn(dev, |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 386 | "DMA failed: %d, falling back to PIO\n", ret); |
| 387 | sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); |
| 388 | } |
| 389 | |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 390 | dev_dbg(dev, "%s(): desc %p, cookie %d\n", __func__, |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 391 | desc, cookie); |
| 392 | } |
| 393 | |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 394 | static struct dma_chan * |
| 395 | sh_mmcif_request_dma_one(struct sh_mmcif_host *host, |
| 396 | struct sh_mmcif_plat_data *pdata, |
| 397 | enum dma_transfer_direction direction) |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 398 | { |
Laurent Pinchart | d25006e | 2014-07-16 00:45:12 +0200 | [diff] [blame] | 399 | struct dma_slave_config cfg = { 0, }; |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 400 | struct dma_chan *chan; |
Kuninori Morimoto | 5f48dd0 | 2015-02-17 01:47:01 +0000 | [diff] [blame] | 401 | void *slave_data = NULL; |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 402 | struct resource *res; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 403 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame] | 404 | dma_cap_mask_t mask; |
| 405 | int ret; |
| 406 | |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 407 | dma_cap_zero(mask); |
| 408 | dma_cap_set(DMA_SLAVE, mask); |
| 409 | |
| 410 | if (pdata) |
Kuninori Morimoto | 5f48dd0 | 2015-02-17 01:47:01 +0000 | [diff] [blame] | 411 | slave_data = direction == DMA_MEM_TO_DEV ? |
| 412 | (void *)pdata->slave_id_tx : |
| 413 | (void *)pdata->slave_id_rx; |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 414 | |
| 415 | chan = dma_request_slave_channel_compat(mask, shdma_chan_filter, |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 416 | slave_data, dev, |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 417 | direction == DMA_MEM_TO_DEV ? "tx" : "rx"); |
| 418 | |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 419 | dev_dbg(dev, "%s: %s: got channel %p\n", __func__, |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 420 | direction == DMA_MEM_TO_DEV ? "TX" : "RX", chan); |
| 421 | |
| 422 | if (!chan) |
| 423 | return NULL; |
| 424 | |
| 425 | res = platform_get_resource(host->pd, IORESOURCE_MEM, 0); |
| 426 | |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 427 | cfg.direction = direction; |
Laurent Pinchart | d25006e | 2014-07-16 00:45:12 +0200 | [diff] [blame] | 428 | |
Laurent Pinchart | e36152a | 2014-07-16 00:45:13 +0200 | [diff] [blame] | 429 | if (direction == DMA_DEV_TO_MEM) { |
Laurent Pinchart | d25006e | 2014-07-16 00:45:12 +0200 | [diff] [blame] | 430 | cfg.src_addr = res->start + MMCIF_CE_DATA; |
Laurent Pinchart | e36152a | 2014-07-16 00:45:13 +0200 | [diff] [blame] | 431 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 432 | } else { |
Laurent Pinchart | d25006e | 2014-07-16 00:45:12 +0200 | [diff] [blame] | 433 | cfg.dst_addr = res->start + MMCIF_CE_DATA; |
Laurent Pinchart | e36152a | 2014-07-16 00:45:13 +0200 | [diff] [blame] | 434 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 435 | } |
Laurent Pinchart | d25006e | 2014-07-16 00:45:12 +0200 | [diff] [blame] | 436 | |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 437 | ret = dmaengine_slave_config(chan, &cfg); |
| 438 | if (ret < 0) { |
| 439 | dma_release_channel(chan); |
| 440 | return NULL; |
| 441 | } |
| 442 | |
| 443 | return chan; |
| 444 | } |
| 445 | |
| 446 | static void sh_mmcif_request_dma(struct sh_mmcif_host *host, |
| 447 | struct sh_mmcif_plat_data *pdata) |
| 448 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 449 | struct device *dev = sh_mmcif_host_to_dev(host); |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 450 | host->dma_active = false; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 451 | |
Guennadi Liakhovetski | acd6d77 | 2013-06-24 14:36:34 +0200 | [diff] [blame] | 452 | if (pdata) { |
| 453 | if (pdata->slave_id_tx <= 0 || pdata->slave_id_rx <= 0) |
| 454 | return; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 455 | } else if (!dev->of_node) { |
Guennadi Liakhovetski | bf68a81 | 2012-05-01 18:18:16 +0200 | [diff] [blame] | 456 | return; |
Guennadi Liakhovetski | acd6d77 | 2013-06-24 14:36:34 +0200 | [diff] [blame] | 457 | } |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame] | 458 | |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 459 | /* We can only either use DMA for both Tx and Rx or not use it at all */ |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 460 | host->chan_tx = sh_mmcif_request_dma_one(host, pdata, DMA_MEM_TO_DEV); |
Guennadi Liakhovetski | 0e79f9a | 2012-07-05 12:29:43 +0200 | [diff] [blame] | 461 | if (!host->chan_tx) |
| 462 | return; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 463 | |
Laurent Pinchart | e5a233c | 2013-10-30 12:34:51 +0100 | [diff] [blame] | 464 | host->chan_rx = sh_mmcif_request_dma_one(host, pdata, DMA_DEV_TO_MEM); |
| 465 | if (!host->chan_rx) { |
| 466 | dma_release_channel(host->chan_tx); |
| 467 | host->chan_tx = NULL; |
| 468 | } |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | static void sh_mmcif_release_dma(struct sh_mmcif_host *host) |
| 472 | { |
| 473 | sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, BUF_ACC_DMAREN | BUF_ACC_DMAWEN); |
| 474 | /* Descriptors are freed automatically */ |
| 475 | if (host->chan_tx) { |
| 476 | struct dma_chan *chan = host->chan_tx; |
| 477 | host->chan_tx = NULL; |
| 478 | dma_release_channel(chan); |
| 479 | } |
| 480 | if (host->chan_rx) { |
| 481 | struct dma_chan *chan = host->chan_rx; |
| 482 | host->chan_rx = NULL; |
| 483 | dma_release_channel(chan); |
| 484 | } |
| 485 | |
Linus Walleij | f38f94c | 2011-02-10 16:09:50 +0100 | [diff] [blame] | 486 | host->dma_active = false; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 487 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 488 | |
| 489 | static void sh_mmcif_clock_control(struct sh_mmcif_host *host, unsigned int clk) |
| 490 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 491 | struct device *dev = sh_mmcif_host_to_dev(host); |
| 492 | struct sh_mmcif_plat_data *p = dev->platform_data; |
Guennadi Liakhovetski | bf68a81 | 2012-05-01 18:18:16 +0200 | [diff] [blame] | 493 | bool sup_pclk = p ? p->sup_pclk : false; |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 494 | unsigned int current_clk = clk_get_rate(host->clk); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 495 | |
| 496 | sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE); |
| 497 | sh_mmcif_bitclr(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR); |
| 498 | |
| 499 | if (!clk) |
| 500 | return; |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 501 | if (sup_pclk && clk == current_clk) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 502 | sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_SUP_PCLK); |
| 503 | else |
| 504 | sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_CLEAR & |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 505 | ((fls(DIV_ROUND_UP(current_clk, |
Simon Horman | f938825 | 2012-03-28 18:01:09 +0900 | [diff] [blame] | 506 | clk) - 1) - 1) << 16)); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 507 | |
| 508 | sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, CLK_ENABLE); |
| 509 | } |
| 510 | |
| 511 | static void sh_mmcif_sync_reset(struct sh_mmcif_host *host) |
| 512 | { |
| 513 | u32 tmp; |
| 514 | |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 515 | tmp = 0x010f0000 & sh_mmcif_readl(host->addr, MMCIF_CE_CLK_CTRL); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 516 | |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 517 | sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_ON); |
| 518 | sh_mmcif_writel(host->addr, MMCIF_CE_VERSION, SOFT_RST_OFF); |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 519 | if (host->ccs_enable) |
| 520 | tmp |= SCCSTO_29; |
Guennadi Liakhovetski | 6d6fd36 | 2013-07-10 21:21:13 +0200 | [diff] [blame] | 521 | if (host->clk_ctrl2_enable) |
| 522 | sh_mmcif_writel(host->addr, MMCIF_CE_CLK_CTRL2, 0x0F0F0000); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 523 | sh_mmcif_bitset(host, MMCIF_CE_CLK_CTRL, tmp | |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 524 | SRSPTO_256 | SRBSYTO_29 | SRWDTO_29); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 525 | /* byte swap on */ |
| 526 | sh_mmcif_bitset(host, MMCIF_CE_BUF_ACC, BUF_ACC_ATYP); |
| 527 | } |
| 528 | |
| 529 | static int sh_mmcif_error_manage(struct sh_mmcif_host *host) |
| 530 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 531 | struct device *dev = sh_mmcif_host_to_dev(host); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 532 | u32 state1, state2; |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 533 | int ret, timeout; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 534 | |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 535 | host->sd_error = false; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 536 | |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 537 | state1 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1); |
| 538 | state2 = sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS2); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 539 | dev_dbg(dev, "ERR HOST_STS1 = %08x\n", state1); |
| 540 | dev_dbg(dev, "ERR HOST_STS2 = %08x\n", state2); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 541 | |
| 542 | if (state1 & STS1_CMDSEQ) { |
| 543 | sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, CMD_CTRL_BREAK); |
| 544 | sh_mmcif_bitset(host, MMCIF_CE_CMD_CTRL, ~CMD_CTRL_BREAK); |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 545 | for (timeout = 10000000; timeout; timeout--) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 546 | if (!(sh_mmcif_readl(host->addr, MMCIF_CE_HOST_STS1) |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 547 | & STS1_CMDSEQ)) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 548 | break; |
| 549 | mdelay(1); |
| 550 | } |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 551 | if (!timeout) { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 552 | dev_err(dev, |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 553 | "Forced end of command sequence timeout err\n"); |
| 554 | return -EIO; |
| 555 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 556 | sh_mmcif_sync_reset(host); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 557 | dev_dbg(dev, "Forced end of command sequence\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 558 | return -EIO; |
| 559 | } |
| 560 | |
| 561 | if (state2 & STS2_CRC_ERR) { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 562 | dev_err(dev, " CRC error: state %u, wait %u\n", |
Teppei Kamijou | e475b27 | 2012-12-12 15:38:18 +0100 | [diff] [blame] | 563 | host->state, host->wait_for); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 564 | ret = -EIO; |
| 565 | } else if (state2 & STS2_TIMEOUT_ERR) { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 566 | dev_err(dev, " Timeout: state %u, wait %u\n", |
Teppei Kamijou | e475b27 | 2012-12-12 15:38:18 +0100 | [diff] [blame] | 567 | host->state, host->wait_for); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 568 | ret = -ETIMEDOUT; |
| 569 | } else { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 570 | dev_dbg(dev, " End/Index error: state %u, wait %u\n", |
Teppei Kamijou | e475b27 | 2012-12-12 15:38:18 +0100 | [diff] [blame] | 571 | host->state, host->wait_for); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 572 | ret = -EIO; |
| 573 | } |
| 574 | return ret; |
| 575 | } |
| 576 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 577 | static bool sh_mmcif_next_block(struct sh_mmcif_host *host, u32 *p) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 578 | { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 579 | struct mmc_data *data = host->mrq->data; |
| 580 | |
| 581 | host->sg_blkidx += host->blocksize; |
| 582 | |
| 583 | /* data->sg->length must be a multiple of host->blocksize? */ |
| 584 | BUG_ON(host->sg_blkidx > data->sg->length); |
| 585 | |
| 586 | if (host->sg_blkidx == data->sg->length) { |
| 587 | host->sg_blkidx = 0; |
| 588 | if (++host->sg_idx < data->sg_len) |
| 589 | host->pio_ptr = sg_virt(++data->sg); |
| 590 | } else { |
| 591 | host->pio_ptr = p; |
| 592 | } |
| 593 | |
Guennadi Liakhovetski | 99eb9d8 | 2012-12-12 15:38:13 +0100 | [diff] [blame] | 594 | return host->sg_idx != data->sg_len; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static void sh_mmcif_single_read(struct sh_mmcif_host *host, |
| 598 | struct mmc_request *mrq) |
| 599 | { |
| 600 | host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & |
| 601 | BLOCK_SIZE_MASK) + 3; |
| 602 | |
| 603 | host->wait_for = MMCIF_WAIT_FOR_READ; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 604 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 605 | /* buf read enable */ |
| 606 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 607 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 608 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 609 | static bool sh_mmcif_read_block(struct sh_mmcif_host *host) |
| 610 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 611 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 612 | struct mmc_data *data = host->mrq->data; |
| 613 | u32 *p = sg_virt(data->sg); |
| 614 | int i; |
| 615 | |
| 616 | if (host->sd_error) { |
| 617 | data->error = sh_mmcif_error_manage(host); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 618 | dev_dbg(dev, "%s(): %d\n", __func__, data->error); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 619 | return false; |
| 620 | } |
| 621 | |
| 622 | for (i = 0; i < host->blocksize / 4; i++) |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 623 | *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 624 | |
| 625 | /* buffer read end */ |
| 626 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFRE); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 627 | host->wait_for = MMCIF_WAIT_FOR_READ_END; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 628 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 629 | return true; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 632 | static void sh_mmcif_multi_read(struct sh_mmcif_host *host, |
| 633 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 634 | { |
| 635 | struct mmc_data *data = mrq->data; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 636 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 637 | if (!data->sg_len || !data->sg->length) |
| 638 | return; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 639 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 640 | host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & |
| 641 | BLOCK_SIZE_MASK; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 642 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 643 | host->wait_for = MMCIF_WAIT_FOR_MREAD; |
| 644 | host->sg_idx = 0; |
| 645 | host->sg_blkidx = 0; |
| 646 | host->pio_ptr = sg_virt(data->sg); |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 647 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 648 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); |
| 649 | } |
| 650 | |
| 651 | static bool sh_mmcif_mread_block(struct sh_mmcif_host *host) |
| 652 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 653 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 654 | struct mmc_data *data = host->mrq->data; |
| 655 | u32 *p = host->pio_ptr; |
| 656 | int i; |
| 657 | |
| 658 | if (host->sd_error) { |
| 659 | data->error = sh_mmcif_error_manage(host); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 660 | dev_dbg(dev, "%s(): %d\n", __func__, data->error); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 661 | return false; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 662 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 663 | |
| 664 | BUG_ON(!data->sg->length); |
| 665 | |
| 666 | for (i = 0; i < host->blocksize / 4; i++) |
| 667 | *p++ = sh_mmcif_readl(host->addr, MMCIF_CE_DATA); |
| 668 | |
| 669 | if (!sh_mmcif_next_block(host, p)) |
| 670 | return false; |
| 671 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 672 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFREN); |
| 673 | |
| 674 | return true; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 677 | static void sh_mmcif_single_write(struct sh_mmcif_host *host, |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 678 | struct mmc_request *mrq) |
| 679 | { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 680 | host->blocksize = (sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & |
| 681 | BLOCK_SIZE_MASK) + 3; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 682 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 683 | host->wait_for = MMCIF_WAIT_FOR_WRITE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 684 | |
| 685 | /* buf write enable */ |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 686 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); |
| 687 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 688 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 689 | static bool sh_mmcif_write_block(struct sh_mmcif_host *host) |
| 690 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 691 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 692 | struct mmc_data *data = host->mrq->data; |
| 693 | u32 *p = sg_virt(data->sg); |
| 694 | int i; |
| 695 | |
| 696 | if (host->sd_error) { |
| 697 | data->error = sh_mmcif_error_manage(host); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 698 | dev_dbg(dev, "%s(): %d\n", __func__, data->error); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 699 | return false; |
| 700 | } |
| 701 | |
| 702 | for (i = 0; i < host->blocksize / 4; i++) |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 703 | sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 704 | |
| 705 | /* buffer write end */ |
| 706 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MDTRANE); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 707 | host->wait_for = MMCIF_WAIT_FOR_WRITE_END; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 708 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 709 | return true; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 712 | static void sh_mmcif_multi_write(struct sh_mmcif_host *host, |
| 713 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 714 | { |
| 715 | struct mmc_data *data = mrq->data; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 716 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 717 | if (!data->sg_len || !data->sg->length) |
| 718 | return; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 719 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 720 | host->blocksize = sh_mmcif_readl(host->addr, MMCIF_CE_BLOCK_SET) & |
| 721 | BLOCK_SIZE_MASK; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 722 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 723 | host->wait_for = MMCIF_WAIT_FOR_MWRITE; |
| 724 | host->sg_idx = 0; |
| 725 | host->sg_blkidx = 0; |
| 726 | host->pio_ptr = sg_virt(data->sg); |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 727 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 728 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); |
| 729 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 730 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 731 | static bool sh_mmcif_mwrite_block(struct sh_mmcif_host *host) |
| 732 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 733 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 734 | struct mmc_data *data = host->mrq->data; |
| 735 | u32 *p = host->pio_ptr; |
| 736 | int i; |
| 737 | |
| 738 | if (host->sd_error) { |
| 739 | data->error = sh_mmcif_error_manage(host); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 740 | dev_dbg(dev, "%s(): %d\n", __func__, data->error); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 741 | return false; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 742 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 743 | |
| 744 | BUG_ON(!data->sg->length); |
| 745 | |
| 746 | for (i = 0; i < host->blocksize / 4; i++) |
| 747 | sh_mmcif_writel(host->addr, MMCIF_CE_DATA, *p++); |
| 748 | |
| 749 | if (!sh_mmcif_next_block(host, p)) |
| 750 | return false; |
| 751 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 752 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MBUFWEN); |
| 753 | |
| 754 | return true; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static void sh_mmcif_get_response(struct sh_mmcif_host *host, |
| 758 | struct mmc_command *cmd) |
| 759 | { |
| 760 | if (cmd->flags & MMC_RSP_136) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 761 | cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP3); |
| 762 | cmd->resp[1] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP2); |
| 763 | cmd->resp[2] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP1); |
| 764 | cmd->resp[3] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 765 | } else |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 766 | cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP0); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | static void sh_mmcif_get_cmd12response(struct sh_mmcif_host *host, |
| 770 | struct mmc_command *cmd) |
| 771 | { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 772 | cmd->resp[0] = sh_mmcif_readl(host->addr, MMCIF_CE_RESP_CMD12); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static u32 sh_mmcif_set_cmd(struct sh_mmcif_host *host, |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 776 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 777 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 778 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 779 | struct mmc_data *data = mrq->data; |
| 780 | struct mmc_command *cmd = mrq->cmd; |
| 781 | u32 opc = cmd->opcode; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 782 | u32 tmp = 0; |
| 783 | |
| 784 | /* Response Type check */ |
| 785 | switch (mmc_resp_type(cmd)) { |
| 786 | case MMC_RSP_NONE: |
| 787 | tmp |= CMD_SET_RTYP_NO; |
| 788 | break; |
| 789 | case MMC_RSP_R1: |
| 790 | case MMC_RSP_R1B: |
| 791 | case MMC_RSP_R3: |
| 792 | tmp |= CMD_SET_RTYP_6B; |
| 793 | break; |
| 794 | case MMC_RSP_R2: |
| 795 | tmp |= CMD_SET_RTYP_17B; |
| 796 | break; |
| 797 | default: |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 798 | dev_err(dev, "Unsupported response type.\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 799 | break; |
| 800 | } |
| 801 | switch (opc) { |
| 802 | /* RBSY */ |
Teppei Kamijou | a812ba0 | 2012-12-12 15:38:10 +0100 | [diff] [blame] | 803 | case MMC_SLEEP_AWAKE: |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 804 | case MMC_SWITCH: |
| 805 | case MMC_STOP_TRANSMISSION: |
| 806 | case MMC_SET_WRITE_PROT: |
| 807 | case MMC_CLR_WRITE_PROT: |
| 808 | case MMC_ERASE: |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 809 | tmp |= CMD_SET_RBSY; |
| 810 | break; |
| 811 | } |
| 812 | /* WDAT / DATW */ |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 813 | if (data) { |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 814 | tmp |= CMD_SET_WDAT; |
| 815 | switch (host->bus_width) { |
| 816 | case MMC_BUS_WIDTH_1: |
| 817 | tmp |= CMD_SET_DATW_1; |
| 818 | break; |
| 819 | case MMC_BUS_WIDTH_4: |
| 820 | tmp |= CMD_SET_DATW_4; |
| 821 | break; |
| 822 | case MMC_BUS_WIDTH_8: |
| 823 | tmp |= CMD_SET_DATW_8; |
| 824 | break; |
| 825 | default: |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 826 | dev_err(dev, "Unsupported bus width.\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 827 | break; |
| 828 | } |
Teppei Kamijou | 555061f | 2012-12-12 15:38:08 +0100 | [diff] [blame] | 829 | switch (host->timing) { |
Seungwon Jeon | 4039ff47 | 2014-03-14 21:12:33 +0900 | [diff] [blame] | 830 | case MMC_TIMING_MMC_DDR52: |
Teppei Kamijou | 555061f | 2012-12-12 15:38:08 +0100 | [diff] [blame] | 831 | /* |
| 832 | * MMC core will only set this timing, if the host |
Seungwon Jeon | 4039ff47 | 2014-03-14 21:12:33 +0900 | [diff] [blame] | 833 | * advertises the MMC_CAP_1_8V_DDR/MMC_CAP_1_2V_DDR |
| 834 | * capability. MMCIF implementations with this |
| 835 | * capability, e.g. sh73a0, will have to set it |
| 836 | * in their platform data. |
Teppei Kamijou | 555061f | 2012-12-12 15:38:08 +0100 | [diff] [blame] | 837 | */ |
| 838 | tmp |= CMD_SET_DARS; |
| 839 | break; |
| 840 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 841 | } |
| 842 | /* DWEN */ |
| 843 | if (opc == MMC_WRITE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) |
| 844 | tmp |= CMD_SET_DWEN; |
| 845 | /* CMLTE/CMD12EN */ |
| 846 | if (opc == MMC_READ_MULTIPLE_BLOCK || opc == MMC_WRITE_MULTIPLE_BLOCK) { |
| 847 | tmp |= CMD_SET_CMLTE | CMD_SET_CMD12EN; |
| 848 | sh_mmcif_bitset(host, MMCIF_CE_BLOCK_SET, |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 849 | data->blocks << 16); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 850 | } |
| 851 | /* RIDXC[1:0] check bits */ |
| 852 | if (opc == MMC_SEND_OP_COND || opc == MMC_ALL_SEND_CID || |
| 853 | opc == MMC_SEND_CSD || opc == MMC_SEND_CID) |
| 854 | tmp |= CMD_SET_RIDXC_BITS; |
| 855 | /* RCRC7C[1:0] check bits */ |
| 856 | if (opc == MMC_SEND_OP_COND) |
| 857 | tmp |= CMD_SET_CRC7C_BITS; |
| 858 | /* RCRC7C[1:0] internal CRC7 */ |
| 859 | if (opc == MMC_ALL_SEND_CID || |
| 860 | opc == MMC_SEND_CSD || opc == MMC_SEND_CID) |
| 861 | tmp |= CMD_SET_CRC7C_INTERNAL; |
| 862 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 863 | return (opc << 24) | tmp; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 866 | static int sh_mmcif_data_trans(struct sh_mmcif_host *host, |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 867 | struct mmc_request *mrq, u32 opc) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 868 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 869 | struct device *dev = sh_mmcif_host_to_dev(host); |
| 870 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 871 | switch (opc) { |
| 872 | case MMC_READ_MULTIPLE_BLOCK: |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 873 | sh_mmcif_multi_read(host, mrq); |
| 874 | return 0; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 875 | case MMC_WRITE_MULTIPLE_BLOCK: |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 876 | sh_mmcif_multi_write(host, mrq); |
| 877 | return 0; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 878 | case MMC_WRITE_BLOCK: |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 879 | sh_mmcif_single_write(host, mrq); |
| 880 | return 0; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 881 | case MMC_READ_SINGLE_BLOCK: |
| 882 | case MMC_SEND_EXT_CSD: |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 883 | sh_mmcif_single_read(host, mrq); |
| 884 | return 0; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 885 | default: |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 886 | dev_err(dev, "Unsupported CMD%d\n", opc); |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 887 | return -EINVAL; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 888 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | static void sh_mmcif_start_cmd(struct sh_mmcif_host *host, |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 892 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 893 | { |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 894 | struct mmc_command *cmd = mrq->cmd; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 895 | u32 opc = cmd->opcode; |
| 896 | u32 mask; |
Kouichi Tomita | dbb42d9 | 2015-02-15 23:46:46 +0900 | [diff] [blame] | 897 | unsigned long flags; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 898 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 899 | switch (opc) { |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 900 | /* response busy check */ |
Teppei Kamijou | a812ba0 | 2012-12-12 15:38:10 +0100 | [diff] [blame] | 901 | case MMC_SLEEP_AWAKE: |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 902 | case MMC_SWITCH: |
| 903 | case MMC_STOP_TRANSMISSION: |
| 904 | case MMC_SET_WRITE_PROT: |
| 905 | case MMC_CLR_WRITE_PROT: |
| 906 | case MMC_ERASE: |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 907 | mask = MASK_START_CMD | MASK_MRBSYE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 908 | break; |
| 909 | default: |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 910 | mask = MASK_START_CMD | MASK_MCRSPE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 911 | break; |
| 912 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 913 | |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 914 | if (host->ccs_enable) |
| 915 | mask |= MASK_MCCSTO; |
| 916 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 917 | if (mrq->data) { |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 918 | sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, 0); |
| 919 | sh_mmcif_writel(host->addr, MMCIF_CE_BLOCK_SET, |
| 920 | mrq->data->blksz); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 921 | } |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 922 | opc = sh_mmcif_set_cmd(host, mrq); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 923 | |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 924 | if (host->ccs_enable) |
| 925 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0); |
| 926 | else |
| 927 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, 0xD80430C0 | INT_CCS); |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 928 | sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, mask); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 929 | /* set arg */ |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 930 | sh_mmcif_writel(host->addr, MMCIF_CE_ARG, cmd->arg); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 931 | /* set cmd */ |
Kouichi Tomita | dbb42d9 | 2015-02-15 23:46:46 +0900 | [diff] [blame] | 932 | spin_lock_irqsave(&host->lock, flags); |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 933 | sh_mmcif_writel(host->addr, MMCIF_CE_CMD_SET, opc); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 934 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 935 | host->wait_for = MMCIF_WAIT_FOR_CMD; |
| 936 | schedule_delayed_work(&host->timeout_work, host->timeout); |
Kouichi Tomita | dbb42d9 | 2015-02-15 23:46:46 +0900 | [diff] [blame] | 937 | spin_unlock_irqrestore(&host->lock, flags); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | static void sh_mmcif_stop_cmd(struct sh_mmcif_host *host, |
Guennadi Liakhovetski | ee4b888 | 2011-12-14 19:31:52 +0100 | [diff] [blame] | 941 | struct mmc_request *mrq) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 942 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 943 | struct device *dev = sh_mmcif_host_to_dev(host); |
| 944 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 945 | switch (mrq->cmd->opcode) { |
| 946 | case MMC_READ_MULTIPLE_BLOCK: |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 947 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12DRE); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 948 | break; |
| 949 | case MMC_WRITE_MULTIPLE_BLOCK: |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 950 | sh_mmcif_bitset(host, MMCIF_CE_INT_MASK, MASK_MCMD12RBE); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 951 | break; |
| 952 | default: |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 953 | dev_err(dev, "unsupported stop cmd\n"); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 954 | mrq->stop->error = sh_mmcif_error_manage(host); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 955 | return; |
| 956 | } |
| 957 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 958 | host->wait_for = MMCIF_WAIT_FOR_STOP; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | static void sh_mmcif_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 962 | { |
| 963 | struct sh_mmcif_host *host = mmc_priv(mmc); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 964 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 965 | unsigned long flags; |
| 966 | |
| 967 | spin_lock_irqsave(&host->lock, flags); |
| 968 | if (host->state != STATE_IDLE) { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 969 | dev_dbg(dev, "%s() rejected, state %u\n", |
| 970 | __func__, host->state); |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 971 | spin_unlock_irqrestore(&host->lock, flags); |
| 972 | mrq->cmd->error = -EAGAIN; |
| 973 | mmc_request_done(mmc, mrq); |
| 974 | return; |
| 975 | } |
| 976 | |
| 977 | host->state = STATE_REQUEST; |
| 978 | spin_unlock_irqrestore(&host->lock, flags); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 979 | |
| 980 | switch (mrq->cmd->opcode) { |
| 981 | /* MMCIF does not support SD/SDIO command */ |
Laurent Pinchart | 7541ca9 | 2012-06-12 22:56:09 +0200 | [diff] [blame] | 982 | case MMC_SLEEP_AWAKE: /* = SD_IO_SEND_OP_COND (5) */ |
| 983 | case MMC_SEND_EXT_CSD: /* = SD_SEND_IF_COND (8) */ |
| 984 | if ((mrq->cmd->flags & MMC_CMD_MASK) != MMC_CMD_BCR) |
| 985 | break; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 986 | case MMC_APP_CMD: |
Teppei Kamijou | 92ff0c5 | 2012-12-12 15:38:05 +0100 | [diff] [blame] | 987 | case SD_IO_RW_DIRECT: |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 988 | host->state = STATE_IDLE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 989 | mrq->cmd->error = -ETIMEDOUT; |
| 990 | mmc_request_done(mmc, mrq); |
| 991 | return; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 992 | default: |
| 993 | break; |
| 994 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 995 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 996 | host->mrq = mrq; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 997 | |
| 998 | sh_mmcif_start_cmd(host, mrq); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
Kuninori Morimoto | 9bb09a3 | 2015-04-23 08:16:04 +0000 | [diff] [blame] | 1001 | static void sh_mmcif_clk_setup(struct sh_mmcif_host *host) |
Guennadi Liakhovetski | a660926 | 2012-04-19 18:02:50 +0200 | [diff] [blame] | 1002 | { |
Kuninori Morimoto | 9bb09a3 | 2015-04-23 08:16:04 +0000 | [diff] [blame] | 1003 | unsigned int clk = clk_get_rate(host->clk); |
Guennadi Liakhovetski | a660926 | 2012-04-19 18:02:50 +0200 | [diff] [blame] | 1004 | |
Kuninori Morimoto | 9bb09a3 | 2015-04-23 08:16:04 +0000 | [diff] [blame] | 1005 | host->mmc->f_max = clk / 2; |
| 1006 | host->mmc->f_min = clk / 512; |
Guennadi Liakhovetski | a660926 | 2012-04-19 18:02:50 +0200 | [diff] [blame] | 1007 | } |
| 1008 | |
Guennadi Liakhovetski | 7d17baa | 2012-04-20 18:27:13 +0200 | [diff] [blame] | 1009 | static void sh_mmcif_set_power(struct sh_mmcif_host *host, struct mmc_ios *ios) |
| 1010 | { |
Guennadi Liakhovetski | 7d17baa | 2012-04-20 18:27:13 +0200 | [diff] [blame] | 1011 | struct mmc_host *mmc = host->mmc; |
| 1012 | |
Guennadi Liakhovetski | 7d17baa | 2012-04-20 18:27:13 +0200 | [diff] [blame] | 1013 | if (!IS_ERR(mmc->supply.vmmc)) |
| 1014 | /* Errors ignored... */ |
| 1015 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, |
| 1016 | ios->power_mode ? ios->vdd : 0); |
| 1017 | } |
| 1018 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1019 | static void sh_mmcif_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1020 | { |
| 1021 | struct sh_mmcif_host *host = mmc_priv(mmc); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1022 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1023 | unsigned long flags; |
| 1024 | |
| 1025 | spin_lock_irqsave(&host->lock, flags); |
| 1026 | if (host->state != STATE_IDLE) { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1027 | dev_dbg(dev, "%s() rejected, state %u\n", |
| 1028 | __func__, host->state); |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1029 | spin_unlock_irqrestore(&host->lock, flags); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | host->state = STATE_IOS; |
| 1034 | spin_unlock_irqrestore(&host->lock, flags); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1035 | |
Guennadi Liakhovetski | f5e0cec | 2011-02-25 16:58:38 +0100 | [diff] [blame] | 1036 | if (ios->power_mode == MMC_POWER_UP) { |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 1037 | if (!host->card_present) { |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1038 | /* See if we also get DMA */ |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1039 | sh_mmcif_request_dma(host, dev->platform_data); |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 1040 | host->card_present = true; |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1041 | } |
Guennadi Liakhovetski | 7d17baa | 2012-04-20 18:27:13 +0200 | [diff] [blame] | 1042 | sh_mmcif_set_power(host, ios); |
Guennadi Liakhovetski | f5e0cec | 2011-02-25 16:58:38 +0100 | [diff] [blame] | 1043 | } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) { |
| 1044 | /* clock stop */ |
| 1045 | sh_mmcif_clock_control(host, 0); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1046 | if (ios->power_mode == MMC_POWER_OFF) { |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 1047 | if (host->card_present) { |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1048 | sh_mmcif_release_dma(host); |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 1049 | host->card_present = false; |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1050 | } |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 1051 | } |
| 1052 | if (host->power) { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1053 | pm_runtime_put_sync(dev); |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 1054 | clk_disable_unprepare(host->clk); |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 1055 | host->power = false; |
Guennadi Liakhovetski | 7d17baa | 2012-04-20 18:27:13 +0200 | [diff] [blame] | 1056 | if (ios->power_mode == MMC_POWER_OFF) |
| 1057 | sh_mmcif_set_power(host, ios); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1058 | } |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1059 | host->state = STATE_IDLE; |
Guennadi Liakhovetski | f5e0cec | 2011-02-25 16:58:38 +0100 | [diff] [blame] | 1060 | return; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 1063 | if (ios->clock) { |
| 1064 | if (!host->power) { |
Kuninori Morimoto | 9bb09a3 | 2015-04-23 08:16:04 +0000 | [diff] [blame] | 1065 | clk_prepare_enable(host->clk); |
| 1066 | |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1067 | pm_runtime_get_sync(dev); |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 1068 | host->power = true; |
| 1069 | sh_mmcif_sync_reset(host); |
| 1070 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1071 | sh_mmcif_clock_control(host, ios->clock); |
Guennadi Liakhovetski | c9b0cef | 2011-05-26 15:33:30 +0200 | [diff] [blame] | 1072 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1073 | |
Teppei Kamijou | 555061f | 2012-12-12 15:38:08 +0100 | [diff] [blame] | 1074 | host->timing = ios->timing; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1075 | host->bus_width = ios->bus_width; |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1076 | host->state = STATE_IDLE; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Arnd Hannemann | 777271d | 2010-08-24 17:27:01 +0200 | [diff] [blame] | 1079 | static int sh_mmcif_get_cd(struct mmc_host *mmc) |
| 1080 | { |
| 1081 | struct sh_mmcif_host *host = mmc_priv(mmc); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1082 | struct device *dev = sh_mmcif_host_to_dev(host); |
| 1083 | struct sh_mmcif_plat_data *p = dev->platform_data; |
Guennadi Liakhovetski | e480606 | 2012-06-14 14:24:35 +0200 | [diff] [blame] | 1084 | int ret = mmc_gpio_get_cd(mmc); |
| 1085 | |
| 1086 | if (ret >= 0) |
| 1087 | return ret; |
Arnd Hannemann | 777271d | 2010-08-24 17:27:01 +0200 | [diff] [blame] | 1088 | |
Guennadi Liakhovetski | bf68a81 | 2012-05-01 18:18:16 +0200 | [diff] [blame] | 1089 | if (!p || !p->get_cd) |
Arnd Hannemann | 777271d | 2010-08-24 17:27:01 +0200 | [diff] [blame] | 1090 | return -ENOSYS; |
| 1091 | else |
| 1092 | return p->get_cd(host->pd); |
| 1093 | } |
| 1094 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1095 | static struct mmc_host_ops sh_mmcif_ops = { |
| 1096 | .request = sh_mmcif_request, |
| 1097 | .set_ios = sh_mmcif_set_ios, |
Arnd Hannemann | 777271d | 2010-08-24 17:27:01 +0200 | [diff] [blame] | 1098 | .get_cd = sh_mmcif_get_cd, |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1099 | }; |
| 1100 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1101 | static bool sh_mmcif_end_cmd(struct sh_mmcif_host *host) |
| 1102 | { |
| 1103 | struct mmc_command *cmd = host->mrq->cmd; |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1104 | struct mmc_data *data = host->mrq->data; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1105 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1106 | long time; |
| 1107 | |
| 1108 | if (host->sd_error) { |
| 1109 | switch (cmd->opcode) { |
| 1110 | case MMC_ALL_SEND_CID: |
| 1111 | case MMC_SELECT_CARD: |
| 1112 | case MMC_APP_CMD: |
| 1113 | cmd->error = -ETIMEDOUT; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1114 | break; |
| 1115 | default: |
| 1116 | cmd->error = sh_mmcif_error_manage(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1117 | break; |
| 1118 | } |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1119 | dev_dbg(dev, "CMD%d error %d\n", |
Teppei Kamijou | e475b27 | 2012-12-12 15:38:18 +0100 | [diff] [blame] | 1120 | cmd->opcode, cmd->error); |
Guennadi Liakhovetski | aba9d64 | 2012-12-12 15:38:15 +0100 | [diff] [blame] | 1121 | host->sd_error = false; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1122 | return false; |
| 1123 | } |
| 1124 | if (!(cmd->flags & MMC_RSP_PRESENT)) { |
| 1125 | cmd->error = 0; |
| 1126 | return false; |
| 1127 | } |
| 1128 | |
| 1129 | sh_mmcif_get_response(host, cmd); |
| 1130 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1131 | if (!data) |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1132 | return false; |
| 1133 | |
Guennadi Liakhovetski | 90f1cb4 | 2012-12-12 15:38:16 +0100 | [diff] [blame] | 1134 | /* |
| 1135 | * Completion can be signalled from DMA callback and error, so, have to |
| 1136 | * reset here, before setting .dma_active |
| 1137 | */ |
| 1138 | init_completion(&host->dma_complete); |
| 1139 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1140 | if (data->flags & MMC_DATA_READ) { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1141 | if (host->chan_rx) |
| 1142 | sh_mmcif_start_dma_rx(host); |
| 1143 | } else { |
| 1144 | if (host->chan_tx) |
| 1145 | sh_mmcif_start_dma_tx(host); |
| 1146 | } |
| 1147 | |
| 1148 | if (!host->dma_active) { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1149 | data->error = sh_mmcif_data_trans(host, host->mrq, cmd->opcode); |
Guennadi Liakhovetski | 99eb9d8 | 2012-12-12 15:38:13 +0100 | [diff] [blame] | 1150 | return !data->error; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /* Running in the IRQ thread, can sleep */ |
| 1154 | time = wait_for_completion_interruptible_timeout(&host->dma_complete, |
| 1155 | host->timeout); |
Teppei Kamijou | eae3098 | 2012-12-12 15:38:12 +0100 | [diff] [blame] | 1156 | |
| 1157 | if (data->flags & MMC_DATA_READ) |
| 1158 | dma_unmap_sg(host->chan_rx->device->dev, |
| 1159 | data->sg, data->sg_len, |
| 1160 | DMA_FROM_DEVICE); |
| 1161 | else |
| 1162 | dma_unmap_sg(host->chan_tx->device->dev, |
| 1163 | data->sg, data->sg_len, |
| 1164 | DMA_TO_DEVICE); |
| 1165 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1166 | if (host->sd_error) { |
| 1167 | dev_err(host->mmc->parent, |
| 1168 | "Error IRQ while waiting for DMA completion!\n"); |
| 1169 | /* Woken up by an error IRQ: abort DMA */ |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1170 | data->error = sh_mmcif_error_manage(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1171 | } else if (!time) { |
Teppei Kamijou | e475b27 | 2012-12-12 15:38:18 +0100 | [diff] [blame] | 1172 | dev_err(host->mmc->parent, "DMA timeout!\n"); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1173 | data->error = -ETIMEDOUT; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1174 | } else if (time < 0) { |
Teppei Kamijou | e475b27 | 2012-12-12 15:38:18 +0100 | [diff] [blame] | 1175 | dev_err(host->mmc->parent, |
| 1176 | "wait_for_completion_...() error %ld!\n", time); |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1177 | data->error = time; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1178 | } |
| 1179 | sh_mmcif_bitclr(host, MMCIF_CE_BUF_ACC, |
| 1180 | BUF_ACC_DMAREN | BUF_ACC_DMAWEN); |
| 1181 | host->dma_active = false; |
| 1182 | |
Teppei Kamijou | eae3098 | 2012-12-12 15:38:12 +0100 | [diff] [blame] | 1183 | if (data->error) { |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1184 | data->bytes_xfered = 0; |
Teppei Kamijou | eae3098 | 2012-12-12 15:38:12 +0100 | [diff] [blame] | 1185 | /* Abort DMA */ |
| 1186 | if (data->flags & MMC_DATA_READ) |
| 1187 | dmaengine_terminate_all(host->chan_rx); |
| 1188 | else |
| 1189 | dmaengine_terminate_all(host->chan_tx); |
| 1190 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1191 | |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
| 1195 | static irqreturn_t sh_mmcif_irqt(int irq, void *dev_id) |
| 1196 | { |
| 1197 | struct sh_mmcif_host *host = dev_id; |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1198 | struct mmc_request *mrq; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1199 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1200 | bool wait = false; |
Kouichi Tomita | dbb42d9 | 2015-02-15 23:46:46 +0900 | [diff] [blame] | 1201 | unsigned long flags; |
| 1202 | int wait_work; |
| 1203 | |
| 1204 | spin_lock_irqsave(&host->lock, flags); |
| 1205 | wait_work = host->wait_for; |
| 1206 | spin_unlock_irqrestore(&host->lock, flags); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1207 | |
| 1208 | cancel_delayed_work_sync(&host->timeout_work); |
| 1209 | |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1210 | mutex_lock(&host->thread_lock); |
| 1211 | |
| 1212 | mrq = host->mrq; |
| 1213 | if (!mrq) { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1214 | dev_dbg(dev, "IRQ thread state %u, wait %u: NULL mrq!\n", |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1215 | host->state, host->wait_for); |
| 1216 | mutex_unlock(&host->thread_lock); |
| 1217 | return IRQ_HANDLED; |
| 1218 | } |
| 1219 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1220 | /* |
| 1221 | * All handlers return true, if processing continues, and false, if the |
| 1222 | * request has to be completed - successfully or not |
| 1223 | */ |
Kouichi Tomita | dbb42d9 | 2015-02-15 23:46:46 +0900 | [diff] [blame] | 1224 | switch (wait_work) { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1225 | case MMCIF_WAIT_FOR_REQUEST: |
| 1226 | /* We're too late, the timeout has already kicked in */ |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1227 | mutex_unlock(&host->thread_lock); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1228 | return IRQ_HANDLED; |
| 1229 | case MMCIF_WAIT_FOR_CMD: |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1230 | /* Wait for data? */ |
| 1231 | wait = sh_mmcif_end_cmd(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1232 | break; |
| 1233 | case MMCIF_WAIT_FOR_MREAD: |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1234 | /* Wait for more data? */ |
| 1235 | wait = sh_mmcif_mread_block(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1236 | break; |
| 1237 | case MMCIF_WAIT_FOR_READ: |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1238 | /* Wait for data end? */ |
| 1239 | wait = sh_mmcif_read_block(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1240 | break; |
| 1241 | case MMCIF_WAIT_FOR_MWRITE: |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1242 | /* Wait data to write? */ |
| 1243 | wait = sh_mmcif_mwrite_block(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1244 | break; |
| 1245 | case MMCIF_WAIT_FOR_WRITE: |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1246 | /* Wait for data end? */ |
| 1247 | wait = sh_mmcif_write_block(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1248 | break; |
| 1249 | case MMCIF_WAIT_FOR_STOP: |
| 1250 | if (host->sd_error) { |
| 1251 | mrq->stop->error = sh_mmcif_error_manage(host); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1252 | dev_dbg(dev, "%s(): %d\n", __func__, mrq->stop->error); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1253 | break; |
| 1254 | } |
| 1255 | sh_mmcif_get_cmd12response(host, mrq->stop); |
| 1256 | mrq->stop->error = 0; |
| 1257 | break; |
| 1258 | case MMCIF_WAIT_FOR_READ_END: |
| 1259 | case MMCIF_WAIT_FOR_WRITE_END: |
Teppei Kamijou | e475b27 | 2012-12-12 15:38:18 +0100 | [diff] [blame] | 1260 | if (host->sd_error) { |
Guennadi Liakhovetski | 91ab252 | 2012-08-22 06:49:47 +0000 | [diff] [blame] | 1261 | mrq->data->error = sh_mmcif_error_manage(host); |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1262 | dev_dbg(dev, "%s(): %d\n", __func__, mrq->data->error); |
Teppei Kamijou | e475b27 | 2012-12-12 15:38:18 +0100 | [diff] [blame] | 1263 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1264 | break; |
| 1265 | default: |
| 1266 | BUG(); |
| 1267 | } |
| 1268 | |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1269 | if (wait) { |
| 1270 | schedule_delayed_work(&host->timeout_work, host->timeout); |
| 1271 | /* Wait for more data */ |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1272 | mutex_unlock(&host->thread_lock); |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1273 | return IRQ_HANDLED; |
| 1274 | } |
| 1275 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1276 | if (host->wait_for != MMCIF_WAIT_FOR_STOP) { |
Guennadi Liakhovetski | 91ab252 | 2012-08-22 06:49:47 +0000 | [diff] [blame] | 1277 | struct mmc_data *data = mrq->data; |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1278 | if (!mrq->cmd->error && data && !data->error) |
| 1279 | data->bytes_xfered = |
| 1280 | data->blocks * data->blksz; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1281 | |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1282 | if (mrq->stop && !mrq->cmd->error && (!data || !data->error)) { |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1283 | sh_mmcif_stop_cmd(host, mrq); |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1284 | if (!mrq->stop->error) { |
| 1285 | schedule_delayed_work(&host->timeout_work, host->timeout); |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1286 | mutex_unlock(&host->thread_lock); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1287 | return IRQ_HANDLED; |
Guennadi Liakhovetski | 5df460b | 2012-12-12 15:38:11 +0100 | [diff] [blame] | 1288 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | host->wait_for = MMCIF_WAIT_FOR_REQUEST; |
| 1293 | host->state = STATE_IDLE; |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1294 | host->mrq = NULL; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1295 | mmc_request_done(host->mmc, mrq); |
| 1296 | |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1297 | mutex_unlock(&host->thread_lock); |
| 1298 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1299 | return IRQ_HANDLED; |
| 1300 | } |
| 1301 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1302 | static irqreturn_t sh_mmcif_intr(int irq, void *dev_id) |
| 1303 | { |
| 1304 | struct sh_mmcif_host *host = dev_id; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1305 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 1306 | u32 state, mask; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1307 | |
Magnus Damm | 487d9fc | 2010-05-18 14:42:51 +0000 | [diff] [blame] | 1308 | state = sh_mmcif_readl(host->addr, MMCIF_CE_INT); |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 1309 | mask = sh_mmcif_readl(host->addr, MMCIF_CE_INT_MASK); |
| 1310 | if (host->ccs_enable) |
| 1311 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, ~(state & mask)); |
| 1312 | else |
| 1313 | sh_mmcif_writel(host->addr, MMCIF_CE_INT, INT_CCS | ~(state & mask)); |
Guennadi Liakhovetski | 8af5075 | 2012-12-12 15:45:14 +0100 | [diff] [blame] | 1314 | sh_mmcif_bitclr(host, MMCIF_CE_INT_MASK, state & MASK_CLEAN); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1315 | |
Guennadi Liakhovetski | 8af5075 | 2012-12-12 15:45:14 +0100 | [diff] [blame] | 1316 | if (state & ~MASK_CLEAN) |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1317 | dev_dbg(dev, "IRQ state = 0x%08x incompletely cleared\n", |
Guennadi Liakhovetski | 8af5075 | 2012-12-12 15:45:14 +0100 | [diff] [blame] | 1318 | state); |
| 1319 | |
| 1320 | if (state & INT_ERR_STS || state & ~INT_ALL) { |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1321 | host->sd_error = true; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1322 | dev_dbg(dev, "int err state = 0x%08x\n", state); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1323 | } |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1324 | if (state & ~(INT_CMD12RBE | INT_CMD12CRE)) { |
Guennadi Liakhovetski | 8af5075 | 2012-12-12 15:45:14 +0100 | [diff] [blame] | 1325 | if (!host->mrq) |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1326 | dev_dbg(dev, "NULL IRQ state = 0x%08x\n", state); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1327 | if (!host->dma_active) |
| 1328 | return IRQ_WAKE_THREAD; |
| 1329 | else if (host->sd_error) |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 1330 | sh_mmcif_dma_complete(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1331 | } else { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1332 | dev_dbg(dev, "Unexpected IRQ 0x%x\n", state); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1333 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1334 | |
| 1335 | return IRQ_HANDLED; |
| 1336 | } |
| 1337 | |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 1338 | static void sh_mmcif_timeout_work(struct work_struct *work) |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1339 | { |
| 1340 | struct delayed_work *d = container_of(work, struct delayed_work, work); |
| 1341 | struct sh_mmcif_host *host = container_of(d, struct sh_mmcif_host, timeout_work); |
| 1342 | struct mmc_request *mrq = host->mrq; |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1343 | struct device *dev = sh_mmcif_host_to_dev(host); |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1344 | unsigned long flags; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1345 | |
| 1346 | if (host->dying) |
| 1347 | /* Don't run after mmc_remove_host() */ |
| 1348 | return; |
| 1349 | |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1350 | spin_lock_irqsave(&host->lock, flags); |
| 1351 | if (host->state == STATE_IDLE) { |
| 1352 | spin_unlock_irqrestore(&host->lock, flags); |
| 1353 | return; |
| 1354 | } |
| 1355 | |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1356 | dev_err(dev, "Timeout waiting for %u on CMD%u\n", |
Kouichi Tomita | 4cbd522 | 2015-02-15 23:46:47 +0900 | [diff] [blame] | 1357 | host->wait_for, mrq->cmd->opcode); |
| 1358 | |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1359 | host->state = STATE_TIMEOUT; |
| 1360 | spin_unlock_irqrestore(&host->lock, flags); |
| 1361 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1362 | /* |
| 1363 | * Handle races with cancel_delayed_work(), unless |
| 1364 | * cancel_delayed_work_sync() is used |
| 1365 | */ |
| 1366 | switch (host->wait_for) { |
| 1367 | case MMCIF_WAIT_FOR_CMD: |
| 1368 | mrq->cmd->error = sh_mmcif_error_manage(host); |
| 1369 | break; |
| 1370 | case MMCIF_WAIT_FOR_STOP: |
| 1371 | mrq->stop->error = sh_mmcif_error_manage(host); |
| 1372 | break; |
| 1373 | case MMCIF_WAIT_FOR_MREAD: |
| 1374 | case MMCIF_WAIT_FOR_MWRITE: |
| 1375 | case MMCIF_WAIT_FOR_READ: |
| 1376 | case MMCIF_WAIT_FOR_WRITE: |
| 1377 | case MMCIF_WAIT_FOR_READ_END: |
| 1378 | case MMCIF_WAIT_FOR_WRITE_END: |
Guennadi Liakhovetski | 6998340 | 2011-12-26 12:52:13 -0500 | [diff] [blame] | 1379 | mrq->data->error = sh_mmcif_error_manage(host); |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1380 | break; |
| 1381 | default: |
| 1382 | BUG(); |
| 1383 | } |
| 1384 | |
| 1385 | host->state = STATE_IDLE; |
| 1386 | host->wait_for = MMCIF_WAIT_FOR_REQUEST; |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1387 | host->mrq = NULL; |
| 1388 | mmc_request_done(host->mmc, mrq); |
| 1389 | } |
| 1390 | |
Guennadi Liakhovetski | 7d17baa | 2012-04-20 18:27:13 +0200 | [diff] [blame] | 1391 | static void sh_mmcif_init_ocr(struct sh_mmcif_host *host) |
| 1392 | { |
Kuninori Morimoto | 585c3a5 | 2015-05-14 07:21:18 +0000 | [diff] [blame] | 1393 | struct device *dev = sh_mmcif_host_to_dev(host); |
| 1394 | struct sh_mmcif_plat_data *pd = dev->platform_data; |
Guennadi Liakhovetski | 7d17baa | 2012-04-20 18:27:13 +0200 | [diff] [blame] | 1395 | struct mmc_host *mmc = host->mmc; |
| 1396 | |
| 1397 | mmc_regulator_get_supply(mmc); |
| 1398 | |
Guennadi Liakhovetski | bf68a81 | 2012-05-01 18:18:16 +0200 | [diff] [blame] | 1399 | if (!pd) |
| 1400 | return; |
| 1401 | |
Guennadi Liakhovetski | 7d17baa | 2012-04-20 18:27:13 +0200 | [diff] [blame] | 1402 | if (!mmc->ocr_avail) |
| 1403 | mmc->ocr_avail = pd->ocr; |
| 1404 | else if (pd->ocr) |
| 1405 | dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n"); |
| 1406 | } |
| 1407 | |
Bill Pemberton | c3be1ef | 2012-11-19 13:23:06 -0500 | [diff] [blame] | 1408 | static int sh_mmcif_probe(struct platform_device *pdev) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1409 | { |
| 1410 | int ret = 0, irq[2]; |
| 1411 | struct mmc_host *mmc; |
Guennadi Liakhovetski | e47bf32 | 2010-11-24 10:05:18 +0000 | [diff] [blame] | 1412 | struct sh_mmcif_host *host; |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1413 | struct device *dev = &pdev->dev; |
| 1414 | struct sh_mmcif_plat_data *pd = dev->platform_data; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1415 | struct resource *res; |
| 1416 | void __iomem *reg; |
Shinya Kuribayashi | 2cd5b3e | 2013-01-14 14:12:36 -0500 | [diff] [blame] | 1417 | const char *name; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1418 | |
| 1419 | irq[0] = platform_get_irq(pdev, 0); |
| 1420 | irq[1] = platform_get_irq(pdev, 1); |
Shinya Kuribayashi | 2cd5b3e | 2013-01-14 14:12:36 -0500 | [diff] [blame] | 1421 | if (irq[0] < 0) { |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1422 | dev_err(dev, "Get irq error\n"); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1423 | return -ENXIO; |
| 1424 | } |
Ben Dooks | 18f55fc | 2014-06-04 12:42:09 +0100 | [diff] [blame] | 1425 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1426 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1427 | reg = devm_ioremap_resource(dev, res); |
Ben Dooks | 18f55fc | 2014-06-04 12:42:09 +0100 | [diff] [blame] | 1428 | if (IS_ERR(reg)) |
| 1429 | return PTR_ERR(reg); |
Guennadi Liakhovetski | e1aae2e | 2012-04-19 16:15:52 +0200 | [diff] [blame] | 1430 | |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1431 | mmc = mmc_alloc_host(sizeof(struct sh_mmcif_host), dev); |
Ben Dooks | 18f55fc | 2014-06-04 12:42:09 +0100 | [diff] [blame] | 1432 | if (!mmc) |
| 1433 | return -ENOMEM; |
Simon Baatz | 2c9054d | 2013-06-09 22:14:12 +0200 | [diff] [blame] | 1434 | |
| 1435 | ret = mmc_of_parse(mmc); |
| 1436 | if (ret < 0) |
Ben Dooks | 4699100 | 2014-06-04 12:42:10 +0100 | [diff] [blame] | 1437 | goto err_host; |
Simon Baatz | 2c9054d | 2013-06-09 22:14:12 +0200 | [diff] [blame] | 1438 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1439 | host = mmc_priv(mmc); |
| 1440 | host->mmc = mmc; |
| 1441 | host->addr = reg; |
Takeshi Kihara | bad4371 | 2015-04-30 02:03:51 +0900 | [diff] [blame] | 1442 | host->timeout = msecs_to_jiffies(10000); |
Guennadi Liakhovetski | 967bcb7 | 2013-07-10 21:21:12 +0200 | [diff] [blame] | 1443 | host->ccs_enable = !pd || !pd->ccs_unsupported; |
Guennadi Liakhovetski | 6d6fd36 | 2013-07-10 21:21:13 +0200 | [diff] [blame] | 1444 | host->clk_ctrl2_enable = pd && pd->clk_ctrl2_present; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1445 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1446 | host->pd = pdev; |
| 1447 | |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1448 | spin_lock_init(&host->lock); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1449 | |
| 1450 | mmc->ops = &sh_mmcif_ops; |
Guennadi Liakhovetski | 7d17baa | 2012-04-20 18:27:13 +0200 | [diff] [blame] | 1451 | sh_mmcif_init_ocr(host); |
| 1452 | |
Guennadi Liakhovetski | eca889f | 2013-02-15 16:13:54 +0100 | [diff] [blame] | 1453 | mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_WAIT_WHILE_BUSY; |
Guennadi Liakhovetski | bf68a81 | 2012-05-01 18:18:16 +0200 | [diff] [blame] | 1454 | if (pd && pd->caps) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1455 | mmc->caps |= pd->caps; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 1456 | mmc->max_segs = 32; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1457 | mmc->max_blk_size = 512; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 1458 | mmc->max_req_size = PAGE_CACHE_SIZE * mmc->max_segs; |
| 1459 | mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1460 | mmc->max_seg_size = mmc->max_req_size; |
| 1461 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1462 | platform_set_drvdata(pdev, host); |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 1463 | |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1464 | pm_runtime_enable(dev); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1465 | host->power = false; |
| 1466 | |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 1467 | host->clk = devm_clk_get(dev, NULL); |
| 1468 | if (IS_ERR(host->clk)) { |
| 1469 | ret = PTR_ERR(host->clk); |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1470 | dev_err(dev, "cannot get clock: %d\n", ret); |
Ben Dooks | 4699100 | 2014-06-04 12:42:10 +0100 | [diff] [blame] | 1471 | goto err_pm; |
Guennadi Liakhovetski | b289174 | 2012-04-19 18:02:05 +0200 | [diff] [blame] | 1472 | } |
Kuninori Morimoto | 9bb09a3 | 2015-04-23 08:16:04 +0000 | [diff] [blame] | 1473 | |
| 1474 | ret = clk_prepare_enable(host->clk); |
Guennadi Liakhovetski | a660926 | 2012-04-19 18:02:50 +0200 | [diff] [blame] | 1475 | if (ret < 0) |
Ben Dooks | 4699100 | 2014-06-04 12:42:10 +0100 | [diff] [blame] | 1476 | goto err_pm; |
Guennadi Liakhovetski | b289174 | 2012-04-19 18:02:05 +0200 | [diff] [blame] | 1477 | |
Kuninori Morimoto | 9bb09a3 | 2015-04-23 08:16:04 +0000 | [diff] [blame] | 1478 | sh_mmcif_clk_setup(host); |
| 1479 | |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1480 | ret = pm_runtime_resume(dev); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1481 | if (ret < 0) |
Ben Dooks | 4699100 | 2014-06-04 12:42:10 +0100 | [diff] [blame] | 1482 | goto err_clk; |
Guennadi Liakhovetski | a782d68 | 2010-11-24 10:05:22 +0000 | [diff] [blame] | 1483 | |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 1484 | INIT_DELAYED_WORK(&host->timeout_work, sh_mmcif_timeout_work); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1485 | |
Guennadi Liakhovetski | b289174 | 2012-04-19 18:02:05 +0200 | [diff] [blame] | 1486 | sh_mmcif_sync_reset(host); |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1487 | sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL); |
| 1488 | |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1489 | name = irq[1] < 0 ? dev_name(dev) : "sh_mmc:error"; |
| 1490 | ret = devm_request_threaded_irq(dev, irq[0], sh_mmcif_intr, |
Ben Dooks | 6f4789e | 2014-06-04 12:42:11 +0100 | [diff] [blame] | 1491 | sh_mmcif_irqt, 0, name, host); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1492 | if (ret) { |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1493 | dev_err(dev, "request_irq error (%s)\n", name); |
Ben Dooks | 11a8085 | 2014-06-04 12:42:12 +0100 | [diff] [blame] | 1494 | goto err_clk; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1495 | } |
Shinya Kuribayashi | 2cd5b3e | 2013-01-14 14:12:36 -0500 | [diff] [blame] | 1496 | if (irq[1] >= 0) { |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1497 | ret = devm_request_threaded_irq(dev, irq[1], |
Ben Dooks | 6f4789e | 2014-06-04 12:42:11 +0100 | [diff] [blame] | 1498 | sh_mmcif_intr, sh_mmcif_irqt, |
| 1499 | 0, "sh_mmc:int", host); |
Shinya Kuribayashi | 2cd5b3e | 2013-01-14 14:12:36 -0500 | [diff] [blame] | 1500 | if (ret) { |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1501 | dev_err(dev, "request_irq error (sh_mmc:int)\n"); |
Ben Dooks | 11a8085 | 2014-06-04 12:42:12 +0100 | [diff] [blame] | 1502 | goto err_clk; |
Shinya Kuribayashi | 2cd5b3e | 2013-01-14 14:12:36 -0500 | [diff] [blame] | 1503 | } |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
Guennadi Liakhovetski | e480606 | 2012-06-14 14:24:35 +0200 | [diff] [blame] | 1506 | if (pd && pd->use_cd_gpio) { |
Laurent Pinchart | 214fc30 | 2013-08-08 12:38:31 +0200 | [diff] [blame] | 1507 | ret = mmc_gpio_request_cd(mmc, pd->cd_gpio, 0); |
Guennadi Liakhovetski | e480606 | 2012-06-14 14:24:35 +0200 | [diff] [blame] | 1508 | if (ret < 0) |
Ben Dooks | 7f67f3a | 2014-06-04 12:42:13 +0100 | [diff] [blame] | 1509 | goto err_clk; |
Guennadi Liakhovetski | e480606 | 2012-06-14 14:24:35 +0200 | [diff] [blame] | 1510 | } |
| 1511 | |
Guennadi Liakhovetski | 8047310 | 2012-12-12 15:38:14 +0100 | [diff] [blame] | 1512 | mutex_init(&host->thread_lock); |
| 1513 | |
Guennadi Liakhovetski | 5ba85d9 | 2012-01-21 00:41:28 +0100 | [diff] [blame] | 1514 | ret = mmc_add_host(mmc); |
| 1515 | if (ret < 0) |
Ben Dooks | 7f67f3a | 2014-06-04 12:42:13 +0100 | [diff] [blame] | 1516 | goto err_clk; |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1517 | |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1518 | dev_pm_qos_expose_latency_limit(dev, 100); |
Rafael J. Wysocki | efe6a8a | 2012-03-13 01:02:15 +0100 | [diff] [blame] | 1519 | |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1520 | dev_info(dev, "Chip version 0x%04x, clock rate %luMHz\n", |
Ben Dooks | ce7eb68 | 2014-06-04 12:42:08 +0100 | [diff] [blame] | 1521 | sh_mmcif_readl(host->addr, MMCIF_CE_VERSION) & 0xffff, |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 1522 | clk_get_rate(host->clk) / 1000000UL); |
Ben Dooks | ce7eb68 | 2014-06-04 12:42:08 +0100 | [diff] [blame] | 1523 | |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 1524 | clk_disable_unprepare(host->clk); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1525 | return ret; |
| 1526 | |
Ben Dooks | 4699100 | 2014-06-04 12:42:10 +0100 | [diff] [blame] | 1527 | err_clk: |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 1528 | clk_disable_unprepare(host->clk); |
Ben Dooks | 4699100 | 2014-06-04 12:42:10 +0100 | [diff] [blame] | 1529 | err_pm: |
Kuninori Morimoto | 60985c3 | 2015-04-23 08:14:12 +0000 | [diff] [blame] | 1530 | pm_runtime_disable(dev); |
Ben Dooks | 4699100 | 2014-06-04 12:42:10 +0100 | [diff] [blame] | 1531 | err_host: |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1532 | mmc_free_host(mmc); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1533 | return ret; |
| 1534 | } |
| 1535 | |
Bill Pemberton | 6e0ee71 | 2012-11-19 13:26:03 -0500 | [diff] [blame] | 1536 | static int sh_mmcif_remove(struct platform_device *pdev) |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1537 | { |
| 1538 | struct sh_mmcif_host *host = platform_get_drvdata(pdev); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1539 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1540 | host->dying = true; |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 1541 | clk_prepare_enable(host->clk); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1542 | pm_runtime_get_sync(&pdev->dev); |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1543 | |
Rafael J. Wysocki | efe6a8a | 2012-03-13 01:02:15 +0100 | [diff] [blame] | 1544 | dev_pm_qos_hide_latency_limit(&pdev->dev); |
| 1545 | |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1546 | mmc_remove_host(host->mmc); |
Guennadi Liakhovetski | 3b0beaf | 2011-04-15 18:30:47 +0000 | [diff] [blame] | 1547 | sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL); |
| 1548 | |
Guennadi Liakhovetski | f985da1 | 2011-12-25 21:07:52 +0100 | [diff] [blame] | 1549 | /* |
| 1550 | * FIXME: cancel_delayed_work(_sync)() and free_irq() race with the |
| 1551 | * mmc_remove_host() call above. But swapping order doesn't help either |
| 1552 | * (a query on the linux-mmc mailing list didn't bring any replies). |
| 1553 | */ |
| 1554 | cancel_delayed_work_sync(&host->timeout_work); |
| 1555 | |
Kuninori Morimoto | 6aed678 | 2015-04-23 08:15:08 +0000 | [diff] [blame] | 1556 | clk_disable_unprepare(host->clk); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1557 | mmc_free_host(host->mmc); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1558 | pm_runtime_put_sync(&pdev->dev); |
| 1559 | pm_runtime_disable(&pdev->dev); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1560 | |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
Ulf Hansson | 51129f3 | 2013-10-01 14:01:46 +0200 | [diff] [blame] | 1564 | #ifdef CONFIG_PM_SLEEP |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1565 | static int sh_mmcif_suspend(struct device *dev) |
| 1566 | { |
Guennadi Liakhovetski | b289174 | 2012-04-19 18:02:05 +0200 | [diff] [blame] | 1567 | struct sh_mmcif_host *host = dev_get_drvdata(dev); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1568 | |
Ulf Hansson | cb3ca1a | 2013-09-26 10:19:09 +0200 | [diff] [blame] | 1569 | sh_mmcif_writel(host->addr, MMCIF_CE_INT_MASK, MASK_ALL); |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1570 | |
Ulf Hansson | cb3ca1a | 2013-09-26 10:19:09 +0200 | [diff] [blame] | 1571 | return 0; |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | static int sh_mmcif_resume(struct device *dev) |
| 1575 | { |
Ulf Hansson | cb3ca1a | 2013-09-26 10:19:09 +0200 | [diff] [blame] | 1576 | return 0; |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1577 | } |
Ulf Hansson | 51129f3 | 2013-10-01 14:01:46 +0200 | [diff] [blame] | 1578 | #endif |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1579 | |
| 1580 | static const struct dev_pm_ops sh_mmcif_dev_pm_ops = { |
Ulf Hansson | 51129f3 | 2013-10-01 14:01:46 +0200 | [diff] [blame] | 1581 | SET_SYSTEM_SLEEP_PM_OPS(sh_mmcif_suspend, sh_mmcif_resume) |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1582 | }; |
| 1583 | |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1584 | static struct platform_driver sh_mmcif_driver = { |
| 1585 | .probe = sh_mmcif_probe, |
| 1586 | .remove = sh_mmcif_remove, |
| 1587 | .driver = { |
| 1588 | .name = DRIVER_NAME, |
Guennadi Liakhovetski | faca664 | 2011-05-05 16:20:48 +0000 | [diff] [blame] | 1589 | .pm = &sh_mmcif_dev_pm_ops, |
Kuninori Morimoto | 1b1a694 | 2015-05-14 07:21:36 +0000 | [diff] [blame^] | 1590 | .of_match_table = sh_mmcif_of_match, |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1591 | }, |
| 1592 | }; |
| 1593 | |
Axel Lin | d1f81a6 | 2011-11-26 12:55:43 +0800 | [diff] [blame] | 1594 | module_platform_driver(sh_mmcif_driver); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1595 | |
| 1596 | MODULE_DESCRIPTION("SuperH on-chip MMC/eMMC interface driver"); |
| 1597 | MODULE_LICENSE("GPL"); |
Guennadi Liakhovetski | aa0787a | 2010-11-24 10:05:12 +0000 | [diff] [blame] | 1598 | MODULE_ALIAS("platform:" DRIVER_NAME); |
Yusuke Goda | fdc50a9 | 2010-05-26 14:41:59 -0700 | [diff] [blame] | 1599 | MODULE_AUTHOR("Yusuke Goda <yusuke.goda.sx@renesas.com>"); |