sata_promise.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * sata_promise.c - Promise SATA
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2003-2004 Red Hat, Inc.
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. *
  26. * libata documentation is available via 'make {ps|pdf}docs',
  27. * as Documentation/DocBook/libata.*
  28. *
  29. * Hardware information only available under NDA.
  30. *
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/pci.h>
  35. #include <linux/init.h>
  36. #include <linux/blkdev.h>
  37. #include <linux/delay.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/sched.h>
  40. #include <linux/device.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_host.h>
  43. #include <scsi/scsi_cmnd.h>
  44. #include <linux/libata.h>
  45. #include <asm/io.h>
  46. #include "sata_promise.h"
  47. #define DRV_NAME "sata_promise"
  48. #define DRV_VERSION "1.05"
  49. enum {
  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_ERR_MASK = (1<<19) | (1<<20) | (1<<21) | (1<<22) |
  69. (1<<8) | (1<<9) | (1<<10),
  70. board_2037x = 0, /* FastTrak S150 TX2plus */
  71. board_20319 = 1, /* FastTrak S150 TX4 */
  72. board_20619 = 2, /* FastTrak TX4000 */
  73. board_2057x = 3, /* SATAII150 Tx2plus */
  74. board_40518 = 4, /* SATAII150 Tx4 */
  75. PDC_HAS_PATA = (1 << 1), /* PDC20375/20575 has PATA */
  76. /* Sequence counter control registers bit definitions */
  77. PDC_SEQCNTRL_INT_MASK = (1 << 5), /* Sequence Interrupt Mask */
  78. /* Feature register values */
  79. PDC_FEATURE_ATAPI_PIO = 0x00, /* ATAPI data xfer by PIO */
  80. PDC_FEATURE_ATAPI_DMA = 0x01, /* ATAPI data xfer by DMA */
  81. /* Device/Head register values */
  82. PDC_DEVICE_SATA = 0xE0, /* Device/Head value for SATA devices */
  83. /* PDC_CTLSTAT bit definitions */
  84. PDC_DMA_ENABLE = (1 << 7),
  85. PDC_IRQ_DISABLE = (1 << 10),
  86. PDC_RESET = (1 << 11), /* HDMA reset */
  87. PDC_COMMON_FLAGS = ATA_FLAG_NO_LEGACY |
  88. ATA_FLAG_MMIO |
  89. ATA_FLAG_PIO_POLLING,
  90. /* hp->flags bits */
  91. PDC_FLAG_GEN_II = (1 << 0),
  92. };
  93. struct pdc_port_priv {
  94. u8 *pkt;
  95. dma_addr_t pkt_dma;
  96. };
  97. struct pdc_host_priv {
  98. unsigned long flags;
  99. unsigned long port_flags[ATA_MAX_PORTS];
  100. };
  101. static u32 pdc_sata_scr_read (struct ata_port *ap, unsigned int sc_reg);
  102. static void pdc_sata_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  103. static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  104. static irqreturn_t pdc_interrupt (int irq, void *dev_instance);
  105. static void pdc_eng_timeout(struct ata_port *ap);
  106. static int pdc_port_start(struct ata_port *ap);
  107. static void pdc_port_stop(struct ata_port *ap);
  108. static void pdc_pata_phy_reset(struct ata_port *ap);
  109. static void pdc_qc_prep(struct ata_queued_cmd *qc);
  110. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  111. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf);
  112. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc);
  113. static int pdc_old_check_atapi_dma(struct ata_queued_cmd *qc);
  114. static void pdc_irq_clear(struct ata_port *ap);
  115. static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc);
  116. static void pdc_host_stop(struct ata_host *host);
  117. static void pdc_freeze(struct ata_port *ap);
  118. static void pdc_thaw(struct ata_port *ap);
  119. static void pdc_error_handler(struct ata_port *ap);
  120. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc);
  121. static struct scsi_host_template pdc_ata_sht = {
  122. .module = THIS_MODULE,
  123. .name = DRV_NAME,
  124. .ioctl = ata_scsi_ioctl,
  125. .queuecommand = ata_scsi_queuecmd,
  126. .can_queue = ATA_DEF_QUEUE,
  127. .this_id = ATA_SHT_THIS_ID,
  128. .sg_tablesize = LIBATA_MAX_PRD,
  129. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  130. .emulated = ATA_SHT_EMULATED,
  131. .use_clustering = ATA_SHT_USE_CLUSTERING,
  132. .proc_name = DRV_NAME,
  133. .dma_boundary = ATA_DMA_BOUNDARY,
  134. .slave_configure = ata_scsi_slave_config,
  135. .slave_destroy = ata_scsi_slave_destroy,
  136. .bios_param = ata_std_bios_param,
  137. };
  138. static const struct ata_port_operations pdc_sata_ops = {
  139. .port_disable = ata_port_disable,
  140. .tf_load = pdc_tf_load_mmio,
  141. .tf_read = ata_tf_read,
  142. .check_status = ata_check_status,
  143. .exec_command = pdc_exec_command_mmio,
  144. .dev_select = ata_std_dev_select,
  145. .check_atapi_dma = pdc_check_atapi_dma,
  146. .qc_prep = pdc_qc_prep,
  147. .qc_issue = pdc_qc_issue_prot,
  148. .freeze = pdc_freeze,
  149. .thaw = pdc_thaw,
  150. .error_handler = pdc_error_handler,
  151. .post_internal_cmd = pdc_post_internal_cmd,
  152. .data_xfer = ata_mmio_data_xfer,
  153. .irq_handler = pdc_interrupt,
  154. .irq_clear = pdc_irq_clear,
  155. .scr_read = pdc_sata_scr_read,
  156. .scr_write = pdc_sata_scr_write,
  157. .port_start = pdc_port_start,
  158. .port_stop = pdc_port_stop,
  159. .host_stop = pdc_host_stop,
  160. };
  161. /* First-generation chips need a more restrictive ->check_atapi_dma op */
  162. static const struct ata_port_operations pdc_old_sata_ops = {
  163. .port_disable = ata_port_disable,
  164. .tf_load = pdc_tf_load_mmio,
  165. .tf_read = ata_tf_read,
  166. .check_status = ata_check_status,
  167. .exec_command = pdc_exec_command_mmio,
  168. .dev_select = ata_std_dev_select,
  169. .check_atapi_dma = pdc_old_check_atapi_dma,
  170. .qc_prep = pdc_qc_prep,
  171. .qc_issue = pdc_qc_issue_prot,
  172. .freeze = pdc_freeze,
  173. .thaw = pdc_thaw,
  174. .error_handler = pdc_error_handler,
  175. .post_internal_cmd = pdc_post_internal_cmd,
  176. .data_xfer = ata_mmio_data_xfer,
  177. .irq_handler = pdc_interrupt,
  178. .irq_clear = pdc_irq_clear,
  179. .scr_read = pdc_sata_scr_read,
  180. .scr_write = pdc_sata_scr_write,
  181. .port_start = pdc_port_start,
  182. .port_stop = pdc_port_stop,
  183. .host_stop = pdc_host_stop,
  184. };
  185. static const struct ata_port_operations pdc_pata_ops = {
  186. .port_disable = ata_port_disable,
  187. .tf_load = pdc_tf_load_mmio,
  188. .tf_read = ata_tf_read,
  189. .check_status = ata_check_status,
  190. .exec_command = pdc_exec_command_mmio,
  191. .dev_select = ata_std_dev_select,
  192. .check_atapi_dma = pdc_check_atapi_dma,
  193. .phy_reset = pdc_pata_phy_reset,
  194. .qc_prep = pdc_qc_prep,
  195. .qc_issue = pdc_qc_issue_prot,
  196. .data_xfer = ata_mmio_data_xfer,
  197. .eng_timeout = pdc_eng_timeout,
  198. .irq_handler = pdc_interrupt,
  199. .irq_clear = pdc_irq_clear,
  200. .port_start = pdc_port_start,
  201. .port_stop = pdc_port_stop,
  202. .host_stop = pdc_host_stop,
  203. };
  204. static const struct ata_port_info pdc_port_info[] = {
  205. /* board_2037x */
  206. {
  207. .sht = &pdc_ata_sht,
  208. .flags = PDC_COMMON_FLAGS,
  209. .pio_mask = 0x1f, /* pio0-4 */
  210. .mwdma_mask = 0x07, /* mwdma0-2 */
  211. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  212. .port_ops = &pdc_old_sata_ops,
  213. },
  214. /* board_20319 */
  215. {
  216. .sht = &pdc_ata_sht,
  217. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA,
  218. .pio_mask = 0x1f, /* pio0-4 */
  219. .mwdma_mask = 0x07, /* mwdma0-2 */
  220. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  221. .port_ops = &pdc_old_sata_ops,
  222. },
  223. /* board_20619 */
  224. {
  225. .sht = &pdc_ata_sht,
  226. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SRST | ATA_FLAG_SLAVE_POSS,
  227. .pio_mask = 0x1f, /* pio0-4 */
  228. .mwdma_mask = 0x07, /* mwdma0-2 */
  229. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  230. .port_ops = &pdc_pata_ops,
  231. },
  232. /* board_2057x */
  233. {
  234. .sht = &pdc_ata_sht,
  235. .flags = PDC_COMMON_FLAGS,
  236. .pio_mask = 0x1f, /* pio0-4 */
  237. .mwdma_mask = 0x07, /* mwdma0-2 */
  238. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  239. .port_ops = &pdc_sata_ops,
  240. },
  241. /* board_40518 */
  242. {
  243. .sht = &pdc_ata_sht,
  244. .flags = PDC_COMMON_FLAGS | ATA_FLAG_SATA,
  245. .pio_mask = 0x1f, /* pio0-4 */
  246. .mwdma_mask = 0x07, /* mwdma0-2 */
  247. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  248. .port_ops = &pdc_sata_ops,
  249. },
  250. };
  251. static const struct pci_device_id pdc_ata_pci_tbl[] = {
  252. { PCI_VDEVICE(PROMISE, 0x3371), board_2037x },
  253. { PCI_VDEVICE(PROMISE, 0x3373), board_2037x },
  254. { PCI_VDEVICE(PROMISE, 0x3375), board_2037x },
  255. { PCI_VDEVICE(PROMISE, 0x3376), board_2037x },
  256. { PCI_VDEVICE(PROMISE, 0x3570), board_2057x },
  257. { PCI_VDEVICE(PROMISE, 0x3571), board_2057x },
  258. { PCI_VDEVICE(PROMISE, 0x3574), board_2057x },
  259. { PCI_VDEVICE(PROMISE, 0x3577), board_2057x },
  260. { PCI_VDEVICE(PROMISE, 0x3d73), board_2057x },
  261. { PCI_VDEVICE(PROMISE, 0x3d75), board_2057x },
  262. { PCI_VDEVICE(PROMISE, 0x3318), board_20319 },
  263. { PCI_VDEVICE(PROMISE, 0x3319), board_20319 },
  264. { PCI_VDEVICE(PROMISE, 0x3515), board_20319 },
  265. { PCI_VDEVICE(PROMISE, 0x3519), board_20319 },
  266. { PCI_VDEVICE(PROMISE, 0x3d17), board_40518 },
  267. { PCI_VDEVICE(PROMISE, 0x3d18), board_40518 },
  268. { PCI_VDEVICE(PROMISE, 0x6629), board_20619 },
  269. { } /* terminate list */
  270. };
  271. static struct pci_driver pdc_ata_pci_driver = {
  272. .name = DRV_NAME,
  273. .id_table = pdc_ata_pci_tbl,
  274. .probe = pdc_ata_init_one,
  275. .remove = ata_pci_remove_one,
  276. };
  277. static int pdc_port_start(struct ata_port *ap)
  278. {
  279. struct device *dev = ap->host->dev;
  280. struct pdc_host_priv *hp = ap->host->private_data;
  281. struct pdc_port_priv *pp;
  282. int rc;
  283. /* fix up port flags and cable type for SATA+PATA chips */
  284. ap->flags |= hp->port_flags[ap->port_no];
  285. if (ap->flags & ATA_FLAG_SATA)
  286. ap->cbl = ATA_CBL_SATA;
  287. rc = ata_port_start(ap);
  288. if (rc)
  289. return rc;
  290. pp = kzalloc(sizeof(*pp), GFP_KERNEL);
  291. if (!pp) {
  292. rc = -ENOMEM;
  293. goto err_out;
  294. }
  295. pp->pkt = dma_alloc_coherent(dev, 128, &pp->pkt_dma, GFP_KERNEL);
  296. if (!pp->pkt) {
  297. rc = -ENOMEM;
  298. goto err_out_kfree;
  299. }
  300. ap->private_data = pp;
  301. /* fix up PHYMODE4 align timing */
  302. if ((hp->flags & PDC_FLAG_GEN_II) && sata_scr_valid(ap)) {
  303. void __iomem *mmio = (void __iomem *) ap->ioaddr.scr_addr;
  304. unsigned int tmp;
  305. tmp = readl(mmio + 0x014);
  306. tmp = (tmp & ~3) | 1; /* set bits 1:0 = 0:1 */
  307. writel(tmp, mmio + 0x014);
  308. }
  309. return 0;
  310. err_out_kfree:
  311. kfree(pp);
  312. err_out:
  313. ata_port_stop(ap);
  314. return rc;
  315. }
  316. static void pdc_port_stop(struct ata_port *ap)
  317. {
  318. struct device *dev = ap->host->dev;
  319. struct pdc_port_priv *pp = ap->private_data;
  320. ap->private_data = NULL;
  321. dma_free_coherent(dev, 128, pp->pkt, pp->pkt_dma);
  322. kfree(pp);
  323. ata_port_stop(ap);
  324. }
  325. static void pdc_host_stop(struct ata_host *host)
  326. {
  327. struct pdc_host_priv *hp = host->private_data;
  328. ata_pci_host_stop(host);
  329. kfree(hp);
  330. }
  331. static void pdc_reset_port(struct ata_port *ap)
  332. {
  333. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr + PDC_CTLSTAT;
  334. unsigned int i;
  335. u32 tmp;
  336. for (i = 11; i > 0; i--) {
  337. tmp = readl(mmio);
  338. if (tmp & PDC_RESET)
  339. break;
  340. udelay(100);
  341. tmp |= PDC_RESET;
  342. writel(tmp, mmio);
  343. }
  344. tmp &= ~PDC_RESET;
  345. writel(tmp, mmio);
  346. readl(mmio); /* flush */
  347. }
  348. static void pdc_pata_cbl_detect(struct ata_port *ap)
  349. {
  350. u8 tmp;
  351. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr + PDC_CTLSTAT + 0x03;
  352. tmp = readb(mmio);
  353. if (tmp & 0x01) {
  354. ap->cbl = ATA_CBL_PATA40;
  355. ap->udma_mask &= ATA_UDMA_MASK_40C;
  356. } else
  357. ap->cbl = ATA_CBL_PATA80;
  358. }
  359. static void pdc_pata_phy_reset(struct ata_port *ap)
  360. {
  361. pdc_pata_cbl_detect(ap);
  362. pdc_reset_port(ap);
  363. ata_port_probe(ap);
  364. ata_bus_reset(ap);
  365. }
  366. static u32 pdc_sata_scr_read (struct ata_port *ap, unsigned int sc_reg)
  367. {
  368. if (sc_reg > SCR_CONTROL || ap->cbl != ATA_CBL_SATA)
  369. return 0xffffffffU;
  370. return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  371. }
  372. static void pdc_sata_scr_write (struct ata_port *ap, unsigned int sc_reg,
  373. u32 val)
  374. {
  375. if (sc_reg > SCR_CONTROL || ap->cbl != ATA_CBL_SATA)
  376. return;
  377. writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  378. }
  379. static void pdc_atapi_pkt(struct ata_queued_cmd *qc)
  380. {
  381. struct ata_port *ap = qc->ap;
  382. dma_addr_t sg_table = ap->prd_dma;
  383. unsigned int cdb_len = qc->dev->cdb_len;
  384. u8 *cdb = qc->cdb;
  385. struct pdc_port_priv *pp = ap->private_data;
  386. u8 *buf = pp->pkt;
  387. u32 *buf32 = (u32 *) buf;
  388. unsigned int dev_sel, feature, nbytes;
  389. /* set control bits (byte 0), zero delay seq id (byte 3),
  390. * and seq id (byte 2)
  391. */
  392. switch (qc->tf.protocol) {
  393. case ATA_PROT_ATAPI_DMA:
  394. if (!(qc->tf.flags & ATA_TFLAG_WRITE))
  395. buf32[0] = cpu_to_le32(PDC_PKT_READ);
  396. else
  397. buf32[0] = 0;
  398. break;
  399. case ATA_PROT_ATAPI_NODATA:
  400. buf32[0] = cpu_to_le32(PDC_PKT_NODATA);
  401. break;
  402. default:
  403. BUG();
  404. break;
  405. }
  406. buf32[1] = cpu_to_le32(sg_table); /* S/G table addr */
  407. buf32[2] = 0; /* no next-packet */
  408. /* select drive */
  409. if (sata_scr_valid(ap)) {
  410. dev_sel = PDC_DEVICE_SATA;
  411. } else {
  412. dev_sel = ATA_DEVICE_OBS;
  413. if (qc->dev->devno != 0)
  414. dev_sel |= ATA_DEV1;
  415. }
  416. buf[12] = (1 << 5) | ATA_REG_DEVICE;
  417. buf[13] = dev_sel;
  418. buf[14] = (1 << 5) | ATA_REG_DEVICE | PDC_PKT_CLEAR_BSY;
  419. buf[15] = dev_sel; /* once more, waiting for BSY to clear */
  420. buf[16] = (1 << 5) | ATA_REG_NSECT;
  421. buf[17] = 0x00;
  422. buf[18] = (1 << 5) | ATA_REG_LBAL;
  423. buf[19] = 0x00;
  424. /* set feature and byte counter registers */
  425. if (qc->tf.protocol != ATA_PROT_ATAPI_DMA) {
  426. feature = PDC_FEATURE_ATAPI_PIO;
  427. /* set byte counter register to real transfer byte count */
  428. nbytes = qc->nbytes;
  429. if (!nbytes)
  430. nbytes = qc->nsect << 9;
  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 = (void __iomem *) 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 = (void __iomem *) 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_error_handler(struct ata_port *ap)
  510. {
  511. ata_reset_fn_t hardreset;
  512. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  513. pdc_reset_port(ap);
  514. hardreset = NULL;
  515. if (sata_scr_valid(ap))
  516. hardreset = sata_std_hardreset;
  517. /* perform recovery */
  518. ata_do_eh(ap, ata_std_prereset, ata_std_softreset, hardreset,
  519. ata_std_postreset);
  520. }
  521. static void pdc_post_internal_cmd(struct ata_queued_cmd *qc)
  522. {
  523. struct ata_port *ap = qc->ap;
  524. if (qc->flags & ATA_QCFLAG_FAILED)
  525. qc->err_mask |= AC_ERR_OTHER;
  526. /* make DMA engine forget about the failed command */
  527. if (qc->err_mask)
  528. pdc_reset_port(ap);
  529. }
  530. static void pdc_eng_timeout(struct ata_port *ap)
  531. {
  532. struct ata_host *host = ap->host;
  533. u8 drv_stat;
  534. struct ata_queued_cmd *qc;
  535. unsigned long flags;
  536. DPRINTK("ENTER\n");
  537. spin_lock_irqsave(&host->lock, flags);
  538. qc = ata_qc_from_tag(ap, ap->active_tag);
  539. switch (qc->tf.protocol) {
  540. case ATA_PROT_DMA:
  541. case ATA_PROT_NODATA:
  542. ata_port_printk(ap, KERN_ERR, "command timeout\n");
  543. drv_stat = ata_wait_idle(ap);
  544. qc->err_mask |= __ac_err_mask(drv_stat);
  545. break;
  546. default:
  547. drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 1000);
  548. ata_port_printk(ap, KERN_ERR,
  549. "unknown timeout, cmd 0x%x stat 0x%x\n",
  550. qc->tf.command, drv_stat);
  551. qc->err_mask |= ac_err_mask(drv_stat);
  552. break;
  553. }
  554. spin_unlock_irqrestore(&host->lock, flags);
  555. ata_eh_qc_complete(qc);
  556. DPRINTK("EXIT\n");
  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. u32 tmp;
  563. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr + PDC_GLOBAL_CTL;
  564. tmp = readl(mmio);
  565. if (tmp & PDC_ERR_MASK) {
  566. qc->err_mask |= AC_ERR_DEV;
  567. pdc_reset_port(ap);
  568. }
  569. switch (qc->tf.protocol) {
  570. case ATA_PROT_DMA:
  571. case ATA_PROT_NODATA:
  572. case ATA_PROT_ATAPI_DMA:
  573. case ATA_PROT_ATAPI_NODATA:
  574. qc->err_mask |= ac_err_mask(ata_wait_idle(ap));
  575. ata_qc_complete(qc);
  576. handled = 1;
  577. break;
  578. default:
  579. ap->stats.idle_irq++;
  580. break;
  581. }
  582. return handled;
  583. }
  584. static void pdc_irq_clear(struct ata_port *ap)
  585. {
  586. struct ata_host *host = ap->host;
  587. void __iomem *mmio = host->mmio_base;
  588. readl(mmio + PDC_INT_SEQMASK);
  589. }
  590. static irqreturn_t pdc_interrupt (int irq, void *dev_instance)
  591. {
  592. struct ata_host *host = dev_instance;
  593. struct ata_port *ap;
  594. u32 mask = 0;
  595. unsigned int i, tmp;
  596. unsigned int handled = 0;
  597. void __iomem *mmio_base;
  598. VPRINTK("ENTER\n");
  599. if (!host || !host->mmio_base) {
  600. VPRINTK("QUICK EXIT\n");
  601. return IRQ_NONE;
  602. }
  603. mmio_base = host->mmio_base;
  604. /* reading should also clear interrupts */
  605. mask = readl(mmio_base + PDC_INT_SEQMASK);
  606. if (mask == 0xffffffff) {
  607. VPRINTK("QUICK EXIT 2\n");
  608. return IRQ_NONE;
  609. }
  610. spin_lock(&host->lock);
  611. mask &= 0xffff; /* only 16 tags possible */
  612. if (!mask) {
  613. VPRINTK("QUICK EXIT 3\n");
  614. goto done_irq;
  615. }
  616. writel(mask, mmio_base + PDC_INT_SEQMASK);
  617. for (i = 0; i < host->n_ports; i++) {
  618. VPRINTK("port %u\n", i);
  619. ap = host->ports[i];
  620. tmp = mask & (1 << (i + 1));
  621. if (tmp && ap &&
  622. !(ap->flags & ATA_FLAG_DISABLED)) {
  623. struct ata_queued_cmd *qc;
  624. qc = ata_qc_from_tag(ap, ap->active_tag);
  625. if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)))
  626. handled += pdc_host_intr(ap, qc);
  627. }
  628. }
  629. VPRINTK("EXIT\n");
  630. done_irq:
  631. spin_unlock(&host->lock);
  632. return IRQ_RETVAL(handled);
  633. }
  634. static inline void pdc_packet_start(struct ata_queued_cmd *qc)
  635. {
  636. struct ata_port *ap = qc->ap;
  637. struct pdc_port_priv *pp = ap->private_data;
  638. unsigned int port_no = ap->port_no;
  639. u8 seq = (u8) (port_no + 1);
  640. VPRINTK("ENTER, ap %p\n", ap);
  641. writel(0x00000001, ap->host->mmio_base + (seq * 4));
  642. readl(ap->host->mmio_base + (seq * 4)); /* flush */
  643. pp->pkt[2] = seq;
  644. wmb(); /* flush PRD, pkt writes */
  645. writel(pp->pkt_dma, (void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT);
  646. readl((void __iomem *) ap->ioaddr.cmd_addr + PDC_PKT_SUBMIT); /* flush */
  647. }
  648. static unsigned int pdc_qc_issue_prot(struct ata_queued_cmd *qc)
  649. {
  650. switch (qc->tf.protocol) {
  651. case ATA_PROT_ATAPI_NODATA:
  652. if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
  653. break;
  654. /*FALLTHROUGH*/
  655. case ATA_PROT_ATAPI_DMA:
  656. case ATA_PROT_DMA:
  657. case ATA_PROT_NODATA:
  658. pdc_packet_start(qc);
  659. return 0;
  660. default:
  661. break;
  662. }
  663. return ata_qc_issue_prot(qc);
  664. }
  665. static void pdc_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  666. {
  667. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  668. tf->protocol == ATA_PROT_NODATA);
  669. ata_tf_load(ap, tf);
  670. }
  671. static void pdc_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
  672. {
  673. WARN_ON (tf->protocol == ATA_PROT_DMA ||
  674. tf->protocol == ATA_PROT_NODATA);
  675. ata_exec_command(ap, tf);
  676. }
  677. static int pdc_check_atapi_dma(struct ata_queued_cmd *qc)
  678. {
  679. u8 *scsicmd = qc->scsicmd->cmnd;
  680. int pio = 1; /* atapi dma off by default */
  681. /* Whitelist commands that may use DMA. */
  682. switch (scsicmd[0]) {
  683. case WRITE_12:
  684. case WRITE_10:
  685. case WRITE_6:
  686. case READ_12:
  687. case READ_10:
  688. case READ_6:
  689. case 0xad: /* READ_DVD_STRUCTURE */
  690. case 0xbe: /* READ_CD */
  691. pio = 0;
  692. }
  693. /* -45150 (FFFF4FA2) to -1 (FFFFFFFF) shall use PIO mode */
  694. if (scsicmd[0] == WRITE_10) {
  695. unsigned int lba;
  696. lba = (scsicmd[2] << 24) | (scsicmd[3] << 16) | (scsicmd[4] << 8) | scsicmd[5];
  697. if (lba >= 0xFFFF4FA2)
  698. pio = 1;
  699. }
  700. return pio;
  701. }
  702. static int pdc_old_check_atapi_dma(struct ata_queued_cmd *qc)
  703. {
  704. struct ata_port *ap = qc->ap;
  705. /* First generation chips cannot use ATAPI DMA on SATA ports */
  706. if (sata_scr_valid(ap))
  707. return 1;
  708. return pdc_check_atapi_dma(qc);
  709. }
  710. static void pdc_ata_setup_port(struct ata_ioports *port, unsigned long base)
  711. {
  712. port->cmd_addr = base;
  713. port->data_addr = base;
  714. port->feature_addr =
  715. port->error_addr = base + 0x4;
  716. port->nsect_addr = base + 0x8;
  717. port->lbal_addr = base + 0xc;
  718. port->lbam_addr = base + 0x10;
  719. port->lbah_addr = base + 0x14;
  720. port->device_addr = base + 0x18;
  721. port->command_addr =
  722. port->status_addr = base + 0x1c;
  723. port->altstatus_addr =
  724. port->ctl_addr = base + 0x38;
  725. }
  726. static void pdc_host_init(unsigned int chip_id, struct ata_probe_ent *pe)
  727. {
  728. void __iomem *mmio = pe->mmio_base;
  729. struct pdc_host_priv *hp = pe->private_data;
  730. int hotplug_offset;
  731. u32 tmp;
  732. if (hp->flags & PDC_FLAG_GEN_II)
  733. hotplug_offset = PDC2_SATA_PLUG_CSR;
  734. else
  735. hotplug_offset = PDC_SATA_PLUG_CSR;
  736. /*
  737. * Except for the hotplug stuff, this is voodoo from the
  738. * Promise driver. Label this entire section
  739. * "TODO: figure out why we do this"
  740. */
  741. /* enable BMR_BURST, maybe change FIFO_SHD to 8 dwords */
  742. tmp = readl(mmio + PDC_FLASH_CTL);
  743. tmp |= 0x02000; /* bit 13 (enable bmr burst) */
  744. if (!(hp->flags & PDC_FLAG_GEN_II))
  745. tmp |= 0x10000; /* bit 16 (fifo threshold at 8 dw) */
  746. writel(tmp, mmio + PDC_FLASH_CTL);
  747. /* clear plug/unplug flags for all ports */
  748. tmp = readl(mmio + hotplug_offset);
  749. writel(tmp | 0xff, mmio + hotplug_offset);
  750. /* mask plug/unplug ints */
  751. tmp = readl(mmio + hotplug_offset);
  752. writel(tmp | 0xff0000, mmio + hotplug_offset);
  753. /* don't initialise TBG or SLEW on 2nd generation chips */
  754. if (hp->flags & PDC_FLAG_GEN_II)
  755. return;
  756. /* reduce TBG clock to 133 Mhz. */
  757. tmp = readl(mmio + PDC_TBG_MODE);
  758. tmp &= ~0x30000; /* clear bit 17, 16*/
  759. tmp |= 0x10000; /* set bit 17:16 = 0:1 */
  760. writel(tmp, mmio + PDC_TBG_MODE);
  761. readl(mmio + PDC_TBG_MODE); /* flush */
  762. msleep(10);
  763. /* adjust slew rate control register. */
  764. tmp = readl(mmio + PDC_SLEW_CTL);
  765. tmp &= 0xFFFFF03F; /* clear bit 11 ~ 6 */
  766. tmp |= 0x00000900; /* set bit 11-9 = 100b , bit 8-6 = 100 */
  767. writel(tmp, mmio + PDC_SLEW_CTL);
  768. }
  769. static int pdc_ata_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  770. {
  771. static int printed_version;
  772. struct ata_probe_ent *probe_ent = NULL;
  773. struct pdc_host_priv *hp;
  774. unsigned long base;
  775. void __iomem *mmio_base;
  776. unsigned int board_idx = (unsigned int) ent->driver_data;
  777. int pci_dev_busy = 0;
  778. int rc;
  779. u8 tmp;
  780. if (!printed_version++)
  781. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  782. rc = pci_enable_device(pdev);
  783. if (rc)
  784. return rc;
  785. rc = pci_request_regions(pdev, DRV_NAME);
  786. if (rc) {
  787. pci_dev_busy = 1;
  788. goto err_out;
  789. }
  790. rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
  791. if (rc)
  792. goto err_out_regions;
  793. rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
  794. if (rc)
  795. goto err_out_regions;
  796. probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
  797. if (probe_ent == NULL) {
  798. rc = -ENOMEM;
  799. goto err_out_regions;
  800. }
  801. probe_ent->dev = pci_dev_to_dev(pdev);
  802. INIT_LIST_HEAD(&probe_ent->node);
  803. mmio_base = pci_iomap(pdev, 3, 0);
  804. if (mmio_base == NULL) {
  805. rc = -ENOMEM;
  806. goto err_out_free_ent;
  807. }
  808. base = (unsigned long) mmio_base;
  809. hp = kzalloc(sizeof(*hp), GFP_KERNEL);
  810. if (hp == NULL) {
  811. rc = -ENOMEM;
  812. goto err_out_free_ent;
  813. }
  814. probe_ent->private_data = hp;
  815. probe_ent->sht = pdc_port_info[board_idx].sht;
  816. probe_ent->port_flags = pdc_port_info[board_idx].flags;
  817. probe_ent->pio_mask = pdc_port_info[board_idx].pio_mask;
  818. probe_ent->mwdma_mask = pdc_port_info[board_idx].mwdma_mask;
  819. probe_ent->udma_mask = pdc_port_info[board_idx].udma_mask;
  820. probe_ent->port_ops = pdc_port_info[board_idx].port_ops;
  821. probe_ent->irq = pdev->irq;
  822. probe_ent->irq_flags = IRQF_SHARED;
  823. probe_ent->mmio_base = mmio_base;
  824. pdc_ata_setup_port(&probe_ent->port[0], base + 0x200);
  825. pdc_ata_setup_port(&probe_ent->port[1], base + 0x280);
  826. probe_ent->port[0].scr_addr = base + 0x400;
  827. probe_ent->port[1].scr_addr = base + 0x500;
  828. /* notice 4-port boards */
  829. switch (board_idx) {
  830. case board_40518:
  831. hp->flags |= PDC_FLAG_GEN_II;
  832. /* Fall through */
  833. case board_20319:
  834. probe_ent->n_ports = 4;
  835. pdc_ata_setup_port(&probe_ent->port[2], base + 0x300);
  836. pdc_ata_setup_port(&probe_ent->port[3], base + 0x380);
  837. probe_ent->port[2].scr_addr = base + 0x600;
  838. probe_ent->port[3].scr_addr = base + 0x700;
  839. break;
  840. case board_2057x:
  841. hp->flags |= PDC_FLAG_GEN_II;
  842. /* Fall through */
  843. case board_2037x:
  844. /* TX2plus boards also have a PATA port */
  845. tmp = readb(mmio_base + PDC_FLASH_CTL+1);
  846. if (!(tmp & 0x80)) {
  847. probe_ent->n_ports = 3;
  848. pdc_ata_setup_port(&probe_ent->port[2], base + 0x300);
  849. hp->port_flags[2] = ATA_FLAG_SLAVE_POSS;
  850. printk(KERN_INFO DRV_NAME " PATA port found\n");
  851. } else
  852. probe_ent->n_ports = 2;
  853. hp->port_flags[0] = ATA_FLAG_SATA;
  854. hp->port_flags[1] = ATA_FLAG_SATA;
  855. break;
  856. case board_20619:
  857. probe_ent->n_ports = 4;
  858. pdc_ata_setup_port(&probe_ent->port[2], base + 0x300);
  859. pdc_ata_setup_port(&probe_ent->port[3], base + 0x380);
  860. probe_ent->port[2].scr_addr = base + 0x600;
  861. probe_ent->port[3].scr_addr = base + 0x700;
  862. break;
  863. default:
  864. BUG();
  865. break;
  866. }
  867. pci_set_master(pdev);
  868. /* initialize adapter */
  869. pdc_host_init(board_idx, probe_ent);
  870. /* FIXME: Need any other frees than hp? */
  871. if (!ata_device_add(probe_ent))
  872. kfree(hp);
  873. kfree(probe_ent);
  874. return 0;
  875. err_out_free_ent:
  876. kfree(probe_ent);
  877. err_out_regions:
  878. pci_release_regions(pdev);
  879. err_out:
  880. if (!pci_dev_busy)
  881. pci_disable_device(pdev);
  882. return rc;
  883. }
  884. static int __init pdc_ata_init(void)
  885. {
  886. return pci_register_driver(&pdc_ata_pci_driver);
  887. }
  888. static void __exit pdc_ata_exit(void)
  889. {
  890. pci_unregister_driver(&pdc_ata_pci_driver);
  891. }
  892. MODULE_AUTHOR("Jeff Garzik");
  893. MODULE_DESCRIPTION("Promise ATA TX2/TX4/TX4000 low-level driver");
  894. MODULE_LICENSE("GPL");
  895. MODULE_DEVICE_TABLE(pci, pdc_ata_pci_tbl);
  896. MODULE_VERSION(DRV_VERSION);
  897. module_init(pdc_ata_init);
  898. module_exit(pdc_ata_exit);