sata_promise.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271
  1. /*
  2. * sata_promise.c - Promise SATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Mikael Pettersson <mikpe@it.uu.se>
  6. * Please ALWAYS copy linux-ide@vger.kernel.org
  7. * on emails.
  8. *
  9. * Copyright 2003-2004 Red Hat, Inc.
  10. *
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; see the file COPYING. If not, write to
  24. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware information only available under NDA.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/gfp.h>
  36. #include <linux/pci.h>
  37. #include <linux/init.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/delay.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/device.h>
  42. #include <scsi/scsi.h>
  43. #include <scsi/scsi_host.h>
  44. #include <scsi/scsi_cmnd.h>
  45. #include <linux/libata.h>
  46. #include "sata_promise.h"
  47. #define DRV_NAME "sata_promise"
  48. #define DRV_VERSION "2.12"
  49. enum {
  50. PDC_MAX_PORTS = 4,
  51. PDC_MMIO_BAR = 3,
  52. PDC_MAX_PRD = LIBATA_MAX_PRD - 1, /* -1 for ASIC PRD bug workaround */
  53. /* host register offsets (from host->iomap[PDC_MMIO_BAR]) */
  54. PDC_INT_SEQMASK = 0x40, /* Mask of asserted SEQ INTs */
  55. PDC_FLASH_CTL = 0x44, /* Flash control register */
  56. PDC_PCI_CTL = 0x48, /* PCI control/status reg */
  57. PDC_SATA_PLUG_CSR = 0x6C, /* SATA Plug control/status reg */
  58. PDC2_SATA_PLUG_CSR = 0x60, /* SATAII Plug control/status reg */
  59. PDC_TBG_MODE = 0x41C, /* TBG mode (not SATAII) */
  60. PDC_SLEW_CTL = 0x470, /* slew rate control reg (not SATAII) */
  61. /* per-port ATA register offsets (from ap->ioaddr.cmd_addr) */
  62. PDC_FEATURE = 0x04, /* Feature/Error reg (per port) */
  63. PDC_SECTOR_COUNT = 0x08, /* Sector count reg (per port) */
  64. PDC_SECTOR_NUMBER = 0x0C, /* Sector number reg (per port) */
  65. PDC_CYLINDER_LOW = 0x10, /* Cylinder low reg (per port) */
  66. PDC_CYLINDER_HIGH = 0x14, /* Cylinder high reg (per port) */
  67. PDC_DEVICE = 0x18, /* Device/Head reg (per port) */
  68. PDC_COMMAND = 0x1C, /* Command/status reg (per port) */
  69. PDC_ALTSTATUS = 0x38, /* Alternate-status/device-control reg (per port) */
  70. PDC_PKT_SUBMIT = 0x40, /* Command packet pointer addr */
  71. PDC_GLOBAL_CTL = 0x48, /* Global control/status (per port) */
  72. PDC_CTLSTAT = 0x60, /* IDE control and status (per port) */
  73. /* per-port SATA register offsets (from ap->ioaddr.scr_addr) */
  74. PDC_SATA_ERROR = 0x04,
  75. PDC_PHYMODE4 = 0x14,
  76. PDC_LINK_LAYER_ERRORS = 0x6C,
  77. PDC_FPDMA_CTLSTAT = 0xD8,
  78. PDC_INTERNAL_DEBUG_1 = 0xF8, /* also used for PATA */
  79. PDC_INTERNAL_DEBUG_2 = 0xFC, /* also used for PATA */
  80. /* PDC_FPDMA_CTLSTAT bit definitions */
  81. PDC_FPDMA_CTLSTAT_RESET = 1 << 3,
  82. PDC_FPDMA_CTLSTAT_DMASETUP_INT_FLAG = 1 << 10,
  83. PDC_FPDMA_CTLSTAT_SETDB_INT_FLAG = 1 << 11,
  84. /* PDC_GLOBAL_CTL bit definitions */
  85. PDC_PH_ERR = (1 << 8), /* PCI error while loading packet */
  86. PDC_SH_ERR = (1 << 9), /* PCI error while loading S/G table */
  87. PDC_DH_ERR = (1 << 10), /* PCI error while loading data */
  88. PDC2_HTO_ERR = (1 << 12), /* host bus timeout */
  89. PDC2_ATA_HBA_ERR = (1 << 13), /* error during SATA DATA FIS transmission */
  90. PDC2_ATA_DMA_CNT_ERR = (1 << 14), /* DMA DATA FIS size differs from S/G count */
  91. PDC_OVERRUN_ERR = (1 << 19), /* S/G byte count larger than HD requires */
  92. PDC_UNDERRUN_ERR = (1 << 20), /* S/G byte count less than HD requires */
  93. PDC_DRIVE_ERR = (1 << 21), /* drive error */
  94. PDC_PCI_SYS_ERR = (1 << 22), /* PCI system error */
  95. PDC1_PCI_PARITY_ERR = (1 << 23), /* PCI parity error (from SATA150 driver) */
  96. PDC1_ERR_MASK = PDC1_PCI_PARITY_ERR,
  97. PDC2_ERR_MASK = PDC2_HTO_ERR | PDC2_ATA_HBA_ERR |
  98. PDC2_ATA_DMA_CNT_ERR,
  99. PDC_ERR_MASK = PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR |
  100. PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR |
  101. PDC_DRIVE_ERR | PDC_PCI_SYS_ERR |
  102. PDC1_ERR_MASK | PDC2_ERR_MASK,
  103. board_2037x = 0, /* FastTrak S150 TX2plus */
  104. board_2037x_pata = 1, /* FastTrak S150 TX2plus PATA port */
  105. board_20319 = 2, /* FastTrak S150 TX4 */
  106. board_20619 = 3, /* FastTrak TX4000 */
  107. board_2057x = 4, /* SATAII150 Tx2plus */
  108. board_2057x_pata = 5, /* SATAII150 Tx2plus PATA port */
  109. board_40518 = 6, /* SATAII150 Tx4 */
  110. PDC_HAS_PATA = (1 << 1), /* PDC20375/20575 has PATA */
  111. /* Sequence counter control registers bit definitions */
  112. PDC_SEQCNTRL_INT_MASK = (1 << 5), /* Sequence Interrupt Mask */
  113. /* Feature register values */
  114. PDC_FEATURE_ATAPI_PIO = 0x00, /* ATAPI data xfer by PIO */
  115. PDC_FEATURE_ATAPI_DMA = 0x01, /* ATAPI data xfer by DMA */
  116. /* Device/Head register values */
  117. PDC_DEVICE_SATA = 0xE0, /* Device/Head value for SATA devices */
  118. /* PDC_CTLSTAT bit definitions */
  119. PDC_DMA_ENABLE = (1 << 7),
  120. PDC_IRQ_DISABLE = (1 << 10),
  121. PDC_RESET = (1 << 11), /* HDMA reset */
  122. PDC_COMMON_FLAGS = ATA_FLAG_NO_LEGACY |
  123. ATA_FLAG_MMIO |
  124. ATA_FLAG_PIO_POLLING,
  125. /* ap->flags bits */
  126. PDC_FLAG_GEN_II = (1 << 24),
  127. PDC_FLAG_SATA_PATA = (1 << 25), /* supports SATA + PATA */
  128. PDC_FLAG_4_PORTS = (1 << 26), /* 4 ports */
  129. };
  130. struct pdc_port_priv {
  131. u8 *pkt;
  132. dma_addr_t pkt_dma;
  133. };
  134. static int pdc_sata_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
  135. static int pdc_sata_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
  136. static int pdc_ata_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
  137. static int pdc_common_port_start(struct ata_port *ap);
  138. static int pdc_sata_port_start(struct ata_port *ap);
  139. static void pdc_qc_prep(struct ata_queued_cmd *qc);
  140. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  141. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  142. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc);
  143. static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc);
  144. static void pdc_irq_clear(struct ata_port *ap);
  145. static unsigned int pdc_qc_issue(struct ata_queued_cmd *qc);
  146. static void pdc_freeze(struct ata_port *ap);
  147. static void pdc_sata_freeze(struct ata_port *ap);
  148. static void pdc_thaw(struct ata_port *ap);
  149. static void pdc_sata_thaw(struct ata_port *ap);
  150. static int pdc_pata_softreset(struct ata_link *link, unsigned int *class,
  151. unsigned long deadline);
  152. static int pdc_sata_hardreset(struct ata_link *link, unsigned int *class,
  153. unsigned long deadline);
  154. static void pdc_error_handler(struct ata_port *ap);
  155. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc);
  156. static int pdc_pata_cable_detect(struct ata_port *ap);
  157. static int pdc_sata_cable_detect(struct ata_port *ap);
  158. static struct scsi_host_template pdc_ata_sht = {
  159. ATA_BASE_SHT(DRV_NAME),
  160. .sg_tablesize = PDC_MAX_PRD,
  161. .dma_boundary = ATA_DMA_BOUNDARY,
  162. };
  163. static const struct ata_port_operations pdc_common_ops = {
  164. .inherits = &ata_sff_port_ops,
  165. .sff_tf_load = pdc_tf_load_mmio,
  166. .sff_exec_command = pdc_exec_command_mmio,
  167. .check_atapi_dma = pdc_check_atapi_dma,
  168. .qc_prep = pdc_qc_prep,
  169. .qc_issue = pdc_qc_issue,
  170. .sff_irq_clear = pdc_irq_clear,
  171. .lost_interrupt = ATA_OP_NULL,
  172. .post_internal_cmd = pdc_post_internal_cmd,
  173. .error_handler = pdc_error_handler,
  174. };
  175. static struct ata_port_operations pdc_sata_ops = {
  176. .inherits = &pdc_common_ops,
  177. .cable_detect = pdc_sata_cable_detect,
  178. .freeze = pdc_sata_freeze,
  179. .thaw = pdc_sata_thaw,
  180. .scr_read = pdc_sata_scr_read,
  181. .scr_write = pdc_sata_scr_write,
  182. .port_start = pdc_sata_port_start,
  183. .hardreset = pdc_sata_hardreset,
  184. };
  185. /* First-generation chips need a more restrictive ->check_atapi_dma op,
  186. and ->freeze/thaw that ignore the hotplug controls. */
  187. static struct ata_port_operations pdc_old_sata_ops = {
  188. .inherits = &pdc_sata_ops,
  189. .freeze = pdc_freeze,
  190. .thaw = pdc_thaw,
  191. .check_atapi_dma = pdc_old_sata_check_atapi_dma,
  192. };
  193. static struct ata_port_operations pdc_pata_ops = {
  194. .inherits = &pdc_common_ops,
  195. .cable_detect = pdc_pata_cable_detect,
  196. .freeze = pdc_freeze,
  197. .thaw = pdc_thaw,
  198. .port_start = pdc_common_port_start,
  199. .softreset = pdc_pata_softreset,
  200. };
  201. static const struct ata_port_info pdc_port_info[] = {
  202. [board_2037x] =
  203. {
  204. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  205. PDC_FLAG_SATA_PATA,
  206. .pio_mask = ATA_PIO4,
  207. .mwdma_mask = ATA_MWDMA2,
  208. .udma_mask = ATA_UDMA6,
  209. .port_ops = &pdc_old_sata_ops,
  210. },
  211. [board_2037x_pata] =
  212. {
  213. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS,
  214. .pio_mask = ATA_PIO4,
  215. .mwdma_mask = ATA_MWDMA2,
  216. .udma_mask = ATA_UDMA6,
  217. .port_ops = &pdc_pata_ops,
  218. },
  219. [board_20319] =
  220. {
  221. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  222. PDC_FLAG_4_PORTS,
  223. .pio_mask = ATA_PIO4,
  224. .mwdma_mask = ATA_MWDMA2,
  225. .udma_mask = ATA_UDMA6,
  226. .port_ops = &pdc_old_sata_ops,
  227. },
  228. [board_20619] =
  229. {
  230. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS |
  231. PDC_FLAG_4_PORTS,
  232. .pio_mask = ATA_PIO4,
  233. .mwdma_mask = ATA_MWDMA2,
  234. .udma_mask = ATA_UDMA6,
  235. .port_ops = &pdc_pata_ops,
  236. },
  237. [board_2057x] =
  238. {
  239. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  240. PDC_FLAG_GEN_II | PDC_FLAG_SATA_PATA,
  241. .pio_mask = ATA_PIO4,
  242. .mwdma_mask = ATA_MWDMA2,
  243. .udma_mask = ATA_UDMA6,
  244. .port_ops = &pdc_sata_ops,
  245. },
  246. [board_2057x_pata] =
  247. {
  248. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS |
  249. PDC_FLAG_GEN_II,
  250. .pio_mask = ATA_PIO4,
  251. .mwdma_mask = ATA_MWDMA2,
  252. .udma_mask = ATA_UDMA6,
  253. .port_ops = &pdc_pata_ops,
  254. },
  255. [board_40518] =
  256. {
  257. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  258. PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS,
  259. .pio_mask = ATA_PIO4,
  260. .mwdma_mask = ATA_MWDMA2,
  261. .udma_mask = ATA_UDMA6,
  262. .port_ops = &pdc_sata_ops,
  263. },
  264. };
  265. static const struct pci_device_id pdc_ata_pci_tbl[] = {
  266. { PCI_VDEVICE(PROMISE, 0x3371), board_2037x },
  267. { PCI_VDEVICE(PROMISE, 0x3373), board_2037x },
  268. { PCI_VDEVICE(PROMISE, 0x3375), board_2037x },
  269. { PCI_VDEVICE(PROMISE, 0x3376), board_2037x },
  270. { PCI_VDEVICE(PROMISE, 0x3570), board_2057x },
  271. { PCI_VDEVICE(PROMISE, 0x3571), board_2057x },
  272. { PCI_VDEVICE(PROMISE, 0x3574), board_2057x },
  273. { PCI_VDEVICE(PROMISE, 0x3577), board_2057x },
  274. { PCI_VDEVICE(PROMISE, 0x3d73), board_2057x },
  275. { PCI_VDEVICE(PROMISE, 0x3d75), board_2057x },
  276. { PCI_VDEVICE(PROMISE, 0x3318), board_20319 },
  277. { PCI_VDEVICE(PROMISE, 0x3319), board_20319 },
  278. { PCI_VDEVICE(PROMISE, 0x3515), board_40518 },
  279. { PCI_VDEVICE(PROMISE, 0x3519), board_40518 },
  280. { PCI_VDEVICE(PROMISE, 0x3d17), board_40518 },
  281. { PCI_VDEVICE(PROMISE, 0x3d18), board_40518 },
  282. { PCI_VDEVICE(PROMISE, 0x6629), board_20619 },
  283. { } /* terminate list */
  284. };
  285. static struct pci_driver pdc_ata_pci_driver = {
  286. .name = DRV_NAME,
  287. .id_table = pdc_ata_pci_tbl,
  288. .probe = pdc_ata_init_one,
  289. .remove = ata_pci_remove_one,
  290. };
  291. static int pdc_common_port_start(struct ata_port *ap)
  292. {
  293. struct device *dev = ap->host->dev;
  294. struct pdc_port_priv *pp;
  295. int rc;
  296. rc = ata_port_start(ap);
  297. if (rc)
  298. return rc;
  299. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  300. if (!pp)
  301. return -ENOMEM;
  302. pp->pkt = dmam_alloc_coherent(dev, 128, &pp->pkt_dma, GFP_KERNEL);
  303. if (!pp->pkt)
  304. return -ENOMEM;
  305. ap->private_data = pp;
  306. return 0;
  307. }
  308. static int pdc_sata_port_start(struct ata_port *ap)
  309. {
  310. int rc;
  311. rc = pdc_common_port_start(ap);
  312. if (rc)
  313. return rc;
  314. /* fix up PHYMODE4 align timing */
  315. if (ap->flags & PDC_FLAG_GEN_II) {
  316. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  317. unsigned int tmp;
  318. tmp = readl(sata_mmio + PDC_PHYMODE4);
  319. tmp = (tmp & ~3) | 1; /* set bits 1:0 = 0:1 */
  320. writel(tmp, sata_mmio + PDC_PHYMODE4);
  321. }
  322. return 0;
  323. }
  324. static void pdc_fpdma_clear_interrupt_flag(struct ata_port *ap)
  325. {
  326. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  327. u32 tmp;
  328. tmp = readl(sata_mmio + PDC_FPDMA_CTLSTAT);
  329. tmp |= PDC_FPDMA_CTLSTAT_DMASETUP_INT_FLAG;
  330. tmp |= PDC_FPDMA_CTLSTAT_SETDB_INT_FLAG;
  331. /* It's not allowed to write to the entire FPDMA_CTLSTAT register
  332. when NCQ is running. So do a byte-sized write to bits 10 and 11. */
  333. writeb(tmp >> 8, sata_mmio + PDC_FPDMA_CTLSTAT + 1);
  334. readb(sata_mmio + PDC_FPDMA_CTLSTAT + 1); /* flush */
  335. }
  336. static void pdc_fpdma_reset(struct ata_port *ap)
  337. {
  338. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  339. u8 tmp;
  340. tmp = (u8)readl(sata_mmio + PDC_FPDMA_CTLSTAT);
  341. tmp &= 0x7F;
  342. tmp |= PDC_FPDMA_CTLSTAT_RESET;
  343. writeb(tmp, sata_mmio + PDC_FPDMA_CTLSTAT);
  344. readl(sata_mmio + PDC_FPDMA_CTLSTAT); /* flush */
  345. udelay(100);
  346. tmp &= ~PDC_FPDMA_CTLSTAT_RESET;
  347. writeb(tmp, sata_mmio + PDC_FPDMA_CTLSTAT);
  348. readl(sata_mmio + PDC_FPDMA_CTLSTAT); /* flush */
  349. pdc_fpdma_clear_interrupt_flag(ap);
  350. }
  351. static void pdc_not_at_command_packet_phase(struct ata_port *ap)
  352. {
  353. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  354. unsigned int i;
  355. u32 tmp;
  356. /* check not at ASIC packet command phase */
  357. for (i = 0; i < 100; ++i) {
  358. writel(0, sata_mmio + PDC_INTERNAL_DEBUG_1);
  359. tmp = readl(sata_mmio + PDC_INTERNAL_DEBUG_2);
  360. if ((tmp & 0xF) != 1)
  361. break;
  362. udelay(100);
  363. }
  364. }
  365. static void pdc_clear_internal_debug_record_error_register(struct ata_port *ap)
  366. {
  367. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  368. writel(0xffffffff, sata_mmio + PDC_SATA_ERROR);
  369. writel(0xffff0000, sata_mmio + PDC_LINK_LAYER_ERRORS);
  370. }
  371. static void pdc_reset_port(struct ata_port *ap)
  372. {
  373. void __iomem *ata_ctlstat_mmio = ap->ioaddr.cmd_addr + PDC_CTLSTAT;
  374. unsigned int i;
  375. u32 tmp;
  376. if (ap->flags & PDC_FLAG_GEN_II)
  377. pdc_not_at_command_packet_phase(ap);
  378. tmp = readl(ata_ctlstat_mmio);
  379. tmp |= PDC_RESET;
  380. writel(tmp, ata_ctlstat_mmio);
  381. for (i = 11; i > 0; i--) {
  382. tmp = readl(ata_ctlstat_mmio);
  383. if (tmp & PDC_RESET)
  384. break;
  385. udelay(100);
  386. tmp |= PDC_RESET;
  387. writel(tmp, ata_ctlstat_mmio);
  388. }
  389. tmp &= ~PDC_RESET;
  390. writel(tmp, ata_ctlstat_mmio);
  391. readl(ata_ctlstat_mmio); /* flush */
  392. if (sata_scr_valid(&ap->link) && (ap->flags & PDC_FLAG_GEN_II)) {
  393. pdc_fpdma_reset(ap);
  394. pdc_clear_internal_debug_record_error_register(ap);
  395. }
  396. }
  397. static int pdc_pata_cable_detect(struct ata_port *ap)
  398. {
  399. u8 tmp;
  400. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  401. tmp = readb(ata_mmio + PDC_CTLSTAT + 3);
  402. if (tmp & 0x01)
  403. return ATA_CBL_PATA40;
  404. return ATA_CBL_PATA80;
  405. }
  406. static int pdc_sata_cable_detect(struct ata_port *ap)
  407. {
  408. return ATA_CBL_SATA;
  409. }
  410. static int pdc_sata_scr_read(struct ata_link *link,
  411. unsigned int sc_reg, u32 *val)
  412. {
  413. if (sc_reg > SCR_CONTROL)
  414. return -EINVAL;
  415. *val = readl(link->ap->ioaddr.scr_addr + (sc_reg * 4));
  416. return 0;
  417. }
  418. static int pdc_sata_scr_write(struct ata_link *link,
  419. unsigned int sc_reg, u32 val)
  420. {
  421. if (sc_reg > SCR_CONTROL)
  422. return -EINVAL;
  423. writel(val, link->ap->ioaddr.scr_addr + (sc_reg * 4));
  424. return 0;
  425. }
  426. static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
  427. {
  428. struct ata_port *ap = qc->ap;
  429. dma_addr_t sg_table = ap->prd_dma;
  430. unsigned int cdb_len = qc->dev->cdb_len;
  431. u8 *cdb = qc->cdb;
  432. struct pdc_port_priv *pp = ap->private_data;
  433. u8 *buf = pp->pkt;
  434. __le32 *buf32 = (__le32 *) buf;
  435. unsigned int dev_sel, feature;
  436. /* set control bits (byte 0), zero delay seq id (byte 3),
  437. * and seq id (byte 2)
  438. */
  439. switch (qc->tf.protocol) {
  440. case ATAPI_PROT_DMA:
  441. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  442. buf32[0] = cpu_to_le32(PDC_PKT_READ);
  443. else
  444. buf32[0] = 0;
  445. break;
  446. case ATAPI_PROT_NODATA:
  447. buf32[0] = cpu_to_le32(PDC_PKT_NODATA);
  448. break;
  449. default:
  450. BUG();
  451. break;
  452. }
  453. buf32[1] = cpu_to_le32(sg_table); /* S/G table addr */
  454. buf32[2] = 0; /* no next-packet */
  455. /* select drive */
  456. if (sata_scr_valid(&ap->link))
  457. dev_sel = PDC_DEVICE_SATA;
  458. else
  459. dev_sel = qc->tf.device;
  460. buf[12] = (1 << 5) | ATA_REG_DEVICE;
  461. buf[13] = dev_sel;
  462. buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY;
  463. buf[15] = dev_sel; /* once more, waiting for BSY to clear */
  464. buf[16] = (1 << 5) | ATA_REG_NSECT;
  465. buf[17] = qc->tf.nsect;
  466. buf[18] = (1 << 5) | ATA_REG_LBAL;
  467. buf[19] = qc->tf.lbal;
  468. /* set feature and byte counter registers */
  469. if (qc->tf.protocol != ATAPI_PROT_DMA)
  470. feature = PDC_FEATURE_ATAPI_PIO;
  471. else
  472. feature = PDC_FEATURE_ATAPI_DMA;
  473. buf[20] = (1 << 5) | ATA_REG_FEATURE;
  474. buf[21] = feature;
  475. buf[22] = (1 << 5) | ATA_REG_BYTEL;
  476. buf[23] = qc->tf.lbam;
  477. buf[24] = (1 << 5) | ATA_REG_BYTEH;
  478. buf[25] = qc->tf.lbah;
  479. /* send ATAPI packet command 0xA0 */
  480. buf[26] = (1 << 5) | ATA_REG_CMD;
  481. buf[27] = qc->tf.command;
  482. /* select drive and check DRQ */
  483. buf[28] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_WAIT_DRDY;
  484. buf[29] = dev_sel;
  485. /* we can represent cdb lengths 2/4/6/8/10/12/14/16 */
  486. BUG_ON(cdb_len & ~0x1E);
  487. /* append the CDB as the final part */
  488. buf[30] = (((cdb_len >> 1) & 7) << 5) | ATA_REG_DATA | PDC_LAST_REG;
  489. memcpy(buf+31, cdb, cdb_len);
  490. }
  491. /**
  492. * pdc_fill_sg - Fill PCI IDE PRD table
  493. * @qc: Metadata associated with taskfile to be transferred
  494. *
  495. * Fill PCI IDE PRD (scatter-gather) table with segments
  496. * associated with the current disk command.
  497. * Make sure hardware does not choke on it.
  498. *
  499. * LOCKING:
  500. * spin_lock_irqsave(host lock)
  501. *
  502. */
  503. static void pdc_fill_sg(struct ata_queued_cmd *qc)
  504. {
  505. struct ata_port *ap = qc->ap;
  506. struct scatterlist *sg;
  507. const u32 SG_COUNT_ASIC_BUG = 41*4;
  508. unsigned int si, idx;
  509. u32 len;
  510. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  511. return;
  512. idx = 0;
  513. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  514. u32 addr, offset;
  515. u32 sg_len;
  516. /* determine if physical DMA addr spans 64K boundary.
  517. * Note h/w doesn't support 64-bit, so we unconditionally
  518. * truncate dma_addr_t to u32.
  519. */
  520. addr = (u32) sg_dma_address(sg);
  521. sg_len = sg_dma_len(sg);
  522. while (sg_len) {
  523. offset = addr & 0xffff;
  524. len = sg_len;
  525. if ((offset + sg_len) > 0x10000)
  526. len = 0x10000 - offset;
  527. ap->prd[idx].addr = cpu_to_le32(addr);
  528. ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  529. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  530. idx++;
  531. sg_len -= len;
  532. addr += len;
  533. }
  534. }
  535. len = le32_to_cpu(ap->prd[idx - 1].flags_len);
  536. if (len > SG_COUNT_ASIC_BUG) {
  537. u32 addr;
  538. VPRINTK("Splitting last PRD.\n");
  539. addr = le32_to_cpu(ap->prd[idx - 1].addr);
  540. ap->prd[idx - 1].flags_len = cpu_to_le32(len - SG_COUNT_ASIC_BUG);
  541. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx - 1, addr, SG_COUNT_ASIC_BUG);
  542. addr = addr + len - SG_COUNT_ASIC_BUG;
  543. len = SG_COUNT_ASIC_BUG;
  544. ap->prd[idx].addr = cpu_to_le32(addr);
  545. ap->prd[idx].flags_len = cpu_to_le32(len);
  546. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  547. idx++;
  548. }
  549. ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  550. }
  551. static void pdc_qc_prep(struct ata_queued_cmd *qc)
  552. {
  553. struct pdc_port_priv *pp = qc->ap->private_data;
  554. unsigned int i;
  555. VPRINTK("ENTER\n");
  556. switch (qc->tf.protocol) {
  557. case ATA_PROT_DMA:
  558. pdc_fill_sg(qc);
  559. /*FALLTHROUGH*/
  560. case ATA_PROT_NODATA:
  561. i = pdc_pkt_header(&qc->tf, qc->ap->prd_dma,
  562. qc->dev->devno, pp->pkt);
  563. if (qc->tf.flags & ATA_TFLAG_LBA48)
  564. i = pdc_prep_lba48(&qc->tf, pp->pkt, i);
  565. else
  566. i = pdc_prep_lba28(&qc->tf, pp->pkt, i);
  567. pdc_pkt_footer(&qc->tf, pp->pkt, i);
  568. break;
  569. case ATAPI_PROT_PIO:
  570. pdc_fill_sg(qc);
  571. break;
  572. case ATAPI_PROT_DMA:
  573. pdc_fill_sg(qc);
  574. /*FALLTHROUGH*/
  575. case ATAPI_PROT_NODATA:
  576. pdc_atapi_pkt(qc);
  577. break;
  578. default:
  579. break;
  580. }
  581. }
  582. static int pdc_is_sataii_tx4(unsigned long flags)
  583. {
  584. const unsigned long mask = PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS;
  585. return (flags & mask) == mask;
  586. }
  587. static unsigned int pdc_port_no_to_ata_no(unsigned int port_no,
  588. int is_sataii_tx4)
  589. {
  590. static const unsigned char sataii_tx4_port_remap[4] = { 3, 1, 0, 2};
  591. return is_sataii_tx4 ? sataii_tx4_port_remap[port_no] : port_no;
  592. }
  593. static unsigned int pdc_sata_nr_ports(const struct ata_port *ap)
  594. {
  595. return (ap->flags & PDC_FLAG_4_PORTS) ? 4 : 2;
  596. }
  597. static unsigned int pdc_sata_ata_port_to_ata_no(const struct ata_port *ap)
  598. {
  599. const struct ata_host *host = ap->host;
  600. unsigned int nr_ports = pdc_sata_nr_ports(ap);
  601. unsigned int i;
  602. for (i = 0; i < nr_ports && host->ports[i] != ap; ++i)
  603. ;
  604. BUG_ON(i >= nr_ports);
  605. return pdc_port_no_to_ata_no(i, pdc_is_sataii_tx4(ap->flags));
  606. }
  607. static void pdc_freeze(struct ata_port *ap)
  608. {
  609. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  610. u32 tmp;
  611. tmp = readl(ata_mmio + PDC_CTLSTAT);
  612. tmp |= PDC_IRQ_DISABLE;
  613. tmp &= ~PDC_DMA_ENABLE;
  614. writel(tmp, ata_mmio + PDC_CTLSTAT);
  615. readl(ata_mmio + PDC_CTLSTAT); /* flush */
  616. }
  617. static void pdc_sata_freeze(struct ata_port *ap)
  618. {
  619. struct ata_host *host = ap->host;
  620. void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
  621. unsigned int hotplug_offset = PDC2_SATA_PLUG_CSR;
  622. unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
  623. u32 hotplug_status;
  624. /* Disable hotplug events on this port.
  625. *
  626. * Locking:
  627. * 1) hotplug register accesses must be serialised via host->lock
  628. * 2) ap->lock == &ap->host->lock
  629. * 3) ->freeze() and ->thaw() are called with ap->lock held
  630. */
  631. hotplug_status = readl(host_mmio + hotplug_offset);
  632. hotplug_status |= 0x11 << (ata_no + 16);
  633. writel(hotplug_status, host_mmio + hotplug_offset);
  634. readl(host_mmio + hotplug_offset); /* flush */
  635. pdc_freeze(ap);
  636. }
  637. static void pdc_thaw(struct ata_port *ap)
  638. {
  639. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  640. u32 tmp;
  641. /* clear IRQ */
  642. readl(ata_mmio + PDC_COMMAND);
  643. /* turn IRQ back on */
  644. tmp = readl(ata_mmio + PDC_CTLSTAT);
  645. tmp &= ~PDC_IRQ_DISABLE;
  646. writel(tmp, ata_mmio + PDC_CTLSTAT);
  647. readl(ata_mmio + PDC_CTLSTAT); /* flush */
  648. }
  649. static void pdc_sata_thaw(struct ata_port *ap)
  650. {
  651. struct ata_host *host = ap->host;
  652. void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
  653. unsigned int hotplug_offset = PDC2_SATA_PLUG_CSR;
  654. unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
  655. u32 hotplug_status;
  656. pdc_thaw(ap);
  657. /* Enable hotplug events on this port.
  658. * Locking: see pdc_sata_freeze().
  659. */
  660. hotplug_status = readl(host_mmio + hotplug_offset);
  661. hotplug_status |= 0x11 << ata_no;
  662. hotplug_status &= ~(0x11 << (ata_no + 16));
  663. writel(hotplug_status, host_mmio + hotplug_offset);
  664. readl(host_mmio + hotplug_offset); /* flush */
  665. }
  666. static int pdc_pata_softreset(struct ata_link *link, unsigned int *class,
  667. unsigned long deadline)
  668. {
  669. pdc_reset_port(link->ap);
  670. return ata_sff_softreset(link, class, deadline);
  671. }
  672. static unsigned int pdc_ata_port_to_ata_no(const struct ata_port *ap)
  673. {
  674. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  675. void __iomem *host_mmio = ap->host->iomap[PDC_MMIO_BAR];
  676. /* ata_mmio == host_mmio + 0x200 + ata_no * 0x80 */
  677. return (ata_mmio - host_mmio - 0x200) / 0x80;
  678. }
  679. static void pdc_hard_reset_port(struct ata_port *ap)
  680. {
  681. void __iomem *host_mmio = ap->host->iomap[PDC_MMIO_BAR];
  682. void __iomem *pcictl_b1_mmio = host_mmio + PDC_PCI_CTL + 1;
  683. unsigned int ata_no = pdc_ata_port_to_ata_no(ap);
  684. u8 tmp;
  685. spin_lock(&ap->host->lock);
  686. tmp = readb(pcictl_b1_mmio);
  687. tmp &= ~(0x10 << ata_no);
  688. writeb(tmp, pcictl_b1_mmio);
  689. readb(pcictl_b1_mmio); /* flush */
  690. udelay(100);
  691. tmp |= (0x10 << ata_no);
  692. writeb(tmp, pcictl_b1_mmio);
  693. readb(pcictl_b1_mmio); /* flush */
  694. spin_unlock(&ap->host->lock);
  695. }
  696. static int pdc_sata_hardreset(struct ata_link *link, unsigned int *class,
  697. unsigned long deadline)
  698. {
  699. if (link->ap->flags & PDC_FLAG_GEN_II)
  700. pdc_not_at_command_packet_phase(link->ap);
  701. /* hotplug IRQs should have been masked by pdc_sata_freeze() */
  702. pdc_hard_reset_port(link->ap);
  703. pdc_reset_port(link->ap);
  704. /* sata_promise can't reliably acquire the first D2H Reg FIS
  705. * after hardreset. Do non-waiting hardreset and request
  706. * follow-up SRST.
  707. */
  708. return sata_std_hardreset(link, class, deadline);
  709. }
  710. static void pdc_error_handler(struct ata_port *ap)
  711. {
  712. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  713. pdc_reset_port(ap);
  714. ata_std_error_handler(ap);
  715. }
  716. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc)
  717. {
  718. struct ata_port *ap = qc->ap;
  719. /* make DMA engine forget about the failed command */
  720. if (qc->flags & ATA_QCFLAG_FAILED)
  721. pdc_reset_port(ap);
  722. }
  723. static void pdc_error_intr(struct ata_port *ap, struct ata_queued_cmd *qc,
  724. u32 port_status, u32 err_mask)
  725. {
  726. struct ata_eh_info *ehi = &ap->link.eh_info;
  727. unsigned int ac_err_mask = 0;
  728. ata_ehi_clear_desc(ehi);
  729. ata_ehi_push_desc(ehi, "port_status 0x%08x", port_status);
  730. port_status &= err_mask;
  731. if (port_status & PDC_DRIVE_ERR)
  732. ac_err_mask |= AC_ERR_DEV;
  733. if (port_status & (PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR))
  734. ac_err_mask |= AC_ERR_OTHER;
  735. if (port_status & (PDC2_ATA_HBA_ERR | PDC2_ATA_DMA_CNT_ERR))
  736. ac_err_mask |= AC_ERR_ATA_BUS;
  737. if (port_status & (PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR | PDC2_HTO_ERR
  738. | PDC_PCI_SYS_ERR | PDC1_PCI_PARITY_ERR))
  739. ac_err_mask |= AC_ERR_HOST_BUS;
  740. if (sata_scr_valid(&ap->link)) {
  741. u32 serror;
  742. pdc_sata_scr_read(&ap->link, SCR_ERROR, &serror);
  743. ehi->serror |= serror;
  744. }
  745. qc->err_mask |= ac_err_mask;
  746. pdc_reset_port(ap);
  747. ata_port_abort(ap);
  748. }
  749. static unsigned int pdc_host_intr(struct ata_port *ap,
  750. struct ata_queued_cmd *qc)
  751. {
  752. unsigned int handled = 0;
  753. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  754. u32 port_status, err_mask;
  755. err_mask = PDC_ERR_MASK;
  756. if (ap->flags & PDC_FLAG_GEN_II)
  757. err_mask &= ~PDC1_ERR_MASK;
  758. else
  759. err_mask &= ~PDC2_ERR_MASK;
  760. port_status = readl(ata_mmio + PDC_GLOBAL_CTL);
  761. if (unlikely(port_status & err_mask)) {
  762. pdc_error_intr(ap, qc, port_status, err_mask);
  763. return 1;
  764. }
  765. switch (qc->tf.protocol) {
  766. case ATA_PROT_DMA:
  767. case ATA_PROT_NODATA:
  768. case ATAPI_PROT_DMA:
  769. case ATAPI_PROT_NODATA:
  770. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  771. ata_qc_complete(qc);
  772. handled = 1;
  773. break;
  774. default:
  775. ap->stats.idle_irq++;
  776. break;
  777. }
  778. return handled;
  779. }
  780. static void pdc_irq_clear(struct ata_port *ap)
  781. {
  782. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  783. readl(ata_mmio + PDC_COMMAND);
  784. }
  785. static irqreturn_t pdc_interrupt(int irq, void *dev_instance)
  786. {
  787. struct ata_host *host = dev_instance;
  788. struct ata_port *ap;
  789. u32 mask = 0;
  790. unsigned int i, tmp;
  791. unsigned int handled = 0;
  792. void __iomem *host_mmio;
  793. unsigned int hotplug_offset, ata_no;
  794. u32 hotplug_status;
  795. int is_sataii_tx4;
  796. VPRINTK("ENTER\n");
  797. if (!host || !host->iomap[PDC_MMIO_BAR]) {
  798. VPRINTK("QUICK EXIT\n");
  799. return IRQ_NONE;
  800. }
  801. host_mmio = host->iomap[PDC_MMIO_BAR];
  802. spin_lock(&host->lock);
  803. /* read and clear hotplug flags for all ports */
  804. if (host->ports[0]->flags & PDC_FLAG_GEN_II) {
  805. hotplug_offset = PDC2_SATA_PLUG_CSR;
  806. hotplug_status = readl(host_mmio + hotplug_offset);
  807. if (hotplug_status & 0xff)
  808. writel(hotplug_status | 0xff, host_mmio + hotplug_offset);
  809. hotplug_status &= 0xff; /* clear uninteresting bits */
  810. } else
  811. hotplug_status = 0;
  812. /* reading should also clear interrupts */
  813. mask = readl(host_mmio + PDC_INT_SEQMASK);
  814. if (mask == 0xffffffff && hotplug_status == 0) {
  815. VPRINTK("QUICK EXIT 2\n");
  816. goto done_irq;
  817. }
  818. mask &= 0xffff; /* only 16 SEQIDs possible */
  819. if (mask == 0 && hotplug_status == 0) {
  820. VPRINTK("QUICK EXIT 3\n");
  821. goto done_irq;
  822. }
  823. writel(mask, host_mmio + PDC_INT_SEQMASK);
  824. is_sataii_tx4 = pdc_is_sataii_tx4(host->ports[0]->flags);
  825. for (i = 0; i < host->n_ports; i++) {
  826. VPRINTK("port %u\n", i);
  827. ap = host->ports[i];
  828. /* check for a plug or unplug event */
  829. ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  830. tmp = hotplug_status & (0x11 << ata_no);
  831. if (tmp) {
  832. struct ata_eh_info *ehi = &ap->link.eh_info;
  833. ata_ehi_clear_desc(ehi);
  834. ata_ehi_hotplugged(ehi);
  835. ata_ehi_push_desc(ehi, "hotplug_status %#x", tmp);
  836. ata_port_freeze(ap);
  837. ++handled;
  838. continue;
  839. }
  840. /* check for a packet interrupt */
  841. tmp = mask & (1 << (i + 1));
  842. if (tmp) {
  843. struct ata_queued_cmd *qc;
  844. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  845. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  846. handled += pdc_host_intr(ap, qc);
  847. }
  848. }
  849. VPRINTK("EXIT\n");
  850. done_irq:
  851. spin_unlock(&host->lock);
  852. return IRQ_RETVAL(handled);
  853. }
  854. static void pdc_packet_start(struct ata_queued_cmd *qc)
  855. {
  856. struct ata_port *ap = qc->ap;
  857. struct pdc_port_priv *pp = ap->private_data;
  858. void __iomem *host_mmio = ap->host->iomap[PDC_MMIO_BAR];
  859. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  860. unsigned int port_no = ap->port_no;
  861. u8 seq = (u8) (port_no + 1);
  862. VPRINTK("ENTER, ap %p\n", ap);
  863. writel(0x00000001, host_mmio + (seq * 4));
  864. readl(host_mmio + (seq * 4)); /* flush */
  865. pp->pkt[2] = seq;
  866. wmb(); /* flush PRD, pkt writes */
  867. writel(pp->pkt_dma, ata_mmio + PDC_PKT_SUBMIT);
  868. readl(ata_mmio + PDC_PKT_SUBMIT); /* flush */
  869. }
  870. static unsigned int pdc_qc_issue(struct ata_queued_cmd *qc)
  871. {
  872. switch (qc->tf.protocol) {
  873. case ATAPI_PROT_NODATA:
  874. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  875. break;
  876. /*FALLTHROUGH*/
  877. case ATA_PROT_NODATA:
  878. if (qc->tf.flags & ATA_TFLAG_POLLING)
  879. break;
  880. /*FALLTHROUGH*/
  881. case ATAPI_PROT_DMA:
  882. case ATA_PROT_DMA:
  883. pdc_packet_start(qc);
  884. return 0;
  885. default:
  886. break;
  887. }
  888. return ata_sff_qc_issue(qc);
  889. }
  890. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  891. {
  892. WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
  893. ata_sff_tf_load(ap, tf);
  894. }
  895. static void pdc_exec_command_mmio(struct ata_port *ap,
  896. const struct ata_taskfile *tf)
  897. {
  898. WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
  899. ata_sff_exec_command(ap, tf);
  900. }
  901. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc)
  902. {
  903. u8 *scsicmd = qc->scsicmd->cmnd;
  904. int pio = 1; /* atapi dma off by default */
  905. /* Whitelist commands that may use DMA. */
  906. switch (scsicmd[0]) {
  907. case WRITE_12:
  908. case WRITE_10:
  909. case WRITE_6:
  910. case READ_12:
  911. case READ_10:
  912. case READ_6:
  913. case 0xad: /* READ_DVD_STRUCTURE */
  914. case 0xbe: /* READ_CD */
  915. pio = 0;
  916. }
  917. /* -45150 (FFFF4FA2) to -1 (FFFFFFFF) shall use PIO mode */
  918. if (scsicmd[0] == WRITE_10) {
  919. unsigned int lba =
  920. (scsicmd[2] << 24) |
  921. (scsicmd[3] << 16) |
  922. (scsicmd[4] << 8) |
  923. scsicmd[5];
  924. if (lba >= 0xFFFF4FA2)
  925. pio = 1;
  926. }
  927. return pio;
  928. }
  929. static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc)
  930. {
  931. /* First generation chips cannot use ATAPI DMA on SATA ports */
  932. return 1;
  933. }
  934. static void pdc_ata_setup_port(struct ata_port *ap,
  935. void __iomem *base, void __iomem *scr_addr)
  936. {
  937. ap->ioaddr.cmd_addr = base;
  938. ap->ioaddr.data_addr = base;
  939. ap->ioaddr.feature_addr =
  940. ap->ioaddr.error_addr = base + 0x4;
  941. ap->ioaddr.nsect_addr = base + 0x8;
  942. ap->ioaddr.lbal_addr = base + 0xc;
  943. ap->ioaddr.lbam_addr = base + 0x10;
  944. ap->ioaddr.lbah_addr = base + 0x14;
  945. ap->ioaddr.device_addr = base + 0x18;
  946. ap->ioaddr.command_addr =
  947. ap->ioaddr.status_addr = base + 0x1c;
  948. ap->ioaddr.altstatus_addr =
  949. ap->ioaddr.ctl_addr = base + 0x38;
  950. ap->ioaddr.scr_addr = scr_addr;
  951. }
  952. static void pdc_host_init(struct ata_host *host)
  953. {
  954. void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
  955. int is_gen2 = host->ports[0]->flags & PDC_FLAG_GEN_II;
  956. int hotplug_offset;
  957. u32 tmp;
  958. if (is_gen2)
  959. hotplug_offset = PDC2_SATA_PLUG_CSR;
  960. else
  961. hotplug_offset = PDC_SATA_PLUG_CSR;
  962. /*
  963. * Except for the hotplug stuff, this is voodoo from the
  964. * Promise driver. Label this entire section
  965. * "TODO: figure out why we do this"
  966. */
  967. /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
  968. tmp = readl(host_mmio + PDC_FLASH_CTL);
  969. tmp |= 0x02000; /* bit 13 (enable bmr burst) */
  970. if (!is_gen2)
  971. tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
  972. writel(tmp, host_mmio + PDC_FLASH_CTL);
  973. /* clear plug/unplug flags for all ports */
  974. tmp = readl(host_mmio + hotplug_offset);
  975. writel(tmp | 0xff, host_mmio + hotplug_offset);
  976. tmp = readl(host_mmio + hotplug_offset);
  977. if (is_gen2) /* unmask plug/unplug ints */
  978. writel(tmp & ~0xff0000, host_mmio + hotplug_offset);
  979. else /* mask plug/unplug ints */
  980. writel(tmp | 0xff0000, host_mmio + hotplug_offset);
  981. /* don't initialise TBG or SLEW on 2nd generation chips */
  982. if (is_gen2)
  983. return;
  984. /* reduce TBG clock to 133 Mhz. */
  985. tmp = readl(host_mmio + PDC_TBG_MODE);
  986. tmp &= ~0x30000; /* clear bit 17, 16*/
  987. tmp |= 0x10000; /* set bit 17:16 = 0:1 */
  988. writel(tmp, host_mmio + PDC_TBG_MODE);
  989. readl(host_mmio + PDC_TBG_MODE); /* flush */
  990. msleep(10);
  991. /* adjust slew rate control register. */
  992. tmp = readl(host_mmio + PDC_SLEW_CTL);
  993. tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
  994. tmp |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
  995. writel(tmp, host_mmio + PDC_SLEW_CTL);
  996. }
  997. static int pdc_ata_init_one(struct pci_dev *pdev,
  998. const struct pci_device_id *ent)
  999. {
  1000. static int printed_version;
  1001. const struct ata_port_info *pi = &pdc_port_info[ent->driver_data];
  1002. const struct ata_port_info *ppi[PDC_MAX_PORTS];
  1003. struct ata_host *host;
  1004. void __iomem *host_mmio;
  1005. int n_ports, i, rc;
  1006. int is_sataii_tx4;
  1007. if (!printed_version++)
  1008. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1009. /* enable and acquire resources */
  1010. rc = pcim_enable_device(pdev);
  1011. if (rc)
  1012. return rc;
  1013. rc = pcim_iomap_regions(pdev, 1 << PDC_MMIO_BAR, DRV_NAME);
  1014. if (rc == -EBUSY)
  1015. pcim_pin_device(pdev);
  1016. if (rc)
  1017. return rc;
  1018. host_mmio = pcim_iomap_table(pdev)[PDC_MMIO_BAR];
  1019. /* determine port configuration and setup host */
  1020. n_ports = 2;
  1021. if (pi->flags & PDC_FLAG_4_PORTS)
  1022. n_ports = 4;
  1023. for (i = 0; i < n_ports; i++)
  1024. ppi[i] = pi;
  1025. if (pi->flags & PDC_FLAG_SATA_PATA) {
  1026. u8 tmp = readb(host_mmio + PDC_FLASH_CTL + 1);
  1027. if (!(tmp & 0x80))
  1028. ppi[n_ports++] = pi + 1;
  1029. }
  1030. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  1031. if (!host) {
  1032. dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n");
  1033. return -ENOMEM;
  1034. }
  1035. host->iomap = pcim_iomap_table(pdev);
  1036. is_sataii_tx4 = pdc_is_sataii_tx4(pi->flags);
  1037. for (i = 0; i < host->n_ports; i++) {
  1038. struct ata_port *ap = host->ports[i];
  1039. unsigned int ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  1040. unsigned int ata_offset = 0x200 + ata_no * 0x80;
  1041. unsigned int scr_offset = 0x400 + ata_no * 0x100;
  1042. pdc_ata_setup_port(ap, host_mmio + ata_offset, host_mmio + scr_offset);
  1043. ata_port_pbar_desc(ap, PDC_MMIO_BAR, -1, "mmio");
  1044. ata_port_pbar_desc(ap, PDC_MMIO_BAR, ata_offset, "ata");
  1045. }
  1046. /* initialize adapter */
  1047. pdc_host_init(host);
  1048. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  1049. if (rc)
  1050. return rc;
  1051. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  1052. if (rc)
  1053. return rc;
  1054. /* start host, request IRQ and attach */
  1055. pci_set_master(pdev);
  1056. return ata_host_activate(host, pdev->irq, pdc_interrupt, IRQF_SHARED,
  1057. &pdc_ata_sht);
  1058. }
  1059. static int __init pdc_ata_init(void)
  1060. {
  1061. return pci_register_driver(&pdc_ata_pci_driver);
  1062. }
  1063. static void __exit pdc_ata_exit(void)
  1064. {
  1065. pci_unregister_driver(&pdc_ata_pci_driver);
  1066. }
  1067. MODULE_AUTHOR("Jeff Garzik");
  1068. MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
  1069. MODULE_LICENSE("GPL");
  1070. MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
  1071. MODULE_VERSION(DRV_VERSION);
  1072. module_init(pdc_ata_init);
  1073. module_exit(pdc_ata_exit);