pdc_adma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. * The contents of this file are subject to the Open
  9. * Software License version 1.1 that can be found at
  10. * http://www.opensource.org/licenses/osl-1.1.txt and is included herein
  11. * by reference.
  12. *
  13. * Alternatively, the contents of this file may be used under the terms
  14. * of the GNU General Public License version 2 (the "GPL") as distributed
  15. * in the kernel source COPYING file, in which case the provisions of
  16. * the GPL are applicable instead of the above. If you wish to allow
  17. * the use of your version of this file only under the terms of the
  18. * GPL and not to allow others to use your version of this file under
  19. * the OSL, indicate your decision by deleting the provisions above and
  20. * replace them with the notice and other provisions required by the GPL.
  21. * If you do not delete the provisions above, a recipient may use your
  22. * version of this file under either the OSL or the GPL.
  23. *
  24. * Supports ATA disks in single-packet ADMA mode.
  25. * Uses PIO for everything else.
  26. *
  27. * TODO: Use ADMA transfers for ATAPI devices, when possible.
  28. * This requires careful attention to a number of quirks of the chip.
  29. *
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/pci.h>
  34. #include <linux/init.h>
  35. #include <linux/blkdev.h>
  36. #include <linux/delay.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/sched.h>
  39. #include "scsi.h"
  40. #include <scsi/scsi_host.h>
  41. #include <asm/io.h>
  42. #include <linux/libata.h>
  43. #define DRV_NAME "pdc_adma"
  44. #define DRV_VERSION "0.01"
  45. /* macro to calculate base address for ATA regs */
  46. #define ADMA_ATA_REGS(base,port_no) ((base) + ((port_no) * 0x40))
  47. /* macro to calculate base address for ADMA regs */
  48. #define ADMA_REGS(base,port_no) ((base) + 0x80 + ((port_no) * 0x20))
  49. enum {
  50. ADMA_PORTS = 2,
  51. ADMA_CPB_BYTES = 40,
  52. ADMA_PRD_BYTES = LIBATA_MAX_PRD * 16,
  53. ADMA_PKT_BYTES = ADMA_CPB_BYTES + ADMA_PRD_BYTES,
  54. ADMA_DMA_BOUNDARY = 0xffffffff,
  55. /* global register offsets */
  56. ADMA_MODE_LOCK = 0x00c7,
  57. /* per-channel register offsets */
  58. ADMA_CONTROL = 0x0000, /* ADMA control */
  59. ADMA_STATUS = 0x0002, /* ADMA status */
  60. ADMA_CPB_COUNT = 0x0004, /* CPB count */
  61. ADMA_CPB_CURRENT = 0x000c, /* current CPB address */
  62. ADMA_CPB_NEXT = 0x000c, /* next CPB address */
  63. ADMA_CPB_LOOKUP = 0x0010, /* CPB lookup table */
  64. ADMA_FIFO_IN = 0x0014, /* input FIFO threshold */
  65. ADMA_FIFO_OUT = 0x0016, /* output FIFO threshold */
  66. /* ADMA_CONTROL register bits */
  67. aNIEN = (1 << 8), /* irq mask: 1==masked */
  68. aGO = (1 << 7), /* packet trigger ("Go!") */
  69. aRSTADM = (1 << 5), /* ADMA logic reset */
  70. aAUTEN = (1 << 3), /* packet trigger ("Go!") */
  71. aRSTA = (1 << 2), /* ATA hard reset */
  72. aPIOMD4 = 0x0003, /* PIO mode 4 */
  73. /* ADMA_STATUS register bits */
  74. aPSD = (1 << 6),
  75. aUIRQ = (1 << 4),
  76. aPERR = (1 << 0),
  77. /* CPB bits */
  78. cDONE = (1 << 0),
  79. cVLD = (1 << 0),
  80. cDAT = (1 << 2),
  81. cIEN = (1 << 3),
  82. /* PRD bits */
  83. pORD = (1 << 4),
  84. pDIRO = (1 << 5),
  85. pEND = (1 << 7),
  86. /* ATA register flags */
  87. rIGN = (1 << 5),
  88. rEND = (1 << 7),
  89. /* ATA register addresses */
  90. ADMA_REGS_CONTROL = 0x0e,
  91. ADMA_REGS_SECTOR_COUNT = 0x12,
  92. ADMA_REGS_LBA_LOW = 0x13,
  93. ADMA_REGS_LBA_MID = 0x14,
  94. ADMA_REGS_LBA_HIGH = 0x15,
  95. ADMA_REGS_DEVICE = 0x16,
  96. ADMA_REGS_COMMAND = 0x17,
  97. /* PCI device IDs */
  98. board_1841_idx = 0, /* ADMA 2-port controller */
  99. };
  100. typedef enum { adma_state_idle, adma_state_pkt, adma_state_mmio } adma_state_t;
  101. struct adma_port_priv {
  102. u8 *pkt;
  103. dma_addr_t pkt_dma;
  104. adma_state_t state;
  105. };
  106. static int adma_ata_init_one (struct pci_dev *pdev,
  107. const struct pci_device_id *ent);
  108. static irqreturn_t adma_intr (int irq, void *dev_instance,
  109. struct pt_regs *regs);
  110. static int adma_port_start(struct ata_port *ap);
  111. static void adma_host_stop(struct ata_host_set *host_set);
  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 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 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. .eh_strategy_handler = ata_scsi_error,
  127. .can_queue = ATA_DEF_QUEUE,
  128. .this_id = ATA_SHT_THIS_ID,
  129. .sg_tablesize = LIBATA_MAX_PRD,
  130. .max_sectors = ATA_MAX_SECTORS,
  131. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  132. .emulated = ATA_SHT_EMULATED,
  133. .use_clustering = ENABLE_CLUSTERING,
  134. .proc_name = DRV_NAME,
  135. .dma_boundary = ADMA_DMA_BOUNDARY,
  136. .slave_configure = ata_scsi_slave_config,
  137. .bios_param = ata_std_bios_param,
  138. };
  139. static struct ata_port_operations adma_ata_ops = {
  140. .port_disable = ata_port_disable,
  141. .tf_load = ata_tf_load,
  142. .tf_read = ata_tf_read,
  143. .check_status = ata_check_status,
  144. .check_atapi_dma = adma_check_atapi_dma,
  145. .exec_command = ata_exec_command,
  146. .dev_select = ata_std_dev_select,
  147. .phy_reset = adma_phy_reset,
  148. .qc_prep = adma_qc_prep,
  149. .qc_issue = adma_qc_issue,
  150. .eng_timeout = adma_eng_timeout,
  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. .host_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST |
  164. ATA_FLAG_NO_LEGACY | ATA_FLAG_MMIO,
  165. .pio_mask = 0x10, /* pio4 */
  166. .udma_mask = 0x1f, /* udma0-4 */
  167. .port_ops = &adma_ata_ops,
  168. },
  169. };
  170. static struct pci_device_id adma_ata_pci_tbl[] = {
  171. { PCI_VENDOR_ID_PDC, 0x1841, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
  172. 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_set->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_set->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 = qc->sg;
  251. struct ata_port *ap = qc->ap;
  252. struct adma_port_priv *pp = ap->private_data;
  253. u8 *buf = pp->pkt;
  254. int nelem, i = (2 + buf[3]) * 8;
  255. u8 pFLAGS = pORD | ((qc->tf.flags & ATA_TFLAG_WRITE) ? pDIRO : 0);
  256. for (nelem = 0; nelem < qc->n_elem; nelem++,sg++) {
  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 ((nelem + 1) == qc->n_elem)
  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", nelem,
  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_set->mmio_base, ap->port_no);
  354. VPRINTK("ENTER, ap %p\n", ap);
  355. /* fire up the ADMA engine */
  356. writew(aPIOMD4 | aGO | aAUTEN, chan + ADMA_CONTROL);
  357. }
  358. static 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_set *host_set)
  376. {
  377. unsigned int handled = 0, port_no;
  378. u8 __iomem *mmio_base = host_set->mmio_base;
  379. for (port_no = 0; port_no < host_set->n_ports; ++port_no) {
  380. struct ata_port *ap = host_set->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 drv_stat, 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_PORT_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. drv_stat = 0;
  396. if ((status & (aPERR | aPSD | aUIRQ)))
  397. drv_stat = ATA_ERR;
  398. else if (pp->pkt[0] != cDONE)
  399. drv_stat = ATA_ERR;
  400. ata_qc_complete(qc, drv_stat);
  401. }
  402. return handled;
  403. }
  404. static inline unsigned int adma_intr_mmio(struct ata_host_set *host_set)
  405. {
  406. unsigned int handled = 0, port_no;
  407. for (port_no = 0; port_no < host_set->n_ports; ++port_no) {
  408. struct ata_port *ap;
  409. ap = host_set->ports[port_no];
  410. if (ap && (!(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR)))) {
  411. struct ata_queued_cmd *qc;
  412. struct adma_port_priv *pp = ap->private_data;
  413. if (!pp || pp->state != adma_state_mmio)
  414. continue;
  415. qc = ata_qc_from_tag(ap, ap->active_tag);
  416. if (qc && (!(qc->tf.ctl & ATA_NIEN))) {
  417. /* check main status, clearing INTRQ */
  418. u8 status = ata_chk_status(ap);
  419. if ((status & ATA_BUSY))
  420. continue;
  421. DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
  422. ap->id, qc->tf.protocol, status);
  423. /* complete taskfile transaction */
  424. pp->state = adma_state_idle;
  425. ata_qc_complete(qc, status);
  426. handled = 1;
  427. }
  428. }
  429. }
  430. return handled;
  431. }
  432. static irqreturn_t adma_intr(int irq, void *dev_instance, struct pt_regs *regs)
  433. {
  434. struct ata_host_set *host_set = dev_instance;
  435. unsigned int handled = 0;
  436. VPRINTK("ENTER\n");
  437. spin_lock(&host_set->lock);
  438. handled = adma_intr_pkt(host_set) | adma_intr_mmio(host_set);
  439. spin_unlock(&host_set->lock);
  440. VPRINTK("EXIT\n");
  441. return IRQ_RETVAL(handled);
  442. }
  443. static void adma_ata_setup_port(struct ata_ioports *port, unsigned long base)
  444. {
  445. port->cmd_addr =
  446. port->data_addr = base + 0x000;
  447. port->error_addr =
  448. port->feature_addr = base + 0x004;
  449. port->nsect_addr = base + 0x008;
  450. port->lbal_addr = base + 0x00c;
  451. port->lbam_addr = base + 0x010;
  452. port->lbah_addr = base + 0x014;
  453. port->device_addr = base + 0x018;
  454. port->status_addr =
  455. port->command_addr = base + 0x01c;
  456. port->altstatus_addr =
  457. port->ctl_addr = base + 0x038;
  458. }
  459. static int adma_port_start(struct ata_port *ap)
  460. {
  461. struct device *dev = ap->host_set->dev;
  462. struct adma_port_priv *pp;
  463. int rc;
  464. rc = ata_port_start(ap);
  465. if (rc)
  466. return rc;
  467. adma_enter_reg_mode(ap);
  468. rc = -ENOMEM;
  469. pp = kcalloc(1, sizeof(*pp), GFP_KERNEL);
  470. if (!pp)
  471. goto err_out;
  472. pp->pkt = dma_alloc_coherent(dev, ADMA_PKT_BYTES, &pp->pkt_dma,
  473. GFP_KERNEL);
  474. if (!pp->pkt)
  475. goto err_out_kfree;
  476. /* paranoia? */
  477. if ((pp->pkt_dma & 7) != 0) {
  478. printk("bad alignment for pp->pkt_dma: %08x\n",
  479. (u32)pp->pkt_dma);
  480. goto err_out_kfree2;
  481. }
  482. memset(pp->pkt, 0, ADMA_PKT_BYTES);
  483. ap->private_data = pp;
  484. adma_reinit_engine(ap);
  485. return 0;
  486. err_out_kfree2:
  487. kfree(pp);
  488. err_out_kfree:
  489. kfree(pp);
  490. err_out:
  491. ata_port_stop(ap);
  492. return rc;
  493. }
  494. static void adma_port_stop(struct ata_port *ap)
  495. {
  496. struct device *dev = ap->host_set->dev;
  497. struct adma_port_priv *pp = ap->private_data;
  498. adma_reset_engine(ADMA_REGS(ap->host_set->mmio_base, ap->port_no));
  499. if (pp != NULL) {
  500. ap->private_data = NULL;
  501. if (pp->pkt != NULL)
  502. dma_free_coherent(dev, ADMA_PKT_BYTES,
  503. pp->pkt, pp->pkt_dma);
  504. kfree(pp);
  505. }
  506. ata_port_stop(ap);
  507. }
  508. static void adma_host_stop(struct ata_host_set *host_set)
  509. {
  510. unsigned int port_no;
  511. for (port_no = 0; port_no < ADMA_PORTS; ++port_no)
  512. adma_reset_engine(ADMA_REGS(host_set->mmio_base, port_no));
  513. ata_pci_host_stop(host_set);
  514. }
  515. static void adma_host_init(unsigned int chip_id,
  516. struct ata_probe_ent *probe_ent)
  517. {
  518. unsigned int port_no;
  519. void __iomem *mmio_base = probe_ent->mmio_base;
  520. /* enable/lock aGO operation */
  521. writeb(7, mmio_base + ADMA_MODE_LOCK);
  522. /* reset the ADMA logic */
  523. for (port_no = 0; port_no < ADMA_PORTS; ++port_no)
  524. adma_reset_engine(ADMA_REGS(mmio_base, port_no));
  525. }
  526. static int adma_set_dma_masks(struct pci_dev *pdev, void __iomem *mmio_base)
  527. {
  528. int rc;
  529. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  530. if (rc) {
  531. printk(KERN_ERR DRV_NAME
  532. "(%s): 32-bit DMA enable failed\n",
  533. pci_name(pdev));
  534. return rc;
  535. }
  536. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  537. if (rc) {
  538. printk(KERN_ERR DRV_NAME
  539. "(%s): 32-bit consistent DMA enable failed\n",
  540. pci_name(pdev));
  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. printk(KERN_DEBUG DRV_NAME " 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->host_flags = adma_port_info[board_idx].host_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 = SA_SHIRQ;
  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_module_init(&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);