pata_octeon_cf.c 27 KB

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