sata_promise.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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.09"
  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 PATA port */
  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 int pdc_sata_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val);
  117. static int 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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = ATA_UDMA6,
  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 = 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 = 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 int pdc_sata_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val)
  374. {
  375. if (sc_reg > SCR_CONTROL)
  376. return -EINVAL;
  377. *val = readl(ap->ioaddr.scr_addr + (sc_reg * 4));
  378. return 0;
  379. }
  380. static int pdc_sata_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
  381. {
  382. if (sc_reg > SCR_CONTROL)
  383. return -EINVAL;
  384. writel(val, ap->ioaddr.scr_addr + (sc_reg * 4));
  385. return 0;
  386. }
  387. static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
  388. {
  389. struct ata_port *ap = qc->ap;
  390. dma_addr_t sg_table = ap->prd_dma;
  391. unsigned int cdb_len = qc->dev->cdb_len;
  392. u8 *cdb = qc->cdb;
  393. struct pdc_port_priv *pp = ap->private_data;
  394. u8 *buf = pp->pkt;
  395. u32 *buf32 = (u32 *) buf;
  396. unsigned int dev_sel, feature, nbytes;
  397. /* set control bits (byte 0), zero delay seq id (byte 3),
  398. * and seq id (byte 2)
  399. */
  400. switch (qc->tf.protocol) {
  401. case ATA_PROT_ATAPI_DMA:
  402. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  403. buf32[0] = cpu_to_le32(PDC_PKT_READ);
  404. else
  405. buf32[0] = 0;
  406. break;
  407. case ATA_PROT_ATAPI_NODATA:
  408. buf32[0] = cpu_to_le32(PDC_PKT_NODATA);
  409. break;
  410. default:
  411. BUG();
  412. break;
  413. }
  414. buf32[1] = cpu_to_le32(sg_table); /* S/G table addr */
  415. buf32[2] = 0; /* no next-packet */
  416. /* select drive */
  417. if (sata_scr_valid(ap)) {
  418. dev_sel = PDC_DEVICE_SATA;
  419. } else {
  420. dev_sel = ATA_DEVICE_OBS;
  421. if (qc->dev->devno != 0)
  422. dev_sel |= ATA_DEV1;
  423. }
  424. buf[12] = (1 << 5) | ATA_REG_DEVICE;
  425. buf[13] = dev_sel;
  426. buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY;
  427. buf[15] = dev_sel; /* once more, waiting for BSY to clear */
  428. buf[16] = (1 << 5) | ATA_REG_NSECT;
  429. buf[17] = 0x00;
  430. buf[18] = (1 << 5) | ATA_REG_LBAL;
  431. buf[19] = 0x00;
  432. /* set feature and byte counter registers */
  433. if (qc->tf.protocol != ATA_PROT_ATAPI_DMA) {
  434. feature = PDC_FEATURE_ATAPI_PIO;
  435. /* set byte counter register to real transfer byte count */
  436. nbytes = qc->nbytes;
  437. if (nbytes > 0xffff)
  438. nbytes = 0xffff;
  439. } else {
  440. feature = PDC_FEATURE_ATAPI_DMA;
  441. /* set byte counter register to 0 */
  442. nbytes = 0;
  443. }
  444. buf[20] = (1 << 5) | ATA_REG_FEATURE;
  445. buf[21] = feature;
  446. buf[22] = (1 << 5) | ATA_REG_BYTEL;
  447. buf[23] = nbytes & 0xFF;
  448. buf[24] = (1 << 5) | ATA_REG_BYTEH;
  449. buf[25] = (nbytes >> 8) & 0xFF;
  450. /* send ATAPI packet command 0xA0 */
  451. buf[26] = (1 << 5) | ATA_REG_CMD;
  452. buf[27] = ATA_CMD_PACKET;
  453. /* select drive and check DRQ */
  454. buf[28] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_WAIT_DRDY;
  455. buf[29] = dev_sel;
  456. /* we can represent cdb lengths 2/4/6/8/10/12/14/16 */
  457. BUG_ON(cdb_len & ~0x1E);
  458. /* append the CDB as the final part */
  459. buf[30] = (((cdb_len >> 1) & 7) << 5) | ATA_REG_DATA | PDC_LAST_REG;
  460. memcpy(buf+31, cdb, cdb_len);
  461. }
  462. static void pdc_qc_prep(struct ata_queued_cmd *qc)
  463. {
  464. struct pdc_port_priv *pp = qc->ap->private_data;
  465. unsigned int i;
  466. VPRINTK("ENTER\n");
  467. switch (qc->tf.protocol) {
  468. case ATA_PROT_DMA:
  469. ata_qc_prep(qc);
  470. /* fall through */
  471. case ATA_PROT_NODATA:
  472. i = pdc_pkt_header(&qc->tf, qc->ap->prd_dma,
  473. qc->dev->devno, pp->pkt);
  474. if (qc->tf.flags & ATA_TFLAG_LBA48)
  475. i = pdc_prep_lba48(&qc->tf, pp->pkt, i);
  476. else
  477. i = pdc_prep_lba28(&qc->tf, pp->pkt, i);
  478. pdc_pkt_footer(&qc->tf, pp->pkt, i);
  479. break;
  480. case ATA_PROT_ATAPI:
  481. ata_qc_prep(qc);
  482. break;
  483. case ATA_PROT_ATAPI_DMA:
  484. ata_qc_prep(qc);
  485. /*FALLTHROUGH*/
  486. case ATA_PROT_ATAPI_NODATA:
  487. pdc_atapi_pkt(qc);
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. static void pdc_freeze(struct ata_port *ap)
  494. {
  495. void __iomem *mmio = ap->ioaddr.cmd_addr;
  496. u32 tmp;
  497. tmp = readl(mmio + PDC_CTLSTAT);
  498. tmp |= PDC_IRQ_DISABLE;
  499. tmp &= ~PDC_DMA_ENABLE;
  500. writel(tmp, mmio + PDC_CTLSTAT);
  501. readl(mmio + PDC_CTLSTAT); /* flush */
  502. }
  503. static void pdc_thaw(struct ata_port *ap)
  504. {
  505. void __iomem *mmio = ap->ioaddr.cmd_addr;
  506. u32 tmp;
  507. /* clear IRQ */
  508. readl(mmio + PDC_INT_SEQMASK);
  509. /* turn IRQ back on */
  510. tmp = readl(mmio + PDC_CTLSTAT);
  511. tmp &= ~PDC_IRQ_DISABLE;
  512. writel(tmp, mmio + PDC_CTLSTAT);
  513. readl(mmio + PDC_CTLSTAT); /* flush */
  514. }
  515. static void pdc_common_error_handler(struct ata_port *ap, ata_reset_fn_t hardreset)
  516. {
  517. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  518. pdc_reset_port(ap);
  519. /* perform recovery */
  520. ata_do_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  521. ata_std_postreset);
  522. }
  523. static void pdc_pata_error_handler(struct ata_port *ap)
  524. {
  525. pdc_common_error_handler(ap, NULL);
  526. }
  527. static void pdc_sata_error_handler(struct ata_port *ap)
  528. {
  529. pdc_common_error_handler(ap, sata_std_hardreset);
  530. }
  531. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc)
  532. {
  533. struct ata_port *ap = qc->ap;
  534. /* make DMA engine forget about the failed command */
  535. if (qc->flags & ATA_QCFLAG_FAILED)
  536. pdc_reset_port(ap);
  537. }
  538. static void pdc_error_intr(struct ata_port *ap, struct ata_queued_cmd *qc,
  539. u32 port_status, u32 err_mask)
  540. {
  541. struct ata_eh_info *ehi = &ap->eh_info;
  542. unsigned int ac_err_mask = 0;
  543. ata_ehi_clear_desc(ehi);
  544. ata_ehi_push_desc(ehi, "port_status 0x%08x", port_status);
  545. port_status &= err_mask;
  546. if (port_status & PDC_DRIVE_ERR)
  547. ac_err_mask |= AC_ERR_DEV;
  548. if (port_status & (PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR))
  549. ac_err_mask |= AC_ERR_HSM;
  550. if (port_status & (PDC2_ATA_HBA_ERR | PDC2_ATA_DMA_CNT_ERR))
  551. ac_err_mask |= AC_ERR_ATA_BUS;
  552. if (port_status & (PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR | PDC2_HTO_ERR
  553. | PDC_PCI_SYS_ERR | PDC1_PCI_PARITY_ERR))
  554. ac_err_mask |= AC_ERR_HOST_BUS;
  555. if (sata_scr_valid(ap)) {
  556. u32 serror;
  557. pdc_sata_scr_read(ap, SCR_ERROR, &serror);
  558. ehi->serror |= serror;
  559. }
  560. qc->err_mask |= ac_err_mask;
  561. pdc_reset_port(ap);
  562. ata_port_abort(ap);
  563. }
  564. static inline unsigned int pdc_host_intr(struct ata_port *ap,
  565. struct ata_queued_cmd *qc)
  566. {
  567. unsigned int handled = 0;
  568. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  569. u32 port_status, err_mask;
  570. err_mask = PDC_ERR_MASK;
  571. if (ap->flags & PDC_FLAG_GEN_II)
  572. err_mask &= ~PDC1_ERR_MASK;
  573. else
  574. err_mask &= ~PDC2_ERR_MASK;
  575. port_status = readl(port_mmio + PDC_GLOBAL_CTL);
  576. if (unlikely(port_status & err_mask)) {
  577. pdc_error_intr(ap, qc, port_status, err_mask);
  578. return 1;
  579. }
  580. switch (qc->tf.protocol) {
  581. case ATA_PROT_DMA:
  582. case ATA_PROT_NODATA:
  583. case ATA_PROT_ATAPI_DMA:
  584. case ATA_PROT_ATAPI_NODATA:
  585. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  586. ata_qc_complete(qc);
  587. handled = 1;
  588. break;
  589. default:
  590. ap->stats.idle_irq++;
  591. break;
  592. }
  593. return handled;
  594. }
  595. static void pdc_irq_clear(struct ata_port *ap)
  596. {
  597. struct ata_host *host = ap->host;
  598. void __iomem *mmio = host->iomap[PDC_MMIO_BAR];
  599. readl(mmio + PDC_INT_SEQMASK);
  600. }
  601. static inline int pdc_is_sataii_tx4(unsigned long flags)
  602. {
  603. const unsigned long mask = PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS;
  604. return (flags & mask) == mask;
  605. }
  606. static inline unsigned int pdc_port_no_to_ata_no(unsigned int port_no, int is_sataii_tx4)
  607. {
  608. static const unsigned char sataii_tx4_port_remap[4] = { 3, 1, 0, 2};
  609. return is_sataii_tx4 ? sataii_tx4_port_remap[port_no] : port_no;
  610. }
  611. static irqreturn_t pdc_interrupt (int irq, void *dev_instance)
  612. {
  613. struct ata_host *host = dev_instance;
  614. struct ata_port *ap;
  615. u32 mask = 0;
  616. unsigned int i, tmp;
  617. unsigned int handled = 0;
  618. void __iomem *mmio_base;
  619. unsigned int hotplug_offset, ata_no;
  620. u32 hotplug_status;
  621. int is_sataii_tx4;
  622. VPRINTK("ENTER\n");
  623. if (!host || !host->iomap[PDC_MMIO_BAR]) {
  624. VPRINTK("QUICK EXIT\n");
  625. return IRQ_NONE;
  626. }
  627. mmio_base = host->iomap[PDC_MMIO_BAR];
  628. /* read and clear hotplug flags for all ports */
  629. if (host->ports[0]->flags & PDC_FLAG_GEN_II)
  630. hotplug_offset = PDC2_SATA_PLUG_CSR;
  631. else
  632. hotplug_offset = PDC_SATA_PLUG_CSR;
  633. hotplug_status = readl(mmio_base + hotplug_offset);
  634. if (hotplug_status & 0xff)
  635. writel(hotplug_status | 0xff, mmio_base + hotplug_offset);
  636. hotplug_status &= 0xff; /* clear uninteresting bits */
  637. /* reading should also clear interrupts */
  638. mask = readl(mmio_base + PDC_INT_SEQMASK);
  639. if (mask == 0xffffffff && hotplug_status == 0) {
  640. VPRINTK("QUICK EXIT 2\n");
  641. return IRQ_NONE;
  642. }
  643. spin_lock(&host->lock);
  644. mask &= 0xffff; /* only 16 tags possible */
  645. if (mask == 0 && hotplug_status == 0) {
  646. VPRINTK("QUICK EXIT 3\n");
  647. goto done_irq;
  648. }
  649. writel(mask, mmio_base + PDC_INT_SEQMASK);
  650. is_sataii_tx4 = pdc_is_sataii_tx4(host->ports[0]->flags);
  651. for (i = 0; i < host->n_ports; i++) {
  652. VPRINTK("port %u\n", i);
  653. ap = host->ports[i];
  654. /* check for a plug or unplug event */
  655. ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  656. tmp = hotplug_status & (0x11 << ata_no);
  657. if (tmp && ap &&
  658. !(ap->flags & ATA_FLAG_DISABLED)) {
  659. struct ata_eh_info *ehi = &ap->eh_info;
  660. ata_ehi_clear_desc(ehi);
  661. ata_ehi_hotplugged(ehi);
  662. ata_ehi_push_desc(ehi, "hotplug_status %#x", tmp);
  663. ata_port_freeze(ap);
  664. ++handled;
  665. continue;
  666. }
  667. /* check for a packet interrupt */
  668. tmp = mask & (1 << (i + 1));
  669. if (tmp && ap &&
  670. !(ap->flags & ATA_FLAG_DISABLED)) {
  671. struct ata_queued_cmd *qc;
  672. qc = ata_qc_from_tag(ap, ap->active_tag);
  673. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  674. handled += pdc_host_intr(ap, qc);
  675. }
  676. }
  677. VPRINTK("EXIT\n");
  678. done_irq:
  679. spin_unlock(&host->lock);
  680. return IRQ_RETVAL(handled);
  681. }
  682. static inline void pdc_packet_start(struct ata_queued_cmd *qc)
  683. {
  684. struct ata_port *ap = qc->ap;
  685. struct pdc_port_priv *pp = ap->private_data;
  686. void __iomem *mmio = ap->host->iomap[PDC_MMIO_BAR];
  687. unsigned int port_no = ap->port_no;
  688. u8 seq = (u8) (port_no + 1);
  689. VPRINTK("ENTER, ap %p\n", ap);
  690. writel(0x00000001, mmio + (seq * 4));
  691. readl(mmio + (seq * 4)); /* flush */
  692. pp->pkt[2] = seq;
  693. wmb(); /* flush PRD, pkt writes */
  694. writel(pp->pkt_dma, ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  695. readl(ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT); /* flush */
  696. }
  697. static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
  698. {
  699. switch (qc->tf.protocol) {
  700. case ATA_PROT_ATAPI_NODATA:
  701. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  702. break;
  703. /*FALLTHROUGH*/
  704. case ATA_PROT_NODATA:
  705. if (qc->tf.flags & ATA_TFLAG_POLLING)
  706. break;
  707. /*FALLTHROUGH*/
  708. case ATA_PROT_ATAPI_DMA:
  709. case ATA_PROT_DMA:
  710. pdc_packet_start(qc);
  711. return 0;
  712. default:
  713. break;
  714. }
  715. return ata_qc_issue_prot(qc);
  716. }
  717. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  718. {
  719. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  720. tf->protocol == ATA_PROT_ATAPI_DMA);
  721. ata_tf_load(ap, tf);
  722. }
  723. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  724. {
  725. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  726. tf->protocol == ATA_PROT_ATAPI_DMA);
  727. ata_exec_command(ap, tf);
  728. }
  729. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc)
  730. {
  731. u8 *scsicmd = qc->scsicmd->cmnd;
  732. int pio = 1; /* atapi dma off by default */
  733. /* Whitelist commands that may use DMA. */
  734. switch (scsicmd[0]) {
  735. case WRITE_12:
  736. case WRITE_10:
  737. case WRITE_6:
  738. case READ_12:
  739. case READ_10:
  740. case READ_6:
  741. case 0xad: /* READ_DVD_STRUCTURE */
  742. case 0xbe: /* READ_CD */
  743. pio = 0;
  744. }
  745. /* -45150 (FFFF4FA2) to -1 (FFFFFFFF) shall use PIO mode */
  746. if (scsicmd[0] == WRITE_10) {
  747. unsigned int lba;
  748. lba = (scsicmd[2] << 24) | (scsicmd[3] << 16) | (scsicmd[4] << 8) | scsicmd[5];
  749. if (lba >= 0xFFFF4FA2)
  750. pio = 1;
  751. }
  752. return pio;
  753. }
  754. static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc)
  755. {
  756. /* First generation chips cannot use ATAPI DMA on SATA ports */
  757. return 1;
  758. }
  759. static void pdc_ata_setup_port(struct ata_port *ap,
  760. void __iomem *base, void __iomem *scr_addr)
  761. {
  762. ap->ioaddr.cmd_addr = base;
  763. ap->ioaddr.data_addr = base;
  764. ap->ioaddr.feature_addr =
  765. ap->ioaddr.error_addr = base + 0x4;
  766. ap->ioaddr.nsect_addr = base + 0x8;
  767. ap->ioaddr.lbal_addr = base + 0xc;
  768. ap->ioaddr.lbam_addr = base + 0x10;
  769. ap->ioaddr.lbah_addr = base + 0x14;
  770. ap->ioaddr.device_addr = base + 0x18;
  771. ap->ioaddr.command_addr =
  772. ap->ioaddr.status_addr = base + 0x1c;
  773. ap->ioaddr.altstatus_addr =
  774. ap->ioaddr.ctl_addr = base + 0x38;
  775. ap->ioaddr.scr_addr = scr_addr;
  776. }
  777. static void pdc_host_init(struct ata_host *host)
  778. {
  779. void __iomem *mmio = host->iomap[PDC_MMIO_BAR];
  780. int is_gen2 = host->ports[0]->flags & PDC_FLAG_GEN_II;
  781. int hotplug_offset;
  782. u32 tmp;
  783. if (is_gen2)
  784. hotplug_offset = PDC2_SATA_PLUG_CSR;
  785. else
  786. hotplug_offset = PDC_SATA_PLUG_CSR;
  787. /*
  788. * Except for the hotplug stuff, this is voodoo from the
  789. * Promise driver. Label this entire section
  790. * "TODO: figure out why we do this"
  791. */
  792. /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
  793. tmp = readl(mmio + PDC_FLASH_CTL);
  794. tmp |= 0x02000; /* bit 13 (enable bmr burst) */
  795. if (!is_gen2)
  796. tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
  797. writel(tmp, mmio + PDC_FLASH_CTL);
  798. /* clear plug/unplug flags for all ports */
  799. tmp = readl(mmio + hotplug_offset);
  800. writel(tmp | 0xff, mmio + hotplug_offset);
  801. /* unmask plug/unplug ints */
  802. tmp = readl(mmio + hotplug_offset);
  803. writel(tmp & ~0xff0000, mmio + hotplug_offset);
  804. /* don't initialise TBG or SLEW on 2nd generation chips */
  805. if (is_gen2)
  806. return;
  807. /* reduce TBG clock to 133 Mhz. */
  808. tmp = readl(mmio + PDC_TBG_MODE);
  809. tmp &= ~0x30000; /* clear bit 17, 16*/
  810. tmp |= 0x10000; /* set bit 17:16 = 0:1 */
  811. writel(tmp, mmio + PDC_TBG_MODE);
  812. readl(mmio + PDC_TBG_MODE); /* flush */
  813. msleep(10);
  814. /* adjust slew rate control register. */
  815. tmp = readl(mmio + PDC_SLEW_CTL);
  816. tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
  817. tmp |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
  818. writel(tmp, mmio + PDC_SLEW_CTL);
  819. }
  820. static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  821. {
  822. static int printed_version;
  823. const struct ata_port_info *pi = &pdc_port_info[ent->driver_data];
  824. const struct ata_port_info *ppi[PDC_MAX_PORTS];
  825. struct ata_host *host;
  826. void __iomem *base;
  827. int n_ports, i, rc;
  828. int is_sataii_tx4;
  829. if (!printed_version++)
  830. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  831. /* enable and acquire resources */
  832. rc = pcim_enable_device(pdev);
  833. if (rc)
  834. return rc;
  835. rc = pcim_iomap_regions(pdev, 1 << PDC_MMIO_BAR, DRV_NAME);
  836. if (rc == -EBUSY)
  837. pcim_pin_device(pdev);
  838. if (rc)
  839. return rc;
  840. base = pcim_iomap_table(pdev)[PDC_MMIO_BAR];
  841. /* determine port configuration and setup host */
  842. n_ports = 2;
  843. if (pi->flags & PDC_FLAG_4_PORTS)
  844. n_ports = 4;
  845. for (i = 0; i < n_ports; i++)
  846. ppi[i] = pi;
  847. if (pi->flags & PDC_FLAG_SATA_PATA) {
  848. u8 tmp = readb(base + PDC_FLASH_CTL+1);
  849. if (!(tmp & 0x80))
  850. ppi[n_ports++] = pi + 1;
  851. }
  852. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  853. if (!host) {
  854. dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n");
  855. return -ENOMEM;
  856. }
  857. host->iomap = pcim_iomap_table(pdev);
  858. is_sataii_tx4 = pdc_is_sataii_tx4(pi->flags);
  859. for (i = 0; i < host->n_ports; i++) {
  860. unsigned int ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  861. pdc_ata_setup_port(host->ports[i],
  862. base + 0x200 + ata_no * 0x80,
  863. base + 0x400 + ata_no * 0x100);
  864. }
  865. /* initialize adapter */
  866. pdc_host_init(host);
  867. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  868. if (rc)
  869. return rc;
  870. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  871. if (rc)
  872. return rc;
  873. /* start host, request IRQ and attach */
  874. pci_set_master(pdev);
  875. return ata_host_activate(host, pdev->irq, pdc_interrupt, IRQF_SHARED,
  876. &pdc_ata_sht);
  877. }
  878. static int __init pdc_ata_init(void)
  879. {
  880. return pci_register_driver(&pdc_ata_pci_driver);
  881. }
  882. static void __exit pdc_ata_exit(void)
  883. {
  884. pci_unregister_driver(&pdc_ata_pci_driver);
  885. }
  886. MODULE_AUTHOR("Jeff Garzik");
  887. MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
  888. MODULE_LICENSE("GPL");
  889. MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
  890. MODULE_VERSION(DRV_VERSION);
  891. module_init(pdc_ata_init);
  892. module_exit(pdc_ata_exit);