sata_inic162x.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /*
  2. * sata_inic162x.c - Driver for Initio 162x SATA controllers
  3. *
  4. * Copyright 2006 SUSE Linux Products GmbH
  5. * Copyright 2006 Tejun Heo <teheo@novell.com>
  6. *
  7. * This file is released under GPL v2.
  8. *
  9. * This controller is eccentric and easily locks up if something isn't
  10. * right. Documentation is available at initio's website but it only
  11. * documents registers (not programming model).
  12. *
  13. * - ATA disks work.
  14. * - Hotplug works.
  15. * - ATAPI read works but burning doesn't. This thing is really
  16. * peculiar about ATAPI and I couldn't figure out how ATAPI PIO and
  17. * ATAPI DMA WRITE should be programmed. If you've got a clue, be
  18. * my guest.
  19. * - Both STR and STD work.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <scsi/scsi_host.h>
  25. #include <linux/libata.h>
  26. #include <linux/blkdev.h>
  27. #include <scsi/scsi_device.h>
  28. #define DRV_NAME "sata_inic162x"
  29. #define DRV_VERSION "0.3"
  30. enum {
  31. MMIO_BAR = 5,
  32. NR_PORTS = 2,
  33. HOST_CTL = 0x7c,
  34. HOST_STAT = 0x7e,
  35. HOST_IRQ_STAT = 0xbc,
  36. HOST_IRQ_MASK = 0xbe,
  37. PORT_SIZE = 0x40,
  38. /* registers for ATA TF operation */
  39. PORT_TF = 0x00,
  40. PORT_ALT_STAT = 0x08,
  41. PORT_IRQ_STAT = 0x09,
  42. PORT_IRQ_MASK = 0x0a,
  43. PORT_PRD_CTL = 0x0b,
  44. PORT_PRD_ADDR = 0x0c,
  45. PORT_PRD_XFERLEN = 0x10,
  46. /* IDMA register */
  47. PORT_IDMA_CTL = 0x14,
  48. PORT_SCR = 0x20,
  49. /* HOST_CTL bits */
  50. HCTL_IRQOFF = (1 << 8), /* global IRQ off */
  51. HCTL_PWRDWN = (1 << 13), /* power down PHYs */
  52. HCTL_SOFTRST = (1 << 13), /* global reset (no phy reset) */
  53. HCTL_RPGSEL = (1 << 15), /* register page select */
  54. HCTL_KNOWN_BITS = HCTL_IRQOFF | HCTL_PWRDWN | HCTL_SOFTRST |
  55. HCTL_RPGSEL,
  56. /* HOST_IRQ_(STAT|MASK) bits */
  57. HIRQ_PORT0 = (1 << 0),
  58. HIRQ_PORT1 = (1 << 1),
  59. HIRQ_SOFT = (1 << 14),
  60. HIRQ_GLOBAL = (1 << 15), /* STAT only */
  61. /* PORT_IRQ_(STAT|MASK) bits */
  62. PIRQ_OFFLINE = (1 << 0), /* device unplugged */
  63. PIRQ_ONLINE = (1 << 1), /* device plugged */
  64. PIRQ_COMPLETE = (1 << 2), /* completion interrupt */
  65. PIRQ_FATAL = (1 << 3), /* fatal error */
  66. PIRQ_ATA = (1 << 4), /* ATA interrupt */
  67. PIRQ_REPLY = (1 << 5), /* reply FIFO not empty */
  68. PIRQ_PENDING = (1 << 7), /* port IRQ pending (STAT only) */
  69. PIRQ_ERR = PIRQ_OFFLINE | PIRQ_ONLINE | PIRQ_FATAL,
  70. PIRQ_MASK_DMA_READ = PIRQ_REPLY | PIRQ_ATA,
  71. PIRQ_MASK_OTHER = PIRQ_REPLY | PIRQ_COMPLETE,
  72. PIRQ_MASK_FREEZE = 0xff,
  73. /* PORT_PRD_CTL bits */
  74. PRD_CTL_START = (1 << 0),
  75. PRD_CTL_WR = (1 << 3),
  76. PRD_CTL_DMAEN = (1 << 7), /* DMA enable */
  77. /* PORT_IDMA_CTL bits */
  78. IDMA_CTL_RST_ATA = (1 << 2), /* hardreset ATA bus */
  79. IDMA_CTL_RST_IDMA = (1 << 5), /* reset IDMA machinary */
  80. IDMA_CTL_GO = (1 << 7), /* IDMA mode go */
  81. IDMA_CTL_ATA_NIEN = (1 << 8), /* ATA IRQ disable */
  82. };
  83. struct inic_host_priv {
  84. u16 cached_hctl;
  85. };
  86. struct inic_port_priv {
  87. u8 dfl_prdctl;
  88. u8 cached_prdctl;
  89. u8 cached_pirq_mask;
  90. };
  91. static struct scsi_host_template inic_sht = {
  92. ATA_BMDMA_SHT(DRV_NAME),
  93. };
  94. static const int scr_map[] = {
  95. [SCR_STATUS] = 0,
  96. [SCR_ERROR] = 1,
  97. [SCR_CONTROL] = 2,
  98. };
  99. static void __iomem *inic_port_base(struct ata_port *ap)
  100. {
  101. return ap->host->iomap[MMIO_BAR] + ap->port_no * PORT_SIZE;
  102. }
  103. static void __inic_set_pirq_mask(struct ata_port *ap, u8 mask)
  104. {
  105. void __iomem *port_base = inic_port_base(ap);
  106. struct inic_port_priv *pp = ap->private_data;
  107. writeb(mask, port_base + PORT_IRQ_MASK);
  108. pp->cached_pirq_mask = mask;
  109. }
  110. static void inic_set_pirq_mask(struct ata_port *ap, u8 mask)
  111. {
  112. struct inic_port_priv *pp = ap->private_data;
  113. if (pp->cached_pirq_mask != mask)
  114. __inic_set_pirq_mask(ap, mask);
  115. }
  116. static void inic_reset_port(void __iomem *port_base)
  117. {
  118. void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
  119. u16 ctl;
  120. ctl = readw(idma_ctl);
  121. ctl &= ~(IDMA_CTL_RST_IDMA | IDMA_CTL_ATA_NIEN | IDMA_CTL_GO);
  122. /* mask IRQ and assert reset */
  123. writew(ctl | IDMA_CTL_RST_IDMA | IDMA_CTL_ATA_NIEN, idma_ctl);
  124. readw(idma_ctl); /* flush */
  125. /* give it some time */
  126. msleep(1);
  127. /* release reset */
  128. writew(ctl | IDMA_CTL_ATA_NIEN, idma_ctl);
  129. /* clear irq */
  130. writeb(0xff, port_base + PORT_IRQ_STAT);
  131. /* reenable ATA IRQ, turn off IDMA mode */
  132. writew(ctl, idma_ctl);
  133. }
  134. static int inic_scr_read(struct ata_port *ap, unsigned sc_reg, u32 *val)
  135. {
  136. void __iomem *scr_addr = ap->ioaddr.scr_addr;
  137. void __iomem *addr;
  138. if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
  139. return -EINVAL;
  140. addr = scr_addr + scr_map[sc_reg] * 4;
  141. *val = readl(scr_addr + scr_map[sc_reg] * 4);
  142. /* this controller has stuck DIAG.N, ignore it */
  143. if (sc_reg == SCR_ERROR)
  144. *val &= ~SERR_PHYRDY_CHG;
  145. return 0;
  146. }
  147. static int inic_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
  148. {
  149. void __iomem *scr_addr = ap->ioaddr.scr_addr;
  150. if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
  151. return -EINVAL;
  152. writel(val, scr_addr + scr_map[sc_reg] * 4);
  153. return 0;
  154. }
  155. /*
  156. * In TF mode, inic162x is very similar to SFF device. TF registers
  157. * function the same. DMA engine behaves similary using the same PRD
  158. * format as BMDMA but different command register, interrupt and event
  159. * notification methods are used. The following inic_bmdma_*()
  160. * functions do the impedance matching.
  161. */
  162. static void inic_bmdma_setup(struct ata_queued_cmd *qc)
  163. {
  164. struct ata_port *ap = qc->ap;
  165. struct inic_port_priv *pp = ap->private_data;
  166. void __iomem *port_base = inic_port_base(ap);
  167. int rw = qc->tf.flags & ATA_TFLAG_WRITE;
  168. /* make sure device sees PRD table writes */
  169. wmb();
  170. /* load transfer length */
  171. writel(qc->nbytes, port_base + PORT_PRD_XFERLEN);
  172. /* turn on DMA and specify data direction */
  173. pp->cached_prdctl = pp->dfl_prdctl | PRD_CTL_DMAEN;
  174. if (!rw)
  175. pp->cached_prdctl |= PRD_CTL_WR;
  176. writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
  177. /* issue r/w command */
  178. ap->ops->sff_exec_command(ap, &qc->tf);
  179. }
  180. static void inic_bmdma_start(struct ata_queued_cmd *qc)
  181. {
  182. struct ata_port *ap = qc->ap;
  183. struct inic_port_priv *pp = ap->private_data;
  184. void __iomem *port_base = inic_port_base(ap);
  185. /* start host DMA transaction */
  186. pp->cached_prdctl |= PRD_CTL_START;
  187. writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
  188. }
  189. static void inic_bmdma_stop(struct ata_queued_cmd *qc)
  190. {
  191. struct ata_port *ap = qc->ap;
  192. struct inic_port_priv *pp = ap->private_data;
  193. void __iomem *port_base = inic_port_base(ap);
  194. /* stop DMA engine */
  195. writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
  196. }
  197. static u8 inic_bmdma_status(struct ata_port *ap)
  198. {
  199. /* event is already verified by the interrupt handler */
  200. return ATA_DMA_INTR;
  201. }
  202. static void inic_host_intr(struct ata_port *ap)
  203. {
  204. void __iomem *port_base = inic_port_base(ap);
  205. struct ata_eh_info *ehi = &ap->link.eh_info;
  206. u8 irq_stat;
  207. /* fetch and clear irq */
  208. irq_stat = readb(port_base + PORT_IRQ_STAT);
  209. writeb(irq_stat, port_base + PORT_IRQ_STAT);
  210. if (likely(!(irq_stat & PIRQ_ERR))) {
  211. struct ata_queued_cmd *qc =
  212. ata_qc_from_tag(ap, ap->link.active_tag);
  213. if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
  214. ap->ops->sff_check_status(ap); /* clear ATA interrupt */
  215. return;
  216. }
  217. if (likely(ata_sff_host_intr(ap, qc)))
  218. return;
  219. ap->ops->sff_check_status(ap); /* clear ATA interrupt */
  220. ata_port_printk(ap, KERN_WARNING, "unhandled "
  221. "interrupt, irq_stat=%x\n", irq_stat);
  222. return;
  223. }
  224. /* error */
  225. ata_ehi_push_desc(ehi, "irq_stat=0x%x", irq_stat);
  226. if (irq_stat & (PIRQ_OFFLINE | PIRQ_ONLINE)) {
  227. ata_ehi_hotplugged(ehi);
  228. ata_port_freeze(ap);
  229. } else
  230. ata_port_abort(ap);
  231. }
  232. static irqreturn_t inic_interrupt(int irq, void *dev_instance)
  233. {
  234. struct ata_host *host = dev_instance;
  235. void __iomem *mmio_base = host->iomap[MMIO_BAR];
  236. u16 host_irq_stat;
  237. int i, handled = 0;;
  238. host_irq_stat = readw(mmio_base + HOST_IRQ_STAT);
  239. if (unlikely(!(host_irq_stat & HIRQ_GLOBAL)))
  240. goto out;
  241. spin_lock(&host->lock);
  242. for (i = 0; i < NR_PORTS; i++) {
  243. struct ata_port *ap = host->ports[i];
  244. if (!(host_irq_stat & (HIRQ_PORT0 << i)))
  245. continue;
  246. if (likely(ap && !(ap->flags & ATA_FLAG_DISABLED))) {
  247. inic_host_intr(ap);
  248. handled++;
  249. } else {
  250. if (ata_ratelimit())
  251. dev_printk(KERN_ERR, host->dev, "interrupt "
  252. "from disabled port %d (0x%x)\n",
  253. i, host_irq_stat);
  254. }
  255. }
  256. spin_unlock(&host->lock);
  257. out:
  258. return IRQ_RETVAL(handled);
  259. }
  260. static unsigned int inic_qc_issue(struct ata_queued_cmd *qc)
  261. {
  262. struct ata_port *ap = qc->ap;
  263. /* ATA IRQ doesn't wait for DMA transfer completion and vice
  264. * versa. Mask IRQ selectively to detect command completion.
  265. * Without it, ATA DMA read command can cause data corruption.
  266. *
  267. * Something similar might be needed for ATAPI writes. I
  268. * tried a lot of combinations but couldn't find the solution.
  269. */
  270. if (qc->tf.protocol == ATA_PROT_DMA &&
  271. !(qc->tf.flags & ATA_TFLAG_WRITE))
  272. inic_set_pirq_mask(ap, PIRQ_MASK_DMA_READ);
  273. else
  274. inic_set_pirq_mask(ap, PIRQ_MASK_OTHER);
  275. /* Issuing a command to yet uninitialized port locks up the
  276. * controller. Most of the time, this happens for the first
  277. * command after reset which are ATA and ATAPI IDENTIFYs.
  278. * Fast fail if stat is 0x7f or 0xff for those commands.
  279. */
  280. if (unlikely(qc->tf.command == ATA_CMD_ID_ATA ||
  281. qc->tf.command == ATA_CMD_ID_ATAPI)) {
  282. u8 stat = ap->ops->sff_check_status(ap);
  283. if (stat == 0x7f || stat == 0xff)
  284. return AC_ERR_HSM;
  285. }
  286. return ata_sff_qc_issue(qc);
  287. }
  288. static void inic_freeze(struct ata_port *ap)
  289. {
  290. void __iomem *port_base = inic_port_base(ap);
  291. __inic_set_pirq_mask(ap, PIRQ_MASK_FREEZE);
  292. ap->ops->sff_check_status(ap);
  293. writeb(0xff, port_base + PORT_IRQ_STAT);
  294. }
  295. static void inic_thaw(struct ata_port *ap)
  296. {
  297. void __iomem *port_base = inic_port_base(ap);
  298. ap->ops->sff_check_status(ap);
  299. writeb(0xff, port_base + PORT_IRQ_STAT);
  300. __inic_set_pirq_mask(ap, PIRQ_MASK_OTHER);
  301. }
  302. /*
  303. * SRST and SControl hardreset don't give valid signature on this
  304. * controller. Only controller specific hardreset mechanism works.
  305. */
  306. static int inic_hardreset(struct ata_link *link, unsigned int *class,
  307. unsigned long deadline)
  308. {
  309. struct ata_port *ap = link->ap;
  310. void __iomem *port_base = inic_port_base(ap);
  311. void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
  312. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  313. u16 val;
  314. int rc;
  315. /* hammer it into sane state */
  316. inic_reset_port(port_base);
  317. val = readw(idma_ctl);
  318. writew(val | IDMA_CTL_RST_ATA, idma_ctl);
  319. readw(idma_ctl); /* flush */
  320. msleep(1);
  321. writew(val & ~IDMA_CTL_RST_ATA, idma_ctl);
  322. rc = sata_link_resume(link, timing, deadline);
  323. if (rc) {
  324. ata_link_printk(link, KERN_WARNING, "failed to resume "
  325. "link after reset (errno=%d)\n", rc);
  326. return rc;
  327. }
  328. *class = ATA_DEV_NONE;
  329. if (ata_link_online(link)) {
  330. struct ata_taskfile tf;
  331. /* wait for link to become ready */
  332. rc = ata_sff_wait_after_reset(link, 1, deadline);
  333. /* link occupied, -ENODEV too is an error */
  334. if (rc) {
  335. ata_link_printk(link, KERN_WARNING, "device not ready "
  336. "after hardreset (errno=%d)\n", rc);
  337. return rc;
  338. }
  339. ata_sff_tf_read(ap, &tf);
  340. *class = ata_dev_classify(&tf);
  341. }
  342. return 0;
  343. }
  344. static void inic_error_handler(struct ata_port *ap)
  345. {
  346. void __iomem *port_base = inic_port_base(ap);
  347. struct inic_port_priv *pp = ap->private_data;
  348. unsigned long flags;
  349. /* reset PIO HSM and stop DMA engine */
  350. inic_reset_port(port_base);
  351. spin_lock_irqsave(ap->lock, flags);
  352. ap->hsm_task_state = HSM_ST_IDLE;
  353. writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
  354. spin_unlock_irqrestore(ap->lock, flags);
  355. /* PIO and DMA engines have been stopped, perform recovery */
  356. ata_std_error_handler(ap);
  357. }
  358. static void inic_post_internal_cmd(struct ata_queued_cmd *qc)
  359. {
  360. /* make DMA engine forget about the failed command */
  361. if (qc->flags & ATA_QCFLAG_FAILED)
  362. inic_reset_port(inic_port_base(qc->ap));
  363. }
  364. static void inic_dev_config(struct ata_device *dev)
  365. {
  366. /* inic can only handle upto LBA28 max sectors */
  367. if (dev->max_sectors > ATA_MAX_SECTORS)
  368. dev->max_sectors = ATA_MAX_SECTORS;
  369. if (dev->n_sectors >= 1 << 28) {
  370. ata_dev_printk(dev, KERN_ERR,
  371. "ERROR: This driver doesn't support LBA48 yet and may cause\n"
  372. " data corruption on such devices. Disabling.\n");
  373. ata_dev_disable(dev);
  374. }
  375. }
  376. static void init_port(struct ata_port *ap)
  377. {
  378. void __iomem *port_base = inic_port_base(ap);
  379. /* Setup PRD address */
  380. writel(ap->prd_dma, port_base + PORT_PRD_ADDR);
  381. }
  382. static int inic_port_resume(struct ata_port *ap)
  383. {
  384. init_port(ap);
  385. return 0;
  386. }
  387. static int inic_port_start(struct ata_port *ap)
  388. {
  389. void __iomem *port_base = inic_port_base(ap);
  390. struct inic_port_priv *pp;
  391. u8 tmp;
  392. int rc;
  393. /* alloc and initialize private data */
  394. pp = devm_kzalloc(ap->host->dev, sizeof(*pp), GFP_KERNEL);
  395. if (!pp)
  396. return -ENOMEM;
  397. ap->private_data = pp;
  398. /* default PRD_CTL value, DMAEN, WR and START off */
  399. tmp = readb(port_base + PORT_PRD_CTL);
  400. tmp &= ~(PRD_CTL_DMAEN | PRD_CTL_WR | PRD_CTL_START);
  401. pp->dfl_prdctl = tmp;
  402. /* Alloc resources */
  403. rc = ata_port_start(ap);
  404. if (rc)
  405. return rc;
  406. init_port(ap);
  407. return 0;
  408. }
  409. static struct ata_port_operations inic_port_ops = {
  410. .inherits = &ata_sff_port_ops,
  411. .bmdma_setup = inic_bmdma_setup,
  412. .bmdma_start = inic_bmdma_start,
  413. .bmdma_stop = inic_bmdma_stop,
  414. .bmdma_status = inic_bmdma_status,
  415. .qc_issue = inic_qc_issue,
  416. .freeze = inic_freeze,
  417. .thaw = inic_thaw,
  418. .softreset = ATA_OP_NULL, /* softreset is broken */
  419. .hardreset = inic_hardreset,
  420. .error_handler = inic_error_handler,
  421. .post_internal_cmd = inic_post_internal_cmd,
  422. .dev_config = inic_dev_config,
  423. .scr_read = inic_scr_read,
  424. .scr_write = inic_scr_write,
  425. .port_resume = inic_port_resume,
  426. .port_start = inic_port_start,
  427. };
  428. static struct ata_port_info inic_port_info = {
  429. /* For some reason, ATAPI_PROT_PIO is broken on this
  430. * controller, and no, PIO_POLLING does't fix it. It somehow
  431. * manages to report the wrong ireason and ignoring ireason
  432. * results in machine lock up. Tell libata to always prefer
  433. * DMA.
  434. */
  435. .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
  436. .pio_mask = 0x1f, /* pio0-4 */
  437. .mwdma_mask = 0x07, /* mwdma0-2 */
  438. .udma_mask = ATA_UDMA6,
  439. .port_ops = &inic_port_ops
  440. };
  441. static int init_controller(void __iomem *mmio_base, u16 hctl)
  442. {
  443. int i;
  444. u16 val;
  445. hctl &= ~HCTL_KNOWN_BITS;
  446. /* Soft reset whole controller. Spec says reset duration is 3
  447. * PCI clocks, be generous and give it 10ms.
  448. */
  449. writew(hctl | HCTL_SOFTRST, mmio_base + HOST_CTL);
  450. readw(mmio_base + HOST_CTL); /* flush */
  451. for (i = 0; i < 10; i++) {
  452. msleep(1);
  453. val = readw(mmio_base + HOST_CTL);
  454. if (!(val & HCTL_SOFTRST))
  455. break;
  456. }
  457. if (val & HCTL_SOFTRST)
  458. return -EIO;
  459. /* mask all interrupts and reset ports */
  460. for (i = 0; i < NR_PORTS; i++) {
  461. void __iomem *port_base = mmio_base + i * PORT_SIZE;
  462. writeb(0xff, port_base + PORT_IRQ_MASK);
  463. inic_reset_port(port_base);
  464. }
  465. /* port IRQ is masked now, unmask global IRQ */
  466. writew(hctl & ~HCTL_IRQOFF, mmio_base + HOST_CTL);
  467. val = readw(mmio_base + HOST_IRQ_MASK);
  468. val &= ~(HIRQ_PORT0 | HIRQ_PORT1);
  469. writew(val, mmio_base + HOST_IRQ_MASK);
  470. return 0;
  471. }
  472. #ifdef CONFIG_PM
  473. static int inic_pci_device_resume(struct pci_dev *pdev)
  474. {
  475. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  476. struct inic_host_priv *hpriv = host->private_data;
  477. void __iomem *mmio_base = host->iomap[MMIO_BAR];
  478. int rc;
  479. rc = ata_pci_device_do_resume(pdev);
  480. if (rc)
  481. return rc;
  482. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  483. rc = init_controller(mmio_base, hpriv->cached_hctl);
  484. if (rc)
  485. return rc;
  486. }
  487. ata_host_resume(host);
  488. return 0;
  489. }
  490. #endif
  491. static int inic_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  492. {
  493. static int printed_version;
  494. const struct ata_port_info *ppi[] = { &inic_port_info, NULL };
  495. struct ata_host *host;
  496. struct inic_host_priv *hpriv;
  497. void __iomem * const *iomap;
  498. int i, rc;
  499. if (!printed_version++)
  500. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  501. /* alloc host */
  502. host = ata_host_alloc_pinfo(&pdev->dev, ppi, NR_PORTS);
  503. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  504. if (!host || !hpriv)
  505. return -ENOMEM;
  506. host->private_data = hpriv;
  507. /* acquire resources and fill host */
  508. rc = pcim_enable_device(pdev);
  509. if (rc)
  510. return rc;
  511. rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME);
  512. if (rc)
  513. return rc;
  514. host->iomap = iomap = pcim_iomap_table(pdev);
  515. for (i = 0; i < NR_PORTS; i++) {
  516. struct ata_port *ap = host->ports[i];
  517. struct ata_ioports *port = &ap->ioaddr;
  518. unsigned int offset = i * PORT_SIZE;
  519. port->cmd_addr = iomap[2 * i];
  520. port->altstatus_addr =
  521. port->ctl_addr = (void __iomem *)
  522. ((unsigned long)iomap[2 * i + 1] | ATA_PCI_CTL_OFS);
  523. port->scr_addr = iomap[MMIO_BAR] + offset + PORT_SCR;
  524. ata_sff_std_ports(port);
  525. ata_port_pbar_desc(ap, MMIO_BAR, -1, "mmio");
  526. ata_port_pbar_desc(ap, MMIO_BAR, offset, "port");
  527. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  528. (unsigned long long)pci_resource_start(pdev, 2 * i),
  529. (unsigned long long)pci_resource_start(pdev, (2 * i + 1)) |
  530. ATA_PCI_CTL_OFS);
  531. }
  532. hpriv->cached_hctl = readw(iomap[MMIO_BAR] + HOST_CTL);
  533. /* Set dma_mask. This devices doesn't support 64bit addressing. */
  534. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  535. if (rc) {
  536. dev_printk(KERN_ERR, &pdev->dev,
  537. "32-bit DMA enable failed\n");
  538. return rc;
  539. }
  540. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  541. if (rc) {
  542. dev_printk(KERN_ERR, &pdev->dev,
  543. "32-bit consistent DMA enable failed\n");
  544. return rc;
  545. }
  546. /*
  547. * This controller is braindamaged. dma_boundary is 0xffff
  548. * like others but it will lock up the whole machine HARD if
  549. * 65536 byte PRD entry is fed. Reduce maximum segment size.
  550. */
  551. rc = pci_set_dma_max_seg_size(pdev, 65536 - 512);
  552. if (rc) {
  553. dev_printk(KERN_ERR, &pdev->dev,
  554. "failed to set the maximum segment size.\n");
  555. return rc;
  556. }
  557. rc = init_controller(iomap[MMIO_BAR], hpriv->cached_hctl);
  558. if (rc) {
  559. dev_printk(KERN_ERR, &pdev->dev,
  560. "failed to initialize controller\n");
  561. return rc;
  562. }
  563. pci_set_master(pdev);
  564. return ata_host_activate(host, pdev->irq, inic_interrupt, IRQF_SHARED,
  565. &inic_sht);
  566. }
  567. static const struct pci_device_id inic_pci_tbl[] = {
  568. { PCI_VDEVICE(INIT, 0x1622), },
  569. { },
  570. };
  571. static struct pci_driver inic_pci_driver = {
  572. .name = DRV_NAME,
  573. .id_table = inic_pci_tbl,
  574. #ifdef CONFIG_PM
  575. .suspend = ata_pci_device_suspend,
  576. .resume = inic_pci_device_resume,
  577. #endif
  578. .probe = inic_init_one,
  579. .remove = ata_pci_remove_one,
  580. };
  581. static int __init inic_init(void)
  582. {
  583. return pci_register_driver(&inic_pci_driver);
  584. }
  585. static void __exit inic_exit(void)
  586. {
  587. pci_unregister_driver(&inic_pci_driver);
  588. }
  589. MODULE_AUTHOR("Tejun Heo");
  590. MODULE_DESCRIPTION("low-level driver for Initio 162x SATA");
  591. MODULE_LICENSE("GPL v2");
  592. MODULE_DEVICE_TABLE(pci, inic_pci_tbl);
  593. MODULE_VERSION(DRV_VERSION);
  594. module_init(inic_init);
  595. module_exit(inic_exit);