sata_inic162x.c 19 KB

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