sata_inic162x.c 19 KB

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