pata_octeon_cf.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. /*
  2. * Driver for the Octeon bootbus compact flash.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2005 - 2012 Cavium Inc.
  9. * Copyright (C) 2008 Wind River Systems
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/libata.h>
  14. #include <linux/hrtimer.h>
  15. #include <linux/slab.h>
  16. #include <linux/irq.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/platform_device.h>
  21. #include <scsi/scsi_host.h>
  22. #include <trace/events/libata.h>
  23. #include <asm/byteorder.h>
  24. #include <asm/octeon/octeon.h>
  25. /*
  26. * The Octeon bootbus compact flash interface is connected in at least
  27. * 3 different configurations on various evaluation boards:
  28. *
  29. * -- 8 bits no irq, no DMA
  30. * -- 16 bits no irq, no DMA
  31. * -- 16 bits True IDE mode with DMA, but no irq.
  32. *
  33. * In the last case the DMA engine can generate an interrupt when the
  34. * transfer is complete. For the first two cases only PIO is supported.
  35. *
  36. */
  37. #define DRV_NAME "pata_octeon_cf"
  38. #define DRV_VERSION "2.2"
  39. /* Poll interval in nS. */
  40. #define OCTEON_CF_BUSY_POLL_INTERVAL 500000
  41. #define DMA_CFG 0
  42. #define DMA_TIM 0x20
  43. #define DMA_INT 0x38
  44. #define DMA_INT_EN 0x50
  45. struct octeon_cf_port {
  46. struct hrtimer delayed_finish;
  47. struct ata_port *ap;
  48. int dma_finished;
  49. void *c0;
  50. unsigned int cs0;
  51. unsigned int cs1;
  52. bool is_true_ide;
  53. u64 dma_base;
  54. };
  55. static const struct scsi_host_template octeon_cf_sht = {
  56. ATA_PIO_SHT(DRV_NAME),
  57. };
  58. static int enable_dma;
  59. module_param(enable_dma, int, 0444);
  60. MODULE_PARM_DESC(enable_dma,
  61. "Enable use of DMA on interfaces that support it (0=no dma [default], 1=use dma)");
  62. /*
  63. * Convert nanosecond based time to setting used in the
  64. * boot bus timing register, based on timing multiple
  65. */
  66. static unsigned int ns_to_tim_reg(unsigned int tim_mult, unsigned int nsecs)
  67. {
  68. /*
  69. * Compute # of eclock periods to get desired duration in
  70. * nanoseconds.
  71. */
  72. return DIV_ROUND_UP(nsecs * (octeon_get_io_clock_rate() / 1000000),
  73. 1000 * tim_mult);
  74. }
  75. static void octeon_cf_set_boot_reg_cfg(int cs, unsigned int multiplier)
  76. {
  77. union cvmx_mio_boot_reg_cfgx reg_cfg;
  78. unsigned int tim_mult;
  79. switch (multiplier) {
  80. case 8:
  81. tim_mult = 3;
  82. break;
  83. case 4:
  84. tim_mult = 0;
  85. break;
  86. case 2:
  87. tim_mult = 2;
  88. break;
  89. default:
  90. tim_mult = 1;
  91. break;
  92. }
  93. reg_cfg.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_CFGX(cs));
  94. reg_cfg.s.dmack = 0; /* Don't assert DMACK on access */
  95. reg_cfg.s.tim_mult = tim_mult; /* Timing mutiplier */
  96. reg_cfg.s.rd_dly = 0; /* Sample on falling edge of BOOT_OE */
  97. reg_cfg.s.sam = 0; /* Don't combine write and output enable */
  98. reg_cfg.s.we_ext = 0; /* No write enable extension */
  99. reg_cfg.s.oe_ext = 0; /* No read enable extension */
  100. reg_cfg.s.en = 1; /* Enable this region */
  101. reg_cfg.s.orbit = 0; /* Don't combine with previous region */
  102. reg_cfg.s.ale = 0; /* Don't do address multiplexing */
  103. cvmx_write_csr(CVMX_MIO_BOOT_REG_CFGX(cs), reg_cfg.u64);
  104. }
  105. /*
  106. * Called after libata determines the needed PIO mode. This
  107. * function programs the Octeon bootbus regions to support the
  108. * timing requirements of the PIO mode.
  109. *
  110. * @ap: ATA port information
  111. * @dev: ATA device
  112. */
  113. static void octeon_cf_set_piomode(struct ata_port *ap, struct ata_device *dev)
  114. {
  115. struct octeon_cf_port *cf_port = ap->private_data;
  116. union cvmx_mio_boot_reg_timx reg_tim;
  117. int T;
  118. struct ata_timing timing;
  119. unsigned int div;
  120. int use_iordy;
  121. int trh;
  122. int pause;
  123. /* These names are timing parameters from the ATA spec */
  124. int t2;
  125. /*
  126. * A divisor value of four will overflow the timing fields at
  127. * clock rates greater than 800MHz
  128. */
  129. if (octeon_get_io_clock_rate() <= 800000000)
  130. div = 4;
  131. else
  132. div = 8;
  133. T = (int)((1000000000000LL * div) / octeon_get_io_clock_rate());
  134. BUG_ON(ata_timing_compute(dev, dev->pio_mode, &timing, T, T));
  135. t2 = timing.active;
  136. if (t2)
  137. t2--;
  138. trh = ns_to_tim_reg(div, 20);
  139. if (trh)
  140. trh--;
  141. pause = (int)timing.cycle - (int)timing.active -
  142. (int)timing.setup - trh;
  143. if (pause < 0)
  144. pause = 0;
  145. if (pause)
  146. pause--;
  147. octeon_cf_set_boot_reg_cfg(cf_port->cs0, div);
  148. if (cf_port->is_true_ide)
  149. /* True IDE mode, program both chip selects. */
  150. octeon_cf_set_boot_reg_cfg(cf_port->cs1, div);
  151. use_iordy = ata_pio_need_iordy(dev);
  152. reg_tim.u64 = cvmx_read_csr(CVMX_MIO_BOOT_REG_TIMX(cf_port->cs0));
  153. /* Disable page mode */
  154. reg_tim.s.pagem = 0;
  155. /* Enable dynamic timing */
  156. reg_tim.s.waitm = use_iordy;
  157. /* Pages are disabled */
  158. reg_tim.s.pages = 0;
  159. /* We don't use multiplexed address mode */
  160. reg_tim.s.ale = 0;
  161. /* Not used */
  162. reg_tim.s.page = 0;
  163. /* Time after IORDY to coninue to assert the data */
  164. reg_tim.s.wait = 0;
  165. /* Time to wait to complete the cycle. */
  166. reg_tim.s.pause = pause;
  167. /* How long to hold after a write to de-assert CE. */
  168. reg_tim.s.wr_hld = trh;
  169. /* How long to wait after a read to de-assert CE. */
  170. reg_tim.s.rd_hld = trh;
  171. /* How long write enable is asserted */
  172. reg_tim.s.we = t2;
  173. /* How long read enable is asserted */
  174. reg_tim.s.oe = t2;
  175. /* Time after CE that read/write starts */
  176. reg_tim.s.ce = ns_to_tim_reg(div, 5);
  177. /* Time before CE that address is valid */
  178. reg_tim.s.adr = 0;
  179. /* Program the bootbus region timing for the data port chip select. */
  180. cvmx_write_csr(CVMX_MIO_BOOT_REG_TIMX(cf_port->cs0), reg_tim.u64);
  181. if (cf_port->is_true_ide)
  182. /* True IDE mode, program both chip selects. */
  183. cvmx_write_csr(CVMX_MIO_BOOT_REG_TIMX(cf_port->cs1),
  184. reg_tim.u64);
  185. }
  186. static void octeon_cf_set_dmamode(struct ata_port *ap, struct ata_device *dev)
  187. {
  188. struct octeon_cf_port *cf_port = ap->private_data;
  189. union cvmx_mio_boot_pin_defs pin_defs;
  190. union cvmx_mio_boot_dma_timx dma_tim;
  191. unsigned int oe_a;
  192. unsigned int oe_n;
  193. unsigned int dma_ackh;
  194. unsigned int dma_arq;
  195. unsigned int pause;
  196. unsigned int T0, Tkr, Td;
  197. unsigned int tim_mult;
  198. int c;
  199. const struct ata_timing *timing;
  200. timing = ata_timing_find_mode(dev->dma_mode);
  201. T0 = timing->cycle;
  202. Td = timing->active;
  203. Tkr = timing->recover;
  204. dma_ackh = timing->dmack_hold;
  205. dma_tim.u64 = 0;
  206. /* dma_tim.s.tim_mult = 0 --> 4x */
  207. tim_mult = 4;
  208. /* not spec'ed, value in eclocks, not affected by tim_mult */
  209. dma_arq = 8;
  210. pause = 25 - dma_arq * 1000 /
  211. (octeon_get_io_clock_rate() / 1000000); /* Tz */
  212. oe_a = Td;
  213. /* Tkr from cf spec, lengthened to meet T0 */
  214. oe_n = max(T0 - oe_a, Tkr);
  215. pin_defs.u64 = cvmx_read_csr(CVMX_MIO_BOOT_PIN_DEFS);
  216. /* DMA channel number. */
  217. c = (cf_port->dma_base & 8) >> 3;
  218. /* Invert the polarity if the default is 0*/
  219. dma_tim.s.dmack_pi = (pin_defs.u64 & (1ull << (11 + c))) ? 0 : 1;
  220. dma_tim.s.oe_n = ns_to_tim_reg(tim_mult, oe_n);
  221. dma_tim.s.oe_a = ns_to_tim_reg(tim_mult, oe_a);
  222. /*
  223. * This is tI, C.F. spec. says 0, but Sony CF card requires
  224. * more, we use 20 nS.
  225. */
  226. dma_tim.s.dmack_s = ns_to_tim_reg(tim_mult, 20);
  227. dma_tim.s.dmack_h = ns_to_tim_reg(tim_mult, dma_ackh);
  228. dma_tim.s.dmarq = dma_arq;
  229. dma_tim.s.pause = ns_to_tim_reg(tim_mult, pause);
  230. dma_tim.s.rd_dly = 0; /* Sample right on edge */
  231. /* writes only */
  232. dma_tim.s.we_n = ns_to_tim_reg(tim_mult, oe_n);
  233. dma_tim.s.we_a = ns_to_tim_reg(tim_mult, oe_a);
  234. ata_dev_dbg(dev, "ns to ticks (mult %d) of %d is: %d\n", tim_mult, 60,
  235. ns_to_tim_reg(tim_mult, 60));
  236. ata_dev_dbg(dev, "oe_n: %d, oe_a: %d, dmack_s: %d, dmack_h: %d, dmarq: %d, pause: %d\n",
  237. dma_tim.s.oe_n, dma_tim.s.oe_a, dma_tim.s.dmack_s,
  238. dma_tim.s.dmack_h, dma_tim.s.dmarq, dma_tim.s.pause);
  239. cvmx_write_csr(cf_port->dma_base + DMA_TIM, dma_tim.u64);
  240. }
  241. /*
  242. * Handle an 8 bit I/O request.
  243. *
  244. * @qc: Queued command
  245. * @buffer: Data buffer
  246. * @buflen: Length of the buffer.
  247. * @rw: True to write.
  248. */
  249. static unsigned int octeon_cf_data_xfer8(struct ata_queued_cmd *qc,
  250. unsigned char *buffer,
  251. unsigned int buflen,
  252. int rw)
  253. {
  254. struct ata_port *ap = qc->dev->link->ap;
  255. void __iomem *data_addr = ap->ioaddr.data_addr;
  256. unsigned long words;
  257. int count;
  258. words = buflen;
  259. if (rw) {
  260. count = 16;
  261. while (words--) {
  262. iowrite8(*buffer, data_addr);
  263. buffer++;
  264. /*
  265. * Every 16 writes do a read so the bootbus
  266. * FIFO doesn't fill up.
  267. */
  268. if (--count == 0) {
  269. ioread8(ap->ioaddr.altstatus_addr);
  270. count = 16;
  271. }
  272. }
  273. } else {
  274. ioread8_rep(data_addr, buffer, words);
  275. }
  276. return buflen;
  277. }
  278. /*
  279. * Handle a 16 bit I/O request.
  280. *
  281. * @qc: Queued command
  282. * @buffer: Data buffer
  283. * @buflen: Length of the buffer.
  284. * @rw: True to write.
  285. */
  286. static unsigned int octeon_cf_data_xfer16(struct ata_queued_cmd *qc,
  287. unsigned char *buffer,
  288. unsigned int buflen,
  289. int rw)
  290. {
  291. struct ata_port *ap = qc->dev->link->ap;
  292. void __iomem *data_addr = ap->ioaddr.data_addr;
  293. unsigned long words;
  294. int count;
  295. words = buflen / 2;
  296. if (rw) {
  297. count = 16;
  298. while (words--) {
  299. iowrite16(*(uint16_t *)buffer, data_addr);
  300. buffer += sizeof(uint16_t);
  301. /*
  302. * Every 16 writes do a read so the bootbus
  303. * FIFO doesn't fill up.
  304. */
  305. if (--count == 0) {
  306. ioread8(ap->ioaddr.altstatus_addr);
  307. count = 16;
  308. }
  309. }
  310. } else {
  311. while (words--) {
  312. *(uint16_t *)buffer = ioread16(data_addr);
  313. buffer += sizeof(uint16_t);
  314. }
  315. }
  316. /* Transfer trailing 1 byte, if any. */
  317. if (unlikely(buflen & 0x01)) {
  318. __le16 align_buf[1] = { 0 };
  319. if (rw == READ) {
  320. align_buf[0] = cpu_to_le16(ioread16(data_addr));
  321. memcpy(buffer, align_buf, 1);
  322. } else {
  323. memcpy(align_buf, buffer, 1);
  324. iowrite16(le16_to_cpu(align_buf[0]), data_addr);
  325. }
  326. words++;
  327. }
  328. return buflen;
  329. }
  330. /*
  331. * Read the taskfile for 16bit non-True IDE only.
  332. */
  333. static void octeon_cf_tf_read16(struct ata_port *ap, struct ata_taskfile *tf)
  334. {
  335. u16 blob;
  336. /* The base of the registers is at ioaddr.data_addr. */
  337. void __iomem *base = ap->ioaddr.data_addr;
  338. blob = __raw_readw(base + 0xc);
  339. tf->error = blob >> 8;
  340. blob = __raw_readw(base + 2);
  341. tf->nsect = blob & 0xff;
  342. tf->lbal = blob >> 8;
  343. blob = __raw_readw(base + 4);
  344. tf->lbam = blob & 0xff;
  345. tf->lbah = blob >> 8;
  346. blob = __raw_readw(base + 6);
  347. tf->device = blob & 0xff;
  348. tf->status = blob >> 8;
  349. if (tf->flags & ATA_TFLAG_LBA48) {
  350. if (likely(ap->ioaddr.ctl_addr)) {
  351. iowrite8(tf->ctl | ATA_HOB, ap->ioaddr.ctl_addr);
  352. blob = __raw_readw(base + 0xc);
  353. tf->hob_feature = blob >> 8;
  354. blob = __raw_readw(base + 2);
  355. tf->hob_nsect = blob & 0xff;
  356. tf->hob_lbal = blob >> 8;
  357. blob = __raw_readw(base + 4);
  358. tf->hob_lbam = blob & 0xff;
  359. tf->hob_lbah = blob >> 8;
  360. iowrite8(tf->ctl, ap->ioaddr.ctl_addr);
  361. ap->last_ctl = tf->ctl;
  362. } else {
  363. WARN_ON(1);
  364. }
  365. }
  366. }
  367. static u8 octeon_cf_check_status16(struct ata_port *ap)
  368. {
  369. u16 blob;
  370. void __iomem *base = ap->ioaddr.data_addr;
  371. blob = __raw_readw(base + 6);
  372. return blob >> 8;
  373. }
  374. static int octeon_cf_softreset16(struct ata_link *link, unsigned int *classes,
  375. unsigned long deadline)
  376. {
  377. struct ata_port *ap = link->ap;
  378. void __iomem *base = ap->ioaddr.data_addr;
  379. int rc;
  380. u8 err;
  381. __raw_writew(ap->ctl, base + 0xe);
  382. udelay(20);
  383. __raw_writew(ap->ctl | ATA_SRST, base + 0xe);
  384. udelay(20);
  385. __raw_writew(ap->ctl, base + 0xe);
  386. rc = ata_sff_wait_after_reset(link, 1, deadline);
  387. if (rc) {
  388. ata_link_err(link, "SRST failed (errno=%d)\n", rc);
  389. return rc;
  390. }
  391. /* determine by signature whether we have ATA or ATAPI devices */
  392. classes[0] = ata_sff_dev_classify(&link->device[0], 1, &err);
  393. return 0;
  394. }
  395. /*
  396. * Load the taskfile for 16bit non-True IDE only. The device_addr is
  397. * not loaded, we do this as part of octeon_cf_exec_command16.
  398. */
  399. static void octeon_cf_tf_load16(struct ata_port *ap,
  400. const struct ata_taskfile *tf)
  401. {
  402. unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
  403. /* The base of the registers is at ioaddr.data_addr. */
  404. void __iomem *base = ap->ioaddr.data_addr;
  405. if (tf->ctl != ap->last_ctl) {
  406. iowrite8(tf->ctl, ap->ioaddr.ctl_addr);
  407. ap->last_ctl = tf->ctl;
  408. ata_wait_idle(ap);
  409. }
  410. if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
  411. __raw_writew(tf->hob_feature << 8, base + 0xc);
  412. __raw_writew(tf->hob_nsect | tf->hob_lbal << 8, base + 2);
  413. __raw_writew(tf->hob_lbam | tf->hob_lbah << 8, base + 4);
  414. }
  415. if (is_addr) {
  416. __raw_writew(tf->feature << 8, base + 0xc);
  417. __raw_writew(tf->nsect | tf->lbal << 8, base + 2);
  418. __raw_writew(tf->lbam | tf->lbah << 8, base + 4);
  419. }
  420. ata_wait_idle(ap);
  421. }
  422. static void octeon_cf_dev_select(struct ata_port *ap, unsigned int device)
  423. {
  424. /* There is only one device, do nothing. */
  425. return;
  426. }
  427. /*
  428. * Issue ATA command to host controller. The device_addr is also sent
  429. * as it must be written in a combined write with the command.
  430. */
  431. static void octeon_cf_exec_command16(struct ata_port *ap,
  432. const struct ata_taskfile *tf)
  433. {
  434. /* The base of the registers is at ioaddr.data_addr. */
  435. void __iomem *base = ap->ioaddr.data_addr;
  436. u16 blob = 0;
  437. if (tf->flags & ATA_TFLAG_DEVICE)
  438. blob = tf->device;
  439. blob |= (tf->command << 8);
  440. __raw_writew(blob, base + 6);
  441. ata_wait_idle(ap);
  442. }
  443. static void octeon_cf_ata_port_noaction(struct ata_port *ap)
  444. {
  445. }
  446. static void octeon_cf_dma_setup(struct ata_queued_cmd *qc)
  447. {
  448. struct ata_port *ap = qc->ap;
  449. struct octeon_cf_port *cf_port;
  450. cf_port = ap->private_data;
  451. /* issue r/w command */
  452. qc->cursg = qc->sg;
  453. cf_port->dma_finished = 0;
  454. ap->ops->sff_exec_command(ap, &qc->tf);
  455. }
  456. /*
  457. * Start a DMA transfer that was already setup
  458. *
  459. * @qc: Information about the DMA
  460. */
  461. static void octeon_cf_dma_start(struct ata_queued_cmd *qc)
  462. {
  463. struct octeon_cf_port *cf_port = qc->ap->private_data;
  464. union cvmx_mio_boot_dma_cfgx mio_boot_dma_cfg;
  465. union cvmx_mio_boot_dma_intx mio_boot_dma_int;
  466. struct scatterlist *sg;
  467. /* Get the scatter list entry we need to DMA into */
  468. sg = qc->cursg;
  469. BUG_ON(!sg);
  470. /*
  471. * Clear the DMA complete status.
  472. */
  473. mio_boot_dma_int.u64 = 0;
  474. mio_boot_dma_int.s.done = 1;
  475. cvmx_write_csr(cf_port->dma_base + DMA_INT, mio_boot_dma_int.u64);
  476. /* Enable the interrupt. */
  477. cvmx_write_csr(cf_port->dma_base + DMA_INT_EN, mio_boot_dma_int.u64);
  478. /* Set the direction of the DMA */
  479. mio_boot_dma_cfg.u64 = 0;
  480. #ifdef __LITTLE_ENDIAN
  481. mio_boot_dma_cfg.s.endian = 1;
  482. #endif
  483. mio_boot_dma_cfg.s.en = 1;
  484. mio_boot_dma_cfg.s.rw = ((qc->tf.flags & ATA_TFLAG_WRITE) != 0);
  485. /*
  486. * Don't stop the DMA if the device deasserts DMARQ. Many
  487. * compact flashes deassert DMARQ for a short time between
  488. * sectors. Instead of stopping and restarting the DMA, we'll
  489. * let the hardware do it. If the DMA is really stopped early
  490. * due to an error condition, a later timeout will force us to
  491. * stop.
  492. */
  493. mio_boot_dma_cfg.s.clr = 0;
  494. /* Size is specified in 16bit words and minus one notation */
  495. mio_boot_dma_cfg.s.size = sg_dma_len(sg) / 2 - 1;
  496. /* We need to swap the high and low bytes of every 16 bits */
  497. mio_boot_dma_cfg.s.swap8 = 1;
  498. mio_boot_dma_cfg.s.adr = sg_dma_address(sg);
  499. cvmx_write_csr(cf_port->dma_base + DMA_CFG, mio_boot_dma_cfg.u64);
  500. }
  501. /*
  502. *
  503. * LOCKING:
  504. * spin_lock_irqsave(host lock)
  505. *
  506. */
  507. static unsigned int octeon_cf_dma_finished(struct ata_port *ap,
  508. struct ata_queued_cmd *qc)
  509. {
  510. struct ata_eh_info *ehi = &ap->link.eh_info;
  511. struct octeon_cf_port *cf_port = ap->private_data;
  512. union cvmx_mio_boot_dma_cfgx dma_cfg;
  513. union cvmx_mio_boot_dma_intx dma_int;
  514. u8 status;
  515. trace_ata_bmdma_stop(ap, &qc->tf, qc->tag);
  516. if (ap->hsm_task_state != HSM_ST_LAST)
  517. return 0;
  518. dma_cfg.u64 = cvmx_read_csr(cf_port->dma_base + DMA_CFG);
  519. if (dma_cfg.s.size != 0xfffff) {
  520. /* Error, the transfer was not complete. */
  521. qc->err_mask |= AC_ERR_HOST_BUS;
  522. ap->hsm_task_state = HSM_ST_ERR;
  523. }
  524. /* Stop and clear the dma engine. */
  525. dma_cfg.u64 = 0;
  526. dma_cfg.s.size = -1;
  527. cvmx_write_csr(cf_port->dma_base + DMA_CFG, dma_cfg.u64);
  528. /* Disable the interrupt. */
  529. dma_int.u64 = 0;
  530. cvmx_write_csr(cf_port->dma_base + DMA_INT_EN, dma_int.u64);
  531. /* Clear the DMA complete status */
  532. dma_int.s.done = 1;
  533. cvmx_write_csr(cf_port->dma_base + DMA_INT, dma_int.u64);
  534. status = ap->ops->sff_check_status(ap);
  535. ata_sff_hsm_move(ap, qc, status, 0);
  536. if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA))
  537. ata_ehi_push_desc(ehi, "DMA stat 0x%x", status);
  538. return 1;
  539. }
  540. /*
  541. * Check if any queued commands have more DMAs, if so start the next
  542. * transfer, else do end of transfer handling.
  543. */
  544. static irqreturn_t octeon_cf_interrupt(int irq, void *dev_instance)
  545. {
  546. struct ata_host *host = dev_instance;
  547. struct octeon_cf_port *cf_port;
  548. int i;
  549. unsigned int handled = 0;
  550. unsigned long flags;
  551. spin_lock_irqsave(&host->lock, flags);
  552. for (i = 0; i < host->n_ports; i++) {
  553. u8 status;
  554. struct ata_port *ap;
  555. struct ata_queued_cmd *qc;
  556. union cvmx_mio_boot_dma_intx dma_int;
  557. union cvmx_mio_boot_dma_cfgx dma_cfg;
  558. ap = host->ports[i];
  559. cf_port = ap->private_data;
  560. dma_int.u64 = cvmx_read_csr(cf_port->dma_base + DMA_INT);
  561. dma_cfg.u64 = cvmx_read_csr(cf_port->dma_base + DMA_CFG);
  562. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  563. if (!qc || (qc->tf.flags & ATA_TFLAG_POLLING))
  564. continue;
  565. if (dma_int.s.done && !dma_cfg.s.en) {
  566. if (!sg_is_last(qc->cursg)) {
  567. qc->cursg = sg_next(qc->cursg);
  568. handled = 1;
  569. trace_ata_bmdma_start(ap, &qc->tf, qc->tag);
  570. octeon_cf_dma_start(qc);
  571. continue;
  572. } else {
  573. cf_port->dma_finished = 1;
  574. }
  575. }
  576. if (!cf_port->dma_finished)
  577. continue;
  578. status = ioread8(ap->ioaddr.altstatus_addr);
  579. if (status & (ATA_BUSY | ATA_DRQ)) {
  580. /*
  581. * We are busy, try to handle it later. This
  582. * is the DMA finished interrupt, and it could
  583. * take a little while for the card to be
  584. * ready for more commands.
  585. */
  586. /* Clear DMA irq. */
  587. dma_int.u64 = 0;
  588. dma_int.s.done = 1;
  589. cvmx_write_csr(cf_port->dma_base + DMA_INT,
  590. dma_int.u64);
  591. hrtimer_start_range_ns(&cf_port->delayed_finish,
  592. ns_to_ktime(OCTEON_CF_BUSY_POLL_INTERVAL),
  593. OCTEON_CF_BUSY_POLL_INTERVAL / 5,
  594. HRTIMER_MODE_REL);
  595. handled = 1;
  596. } else {
  597. handled |= octeon_cf_dma_finished(ap, qc);
  598. }
  599. }
  600. spin_unlock_irqrestore(&host->lock, flags);
  601. return IRQ_RETVAL(handled);
  602. }
  603. static enum hrtimer_restart octeon_cf_delayed_finish(struct hrtimer *hrt)
  604. {
  605. struct octeon_cf_port *cf_port = container_of(hrt,
  606. struct octeon_cf_port,
  607. delayed_finish);
  608. struct ata_port *ap = cf_port->ap;
  609. struct ata_host *host = ap->host;
  610. struct ata_queued_cmd *qc;
  611. unsigned long flags;
  612. u8 status;
  613. enum hrtimer_restart rv = HRTIMER_NORESTART;
  614. spin_lock_irqsave(&host->lock, flags);
  615. /*
  616. * If the port is not waiting for completion, it must have
  617. * handled it previously. The hsm_task_state is
  618. * protected by host->lock.
  619. */
  620. if (ap->hsm_task_state != HSM_ST_LAST || !cf_port->dma_finished)
  621. goto out;
  622. status = ioread8(ap->ioaddr.altstatus_addr);
  623. if (status & (ATA_BUSY | ATA_DRQ)) {
  624. /* Still busy, try again. */
  625. hrtimer_forward_now(hrt,
  626. ns_to_ktime(OCTEON_CF_BUSY_POLL_INTERVAL));
  627. rv = HRTIMER_RESTART;
  628. goto out;
  629. }
  630. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  631. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  632. octeon_cf_dma_finished(ap, qc);
  633. out:
  634. spin_unlock_irqrestore(&host->lock, flags);
  635. return rv;
  636. }
  637. static void octeon_cf_dev_config(struct ata_device *dev)
  638. {
  639. /*
  640. * A maximum of 2^20 - 1 16 bit transfers are possible with
  641. * the bootbus DMA. So we need to throttle max_sectors to
  642. * (2^12 - 1 == 4095) to assure that this can never happen.
  643. */
  644. dev->max_sectors = min(dev->max_sectors, 4095U);
  645. }
  646. /*
  647. * We don't do ATAPI DMA so return 0.
  648. */
  649. static int octeon_cf_check_atapi_dma(struct ata_queued_cmd *qc)
  650. {
  651. return 0;
  652. }
  653. static unsigned int octeon_cf_qc_issue(struct ata_queued_cmd *qc)
  654. {
  655. struct ata_port *ap = qc->ap;
  656. switch (qc->tf.protocol) {
  657. case ATA_PROT_DMA:
  658. WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
  659. trace_ata_tf_load(ap, &qc->tf);
  660. ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
  661. trace_ata_bmdma_setup(ap, &qc->tf, qc->tag);
  662. octeon_cf_dma_setup(qc); /* set up dma */
  663. trace_ata_bmdma_start(ap, &qc->tf, qc->tag);
  664. octeon_cf_dma_start(qc); /* initiate dma */
  665. ap->hsm_task_state = HSM_ST_LAST;
  666. break;
  667. case ATAPI_PROT_DMA:
  668. dev_err(ap->dev, "Error, ATAPI not supported\n");
  669. BUG();
  670. default:
  671. return ata_sff_qc_issue(qc);
  672. }
  673. return 0;
  674. }
  675. static struct ata_port_operations octeon_cf_ops = {
  676. .inherits = &ata_sff_port_ops,
  677. .check_atapi_dma = octeon_cf_check_atapi_dma,
  678. .qc_issue = octeon_cf_qc_issue,
  679. .sff_dev_select = octeon_cf_dev_select,
  680. .sff_irq_on = octeon_cf_ata_port_noaction,
  681. .sff_irq_clear = octeon_cf_ata_port_noaction,
  682. .cable_detect = ata_cable_40wire,
  683. .set_piomode = octeon_cf_set_piomode,
  684. .set_dmamode = octeon_cf_set_dmamode,
  685. .dev_config = octeon_cf_dev_config,
  686. };
  687. static int octeon_cf_probe(struct platform_device *pdev)
  688. {
  689. struct resource *res_cs0, *res_cs1;
  690. bool is_16bit;
  691. u64 reg;
  692. struct device_node *node;
  693. void __iomem *cs0;
  694. void __iomem *cs1 = NULL;
  695. struct ata_host *host;
  696. struct ata_port *ap;
  697. int irq = 0;
  698. irq_handler_t irq_handler = NULL;
  699. void __iomem *base;
  700. struct octeon_cf_port *cf_port;
  701. u32 bus_width;
  702. int rv;
  703. node = pdev->dev.of_node;
  704. if (node == NULL)
  705. return -EINVAL;
  706. cf_port = devm_kzalloc(&pdev->dev, sizeof(*cf_port), GFP_KERNEL);
  707. if (!cf_port)
  708. return -ENOMEM;
  709. cf_port->is_true_ide = of_property_read_bool(node, "cavium,true-ide");
  710. if (of_property_read_u32(node, "cavium,bus-width", &bus_width) == 0)
  711. is_16bit = (bus_width == 16);
  712. else
  713. is_16bit = false;
  714. rv = of_property_read_reg(node, 0, &reg, NULL);
  715. if (rv < 0)
  716. return rv;
  717. cf_port->cs0 = upper_32_bits(reg);
  718. if (cf_port->is_true_ide) {
  719. struct device_node *dma_node;
  720. dma_node = of_parse_phandle(node,
  721. "cavium,dma-engine-handle", 0);
  722. if (dma_node) {
  723. struct platform_device *dma_dev;
  724. dma_dev = of_find_device_by_node(dma_node);
  725. if (dma_dev) {
  726. struct resource *res_dma;
  727. int i;
  728. res_dma = platform_get_resource(dma_dev, IORESOURCE_MEM, 0);
  729. if (!res_dma) {
  730. put_device(&dma_dev->dev);
  731. of_node_put(dma_node);
  732. return -EINVAL;
  733. }
  734. cf_port->dma_base = (u64)devm_ioremap(&pdev->dev, res_dma->start,
  735. resource_size(res_dma));
  736. if (!cf_port->dma_base) {
  737. put_device(&dma_dev->dev);
  738. of_node_put(dma_node);
  739. return -EINVAL;
  740. }
  741. i = platform_get_irq(dma_dev, 0);
  742. if (i > 0) {
  743. irq = i;
  744. irq_handler = octeon_cf_interrupt;
  745. }
  746. put_device(&dma_dev->dev);
  747. }
  748. of_node_put(dma_node);
  749. }
  750. res_cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  751. if (!res_cs1)
  752. return -EINVAL;
  753. cs1 = devm_ioremap(&pdev->dev, res_cs1->start,
  754. resource_size(res_cs1));
  755. if (!cs1)
  756. return -EINVAL;
  757. rv = of_property_read_reg(node, 1, &reg, NULL);
  758. if (rv < 0)
  759. return rv;
  760. cf_port->cs1 = upper_32_bits(reg);
  761. }
  762. res_cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  763. if (!res_cs0)
  764. return -EINVAL;
  765. cs0 = devm_ioremap(&pdev->dev, res_cs0->start,
  766. resource_size(res_cs0));
  767. if (!cs0)
  768. return -ENOMEM;
  769. /* allocate host */
  770. host = ata_host_alloc(&pdev->dev, 1);
  771. if (!host)
  772. return -ENOMEM;
  773. ap = host->ports[0];
  774. ap->private_data = cf_port;
  775. pdev->dev.platform_data = cf_port;
  776. cf_port->ap = ap;
  777. ap->ops = &octeon_cf_ops;
  778. ap->pio_mask = ATA_PIO6;
  779. ap->flags |= ATA_FLAG_NO_ATAPI | ATA_FLAG_PIO_POLLING;
  780. if (!is_16bit) {
  781. base = cs0 + 0x800;
  782. ap->ioaddr.cmd_addr = base;
  783. ata_sff_std_ports(&ap->ioaddr);
  784. ap->ioaddr.altstatus_addr = base + 0xe;
  785. ap->ioaddr.ctl_addr = base + 0xe;
  786. octeon_cf_ops.sff_data_xfer = octeon_cf_data_xfer8;
  787. } else if (cf_port->is_true_ide) {
  788. base = cs0;
  789. ap->ioaddr.cmd_addr = base + (ATA_REG_CMD << 1) + 1;
  790. ap->ioaddr.data_addr = base + (ATA_REG_DATA << 1);
  791. ap->ioaddr.error_addr = base + (ATA_REG_ERR << 1) + 1;
  792. ap->ioaddr.feature_addr = base + (ATA_REG_FEATURE << 1) + 1;
  793. ap->ioaddr.nsect_addr = base + (ATA_REG_NSECT << 1) + 1;
  794. ap->ioaddr.lbal_addr = base + (ATA_REG_LBAL << 1) + 1;
  795. ap->ioaddr.lbam_addr = base + (ATA_REG_LBAM << 1) + 1;
  796. ap->ioaddr.lbah_addr = base + (ATA_REG_LBAH << 1) + 1;
  797. ap->ioaddr.device_addr = base + (ATA_REG_DEVICE << 1) + 1;
  798. ap->ioaddr.status_addr = base + (ATA_REG_STATUS << 1) + 1;
  799. ap->ioaddr.command_addr = base + (ATA_REG_CMD << 1) + 1;
  800. ap->ioaddr.altstatus_addr = cs1 + (6 << 1) + 1;
  801. ap->ioaddr.ctl_addr = cs1 + (6 << 1) + 1;
  802. octeon_cf_ops.sff_data_xfer = octeon_cf_data_xfer16;
  803. ap->mwdma_mask = enable_dma ? ATA_MWDMA4 : 0;
  804. /* True IDE mode needs a timer to poll for not-busy. */
  805. hrtimer_init(&cf_port->delayed_finish, CLOCK_MONOTONIC,
  806. HRTIMER_MODE_REL);
  807. cf_port->delayed_finish.function = octeon_cf_delayed_finish;
  808. } else {
  809. /* 16 bit but not True IDE */
  810. base = cs0 + 0x800;
  811. octeon_cf_ops.sff_data_xfer = octeon_cf_data_xfer16;
  812. octeon_cf_ops.softreset = octeon_cf_softreset16;
  813. octeon_cf_ops.sff_check_status = octeon_cf_check_status16;
  814. octeon_cf_ops.sff_tf_read = octeon_cf_tf_read16;
  815. octeon_cf_ops.sff_tf_load = octeon_cf_tf_load16;
  816. octeon_cf_ops.sff_exec_command = octeon_cf_exec_command16;
  817. ap->ioaddr.data_addr = base + ATA_REG_DATA;
  818. ap->ioaddr.nsect_addr = base + ATA_REG_NSECT;
  819. ap->ioaddr.lbal_addr = base + ATA_REG_LBAL;
  820. ap->ioaddr.ctl_addr = base + 0xe;
  821. ap->ioaddr.altstatus_addr = base + 0xe;
  822. }
  823. cf_port->c0 = ap->ioaddr.ctl_addr;
  824. rv = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  825. if (rv)
  826. return rv;
  827. ata_port_desc(ap, "cmd %p ctl %p", base, ap->ioaddr.ctl_addr);
  828. dev_info(&pdev->dev, "version " DRV_VERSION" %d bit%s.\n",
  829. is_16bit ? 16 : 8,
  830. cf_port->is_true_ide ? ", True IDE" : "");
  831. return ata_host_activate(host, irq, irq_handler,
  832. IRQF_SHARED, &octeon_cf_sht);
  833. }
  834. static void octeon_cf_shutdown(struct device *dev)
  835. {
  836. union cvmx_mio_boot_dma_cfgx dma_cfg;
  837. union cvmx_mio_boot_dma_intx dma_int;
  838. struct octeon_cf_port *cf_port = dev_get_platdata(dev);
  839. if (cf_port->dma_base) {
  840. /* Stop and clear the dma engine. */
  841. dma_cfg.u64 = 0;
  842. dma_cfg.s.size = -1;
  843. cvmx_write_csr(cf_port->dma_base + DMA_CFG, dma_cfg.u64);
  844. /* Disable the interrupt. */
  845. dma_int.u64 = 0;
  846. cvmx_write_csr(cf_port->dma_base + DMA_INT_EN, dma_int.u64);
  847. /* Clear the DMA complete status */
  848. dma_int.s.done = 1;
  849. cvmx_write_csr(cf_port->dma_base + DMA_INT, dma_int.u64);
  850. __raw_writeb(0, cf_port->c0);
  851. udelay(20);
  852. __raw_writeb(ATA_SRST, cf_port->c0);
  853. udelay(20);
  854. __raw_writeb(0, cf_port->c0);
  855. mdelay(100);
  856. }
  857. }
  858. static const struct of_device_id octeon_cf_match[] = {
  859. { .compatible = "cavium,ebt3000-compact-flash", },
  860. { /* sentinel */ }
  861. };
  862. MODULE_DEVICE_TABLE(of, octeon_cf_match);
  863. static struct platform_driver octeon_cf_driver = {
  864. .probe = octeon_cf_probe,
  865. .driver = {
  866. .name = DRV_NAME,
  867. .of_match_table = octeon_cf_match,
  868. .shutdown = octeon_cf_shutdown
  869. },
  870. };
  871. static int __init octeon_cf_init(void)
  872. {
  873. return platform_driver_register(&octeon_cf_driver);
  874. }
  875. MODULE_AUTHOR("David Daney <ddaney@caviumnetworks.com>");
  876. MODULE_DESCRIPTION("low-level driver for Cavium OCTEON Compact Flash PATA");
  877. MODULE_LICENSE("GPL");
  878. MODULE_VERSION(DRV_VERSION);
  879. MODULE_ALIAS("platform:" DRV_NAME);
  880. module_init(octeon_cf_init);