sata_promise.c 26 KB

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