sata_inic162x.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. IDMA_CPB_TBL_SIZE = 4 * 32,
  34. INIC_DMA_BOUNDARY = 0xffffff,
  35. HOST_ACTRL = 0x08,
  36. HOST_CTL = 0x7c,
  37. HOST_STAT = 0x7e,
  38. HOST_IRQ_STAT = 0xbc,
  39. HOST_IRQ_MASK = 0xbe,
  40. PORT_SIZE = 0x40,
  41. /* registers for ATA TF operation */
  42. PORT_TF_DATA = 0x00,
  43. PORT_TF_FEATURE = 0x01,
  44. PORT_TF_NSECT = 0x02,
  45. PORT_TF_LBAL = 0x03,
  46. PORT_TF_LBAM = 0x04,
  47. PORT_TF_LBAH = 0x05,
  48. PORT_TF_DEVICE = 0x06,
  49. PORT_TF_COMMAND = 0x07,
  50. PORT_TF_ALT_STAT = 0x08,
  51. PORT_IRQ_STAT = 0x09,
  52. PORT_IRQ_MASK = 0x0a,
  53. PORT_PRD_CTL = 0x0b,
  54. PORT_PRD_ADDR = 0x0c,
  55. PORT_PRD_XFERLEN = 0x10,
  56. PORT_CPB_CPBLAR = 0x18,
  57. PORT_CPB_PTQFIFO = 0x1c,
  58. /* IDMA register */
  59. PORT_IDMA_CTL = 0x14,
  60. PORT_IDMA_STAT = 0x16,
  61. PORT_RPQ_FIFO = 0x1e,
  62. PORT_RPQ_CNT = 0x1f,
  63. PORT_SCR = 0x20,
  64. /* HOST_CTL bits */
  65. HCTL_IRQOFF = (1 << 8), /* global IRQ off */
  66. HCTL_FTHD0 = (1 << 10), /* fifo threshold 0 */
  67. HCTL_FTHD1 = (1 << 11), /* fifo threshold 1*/
  68. HCTL_PWRDWN = (1 << 12), /* power down PHYs */
  69. HCTL_SOFTRST = (1 << 13), /* global reset (no phy reset) */
  70. HCTL_RPGSEL = (1 << 15), /* register page select */
  71. HCTL_KNOWN_BITS = HCTL_IRQOFF | HCTL_PWRDWN | HCTL_SOFTRST |
  72. HCTL_RPGSEL,
  73. /* HOST_IRQ_(STAT|MASK) bits */
  74. HIRQ_PORT0 = (1 << 0),
  75. HIRQ_PORT1 = (1 << 1),
  76. HIRQ_SOFT = (1 << 14),
  77. HIRQ_GLOBAL = (1 << 15), /* STAT only */
  78. /* PORT_IRQ_(STAT|MASK) bits */
  79. PIRQ_OFFLINE = (1 << 0), /* device unplugged */
  80. PIRQ_ONLINE = (1 << 1), /* device plugged */
  81. PIRQ_COMPLETE = (1 << 2), /* completion interrupt */
  82. PIRQ_FATAL = (1 << 3), /* fatal error */
  83. PIRQ_ATA = (1 << 4), /* ATA interrupt */
  84. PIRQ_REPLY = (1 << 5), /* reply FIFO not empty */
  85. PIRQ_PENDING = (1 << 7), /* port IRQ pending (STAT only) */
  86. PIRQ_ERR = PIRQ_OFFLINE | PIRQ_ONLINE | PIRQ_FATAL,
  87. PIRQ_MASK_DMA_READ = PIRQ_REPLY | PIRQ_ATA,
  88. PIRQ_MASK_OTHER = PIRQ_REPLY | PIRQ_COMPLETE,
  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];
  170. } __packed;
  171. struct inic_host_priv {
  172. u16 cached_hctl;
  173. };
  174. struct inic_port_priv {
  175. struct inic_pkt *pkt;
  176. dma_addr_t pkt_dma;
  177. u32 *cpb_tbl;
  178. dma_addr_t cpb_tbl_dma;
  179. u8 dfl_prdctl;
  180. u8 cached_prdctl;
  181. u8 cached_pirq_mask;
  182. };
  183. static struct scsi_host_template inic_sht = {
  184. ATA_BMDMA_SHT(DRV_NAME),
  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. return ap->host->iomap[MMIO_BAR] + ap->port_no * PORT_SIZE;
  195. }
  196. static void __inic_set_pirq_mask(struct ata_port *ap, u8 mask)
  197. {
  198. void __iomem *port_base = inic_port_base(ap);
  199. struct inic_port_priv *pp = ap->private_data;
  200. writeb(mask, port_base + PORT_IRQ_MASK);
  201. pp->cached_pirq_mask = mask;
  202. }
  203. static void inic_set_pirq_mask(struct ata_port *ap, u8 mask)
  204. {
  205. struct inic_port_priv *pp = ap->private_data;
  206. if (pp->cached_pirq_mask != mask)
  207. __inic_set_pirq_mask(ap, mask);
  208. }
  209. static void inic_reset_port(void __iomem *port_base)
  210. {
  211. void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
  212. u16 ctl;
  213. ctl = readw(idma_ctl);
  214. ctl &= ~(IDMA_CTL_RST_IDMA | IDMA_CTL_ATA_NIEN | IDMA_CTL_GO);
  215. /* mask IRQ and assert reset */
  216. writew(ctl | IDMA_CTL_RST_IDMA | IDMA_CTL_ATA_NIEN, idma_ctl);
  217. readw(idma_ctl); /* flush */
  218. /* give it some time */
  219. msleep(1);
  220. /* release reset */
  221. writew(ctl | IDMA_CTL_ATA_NIEN, idma_ctl);
  222. /* clear irq */
  223. writeb(0xff, port_base + PORT_IRQ_STAT);
  224. /* reenable ATA IRQ, turn off IDMA mode */
  225. writew(ctl, idma_ctl);
  226. }
  227. static int inic_scr_read(struct ata_port *ap, unsigned sc_reg, u32 *val)
  228. {
  229. void __iomem *scr_addr = ap->ioaddr.scr_addr;
  230. void __iomem *addr;
  231. if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
  232. return -EINVAL;
  233. addr = scr_addr + scr_map[sc_reg] * 4;
  234. *val = readl(scr_addr + scr_map[sc_reg] * 4);
  235. /* this controller has stuck DIAG.N, ignore it */
  236. if (sc_reg == SCR_ERROR)
  237. *val &= ~SERR_PHYRDY_CHG;
  238. return 0;
  239. }
  240. static int inic_scr_write(struct ata_port *ap, unsigned sc_reg, u32 val)
  241. {
  242. void __iomem *scr_addr = ap->ioaddr.scr_addr;
  243. if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
  244. return -EINVAL;
  245. writel(val, scr_addr + scr_map[sc_reg] * 4);
  246. return 0;
  247. }
  248. /*
  249. * In TF mode, inic162x is very similar to SFF device. TF registers
  250. * function the same. DMA engine behaves similary using the same PRD
  251. * format as BMDMA but different command register, interrupt and event
  252. * notification methods are used. The following inic_bmdma_*()
  253. * functions do the impedance matching.
  254. */
  255. static void inic_bmdma_setup(struct ata_queued_cmd *qc)
  256. {
  257. struct ata_port *ap = qc->ap;
  258. struct inic_port_priv *pp = ap->private_data;
  259. void __iomem *port_base = inic_port_base(ap);
  260. int rw = qc->tf.flags & ATA_TFLAG_WRITE;
  261. /* make sure device sees PRD table writes */
  262. wmb();
  263. /* load transfer length */
  264. writel(qc->nbytes, port_base + PORT_PRD_XFERLEN);
  265. /* turn on DMA and specify data direction */
  266. pp->cached_prdctl = pp->dfl_prdctl | PRD_CTL_DMAEN;
  267. if (!rw)
  268. pp->cached_prdctl |= PRD_CTL_WR;
  269. writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
  270. /* issue r/w command */
  271. ap->ops->sff_exec_command(ap, &qc->tf);
  272. }
  273. static void inic_bmdma_start(struct ata_queued_cmd *qc)
  274. {
  275. struct ata_port *ap = qc->ap;
  276. struct inic_port_priv *pp = ap->private_data;
  277. void __iomem *port_base = inic_port_base(ap);
  278. /* start host DMA transaction */
  279. pp->cached_prdctl |= PRD_CTL_START;
  280. writeb(pp->cached_prdctl, port_base + PORT_PRD_CTL);
  281. }
  282. static void inic_bmdma_stop(struct ata_queued_cmd *qc)
  283. {
  284. struct ata_port *ap = qc->ap;
  285. struct inic_port_priv *pp = ap->private_data;
  286. void __iomem *port_base = inic_port_base(ap);
  287. /* stop DMA engine */
  288. writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
  289. }
  290. static u8 inic_bmdma_status(struct ata_port *ap)
  291. {
  292. /* event is already verified by the interrupt handler */
  293. return ATA_DMA_INTR;
  294. }
  295. static void inic_stop_idma(struct ata_port *ap)
  296. {
  297. void __iomem *port_base = inic_port_base(ap);
  298. readb(port_base + PORT_RPQ_FIFO);
  299. readb(port_base + PORT_RPQ_CNT);
  300. writew(0, port_base + PORT_IDMA_CTL);
  301. }
  302. static void inic_host_err_intr(struct ata_port *ap, u8 irq_stat, u16 idma_stat)
  303. {
  304. struct ata_eh_info *ehi = &ap->link.eh_info;
  305. struct inic_port_priv *pp = ap->private_data;
  306. struct inic_cpb *cpb = &pp->pkt->cpb;
  307. bool freeze = false;
  308. ata_ehi_clear_desc(ehi);
  309. ata_ehi_push_desc(ehi, "irq_stat=0x%x idma_stat=0x%x",
  310. irq_stat, idma_stat);
  311. inic_stop_idma(ap);
  312. if (irq_stat & (PIRQ_OFFLINE | PIRQ_ONLINE)) {
  313. ata_ehi_push_desc(ehi, "hotplug");
  314. ata_ehi_hotplugged(ehi);
  315. freeze = true;
  316. }
  317. if (idma_stat & IDMA_STAT_PERR) {
  318. ata_ehi_push_desc(ehi, "PCI error");
  319. freeze = true;
  320. }
  321. if (idma_stat & IDMA_STAT_CPBERR) {
  322. ata_ehi_push_desc(ehi, "CPB error");
  323. if (cpb->resp_flags & CPB_RESP_IGNORED) {
  324. __ata_ehi_push_desc(ehi, " ignored");
  325. ehi->err_mask |= AC_ERR_INVALID;
  326. freeze = true;
  327. }
  328. if (cpb->resp_flags & CPB_RESP_ATA_ERR)
  329. ehi->err_mask |= AC_ERR_DEV;
  330. if (cpb->resp_flags & CPB_RESP_SPURIOUS) {
  331. __ata_ehi_push_desc(ehi, " spurious-intr");
  332. ehi->err_mask |= AC_ERR_HSM;
  333. freeze = true;
  334. }
  335. if (cpb->resp_flags &
  336. (CPB_RESP_UNDERFLOW | CPB_RESP_OVERFLOW)) {
  337. __ata_ehi_push_desc(ehi, " data-over/underflow");
  338. ehi->err_mask |= AC_ERR_HSM;
  339. freeze = true;
  340. }
  341. }
  342. if (freeze)
  343. ata_port_freeze(ap);
  344. else
  345. ata_port_abort(ap);
  346. }
  347. static void inic_host_intr(struct ata_port *ap)
  348. {
  349. void __iomem *port_base = inic_port_base(ap);
  350. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
  351. u8 irq_stat;
  352. u16 idma_stat;
  353. /* read and clear IRQ status */
  354. irq_stat = readb(port_base + PORT_IRQ_STAT);
  355. writeb(irq_stat, port_base + PORT_IRQ_STAT);
  356. idma_stat = readw(port_base + PORT_IDMA_STAT);
  357. if (unlikely((irq_stat & PIRQ_ERR) || (idma_stat & IDMA_STAT_ERR)))
  358. inic_host_err_intr(ap, irq_stat, idma_stat);
  359. if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
  360. ap->ops->sff_check_status(ap); /* clear ATA interrupt */
  361. goto spurious;
  362. }
  363. if (qc->tf.protocol == ATA_PROT_DMA) {
  364. if (likely(idma_stat & IDMA_STAT_DONE)) {
  365. inic_stop_idma(ap);
  366. /* Depending on circumstances, device error
  367. * isn't reported by IDMA, check it explicitly.
  368. */
  369. if (unlikely(readb(port_base + PORT_TF_COMMAND) &
  370. (ATA_DF | ATA_ERR)))
  371. qc->err_mask |= AC_ERR_DEV;
  372. ata_qc_complete(qc);
  373. return;
  374. }
  375. } else {
  376. if (likely(ata_sff_host_intr(ap, qc)))
  377. return;
  378. }
  379. spurious:
  380. ap->ops->sff_check_status(ap); /* clear ATA interrupt */
  381. }
  382. static irqreturn_t inic_interrupt(int irq, void *dev_instance)
  383. {
  384. struct ata_host *host = dev_instance;
  385. void __iomem *mmio_base = host->iomap[MMIO_BAR];
  386. u16 host_irq_stat;
  387. int i, handled = 0;;
  388. host_irq_stat = readw(mmio_base + HOST_IRQ_STAT);
  389. if (unlikely(!(host_irq_stat & HIRQ_GLOBAL)))
  390. goto out;
  391. spin_lock(&host->lock);
  392. for (i = 0; i < NR_PORTS; i++) {
  393. struct ata_port *ap = host->ports[i];
  394. if (!(host_irq_stat & (HIRQ_PORT0 << i)))
  395. continue;
  396. if (likely(ap && !(ap->flags & ATA_FLAG_DISABLED))) {
  397. inic_host_intr(ap);
  398. handled++;
  399. } else {
  400. if (ata_ratelimit())
  401. dev_printk(KERN_ERR, host->dev, "interrupt "
  402. "from disabled port %d (0x%x)\n",
  403. i, host_irq_stat);
  404. }
  405. }
  406. spin_unlock(&host->lock);
  407. out:
  408. return IRQ_RETVAL(handled);
  409. }
  410. static void inic_fill_sg(struct inic_prd *prd, struct ata_queued_cmd *qc)
  411. {
  412. struct scatterlist *sg;
  413. unsigned int si;
  414. u8 flags = PRD_DMA;
  415. if (qc->tf.flags & ATA_TFLAG_WRITE)
  416. flags |= PRD_WRITE;
  417. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  418. prd->mad = cpu_to_le32(sg_dma_address(sg));
  419. prd->len = cpu_to_le16(sg_dma_len(sg));
  420. prd->flags = flags;
  421. prd++;
  422. }
  423. WARN_ON(!si);
  424. prd[-1].flags |= PRD_END;
  425. }
  426. static void inic_qc_prep(struct ata_queued_cmd *qc)
  427. {
  428. struct inic_port_priv *pp = qc->ap->private_data;
  429. struct inic_pkt *pkt = pp->pkt;
  430. struct inic_cpb *cpb = &pkt->cpb;
  431. struct inic_prd *prd = pkt->prd;
  432. VPRINTK("ENTER\n");
  433. if (qc->tf.protocol != ATA_PROT_DMA)
  434. return;
  435. /* prepare packet, based on initio driver */
  436. memset(pkt, 0, sizeof(struct inic_pkt));
  437. cpb->ctl_flags = CPB_CTL_VALID | CPB_CTL_IEN | CPB_CTL_DATA;
  438. cpb->len = cpu_to_le32(qc->nbytes);
  439. cpb->prd = cpu_to_le32(pp->pkt_dma + offsetof(struct inic_pkt, prd));
  440. cpb->device = qc->tf.device;
  441. cpb->feature = qc->tf.feature;
  442. cpb->nsect = qc->tf.nsect;
  443. cpb->lbal = qc->tf.lbal;
  444. cpb->lbam = qc->tf.lbam;
  445. cpb->lbah = qc->tf.lbah;
  446. if (qc->tf.flags & ATA_TFLAG_LBA48) {
  447. cpb->hob_feature = qc->tf.hob_feature;
  448. cpb->hob_nsect = qc->tf.hob_nsect;
  449. cpb->hob_lbal = qc->tf.hob_lbal;
  450. cpb->hob_lbam = qc->tf.hob_lbam;
  451. cpb->hob_lbah = qc->tf.hob_lbah;
  452. }
  453. cpb->command = qc->tf.command;
  454. /* don't load ctl - dunno why. it's like that in the initio driver */
  455. /* setup sg table */
  456. inic_fill_sg(prd, qc);
  457. pp->cpb_tbl[0] = pp->pkt_dma;
  458. }
  459. static unsigned int inic_qc_issue(struct ata_queued_cmd *qc)
  460. {
  461. struct ata_port *ap = qc->ap;
  462. void __iomem *port_base = inic_port_base(ap);
  463. if (qc->tf.protocol == ATA_PROT_DMA) {
  464. /* fire up the ADMA engine */
  465. writew(HCTL_FTHD0, port_base + HOST_CTL);
  466. writew(IDMA_CTL_GO, port_base + PORT_IDMA_CTL);
  467. writeb(0, port_base + PORT_CPB_PTQFIFO);
  468. return 0;
  469. }
  470. /* Issuing a command to yet uninitialized port locks up the
  471. * controller. Most of the time, this happens for the first
  472. * command after reset which are ATA and ATAPI IDENTIFYs.
  473. * Fast fail if stat is 0x7f or 0xff for those commands.
  474. */
  475. if (unlikely(qc->tf.command == ATA_CMD_ID_ATA ||
  476. qc->tf.command == ATA_CMD_ID_ATAPI)) {
  477. u8 stat = ap->ops->sff_check_status(ap);
  478. if (stat == 0x7f || stat == 0xff)
  479. return AC_ERR_HSM;
  480. }
  481. return ata_sff_qc_issue(qc);
  482. }
  483. static void inic_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  484. {
  485. void __iomem *port_base = inic_port_base(ap);
  486. tf->feature = readb(port_base + PORT_TF_FEATURE);
  487. tf->nsect = readb(port_base + PORT_TF_NSECT);
  488. tf->lbal = readb(port_base + PORT_TF_LBAL);
  489. tf->lbam = readb(port_base + PORT_TF_LBAM);
  490. tf->lbah = readb(port_base + PORT_TF_LBAH);
  491. tf->device = readb(port_base + PORT_TF_DEVICE);
  492. tf->command = readb(port_base + PORT_TF_COMMAND);
  493. }
  494. static bool inic_qc_fill_rtf(struct ata_queued_cmd *qc)
  495. {
  496. struct ata_taskfile *rtf = &qc->result_tf;
  497. struct ata_taskfile tf;
  498. /* FIXME: Except for status and error, result TF access
  499. * doesn't work. I tried reading from BAR0/2, CPB and BAR5.
  500. * None works regardless of which command interface is used.
  501. * For now return true iff status indicates device error.
  502. * This means that we're reporting bogus sector for RW
  503. * failures. Eeekk....
  504. */
  505. inic_tf_read(qc->ap, &tf);
  506. if (!(tf.command & ATA_ERR))
  507. return false;
  508. rtf->command = tf.command;
  509. rtf->feature = tf.feature;
  510. return true;
  511. }
  512. static void inic_freeze(struct ata_port *ap)
  513. {
  514. void __iomem *port_base = inic_port_base(ap);
  515. __inic_set_pirq_mask(ap, PIRQ_MASK_FREEZE);
  516. ap->ops->sff_check_status(ap);
  517. writeb(0xff, port_base + PORT_IRQ_STAT);
  518. }
  519. static void inic_thaw(struct ata_port *ap)
  520. {
  521. void __iomem *port_base = inic_port_base(ap);
  522. ap->ops->sff_check_status(ap);
  523. writeb(0xff, port_base + PORT_IRQ_STAT);
  524. __inic_set_pirq_mask(ap, PIRQ_MASK_OTHER);
  525. }
  526. static int inic_check_ready(struct ata_link *link)
  527. {
  528. void __iomem *port_base = inic_port_base(link->ap);
  529. return ata_check_ready(readb(port_base + PORT_TF_COMMAND));
  530. }
  531. /*
  532. * SRST and SControl hardreset don't give valid signature on this
  533. * controller. Only controller specific hardreset mechanism works.
  534. */
  535. static int inic_hardreset(struct ata_link *link, unsigned int *class,
  536. unsigned long deadline)
  537. {
  538. struct ata_port *ap = link->ap;
  539. void __iomem *port_base = inic_port_base(ap);
  540. void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
  541. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  542. u16 val;
  543. int rc;
  544. /* hammer it into sane state */
  545. inic_reset_port(port_base);
  546. val = readw(idma_ctl);
  547. writew(val | IDMA_CTL_RST_ATA, idma_ctl);
  548. readw(idma_ctl); /* flush */
  549. msleep(1);
  550. writew(val & ~IDMA_CTL_RST_ATA, idma_ctl);
  551. rc = sata_link_resume(link, timing, deadline);
  552. if (rc) {
  553. ata_link_printk(link, KERN_WARNING, "failed to resume "
  554. "link after reset (errno=%d)\n", rc);
  555. return rc;
  556. }
  557. *class = ATA_DEV_NONE;
  558. if (ata_link_online(link)) {
  559. struct ata_taskfile tf;
  560. /* wait for link to become ready */
  561. rc = ata_wait_after_reset(link, deadline, inic_check_ready);
  562. /* link occupied, -ENODEV too is an error */
  563. if (rc) {
  564. ata_link_printk(link, KERN_WARNING, "device not ready "
  565. "after hardreset (errno=%d)\n", rc);
  566. return rc;
  567. }
  568. inic_tf_read(ap, &tf);
  569. *class = ata_dev_classify(&tf);
  570. }
  571. return 0;
  572. }
  573. static void inic_error_handler(struct ata_port *ap)
  574. {
  575. void __iomem *port_base = inic_port_base(ap);
  576. struct inic_port_priv *pp = ap->private_data;
  577. unsigned long flags;
  578. /* reset PIO HSM and stop DMA engine */
  579. inic_reset_port(port_base);
  580. spin_lock_irqsave(ap->lock, flags);
  581. ap->hsm_task_state = HSM_ST_IDLE;
  582. writeb(pp->dfl_prdctl, port_base + PORT_PRD_CTL);
  583. spin_unlock_irqrestore(ap->lock, flags);
  584. /* PIO and DMA engines have been stopped, perform recovery */
  585. ata_std_error_handler(ap);
  586. }
  587. static void inic_post_internal_cmd(struct ata_queued_cmd *qc)
  588. {
  589. /* make DMA engine forget about the failed command */
  590. if (qc->flags & ATA_QCFLAG_FAILED)
  591. inic_reset_port(inic_port_base(qc->ap));
  592. }
  593. static void inic_dev_config(struct ata_device *dev)
  594. {
  595. /* inic can only handle upto LBA28 max sectors */
  596. if (dev->max_sectors > ATA_MAX_SECTORS)
  597. dev->max_sectors = ATA_MAX_SECTORS;
  598. if (dev->n_sectors >= 1 << 28) {
  599. ata_dev_printk(dev, KERN_ERR,
  600. "ERROR: This driver doesn't support LBA48 yet and may cause\n"
  601. " data corruption on such devices. Disabling.\n");
  602. ata_dev_disable(dev);
  603. }
  604. }
  605. static void init_port(struct ata_port *ap)
  606. {
  607. void __iomem *port_base = inic_port_base(ap);
  608. struct inic_port_priv *pp = ap->private_data;
  609. /* clear packet and CPB table */
  610. memset(pp->pkt, 0, sizeof(struct inic_pkt));
  611. memset(pp->cpb_tbl, 0, IDMA_CPB_TBL_SIZE);
  612. /* setup PRD and CPB lookup table addresses */
  613. writel(ap->prd_dma, port_base + PORT_PRD_ADDR);
  614. writel(pp->cpb_tbl_dma, port_base + PORT_CPB_CPBLAR);
  615. }
  616. static int inic_port_resume(struct ata_port *ap)
  617. {
  618. init_port(ap);
  619. return 0;
  620. }
  621. static int inic_port_start(struct ata_port *ap)
  622. {
  623. void __iomem *port_base = inic_port_base(ap);
  624. struct device *dev = ap->host->dev;
  625. struct inic_port_priv *pp;
  626. u8 tmp;
  627. int rc;
  628. /* alloc and initialize private data */
  629. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  630. if (!pp)
  631. return -ENOMEM;
  632. ap->private_data = pp;
  633. /* default PRD_CTL value, DMAEN, WR and START off */
  634. tmp = readb(port_base + PORT_PRD_CTL);
  635. tmp &= ~(PRD_CTL_DMAEN | PRD_CTL_WR | PRD_CTL_START);
  636. pp->dfl_prdctl = tmp;
  637. /* Alloc resources */
  638. rc = ata_port_start(ap);
  639. if (rc)
  640. return rc;
  641. pp->pkt = dmam_alloc_coherent(dev, sizeof(struct inic_pkt),
  642. &pp->pkt_dma, GFP_KERNEL);
  643. if (!pp->pkt)
  644. return -ENOMEM;
  645. pp->cpb_tbl = dmam_alloc_coherent(dev, IDMA_CPB_TBL_SIZE,
  646. &pp->cpb_tbl_dma, GFP_KERNEL);
  647. if (!pp->cpb_tbl)
  648. return -ENOMEM;
  649. init_port(ap);
  650. return 0;
  651. }
  652. static struct ata_port_operations inic_port_ops = {
  653. .inherits = &ata_sff_port_ops,
  654. .bmdma_setup = inic_bmdma_setup,
  655. .bmdma_start = inic_bmdma_start,
  656. .bmdma_stop = inic_bmdma_stop,
  657. .bmdma_status = inic_bmdma_status,
  658. .qc_prep = inic_qc_prep,
  659. .qc_issue = inic_qc_issue,
  660. .qc_fill_rtf = inic_qc_fill_rtf,
  661. .freeze = inic_freeze,
  662. .thaw = inic_thaw,
  663. .softreset = ATA_OP_NULL, /* softreset is broken */
  664. .hardreset = inic_hardreset,
  665. .error_handler = inic_error_handler,
  666. .post_internal_cmd = inic_post_internal_cmd,
  667. .dev_config = inic_dev_config,
  668. .scr_read = inic_scr_read,
  669. .scr_write = inic_scr_write,
  670. .port_resume = inic_port_resume,
  671. .port_start = inic_port_start,
  672. };
  673. static struct ata_port_info inic_port_info = {
  674. /* For some reason, ATAPI_PROT_PIO is broken on this
  675. * controller, and no, PIO_POLLING does't fix it. It somehow
  676. * manages to report the wrong ireason and ignoring ireason
  677. * results in machine lock up. Tell libata to always prefer
  678. * DMA.
  679. */
  680. .flags = ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
  681. .pio_mask = 0x1f, /* pio0-4 */
  682. .mwdma_mask = 0x07, /* mwdma0-2 */
  683. .udma_mask = ATA_UDMA6,
  684. .port_ops = &inic_port_ops
  685. };
  686. static int init_controller(void __iomem *mmio_base, u16 hctl)
  687. {
  688. int i;
  689. u16 val;
  690. hctl &= ~HCTL_KNOWN_BITS;
  691. /* Soft reset whole controller. Spec says reset duration is 3
  692. * PCI clocks, be generous and give it 10ms.
  693. */
  694. writew(hctl | HCTL_SOFTRST, mmio_base + HOST_CTL);
  695. readw(mmio_base + HOST_CTL); /* flush */
  696. for (i = 0; i < 10; i++) {
  697. msleep(1);
  698. val = readw(mmio_base + HOST_CTL);
  699. if (!(val & HCTL_SOFTRST))
  700. break;
  701. }
  702. if (val & HCTL_SOFTRST)
  703. return -EIO;
  704. /* mask all interrupts and reset ports */
  705. for (i = 0; i < NR_PORTS; i++) {
  706. void __iomem *port_base = mmio_base + i * PORT_SIZE;
  707. writeb(0xff, port_base + PORT_IRQ_MASK);
  708. inic_reset_port(port_base);
  709. }
  710. /* port IRQ is masked now, unmask global IRQ */
  711. writew(hctl & ~HCTL_IRQOFF, mmio_base + HOST_CTL);
  712. val = readw(mmio_base + HOST_IRQ_MASK);
  713. val &= ~(HIRQ_PORT0 | HIRQ_PORT1);
  714. writew(val, mmio_base + HOST_IRQ_MASK);
  715. return 0;
  716. }
  717. #ifdef CONFIG_PM
  718. static int inic_pci_device_resume(struct pci_dev *pdev)
  719. {
  720. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  721. struct inic_host_priv *hpriv = host->private_data;
  722. void __iomem *mmio_base = host->iomap[MMIO_BAR];
  723. int rc;
  724. rc = ata_pci_device_do_resume(pdev);
  725. if (rc)
  726. return rc;
  727. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  728. rc = init_controller(mmio_base, hpriv->cached_hctl);
  729. if (rc)
  730. return rc;
  731. }
  732. ata_host_resume(host);
  733. return 0;
  734. }
  735. #endif
  736. static int inic_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  737. {
  738. static int printed_version;
  739. const struct ata_port_info *ppi[] = { &inic_port_info, NULL };
  740. struct ata_host *host;
  741. struct inic_host_priv *hpriv;
  742. void __iomem * const *iomap;
  743. int i, rc;
  744. if (!printed_version++)
  745. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  746. /* alloc host */
  747. host = ata_host_alloc_pinfo(&pdev->dev, ppi, NR_PORTS);
  748. hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
  749. if (!host || !hpriv)
  750. return -ENOMEM;
  751. host->private_data = hpriv;
  752. /* acquire resources and fill host */
  753. rc = pcim_enable_device(pdev);
  754. if (rc)
  755. return rc;
  756. rc = pcim_iomap_regions(pdev, 0x3f, DRV_NAME);
  757. if (rc)
  758. return rc;
  759. host->iomap = iomap = pcim_iomap_table(pdev);
  760. for (i = 0; i < NR_PORTS; i++) {
  761. struct ata_port *ap = host->ports[i];
  762. struct ata_ioports *port = &ap->ioaddr;
  763. unsigned int offset = i * PORT_SIZE;
  764. port->cmd_addr = iomap[2 * i];
  765. port->altstatus_addr =
  766. port->ctl_addr = (void __iomem *)
  767. ((unsigned long)iomap[2 * i + 1] | ATA_PCI_CTL_OFS);
  768. port->scr_addr = iomap[MMIO_BAR] + offset + PORT_SCR;
  769. ata_sff_std_ports(port);
  770. ata_port_pbar_desc(ap, MMIO_BAR, -1, "mmio");
  771. ata_port_pbar_desc(ap, MMIO_BAR, offset, "port");
  772. ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
  773. (unsigned long long)pci_resource_start(pdev, 2 * i),
  774. (unsigned long long)pci_resource_start(pdev, (2 * i + 1)) |
  775. ATA_PCI_CTL_OFS);
  776. }
  777. hpriv->cached_hctl = readw(iomap[MMIO_BAR] + HOST_CTL);
  778. /* Set dma_mask. This devices doesn't support 64bit addressing. */
  779. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  780. if (rc) {
  781. dev_printk(KERN_ERR, &pdev->dev,
  782. "32-bit DMA enable failed\n");
  783. return rc;
  784. }
  785. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  786. if (rc) {
  787. dev_printk(KERN_ERR, &pdev->dev,
  788. "32-bit consistent DMA enable failed\n");
  789. return rc;
  790. }
  791. /*
  792. * This controller is braindamaged. dma_boundary is 0xffff
  793. * like others but it will lock up the whole machine HARD if
  794. * 65536 byte PRD entry is fed. Reduce maximum segment size.
  795. */
  796. rc = pci_set_dma_max_seg_size(pdev, 65536 - 512);
  797. if (rc) {
  798. dev_printk(KERN_ERR, &pdev->dev,
  799. "failed to set the maximum segment size.\n");
  800. return rc;
  801. }
  802. rc = init_controller(iomap[MMIO_BAR], hpriv->cached_hctl);
  803. if (rc) {
  804. dev_printk(KERN_ERR, &pdev->dev,
  805. "failed to initialize controller\n");
  806. return rc;
  807. }
  808. pci_set_master(pdev);
  809. return ata_host_activate(host, pdev->irq, inic_interrupt, IRQF_SHARED,
  810. &inic_sht);
  811. }
  812. static const struct pci_device_id inic_pci_tbl[] = {
  813. { PCI_VDEVICE(INIT, 0x1622), },
  814. { },
  815. };
  816. static struct pci_driver inic_pci_driver = {
  817. .name = DRV_NAME,
  818. .id_table = inic_pci_tbl,
  819. #ifdef CONFIG_PM
  820. .suspend = ata_pci_device_suspend,
  821. .resume = inic_pci_device_resume,
  822. #endif
  823. .probe = inic_init_one,
  824. .remove = ata_pci_remove_one,
  825. };
  826. static int __init inic_init(void)
  827. {
  828. return pci_register_driver(&inic_pci_driver);
  829. }
  830. static void __exit inic_exit(void)
  831. {
  832. pci_unregister_driver(&inic_pci_driver);
  833. }
  834. MODULE_AUTHOR("Tejun Heo");
  835. MODULE_DESCRIPTION("low-level driver for Initio 162x SATA");
  836. MODULE_LICENSE("GPL v2");
  837. MODULE_DEVICE_TABLE(pci, inic_pci_tbl);
  838. MODULE_VERSION(DRV_VERSION);
  839. module_init(inic_init);
  840. module_exit(inic_exit);