sata_promise.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  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. /* we use the same prd table as bmdma, allocate it */
  297. rc = ata_bmdma_port_start(ap);
  298. if (rc)
  299. return rc;
  300. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  301. if (!pp)
  302. return -ENOMEM;
  303. pp->pkt = dmam_alloc_coherent(dev, 128, &pp->pkt_dma, GFP_KERNEL);
  304. if (!pp->pkt)
  305. return -ENOMEM;
  306. ap->private_data = pp;
  307. return 0;
  308. }
  309. static int pdc_sata_port_start(struct ata_port *ap)
  310. {
  311. int rc;
  312. rc = pdc_common_port_start(ap);
  313. if (rc)
  314. return rc;
  315. /* fix up PHYMODE4 align timing */
  316. if (ap->flags & PDC_FLAG_GEN_II) {
  317. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  318. unsigned int tmp;
  319. tmp = readl(sata_mmio + PDC_PHYMODE4);
  320. tmp = (tmp & ~3) | 1; /* set bits 1:0 = 0:1 */
  321. writel(tmp, sata_mmio + PDC_PHYMODE4);
  322. }
  323. return 0;
  324. }
  325. static void pdc_fpdma_clear_interrupt_flag(struct ata_port *ap)
  326. {
  327. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  328. u32 tmp;
  329. tmp = readl(sata_mmio + PDC_FPDMA_CTLSTAT);
  330. tmp |= PDC_FPDMA_CTLSTAT_DMASETUP_INT_FLAG;
  331. tmp |= PDC_FPDMA_CTLSTAT_SETDB_INT_FLAG;
  332. /* It's not allowed to write to the entire FPDMA_CTLSTAT register
  333. when NCQ is running. So do a byte-sized write to bits 10 and 11. */
  334. writeb(tmp >> 8, sata_mmio + PDC_FPDMA_CTLSTAT + 1);
  335. readb(sata_mmio + PDC_FPDMA_CTLSTAT + 1); /* flush */
  336. }
  337. static void pdc_fpdma_reset(struct ata_port *ap)
  338. {
  339. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  340. u8 tmp;
  341. tmp = (u8)readl(sata_mmio + PDC_FPDMA_CTLSTAT);
  342. tmp &= 0x7F;
  343. tmp |= PDC_FPDMA_CTLSTAT_RESET;
  344. writeb(tmp, sata_mmio + PDC_FPDMA_CTLSTAT);
  345. readl(sata_mmio + PDC_FPDMA_CTLSTAT); /* flush */
  346. udelay(100);
  347. tmp &= ~PDC_FPDMA_CTLSTAT_RESET;
  348. writeb(tmp, sata_mmio + PDC_FPDMA_CTLSTAT);
  349. readl(sata_mmio + PDC_FPDMA_CTLSTAT); /* flush */
  350. pdc_fpdma_clear_interrupt_flag(ap);
  351. }
  352. static void pdc_not_at_command_packet_phase(struct ata_port *ap)
  353. {
  354. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  355. unsigned int i;
  356. u32 tmp;
  357. /* check not at ASIC packet command phase */
  358. for (i = 0; i < 100; ++i) {
  359. writel(0, sata_mmio + PDC_INTERNAL_DEBUG_1);
  360. tmp = readl(sata_mmio + PDC_INTERNAL_DEBUG_2);
  361. if ((tmp & 0xF) != 1)
  362. break;
  363. udelay(100);
  364. }
  365. }
  366. static void pdc_clear_internal_debug_record_error_register(struct ata_port *ap)
  367. {
  368. void __iomem *sata_mmio = ap->ioaddr.scr_addr;
  369. writel(0xffffffff, sata_mmio + PDC_SATA_ERROR);
  370. writel(0xffff0000, sata_mmio + PDC_LINK_LAYER_ERRORS);
  371. }
  372. static void pdc_reset_port(struct ata_port *ap)
  373. {
  374. void __iomem *ata_ctlstat_mmio = ap->ioaddr.cmd_addr + PDC_CTLSTAT;
  375. unsigned int i;
  376. u32 tmp;
  377. if (ap->flags & PDC_FLAG_GEN_II)
  378. pdc_not_at_command_packet_phase(ap);
  379. tmp = readl(ata_ctlstat_mmio);
  380. tmp |= PDC_RESET;
  381. writel(tmp, ata_ctlstat_mmio);
  382. for (i = 11; i > 0; i--) {
  383. tmp = readl(ata_ctlstat_mmio);
  384. if (tmp & PDC_RESET)
  385. break;
  386. udelay(100);
  387. tmp |= PDC_RESET;
  388. writel(tmp, ata_ctlstat_mmio);
  389. }
  390. tmp &= ~PDC_RESET;
  391. writel(tmp, ata_ctlstat_mmio);
  392. readl(ata_ctlstat_mmio); /* flush */
  393. if (sata_scr_valid(&ap->link) && (ap->flags & PDC_FLAG_GEN_II)) {
  394. pdc_fpdma_reset(ap);
  395. pdc_clear_internal_debug_record_error_register(ap);
  396. }
  397. }
  398. static int pdc_pata_cable_detect(struct ata_port *ap)
  399. {
  400. u8 tmp;
  401. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  402. tmp = readb(ata_mmio + PDC_CTLSTAT + 3);
  403. if (tmp & 0x01)
  404. return ATA_CBL_PATA40;
  405. return ATA_CBL_PATA80;
  406. }
  407. static int pdc_sata_cable_detect(struct ata_port *ap)
  408. {
  409. return ATA_CBL_SATA;
  410. }
  411. static int pdc_sata_scr_read(struct ata_link *link,
  412. unsigned int sc_reg, u32 *val)
  413. {
  414. if (sc_reg > SCR_CONTROL)
  415. return -EINVAL;
  416. *val = readl(link->ap->ioaddr.scr_addr + (sc_reg * 4));
  417. return 0;
  418. }
  419. static int pdc_sata_scr_write(struct ata_link *link,
  420. unsigned int sc_reg, u32 val)
  421. {
  422. if (sc_reg > SCR_CONTROL)
  423. return -EINVAL;
  424. writel(val, link->ap->ioaddr.scr_addr + (sc_reg * 4));
  425. return 0;
  426. }
  427. static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
  428. {
  429. struct ata_port *ap = qc->ap;
  430. dma_addr_t sg_table = ap->bmdma_prd_dma;
  431. unsigned int cdb_len = qc->dev->cdb_len;
  432. u8 *cdb = qc->cdb;
  433. struct pdc_port_priv *pp = ap->private_data;
  434. u8 *buf = pp->pkt;
  435. __le32 *buf32 = (__le32 *) buf;
  436. unsigned int dev_sel, feature;
  437. /* set control bits (byte 0), zero delay seq id (byte 3),
  438. * and seq id (byte 2)
  439. */
  440. switch (qc->tf.protocol) {
  441. case ATAPI_PROT_DMA:
  442. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  443. buf32[0] = cpu_to_le32(PDC_PKT_READ);
  444. else
  445. buf32[0] = 0;
  446. break;
  447. case ATAPI_PROT_NODATA:
  448. buf32[0] = cpu_to_le32(PDC_PKT_NODATA);
  449. break;
  450. default:
  451. BUG();
  452. break;
  453. }
  454. buf32[1] = cpu_to_le32(sg_table); /* S/G table addr */
  455. buf32[2] = 0; /* no next-packet */
  456. /* select drive */
  457. if (sata_scr_valid(&ap->link))
  458. dev_sel = PDC_DEVICE_SATA;
  459. else
  460. dev_sel = qc->tf.device;
  461. buf[12] = (1 << 5) | ATA_REG_DEVICE;
  462. buf[13] = dev_sel;
  463. buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY;
  464. buf[15] = dev_sel; /* once more, waiting for BSY to clear */
  465. buf[16] = (1 << 5) | ATA_REG_NSECT;
  466. buf[17] = qc->tf.nsect;
  467. buf[18] = (1 << 5) | ATA_REG_LBAL;
  468. buf[19] = qc->tf.lbal;
  469. /* set feature and byte counter registers */
  470. if (qc->tf.protocol != ATAPI_PROT_DMA)
  471. feature = PDC_FEATURE_ATAPI_PIO;
  472. else
  473. feature = PDC_FEATURE_ATAPI_DMA;
  474. buf[20] = (1 << 5) | ATA_REG_FEATURE;
  475. buf[21] = feature;
  476. buf[22] = (1 << 5) | ATA_REG_BYTEL;
  477. buf[23] = qc->tf.lbam;
  478. buf[24] = (1 << 5) | ATA_REG_BYTEH;
  479. buf[25] = qc->tf.lbah;
  480. /* send ATAPI packet command 0xA0 */
  481. buf[26] = (1 << 5) | ATA_REG_CMD;
  482. buf[27] = qc->tf.command;
  483. /* select drive and check DRQ */
  484. buf[28] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_WAIT_DRDY;
  485. buf[29] = dev_sel;
  486. /* we can represent cdb lengths 2/4/6/8/10/12/14/16 */
  487. BUG_ON(cdb_len & ~0x1E);
  488. /* append the CDB as the final part */
  489. buf[30] = (((cdb_len >> 1) & 7) << 5) | ATA_REG_DATA | PDC_LAST_REG;
  490. memcpy(buf+31, cdb, cdb_len);
  491. }
  492. /**
  493. * pdc_fill_sg - Fill PCI IDE PRD table
  494. * @qc: Metadata associated with taskfile to be transferred
  495. *
  496. * Fill PCI IDE PRD (scatter-gather) table with segments
  497. * associated with the current disk command.
  498. * Make sure hardware does not choke on it.
  499. *
  500. * LOCKING:
  501. * spin_lock_irqsave(host lock)
  502. *
  503. */
  504. static void pdc_fill_sg(struct ata_queued_cmd *qc)
  505. {
  506. struct ata_port *ap = qc->ap;
  507. struct ata_bmdma_prd *prd = ap->bmdma_prd;
  508. struct scatterlist *sg;
  509. const u32 SG_COUNT_ASIC_BUG = 41*4;
  510. unsigned int si, idx;
  511. u32 len;
  512. if (!(qc->flags & ATA_QCFLAG_DMAMAP))
  513. return;
  514. idx = 0;
  515. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  516. u32 addr, offset;
  517. u32 sg_len;
  518. /* determine if physical DMA addr spans 64K boundary.
  519. * Note h/w doesn't support 64-bit, so we unconditionally
  520. * truncate dma_addr_t to u32.
  521. */
  522. addr = (u32) sg_dma_address(sg);
  523. sg_len = sg_dma_len(sg);
  524. while (sg_len) {
  525. offset = addr & 0xffff;
  526. len = sg_len;
  527. if ((offset + sg_len) > 0x10000)
  528. len = 0x10000 - offset;
  529. prd[idx].addr = cpu_to_le32(addr);
  530. prd[idx].flags_len = cpu_to_le32(len & 0xffff);
  531. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  532. idx++;
  533. sg_len -= len;
  534. addr += len;
  535. }
  536. }
  537. len = le32_to_cpu(prd[idx - 1].flags_len);
  538. if (len > SG_COUNT_ASIC_BUG) {
  539. u32 addr;
  540. VPRINTK("Splitting last PRD.\n");
  541. addr = le32_to_cpu(prd[idx - 1].addr);
  542. prd[idx - 1].flags_len = cpu_to_le32(len - SG_COUNT_ASIC_BUG);
  543. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx - 1, addr, SG_COUNT_ASIC_BUG);
  544. addr = addr + len - SG_COUNT_ASIC_BUG;
  545. len = SG_COUNT_ASIC_BUG;
  546. prd[idx].addr = cpu_to_le32(addr);
  547. prd[idx].flags_len = cpu_to_le32(len);
  548. VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
  549. idx++;
  550. }
  551. prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
  552. }
  553. static void pdc_qc_prep(struct ata_queued_cmd *qc)
  554. {
  555. struct pdc_port_priv *pp = qc->ap->private_data;
  556. unsigned int i;
  557. VPRINTK("ENTER\n");
  558. switch (qc->tf.protocol) {
  559. case ATA_PROT_DMA:
  560. pdc_fill_sg(qc);
  561. /*FALLTHROUGH*/
  562. case ATA_PROT_NODATA:
  563. i = pdc_pkt_header(&qc->tf, qc->ap->bmdma_prd_dma,
  564. qc->dev->devno, pp->pkt);
  565. if (qc->tf.flags & ATA_TFLAG_LBA48)
  566. i = pdc_prep_lba48(&qc->tf, pp->pkt, i);
  567. else
  568. i = pdc_prep_lba28(&qc->tf, pp->pkt, i);
  569. pdc_pkt_footer(&qc->tf, pp->pkt, i);
  570. break;
  571. case ATAPI_PROT_PIO:
  572. pdc_fill_sg(qc);
  573. break;
  574. case ATAPI_PROT_DMA:
  575. pdc_fill_sg(qc);
  576. /*FALLTHROUGH*/
  577. case ATAPI_PROT_NODATA:
  578. pdc_atapi_pkt(qc);
  579. break;
  580. default:
  581. break;
  582. }
  583. }
  584. static int pdc_is_sataii_tx4(unsigned long flags)
  585. {
  586. const unsigned long mask = PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS;
  587. return (flags & mask) == mask;
  588. }
  589. static unsigned int pdc_port_no_to_ata_no(unsigned int port_no,
  590. int is_sataii_tx4)
  591. {
  592. static const unsigned char sataii_tx4_port_remap[4] = { 3, 1, 0, 2};
  593. return is_sataii_tx4 ? sataii_tx4_port_remap[port_no] : port_no;
  594. }
  595. static unsigned int pdc_sata_nr_ports(const struct ata_port *ap)
  596. {
  597. return (ap->flags & PDC_FLAG_4_PORTS) ? 4 : 2;
  598. }
  599. static unsigned int pdc_sata_ata_port_to_ata_no(const struct ata_port *ap)
  600. {
  601. const struct ata_host *host = ap->host;
  602. unsigned int nr_ports = pdc_sata_nr_ports(ap);
  603. unsigned int i;
  604. for (i = 0; i < nr_ports && host->ports[i] != ap; ++i)
  605. ;
  606. BUG_ON(i >= nr_ports);
  607. return pdc_port_no_to_ata_no(i, pdc_is_sataii_tx4(ap->flags));
  608. }
  609. static void pdc_freeze(struct ata_port *ap)
  610. {
  611. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  612. u32 tmp;
  613. tmp = readl(ata_mmio + PDC_CTLSTAT);
  614. tmp |= PDC_IRQ_DISABLE;
  615. tmp &= ~PDC_DMA_ENABLE;
  616. writel(tmp, ata_mmio + PDC_CTLSTAT);
  617. readl(ata_mmio + PDC_CTLSTAT); /* flush */
  618. }
  619. static void pdc_sata_freeze(struct ata_port *ap)
  620. {
  621. struct ata_host *host = ap->host;
  622. void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
  623. unsigned int hotplug_offset = PDC2_SATA_PLUG_CSR;
  624. unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
  625. u32 hotplug_status;
  626. /* Disable hotplug events on this port.
  627. *
  628. * Locking:
  629. * 1) hotplug register accesses must be serialised via host->lock
  630. * 2) ap->lock == &ap->host->lock
  631. * 3) ->freeze() and ->thaw() are called with ap->lock held
  632. */
  633. hotplug_status = readl(host_mmio + hotplug_offset);
  634. hotplug_status |= 0x11 << (ata_no + 16);
  635. writel(hotplug_status, host_mmio + hotplug_offset);
  636. readl(host_mmio + hotplug_offset); /* flush */
  637. pdc_freeze(ap);
  638. }
  639. static void pdc_thaw(struct ata_port *ap)
  640. {
  641. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  642. u32 tmp;
  643. /* clear IRQ */
  644. readl(ata_mmio + PDC_COMMAND);
  645. /* turn IRQ back on */
  646. tmp = readl(ata_mmio + PDC_CTLSTAT);
  647. tmp &= ~PDC_IRQ_DISABLE;
  648. writel(tmp, ata_mmio + PDC_CTLSTAT);
  649. readl(ata_mmio + PDC_CTLSTAT); /* flush */
  650. }
  651. static void pdc_sata_thaw(struct ata_port *ap)
  652. {
  653. struct ata_host *host = ap->host;
  654. void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
  655. unsigned int hotplug_offset = PDC2_SATA_PLUG_CSR;
  656. unsigned int ata_no = pdc_sata_ata_port_to_ata_no(ap);
  657. u32 hotplug_status;
  658. pdc_thaw(ap);
  659. /* Enable hotplug events on this port.
  660. * Locking: see pdc_sata_freeze().
  661. */
  662. hotplug_status = readl(host_mmio + hotplug_offset);
  663. hotplug_status |= 0x11 << ata_no;
  664. hotplug_status &= ~(0x11 << (ata_no + 16));
  665. writel(hotplug_status, host_mmio + hotplug_offset);
  666. readl(host_mmio + hotplug_offset); /* flush */
  667. }
  668. static int pdc_pata_softreset(struct ata_link *link, unsigned int *class,
  669. unsigned long deadline)
  670. {
  671. pdc_reset_port(link->ap);
  672. return ata_sff_softreset(link, class, deadline);
  673. }
  674. static unsigned int pdc_ata_port_to_ata_no(const struct ata_port *ap)
  675. {
  676. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  677. void __iomem *host_mmio = ap->host->iomap[PDC_MMIO_BAR];
  678. /* ata_mmio == host_mmio + 0x200 + ata_no * 0x80 */
  679. return (ata_mmio - host_mmio - 0x200) / 0x80;
  680. }
  681. static void pdc_hard_reset_port(struct ata_port *ap)
  682. {
  683. void __iomem *host_mmio = ap->host->iomap[PDC_MMIO_BAR];
  684. void __iomem *pcictl_b1_mmio = host_mmio + PDC_PCI_CTL + 1;
  685. unsigned int ata_no = pdc_ata_port_to_ata_no(ap);
  686. u8 tmp;
  687. spin_lock(&ap->host->lock);
  688. tmp = readb(pcictl_b1_mmio);
  689. tmp &= ~(0x10 << ata_no);
  690. writeb(tmp, pcictl_b1_mmio);
  691. readb(pcictl_b1_mmio); /* flush */
  692. udelay(100);
  693. tmp |= (0x10 << ata_no);
  694. writeb(tmp, pcictl_b1_mmio);
  695. readb(pcictl_b1_mmio); /* flush */
  696. spin_unlock(&ap->host->lock);
  697. }
  698. static int pdc_sata_hardreset(struct ata_link *link, unsigned int *class,
  699. unsigned long deadline)
  700. {
  701. if (link->ap->flags & PDC_FLAG_GEN_II)
  702. pdc_not_at_command_packet_phase(link->ap);
  703. /* hotplug IRQs should have been masked by pdc_sata_freeze() */
  704. pdc_hard_reset_port(link->ap);
  705. pdc_reset_port(link->ap);
  706. /* sata_promise can't reliably acquire the first D2H Reg FIS
  707. * after hardreset. Do non-waiting hardreset and request
  708. * follow-up SRST.
  709. */
  710. return sata_std_hardreset(link, class, deadline);
  711. }
  712. static void pdc_error_handler(struct ata_port *ap)
  713. {
  714. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  715. pdc_reset_port(ap);
  716. ata_sff_error_handler(ap);
  717. }
  718. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc)
  719. {
  720. struct ata_port *ap = qc->ap;
  721. /* make DMA engine forget about the failed command */
  722. if (qc->flags & ATA_QCFLAG_FAILED)
  723. pdc_reset_port(ap);
  724. }
  725. static void pdc_error_intr(struct ata_port *ap, struct ata_queued_cmd *qc,
  726. u32 port_status, u32 err_mask)
  727. {
  728. struct ata_eh_info *ehi = &ap->link.eh_info;
  729. unsigned int ac_err_mask = 0;
  730. ata_ehi_clear_desc(ehi);
  731. ata_ehi_push_desc(ehi, "port_status 0x%08x", port_status);
  732. port_status &= err_mask;
  733. if (port_status & PDC_DRIVE_ERR)
  734. ac_err_mask |= AC_ERR_DEV;
  735. if (port_status & (PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR))
  736. ac_err_mask |= AC_ERR_OTHER;
  737. if (port_status & (PDC2_ATA_HBA_ERR | PDC2_ATA_DMA_CNT_ERR))
  738. ac_err_mask |= AC_ERR_ATA_BUS;
  739. if (port_status & (PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR | PDC2_HTO_ERR
  740. | PDC_PCI_SYS_ERR | PDC1_PCI_PARITY_ERR))
  741. ac_err_mask |= AC_ERR_HOST_BUS;
  742. if (sata_scr_valid(&ap->link)) {
  743. u32 serror;
  744. pdc_sata_scr_read(&ap->link, SCR_ERROR, &serror);
  745. ehi->serror |= serror;
  746. }
  747. qc->err_mask |= ac_err_mask;
  748. pdc_reset_port(ap);
  749. ata_port_abort(ap);
  750. }
  751. static unsigned int pdc_host_intr(struct ata_port *ap,
  752. struct ata_queued_cmd *qc)
  753. {
  754. unsigned int handled = 0;
  755. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  756. u32 port_status, err_mask;
  757. err_mask = PDC_ERR_MASK;
  758. if (ap->flags & PDC_FLAG_GEN_II)
  759. err_mask &= ~PDC1_ERR_MASK;
  760. else
  761. err_mask &= ~PDC2_ERR_MASK;
  762. port_status = readl(ata_mmio + PDC_GLOBAL_CTL);
  763. if (unlikely(port_status & err_mask)) {
  764. pdc_error_intr(ap, qc, port_status, err_mask);
  765. return 1;
  766. }
  767. switch (qc->tf.protocol) {
  768. case ATA_PROT_DMA:
  769. case ATA_PROT_NODATA:
  770. case ATAPI_PROT_DMA:
  771. case ATAPI_PROT_NODATA:
  772. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  773. ata_qc_complete(qc);
  774. handled = 1;
  775. break;
  776. default:
  777. ap->stats.idle_irq++;
  778. break;
  779. }
  780. return handled;
  781. }
  782. static void pdc_irq_clear(struct ata_port *ap)
  783. {
  784. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  785. readl(ata_mmio + PDC_COMMAND);
  786. }
  787. static irqreturn_t pdc_interrupt(int irq, void *dev_instance)
  788. {
  789. struct ata_host *host = dev_instance;
  790. struct ata_port *ap;
  791. u32 mask = 0;
  792. unsigned int i, tmp;
  793. unsigned int handled = 0;
  794. void __iomem *host_mmio;
  795. unsigned int hotplug_offset, ata_no;
  796. u32 hotplug_status;
  797. int is_sataii_tx4;
  798. VPRINTK("ENTER\n");
  799. if (!host || !host->iomap[PDC_MMIO_BAR]) {
  800. VPRINTK("QUICK EXIT\n");
  801. return IRQ_NONE;
  802. }
  803. host_mmio = host->iomap[PDC_MMIO_BAR];
  804. spin_lock(&host->lock);
  805. /* read and clear hotplug flags for all ports */
  806. if (host->ports[0]->flags & PDC_FLAG_GEN_II) {
  807. hotplug_offset = PDC2_SATA_PLUG_CSR;
  808. hotplug_status = readl(host_mmio + hotplug_offset);
  809. if (hotplug_status & 0xff)
  810. writel(hotplug_status | 0xff, host_mmio + hotplug_offset);
  811. hotplug_status &= 0xff; /* clear uninteresting bits */
  812. } else
  813. hotplug_status = 0;
  814. /* reading should also clear interrupts */
  815. mask = readl(host_mmio + PDC_INT_SEQMASK);
  816. if (mask == 0xffffffff && hotplug_status == 0) {
  817. VPRINTK("QUICK EXIT 2\n");
  818. goto done_irq;
  819. }
  820. mask &= 0xffff; /* only 16 SEQIDs possible */
  821. if (mask == 0 && hotplug_status == 0) {
  822. VPRINTK("QUICK EXIT 3\n");
  823. goto done_irq;
  824. }
  825. writel(mask, host_mmio + PDC_INT_SEQMASK);
  826. is_sataii_tx4 = pdc_is_sataii_tx4(host->ports[0]->flags);
  827. for (i = 0; i < host->n_ports; i++) {
  828. VPRINTK("port %u\n", i);
  829. ap = host->ports[i];
  830. /* check for a plug or unplug event */
  831. ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  832. tmp = hotplug_status & (0x11 << ata_no);
  833. if (tmp) {
  834. struct ata_eh_info *ehi = &ap->link.eh_info;
  835. ata_ehi_clear_desc(ehi);
  836. ata_ehi_hotplugged(ehi);
  837. ata_ehi_push_desc(ehi, "hotplug_status %#x", tmp);
  838. ata_port_freeze(ap);
  839. ++handled;
  840. continue;
  841. }
  842. /* check for a packet interrupt */
  843. tmp = mask & (1 << (i + 1));
  844. if (tmp) {
  845. struct ata_queued_cmd *qc;
  846. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  847. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  848. handled += pdc_host_intr(ap, qc);
  849. }
  850. }
  851. VPRINTK("EXIT\n");
  852. done_irq:
  853. spin_unlock(&host->lock);
  854. return IRQ_RETVAL(handled);
  855. }
  856. static void pdc_packet_start(struct ata_queued_cmd *qc)
  857. {
  858. struct ata_port *ap = qc->ap;
  859. struct pdc_port_priv *pp = ap->private_data;
  860. void __iomem *host_mmio = ap->host->iomap[PDC_MMIO_BAR];
  861. void __iomem *ata_mmio = ap->ioaddr.cmd_addr;
  862. unsigned int port_no = ap->port_no;
  863. u8 seq = (u8) (port_no + 1);
  864. VPRINTK("ENTER, ap %p\n", ap);
  865. writel(0x00000001, host_mmio + (seq * 4));
  866. readl(host_mmio + (seq * 4)); /* flush */
  867. pp->pkt[2] = seq;
  868. wmb(); /* flush PRD, pkt writes */
  869. writel(pp->pkt_dma, ata_mmio + PDC_PKT_SUBMIT);
  870. readl(ata_mmio + PDC_PKT_SUBMIT); /* flush */
  871. }
  872. static unsigned int pdc_qc_issue(struct ata_queued_cmd *qc)
  873. {
  874. switch (qc->tf.protocol) {
  875. case ATAPI_PROT_NODATA:
  876. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  877. break;
  878. /*FALLTHROUGH*/
  879. case ATA_PROT_NODATA:
  880. if (qc->tf.flags & ATA_TFLAG_POLLING)
  881. break;
  882. /*FALLTHROUGH*/
  883. case ATAPI_PROT_DMA:
  884. case ATA_PROT_DMA:
  885. pdc_packet_start(qc);
  886. return 0;
  887. default:
  888. break;
  889. }
  890. return ata_sff_qc_issue(qc);
  891. }
  892. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  893. {
  894. WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
  895. ata_sff_tf_load(ap, tf);
  896. }
  897. static void pdc_exec_command_mmio(struct ata_port *ap,
  898. const struct ata_taskfile *tf)
  899. {
  900. WARN_ON(tf->protocol == ATA_PROT_DMA || tf->protocol == ATAPI_PROT_DMA);
  901. ata_sff_exec_command(ap, tf);
  902. }
  903. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc)
  904. {
  905. u8 *scsicmd = qc->scsicmd->cmnd;
  906. int pio = 1; /* atapi dma off by default */
  907. /* Whitelist commands that may use DMA. */
  908. switch (scsicmd[0]) {
  909. case WRITE_12:
  910. case WRITE_10:
  911. case WRITE_6:
  912. case READ_12:
  913. case READ_10:
  914. case READ_6:
  915. case 0xad: /* READ_DVD_STRUCTURE */
  916. case 0xbe: /* READ_CD */
  917. pio = 0;
  918. }
  919. /* -45150 (FFFF4FA2) to -1 (FFFFFFFF) shall use PIO mode */
  920. if (scsicmd[0] == WRITE_10) {
  921. unsigned int lba =
  922. (scsicmd[2] << 24) |
  923. (scsicmd[3] << 16) |
  924. (scsicmd[4] << 8) |
  925. scsicmd[5];
  926. if (lba >= 0xFFFF4FA2)
  927. pio = 1;
  928. }
  929. return pio;
  930. }
  931. static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc)
  932. {
  933. /* First generation chips cannot use ATAPI DMA on SATA ports */
  934. return 1;
  935. }
  936. static void pdc_ata_setup_port(struct ata_port *ap,
  937. void __iomem *base, void __iomem *scr_addr)
  938. {
  939. ap->ioaddr.cmd_addr = base;
  940. ap->ioaddr.data_addr = base;
  941. ap->ioaddr.feature_addr =
  942. ap->ioaddr.error_addr = base + 0x4;
  943. ap->ioaddr.nsect_addr = base + 0x8;
  944. ap->ioaddr.lbal_addr = base + 0xc;
  945. ap->ioaddr.lbam_addr = base + 0x10;
  946. ap->ioaddr.lbah_addr = base + 0x14;
  947. ap->ioaddr.device_addr = base + 0x18;
  948. ap->ioaddr.command_addr =
  949. ap->ioaddr.status_addr = base + 0x1c;
  950. ap->ioaddr.altstatus_addr =
  951. ap->ioaddr.ctl_addr = base + 0x38;
  952. ap->ioaddr.scr_addr = scr_addr;
  953. }
  954. static void pdc_host_init(struct ata_host *host)
  955. {
  956. void __iomem *host_mmio = host->iomap[PDC_MMIO_BAR];
  957. int is_gen2 = host->ports[0]->flags & PDC_FLAG_GEN_II;
  958. int hotplug_offset;
  959. u32 tmp;
  960. if (is_gen2)
  961. hotplug_offset = PDC2_SATA_PLUG_CSR;
  962. else
  963. hotplug_offset = PDC_SATA_PLUG_CSR;
  964. /*
  965. * Except for the hotplug stuff, this is voodoo from the
  966. * Promise driver. Label this entire section
  967. * "TODO: figure out why we do this"
  968. */
  969. /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
  970. tmp = readl(host_mmio + PDC_FLASH_CTL);
  971. tmp |= 0x02000; /* bit 13 (enable bmr burst) */
  972. if (!is_gen2)
  973. tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
  974. writel(tmp, host_mmio + PDC_FLASH_CTL);
  975. /* clear plug/unplug flags for all ports */
  976. tmp = readl(host_mmio + hotplug_offset);
  977. writel(tmp | 0xff, host_mmio + hotplug_offset);
  978. tmp = readl(host_mmio + hotplug_offset);
  979. if (is_gen2) /* unmask plug/unplug ints */
  980. writel(tmp & ~0xff0000, host_mmio + hotplug_offset);
  981. else /* mask plug/unplug ints */
  982. writel(tmp | 0xff0000, host_mmio + hotplug_offset);
  983. /* don't initialise TBG or SLEW on 2nd generation chips */
  984. if (is_gen2)
  985. return;
  986. /* reduce TBG clock to 133 Mhz. */
  987. tmp = readl(host_mmio + PDC_TBG_MODE);
  988. tmp &= ~0x30000; /* clear bit 17, 16*/
  989. tmp |= 0x10000; /* set bit 17:16 = 0:1 */
  990. writel(tmp, host_mmio + PDC_TBG_MODE);
  991. readl(host_mmio + PDC_TBG_MODE); /* flush */
  992. msleep(10);
  993. /* adjust slew rate control register. */
  994. tmp = readl(host_mmio + PDC_SLEW_CTL);
  995. tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
  996. tmp |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
  997. writel(tmp, host_mmio + PDC_SLEW_CTL);
  998. }
  999. static int pdc_ata_init_one(struct pci_dev *pdev,
  1000. const struct pci_device_id *ent)
  1001. {
  1002. static int printed_version;
  1003. const struct ata_port_info *pi = &pdc_port_info[ent->driver_data];
  1004. const struct ata_port_info *ppi[PDC_MAX_PORTS];
  1005. struct ata_host *host;
  1006. void __iomem *host_mmio;
  1007. int n_ports, i, rc;
  1008. int is_sataii_tx4;
  1009. if (!printed_version++)
  1010. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1011. /* enable and acquire resources */
  1012. rc = pcim_enable_device(pdev);
  1013. if (rc)
  1014. return rc;
  1015. rc = pcim_iomap_regions(pdev, 1 << PDC_MMIO_BAR, DRV_NAME);
  1016. if (rc == -EBUSY)
  1017. pcim_pin_device(pdev);
  1018. if (rc)
  1019. return rc;
  1020. host_mmio = pcim_iomap_table(pdev)[PDC_MMIO_BAR];
  1021. /* determine port configuration and setup host */
  1022. n_ports = 2;
  1023. if (pi->flags & PDC_FLAG_4_PORTS)
  1024. n_ports = 4;
  1025. for (i = 0; i < n_ports; i++)
  1026. ppi[i] = pi;
  1027. if (pi->flags & PDC_FLAG_SATA_PATA) {
  1028. u8 tmp = readb(host_mmio + PDC_FLASH_CTL + 1);
  1029. if (!(tmp & 0x80))
  1030. ppi[n_ports++] = pi + 1;
  1031. }
  1032. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  1033. if (!host) {
  1034. dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n");
  1035. return -ENOMEM;
  1036. }
  1037. host->iomap = pcim_iomap_table(pdev);
  1038. is_sataii_tx4 = pdc_is_sataii_tx4(pi->flags);
  1039. for (i = 0; i < host->n_ports; i++) {
  1040. struct ata_port *ap = host->ports[i];
  1041. unsigned int ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  1042. unsigned int ata_offset = 0x200 + ata_no * 0x80;
  1043. unsigned int scr_offset = 0x400 + ata_no * 0x100;
  1044. pdc_ata_setup_port(ap, host_mmio + ata_offset, host_mmio + scr_offset);
  1045. ata_port_pbar_desc(ap, PDC_MMIO_BAR, -1, "mmio");
  1046. ata_port_pbar_desc(ap, PDC_MMIO_BAR, ata_offset, "ata");
  1047. }
  1048. /* initialize adapter */
  1049. pdc_host_init(host);
  1050. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  1051. if (rc)
  1052. return rc;
  1053. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  1054. if (rc)
  1055. return rc;
  1056. /* start host, request IRQ and attach */
  1057. pci_set_master(pdev);
  1058. return ata_host_activate(host, pdev->irq, pdc_interrupt, IRQF_SHARED,
  1059. &pdc_ata_sht);
  1060. }
  1061. static int __init pdc_ata_init(void)
  1062. {
  1063. return pci_register_driver(&pdc_ata_pci_driver);
  1064. }
  1065. static void __exit pdc_ata_exit(void)
  1066. {
  1067. pci_unregister_driver(&pdc_ata_pci_driver);
  1068. }
  1069. MODULE_AUTHOR("Jeff Garzik");
  1070. MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
  1071. MODULE_LICENSE("GPL");
  1072. MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
  1073. MODULE_VERSION(DRV_VERSION);
  1074. module_init(pdc_ata_init);
  1075. module_exit(pdc_ata_exit);