au1xmmc.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/mmc/host/au1xmmc.c - AU1XX0 MMC driver
  4. *
  5. * Copyright (c) 2005, Advanced Micro Devices, Inc.
  6. *
  7. * Developed with help from the 2.4.30 MMC AU1XXX controller including
  8. * the following copyright notices:
  9. * Copyright (c) 2003-2004 Embedded Edge, LLC.
  10. * Portions Copyright (C) 2002 Embedix, Inc
  11. * Copyright 2002 Hewlett-Packard Company
  12. * 2.6 version of this driver inspired by:
  13. * (drivers/mmc/wbsd.c) Copyright (C) 2004-2005 Pierre Ossman,
  14. * All Rights Reserved.
  15. * (drivers/mmc/pxa.c) Copyright (C) 2003 Russell King,
  16. * All Rights Reserved.
  17. *
  18. */
  19. /* Why don't we use the SD controllers' carddetect feature?
  20. *
  21. * From the AU1100 MMC application guide:
  22. * If the Au1100-based design is intended to support both MultiMediaCards
  23. * and 1- or 4-data bit SecureDigital cards, then the solution is to
  24. * connect a weak (560KOhm) pull-up resistor to connector pin 1.
  25. * In doing so, a MMC card never enters SPI-mode communications,
  26. * but now the SecureDigital card-detect feature of CD/DAT3 is ineffective
  27. * (the low to high transition will not occur).
  28. */
  29. #include <linux/clk.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/mm.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/scatterlist.h>
  37. #include <linux/highmem.h>
  38. #include <linux/leds.h>
  39. #include <linux/mmc/host.h>
  40. #include <linux/slab.h>
  41. #include <linux/workqueue.h>
  42. #include <asm/io.h>
  43. #include <asm/mach-au1x00/au1000.h>
  44. #include <asm/mach-au1x00/au1xxx_dbdma.h>
  45. #include <asm/mach-au1x00/au1100_mmc.h>
  46. #define DRIVER_NAME "au1xxx-mmc"
  47. /* Set this to enable special debugging macros */
  48. /* #define DEBUG */
  49. #ifdef DEBUG
  50. #define DBG(fmt, idx, args...) \
  51. pr_debug("au1xmmc(%d): DEBUG: " fmt, idx, ##args)
  52. #else
  53. #define DBG(fmt, idx, args...) do {} while (0)
  54. #endif
  55. /* Hardware definitions */
  56. #define AU1XMMC_DESCRIPTOR_COUNT 1
  57. /* max DMA seg size: 64KB on Au1100, 4MB on Au1200 */
  58. #define AU1100_MMC_DESCRIPTOR_SIZE 0x0000ffff
  59. #define AU1200_MMC_DESCRIPTOR_SIZE 0x003fffff
  60. #define AU1XMMC_OCR (MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 | \
  61. MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 | \
  62. MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36)
  63. /* This gives us a hard value for the stop command that we can write directly
  64. * to the command register.
  65. */
  66. #define STOP_CMD \
  67. (SD_CMD_RT_1B | SD_CMD_CT_7 | (0xC << SD_CMD_CI_SHIFT) | SD_CMD_GO)
  68. /* This is the set of interrupts that we configure by default. */
  69. #define AU1XMMC_INTERRUPTS \
  70. (SD_CONFIG_SC | SD_CONFIG_DT | SD_CONFIG_RAT | \
  71. SD_CONFIG_CR | SD_CONFIG_I)
  72. /* The poll event (looking for insert/remove events runs twice a second. */
  73. #define AU1XMMC_DETECT_TIMEOUT (HZ/2)
  74. struct au1xmmc_host {
  75. struct mmc_host *mmc;
  76. struct mmc_request *mrq;
  77. u32 flags;
  78. void __iomem *iobase;
  79. u32 clock;
  80. u32 bus_width;
  81. u32 power_mode;
  82. int status;
  83. struct {
  84. int len;
  85. int dir;
  86. } dma;
  87. struct {
  88. int index;
  89. int offset;
  90. int len;
  91. } pio;
  92. u32 tx_chan;
  93. u32 rx_chan;
  94. int irq;
  95. struct work_struct finish_bh_work;
  96. struct work_struct data_bh_work;
  97. struct au1xmmc_platform_data *platdata;
  98. struct platform_device *pdev;
  99. struct resource *ioarea;
  100. struct clk *clk;
  101. };
  102. /* Status flags used by the host structure */
  103. #define HOST_F_XMIT 0x0001
  104. #define HOST_F_RECV 0x0002
  105. #define HOST_F_DMA 0x0010
  106. #define HOST_F_DBDMA 0x0020
  107. #define HOST_F_ACTIVE 0x0100
  108. #define HOST_F_STOP 0x1000
  109. #define HOST_S_IDLE 0x0001
  110. #define HOST_S_CMD 0x0002
  111. #define HOST_S_DATA 0x0003
  112. #define HOST_S_STOP 0x0004
  113. /* Easy access macros */
  114. #define HOST_STATUS(h) ((h)->iobase + SD_STATUS)
  115. #define HOST_CONFIG(h) ((h)->iobase + SD_CONFIG)
  116. #define HOST_ENABLE(h) ((h)->iobase + SD_ENABLE)
  117. #define HOST_TXPORT(h) ((h)->iobase + SD_TXPORT)
  118. #define HOST_RXPORT(h) ((h)->iobase + SD_RXPORT)
  119. #define HOST_CMDARG(h) ((h)->iobase + SD_CMDARG)
  120. #define HOST_BLKSIZE(h) ((h)->iobase + SD_BLKSIZE)
  121. #define HOST_CMD(h) ((h)->iobase + SD_CMD)
  122. #define HOST_CONFIG2(h) ((h)->iobase + SD_CONFIG2)
  123. #define HOST_TIMEOUT(h) ((h)->iobase + SD_TIMEOUT)
  124. #define HOST_DEBUG(h) ((h)->iobase + SD_DEBUG)
  125. #define DMA_CHANNEL(h) \
  126. (((h)->flags & HOST_F_XMIT) ? (h)->tx_chan : (h)->rx_chan)
  127. static inline int has_dbdma(void)
  128. {
  129. switch (alchemy_get_cputype()) {
  130. case ALCHEMY_CPU_AU1200:
  131. case ALCHEMY_CPU_AU1300:
  132. return 1;
  133. default:
  134. return 0;
  135. }
  136. }
  137. static inline void IRQ_ON(struct au1xmmc_host *host, u32 mask)
  138. {
  139. u32 val = __raw_readl(HOST_CONFIG(host));
  140. val |= mask;
  141. __raw_writel(val, HOST_CONFIG(host));
  142. wmb(); /* drain writebuffer */
  143. }
  144. static inline void FLUSH_FIFO(struct au1xmmc_host *host)
  145. {
  146. u32 val = __raw_readl(HOST_CONFIG2(host));
  147. __raw_writel(val | SD_CONFIG2_FF, HOST_CONFIG2(host));
  148. wmb(); /* drain writebuffer */
  149. mdelay(1);
  150. /* SEND_STOP will turn off clock control - this re-enables it */
  151. val &= ~SD_CONFIG2_DF;
  152. __raw_writel(val, HOST_CONFIG2(host));
  153. wmb(); /* drain writebuffer */
  154. }
  155. static inline void IRQ_OFF(struct au1xmmc_host *host, u32 mask)
  156. {
  157. u32 val = __raw_readl(HOST_CONFIG(host));
  158. val &= ~mask;
  159. __raw_writel(val, HOST_CONFIG(host));
  160. wmb(); /* drain writebuffer */
  161. }
  162. static inline void SEND_STOP(struct au1xmmc_host *host)
  163. {
  164. u32 config2;
  165. WARN_ON(host->status != HOST_S_DATA);
  166. host->status = HOST_S_STOP;
  167. config2 = __raw_readl(HOST_CONFIG2(host));
  168. __raw_writel(config2 | SD_CONFIG2_DF, HOST_CONFIG2(host));
  169. wmb(); /* drain writebuffer */
  170. /* Send the stop command */
  171. __raw_writel(STOP_CMD, HOST_CMD(host));
  172. wmb(); /* drain writebuffer */
  173. }
  174. static void au1xmmc_set_power(struct au1xmmc_host *host, int state)
  175. {
  176. if (host->platdata && host->platdata->set_power)
  177. host->platdata->set_power(host->mmc, state);
  178. }
  179. static int au1xmmc_card_inserted(struct mmc_host *mmc)
  180. {
  181. struct au1xmmc_host *host = mmc_priv(mmc);
  182. if (host->platdata && host->platdata->card_inserted)
  183. return !!host->platdata->card_inserted(host->mmc);
  184. return -ENOSYS;
  185. }
  186. static int au1xmmc_card_readonly(struct mmc_host *mmc)
  187. {
  188. struct au1xmmc_host *host = mmc_priv(mmc);
  189. if (host->platdata && host->platdata->card_readonly)
  190. return !!host->platdata->card_readonly(mmc);
  191. return -ENOSYS;
  192. }
  193. static void au1xmmc_finish_request(struct au1xmmc_host *host)
  194. {
  195. struct mmc_request *mrq = host->mrq;
  196. host->mrq = NULL;
  197. host->flags &= HOST_F_ACTIVE | HOST_F_DMA;
  198. host->dma.len = 0;
  199. host->dma.dir = 0;
  200. host->pio.index = 0;
  201. host->pio.offset = 0;
  202. host->pio.len = 0;
  203. host->status = HOST_S_IDLE;
  204. mmc_request_done(host->mmc, mrq);
  205. }
  206. static void au1xmmc_finish_bh_work(struct work_struct *t)
  207. {
  208. struct au1xmmc_host *host = from_work(host, t, finish_bh_work);
  209. au1xmmc_finish_request(host);
  210. }
  211. static int au1xmmc_send_command(struct au1xmmc_host *host,
  212. struct mmc_command *cmd, struct mmc_data *data)
  213. {
  214. u32 mmccmd = (cmd->opcode << SD_CMD_CI_SHIFT);
  215. switch (mmc_resp_type(cmd)) {
  216. case MMC_RSP_NONE:
  217. break;
  218. case MMC_RSP_R1:
  219. mmccmd |= SD_CMD_RT_1;
  220. break;
  221. case MMC_RSP_R1B:
  222. mmccmd |= SD_CMD_RT_1B;
  223. break;
  224. case MMC_RSP_R2:
  225. mmccmd |= SD_CMD_RT_2;
  226. break;
  227. case MMC_RSP_R3:
  228. mmccmd |= SD_CMD_RT_3;
  229. break;
  230. default:
  231. pr_info("au1xmmc: unhandled response type %02x\n",
  232. mmc_resp_type(cmd));
  233. return -EINVAL;
  234. }
  235. if (data) {
  236. if (data->flags & MMC_DATA_READ) {
  237. if (data->blocks > 1)
  238. mmccmd |= SD_CMD_CT_4;
  239. else
  240. mmccmd |= SD_CMD_CT_2;
  241. } else if (data->flags & MMC_DATA_WRITE) {
  242. if (data->blocks > 1)
  243. mmccmd |= SD_CMD_CT_3;
  244. else
  245. mmccmd |= SD_CMD_CT_1;
  246. }
  247. }
  248. __raw_writel(cmd->arg, HOST_CMDARG(host));
  249. wmb(); /* drain writebuffer */
  250. __raw_writel((mmccmd | SD_CMD_GO), HOST_CMD(host));
  251. wmb(); /* drain writebuffer */
  252. /* Wait for the command to go on the line */
  253. while (__raw_readl(HOST_CMD(host)) & SD_CMD_GO)
  254. /* nop */;
  255. return 0;
  256. }
  257. static void au1xmmc_data_complete(struct au1xmmc_host *host, u32 status)
  258. {
  259. struct mmc_request *mrq = host->mrq;
  260. struct mmc_data *data;
  261. u32 crc;
  262. WARN_ON((host->status != HOST_S_DATA) && (host->status != HOST_S_STOP));
  263. if (host->mrq == NULL)
  264. return;
  265. data = mrq->cmd->data;
  266. if (status == 0)
  267. status = __raw_readl(HOST_STATUS(host));
  268. /* The transaction is really over when the SD_STATUS_DB bit is clear */
  269. while ((host->flags & HOST_F_XMIT) && (status & SD_STATUS_DB))
  270. status = __raw_readl(HOST_STATUS(host));
  271. data->error = 0;
  272. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len, host->dma.dir);
  273. /* Process any errors */
  274. crc = (status & (SD_STATUS_WC | SD_STATUS_RC));
  275. if (host->flags & HOST_F_XMIT)
  276. crc |= ((status & 0x07) == 0x02) ? 0 : 1;
  277. if (crc)
  278. data->error = -EILSEQ;
  279. /* Clear the CRC bits */
  280. __raw_writel(SD_STATUS_WC | SD_STATUS_RC, HOST_STATUS(host));
  281. data->bytes_xfered = 0;
  282. if (!data->error) {
  283. if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
  284. u32 chan = DMA_CHANNEL(host);
  285. chan_tab_t *c = *((chan_tab_t **)chan);
  286. au1x_dma_chan_t *cp = c->chan_ptr;
  287. data->bytes_xfered = cp->ddma_bytecnt;
  288. } else
  289. data->bytes_xfered =
  290. (data->blocks * data->blksz) - host->pio.len;
  291. }
  292. au1xmmc_finish_request(host);
  293. }
  294. static void au1xmmc_data_bh_work(struct work_struct *t)
  295. {
  296. struct au1xmmc_host *host = from_work(host, t, data_bh_work);
  297. u32 status = __raw_readl(HOST_STATUS(host));
  298. au1xmmc_data_complete(host, status);
  299. }
  300. #define AU1XMMC_MAX_TRANSFER 8
  301. static void au1xmmc_send_pio(struct au1xmmc_host *host)
  302. {
  303. struct mmc_data *data;
  304. int sg_len, max, count;
  305. unsigned char *sg_ptr, val;
  306. u32 status;
  307. struct scatterlist *sg;
  308. data = host->mrq->data;
  309. if (!(host->flags & HOST_F_XMIT))
  310. return;
  311. /* This is the pointer to the data buffer */
  312. sg = &data->sg[host->pio.index];
  313. sg_ptr = kmap_local_page(sg_page(sg)) + sg->offset + host->pio.offset;
  314. /* This is the space left inside the buffer */
  315. sg_len = data->sg[host->pio.index].length - host->pio.offset;
  316. /* Check if we need less than the size of the sg_buffer */
  317. max = (sg_len > host->pio.len) ? host->pio.len : sg_len;
  318. if (max > AU1XMMC_MAX_TRANSFER)
  319. max = AU1XMMC_MAX_TRANSFER;
  320. for (count = 0; count < max; count++) {
  321. status = __raw_readl(HOST_STATUS(host));
  322. if (!(status & SD_STATUS_TH))
  323. break;
  324. val = sg_ptr[count];
  325. __raw_writel((unsigned long)val, HOST_TXPORT(host));
  326. wmb(); /* drain writebuffer */
  327. }
  328. kunmap_local(sg_ptr);
  329. host->pio.len -= count;
  330. host->pio.offset += count;
  331. if (count == sg_len) {
  332. host->pio.index++;
  333. host->pio.offset = 0;
  334. }
  335. if (host->pio.len == 0) {
  336. IRQ_OFF(host, SD_CONFIG_TH);
  337. if (host->flags & HOST_F_STOP)
  338. SEND_STOP(host);
  339. queue_work(system_bh_wq, &host->data_bh_work);
  340. }
  341. }
  342. static void au1xmmc_receive_pio(struct au1xmmc_host *host)
  343. {
  344. struct mmc_data *data;
  345. int max, count, sg_len = 0;
  346. unsigned char *sg_ptr = NULL;
  347. u32 status, val;
  348. struct scatterlist *sg;
  349. data = host->mrq->data;
  350. if (!(host->flags & HOST_F_RECV))
  351. return;
  352. max = host->pio.len;
  353. if (host->pio.index < host->dma.len) {
  354. sg = &data->sg[host->pio.index];
  355. sg_ptr = kmap_local_page(sg_page(sg)) + sg->offset + host->pio.offset;
  356. /* This is the space left inside the buffer */
  357. sg_len = sg_dma_len(&data->sg[host->pio.index]) - host->pio.offset;
  358. /* Check if we need less than the size of the sg_buffer */
  359. if (sg_len < max)
  360. max = sg_len;
  361. }
  362. if (max > AU1XMMC_MAX_TRANSFER)
  363. max = AU1XMMC_MAX_TRANSFER;
  364. for (count = 0; count < max; count++) {
  365. status = __raw_readl(HOST_STATUS(host));
  366. if (!(status & SD_STATUS_NE))
  367. break;
  368. if (status & SD_STATUS_RC) {
  369. DBG("RX CRC Error [%d + %d].\n", host->pdev->id,
  370. host->pio.len, count);
  371. break;
  372. }
  373. if (status & SD_STATUS_RO) {
  374. DBG("RX Overrun [%d + %d]\n", host->pdev->id,
  375. host->pio.len, count);
  376. break;
  377. }
  378. else if (status & SD_STATUS_RU) {
  379. DBG("RX Underrun [%d + %d]\n", host->pdev->id,
  380. host->pio.len, count);
  381. break;
  382. }
  383. val = __raw_readl(HOST_RXPORT(host));
  384. if (sg_ptr)
  385. sg_ptr[count] = (unsigned char)(val & 0xFF);
  386. }
  387. if (sg_ptr)
  388. kunmap_local(sg_ptr);
  389. host->pio.len -= count;
  390. host->pio.offset += count;
  391. if (sg_len && count == sg_len) {
  392. host->pio.index++;
  393. host->pio.offset = 0;
  394. }
  395. if (host->pio.len == 0) {
  396. /* IRQ_OFF(host, SD_CONFIG_RA | SD_CONFIG_RF); */
  397. IRQ_OFF(host, SD_CONFIG_NE);
  398. if (host->flags & HOST_F_STOP)
  399. SEND_STOP(host);
  400. queue_work(system_bh_wq, &host->data_bh_work);
  401. }
  402. }
  403. /* This is called when a command has been completed - grab the response
  404. * and check for errors. Then start the data transfer if it is indicated.
  405. */
  406. static void au1xmmc_cmd_complete(struct au1xmmc_host *host, u32 status)
  407. {
  408. struct mmc_request *mrq = host->mrq;
  409. struct mmc_command *cmd;
  410. u32 r[4];
  411. int i, trans;
  412. if (!host->mrq)
  413. return;
  414. cmd = mrq->cmd;
  415. cmd->error = 0;
  416. if (cmd->flags & MMC_RSP_PRESENT) {
  417. if (cmd->flags & MMC_RSP_136) {
  418. r[0] = __raw_readl(host->iobase + SD_RESP3);
  419. r[1] = __raw_readl(host->iobase + SD_RESP2);
  420. r[2] = __raw_readl(host->iobase + SD_RESP1);
  421. r[3] = __raw_readl(host->iobase + SD_RESP0);
  422. /* The CRC is omitted from the response, so really
  423. * we only got 120 bytes, but the engine expects
  424. * 128 bits, so we have to shift things up.
  425. */
  426. for (i = 0; i < 4; i++) {
  427. cmd->resp[i] = (r[i] & 0x00FFFFFF) << 8;
  428. if (i != 3)
  429. cmd->resp[i] |= (r[i + 1] & 0xFF000000) >> 24;
  430. }
  431. } else {
  432. /* Techincally, we should be getting all 48 bits of
  433. * the response (SD_RESP1 + SD_RESP2), but because
  434. * our response omits the CRC, our data ends up
  435. * being shifted 8 bits to the right. In this case,
  436. * that means that the OSR data starts at bit 31,
  437. * so we can just read RESP0 and return that.
  438. */
  439. cmd->resp[0] = __raw_readl(host->iobase + SD_RESP0);
  440. }
  441. }
  442. /* Figure out errors */
  443. if (status & (SD_STATUS_SC | SD_STATUS_WC | SD_STATUS_RC))
  444. cmd->error = -EILSEQ;
  445. trans = host->flags & (HOST_F_XMIT | HOST_F_RECV);
  446. if (!trans || cmd->error) {
  447. IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF);
  448. queue_work(system_bh_wq, &host->finish_bh_work);
  449. return;
  450. }
  451. host->status = HOST_S_DATA;
  452. if ((host->flags & (HOST_F_DMA | HOST_F_DBDMA))) {
  453. u32 channel = DMA_CHANNEL(host);
  454. /* Start the DBDMA as soon as the buffer gets something in it */
  455. if (host->flags & HOST_F_RECV) {
  456. u32 mask = SD_STATUS_DB | SD_STATUS_NE;
  457. while((status & mask) != mask)
  458. status = __raw_readl(HOST_STATUS(host));
  459. }
  460. au1xxx_dbdma_start(channel);
  461. }
  462. }
  463. static void au1xmmc_set_clock(struct au1xmmc_host *host, int rate)
  464. {
  465. unsigned int pbus = clk_get_rate(host->clk);
  466. unsigned int divisor = ((pbus / rate) / 2) - 1;
  467. u32 config;
  468. config = __raw_readl(HOST_CONFIG(host));
  469. config &= ~(SD_CONFIG_DIV);
  470. config |= (divisor & SD_CONFIG_DIV) | SD_CONFIG_DE;
  471. __raw_writel(config, HOST_CONFIG(host));
  472. wmb(); /* drain writebuffer */
  473. }
  474. static int au1xmmc_prepare_data(struct au1xmmc_host *host,
  475. struct mmc_data *data)
  476. {
  477. int datalen = data->blocks * data->blksz;
  478. if (data->flags & MMC_DATA_READ)
  479. host->flags |= HOST_F_RECV;
  480. else
  481. host->flags |= HOST_F_XMIT;
  482. if (host->mrq->stop)
  483. host->flags |= HOST_F_STOP;
  484. host->dma.dir = DMA_BIDIRECTIONAL;
  485. host->dma.len = dma_map_sg(mmc_dev(host->mmc), data->sg,
  486. data->sg_len, host->dma.dir);
  487. if (host->dma.len == 0)
  488. return -ETIMEDOUT;
  489. __raw_writel(data->blksz - 1, HOST_BLKSIZE(host));
  490. if (host->flags & (HOST_F_DMA | HOST_F_DBDMA)) {
  491. int i;
  492. u32 channel = DMA_CHANNEL(host);
  493. au1xxx_dbdma_stop(channel);
  494. for (i = 0; i < host->dma.len; i++) {
  495. u32 ret = 0, flags = DDMA_FLAGS_NOIE;
  496. struct scatterlist *sg = &data->sg[i];
  497. int sg_len = sg->length;
  498. int len = (datalen > sg_len) ? sg_len : datalen;
  499. if (i == host->dma.len - 1)
  500. flags = DDMA_FLAGS_IE;
  501. if (host->flags & HOST_F_XMIT) {
  502. ret = au1xxx_dbdma_put_source(channel,
  503. sg_phys(sg), len, flags);
  504. } else {
  505. ret = au1xxx_dbdma_put_dest(channel,
  506. sg_phys(sg), len, flags);
  507. }
  508. if (!ret)
  509. goto dataerr;
  510. datalen -= len;
  511. }
  512. } else {
  513. host->pio.index = 0;
  514. host->pio.offset = 0;
  515. host->pio.len = datalen;
  516. if (host->flags & HOST_F_XMIT)
  517. IRQ_ON(host, SD_CONFIG_TH);
  518. else
  519. IRQ_ON(host, SD_CONFIG_NE);
  520. /* IRQ_ON(host, SD_CONFIG_RA | SD_CONFIG_RF); */
  521. }
  522. return 0;
  523. dataerr:
  524. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  525. host->dma.dir);
  526. return -ETIMEDOUT;
  527. }
  528. /* This actually starts a command or data transaction */
  529. static void au1xmmc_request(struct mmc_host* mmc, struct mmc_request* mrq)
  530. {
  531. struct au1xmmc_host *host = mmc_priv(mmc);
  532. int ret = 0;
  533. WARN_ON(irqs_disabled());
  534. WARN_ON(host->status != HOST_S_IDLE);
  535. host->mrq = mrq;
  536. host->status = HOST_S_CMD;
  537. /* fail request immediately if no card is present */
  538. if (0 == au1xmmc_card_inserted(mmc)) {
  539. mrq->cmd->error = -ENOMEDIUM;
  540. au1xmmc_finish_request(host);
  541. return;
  542. }
  543. if (mrq->data) {
  544. FLUSH_FIFO(host);
  545. ret = au1xmmc_prepare_data(host, mrq->data);
  546. }
  547. if (!ret)
  548. ret = au1xmmc_send_command(host, mrq->cmd, mrq->data);
  549. if (ret) {
  550. mrq->cmd->error = ret;
  551. au1xmmc_finish_request(host);
  552. }
  553. }
  554. static void au1xmmc_reset_controller(struct au1xmmc_host *host)
  555. {
  556. /* Apply the clock */
  557. __raw_writel(SD_ENABLE_CE, HOST_ENABLE(host));
  558. wmb(); /* drain writebuffer */
  559. mdelay(1);
  560. __raw_writel(SD_ENABLE_R | SD_ENABLE_CE, HOST_ENABLE(host));
  561. wmb(); /* drain writebuffer */
  562. mdelay(5);
  563. __raw_writel(~0, HOST_STATUS(host));
  564. wmb(); /* drain writebuffer */
  565. __raw_writel(0, HOST_BLKSIZE(host));
  566. __raw_writel(0x001fffff, HOST_TIMEOUT(host));
  567. wmb(); /* drain writebuffer */
  568. __raw_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  569. wmb(); /* drain writebuffer */
  570. __raw_writel(SD_CONFIG2_EN | SD_CONFIG2_FF, HOST_CONFIG2(host));
  571. wmb(); /* drain writebuffer */
  572. mdelay(1);
  573. __raw_writel(SD_CONFIG2_EN, HOST_CONFIG2(host));
  574. wmb(); /* drain writebuffer */
  575. /* Configure interrupts */
  576. __raw_writel(AU1XMMC_INTERRUPTS, HOST_CONFIG(host));
  577. wmb(); /* drain writebuffer */
  578. }
  579. static void au1xmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  580. {
  581. struct au1xmmc_host *host = mmc_priv(mmc);
  582. u32 config2;
  583. if (ios->power_mode == MMC_POWER_OFF)
  584. au1xmmc_set_power(host, 0);
  585. else if (ios->power_mode == MMC_POWER_ON) {
  586. au1xmmc_set_power(host, 1);
  587. }
  588. if (ios->clock && ios->clock != host->clock) {
  589. au1xmmc_set_clock(host, ios->clock);
  590. host->clock = ios->clock;
  591. }
  592. config2 = __raw_readl(HOST_CONFIG2(host));
  593. switch (ios->bus_width) {
  594. case MMC_BUS_WIDTH_8:
  595. config2 |= SD_CONFIG2_BB;
  596. break;
  597. case MMC_BUS_WIDTH_4:
  598. config2 &= ~SD_CONFIG2_BB;
  599. config2 |= SD_CONFIG2_WB;
  600. break;
  601. case MMC_BUS_WIDTH_1:
  602. config2 &= ~(SD_CONFIG2_WB | SD_CONFIG2_BB);
  603. break;
  604. }
  605. __raw_writel(config2, HOST_CONFIG2(host));
  606. wmb(); /* drain writebuffer */
  607. }
  608. #define STATUS_TIMEOUT (SD_STATUS_RAT | SD_STATUS_DT)
  609. #define STATUS_DATA_IN (SD_STATUS_NE)
  610. #define STATUS_DATA_OUT (SD_STATUS_TH)
  611. static irqreturn_t au1xmmc_irq(int irq, void *dev_id)
  612. {
  613. struct au1xmmc_host *host = dev_id;
  614. u32 status;
  615. status = __raw_readl(HOST_STATUS(host));
  616. if (!(status & SD_STATUS_I))
  617. return IRQ_NONE; /* not ours */
  618. if (status & SD_STATUS_SI) /* SDIO */
  619. mmc_signal_sdio_irq(host->mmc);
  620. if (host->mrq && (status & STATUS_TIMEOUT)) {
  621. if (status & SD_STATUS_RAT)
  622. host->mrq->cmd->error = -ETIMEDOUT;
  623. else if (status & SD_STATUS_DT)
  624. host->mrq->data->error = -ETIMEDOUT;
  625. /* In PIO mode, interrupts might still be enabled */
  626. IRQ_OFF(host, SD_CONFIG_NE | SD_CONFIG_TH);
  627. /* IRQ_OFF(host, SD_CONFIG_TH | SD_CONFIG_RA | SD_CONFIG_RF); */
  628. queue_work(system_bh_wq, &host->finish_bh_work);
  629. }
  630. #if 0
  631. else if (status & SD_STATUS_DD) {
  632. /* Sometimes we get a DD before a NE in PIO mode */
  633. if (!(host->flags & HOST_F_DMA) && (status & SD_STATUS_NE))
  634. au1xmmc_receive_pio(host);
  635. else {
  636. au1xmmc_data_complete(host, status);
  637. /* queue_work(system_bh_wq, &host->data_bh_work); */
  638. }
  639. }
  640. #endif
  641. else if (status & SD_STATUS_CR) {
  642. if (host->status == HOST_S_CMD)
  643. au1xmmc_cmd_complete(host, status);
  644. } else if (!(host->flags & HOST_F_DMA)) {
  645. if ((host->flags & HOST_F_XMIT) && (status & STATUS_DATA_OUT))
  646. au1xmmc_send_pio(host);
  647. else if ((host->flags & HOST_F_RECV) && (status & STATUS_DATA_IN))
  648. au1xmmc_receive_pio(host);
  649. } else if (status & 0x203F3C70) {
  650. DBG("Unhandled status %8.8x\n", host->pdev->id,
  651. status);
  652. }
  653. __raw_writel(status, HOST_STATUS(host));
  654. wmb(); /* drain writebuffer */
  655. return IRQ_HANDLED;
  656. }
  657. /* 8bit memory DMA device */
  658. static dbdev_tab_t au1xmmc_mem_dbdev = {
  659. .dev_id = DSCR_CMD0_ALWAYS,
  660. .dev_flags = DEV_FLAGS_ANYUSE,
  661. .dev_tsize = 0,
  662. .dev_devwidth = 8,
  663. .dev_physaddr = 0x00000000,
  664. .dev_intlevel = 0,
  665. .dev_intpolarity = 0,
  666. };
  667. static int memid;
  668. static void au1xmmc_dbdma_callback(int irq, void *dev_id)
  669. {
  670. struct au1xmmc_host *host = (struct au1xmmc_host *)dev_id;
  671. /* Avoid spurious interrupts */
  672. if (!host->mrq)
  673. return;
  674. if (host->flags & HOST_F_STOP)
  675. SEND_STOP(host);
  676. queue_work(system_bh_wq, &host->data_bh_work);
  677. }
  678. static int au1xmmc_dbdma_init(struct au1xmmc_host *host)
  679. {
  680. struct resource *res;
  681. int txid, rxid;
  682. res = platform_get_resource(host->pdev, IORESOURCE_DMA, 0);
  683. if (!res)
  684. return -ENODEV;
  685. txid = res->start;
  686. res = platform_get_resource(host->pdev, IORESOURCE_DMA, 1);
  687. if (!res)
  688. return -ENODEV;
  689. rxid = res->start;
  690. if (!memid)
  691. return -ENODEV;
  692. host->tx_chan = au1xxx_dbdma_chan_alloc(memid, txid,
  693. au1xmmc_dbdma_callback, (void *)host);
  694. if (!host->tx_chan) {
  695. dev_err(&host->pdev->dev, "cannot allocate TX DMA\n");
  696. return -ENODEV;
  697. }
  698. host->rx_chan = au1xxx_dbdma_chan_alloc(rxid, memid,
  699. au1xmmc_dbdma_callback, (void *)host);
  700. if (!host->rx_chan) {
  701. dev_err(&host->pdev->dev, "cannot allocate RX DMA\n");
  702. au1xxx_dbdma_chan_free(host->tx_chan);
  703. return -ENODEV;
  704. }
  705. au1xxx_dbdma_set_devwidth(host->tx_chan, 8);
  706. au1xxx_dbdma_set_devwidth(host->rx_chan, 8);
  707. au1xxx_dbdma_ring_alloc(host->tx_chan, AU1XMMC_DESCRIPTOR_COUNT);
  708. au1xxx_dbdma_ring_alloc(host->rx_chan, AU1XMMC_DESCRIPTOR_COUNT);
  709. /* DBDMA is good to go */
  710. host->flags |= HOST_F_DMA | HOST_F_DBDMA;
  711. return 0;
  712. }
  713. static void au1xmmc_dbdma_shutdown(struct au1xmmc_host *host)
  714. {
  715. if (host->flags & HOST_F_DMA) {
  716. host->flags &= ~HOST_F_DMA;
  717. au1xxx_dbdma_chan_free(host->tx_chan);
  718. au1xxx_dbdma_chan_free(host->rx_chan);
  719. }
  720. }
  721. static void au1xmmc_enable_sdio_irq(struct mmc_host *mmc, int en)
  722. {
  723. struct au1xmmc_host *host = mmc_priv(mmc);
  724. if (en)
  725. IRQ_ON(host, SD_CONFIG_SI);
  726. else
  727. IRQ_OFF(host, SD_CONFIG_SI);
  728. }
  729. static const struct mmc_host_ops au1xmmc_ops = {
  730. .request = au1xmmc_request,
  731. .set_ios = au1xmmc_set_ios,
  732. .get_ro = au1xmmc_card_readonly,
  733. .get_cd = au1xmmc_card_inserted,
  734. .enable_sdio_irq = au1xmmc_enable_sdio_irq,
  735. };
  736. static int au1xmmc_probe(struct platform_device *pdev)
  737. {
  738. struct mmc_host *mmc;
  739. struct au1xmmc_host *host;
  740. struct resource *r;
  741. int ret, iflag;
  742. mmc = mmc_alloc_host(sizeof(struct au1xmmc_host), &pdev->dev);
  743. if (!mmc) {
  744. dev_err(&pdev->dev, "no memory for mmc_host\n");
  745. ret = -ENOMEM;
  746. goto out0;
  747. }
  748. host = mmc_priv(mmc);
  749. host->mmc = mmc;
  750. host->platdata = pdev->dev.platform_data;
  751. host->pdev = pdev;
  752. ret = -ENODEV;
  753. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  754. if (!r) {
  755. dev_err(&pdev->dev, "no mmio defined\n");
  756. goto out1;
  757. }
  758. host->ioarea = request_mem_region(r->start, resource_size(r),
  759. pdev->name);
  760. if (!host->ioarea) {
  761. dev_err(&pdev->dev, "mmio already in use\n");
  762. goto out1;
  763. }
  764. host->iobase = ioremap(r->start, 0x3c);
  765. if (!host->iobase) {
  766. dev_err(&pdev->dev, "cannot remap mmio\n");
  767. goto out2;
  768. }
  769. host->irq = platform_get_irq(pdev, 0);
  770. if (host->irq < 0) {
  771. ret = host->irq;
  772. goto out3;
  773. }
  774. mmc->ops = &au1xmmc_ops;
  775. mmc->f_min = 450000;
  776. mmc->f_max = 24000000;
  777. mmc->max_blk_size = 2048;
  778. mmc->max_blk_count = 512;
  779. mmc->ocr_avail = AU1XMMC_OCR;
  780. mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
  781. mmc->max_segs = AU1XMMC_DESCRIPTOR_COUNT;
  782. iflag = IRQF_SHARED; /* Au1100/Au1200: one int for both ctrls */
  783. switch (alchemy_get_cputype()) {
  784. case ALCHEMY_CPU_AU1100:
  785. mmc->max_seg_size = AU1100_MMC_DESCRIPTOR_SIZE;
  786. break;
  787. case ALCHEMY_CPU_AU1200:
  788. mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
  789. break;
  790. case ALCHEMY_CPU_AU1300:
  791. iflag = 0; /* nothing is shared */
  792. mmc->max_seg_size = AU1200_MMC_DESCRIPTOR_SIZE;
  793. mmc->f_max = 52000000;
  794. if (host->ioarea->start == AU1100_SD0_PHYS_ADDR)
  795. mmc->caps |= MMC_CAP_8_BIT_DATA;
  796. break;
  797. }
  798. ret = request_irq(host->irq, au1xmmc_irq, iflag, DRIVER_NAME, host);
  799. if (ret) {
  800. dev_err(&pdev->dev, "cannot grab IRQ\n");
  801. goto out3;
  802. }
  803. host->clk = clk_get(&pdev->dev, ALCHEMY_PERIPH_CLK);
  804. if (IS_ERR(host->clk)) {
  805. dev_err(&pdev->dev, "cannot find clock\n");
  806. ret = PTR_ERR(host->clk);
  807. goto out_irq;
  808. }
  809. ret = clk_prepare_enable(host->clk);
  810. if (ret) {
  811. dev_err(&pdev->dev, "cannot enable clock\n");
  812. goto out_clk;
  813. }
  814. host->status = HOST_S_IDLE;
  815. /* board-specific carddetect setup, if any */
  816. if (host->platdata && host->platdata->cd_setup) {
  817. ret = host->platdata->cd_setup(mmc, 1);
  818. if (ret) {
  819. dev_warn(&pdev->dev, "board CD setup failed\n");
  820. mmc->caps |= MMC_CAP_NEEDS_POLL;
  821. }
  822. } else
  823. mmc->caps |= MMC_CAP_NEEDS_POLL;
  824. /* platform may not be able to use all advertised caps */
  825. if (host->platdata)
  826. mmc->caps &= ~(host->platdata->mask_host_caps);
  827. INIT_WORK(&host->data_bh_work, au1xmmc_data_bh_work);
  828. INIT_WORK(&host->finish_bh_work, au1xmmc_finish_bh_work);
  829. if (has_dbdma()) {
  830. ret = au1xmmc_dbdma_init(host);
  831. if (ret)
  832. pr_info(DRIVER_NAME ": DBDMA init failed; using PIO\n");
  833. }
  834. #ifdef CONFIG_LEDS_CLASS
  835. if (host->platdata && host->platdata->led) {
  836. struct led_classdev *led = host->platdata->led;
  837. led->name = mmc_hostname(mmc);
  838. led->brightness = LED_OFF;
  839. led->default_trigger = mmc_hostname(mmc);
  840. ret = led_classdev_register(mmc_dev(mmc), led);
  841. if (ret)
  842. goto out5;
  843. }
  844. #endif
  845. au1xmmc_reset_controller(host);
  846. ret = mmc_add_host(mmc);
  847. if (ret) {
  848. dev_err(&pdev->dev, "cannot add mmc host\n");
  849. goto out6;
  850. }
  851. platform_set_drvdata(pdev, host);
  852. pr_info(DRIVER_NAME ": MMC Controller %d set up at %p"
  853. " (mode=%s)\n", pdev->id, host->iobase,
  854. host->flags & HOST_F_DMA ? "dma" : "pio");
  855. return 0; /* all ok */
  856. out6:
  857. #ifdef CONFIG_LEDS_CLASS
  858. if (host->platdata && host->platdata->led)
  859. led_classdev_unregister(host->platdata->led);
  860. out5:
  861. #endif
  862. __raw_writel(0, HOST_ENABLE(host));
  863. __raw_writel(0, HOST_CONFIG(host));
  864. __raw_writel(0, HOST_CONFIG2(host));
  865. wmb(); /* drain writebuffer */
  866. if (host->flags & HOST_F_DBDMA)
  867. au1xmmc_dbdma_shutdown(host);
  868. cancel_work_sync(&host->data_bh_work);
  869. cancel_work_sync(&host->finish_bh_work);
  870. if (host->platdata && host->platdata->cd_setup &&
  871. !(mmc->caps & MMC_CAP_NEEDS_POLL))
  872. host->platdata->cd_setup(mmc, 0);
  873. clk_disable_unprepare(host->clk);
  874. out_clk:
  875. clk_put(host->clk);
  876. out_irq:
  877. free_irq(host->irq, host);
  878. out3:
  879. iounmap((void *)host->iobase);
  880. out2:
  881. release_resource(host->ioarea);
  882. kfree(host->ioarea);
  883. out1:
  884. mmc_free_host(mmc);
  885. out0:
  886. return ret;
  887. }
  888. static void au1xmmc_remove(struct platform_device *pdev)
  889. {
  890. struct au1xmmc_host *host = platform_get_drvdata(pdev);
  891. if (host) {
  892. mmc_remove_host(host->mmc);
  893. #ifdef CONFIG_LEDS_CLASS
  894. if (host->platdata && host->platdata->led)
  895. led_classdev_unregister(host->platdata->led);
  896. #endif
  897. if (host->platdata && host->platdata->cd_setup &&
  898. !(host->mmc->caps & MMC_CAP_NEEDS_POLL))
  899. host->platdata->cd_setup(host->mmc, 0);
  900. __raw_writel(0, HOST_ENABLE(host));
  901. __raw_writel(0, HOST_CONFIG(host));
  902. __raw_writel(0, HOST_CONFIG2(host));
  903. wmb(); /* drain writebuffer */
  904. cancel_work_sync(&host->data_bh_work);
  905. cancel_work_sync(&host->finish_bh_work);
  906. if (host->flags & HOST_F_DBDMA)
  907. au1xmmc_dbdma_shutdown(host);
  908. au1xmmc_set_power(host, 0);
  909. clk_disable_unprepare(host->clk);
  910. clk_put(host->clk);
  911. free_irq(host->irq, host);
  912. iounmap((void *)host->iobase);
  913. release_resource(host->ioarea);
  914. kfree(host->ioarea);
  915. mmc_free_host(host->mmc);
  916. }
  917. }
  918. #ifdef CONFIG_PM
  919. static int au1xmmc_suspend(struct platform_device *pdev, pm_message_t state)
  920. {
  921. struct au1xmmc_host *host = platform_get_drvdata(pdev);
  922. __raw_writel(0, HOST_CONFIG2(host));
  923. __raw_writel(0, HOST_CONFIG(host));
  924. __raw_writel(0xffffffff, HOST_STATUS(host));
  925. __raw_writel(0, HOST_ENABLE(host));
  926. wmb(); /* drain writebuffer */
  927. return 0;
  928. }
  929. static int au1xmmc_resume(struct platform_device *pdev)
  930. {
  931. struct au1xmmc_host *host = platform_get_drvdata(pdev);
  932. au1xmmc_reset_controller(host);
  933. return 0;
  934. }
  935. #else
  936. #define au1xmmc_suspend NULL
  937. #define au1xmmc_resume NULL
  938. #endif
  939. static struct platform_driver au1xmmc_driver = {
  940. .probe = au1xmmc_probe,
  941. .remove_new = au1xmmc_remove,
  942. .suspend = au1xmmc_suspend,
  943. .resume = au1xmmc_resume,
  944. .driver = {
  945. .name = DRIVER_NAME,
  946. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  947. },
  948. };
  949. static int __init au1xmmc_init(void)
  950. {
  951. if (has_dbdma()) {
  952. /* DSCR_CMD0_ALWAYS has a stride of 32 bits, we need a stride
  953. * of 8 bits. And since devices are shared, we need to create
  954. * our own to avoid freaking out other devices.
  955. */
  956. memid = au1xxx_ddma_add_device(&au1xmmc_mem_dbdev);
  957. if (!memid)
  958. pr_err("au1xmmc: cannot add memory dbdma\n");
  959. }
  960. return platform_driver_register(&au1xmmc_driver);
  961. }
  962. static void __exit au1xmmc_exit(void)
  963. {
  964. if (has_dbdma() && memid)
  965. au1xxx_ddma_del_device(memid);
  966. platform_driver_unregister(&au1xmmc_driver);
  967. }
  968. module_init(au1xmmc_init);
  969. module_exit(au1xmmc_exit);
  970. MODULE_AUTHOR("Advanced Micro Devices, Inc");
  971. MODULE_DESCRIPTION("MMC/SD driver for the Alchemy Au1XXX");
  972. MODULE_LICENSE("GPL");
  973. MODULE_ALIAS("platform:au1xxx-mmc");