sata_inic162x.c 19 KB

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