pdc_adma.c 18 KB

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