pata_arasan_cf.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * drivers/ata/pata_arasan_cf.c
  3. *
  4. * Arasan Compact Flash host controller source file
  5. *
  6. * Copyright (C) 2011 ST Microelectronics
  7. * Viresh Kumar <vireshk@kernel.org>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. /*
  14. * The Arasan CompactFlash Device Controller IP core has three basic modes of
  15. * operation: PC card ATA using I/O mode, PC card ATA using memory mode, PC card
  16. * ATA using true IDE modes. This driver supports only True IDE mode currently.
  17. *
  18. * Arasan CF Controller shares global irq register with Arasan XD Controller.
  19. *
  20. * Tested on arch/arm/mach-spear13xx
  21. */
  22. #include <linux/ata.h>
  23. #include <linux/clk.h>
  24. #include <linux/completion.h>
  25. #include <linux/delay.h>
  26. #include <linux/dmaengine.h>
  27. #include <linux/io.h>
  28. #include <linux/irq.h>
  29. #include <linux/kernel.h>
  30. #include <linux/libata.h>
  31. #include <linux/module.h>
  32. #include <linux/of.h>
  33. #include <linux/pata_arasan_cf_data.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/pm.h>
  36. #include <linux/slab.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/types.h>
  39. #include <linux/workqueue.h>
  40. #define DRIVER_NAME "arasan_cf"
  41. #define TIMEOUT msecs_to_jiffies(3000)
  42. /* Registers */
  43. /* CompactFlash Interface Status */
  44. #define CFI_STS 0x000
  45. #define STS_CHG (1)
  46. #define BIN_AUDIO_OUT (1 << 1)
  47. #define CARD_DETECT1 (1 << 2)
  48. #define CARD_DETECT2 (1 << 3)
  49. #define INP_ACK (1 << 4)
  50. #define CARD_READY (1 << 5)
  51. #define IO_READY (1 << 6)
  52. #define B16_IO_PORT_SEL (1 << 7)
  53. /* IRQ */
  54. #define IRQ_STS 0x004
  55. /* Interrupt Enable */
  56. #define IRQ_EN 0x008
  57. #define CARD_DETECT_IRQ (1)
  58. #define STATUS_CHNG_IRQ (1 << 1)
  59. #define MEM_MODE_IRQ (1 << 2)
  60. #define IO_MODE_IRQ (1 << 3)
  61. #define TRUE_IDE_MODE_IRQ (1 << 8)
  62. #define PIO_XFER_ERR_IRQ (1 << 9)
  63. #define BUF_AVAIL_IRQ (1 << 10)
  64. #define XFER_DONE_IRQ (1 << 11)
  65. #define IGNORED_IRQS (STATUS_CHNG_IRQ | MEM_MODE_IRQ | IO_MODE_IRQ |\
  66. TRUE_IDE_MODE_IRQ)
  67. #define TRUE_IDE_IRQS (CARD_DETECT_IRQ | PIO_XFER_ERR_IRQ |\
  68. BUF_AVAIL_IRQ | XFER_DONE_IRQ)
  69. /* Operation Mode */
  70. #define OP_MODE 0x00C
  71. #define CARD_MODE_MASK (0x3)
  72. #define MEM_MODE (0x0)
  73. #define IO_MODE (0x1)
  74. #define TRUE_IDE_MODE (0x2)
  75. #define CARD_TYPE_MASK (1 << 2)
  76. #define CF_CARD (0)
  77. #define CF_PLUS_CARD (1 << 2)
  78. #define CARD_RESET (1 << 3)
  79. #define CFHOST_ENB (1 << 4)
  80. #define OUTPUTS_TRISTATE (1 << 5)
  81. #define ULTRA_DMA_ENB (1 << 8)
  82. #define MULTI_WORD_DMA_ENB (1 << 9)
  83. #define DRQ_BLOCK_SIZE_MASK (0x3 << 11)
  84. #define DRQ_BLOCK_SIZE_512 (0)
  85. #define DRQ_BLOCK_SIZE_1024 (1 << 11)
  86. #define DRQ_BLOCK_SIZE_2048 (2 << 11)
  87. #define DRQ_BLOCK_SIZE_4096 (3 << 11)
  88. /* CF Interface Clock Configuration */
  89. #define CLK_CFG 0x010
  90. #define CF_IF_CLK_MASK (0XF)
  91. /* CF Timing Mode Configuration */
  92. #define TM_CFG 0x014
  93. #define MEM_MODE_TIMING_MASK (0x3)
  94. #define MEM_MODE_TIMING_250NS (0x0)
  95. #define MEM_MODE_TIMING_120NS (0x1)
  96. #define MEM_MODE_TIMING_100NS (0x2)
  97. #define MEM_MODE_TIMING_80NS (0x3)
  98. #define IO_MODE_TIMING_MASK (0x3 << 2)
  99. #define IO_MODE_TIMING_250NS (0x0 << 2)
  100. #define IO_MODE_TIMING_120NS (0x1 << 2)
  101. #define IO_MODE_TIMING_100NS (0x2 << 2)
  102. #define IO_MODE_TIMING_80NS (0x3 << 2)
  103. #define TRUEIDE_PIO_TIMING_MASK (0x7 << 4)
  104. #define TRUEIDE_PIO_TIMING_SHIFT 4
  105. #define TRUEIDE_MWORD_DMA_TIMING_MASK (0x7 << 7)
  106. #define TRUEIDE_MWORD_DMA_TIMING_SHIFT 7
  107. #define ULTRA_DMA_TIMING_MASK (0x7 << 10)
  108. #define ULTRA_DMA_TIMING_SHIFT 10
  109. /* CF Transfer Address */
  110. #define XFER_ADDR 0x014
  111. #define XFER_ADDR_MASK (0x7FF)
  112. #define MAX_XFER_COUNT 0x20000u
  113. /* Transfer Control */
  114. #define XFER_CTR 0x01C
  115. #define XFER_COUNT_MASK (0x3FFFF)
  116. #define ADDR_INC_DISABLE (1 << 24)
  117. #define XFER_WIDTH_MASK (1 << 25)
  118. #define XFER_WIDTH_8B (0)
  119. #define XFER_WIDTH_16B (1 << 25)
  120. #define MEM_TYPE_MASK (1 << 26)
  121. #define MEM_TYPE_COMMON (0)
  122. #define MEM_TYPE_ATTRIBUTE (1 << 26)
  123. #define MEM_IO_XFER_MASK (1 << 27)
  124. #define MEM_XFER (0)
  125. #define IO_XFER (1 << 27)
  126. #define DMA_XFER_MODE (1 << 28)
  127. #define AHB_BUS_NORMAL_PIO_OPRTN (~(1 << 29))
  128. #define XFER_DIR_MASK (1 << 30)
  129. #define XFER_READ (0)
  130. #define XFER_WRITE (1 << 30)
  131. #define XFER_START (1 << 31)
  132. /* Write Data Port */
  133. #define WRITE_PORT 0x024
  134. /* Read Data Port */
  135. #define READ_PORT 0x028
  136. /* ATA Data Port */
  137. #define ATA_DATA_PORT 0x030
  138. #define ATA_DATA_PORT_MASK (0xFFFF)
  139. /* ATA Error/Features */
  140. #define ATA_ERR_FTR 0x034
  141. /* ATA Sector Count */
  142. #define ATA_SC 0x038
  143. /* ATA Sector Number */
  144. #define ATA_SN 0x03C
  145. /* ATA Cylinder Low */
  146. #define ATA_CL 0x040
  147. /* ATA Cylinder High */
  148. #define ATA_CH 0x044
  149. /* ATA Select Card/Head */
  150. #define ATA_SH 0x048
  151. /* ATA Status-Command */
  152. #define ATA_STS_CMD 0x04C
  153. /* ATA Alternate Status/Device Control */
  154. #define ATA_ASTS_DCTR 0x050
  155. /* Extended Write Data Port 0x200-0x3FC */
  156. #define EXT_WRITE_PORT 0x200
  157. /* Extended Read Data Port 0x400-0x5FC */
  158. #define EXT_READ_PORT 0x400
  159. #define FIFO_SIZE 0x200u
  160. /* Global Interrupt Status */
  161. #define GIRQ_STS 0x800
  162. /* Global Interrupt Status enable */
  163. #define GIRQ_STS_EN 0x804
  164. /* Global Interrupt Signal enable */
  165. #define GIRQ_SGN_EN 0x808
  166. #define GIRQ_CF (1)
  167. #define GIRQ_XD (1 << 1)
  168. /* Compact Flash Controller Dev Structure */
  169. struct arasan_cf_dev {
  170. /* pointer to ata_host structure */
  171. struct ata_host *host;
  172. /* clk structure */
  173. struct clk *clk;
  174. /* physical base address of controller */
  175. dma_addr_t pbase;
  176. /* virtual base address of controller */
  177. void __iomem *vbase;
  178. /* irq number*/
  179. int irq;
  180. /* status to be updated to framework regarding DMA transfer */
  181. u8 dma_status;
  182. /* Card is present or Not */
  183. u8 card_present;
  184. /* dma specific */
  185. /* Completion for transfer complete interrupt from controller */
  186. struct completion cf_completion;
  187. /* Completion for DMA transfer complete. */
  188. struct completion dma_completion;
  189. /* Dma channel allocated */
  190. struct dma_chan *dma_chan;
  191. /* Mask for DMA transfers */
  192. dma_cap_mask_t mask;
  193. /* DMA transfer work */
  194. struct work_struct work;
  195. /* DMA delayed finish work */
  196. struct delayed_work dwork;
  197. /* qc to be transferred using DMA */
  198. struct ata_queued_cmd *qc;
  199. };
  200. static struct scsi_host_template arasan_cf_sht = {
  201. ATA_BASE_SHT(DRIVER_NAME),
  202. .sg_tablesize = SG_NONE,
  203. .dma_boundary = 0xFFFFFFFFUL,
  204. };
  205. static void cf_dumpregs(struct arasan_cf_dev *acdev)
  206. {
  207. struct device *dev = acdev->host->dev;
  208. dev_dbg(dev, ": =========== REGISTER DUMP ===========");
  209. dev_dbg(dev, ": CFI_STS: %x", readl(acdev->vbase + CFI_STS));
  210. dev_dbg(dev, ": IRQ_STS: %x", readl(acdev->vbase + IRQ_STS));
  211. dev_dbg(dev, ": IRQ_EN: %x", readl(acdev->vbase + IRQ_EN));
  212. dev_dbg(dev, ": OP_MODE: %x", readl(acdev->vbase + OP_MODE));
  213. dev_dbg(dev, ": CLK_CFG: %x", readl(acdev->vbase + CLK_CFG));
  214. dev_dbg(dev, ": TM_CFG: %x", readl(acdev->vbase + TM_CFG));
  215. dev_dbg(dev, ": XFER_CTR: %x", readl(acdev->vbase + XFER_CTR));
  216. dev_dbg(dev, ": GIRQ_STS: %x", readl(acdev->vbase + GIRQ_STS));
  217. dev_dbg(dev, ": GIRQ_STS_EN: %x", readl(acdev->vbase + GIRQ_STS_EN));
  218. dev_dbg(dev, ": GIRQ_SGN_EN: %x", readl(acdev->vbase + GIRQ_SGN_EN));
  219. dev_dbg(dev, ": =====================================");
  220. }
  221. /* Enable/Disable global interrupts shared between CF and XD ctrlr. */
  222. static void cf_ginterrupt_enable(struct arasan_cf_dev *acdev, bool enable)
  223. {
  224. /* enable should be 0 or 1 */
  225. writel(enable, acdev->vbase + GIRQ_STS_EN);
  226. writel(enable, acdev->vbase + GIRQ_SGN_EN);
  227. }
  228. /* Enable/Disable CF interrupts */
  229. static inline void
  230. cf_interrupt_enable(struct arasan_cf_dev *acdev, u32 mask, bool enable)
  231. {
  232. u32 val = readl(acdev->vbase + IRQ_EN);
  233. /* clear & enable/disable irqs */
  234. if (enable) {
  235. writel(mask, acdev->vbase + IRQ_STS);
  236. writel(val | mask, acdev->vbase + IRQ_EN);
  237. } else
  238. writel(val & ~mask, acdev->vbase + IRQ_EN);
  239. }
  240. static inline void cf_card_reset(struct arasan_cf_dev *acdev)
  241. {
  242. u32 val = readl(acdev->vbase + OP_MODE);
  243. writel(val | CARD_RESET, acdev->vbase + OP_MODE);
  244. udelay(200);
  245. writel(val & ~CARD_RESET, acdev->vbase + OP_MODE);
  246. }
  247. static inline void cf_ctrl_reset(struct arasan_cf_dev *acdev)
  248. {
  249. writel(readl(acdev->vbase + OP_MODE) & ~CFHOST_ENB,
  250. acdev->vbase + OP_MODE);
  251. writel(readl(acdev->vbase + OP_MODE) | CFHOST_ENB,
  252. acdev->vbase + OP_MODE);
  253. }
  254. static void cf_card_detect(struct arasan_cf_dev *acdev, bool hotplugged)
  255. {
  256. struct ata_port *ap = acdev->host->ports[0];
  257. struct ata_eh_info *ehi = &ap->link.eh_info;
  258. u32 val = readl(acdev->vbase + CFI_STS);
  259. /* Both CD1 & CD2 should be low if card inserted completely */
  260. if (!(val & (CARD_DETECT1 | CARD_DETECT2))) {
  261. if (acdev->card_present)
  262. return;
  263. acdev->card_present = 1;
  264. cf_card_reset(acdev);
  265. } else {
  266. if (!acdev->card_present)
  267. return;
  268. acdev->card_present = 0;
  269. }
  270. if (hotplugged) {
  271. ata_ehi_hotplugged(ehi);
  272. ata_port_freeze(ap);
  273. }
  274. }
  275. static int cf_init(struct arasan_cf_dev *acdev)
  276. {
  277. struct arasan_cf_pdata *pdata = dev_get_platdata(acdev->host->dev);
  278. unsigned int if_clk;
  279. unsigned long flags;
  280. int ret = 0;
  281. ret = clk_prepare_enable(acdev->clk);
  282. if (ret) {
  283. dev_dbg(acdev->host->dev, "clock enable failed");
  284. return ret;
  285. }
  286. ret = clk_set_rate(acdev->clk, 166000000);
  287. if (ret) {
  288. dev_warn(acdev->host->dev, "clock set rate failed");
  289. clk_disable_unprepare(acdev->clk);
  290. return ret;
  291. }
  292. spin_lock_irqsave(&acdev->host->lock, flags);
  293. /* configure CF interface clock */
  294. /* TODO: read from device tree */
  295. if_clk = CF_IF_CLK_166M;
  296. if (pdata && pdata->cf_if_clk <= CF_IF_CLK_200M)
  297. if_clk = pdata->cf_if_clk;
  298. writel(if_clk, acdev->vbase + CLK_CFG);
  299. writel(TRUE_IDE_MODE | CFHOST_ENB, acdev->vbase + OP_MODE);
  300. cf_interrupt_enable(acdev, CARD_DETECT_IRQ, 1);
  301. cf_ginterrupt_enable(acdev, 1);
  302. spin_unlock_irqrestore(&acdev->host->lock, flags);
  303. return ret;
  304. }
  305. static void cf_exit(struct arasan_cf_dev *acdev)
  306. {
  307. unsigned long flags;
  308. spin_lock_irqsave(&acdev->host->lock, flags);
  309. cf_ginterrupt_enable(acdev, 0);
  310. cf_interrupt_enable(acdev, TRUE_IDE_IRQS, 0);
  311. cf_card_reset(acdev);
  312. writel(readl(acdev->vbase + OP_MODE) & ~CFHOST_ENB,
  313. acdev->vbase + OP_MODE);
  314. spin_unlock_irqrestore(&acdev->host->lock, flags);
  315. clk_disable_unprepare(acdev->clk);
  316. }
  317. static void dma_callback(void *dev)
  318. {
  319. struct arasan_cf_dev *acdev = dev;
  320. complete(&acdev->dma_completion);
  321. }
  322. static inline void dma_complete(struct arasan_cf_dev *acdev)
  323. {
  324. struct ata_queued_cmd *qc = acdev->qc;
  325. unsigned long flags;
  326. acdev->qc = NULL;
  327. ata_sff_interrupt(acdev->irq, acdev->host);
  328. spin_lock_irqsave(&acdev->host->lock, flags);
  329. if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
  330. ata_ehi_push_desc(&qc->ap->link.eh_info, "DMA Failed: Timeout");
  331. spin_unlock_irqrestore(&acdev->host->lock, flags);
  332. }
  333. static inline int wait4buf(struct arasan_cf_dev *acdev)
  334. {
  335. if (!wait_for_completion_timeout(&acdev->cf_completion, TIMEOUT)) {
  336. u32 rw = acdev->qc->tf.flags & ATA_TFLAG_WRITE;
  337. dev_err(acdev->host->dev, "%s TimeOut", rw ? "write" : "read");
  338. return -ETIMEDOUT;
  339. }
  340. /* Check if PIO Error interrupt has occurred */
  341. if (acdev->dma_status & ATA_DMA_ERR)
  342. return -EAGAIN;
  343. return 0;
  344. }
  345. static int
  346. dma_xfer(struct arasan_cf_dev *acdev, dma_addr_t src, dma_addr_t dest, u32 len)
  347. {
  348. struct dma_async_tx_descriptor *tx;
  349. struct dma_chan *chan = acdev->dma_chan;
  350. dma_cookie_t cookie;
  351. unsigned long flags = DMA_PREP_INTERRUPT;
  352. int ret = 0;
  353. tx = chan->device->device_prep_dma_memcpy(chan, dest, src, len, flags);
  354. if (!tx) {
  355. dev_err(acdev->host->dev, "device_prep_dma_memcpy failed\n");
  356. return -EAGAIN;
  357. }
  358. tx->callback = dma_callback;
  359. tx->callback_param = acdev;
  360. cookie = tx->tx_submit(tx);
  361. ret = dma_submit_error(cookie);
  362. if (ret) {
  363. dev_err(acdev->host->dev, "dma_submit_error\n");
  364. return ret;
  365. }
  366. chan->device->device_issue_pending(chan);
  367. /* Wait for DMA to complete */
  368. if (!wait_for_completion_timeout(&acdev->dma_completion, TIMEOUT)) {
  369. dmaengine_terminate_all(chan);
  370. dev_err(acdev->host->dev, "wait_for_completion_timeout\n");
  371. return -ETIMEDOUT;
  372. }
  373. return ret;
  374. }
  375. static int sg_xfer(struct arasan_cf_dev *acdev, struct scatterlist *sg)
  376. {
  377. dma_addr_t dest = 0, src = 0;
  378. u32 xfer_cnt, sglen, dma_len, xfer_ctr;
  379. u32 write = acdev->qc->tf.flags & ATA_TFLAG_WRITE;
  380. unsigned long flags;
  381. int ret = 0;
  382. sglen = sg_dma_len(sg);
  383. if (write) {
  384. src = sg_dma_address(sg);
  385. dest = acdev->pbase + EXT_WRITE_PORT;
  386. } else {
  387. dest = sg_dma_address(sg);
  388. src = acdev->pbase + EXT_READ_PORT;
  389. }
  390. /*
  391. * For each sg:
  392. * MAX_XFER_COUNT data will be transferred before we get transfer
  393. * complete interrupt. Between after FIFO_SIZE data
  394. * buffer available interrupt will be generated. At this time we will
  395. * fill FIFO again: max FIFO_SIZE data.
  396. */
  397. while (sglen) {
  398. xfer_cnt = min(sglen, MAX_XFER_COUNT);
  399. spin_lock_irqsave(&acdev->host->lock, flags);
  400. xfer_ctr = readl(acdev->vbase + XFER_CTR) &
  401. ~XFER_COUNT_MASK;
  402. writel(xfer_ctr | xfer_cnt | XFER_START,
  403. acdev->vbase + XFER_CTR);
  404. spin_unlock_irqrestore(&acdev->host->lock, flags);
  405. /* continue dma xfers until current sg is completed */
  406. while (xfer_cnt) {
  407. /* wait for read to complete */
  408. if (!write) {
  409. ret = wait4buf(acdev);
  410. if (ret)
  411. goto fail;
  412. }
  413. /* read/write FIFO in chunk of FIFO_SIZE */
  414. dma_len = min(xfer_cnt, FIFO_SIZE);
  415. ret = dma_xfer(acdev, src, dest, dma_len);
  416. if (ret) {
  417. dev_err(acdev->host->dev, "dma failed");
  418. goto fail;
  419. }
  420. if (write)
  421. src += dma_len;
  422. else
  423. dest += dma_len;
  424. sglen -= dma_len;
  425. xfer_cnt -= dma_len;
  426. /* wait for write to complete */
  427. if (write) {
  428. ret = wait4buf(acdev);
  429. if (ret)
  430. goto fail;
  431. }
  432. }
  433. }
  434. fail:
  435. spin_lock_irqsave(&acdev->host->lock, flags);
  436. writel(readl(acdev->vbase + XFER_CTR) & ~XFER_START,
  437. acdev->vbase + XFER_CTR);
  438. spin_unlock_irqrestore(&acdev->host->lock, flags);
  439. return ret;
  440. }
  441. /*
  442. * This routine uses External DMA controller to read/write data to FIFO of CF
  443. * controller. There are two xfer related interrupt supported by CF controller:
  444. * - buf_avail: This interrupt is generated as soon as we have buffer of 512
  445. * bytes available for reading or empty buffer available for writing.
  446. * - xfer_done: This interrupt is generated on transfer of "xfer_size" amount of
  447. * data to/from FIFO. xfer_size is programmed in XFER_CTR register.
  448. *
  449. * Max buffer size = FIFO_SIZE = 512 Bytes.
  450. * Max xfer_size = MAX_XFER_COUNT = 256 KB.
  451. */
  452. static void data_xfer(struct work_struct *work)
  453. {
  454. struct arasan_cf_dev *acdev = container_of(work, struct arasan_cf_dev,
  455. work);
  456. struct ata_queued_cmd *qc = acdev->qc;
  457. struct scatterlist *sg;
  458. unsigned long flags;
  459. u32 temp;
  460. int ret = 0;
  461. /* request dma channels */
  462. /* dma_request_channel may sleep, so calling from process context */
  463. acdev->dma_chan = dma_request_slave_channel(acdev->host->dev, "data");
  464. if (!acdev->dma_chan) {
  465. dev_err(acdev->host->dev, "Unable to get dma_chan\n");
  466. goto chan_request_fail;
  467. }
  468. for_each_sg(qc->sg, sg, qc->n_elem, temp) {
  469. ret = sg_xfer(acdev, sg);
  470. if (ret)
  471. break;
  472. }
  473. dma_release_channel(acdev->dma_chan);
  474. /* data xferred successfully */
  475. if (!ret) {
  476. u32 status;
  477. spin_lock_irqsave(&acdev->host->lock, flags);
  478. status = ioread8(qc->ap->ioaddr.altstatus_addr);
  479. spin_unlock_irqrestore(&acdev->host->lock, flags);
  480. if (status & (ATA_BUSY | ATA_DRQ)) {
  481. ata_sff_queue_delayed_work(&acdev->dwork, 1);
  482. return;
  483. }
  484. goto sff_intr;
  485. }
  486. cf_dumpregs(acdev);
  487. chan_request_fail:
  488. spin_lock_irqsave(&acdev->host->lock, flags);
  489. /* error when transferring data to/from memory */
  490. qc->err_mask |= AC_ERR_HOST_BUS;
  491. qc->ap->hsm_task_state = HSM_ST_ERR;
  492. cf_ctrl_reset(acdev);
  493. spin_unlock_irqrestore(qc->ap->lock, flags);
  494. sff_intr:
  495. dma_complete(acdev);
  496. }
  497. static void delayed_finish(struct work_struct *work)
  498. {
  499. struct arasan_cf_dev *acdev = container_of(work, struct arasan_cf_dev,
  500. dwork.work);
  501. struct ata_queued_cmd *qc = acdev->qc;
  502. unsigned long flags;
  503. u8 status;
  504. spin_lock_irqsave(&acdev->host->lock, flags);
  505. status = ioread8(qc->ap->ioaddr.altstatus_addr);
  506. spin_unlock_irqrestore(&acdev->host->lock, flags);
  507. if (status & (ATA_BUSY | ATA_DRQ))
  508. ata_sff_queue_delayed_work(&acdev->dwork, 1);
  509. else
  510. dma_complete(acdev);
  511. }
  512. static irqreturn_t arasan_cf_interrupt(int irq, void *dev)
  513. {
  514. struct arasan_cf_dev *acdev = ((struct ata_host *)dev)->private_data;
  515. unsigned long flags;
  516. u32 irqsts;
  517. irqsts = readl(acdev->vbase + GIRQ_STS);
  518. if (!(irqsts & GIRQ_CF))
  519. return IRQ_NONE;
  520. spin_lock_irqsave(&acdev->host->lock, flags);
  521. irqsts = readl(acdev->vbase + IRQ_STS);
  522. writel(irqsts, acdev->vbase + IRQ_STS); /* clear irqs */
  523. writel(GIRQ_CF, acdev->vbase + GIRQ_STS); /* clear girqs */
  524. /* handle only relevant interrupts */
  525. irqsts &= ~IGNORED_IRQS;
  526. if (irqsts & CARD_DETECT_IRQ) {
  527. cf_card_detect(acdev, 1);
  528. spin_unlock_irqrestore(&acdev->host->lock, flags);
  529. return IRQ_HANDLED;
  530. }
  531. if (irqsts & PIO_XFER_ERR_IRQ) {
  532. acdev->dma_status = ATA_DMA_ERR;
  533. writel(readl(acdev->vbase + XFER_CTR) & ~XFER_START,
  534. acdev->vbase + XFER_CTR);
  535. spin_unlock_irqrestore(&acdev->host->lock, flags);
  536. complete(&acdev->cf_completion);
  537. dev_err(acdev->host->dev, "pio xfer err irq\n");
  538. return IRQ_HANDLED;
  539. }
  540. spin_unlock_irqrestore(&acdev->host->lock, flags);
  541. if (irqsts & BUF_AVAIL_IRQ) {
  542. complete(&acdev->cf_completion);
  543. return IRQ_HANDLED;
  544. }
  545. if (irqsts & XFER_DONE_IRQ) {
  546. struct ata_queued_cmd *qc = acdev->qc;
  547. /* Send Complete only for write */
  548. if (qc->tf.flags & ATA_TFLAG_WRITE)
  549. complete(&acdev->cf_completion);
  550. }
  551. return IRQ_HANDLED;
  552. }
  553. static void arasan_cf_freeze(struct ata_port *ap)
  554. {
  555. struct arasan_cf_dev *acdev = ap->host->private_data;
  556. /* stop transfer and reset controller */
  557. writel(readl(acdev->vbase + XFER_CTR) & ~XFER_START,
  558. acdev->vbase + XFER_CTR);
  559. cf_ctrl_reset(acdev);
  560. acdev->dma_status = ATA_DMA_ERR;
  561. ata_sff_dma_pause(ap);
  562. ata_sff_freeze(ap);
  563. }
  564. static void arasan_cf_error_handler(struct ata_port *ap)
  565. {
  566. struct arasan_cf_dev *acdev = ap->host->private_data;
  567. /*
  568. * DMA transfers using an external DMA controller may be scheduled.
  569. * Abort them before handling error. Refer data_xfer() for further
  570. * details.
  571. */
  572. cancel_work_sync(&acdev->work);
  573. cancel_delayed_work_sync(&acdev->dwork);
  574. return ata_sff_error_handler(ap);
  575. }
  576. static void arasan_cf_dma_start(struct arasan_cf_dev *acdev)
  577. {
  578. struct ata_queued_cmd *qc = acdev->qc;
  579. struct ata_port *ap = qc->ap;
  580. struct ata_taskfile *tf = &qc->tf;
  581. u32 xfer_ctr = readl(acdev->vbase + XFER_CTR) & ~XFER_DIR_MASK;
  582. u32 write = tf->flags & ATA_TFLAG_WRITE;
  583. xfer_ctr |= write ? XFER_WRITE : XFER_READ;
  584. writel(xfer_ctr, acdev->vbase + XFER_CTR);
  585. ap->ops->sff_exec_command(ap, tf);
  586. ata_sff_queue_work(&acdev->work);
  587. }
  588. static unsigned int arasan_cf_qc_issue(struct ata_queued_cmd *qc)
  589. {
  590. struct ata_port *ap = qc->ap;
  591. struct arasan_cf_dev *acdev = ap->host->private_data;
  592. /* defer PIO handling to sff_qc_issue */
  593. if (!ata_is_dma(qc->tf.protocol))
  594. return ata_sff_qc_issue(qc);
  595. /* select the device */
  596. ata_wait_idle(ap);
  597. ata_sff_dev_select(ap, qc->dev->devno);
  598. ata_wait_idle(ap);
  599. /* start the command */
  600. switch (qc->tf.protocol) {
  601. case ATA_PROT_DMA:
  602. WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
  603. ap->ops->sff_tf_load(ap, &qc->tf);
  604. acdev->dma_status = 0;
  605. acdev->qc = qc;
  606. arasan_cf_dma_start(acdev);
  607. ap->hsm_task_state = HSM_ST_LAST;
  608. break;
  609. default:
  610. WARN_ON(1);
  611. return AC_ERR_SYSTEM;
  612. }
  613. return 0;
  614. }
  615. static void arasan_cf_set_piomode(struct ata_port *ap, struct ata_device *adev)
  616. {
  617. struct arasan_cf_dev *acdev = ap->host->private_data;
  618. u8 pio = adev->pio_mode - XFER_PIO_0;
  619. unsigned long flags;
  620. u32 val;
  621. /* Arasan ctrl supports Mode0 -> Mode6 */
  622. if (pio > 6) {
  623. dev_err(ap->dev, "Unknown PIO mode\n");
  624. return;
  625. }
  626. spin_lock_irqsave(&acdev->host->lock, flags);
  627. val = readl(acdev->vbase + OP_MODE) &
  628. ~(ULTRA_DMA_ENB | MULTI_WORD_DMA_ENB | DRQ_BLOCK_SIZE_MASK);
  629. writel(val, acdev->vbase + OP_MODE);
  630. val = readl(acdev->vbase + TM_CFG) & ~TRUEIDE_PIO_TIMING_MASK;
  631. val |= pio << TRUEIDE_PIO_TIMING_SHIFT;
  632. writel(val, acdev->vbase + TM_CFG);
  633. cf_interrupt_enable(acdev, BUF_AVAIL_IRQ | XFER_DONE_IRQ, 0);
  634. cf_interrupt_enable(acdev, PIO_XFER_ERR_IRQ, 1);
  635. spin_unlock_irqrestore(&acdev->host->lock, flags);
  636. }
  637. static void arasan_cf_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  638. {
  639. struct arasan_cf_dev *acdev = ap->host->private_data;
  640. u32 opmode, tmcfg, dma_mode = adev->dma_mode;
  641. unsigned long flags;
  642. spin_lock_irqsave(&acdev->host->lock, flags);
  643. opmode = readl(acdev->vbase + OP_MODE) &
  644. ~(MULTI_WORD_DMA_ENB | ULTRA_DMA_ENB);
  645. tmcfg = readl(acdev->vbase + TM_CFG);
  646. if ((dma_mode >= XFER_UDMA_0) && (dma_mode <= XFER_UDMA_6)) {
  647. opmode |= ULTRA_DMA_ENB;
  648. tmcfg &= ~ULTRA_DMA_TIMING_MASK;
  649. tmcfg |= (dma_mode - XFER_UDMA_0) << ULTRA_DMA_TIMING_SHIFT;
  650. } else if ((dma_mode >= XFER_MW_DMA_0) && (dma_mode <= XFER_MW_DMA_4)) {
  651. opmode |= MULTI_WORD_DMA_ENB;
  652. tmcfg &= ~TRUEIDE_MWORD_DMA_TIMING_MASK;
  653. tmcfg |= (dma_mode - XFER_MW_DMA_0) <<
  654. TRUEIDE_MWORD_DMA_TIMING_SHIFT;
  655. } else {
  656. dev_err(ap->dev, "Unknown DMA mode\n");
  657. spin_unlock_irqrestore(&acdev->host->lock, flags);
  658. return;
  659. }
  660. writel(opmode, acdev->vbase + OP_MODE);
  661. writel(tmcfg, acdev->vbase + TM_CFG);
  662. writel(DMA_XFER_MODE, acdev->vbase + XFER_CTR);
  663. cf_interrupt_enable(acdev, PIO_XFER_ERR_IRQ, 0);
  664. cf_interrupt_enable(acdev, BUF_AVAIL_IRQ | XFER_DONE_IRQ, 1);
  665. spin_unlock_irqrestore(&acdev->host->lock, flags);
  666. }
  667. static struct ata_port_operations arasan_cf_ops = {
  668. .inherits = &ata_sff_port_ops,
  669. .freeze = arasan_cf_freeze,
  670. .error_handler = arasan_cf_error_handler,
  671. .qc_issue = arasan_cf_qc_issue,
  672. .set_piomode = arasan_cf_set_piomode,
  673. .set_dmamode = arasan_cf_set_dmamode,
  674. };
  675. static int arasan_cf_probe(struct platform_device *pdev)
  676. {
  677. struct arasan_cf_dev *acdev;
  678. struct arasan_cf_pdata *pdata = dev_get_platdata(&pdev->dev);
  679. struct ata_host *host;
  680. struct ata_port *ap;
  681. struct resource *res;
  682. u32 quirk;
  683. irq_handler_t irq_handler = NULL;
  684. int ret = 0;
  685. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  686. if (!res)
  687. return -EINVAL;
  688. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  689. DRIVER_NAME)) {
  690. dev_warn(&pdev->dev, "Failed to get memory region resource\n");
  691. return -ENOENT;
  692. }
  693. acdev = devm_kzalloc(&pdev->dev, sizeof(*acdev), GFP_KERNEL);
  694. if (!acdev) {
  695. dev_warn(&pdev->dev, "kzalloc fail\n");
  696. return -ENOMEM;
  697. }
  698. if (pdata)
  699. quirk = pdata->quirk;
  700. else
  701. quirk = CF_BROKEN_UDMA; /* as it is on spear1340 */
  702. /* if irq is 0, support only PIO */
  703. acdev->irq = platform_get_irq(pdev, 0);
  704. if (acdev->irq)
  705. irq_handler = arasan_cf_interrupt;
  706. else
  707. quirk |= CF_BROKEN_MWDMA | CF_BROKEN_UDMA;
  708. acdev->pbase = res->start;
  709. acdev->vbase = devm_ioremap_nocache(&pdev->dev, res->start,
  710. resource_size(res));
  711. if (!acdev->vbase) {
  712. dev_warn(&pdev->dev, "ioremap fail\n");
  713. return -ENOMEM;
  714. }
  715. acdev->clk = devm_clk_get(&pdev->dev, NULL);
  716. if (IS_ERR(acdev->clk)) {
  717. dev_warn(&pdev->dev, "Clock not found\n");
  718. return PTR_ERR(acdev->clk);
  719. }
  720. /* allocate host */
  721. host = ata_host_alloc(&pdev->dev, 1);
  722. if (!host) {
  723. dev_warn(&pdev->dev, "alloc host fail\n");
  724. return -ENOMEM;
  725. }
  726. ap = host->ports[0];
  727. host->private_data = acdev;
  728. acdev->host = host;
  729. ap->ops = &arasan_cf_ops;
  730. ap->pio_mask = ATA_PIO6;
  731. ap->mwdma_mask = ATA_MWDMA4;
  732. ap->udma_mask = ATA_UDMA6;
  733. init_completion(&acdev->cf_completion);
  734. init_completion(&acdev->dma_completion);
  735. INIT_WORK(&acdev->work, data_xfer);
  736. INIT_DELAYED_WORK(&acdev->dwork, delayed_finish);
  737. dma_cap_set(DMA_MEMCPY, acdev->mask);
  738. /* Handle platform specific quirks */
  739. if (quirk) {
  740. if (quirk & CF_BROKEN_PIO) {
  741. ap->ops->set_piomode = NULL;
  742. ap->pio_mask = 0;
  743. }
  744. if (quirk & CF_BROKEN_MWDMA)
  745. ap->mwdma_mask = 0;
  746. if (quirk & CF_BROKEN_UDMA)
  747. ap->udma_mask = 0;
  748. }
  749. ap->flags |= ATA_FLAG_PIO_POLLING | ATA_FLAG_NO_ATAPI;
  750. ap->ioaddr.cmd_addr = acdev->vbase + ATA_DATA_PORT;
  751. ap->ioaddr.data_addr = acdev->vbase + ATA_DATA_PORT;
  752. ap->ioaddr.error_addr = acdev->vbase + ATA_ERR_FTR;
  753. ap->ioaddr.feature_addr = acdev->vbase + ATA_ERR_FTR;
  754. ap->ioaddr.nsect_addr = acdev->vbase + ATA_SC;
  755. ap->ioaddr.lbal_addr = acdev->vbase + ATA_SN;
  756. ap->ioaddr.lbam_addr = acdev->vbase + ATA_CL;
  757. ap->ioaddr.lbah_addr = acdev->vbase + ATA_CH;
  758. ap->ioaddr.device_addr = acdev->vbase + ATA_SH;
  759. ap->ioaddr.status_addr = acdev->vbase + ATA_STS_CMD;
  760. ap->ioaddr.command_addr = acdev->vbase + ATA_STS_CMD;
  761. ap->ioaddr.altstatus_addr = acdev->vbase + ATA_ASTS_DCTR;
  762. ap->ioaddr.ctl_addr = acdev->vbase + ATA_ASTS_DCTR;
  763. ata_port_desc(ap, "phy_addr %llx virt_addr %p",
  764. (unsigned long long) res->start, acdev->vbase);
  765. ret = cf_init(acdev);
  766. if (ret)
  767. return ret;
  768. cf_card_detect(acdev, 0);
  769. ret = ata_host_activate(host, acdev->irq, irq_handler, 0,
  770. &arasan_cf_sht);
  771. if (!ret)
  772. return 0;
  773. cf_exit(acdev);
  774. return ret;
  775. }
  776. static int arasan_cf_remove(struct platform_device *pdev)
  777. {
  778. struct ata_host *host = platform_get_drvdata(pdev);
  779. struct arasan_cf_dev *acdev = host->ports[0]->private_data;
  780. ata_host_detach(host);
  781. cf_exit(acdev);
  782. return 0;
  783. }
  784. #ifdef CONFIG_PM_SLEEP
  785. static int arasan_cf_suspend(struct device *dev)
  786. {
  787. struct ata_host *host = dev_get_drvdata(dev);
  788. struct arasan_cf_dev *acdev = host->ports[0]->private_data;
  789. if (acdev->dma_chan)
  790. dmaengine_terminate_all(acdev->dma_chan);
  791. cf_exit(acdev);
  792. return ata_host_suspend(host, PMSG_SUSPEND);
  793. }
  794. static int arasan_cf_resume(struct device *dev)
  795. {
  796. struct ata_host *host = dev_get_drvdata(dev);
  797. struct arasan_cf_dev *acdev = host->ports[0]->private_data;
  798. cf_init(acdev);
  799. ata_host_resume(host);
  800. return 0;
  801. }
  802. #endif
  803. static SIMPLE_DEV_PM_OPS(arasan_cf_pm_ops, arasan_cf_suspend, arasan_cf_resume);
  804. #ifdef CONFIG_OF
  805. static const struct of_device_id arasan_cf_id_table[] = {
  806. { .compatible = "arasan,cf-spear1340" },
  807. {}
  808. };
  809. MODULE_DEVICE_TABLE(of, arasan_cf_id_table);
  810. #endif
  811. static struct platform_driver arasan_cf_driver = {
  812. .probe = arasan_cf_probe,
  813. .remove = arasan_cf_remove,
  814. .driver = {
  815. .name = DRIVER_NAME,
  816. .pm = &arasan_cf_pm_ops,
  817. .of_match_table = of_match_ptr(arasan_cf_id_table),
  818. },
  819. };
  820. module_platform_driver(arasan_cf_driver);
  821. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
  822. MODULE_DESCRIPTION("Arasan ATA Compact Flash driver");
  823. MODULE_LICENSE("GPL");
  824. MODULE_ALIAS("platform:" DRIVER_NAME);