pata_ep93xx.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * EP93XX PATA controller driver.
  4. *
  5. * Copyright (c) 2012, Metasoft s.c.
  6. * Rafal Prylowski <[email protected]>
  7. *
  8. * Based on pata_scc.c, pata_icside.c and on earlier version of EP93XX
  9. * PATA driver by Lennert Buytenhek and Alessandro Zummo.
  10. * Read/Write timings, resource management and other improvements
  11. * from driver by Joao Ramos and Bartlomiej Zolnierkiewicz.
  12. * DMA engine support based on spi-ep93xx.c by Mika Westerberg.
  13. *
  14. * Original copyrights:
  15. *
  16. * Support for Cirrus Logic's EP93xx (EP9312, EP9315) CPUs
  17. * PATA host controller driver.
  18. *
  19. * Copyright (c) 2009, Bartlomiej Zolnierkiewicz
  20. *
  21. * Heavily based on the ep93xx-ide.c driver:
  22. *
  23. * Copyright (c) 2009, Joao Ramos <[email protected]>
  24. * INESC Inovacao (INOV)
  25. *
  26. * EP93XX PATA controller driver.
  27. * Copyright (C) 2007 Lennert Buytenhek <[email protected]>
  28. *
  29. * An ATA driver for the Cirrus Logic EP93xx PATA controller.
  30. *
  31. * Based on an earlier version by Alessandro Zummo, which is:
  32. * Copyright (C) 2006 Tower Technologies
  33. */
  34. #include <linux/err.h>
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/blkdev.h>
  38. #include <scsi/scsi_host.h>
  39. #include <linux/ata.h>
  40. #include <linux/libata.h>
  41. #include <linux/platform_device.h>
  42. #include <linux/delay.h>
  43. #include <linux/dmaengine.h>
  44. #include <linux/ktime.h>
  45. #include <linux/platform_data/dma-ep93xx.h>
  46. #include <linux/soc/cirrus/ep93xx.h>
  47. #define DRV_NAME "ep93xx-ide"
  48. #define DRV_VERSION "1.0"
  49. enum {
  50. /* IDE Control Register */
  51. IDECTRL = 0x00,
  52. IDECTRL_CS0N = (1 << 0),
  53. IDECTRL_CS1N = (1 << 1),
  54. IDECTRL_DIORN = (1 << 5),
  55. IDECTRL_DIOWN = (1 << 6),
  56. IDECTRL_INTRQ = (1 << 9),
  57. IDECTRL_IORDY = (1 << 10),
  58. /*
  59. * the device IDE register to be accessed is selected through
  60. * IDECTRL register's specific bitfields 'DA', 'CS1N' and 'CS0N':
  61. * b4 b3 b2 b1 b0
  62. * A2 A1 A0 CS1N CS0N
  63. * the values filled in this structure allows the value to be directly
  64. * ORed to the IDECTRL register, hence giving directly the A[2:0] and
  65. * CS1N/CS0N values for each IDE register.
  66. * The values correspond to the transformation:
  67. * ((real IDE address) << 2) | CS1N value << 1 | CS0N value
  68. */
  69. IDECTRL_ADDR_CMD = 0 + 2, /* CS1 */
  70. IDECTRL_ADDR_DATA = (ATA_REG_DATA << 2) + 2,
  71. IDECTRL_ADDR_ERROR = (ATA_REG_ERR << 2) + 2,
  72. IDECTRL_ADDR_FEATURE = (ATA_REG_FEATURE << 2) + 2,
  73. IDECTRL_ADDR_NSECT = (ATA_REG_NSECT << 2) + 2,
  74. IDECTRL_ADDR_LBAL = (ATA_REG_LBAL << 2) + 2,
  75. IDECTRL_ADDR_LBAM = (ATA_REG_LBAM << 2) + 2,
  76. IDECTRL_ADDR_LBAH = (ATA_REG_LBAH << 2) + 2,
  77. IDECTRL_ADDR_DEVICE = (ATA_REG_DEVICE << 2) + 2,
  78. IDECTRL_ADDR_STATUS = (ATA_REG_STATUS << 2) + 2,
  79. IDECTRL_ADDR_COMMAND = (ATA_REG_CMD << 2) + 2,
  80. IDECTRL_ADDR_ALTSTATUS = (0x06 << 2) + 1, /* CS0 */
  81. IDECTRL_ADDR_CTL = (0x06 << 2) + 1, /* CS0 */
  82. /* IDE Configuration Register */
  83. IDECFG = 0x04,
  84. IDECFG_IDEEN = (1 << 0),
  85. IDECFG_PIO = (1 << 1),
  86. IDECFG_MDMA = (1 << 2),
  87. IDECFG_UDMA = (1 << 3),
  88. IDECFG_MODE_SHIFT = 4,
  89. IDECFG_MODE_MASK = (0xf << 4),
  90. IDECFG_WST_SHIFT = 8,
  91. IDECFG_WST_MASK = (0x3 << 8),
  92. /* MDMA Operation Register */
  93. IDEMDMAOP = 0x08,
  94. /* UDMA Operation Register */
  95. IDEUDMAOP = 0x0c,
  96. IDEUDMAOP_UEN = (1 << 0),
  97. IDEUDMAOP_RWOP = (1 << 1),
  98. /* PIO/MDMA/UDMA Data Registers */
  99. IDEDATAOUT = 0x10,
  100. IDEDATAIN = 0x14,
  101. IDEMDMADATAOUT = 0x18,
  102. IDEMDMADATAIN = 0x1c,
  103. IDEUDMADATAOUT = 0x20,
  104. IDEUDMADATAIN = 0x24,
  105. /* UDMA Status Register */
  106. IDEUDMASTS = 0x28,
  107. IDEUDMASTS_DMAIDE = (1 << 16),
  108. IDEUDMASTS_INTIDE = (1 << 17),
  109. IDEUDMASTS_SBUSY = (1 << 18),
  110. IDEUDMASTS_NDO = (1 << 24),
  111. IDEUDMASTS_NDI = (1 << 25),
  112. IDEUDMASTS_N4X = (1 << 26),
  113. /* UDMA Debug Status Register */
  114. IDEUDMADEBUG = 0x2c,
  115. };
  116. struct ep93xx_pata_data {
  117. const struct platform_device *pdev;
  118. void __iomem *ide_base;
  119. struct ata_timing t;
  120. bool iordy;
  121. unsigned long udma_in_phys;
  122. unsigned long udma_out_phys;
  123. struct dma_chan *dma_rx_channel;
  124. struct ep93xx_dma_data dma_rx_data;
  125. struct dma_chan *dma_tx_channel;
  126. struct ep93xx_dma_data dma_tx_data;
  127. };
  128. static void ep93xx_pata_clear_regs(void __iomem *base)
  129. {
  130. writel(IDECTRL_CS0N | IDECTRL_CS1N | IDECTRL_DIORN |
  131. IDECTRL_DIOWN, base + IDECTRL);
  132. writel(0, base + IDECFG);
  133. writel(0, base + IDEMDMAOP);
  134. writel(0, base + IDEUDMAOP);
  135. writel(0, base + IDEDATAOUT);
  136. writel(0, base + IDEDATAIN);
  137. writel(0, base + IDEMDMADATAOUT);
  138. writel(0, base + IDEMDMADATAIN);
  139. writel(0, base + IDEUDMADATAOUT);
  140. writel(0, base + IDEUDMADATAIN);
  141. writel(0, base + IDEUDMADEBUG);
  142. }
  143. static bool ep93xx_pata_check_iordy(void __iomem *base)
  144. {
  145. return !!(readl(base + IDECTRL) & IDECTRL_IORDY);
  146. }
  147. /*
  148. * According to EP93xx User's Guide, WST field of IDECFG specifies number
  149. * of HCLK cycles to hold the data bus after a PIO write operation.
  150. * It should be programmed to guarantee following delays:
  151. *
  152. * PIO Mode [ns]
  153. * 0 30
  154. * 1 20
  155. * 2 15
  156. * 3 10
  157. * 4 5
  158. *
  159. * Maximum possible value for HCLK is 100MHz.
  160. */
  161. static int ep93xx_pata_get_wst(int pio_mode)
  162. {
  163. int val;
  164. if (pio_mode == 0)
  165. val = 3;
  166. else if (pio_mode < 3)
  167. val = 2;
  168. else
  169. val = 1;
  170. return val << IDECFG_WST_SHIFT;
  171. }
  172. static void ep93xx_pata_enable_pio(void __iomem *base, int pio_mode)
  173. {
  174. writel(IDECFG_IDEEN | IDECFG_PIO |
  175. ep93xx_pata_get_wst(pio_mode) |
  176. (pio_mode << IDECFG_MODE_SHIFT), base + IDECFG);
  177. }
  178. /*
  179. * Based on delay loop found in mach-pxa/mp900.c.
  180. *
  181. * Single iteration should take 5 cpu cycles. This is 25ns assuming the
  182. * fastest ep93xx cpu speed (200MHz) and is better optimized for PIO4 timings
  183. * than eg. 20ns.
  184. */
  185. static void ep93xx_pata_delay(unsigned long count)
  186. {
  187. __asm__ volatile (
  188. "0:\n"
  189. "mov r0, r0\n"
  190. "subs %0, %1, #1\n"
  191. "bge 0b\n"
  192. : "=r" (count)
  193. : "0" (count)
  194. );
  195. }
  196. static unsigned long ep93xx_pata_wait_for_iordy(void __iomem *base,
  197. unsigned long t2)
  198. {
  199. /*
  200. * According to ATA specification, IORDY pin can be first sampled
  201. * tA = 35ns after activation of DIOR-/DIOW-. Maximum IORDY pulse
  202. * width is tB = 1250ns.
  203. *
  204. * We are already t2 delay loop iterations after activation of
  205. * DIOR-/DIOW-, so we set timeout to (1250 + 35) / 25 - t2 additional
  206. * delay loop iterations.
  207. */
  208. unsigned long start = (1250 + 35) / 25 - t2;
  209. unsigned long counter = start;
  210. while (!ep93xx_pata_check_iordy(base) && counter--)
  211. ep93xx_pata_delay(1);
  212. return start - counter;
  213. }
  214. /* common part at start of ep93xx_pata_read/write() */
  215. static void ep93xx_pata_rw_begin(void __iomem *base, unsigned long addr,
  216. unsigned long t1)
  217. {
  218. writel(IDECTRL_DIOWN | IDECTRL_DIORN | addr, base + IDECTRL);
  219. ep93xx_pata_delay(t1);
  220. }
  221. /* common part at end of ep93xx_pata_read/write() */
  222. static void ep93xx_pata_rw_end(void __iomem *base, unsigned long addr,
  223. bool iordy, unsigned long t0, unsigned long t2,
  224. unsigned long t2i)
  225. {
  226. ep93xx_pata_delay(t2);
  227. /* lengthen t2 if needed */
  228. if (iordy)
  229. t2 += ep93xx_pata_wait_for_iordy(base, t2);
  230. writel(IDECTRL_DIOWN | IDECTRL_DIORN | addr, base + IDECTRL);
  231. if (t0 > t2 && t0 - t2 > t2i)
  232. ep93xx_pata_delay(t0 - t2);
  233. else
  234. ep93xx_pata_delay(t2i);
  235. }
  236. static u16 ep93xx_pata_read(struct ep93xx_pata_data *drv_data,
  237. unsigned long addr,
  238. bool reg)
  239. {
  240. void __iomem *base = drv_data->ide_base;
  241. const struct ata_timing *t = &drv_data->t;
  242. unsigned long t0 = reg ? t->cyc8b : t->cycle;
  243. unsigned long t2 = reg ? t->act8b : t->active;
  244. unsigned long t2i = reg ? t->rec8b : t->recover;
  245. ep93xx_pata_rw_begin(base, addr, t->setup);
  246. writel(IDECTRL_DIOWN | addr, base + IDECTRL);
  247. /*
  248. * The IDEDATAIN register is loaded from the DD pins at the positive
  249. * edge of the DIORN signal. (EP93xx UG p27-14)
  250. */
  251. ep93xx_pata_rw_end(base, addr, drv_data->iordy, t0, t2, t2i);
  252. return readl(base + IDEDATAIN);
  253. }
  254. /* IDE register read */
  255. static u16 ep93xx_pata_read_reg(struct ep93xx_pata_data *drv_data,
  256. unsigned long addr)
  257. {
  258. return ep93xx_pata_read(drv_data, addr, true);
  259. }
  260. /* PIO data read */
  261. static u16 ep93xx_pata_read_data(struct ep93xx_pata_data *drv_data,
  262. unsigned long addr)
  263. {
  264. return ep93xx_pata_read(drv_data, addr, false);
  265. }
  266. static void ep93xx_pata_write(struct ep93xx_pata_data *drv_data,
  267. u16 value, unsigned long addr,
  268. bool reg)
  269. {
  270. void __iomem *base = drv_data->ide_base;
  271. const struct ata_timing *t = &drv_data->t;
  272. unsigned long t0 = reg ? t->cyc8b : t->cycle;
  273. unsigned long t2 = reg ? t->act8b : t->active;
  274. unsigned long t2i = reg ? t->rec8b : t->recover;
  275. ep93xx_pata_rw_begin(base, addr, t->setup);
  276. /*
  277. * Value from IDEDATAOUT register is driven onto the DD pins when
  278. * DIOWN is low. (EP93xx UG p27-13)
  279. */
  280. writel(value, base + IDEDATAOUT);
  281. writel(IDECTRL_DIORN | addr, base + IDECTRL);
  282. ep93xx_pata_rw_end(base, addr, drv_data->iordy, t0, t2, t2i);
  283. }
  284. /* IDE register write */
  285. static void ep93xx_pata_write_reg(struct ep93xx_pata_data *drv_data,
  286. u16 value, unsigned long addr)
  287. {
  288. ep93xx_pata_write(drv_data, value, addr, true);
  289. }
  290. /* PIO data write */
  291. static void ep93xx_pata_write_data(struct ep93xx_pata_data *drv_data,
  292. u16 value, unsigned long addr)
  293. {
  294. ep93xx_pata_write(drv_data, value, addr, false);
  295. }
  296. static void ep93xx_pata_set_piomode(struct ata_port *ap,
  297. struct ata_device *adev)
  298. {
  299. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  300. struct ata_device *pair = ata_dev_pair(adev);
  301. /*
  302. * Calculate timings for the delay loop, assuming ep93xx cpu speed
  303. * is 200MHz (maximum possible for ep93xx). If actual cpu speed is
  304. * slower, we will wait a bit longer in each delay.
  305. * Additional division of cpu speed by 5, because single iteration
  306. * of our delay loop takes 5 cpu cycles (25ns).
  307. */
  308. unsigned long T = 1000000 / (200 / 5);
  309. ata_timing_compute(adev, adev->pio_mode, &drv_data->t, T, 0);
  310. if (pair && pair->pio_mode) {
  311. struct ata_timing t;
  312. ata_timing_compute(pair, pair->pio_mode, &t, T, 0);
  313. ata_timing_merge(&t, &drv_data->t, &drv_data->t,
  314. ATA_TIMING_SETUP | ATA_TIMING_8BIT);
  315. }
  316. drv_data->iordy = ata_pio_need_iordy(adev);
  317. ep93xx_pata_enable_pio(drv_data->ide_base,
  318. adev->pio_mode - XFER_PIO_0);
  319. }
  320. /* Note: original code is ata_sff_check_status */
  321. static u8 ep93xx_pata_check_status(struct ata_port *ap)
  322. {
  323. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  324. return ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_STATUS);
  325. }
  326. static u8 ep93xx_pata_check_altstatus(struct ata_port *ap)
  327. {
  328. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  329. return ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_ALTSTATUS);
  330. }
  331. /* Note: original code is ata_sff_tf_load */
  332. static void ep93xx_pata_tf_load(struct ata_port *ap,
  333. const struct ata_taskfile *tf)
  334. {
  335. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  336. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  337. if (tf->ctl != ap->last_ctl) {
  338. ep93xx_pata_write_reg(drv_data, tf->ctl, IDECTRL_ADDR_CTL);
  339. ap->last_ctl = tf->ctl;
  340. ata_wait_idle(ap);
  341. }
  342. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  343. ep93xx_pata_write_reg(drv_data, tf->hob_feature,
  344. IDECTRL_ADDR_FEATURE);
  345. ep93xx_pata_write_reg(drv_data, tf->hob_nsect,
  346. IDECTRL_ADDR_NSECT);
  347. ep93xx_pata_write_reg(drv_data, tf->hob_lbal,
  348. IDECTRL_ADDR_LBAL);
  349. ep93xx_pata_write_reg(drv_data, tf->hob_lbam,
  350. IDECTRL_ADDR_LBAM);
  351. ep93xx_pata_write_reg(drv_data, tf->hob_lbah,
  352. IDECTRL_ADDR_LBAH);
  353. }
  354. if (is_addr) {
  355. ep93xx_pata_write_reg(drv_data, tf->feature,
  356. IDECTRL_ADDR_FEATURE);
  357. ep93xx_pata_write_reg(drv_data, tf->nsect, IDECTRL_ADDR_NSECT);
  358. ep93xx_pata_write_reg(drv_data, tf->lbal, IDECTRL_ADDR_LBAL);
  359. ep93xx_pata_write_reg(drv_data, tf->lbam, IDECTRL_ADDR_LBAM);
  360. ep93xx_pata_write_reg(drv_data, tf->lbah, IDECTRL_ADDR_LBAH);
  361. }
  362. if (tf->flags & ATA_TFLAG_DEVICE)
  363. ep93xx_pata_write_reg(drv_data, tf->device,
  364. IDECTRL_ADDR_DEVICE);
  365. ata_wait_idle(ap);
  366. }
  367. /* Note: original code is ata_sff_tf_read */
  368. static void ep93xx_pata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  369. {
  370. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  371. tf->status = ep93xx_pata_check_status(ap);
  372. tf->error = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_FEATURE);
  373. tf->nsect = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_NSECT);
  374. tf->lbal = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_LBAL);
  375. tf->lbam = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_LBAM);
  376. tf->lbah = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_LBAH);
  377. tf->device = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_DEVICE);
  378. if (tf->flags & ATA_TFLAG_LBA48) {
  379. ep93xx_pata_write_reg(drv_data, tf->ctl | ATA_HOB,
  380. IDECTRL_ADDR_CTL);
  381. tf->hob_feature = ep93xx_pata_read_reg(drv_data,
  382. IDECTRL_ADDR_FEATURE);
  383. tf->hob_nsect = ep93xx_pata_read_reg(drv_data,
  384. IDECTRL_ADDR_NSECT);
  385. tf->hob_lbal = ep93xx_pata_read_reg(drv_data,
  386. IDECTRL_ADDR_LBAL);
  387. tf->hob_lbam = ep93xx_pata_read_reg(drv_data,
  388. IDECTRL_ADDR_LBAM);
  389. tf->hob_lbah = ep93xx_pata_read_reg(drv_data,
  390. IDECTRL_ADDR_LBAH);
  391. ep93xx_pata_write_reg(drv_data, tf->ctl, IDECTRL_ADDR_CTL);
  392. ap->last_ctl = tf->ctl;
  393. }
  394. }
  395. /* Note: original code is ata_sff_exec_command */
  396. static void ep93xx_pata_exec_command(struct ata_port *ap,
  397. const struct ata_taskfile *tf)
  398. {
  399. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  400. ep93xx_pata_write_reg(drv_data, tf->command,
  401. IDECTRL_ADDR_COMMAND);
  402. ata_sff_pause(ap);
  403. }
  404. /* Note: original code is ata_sff_dev_select */
  405. static void ep93xx_pata_dev_select(struct ata_port *ap, unsigned int device)
  406. {
  407. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  408. u8 tmp = ATA_DEVICE_OBS;
  409. if (device != 0)
  410. tmp |= ATA_DEV1;
  411. ep93xx_pata_write_reg(drv_data, tmp, IDECTRL_ADDR_DEVICE);
  412. ata_sff_pause(ap); /* needed; also flushes, for mmio */
  413. }
  414. /* Note: original code is ata_sff_set_devctl */
  415. static void ep93xx_pata_set_devctl(struct ata_port *ap, u8 ctl)
  416. {
  417. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  418. ep93xx_pata_write_reg(drv_data, ctl, IDECTRL_ADDR_CTL);
  419. }
  420. /* Note: original code is ata_sff_data_xfer */
  421. static unsigned int ep93xx_pata_data_xfer(struct ata_queued_cmd *qc,
  422. unsigned char *buf,
  423. unsigned int buflen, int rw)
  424. {
  425. struct ata_port *ap = qc->dev->link->ap;
  426. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  427. u16 *data = (u16 *)buf;
  428. unsigned int words = buflen >> 1;
  429. /* Transfer multiple of 2 bytes */
  430. while (words--)
  431. if (rw == READ)
  432. *data++ = cpu_to_le16(
  433. ep93xx_pata_read_data(
  434. drv_data, IDECTRL_ADDR_DATA));
  435. else
  436. ep93xx_pata_write_data(drv_data, le16_to_cpu(*data++),
  437. IDECTRL_ADDR_DATA);
  438. /* Transfer trailing 1 byte, if any. */
  439. if (unlikely(buflen & 0x01)) {
  440. unsigned char pad[2] = { };
  441. buf += buflen - 1;
  442. if (rw == READ) {
  443. *pad = cpu_to_le16(
  444. ep93xx_pata_read_data(
  445. drv_data, IDECTRL_ADDR_DATA));
  446. *buf = pad[0];
  447. } else {
  448. pad[0] = *buf;
  449. ep93xx_pata_write_data(drv_data, le16_to_cpu(*pad),
  450. IDECTRL_ADDR_DATA);
  451. }
  452. words++;
  453. }
  454. return words << 1;
  455. }
  456. /* Note: original code is ata_devchk */
  457. static bool ep93xx_pata_device_is_present(struct ata_port *ap,
  458. unsigned int device)
  459. {
  460. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  461. u8 nsect, lbal;
  462. ap->ops->sff_dev_select(ap, device);
  463. ep93xx_pata_write_reg(drv_data, 0x55, IDECTRL_ADDR_NSECT);
  464. ep93xx_pata_write_reg(drv_data, 0xaa, IDECTRL_ADDR_LBAL);
  465. ep93xx_pata_write_reg(drv_data, 0xaa, IDECTRL_ADDR_NSECT);
  466. ep93xx_pata_write_reg(drv_data, 0x55, IDECTRL_ADDR_LBAL);
  467. ep93xx_pata_write_reg(drv_data, 0x55, IDECTRL_ADDR_NSECT);
  468. ep93xx_pata_write_reg(drv_data, 0xaa, IDECTRL_ADDR_LBAL);
  469. nsect = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_NSECT);
  470. lbal = ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_LBAL);
  471. if ((nsect == 0x55) && (lbal == 0xaa))
  472. return true;
  473. return false;
  474. }
  475. /* Note: original code is ata_sff_wait_after_reset */
  476. static int ep93xx_pata_wait_after_reset(struct ata_link *link,
  477. unsigned int devmask,
  478. unsigned long deadline)
  479. {
  480. struct ata_port *ap = link->ap;
  481. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  482. unsigned int dev0 = devmask & (1 << 0);
  483. unsigned int dev1 = devmask & (1 << 1);
  484. int rc, ret = 0;
  485. ata_msleep(ap, ATA_WAIT_AFTER_RESET);
  486. /* always check readiness of the master device */
  487. rc = ata_sff_wait_ready(link, deadline);
  488. /*
  489. * -ENODEV means the odd clown forgot the D7 pulldown resistor
  490. * and TF status is 0xff, bail out on it too.
  491. */
  492. if (rc)
  493. return rc;
  494. /*
  495. * if device 1 was found in ata_devchk, wait for register
  496. * access briefly, then wait for BSY to clear.
  497. */
  498. if (dev1) {
  499. int i;
  500. ap->ops->sff_dev_select(ap, 1);
  501. /*
  502. * Wait for register access. Some ATAPI devices fail
  503. * to set nsect/lbal after reset, so don't waste too
  504. * much time on it. We're gonna wait for !BSY anyway.
  505. */
  506. for (i = 0; i < 2; i++) {
  507. u8 nsect, lbal;
  508. nsect = ep93xx_pata_read_reg(drv_data,
  509. IDECTRL_ADDR_NSECT);
  510. lbal = ep93xx_pata_read_reg(drv_data,
  511. IDECTRL_ADDR_LBAL);
  512. if (nsect == 1 && lbal == 1)
  513. break;
  514. msleep(50); /* give drive a breather */
  515. }
  516. rc = ata_sff_wait_ready(link, deadline);
  517. if (rc) {
  518. if (rc != -ENODEV)
  519. return rc;
  520. ret = rc;
  521. }
  522. }
  523. /* is all this really necessary? */
  524. ap->ops->sff_dev_select(ap, 0);
  525. if (dev1)
  526. ap->ops->sff_dev_select(ap, 1);
  527. if (dev0)
  528. ap->ops->sff_dev_select(ap, 0);
  529. return ret;
  530. }
  531. /* Note: original code is ata_bus_softreset */
  532. static int ep93xx_pata_bus_softreset(struct ata_port *ap, unsigned int devmask,
  533. unsigned long deadline)
  534. {
  535. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  536. ep93xx_pata_write_reg(drv_data, ap->ctl, IDECTRL_ADDR_CTL);
  537. udelay(20); /* FIXME: flush */
  538. ep93xx_pata_write_reg(drv_data, ap->ctl | ATA_SRST, IDECTRL_ADDR_CTL);
  539. udelay(20); /* FIXME: flush */
  540. ep93xx_pata_write_reg(drv_data, ap->ctl, IDECTRL_ADDR_CTL);
  541. ap->last_ctl = ap->ctl;
  542. return ep93xx_pata_wait_after_reset(&ap->link, devmask, deadline);
  543. }
  544. static void ep93xx_pata_release_dma(struct ep93xx_pata_data *drv_data)
  545. {
  546. if (drv_data->dma_rx_channel) {
  547. dma_release_channel(drv_data->dma_rx_channel);
  548. drv_data->dma_rx_channel = NULL;
  549. }
  550. if (drv_data->dma_tx_channel) {
  551. dma_release_channel(drv_data->dma_tx_channel);
  552. drv_data->dma_tx_channel = NULL;
  553. }
  554. }
  555. static bool ep93xx_pata_dma_filter(struct dma_chan *chan, void *filter_param)
  556. {
  557. if (ep93xx_dma_chan_is_m2p(chan))
  558. return false;
  559. chan->private = filter_param;
  560. return true;
  561. }
  562. static void ep93xx_pata_dma_init(struct ep93xx_pata_data *drv_data)
  563. {
  564. const struct platform_device *pdev = drv_data->pdev;
  565. dma_cap_mask_t mask;
  566. struct dma_slave_config conf;
  567. dma_cap_zero(mask);
  568. dma_cap_set(DMA_SLAVE, mask);
  569. /*
  570. * Request two channels for IDE. Another possibility would be
  571. * to request only one channel, and reprogram it's direction at
  572. * start of new transfer.
  573. */
  574. drv_data->dma_rx_data.port = EP93XX_DMA_IDE;
  575. drv_data->dma_rx_data.direction = DMA_DEV_TO_MEM;
  576. drv_data->dma_rx_data.name = "ep93xx-pata-rx";
  577. drv_data->dma_rx_channel = dma_request_channel(mask,
  578. ep93xx_pata_dma_filter, &drv_data->dma_rx_data);
  579. if (!drv_data->dma_rx_channel)
  580. return;
  581. drv_data->dma_tx_data.port = EP93XX_DMA_IDE;
  582. drv_data->dma_tx_data.direction = DMA_MEM_TO_DEV;
  583. drv_data->dma_tx_data.name = "ep93xx-pata-tx";
  584. drv_data->dma_tx_channel = dma_request_channel(mask,
  585. ep93xx_pata_dma_filter, &drv_data->dma_tx_data);
  586. if (!drv_data->dma_tx_channel) {
  587. dma_release_channel(drv_data->dma_rx_channel);
  588. return;
  589. }
  590. /* Configure receive channel direction and source address */
  591. memset(&conf, 0, sizeof(conf));
  592. conf.direction = DMA_DEV_TO_MEM;
  593. conf.src_addr = drv_data->udma_in_phys;
  594. conf.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  595. if (dmaengine_slave_config(drv_data->dma_rx_channel, &conf)) {
  596. dev_err(&pdev->dev, "failed to configure rx dma channel\n");
  597. ep93xx_pata_release_dma(drv_data);
  598. return;
  599. }
  600. /* Configure transmit channel direction and destination address */
  601. memset(&conf, 0, sizeof(conf));
  602. conf.direction = DMA_MEM_TO_DEV;
  603. conf.dst_addr = drv_data->udma_out_phys;
  604. conf.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  605. if (dmaengine_slave_config(drv_data->dma_tx_channel, &conf)) {
  606. dev_err(&pdev->dev, "failed to configure tx dma channel\n");
  607. ep93xx_pata_release_dma(drv_data);
  608. }
  609. }
  610. static void ep93xx_pata_dma_start(struct ata_queued_cmd *qc)
  611. {
  612. struct dma_async_tx_descriptor *txd;
  613. struct ep93xx_pata_data *drv_data = qc->ap->host->private_data;
  614. void __iomem *base = drv_data->ide_base;
  615. struct ata_device *adev = qc->dev;
  616. u32 v = qc->dma_dir == DMA_TO_DEVICE ? IDEUDMAOP_RWOP : 0;
  617. struct dma_chan *channel = qc->dma_dir == DMA_TO_DEVICE
  618. ? drv_data->dma_tx_channel : drv_data->dma_rx_channel;
  619. txd = dmaengine_prep_slave_sg(channel, qc->sg, qc->n_elem, qc->dma_dir,
  620. DMA_CTRL_ACK);
  621. if (!txd) {
  622. dev_err(qc->ap->dev, "failed to prepare slave for sg dma\n");
  623. return;
  624. }
  625. txd->callback = NULL;
  626. txd->callback_param = NULL;
  627. if (dmaengine_submit(txd) < 0) {
  628. dev_err(qc->ap->dev, "failed to submit dma transfer\n");
  629. return;
  630. }
  631. dma_async_issue_pending(channel);
  632. /*
  633. * When enabling UDMA operation, IDEUDMAOP register needs to be
  634. * programmed in three step sequence:
  635. * 1) set or clear the RWOP bit,
  636. * 2) perform dummy read of the register,
  637. * 3) set the UEN bit.
  638. */
  639. writel(v, base + IDEUDMAOP);
  640. readl(base + IDEUDMAOP);
  641. writel(v | IDEUDMAOP_UEN, base + IDEUDMAOP);
  642. writel(IDECFG_IDEEN | IDECFG_UDMA |
  643. ((adev->xfer_mode - XFER_UDMA_0) << IDECFG_MODE_SHIFT),
  644. base + IDECFG);
  645. }
  646. static void ep93xx_pata_dma_stop(struct ata_queued_cmd *qc)
  647. {
  648. struct ep93xx_pata_data *drv_data = qc->ap->host->private_data;
  649. void __iomem *base = drv_data->ide_base;
  650. /* terminate all dma transfers, if not yet finished */
  651. dmaengine_terminate_all(drv_data->dma_rx_channel);
  652. dmaengine_terminate_all(drv_data->dma_tx_channel);
  653. /*
  654. * To properly stop IDE-DMA, IDEUDMAOP register must to be cleared
  655. * and IDECTRL register must be set to default value.
  656. */
  657. writel(0, base + IDEUDMAOP);
  658. writel(readl(base + IDECTRL) | IDECTRL_DIOWN | IDECTRL_DIORN |
  659. IDECTRL_CS0N | IDECTRL_CS1N, base + IDECTRL);
  660. ep93xx_pata_enable_pio(drv_data->ide_base,
  661. qc->dev->pio_mode - XFER_PIO_0);
  662. ata_sff_dma_pause(qc->ap);
  663. }
  664. static void ep93xx_pata_dma_setup(struct ata_queued_cmd *qc)
  665. {
  666. qc->ap->ops->sff_exec_command(qc->ap, &qc->tf);
  667. }
  668. static u8 ep93xx_pata_dma_status(struct ata_port *ap)
  669. {
  670. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  671. u32 val = readl(drv_data->ide_base + IDEUDMASTS);
  672. /*
  673. * UDMA Status Register bits:
  674. *
  675. * DMAIDE - DMA request signal from UDMA state machine,
  676. * INTIDE - INT line generated by UDMA because of errors in the
  677. * state machine,
  678. * SBUSY - UDMA state machine busy, not in idle state,
  679. * NDO - error for data-out not completed,
  680. * NDI - error for data-in not completed,
  681. * N4X - error for data transferred not multiplies of four
  682. * 32-bit words.
  683. * (EP93xx UG p27-17)
  684. */
  685. if (val & IDEUDMASTS_NDO || val & IDEUDMASTS_NDI ||
  686. val & IDEUDMASTS_N4X || val & IDEUDMASTS_INTIDE)
  687. return ATA_DMA_ERR;
  688. /* read INTRQ (INT[3]) pin input state */
  689. if (readl(drv_data->ide_base + IDECTRL) & IDECTRL_INTRQ)
  690. return ATA_DMA_INTR;
  691. if (val & IDEUDMASTS_SBUSY || val & IDEUDMASTS_DMAIDE)
  692. return ATA_DMA_ACTIVE;
  693. return 0;
  694. }
  695. /* Note: original code is ata_sff_softreset */
  696. static int ep93xx_pata_softreset(struct ata_link *al, unsigned int *classes,
  697. unsigned long deadline)
  698. {
  699. struct ata_port *ap = al->ap;
  700. unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
  701. unsigned int devmask = 0;
  702. int rc;
  703. u8 err;
  704. /* determine if device 0/1 are present */
  705. if (ep93xx_pata_device_is_present(ap, 0))
  706. devmask |= (1 << 0);
  707. if (slave_possible && ep93xx_pata_device_is_present(ap, 1))
  708. devmask |= (1 << 1);
  709. /* select device 0 again */
  710. ap->ops->sff_dev_select(al->ap, 0);
  711. /* issue bus reset */
  712. rc = ep93xx_pata_bus_softreset(ap, devmask, deadline);
  713. /* if link is ocuppied, -ENODEV too is an error */
  714. if (rc && (rc != -ENODEV || sata_scr_valid(al))) {
  715. ata_link_err(al, "SRST failed (errno=%d)\n", rc);
  716. return rc;
  717. }
  718. /* determine by signature whether we have ATA or ATAPI devices */
  719. classes[0] = ata_sff_dev_classify(&al->device[0], devmask & (1 << 0),
  720. &err);
  721. if (slave_possible && err != 0x81)
  722. classes[1] = ata_sff_dev_classify(&al->device[1],
  723. devmask & (1 << 1), &err);
  724. return 0;
  725. }
  726. /* Note: original code is ata_sff_drain_fifo */
  727. static void ep93xx_pata_drain_fifo(struct ata_queued_cmd *qc)
  728. {
  729. int count;
  730. struct ata_port *ap;
  731. struct ep93xx_pata_data *drv_data;
  732. /* We only need to flush incoming data when a command was running */
  733. if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
  734. return;
  735. ap = qc->ap;
  736. drv_data = ap->host->private_data;
  737. /* Drain up to 64K of data before we give up this recovery method */
  738. for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
  739. && count < 65536; count += 2)
  740. ep93xx_pata_read_reg(drv_data, IDECTRL_ADDR_DATA);
  741. if (count)
  742. ata_port_dbg(ap, "drained %d bytes to clear DRQ.\n", count);
  743. }
  744. static int ep93xx_pata_port_start(struct ata_port *ap)
  745. {
  746. struct ep93xx_pata_data *drv_data = ap->host->private_data;
  747. /*
  748. * Set timings to safe values at startup (= number of ns from ATA
  749. * specification), we'll switch to properly calculated values later.
  750. */
  751. drv_data->t = *ata_timing_find_mode(XFER_PIO_0);
  752. return 0;
  753. }
  754. static struct scsi_host_template ep93xx_pata_sht = {
  755. ATA_BASE_SHT(DRV_NAME),
  756. /* ep93xx dma implementation limit */
  757. .sg_tablesize = 32,
  758. /* ep93xx dma can't transfer 65536 bytes at once */
  759. .dma_boundary = 0x7fff,
  760. };
  761. static struct ata_port_operations ep93xx_pata_port_ops = {
  762. .inherits = &ata_bmdma_port_ops,
  763. .qc_prep = ata_noop_qc_prep,
  764. .softreset = ep93xx_pata_softreset,
  765. .hardreset = ATA_OP_NULL,
  766. .sff_dev_select = ep93xx_pata_dev_select,
  767. .sff_set_devctl = ep93xx_pata_set_devctl,
  768. .sff_check_status = ep93xx_pata_check_status,
  769. .sff_check_altstatus = ep93xx_pata_check_altstatus,
  770. .sff_tf_load = ep93xx_pata_tf_load,
  771. .sff_tf_read = ep93xx_pata_tf_read,
  772. .sff_exec_command = ep93xx_pata_exec_command,
  773. .sff_data_xfer = ep93xx_pata_data_xfer,
  774. .sff_drain_fifo = ep93xx_pata_drain_fifo,
  775. .sff_irq_clear = ATA_OP_NULL,
  776. .set_piomode = ep93xx_pata_set_piomode,
  777. .bmdma_setup = ep93xx_pata_dma_setup,
  778. .bmdma_start = ep93xx_pata_dma_start,
  779. .bmdma_stop = ep93xx_pata_dma_stop,
  780. .bmdma_status = ep93xx_pata_dma_status,
  781. .cable_detect = ata_cable_unknown,
  782. .port_start = ep93xx_pata_port_start,
  783. };
  784. static int ep93xx_pata_probe(struct platform_device *pdev)
  785. {
  786. struct ep93xx_pata_data *drv_data;
  787. struct ata_host *host;
  788. struct ata_port *ap;
  789. int irq;
  790. struct resource *mem_res;
  791. void __iomem *ide_base;
  792. int err;
  793. err = ep93xx_ide_acquire_gpio(pdev);
  794. if (err)
  795. return err;
  796. /* INT[3] (IRQ_EP93XX_EXT3) line connected as pull down */
  797. irq = platform_get_irq(pdev, 0);
  798. if (irq < 0) {
  799. err = irq;
  800. goto err_rel_gpio;
  801. }
  802. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  803. ide_base = devm_ioremap_resource(&pdev->dev, mem_res);
  804. if (IS_ERR(ide_base)) {
  805. err = PTR_ERR(ide_base);
  806. goto err_rel_gpio;
  807. }
  808. drv_data = devm_kzalloc(&pdev->dev, sizeof(*drv_data), GFP_KERNEL);
  809. if (!drv_data) {
  810. err = -ENXIO;
  811. goto err_rel_gpio;
  812. }
  813. drv_data->pdev = pdev;
  814. drv_data->ide_base = ide_base;
  815. drv_data->udma_in_phys = mem_res->start + IDEUDMADATAIN;
  816. drv_data->udma_out_phys = mem_res->start + IDEUDMADATAOUT;
  817. ep93xx_pata_dma_init(drv_data);
  818. /* allocate host */
  819. host = ata_host_alloc(&pdev->dev, 1);
  820. if (!host) {
  821. err = -ENXIO;
  822. goto err_rel_dma;
  823. }
  824. ep93xx_pata_clear_regs(ide_base);
  825. host->private_data = drv_data;
  826. ap = host->ports[0];
  827. ap->dev = &pdev->dev;
  828. ap->ops = &ep93xx_pata_port_ops;
  829. ap->flags |= ATA_FLAG_SLAVE_POSS;
  830. ap->pio_mask = ATA_PIO4;
  831. /*
  832. * Maximum UDMA modes:
  833. * EP931x rev.E0 - UDMA2
  834. * EP931x rev.E1 - UDMA3
  835. * EP931x rev.E2 - UDMA4
  836. *
  837. * MWDMA support was removed from EP931x rev.E2,
  838. * so this driver supports only UDMA modes.
  839. */
  840. if (drv_data->dma_rx_channel && drv_data->dma_tx_channel) {
  841. int chip_rev = ep93xx_chip_revision();
  842. if (chip_rev == EP93XX_CHIP_REV_E1)
  843. ap->udma_mask = ATA_UDMA3;
  844. else if (chip_rev == EP93XX_CHIP_REV_E2)
  845. ap->udma_mask = ATA_UDMA4;
  846. else
  847. ap->udma_mask = ATA_UDMA2;
  848. }
  849. /* defaults, pio 0 */
  850. ep93xx_pata_enable_pio(ide_base, 0);
  851. dev_info(&pdev->dev, "version " DRV_VERSION "\n");
  852. /* activate host */
  853. err = ata_host_activate(host, irq, ata_bmdma_interrupt, 0,
  854. &ep93xx_pata_sht);
  855. if (err == 0)
  856. return 0;
  857. err_rel_dma:
  858. ep93xx_pata_release_dma(drv_data);
  859. err_rel_gpio:
  860. ep93xx_ide_release_gpio(pdev);
  861. return err;
  862. }
  863. static int ep93xx_pata_remove(struct platform_device *pdev)
  864. {
  865. struct ata_host *host = platform_get_drvdata(pdev);
  866. struct ep93xx_pata_data *drv_data = host->private_data;
  867. ata_host_detach(host);
  868. ep93xx_pata_release_dma(drv_data);
  869. ep93xx_pata_clear_regs(drv_data->ide_base);
  870. ep93xx_ide_release_gpio(pdev);
  871. return 0;
  872. }
  873. static struct platform_driver ep93xx_pata_platform_driver = {
  874. .driver = {
  875. .name = DRV_NAME,
  876. },
  877. .probe = ep93xx_pata_probe,
  878. .remove = ep93xx_pata_remove,
  879. };
  880. module_platform_driver(ep93xx_pata_platform_driver);
  881. MODULE_AUTHOR("Alessandro Zummo, Lennert Buytenhek, Joao Ramos, "
  882. "Bartlomiej Zolnierkiewicz, Rafal Prylowski");
  883. MODULE_DESCRIPTION("low-level driver for cirrus ep93xx IDE controller");
  884. MODULE_LICENSE("GPL");
  885. MODULE_VERSION(DRV_VERSION);
  886. MODULE_ALIAS("platform:pata_ep93xx");