sata_promise.c 28 KB

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