sata_inic162x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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_PCI = 5,
  32. MMIO_BAR_CARDBUS = 1,
  33. NR_PORTS = 2,
  34. IDMA_CPB_TBL_SIZE = 4 * 32,
  35. INIC_DMA_BOUNDARY = 0xffffff,
  36. HOST_ACTRL = 0x08,
  37. HOST_CTL = 0x7c,
  38. HOST_STAT = 0x7e,
  39. HOST_IRQ_STAT = 0xbc,
  40. HOST_IRQ_MASK = 0xbe,
  41. PORT_SIZE = 0x40,
  42. /* registers for ATA TF operation */
  43. PORT_TF_DATA = 0x00,
  44. PORT_TF_FEATURE = 0x01,
  45. PORT_TF_NSECT = 0x02,
  46. PORT_TF_LBAL = 0x03,
  47. PORT_TF_LBAM = 0x04,
  48. PORT_TF_LBAH = 0x05,
  49. PORT_TF_DEVICE = 0x06,
  50. PORT_TF_COMMAND = 0x07,
  51. PORT_TF_ALT_STAT = 0x08,
  52. PORT_IRQ_STAT = 0x09,
  53. PORT_IRQ_MASK = 0x0a,
  54. PORT_PRD_CTL = 0x0b,
  55. PORT_PRD_ADDR = 0x0c,
  56. PORT_PRD_XFERLEN = 0x10,
  57. PORT_CPB_CPBLAR = 0x18,
  58. PORT_CPB_PTQFIFO = 0x1c,
  59. /* IDMA register */
  60. PORT_IDMA_CTL = 0x14,
  61. PORT_IDMA_STAT = 0x16,
  62. PORT_RPQ_FIFO = 0x1e,
  63. PORT_RPQ_CNT = 0x1f,
  64. PORT_SCR = 0x20,
  65. /* HOST_CTL bits */
  66. HCTL_IRQOFF = (1 << 8), /* global IRQ off */
  67. HCTL_FTHD0 = (1 << 10), /* fifo threshold 0 */
  68. HCTL_FTHD1 = (1 << 11), /* fifo threshold 1*/
  69. HCTL_PWRDWN = (1 << 12), /* power down PHYs */
  70. HCTL_SOFTRST = (1 << 13), /* global reset (no phy reset) */
  71. HCTL_RPGSEL = (1 << 15), /* register page select */
  72. HCTL_KNOWN_BITS = HCTL_IRQOFF | HCTL_PWRDWN | HCTL_SOFTRST |
  73. HCTL_RPGSEL,
  74. /* HOST_IRQ_(STAT|MASK) bits */
  75. HIRQ_PORT0 = (1 << 0),
  76. HIRQ_PORT1 = (1 << 1),
  77. HIRQ_SOFT = (1 << 14),
  78. HIRQ_GLOBAL = (1 << 15), /* STAT only */
  79. /* PORT_IRQ_(STAT|MASK) bits */
  80. PIRQ_OFFLINE = (1 << 0), /* device unplugged */
  81. PIRQ_ONLINE = (1 << 1), /* device plugged */
  82. PIRQ_COMPLETE = (1 << 2), /* completion interrupt */
  83. PIRQ_FATAL = (1 << 3), /* fatal error */
  84. PIRQ_ATA = (1 << 4), /* ATA interrupt */
  85. PIRQ_REPLY = (1 << 5), /* reply FIFO not empty */
  86. PIRQ_PENDING = (1 << 7), /* port IRQ pending (STAT only) */
  87. PIRQ_ERR = PIRQ_OFFLINE | PIRQ_ONLINE | PIRQ_FATAL,
  88. PIRQ_MASK_DEFAULT = PIRQ_REPLY | PIRQ_ATA,
  89. PIRQ_MASK_FREEZE = 0xff,
  90. /* PORT_PRD_CTL bits */
  91. PRD_CTL_START = (1 << 0),
  92. PRD_CTL_WR = (1 << 3),
  93. PRD_CTL_DMAEN = (1 << 7), /* DMA enable */
  94. /* PORT_IDMA_CTL bits */
  95. IDMA_CTL_RST_ATA = (1 << 2), /* hardreset ATA bus */
  96. IDMA_CTL_RST_IDMA = (1 << 5), /* reset IDMA machinary */
  97. IDMA_CTL_GO = (1 << 7), /* IDMA mode go */
  98. IDMA_CTL_ATA_NIEN = (1 << 8), /* ATA IRQ disable */
  99. /* PORT_IDMA_STAT bits */
  100. IDMA_STAT_PERR = (1 << 0), /* PCI ERROR MODE */
  101. IDMA_STAT_CPBERR = (1 << 1), /* ADMA CPB error */
  102. IDMA_STAT_LGCY = (1 << 3), /* ADMA legacy */
  103. IDMA_STAT_UIRQ = (1 << 4), /* ADMA unsolicited irq */
  104. IDMA_STAT_STPD = (1 << 5), /* ADMA stopped */
  105. IDMA_STAT_PSD = (1 << 6), /* ADMA pause */
  106. IDMA_STAT_DONE = (1 << 7), /* ADMA done */
  107. IDMA_STAT_ERR = IDMA_STAT_PERR | IDMA_STAT_CPBERR,
  108. /* CPB Control Flags*/
  109. CPB_CTL_VALID = (1 << 0), /* CPB valid */
  110. CPB_CTL_QUEUED = (1 << 1), /* queued command */
  111. CPB_CTL_DATA = (1 << 2), /* data, rsvd in datasheet */
  112. CPB_CTL_IEN = (1 << 3), /* PCI interrupt enable */
  113. CPB_CTL_DEVDIR = (1 << 4), /* device direction control */
  114. /* CPB Response Flags */
  115. CPB_RESP_DONE = (1 << 0), /* ATA command complete */
  116. CPB_RESP_REL = (1 << 1), /* ATA release */
  117. CPB_RESP_IGNORED = (1 << 2), /* CPB ignored */
  118. CPB_RESP_ATA_ERR = (1 << 3), /* ATA command error */
  119. CPB_RESP_SPURIOUS = (1 << 4), /* ATA spurious interrupt error */
  120. CPB_RESP_UNDERFLOW = (1 << 5), /* APRD deficiency length error */
  121. CPB_RESP_OVERFLOW = (1 << 6), /* APRD exccess length error */
  122. CPB_RESP_CPB_ERR = (1 << 7), /* CPB error flag */
  123. /* PRD Control Flags */
  124. PRD_DRAIN = (1 << 1), /* ignore data excess */
  125. PRD_CDB = (1 << 2), /* atapi packet command pointer */
  126. PRD_DIRECT_INTR = (1 << 3), /* direct interrupt */
  127. PRD_DMA = (1 << 4), /* data transfer method */
  128. PRD_WRITE = (1 << 5), /* data dir, rsvd in datasheet */
  129. PRD_IOM = (1 << 6), /* io/memory transfer */
  130. PRD_END = (1 << 7), /* APRD chain end */
  131. };
  132. /* Comman Parameter Block */
  133. struct inic_cpb {
  134. u8 resp_flags; /* Response Flags */
  135. u8 error; /* ATA Error */
  136. u8 status; /* ATA Status */
  137. u8 ctl_flags; /* Control Flags */
  138. __le32 len; /* Total Transfer Length */
  139. __le32 prd; /* First PRD pointer */
  140. u8 rsvd[4];
  141. /* 16 bytes */
  142. u8 feature; /* ATA Feature */
  143. u8 hob_feature; /* ATA Ex. Feature */
  144. u8 device; /* ATA Device/Head */
  145. u8 mirctl; /* Mirror Control */
  146. u8 nsect; /* ATA Sector Count */
  147. u8 hob_nsect; /* ATA Ex. Sector Count */
  148. u8 lbal; /* ATA Sector Number */
  149. u8 hob_lbal; /* ATA Ex. Sector Number */
  150. u8 lbam; /* ATA Cylinder Low */
  151. u8 hob_lbam; /* ATA Ex. Cylinder Low */
  152. u8 lbah; /* ATA Cylinder High */
  153. u8 hob_lbah; /* ATA Ex. Cylinder High */
  154. u8 command; /* ATA Command */
  155. u8 ctl; /* ATA Control */
  156. u8 slave_error; /* Slave ATA Error */
  157. u8 slave_status; /* Slave ATA Status */
  158. /* 32 bytes */
  159. } __packed;
  160. /* Physical Region Descriptor */
  161. struct inic_prd {
  162. __le32 mad; /* Physical Memory Address */
  163. __le16 len; /* Transfer Length */
  164. u8 rsvd;
  165. u8 flags; /* Control Flags */
  166. } __packed;
  167. struct inic_pkt {
  168. struct inic_cpb cpb;
  169. struct inic_prd prd[LIBATA_MAX_PRD + 1]; /* + 1 for cdb */
  170. u8 cdb[ATAPI_CDB_LEN];
  171. } __packed;
  172. struct inic_host_priv {
  173. void __iomem *mmio_base;
  174. u16 cached_hctl;
  175. };
  176. struct inic_port_priv {
  177. struct inic_pkt *pkt;
  178. dma_addr_t pkt_dma;
  179. u32 *cpb_tbl;
  180. dma_addr_t cpb_tbl_dma;
  181. };
  182. static struct scsi_host_template inic_sht = {
  183. ATA_BASE_SHT(DRV_NAME),
  184. .sg_tablesize = LIBATA_MAX_PRD, /* maybe it can be larger? */
  185. .dma_boundary = INIC_DMA_BOUNDARY,
  186. };
  187. static const int scr_map[] = {
  188. [SCR_STATUS] = 0,
  189. [SCR_ERROR] = 1,
  190. [SCR_CONTROL] = 2,
  191. };
  192. static void __iomem *inic_port_base(struct ata_port *ap)
  193. {
  194. struct inic_host_priv *hpriv = ap->host->private_data;
  195. return hpriv->mmio_base + ap->port_no * PORT_SIZE;
  196. }
  197. static void inic_reset_port(void __iomem *port_base)
  198. {
  199. void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
  200. /* stop IDMA engine */
  201. readw(idma_ctl); /* flush */
  202. msleep(1);
  203. /* mask IRQ and assert reset */
  204. writew(IDMA_CTL_RST_IDMA, idma_ctl);
  205. readw(idma_ctl); /* flush */
  206. msleep(1);
  207. /* release reset */
  208. writew(0, idma_ctl);
  209. /* clear irq */
  210. writeb(0xff, port_base + PORT_IRQ_STAT);
  211. }
  212. static int inic_scr_read(struct ata_port *ap, unsigned sc_reg, u32 *val)
  213. {
  214. void __iomem *scr_addr = inic_port_base(ap) + PORT_SCR;
  215. void __iomem *addr;
  216. if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
  217. return -EINVAL;
  218. addr = scr_addr + scr_map[sc_reg] * 4;
  219. *val = readl(scr_addr + scr_map[sc_reg] * 4);
  220. /* this controller has stuck DIAG.N, ignore it */
  221. if (sc_reg == SCR_ERROR)
  222. *val &= ~SERR_PHYRDY_CHG;
  223. return 0;
  224. }
  225. static int inic_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
  226. {
  227. void __iomem *scr_addr = inic_port_base(ap) + PORT_SCR;
  228. if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
  229. return -EINVAL;
  230. writel(val, scr_addr + scr_map[sc_reg] * 4);
  231. return 0;
  232. }
  233. static void inic_stop_idma(struct ata_port *ap)
  234. {
  235. void __iomem *port_base = inic_port_base(ap);
  236. readb(port_base + PORT_RPQ_FIFO);
  237. readb(port_base + PORT_RPQ_CNT);
  238. writew(0, port_base + PORT_IDMA_CTL);
  239. }
  240. static void inic_host_err_intr(struct ata_port *ap, u8 irq_stat, u16 idma_stat)
  241. {
  242. struct ata_eh_info *ehi = &ap->link.eh_info;
  243. struct inic_port_priv *pp = ap->private_data;
  244. struct inic_cpb *cpb = &pp->pkt->cpb;
  245. bool freeze = false;
  246. ata_ehi_clear_desc(ehi);
  247. ata_ehi_push_desc(ehi, "irq_stat=0x%x idma_stat=0x%x",
  248. irq_stat, idma_stat);
  249. inic_stop_idma(ap);
  250. if (irq_stat & (PIRQ_OFFLINE | PIRQ_ONLINE)) {
  251. ata_ehi_push_desc(ehi, "hotplug");
  252. ata_ehi_hotplugged(ehi);
  253. freeze = true;
  254. }
  255. if (idma_stat & IDMA_STAT_PERR) {
  256. ata_ehi_push_desc(ehi, "PCI error");
  257. freeze = true;
  258. }
  259. if (idma_stat & IDMA_STAT_CPBERR) {
  260. ata_ehi_push_desc(ehi, "CPB error");
  261. if (cpb->resp_flags & CPB_RESP_IGNORED) {
  262. __ata_ehi_push_desc(ehi, " ignored");
  263. ehi->err_mask |= AC_ERR_INVALID;
  264. freeze = true;
  265. }
  266. if (cpb->resp_flags & CPB_RESP_ATA_ERR)
  267. ehi->err_mask |= AC_ERR_DEV;
  268. if (cpb->resp_flags & CPB_RESP_SPURIOUS) {
  269. __ata_ehi_push_desc(ehi, " spurious-intr");
  270. ehi->err_mask |= AC_ERR_HSM;
  271. freeze = true;
  272. }
  273. if (cpb->resp_flags &
  274. (CPB_RESP_UNDERFLOW | CPB_RESP_OVERFLOW)) {
  275. __ata_ehi_push_desc(ehi, " data-over/underflow");
  276. ehi->err_mask |= AC_ERR_HSM;
  277. freeze = true;
  278. }
  279. }
  280. if (freeze)
  281. ata_port_freeze(ap);
  282. else
  283. ata_port_abort(ap);
  284. }
  285. static void inic_host_intr(struct ata_port *ap)
  286. {
  287. void __iomem *port_base = inic_port_base(ap);
  288. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
  289. u8 irq_stat;
  290. u16 idma_stat;
  291. /* read and clear IRQ status */
  292. irq_stat = readb(port_base + PORT_IRQ_STAT);
  293. writeb(irq_stat, port_base + PORT_IRQ_STAT);
  294. idma_stat = readw(port_base + PORT_IDMA_STAT);
  295. if (unlikely((irq_stat & PIRQ_ERR) || (idma_stat & IDMA_STAT_ERR)))
  296. inic_host_err_intr(ap, irq_stat, idma_stat);
  297. if (unlikely(!qc))
  298. goto spurious;
  299. if (likely(idma_stat & IDMA_STAT_DONE)) {
  300. inic_stop_idma(ap);
  301. /* Depending on circumstances, device error
  302. * isn't reported by IDMA, check it explicitly.
  303. */
  304. if (unlikely(readb(port_base + PORT_TF_COMMAND) &
  305. (ATA_DF | ATA_ERR)))
  306. qc->err_mask |= AC_ERR_DEV;
  307. ata_qc_complete(qc);
  308. return;
  309. }
  310. spurious:
  311. ata_port_printk(ap, KERN_WARNING, "unhandled interrupt: "
  312. "cmd=0x%x irq_stat=0x%x idma_stat=0x%x\n",
  313. qc ? qc->tf.command : 0xff, irq_stat, idma_stat);
  314. }
  315. static irqreturn_t inic_interrupt(int irq, void *dev_instance)
  316. {
  317. struct ata_host *host = dev_instance;
  318. struct inic_host_priv *hpriv = host->private_data;
  319. u16 host_irq_stat;
  320. int i, handled = 0;;
  321. host_irq_stat = readw(hpriv->mmio_base + HOST_IRQ_STAT);
  322. if (unlikely(!(host_irq_stat & HIRQ_GLOBAL)))
  323. goto out;
  324. spin_lock(&host->lock);
  325. for (i = 0; i < NR_PORTS; i++) {
  326. struct ata_port *ap = host->ports[i];
  327. if (!(host_irq_stat & (HIRQ_PORT0 << i)))
  328. continue;
  329. if (likely(ap && !(ap->flags & ATA_FLAG_DISABLED))) {
  330. inic_host_intr(ap);
  331. handled++;
  332. } else {
  333. if (ata_ratelimit())
  334. dev_printk(KERN_ERR, host->dev, "interrupt "
  335. "from disabled port %d (0x%x)\n",
  336. i, host_irq_stat);
  337. }
  338. }
  339. spin_unlock(&host->lock);
  340. out:
  341. return IRQ_RETVAL(handled);
  342. }
  343. static int inic_check_atapi_dma(struct ata_queued_cmd *qc)
  344. {
  345. /* For some reason ATAPI_PROT_DMA doesn't work for some
  346. * commands including writes and other misc ops. Use PIO
  347. * protocol instead, which BTW is driven by the DMA engine
  348. * anyway, so it shouldn't make much difference for native
  349. * SATA devices.
  350. */
  351. if (atapi_cmd_type(qc->cdb[0]) == READ)
  352. return 0;
  353. return 1;
  354. }
  355. static void inic_fill_sg(struct inic_prd *prd, struct ata_queued_cmd *qc)
  356. {
  357. struct scatterlist *sg;
  358. unsigned int si;
  359. u8 flags = 0;
  360. if (qc->tf.flags & ATA_TFLAG_WRITE)
  361. flags |= PRD_WRITE;
  362. if (ata_is_dma(qc->tf.protocol))
  363. flags |= PRD_DMA;
  364. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  365. prd->mad = cpu_to_le32(sg_dma_address(sg));
  366. prd->len = cpu_to_le16(sg_dma_len(sg));
  367. prd->flags = flags;
  368. prd++;
  369. }
  370. WARN_ON(!si);
  371. prd[-1].flags |= PRD_END;
  372. }
  373. static void inic_qc_prep(struct ata_queued_cmd *qc)
  374. {
  375. struct inic_port_priv *pp = qc->ap->private_data;
  376. struct inic_pkt *pkt = pp->pkt;
  377. struct inic_cpb *cpb = &pkt->cpb;
  378. struct inic_prd *prd = pkt->prd;
  379. bool is_atapi = ata_is_atapi(qc->tf.protocol);
  380. bool is_data = ata_is_data(qc->tf.protocol);
  381. unsigned int cdb_len = 0;
  382. VPRINTK("ENTER\n");
  383. if (is_atapi)
  384. cdb_len = qc->dev->cdb_len;
  385. /* prepare packet, based on initio driver */
  386. memset(pkt, 0, sizeof(struct inic_pkt));
  387. cpb->ctl_flags = CPB_CTL_VALID | CPB_CTL_IEN;
  388. if (is_atapi || is_data)
  389. cpb->ctl_flags |= CPB_CTL_DATA;
  390. cpb->len = cpu_to_le32(qc->nbytes + cdb_len);
  391. cpb->prd = cpu_to_le32(pp->pkt_dma + offsetof(struct inic_pkt, prd));
  392. cpb->device = qc->tf.device;
  393. cpb->feature = qc->tf.feature;
  394. cpb->nsect = qc->tf.nsect;
  395. cpb->lbal = qc->tf.lbal;
  396. cpb->lbam = qc->tf.lbam;
  397. cpb->lbah = qc->tf.lbah;
  398. if (qc->tf.flags & ATA_TFLAG_LBA48) {
  399. cpb->hob_feature = qc->tf.hob_feature;
  400. cpb->hob_nsect = qc->tf.hob_nsect;
  401. cpb->hob_lbal = qc->tf.hob_lbal;
  402. cpb->hob_lbam = qc->tf.hob_lbam;
  403. cpb->hob_lbah = qc->tf.hob_lbah;
  404. }
  405. cpb->command = qc->tf.command;
  406. /* don't load ctl - dunno why. it's like that in the initio driver */
  407. /* setup PRD for CDB */
  408. if (is_atapi) {
  409. memcpy(pkt->cdb, qc->cdb, ATAPI_CDB_LEN);
  410. prd->mad = cpu_to_le32(pp->pkt_dma +
  411. offsetof(struct inic_pkt, cdb));
  412. prd->len = cpu_to_le16(cdb_len);
  413. prd->flags = PRD_CDB | PRD_WRITE;
  414. if (!is_data)
  415. prd->flags |= PRD_END;
  416. prd++;
  417. }
  418. /* setup sg table */
  419. if (is_data)
  420. inic_fill_sg(prd, qc);
  421. pp->cpb_tbl[0] = pp->pkt_dma;
  422. }
  423. static unsigned int inic_qc_issue(struct ata_queued_cmd *qc)
  424. {
  425. struct ata_port *ap = qc->ap;
  426. void __iomem *port_base = inic_port_base(ap);
  427. /* fire up the ADMA engine */
  428. writew(HCTL_FTHD0, port_base + HOST_CTL);
  429. writew(IDMA_CTL_GO, port_base + PORT_IDMA_CTL);
  430. writeb(0, port_base + PORT_CPB_PTQFIFO);
  431. return 0;
  432. }
  433. static void inic_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  434. {
  435. void __iomem *port_base = inic_port_base(ap);
  436. tf->feature = readb(port_base + PORT_TF_FEATURE);
  437. tf->nsect = readb(port_base + PORT_TF_NSECT);
  438. tf->lbal = readb(port_base + PORT_TF_LBAL);
  439. tf->lbam = readb(port_base + PORT_TF_LBAM);
  440. tf->lbah = readb(port_base + PORT_TF_LBAH);
  441. tf->device = readb(port_base + PORT_TF_DEVICE);
  442. tf->command = readb(port_base + PORT_TF_COMMAND);
  443. }
  444. static bool inic_qc_fill_rtf(struct ata_queued_cmd *qc)
  445. {
  446. struct ata_taskfile *rtf = &qc->result_tf;
  447. struct ata_taskfile tf;
  448. /* FIXME: Except for status and error, result TF access
  449. * doesn't work. I tried reading from BAR0/2, CPB and BAR5.
  450. * None works regardless of which command interface is used.
  451. * For now return true iff status indicates device error.
  452. * This means that we're reporting bogus sector for RW
  453. * failures. Eeekk....
  454. */
  455. inic_tf_read(qc->ap, &tf);
  456. if (!(tf.command & ATA_ERR))
  457. return false;
  458. rtf->command = tf.command;
  459. rtf->feature = tf.feature;
  460. return true;
  461. }
  462. static void inic_freeze(struct ata_port *ap)
  463. {
  464. void __iomem *port_base = inic_port_base(ap);
  465. writeb(PIRQ_MASK_FREEZE, port_base + PORT_IRQ_MASK);
  466. writeb(0xff, port_base + PORT_IRQ_STAT);
  467. }
  468. static void inic_thaw(struct ata_port *ap)
  469. {
  470. void __iomem *port_base = inic_port_base(ap);
  471. writeb(0xff, port_base + PORT_IRQ_STAT);
  472. writeb(PIRQ_MASK_DEFAULT, port_base + PORT_IRQ_MASK);
  473. }
  474. static int inic_check_ready(struct ata_link *link)
  475. {
  476. void __iomem *port_base = inic_port_base(link->ap);
  477. return ata_check_ready(readb(port_base + PORT_TF_COMMAND));
  478. }
  479. /*
  480. * SRST and SControl hardreset don't give valid signature on this
  481. * controller. Only controller specific hardreset mechanism works.
  482. */
  483. static int inic_hardreset(struct ata_link *link, unsigned int *class,
  484. unsigned long deadline)
  485. {
  486. struct ata_port *ap = link->ap;
  487. void __iomem *port_base = inic_port_base(ap);
  488. void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
  489. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  490. int rc;
  491. /* hammer it into sane state */
  492. inic_reset_port(port_base);
  493. writew(IDMA_CTL_RST_ATA, idma_ctl);
  494. readw(idma_ctl); /* flush */
  495. msleep(1);
  496. writew(0, idma_ctl);
  497. rc = sata_link_resume(link, timing, deadline);
  498. if (rc) {
  499. ata_link_printk(link, KERN_WARNING, "failed to resume "
  500. "link after reset (errno=%d)\n", rc);
  501. return rc;
  502. }
  503. *class = ATA_DEV_NONE;
  504. if (ata_link_online(link)) {
  505. struct ata_taskfile tf;
  506. /* wait for link to become ready */
  507. rc = ata_wait_after_reset(link, deadline, inic_check_ready);
  508. /* link occupied, -ENODEV too is an error */
  509. if (rc) {
  510. ata_link_printk(link, KERN_WARNING, "device not ready "
  511. "after hardreset (errno=%d)\n", rc);
  512. return rc;
  513. }
  514. inic_tf_read(ap, &tf);
  515. *class = ata_dev_classify(&tf);
  516. }
  517. return 0;
  518. }
  519. static void inic_error_handler(struct ata_port *ap)
  520. {
  521. void __iomem *port_base = inic_port_base(ap);
  522. inic_reset_port(port_base);
  523. ata_std_error_handler(ap);
  524. }
  525. static void inic_post_internal_cmd(struct ata_queued_cmd *qc)
  526. {
  527. /* make DMA engine forget about the failed command */
  528. if (qc->flags & ATA_QCFLAG_FAILED)
  529. inic_reset_port(inic_port_base(qc->ap));
  530. }
  531. static void init_port(struct ata_port *ap)
  532. {
  533. void __iomem *port_base = inic_port_base(ap);
  534. struct inic_port_priv *pp = ap->private_data;
  535. /* clear packet and CPB table */
  536. memset(pp->pkt, 0, sizeof(struct inic_pkt));
  537. memset(pp->cpb_tbl, 0, IDMA_CPB_TBL_SIZE);
  538. /* setup PRD and CPB lookup table addresses */
  539. writel(ap->prd_dma, port_base + PORT_PRD_ADDR);
  540. writel(pp->cpb_tbl_dma, port_base + PORT_CPB_CPBLAR);
  541. }
  542. static int inic_port_resume(struct ata_port *ap)
  543. {
  544. init_port(ap);
  545. return 0;
  546. }
  547. static int inic_port_start(struct ata_port *ap)
  548. {
  549. struct device *dev = ap->host->dev;
  550. struct inic_port_priv *pp;
  551. int rc;
  552. /* alloc and initialize private data */
  553. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  554. if (!pp)
  555. return -ENOMEM;
  556. ap->private_data = pp;
  557. /* Alloc resources */
  558. rc = ata_port_start(ap);
  559. if (rc)
  560. return rc;
  561. pp->pkt = dmam_alloc_coherent(dev, sizeof(struct inic_pkt),
  562. &pp->pkt_dma, GFP_KERNEL);
  563. if (!pp->pkt)
  564. return -ENOMEM;
  565. pp->cpb_tbl = dmam_alloc_coherent(dev, IDMA_CPB_TBL_SIZE,
  566. &pp->cpb_tbl_dma, GFP_KERNEL);
  567. if (!pp->cpb_tbl)
  568. return -ENOMEM;
  569. init_port(ap);
  570. return 0;
  571. }
  572. static struct ata_port_operations inic_port_ops = {
  573. .inherits = &sata_port_ops,
  574. .check_atapi_dma = inic_check_atapi_dma,
  575. .qc_prep = inic_qc_prep,
  576. .qc_issue = inic_qc_issue,
  577. .qc_fill_rtf = inic_qc_fill_rtf,
  578. .freeze = inic_freeze,
  579. .thaw = inic_thaw,
  580. .hardreset = inic_hardreset,
  581. .error_handler = inic_error_handler,
  582. .post_internal_cmd = inic_post_internal_cmd,
  583. .scr_read = inic_scr_read,
  584. .scr_write = inic_scr_write,
  585. .port_resume = inic_port_resume,
  586. .port_start = inic_port_start,
  587. };
  588. static struct ata_port_info inic_port_info = {
  589. .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
  590. .pio_mask = 0x1f, /* pio0-4 */
  591. .mwdma_mask = 0x07, /* mwdma0-2 */
  592. .udma_mask = ATA_UDMA6,
  593. .port_ops = &inic_port_ops
  594. };
  595. static int init_controller(void __iomem *mmio_base, u16 hctl)
  596. {
  597. int i;
  598. u16 val;
  599. hctl &= ~HCTL_KNOWN_BITS;
  600. /* Soft reset whole controller. Spec says reset duration is 3
  601. * PCI clocks, be generous and give it 10ms.
  602. */
  603. writew(hctl | HCTL_SOFTRST, mmio_base + HOST_CTL);
  604. readw(mmio_base + HOST_CTL); /* flush */
  605. for (i = 0; i < 10; i++) {
  606. msleep(1);
  607. val = readw(mmio_base + HOST_CTL);
  608. if (!(val & HCTL_SOFTRST))
  609. break;
  610. }
  611. if (val & HCTL_SOFTRST)
  612. return -EIO;
  613. /* mask all interrupts and reset ports */
  614. for (i = 0; i < NR_PORTS; i++) {
  615. void __iomem *port_base = mmio_base + i * PORT_SIZE;
  616. writeb(0xff, port_base + PORT_IRQ_MASK);
  617. inic_reset_port(port_base);
  618. }
  619. /* port IRQ is masked now, unmask global IRQ */
  620. writew(hctl & ~HCTL_IRQOFF, mmio_base + HOST_CTL);
  621. val = readw(mmio_base + HOST_IRQ_MASK);
  622. val &= ~(HIRQ_PORT0 | HIRQ_PORT1);
  623. writew(val, mmio_base + HOST_IRQ_MASK);
  624. return 0;
  625. }
  626. #ifdef CONFIG_PM
  627. static int inic_pci_device_resume(struct pci_dev *pdev)
  628. {
  629. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  630. struct inic_host_priv *hpriv = host->private_data;
  631. int rc;
  632. rc = ata_pci_device_do_resume(pdev);
  633. if (rc)
  634. return rc;
  635. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  636. rc = init_controller(hpriv->mmio_base, hpriv->cached_hctl);
  637. if (rc)
  638. return rc;
  639. }
  640. ata_host_resume(host);
  641. return 0;
  642. }
  643. #endif
  644. static int inic_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  645. {
  646. static int printed_version;
  647. const struct ata_port_info *ppi[] = { &inic_port_info, NULL };
  648. struct ata_host *host;
  649. struct inic_host_priv *hpriv;
  650. void __iomem * const *iomap;
  651. int mmio_bar;
  652. int i, rc;
  653. if (!printed_version++)
  654. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  655. /* alloc host */
  656. host = ata_host_alloc_pinfo(&pdev->dev, ppi, NR_PORTS);
  657. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  658. if (!host || !hpriv)
  659. return -ENOMEM;
  660. host->private_data = hpriv;
  661. /* Acquire resources and fill host. Note that PCI and cardbus
  662. * use different BARs.
  663. */
  664. rc = pcim_enable_device(pdev);
  665. if (rc)
  666. return rc;
  667. if (pci_resource_flags(pdev, MMIO_BAR_PCI) & IORESOURCE_MEM)
  668. mmio_bar = MMIO_BAR_PCI;
  669. else
  670. mmio_bar = MMIO_BAR_CARDBUS;
  671. rc = pcim_iomap_regions(pdev, 1 << mmio_bar, DRV_NAME);
  672. if (rc)
  673. return rc;
  674. host->iomap = iomap = pcim_iomap_table(pdev);
  675. hpriv->mmio_base = iomap[mmio_bar];
  676. hpriv->cached_hctl = readw(hpriv->mmio_base + HOST_CTL);
  677. for (i = 0; i < NR_PORTS; i++) {
  678. struct ata_port *ap = host->ports[i];
  679. ata_port_pbar_desc(ap, mmio_bar, -1, "mmio");
  680. ata_port_pbar_desc(ap, mmio_bar, i * PORT_SIZE, "port");
  681. }
  682. /* Set dma_mask. This devices doesn't support 64bit addressing. */
  683. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  684. if (rc) {
  685. dev_printk(KERN_ERR, &pdev->dev,
  686. "32-bit DMA enable failed\n");
  687. return rc;
  688. }
  689. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  690. if (rc) {
  691. dev_printk(KERN_ERR, &pdev->dev,
  692. "32-bit consistent DMA enable failed\n");
  693. return rc;
  694. }
  695. /*
  696. * This controller is braindamaged. dma_boundary is 0xffff
  697. * like others but it will lock up the whole machine HARD if
  698. * 65536 byte PRD entry is fed. Reduce maximum segment size.
  699. */
  700. rc = pci_set_dma_max_seg_size(pdev, 65536 - 512);
  701. if (rc) {
  702. dev_printk(KERN_ERR, &pdev->dev,
  703. "failed to set the maximum segment size.\n");
  704. return rc;
  705. }
  706. rc = init_controller(hpriv->mmio_base, hpriv->cached_hctl);
  707. if (rc) {
  708. dev_printk(KERN_ERR, &pdev->dev,
  709. "failed to initialize controller\n");
  710. return rc;
  711. }
  712. pci_set_master(pdev);
  713. return ata_host_activate(host, pdev->irq, inic_interrupt, IRQF_SHARED,
  714. &inic_sht);
  715. }
  716. static const struct pci_device_id inic_pci_tbl[] = {
  717. { PCI_VDEVICE(INIT, 0x1622), },
  718. { },
  719. };
  720. static struct pci_driver inic_pci_driver = {
  721. .name = DRV_NAME,
  722. .id_table = inic_pci_tbl,
  723. #ifdef CONFIG_PM
  724. .suspend = ata_pci_device_suspend,
  725. .resume = inic_pci_device_resume,
  726. #endif
  727. .probe = inic_init_one,
  728. .remove = ata_pci_remove_one,
  729. };
  730. static int __init inic_init(void)
  731. {
  732. return pci_register_driver(&inic_pci_driver);
  733. }
  734. static void __exit inic_exit(void)
  735. {
  736. pci_unregister_driver(&inic_pci_driver);
  737. }
  738. MODULE_AUTHOR("Tejun Heo");
  739. MODULE_DESCRIPTION("low-level driver for Initio 162x SATA");
  740. MODULE_LICENSE("GPL v2");
  741. MODULE_DEVICE_TABLE(pci, inic_pci_tbl);
  742. MODULE_VERSION(DRV_VERSION);
  743. module_init(inic_init);
  744. module_exit(inic_exit);