sata_promise.c 28 KB

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