pata_octeon_cf.c 27 KB

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