sata_promise.c 25 KB

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