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