sata_promise.c 34 KB

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