sata_promise.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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.10"
  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. .scr_read = pdc_sata_scr_read,
  171. .scr_write = pdc_sata_scr_write,
  172. .port_start = pdc_sata_port_start,
  173. };
  174. /* First-generation chips need a more restrictive ->check_atapi_dma op */
  175. static const struct ata_port_operations pdc_old_sata_ops = {
  176. .port_disable = ata_port_disable,
  177. .tf_load = pdc_tf_load_mmio,
  178. .tf_read = ata_tf_read,
  179. .check_status = ata_check_status,
  180. .exec_command = pdc_exec_command_mmio,
  181. .dev_select = ata_std_dev_select,
  182. .check_atapi_dma = pdc_old_sata_check_atapi_dma,
  183. .qc_prep = pdc_qc_prep,
  184. .qc_issue = pdc_qc_issue_prot,
  185. .freeze = pdc_freeze,
  186. .thaw = pdc_thaw,
  187. .error_handler = pdc_sata_error_handler,
  188. .post_internal_cmd = pdc_post_internal_cmd,
  189. .cable_detect = pdc_sata_cable_detect,
  190. .data_xfer = ata_data_xfer,
  191. .irq_clear = pdc_irq_clear,
  192. .irq_on = ata_irq_on,
  193. .scr_read = pdc_sata_scr_read,
  194. .scr_write = pdc_sata_scr_write,
  195. .port_start = pdc_sata_port_start,
  196. };
  197. static const struct ata_port_operations pdc_pata_ops = {
  198. .port_disable = ata_port_disable,
  199. .tf_load = pdc_tf_load_mmio,
  200. .tf_read = ata_tf_read,
  201. .check_status = ata_check_status,
  202. .exec_command = pdc_exec_command_mmio,
  203. .dev_select = ata_std_dev_select,
  204. .check_atapi_dma = pdc_check_atapi_dma,
  205. .qc_prep = pdc_qc_prep,
  206. .qc_issue = pdc_qc_issue_prot,
  207. .freeze = pdc_freeze,
  208. .thaw = pdc_thaw,
  209. .error_handler = pdc_pata_error_handler,
  210. .post_internal_cmd = pdc_post_internal_cmd,
  211. .cable_detect = pdc_pata_cable_detect,
  212. .data_xfer = ata_data_xfer,
  213. .irq_clear = pdc_irq_clear,
  214. .irq_on = ata_irq_on,
  215. .port_start = pdc_common_port_start,
  216. };
  217. static const struct ata_port_info pdc_port_info[] = {
  218. /* board_2037x */
  219. {
  220. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  221. PDC_FLAG_SATA_PATA,
  222. .pio_mask = 0x1f, /* pio0-4 */
  223. .mwdma_mask = 0x07, /* mwdma0-2 */
  224. .udma_mask = ATA_UDMA6,
  225. .port_ops = &pdc_old_sata_ops,
  226. },
  227. /* board_2037x_pata */
  228. {
  229. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS,
  230. .pio_mask = 0x1f, /* pio0-4 */
  231. .mwdma_mask = 0x07, /* mwdma0-2 */
  232. .udma_mask = ATA_UDMA6,
  233. .port_ops = &pdc_pata_ops,
  234. },
  235. /* board_20319 */
  236. {
  237. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  238. PDC_FLAG_4_PORTS,
  239. .pio_mask = 0x1f, /* pio0-4 */
  240. .mwdma_mask = 0x07, /* mwdma0-2 */
  241. .udma_mask = ATA_UDMA6,
  242. .port_ops = &pdc_old_sata_ops,
  243. },
  244. /* board_20619 */
  245. {
  246. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS |
  247. PDC_FLAG_4_PORTS,
  248. .pio_mask = 0x1f, /* pio0-4 */
  249. .mwdma_mask = 0x07, /* mwdma0-2 */
  250. .udma_mask = ATA_UDMA6,
  251. .port_ops = &pdc_pata_ops,
  252. },
  253. /* board_2057x */
  254. {
  255. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  256. PDC_FLAG_GEN_II | PDC_FLAG_SATA_PATA,
  257. .pio_mask = 0x1f, /* pio0-4 */
  258. .mwdma_mask = 0x07, /* mwdma0-2 */
  259. .udma_mask = ATA_UDMA6,
  260. .port_ops = &pdc_sata_ops,
  261. },
  262. /* board_2057x_pata */
  263. {
  264. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SLAVE_POSS |
  265. PDC_FLAG_GEN_II,
  266. .pio_mask = 0x1f, /* pio0-4 */
  267. .mwdma_mask = 0x07, /* mwdma0-2 */
  268. .udma_mask = ATA_UDMA6,
  269. .port_ops = &pdc_pata_ops,
  270. },
  271. /* board_40518 */
  272. {
  273. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA |
  274. PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS,
  275. .pio_mask = 0x1f, /* pio0-4 */
  276. .mwdma_mask = 0x07, /* mwdma0-2 */
  277. .udma_mask = ATA_UDMA6,
  278. .port_ops = &pdc_sata_ops,
  279. },
  280. };
  281. static const struct pci_device_id pdc_ata_pci_tbl[] = {
  282. { PCI_VDEVICE(PROMISE, 0x3371), board_2037x },
  283. { PCI_VDEVICE(PROMISE, 0x3373), board_2037x },
  284. { PCI_VDEVICE(PROMISE, 0x3375), board_2037x },
  285. { PCI_VDEVICE(PROMISE, 0x3376), board_2037x },
  286. { PCI_VDEVICE(PROMISE, 0x3570), board_2057x },
  287. { PCI_VDEVICE(PROMISE, 0x3571), board_2057x },
  288. { PCI_VDEVICE(PROMISE, 0x3574), board_2057x },
  289. { PCI_VDEVICE(PROMISE, 0x3577), board_2057x },
  290. { PCI_VDEVICE(PROMISE, 0x3d73), board_2057x },
  291. { PCI_VDEVICE(PROMISE, 0x3d75), board_2057x },
  292. { PCI_VDEVICE(PROMISE, 0x3318), board_20319 },
  293. { PCI_VDEVICE(PROMISE, 0x3319), board_20319 },
  294. { PCI_VDEVICE(PROMISE, 0x3515), board_40518 },
  295. { PCI_VDEVICE(PROMISE, 0x3519), board_40518 },
  296. { PCI_VDEVICE(PROMISE, 0x3d17), board_40518 },
  297. { PCI_VDEVICE(PROMISE, 0x3d18), board_40518 },
  298. { PCI_VDEVICE(PROMISE, 0x6629), board_20619 },
  299. { } /* terminate list */
  300. };
  301. static struct pci_driver pdc_ata_pci_driver = {
  302. .name = DRV_NAME,
  303. .id_table = pdc_ata_pci_tbl,
  304. .probe = pdc_ata_init_one,
  305. .remove = ata_pci_remove_one,
  306. };
  307. static int pdc_common_port_start(struct ata_port *ap)
  308. {
  309. struct device *dev = ap->host->dev;
  310. struct pdc_port_priv *pp;
  311. int rc;
  312. rc = ata_port_start(ap);
  313. if (rc)
  314. return rc;
  315. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  316. if (!pp)
  317. return -ENOMEM;
  318. pp->pkt = dmam_alloc_coherent(dev, 128, &pp->pkt_dma, GFP_KERNEL);
  319. if (!pp->pkt)
  320. return -ENOMEM;
  321. ap->private_data = pp;
  322. return 0;
  323. }
  324. static int pdc_sata_port_start(struct ata_port *ap)
  325. {
  326. int rc;
  327. rc = pdc_common_port_start(ap);
  328. if (rc)
  329. return rc;
  330. /* fix up PHYMODE4 align timing */
  331. if (ap->flags & PDC_FLAG_GEN_II) {
  332. void __iomem *mmio = ap->ioaddr.scr_addr;
  333. unsigned int tmp;
  334. tmp = readl(mmio + 0x014);
  335. tmp = (tmp & ~3) | 1; /* set bits 1:0 = 0:1 */
  336. writel(tmp, mmio + 0x014);
  337. }
  338. return 0;
  339. }
  340. static void pdc_reset_port(struct ata_port *ap)
  341. {
  342. void __iomem *mmio = ap->ioaddr.cmd_addr + PDC_CTLSTAT;
  343. unsigned int i;
  344. u32 tmp;
  345. for (i = 11; i > 0; i--) {
  346. tmp = readl(mmio);
  347. if (tmp & PDC_RESET)
  348. break;
  349. udelay(100);
  350. tmp |= PDC_RESET;
  351. writel(tmp, mmio);
  352. }
  353. tmp &= ~PDC_RESET;
  354. writel(tmp, mmio);
  355. readl(mmio); /* flush */
  356. }
  357. static int pdc_pata_cable_detect(struct ata_port *ap)
  358. {
  359. u8 tmp;
  360. void __iomem *mmio = ap->ioaddr.cmd_addr + PDC_CTLSTAT + 0x03;
  361. tmp = readb(mmio);
  362. if (tmp & 0x01)
  363. return ATA_CBL_PATA40;
  364. return ATA_CBL_PATA80;
  365. }
  366. static int pdc_sata_cable_detect(struct ata_port *ap)
  367. {
  368. return ATA_CBL_SATA;
  369. }
  370. static int pdc_sata_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val)
  371. {
  372. if (sc_reg > SCR_CONTROL)
  373. return -EINVAL;
  374. *val = readl(ap->ioaddr.scr_addr + (sc_reg * 4));
  375. return 0;
  376. }
  377. static int pdc_sata_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
  378. {
  379. if (sc_reg > SCR_CONTROL)
  380. return -EINVAL;
  381. writel(val, ap->ioaddr.scr_addr + (sc_reg * 4));
  382. return 0;
  383. }
  384. static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
  385. {
  386. struct ata_port *ap = qc->ap;
  387. dma_addr_t sg_table = ap->prd_dma;
  388. unsigned int cdb_len = qc->dev->cdb_len;
  389. u8 *cdb = qc->cdb;
  390. struct pdc_port_priv *pp = ap->private_data;
  391. u8 *buf = pp->pkt;
  392. u32 *buf32 = (u32 *) buf;
  393. unsigned int dev_sel, feature, nbytes;
  394. /* set control bits (byte 0), zero delay seq id (byte 3),
  395. * and seq id (byte 2)
  396. */
  397. switch (qc->tf.protocol) {
  398. case ATA_PROT_ATAPI_DMA:
  399. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  400. buf32[0] = cpu_to_le32(PDC_PKT_READ);
  401. else
  402. buf32[0] = 0;
  403. break;
  404. case ATA_PROT_ATAPI_NODATA:
  405. buf32[0] = cpu_to_le32(PDC_PKT_NODATA);
  406. break;
  407. default:
  408. BUG();
  409. break;
  410. }
  411. buf32[1] = cpu_to_le32(sg_table); /* S/G table addr */
  412. buf32[2] = 0; /* no next-packet */
  413. /* select drive */
  414. if (sata_scr_valid(&ap->link)) {
  415. dev_sel = PDC_DEVICE_SATA;
  416. } else {
  417. dev_sel = ATA_DEVICE_OBS;
  418. if (qc->dev->devno != 0)
  419. dev_sel |= ATA_DEV1;
  420. }
  421. buf[12] = (1 << 5) | ATA_REG_DEVICE;
  422. buf[13] = dev_sel;
  423. buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY;
  424. buf[15] = dev_sel; /* once more, waiting for BSY to clear */
  425. buf[16] = (1 << 5) | ATA_REG_NSECT;
  426. buf[17] = 0x00;
  427. buf[18] = (1 << 5) | ATA_REG_LBAL;
  428. buf[19] = 0x00;
  429. /* set feature and byte counter registers */
  430. if (qc->tf.protocol != ATA_PROT_ATAPI_DMA) {
  431. feature = PDC_FEATURE_ATAPI_PIO;
  432. /* set byte counter register to real transfer byte count */
  433. nbytes = qc->nbytes;
  434. if (nbytes > 0xffff)
  435. nbytes = 0xffff;
  436. } else {
  437. feature = PDC_FEATURE_ATAPI_DMA;
  438. /* set byte counter register to 0 */
  439. nbytes = 0;
  440. }
  441. buf[20] = (1 << 5) | ATA_REG_FEATURE;
  442. buf[21] = feature;
  443. buf[22] = (1 << 5) | ATA_REG_BYTEL;
  444. buf[23] = nbytes & 0xFF;
  445. buf[24] = (1 << 5) | ATA_REG_BYTEH;
  446. buf[25] = (nbytes >> 8) & 0xFF;
  447. /* send ATAPI packet command 0xA0 */
  448. buf[26] = (1 << 5) | ATA_REG_CMD;
  449. buf[27] = ATA_CMD_PACKET;
  450. /* select drive and check DRQ */
  451. buf[28] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_WAIT_DRDY;
  452. buf[29] = dev_sel;
  453. /* we can represent cdb lengths 2/4/6/8/10/12/14/16 */
  454. BUG_ON(cdb_len & ~0x1E);
  455. /* append the CDB as the final part */
  456. buf[30] = (((cdb_len >> 1) & 7) << 5) | ATA_REG_DATA | PDC_LAST_REG;
  457. memcpy(buf+31, cdb, cdb_len);
  458. }
  459. static void pdc_qc_prep(struct ata_queued_cmd *qc)
  460. {
  461. struct pdc_port_priv *pp = qc->ap->private_data;
  462. unsigned int i;
  463. VPRINTK("ENTER\n");
  464. switch (qc->tf.protocol) {
  465. case ATA_PROT_DMA:
  466. ata_qc_prep(qc);
  467. /* fall through */
  468. case ATA_PROT_NODATA:
  469. i = pdc_pkt_header(&qc->tf, qc->ap->prd_dma,
  470. qc->dev->devno, pp->pkt);
  471. if (qc->tf.flags & ATA_TFLAG_LBA48)
  472. i = pdc_prep_lba48(&qc->tf, pp->pkt, i);
  473. else
  474. i = pdc_prep_lba28(&qc->tf, pp->pkt, i);
  475. pdc_pkt_footer(&qc->tf, pp->pkt, i);
  476. break;
  477. case ATA_PROT_ATAPI:
  478. ata_qc_prep(qc);
  479. break;
  480. case ATA_PROT_ATAPI_DMA:
  481. ata_qc_prep(qc);
  482. /*FALLTHROUGH*/
  483. case ATA_PROT_ATAPI_NODATA:
  484. pdc_atapi_pkt(qc);
  485. break;
  486. default:
  487. break;
  488. }
  489. }
  490. static void pdc_freeze(struct ata_port *ap)
  491. {
  492. void __iomem *mmio = ap->ioaddr.cmd_addr;
  493. u32 tmp;
  494. tmp = readl(mmio + PDC_CTLSTAT);
  495. tmp |= PDC_IRQ_DISABLE;
  496. tmp &= ~PDC_DMA_ENABLE;
  497. writel(tmp, mmio + PDC_CTLSTAT);
  498. readl(mmio + PDC_CTLSTAT); /* flush */
  499. }
  500. static void pdc_thaw(struct ata_port *ap)
  501. {
  502. void __iomem *mmio = ap->ioaddr.cmd_addr;
  503. u32 tmp;
  504. /* clear IRQ */
  505. readl(mmio + PDC_INT_SEQMASK);
  506. /* turn IRQ back on */
  507. tmp = readl(mmio + PDC_CTLSTAT);
  508. tmp &= ~PDC_IRQ_DISABLE;
  509. writel(tmp, mmio + PDC_CTLSTAT);
  510. readl(mmio + PDC_CTLSTAT); /* flush */
  511. }
  512. static void pdc_common_error_handler(struct ata_port *ap, ata_reset_fn_t hardreset)
  513. {
  514. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  515. pdc_reset_port(ap);
  516. /* perform recovery */
  517. ata_do_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  518. ata_std_postreset);
  519. }
  520. static void pdc_pata_error_handler(struct ata_port *ap)
  521. {
  522. pdc_common_error_handler(ap, NULL);
  523. }
  524. static void pdc_sata_error_handler(struct ata_port *ap)
  525. {
  526. pdc_common_error_handler(ap, sata_std_hardreset);
  527. }
  528. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc)
  529. {
  530. struct ata_port *ap = qc->ap;
  531. /* make DMA engine forget about the failed command */
  532. if (qc->flags & ATA_QCFLAG_FAILED)
  533. pdc_reset_port(ap);
  534. }
  535. static void pdc_error_intr(struct ata_port *ap, struct ata_queued_cmd *qc,
  536. u32 port_status, u32 err_mask)
  537. {
  538. struct ata_eh_info *ehi = &ap->link.eh_info;
  539. unsigned int ac_err_mask = 0;
  540. ata_ehi_clear_desc(ehi);
  541. ata_ehi_push_desc(ehi, "port_status 0x%08x", port_status);
  542. port_status &= err_mask;
  543. if (port_status & PDC_DRIVE_ERR)
  544. ac_err_mask |= AC_ERR_DEV;
  545. if (port_status & (PDC_OVERRUN_ERR | PDC_UNDERRUN_ERR))
  546. ac_err_mask |= AC_ERR_HSM;
  547. if (port_status & (PDC2_ATA_HBA_ERR | PDC2_ATA_DMA_CNT_ERR))
  548. ac_err_mask |= AC_ERR_ATA_BUS;
  549. if (port_status & (PDC_PH_ERR | PDC_SH_ERR | PDC_DH_ERR | PDC2_HTO_ERR
  550. | PDC_PCI_SYS_ERR | PDC1_PCI_PARITY_ERR))
  551. ac_err_mask |= AC_ERR_HOST_BUS;
  552. if (sata_scr_valid(&ap->link)) {
  553. u32 serror;
  554. pdc_sata_scr_read(ap, SCR_ERROR, &serror);
  555. ehi->serror |= serror;
  556. }
  557. qc->err_mask |= ac_err_mask;
  558. pdc_reset_port(ap);
  559. ata_port_abort(ap);
  560. }
  561. static inline unsigned int pdc_host_intr(struct ata_port *ap,
  562. struct ata_queued_cmd *qc)
  563. {
  564. unsigned int handled = 0;
  565. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  566. u32 port_status, err_mask;
  567. err_mask = PDC_ERR_MASK;
  568. if (ap->flags & PDC_FLAG_GEN_II)
  569. err_mask &= ~PDC1_ERR_MASK;
  570. else
  571. err_mask &= ~PDC2_ERR_MASK;
  572. port_status = readl(port_mmio + PDC_GLOBAL_CTL);
  573. if (unlikely(port_status & err_mask)) {
  574. pdc_error_intr(ap, qc, port_status, err_mask);
  575. return 1;
  576. }
  577. switch (qc->tf.protocol) {
  578. case ATA_PROT_DMA:
  579. case ATA_PROT_NODATA:
  580. case ATA_PROT_ATAPI_DMA:
  581. case ATA_PROT_ATAPI_NODATA:
  582. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  583. ata_qc_complete(qc);
  584. handled = 1;
  585. break;
  586. default:
  587. ap->stats.idle_irq++;
  588. break;
  589. }
  590. return handled;
  591. }
  592. static void pdc_irq_clear(struct ata_port *ap)
  593. {
  594. struct ata_host *host = ap->host;
  595. void __iomem *mmio = host->iomap[PDC_MMIO_BAR];
  596. readl(mmio + PDC_INT_SEQMASK);
  597. }
  598. static inline int pdc_is_sataii_tx4(unsigned long flags)
  599. {
  600. const unsigned long mask = PDC_FLAG_GEN_II | PDC_FLAG_4_PORTS;
  601. return (flags & mask) == mask;
  602. }
  603. static inline unsigned int pdc_port_no_to_ata_no(unsigned int port_no, int is_sataii_tx4)
  604. {
  605. static const unsigned char sataii_tx4_port_remap[4] = { 3, 1, 0, 2};
  606. return is_sataii_tx4 ? sataii_tx4_port_remap[port_no] : port_no;
  607. }
  608. static irqreturn_t pdc_interrupt (int irq, void *dev_instance)
  609. {
  610. struct ata_host *host = dev_instance;
  611. struct ata_port *ap;
  612. u32 mask = 0;
  613. unsigned int i, tmp;
  614. unsigned int handled = 0;
  615. void __iomem *mmio_base;
  616. unsigned int hotplug_offset, ata_no;
  617. u32 hotplug_status;
  618. int is_sataii_tx4;
  619. VPRINTK("ENTER\n");
  620. if (!host || !host->iomap[PDC_MMIO_BAR]) {
  621. VPRINTK("QUICK EXIT\n");
  622. return IRQ_NONE;
  623. }
  624. mmio_base = host->iomap[PDC_MMIO_BAR];
  625. /* read and clear hotplug flags for all ports */
  626. if (host->ports[0]->flags & PDC_FLAG_GEN_II)
  627. hotplug_offset = PDC2_SATA_PLUG_CSR;
  628. else
  629. hotplug_offset = PDC_SATA_PLUG_CSR;
  630. hotplug_status = readl(mmio_base + hotplug_offset);
  631. if (hotplug_status & 0xff)
  632. writel(hotplug_status | 0xff, mmio_base + hotplug_offset);
  633. hotplug_status &= 0xff; /* clear uninteresting bits */
  634. /* reading should also clear interrupts */
  635. mask = readl(mmio_base + PDC_INT_SEQMASK);
  636. if (mask == 0xffffffff && hotplug_status == 0) {
  637. VPRINTK("QUICK EXIT 2\n");
  638. return IRQ_NONE;
  639. }
  640. spin_lock(&host->lock);
  641. mask &= 0xffff; /* only 16 tags possible */
  642. if (mask == 0 && hotplug_status == 0) {
  643. VPRINTK("QUICK EXIT 3\n");
  644. goto done_irq;
  645. }
  646. writel(mask, mmio_base + PDC_INT_SEQMASK);
  647. is_sataii_tx4 = pdc_is_sataii_tx4(host->ports[0]->flags);
  648. for (i = 0; i < host->n_ports; i++) {
  649. VPRINTK("port %u\n", i);
  650. ap = host->ports[i];
  651. /* check for a plug or unplug event */
  652. ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  653. tmp = hotplug_status & (0x11 << ata_no);
  654. if (tmp && ap &&
  655. !(ap->flags & ATA_FLAG_DISABLED)) {
  656. struct ata_eh_info *ehi = &ap->link.eh_info;
  657. ata_ehi_clear_desc(ehi);
  658. ata_ehi_hotplugged(ehi);
  659. ata_ehi_push_desc(ehi, "hotplug_status %#x", tmp);
  660. ata_port_freeze(ap);
  661. ++handled;
  662. continue;
  663. }
  664. /* check for a packet interrupt */
  665. tmp = mask & (1 << (i + 1));
  666. if (tmp && ap &&
  667. !(ap->flags & ATA_FLAG_DISABLED)) {
  668. struct ata_queued_cmd *qc;
  669. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  670. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  671. handled += pdc_host_intr(ap, qc);
  672. }
  673. }
  674. VPRINTK("EXIT\n");
  675. done_irq:
  676. spin_unlock(&host->lock);
  677. return IRQ_RETVAL(handled);
  678. }
  679. static inline void pdc_packet_start(struct ata_queued_cmd *qc)
  680. {
  681. struct ata_port *ap = qc->ap;
  682. struct pdc_port_priv *pp = ap->private_data;
  683. void __iomem *mmio = ap->host->iomap[PDC_MMIO_BAR];
  684. unsigned int port_no = ap->port_no;
  685. u8 seq = (u8) (port_no + 1);
  686. VPRINTK("ENTER, ap %p\n", ap);
  687. writel(0x00000001, mmio + (seq * 4));
  688. readl(mmio + (seq * 4)); /* flush */
  689. pp->pkt[2] = seq;
  690. wmb(); /* flush PRD, pkt writes */
  691. writel(pp->pkt_dma, ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  692. readl(ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT); /* flush */
  693. }
  694. static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
  695. {
  696. switch (qc->tf.protocol) {
  697. case ATA_PROT_ATAPI_NODATA:
  698. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  699. break;
  700. /*FALLTHROUGH*/
  701. case ATA_PROT_NODATA:
  702. if (qc->tf.flags & ATA_TFLAG_POLLING)
  703. break;
  704. /*FALLTHROUGH*/
  705. case ATA_PROT_ATAPI_DMA:
  706. case ATA_PROT_DMA:
  707. pdc_packet_start(qc);
  708. return 0;
  709. default:
  710. break;
  711. }
  712. return ata_qc_issue_prot(qc);
  713. }
  714. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  715. {
  716. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  717. tf->protocol == ATA_PROT_ATAPI_DMA);
  718. ata_tf_load(ap, tf);
  719. }
  720. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  721. {
  722. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  723. tf->protocol == ATA_PROT_ATAPI_DMA);
  724. ata_exec_command(ap, tf);
  725. }
  726. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc)
  727. {
  728. u8 *scsicmd = qc->scsicmd->cmnd;
  729. int pio = 1; /* atapi dma off by default */
  730. /* Whitelist commands that may use DMA. */
  731. switch (scsicmd[0]) {
  732. case WRITE_12:
  733. case WRITE_10:
  734. case WRITE_6:
  735. case READ_12:
  736. case READ_10:
  737. case READ_6:
  738. case 0xad: /* READ_DVD_STRUCTURE */
  739. case 0xbe: /* READ_CD */
  740. pio = 0;
  741. }
  742. /* -45150 (FFFF4FA2) to -1 (FFFFFFFF) shall use PIO mode */
  743. if (scsicmd[0] == WRITE_10) {
  744. unsigned int lba;
  745. lba = (scsicmd[2] << 24) | (scsicmd[3] << 16) | (scsicmd[4] << 8) | scsicmd[5];
  746. if (lba >= 0xFFFF4FA2)
  747. pio = 1;
  748. }
  749. return pio;
  750. }
  751. static int pdc_old_sata_check_atapi_dma(struct ata_queued_cmd *qc)
  752. {
  753. /* First generation chips cannot use ATAPI DMA on SATA ports */
  754. return 1;
  755. }
  756. static void pdc_ata_setup_port(struct ata_port *ap,
  757. void __iomem *base, void __iomem *scr_addr)
  758. {
  759. ap->ioaddr.cmd_addr = base;
  760. ap->ioaddr.data_addr = base;
  761. ap->ioaddr.feature_addr =
  762. ap->ioaddr.error_addr = base + 0x4;
  763. ap->ioaddr.nsect_addr = base + 0x8;
  764. ap->ioaddr.lbal_addr = base + 0xc;
  765. ap->ioaddr.lbam_addr = base + 0x10;
  766. ap->ioaddr.lbah_addr = base + 0x14;
  767. ap->ioaddr.device_addr = base + 0x18;
  768. ap->ioaddr.command_addr =
  769. ap->ioaddr.status_addr = base + 0x1c;
  770. ap->ioaddr.altstatus_addr =
  771. ap->ioaddr.ctl_addr = base + 0x38;
  772. ap->ioaddr.scr_addr = scr_addr;
  773. }
  774. static void pdc_host_init(struct ata_host *host)
  775. {
  776. void __iomem *mmio = host->iomap[PDC_MMIO_BAR];
  777. int is_gen2 = host->ports[0]->flags & PDC_FLAG_GEN_II;
  778. int hotplug_offset;
  779. u32 tmp;
  780. if (is_gen2)
  781. hotplug_offset = PDC2_SATA_PLUG_CSR;
  782. else
  783. hotplug_offset = PDC_SATA_PLUG_CSR;
  784. /*
  785. * Except for the hotplug stuff, this is voodoo from the
  786. * Promise driver. Label this entire section
  787. * "TODO: figure out why we do this"
  788. */
  789. /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
  790. tmp = readl(mmio + PDC_FLASH_CTL);
  791. tmp |= 0x02000; /* bit 13 (enable bmr burst) */
  792. if (!is_gen2)
  793. tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
  794. writel(tmp, mmio + PDC_FLASH_CTL);
  795. /* clear plug/unplug flags for all ports */
  796. tmp = readl(mmio + hotplug_offset);
  797. writel(tmp | 0xff, mmio + hotplug_offset);
  798. /* unmask plug/unplug ints */
  799. tmp = readl(mmio + hotplug_offset);
  800. writel(tmp & ~0xff0000, mmio + hotplug_offset);
  801. /* don't initialise TBG or SLEW on 2nd generation chips */
  802. if (is_gen2)
  803. return;
  804. /* reduce TBG clock to 133 Mhz. */
  805. tmp = readl(mmio + PDC_TBG_MODE);
  806. tmp &= ~0x30000; /* clear bit 17, 16*/
  807. tmp |= 0x10000; /* set bit 17:16 = 0:1 */
  808. writel(tmp, mmio + PDC_TBG_MODE);
  809. readl(mmio + PDC_TBG_MODE); /* flush */
  810. msleep(10);
  811. /* adjust slew rate control register. */
  812. tmp = readl(mmio + PDC_SLEW_CTL);
  813. tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
  814. tmp |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
  815. writel(tmp, mmio + PDC_SLEW_CTL);
  816. }
  817. static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  818. {
  819. static int printed_version;
  820. const struct ata_port_info *pi = &pdc_port_info[ent->driver_data];
  821. const struct ata_port_info *ppi[PDC_MAX_PORTS];
  822. struct ata_host *host;
  823. void __iomem *base;
  824. int n_ports, i, rc;
  825. int is_sataii_tx4;
  826. if (!printed_version++)
  827. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  828. /* enable and acquire resources */
  829. rc = pcim_enable_device(pdev);
  830. if (rc)
  831. return rc;
  832. rc = pcim_iomap_regions(pdev, 1 << PDC_MMIO_BAR, DRV_NAME);
  833. if (rc == -EBUSY)
  834. pcim_pin_device(pdev);
  835. if (rc)
  836. return rc;
  837. base = pcim_iomap_table(pdev)[PDC_MMIO_BAR];
  838. /* determine port configuration and setup host */
  839. n_ports = 2;
  840. if (pi->flags & PDC_FLAG_4_PORTS)
  841. n_ports = 4;
  842. for (i = 0; i < n_ports; i++)
  843. ppi[i] = pi;
  844. if (pi->flags & PDC_FLAG_SATA_PATA) {
  845. u8 tmp = readb(base + PDC_FLASH_CTL+1);
  846. if (!(tmp & 0x80))
  847. ppi[n_ports++] = pi + 1;
  848. }
  849. host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
  850. if (!host) {
  851. dev_printk(KERN_ERR, &pdev->dev, "failed to allocate host\n");
  852. return -ENOMEM;
  853. }
  854. host->iomap = pcim_iomap_table(pdev);
  855. is_sataii_tx4 = pdc_is_sataii_tx4(pi->flags);
  856. for (i = 0; i < host->n_ports; i++) {
  857. unsigned int ata_no = pdc_port_no_to_ata_no(i, is_sataii_tx4);
  858. pdc_ata_setup_port(host->ports[i],
  859. base + 0x200 + ata_no * 0x80,
  860. base + 0x400 + ata_no * 0x100);
  861. }
  862. /* initialize adapter */
  863. pdc_host_init(host);
  864. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  865. if (rc)
  866. return rc;
  867. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  868. if (rc)
  869. return rc;
  870. /* start host, request IRQ and attach */
  871. pci_set_master(pdev);
  872. return ata_host_activate(host, pdev->irq, pdc_interrupt, IRQF_SHARED,
  873. &pdc_ata_sht);
  874. }
  875. static int __init pdc_ata_init(void)
  876. {
  877. return pci_register_driver(&pdc_ata_pci_driver);
  878. }
  879. static void __exit pdc_ata_exit(void)
  880. {
  881. pci_unregister_driver(&pdc_ata_pci_driver);
  882. }
  883. MODULE_AUTHOR("Jeff Garzik");
  884. MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
  885. MODULE_LICENSE("GPL");
  886. MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
  887. MODULE_VERSION(DRV_VERSION);
  888. module_init(pdc_ata_init);
  889. module_exit(pdc_ata_exit);