pdc_adma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * pdc_adma.c - Pacific Digital Corporation ADMA
  3. *
  4. * Maintained by: Mark Lord <mlord@pobox.com>
  5. *
  6. * Copyright 2005 Mark Lord
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; see the file COPYING. If not, write to
  20. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. *
  23. * libata documentation is available via 'make {ps|pdf}docs',
  24. * as Documentation/DocBook/libata.*
  25. *
  26. *
  27. * Supports ATA disks in single-packet ADMA mode.
  28. * Uses PIO for everything else.
  29. *
  30. * TODO: Use ADMA transfers for ATAPI devices, when possible.
  31. * This requires careful attention to a number of quirks of the chip.
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/init.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/delay.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/sched.h>
  42. #include <linux/device.h>
  43. #include <scsi/scsi_host.h>
  44. #include <asm/io.h>
  45. #include <linux/libata.h>
  46. #define DRV_NAME "pdc_adma"
  47. #define DRV_VERSION "0.04"
  48. /* macro to calculate base address for ATA regs */
  49. #define ADMA_ATA_REGS(base,port_no) ((base) + ((port_no) * 0x40))
  50. /* macro to calculate base address for ADMA regs */
  51. #define ADMA_REGS(base,port_no) ((base) + 0x80 + ((port_no) * 0x20))
  52. enum {
  53. ADMA_PORTS = 2,
  54. ADMA_CPB_BYTES = 40,
  55. ADMA_PRD_BYTES = LIBATA_MAX_PRD * 16,
  56. ADMA_PKT_BYTES = ADMA_CPB_BYTES + ADMA_PRD_BYTES,
  57. ADMA_DMA_BOUNDARY = 0xffffffff,
  58. /* global register offsets */
  59. ADMA_MODE_LOCK = 0x00c7,
  60. /* per-channel register offsets */
  61. ADMA_CONTROL = 0x0000, /* ADMA control */
  62. ADMA_STATUS = 0x0002, /* ADMA status */
  63. ADMA_CPB_COUNT = 0x0004, /* CPB count */
  64. ADMA_CPB_CURRENT = 0x000c, /* current CPB address */
  65. ADMA_CPB_NEXT = 0x000c, /* next CPB address */
  66. ADMA_CPB_LOOKUP = 0x0010, /* CPB lookup table */
  67. ADMA_FIFO_IN = 0x0014, /* input FIFO threshold */
  68. ADMA_FIFO_OUT = 0x0016, /* output FIFO threshold */
  69. /* ADMA_CONTROL register bits */
  70. aNIEN = (1 << 8), /* irq mask: 1==masked */
  71. aGO = (1 << 7), /* packet trigger ("Go!") */
  72. aRSTADM = (1 << 5), /* ADMA logic reset */
  73. aPIOMD4 = 0x0003, /* PIO mode 4 */
  74. /* ADMA_STATUS register bits */
  75. aPSD = (1 << 6),
  76. aUIRQ = (1 << 4),
  77. aPERR = (1 << 0),
  78. /* CPB bits */
  79. cDONE = (1 << 0),
  80. cVLD = (1 << 0),
  81. cDAT = (1 << 2),
  82. cIEN = (1 << 3),
  83. /* PRD bits */
  84. pORD = (1 << 4),
  85. pDIRO = (1 << 5),
  86. pEND = (1 << 7),
  87. /* ATA register flags */
  88. rIGN = (1 << 5),
  89. rEND = (1 << 7),
  90. /* ATA register addresses */
  91. ADMA_REGS_CONTROL = 0x0e,
  92. ADMA_REGS_SECTOR_COUNT = 0x12,
  93. ADMA_REGS_LBA_LOW = 0x13,
  94. ADMA_REGS_LBA_MID = 0x14,
  95. ADMA_REGS_LBA_HIGH = 0x15,
  96. ADMA_REGS_DEVICE = 0x16,
  97. ADMA_REGS_COMMAND = 0x17,
  98. /* PCI device IDs */
  99. board_1841_idx = 0, /* ADMA 2-port controller */
  100. };
  101. typedef enum { adma_state_idle, adma_state_pkt, adma_state_mmio } adma_state_t;
  102. struct adma_port_priv {
  103. u8 *pkt;
  104. dma_addr_t pkt_dma;
  105. adma_state_t state;
  106. };
  107. static int adma_ata_init_one (struct pci_dev *pdev,
  108. const struct pci_device_id *ent);
  109. static irqreturn_t adma_intr (int irq, void *dev_instance);
  110. static int adma_port_start(struct ata_port *ap);
  111. static void adma_host_stop(struct ata_host *host);
  112. static void adma_port_stop(struct ata_port *ap);
  113. static void adma_phy_reset(struct ata_port *ap);
  114. static void adma_qc_prep(struct ata_queued_cmd *qc);
  115. static unsigned int adma_qc_issue(struct ata_queued_cmd *qc);
  116. static int adma_check_atapi_dma(struct ata_queued_cmd *qc);
  117. static void adma_bmdma_stop(struct ata_queued_cmd *qc);
  118. static u8 adma_bmdma_status(struct ata_port *ap);
  119. static void adma_irq_clear(struct ata_port *ap);
  120. static void adma_eng_timeout(struct ata_port *ap);
  121. static struct scsi_host_template adma_ata_sht = {
  122. .module = THIS_MODULE,
  123. .name = DRV_NAME,
  124. .ioctl = ata_scsi_ioctl,
  125. .queuecommand = ata_scsi_queuecmd,
  126. .can_queue = ATA_DEF_QUEUE,
  127. .this_id = ATA_SHT_THIS_ID,
  128. .sg_tablesize = LIBATA_MAX_PRD,
  129. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  130. .emulated = ATA_SHT_EMULATED,
  131. .use_clustering = ENABLE_CLUSTERING,
  132. .proc_name = DRV_NAME,
  133. .dma_boundary = ADMA_DMA_BOUNDARY,
  134. .slave_configure = ata_scsi_slave_config,
  135. .slave_destroy = ata_scsi_slave_destroy,
  136. .bios_param = ata_std_bios_param,
  137. };
  138. static const struct ata_port_operations adma_ata_ops = {
  139. .port_disable = ata_port_disable,
  140. .tf_load = ata_tf_load,
  141. .tf_read = ata_tf_read,
  142. .check_status = ata_check_status,
  143. .check_atapi_dma = adma_check_atapi_dma,
  144. .exec_command = ata_exec_command,
  145. .dev_select = ata_std_dev_select,
  146. .phy_reset = adma_phy_reset,
  147. .qc_prep = adma_qc_prep,
  148. .qc_issue = adma_qc_issue,
  149. .eng_timeout = adma_eng_timeout,
  150. .data_xfer = ata_mmio_data_xfer,
  151. .irq_handler = adma_intr,
  152. .irq_clear = adma_irq_clear,
  153. .port_start = adma_port_start,
  154. .port_stop = adma_port_stop,
  155. .host_stop = adma_host_stop,
  156. .bmdma_stop = adma_bmdma_stop,
  157. .bmdma_status = adma_bmdma_status,
  158. };
  159. static struct ata_port_info adma_port_info[] = {
  160. /* board_1841_idx */
  161. {
  162. .sht = &adma_ata_sht,
  163. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST |
  164. ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO |
  165. ATA_FLAG_PIO_POLLING,
  166. .pio_mask = 0x10, /* pio4 */
  167. .udma_mask = 0x1f, /* udma0-4 */
  168. .port_ops = &adma_ata_ops,
  169. },
  170. };
  171. static const struct pci_device_id adma_ata_pci_tbl[] = {
  172. { PCI_VDEVICE(PDC, 0x1841), board_1841_idx },
  173. { } /* terminate list */
  174. };
  175. static struct pci_driver adma_ata_pci_driver = {
  176. .name = DRV_NAME,
  177. .id_table = adma_ata_pci_tbl,
  178. .probe = adma_ata_init_one,
  179. .remove = ata_pci_remove_one,
  180. };
  181. static int adma_check_atapi_dma(struct ata_queued_cmd *qc)
  182. {
  183. return 1; /* ATAPI DMA not yet supported */
  184. }
  185. static void adma_bmdma_stop(struct ata_queued_cmd *qc)
  186. {
  187. /* nothing */
  188. }
  189. static u8 adma_bmdma_status(struct ata_port *ap)
  190. {
  191. return 0;
  192. }
  193. static void adma_irq_clear(struct ata_port *ap)
  194. {
  195. /* nothing */
  196. }
  197. static void adma_reset_engine(void __iomem *chan)
  198. {
  199. /* reset ADMA to idle state */
  200. writew(aPIOMD4 | aNIEN | aRSTADM, chan + ADMA_CONTROL);
  201. udelay(2);
  202. writew(aPIOMD4, chan + ADMA_CONTROL);
  203. udelay(2);
  204. }
  205. static void adma_reinit_engine(struct ata_port *ap)
  206. {
  207. struct adma_port_priv *pp = ap->private_data;
  208. void __iomem *mmio_base = ap->host->mmio_base;
  209. void __iomem *chan = ADMA_REGS(mmio_base, ap->port_no);
  210. /* mask/clear ATA interrupts */
  211. writeb(ATA_NIEN, (void __iomem *)ap->ioaddr.ctl_addr);
  212. ata_check_status(ap);
  213. /* reset the ADMA engine */
  214. adma_reset_engine(chan);
  215. /* set in-FIFO threshold to 0x100 */
  216. writew(0x100, chan + ADMA_FIFO_IN);
  217. /* set CPB pointer */
  218. writel((u32)pp->pkt_dma, chan + ADMA_CPB_NEXT);
  219. /* set out-FIFO threshold to 0x100 */
  220. writew(0x100, chan + ADMA_FIFO_OUT);
  221. /* set CPB count */
  222. writew(1, chan + ADMA_CPB_COUNT);
  223. /* read/discard ADMA status */
  224. readb(chan + ADMA_STATUS);
  225. }
  226. static inline void adma_enter_reg_mode(struct ata_port *ap)
  227. {
  228. void __iomem *chan = ADMA_REGS(ap->host->mmio_base, ap->port_no);
  229. writew(aPIOMD4, chan + ADMA_CONTROL);
  230. readb(chan + ADMA_STATUS); /* flush */
  231. }
  232. static void adma_phy_reset(struct ata_port *ap)
  233. {
  234. struct adma_port_priv *pp = ap->private_data;
  235. pp->state = adma_state_idle;
  236. adma_reinit_engine(ap);
  237. ata_port_probe(ap);
  238. ata_bus_reset(ap);
  239. }
  240. static void adma_eng_timeout(struct ata_port *ap)
  241. {
  242. struct adma_port_priv *pp = ap->private_data;
  243. if (pp->state != adma_state_idle) /* healthy paranoia */
  244. pp->state = adma_state_mmio;
  245. adma_reinit_engine(ap);
  246. ata_eng_timeout(ap);
  247. }
  248. static int adma_fill_sg(struct ata_queued_cmd *qc)
  249. {
  250. struct scatterlist *sg;
  251. struct ata_port *ap = qc->ap;
  252. struct adma_port_priv *pp = ap->private_data;
  253. u8 *buf = pp->pkt;
  254. int i = (2 + buf[3]) * 8;
  255. u8 pFLAGS = pORD | ((qc->tf.flags & ATA_TFLAG_WRITE) ? pDIRO : 0);
  256. ata_for_each_sg(sg, qc) {
  257. u32 addr;
  258. u32 len;
  259. addr = (u32)sg_dma_address(sg);
  260. *(__le32 *)(buf + i) = cpu_to_le32(addr);
  261. i += 4;
  262. len = sg_dma_len(sg) >> 3;
  263. *(__le32 *)(buf + i) = cpu_to_le32(len);
  264. i += 4;
  265. if (ata_sg_is_last(sg, qc))
  266. pFLAGS |= pEND;
  267. buf[i++] = pFLAGS;
  268. buf[i++] = qc->dev->dma_mode & 0xf;
  269. buf[i++] = 0; /* pPKLW */
  270. buf[i++] = 0; /* reserved */
  271. *(__le32 *)(buf + i)
  272. = (pFLAGS & pEND) ? 0 : cpu_to_le32(pp->pkt_dma + i + 4);
  273. i += 4;
  274. VPRINTK("PRD[%u] = (0x%lX, 0x%X)\n", i/4,
  275. (unsigned long)addr, len);
  276. }
  277. return i;
  278. }
  279. static void adma_qc_prep(struct ata_queued_cmd *qc)
  280. {
  281. struct adma_port_priv *pp = qc->ap->private_data;
  282. u8 *buf = pp->pkt;
  283. u32 pkt_dma = (u32)pp->pkt_dma;
  284. int i = 0;
  285. VPRINTK("ENTER\n");
  286. adma_enter_reg_mode(qc->ap);
  287. if (qc->tf.protocol != ATA_PROT_DMA) {
  288. ata_qc_prep(qc);
  289. return;
  290. }
  291. buf[i++] = 0; /* Response flags */
  292. buf[i++] = 0; /* reserved */
  293. buf[i++] = cVLD | cDAT | cIEN;
  294. i++; /* cLEN, gets filled in below */
  295. *(__le32 *)(buf+i) = cpu_to_le32(pkt_dma); /* cNCPB */
  296. i += 4; /* cNCPB */
  297. i += 4; /* cPRD, gets filled in below */
  298. buf[i++] = 0; /* reserved */
  299. buf[i++] = 0; /* reserved */
  300. buf[i++] = 0; /* reserved */
  301. buf[i++] = 0; /* reserved */
  302. /* ATA registers; must be a multiple of 4 */
  303. buf[i++] = qc->tf.device;
  304. buf[i++] = ADMA_REGS_DEVICE;
  305. if ((qc->tf.flags & ATA_TFLAG_LBA48)) {
  306. buf[i++] = qc->tf.hob_nsect;
  307. buf[i++] = ADMA_REGS_SECTOR_COUNT;
  308. buf[i++] = qc->tf.hob_lbal;
  309. buf[i++] = ADMA_REGS_LBA_LOW;
  310. buf[i++] = qc->tf.hob_lbam;
  311. buf[i++] = ADMA_REGS_LBA_MID;
  312. buf[i++] = qc->tf.hob_lbah;
  313. buf[i++] = ADMA_REGS_LBA_HIGH;
  314. }
  315. buf[i++] = qc->tf.nsect;
  316. buf[i++] = ADMA_REGS_SECTOR_COUNT;
  317. buf[i++] = qc->tf.lbal;
  318. buf[i++] = ADMA_REGS_LBA_LOW;
  319. buf[i++] = qc->tf.lbam;
  320. buf[i++] = ADMA_REGS_LBA_MID;
  321. buf[i++] = qc->tf.lbah;
  322. buf[i++] = ADMA_REGS_LBA_HIGH;
  323. buf[i++] = 0;
  324. buf[i++] = ADMA_REGS_CONTROL;
  325. buf[i++] = rIGN;
  326. buf[i++] = 0;
  327. buf[i++] = qc->tf.command;
  328. buf[i++] = ADMA_REGS_COMMAND | rEND;
  329. buf[3] = (i >> 3) - 2; /* cLEN */
  330. *(__le32 *)(buf+8) = cpu_to_le32(pkt_dma + i); /* cPRD */
  331. i = adma_fill_sg(qc);
  332. wmb(); /* flush PRDs and pkt to memory */
  333. #if 0
  334. /* dump out CPB + PRDs for debug */
  335. {
  336. int j, len = 0;
  337. static char obuf[2048];
  338. for (j = 0; j < i; ++j) {
  339. len += sprintf(obuf+len, "%02x ", buf[j]);
  340. if ((j & 7) == 7) {
  341. printk("%s\n", obuf);
  342. len = 0;
  343. }
  344. }
  345. if (len)
  346. printk("%s\n", obuf);
  347. }
  348. #endif
  349. }
  350. static inline void adma_packet_start(struct ata_queued_cmd *qc)
  351. {
  352. struct ata_port *ap = qc->ap;
  353. void __iomem *chan = ADMA_REGS(ap->host->mmio_base, ap->port_no);
  354. VPRINTK("ENTER, ap %p\n", ap);
  355. /* fire up the ADMA engine */
  356. writew(aPIOMD4 | aGO, chan + ADMA_CONTROL);
  357. }
  358. static unsigned int adma_qc_issue(struct ata_queued_cmd *qc)
  359. {
  360. struct adma_port_priv *pp = qc->ap->private_data;
  361. switch (qc->tf.protocol) {
  362. case ATA_PROT_DMA:
  363. pp->state = adma_state_pkt;
  364. adma_packet_start(qc);
  365. return 0;
  366. case ATA_PROT_ATAPI_DMA:
  367. BUG();
  368. break;
  369. default:
  370. break;
  371. }
  372. pp->state = adma_state_mmio;
  373. return ata_qc_issue_prot(qc);
  374. }
  375. static inline unsigned int adma_intr_pkt(struct ata_host *host)
  376. {
  377. unsigned int handled = 0, port_no;
  378. u8 __iomem *mmio_base = host->mmio_base;
  379. for (port_no = 0; port_no < host->n_ports; ++port_no) {
  380. struct ata_port *ap = host->ports[port_no];
  381. struct adma_port_priv *pp;
  382. struct ata_queued_cmd *qc;
  383. void __iomem *chan = ADMA_REGS(mmio_base, port_no);
  384. u8 status = readb(chan + ADMA_STATUS);
  385. if (status == 0)
  386. continue;
  387. handled = 1;
  388. adma_enter_reg_mode(ap);
  389. if (ap->flags & ATA_FLAG_DISABLED)
  390. continue;
  391. pp = ap->private_data;
  392. if (!pp || pp->state != adma_state_pkt)
  393. continue;
  394. qc = ata_qc_from_tag(ap, ap->active_tag);
  395. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING))) {
  396. if ((status & (aPERR | aPSD | aUIRQ)))
  397. qc->err_mask |= AC_ERR_OTHER;
  398. else if (pp->pkt[0] != cDONE)
  399. qc->err_mask |= AC_ERR_OTHER;
  400. ata_qc_complete(qc);
  401. }
  402. }
  403. return handled;
  404. }
  405. static inline unsigned int adma_intr_mmio(struct ata_host *host)
  406. {
  407. unsigned int handled = 0, port_no;
  408. for (port_no = 0; port_no < host->n_ports; ++port_no) {
  409. struct ata_port *ap;
  410. ap = host->ports[port_no];
  411. if (ap && (!(ap->flags & ATA_FLAG_DISABLED))) {
  412. struct ata_queued_cmd *qc;
  413. struct adma_port_priv *pp = ap->private_data;
  414. if (!pp || pp->state != adma_state_mmio)
  415. continue;
  416. qc = ata_qc_from_tag(ap, ap->active_tag);
  417. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING))) {
  418. /* check main status, clearing INTRQ */
  419. u8 status = ata_check_status(ap);
  420. if ((status & ATA_BUSY))
  421. continue;
  422. DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
  423. ap->id, qc->tf.protocol, status);
  424. /* complete taskfile transaction */
  425. pp->state = adma_state_idle;
  426. qc->err_mask |= ac_err_mask(status);
  427. ata_qc_complete(qc);
  428. handled = 1;
  429. }
  430. }
  431. }
  432. return handled;
  433. }
  434. static irqreturn_t adma_intr(int irq, void *dev_instance)
  435. {
  436. struct ata_host *host = dev_instance;
  437. unsigned int handled = 0;
  438. VPRINTK("ENTER\n");
  439. spin_lock(&host->lock);
  440. handled = adma_intr_pkt(host) | adma_intr_mmio(host);
  441. spin_unlock(&host->lock);
  442. VPRINTK("EXIT\n");
  443. return IRQ_RETVAL(handled);
  444. }
  445. static void adma_ata_setup_port(struct ata_ioports *port, unsigned long base)
  446. {
  447. port->cmd_addr =
  448. port->data_addr = base + 0x000;
  449. port->error_addr =
  450. port->feature_addr = base + 0x004;
  451. port->nsect_addr = base + 0x008;
  452. port->lbal_addr = base + 0x00c;
  453. port->lbam_addr = base + 0x010;
  454. port->lbah_addr = base + 0x014;
  455. port->device_addr = base + 0x018;
  456. port->status_addr =
  457. port->command_addr = base + 0x01c;
  458. port->altstatus_addr =
  459. port->ctl_addr = base + 0x038;
  460. }
  461. static int adma_port_start(struct ata_port *ap)
  462. {
  463. struct device *dev = ap->host->dev;
  464. struct adma_port_priv *pp;
  465. int rc;
  466. rc = ata_port_start(ap);
  467. if (rc)
  468. return rc;
  469. adma_enter_reg_mode(ap);
  470. rc = -ENOMEM;
  471. pp = kcalloc(1, sizeof(*pp), GFP_KERNEL);
  472. if (!pp)
  473. goto err_out;
  474. pp->pkt = dma_alloc_coherent(dev, ADMA_PKT_BYTES, &pp->pkt_dma,
  475. GFP_KERNEL);
  476. if (!pp->pkt)
  477. goto err_out_kfree;
  478. /* paranoia? */
  479. if ((pp->pkt_dma & 7) != 0) {
  480. printk("bad alignment for pp->pkt_dma: %08x\n",
  481. (u32)pp->pkt_dma);
  482. dma_free_coherent(dev, ADMA_PKT_BYTES,
  483. pp->pkt, pp->pkt_dma);
  484. goto err_out_kfree;
  485. }
  486. memset(pp->pkt, 0, ADMA_PKT_BYTES);
  487. ap->private_data = pp;
  488. adma_reinit_engine(ap);
  489. return 0;
  490. err_out_kfree:
  491. kfree(pp);
  492. err_out:
  493. ata_port_stop(ap);
  494. return rc;
  495. }
  496. static void adma_port_stop(struct ata_port *ap)
  497. {
  498. struct device *dev = ap->host->dev;
  499. struct adma_port_priv *pp = ap->private_data;
  500. adma_reset_engine(ADMA_REGS(ap->host->mmio_base, ap->port_no));
  501. if (pp != NULL) {
  502. ap->private_data = NULL;
  503. if (pp->pkt != NULL)
  504. dma_free_coherent(dev, ADMA_PKT_BYTES,
  505. pp->pkt, pp->pkt_dma);
  506. kfree(pp);
  507. }
  508. ata_port_stop(ap);
  509. }
  510. static void adma_host_stop(struct ata_host *host)
  511. {
  512. unsigned int port_no;
  513. for (port_no = 0; port_no < ADMA_PORTS; ++port_no)
  514. adma_reset_engine(ADMA_REGS(host->mmio_base, port_no));
  515. ata_pci_host_stop(host);
  516. }
  517. static void adma_host_init(unsigned int chip_id,
  518. struct ata_probe_ent *probe_ent)
  519. {
  520. unsigned int port_no;
  521. void __iomem *mmio_base = probe_ent->mmio_base;
  522. /* enable/lock aGO operation */
  523. writeb(7, mmio_base + ADMA_MODE_LOCK);
  524. /* reset the ADMA logic */
  525. for (port_no = 0; port_no < ADMA_PORTS; ++port_no)
  526. adma_reset_engine(ADMA_REGS(mmio_base, port_no));
  527. }
  528. static int adma_set_dma_masks(struct pci_dev *pdev, void __iomem *mmio_base)
  529. {
  530. int rc;
  531. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  532. if (rc) {
  533. dev_printk(KERN_ERR, &pdev->dev,
  534. "32-bit DMA enable failed\n");
  535. return rc;
  536. }
  537. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  538. if (rc) {
  539. dev_printk(KERN_ERR, &pdev->dev,
  540. "32-bit consistent DMA enable failed\n");
  541. return rc;
  542. }
  543. return 0;
  544. }
  545. static int adma_ata_init_one(struct pci_dev *pdev,
  546. const struct pci_device_id *ent)
  547. {
  548. static int printed_version;
  549. struct ata_probe_ent *probe_ent = NULL;
  550. void __iomem *mmio_base;
  551. unsigned int board_idx = (unsigned int) ent->driver_data;
  552. int rc, port_no;
  553. if (!printed_version++)
  554. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  555. rc = pci_enable_device(pdev);
  556. if (rc)
  557. return rc;
  558. rc = pci_request_regions(pdev, DRV_NAME);
  559. if (rc)
  560. goto err_out;
  561. if ((pci_resource_flags(pdev, 4) & IORESOURCE_MEM) == 0) {
  562. rc = -ENODEV;
  563. goto err_out_regions;
  564. }
  565. mmio_base = pci_iomap(pdev, 4, 0);
  566. if (mmio_base == NULL) {
  567. rc = -ENOMEM;
  568. goto err_out_regions;
  569. }
  570. rc = adma_set_dma_masks(pdev, mmio_base);
  571. if (rc)
  572. goto err_out_iounmap;
  573. probe_ent = kcalloc(1, sizeof(*probe_ent), GFP_KERNEL);
  574. if (probe_ent == NULL) {
  575. rc = -ENOMEM;
  576. goto err_out_iounmap;
  577. }
  578. probe_ent->dev = pci_dev_to_dev(pdev);
  579. INIT_LIST_HEAD(&probe_ent->node);
  580. probe_ent->sht = adma_port_info[board_idx].sht;
  581. probe_ent->port_flags = adma_port_info[board_idx].flags;
  582. probe_ent->pio_mask = adma_port_info[board_idx].pio_mask;
  583. probe_ent->mwdma_mask = adma_port_info[board_idx].mwdma_mask;
  584. probe_ent->udma_mask = adma_port_info[board_idx].udma_mask;
  585. probe_ent->port_ops = adma_port_info[board_idx].port_ops;
  586. probe_ent->irq = pdev->irq;
  587. probe_ent->irq_flags = IRQF_SHARED;
  588. probe_ent->mmio_base = mmio_base;
  589. probe_ent->n_ports = ADMA_PORTS;
  590. for (port_no = 0; port_no < probe_ent->n_ports; ++port_no) {
  591. adma_ata_setup_port(&probe_ent->port[port_no],
  592. ADMA_ATA_REGS((unsigned long)mmio_base, port_no));
  593. }
  594. pci_set_master(pdev);
  595. /* initialize adapter */
  596. adma_host_init(board_idx, probe_ent);
  597. rc = ata_device_add(probe_ent);
  598. kfree(probe_ent);
  599. if (rc != ADMA_PORTS)
  600. goto err_out_iounmap;
  601. return 0;
  602. err_out_iounmap:
  603. pci_iounmap(pdev, mmio_base);
  604. err_out_regions:
  605. pci_release_regions(pdev);
  606. err_out:
  607. pci_disable_device(pdev);
  608. return rc;
  609. }
  610. static int __init adma_ata_init(void)
  611. {
  612. return pci_register_driver(&adma_ata_pci_driver);
  613. }
  614. static void __exit adma_ata_exit(void)
  615. {
  616. pci_unregister_driver(&adma_ata_pci_driver);
  617. }
  618. MODULE_AUTHOR("Mark Lord");
  619. MODULE_DESCRIPTION("Pacific Digital Corporation ADMA low-level driver");
  620. MODULE_LICENSE("GPL");
  621. MODULE_DEVICE_TABLE(pci, adma_ata_pci_tbl);
  622. MODULE_VERSION(DRV_VERSION);
  623. module_init(adma_ata_init);
  624. module_exit(adma_ata_exit);