sata_promise.c 25 KB

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