pdc_adma.c 18 KB

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