jz4740_mmc.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. /*
  2. * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
  3. * JZ4740 SD/MMC controller driver
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License along
  11. * with this program; if not, write to the Free Software Foundation, Inc.,
  12. * 675 Mass Ave, Cambridge, MA 02139, USA.
  13. *
  14. */
  15. #include <linux/mmc/host.h>
  16. #include <linux/mmc/slot-gpio.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/delay.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/clk.h>
  26. #include <linux/bitops.h>
  27. #include <linux/gpio.h>
  28. #include <asm/mach-jz4740/gpio.h>
  29. #include <asm/cacheflush.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/dmaengine.h>
  32. #include <asm/mach-jz4740/dma.h>
  33. #include <asm/mach-jz4740/jz4740_mmc.h>
  34. #define JZ_REG_MMC_STRPCL 0x00
  35. #define JZ_REG_MMC_STATUS 0x04
  36. #define JZ_REG_MMC_CLKRT 0x08
  37. #define JZ_REG_MMC_CMDAT 0x0C
  38. #define JZ_REG_MMC_RESTO 0x10
  39. #define JZ_REG_MMC_RDTO 0x14
  40. #define JZ_REG_MMC_BLKLEN 0x18
  41. #define JZ_REG_MMC_NOB 0x1C
  42. #define JZ_REG_MMC_SNOB 0x20
  43. #define JZ_REG_MMC_IMASK 0x24
  44. #define JZ_REG_MMC_IREG 0x28
  45. #define JZ_REG_MMC_CMD 0x2C
  46. #define JZ_REG_MMC_ARG 0x30
  47. #define JZ_REG_MMC_RESP_FIFO 0x34
  48. #define JZ_REG_MMC_RXFIFO 0x38
  49. #define JZ_REG_MMC_TXFIFO 0x3C
  50. #define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
  51. #define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
  52. #define JZ_MMC_STRPCL_START_READWAIT BIT(5)
  53. #define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
  54. #define JZ_MMC_STRPCL_RESET BIT(3)
  55. #define JZ_MMC_STRPCL_START_OP BIT(2)
  56. #define JZ_MMC_STRPCL_CLOCK_CONTROL (BIT(1) | BIT(0))
  57. #define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
  58. #define JZ_MMC_STRPCL_CLOCK_START BIT(1)
  59. #define JZ_MMC_STATUS_IS_RESETTING BIT(15)
  60. #define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
  61. #define JZ_MMC_STATUS_PRG_DONE BIT(13)
  62. #define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
  63. #define JZ_MMC_STATUS_END_CMD_RES BIT(11)
  64. #define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
  65. #define JZ_MMC_STATUS_IS_READWAIT BIT(9)
  66. #define JZ_MMC_STATUS_CLK_EN BIT(8)
  67. #define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
  68. #define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
  69. #define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
  70. #define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
  71. #define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
  72. #define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
  73. #define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
  74. #define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
  75. #define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
  76. #define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
  77. #define JZ_MMC_CMDAT_IO_ABORT BIT(11)
  78. #define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
  79. #define JZ_MMC_CMDAT_DMA_EN BIT(8)
  80. #define JZ_MMC_CMDAT_INIT BIT(7)
  81. #define JZ_MMC_CMDAT_BUSY BIT(6)
  82. #define JZ_MMC_CMDAT_STREAM BIT(5)
  83. #define JZ_MMC_CMDAT_WRITE BIT(4)
  84. #define JZ_MMC_CMDAT_DATA_EN BIT(3)
  85. #define JZ_MMC_CMDAT_RESPONSE_FORMAT (BIT(2) | BIT(1) | BIT(0))
  86. #define JZ_MMC_CMDAT_RSP_R1 1
  87. #define JZ_MMC_CMDAT_RSP_R2 2
  88. #define JZ_MMC_CMDAT_RSP_R3 3
  89. #define JZ_MMC_IRQ_SDIO BIT(7)
  90. #define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
  91. #define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
  92. #define JZ_MMC_IRQ_END_CMD_RES BIT(2)
  93. #define JZ_MMC_IRQ_PRG_DONE BIT(1)
  94. #define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
  95. #define JZ_MMC_CLK_RATE 24000000
  96. enum jz4740_mmc_state {
  97. JZ4740_MMC_STATE_READ_RESPONSE,
  98. JZ4740_MMC_STATE_TRANSFER_DATA,
  99. JZ4740_MMC_STATE_SEND_STOP,
  100. JZ4740_MMC_STATE_DONE,
  101. };
  102. struct jz4740_mmc_host_next {
  103. int sg_len;
  104. s32 cookie;
  105. };
  106. struct jz4740_mmc_host {
  107. struct mmc_host *mmc;
  108. struct platform_device *pdev;
  109. struct jz4740_mmc_platform_data *pdata;
  110. struct clk *clk;
  111. int irq;
  112. int card_detect_irq;
  113. void __iomem *base;
  114. struct resource *mem_res;
  115. struct mmc_request *req;
  116. struct mmc_command *cmd;
  117. unsigned long waiting;
  118. uint32_t cmdat;
  119. uint16_t irq_mask;
  120. spinlock_t lock;
  121. struct timer_list timeout_timer;
  122. struct sg_mapping_iter miter;
  123. enum jz4740_mmc_state state;
  124. /* DMA support */
  125. struct dma_chan *dma_rx;
  126. struct dma_chan *dma_tx;
  127. struct jz4740_mmc_host_next next_data;
  128. bool use_dma;
  129. int sg_len;
  130. /* The DMA trigger level is 8 words, that is to say, the DMA read
  131. * trigger is when data words in MSC_RXFIFO is >= 8 and the DMA write
  132. * trigger is when data words in MSC_TXFIFO is < 8.
  133. */
  134. #define JZ4740_MMC_FIFO_HALF_SIZE 8
  135. };
  136. /*----------------------------------------------------------------------------*/
  137. /* DMA infrastructure */
  138. static void jz4740_mmc_release_dma_channels(struct jz4740_mmc_host *host)
  139. {
  140. if (!host->use_dma)
  141. return;
  142. dma_release_channel(host->dma_tx);
  143. dma_release_channel(host->dma_rx);
  144. }
  145. static int jz4740_mmc_acquire_dma_channels(struct jz4740_mmc_host *host)
  146. {
  147. dma_cap_mask_t mask;
  148. dma_cap_zero(mask);
  149. dma_cap_set(DMA_SLAVE, mask);
  150. host->dma_tx = dma_request_channel(mask, NULL, host);
  151. if (!host->dma_tx) {
  152. dev_err(mmc_dev(host->mmc), "Failed to get dma_tx channel\n");
  153. return -ENODEV;
  154. }
  155. host->dma_rx = dma_request_channel(mask, NULL, host);
  156. if (!host->dma_rx) {
  157. dev_err(mmc_dev(host->mmc), "Failed to get dma_rx channel\n");
  158. goto free_master_write;
  159. }
  160. /* Initialize DMA pre request cookie */
  161. host->next_data.cookie = 1;
  162. return 0;
  163. free_master_write:
  164. dma_release_channel(host->dma_tx);
  165. return -ENODEV;
  166. }
  167. static inline int jz4740_mmc_get_dma_dir(struct mmc_data *data)
  168. {
  169. return (data->flags & MMC_DATA_READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  170. }
  171. static inline struct dma_chan *jz4740_mmc_get_dma_chan(struct jz4740_mmc_host *host,
  172. struct mmc_data *data)
  173. {
  174. return (data->flags & MMC_DATA_READ) ? host->dma_rx : host->dma_tx;
  175. }
  176. static void jz4740_mmc_dma_unmap(struct jz4740_mmc_host *host,
  177. struct mmc_data *data)
  178. {
  179. struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
  180. enum dma_data_direction dir = jz4740_mmc_get_dma_dir(data);
  181. dma_unmap_sg(chan->device->dev, data->sg, data->sg_len, dir);
  182. }
  183. /* Prepares DMA data for current/next transfer, returns non-zero on failure */
  184. static int jz4740_mmc_prepare_dma_data(struct jz4740_mmc_host *host,
  185. struct mmc_data *data,
  186. struct jz4740_mmc_host_next *next,
  187. struct dma_chan *chan)
  188. {
  189. struct jz4740_mmc_host_next *next_data = &host->next_data;
  190. enum dma_data_direction dir = jz4740_mmc_get_dma_dir(data);
  191. int sg_len;
  192. if (!next && data->host_cookie &&
  193. data->host_cookie != host->next_data.cookie) {
  194. dev_warn(mmc_dev(host->mmc),
  195. "[%s] invalid cookie: data->host_cookie %d host->next_data.cookie %d\n",
  196. __func__,
  197. data->host_cookie,
  198. host->next_data.cookie);
  199. data->host_cookie = 0;
  200. }
  201. /* Check if next job is already prepared */
  202. if (next || data->host_cookie != host->next_data.cookie) {
  203. sg_len = dma_map_sg(chan->device->dev,
  204. data->sg,
  205. data->sg_len,
  206. dir);
  207. } else {
  208. sg_len = next_data->sg_len;
  209. next_data->sg_len = 0;
  210. }
  211. if (sg_len <= 0) {
  212. dev_err(mmc_dev(host->mmc),
  213. "Failed to map scatterlist for DMA operation\n");
  214. return -EINVAL;
  215. }
  216. if (next) {
  217. next->sg_len = sg_len;
  218. data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
  219. } else
  220. host->sg_len = sg_len;
  221. return 0;
  222. }
  223. static int jz4740_mmc_start_dma_transfer(struct jz4740_mmc_host *host,
  224. struct mmc_data *data)
  225. {
  226. int ret;
  227. struct dma_chan *chan;
  228. struct dma_async_tx_descriptor *desc;
  229. struct dma_slave_config conf = {
  230. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  231. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  232. .src_maxburst = JZ4740_MMC_FIFO_HALF_SIZE,
  233. .dst_maxburst = JZ4740_MMC_FIFO_HALF_SIZE,
  234. };
  235. if (data->flags & MMC_DATA_WRITE) {
  236. conf.direction = DMA_MEM_TO_DEV;
  237. conf.dst_addr = host->mem_res->start + JZ_REG_MMC_TXFIFO;
  238. conf.slave_id = JZ4740_DMA_TYPE_MMC_TRANSMIT;
  239. chan = host->dma_tx;
  240. } else {
  241. conf.direction = DMA_DEV_TO_MEM;
  242. conf.src_addr = host->mem_res->start + JZ_REG_MMC_RXFIFO;
  243. conf.slave_id = JZ4740_DMA_TYPE_MMC_RECEIVE;
  244. chan = host->dma_rx;
  245. }
  246. ret = jz4740_mmc_prepare_dma_data(host, data, NULL, chan);
  247. if (ret)
  248. return ret;
  249. dmaengine_slave_config(chan, &conf);
  250. desc = dmaengine_prep_slave_sg(chan,
  251. data->sg,
  252. host->sg_len,
  253. conf.direction,
  254. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  255. if (!desc) {
  256. dev_err(mmc_dev(host->mmc),
  257. "Failed to allocate DMA %s descriptor",
  258. conf.direction == DMA_MEM_TO_DEV ? "TX" : "RX");
  259. goto dma_unmap;
  260. }
  261. dmaengine_submit(desc);
  262. dma_async_issue_pending(chan);
  263. return 0;
  264. dma_unmap:
  265. jz4740_mmc_dma_unmap(host, data);
  266. return -ENOMEM;
  267. }
  268. static void jz4740_mmc_pre_request(struct mmc_host *mmc,
  269. struct mmc_request *mrq,
  270. bool is_first_req)
  271. {
  272. struct jz4740_mmc_host *host = mmc_priv(mmc);
  273. struct mmc_data *data = mrq->data;
  274. struct jz4740_mmc_host_next *next_data = &host->next_data;
  275. BUG_ON(data->host_cookie);
  276. if (host->use_dma) {
  277. struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
  278. if (jz4740_mmc_prepare_dma_data(host, data, next_data, chan))
  279. data->host_cookie = 0;
  280. }
  281. }
  282. static void jz4740_mmc_post_request(struct mmc_host *mmc,
  283. struct mmc_request *mrq,
  284. int err)
  285. {
  286. struct jz4740_mmc_host *host = mmc_priv(mmc);
  287. struct mmc_data *data = mrq->data;
  288. if (host->use_dma && data->host_cookie) {
  289. jz4740_mmc_dma_unmap(host, data);
  290. data->host_cookie = 0;
  291. }
  292. if (err) {
  293. struct dma_chan *chan = jz4740_mmc_get_dma_chan(host, data);
  294. dmaengine_terminate_all(chan);
  295. }
  296. }
  297. /*----------------------------------------------------------------------------*/
  298. static void jz4740_mmc_set_irq_enabled(struct jz4740_mmc_host *host,
  299. unsigned int irq, bool enabled)
  300. {
  301. unsigned long flags;
  302. spin_lock_irqsave(&host->lock, flags);
  303. if (enabled)
  304. host->irq_mask &= ~irq;
  305. else
  306. host->irq_mask |= irq;
  307. writew(host->irq_mask, host->base + JZ_REG_MMC_IMASK);
  308. spin_unlock_irqrestore(&host->lock, flags);
  309. }
  310. static void jz4740_mmc_clock_enable(struct jz4740_mmc_host *host,
  311. bool start_transfer)
  312. {
  313. uint16_t val = JZ_MMC_STRPCL_CLOCK_START;
  314. if (start_transfer)
  315. val |= JZ_MMC_STRPCL_START_OP;
  316. writew(val, host->base + JZ_REG_MMC_STRPCL);
  317. }
  318. static void jz4740_mmc_clock_disable(struct jz4740_mmc_host *host)
  319. {
  320. uint32_t status;
  321. unsigned int timeout = 1000;
  322. writew(JZ_MMC_STRPCL_CLOCK_STOP, host->base + JZ_REG_MMC_STRPCL);
  323. do {
  324. status = readl(host->base + JZ_REG_MMC_STATUS);
  325. } while (status & JZ_MMC_STATUS_CLK_EN && --timeout);
  326. }
  327. static void jz4740_mmc_reset(struct jz4740_mmc_host *host)
  328. {
  329. uint32_t status;
  330. unsigned int timeout = 1000;
  331. writew(JZ_MMC_STRPCL_RESET, host->base + JZ_REG_MMC_STRPCL);
  332. udelay(10);
  333. do {
  334. status = readl(host->base + JZ_REG_MMC_STATUS);
  335. } while (status & JZ_MMC_STATUS_IS_RESETTING && --timeout);
  336. }
  337. static void jz4740_mmc_request_done(struct jz4740_mmc_host *host)
  338. {
  339. struct mmc_request *req;
  340. req = host->req;
  341. host->req = NULL;
  342. mmc_request_done(host->mmc, req);
  343. }
  344. static unsigned int jz4740_mmc_poll_irq(struct jz4740_mmc_host *host,
  345. unsigned int irq)
  346. {
  347. unsigned int timeout = 0x800;
  348. uint16_t status;
  349. do {
  350. status = readw(host->base + JZ_REG_MMC_IREG);
  351. } while (!(status & irq) && --timeout);
  352. if (timeout == 0) {
  353. set_bit(0, &host->waiting);
  354. mod_timer(&host->timeout_timer, jiffies + 5*HZ);
  355. jz4740_mmc_set_irq_enabled(host, irq, true);
  356. return true;
  357. }
  358. return false;
  359. }
  360. static void jz4740_mmc_transfer_check_state(struct jz4740_mmc_host *host,
  361. struct mmc_data *data)
  362. {
  363. int status;
  364. status = readl(host->base + JZ_REG_MMC_STATUS);
  365. if (status & JZ_MMC_STATUS_WRITE_ERROR_MASK) {
  366. if (status & (JZ_MMC_STATUS_TIMEOUT_WRITE)) {
  367. host->req->cmd->error = -ETIMEDOUT;
  368. data->error = -ETIMEDOUT;
  369. } else {
  370. host->req->cmd->error = -EIO;
  371. data->error = -EIO;
  372. }
  373. } else if (status & JZ_MMC_STATUS_READ_ERROR_MASK) {
  374. if (status & (JZ_MMC_STATUS_TIMEOUT_READ)) {
  375. host->req->cmd->error = -ETIMEDOUT;
  376. data->error = -ETIMEDOUT;
  377. } else {
  378. host->req->cmd->error = -EIO;
  379. data->error = -EIO;
  380. }
  381. }
  382. }
  383. static bool jz4740_mmc_write_data(struct jz4740_mmc_host *host,
  384. struct mmc_data *data)
  385. {
  386. struct sg_mapping_iter *miter = &host->miter;
  387. void __iomem *fifo_addr = host->base + JZ_REG_MMC_TXFIFO;
  388. uint32_t *buf;
  389. bool timeout;
  390. size_t i, j;
  391. while (sg_miter_next(miter)) {
  392. buf = miter->addr;
  393. i = miter->length / 4;
  394. j = i / 8;
  395. i = i & 0x7;
  396. while (j) {
  397. timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
  398. if (unlikely(timeout))
  399. goto poll_timeout;
  400. writel(buf[0], fifo_addr);
  401. writel(buf[1], fifo_addr);
  402. writel(buf[2], fifo_addr);
  403. writel(buf[3], fifo_addr);
  404. writel(buf[4], fifo_addr);
  405. writel(buf[5], fifo_addr);
  406. writel(buf[6], fifo_addr);
  407. writel(buf[7], fifo_addr);
  408. buf += 8;
  409. --j;
  410. }
  411. if (unlikely(i)) {
  412. timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_TXFIFO_WR_REQ);
  413. if (unlikely(timeout))
  414. goto poll_timeout;
  415. while (i) {
  416. writel(*buf, fifo_addr);
  417. ++buf;
  418. --i;
  419. }
  420. }
  421. data->bytes_xfered += miter->length;
  422. }
  423. sg_miter_stop(miter);
  424. return false;
  425. poll_timeout:
  426. miter->consumed = (void *)buf - miter->addr;
  427. data->bytes_xfered += miter->consumed;
  428. sg_miter_stop(miter);
  429. return true;
  430. }
  431. static bool jz4740_mmc_read_data(struct jz4740_mmc_host *host,
  432. struct mmc_data *data)
  433. {
  434. struct sg_mapping_iter *miter = &host->miter;
  435. void __iomem *fifo_addr = host->base + JZ_REG_MMC_RXFIFO;
  436. uint32_t *buf;
  437. uint32_t d;
  438. uint16_t status;
  439. size_t i, j;
  440. unsigned int timeout;
  441. while (sg_miter_next(miter)) {
  442. buf = miter->addr;
  443. i = miter->length;
  444. j = i / 32;
  445. i = i & 0x1f;
  446. while (j) {
  447. timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
  448. if (unlikely(timeout))
  449. goto poll_timeout;
  450. buf[0] = readl(fifo_addr);
  451. buf[1] = readl(fifo_addr);
  452. buf[2] = readl(fifo_addr);
  453. buf[3] = readl(fifo_addr);
  454. buf[4] = readl(fifo_addr);
  455. buf[5] = readl(fifo_addr);
  456. buf[6] = readl(fifo_addr);
  457. buf[7] = readl(fifo_addr);
  458. buf += 8;
  459. --j;
  460. }
  461. if (unlikely(i)) {
  462. timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_RXFIFO_RD_REQ);
  463. if (unlikely(timeout))
  464. goto poll_timeout;
  465. while (i >= 4) {
  466. *buf++ = readl(fifo_addr);
  467. i -= 4;
  468. }
  469. if (unlikely(i > 0)) {
  470. d = readl(fifo_addr);
  471. memcpy(buf, &d, i);
  472. }
  473. }
  474. data->bytes_xfered += miter->length;
  475. /* This can go away once MIPS implements
  476. * flush_kernel_dcache_page */
  477. flush_dcache_page(miter->page);
  478. }
  479. sg_miter_stop(miter);
  480. /* For whatever reason there is sometime one word more in the fifo then
  481. * requested */
  482. timeout = 1000;
  483. status = readl(host->base + JZ_REG_MMC_STATUS);
  484. while (!(status & JZ_MMC_STATUS_DATA_FIFO_EMPTY) && --timeout) {
  485. d = readl(fifo_addr);
  486. status = readl(host->base + JZ_REG_MMC_STATUS);
  487. }
  488. return false;
  489. poll_timeout:
  490. miter->consumed = (void *)buf - miter->addr;
  491. data->bytes_xfered += miter->consumed;
  492. sg_miter_stop(miter);
  493. return true;
  494. }
  495. static void jz4740_mmc_timeout(unsigned long data)
  496. {
  497. struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)data;
  498. if (!test_and_clear_bit(0, &host->waiting))
  499. return;
  500. jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, false);
  501. host->req->cmd->error = -ETIMEDOUT;
  502. jz4740_mmc_request_done(host);
  503. }
  504. static void jz4740_mmc_read_response(struct jz4740_mmc_host *host,
  505. struct mmc_command *cmd)
  506. {
  507. int i;
  508. uint16_t tmp;
  509. void __iomem *fifo_addr = host->base + JZ_REG_MMC_RESP_FIFO;
  510. if (cmd->flags & MMC_RSP_136) {
  511. tmp = readw(fifo_addr);
  512. for (i = 0; i < 4; ++i) {
  513. cmd->resp[i] = tmp << 24;
  514. tmp = readw(fifo_addr);
  515. cmd->resp[i] |= tmp << 8;
  516. tmp = readw(fifo_addr);
  517. cmd->resp[i] |= tmp >> 8;
  518. }
  519. } else {
  520. cmd->resp[0] = readw(fifo_addr) << 24;
  521. cmd->resp[0] |= readw(fifo_addr) << 8;
  522. cmd->resp[0] |= readw(fifo_addr) & 0xff;
  523. }
  524. }
  525. static void jz4740_mmc_send_command(struct jz4740_mmc_host *host,
  526. struct mmc_command *cmd)
  527. {
  528. uint32_t cmdat = host->cmdat;
  529. host->cmdat &= ~JZ_MMC_CMDAT_INIT;
  530. jz4740_mmc_clock_disable(host);
  531. host->cmd = cmd;
  532. if (cmd->flags & MMC_RSP_BUSY)
  533. cmdat |= JZ_MMC_CMDAT_BUSY;
  534. switch (mmc_resp_type(cmd)) {
  535. case MMC_RSP_R1B:
  536. case MMC_RSP_R1:
  537. cmdat |= JZ_MMC_CMDAT_RSP_R1;
  538. break;
  539. case MMC_RSP_R2:
  540. cmdat |= JZ_MMC_CMDAT_RSP_R2;
  541. break;
  542. case MMC_RSP_R3:
  543. cmdat |= JZ_MMC_CMDAT_RSP_R3;
  544. break;
  545. default:
  546. break;
  547. }
  548. if (cmd->data) {
  549. cmdat |= JZ_MMC_CMDAT_DATA_EN;
  550. if (cmd->data->flags & MMC_DATA_WRITE)
  551. cmdat |= JZ_MMC_CMDAT_WRITE;
  552. if (host->use_dma)
  553. cmdat |= JZ_MMC_CMDAT_DMA_EN;
  554. writew(cmd->data->blksz, host->base + JZ_REG_MMC_BLKLEN);
  555. writew(cmd->data->blocks, host->base + JZ_REG_MMC_NOB);
  556. }
  557. writeb(cmd->opcode, host->base + JZ_REG_MMC_CMD);
  558. writel(cmd->arg, host->base + JZ_REG_MMC_ARG);
  559. writel(cmdat, host->base + JZ_REG_MMC_CMDAT);
  560. jz4740_mmc_clock_enable(host, 1);
  561. }
  562. static void jz_mmc_prepare_data_transfer(struct jz4740_mmc_host *host)
  563. {
  564. struct mmc_command *cmd = host->req->cmd;
  565. struct mmc_data *data = cmd->data;
  566. int direction;
  567. if (data->flags & MMC_DATA_READ)
  568. direction = SG_MITER_TO_SG;
  569. else
  570. direction = SG_MITER_FROM_SG;
  571. sg_miter_start(&host->miter, data->sg, data->sg_len, direction);
  572. }
  573. static irqreturn_t jz_mmc_irq_worker(int irq, void *devid)
  574. {
  575. struct jz4740_mmc_host *host = (struct jz4740_mmc_host *)devid;
  576. struct mmc_command *cmd = host->req->cmd;
  577. struct mmc_request *req = host->req;
  578. struct mmc_data *data = cmd->data;
  579. bool timeout = false;
  580. if (cmd->error)
  581. host->state = JZ4740_MMC_STATE_DONE;
  582. switch (host->state) {
  583. case JZ4740_MMC_STATE_READ_RESPONSE:
  584. if (cmd->flags & MMC_RSP_PRESENT)
  585. jz4740_mmc_read_response(host, cmd);
  586. if (!data)
  587. break;
  588. jz_mmc_prepare_data_transfer(host);
  589. case JZ4740_MMC_STATE_TRANSFER_DATA:
  590. if (host->use_dma) {
  591. /* Use DMA if enabled.
  592. * Data transfer direction is defined later by
  593. * relying on data flags in
  594. * jz4740_mmc_prepare_dma_data() and
  595. * jz4740_mmc_start_dma_transfer().
  596. */
  597. timeout = jz4740_mmc_start_dma_transfer(host, data);
  598. data->bytes_xfered = data->blocks * data->blksz;
  599. } else if (data->flags & MMC_DATA_READ)
  600. /* Use PIO if DMA is not enabled.
  601. * Data transfer direction was defined before
  602. * by relying on data flags in
  603. * jz_mmc_prepare_data_transfer().
  604. */
  605. timeout = jz4740_mmc_read_data(host, data);
  606. else
  607. timeout = jz4740_mmc_write_data(host, data);
  608. if (unlikely(timeout)) {
  609. host->state = JZ4740_MMC_STATE_TRANSFER_DATA;
  610. break;
  611. }
  612. jz4740_mmc_transfer_check_state(host, data);
  613. timeout = jz4740_mmc_poll_irq(host, JZ_MMC_IRQ_DATA_TRAN_DONE);
  614. if (unlikely(timeout)) {
  615. host->state = JZ4740_MMC_STATE_SEND_STOP;
  616. break;
  617. }
  618. writew(JZ_MMC_IRQ_DATA_TRAN_DONE, host->base + JZ_REG_MMC_IREG);
  619. case JZ4740_MMC_STATE_SEND_STOP:
  620. if (!req->stop)
  621. break;
  622. jz4740_mmc_send_command(host, req->stop);
  623. if (mmc_resp_type(req->stop) & MMC_RSP_BUSY) {
  624. timeout = jz4740_mmc_poll_irq(host,
  625. JZ_MMC_IRQ_PRG_DONE);
  626. if (timeout) {
  627. host->state = JZ4740_MMC_STATE_DONE;
  628. break;
  629. }
  630. }
  631. case JZ4740_MMC_STATE_DONE:
  632. break;
  633. }
  634. if (!timeout)
  635. jz4740_mmc_request_done(host);
  636. return IRQ_HANDLED;
  637. }
  638. static irqreturn_t jz_mmc_irq(int irq, void *devid)
  639. {
  640. struct jz4740_mmc_host *host = devid;
  641. struct mmc_command *cmd = host->cmd;
  642. uint16_t irq_reg, status, tmp;
  643. irq_reg = readw(host->base + JZ_REG_MMC_IREG);
  644. tmp = irq_reg;
  645. irq_reg &= ~host->irq_mask;
  646. tmp &= ~(JZ_MMC_IRQ_TXFIFO_WR_REQ | JZ_MMC_IRQ_RXFIFO_RD_REQ |
  647. JZ_MMC_IRQ_PRG_DONE | JZ_MMC_IRQ_DATA_TRAN_DONE);
  648. if (tmp != irq_reg)
  649. writew(tmp & ~irq_reg, host->base + JZ_REG_MMC_IREG);
  650. if (irq_reg & JZ_MMC_IRQ_SDIO) {
  651. writew(JZ_MMC_IRQ_SDIO, host->base + JZ_REG_MMC_IREG);
  652. mmc_signal_sdio_irq(host->mmc);
  653. irq_reg &= ~JZ_MMC_IRQ_SDIO;
  654. }
  655. if (host->req && cmd && irq_reg) {
  656. if (test_and_clear_bit(0, &host->waiting)) {
  657. del_timer(&host->timeout_timer);
  658. status = readl(host->base + JZ_REG_MMC_STATUS);
  659. if (status & JZ_MMC_STATUS_TIMEOUT_RES) {
  660. cmd->error = -ETIMEDOUT;
  661. } else if (status & JZ_MMC_STATUS_CRC_RES_ERR) {
  662. cmd->error = -EIO;
  663. } else if (status & (JZ_MMC_STATUS_CRC_READ_ERROR |
  664. JZ_MMC_STATUS_CRC_WRITE_ERROR)) {
  665. if (cmd->data)
  666. cmd->data->error = -EIO;
  667. cmd->error = -EIO;
  668. }
  669. jz4740_mmc_set_irq_enabled(host, irq_reg, false);
  670. writew(irq_reg, host->base + JZ_REG_MMC_IREG);
  671. return IRQ_WAKE_THREAD;
  672. }
  673. }
  674. return IRQ_HANDLED;
  675. }
  676. static int jz4740_mmc_set_clock_rate(struct jz4740_mmc_host *host, int rate)
  677. {
  678. int div = 0;
  679. int real_rate;
  680. jz4740_mmc_clock_disable(host);
  681. clk_set_rate(host->clk, JZ_MMC_CLK_RATE);
  682. real_rate = clk_get_rate(host->clk);
  683. while (real_rate > rate && div < 7) {
  684. ++div;
  685. real_rate >>= 1;
  686. }
  687. writew(div, host->base + JZ_REG_MMC_CLKRT);
  688. return real_rate;
  689. }
  690. static void jz4740_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
  691. {
  692. struct jz4740_mmc_host *host = mmc_priv(mmc);
  693. host->req = req;
  694. writew(0xffff, host->base + JZ_REG_MMC_IREG);
  695. writew(JZ_MMC_IRQ_END_CMD_RES, host->base + JZ_REG_MMC_IREG);
  696. jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_END_CMD_RES, true);
  697. host->state = JZ4740_MMC_STATE_READ_RESPONSE;
  698. set_bit(0, &host->waiting);
  699. mod_timer(&host->timeout_timer, jiffies + 5*HZ);
  700. jz4740_mmc_send_command(host, req->cmd);
  701. }
  702. static void jz4740_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  703. {
  704. struct jz4740_mmc_host *host = mmc_priv(mmc);
  705. if (ios->clock)
  706. jz4740_mmc_set_clock_rate(host, ios->clock);
  707. switch (ios->power_mode) {
  708. case MMC_POWER_UP:
  709. jz4740_mmc_reset(host);
  710. if (gpio_is_valid(host->pdata->gpio_power))
  711. gpio_set_value(host->pdata->gpio_power,
  712. !host->pdata->power_active_low);
  713. host->cmdat |= JZ_MMC_CMDAT_INIT;
  714. clk_prepare_enable(host->clk);
  715. break;
  716. case MMC_POWER_ON:
  717. break;
  718. default:
  719. if (gpio_is_valid(host->pdata->gpio_power))
  720. gpio_set_value(host->pdata->gpio_power,
  721. host->pdata->power_active_low);
  722. clk_disable_unprepare(host->clk);
  723. break;
  724. }
  725. switch (ios->bus_width) {
  726. case MMC_BUS_WIDTH_1:
  727. host->cmdat &= ~JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
  728. break;
  729. case MMC_BUS_WIDTH_4:
  730. host->cmdat |= JZ_MMC_CMDAT_BUS_WIDTH_4BIT;
  731. break;
  732. default:
  733. break;
  734. }
  735. }
  736. static void jz4740_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  737. {
  738. struct jz4740_mmc_host *host = mmc_priv(mmc);
  739. jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_SDIO, enable);
  740. }
  741. static const struct mmc_host_ops jz4740_mmc_ops = {
  742. .request = jz4740_mmc_request,
  743. .pre_req = jz4740_mmc_pre_request,
  744. .post_req = jz4740_mmc_post_request,
  745. .set_ios = jz4740_mmc_set_ios,
  746. .get_ro = mmc_gpio_get_ro,
  747. .get_cd = mmc_gpio_get_cd,
  748. .enable_sdio_irq = jz4740_mmc_enable_sdio_irq,
  749. };
  750. static const struct jz_gpio_bulk_request jz4740_mmc_pins[] = {
  751. JZ_GPIO_BULK_PIN(MSC_CMD),
  752. JZ_GPIO_BULK_PIN(MSC_CLK),
  753. JZ_GPIO_BULK_PIN(MSC_DATA0),
  754. JZ_GPIO_BULK_PIN(MSC_DATA1),
  755. JZ_GPIO_BULK_PIN(MSC_DATA2),
  756. JZ_GPIO_BULK_PIN(MSC_DATA3),
  757. };
  758. static int jz4740_mmc_request_gpio(struct device *dev, int gpio,
  759. const char *name, bool output, int value)
  760. {
  761. int ret;
  762. if (!gpio_is_valid(gpio))
  763. return 0;
  764. ret = gpio_request(gpio, name);
  765. if (ret) {
  766. dev_err(dev, "Failed to request %s gpio: %d\n", name, ret);
  767. return ret;
  768. }
  769. if (output)
  770. gpio_direction_output(gpio, value);
  771. else
  772. gpio_direction_input(gpio);
  773. return 0;
  774. }
  775. static int jz4740_mmc_request_gpios(struct mmc_host *mmc,
  776. struct platform_device *pdev)
  777. {
  778. struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
  779. int ret = 0;
  780. if (!pdata)
  781. return 0;
  782. if (!pdata->card_detect_active_low)
  783. mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  784. if (!pdata->read_only_active_low)
  785. mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  786. if (gpio_is_valid(pdata->gpio_card_detect)) {
  787. ret = mmc_gpio_request_cd(mmc, pdata->gpio_card_detect, 0);
  788. if (ret)
  789. return ret;
  790. }
  791. if (gpio_is_valid(pdata->gpio_read_only)) {
  792. ret = mmc_gpio_request_ro(mmc, pdata->gpio_read_only);
  793. if (ret)
  794. return ret;
  795. }
  796. return jz4740_mmc_request_gpio(&pdev->dev, pdata->gpio_power,
  797. "MMC read only", true, pdata->power_active_low);
  798. }
  799. static void jz4740_mmc_free_gpios(struct platform_device *pdev)
  800. {
  801. struct jz4740_mmc_platform_data *pdata = pdev->dev.platform_data;
  802. if (!pdata)
  803. return;
  804. if (gpio_is_valid(pdata->gpio_power))
  805. gpio_free(pdata->gpio_power);
  806. }
  807. static inline size_t jz4740_mmc_num_pins(struct jz4740_mmc_host *host)
  808. {
  809. size_t num_pins = ARRAY_SIZE(jz4740_mmc_pins);
  810. if (host->pdata && host->pdata->data_1bit)
  811. num_pins -= 3;
  812. return num_pins;
  813. }
  814. static int jz4740_mmc_probe(struct platform_device* pdev)
  815. {
  816. int ret;
  817. struct mmc_host *mmc;
  818. struct jz4740_mmc_host *host;
  819. struct jz4740_mmc_platform_data *pdata;
  820. pdata = pdev->dev.platform_data;
  821. mmc = mmc_alloc_host(sizeof(struct jz4740_mmc_host), &pdev->dev);
  822. if (!mmc) {
  823. dev_err(&pdev->dev, "Failed to alloc mmc host structure\n");
  824. return -ENOMEM;
  825. }
  826. host = mmc_priv(mmc);
  827. host->pdata = pdata;
  828. host->irq = platform_get_irq(pdev, 0);
  829. if (host->irq < 0) {
  830. ret = host->irq;
  831. dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
  832. goto err_free_host;
  833. }
  834. host->clk = devm_clk_get(&pdev->dev, "mmc");
  835. if (IS_ERR(host->clk)) {
  836. ret = PTR_ERR(host->clk);
  837. dev_err(&pdev->dev, "Failed to get mmc clock\n");
  838. goto err_free_host;
  839. }
  840. host->mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  841. host->base = devm_ioremap_resource(&pdev->dev, host->mem_res);
  842. if (IS_ERR(host->base)) {
  843. ret = PTR_ERR(host->base);
  844. dev_err(&pdev->dev, "Failed to ioremap base memory\n");
  845. goto err_free_host;
  846. }
  847. ret = jz_gpio_bulk_request(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
  848. if (ret) {
  849. dev_err(&pdev->dev, "Failed to request mmc pins: %d\n", ret);
  850. goto err_free_host;
  851. }
  852. ret = jz4740_mmc_request_gpios(mmc, pdev);
  853. if (ret)
  854. goto err_gpio_bulk_free;
  855. mmc->ops = &jz4740_mmc_ops;
  856. mmc->f_min = JZ_MMC_CLK_RATE / 128;
  857. mmc->f_max = JZ_MMC_CLK_RATE;
  858. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  859. mmc->caps = (pdata && pdata->data_1bit) ? 0 : MMC_CAP_4_BIT_DATA;
  860. mmc->caps |= MMC_CAP_SDIO_IRQ;
  861. mmc->max_blk_size = (1 << 10) - 1;
  862. mmc->max_blk_count = (1 << 15) - 1;
  863. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  864. mmc->max_segs = 128;
  865. mmc->max_seg_size = mmc->max_req_size;
  866. host->mmc = mmc;
  867. host->pdev = pdev;
  868. spin_lock_init(&host->lock);
  869. host->irq_mask = 0xffff;
  870. ret = request_threaded_irq(host->irq, jz_mmc_irq, jz_mmc_irq_worker, 0,
  871. dev_name(&pdev->dev), host);
  872. if (ret) {
  873. dev_err(&pdev->dev, "Failed to request irq: %d\n", ret);
  874. goto err_free_gpios;
  875. }
  876. jz4740_mmc_reset(host);
  877. jz4740_mmc_clock_disable(host);
  878. setup_timer(&host->timeout_timer, jz4740_mmc_timeout,
  879. (unsigned long)host);
  880. host->use_dma = true;
  881. if (host->use_dma && jz4740_mmc_acquire_dma_channels(host) != 0)
  882. host->use_dma = false;
  883. platform_set_drvdata(pdev, host);
  884. ret = mmc_add_host(mmc);
  885. if (ret) {
  886. dev_err(&pdev->dev, "Failed to add mmc host: %d\n", ret);
  887. goto err_free_irq;
  888. }
  889. dev_info(&pdev->dev, "JZ SD/MMC card driver registered\n");
  890. dev_info(&pdev->dev, "Using %s, %d-bit mode\n",
  891. host->use_dma ? "DMA" : "PIO",
  892. (mmc->caps & MMC_CAP_4_BIT_DATA) ? 4 : 1);
  893. return 0;
  894. err_free_irq:
  895. free_irq(host->irq, host);
  896. err_free_gpios:
  897. jz4740_mmc_free_gpios(pdev);
  898. err_gpio_bulk_free:
  899. if (host->use_dma)
  900. jz4740_mmc_release_dma_channels(host);
  901. jz_gpio_bulk_free(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
  902. err_free_host:
  903. mmc_free_host(mmc);
  904. return ret;
  905. }
  906. static int jz4740_mmc_remove(struct platform_device *pdev)
  907. {
  908. struct jz4740_mmc_host *host = platform_get_drvdata(pdev);
  909. del_timer_sync(&host->timeout_timer);
  910. jz4740_mmc_set_irq_enabled(host, 0xff, false);
  911. jz4740_mmc_reset(host);
  912. mmc_remove_host(host->mmc);
  913. free_irq(host->irq, host);
  914. jz4740_mmc_free_gpios(pdev);
  915. jz_gpio_bulk_free(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
  916. if (host->use_dma)
  917. jz4740_mmc_release_dma_channels(host);
  918. mmc_free_host(host->mmc);
  919. return 0;
  920. }
  921. #ifdef CONFIG_PM_SLEEP
  922. static int jz4740_mmc_suspend(struct device *dev)
  923. {
  924. struct jz4740_mmc_host *host = dev_get_drvdata(dev);
  925. jz_gpio_bulk_suspend(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
  926. return 0;
  927. }
  928. static int jz4740_mmc_resume(struct device *dev)
  929. {
  930. struct jz4740_mmc_host *host = dev_get_drvdata(dev);
  931. jz_gpio_bulk_resume(jz4740_mmc_pins, jz4740_mmc_num_pins(host));
  932. return 0;
  933. }
  934. static SIMPLE_DEV_PM_OPS(jz4740_mmc_pm_ops, jz4740_mmc_suspend,
  935. jz4740_mmc_resume);
  936. #define JZ4740_MMC_PM_OPS (&jz4740_mmc_pm_ops)
  937. #else
  938. #define JZ4740_MMC_PM_OPS NULL
  939. #endif
  940. static struct platform_driver jz4740_mmc_driver = {
  941. .probe = jz4740_mmc_probe,
  942. .remove = jz4740_mmc_remove,
  943. .driver = {
  944. .name = "jz4740-mmc",
  945. .pm = JZ4740_MMC_PM_OPS,
  946. },
  947. };
  948. module_platform_driver(jz4740_mmc_driver);
  949. MODULE_DESCRIPTION("JZ4740 SD/MMC controller driver");
  950. MODULE_LICENSE("GPL");
  951. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");