pata_ep93xx.c 29 KB

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