sata_promise.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * sata_promise.c - Promise SATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc.
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. *
  26. * libata documentation is available via 'make {ps|pdf}docs',
  27. * as Documentation/DocBook/libata.*
  28. *
  29. * Hardware information only available under NDA.
  30. *
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/pci.h>
  35. #include <linux/init.h>
  36. #include <linux/blkdev.h>
  37. #include <linux/delay.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/device.h>
  40. #include <scsi/scsi.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <linux/libata.h>
  44. #include "sata_promise.h"
  45. #define DRV_NAME "sata_promise"
  46. #define DRV_VERSION "2.05"
  47. enum {
  48. PDC_MAX_PORTS = 4,
  49. PDC_MMIO_BAR = 3,
  50. /* register offsets */
  51. PDC_FEATURE = 0x04, /* Feature/Error reg (per port) */
  52. PDC_SECTOR_COUNT = 0x08, /* Sector count reg (per port) */
  53. PDC_SECTOR_NUMBER = 0x0C, /* Sector number reg (per port) */
  54. PDC_CYLINDER_LOW = 0x10, /* Cylinder low reg (per port) */
  55. PDC_CYLINDER_HIGH = 0x14, /* Cylinder high reg (per port) */
  56. PDC_DEVICE = 0x18, /* Device/Head reg (per port) */
  57. PDC_COMMAND = 0x1C, /* Command/status reg (per port) */
  58. PDC_ALTSTATUS = 0x38, /* Alternate-status/device-control reg (per port) */
  59. PDC_PKT_SUBMIT = 0x40, /* Command packet pointer addr */
  60. PDC_INT_SEQMASK = 0x40, /* Mask of asserted SEQ INTs */
  61. PDC_FLASH_CTL = 0x44, /* Flash control register */
  62. PDC_GLOBAL_CTL = 0x48, /* Global control/status (per port) */
  63. PDC_CTLSTAT = 0x60, /* IDE control and status (per port) */
  64. PDC_SATA_PLUG_CSR = 0x6C, /* SATA Plug control/status reg */
  65. PDC2_SATA_PLUG_CSR = 0x60, /* SATAII Plug control/status reg */
  66. PDC_TBG_MODE = 0x41C, /* TBG mode (not SATAII) */
  67. PDC_SLEW_CTL = 0x470, /* slew rate control reg (not SATAII) */
  68. /* PDC_GLOBAL_CTL bit definitions */
  69. PDC_PH_ERR = (1 << 8), /* PCI error while loading packet */
  70. PDC_SH_ERR = (1 << 9), /* PCI error while loading S/G table */
  71. PDC_DH_ERR = (1 << 10), /* PCI error while loading data */
  72. PDC2_HTO_ERR = (1 << 12), /* host bus timeout */
  73. PDC2_ATA_HBA_ERR = (1 << 13), /* error during SATA DATA FIS transmission */
  74. PDC2_ATA_DMA_CNT_ERR = (1 << 14), /* DMA DATA FIS size differs from S/G count */
  75. PDC_OVERRUN_ERR = (1 << 19), /* S/G byte count larger than HD requires */
  76. PDC_UNDERRUN_ERR = (1 << 20), /* S/G byte count less than HD requires */
  77. PDC_DRIVE_ERR = (1 << 21), /* drive error */
  78. PDC_PCI_SYS_ERR = (1 << 22), /* PCI system error */
  79. PDC1_PCI_PARITY_ERR = (1 << 23), /* PCI parity error (from SATA150 driver) */
  80. PDC1_ERR_MASK = PDC1_PCI_PARITY_ERR,
  81. PDC2_ERR_MASK = PDC2_HTO_ERR | PDC2_ATA_HBA_ERR | PDC2_ATA_DMA_CNT_ERR,
  82. PDC_ERR_MASK = (PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR | PDC_OVERRUN_ERR
  83. | PDC_UNDERRUN_ERR | PDC_DRIVE_ERR | PDC_PCI_SYS_ERR
  84. | PDC1_ERR_MASK | PDC2_ERR_MASK),
  85. board_2037x = 0, /* FastTrak S150 TX2plus */
  86. board_2037x_pata = 1, /* FastTrak S150 TX2plus PATA port */
  87. board_20319 = 2, /* FastTrak S150 TX4 */
  88. board_20619 = 3, /* FastTrak TX4000 */
  89. board_2057x = 4, /* SATAII150 Tx2plus */
  90. board_2057x_pata = 5, /* SATAII150 Tx2plus */
  91. board_40518 = 6, /* SATAII150 Tx4 */
  92. PDC_HAS_PATA = (1 << 1), /* PDC20375/20575 has PATA */
  93. /* Sequence counter control registers bit definitions */
  94. PDC_SEQCNTRL_INT_MASK = (1 << 5), /* Sequence Interrupt Mask */
  95. /* Feature register values */
  96. PDC_FEATURE_ATAPI_PIO = 0x00, /* ATAPI data xfer by PIO */
  97. PDC_FEATURE_ATAPI_DMA = 0x01, /* ATAPI data xfer by DMA */
  98. /* Device/Head register values */
  99. PDC_DEVICE_SATA = 0xE0, /* Device/Head value for SATA devices */
  100. /* PDC_CTLSTAT bit definitions */
  101. PDC_DMA_ENABLE = (1 << 7),
  102. PDC_IRQ_DISABLE = (1 << 10),
  103. PDC_RESET = (1 << 11), /* HDMA reset */
  104. PDC_COMMON_FLAGS = ATA_FLAG_NO_LEGACY |
  105. ATA_FLAG_MMIO |
  106. ATA_FLAG_PIO_POLLING,
  107. /* ap->flags bits */
  108. PDC_FLAG_GEN_II = (1 << 24),
  109. PDC_FLAG_SATA_PATA = (1 << 25), /* supports SATA + PATA */
  110. PDC_FLAG_4_PORTS = (1 << 26), /* 4 ports */
  111. };
  112. struct pdc_port_priv {
  113. u8 *pkt;
  114. dma_addr_t pkt_dma;
  115. };
  116. static u32 pdc_sata_scr_read (struct ata_port *ap, unsigned int sc_reg);
  117. static void pdc_sata_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  118. static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  119. static int pdc_common_port_start(struct ata_port *ap);
  120. static int pdc_sata_port_start(struct ata_port *ap);
  121. static void pdc_qc_prep(struct ata_queued_cmd *qc);
  122. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  123. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  124. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc);
  125. static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc);
  126. static void pdc_irq_clear(struct ata_port *ap);
  127. static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc);
  128. static void pdc_freeze(struct ata_port *ap);
  129. static void pdc_thaw(struct ata_port *ap);
  130. static void pdc_pata_error_handler(struct ata_port *ap);
  131. static void pdc_sata_error_handler(struct ata_port *ap);
  132. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc);
  133. static int pdc_pata_cable_detect(struct ata_port *ap);
  134. static int pdc_sata_cable_detect(struct ata_port *ap);
  135. static struct scsi_host_template pdc_ata_sht = {
  136. .module = THIS_MODULE,
  137. .name = DRV_NAME,
  138. .ioctl = ata_scsi_ioctl,
  139. .queuecommand = ata_scsi_queuecmd,
  140. .can_queue = ATA_DEF_QUEUE,
  141. .this_id = ATA_SHT_THIS_ID,
  142. .sg_tablesize = LIBATA_MAX_PRD,
  143. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  144. .emulated = ATA_SHT_EMULATED,
  145. .use_clustering = ATA_SHT_USE_CLUSTERING,
  146. .proc_name = DRV_NAME,
  147. .dma_boundary = ATA_DMA_BOUNDARY,
  148. .slave_configure = ata_scsi_slave_config,
  149. .slave_destroy = ata_scsi_slave_destroy,
  150. .bios_param = ata_std_bios_param,
  151. };
  152. static const struct ata_port_operations pdc_sata_ops = {
  153. .port_disable = ata_port_disable,
  154. .tf_load = pdc_tf_load_mmio,
  155. .tf_read = ata_tf_read,
  156. .check_status = ata_check_status,
  157. .exec_command = pdc_exec_command_mmio,
  158. .dev_select = ata_std_dev_select,
  159. .check_atapi_dma = pdc_check_atapi_dma,
  160. .qc_prep = pdc_qc_prep,
  161. .qc_issue = pdc_qc_issue_prot,
  162. .freeze = pdc_freeze,
  163. .thaw = pdc_thaw,
  164. .error_handler = pdc_sata_error_handler,
  165. .post_internal_cmd = pdc_post_internal_cmd,
  166. .cable_detect = pdc_sata_cable_detect,
  167. .data_xfer = ata_data_xfer,
  168. .irq_clear = pdc_irq_clear,
  169. .irq_on = ata_irq_on,
  170. .irq_ack = ata_irq_ack,
  171. .scr_read = pdc_sata_scr_read,
  172. .scr_write = pdc_sata_scr_write,
  173. .port_start = pdc_sata_port_start,
  174. };
  175. /* First-generation chips need a more restrictive ->check_atapi_dma op */
  176. static const struct ata_port_operations pdc_old_sata_ops = {
  177. .port_disable = ata_port_disable,
  178. .tf_load = pdc_tf_load_mmio,
  179. .tf_read = ata_tf_read,
  180. .check_status = ata_check_status,
  181. .exec_command = pdc_exec_command_mmio,
  182. .dev_select = ata_std_dev_select,
  183. .check_atapi_dma = pdc_old_sata_check_atapi_dma,
  184. .qc_prep = pdc_qc_prep,
  185. .qc_issue = pdc_qc_issue_prot,
  186. .freeze = pdc_freeze,
  187. .thaw = pdc_thaw,
  188. .error_handler = pdc_sata_error_handler,
  189. .post_internal_cmd = pdc_post_internal_cmd,
  190. .cable_detect = pdc_sata_cable_detect,
  191. .data_xfer = ata_data_xfer,
  192. .irq_clear = pdc_irq_clear,
  193. .irq_on = ata_irq_on,
  194. .irq_ack = ata_irq_ack,
  195. .scr_read = pdc_sata_scr_read,
  196. .scr_write = pdc_sata_scr_write,
  197. .port_start = pdc_sata_port_start,
  198. };
  199. static const struct ata_port_operations pdc_pata_ops = {
  200. .port_disable = ata_port_disable,
  201. .tf_load = pdc_tf_load_mmio,
  202. .tf_read = ata_tf_read,
  203. .check_status = ata_check_status,
  204. .exec_command = pdc_exec_command_mmio,
  205. .dev_select = ata_std_dev_select,
  206. .check_atapi_dma = pdc_check_atapi_dma,
  207. .qc_prep = pdc_qc_prep,
  208. .qc_issue = pdc_qc_issue_prot,
  209. .freeze = pdc_freeze,
  210. .thaw = pdc_thaw,
  211. .error_handler = pdc_pata_error_handler,
  212. .post_internal_cmd = pdc_post_internal_cmd,
  213. .cable_detect = pdc_pata_cable_detect,
  214. .data_xfer = ata_data_xfer,
  215. .irq_clear = pdc_irq_clear,
  216. .irq_on = ata_irq_on,
  217. .irq_ack = ata_irq_ack,
  218. .port_start = pdc_common_port_start,
  219. };
  220. static const struct ata_port_info pdc_port_info[] = {
  221. /* board_2037x */
  222. {
  223. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  224. PDC_FLAG_SATA_PATA,
  225. .pio_mask = 0x1f, /* pio0-4 */
  226. .mwdma_mask = 0x07, /* mwdma0-2 */
  227. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  228. .port_ops = &pdc_old_sata_ops,
  229. },
  230. /* board_2037x_pata */
  231. {
  232. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS,
  233. .pio_mask = 0x1f, /* pio0-4 */
  234. .mwdma_mask = 0x07, /* mwdma0-2 */
  235. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  236. .port_ops = &pdc_pata_ops,
  237. },
  238. /* board_20319 */
  239. {
  240. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  241. PDC_FLAG_4_PORTS,
  242. .pio_mask = 0x1f, /* pio0-4 */
  243. .mwdma_mask = 0x07, /* mwdma0-2 */
  244. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  245. .port_ops = &pdc_old_sata_ops,
  246. },
  247. /* board_20619 */
  248. {
  249. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS |
  250. PDC_FLAG_4_PORTS,
  251. .pio_mask = 0x1f, /* pio0-4 */
  252. .mwdma_mask = 0x07, /* mwdma0-2 */
  253. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  254. .port_ops = &pdc_pata_ops,
  255. },
  256. /* board_2057x */
  257. {
  258. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  259. PDC_FLAG_GEN_II | PDC_FLAG_SATA_PATA,
  260. .pio_mask = 0x1f, /* pio0-4 */
  261. .mwdma_mask = 0x07, /* mwdma0-2 */
  262. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  263. .port_ops = &pdc_sata_ops,
  264. },
  265. /* board_2057x_pata */
  266. {
  267. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS,
  268. PDC_FLAG_GEN_II,
  269. .pio_mask = 0x1f, /* pio0-4 */
  270. .mwdma_mask = 0x07, /* mwdma0-2 */
  271. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  272. .port_ops = &pdc_pata_ops,
  273. },
  274. /* board_40518 */
  275. {
  276. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  277. PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS,
  278. .pio_mask = 0x1f, /* pio0-4 */
  279. .mwdma_mask = 0x07, /* mwdma0-2 */
  280. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  281. .port_ops = &pdc_sata_ops,
  282. },
  283. };
  284. static const struct pci_device_id pdc_ata_pci_tbl[] = {
  285. { PCI_VDEVICE(PROMISE, 0x3371), board_2037x },
  286. { PCI_VDEVICE(PROMISE, 0x3373), board_2037x },
  287. { PCI_VDEVICE(PROMISE, 0x3375), board_2037x },
  288. { PCI_VDEVICE(PROMISE, 0x3376), board_2037x },
  289. { PCI_VDEVICE(PROMISE, 0x3570), board_2057x },
  290. { PCI_VDEVICE(PROMISE, 0x3571), board_2057x },
  291. { PCI_VDEVICE(PROMISE, 0x3574), board_2057x },
  292. { PCI_VDEVICE(PROMISE, 0x3577), board_2057x },
  293. { PCI_VDEVICE(PROMISE, 0x3d73), board_2057x },
  294. { PCI_VDEVICE(PROMISE, 0x3d75), board_2057x },
  295. { PCI_VDEVICE(PROMISE, 0x3318), board_20319 },
  296. { PCI_VDEVICE(PROMISE, 0x3319), board_20319 },
  297. { PCI_VDEVICE(PROMISE, 0x3515), board_20319 },
  298. { PCI_VDEVICE(PROMISE, 0x3519), board_20319 },
  299. { PCI_VDEVICE(PROMISE, 0x3d17), board_40518 },
  300. { PCI_VDEVICE(PROMISE, 0x3d18), board_40518 },
  301. { PCI_VDEVICE(PROMISE, 0x6629), board_20619 },
  302. { } /* terminate list */
  303. };
  304. static struct pci_driver pdc_ata_pci_driver = {
  305. .name = DRV_NAME,
  306. .id_table = pdc_ata_pci_tbl,
  307. .probe = pdc_ata_init_one,
  308. .remove = ata_pci_remove_one,
  309. };
  310. static int pdc_common_port_start(struct ata_port *ap)
  311. {
  312. struct device *dev = ap->host->dev;
  313. struct pdc_port_priv *pp;
  314. int rc;
  315. rc = ata_port_start(ap);
  316. if (rc)
  317. return rc;
  318. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  319. if (!pp)
  320. return -ENOMEM;
  321. pp->pkt = dmam_alloc_coherent(dev, 128, &pp->pkt_dma, GFP_KERNEL);
  322. if (!pp->pkt)
  323. return -ENOMEM;
  324. ap->private_data = pp;
  325. return 0;
  326. }
  327. static int pdc_sata_port_start(struct ata_port *ap)
  328. {
  329. int rc;
  330. rc = pdc_common_port_start(ap);
  331. if (rc)
  332. return rc;
  333. /* fix up PHYMODE4 align timing */
  334. if (ap->flags & PDC_FLAG_GEN_II) {
  335. void __iomem *mmio = (void __iomem *) ap->ioaddr.scr_addr;
  336. unsigned int tmp;
  337. tmp = readl(mmio + 0x014);
  338. tmp = (tmp & ~3) | 1; /* set bits 1:0 = 0:1 */
  339. writel(tmp, mmio + 0x014);
  340. }
  341. return 0;
  342. }
  343. static void pdc_reset_port(struct ata_port *ap)
  344. {
  345. void __iomem *mmio = ap->ioaddr.cmd_addr + PDC_CTLSTAT;
  346. unsigned int i;
  347. u32 tmp;
  348. for (i = 11; i > 0; i--) {
  349. tmp = readl(mmio);
  350. if (tmp & PDC_RESET)
  351. break;
  352. udelay(100);
  353. tmp |= PDC_RESET;
  354. writel(tmp, mmio);
  355. }
  356. tmp &= ~PDC_RESET;
  357. writel(tmp, mmio);
  358. readl(mmio); /* flush */
  359. }
  360. static int pdc_pata_cable_detect(struct ata_port *ap)
  361. {
  362. u8 tmp;
  363. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr + PDC_CTLSTAT + 0x03;
  364. tmp = readb(mmio);
  365. if (tmp & 0x01)
  366. return ATA_CBL_PATA40;
  367. return ATA_CBL_PATA80;
  368. }
  369. static int pdc_sata_cable_detect(struct ata_port *ap)
  370. {
  371. return ATA_CBL_SATA;
  372. }
  373. static u32 pdc_sata_scr_read (struct ata_port *ap, unsigned int sc_reg)
  374. {
  375. if (sc_reg > SCR_CONTROL)
  376. return 0xffffffffU;
  377. return readl(ap->ioaddr.scr_addr + (sc_reg * 4));
  378. }
  379. static void pdc_sata_scr_write (struct ata_port *ap, unsigned int sc_reg,
  380. u32 val)
  381. {
  382. if (sc_reg > SCR_CONTROL)
  383. return;
  384. writel(val, ap->ioaddr.scr_addr + (sc_reg * 4));
  385. }
  386. static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
  387. {
  388. struct ata_port *ap = qc->ap;
  389. dma_addr_t sg_table = ap->prd_dma;
  390. unsigned int cdb_len = qc->dev->cdb_len;
  391. u8 *cdb = qc->cdb;
  392. struct pdc_port_priv *pp = ap->private_data;
  393. u8 *buf = pp->pkt;
  394. u32 *buf32 = (u32 *) buf;
  395. unsigned int dev_sel, feature, nbytes;
  396. /* set control bits (byte 0), zero delay seq id (byte 3),
  397. * and seq id (byte 2)
  398. */
  399. switch (qc->tf.protocol) {
  400. case ATA_PROT_ATAPI_DMA:
  401. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  402. buf32[0] = cpu_to_le32(PDC_PKT_READ);
  403. else
  404. buf32[0] = 0;
  405. break;
  406. case ATA_PROT_ATAPI_NODATA:
  407. buf32[0] = cpu_to_le32(PDC_PKT_NODATA);
  408. break;
  409. default:
  410. BUG();
  411. break;
  412. }
  413. buf32[1] = cpu_to_le32(sg_table); /* S/G table addr */
  414. buf32[2] = 0; /* no next-packet */
  415. /* select drive */
  416. if (sata_scr_valid(ap)) {
  417. dev_sel = PDC_DEVICE_SATA;
  418. } else {
  419. dev_sel = ATA_DEVICE_OBS;
  420. if (qc->dev->devno != 0)
  421. dev_sel |= ATA_DEV1;
  422. }
  423. buf[12] = (1 << 5) | ATA_REG_DEVICE;
  424. buf[13] = dev_sel;
  425. buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY;
  426. buf[15] = dev_sel; /* once more, waiting for BSY to clear */
  427. buf[16] = (1 << 5) | ATA_REG_NSECT;
  428. buf[17] = 0x00;
  429. buf[18] = (1 << 5) | ATA_REG_LBAL;
  430. buf[19] = 0x00;
  431. /* set feature and byte counter registers */
  432. if (qc->tf.protocol != ATA_PROT_ATAPI_DMA) {
  433. feature = PDC_FEATURE_ATAPI_PIO;
  434. /* set byte counter register to real transfer byte count */
  435. nbytes = qc->nbytes;
  436. if (nbytes > 0xffff)
  437. nbytes = 0xffff;
  438. } else {
  439. feature = PDC_FEATURE_ATAPI_DMA;
  440. /* set byte counter register to 0 */
  441. nbytes = 0;
  442. }
  443. buf[20] = (1 << 5) | ATA_REG_FEATURE;
  444. buf[21] = feature;
  445. buf[22] = (1 << 5) | ATA_REG_BYTEL;
  446. buf[23] = nbytes & 0xFF;
  447. buf[24] = (1 << 5) | ATA_REG_BYTEH;
  448. buf[25] = (nbytes >> 8) & 0xFF;
  449. /* send ATAPI packet command 0xA0 */
  450. buf[26] = (1 << 5) | ATA_REG_CMD;
  451. buf[27] = ATA_CMD_PACKET;
  452. /* select drive and check DRQ */
  453. buf[28] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_WAIT_DRDY;
  454. buf[29] = dev_sel;
  455. /* we can represent cdb lengths 2/4/6/8/10/12/14/16 */
  456. BUG_ON(cdb_len & ~0x1E);
  457. /* append the CDB as the final part */
  458. buf[30] = (((cdb_len >> 1) & 7) << 5) | ATA_REG_DATA | PDC_LAST_REG;
  459. memcpy(buf+31, cdb, cdb_len);
  460. }
  461. static void pdc_qc_prep(struct ata_queued_cmd *qc)
  462. {
  463. struct pdc_port_priv *pp = qc->ap->private_data;
  464. unsigned int i;
  465. VPRINTK("ENTER\n");
  466. switch (qc->tf.protocol) {
  467. case ATA_PROT_DMA:
  468. ata_qc_prep(qc);
  469. /* fall through */
  470. case ATA_PROT_NODATA:
  471. i = pdc_pkt_header(&qc->tf, qc->ap->prd_dma,
  472. qc->dev->devno, pp->pkt);
  473. if (qc->tf.flags & ATA_TFLAG_LBA48)
  474. i = pdc_prep_lba48(&qc->tf, pp->pkt, i);
  475. else
  476. i = pdc_prep_lba28(&qc->tf, pp->pkt, i);
  477. pdc_pkt_footer(&qc->tf, pp->pkt, i);
  478. break;
  479. case ATA_PROT_ATAPI:
  480. ata_qc_prep(qc);
  481. break;
  482. case ATA_PROT_ATAPI_DMA:
  483. ata_qc_prep(qc);
  484. /*FALLTHROUGH*/
  485. case ATA_PROT_ATAPI_NODATA:
  486. pdc_atapi_pkt(qc);
  487. break;
  488. default:
  489. break;
  490. }
  491. }
  492. static void pdc_freeze(struct ata_port *ap)
  493. {
  494. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  495. u32 tmp;
  496. tmp = readl(mmio + PDC_CTLSTAT);
  497. tmp |= PDC_IRQ_DISABLE;
  498. tmp &= ~PDC_DMA_ENABLE;
  499. writel(tmp, mmio + PDC_CTLSTAT);
  500. readl(mmio + PDC_CTLSTAT); /* flush */
  501. }
  502. static void pdc_thaw(struct ata_port *ap)
  503. {
  504. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  505. u32 tmp;
  506. /* clear IRQ */
  507. readl(mmio + PDC_INT_SEQMASK);
  508. /* turn IRQ back on */
  509. tmp = readl(mmio + PDC_CTLSTAT);
  510. tmp &= ~PDC_IRQ_DISABLE;
  511. writel(tmp, mmio + PDC_CTLSTAT);
  512. readl(mmio + PDC_CTLSTAT); /* flush */
  513. }
  514. static void pdc_common_error_handler(struct ata_port *ap, ata_reset_fn_t hardreset)
  515. {
  516. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  517. pdc_reset_port(ap);
  518. /* perform recovery */
  519. ata_do_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  520. ata_std_postreset);
  521. }
  522. static void pdc_pata_error_handler(struct ata_port *ap)
  523. {
  524. pdc_common_error_handler(ap, NULL);
  525. }
  526. static void pdc_sata_error_handler(struct ata_port *ap)
  527. {
  528. pdc_common_error_handler(ap, sata_std_hardreset);
  529. }
  530. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc)
  531. {
  532. struct ata_port *ap = qc->ap;
  533. /* make DMA engine forget about the failed command */
  534. if (qc->flags & ATA_QCFLAG_FAILED)
  535. pdc_reset_port(ap);
  536. }
  537. static void pdc_error_intr(struct ata_port *ap, struct ata_queued_cmd *qc,
  538. u32 port_status, u32 err_mask)
  539. {
  540. struct ata_eh_info *ehi = &ap->eh_info;
  541. unsigned int ac_err_mask = 0;
  542. ata_ehi_clear_desc(ehi);
  543. ata_ehi_push_desc(ehi, "port_status 0x%08x", port_status);
  544. port_status &= err_mask;
  545. if (port_status & PDC_DRIVE_ERR)
  546. ac_err_mask |= AC_ERR_DEV;
  547. if (port_status & (PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR))
  548. ac_err_mask |= AC_ERR_HSM;
  549. if (port_status & (PDC2_ATA_HBA_ERR | PDC2_ATA_DMA_CNT_ERR))
  550. ac_err_mask |= AC_ERR_ATA_BUS;
  551. if (port_status & (PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR | PDC2_HTO_ERR
  552. | PDC_PCI_SYS_ERR | PDC1_PCI_PARITY_ERR))
  553. ac_err_mask |= AC_ERR_HOST_BUS;
  554. if (sata_scr_valid(ap))
  555. ehi->serror |= pdc_sata_scr_read(ap, SCR_ERROR);
  556. qc->err_mask |= ac_err_mask;
  557. pdc_reset_port(ap);
  558. }
  559. static inline unsigned int pdc_host_intr( struct ata_port *ap,
  560. struct ata_queued_cmd *qc)
  561. {
  562. unsigned int handled = 0;
  563. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  564. u32 port_status, err_mask;
  565. err_mask = PDC_ERR_MASK;
  566. if (ap->flags & PDC_FLAG_GEN_II)
  567. err_mask &= ~PDC1_ERR_MASK;
  568. else
  569. err_mask &= ~PDC2_ERR_MASK;
  570. port_status = readl(port_mmio + PDC_GLOBAL_CTL);
  571. if (unlikely(port_status & err_mask)) {
  572. pdc_error_intr(ap, qc, port_status, err_mask);
  573. return 1;
  574. }
  575. switch (qc->tf.protocol) {
  576. case ATA_PROT_DMA:
  577. case ATA_PROT_NODATA:
  578. case ATA_PROT_ATAPI_DMA:
  579. case ATA_PROT_ATAPI_NODATA:
  580. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  581. ata_qc_complete(qc);
  582. handled = 1;
  583. break;
  584. default:
  585. ap->stats.idle_irq++;
  586. break;
  587. }
  588. return handled;
  589. }
  590. static void pdc_irq_clear(struct ata_port *ap)
  591. {
  592. struct ata_host *host = ap->host;
  593. void __iomem *mmio = host->iomap[PDC_MMIO_BAR];
  594. readl(mmio + PDC_INT_SEQMASK);
  595. }
  596. static irqreturn_t pdc_interrupt (int irq, void *dev_instance)
  597. {
  598. struct ata_host *host = dev_instance;
  599. struct ata_port *ap;
  600. u32 mask = 0;
  601. unsigned int i, tmp;
  602. unsigned int handled = 0;
  603. void __iomem *mmio_base;
  604. VPRINTK("ENTER\n");
  605. if (!host || !host->iomap[PDC_MMIO_BAR]) {
  606. VPRINTK("QUICK EXIT\n");
  607. return IRQ_NONE;
  608. }
  609. mmio_base = host->iomap[PDC_MMIO_BAR];
  610. /* reading should also clear interrupts */
  611. mask = readl(mmio_base + PDC_INT_SEQMASK);
  612. if (mask == 0xffffffff) {
  613. VPRINTK("QUICK EXIT 2\n");
  614. return IRQ_NONE;
  615. }
  616. spin_lock(&host->lock);
  617. mask &= 0xffff; /* only 16 tags possible */
  618. if (!mask) {
  619. VPRINTK("QUICK EXIT 3\n");
  620. goto done_irq;
  621. }
  622. writel(mask, mmio_base + PDC_INT_SEQMASK);
  623. for (i = 0; i < host->n_ports; i++) {
  624. VPRINTK("port %u\n", i);
  625. ap = host->ports[i];
  626. tmp = mask & (1 << (i + 1));
  627. if (tmp && ap &&
  628. !(ap->flags & ATA_FLAG_DISABLED)) {
  629. struct ata_queued_cmd *qc;
  630. qc = ata_qc_from_tag(ap, ap->active_tag);
  631. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  632. handled += pdc_host_intr(ap, qc);
  633. }
  634. }
  635. VPRINTK("EXIT\n");
  636. done_irq:
  637. spin_unlock(&host->lock);
  638. return IRQ_RETVAL(handled);
  639. }
  640. static inline void pdc_packet_start(struct ata_queued_cmd *qc)
  641. {
  642. struct ata_port *ap = qc->ap;
  643. struct pdc_port_priv *pp = ap->private_data;
  644. void __iomem *mmio = ap->host->iomap[PDC_MMIO_BAR];
  645. unsigned int port_no = ap->port_no;
  646. u8 seq = (u8) (port_no + 1);
  647. VPRINTK("ENTER, ap %p\n", ap);
  648. writel(0x00000001, mmio + (seq * 4));
  649. readl(mmio + (seq * 4)); /* flush */
  650. pp->pkt[2] = seq;
  651. wmb(); /* flush PRD, pkt writes */
  652. writel(pp->pkt_dma, ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  653. readl(ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT); /* flush */
  654. }
  655. static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
  656. {
  657. switch (qc->tf.protocol) {
  658. case ATA_PROT_ATAPI_NODATA:
  659. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  660. break;
  661. /*FALLTHROUGH*/
  662. case ATA_PROT_ATAPI_DMA:
  663. case ATA_PROT_DMA:
  664. case ATA_PROT_NODATA:
  665. pdc_packet_start(qc);
  666. return 0;
  667. default:
  668. break;
  669. }
  670. return ata_qc_issue_prot(qc);
  671. }
  672. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  673. {
  674. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  675. tf->protocol == ATA_PROT_NODATA);
  676. ata_tf_load(ap, tf);
  677. }
  678. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  679. {
  680. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  681. tf->protocol == ATA_PROT_NODATA);
  682. ata_exec_command(ap, tf);
  683. }
  684. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc)
  685. {
  686. u8 *scsicmd = qc->scsicmd->cmnd;
  687. int pio = 1; /* atapi dma off by default */
  688. /* Whitelist commands that may use DMA. */
  689. switch (scsicmd[0]) {
  690. case WRITE_12:
  691. case WRITE_10:
  692. case WRITE_6:
  693. case READ_12:
  694. case READ_10:
  695. case READ_6:
  696. case 0xad: /* READ_DVD_STRUCTURE */
  697. case 0xbe: /* READ_CD */
  698. pio = 0;
  699. }
  700. /* -45150 (FFFF4FA2) to -1 (FFFFFFFF) shall use PIO mode */
  701. if (scsicmd[0] == WRITE_10) {
  702. unsigned int lba;
  703. lba = (scsicmd[2] << 24) | (scsicmd[3] << 16) | (scsicmd[4] << 8) | scsicmd[5];
  704. if (lba >= 0xFFFF4FA2)
  705. pio = 1;
  706. }
  707. return pio;
  708. }
  709. static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc)
  710. {
  711. /* First generation chips cannot use ATAPI DMA on SATA ports */
  712. return 1;
  713. }
  714. static void pdc_ata_setup_port(struct ata_port *ap,
  715. void __iomem *base, void __iomem *scr_addr)
  716. {
  717. ap->ioaddr.cmd_addr = base;
  718. ap->ioaddr.data_addr = base;
  719. ap->ioaddr.feature_addr =
  720. ap->ioaddr.error_addr = base + 0x4;
  721. ap->ioaddr.nsect_addr = base + 0x8;
  722. ap->ioaddr.lbal_addr = base + 0xc;
  723. ap->ioaddr.lbam_addr = base + 0x10;
  724. ap->ioaddr.lbah_addr = base + 0x14;
  725. ap->ioaddr.device_addr = base + 0x18;
  726. ap->ioaddr.command_addr =
  727. ap->ioaddr.status_addr = base + 0x1c;
  728. ap->ioaddr.altstatus_addr =
  729. ap->ioaddr.ctl_addr = base + 0x38;
  730. ap->ioaddr.scr_addr = scr_addr;
  731. }
  732. static void pdc_host_init(struct ata_host *host)
  733. {
  734. void __iomem *mmio = host->iomap[PDC_MMIO_BAR];
  735. int is_gen2 = host->ports[0]->flags & PDC_FLAG_GEN_II;
  736. int hotplug_offset;
  737. u32 tmp;
  738. if (is_gen2)
  739. hotplug_offset = PDC2_SATA_PLUG_CSR;
  740. else
  741. hotplug_offset = PDC_SATA_PLUG_CSR;
  742. /*
  743. * Except for the hotplug stuff, this is voodoo from the
  744. * Promise driver. Label this entire section
  745. * "TODO: figure out why we do this"
  746. */
  747. /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
  748. tmp = readl(mmio + PDC_FLASH_CTL);
  749. tmp |= 0x02000; /* bit 13 (enable bmr burst) */
  750. if (!is_gen2)
  751. tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
  752. writel(tmp, mmio + PDC_FLASH_CTL);
  753. /* clear plug/unplug flags for all ports */
  754. tmp = readl(mmio + hotplug_offset);
  755. writel(tmp | 0xff, mmio + hotplug_offset);
  756. /* mask plug/unplug ints */
  757. tmp = readl(mmio + hotplug_offset);
  758. writel(tmp | 0xff0000, mmio + hotplug_offset);
  759. /* don't initialise TBG or SLEW on 2nd generation chips */
  760. if (is_gen2)
  761. return;
  762. /* reduce TBG clock to 133 Mhz. */
  763. tmp = readl(mmio + PDC_TBG_MODE);
  764. tmp &= ~0x30000; /* clear bit 17, 16*/
  765. tmp |= 0x10000; /* set bit 17:16 = 0:1 */
  766. writel(tmp, mmio + PDC_TBG_MODE);
  767. readl(mmio + PDC_TBG_MODE); /* flush */
  768. msleep(10);
  769. /* adjust slew rate control register. */
  770. tmp = readl(mmio + PDC_SLEW_CTL);
  771. tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
  772. tmp |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
  773. writel(tmp, mmio + PDC_SLEW_CTL);
  774. }
  775. static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  776. {
  777. static int printed_version;
  778. const struct ata_port_info *pi = &pdc_port_info[ent->driver_data];
  779. const struct ata_port_info *ppi[PDC_MAX_PORTS];
  780. struct ata_host *host;
  781. void __iomem *base;
  782. int n_ports, i, rc;
  783. if (!printed_version++)
  784. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  785. /* enable and acquire resources */
  786. rc = pcim_enable_device(pdev);
  787. if (rc)
  788. return rc;
  789. rc = pcim_iomap_regions(pdev, 1 << PDC_MMIO_BAR, DRV_NAME);
  790. if (rc == -EBUSY)
  791. pcim_pin_device(pdev);
  792. if (rc)
  793. return rc;
  794. base = pcim_iomap_table(pdev)[PDC_MMIO_BAR];
  795. /* determine port configuration and setup host */
  796. n_ports = 2;
  797. if (pi->flags & PDC_FLAG_4_PORTS)
  798. n_ports = 4;
  799. for (i = 0; i < n_ports; i++)
  800. ppi[i] = pi;
  801. if (pi->flags & PDC_FLAG_SATA_PATA) {
  802. u8 tmp = readb(base + PDC_FLASH_CTL+1);
  803. if (!(tmp & 0x80)) {
  804. ppi[n_ports++] = pi + 1;
  805. dev_printk(KERN_INFO, &pdev->dev, "PATA port found\n");
  806. }
  807. }
  808. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  809. if (!host) {
  810. dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n");
  811. return -ENOMEM;
  812. }
  813. host->iomap = pcim_iomap_table(pdev);
  814. for (i = 0; i < host->n_ports; i++)
  815. pdc_ata_setup_port(host->ports[i],
  816. base + 0x200 + i * 0x80,
  817. base + 0x400 + i * 0x100);
  818. /* initialize adapter */
  819. pdc_host_init(host);
  820. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  821. if (rc)
  822. return rc;
  823. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  824. if (rc)
  825. return rc;
  826. /* start host, request IRQ and attach */
  827. pci_set_master(pdev);
  828. return ata_host_activate(host, pdev->irq, pdc_interrupt, IRQF_SHARED,
  829. &pdc_ata_sht);
  830. }
  831. static int __init pdc_ata_init(void)
  832. {
  833. return pci_register_driver(&pdc_ata_pci_driver);
  834. }
  835. static void __exit pdc_ata_exit(void)
  836. {
  837. pci_unregister_driver(&pdc_ata_pci_driver);
  838. }
  839. MODULE_AUTHOR("Jeff Garzik");
  840. MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
  841. MODULE_LICENSE("GPL");
  842. MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
  843. MODULE_VERSION(DRV_VERSION);
  844. module_init(pdc_ata_init);
  845. module_exit(pdc_ata_exit);