sata_inic162x.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. void __iomem *addr;
  151. if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
  152. return -EINVAL;
  153. addr = scr_addr + scr_map[sc_reg] * 4;
  154. writel(val, scr_addr + scr_map[sc_reg] * 4);
  155. return 0;
  156. }
  157. /*
  158. * In TF mode, inic162x is very similar to SFF device. TF registers
  159. * function the same. DMA engine behaves similary using the same PRD
  160. * format as BMDMA but different command register, interrupt and event
  161. * notification methods are used. The following inic_bmdma_*()
  162. * functions do the impedance matching.
  163. */
  164. static void inic_bmdma_setup(struct ata_queued_cmd *qc)
  165. {
  166. struct ata_port *ap = qc->ap;
  167. struct inic_port_priv *pp = ap->private_data;
  168. void __iomem *port_base = inic_port_base(ap);
  169. int rw = qc->tf.flags & ATA_TFLAG_WRITE;
  170. /* make sure device sees PRD table writes */
  171. wmb();
  172. /* load transfer length */
  173. writel(qc->nbytes, port_base + PORT_PRD_XFERLEN);
  174. /* turn on DMA and specify data direction */
  175. pp->cached_prdctl = pp->dfl_prdctl | PRD_CTL_DMAEN;
  176. if (!rw)
  177. pp->cached_prdctl |= PRD_CTL_WR;
  178. writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
  179. /* issue r/w command */
  180. ap->ops->exec_command(ap, &qc->tf);
  181. }
  182. static void inic_bmdma_start(struct ata_queued_cmd *qc)
  183. {
  184. struct ata_port *ap = qc->ap;
  185. struct inic_port_priv *pp = ap->private_data;
  186. void __iomem *port_base = inic_port_base(ap);
  187. /* start host DMA transaction */
  188. pp->cached_prdctl |= PRD_CTL_START;
  189. writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
  190. }
  191. static void inic_bmdma_stop(struct ata_queued_cmd *qc)
  192. {
  193. struct ata_port *ap = qc->ap;
  194. struct inic_port_priv *pp = ap->private_data;
  195. void __iomem *port_base = inic_port_base(ap);
  196. /* stop DMA engine */
  197. writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
  198. }
  199. static u8 inic_bmdma_status(struct ata_port *ap)
  200. {
  201. /* event is already verified by the interrupt handler */
  202. return ATA_DMA_INTR;
  203. }
  204. static void inic_host_intr(struct ata_port *ap)
  205. {
  206. void __iomem *port_base = inic_port_base(ap);
  207. struct ata_eh_info *ehi = &ap->link.eh_info;
  208. u8 irq_stat;
  209. /* fetch and clear irq */
  210. irq_stat = readb(port_base + PORT_IRQ_STAT);
  211. writeb(irq_stat, port_base + PORT_IRQ_STAT);
  212. if (likely(!(irq_stat & PIRQ_ERR))) {
  213. struct ata_queued_cmd *qc =
  214. ata_qc_from_tag(ap, ap->link.active_tag);
  215. if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
  216. ata_chk_status(ap); /* clear ATA interrupt */
  217. return;
  218. }
  219. if (likely(ata_host_intr(ap, qc)))
  220. return;
  221. ata_chk_status(ap); /* clear ATA interrupt */
  222. ata_port_printk(ap, KERN_WARNING, "unhandled "
  223. "interrupt, irq_stat=%x\n", irq_stat);
  224. return;
  225. }
  226. /* error */
  227. ata_ehi_push_desc(ehi, "irq_stat=0x%x", irq_stat);
  228. if (irq_stat & (PIRQ_OFFLINE | PIRQ_ONLINE)) {
  229. ata_ehi_hotplugged(ehi);
  230. ata_port_freeze(ap);
  231. } else
  232. ata_port_abort(ap);
  233. }
  234. static irqreturn_t inic_interrupt(int irq, void *dev_instance)
  235. {
  236. struct ata_host *host = dev_instance;
  237. void __iomem *mmio_base = host->iomap[MMIO_BAR];
  238. u16 host_irq_stat;
  239. int i, handled = 0;;
  240. host_irq_stat = readw(mmio_base + HOST_IRQ_STAT);
  241. if (unlikely(!(host_irq_stat & HIRQ_GLOBAL)))
  242. goto out;
  243. spin_lock(&host->lock);
  244. for (i = 0; i < NR_PORTS; i++) {
  245. struct ata_port *ap = host->ports[i];
  246. if (!(host_irq_stat & (HIRQ_PORT0 << i)))
  247. continue;
  248. if (likely(ap && !(ap->flags & ATA_FLAG_DISABLED))) {
  249. inic_host_intr(ap);
  250. handled++;
  251. } else {
  252. if (ata_ratelimit())
  253. dev_printk(KERN_ERR, host->dev, "interrupt "
  254. "from disabled port %d (0x%x)\n",
  255. i, host_irq_stat);
  256. }
  257. }
  258. spin_unlock(&host->lock);
  259. out:
  260. return IRQ_RETVAL(handled);
  261. }
  262. static unsigned int inic_qc_issue(struct ata_queued_cmd *qc)
  263. {
  264. struct ata_port *ap = qc->ap;
  265. /* ATA IRQ doesn't wait for DMA transfer completion and vice
  266. * versa. Mask IRQ selectively to detect command completion.
  267. * Without it, ATA DMA read command can cause data corruption.
  268. *
  269. * Something similar might be needed for ATAPI writes. I
  270. * tried a lot of combinations but couldn't find the solution.
  271. */
  272. if (qc->tf.protocol == ATA_PROT_DMA &&
  273. !(qc->tf.flags & ATA_TFLAG_WRITE))
  274. inic_set_pirq_mask(ap, PIRQ_MASK_DMA_READ);
  275. else
  276. inic_set_pirq_mask(ap, PIRQ_MASK_OTHER);
  277. /* Issuing a command to yet uninitialized port locks up the
  278. * controller. Most of the time, this happens for the first
  279. * command after reset which are ATA and ATAPI IDENTIFYs.
  280. * Fast fail if stat is 0x7f or 0xff for those commands.
  281. */
  282. if (unlikely(qc->tf.command == ATA_CMD_ID_ATA ||
  283. qc->tf.command == ATA_CMD_ID_ATAPI)) {
  284. u8 stat = ata_chk_status(ap);
  285. if (stat == 0x7f || stat == 0xff)
  286. return AC_ERR_HSM;
  287. }
  288. return ata_qc_issue_prot(qc);
  289. }
  290. static void inic_freeze(struct ata_port *ap)
  291. {
  292. void __iomem *port_base = inic_port_base(ap);
  293. __inic_set_pirq_mask(ap, PIRQ_MASK_FREEZE);
  294. ata_chk_status(ap);
  295. writeb(0xff, port_base + PORT_IRQ_STAT);
  296. readb(port_base + PORT_IRQ_STAT); /* flush */
  297. }
  298. static void inic_thaw(struct ata_port *ap)
  299. {
  300. void __iomem *port_base = inic_port_base(ap);
  301. ata_chk_status(ap);
  302. writeb(0xff, port_base + PORT_IRQ_STAT);
  303. __inic_set_pirq_mask(ap, PIRQ_MASK_OTHER);
  304. readb(port_base + PORT_IRQ_STAT); /* flush */
  305. }
  306. /*
  307. * SRST and SControl hardreset don't give valid signature on this
  308. * controller. Only controller specific hardreset mechanism works.
  309. */
  310. static int inic_hardreset(struct ata_link *link, unsigned int *class,
  311. unsigned long deadline)
  312. {
  313. struct ata_port *ap = link->ap;
  314. void __iomem *port_base = inic_port_base(ap);
  315. void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
  316. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  317. u16 val;
  318. int rc;
  319. /* hammer it into sane state */
  320. inic_reset_port(port_base);
  321. val = readw(idma_ctl);
  322. writew(val | IDMA_CTL_RST_ATA, idma_ctl);
  323. readw(idma_ctl); /* flush */
  324. msleep(1);
  325. writew(val & ~IDMA_CTL_RST_ATA, idma_ctl);
  326. rc = sata_link_resume(link, timing, deadline);
  327. if (rc) {
  328. ata_link_printk(link, KERN_WARNING, "failed to resume "
  329. "link after reset (errno=%d)\n", rc);
  330. return rc;
  331. }
  332. *class = ATA_DEV_NONE;
  333. if (ata_link_online(link)) {
  334. struct ata_taskfile tf;
  335. /* wait a while before checking status */
  336. ata_wait_after_reset(ap, deadline);
  337. rc = ata_wait_ready(ap, deadline);
  338. /* link occupied, -ENODEV too is an error */
  339. if (rc) {
  340. ata_link_printk(link, KERN_WARNING, "device not ready "
  341. "after hardreset (errno=%d)\n", rc);
  342. return rc;
  343. }
  344. ata_tf_read(ap, &tf);
  345. *class = ata_dev_classify(&tf);
  346. if (*class == ATA_DEV_UNKNOWN)
  347. *class = ATA_DEV_NONE;
  348. }
  349. return 0;
  350. }
  351. static void inic_error_handler(struct ata_port *ap)
  352. {
  353. void __iomem *port_base = inic_port_base(ap);
  354. struct inic_port_priv *pp = ap->private_data;
  355. unsigned long flags;
  356. /* reset PIO HSM and stop DMA engine */
  357. inic_reset_port(port_base);
  358. spin_lock_irqsave(ap->lock, flags);
  359. ap->hsm_task_state = HSM_ST_IDLE;
  360. writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
  361. spin_unlock_irqrestore(ap->lock, flags);
  362. /* PIO and DMA engines have been stopped, perform recovery */
  363. ata_std_error_handler(ap);
  364. }
  365. static void inic_post_internal_cmd(struct ata_queued_cmd *qc)
  366. {
  367. /* make DMA engine forget about the failed command */
  368. if (qc->flags & ATA_QCFLAG_FAILED)
  369. inic_reset_port(inic_port_base(qc->ap));
  370. }
  371. static void inic_dev_config(struct ata_device *dev)
  372. {
  373. /* inic can only handle upto LBA28 max sectors */
  374. if (dev->max_sectors > ATA_MAX_SECTORS)
  375. dev->max_sectors = ATA_MAX_SECTORS;
  376. if (dev->n_sectors >= 1 << 28) {
  377. ata_dev_printk(dev, KERN_ERR,
  378. "ERROR: This driver doesn't support LBA48 yet and may cause\n"
  379. " data corruption on such devices. Disabling.\n");
  380. ata_dev_disable(dev);
  381. }
  382. }
  383. static void init_port(struct ata_port *ap)
  384. {
  385. void __iomem *port_base = inic_port_base(ap);
  386. /* Setup PRD address */
  387. writel(ap->prd_dma, port_base + PORT_PRD_ADDR);
  388. }
  389. static int inic_port_resume(struct ata_port *ap)
  390. {
  391. init_port(ap);
  392. return 0;
  393. }
  394. static int inic_port_start(struct ata_port *ap)
  395. {
  396. void __iomem *port_base = inic_port_base(ap);
  397. struct inic_port_priv *pp;
  398. u8 tmp;
  399. int rc;
  400. /* alloc and initialize private data */
  401. pp = devm_kzalloc(ap->host->dev, sizeof(*pp), GFP_KERNEL);
  402. if (!pp)
  403. return -ENOMEM;
  404. ap->private_data = pp;
  405. /* default PRD_CTL value, DMAEN, WR and START off */
  406. tmp = readb(port_base + PORT_PRD_CTL);
  407. tmp &= ~(PRD_CTL_DMAEN | PRD_CTL_WR | PRD_CTL_START);
  408. pp->dfl_prdctl = tmp;
  409. /* Alloc resources */
  410. rc = ata_port_start(ap);
  411. if (rc) {
  412. kfree(pp);
  413. return rc;
  414. }
  415. init_port(ap);
  416. return 0;
  417. }
  418. static struct ata_port_operations inic_port_ops = {
  419. .inherits = &ata_sff_port_ops,
  420. .bmdma_setup = inic_bmdma_setup,
  421. .bmdma_start = inic_bmdma_start,
  422. .bmdma_stop = inic_bmdma_stop,
  423. .bmdma_status = inic_bmdma_status,
  424. .qc_issue = inic_qc_issue,
  425. .freeze = inic_freeze,
  426. .thaw = inic_thaw,
  427. .softreset = ATA_OP_NULL, /* softreset is broken */
  428. .hardreset = inic_hardreset,
  429. .error_handler = inic_error_handler,
  430. .post_internal_cmd = inic_post_internal_cmd,
  431. .dev_config = inic_dev_config,
  432. .scr_read = inic_scr_read,
  433. .scr_write = inic_scr_write,
  434. .port_resume = inic_port_resume,
  435. .port_start = inic_port_start,
  436. };
  437. static struct ata_port_info inic_port_info = {
  438. /* For some reason, ATAPI_PROT_PIO is broken on this
  439. * controller, and no, PIO_POLLING does't fix it. It somehow
  440. * manages to report the wrong ireason and ignoring ireason
  441. * results in machine lock up. Tell libata to always prefer
  442. * DMA.
  443. */
  444. .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
  445. .pio_mask = 0x1f, /* pio0-4 */
  446. .mwdma_mask = 0x07, /* mwdma0-2 */
  447. .udma_mask = ATA_UDMA6,
  448. .port_ops = &inic_port_ops
  449. };
  450. static int init_controller(void __iomem *mmio_base, u16 hctl)
  451. {
  452. int i;
  453. u16 val;
  454. hctl &= ~HCTL_KNOWN_BITS;
  455. /* Soft reset whole controller. Spec says reset duration is 3
  456. * PCI clocks, be generous and give it 10ms.
  457. */
  458. writew(hctl | HCTL_SOFTRST, mmio_base + HOST_CTL);
  459. readw(mmio_base + HOST_CTL); /* flush */
  460. for (i = 0; i < 10; i++) {
  461. msleep(1);
  462. val = readw(mmio_base + HOST_CTL);
  463. if (!(val & HCTL_SOFTRST))
  464. break;
  465. }
  466. if (val & HCTL_SOFTRST)
  467. return -EIO;
  468. /* mask all interrupts and reset ports */
  469. for (i = 0; i < NR_PORTS; i++) {
  470. void __iomem *port_base = mmio_base + i * PORT_SIZE;
  471. writeb(0xff, port_base + PORT_IRQ_MASK);
  472. inic_reset_port(port_base);
  473. }
  474. /* port IRQ is masked now, unmask global IRQ */
  475. writew(hctl & ~HCTL_IRQOFF, mmio_base + HOST_CTL);
  476. val = readw(mmio_base + HOST_IRQ_MASK);
  477. val &= ~(HIRQ_PORT0 | HIRQ_PORT1);
  478. writew(val, mmio_base + HOST_IRQ_MASK);
  479. return 0;
  480. }
  481. #ifdef CONFIG_PM
  482. static int inic_pci_device_resume(struct pci_dev *pdev)
  483. {
  484. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  485. struct inic_host_priv *hpriv = host->private_data;
  486. void __iomem *mmio_base = host->iomap[MMIO_BAR];
  487. int rc;
  488. rc = ata_pci_device_do_resume(pdev);
  489. if (rc)
  490. return rc;
  491. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  492. rc = init_controller(mmio_base, hpriv->cached_hctl);
  493. if (rc)
  494. return rc;
  495. }
  496. ata_host_resume(host);
  497. return 0;
  498. }
  499. #endif
  500. static int inic_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  501. {
  502. static int printed_version;
  503. const struct ata_port_info *ppi[] = { &inic_port_info, NULL };
  504. struct ata_host *host;
  505. struct inic_host_priv *hpriv;
  506. void __iomem * const *iomap;
  507. int i, rc;
  508. if (!printed_version++)
  509. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  510. /* alloc host */
  511. host = ata_host_alloc_pinfo(&pdev->dev, ppi, NR_PORTS);
  512. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  513. if (!host || !hpriv)
  514. return -ENOMEM;
  515. host->private_data = hpriv;
  516. /* acquire resources and fill host */
  517. rc = pcim_enable_device(pdev);
  518. if (rc)
  519. return rc;
  520. rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME);
  521. if (rc)
  522. return rc;
  523. host->iomap = iomap = pcim_iomap_table(pdev);
  524. for (i = 0; i < NR_PORTS; i++) {
  525. struct ata_port *ap = host->ports[i];
  526. struct ata_ioports *port = &ap->ioaddr;
  527. unsigned int offset = i * PORT_SIZE;
  528. port->cmd_addr = iomap[2 * i];
  529. port->altstatus_addr =
  530. port->ctl_addr = (void __iomem *)
  531. ((unsigned long)iomap[2 * i + 1] | ATA_PCI_CTL_OFS);
  532. port->scr_addr = iomap[MMIO_BAR] + offset + PORT_SCR;
  533. ata_std_ports(port);
  534. ata_port_pbar_desc(ap, MMIO_BAR, -1, "mmio");
  535. ata_port_pbar_desc(ap, MMIO_BAR, offset, "port");
  536. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  537. (unsigned long long)pci_resource_start(pdev, 2 * i),
  538. (unsigned long long)pci_resource_start(pdev, (2 * i + 1)) |
  539. ATA_PCI_CTL_OFS);
  540. }
  541. hpriv->cached_hctl = readw(iomap[MMIO_BAR] + HOST_CTL);
  542. /* Set dma_mask. This devices doesn't support 64bit addressing. */
  543. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  544. if (rc) {
  545. dev_printk(KERN_ERR, &pdev->dev,
  546. "32-bit DMA enable failed\n");
  547. return rc;
  548. }
  549. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  550. if (rc) {
  551. dev_printk(KERN_ERR, &pdev->dev,
  552. "32-bit consistent DMA enable failed\n");
  553. return rc;
  554. }
  555. /*
  556. * This controller is braindamaged. dma_boundary is 0xffff
  557. * like others but it will lock up the whole machine HARD if
  558. * 65536 byte PRD entry is fed. Reduce maximum segment size.
  559. */
  560. rc = pci_set_dma_max_seg_size(pdev, 65536 - 512);
  561. if (rc) {
  562. dev_printk(KERN_ERR, &pdev->dev,
  563. "failed to set the maximum segment size.\n");
  564. return rc;
  565. }
  566. rc = init_controller(iomap[MMIO_BAR], hpriv->cached_hctl);
  567. if (rc) {
  568. dev_printk(KERN_ERR, &pdev->dev,
  569. "failed to initialize controller\n");
  570. return rc;
  571. }
  572. pci_set_master(pdev);
  573. return ata_host_activate(host, pdev->irq, inic_interrupt, IRQF_SHARED,
  574. &inic_sht);
  575. }
  576. static const struct pci_device_id inic_pci_tbl[] = {
  577. { PCI_VDEVICE(INIT, 0x1622), },
  578. { },
  579. };
  580. static struct pci_driver inic_pci_driver = {
  581. .name = DRV_NAME,
  582. .id_table = inic_pci_tbl,
  583. #ifdef CONFIG_PM
  584. .suspend = ata_pci_device_suspend,
  585. .resume = inic_pci_device_resume,
  586. #endif
  587. .probe = inic_init_one,
  588. .remove = ata_pci_remove_one,
  589. };
  590. static int __init inic_init(void)
  591. {
  592. return pci_register_driver(&inic_pci_driver);
  593. }
  594. static void __exit inic_exit(void)
  595. {
  596. pci_unregister_driver(&inic_pci_driver);
  597. }
  598. MODULE_AUTHOR("Tejun Heo");
  599. MODULE_DESCRIPTION("low-level driver for Initio 162x SATA");
  600. MODULE_LICENSE("GPL v2");
  601. MODULE_DEVICE_TABLE(pci, inic_pci_tbl);
  602. MODULE_VERSION(DRV_VERSION);
  603. module_init(inic_init);
  604. module_exit(inic_exit);