sata_promise.c 27 KB

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